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