Jack Palevich | ae54f1f | 2009-05-08 14:54:15 -0700 | [diff] [blame] | 1 | /* |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 2 | * Android "Almost" C Compiler. |
| 3 | * This is a compiler for a small subset of the C language, intended for use |
| 4 | * in scripting environments where speed and memory footprint are important. |
| 5 | * |
| 6 | * This code is based upon the "unobfuscated" version of the |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 7 | * Obfuscated Tiny C compiler, see the file LICENSE for details. |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 8 | * |
| 9 | */ |
| 10 | |
Jack Palevich | 77ae76e | 2009-05-10 19:59:24 -0700 | [diff] [blame] | 11 | #include <ctype.h> |
| 12 | #include <dlfcn.h> |
Jack Palevich | 8dc662e | 2009-06-09 22:53:47 +0000 | [diff] [blame] | 13 | #include <errno.h> |
Jack Palevich | e27bf3e | 2009-05-10 14:09:03 -0700 | [diff] [blame] | 14 | #include <stdarg.h> |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 15 | #include <stdint.h> |
Jack Palevich | ae54f1f | 2009-05-08 14:54:15 -0700 | [diff] [blame] | 16 | #include <stdio.h> |
Jack Palevich | f6b5a53 | 2009-05-10 19:16:42 -0700 | [diff] [blame] | 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 19 | #include <cutils/hashmap.h> |
Jack Palevich | ae54f1f | 2009-05-08 14:54:15 -0700 | [diff] [blame] | 20 | |
Jack Palevich | 8dc662e | 2009-06-09 22:53:47 +0000 | [diff] [blame] | 21 | #if defined(__i386__) |
| 22 | #include <sys/mman.h> |
| 23 | #endif |
| 24 | |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 25 | #if defined(__arm__) |
| 26 | #include <unistd.h> |
| 27 | #endif |
| 28 | |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 29 | #if defined(__arm__) |
| 30 | #define DEFAULT_ARM_CODEGEN |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 31 | #define PROVIDE_ARM_CODEGEN |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 32 | #elif defined(__i386__) |
| 33 | #define DEFAULT_X86_CODEGEN |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 34 | #define PROVIDE_X86_CODEGEN |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 35 | #elif defined(__x86_64__) |
| 36 | #define DEFAULT_X64_CODEGEN |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 37 | #define PROVIDE_X64_CODEGEN |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 38 | #endif |
| 39 | |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 40 | |
| 41 | #ifdef PROVIDE_ARM_CODEGEN |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 42 | #include "disassem.h" |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 43 | #endif |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 44 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 45 | #include <acc/acc.h> |
| 46 | |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 47 | #define LOG_API(...) do {} while(0) |
| 48 | // #define LOG_API(...) fprintf (stderr, __VA_ARGS__) |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 49 | |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 50 | #define LOG_STACK(...) do {} while(0) |
| 51 | // #define LOG_STACK(...) fprintf (stderr, __VA_ARGS__) |
| 52 | |
| 53 | // #define ENABLE_ARM_DISASSEMBLY |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 54 | // #define PROVIDE_TRACE_CODEGEN |
| 55 | |
Jack Palevich | bbf8ab5 | 2009-05-11 11:54:30 -0700 | [diff] [blame] | 56 | namespace acc { |
| 57 | |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 58 | class ErrorSink { |
| 59 | public: |
| 60 | void error(const char *fmt, ...) { |
| 61 | va_list ap; |
| 62 | va_start(ap, fmt); |
| 63 | verror(fmt, ap); |
| 64 | va_end(ap); |
| 65 | } |
| 66 | |
| 67 | virtual void verror(const char* fmt, va_list ap) = 0; |
| 68 | }; |
| 69 | |
| 70 | class Compiler : public ErrorSink { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 71 | class CodeBuf { |
Jack Palevich | 653f42d | 2009-05-28 17:15:32 -0700 | [diff] [blame] | 72 | char* ind; // Output code pointer |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 73 | char* pProgramBase; |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 74 | ErrorSink* mErrorSink; |
| 75 | int mSize; |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 76 | bool mOverflowed; |
Jack Palevich | f0cbc92 | 2009-05-08 16:35:13 -0700 | [diff] [blame] | 77 | |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 78 | void release() { |
| 79 | if (pProgramBase != 0) { |
| 80 | free(pProgramBase); |
| 81 | pProgramBase = 0; |
Jack Palevich | ae54f1f | 2009-05-08 14:54:15 -0700 | [diff] [blame] | 82 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 85 | bool check(int n) { |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 86 | int newSize = ind - pProgramBase + n; |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 87 | bool overflow = newSize > mSize; |
| 88 | if (overflow && !mOverflowed) { |
| 89 | mOverflowed = true; |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 90 | if (mErrorSink) { |
| 91 | mErrorSink->error("Code too large: %d bytes", newSize); |
| 92 | } |
| 93 | } |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 94 | return overflow; |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 97 | public: |
| 98 | CodeBuf() { |
| 99 | pProgramBase = 0; |
| 100 | ind = 0; |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 101 | mErrorSink = 0; |
| 102 | mSize = 0; |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 103 | mOverflowed = false; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | ~CodeBuf() { |
| 107 | release(); |
| 108 | } |
| 109 | |
| 110 | void init(int size) { |
| 111 | release(); |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 112 | mSize = size; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 113 | pProgramBase = (char*) calloc(1, size); |
| 114 | ind = pProgramBase; |
| 115 | } |
| 116 | |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 117 | void setErrorSink(ErrorSink* pErrorSink) { |
| 118 | mErrorSink = pErrorSink; |
| 119 | } |
| 120 | |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 121 | int o4(int n) { |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 122 | if(check(4)) { |
| 123 | return 0; |
| 124 | } |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 125 | intptr_t result = (intptr_t) ind; |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 126 | * (int*) ind = n; |
| 127 | ind += 4; |
| 128 | return result; |
| 129 | } |
| 130 | |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 131 | /* |
| 132 | * Output a byte. Handles all values, 0..ff. |
| 133 | */ |
| 134 | void ob(int n) { |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 135 | if(check(1)) { |
| 136 | return; |
| 137 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 138 | *ind++ = n; |
| 139 | } |
| 140 | |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 141 | inline void* getBase() { |
| 142 | return (void*) pProgramBase; |
| 143 | } |
| 144 | |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 145 | intptr_t getSize() { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 146 | return ind - pProgramBase; |
| 147 | } |
| 148 | |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 149 | intptr_t getPC() { |
| 150 | return (intptr_t) ind; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 151 | } |
| 152 | }; |
| 153 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 154 | /** |
| 155 | * A code generator creates an in-memory program, generating the code on |
| 156 | * the fly. There is one code generator implementation for each supported |
| 157 | * architecture. |
| 158 | * |
| 159 | * The code generator implements the following abstract machine: |
| 160 | * R0 - the main accumulator. |
| 161 | * R1 - the secondary accumulator. |
| 162 | * FP - a frame pointer for accessing function arguments and local |
| 163 | * variables. |
| 164 | * SP - a stack pointer for storing intermediate results while evaluating |
| 165 | * expressions. The stack pointer grows downwards. |
| 166 | * |
| 167 | * The function calling convention is that all arguments are placed on the |
| 168 | * stack such that the first argument has the lowest address. |
| 169 | * After the call, the result is in R0. The caller is responsible for |
| 170 | * removing the arguments from the stack. |
| 171 | * The R0 and R1 registers are not saved across function calls. The |
| 172 | * FP and SP registers are saved. |
| 173 | */ |
| 174 | |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 175 | class CodeGenerator { |
| 176 | public: |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 177 | CodeGenerator() { |
| 178 | mErrorSink = 0; |
| 179 | pCodeBuf = 0; |
| 180 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 181 | virtual ~CodeGenerator() {} |
| 182 | |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 183 | virtual void init(CodeBuf* pCodeBuf) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 184 | this->pCodeBuf = pCodeBuf; |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 185 | pCodeBuf->setErrorSink(mErrorSink); |
| 186 | } |
| 187 | |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 188 | virtual void setErrorSink(ErrorSink* pErrorSink) { |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 189 | mErrorSink = pErrorSink; |
| 190 | if (pCodeBuf) { |
| 191 | pCodeBuf->setErrorSink(mErrorSink); |
| 192 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 195 | /* Emit a function prolog. |
| 196 | * argCount is the number of arguments. |
| 197 | * Save the old value of the FP. |
| 198 | * Set the new value of the FP. |
| 199 | * Convert from the native platform calling convention to |
| 200 | * our stack-based calling convention. This may require |
| 201 | * pushing arguments from registers to the stack. |
| 202 | * Allocate "N" bytes of stack space. N isn't known yet, so |
| 203 | * just emit the instructions for adjusting the stack, and return |
| 204 | * the address to patch up. The patching will be done in |
| 205 | * functionExit(). |
| 206 | * returns address to patch with local variable size. |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 207 | */ |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 208 | virtual int functionEntry(int argCount) = 0; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 209 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 210 | /* Emit a function epilog. |
| 211 | * Restore the old SP and FP register values. |
| 212 | * Return to the calling function. |
| 213 | * argCount - the number of arguments to the function. |
| 214 | * localVariableAddress - returned from functionEntry() |
| 215 | * localVariableSize - the size in bytes of the local variables. |
| 216 | */ |
| 217 | virtual void functionExit(int argCount, int localVariableAddress, |
| 218 | int localVariableSize) = 0; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 219 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 220 | /* load immediate value to R0 */ |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 221 | virtual void li(int t) = 0; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 222 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 223 | /* Jump to a target, and return the address of the word that |
| 224 | * holds the target data, in case it needs to be fixed up later. |
| 225 | */ |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 226 | virtual int gjmp(int t) = 0; |
| 227 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 228 | /* Test R0 and jump to a target if the test succeeds. |
| 229 | * l = 0: je, l == 1: jne |
| 230 | * Return the address of the word that holds the targed data, in |
| 231 | * case it needs to be fixed up later. |
| 232 | */ |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 233 | virtual int gtst(bool l, int t) = 0; |
| 234 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 235 | /* Compare R1 against R0, and store the boolean result in R0. |
| 236 | * op specifies the comparison. |
| 237 | */ |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 238 | virtual void gcmp(int op) = 0; |
| 239 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 240 | /* Perform the arithmetic op specified by op. R1 is the |
| 241 | * left argument, R0 is the right argument. |
| 242 | */ |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 243 | virtual void genOp(int op) = 0; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 244 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 245 | /* Set R1 to 0. |
| 246 | */ |
| 247 | virtual void clearR1() = 0; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 248 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 249 | /* Push R0 onto the stack. |
| 250 | */ |
| 251 | virtual void pushR0() = 0; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 252 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 253 | /* Pop R1 off of the stack. |
| 254 | */ |
| 255 | virtual void popR1() = 0; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 256 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 257 | /* Store R0 to the address stored in R1. |
| 258 | * isInt is true if a whole 4-byte integer value |
| 259 | * should be stored, otherwise a 1-byte character |
| 260 | * value should be stored. |
| 261 | */ |
| 262 | virtual void storeR0ToR1(bool isInt) = 0; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 263 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 264 | /* Load R0 from the address stored in R0. |
| 265 | * isInt is true if a whole 4-byte integer value |
| 266 | * should be loaded, otherwise a 1-byte character |
| 267 | * value should be loaded. |
| 268 | */ |
| 269 | virtual void loadR0FromR0(bool isInt) = 0; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 270 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 271 | /* Load the absolute address of a variable to R0. |
| 272 | * If ea <= LOCAL, then this is a local variable, or an |
| 273 | * argument, addressed relative to FP. |
| 274 | * else it is an absolute global address. |
| 275 | */ |
| 276 | virtual void leaR0(int ea) = 0; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 277 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 278 | /* Store R0 to a variable. |
| 279 | * If ea <= LOCAL, then this is a local variable, or an |
| 280 | * argument, addressed relative to FP. |
| 281 | * else it is an absolute global address. |
| 282 | */ |
| 283 | virtual void storeR0(int ea) = 0; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 284 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 285 | /* load R0 from a variable. |
| 286 | * If ea <= LOCAL, then this is a local variable, or an |
| 287 | * argument, addressed relative to FP. |
| 288 | * else it is an absolute global address. |
| 289 | * If isIncDec is true, then the stored variable's value |
| 290 | * should be post-incremented or post-decremented, based |
| 291 | * on the value of op. |
| 292 | */ |
| 293 | virtual void loadR0(int ea, bool isIncDec, int op) = 0; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 294 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 295 | /* Emit code to adjust the stack for a function call. Return the |
| 296 | * label for the address of the instruction that adjusts the |
| 297 | * stack size. This will be passed as argument "a" to |
| 298 | * endFunctionCallArguments. |
| 299 | */ |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 300 | virtual int beginFunctionCallArguments() = 0; |
| 301 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 302 | /* Emit code to store R0 to the stack at byte offset l. |
| 303 | */ |
| 304 | virtual void storeR0ToArg(int l) = 0; |
Jack Palevich | 7810bc9 | 2009-05-15 14:31:47 -0700 | [diff] [blame] | 305 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 306 | /* Patch the function call preamble. |
| 307 | * a is the address returned from beginFunctionCallArguments |
| 308 | * l is the number of bytes the arguments took on the stack. |
| 309 | * Typically you would also emit code to convert the argument |
| 310 | * list into whatever the native function calling convention is. |
| 311 | * On ARM for example you would pop the first 5 arguments into |
| 312 | * R0..R4 |
| 313 | */ |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 314 | virtual void endFunctionCallArguments(int a, int l) = 0; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 315 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 316 | /* Emit a call to an unknown function. The argument "symbol" needs to |
| 317 | * be stored in the location where the address should go. It forms |
| 318 | * a chain. The address will be patched later. |
| 319 | * Return the address of the word that has to be patched. |
| 320 | */ |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 321 | virtual int callForward(int symbol) = 0; |
| 322 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 323 | /* Call a function using PC-relative addressing. t is the PC-relative |
| 324 | * address of the function. It has already been adjusted for the |
| 325 | * architectural jump offset, so just store it as-is. |
| 326 | */ |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 327 | virtual void callRelative(int t) = 0; |
| 328 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 329 | /* Call a function pointer. L is the number of bytes the arguments |
| 330 | * take on the stack. The address of the function is stored at |
| 331 | * location SP + l. |
| 332 | */ |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 333 | virtual void callIndirect(int l) = 0; |
| 334 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 335 | /* Adjust SP after returning from a function call. l is the |
| 336 | * number of bytes of arguments stored on the stack. isIndirect |
| 337 | * is true if this was an indirect call. (In which case the |
| 338 | * address of the function is stored at location SP + l.) |
| 339 | */ |
Jack Palevich | 7810bc9 | 2009-05-15 14:31:47 -0700 | [diff] [blame] | 340 | virtual void adjustStackAfterCall(int l, bool isIndirect) = 0; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 341 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 342 | /* Print a disassembly of the assembled code to out. Return |
| 343 | * non-zero if there is an error. |
| 344 | */ |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 345 | virtual int disassemble(FILE* out) = 0; |
| 346 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 347 | /* Generate a symbol at the current PC. t is the head of a |
| 348 | * linked list of addresses to patch. |
| 349 | */ |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 350 | virtual void gsym(int t) = 0; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 351 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 352 | /* |
| 353 | * Do any cleanup work required at the end of a compile. |
| 354 | * For example, an instruction cache might need to be |
| 355 | * invalidated. |
| 356 | * Return non-zero if there is an error. |
| 357 | */ |
| 358 | virtual int finishCompile() = 0; |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 359 | |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 360 | /** |
| 361 | * Adjust relative branches by this amount. |
| 362 | */ |
| 363 | virtual int jumpOffset() = 0; |
| 364 | |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 365 | protected: |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 366 | /* |
| 367 | * Output a byte. Handles all values, 0..ff. |
| 368 | */ |
| 369 | void ob(int n) { |
| 370 | pCodeBuf->ob(n); |
| 371 | } |
| 372 | |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 373 | intptr_t o4(int data) { |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 374 | return pCodeBuf->o4(data); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 377 | intptr_t getBase() { |
| 378 | return (intptr_t) pCodeBuf->getBase(); |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 379 | } |
| 380 | |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 381 | intptr_t getPC() { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 382 | return pCodeBuf->getPC(); |
| 383 | } |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 384 | |
| 385 | intptr_t getSize() { |
| 386 | return pCodeBuf->getSize(); |
| 387 | } |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 388 | |
| 389 | void error(const char* fmt,...) { |
| 390 | va_list ap; |
| 391 | va_start(ap, fmt); |
| 392 | mErrorSink->verror(fmt, ap); |
| 393 | va_end(ap); |
| 394 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 395 | private: |
| 396 | CodeBuf* pCodeBuf; |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 397 | ErrorSink* mErrorSink; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 398 | }; |
| 399 | |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 400 | #ifdef PROVIDE_ARM_CODEGEN |
| 401 | |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 402 | class ARMCodeGenerator : public CodeGenerator { |
| 403 | public: |
| 404 | ARMCodeGenerator() {} |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 405 | |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 406 | virtual ~ARMCodeGenerator() {} |
| 407 | |
| 408 | /* returns address to patch with local variable size |
| 409 | */ |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 410 | virtual int functionEntry(int argCount) { |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 411 | LOG_API("functionEntry(%d);\n", argCount); |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 412 | mStackUse = 0; |
Jack Palevich | 69796b6 | 2009-05-14 15:42:26 -0700 | [diff] [blame] | 413 | // sp -> arg4 arg5 ... |
| 414 | // Push our register-based arguments back on the stack |
| 415 | if (argCount > 0) { |
| 416 | int regArgCount = argCount <= 4 ? argCount : 4; |
| 417 | o4(0xE92D0000 | ((1 << argCount) - 1)); // stmfd sp!, {} |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 418 | mStackUse += regArgCount * 4; |
Jack Palevich | 69796b6 | 2009-05-14 15:42:26 -0700 | [diff] [blame] | 419 | } |
| 420 | // sp -> arg0 arg1 ... |
| 421 | o4(0xE92D4800); // stmfd sp!, {fp, lr} |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 422 | mStackUse += 2 * 4; |
Jack Palevich | 69796b6 | 2009-05-14 15:42:26 -0700 | [diff] [blame] | 423 | // sp, fp -> oldfp, retadr, arg0 arg1 .... |
| 424 | o4(0xE1A0B00D); // mov fp, sp |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 425 | LOG_STACK("functionEntry: %d\n", mStackUse); |
Jack Palevich | 69796b6 | 2009-05-14 15:42:26 -0700 | [diff] [blame] | 426 | return o4(0xE24DD000); // sub sp, sp, # <local variables> |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 427 | // We don't know how many local variables we are going to use, |
| 428 | // but we will round the allocation up to a multiple of |
| 429 | // STACK_ALIGNMENT, so it won't affect the stack alignment. |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 430 | } |
| 431 | |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 432 | virtual void functionExit(int argCount, int localVariableAddress, int localVariableSize) { |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 433 | LOG_API("functionExit(%d, %d, %d);\n", argCount, localVariableAddress, localVariableSize); |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 434 | // Round local variable size up to a multiple of stack alignment |
| 435 | localVariableSize = ((localVariableSize + STACK_ALIGNMENT - 1) / |
| 436 | STACK_ALIGNMENT) * STACK_ALIGNMENT; |
Jack Palevich | 69796b6 | 2009-05-14 15:42:26 -0700 | [diff] [blame] | 437 | // Patch local variable allocation code: |
| 438 | if (localVariableSize < 0 || localVariableSize > 255) { |
Jack Palevich | 8de461d | 2009-05-14 17:21:45 -0700 | [diff] [blame] | 439 | error("localVariables out of range: %d", localVariableSize); |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 440 | } |
Jack Palevich | 69796b6 | 2009-05-14 15:42:26 -0700 | [diff] [blame] | 441 | *(char*) (localVariableAddress) = localVariableSize; |
| 442 | |
| 443 | // sp -> locals .... fp -> oldfp, retadr, arg0, arg1, ... |
| 444 | o4(0xE1A0E00B); // mov lr, fp |
| 445 | o4(0xE59BB000); // ldr fp, [fp] |
| 446 | o4(0xE28ED004); // add sp, lr, #4 |
| 447 | // sp -> retadr, arg0, ... |
| 448 | o4(0xE8BD4000); // ldmfd sp!, {lr} |
| 449 | // sp -> arg0 .... |
| 450 | if (argCount > 0) { |
| 451 | // We store the PC into the lr so we can adjust the sp before |
Jack Palevich | 8de461d | 2009-05-14 17:21:45 -0700 | [diff] [blame] | 452 | // returning. We need to pull off the registers we pushed |
Jack Palevich | 69796b6 | 2009-05-14 15:42:26 -0700 | [diff] [blame] | 453 | // earlier. We don't need to actually store them anywhere, |
| 454 | // just adjust the stack. |
| 455 | int regArgCount = argCount <= 4 ? argCount : 4; |
| 456 | o4(0xE28DD000 | (regArgCount << 2)); // add sp, sp, #argCount << 2 |
| 457 | } |
| 458 | o4(0xE12FFF1E); // bx lr |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | /* load immediate value */ |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 462 | virtual void li(int t) { |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 463 | LOG_API("li(%d);\n", t); |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 464 | if (t >= 0 && t < 255) { |
Jack Palevich | 69796b6 | 2009-05-14 15:42:26 -0700 | [diff] [blame] | 465 | o4(0xE3A00000 + t); // mov r0, #0 |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 466 | } else if (t >= -256 && t < 0) { |
| 467 | // mvn means move constant ^ ~0 |
Jack Palevich | 69796b6 | 2009-05-14 15:42:26 -0700 | [diff] [blame] | 468 | o4(0xE3E00001 - t); // mvn r0, #0 |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 469 | } else { |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 470 | o4(0xE51F0000); // ldr r0, .L3 |
| 471 | o4(0xEA000000); // b .L99 |
| 472 | o4(t); // .L3: .word 0 |
| 473 | // .L99: |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 474 | } |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | virtual int gjmp(int t) { |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 478 | LOG_API("gjmp(%d);\n", t); |
Jack Palevich | 8de461d | 2009-05-14 17:21:45 -0700 | [diff] [blame] | 479 | return o4(0xEA000000 | encodeAddress(t)); // b .L33 |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | /* l = 0: je, l == 1: jne */ |
| 483 | virtual int gtst(bool l, int t) { |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 484 | LOG_API("gtst(%d, %d);\n", l, t); |
Jack Palevich | 8de461d | 2009-05-14 17:21:45 -0700 | [diff] [blame] | 485 | o4(0xE3500000); // cmp r0,#0 |
| 486 | int branch = l ? 0x1A000000 : 0x0A000000; // bne : beq |
| 487 | return o4(branch | encodeAddress(t)); |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | virtual void gcmp(int op) { |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 491 | LOG_API("gcmp(%d);\n", op); |
Jack Palevich | 8de461d | 2009-05-14 17:21:45 -0700 | [diff] [blame] | 492 | o4(0xE1510000); // cmp r1, r1 |
| 493 | switch(op) { |
| 494 | case OP_EQUALS: |
| 495 | o4(0x03A00001); // moveq r0,#1 |
| 496 | o4(0x13A00000); // movne r0,#0 |
| 497 | break; |
| 498 | case OP_NOT_EQUALS: |
| 499 | o4(0x03A00000); // moveq r0,#0 |
| 500 | o4(0x13A00001); // movne r0,#1 |
| 501 | break; |
| 502 | case OP_LESS_EQUAL: |
| 503 | o4(0xD3A00001); // movle r0,#1 |
| 504 | o4(0xC3A00000); // movgt r0,#0 |
| 505 | break; |
| 506 | case OP_GREATER: |
| 507 | o4(0xD3A00000); // movle r0,#0 |
| 508 | o4(0xC3A00001); // movgt r0,#1 |
| 509 | break; |
| 510 | case OP_GREATER_EQUAL: |
| 511 | o4(0xA3A00001); // movge r0,#1 |
| 512 | o4(0xB3A00000); // movlt r0,#0 |
| 513 | break; |
| 514 | case OP_LESS: |
| 515 | o4(0xA3A00000); // movge r0,#0 |
| 516 | o4(0xB3A00001); // movlt r0,#1 |
| 517 | break; |
| 518 | default: |
| 519 | error("Unknown comparison op %d", op); |
| 520 | break; |
| 521 | } |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 522 | } |
| 523 | |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 524 | virtual void genOp(int op) { |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 525 | LOG_API("genOp(%d);\n", op); |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 526 | switch(op) { |
| 527 | case OP_MUL: |
| 528 | o4(0x0E0000091); // mul r0,r1,r0 |
| 529 | break; |
Jack Palevich | 3d474a7 | 2009-05-15 15:12:38 -0700 | [diff] [blame] | 530 | case OP_DIV: |
| 531 | callRuntime(runtime_DIV); |
| 532 | break; |
| 533 | case OP_MOD: |
| 534 | callRuntime(runtime_MOD); |
| 535 | break; |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 536 | case OP_PLUS: |
| 537 | o4(0xE0810000); // add r0,r1,r0 |
| 538 | break; |
| 539 | case OP_MINUS: |
| 540 | o4(0xE0410000); // sub r0,r1,r0 |
| 541 | break; |
| 542 | case OP_SHIFT_LEFT: |
| 543 | o4(0xE1A00011); // lsl r0,r1,r0 |
| 544 | break; |
| 545 | case OP_SHIFT_RIGHT: |
| 546 | o4(0xE1A00051); // asr r0,r1,r0 |
| 547 | break; |
| 548 | case OP_BIT_AND: |
| 549 | o4(0xE0010000); // and r0,r1,r0 |
| 550 | break; |
| 551 | case OP_BIT_XOR: |
| 552 | o4(0xE0210000); // eor r0,r1,r0 |
| 553 | break; |
| 554 | case OP_BIT_OR: |
| 555 | o4(0xE1810000); // orr r0,r1,r0 |
| 556 | break; |
| 557 | case OP_BIT_NOT: |
| 558 | o4(0xE1E00000); // mvn r0, r0 |
| 559 | break; |
| 560 | default: |
Jack Palevich | 69796b6 | 2009-05-14 15:42:26 -0700 | [diff] [blame] | 561 | error("Unimplemented op %d\n", op); |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 562 | break; |
| 563 | } |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 564 | } |
| 565 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 566 | virtual void clearR1() { |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 567 | LOG_API("clearR1();\n"); |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 568 | o4(0xE3A01000); // mov r1, #0 |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 569 | } |
| 570 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 571 | virtual void pushR0() { |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 572 | LOG_API("pushR0();\n"); |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 573 | o4(0xE92D0001); // stmfd sp!,{r0} |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 574 | mStackUse += 4; |
| 575 | LOG_STACK("pushR0: %d\n", mStackUse); |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 576 | } |
| 577 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 578 | virtual void popR1() { |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 579 | LOG_API("popR1();\n"); |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 580 | o4(0xE8BD0002); // ldmfd sp!,{r1} |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 581 | mStackUse -= 4; |
| 582 | LOG_STACK("popR1: %d\n", mStackUse); |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 583 | } |
| 584 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 585 | virtual void storeR0ToR1(bool isInt) { |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 586 | LOG_API("storeR0ToR1(%d);\n", isInt); |
Jack Palevich | bd89490 | 2009-05-14 19:35:31 -0700 | [diff] [blame] | 587 | if (isInt) { |
| 588 | o4(0xE5810000); // str r0, [r1] |
| 589 | } else { |
| 590 | o4(0xE5C10000); // strb r0, [r1] |
| 591 | } |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 592 | } |
| 593 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 594 | virtual void loadR0FromR0(bool isInt) { |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 595 | LOG_API("loadR0FromR0(%d);\n", isInt); |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 596 | if (isInt) |
Jack Palevich | 69796b6 | 2009-05-14 15:42:26 -0700 | [diff] [blame] | 597 | o4(0xE5900000); // ldr r0, [r0] |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 598 | else |
Jack Palevich | 69796b6 | 2009-05-14 15:42:26 -0700 | [diff] [blame] | 599 | o4(0xE5D00000); // ldrb r0, [r0] |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 600 | } |
| 601 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 602 | virtual void leaR0(int ea) { |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 603 | LOG_API("leaR0(%d);\n", ea); |
Jack Palevich | 4d93f30 | 2009-05-15 13:30:00 -0700 | [diff] [blame] | 604 | if (ea < LOCAL) { |
| 605 | // Local, fp relative |
| 606 | if (ea < -1023 || ea > 1023 || ((ea & 3) != 0)) { |
| 607 | error("Offset out of range: %08x", ea); |
| 608 | } |
| 609 | if (ea < 0) { |
| 610 | o4(0xE24B0F00 | (0xff & ((-ea) >> 2))); // sub r0, fp, #ea |
| 611 | } else { |
| 612 | o4(0xE28B0F00 | (0xff & (ea >> 2))); // add r0, fp, #ea |
| 613 | } |
Jack Palevich | bd89490 | 2009-05-14 19:35:31 -0700 | [diff] [blame] | 614 | } else { |
Jack Palevich | 4d93f30 | 2009-05-15 13:30:00 -0700 | [diff] [blame] | 615 | // Global, absolute. |
| 616 | o4(0xE59F0000); // ldr r0, .L1 |
| 617 | o4(0xEA000000); // b .L99 |
| 618 | o4(ea); // .L1: .word 0 |
| 619 | // .L99: |
Jack Palevich | bd89490 | 2009-05-14 19:35:31 -0700 | [diff] [blame] | 620 | } |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 621 | } |
| 622 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 623 | virtual void storeR0(int ea) { |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 624 | LOG_API("storeR0(%d);\n", ea); |
Jack Palevich | 4d93f30 | 2009-05-15 13:30:00 -0700 | [diff] [blame] | 625 | if (ea < LOCAL) { |
| 626 | // Local, fp relative |
| 627 | if (ea < -4095 || ea > 4095) { |
| 628 | error("Offset out of range: %08x", ea); |
| 629 | } |
| 630 | if (ea < 0) { |
| 631 | o4(0xE50B0000 | (0xfff & (-ea))); // str r0, [fp,#-ea] |
| 632 | } else { |
| 633 | o4(0xE58B0000 | (0xfff & ea)); // str r0, [fp,#ea] |
| 634 | } |
| 635 | } else{ |
| 636 | // Global, absolute |
| 637 | o4(0xE59F1000); // ldr r1, .L1 |
| 638 | o4(0xEA000000); // b .L99 |
| 639 | o4(ea); // .L1: .word 0 |
| 640 | o4(0xE5810000); // .L99: str r0, [r1] |
Jack Palevich | 69796b6 | 2009-05-14 15:42:26 -0700 | [diff] [blame] | 641 | } |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 642 | } |
| 643 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 644 | virtual void loadR0(int ea, bool isIncDec, int op) { |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 645 | LOG_API("loadR0(%d, %d, %d);\n", ea, isIncDec, op); |
Jack Palevich | 4d93f30 | 2009-05-15 13:30:00 -0700 | [diff] [blame] | 646 | if (ea < LOCAL) { |
| 647 | // Local, fp relative |
| 648 | if (ea < -4095 || ea > 4095) { |
| 649 | error("Offset out of range: %08x", ea); |
| 650 | } |
| 651 | if (ea < 0) { |
| 652 | o4(0xE51B0000 | (0xfff & (-ea))); // ldr r0, [fp,#-ea] |
| 653 | } else { |
| 654 | o4(0xE59B0000 | (0xfff & ea)); // ldr r0, [fp,#ea] |
| 655 | } |
Jack Palevich | 69796b6 | 2009-05-14 15:42:26 -0700 | [diff] [blame] | 656 | } else { |
Jack Palevich | 4d93f30 | 2009-05-15 13:30:00 -0700 | [diff] [blame] | 657 | // Global, absolute |
| 658 | o4(0xE59F2000); // ldr r2, .L1 |
| 659 | o4(0xEA000000); // b .L99 |
| 660 | o4(ea); // .L1: .word ea |
| 661 | o4(0xE5920000); // .L99: ldr r0, [r2] |
Jack Palevich | 69796b6 | 2009-05-14 15:42:26 -0700 | [diff] [blame] | 662 | } |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 663 | |
Jack Palevich | 4d93f30 | 2009-05-15 13:30:00 -0700 | [diff] [blame] | 664 | if (isIncDec) { |
| 665 | switch (op) { |
| 666 | case OP_INCREMENT: |
| 667 | o4(0xE2801001); // add r1, r0, #1 |
| 668 | break; |
| 669 | case OP_DECREMENT: |
| 670 | o4(0xE2401001); // sub r1, r0, #1 |
| 671 | break; |
| 672 | default: |
| 673 | error("unknown opcode: %d", op); |
| 674 | } |
| 675 | if (ea < LOCAL) { |
| 676 | // Local, fp relative |
| 677 | // Don't need range check, was already checked above |
| 678 | if (ea < 0) { |
| 679 | o4(0xE50B1000 | (0xfff & (-ea))); // str r1, [fp,#-ea] |
| 680 | } else { |
| 681 | o4(0xE58B1000 | (0xfff & ea)); // str r1, [fp,#ea] |
| 682 | } |
| 683 | } else{ |
| 684 | // Global, absolute |
| 685 | // r2 is already set up from before. |
| 686 | o4(0xE5821000); // str r1, [r2] |
| 687 | } |
Jack Palevich | bd89490 | 2009-05-14 19:35:31 -0700 | [diff] [blame] | 688 | } |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 689 | } |
| 690 | |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 691 | virtual int beginFunctionCallArguments() { |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 692 | LOG_API("beginFunctionCallArguments();\n"); |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 693 | return o4(0xE24DDF00); // Placeholder |
| 694 | } |
| 695 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 696 | virtual void storeR0ToArg(int l) { |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 697 | LOG_API("storeR0ToArg(%d);\n", l); |
Jack Palevich | 7810bc9 | 2009-05-15 14:31:47 -0700 | [diff] [blame] | 698 | if (l < 0 || l > 4096-4) { |
| 699 | error("l out of range for stack offset: 0x%08x", l); |
| 700 | } |
| 701 | o4(0xE58D0000 + l); // str r0, [sp, #4] |
| 702 | } |
| 703 | |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 704 | virtual void endFunctionCallArguments(int a, int l) { |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 705 | LOG_API("endFunctionCallArguments(0x%08x, %d);\n", a, l); |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 706 | int argCount = l >> 2; |
| 707 | int argumentStackUse = l; |
| 708 | if (argCount > 0) { |
| 709 | int regArgCount = argCount > 4 ? 4 : argCount; |
| 710 | argumentStackUse -= regArgCount * 4; |
| 711 | o4(0xE8BD0000 | ((1 << regArgCount) - 1)); // ldmfd sp!,{} |
| 712 | } |
| 713 | mStackUse += argumentStackUse; |
| 714 | |
| 715 | // Align stack. |
| 716 | int missalignment = mStackUse - ((mStackUse / STACK_ALIGNMENT) |
| 717 | * STACK_ALIGNMENT); |
| 718 | mStackAlignmentAdjustment = 0; |
| 719 | if (missalignment > 0) { |
| 720 | mStackAlignmentAdjustment = STACK_ALIGNMENT - missalignment; |
| 721 | } |
| 722 | l += mStackAlignmentAdjustment; |
| 723 | |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 724 | if (l < 0 || l > 0x3FC) { |
| 725 | error("L out of range for stack adjustment: 0x%08x", l); |
| 726 | } |
| 727 | * (int*) a = 0xE24DDF00 | (l >> 2); // sub sp, sp, #0 << 2 |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 728 | mStackUse += mStackAlignmentAdjustment; |
| 729 | LOG_STACK("endFunctionCallArguments mStackUse: %d, mStackAlignmentAdjustment %d\n", |
| 730 | mStackUse, mStackAlignmentAdjustment); |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 731 | } |
| 732 | |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 733 | virtual int callForward(int symbol) { |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 734 | LOG_API("callForward(%d);\n", symbol); |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 735 | // Forward calls are always short (local) |
| 736 | return o4(0xEB000000 | encodeAddress(symbol)); |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | virtual void callRelative(int t) { |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 740 | LOG_API("callRelative(%d);\n", t); |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 741 | int abs = t + getPC() + jumpOffset(); |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 742 | LOG_API("abs=%d (0x%08x)\n", abs, abs); |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 743 | if (t >= - (1 << 25) && t < (1 << 25)) { |
| 744 | o4(0xEB000000 | encodeAddress(t)); |
| 745 | } else { |
| 746 | // Long call. |
| 747 | o4(0xE59FC000); // ldr r12, .L1 |
| 748 | o4(0xEA000000); // b .L99 |
Jack Palevich | bd89490 | 2009-05-14 19:35:31 -0700 | [diff] [blame] | 749 | o4(t - 12); // .L1: .word 0 |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 750 | o4(0xE08CC00F); // .L99: add r12,pc |
| 751 | o4(0xE12FFF3C); // blx r12 |
| 752 | } |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | virtual void callIndirect(int l) { |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 756 | LOG_API("callIndirect(%d);\n", l); |
Jack Palevich | 7810bc9 | 2009-05-15 14:31:47 -0700 | [diff] [blame] | 757 | int argCount = l >> 2; |
| 758 | int poppedArgs = argCount > 4 ? 4 : argCount; |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 759 | int adjustedL = l - (poppedArgs << 2) + mStackAlignmentAdjustment; |
Jack Palevich | 7810bc9 | 2009-05-15 14:31:47 -0700 | [diff] [blame] | 760 | if (adjustedL < 0 || adjustedL > 4096-4) { |
| 761 | error("l out of range for stack offset: 0x%08x", l); |
| 762 | } |
| 763 | o4(0xE59DC000 | (0xfff & adjustedL)); // ldr r12, [sp,#adjustedL] |
| 764 | o4(0xE12FFF3C); // blx r12 |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 765 | } |
| 766 | |
Jack Palevich | 7810bc9 | 2009-05-15 14:31:47 -0700 | [diff] [blame] | 767 | virtual void adjustStackAfterCall(int l, bool isIndirect) { |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 768 | LOG_API("adjustStackAfterCall(%d, %d);\n", l, isIndirect); |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 769 | int argCount = l >> 2; |
Jack Palevich | 7810bc9 | 2009-05-15 14:31:47 -0700 | [diff] [blame] | 770 | int stackArgs = argCount > 4 ? argCount - 4 : 0; |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 771 | int stackUse = stackArgs + (isIndirect ? 1 : 0) |
| 772 | + (mStackAlignmentAdjustment >> 2); |
Jack Palevich | 7810bc9 | 2009-05-15 14:31:47 -0700 | [diff] [blame] | 773 | if (stackUse) { |
| 774 | if (stackUse < 0 || stackUse > 255) { |
| 775 | error("L out of range for stack adjustment: 0x%08x", l); |
| 776 | } |
| 777 | o4(0xE28DDF00 | stackUse); // add sp, sp, #stackUse << 2 |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 778 | mStackUse -= stackUse * 4; |
| 779 | LOG_STACK("adjustStackAfterCall: %d\n", mStackUse); |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 780 | } |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 781 | } |
| 782 | |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 783 | virtual int jumpOffset() { |
Jack Palevich | bd89490 | 2009-05-14 19:35:31 -0700 | [diff] [blame] | 784 | return 8; |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | /* output a symbol and patch all calls to it */ |
| 788 | virtual void gsym(int t) { |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 789 | LOG_API("gsym(0x%x)\n", t); |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 790 | int n; |
| 791 | int base = getBase(); |
| 792 | int pc = getPC(); |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 793 | LOG_API("pc = 0x%x\n", pc); |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 794 | while (t) { |
| 795 | int data = * (int*) t; |
| 796 | int decodedOffset = ((BRANCH_REL_ADDRESS_MASK & data) << 2); |
| 797 | if (decodedOffset == 0) { |
| 798 | n = 0; |
| 799 | } else { |
| 800 | n = base + decodedOffset; /* next value */ |
| 801 | } |
| 802 | *(int *) t = (data & ~BRANCH_REL_ADDRESS_MASK) |
| 803 | | encodeRelAddress(pc - t - 8); |
| 804 | t = n; |
| 805 | } |
| 806 | } |
| 807 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 808 | virtual int finishCompile() { |
| 809 | #if defined(__arm__) |
| 810 | const long base = long(getBase()); |
| 811 | const long curr = long(getPC()); |
| 812 | int err = cacheflush(base, curr, 0); |
| 813 | return err; |
| 814 | #else |
| 815 | return 0; |
| 816 | #endif |
| 817 | } |
| 818 | |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 819 | virtual int disassemble(FILE* out) { |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 820 | #ifdef ENABLE_ARM_DISASSEMBLY |
| 821 | disasmOut = out; |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 822 | disasm_interface_t di; |
| 823 | di.di_readword = disassemble_readword; |
| 824 | di.di_printaddr = disassemble_printaddr; |
| 825 | di.di_printf = disassemble_printf; |
| 826 | |
| 827 | int base = getBase(); |
| 828 | int pc = getPC(); |
| 829 | for(int i = base; i < pc; i += 4) { |
| 830 | fprintf(out, "%08x: %08x ", i, *(int*) i); |
| 831 | ::disasm(&di, i, 0); |
| 832 | } |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 833 | #endif |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 834 | return 0; |
| 835 | } |
Jack Palevich | 7810bc9 | 2009-05-15 14:31:47 -0700 | [diff] [blame] | 836 | |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 837 | private: |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 838 | static FILE* disasmOut; |
| 839 | |
| 840 | static u_int |
| 841 | disassemble_readword(u_int address) |
| 842 | { |
| 843 | return(*((u_int *)address)); |
| 844 | } |
| 845 | |
| 846 | static void |
| 847 | disassemble_printaddr(u_int address) |
| 848 | { |
| 849 | fprintf(disasmOut, "0x%08x", address); |
| 850 | } |
| 851 | |
| 852 | static void |
| 853 | disassemble_printf(const char *fmt, ...) { |
| 854 | va_list ap; |
| 855 | va_start(ap, fmt); |
| 856 | vfprintf(disasmOut, fmt, ap); |
| 857 | va_end(ap); |
| 858 | } |
| 859 | |
| 860 | static const int BRANCH_REL_ADDRESS_MASK = 0x00ffffff; |
| 861 | |
| 862 | /** Encode a relative address that might also be |
| 863 | * a label. |
| 864 | */ |
| 865 | int encodeAddress(int value) { |
| 866 | int base = getBase(); |
| 867 | if (value >= base && value <= getPC() ) { |
| 868 | // This is a label, encode it relative to the base. |
| 869 | value = value - base; |
| 870 | } |
| 871 | return encodeRelAddress(value); |
| 872 | } |
| 873 | |
| 874 | int encodeRelAddress(int value) { |
| 875 | return BRANCH_REL_ADDRESS_MASK & (value >> 2); |
| 876 | } |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 877 | |
Jack Palevich | 3d474a7 | 2009-05-15 15:12:38 -0700 | [diff] [blame] | 878 | typedef int (*int2FnPtr)(int a, int b); |
| 879 | void callRuntime(int2FnPtr fn) { |
| 880 | o4(0xE59F2000); // ldr r2, .L1 |
| 881 | o4(0xEA000000); // b .L99 |
| 882 | o4((int) fn); //.L1: .word fn |
| 883 | o4(0xE12FFF32); //.L99: blx r2 |
| 884 | } |
| 885 | |
| 886 | static int runtime_DIV(int a, int b) { |
| 887 | return b / a; |
| 888 | } |
| 889 | |
| 890 | static int runtime_MOD(int a, int b) { |
| 891 | return b % a; |
| 892 | } |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 893 | |
| 894 | static const int STACK_ALIGNMENT = 8; |
| 895 | int mStackUse; |
| 896 | // This variable holds the amount we adjusted the stack in the most |
| 897 | // recent endFunctionCallArguments call. It's examined by the |
| 898 | // following adjustStackAfterCall call. |
| 899 | int mStackAlignmentAdjustment; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 900 | }; |
| 901 | |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 902 | #endif // PROVIDE_ARM_CODEGEN |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 903 | |
| 904 | #ifdef PROVIDE_X86_CODEGEN |
| 905 | |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 906 | class X86CodeGenerator : public CodeGenerator { |
| 907 | public: |
| 908 | X86CodeGenerator() {} |
| 909 | virtual ~X86CodeGenerator() {} |
| 910 | |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 911 | /* returns address to patch with local variable size |
| 912 | */ |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 913 | virtual int functionEntry(int argCount) { |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 914 | o(0xe58955); /* push %ebp, mov %esp, %ebp */ |
| 915 | return oad(0xec81, 0); /* sub $xxx, %esp */ |
| 916 | } |
| 917 | |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 918 | virtual void functionExit(int argCount, int localVariableAddress, int localVariableSize) { |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 919 | o(0xc3c9); /* leave, ret */ |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 920 | *(int *) localVariableAddress = localVariableSize; /* save local variables */ |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 921 | } |
| 922 | |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 923 | /* load immediate value */ |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 924 | virtual void li(int t) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 925 | oad(0xb8, t); /* mov $xx, %eax */ |
| 926 | } |
| 927 | |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 928 | virtual int gjmp(int t) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 929 | return psym(0xe9, t); |
| 930 | } |
| 931 | |
| 932 | /* l = 0: je, l == 1: jne */ |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 933 | virtual int gtst(bool l, int t) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 934 | o(0x0fc085); /* test %eax, %eax, je/jne xxx */ |
| 935 | return psym(0x84 + l, t); |
| 936 | } |
| 937 | |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 938 | virtual void gcmp(int op) { |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 939 | int t = decodeOp(op); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 940 | o(0xc139); /* cmp %eax,%ecx */ |
| 941 | li(0); |
| 942 | o(0x0f); /* setxx %al */ |
| 943 | o(t + 0x90); |
| 944 | o(0xc0); |
| 945 | } |
| 946 | |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 947 | virtual void genOp(int op) { |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 948 | o(decodeOp(op)); |
| 949 | if (op == OP_MOD) |
| 950 | o(0x92); /* xchg %edx, %eax */ |
| 951 | } |
| 952 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 953 | virtual void clearR1() { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 954 | oad(0xb9, 0); /* movl $0, %ecx */ |
| 955 | } |
| 956 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 957 | virtual void pushR0() { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 958 | o(0x50); /* push %eax */ |
| 959 | } |
| 960 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 961 | virtual void popR1() { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 962 | o(0x59); /* pop %ecx */ |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 963 | } |
| 964 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 965 | virtual void storeR0ToR1(bool isInt) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 966 | o(0x0188 + isInt); /* movl %eax/%al, (%ecx) */ |
| 967 | } |
| 968 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 969 | virtual void loadR0FromR0(bool isInt) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 970 | if (isInt) |
| 971 | o(0x8b); /* mov (%eax), %eax */ |
| 972 | else |
| 973 | o(0xbe0f); /* movsbl (%eax), %eax */ |
| 974 | ob(0); /* add zero in code */ |
| 975 | } |
| 976 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 977 | virtual void leaR0(int ea) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 978 | gmov(10, ea); /* leal EA, %eax */ |
| 979 | } |
| 980 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 981 | virtual void storeR0(int ea) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 982 | gmov(6, ea); /* mov %eax, EA */ |
| 983 | } |
| 984 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 985 | virtual void loadR0(int ea, bool isIncDec, int op) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 986 | gmov(8, ea); /* mov EA, %eax */ |
Jack Palevich | 4d93f30 | 2009-05-15 13:30:00 -0700 | [diff] [blame] | 987 | if (isIncDec) { |
| 988 | /* Implement post-increment or post decrement. |
| 989 | */ |
| 990 | gmov(0, ea); /* 83 ADD */ |
| 991 | o(decodeOp(op)); |
| 992 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 993 | } |
| 994 | |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 995 | virtual int beginFunctionCallArguments() { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 996 | return oad(0xec81, 0); /* sub $xxx, %esp */ |
| 997 | } |
| 998 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 999 | virtual void storeR0ToArg(int l) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 1000 | oad(0x248489, l); /* movl %eax, xxx(%esp) */ |
| 1001 | } |
| 1002 | |
Jack Palevich | 7810bc9 | 2009-05-15 14:31:47 -0700 | [diff] [blame] | 1003 | virtual void endFunctionCallArguments(int a, int l) { |
| 1004 | * (int*) a = l; |
| 1005 | } |
| 1006 | |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 1007 | virtual int callForward(int symbol) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 1008 | return psym(0xe8, symbol); /* call xxx */ |
| 1009 | } |
| 1010 | |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 1011 | virtual void callRelative(int t) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 1012 | psym(0xe8, t); /* call xxx */ |
| 1013 | } |
| 1014 | |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 1015 | virtual void callIndirect(int l) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 1016 | oad(0x2494ff, l); /* call *xxx(%esp) */ |
| 1017 | } |
| 1018 | |
Jack Palevich | 7810bc9 | 2009-05-15 14:31:47 -0700 | [diff] [blame] | 1019 | virtual void adjustStackAfterCall(int l, bool isIndirect) { |
| 1020 | if (isIndirect) { |
| 1021 | l += 4; |
| 1022 | } |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 1023 | if (l > 0) { |
| 1024 | oad(0xc481, l); /* add $xxx, %esp */ |
| 1025 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 1026 | } |
| 1027 | |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 1028 | virtual int jumpOffset() { |
| 1029 | return 5; |
| 1030 | } |
| 1031 | |
| 1032 | virtual int disassemble(FILE* out) { |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 1033 | return 0; |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 1034 | } |
| 1035 | |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 1036 | /* output a symbol and patch all calls to it */ |
| 1037 | virtual void gsym(int t) { |
| 1038 | int n; |
| 1039 | int pc = getPC(); |
| 1040 | while (t) { |
| 1041 | n = *(int *) t; /* next value */ |
| 1042 | *(int *) t = pc - t - 4; |
| 1043 | t = n; |
| 1044 | } |
| 1045 | } |
| 1046 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 1047 | virtual int finishCompile() { |
Jack Palevich | 8dc662e | 2009-06-09 22:53:47 +0000 | [diff] [blame] | 1048 | size_t pagesize = 4096; |
| 1049 | size_t base = (size_t) getBase() & ~ (pagesize - 1); |
| 1050 | size_t top = ((size_t) getPC() + pagesize - 1) & ~ (pagesize - 1); |
| 1051 | int err = mprotect((void*) base, top - base, PROT_READ | PROT_WRITE | PROT_EXEC); |
| 1052 | if (err) { |
| 1053 | error("mprotect() failed: %d", errno); |
| 1054 | } |
| 1055 | return err; |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 1056 | } |
| 1057 | |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 1058 | private: |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 1059 | |
| 1060 | /** Output 1 to 4 bytes. |
| 1061 | * |
| 1062 | */ |
| 1063 | void o(int n) { |
| 1064 | /* cannot use unsigned, so we must do a hack */ |
| 1065 | while (n && n != -1) { |
| 1066 | ob(n & 0xff); |
| 1067 | n = n >> 8; |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | /* psym is used to put an instruction with a data field which is a |
| 1072 | reference to a symbol. It is in fact the same as oad ! */ |
| 1073 | int psym(int n, int t) { |
| 1074 | return oad(n, t); |
| 1075 | } |
| 1076 | |
| 1077 | /* instruction + address */ |
| 1078 | int oad(int n, int t) { |
| 1079 | o(n); |
| 1080 | int result = getPC(); |
| 1081 | o4(t); |
| 1082 | return result; |
| 1083 | } |
| 1084 | |
| 1085 | |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 1086 | static const int operatorHelper[]; |
| 1087 | |
| 1088 | int decodeOp(int op) { |
| 1089 | if (op < 0 || op > OP_COUNT) { |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 1090 | error("Out-of-range operator: %d\n", op); |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 1091 | op = 0; |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 1092 | } |
| 1093 | return operatorHelper[op]; |
| 1094 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 1095 | |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 1096 | void gmov(int l, int t) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 1097 | o(l + 0x83); |
Jack Palevich | 8dc662e | 2009-06-09 22:53:47 +0000 | [diff] [blame] | 1098 | oad((t > -LOCAL && t < LOCAL) << 7 | 5, t); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 1099 | } |
| 1100 | }; |
| 1101 | |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 1102 | #endif // PROVIDE_X86_CODEGEN |
| 1103 | |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 1104 | #ifdef PROVIDE_TRACE_CODEGEN |
| 1105 | class TraceCodeGenerator : public CodeGenerator { |
| 1106 | private: |
| 1107 | CodeGenerator* mpBase; |
| 1108 | |
| 1109 | public: |
| 1110 | TraceCodeGenerator(CodeGenerator* pBase) { |
| 1111 | mpBase = pBase; |
| 1112 | } |
| 1113 | |
| 1114 | virtual ~TraceCodeGenerator() { |
| 1115 | delete mpBase; |
| 1116 | } |
| 1117 | |
| 1118 | virtual void init(CodeBuf* pCodeBuf) { |
| 1119 | mpBase->init(pCodeBuf); |
| 1120 | } |
| 1121 | |
| 1122 | void setErrorSink(ErrorSink* pErrorSink) { |
| 1123 | mpBase->setErrorSink(pErrorSink); |
| 1124 | } |
| 1125 | |
| 1126 | /* returns address to patch with local variable size |
| 1127 | */ |
| 1128 | virtual int functionEntry(int argCount) { |
| 1129 | int result = mpBase->functionEntry(argCount); |
| 1130 | fprintf(stderr, "functionEntry(%d) -> %d\n", argCount, result); |
| 1131 | return result; |
| 1132 | } |
| 1133 | |
| 1134 | virtual void functionExit(int argCount, int localVariableAddress, int localVariableSize) { |
| 1135 | fprintf(stderr, "functionExit(%d, %d, %d)\n", |
| 1136 | argCount, localVariableAddress, localVariableSize); |
| 1137 | mpBase->functionExit(argCount, localVariableAddress, localVariableSize); |
| 1138 | } |
| 1139 | |
| 1140 | /* load immediate value */ |
| 1141 | virtual void li(int t) { |
| 1142 | fprintf(stderr, "li(%d)\n", t); |
| 1143 | mpBase->li(t); |
| 1144 | } |
| 1145 | |
| 1146 | virtual int gjmp(int t) { |
| 1147 | int result = mpBase->gjmp(t); |
| 1148 | fprintf(stderr, "gjmp(%d) = %d\n", t, result); |
| 1149 | return result; |
| 1150 | } |
| 1151 | |
| 1152 | /* l = 0: je, l == 1: jne */ |
| 1153 | virtual int gtst(bool l, int t) { |
| 1154 | int result = mpBase->gtst(l, t); |
| 1155 | fprintf(stderr, "gtst(%d,%d) = %d\n", l, t, result); |
| 1156 | return result; |
| 1157 | } |
| 1158 | |
| 1159 | virtual void gcmp(int op) { |
| 1160 | fprintf(stderr, "gcmp(%d)\n", op); |
| 1161 | mpBase->gcmp(op); |
| 1162 | } |
| 1163 | |
| 1164 | virtual void genOp(int op) { |
| 1165 | fprintf(stderr, "genOp(%d)\n", op); |
| 1166 | mpBase->genOp(op); |
| 1167 | } |
| 1168 | |
| 1169 | virtual void clearR1() { |
| 1170 | fprintf(stderr, "clearR1()\n"); |
| 1171 | mpBase->clearR1(); |
| 1172 | } |
| 1173 | |
| 1174 | virtual void pushR0() { |
| 1175 | fprintf(stderr, "pushR0()\n"); |
| 1176 | mpBase->pushR0(); |
| 1177 | } |
| 1178 | |
| 1179 | virtual void popR1() { |
| 1180 | fprintf(stderr, "popR1()\n"); |
| 1181 | mpBase->popR1(); |
| 1182 | } |
| 1183 | |
| 1184 | virtual void storeR0ToR1(bool isInt) { |
| 1185 | fprintf(stderr, "storeR0ToR1(%d)\n", isInt); |
| 1186 | mpBase->storeR0ToR1(isInt); |
| 1187 | } |
| 1188 | |
| 1189 | virtual void loadR0FromR0(bool isInt) { |
| 1190 | fprintf(stderr, "loadR0FromR0(%d)\n", isInt); |
| 1191 | mpBase->loadR0FromR0(isInt); |
| 1192 | } |
| 1193 | |
| 1194 | virtual void leaR0(int ea) { |
| 1195 | fprintf(stderr, "leaR0(%d)\n", ea); |
| 1196 | mpBase->leaR0(ea); |
| 1197 | } |
| 1198 | |
| 1199 | virtual void storeR0(int ea) { |
| 1200 | fprintf(stderr, "storeR0(%d)\n", ea); |
| 1201 | mpBase->storeR0(ea); |
| 1202 | } |
| 1203 | |
| 1204 | virtual void loadR0(int ea, bool isIncDec, int op) { |
| 1205 | fprintf(stderr, "loadR0(%d, %d, %d)\n", ea, isIncDec, op); |
| 1206 | mpBase->loadR0(ea, isIncDec, op); |
| 1207 | } |
| 1208 | |
| 1209 | virtual int beginFunctionCallArguments() { |
| 1210 | int result = mpBase->beginFunctionCallArguments(); |
| 1211 | fprintf(stderr, "beginFunctionCallArguments() = %d\n", result); |
| 1212 | return result; |
| 1213 | } |
| 1214 | |
| 1215 | virtual void storeR0ToArg(int l) { |
| 1216 | fprintf(stderr, "storeR0ToArg(%d)\n", l); |
| 1217 | mpBase->storeR0ToArg(l); |
| 1218 | } |
| 1219 | |
| 1220 | virtual void endFunctionCallArguments(int a, int l) { |
| 1221 | fprintf(stderr, "endFunctionCallArguments(%d, %d)\n", a, l); |
| 1222 | mpBase->endFunctionCallArguments(a, l); |
| 1223 | } |
| 1224 | |
| 1225 | virtual int callForward(int symbol) { |
| 1226 | int result = mpBase->callForward(symbol); |
| 1227 | fprintf(stderr, "callForward(%d) = %d\n", symbol, result); |
| 1228 | return result; |
| 1229 | } |
| 1230 | |
| 1231 | virtual void callRelative(int t) { |
| 1232 | fprintf(stderr, "callRelative(%d)\n", t); |
| 1233 | mpBase->callRelative(t); |
| 1234 | } |
| 1235 | |
| 1236 | virtual void callIndirect(int l) { |
| 1237 | fprintf(stderr, "callIndirect(%d)\n", l); |
| 1238 | mpBase->callIndirect(l); |
| 1239 | } |
| 1240 | |
| 1241 | virtual void adjustStackAfterCall(int l, bool isIndirect) { |
| 1242 | fprintf(stderr, "adjustStackAfterCall(%d, %d)\n", l, isIndirect); |
| 1243 | mpBase->adjustStackAfterCall(l, isIndirect); |
| 1244 | } |
| 1245 | |
| 1246 | virtual int jumpOffset() { |
| 1247 | return mpBase->jumpOffset(); |
| 1248 | } |
| 1249 | |
| 1250 | virtual int disassemble(FILE* out) { |
| 1251 | return mpBase->disassemble(out); |
| 1252 | } |
| 1253 | |
| 1254 | /* output a symbol and patch all calls to it */ |
| 1255 | virtual void gsym(int t) { |
| 1256 | fprintf(stderr, "gsym(%d)\n", t); |
| 1257 | mpBase->gsym(t); |
| 1258 | } |
| 1259 | |
| 1260 | virtual int finishCompile() { |
| 1261 | int result = mpBase->finishCompile(); |
| 1262 | fprintf(stderr, "finishCompile() = %d\n", result); |
| 1263 | return result; |
| 1264 | } |
| 1265 | }; |
| 1266 | |
| 1267 | #endif // PROVIDE_TRACE_CODEGEN |
| 1268 | |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 1269 | |
| 1270 | // Subset of STL vector. |
| 1271 | template<class E> class Vector { |
| 1272 | public: |
| 1273 | Vector() { |
| 1274 | mpBase = 0; |
| 1275 | mUsed = 0; |
| 1276 | mSize = 0; |
| 1277 | } |
| 1278 | |
| 1279 | ~Vector() { |
| 1280 | if (mpBase) { |
| 1281 | for(size_t i = 0; i < mUsed; i++) { |
| 1282 | mpBase[mUsed].~E(); |
| 1283 | } |
| 1284 | free(mpBase); |
| 1285 | } |
| 1286 | } |
| 1287 | |
| 1288 | inline E& operator[](size_t i) { |
| 1289 | return mpBase[i]; |
| 1290 | } |
| 1291 | |
| 1292 | inline E& front() { |
| 1293 | return mpBase[0]; |
| 1294 | } |
| 1295 | |
| 1296 | inline E& back() { |
| 1297 | return mpBase[mUsed - 1]; |
| 1298 | } |
| 1299 | |
| 1300 | void pop_back() { |
| 1301 | mUsed -= 1; |
| 1302 | mpBase[mUsed].~E(); |
| 1303 | } |
| 1304 | |
| 1305 | void push_back(const E& item) { |
| 1306 | * ensure(1) = item; |
| 1307 | } |
| 1308 | |
| 1309 | size_t size() { |
| 1310 | return mUsed; |
| 1311 | } |
| 1312 | |
| 1313 | private: |
| 1314 | E* ensure(int n) { |
| 1315 | size_t newUsed = mUsed + n; |
| 1316 | if (newUsed > mSize) { |
| 1317 | size_t newSize = mSize * 2 + 10; |
| 1318 | if (newSize < newUsed) { |
| 1319 | newSize = newUsed; |
| 1320 | } |
| 1321 | mpBase = (E*) realloc(mpBase, sizeof(E) * newSize); |
| 1322 | mSize = newSize; |
| 1323 | } |
| 1324 | E* result = mpBase + mUsed; |
| 1325 | mUsed = newUsed; |
| 1326 | return result; |
| 1327 | } |
| 1328 | |
| 1329 | E* mpBase; |
| 1330 | size_t mUsed; |
| 1331 | size_t mSize; |
| 1332 | }; |
| 1333 | |
| 1334 | class Arena { |
| 1335 | public: |
| 1336 | // Used to record a given allocation amount. |
| 1337 | // Used: |
| 1338 | // Mark mark = arena.mark(); |
| 1339 | // ... lots of arena.allocate() |
| 1340 | // arena.free(mark); |
| 1341 | |
| 1342 | struct Mark { |
| 1343 | size_t chunk; |
| 1344 | size_t offset; |
| 1345 | }; |
| 1346 | |
| 1347 | Arena() { |
| 1348 | mCurrentChunk = 0; |
| 1349 | Chunk start(CHUNK_SIZE); |
| 1350 | mData.push_back(start); |
| 1351 | } |
| 1352 | |
| 1353 | ~Arena() { |
| 1354 | for(size_t i = 0; i < mData.size(); i++) { |
| 1355 | mData[i].free(); |
| 1356 | } |
| 1357 | } |
| 1358 | |
| 1359 | // Alloc using the standard alignment size safe for any variable |
| 1360 | void* alloc(size_t size) { |
| 1361 | return alloc(size, 8); |
| 1362 | } |
| 1363 | |
| 1364 | Mark mark(){ |
| 1365 | Mark result; |
| 1366 | result.chunk = mCurrentChunk; |
| 1367 | result.offset = mData[mCurrentChunk].mOffset; |
| 1368 | return result; |
| 1369 | } |
| 1370 | |
| 1371 | void freeToMark(const Mark& mark) { |
| 1372 | mCurrentChunk = mark.chunk; |
| 1373 | mData[mCurrentChunk].mOffset = mark.offset; |
| 1374 | } |
| 1375 | |
| 1376 | private: |
| 1377 | // Allocate memory aligned to a given size |
| 1378 | // and a given power-of-two-sized alignment (e.g. 1,2,4,8,...) |
| 1379 | // Memory is not zero filled. |
| 1380 | |
| 1381 | void* alloc(size_t size, size_t alignment) { |
| 1382 | while (size > mData[mCurrentChunk].remainingCapacity(alignment)) { |
| 1383 | if (mCurrentChunk + 1 < mData.size()) { |
| 1384 | mCurrentChunk++; |
| 1385 | } else { |
| 1386 | size_t allocSize = CHUNK_SIZE; |
| 1387 | if (allocSize < size + alignment - 1) { |
| 1388 | allocSize = size + alignment - 1; |
| 1389 | } |
| 1390 | Chunk chunk(allocSize); |
| 1391 | mData.push_back(chunk); |
| 1392 | mCurrentChunk++; |
| 1393 | } |
| 1394 | } |
| 1395 | return mData[mCurrentChunk].allocate(size, alignment); |
| 1396 | } |
| 1397 | |
| 1398 | static const size_t CHUNK_SIZE = 128*1024; |
| 1399 | // Note: this class does not deallocate its |
| 1400 | // memory when it's destroyed. It depends upon |
| 1401 | // its parent to deallocate the memory. |
| 1402 | struct Chunk { |
| 1403 | Chunk() { |
| 1404 | mpData = 0; |
| 1405 | mSize = 0; |
| 1406 | mOffset = 0; |
| 1407 | } |
| 1408 | |
| 1409 | Chunk(size_t size) { |
| 1410 | mSize = size; |
| 1411 | mpData = (char*) malloc(size); |
| 1412 | mOffset = 0; |
| 1413 | } |
| 1414 | |
| 1415 | ~Chunk() { |
| 1416 | // Doesn't deallocate memory. |
| 1417 | } |
| 1418 | |
| 1419 | void* allocate(size_t size, size_t alignment) { |
| 1420 | size_t alignedOffset = aligned(mOffset, alignment); |
| 1421 | void* result = mpData + alignedOffset; |
| 1422 | mOffset = alignedOffset + size; |
| 1423 | return result; |
| 1424 | } |
| 1425 | |
| 1426 | void free() { |
| 1427 | if (mpData) { |
| 1428 | ::free(mpData); |
| 1429 | mpData = 0; |
| 1430 | } |
| 1431 | } |
| 1432 | |
| 1433 | size_t remainingCapacity(size_t alignment) { |
| 1434 | return aligned(mSize, alignment) - aligned(mOffset, alignment); |
| 1435 | } |
| 1436 | |
| 1437 | // Assume alignment is a power of two |
| 1438 | inline size_t aligned(size_t v, size_t alignment) { |
| 1439 | size_t mask = alignment-1; |
| 1440 | return (v + mask) & ~mask; |
| 1441 | } |
| 1442 | |
| 1443 | char* mpData; |
| 1444 | size_t mSize; |
| 1445 | size_t mOffset; |
| 1446 | }; |
| 1447 | |
| 1448 | size_t mCurrentChunk; |
| 1449 | |
| 1450 | Vector<Chunk> mData; |
| 1451 | }; |
| 1452 | |
| 1453 | typedef int tokenid_t; |
| 1454 | struct VariableInfo; |
| 1455 | |
| 1456 | struct Token { |
| 1457 | int hash; |
| 1458 | size_t length; |
| 1459 | char* pText; |
| 1460 | tokenid_t id; |
| 1461 | |
| 1462 | // Current values for the token |
| 1463 | char* mpMacroDefinition; |
| 1464 | VariableInfo* mpVariableInfo; |
| 1465 | }; |
| 1466 | |
| 1467 | class TokenTable { |
| 1468 | public: |
| 1469 | // Don't use 0..0xff, allows characters and operators to be tokens too. |
| 1470 | |
| 1471 | static const int TOKEN_BASE = 0x100; |
| 1472 | TokenTable() { |
| 1473 | mpMap = hashmapCreate(128, hashFn, equalsFn); |
| 1474 | } |
| 1475 | |
| 1476 | ~TokenTable() { |
| 1477 | hashmapFree(mpMap); |
| 1478 | } |
| 1479 | |
| 1480 | void setArena(Arena* pArena) { |
| 1481 | mpArena = pArena; |
| 1482 | } |
| 1483 | |
| 1484 | // Returns a token for a given string of characters. |
| 1485 | tokenid_t intern(const char* pText, size_t length) { |
| 1486 | Token probe; |
| 1487 | int hash = hashmapHash((void*) pText, length); |
| 1488 | { |
| 1489 | Token probe; |
| 1490 | probe.hash = hash; |
| 1491 | probe.length = length; |
| 1492 | probe.pText = (char*) pText; |
| 1493 | Token* pValue = (Token*) hashmapGet(mpMap, &probe); |
| 1494 | if (pValue) { |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 1495 | return pValue->id; |
| 1496 | } |
| 1497 | } |
| 1498 | |
| 1499 | Token* pToken = (Token*) mpArena->alloc(sizeof(Token)); |
| 1500 | memset(pToken, 0, sizeof(*pToken)); |
| 1501 | pToken->hash = hash; |
| 1502 | pToken->length = length; |
| 1503 | pToken->pText = (char*) mpArena->alloc(length + 1); |
| 1504 | memcpy(pToken->pText, pText, length); |
| 1505 | pToken->pText[length] = 0; |
| 1506 | pToken->id = mTokens.size() + TOKEN_BASE; |
| 1507 | mTokens.push_back(pToken); |
| 1508 | hashmapPut(mpMap, pToken, pToken); |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 1509 | return pToken->id; |
| 1510 | } |
| 1511 | |
| 1512 | // Return the Token for a given tokenid. |
| 1513 | Token& operator[](tokenid_t id) { |
| 1514 | return *mTokens[id - TOKEN_BASE]; |
| 1515 | } |
| 1516 | |
| 1517 | inline size_t size() { |
| 1518 | return mTokens.size(); |
| 1519 | } |
| 1520 | |
| 1521 | private: |
| 1522 | |
| 1523 | static int hashFn(void* pKey) { |
| 1524 | Token* pToken = (Token*) pKey; |
| 1525 | return pToken->hash; |
| 1526 | } |
| 1527 | |
| 1528 | static bool equalsFn(void* keyA, void* keyB) { |
| 1529 | Token* pTokenA = (Token*) keyA; |
| 1530 | Token* pTokenB = (Token*) keyB; |
| 1531 | // Don't need to compare hash values, they should always be equal |
| 1532 | return pTokenA->length == pTokenB->length |
| 1533 | && strcmp(pTokenA->pText, pTokenB->pText) == 0; |
| 1534 | } |
| 1535 | |
| 1536 | Hashmap* mpMap; |
| 1537 | Vector<Token*> mTokens; |
| 1538 | Arena* mpArena; |
| 1539 | }; |
| 1540 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 1541 | class InputStream { |
| 1542 | public: |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 1543 | int getChar() { |
| 1544 | if (bumpLine) { |
| 1545 | line++; |
| 1546 | bumpLine = false; |
| 1547 | } |
| 1548 | int ch = get(); |
| 1549 | if (ch == '\n') { |
| 1550 | bumpLine = true; |
| 1551 | } |
| 1552 | return ch; |
| 1553 | } |
| 1554 | int getLine() { |
| 1555 | return line; |
| 1556 | } |
| 1557 | protected: |
| 1558 | InputStream() : |
| 1559 | line(1), bumpLine(false) { |
| 1560 | } |
| 1561 | private: |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 1562 | virtual int get() = 0; |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 1563 | int line; |
| 1564 | bool bumpLine; |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 1565 | }; |
| 1566 | |
| 1567 | class FileInputStream : public InputStream { |
| 1568 | public: |
| 1569 | FileInputStream(FILE* in) : f(in) {} |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 1570 | private: |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 1571 | virtual int get() { return fgetc(f); } |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 1572 | FILE* f; |
| 1573 | }; |
| 1574 | |
| 1575 | class TextInputStream : public InputStream { |
| 1576 | public: |
| 1577 | TextInputStream(const char* text, size_t textLength) |
| 1578 | : pText(text), mTextLength(textLength), mPosition(0) { |
| 1579 | } |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 1580 | |
| 1581 | private: |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 1582 | virtual int get() { |
| 1583 | return mPosition < mTextLength ? pText[mPosition++] : EOF; |
| 1584 | } |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 1585 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 1586 | const char* pText; |
| 1587 | size_t mTextLength; |
| 1588 | size_t mPosition; |
| 1589 | }; |
| 1590 | |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 1591 | class String { |
| 1592 | public: |
| 1593 | String() { |
| 1594 | mpBase = 0; |
| 1595 | mUsed = 0; |
| 1596 | mSize = 0; |
| 1597 | } |
| 1598 | |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 1599 | String(const char* item, int len, bool adopt) { |
| 1600 | if (len < 0) { |
| 1601 | len = strlen(item); |
| 1602 | } |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 1603 | if (adopt) { |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 1604 | mpBase = (char*) item; |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 1605 | mUsed = len; |
| 1606 | mSize = len + 1; |
| 1607 | } else { |
| 1608 | mpBase = 0; |
| 1609 | mUsed = 0; |
| 1610 | mSize = 0; |
| 1611 | appendBytes(item, len); |
| 1612 | } |
| 1613 | } |
| 1614 | |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 1615 | String(const String& other) { |
| 1616 | mpBase = 0; |
| 1617 | mUsed = 0; |
| 1618 | mSize = 0; |
| 1619 | appendBytes(other.getUnwrapped(), other.len()); |
| 1620 | } |
| 1621 | |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 1622 | ~String() { |
| 1623 | if (mpBase) { |
| 1624 | free(mpBase); |
| 1625 | } |
| 1626 | } |
| 1627 | |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 1628 | String& operator=(const String& other) { |
| 1629 | clear(); |
| 1630 | appendBytes(other.getUnwrapped(), other.len()); |
| 1631 | return *this; |
| 1632 | } |
| 1633 | |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 1634 | inline char* getUnwrapped() const { |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 1635 | return mpBase; |
| 1636 | } |
| 1637 | |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 1638 | void clear() { |
| 1639 | mUsed = 0; |
| 1640 | if (mSize > 0) { |
| 1641 | mpBase[0] = 0; |
| 1642 | } |
| 1643 | } |
| 1644 | |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 1645 | void appendCStr(const char* s) { |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 1646 | appendBytes(s, strlen(s)); |
| 1647 | } |
| 1648 | |
| 1649 | void appendBytes(const char* s, int n) { |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 1650 | memcpy(ensure(n), s, n + 1); |
| 1651 | } |
| 1652 | |
| 1653 | void append(char c) { |
| 1654 | * ensure(1) = c; |
| 1655 | } |
| 1656 | |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 1657 | void append(String& other) { |
| 1658 | appendBytes(other.getUnwrapped(), other.len()); |
| 1659 | } |
| 1660 | |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 1661 | char* orphan() { |
| 1662 | char* result = mpBase; |
| 1663 | mpBase = 0; |
| 1664 | mUsed = 0; |
| 1665 | mSize = 0; |
| 1666 | return result; |
| 1667 | } |
| 1668 | |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 1669 | void printf(const char* fmt,...) { |
| 1670 | va_list ap; |
| 1671 | va_start(ap, fmt); |
| 1672 | vprintf(fmt, ap); |
| 1673 | va_end(ap); |
| 1674 | } |
| 1675 | |
| 1676 | void vprintf(const char* fmt, va_list ap) { |
| 1677 | char* temp; |
| 1678 | int numChars = vasprintf(&temp, fmt, ap); |
| 1679 | memcpy(ensure(numChars), temp, numChars+1); |
| 1680 | free(temp); |
| 1681 | } |
| 1682 | |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 1683 | inline size_t len() const { |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 1684 | return mUsed; |
| 1685 | } |
| 1686 | |
| 1687 | private: |
| 1688 | char* ensure(int n) { |
| 1689 | size_t newUsed = mUsed + n; |
| 1690 | if (newUsed > mSize) { |
| 1691 | size_t newSize = mSize * 2 + 10; |
| 1692 | if (newSize < newUsed) { |
| 1693 | newSize = newUsed; |
| 1694 | } |
| 1695 | mpBase = (char*) realloc(mpBase, newSize + 1); |
| 1696 | mSize = newSize; |
| 1697 | } |
| 1698 | mpBase[newUsed] = '\0'; |
| 1699 | char* result = mpBase + mUsed; |
| 1700 | mUsed = newUsed; |
| 1701 | return result; |
| 1702 | } |
| 1703 | |
| 1704 | char* mpBase; |
| 1705 | size_t mUsed; |
| 1706 | size_t mSize; |
| 1707 | }; |
| 1708 | |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 1709 | void internKeywords() { |
| 1710 | // Note: order has to match TOK_ constants |
| 1711 | static const char* keywords[] = { |
| 1712 | "int", |
| 1713 | "char", |
| 1714 | "void", |
| 1715 | "if", |
| 1716 | "else", |
| 1717 | "while", |
| 1718 | "break", |
| 1719 | "return", |
| 1720 | "for", |
| 1721 | "pragma", |
| 1722 | "define", |
| 1723 | "auto", |
| 1724 | "case", |
| 1725 | "const", |
| 1726 | "continue", |
| 1727 | "default", |
| 1728 | "do", |
| 1729 | "double", |
| 1730 | "enum", |
| 1731 | "extern", |
| 1732 | "float", |
| 1733 | "goto", |
| 1734 | "long", |
| 1735 | "register", |
| 1736 | "short", |
| 1737 | "signed", |
| 1738 | "sizeof", |
| 1739 | "static", |
| 1740 | "struct", |
| 1741 | "switch", |
| 1742 | "typedef", |
| 1743 | "union", |
| 1744 | "unsigned", |
| 1745 | "volatile", |
| 1746 | "_Bool", |
| 1747 | "_Complex", |
| 1748 | "_Imaginary", |
| 1749 | "inline", |
| 1750 | "restrict", |
| 1751 | 0}; |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 1752 | |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 1753 | for(int i = 0; keywords[i]; i++) { |
| 1754 | mTokenTable.intern(keywords[i], strlen(keywords[i])); |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 1755 | } |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 1756 | } |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 1757 | |
Jack Palevich | 36d9414 | 2009-06-08 15:55:32 -0700 | [diff] [blame] | 1758 | struct InputState { |
| 1759 | InputStream* pStream; |
| 1760 | int oldCh; |
| 1761 | }; |
| 1762 | |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 1763 | struct Type; |
| 1764 | |
Jack Palevich | 2db168f | 2009-06-11 14:29:47 -0700 | [diff] [blame] | 1765 | struct VariableInfo { |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 1766 | void* pAddress; |
| 1767 | void* pForward; // For a forward direction, linked list of data to fix up |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 1768 | tokenid_t tok; |
| 1769 | size_t level; |
| 1770 | VariableInfo* pOldDefinition; |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 1771 | Type* pType; |
Jack Palevich | 2db168f | 2009-06-11 14:29:47 -0700 | [diff] [blame] | 1772 | }; |
| 1773 | |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 1774 | class SymbolStack { |
| 1775 | public: |
| 1776 | SymbolStack() { |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 1777 | mpArena = 0; |
| 1778 | mpTokenTable = 0; |
| 1779 | } |
| 1780 | |
| 1781 | void setArena(Arena* pArena) { |
| 1782 | mpArena = pArena; |
| 1783 | } |
| 1784 | |
| 1785 | void setTokenTable(TokenTable* pTokenTable) { |
| 1786 | mpTokenTable = pTokenTable; |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 1787 | } |
| 1788 | |
| 1789 | void pushLevel() { |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 1790 | Mark mark; |
| 1791 | mark.mArenaMark = mpArena->mark(); |
| 1792 | mark.mSymbolHead = mStack.size(); |
| 1793 | mLevelStack.push_back(mark); |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 1794 | } |
| 1795 | |
| 1796 | void popLevel() { |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 1797 | // Undo any shadowing that was done: |
| 1798 | Mark mark = mLevelStack.back(); |
| 1799 | mLevelStack.pop_back(); |
| 1800 | while (mStack.size() > mark.mSymbolHead) { |
| 1801 | VariableInfo* pV = mStack.back(); |
| 1802 | mStack.pop_back(); |
| 1803 | (*mpTokenTable)[pV->tok].mpVariableInfo = pV->pOldDefinition; |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 1804 | } |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 1805 | mpArena->freeToMark(mark.mArenaMark); |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 1806 | } |
| 1807 | |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 1808 | bool isDefinedAtCurrentLevel(tokenid_t tok) { |
| 1809 | VariableInfo* pV = (*mpTokenTable)[tok].mpVariableInfo; |
| 1810 | return pV && pV->level == level(); |
| 1811 | } |
| 1812 | |
| 1813 | VariableInfo* add(tokenid_t tok) { |
| 1814 | Token& token = (*mpTokenTable)[tok]; |
| 1815 | VariableInfo* pOldV = token.mpVariableInfo; |
| 1816 | VariableInfo* pNewV = |
| 1817 | (VariableInfo*) mpArena->alloc(sizeof(VariableInfo)); |
| 1818 | memset(pNewV, 0, sizeof(VariableInfo)); |
| 1819 | pNewV->tok = tok; |
| 1820 | pNewV->level = level(); |
| 1821 | pNewV->pOldDefinition = pOldV; |
| 1822 | token.mpVariableInfo = pNewV; |
| 1823 | mStack.push_back(pNewV); |
| 1824 | return pNewV; |
| 1825 | } |
| 1826 | |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 1827 | VariableInfo* add(Type* pType) { |
| 1828 | VariableInfo* pVI = add(pType->id); |
| 1829 | pVI->pType = pType; |
| 1830 | return pVI; |
| 1831 | } |
| 1832 | |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 1833 | void forEach(bool (*fn)(VariableInfo*, void*), void* context) { |
| 1834 | for (size_t i = 0; i < mStack.size(); i++) { |
| 1835 | if (! fn(mStack[i], context)) { |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 1836 | break; |
| 1837 | } |
| 1838 | } |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 1839 | } |
| 1840 | |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 1841 | private: |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 1842 | inline size_t level() { |
| 1843 | return mLevelStack.size(); |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 1844 | } |
| 1845 | |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 1846 | struct Mark { |
| 1847 | Arena::Mark mArenaMark; |
| 1848 | size_t mSymbolHead; |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 1849 | }; |
| 1850 | |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 1851 | Arena* mpArena; |
| 1852 | TokenTable* mpTokenTable; |
| 1853 | Vector<VariableInfo*> mStack; |
| 1854 | Vector<Mark> mLevelStack; |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 1855 | }; |
Jack Palevich | 36d9414 | 2009-06-08 15:55:32 -0700 | [diff] [blame] | 1856 | |
| 1857 | int ch; // Current input character, or EOF |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 1858 | tokenid_t tok; // token |
Jack Palevich | 36d9414 | 2009-06-08 15:55:32 -0700 | [diff] [blame] | 1859 | intptr_t tokc; // token extra info |
| 1860 | int tokl; // token operator level |
| 1861 | intptr_t rsym; // return symbol |
| 1862 | intptr_t loc; // local variable index |
| 1863 | char* glo; // global variable index |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 1864 | String mTokenString; |
Jack Palevich | 36d9414 | 2009-06-08 15:55:32 -0700 | [diff] [blame] | 1865 | char* dptr; // Macro state: Points to macro text during macro playback. |
| 1866 | int dch; // Macro state: Saves old value of ch during a macro playback. |
Jack Palevich | 36d9414 | 2009-06-08 15:55:32 -0700 | [diff] [blame] | 1867 | char* pGlobalBase; |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 1868 | |
| 1869 | // Arena for the duration of the compile |
| 1870 | Arena mGlobalArena; |
| 1871 | // Arena for data that's only needed when compiling a single function |
| 1872 | Arena mLocalArena; |
| 1873 | |
| 1874 | TokenTable mTokenTable; |
| 1875 | SymbolStack mGlobals; |
| 1876 | SymbolStack mLocals; |
| 1877 | |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 1878 | // Prebuilt types, makes things slightly faster. |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 1879 | Type* mkpInt; |
| 1880 | Type* mkpChar; |
| 1881 | Type* mkpVoid; |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 1882 | Type* mkpFloat; |
| 1883 | Type* mkpDouble; |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 1884 | Type* mkpIntPtr; |
| 1885 | Type* mkpCharPtr; |
| 1886 | Type* mkpPtrIntFn; |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 1887 | |
Jack Palevich | 36d9414 | 2009-06-08 15:55:32 -0700 | [diff] [blame] | 1888 | InputStream* file; |
| 1889 | |
| 1890 | CodeBuf codeBuf; |
| 1891 | CodeGenerator* pGen; |
| 1892 | |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 1893 | String mErrorBuf; |
| 1894 | |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 1895 | String mPragmas; |
| 1896 | int mPragmaStringCount; |
| 1897 | |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 1898 | static const int ALLOC_SIZE = 99999; |
| 1899 | |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 1900 | static const int TOK_DUMMY = 1; |
| 1901 | static const int TOK_NUM = 2; |
| 1902 | |
| 1903 | // 3..255 are character and/or operators |
| 1904 | |
Jack Palevich | 2db168f | 2009-06-11 14:29:47 -0700 | [diff] [blame] | 1905 | // Keywords start at 0x100 and increase by 1 |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 1906 | // Order has to match string list in "internKeywords". |
| 1907 | enum { |
| 1908 | TOK_KEYWORD = TokenTable::TOKEN_BASE, |
| 1909 | TOK_INT = TOK_KEYWORD, |
| 1910 | TOK_CHAR, |
| 1911 | TOK_VOID, |
| 1912 | TOK_IF, |
| 1913 | TOK_ELSE, |
| 1914 | TOK_WHILE, |
| 1915 | TOK_BREAK, |
| 1916 | TOK_RETURN, |
| 1917 | TOK_FOR, |
| 1918 | TOK_PRAGMA, |
| 1919 | TOK_DEFINE, |
| 1920 | TOK_AUTO, |
| 1921 | TOK_CASE, |
| 1922 | TOK_CONST, |
| 1923 | TOK_CONTINUE, |
| 1924 | TOK_DEFAULT, |
| 1925 | TOK_DO, |
| 1926 | TOK_DOUBLE, |
| 1927 | TOK_ENUM, |
| 1928 | TOK_EXTERN, |
| 1929 | TOK_FLOAT, |
| 1930 | TOK_GOTO, |
| 1931 | TOK_LONG, |
| 1932 | TOK_REGISTER, |
| 1933 | TOK_SHORT, |
| 1934 | TOK_SIGNED, |
| 1935 | TOK_SIZEOF, |
| 1936 | TOK_STATIC, |
| 1937 | TOK_STRUCT, |
| 1938 | TOK_SWITCH, |
| 1939 | TOK_TYPEDEF, |
| 1940 | TOK_UNION, |
| 1941 | TOK_UNSIGNED, |
| 1942 | TOK_VOLATILE, |
| 1943 | TOK__BOOL, |
| 1944 | TOK__COMPLEX, |
| 1945 | TOK__IMAGINARY, |
| 1946 | TOK_INLINE, |
| 1947 | TOK_RESTRICT, |
| 1948 | // Symbols start after tokens |
| 1949 | TOK_SYMBOL |
| 1950 | }; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 1951 | |
| 1952 | static const int LOCAL = 0x200; |
| 1953 | |
| 1954 | static const int SYM_FORWARD = 0; |
| 1955 | static const int SYM_DEFINE = 1; |
| 1956 | |
| 1957 | /* tokens in string heap */ |
| 1958 | static const int TAG_TOK = ' '; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 1959 | |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 1960 | static const int OP_INCREMENT = 0; |
| 1961 | static const int OP_DECREMENT = 1; |
| 1962 | static const int OP_MUL = 2; |
| 1963 | static const int OP_DIV = 3; |
| 1964 | static const int OP_MOD = 4; |
| 1965 | static const int OP_PLUS = 5; |
| 1966 | static const int OP_MINUS = 6; |
| 1967 | static const int OP_SHIFT_LEFT = 7; |
| 1968 | static const int OP_SHIFT_RIGHT = 8; |
| 1969 | static const int OP_LESS_EQUAL = 9; |
| 1970 | static const int OP_GREATER_EQUAL = 10; |
| 1971 | static const int OP_LESS = 11; |
| 1972 | static const int OP_GREATER = 12; |
| 1973 | static const int OP_EQUALS = 13; |
| 1974 | static const int OP_NOT_EQUALS = 14; |
| 1975 | static const int OP_LOGICAL_AND = 15; |
| 1976 | static const int OP_LOGICAL_OR = 16; |
| 1977 | static const int OP_BIT_AND = 17; |
| 1978 | static const int OP_BIT_XOR = 18; |
| 1979 | static const int OP_BIT_OR = 19; |
| 1980 | static const int OP_BIT_NOT = 20; |
| 1981 | static const int OP_LOGICAL_NOT = 21; |
| 1982 | static const int OP_COUNT = 22; |
| 1983 | |
| 1984 | /* Operators are searched from front, the two-character operators appear |
| 1985 | * before the single-character operators with the same first character. |
| 1986 | * @ is used to pad out single-character operators. |
| 1987 | */ |
| 1988 | static const char* operatorChars; |
| 1989 | static const char operatorLevel[]; |
| 1990 | |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 1991 | /* Called when we detect an internal problem. Does nothing in production. |
| 1992 | * |
| 1993 | */ |
| 1994 | void internalError() { |
| 1995 | * (char*) 0 = 0; |
| 1996 | } |
| 1997 | |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 1998 | void assert(bool isTrue) { |
| 1999 | if (!isTrue) { |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 2000 | internalError(); |
| 2001 | } |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 2002 | } |
| 2003 | |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 2004 | bool isSymbol(tokenid_t t) { |
| 2005 | return t >= TOK_SYMBOL && |
| 2006 | ((size_t) (t-TOK_SYMBOL)) < mTokenTable.size(); |
| 2007 | } |
| 2008 | |
| 2009 | bool isSymbolOrKeyword(tokenid_t t) { |
| 2010 | return t >= TOK_KEYWORD && |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 2011 | ((size_t) (t-TOK_KEYWORD)) < mTokenTable.size(); |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 2012 | } |
| 2013 | |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 2014 | VariableInfo* VI(tokenid_t t) { |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 2015 | assert(isSymbol(t)); |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 2016 | VariableInfo* pV = mTokenTable[t].mpVariableInfo; |
| 2017 | if (pV && pV->tok != t) { |
| 2018 | internalError(); |
| 2019 | } |
| 2020 | return pV; |
| 2021 | } |
| 2022 | |
| 2023 | inline bool isDefined(tokenid_t t) { |
| 2024 | return t >= TOK_SYMBOL && VI(t) != 0; |
| 2025 | } |
| 2026 | |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 2027 | const char* nameof(tokenid_t t) { |
| 2028 | assert(isSymbolOrKeyword(t)); |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 2029 | return mTokenTable[t].pText; |
| 2030 | } |
| 2031 | |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2032 | void pdef(int t) { |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 2033 | mTokenString.append(t); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2034 | } |
| 2035 | |
| 2036 | void inp() { |
| 2037 | if (dptr) { |
Jack Palevich | 653f42d | 2009-05-28 17:15:32 -0700 | [diff] [blame] | 2038 | ch = *dptr++; |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 2039 | if (ch == 0) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2040 | dptr = 0; |
| 2041 | ch = dch; |
| 2042 | } |
| 2043 | } else |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 2044 | ch = file->getChar(); |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 2045 | #if 0 |
| 2046 | printf("ch='%c' 0x%x\n", ch, ch); |
| 2047 | #endif |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2048 | } |
| 2049 | |
| 2050 | int isid() { |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 2051 | return isalnum(ch) | (ch == '_'); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2052 | } |
| 2053 | |
Jack Palevich | b4758ff | 2009-06-12 12:49:14 -0700 | [diff] [blame] | 2054 | /* read a character constant, advances ch to after end of constant */ |
| 2055 | int getq() { |
| 2056 | int val = ch; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2057 | if (ch == '\\') { |
| 2058 | inp(); |
Jack Palevich | b4758ff | 2009-06-12 12:49:14 -0700 | [diff] [blame] | 2059 | if (isoctal(ch)) { |
| 2060 | // 1 to 3 octal characters. |
| 2061 | val = 0; |
| 2062 | for(int i = 0; i < 3; i++) { |
| 2063 | if (isoctal(ch)) { |
| 2064 | val = (val << 3) + ch - '0'; |
| 2065 | inp(); |
| 2066 | } |
| 2067 | } |
| 2068 | return val; |
| 2069 | } else if (ch == 'x' || ch == 'X') { |
| 2070 | // N hex chars |
| 2071 | inp(); |
| 2072 | if (! isxdigit(ch)) { |
| 2073 | error("'x' character escape requires at least one digit."); |
| 2074 | } else { |
| 2075 | val = 0; |
| 2076 | while (isxdigit(ch)) { |
| 2077 | int d = ch; |
| 2078 | if (isdigit(d)) { |
| 2079 | d -= '0'; |
| 2080 | } else if (d <= 'F') { |
| 2081 | d = d - 'A' + 10; |
| 2082 | } else { |
| 2083 | d = d - 'a' + 10; |
| 2084 | } |
| 2085 | val = (val << 4) + d; |
| 2086 | inp(); |
| 2087 | } |
| 2088 | } |
| 2089 | } else { |
| 2090 | int val = ch; |
| 2091 | switch (ch) { |
| 2092 | case 'a': |
| 2093 | val = '\a'; |
| 2094 | break; |
| 2095 | case 'b': |
| 2096 | val = '\b'; |
| 2097 | break; |
| 2098 | case 'f': |
| 2099 | val = '\f'; |
| 2100 | break; |
| 2101 | case 'n': |
| 2102 | val = '\n'; |
| 2103 | break; |
| 2104 | case 'r': |
| 2105 | val = '\r'; |
| 2106 | break; |
| 2107 | case 't': |
| 2108 | val = '\t'; |
| 2109 | break; |
| 2110 | case 'v': |
| 2111 | val = '\v'; |
| 2112 | break; |
| 2113 | case '\\': |
| 2114 | val = '\\'; |
| 2115 | break; |
| 2116 | case '\'': |
| 2117 | val = '\''; |
| 2118 | break; |
| 2119 | case '"': |
| 2120 | val = '"'; |
| 2121 | break; |
| 2122 | case '?': |
| 2123 | val = '?'; |
| 2124 | break; |
| 2125 | default: |
| 2126 | error("Undefined character escape %c", ch); |
| 2127 | break; |
| 2128 | } |
| 2129 | inp(); |
| 2130 | return val; |
| 2131 | } |
| 2132 | } else { |
| 2133 | inp(); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2134 | } |
Jack Palevich | b4758ff | 2009-06-12 12:49:14 -0700 | [diff] [blame] | 2135 | return val; |
| 2136 | } |
| 2137 | |
| 2138 | static bool isoctal(int ch) { |
| 2139 | return ch >= '0' && ch <= '7'; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2140 | } |
| 2141 | |
| 2142 | void next() { |
| 2143 | int l, a; |
| 2144 | |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 2145 | while (isspace(ch) | (ch == '#')) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2146 | if (ch == '#') { |
| 2147 | inp(); |
| 2148 | next(); |
| 2149 | if (tok == TOK_DEFINE) { |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 2150 | doDefine(); |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 2151 | } else if (tok == TOK_PRAGMA) { |
| 2152 | doPragma(); |
| 2153 | } else { |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 2154 | error("Unsupported preprocessor directive \"%s\"", |
| 2155 | mTokenString.getUnwrapped()); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2156 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2157 | } |
| 2158 | inp(); |
| 2159 | } |
| 2160 | tokl = 0; |
| 2161 | tok = ch; |
| 2162 | /* encode identifiers & numbers */ |
| 2163 | if (isid()) { |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 2164 | mTokenString.clear(); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2165 | while (isid()) { |
Jack Palevich | e27bf3e | 2009-05-10 14:09:03 -0700 | [diff] [blame] | 2166 | pdef(ch); |
| 2167 | inp(); |
Jack Palevich | ae54f1f | 2009-05-08 14:54:15 -0700 | [diff] [blame] | 2168 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2169 | if (isdigit(tok)) { |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 2170 | tokc = strtol(mTokenString.getUnwrapped(), 0, 0); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2171 | tok = TOK_NUM; |
Jack Palevich | e27bf3e | 2009-05-10 14:09:03 -0700 | [diff] [blame] | 2172 | } else { |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 2173 | tok = mTokenTable.intern(mTokenString.getUnwrapped(), |
| 2174 | mTokenString.len()); |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 2175 | // Is this a macro? |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 2176 | char* pMacroDefinition = mTokenTable[tok].mpMacroDefinition; |
| 2177 | if(pMacroDefinition) { |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 2178 | // Yes, it is a macro |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 2179 | dptr = pMacroDefinition; |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 2180 | dch = ch; |
| 2181 | inp(); |
| 2182 | next(); |
Jack Palevich | e27bf3e | 2009-05-10 14:09:03 -0700 | [diff] [blame] | 2183 | } |
| 2184 | } |
Jack Palevich | e27bf3e | 2009-05-10 14:09:03 -0700 | [diff] [blame] | 2185 | } else { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2186 | inp(); |
| 2187 | if (tok == '\'') { |
| 2188 | tok = TOK_NUM; |
Jack Palevich | b4758ff | 2009-06-12 12:49:14 -0700 | [diff] [blame] | 2189 | tokc = getq(); |
| 2190 | if (ch != '\'') { |
| 2191 | error("Expected a ' character, got %c", ch); |
| 2192 | } else { |
| 2193 | inp(); |
| 2194 | } |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 2195 | } else if ((tok == '/') & (ch == '*')) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2196 | inp(); |
Jack Palevich | 22e3e8e | 2009-06-12 13:12:55 -0700 | [diff] [blame] | 2197 | while (ch && ch != EOF) { |
| 2198 | while (ch != '*' && ch != EOF) |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2199 | inp(); |
| 2200 | inp(); |
| 2201 | if (ch == '/') |
| 2202 | ch = 0; |
Jack Palevich | e27bf3e | 2009-05-10 14:09:03 -0700 | [diff] [blame] | 2203 | } |
Jack Palevich | 22e3e8e | 2009-06-12 13:12:55 -0700 | [diff] [blame] | 2204 | if (ch == EOF) { |
| 2205 | error("End of file inside comment."); |
| 2206 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2207 | inp(); |
Jack Palevich | e27bf3e | 2009-05-10 14:09:03 -0700 | [diff] [blame] | 2208 | next(); |
Jack Palevich | bd89490 | 2009-05-14 19:35:31 -0700 | [diff] [blame] | 2209 | } else if ((tok == '/') & (ch == '/')) { |
| 2210 | inp(); |
Jack Palevich | 22e3e8e | 2009-06-12 13:12:55 -0700 | [diff] [blame] | 2211 | while (ch && (ch != '\n') && (ch != EOF)) { |
Jack Palevich | bd89490 | 2009-05-14 19:35:31 -0700 | [diff] [blame] | 2212 | inp(); |
| 2213 | } |
| 2214 | inp(); |
| 2215 | next(); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2216 | } else { |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 2217 | const char* t = operatorChars; |
| 2218 | int opIndex = 0; |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 2219 | while ((l = *t++) != 0) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2220 | a = *t++; |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 2221 | tokl = operatorLevel[opIndex]; |
| 2222 | tokc = opIndex; |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 2223 | if ((l == tok) & ((a == ch) | (a == '@'))) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2224 | #if 0 |
| 2225 | printf("%c%c -> tokl=%d tokc=0x%x\n", |
| 2226 | l, a, tokl, tokc); |
| 2227 | #endif |
| 2228 | if (a == ch) { |
| 2229 | inp(); |
| 2230 | tok = TOK_DUMMY; /* dummy token for double tokens */ |
| 2231 | } |
| 2232 | break; |
| 2233 | } |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 2234 | opIndex++; |
| 2235 | } |
| 2236 | if (l == 0) { |
| 2237 | tokl = 0; |
| 2238 | tokc = 0; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2239 | } |
| 2240 | } |
| 2241 | } |
| 2242 | #if 0 |
| 2243 | { |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 2244 | String buf; |
| 2245 | decodeToken(buf, tok); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 2246 | fprintf(stderr, "%s\n", buf.getUnwrapped()); |
| 2247 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2248 | #endif |
| 2249 | } |
| 2250 | |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 2251 | void doDefine() { |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 2252 | next(); |
| 2253 | tokenid_t name = tok; |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 2254 | String* pName = new String(); |
| 2255 | while (isspace(ch)) { |
| 2256 | inp(); |
| 2257 | } |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 2258 | if (ch == '(') { |
| 2259 | delete pName; |
| 2260 | error("Defines with arguments not supported"); |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 2261 | return; |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 2262 | } |
| 2263 | while (isspace(ch)) { |
| 2264 | inp(); |
| 2265 | } |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 2266 | String value; |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 2267 | while (ch != '\n' && ch != EOF) { |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 2268 | value.append(ch); |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 2269 | inp(); |
| 2270 | } |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 2271 | char* pDefn = (char*)mGlobalArena.alloc(value.len() + 1); |
| 2272 | memcpy(pDefn, value.getUnwrapped(), value.len()); |
| 2273 | pDefn[value.len()] = 0; |
| 2274 | mTokenTable[name].mpMacroDefinition = pDefn; |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 2275 | } |
| 2276 | |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 2277 | void doPragma() { |
| 2278 | // # pragma name(val) |
| 2279 | int state = 0; |
| 2280 | while(ch != EOF && ch != '\n' && state < 10) { |
| 2281 | switch(state) { |
| 2282 | case 0: |
| 2283 | if (isspace(ch)) { |
| 2284 | inp(); |
| 2285 | } else { |
| 2286 | state++; |
| 2287 | } |
| 2288 | break; |
| 2289 | case 1: |
| 2290 | if (isalnum(ch)) { |
| 2291 | mPragmas.append(ch); |
| 2292 | inp(); |
| 2293 | } else if (ch == '(') { |
| 2294 | mPragmas.append(0); |
| 2295 | inp(); |
| 2296 | state++; |
| 2297 | } else { |
| 2298 | state = 11; |
| 2299 | } |
| 2300 | break; |
| 2301 | case 2: |
| 2302 | if (isalnum(ch)) { |
| 2303 | mPragmas.append(ch); |
| 2304 | inp(); |
| 2305 | } else if (ch == ')') { |
| 2306 | mPragmas.append(0); |
| 2307 | inp(); |
| 2308 | state = 10; |
| 2309 | } else { |
| 2310 | state = 11; |
| 2311 | } |
| 2312 | break; |
| 2313 | } |
| 2314 | } |
| 2315 | if(state != 10) { |
| 2316 | error("Unexpected pragma syntax"); |
| 2317 | } |
| 2318 | mPragmaStringCount += 2; |
| 2319 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2320 | |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 2321 | virtual void verror(const char* fmt, va_list ap) { |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 2322 | mErrorBuf.printf("%ld: ", file->getLine()); |
| 2323 | mErrorBuf.vprintf(fmt, ap); |
| 2324 | mErrorBuf.printf("\n"); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2325 | } |
| 2326 | |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 2327 | void skip(intptr_t c) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2328 | if (tok != c) { |
| 2329 | error("'%c' expected", c); |
| 2330 | } |
| 2331 | next(); |
| 2332 | } |
| 2333 | |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 2334 | bool accept(intptr_t c) { |
| 2335 | if (tok == c) { |
| 2336 | next(); |
| 2337 | return true; |
| 2338 | } |
| 2339 | return false; |
| 2340 | } |
| 2341 | |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 2342 | bool acceptStringLiteral() { |
| 2343 | if (tok == '"') { |
Jack Palevich | 653f42d | 2009-05-28 17:15:32 -0700 | [diff] [blame] | 2344 | pGen->li((int) glo); |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 2345 | // This while loop merges multiple adjacent string constants. |
| 2346 | while (tok == '"') { |
| 2347 | while (ch != '"' && ch != EOF) { |
| 2348 | *allocGlobalSpace(1) = getq(); |
| 2349 | } |
| 2350 | if (ch != '"') { |
| 2351 | error("Unterminated string constant."); |
| 2352 | } |
| 2353 | inp(); |
| 2354 | next(); |
Jack Palevich | b4758ff | 2009-06-12 12:49:14 -0700 | [diff] [blame] | 2355 | } |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 2356 | /* Null terminate */ |
Jack Palevich | 653f42d | 2009-05-28 17:15:32 -0700 | [diff] [blame] | 2357 | *glo = 0; |
Jack Palevich | f1f39cc | 2009-05-29 18:03:15 -0700 | [diff] [blame] | 2358 | /* align heap */ |
| 2359 | allocGlobalSpace((char*) (((intptr_t) glo + 4) & -4) - glo); |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 2360 | |
| 2361 | return true; |
| 2362 | } |
| 2363 | return false; |
| 2364 | } |
| 2365 | /* Parse and evaluate a unary expression. |
| 2366 | * allowAssignment is true if '=' parsing wanted (quick hack) |
| 2367 | */ |
| 2368 | void unary(bool allowAssignment) { |
| 2369 | intptr_t n, t, a; |
| 2370 | t = 0; |
| 2371 | n = 1; /* type of expression 0 = forward, 1 = value, other = lvalue */ |
| 2372 | if (acceptStringLiteral()) { |
| 2373 | // Nothing else to do. |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2374 | } else { |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 2375 | int c = tokl; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2376 | a = tokc; |
| 2377 | t = tok; |
| 2378 | next(); |
| 2379 | if (t == TOK_NUM) { |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 2380 | pGen->li(a); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2381 | } else if (c == 2) { |
| 2382 | /* -, +, !, ~ */ |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 2383 | unary(false); |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 2384 | pGen->clearR1(); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2385 | if (t == '!') |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 2386 | pGen->gcmp(a); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2387 | else |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 2388 | pGen->genOp(a); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2389 | } else if (t == '(') { |
| 2390 | expr(); |
| 2391 | skip(')'); |
| 2392 | } else if (t == '*') { |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 2393 | /* This is a pointer dereference, but we currently only |
| 2394 | * support a pointer dereference if it's immediately |
| 2395 | * in front of a cast. So parse the cast right here. |
| 2396 | */ |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2397 | skip('('); |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 2398 | Type* pCast = expectCastTypeDeclaration(mLocalArena); |
| 2399 | // We currently only handle 3 types of cast: |
| 2400 | // (int*), (char*) , (int (*)()) |
| 2401 | if(typeEqual(pCast, mkpIntPtr)) { |
| 2402 | t = TOK_INT; |
| 2403 | } else if (typeEqual(pCast, mkpCharPtr)) { |
| 2404 | t = TOK_CHAR; |
| 2405 | } else if (typeEqual(pCast, mkpPtrIntFn)){ |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2406 | t = 0; |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 2407 | } else { |
| 2408 | String buffer; |
| 2409 | decodeType(buffer, pCast); |
| 2410 | error("Unsupported cast type %s", buffer.getUnwrapped()); |
| 2411 | decodeType(buffer, mkpPtrIntFn); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2412 | } |
| 2413 | skip(')'); |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 2414 | unary(false); |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 2415 | if (accept('=')) { |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 2416 | pGen->pushR0(); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2417 | expr(); |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 2418 | pGen->popR1(); |
| 2419 | pGen->storeR0ToR1(t == TOK_INT); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2420 | } else if (t) { |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 2421 | pGen->loadR0FromR0(t == TOK_INT); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2422 | } |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 2423 | // Else we fall through to the function call below, with |
| 2424 | // t == 0 to trigger an indirect function call. Hack! |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2425 | } else if (t == '&') { |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 2426 | pGen->leaR0((int) VI(tok)->pAddress); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2427 | next(); |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 2428 | } else if (t == EOF ) { |
| 2429 | error("Unexpected EOF."); |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 2430 | } else if (!checkSymbol(t)) { |
Jack Palevich | a1804dd | 2009-06-12 14:40:04 -0700 | [diff] [blame] | 2431 | // Don't have to do anything special here, the error |
| 2432 | // message was printed by checkSymbol() above. |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2433 | } else { |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 2434 | if (!isDefined(t)) { |
| 2435 | mGlobals.add(t); |
| 2436 | // printf("Adding new global function %s\n", nameof(t)); |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 2437 | } |
| 2438 | |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 2439 | n = (intptr_t) VI(t)->pAddress; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2440 | /* forward reference: try dlsym */ |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 2441 | if (!n) { |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 2442 | n = (intptr_t) dlsym(RTLD_DEFAULT, nameof(t)); |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 2443 | VI(t)->pAddress = (void*) n; |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 2444 | } |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 2445 | if ((tok == '=') & allowAssignment) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2446 | /* assignment */ |
| 2447 | next(); |
| 2448 | expr(); |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 2449 | pGen->storeR0(n); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2450 | } else if (tok != '(') { |
| 2451 | /* variable */ |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 2452 | if (!n) { |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 2453 | error("Undefined variable %s", nameof(t)); |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 2454 | } |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 2455 | pGen->loadR0(n, tokl == 11, tokc); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2456 | if (tokl == 11) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2457 | next(); |
| 2458 | } |
| 2459 | } |
| 2460 | } |
| 2461 | } |
| 2462 | |
| 2463 | /* function call */ |
| 2464 | if (tok == '(') { |
| 2465 | if (n == 1) |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 2466 | pGen->pushR0(); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2467 | |
| 2468 | /* push args and invert order */ |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 2469 | a = pGen->beginFunctionCallArguments(); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2470 | next(); |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 2471 | int l = 0; |
Jack Palevich | b4758ff | 2009-06-12 12:49:14 -0700 | [diff] [blame] | 2472 | while (tok != ')' && tok != EOF) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2473 | expr(); |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 2474 | pGen->storeR0ToArg(l); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2475 | l = l + 4; |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 2476 | if (accept(',')) { |
| 2477 | // fine |
| 2478 | } else if ( tok != ')') { |
| 2479 | error("Expected ',' or ')'"); |
| 2480 | } |
Jack Palevich | e27bf3e | 2009-05-10 14:09:03 -0700 | [diff] [blame] | 2481 | } |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 2482 | pGen->endFunctionCallArguments(a, l); |
Jack Palevich | b4758ff | 2009-06-12 12:49:14 -0700 | [diff] [blame] | 2483 | skip(')'); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2484 | if (!n) { |
| 2485 | /* forward reference */ |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 2486 | VariableInfo* pVI = VI(t); |
| 2487 | pVI->pForward = (void*) pGen->callForward((int) pVI->pForward); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2488 | } else if (n == 1) { |
| 2489 | pGen->callIndirect(l); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2490 | } else { |
Jack Palevich | 7810bc9 | 2009-05-15 14:31:47 -0700 | [diff] [blame] | 2491 | pGen->callRelative(n - codeBuf.getPC() - pGen->jumpOffset()); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2492 | } |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 2493 | pGen->adjustStackAfterCall(l, n == 1); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2494 | } |
| 2495 | } |
| 2496 | |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 2497 | /* Recursive descent parser for binary operations. |
| 2498 | */ |
| 2499 | void binaryOp(int level) { |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 2500 | intptr_t t, n, a; |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 2501 | t = 0; |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 2502 | if (level-- == 1) |
| 2503 | unary(true); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2504 | else { |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 2505 | binaryOp(level); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2506 | a = 0; |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 2507 | while (level == tokl) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2508 | n = tok; |
| 2509 | t = tokc; |
| 2510 | next(); |
| 2511 | |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 2512 | if (level > 8) { |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 2513 | a = pGen->gtst(t == OP_LOGICAL_OR, a); /* && and || output code generation */ |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 2514 | binaryOp(level); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2515 | } else { |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 2516 | pGen->pushR0(); |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 2517 | binaryOp(level); |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 2518 | pGen->popR1(); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2519 | |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 2520 | if ((level == 4) | (level == 5)) { |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 2521 | pGen->gcmp(t); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2522 | } else { |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 2523 | pGen->genOp(t); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2524 | } |
| 2525 | } |
| 2526 | } |
| 2527 | /* && and || output code generation */ |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 2528 | if (a && level > 8) { |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 2529 | a = pGen->gtst(t == OP_LOGICAL_OR, a); |
| 2530 | pGen->li(t != OP_LOGICAL_OR); |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 2531 | pGen->gjmp(5); /* jmp $ + 5 (sizeof li, FIXME for ARM) */ |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 2532 | pGen->gsym(a); |
| 2533 | pGen->li(t == OP_LOGICAL_OR); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2534 | } |
| 2535 | } |
| 2536 | } |
| 2537 | |
| 2538 | void expr() { |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 2539 | binaryOp(11); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2540 | } |
| 2541 | |
| 2542 | int test_expr() { |
| 2543 | expr(); |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 2544 | return pGen->gtst(0, 0); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2545 | } |
| 2546 | |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 2547 | void block(intptr_t l, bool outermostFunctionBlock) { |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 2548 | intptr_t a, n, t; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2549 | |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 2550 | Type* pBaseType; |
| 2551 | if ((pBaseType = acceptPrimitiveType(mLocalArena))) { |
Jack Palevich | a1804dd | 2009-06-12 14:40:04 -0700 | [diff] [blame] | 2552 | /* declarations */ |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 2553 | localDeclarations(pBaseType); |
Jack Palevich | a1804dd | 2009-06-12 14:40:04 -0700 | [diff] [blame] | 2554 | } else if (tok == TOK_IF) { |
Jack Palevich | e27bf3e | 2009-05-10 14:09:03 -0700 | [diff] [blame] | 2555 | next(); |
| 2556 | skip('('); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2557 | a = test_expr(); |
| 2558 | skip(')'); |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 2559 | block(l, false); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2560 | if (tok == TOK_ELSE) { |
Jack Palevich | e27bf3e | 2009-05-10 14:09:03 -0700 | [diff] [blame] | 2561 | next(); |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 2562 | n = pGen->gjmp(0); /* jmp */ |
| 2563 | pGen->gsym(a); |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 2564 | block(l, false); |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 2565 | pGen->gsym(n); /* patch else jmp */ |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2566 | } else { |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 2567 | pGen->gsym(a); /* patch if test */ |
Jack Palevich | e27bf3e | 2009-05-10 14:09:03 -0700 | [diff] [blame] | 2568 | } |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 2569 | } else if ((tok == TOK_WHILE) | (tok == TOK_FOR)) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2570 | t = tok; |
| 2571 | next(); |
| 2572 | skip('('); |
| 2573 | if (t == TOK_WHILE) { |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 2574 | n = codeBuf.getPC(); // top of loop, target of "next" iteration |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2575 | a = test_expr(); |
| 2576 | } else { |
| 2577 | if (tok != ';') |
| 2578 | expr(); |
| 2579 | skip(';'); |
| 2580 | n = codeBuf.getPC(); |
| 2581 | a = 0; |
| 2582 | if (tok != ';') |
| 2583 | a = test_expr(); |
| 2584 | skip(';'); |
| 2585 | if (tok != ')') { |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 2586 | t = pGen->gjmp(0); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2587 | expr(); |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 2588 | pGen->gjmp(n - codeBuf.getPC() - pGen->jumpOffset()); |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 2589 | pGen->gsym(t); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2590 | n = t + 4; |
| 2591 | } |
| 2592 | } |
| 2593 | skip(')'); |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 2594 | block((intptr_t) &a, false); |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 2595 | pGen->gjmp(n - codeBuf.getPC() - pGen->jumpOffset()); /* jmp */ |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 2596 | pGen->gsym(a); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2597 | } else if (tok == '{') { |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 2598 | if (! outermostFunctionBlock) { |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 2599 | mLocals.pushLevel(); |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 2600 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2601 | next(); |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 2602 | while (tok != '}' && tok != EOF) |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 2603 | block(l, false); |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 2604 | skip('}'); |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 2605 | if (! outermostFunctionBlock) { |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 2606 | mLocals.popLevel(); |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 2607 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2608 | } else { |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 2609 | if (accept(TOK_RETURN)) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2610 | if (tok != ';') |
| 2611 | expr(); |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 2612 | rsym = pGen->gjmp(rsym); /* jmp */ |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 2613 | } else if (accept(TOK_BREAK)) { |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 2614 | *(int *) l = pGen->gjmp(*(int *) l); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2615 | } else if (tok != ';') |
| 2616 | expr(); |
| 2617 | skip(';'); |
Jack Palevich | e27bf3e | 2009-05-10 14:09:03 -0700 | [diff] [blame] | 2618 | } |
| 2619 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2620 | |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 2621 | enum TypeTag { |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 2622 | TY_INT, TY_CHAR, TY_VOID, TY_FLOAT, TY_DOUBLE, |
| 2623 | TY_POINTER, TY_FUNC, TY_PARAM |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 2624 | }; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2625 | |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 2626 | struct Type { |
| 2627 | TypeTag tag; |
| 2628 | tokenid_t id; // For function arguments |
| 2629 | Type* pHead; |
| 2630 | Type* pTail; |
| 2631 | }; |
| 2632 | |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 2633 | bool typeEqual(Type* a, Type* b) { |
| 2634 | if (a == b) { |
| 2635 | return true; |
| 2636 | } |
| 2637 | if (a == NULL || b == NULL) { |
| 2638 | return false; |
| 2639 | } |
| 2640 | TypeTag at = a->tag; |
| 2641 | if (at != b->tag) { |
| 2642 | return false; |
| 2643 | } |
| 2644 | if (at == TY_POINTER) { |
| 2645 | return typeEqual(a->pHead, b->pHead); |
| 2646 | } else if (at == TY_FUNC || at == TY_PARAM) { |
| 2647 | return typeEqual(a->pHead, b->pHead) |
| 2648 | && typeEqual(a->pTail, b->pTail); |
| 2649 | } |
| 2650 | return true; |
| 2651 | } |
| 2652 | |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 2653 | Type* createType(TypeTag tag, Type* pHead, Type* pTail, Arena& arena) { |
| 2654 | assert(tag >= TY_INT && tag <= TY_PARAM); |
| 2655 | Type* pType = (Type*) arena.alloc(sizeof(Type)); |
| 2656 | memset(pType, 0, sizeof(*pType)); |
| 2657 | pType->tag = tag; |
| 2658 | pType->pHead = pHead; |
| 2659 | pType->pTail = pTail; |
| 2660 | return pType; |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 2661 | } |
| 2662 | |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 2663 | Type* createPtrType(Type* pType, Arena& arena) { |
| 2664 | return createType(TY_POINTER, pType, NULL, arena); |
| 2665 | } |
| 2666 | |
| 2667 | /** |
| 2668 | * Try to print a type in declaration order |
| 2669 | */ |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 2670 | void decodeType(String& buffer, Type* pType) { |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 2671 | buffer.clear(); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 2672 | if (pType == NULL) { |
| 2673 | buffer.appendCStr("null"); |
| 2674 | return; |
| 2675 | } |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 2676 | decodeTypeImp(buffer, pType); |
| 2677 | } |
| 2678 | |
| 2679 | void decodeTypeImp(String& buffer, Type* pType) { |
| 2680 | decodeTypeImpPrefix(buffer, pType); |
| 2681 | |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 2682 | String temp; |
| 2683 | if (pType->id != 0) { |
| 2684 | decodeToken(temp, pType->id); |
| 2685 | buffer.append(temp); |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 2686 | } |
| 2687 | |
| 2688 | decodeTypeImpPostfix(buffer, pType); |
| 2689 | } |
| 2690 | |
| 2691 | void decodeTypeImpPrefix(String& buffer, Type* pType) { |
| 2692 | TypeTag tag = pType->tag; |
| 2693 | |
| 2694 | if (tag >= TY_INT && tag <= TY_VOID) { |
| 2695 | switch (tag) { |
| 2696 | case TY_INT: |
| 2697 | buffer.appendCStr("int"); |
| 2698 | break; |
| 2699 | case TY_CHAR: |
| 2700 | buffer.appendCStr("char"); |
| 2701 | break; |
| 2702 | case TY_VOID: |
| 2703 | buffer.appendCStr("void"); |
| 2704 | break; |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 2705 | case TY_FLOAT: |
| 2706 | buffer.appendCStr("float"); |
| 2707 | break; |
| 2708 | case TY_DOUBLE: |
| 2709 | buffer.appendCStr("double"); |
| 2710 | break; |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 2711 | default: |
| 2712 | break; |
| 2713 | } |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 2714 | buffer.append(' '); |
| 2715 | } |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 2716 | |
| 2717 | switch (tag) { |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 2718 | case TY_INT: |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 2719 | break; |
| 2720 | case TY_CHAR: |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 2721 | break; |
| 2722 | case TY_VOID: |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 2723 | break; |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 2724 | case TY_FLOAT: |
| 2725 | break; |
| 2726 | case TY_DOUBLE: |
| 2727 | break; |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 2728 | case TY_POINTER: |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 2729 | decodeTypeImpPrefix(buffer, pType->pHead); |
| 2730 | if(pType->pHead && pType->pHead->tag == TY_FUNC) { |
| 2731 | buffer.append('('); |
| 2732 | } |
| 2733 | buffer.append('*'); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 2734 | break; |
| 2735 | case TY_FUNC: |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 2736 | decodeTypeImp(buffer, pType->pHead); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 2737 | break; |
| 2738 | case TY_PARAM: |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 2739 | decodeTypeImp(buffer, pType->pHead); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 2740 | break; |
| 2741 | default: |
| 2742 | String temp; |
| 2743 | temp.printf("Unknown tag %d", pType->tag); |
| 2744 | buffer.append(temp); |
| 2745 | break; |
| 2746 | } |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 2747 | } |
| 2748 | |
| 2749 | void decodeTypeImpPostfix(String& buffer, Type* pType) { |
| 2750 | TypeTag tag = pType->tag; |
| 2751 | |
| 2752 | switch(tag) { |
| 2753 | case TY_POINTER: |
| 2754 | if(pType->pHead && pType->pHead->tag == TY_FUNC) { |
| 2755 | buffer.append(')'); |
| 2756 | } |
| 2757 | decodeTypeImpPostfix(buffer, pType->pHead); |
| 2758 | break; |
| 2759 | case TY_FUNC: |
| 2760 | buffer.append('('); |
| 2761 | for(Type* pArg = pType->pTail; pArg; pArg = pArg->pTail) { |
| 2762 | decodeTypeImp(buffer, pArg); |
| 2763 | if (pArg->pTail) { |
| 2764 | buffer.appendCStr(", "); |
| 2765 | } |
| 2766 | } |
| 2767 | buffer.append(')'); |
| 2768 | break; |
| 2769 | default: |
| 2770 | break; |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 2771 | } |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 2772 | } |
| 2773 | |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 2774 | void printType(Type* pType) { |
| 2775 | String buffer; |
| 2776 | decodeType(buffer, pType); |
| 2777 | fprintf(stderr, "%s\n", buffer.getUnwrapped()); |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 2778 | } |
| 2779 | |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 2780 | Type* acceptPrimitiveType(Arena& arena) { |
| 2781 | Type* pType; |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 2782 | if (tok == TOK_INT) { |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 2783 | pType = mkpInt; |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 2784 | } else if (tok == TOK_CHAR) { |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 2785 | pType = mkpChar; |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 2786 | } else if (tok == TOK_VOID) { |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 2787 | pType = mkpVoid; |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 2788 | } else if (tok == TOK_FLOAT) { |
| 2789 | pType = mkpFloat; |
| 2790 | } else if (tok == TOK_DOUBLE) { |
| 2791 | pType = mkpDouble; |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 2792 | } else { |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 2793 | return NULL; |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 2794 | } |
| 2795 | next(); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 2796 | return pType; |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 2797 | } |
| 2798 | |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 2799 | Type* acceptDeclaration(Type* pType, bool nameAllowed, bool nameRequired, |
| 2800 | Arena& arena) { |
| 2801 | tokenid_t declName = 0; |
| 2802 | pType = acceptDecl2(pType, declName, nameAllowed, |
| 2803 | nameRequired, arena); |
| 2804 | if (declName) { |
| 2805 | // Clone the parent type so we can set a unique ID |
| 2806 | pType = createType(pType->tag, pType->pHead, |
| 2807 | pType->pTail, arena); |
| 2808 | |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 2809 | pType->id = declName; |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 2810 | } |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 2811 | // fprintf(stderr, "Parsed a declaration: "); |
| 2812 | // printType(pType); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 2813 | return pType; |
| 2814 | } |
| 2815 | |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 2816 | Type* expectDeclaration(Type* pBaseType, Arena& arena) { |
| 2817 | Type* pType = acceptDeclaration(pBaseType, true, true, arena); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 2818 | if (! pType) { |
| 2819 | error("Expected a declaration"); |
| 2820 | } |
| 2821 | return pType; |
| 2822 | } |
| 2823 | |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 2824 | /* Used for accepting types that appear in casts */ |
| 2825 | Type* acceptCastTypeDeclaration(Arena& arena) { |
| 2826 | Type* pType = acceptPrimitiveType(arena); |
| 2827 | if (pType) { |
| 2828 | pType = acceptDeclaration(pType, false, false, arena); |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 2829 | } |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 2830 | return pType; |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 2831 | } |
| 2832 | |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 2833 | Type* expectCastTypeDeclaration(Arena& arena) { |
| 2834 | Type* pType = acceptCastTypeDeclaration(arena); |
| 2835 | if (! pType) { |
| 2836 | error("Expected a declaration"); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 2837 | } |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 2838 | return pType; |
| 2839 | } |
| 2840 | |
| 2841 | Type* acceptDecl2(Type* pType, tokenid_t& declName, |
| 2842 | bool nameAllowed, bool nameRequired, Arena& arena) { |
| 2843 | int ptrCounter = 0; |
| 2844 | while (accept('*')) { |
| 2845 | ptrCounter++; |
| 2846 | } |
| 2847 | pType = acceptDecl3(pType, declName, nameAllowed, nameRequired, arena); |
| 2848 | while (ptrCounter-- > 0) { |
| 2849 | pType = createType(TY_POINTER, pType, NULL, arena); |
| 2850 | } |
| 2851 | return pType; |
| 2852 | } |
| 2853 | |
| 2854 | Type* acceptDecl3(Type* pType, tokenid_t& declName, |
| 2855 | bool nameAllowed, bool nameRequired, Arena& arena) { |
| 2856 | // direct-dcl : |
| 2857 | // name |
| 2858 | // (dcl) |
| 2859 | // direct-dcl() |
| 2860 | // direct-dcl[] |
| 2861 | Type* pNewHead = NULL; |
| 2862 | if (accept('(')) { |
| 2863 | pNewHead = acceptDecl2(pNewHead, declName, nameAllowed, |
| 2864 | nameRequired, arena); |
| 2865 | skip(')'); |
| 2866 | } else if ((declName = acceptSymbol()) != 0) { |
| 2867 | if (nameAllowed == false && declName) { |
| 2868 | error("Symbol %s not allowed here", nameof(declName)); |
| 2869 | } else if (nameRequired && ! declName) { |
| 2870 | String temp; |
| 2871 | decodeToken(temp, tok); |
| 2872 | error("Expected symbol. Got %s", temp.getUnwrapped()); |
| 2873 | } |
| 2874 | } |
| 2875 | while (accept('(')) { |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 2876 | // Function declaration |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 2877 | Type* pTail = acceptArgs(nameAllowed, arena); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 2878 | pType = createType(TY_FUNC, pType, pTail, arena); |
| 2879 | skip(')'); |
| 2880 | } |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 2881 | |
| 2882 | if (pNewHead) { |
| 2883 | Type* pA = pNewHead; |
| 2884 | while (pA->pHead) { |
| 2885 | pA = pA->pHead; |
| 2886 | } |
| 2887 | pA->pHead = pType; |
| 2888 | pType = pNewHead; |
| 2889 | } |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 2890 | return pType; |
| 2891 | } |
| 2892 | |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 2893 | Type* acceptArgs(bool nameAllowed, Arena& arena) { |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 2894 | Type* pHead = NULL; |
| 2895 | Type* pTail = NULL; |
| 2896 | for(;;) { |
| 2897 | Type* pBaseArg = acceptPrimitiveType(arena); |
| 2898 | if (pBaseArg) { |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 2899 | Type* pArg = acceptDeclaration(pBaseArg, nameAllowed, false, |
| 2900 | arena); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 2901 | if (pArg) { |
| 2902 | Type* pParam = createType(TY_PARAM, pArg, NULL, arena); |
| 2903 | if (!pHead) { |
| 2904 | pHead = pParam; |
| 2905 | pTail = pParam; |
| 2906 | } else { |
| 2907 | pTail->pTail = pParam; |
| 2908 | pTail = pParam; |
| 2909 | } |
| 2910 | } |
| 2911 | } |
| 2912 | if (! accept(',')) { |
| 2913 | break; |
| 2914 | } |
| 2915 | } |
| 2916 | return pHead; |
| 2917 | } |
| 2918 | |
| 2919 | Type* expectPrimitiveType(Arena& arena) { |
| 2920 | Type* pType = acceptPrimitiveType(arena); |
| 2921 | if (!pType) { |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 2922 | String buf; |
| 2923 | decodeToken(buf, tok); |
| 2924 | error("Expected a type, got %s", buf.getUnwrapped()); |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 2925 | } |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 2926 | return pType; |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 2927 | } |
| 2928 | |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 2929 | void addGlobalSymbol(Type* pDecl) { |
| 2930 | tokenid_t t = pDecl->id; |
| 2931 | VariableInfo* pVI = VI(t); |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 2932 | if(pVI && pVI->pAddress) { |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 2933 | reportDuplicate(t); |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 2934 | } |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 2935 | mGlobals.add(pDecl); |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 2936 | } |
| 2937 | |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 2938 | void reportDuplicate(tokenid_t t) { |
| 2939 | error("Duplicate definition of %s", nameof(t)); |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 2940 | } |
| 2941 | |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 2942 | void addLocalSymbol(Type* pDecl) { |
| 2943 | tokenid_t t = pDecl->id; |
| 2944 | if (mLocals.isDefinedAtCurrentLevel(t)) { |
| 2945 | reportDuplicate(t); |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 2946 | } |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 2947 | mLocals.add(pDecl); |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 2948 | } |
| 2949 | |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 2950 | void localDeclarations(Type* pBaseType) { |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 2951 | intptr_t a; |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 2952 | |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 2953 | while (pBaseType) { |
Jack Palevich | 22e3e8e | 2009-06-12 13:12:55 -0700 | [diff] [blame] | 2954 | while (tok != ';' && tok != EOF) { |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 2955 | Type* pDecl = expectDeclaration(pBaseType, mLocalArena); |
| 2956 | if (!pDecl) { |
| 2957 | break; |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 2958 | } |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 2959 | int variableAddress = 0; |
| 2960 | addLocalSymbol(pDecl); |
| 2961 | loc = loc + 4; |
| 2962 | variableAddress = -loc; |
| 2963 | VI(pDecl->id)->pAddress = (void*) variableAddress; |
| 2964 | if (accept('=')) { |
Jack Palevich | d7461a7 | 2009-06-12 14:26:58 -0700 | [diff] [blame] | 2965 | /* assignment */ |
Jack Palevich | d7461a7 | 2009-06-12 14:26:58 -0700 | [diff] [blame] | 2966 | expr(); |
| 2967 | pGen->storeR0(variableAddress); |
| 2968 | } |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 2969 | if (tok == ',') |
| 2970 | next(); |
| 2971 | } |
| 2972 | skip(';'); |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 2973 | pBaseType = acceptPrimitiveType(mLocalArena); |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 2974 | } |
| 2975 | } |
| 2976 | |
Jack Palevich | f1728be | 2009-06-12 13:53:51 -0700 | [diff] [blame] | 2977 | bool checkSymbol() { |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 2978 | return checkSymbol(tok); |
Jack Palevich | a1804dd | 2009-06-12 14:40:04 -0700 | [diff] [blame] | 2979 | } |
| 2980 | |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 2981 | void decodeToken(String& buffer, tokenid_t token) { |
| 2982 | if (token == EOF ) { |
| 2983 | buffer.printf("EOF"); |
| 2984 | } else if (token == TOK_NUM) { |
| 2985 | buffer.printf("numeric constant"); |
| 2986 | } else if (token >= 0 && token < 256) { |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 2987 | if (token < 32) { |
| 2988 | buffer.printf("'\\x%02x'", token); |
| 2989 | } else { |
| 2990 | buffer.printf("'%c'", token); |
| 2991 | } |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 2992 | } else if (token >= TOK_KEYWORD && token < TOK_SYMBOL) { |
| 2993 | buffer.printf("keyword \"%s\"", nameof(token)); |
| 2994 | } else { |
| 2995 | buffer.printf("symbol \"%s\"", nameof(token)); |
| 2996 | } |
| 2997 | } |
| 2998 | |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 2999 | bool checkSymbol(tokenid_t token) { |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3000 | bool result = token >= TOK_SYMBOL; |
Jack Palevich | f1728be | 2009-06-12 13:53:51 -0700 | [diff] [blame] | 3001 | if (!result) { |
| 3002 | String temp; |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3003 | decodeToken(temp, token); |
Jack Palevich | f1728be | 2009-06-12 13:53:51 -0700 | [diff] [blame] | 3004 | error("Expected symbol. Got %s", temp.getUnwrapped()); |
| 3005 | } |
| 3006 | return result; |
| 3007 | } |
| 3008 | |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 3009 | tokenid_t acceptSymbol() { |
| 3010 | tokenid_t result = 0; |
| 3011 | if (tok >= TOK_SYMBOL) { |
| 3012 | result = tok; |
| 3013 | next(); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 3014 | } |
| 3015 | return result; |
| 3016 | } |
| 3017 | |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 3018 | void globalDeclarations() { |
| 3019 | while (tok != EOF) { |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 3020 | Type* pBaseType = expectPrimitiveType(mGlobalArena); |
| 3021 | if (!pBaseType) { |
Jack Palevich | f1728be | 2009-06-12 13:53:51 -0700 | [diff] [blame] | 3022 | break; |
| 3023 | } |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 3024 | Type* pDecl = expectDeclaration(pBaseType, mGlobalArena); |
| 3025 | if (!pDecl) { |
| 3026 | break; |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 3027 | } |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 3028 | if (! isDefined(pDecl->id)) { |
| 3029 | addGlobalSymbol(pDecl); |
| 3030 | } |
| 3031 | VariableInfo* name = VI(pDecl->id); |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 3032 | if (name && name->pAddress) { |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 3033 | error("Already defined global %s", nameof(pDecl->id)); |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 3034 | } |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 3035 | if (pDecl->tag < TY_FUNC) { |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 3036 | // it's a variable declaration |
| 3037 | for(;;) { |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 3038 | if (name && !name->pAddress) { |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 3039 | name->pAddress = (int*) allocGlobalSpace(4); |
| 3040 | } |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 3041 | if (accept('=')) { |
Jack Palevich | d7461a7 | 2009-06-12 14:26:58 -0700 | [diff] [blame] | 3042 | if (tok == TOK_NUM) { |
| 3043 | if (name) { |
| 3044 | * (int*) name->pAddress = tokc; |
| 3045 | } |
| 3046 | next(); |
| 3047 | } else { |
| 3048 | error("Expected an integer constant"); |
| 3049 | } |
| 3050 | } |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 3051 | if (!accept(',')) { |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 3052 | break; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3053 | } |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 3054 | pDecl = expectDeclaration(pBaseType, mGlobalArena); |
| 3055 | if (!pDecl) { |
| 3056 | break; |
| 3057 | } |
| 3058 | if (! isDefined(pDecl->id)) { |
| 3059 | addGlobalSymbol(pDecl); |
| 3060 | } |
| 3061 | name = VI(pDecl->id); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3062 | } |
| 3063 | skip(';'); |
| 3064 | } else { |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 3065 | // Function declaration |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 3066 | if (accept(';')) { |
| 3067 | // forward declaration. |
| 3068 | } else { |
| 3069 | if (name) { |
| 3070 | /* patch forward references (XXX: does not work for function |
| 3071 | pointers) */ |
| 3072 | pGen->gsym((int) name->pForward); |
| 3073 | /* put function address */ |
| 3074 | name->pAddress = (void*) codeBuf.getPC(); |
| 3075 | } |
| 3076 | // Calculate stack offsets for parameters |
| 3077 | mLocals.pushLevel(); |
| 3078 | intptr_t a = 8; |
| 3079 | int argCount = 0; |
| 3080 | for (Type* pP = pDecl->pTail; pP; pP = pP->pTail) { |
| 3081 | Type* pArg = pP->pHead; |
| 3082 | addLocalSymbol(pArg); |
| 3083 | /* read param name and compute offset */ |
| 3084 | VI(pArg->id)->pAddress = (void*) a; |
| 3085 | a = a + 4; |
| 3086 | argCount++; |
| 3087 | } |
| 3088 | rsym = loc = 0; |
| 3089 | a = pGen->functionEntry(argCount); |
| 3090 | block(0, true); |
| 3091 | pGen->gsym(rsym); |
| 3092 | pGen->functionExit(argCount, a, loc); |
| 3093 | mLocals.popLevel(); |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 3094 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3095 | } |
| 3096 | } |
| 3097 | } |
| 3098 | |
Jack Palevich | f1f39cc | 2009-05-29 18:03:15 -0700 | [diff] [blame] | 3099 | char* allocGlobalSpace(int bytes) { |
| 3100 | if (glo - pGlobalBase + bytes > ALLOC_SIZE) { |
| 3101 | error("Global space exhausted"); |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 3102 | return NULL; |
Jack Palevich | f1f39cc | 2009-05-29 18:03:15 -0700 | [diff] [blame] | 3103 | } |
| 3104 | char* result = glo; |
| 3105 | glo += bytes; |
| 3106 | return result; |
| 3107 | } |
| 3108 | |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3109 | void cleanup() { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3110 | if (pGlobalBase != 0) { |
Jack Palevich | f1f39cc | 2009-05-29 18:03:15 -0700 | [diff] [blame] | 3111 | free(pGlobalBase); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3112 | pGlobalBase = 0; |
| 3113 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3114 | if (pGen) { |
| 3115 | delete pGen; |
| 3116 | pGen = 0; |
| 3117 | } |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 3118 | if (file) { |
| 3119 | delete file; |
| 3120 | file = 0; |
| 3121 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3122 | } |
| 3123 | |
| 3124 | void clear() { |
| 3125 | tok = 0; |
| 3126 | tokc = 0; |
| 3127 | tokl = 0; |
| 3128 | ch = 0; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3129 | rsym = 0; |
| 3130 | loc = 0; |
| 3131 | glo = 0; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3132 | dptr = 0; |
| 3133 | dch = 0; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3134 | file = 0; |
| 3135 | pGlobalBase = 0; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3136 | pGen = 0; |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 3137 | mPragmaStringCount = 0; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3138 | } |
Jack Palevich | e27bf3e | 2009-05-10 14:09:03 -0700 | [diff] [blame] | 3139 | |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 3140 | void setArchitecture(const char* architecture) { |
| 3141 | delete pGen; |
| 3142 | pGen = 0; |
| 3143 | |
| 3144 | if (architecture != NULL) { |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 3145 | #ifdef PROVIDE_ARM_CODEGEN |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 3146 | if (! pGen && strcmp(architecture, "arm") == 0) { |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 3147 | pGen = new ARMCodeGenerator(); |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 3148 | } |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 3149 | #endif |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 3150 | #ifdef PROVIDE_X86_CODEGEN |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 3151 | if (! pGen && strcmp(architecture, "x86") == 0) { |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 3152 | pGen = new X86CodeGenerator(); |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 3153 | } |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 3154 | #endif |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 3155 | if (!pGen ) { |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 3156 | error("Unknown architecture %s\n", architecture); |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 3157 | } |
| 3158 | } |
| 3159 | |
| 3160 | if (pGen == NULL) { |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 3161 | #if defined(DEFAULT_ARM_CODEGEN) |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 3162 | pGen = new ARMCodeGenerator(); |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 3163 | #elif defined(DEFAULT_X86_CODEGEN) |
| 3164 | pGen = new X86CodeGenerator(); |
| 3165 | #endif |
| 3166 | } |
| 3167 | if (pGen == NULL) { |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 3168 | error("No code generator defined."); |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 3169 | } else { |
| 3170 | pGen->setErrorSink(this); |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 3171 | } |
| 3172 | } |
| 3173 | |
Jack Palevich | 77ae76e | 2009-05-10 19:59:24 -0700 | [diff] [blame] | 3174 | public: |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 3175 | struct args { |
| 3176 | args() { |
| 3177 | architecture = 0; |
| 3178 | } |
| 3179 | const char* architecture; |
| 3180 | }; |
| 3181 | |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 3182 | Compiler() { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3183 | clear(); |
Jack Palevich | e27bf3e | 2009-05-10 14:09:03 -0700 | [diff] [blame] | 3184 | } |
Jack Palevich | bbf8ab5 | 2009-05-11 11:54:30 -0700 | [diff] [blame] | 3185 | |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 3186 | ~Compiler() { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3187 | cleanup(); |
| 3188 | } |
| 3189 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 3190 | int compile(const char* text, size_t textLength) { |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 3191 | int result; |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 3192 | |
| 3193 | cleanup(); |
| 3194 | clear(); |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3195 | mTokenTable.setArena(&mGlobalArena); |
| 3196 | mGlobals.setArena(&mGlobalArena); |
| 3197 | mGlobals.setTokenTable(&mTokenTable); |
| 3198 | mLocals.setArena(&mLocalArena); |
| 3199 | mLocals.setTokenTable(&mTokenTable); |
| 3200 | |
| 3201 | internKeywords(); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 3202 | createPrimitiveTypes(); |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 3203 | codeBuf.init(ALLOC_SIZE); |
| 3204 | setArchitecture(NULL); |
| 3205 | if (!pGen) { |
| 3206 | return -1; |
| 3207 | } |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 3208 | #ifdef PROVIDE_TRACE_CODEGEN |
| 3209 | pGen = new TraceCodeGenerator(pGen); |
| 3210 | #endif |
| 3211 | pGen->setErrorSink(this); |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 3212 | pGen->init(&codeBuf); |
| 3213 | file = new TextInputStream(text, textLength); |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 3214 | pGlobalBase = (char*) calloc(1, ALLOC_SIZE); |
| 3215 | glo = pGlobalBase; |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 3216 | inp(); |
| 3217 | next(); |
| 3218 | globalDeclarations(); |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 3219 | checkForUndefinedForwardReferences(); |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 3220 | result = pGen->finishCompile(); |
| 3221 | if (result == 0) { |
| 3222 | if (mErrorBuf.len()) { |
| 3223 | result = -2; |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 3224 | } |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 3225 | } |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 3226 | return result; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3227 | } |
| 3228 | |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 3229 | void createPrimitiveTypes() { |
| 3230 | mkpInt = createType(TY_INT, NULL, NULL, mGlobalArena); |
| 3231 | mkpChar = createType(TY_CHAR, NULL, NULL, mGlobalArena); |
| 3232 | mkpVoid = createType(TY_VOID, NULL, NULL, mGlobalArena); |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 3233 | mkpFloat = createType(TY_FLOAT, NULL, NULL, mGlobalArena); |
| 3234 | mkpDouble = createType(TY_DOUBLE, NULL, NULL, mGlobalArena); |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 3235 | mkpIntPtr = createPtrType(mkpInt, mGlobalArena); |
| 3236 | mkpCharPtr = createPtrType(mkpChar, mGlobalArena); |
| 3237 | mkpPtrIntFn = createPtrType( |
| 3238 | createType(TY_FUNC, mkpInt, NULL, mGlobalArena), |
| 3239 | mGlobalArena); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 3240 | } |
| 3241 | |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 3242 | void checkForUndefinedForwardReferences() { |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3243 | mGlobals.forEach(static_ufrcFn, this); |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 3244 | } |
| 3245 | |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3246 | static bool static_ufrcFn(VariableInfo* value, void* context) { |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 3247 | Compiler* pCompiler = (Compiler*) context; |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3248 | return pCompiler->undefinedForwardReferenceCheck(value); |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 3249 | } |
| 3250 | |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3251 | bool undefinedForwardReferenceCheck(VariableInfo* value) { |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 3252 | if (!value->pAddress && value->pForward) { |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3253 | error("Undefined forward reference: %s", |
| 3254 | mTokenTable[value->tok].pText); |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 3255 | } |
| 3256 | return true; |
| 3257 | } |
| 3258 | |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3259 | int dump(FILE* out) { |
| 3260 | fwrite(codeBuf.getBase(), 1, codeBuf.getSize(), out); |
| 3261 | return 0; |
| 3262 | } |
Jack Palevich | 77ae76e | 2009-05-10 19:59:24 -0700 | [diff] [blame] | 3263 | |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 3264 | int disassemble(FILE* out) { |
| 3265 | return pGen->disassemble(out); |
| 3266 | } |
| 3267 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 3268 | /* Look through the symbol table to find a symbol. |
| 3269 | * If found, return its value. |
| 3270 | */ |
| 3271 | void* lookup(const char* name) { |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3272 | tokenid_t tok = mTokenTable.intern(name, strlen(name)); |
| 3273 | VariableInfo* pVariableInfo = VI(tok); |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 3274 | if (pVariableInfo) { |
| 3275 | return pVariableInfo->pAddress; |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 3276 | } |
| 3277 | return NULL; |
| 3278 | } |
| 3279 | |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 3280 | void getPragmas(ACCsizei* actualStringCount, |
| 3281 | ACCsizei maxStringCount, ACCchar** strings) { |
| 3282 | int stringCount = mPragmaStringCount; |
| 3283 | if (actualStringCount) { |
| 3284 | *actualStringCount = stringCount; |
| 3285 | } |
| 3286 | if (stringCount > maxStringCount) { |
| 3287 | stringCount = maxStringCount; |
| 3288 | } |
| 3289 | if (strings) { |
| 3290 | char* pPragmas = mPragmas.getUnwrapped(); |
| 3291 | while (stringCount-- > 0) { |
| 3292 | *strings++ = pPragmas; |
| 3293 | pPragmas += strlen(pPragmas) + 1; |
| 3294 | } |
| 3295 | } |
| 3296 | } |
| 3297 | |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 3298 | char* getErrorMessage() { |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 3299 | return mErrorBuf.getUnwrapped(); |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 3300 | } |
| 3301 | |
Jack Palevich | 77ae76e | 2009-05-10 19:59:24 -0700 | [diff] [blame] | 3302 | }; |
| 3303 | |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 3304 | const char* Compiler::operatorChars = |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 3305 | "++--*@/@%@+@-@<<>><=>=<@>@==!=&&||&@^@|@~@!@"; |
| 3306 | |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 3307 | const char Compiler::operatorLevel[] = |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 3308 | {11, 11, 1, 1, 1, 2, 2, 3, 3, 4, 4, 4, 4, |
| 3309 | 5, 5, /* ==, != */ |
| 3310 | 9, 10, /* &&, || */ |
| 3311 | 6, 7, 8, /* & ^ | */ |
| 3312 | 2, 2 /* ~ ! */ |
| 3313 | }; |
| 3314 | |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 3315 | #ifdef PROVIDE_ARM_CODEGEN |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 3316 | FILE* Compiler::ARMCodeGenerator::disasmOut; |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 3317 | #endif |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 3318 | |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 3319 | #ifdef PROVIDE_X86_CODEGEN |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 3320 | const int Compiler::X86CodeGenerator::operatorHelper[] = { |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 3321 | 0x1, // ++ |
| 3322 | 0xff, // -- |
| 3323 | 0xc1af0f, // * |
| 3324 | 0xf9f79991, // / |
| 3325 | 0xf9f79991, // % (With manual assist to swap results) |
| 3326 | 0xc801, // + |
| 3327 | 0xd8f7c829, // - |
| 3328 | 0xe0d391, // << |
| 3329 | 0xf8d391, // >> |
| 3330 | 0xe, // <= |
| 3331 | 0xd, // >= |
| 3332 | 0xc, // < |
| 3333 | 0xf, // > |
| 3334 | 0x4, // == |
| 3335 | 0x5, // != |
| 3336 | 0x0, // && |
| 3337 | 0x1, // || |
| 3338 | 0xc821, // & |
| 3339 | 0xc831, // ^ |
| 3340 | 0xc809, // | |
| 3341 | 0xd0f7, // ~ |
| 3342 | 0x4 // ! |
| 3343 | }; |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 3344 | #endif |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 3345 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 3346 | struct ACCscript { |
| 3347 | ACCscript() { |
| 3348 | text = 0; |
| 3349 | textLength = 0; |
| 3350 | accError = ACC_NO_ERROR; |
| 3351 | } |
Jack Palevich | bbf8ab5 | 2009-05-11 11:54:30 -0700 | [diff] [blame] | 3352 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 3353 | ~ACCscript() { |
| 3354 | delete text; |
| 3355 | } |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 3356 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 3357 | void setError(ACCenum error) { |
| 3358 | if (accError == ACC_NO_ERROR && error != ACC_NO_ERROR) { |
| 3359 | accError = error; |
Jack Palevich | bbf8ab5 | 2009-05-11 11:54:30 -0700 | [diff] [blame] | 3360 | } |
| 3361 | } |
| 3362 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 3363 | ACCenum getError() { |
| 3364 | ACCenum result = accError; |
| 3365 | accError = ACC_NO_ERROR; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 3366 | return result; |
Jack Palevich | bbf8ab5 | 2009-05-11 11:54:30 -0700 | [diff] [blame] | 3367 | } |
| 3368 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 3369 | Compiler compiler; |
| 3370 | char* text; |
| 3371 | int textLength; |
| 3372 | ACCenum accError; |
| 3373 | }; |
| 3374 | |
| 3375 | |
| 3376 | extern "C" |
| 3377 | ACCscript* accCreateScript() { |
| 3378 | return new ACCscript(); |
Jack Palevich | bbf8ab5 | 2009-05-11 11:54:30 -0700 | [diff] [blame] | 3379 | } |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 3380 | |
| 3381 | extern "C" |
| 3382 | ACCenum accGetError( ACCscript* script ) { |
| 3383 | return script->getError(); |
| 3384 | } |
| 3385 | |
| 3386 | extern "C" |
| 3387 | void accDeleteScript(ACCscript* script) { |
| 3388 | delete script; |
| 3389 | } |
| 3390 | |
| 3391 | extern "C" |
| 3392 | void accScriptSource(ACCscript* script, |
| 3393 | ACCsizei count, |
| 3394 | const ACCchar ** string, |
| 3395 | const ACCint * length) { |
| 3396 | int totalLength = 0; |
| 3397 | for(int i = 0; i < count; i++) { |
| 3398 | int len = -1; |
| 3399 | const ACCchar* s = string[i]; |
| 3400 | if (length) { |
| 3401 | len = length[i]; |
| 3402 | } |
| 3403 | if (len < 0) { |
| 3404 | len = strlen(s); |
| 3405 | } |
| 3406 | totalLength += len; |
| 3407 | } |
| 3408 | delete script->text; |
| 3409 | char* text = new char[totalLength + 1]; |
| 3410 | script->text = text; |
| 3411 | script->textLength = totalLength; |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 3412 | char* dest = text; |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 3413 | for(int i = 0; i < count; i++) { |
| 3414 | int len = -1; |
| 3415 | const ACCchar* s = string[i]; |
| 3416 | if (length) { |
| 3417 | len = length[i]; |
| 3418 | } |
| 3419 | if (len < 0) { |
| 3420 | len = strlen(s); |
| 3421 | } |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 3422 | memcpy(dest, s, len); |
| 3423 | dest += len; |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 3424 | } |
| 3425 | text[totalLength] = '\0'; |
| 3426 | } |
| 3427 | |
| 3428 | extern "C" |
| 3429 | void accCompileScript(ACCscript* script) { |
| 3430 | int result = script->compiler.compile(script->text, script->textLength); |
| 3431 | if (result) { |
| 3432 | script->setError(ACC_INVALID_OPERATION); |
| 3433 | } |
| 3434 | } |
| 3435 | |
| 3436 | extern "C" |
| 3437 | void accGetScriptiv(ACCscript* script, |
| 3438 | ACCenum pname, |
| 3439 | ACCint * params) { |
| 3440 | switch (pname) { |
| 3441 | case ACC_INFO_LOG_LENGTH: |
| 3442 | *params = 0; |
| 3443 | break; |
| 3444 | } |
| 3445 | } |
| 3446 | |
| 3447 | extern "C" |
| 3448 | void accGetScriptInfoLog(ACCscript* script, |
| 3449 | ACCsizei maxLength, |
| 3450 | ACCsizei * length, |
| 3451 | ACCchar * infoLog) { |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 3452 | char* message = script->compiler.getErrorMessage(); |
| 3453 | int messageLength = strlen(message) + 1; |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 3454 | if (length) { |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 3455 | *length = messageLength; |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 3456 | } |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 3457 | if (infoLog && maxLength > 0) { |
| 3458 | int trimmedLength = maxLength < messageLength ? |
| 3459 | maxLength : messageLength; |
| 3460 | memcpy(infoLog, message, trimmedLength); |
| 3461 | infoLog[trimmedLength] = 0; |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 3462 | } |
| 3463 | } |
| 3464 | |
| 3465 | extern "C" |
| 3466 | void accGetScriptLabel(ACCscript* script, const ACCchar * name, |
| 3467 | ACCvoid ** address) { |
| 3468 | void* value = script->compiler.lookup(name); |
| 3469 | if (value) { |
| 3470 | *address = value; |
| 3471 | } else { |
| 3472 | script->setError(ACC_INVALID_VALUE); |
| 3473 | } |
| 3474 | } |
| 3475 | |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 3476 | extern "C" |
| 3477 | void accGetPragmas(ACCscript* script, ACCsizei* actualStringCount, |
| 3478 | ACCsizei maxStringCount, ACCchar** strings){ |
| 3479 | script->compiler.getPragmas(actualStringCount, maxStringCount, strings); |
| 3480 | } |
| 3481 | |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 3482 | extern "C" |
| 3483 | void accDisassemble(ACCscript* script) { |
| 3484 | script->compiler.disassemble(stderr); |
| 3485 | } |
| 3486 | |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 3487 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 3488 | } // namespace acc |
| 3489 | |