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