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> |
Jack Palevich | 8dc662e | 2009-06-09 22:53:47 +0000 | [diff] [blame] | 12 | #include <errno.h> |
Jack Palevich | e27bf3e | 2009-05-10 14:09:03 -0700 | [diff] [blame] | 13 | #include <stdarg.h> |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 14 | #include <stdint.h> |
Jack Palevich | ae54f1f | 2009-05-08 14:54:15 -0700 | [diff] [blame] | 15 | #include <stdio.h> |
Jack Palevich | f6b5a53 | 2009-05-10 19:16:42 -0700 | [diff] [blame] | 16 | #include <stdlib.h> |
| 17 | #include <string.h> |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 18 | #include <cutils/hashmap.h> |
Jack Palevich | ae54f1f | 2009-05-08 14:54:15 -0700 | [diff] [blame] | 19 | |
Jack Palevich | 8dc662e | 2009-06-09 22:53:47 +0000 | [diff] [blame] | 20 | #if defined(__i386__) |
| 21 | #include <sys/mman.h> |
| 22 | #endif |
| 23 | |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 24 | #if defined(__arm__) |
| 25 | #include <unistd.h> |
| 26 | #endif |
| 27 | |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 28 | #if defined(__arm__) |
| 29 | #define DEFAULT_ARM_CODEGEN |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 30 | #define PROVIDE_ARM_CODEGEN |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 31 | #elif defined(__i386__) |
| 32 | #define DEFAULT_X86_CODEGEN |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 33 | #define PROVIDE_X86_CODEGEN |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 34 | #elif defined(__x86_64__) |
| 35 | #define DEFAULT_X64_CODEGEN |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 36 | #define PROVIDE_X64_CODEGEN |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 37 | #endif |
| 38 | |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 39 | #ifdef PROVIDE_ARM_CODEGEN |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 40 | #include "disassem.h" |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 41 | #endif |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 42 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 43 | #include <acc/acc.h> |
| 44 | |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 45 | #define LOG_API(...) do {} while(0) |
| 46 | // #define LOG_API(...) fprintf (stderr, __VA_ARGS__) |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 47 | |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 48 | #define LOG_STACK(...) do {} while(0) |
| 49 | // #define LOG_STACK(...) fprintf (stderr, __VA_ARGS__) |
| 50 | |
| 51 | // #define ENABLE_ARM_DISASSEMBLY |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 52 | // #define PROVIDE_TRACE_CODEGEN |
| 53 | |
Jack Palevich | bbf8ab5 | 2009-05-11 11:54:30 -0700 | [diff] [blame] | 54 | namespace acc { |
| 55 | |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 56 | // Subset of STL vector. |
| 57 | template<class E> class Vector { |
| 58 | public: |
| 59 | Vector() { |
| 60 | mpBase = 0; |
| 61 | mUsed = 0; |
| 62 | mSize = 0; |
| 63 | } |
| 64 | |
| 65 | ~Vector() { |
| 66 | if (mpBase) { |
| 67 | for(size_t i = 0; i < mUsed; i++) { |
| 68 | mpBase[mUsed].~E(); |
| 69 | } |
| 70 | free(mpBase); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | inline E& operator[](size_t i) { |
| 75 | return mpBase[i]; |
| 76 | } |
| 77 | |
| 78 | inline E& front() { |
| 79 | return mpBase[0]; |
| 80 | } |
| 81 | |
| 82 | inline E& back() { |
| 83 | return mpBase[mUsed - 1]; |
| 84 | } |
| 85 | |
| 86 | void pop_back() { |
| 87 | mUsed -= 1; |
| 88 | mpBase[mUsed].~E(); |
| 89 | } |
| 90 | |
| 91 | void push_back(const E& item) { |
| 92 | * ensure(1) = item; |
| 93 | } |
| 94 | |
| 95 | size_t size() { |
| 96 | return mUsed; |
| 97 | } |
| 98 | |
| 99 | private: |
| 100 | E* ensure(int n) { |
| 101 | size_t newUsed = mUsed + n; |
| 102 | if (newUsed > mSize) { |
| 103 | size_t newSize = mSize * 2 + 10; |
| 104 | if (newSize < newUsed) { |
| 105 | newSize = newUsed; |
| 106 | } |
| 107 | mpBase = (E*) realloc(mpBase, sizeof(E) * newSize); |
| 108 | mSize = newSize; |
| 109 | } |
| 110 | E* result = mpBase + mUsed; |
| 111 | mUsed = newUsed; |
| 112 | return result; |
| 113 | } |
| 114 | |
| 115 | E* mpBase; |
| 116 | size_t mUsed; |
| 117 | size_t mSize; |
| 118 | }; |
| 119 | |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 120 | class ErrorSink { |
| 121 | public: |
| 122 | void error(const char *fmt, ...) { |
| 123 | va_list ap; |
| 124 | va_start(ap, fmt); |
| 125 | verror(fmt, ap); |
| 126 | va_end(ap); |
| 127 | } |
| 128 | |
Marco Nelissen | eea5ae9 | 2009-07-08 16:59:18 -0700 | [diff] [blame] | 129 | virtual ~ErrorSink() {} |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 130 | virtual void verror(const char* fmt, va_list ap) = 0; |
| 131 | }; |
| 132 | |
| 133 | class Compiler : public ErrorSink { |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 134 | typedef int tokenid_t; |
| 135 | enum TypeTag { |
| 136 | TY_INT, TY_CHAR, TY_VOID, TY_FLOAT, TY_DOUBLE, |
| 137 | TY_POINTER, TY_FUNC, TY_PARAM |
| 138 | }; |
| 139 | |
| 140 | struct Type { |
| 141 | TypeTag tag; |
| 142 | tokenid_t id; // For function arguments |
| 143 | Type* pHead; |
| 144 | Type* pTail; |
| 145 | }; |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 146 | |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 147 | class CodeBuf { |
Jack Palevich | 653f42d | 2009-05-28 17:15:32 -0700 | [diff] [blame] | 148 | char* ind; // Output code pointer |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 149 | char* pProgramBase; |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 150 | ErrorSink* mErrorSink; |
| 151 | int mSize; |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 152 | bool mOverflowed; |
Jack Palevich | f0cbc92 | 2009-05-08 16:35:13 -0700 | [diff] [blame] | 153 | |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 154 | void release() { |
| 155 | if (pProgramBase != 0) { |
| 156 | free(pProgramBase); |
| 157 | pProgramBase = 0; |
Jack Palevich | ae54f1f | 2009-05-08 14:54:15 -0700 | [diff] [blame] | 158 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 161 | bool check(int n) { |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 162 | int newSize = ind - pProgramBase + n; |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 163 | bool overflow = newSize > mSize; |
| 164 | if (overflow && !mOverflowed) { |
| 165 | mOverflowed = true; |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 166 | if (mErrorSink) { |
| 167 | mErrorSink->error("Code too large: %d bytes", newSize); |
| 168 | } |
| 169 | } |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 170 | return overflow; |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 173 | public: |
| 174 | CodeBuf() { |
| 175 | pProgramBase = 0; |
| 176 | ind = 0; |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 177 | mErrorSink = 0; |
| 178 | mSize = 0; |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 179 | mOverflowed = false; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | ~CodeBuf() { |
| 183 | release(); |
| 184 | } |
| 185 | |
| 186 | void init(int size) { |
| 187 | release(); |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 188 | mSize = size; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 189 | pProgramBase = (char*) calloc(1, size); |
| 190 | ind = pProgramBase; |
| 191 | } |
| 192 | |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 193 | void setErrorSink(ErrorSink* pErrorSink) { |
| 194 | mErrorSink = pErrorSink; |
| 195 | } |
| 196 | |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 197 | int o4(int n) { |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 198 | if(check(4)) { |
| 199 | return 0; |
| 200 | } |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 201 | intptr_t result = (intptr_t) ind; |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 202 | * (int*) ind = n; |
| 203 | ind += 4; |
| 204 | return result; |
| 205 | } |
| 206 | |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 207 | /* |
| 208 | * Output a byte. Handles all values, 0..ff. |
| 209 | */ |
| 210 | void ob(int n) { |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 211 | if(check(1)) { |
| 212 | return; |
| 213 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 214 | *ind++ = n; |
| 215 | } |
| 216 | |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 217 | inline void* getBase() { |
| 218 | return (void*) pProgramBase; |
| 219 | } |
| 220 | |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 221 | intptr_t getSize() { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 222 | return ind - pProgramBase; |
| 223 | } |
| 224 | |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 225 | intptr_t getPC() { |
| 226 | return (intptr_t) ind; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 227 | } |
| 228 | }; |
| 229 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 230 | /** |
| 231 | * A code generator creates an in-memory program, generating the code on |
| 232 | * the fly. There is one code generator implementation for each supported |
| 233 | * architecture. |
| 234 | * |
| 235 | * The code generator implements the following abstract machine: |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 236 | * R0 - the accumulator. |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 237 | * FP - a frame pointer for accessing function arguments and local |
| 238 | * variables. |
| 239 | * SP - a stack pointer for storing intermediate results while evaluating |
| 240 | * expressions. The stack pointer grows downwards. |
| 241 | * |
| 242 | * The function calling convention is that all arguments are placed on the |
| 243 | * stack such that the first argument has the lowest address. |
| 244 | * After the call, the result is in R0. The caller is responsible for |
| 245 | * removing the arguments from the stack. |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 246 | * The R0 register is not saved across function calls. The |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 247 | * FP and SP registers are saved. |
| 248 | */ |
| 249 | |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 250 | class CodeGenerator { |
| 251 | public: |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 252 | CodeGenerator() { |
| 253 | mErrorSink = 0; |
| 254 | pCodeBuf = 0; |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 255 | pushType(); |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 256 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 257 | virtual ~CodeGenerator() {} |
| 258 | |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 259 | virtual void init(CodeBuf* pCodeBuf) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 260 | this->pCodeBuf = pCodeBuf; |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 261 | pCodeBuf->setErrorSink(mErrorSink); |
| 262 | } |
| 263 | |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 264 | virtual void setErrorSink(ErrorSink* pErrorSink) { |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 265 | mErrorSink = pErrorSink; |
| 266 | if (pCodeBuf) { |
| 267 | pCodeBuf->setErrorSink(mErrorSink); |
| 268 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Jack Palevich | a8f427f | 2009-07-13 18:40:08 -0700 | [diff] [blame] | 271 | void setTypes(Type* pInt) { |
| 272 | mkpInt = pInt; |
| 273 | } |
| 274 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 275 | /* Emit a function prolog. |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 276 | * pDecl is the function declaration, which gives the arguments. |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 277 | * Save the old value of the FP. |
| 278 | * Set the new value of the FP. |
| 279 | * Convert from the native platform calling convention to |
| 280 | * our stack-based calling convention. This may require |
| 281 | * pushing arguments from registers to the stack. |
| 282 | * Allocate "N" bytes of stack space. N isn't known yet, so |
| 283 | * just emit the instructions for adjusting the stack, and return |
| 284 | * the address to patch up. The patching will be done in |
| 285 | * functionExit(). |
| 286 | * returns address to patch with local variable size. |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 287 | */ |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 288 | virtual int functionEntry(Type* pDecl) = 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 a function epilog. |
| 291 | * Restore the old SP and FP register values. |
| 292 | * Return to the calling function. |
| 293 | * argCount - the number of arguments to the function. |
| 294 | * localVariableAddress - returned from functionEntry() |
| 295 | * localVariableSize - the size in bytes of the local variables. |
| 296 | */ |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 297 | virtual void functionExit(Type* pDecl, int localVariableAddress, |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 298 | int localVariableSize) = 0; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 299 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 300 | /* load immediate value to R0 */ |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 301 | virtual void li(int i, Type* pType) = 0; |
Jack Palevich | 1aeb87b | 2009-07-06 18:33:20 -0700 | [diff] [blame] | 302 | |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 303 | /* Load floating point value from global address. */ |
| 304 | virtual void loadFloat(int address, Type* pType) = 0; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 305 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 306 | /* Jump to a target, and return the address of the word that |
| 307 | * holds the target data, in case it needs to be fixed up later. |
| 308 | */ |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 309 | virtual int gjmp(int t) = 0; |
| 310 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 311 | /* Test R0 and jump to a target if the test succeeds. |
| 312 | * l = 0: je, l == 1: jne |
| 313 | * Return the address of the word that holds the targed data, in |
| 314 | * case it needs to be fixed up later. |
| 315 | */ |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 316 | virtual int gtst(bool l, int t) = 0; |
| 317 | |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 318 | /* Compare TOS against R0, and store the boolean result in R0. |
| 319 | * Pops TOS. |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 320 | * op specifies the comparison. |
| 321 | */ |
Jack Palevich | a39749f | 2009-07-08 20:40:31 -0700 | [diff] [blame] | 322 | virtual void gcmp(int op, Type* pResultType) = 0; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 323 | |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 324 | /* Perform the arithmetic op specified by op. TOS is the |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 325 | * left argument, R0 is the right argument. |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 326 | * Pops TOS. |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 327 | */ |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 328 | virtual void genOp(int op) = 0; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 329 | |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 330 | /* Compare 0 against R0, and store the boolean result in R0. |
| 331 | * op specifies the comparison. |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 332 | */ |
Jack Palevich | a39749f | 2009-07-08 20:40:31 -0700 | [diff] [blame] | 333 | virtual void gUnaryCmp(int op, Type* pResultType) = 0; |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 334 | |
| 335 | /* Perform the arithmetic op specified by op. 0 is the |
| 336 | * left argument, R0 is the right argument. |
| 337 | */ |
| 338 | virtual void genUnaryOp(int op) = 0; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 339 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 340 | /* Push R0 onto the stack. |
| 341 | */ |
| 342 | virtual void pushR0() = 0; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 343 | |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 344 | /* Store R0 to the address stored in TOS. |
| 345 | * The TOS is popped. |
| 346 | * pPointerType is the type of the pointer (of the input R0). |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 347 | */ |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 348 | virtual void storeR0ToTOS(Type* pPointerType) = 0; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 349 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 350 | /* Load R0 from the address stored in R0. |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 351 | * pPointerType is the type of the pointer (of the input R0). |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 352 | */ |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 353 | virtual void loadR0FromR0(Type* pPointerType) = 0; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 354 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 355 | /* Load the absolute address of a variable to R0. |
| 356 | * If ea <= LOCAL, then this is a local variable, or an |
| 357 | * argument, addressed relative to FP. |
| 358 | * else it is an absolute global address. |
| 359 | */ |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 360 | virtual void leaR0(int ea, Type* pPointerType) = 0; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 361 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 362 | /* Store R0 to a variable. |
| 363 | * If ea <= LOCAL, then this is a local variable, or an |
| 364 | * argument, addressed relative to FP. |
| 365 | * else it is an absolute global address. |
| 366 | */ |
Jack Palevich | 9cbd226 | 2009-07-08 16:48:41 -0700 | [diff] [blame] | 367 | virtual void storeR0(int ea, Type* pType) = 0; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 368 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 369 | /* load R0 from a variable. |
| 370 | * If ea <= LOCAL, then this is a local variable, or an |
| 371 | * argument, addressed relative to FP. |
| 372 | * else it is an absolute global address. |
| 373 | * If isIncDec is true, then the stored variable's value |
| 374 | * should be post-incremented or post-decremented, based |
| 375 | * on the value of op. |
| 376 | */ |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 377 | virtual void loadR0(int ea, bool isIncDec, int op, Type* pType) = 0; |
| 378 | |
| 379 | /** |
| 380 | * Convert R0 to the given type. |
| 381 | */ |
| 382 | virtual void convertR0(Type* pType) = 0; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 383 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 384 | /* Emit code to adjust the stack for a function call. Return the |
| 385 | * label for the address of the instruction that adjusts the |
| 386 | * stack size. This will be passed as argument "a" to |
| 387 | * endFunctionCallArguments. |
| 388 | */ |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 389 | virtual int beginFunctionCallArguments() = 0; |
| 390 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 391 | /* Emit code to store R0 to the stack at byte offset l. |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 392 | * Returns stack size of object (typically 4 or 8 bytes) |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 393 | */ |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 394 | virtual size_t storeR0ToArg(int l) = 0; |
Jack Palevich | 7810bc9 | 2009-05-15 14:31:47 -0700 | [diff] [blame] | 395 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 396 | /* Patch the function call preamble. |
| 397 | * a is the address returned from beginFunctionCallArguments |
| 398 | * l is the number of bytes the arguments took on the stack. |
| 399 | * Typically you would also emit code to convert the argument |
| 400 | * list into whatever the native function calling convention is. |
| 401 | * On ARM for example you would pop the first 5 arguments into |
| 402 | * R0..R4 |
| 403 | */ |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 404 | virtual void endFunctionCallArguments(Type* pDecl, int a, int l) = 0; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 405 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 406 | /* Emit a call to an unknown function. The argument "symbol" needs to |
| 407 | * be stored in the location where the address should go. It forms |
| 408 | * a chain. The address will be patched later. |
| 409 | * Return the address of the word that has to be patched. |
| 410 | */ |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 411 | virtual int callForward(int symbol, Type* pFunc) = 0; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 412 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 413 | /* Call a function using PC-relative addressing. t is the PC-relative |
| 414 | * address of the function. It has already been adjusted for the |
| 415 | * architectural jump offset, so just store it as-is. |
| 416 | */ |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 417 | virtual void callRelative(int t, Type* pFunc) = 0; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 418 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 419 | /* Call a function pointer. L is the number of bytes the arguments |
| 420 | * take on the stack. The address of the function is stored at |
| 421 | * location SP + l. |
| 422 | */ |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 423 | virtual void callIndirect(int l, Type* pFunc) = 0; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 424 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 425 | /* Adjust SP after returning from a function call. l is the |
| 426 | * number of bytes of arguments stored on the stack. isIndirect |
| 427 | * is true if this was an indirect call. (In which case the |
| 428 | * address of the function is stored at location SP + l.) |
| 429 | */ |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 430 | virtual void adjustStackAfterCall(Type* pDecl, int l, bool isIndirect) = 0; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 431 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 432 | /* Print a disassembly of the assembled code to out. Return |
| 433 | * non-zero if there is an error. |
| 434 | */ |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 435 | virtual int disassemble(FILE* out) = 0; |
| 436 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 437 | /* Generate a symbol at the current PC. t is the head of a |
| 438 | * linked list of addresses to patch. |
| 439 | */ |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 440 | virtual void gsym(int t) = 0; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 441 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 442 | /* |
| 443 | * Do any cleanup work required at the end of a compile. |
| 444 | * For example, an instruction cache might need to be |
| 445 | * invalidated. |
| 446 | * Return non-zero if there is an error. |
| 447 | */ |
| 448 | virtual int finishCompile() = 0; |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 449 | |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 450 | /** |
| 451 | * Adjust relative branches by this amount. |
| 452 | */ |
| 453 | virtual int jumpOffset() = 0; |
| 454 | |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 455 | /** |
Jack Palevich | 9cbd226 | 2009-07-08 16:48:41 -0700 | [diff] [blame] | 456 | * Memory alignment (in bytes) for this type of data |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 457 | */ |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 458 | virtual size_t alignmentOf(Type* type) = 0; |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 459 | |
| 460 | /** |
| 461 | * Array element alignment (in bytes) for this type of data. |
| 462 | */ |
| 463 | virtual size_t sizeOf(Type* type) = 0; |
| 464 | |
Jack Palevich | 9cbd226 | 2009-07-08 16:48:41 -0700 | [diff] [blame] | 465 | /** |
| 466 | * Stack argument size of this data type. |
| 467 | */ |
| 468 | virtual size_t stackSizeOf(Type* pType) = 0; |
| 469 | |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 470 | virtual Type* getR0Type() { |
| 471 | return mExpressionStack.back(); |
| 472 | } |
| 473 | |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 474 | protected: |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 475 | /* |
| 476 | * Output a byte. Handles all values, 0..ff. |
| 477 | */ |
| 478 | void ob(int n) { |
| 479 | pCodeBuf->ob(n); |
| 480 | } |
| 481 | |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 482 | intptr_t o4(int data) { |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 483 | return pCodeBuf->o4(data); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 484 | } |
| 485 | |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 486 | intptr_t getBase() { |
| 487 | return (intptr_t) pCodeBuf->getBase(); |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 488 | } |
| 489 | |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 490 | intptr_t getPC() { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 491 | return pCodeBuf->getPC(); |
| 492 | } |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 493 | |
| 494 | intptr_t getSize() { |
| 495 | return pCodeBuf->getSize(); |
| 496 | } |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 497 | |
| 498 | void error(const char* fmt,...) { |
| 499 | va_list ap; |
| 500 | va_start(ap, fmt); |
| 501 | mErrorSink->verror(fmt, ap); |
| 502 | va_end(ap); |
| 503 | } |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 504 | |
| 505 | void assert(bool test) { |
| 506 | if (!test) { |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 507 | * (char*) 0 = 0; |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 508 | error("code generator assertion failed."); |
| 509 | } |
| 510 | } |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 511 | |
| 512 | void setR0Type(Type* pType) { |
| 513 | mExpressionStack.back() = pType; |
| 514 | } |
| 515 | |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 516 | Type* getTOSType() { |
| 517 | return mExpressionStack[mExpressionStack.size()-2]; |
| 518 | } |
| 519 | |
| 520 | void pushType() { |
| 521 | mExpressionStack.push_back(NULL); |
| 522 | } |
| 523 | |
| 524 | void popType() { |
| 525 | mExpressionStack.pop_back(); |
| 526 | } |
| 527 | |
| 528 | bool bitsSame(Type* pA, Type* pB) { |
| 529 | return collapseType(pA->tag) == collapseType(pB->tag); |
| 530 | } |
| 531 | |
| 532 | TypeTag collapseType(TypeTag tag) { |
| 533 | static const TypeTag collapsedTag[] = { |
| 534 | TY_INT, TY_INT, TY_VOID, TY_FLOAT, TY_DOUBLE, TY_INT, |
| 535 | TY_VOID, TY_VOID}; |
| 536 | return collapsedTag[tag]; |
| 537 | } |
| 538 | |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 539 | TypeTag collapseTypeR0() { |
| 540 | return collapseType(getR0Type()->tag); |
| 541 | } |
| 542 | |
| 543 | bool isFloatType(Type* pType) { |
Jack Palevich | 128ad2d | 2009-07-08 14:51:31 -0700 | [diff] [blame] | 544 | return isFloatTag(pType->tag); |
| 545 | } |
| 546 | |
| 547 | bool isFloatTag(TypeTag tag) { |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 548 | return tag == TY_FLOAT || tag == TY_DOUBLE; |
| 549 | } |
| 550 | |
Jack Palevich | a8f427f | 2009-07-13 18:40:08 -0700 | [diff] [blame] | 551 | Type* mkpInt; |
| 552 | |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 553 | private: |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 554 | Vector<Type*> mExpressionStack; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 555 | CodeBuf* pCodeBuf; |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 556 | ErrorSink* mErrorSink; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 557 | }; |
| 558 | |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 559 | #ifdef PROVIDE_ARM_CODEGEN |
| 560 | |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 561 | class ARMCodeGenerator : public CodeGenerator { |
| 562 | public: |
| 563 | ARMCodeGenerator() {} |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 564 | |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 565 | virtual ~ARMCodeGenerator() {} |
| 566 | |
| 567 | /* returns address to patch with local variable size |
| 568 | */ |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 569 | virtual int functionEntry(Type* pDecl) { |
| 570 | LOG_API("functionEntry(%d);\n", pDecl); |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 571 | mStackUse = 0; |
Jack Palevich | 69796b6 | 2009-05-14 15:42:26 -0700 | [diff] [blame] | 572 | // sp -> arg4 arg5 ... |
| 573 | // Push our register-based arguments back on the stack |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 574 | int regArgCount = calcRegArgCount(pDecl); |
| 575 | if (regArgCount > 0) { |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 576 | mStackUse += regArgCount * 4; |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 577 | o4(0xE92D0000 | ((1 << regArgCount) - 1)); // stmfd sp!, {} |
Jack Palevich | 69796b6 | 2009-05-14 15:42:26 -0700 | [diff] [blame] | 578 | } |
| 579 | // sp -> arg0 arg1 ... |
| 580 | o4(0xE92D4800); // stmfd sp!, {fp, lr} |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 581 | mStackUse += 2 * 4; |
Jack Palevich | 69796b6 | 2009-05-14 15:42:26 -0700 | [diff] [blame] | 582 | // sp, fp -> oldfp, retadr, arg0 arg1 .... |
| 583 | o4(0xE1A0B00D); // mov fp, sp |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 584 | LOG_STACK("functionEntry: %d\n", mStackUse); |
Jack Palevich | 69796b6 | 2009-05-14 15:42:26 -0700 | [diff] [blame] | 585 | return o4(0xE24DD000); // sub sp, sp, # <local variables> |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 586 | // We don't know how many local variables we are going to use, |
| 587 | // but we will round the allocation up to a multiple of |
| 588 | // STACK_ALIGNMENT, so it won't affect the stack alignment. |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 589 | } |
| 590 | |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 591 | virtual void functionExit(Type* pDecl, int localVariableAddress, int localVariableSize) { |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 592 | LOG_API("functionExit(%d, %d, %d);\n", argCount, localVariableAddress, localVariableSize); |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 593 | // Round local variable size up to a multiple of stack alignment |
| 594 | localVariableSize = ((localVariableSize + STACK_ALIGNMENT - 1) / |
| 595 | STACK_ALIGNMENT) * STACK_ALIGNMENT; |
Jack Palevich | 69796b6 | 2009-05-14 15:42:26 -0700 | [diff] [blame] | 596 | // Patch local variable allocation code: |
| 597 | if (localVariableSize < 0 || localVariableSize > 255) { |
Jack Palevich | 8de461d | 2009-05-14 17:21:45 -0700 | [diff] [blame] | 598 | error("localVariables out of range: %d", localVariableSize); |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 599 | } |
Jack Palevich | 69796b6 | 2009-05-14 15:42:26 -0700 | [diff] [blame] | 600 | *(char*) (localVariableAddress) = localVariableSize; |
| 601 | |
| 602 | // sp -> locals .... fp -> oldfp, retadr, arg0, arg1, ... |
| 603 | o4(0xE1A0E00B); // mov lr, fp |
| 604 | o4(0xE59BB000); // ldr fp, [fp] |
| 605 | o4(0xE28ED004); // add sp, lr, #4 |
| 606 | // sp -> retadr, arg0, ... |
| 607 | o4(0xE8BD4000); // ldmfd sp!, {lr} |
| 608 | // sp -> arg0 .... |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 609 | |
| 610 | // We store the PC into the lr so we can adjust the sp before |
| 611 | // returning. We need to pull off the registers we pushed |
| 612 | // earlier. We don't need to actually store them anywhere, |
| 613 | // just adjust the stack. |
| 614 | int regArgCount = calcRegArgCount(pDecl); |
| 615 | if (regArgCount) { |
Jack Palevich | 69796b6 | 2009-05-14 15:42:26 -0700 | [diff] [blame] | 616 | o4(0xE28DD000 | (regArgCount << 2)); // add sp, sp, #argCount << 2 |
| 617 | } |
| 618 | o4(0xE12FFF1E); // bx lr |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | /* load immediate value */ |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 622 | virtual void li(int t, Type* pType) { |
Jack Palevich | a8f427f | 2009-07-13 18:40:08 -0700 | [diff] [blame] | 623 | liReg(t, 0); |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 624 | setR0Type(pType); |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 625 | } |
| 626 | |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 627 | virtual void loadFloat(int address, Type* pType) { |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 628 | setR0Type(pType); |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 629 | // Global, absolute address |
| 630 | o4(0xE59F0000); // ldr r0, .L1 |
| 631 | o4(0xEA000000); // b .L99 |
| 632 | o4(address); // .L1: .word ea |
| 633 | // .L99: |
| 634 | |
| 635 | switch (pType->tag) { |
| 636 | case TY_FLOAT: |
| 637 | o4(0xE5900000); // ldr r0, [r0] |
| 638 | break; |
| 639 | case TY_DOUBLE: |
| 640 | o4(0xE1C000D0); // ldrd r0, [r0] |
| 641 | break; |
| 642 | default: |
| 643 | assert(false); |
| 644 | break; |
| 645 | } |
Jack Palevich | 1aeb87b | 2009-07-06 18:33:20 -0700 | [diff] [blame] | 646 | } |
| 647 | |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 648 | virtual int gjmp(int t) { |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 649 | LOG_API("gjmp(%d);\n", t); |
Jack Palevich | 8de461d | 2009-05-14 17:21:45 -0700 | [diff] [blame] | 650 | return o4(0xEA000000 | encodeAddress(t)); // b .L33 |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | /* l = 0: je, l == 1: jne */ |
| 654 | virtual int gtst(bool l, int t) { |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 655 | LOG_API("gtst(%d, %d);\n", l, t); |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 656 | Type* pR0Type = getR0Type(); |
| 657 | TypeTag tagR0 = pR0Type->tag; |
| 658 | switch(tagR0) { |
| 659 | case TY_FLOAT: |
| 660 | callRuntime((void*) runtime_is_non_zero_f); |
| 661 | break; |
| 662 | case TY_DOUBLE: |
| 663 | callRuntime((void*) runtime_is_non_zero_d); |
| 664 | break; |
| 665 | default: |
| 666 | break; |
| 667 | } |
Jack Palevich | 8de461d | 2009-05-14 17:21:45 -0700 | [diff] [blame] | 668 | o4(0xE3500000); // cmp r0,#0 |
| 669 | int branch = l ? 0x1A000000 : 0x0A000000; // bne : beq |
| 670 | return o4(branch | encodeAddress(t)); |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 671 | } |
| 672 | |
Jack Palevich | a39749f | 2009-07-08 20:40:31 -0700 | [diff] [blame] | 673 | virtual void gcmp(int op, Type* pResultType) { |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 674 | LOG_API("gcmp(%d);\n", op); |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 675 | Type* pR0Type = getR0Type(); |
| 676 | Type* pTOSType = getTOSType(); |
| 677 | TypeTag tagR0 = collapseType(pR0Type->tag); |
| 678 | TypeTag tagTOS = collapseType(pTOSType->tag); |
| 679 | if (tagR0 == TY_INT && tagTOS == TY_INT) { |
| 680 | o4(0xE8BD0002); // ldmfd sp!,{r1} |
| 681 | mStackUse -= 4; |
| 682 | o4(0xE1510000); // cmp r1, r1 |
| 683 | switch(op) { |
| 684 | case OP_EQUALS: |
| 685 | o4(0x03A00001); // moveq r0,#1 |
| 686 | o4(0x13A00000); // movne r0,#0 |
| 687 | break; |
| 688 | case OP_NOT_EQUALS: |
| 689 | o4(0x03A00000); // moveq r0,#0 |
| 690 | o4(0x13A00001); // movne r0,#1 |
| 691 | break; |
| 692 | case OP_LESS_EQUAL: |
| 693 | o4(0xD3A00001); // movle r0,#1 |
| 694 | o4(0xC3A00000); // movgt r0,#0 |
| 695 | break; |
| 696 | case OP_GREATER: |
| 697 | o4(0xD3A00000); // movle r0,#0 |
| 698 | o4(0xC3A00001); // movgt r0,#1 |
| 699 | break; |
| 700 | case OP_GREATER_EQUAL: |
| 701 | o4(0xA3A00001); // movge r0,#1 |
| 702 | o4(0xB3A00000); // movlt r0,#0 |
| 703 | break; |
| 704 | case OP_LESS: |
| 705 | o4(0xA3A00000); // movge r0,#0 |
| 706 | o4(0xB3A00001); // movlt r0,#1 |
| 707 | break; |
| 708 | default: |
| 709 | error("Unknown comparison op %d", op); |
| 710 | break; |
| 711 | } |
| 712 | popType(); |
| 713 | } else if (tagR0 == TY_DOUBLE || tagTOS == TY_DOUBLE) { |
| 714 | setupDoubleArgs(); |
| 715 | switch(op) { |
| 716 | case OP_EQUALS: |
| 717 | callRuntime((void*) runtime_cmp_eq_dd); |
| 718 | break; |
| 719 | case OP_NOT_EQUALS: |
| 720 | callRuntime((void*) runtime_cmp_ne_dd); |
| 721 | break; |
| 722 | case OP_LESS_EQUAL: |
| 723 | callRuntime((void*) runtime_cmp_le_dd); |
| 724 | break; |
| 725 | case OP_GREATER: |
| 726 | callRuntime((void*) runtime_cmp_gt_dd); |
| 727 | break; |
| 728 | case OP_GREATER_EQUAL: |
| 729 | callRuntime((void*) runtime_cmp_ge_dd); |
| 730 | break; |
| 731 | case OP_LESS: |
| 732 | callRuntime((void*) runtime_cmp_lt_dd); |
| 733 | break; |
| 734 | default: |
| 735 | error("Unknown comparison op %d", op); |
| 736 | break; |
| 737 | } |
| 738 | } else { |
| 739 | setupFloatArgs(); |
| 740 | switch(op) { |
| 741 | case OP_EQUALS: |
| 742 | callRuntime((void*) runtime_cmp_eq_ff); |
| 743 | break; |
| 744 | case OP_NOT_EQUALS: |
| 745 | callRuntime((void*) runtime_cmp_ne_ff); |
| 746 | break; |
| 747 | case OP_LESS_EQUAL: |
| 748 | callRuntime((void*) runtime_cmp_le_ff); |
| 749 | break; |
| 750 | case OP_GREATER: |
| 751 | callRuntime((void*) runtime_cmp_gt_ff); |
| 752 | break; |
| 753 | case OP_GREATER_EQUAL: |
| 754 | callRuntime((void*) runtime_cmp_ge_ff); |
| 755 | break; |
| 756 | case OP_LESS: |
| 757 | callRuntime((void*) runtime_cmp_lt_ff); |
| 758 | break; |
| 759 | default: |
| 760 | error("Unknown comparison op %d", op); |
| 761 | break; |
| 762 | } |
Jack Palevich | 8de461d | 2009-05-14 17:21:45 -0700 | [diff] [blame] | 763 | } |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 764 | setR0Type(pResultType); |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 765 | } |
| 766 | |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 767 | virtual void genOp(int op) { |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 768 | LOG_API("genOp(%d);\n", op); |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 769 | Type* pR0Type = getR0Type(); |
| 770 | Type* pTOSType = getTOSType(); |
Jack Palevich | a8f427f | 2009-07-13 18:40:08 -0700 | [diff] [blame] | 771 | TypeTag tagR0 = pR0Type->tag; |
| 772 | TypeTag tagTOS = pTOSType->tag; |
| 773 | bool isFloatR0 = isFloatTag(tagR0); |
| 774 | bool isFloatTOS = isFloatTag(tagTOS); |
| 775 | if (!isFloatR0 && !isFloatTOS) { |
| 776 | bool isPtrR0 = tagR0 == TY_POINTER; |
| 777 | bool isPtrTOS = tagTOS == TY_POINTER; |
| 778 | if (isPtrR0 || isPtrTOS) { |
| 779 | if (isPtrR0 && isPtrTOS) { |
| 780 | if (op != OP_MINUS) { |
| 781 | error("Unsupported pointer-pointer operation %d.", op); |
| 782 | } |
| 783 | if (! typeEqual(pR0Type, pTOSType)) { |
| 784 | error("Incompatible pointer types for subtraction."); |
| 785 | } |
| 786 | o4(0xE8BD0002); // ldmfd sp!,{r1} |
| 787 | o4(0xE0410000); // sub r0,r1,r0 |
| 788 | popType(); |
| 789 | setR0Type(mkpInt); |
| 790 | int size = sizeOf(pR0Type->pHead); |
| 791 | if (size != 1) { |
| 792 | pushR0(); |
| 793 | li(size, mkpInt); |
| 794 | // TODO: Optimize for power-of-two. |
| 795 | genOp(OP_DIV); |
| 796 | } |
| 797 | } else { |
| 798 | if (! (op == OP_PLUS || (op == OP_MINUS && isPtrR0))) { |
| 799 | error("Unsupported pointer-scalar operation %d", op); |
| 800 | } |
| 801 | Type* pPtrType = isPtrR0 ? pR0Type : pTOSType; |
| 802 | o4(0xE8BD0002); // ldmfd sp!,{r1} |
| 803 | int size = sizeOf(pPtrType->pHead); |
| 804 | if (size != 1) { |
| 805 | // TODO: Optimize for power-of-two. |
| 806 | liReg(size, 2); |
| 807 | if (isPtrR0) { |
| 808 | o4(0x0E0010192); // mul r1,r2,r1 |
| 809 | } else { |
| 810 | o4(0x0E0000092); // mul r0,r2,r0 |
| 811 | } |
| 812 | } |
| 813 | switch(op) { |
| 814 | case OP_PLUS: |
| 815 | o4(0xE0810000); // add r0,r1,r0 |
| 816 | break; |
| 817 | case OP_MINUS: |
| 818 | o4(0xE0410000); // sub r0,r1,r0 |
| 819 | break; |
| 820 | } |
| 821 | popType(); |
| 822 | setR0Type(pPtrType); |
| 823 | } |
| 824 | } else { |
| 825 | o4(0xE8BD0002); // ldmfd sp!,{r1} |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 826 | mStackUse -= 4; |
Jack Palevich | a8f427f | 2009-07-13 18:40:08 -0700 | [diff] [blame] | 827 | switch(op) { |
| 828 | case OP_MUL: |
| 829 | o4(0x0E0000091); // mul r0,r1,r0 |
| 830 | break; |
| 831 | case OP_DIV: |
| 832 | callRuntime((void*) runtime_DIV); |
| 833 | break; |
| 834 | case OP_MOD: |
| 835 | callRuntime((void*) runtime_MOD); |
| 836 | break; |
| 837 | case OP_PLUS: |
| 838 | o4(0xE0810000); // add r0,r1,r0 |
| 839 | break; |
| 840 | case OP_MINUS: |
| 841 | o4(0xE0410000); // sub r0,r1,r0 |
| 842 | break; |
| 843 | case OP_SHIFT_LEFT: |
| 844 | o4(0xE1A00011); // lsl r0,r1,r0 |
| 845 | break; |
| 846 | case OP_SHIFT_RIGHT: |
| 847 | o4(0xE1A00051); // asr r0,r1,r0 |
| 848 | break; |
| 849 | case OP_BIT_AND: |
| 850 | o4(0xE0010000); // and r0,r1,r0 |
| 851 | break; |
| 852 | case OP_BIT_XOR: |
| 853 | o4(0xE0210000); // eor r0,r1,r0 |
| 854 | break; |
| 855 | case OP_BIT_OR: |
| 856 | o4(0xE1810000); // orr r0,r1,r0 |
| 857 | break; |
| 858 | case OP_BIT_NOT: |
| 859 | o4(0xE1E00000); // mvn r0, r0 |
| 860 | break; |
| 861 | default: |
| 862 | error("Unimplemented op %d\n", op); |
| 863 | break; |
| 864 | } |
| 865 | popType(); |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 866 | } |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 867 | } else { |
| 868 | Type* pResultType = tagR0 > tagTOS ? pR0Type : pTOSType; |
| 869 | if (pResultType->tag == TY_DOUBLE) { |
| 870 | setupDoubleArgs(); |
| 871 | switch(op) { |
| 872 | case OP_MUL: |
| 873 | callRuntime((void*) runtime_op_mul_dd); |
| 874 | break; |
| 875 | case OP_DIV: |
| 876 | callRuntime((void*) runtime_op_div_dd); |
| 877 | break; |
| 878 | case OP_PLUS: |
| 879 | callRuntime((void*) runtime_op_add_dd); |
| 880 | break; |
| 881 | case OP_MINUS: |
| 882 | callRuntime((void*) runtime_op_sub_dd); |
| 883 | break; |
| 884 | default: |
| 885 | error("Unsupported binary floating operation %d\n", op); |
| 886 | break; |
| 887 | } |
| 888 | } else { |
| 889 | setupFloatArgs(); |
| 890 | switch(op) { |
| 891 | case OP_MUL: |
| 892 | callRuntime((void*) runtime_op_mul_ff); |
| 893 | break; |
| 894 | case OP_DIV: |
| 895 | callRuntime((void*) runtime_op_div_ff); |
| 896 | break; |
| 897 | case OP_PLUS: |
| 898 | callRuntime((void*) runtime_op_add_ff); |
| 899 | break; |
| 900 | case OP_MINUS: |
| 901 | callRuntime((void*) runtime_op_sub_ff); |
| 902 | break; |
| 903 | default: |
| 904 | error("Unsupported binary floating operation %d\n", op); |
| 905 | break; |
| 906 | } |
| 907 | } |
| 908 | setR0Type(pResultType); |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 909 | } |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 910 | } |
| 911 | |
Jack Palevich | a39749f | 2009-07-08 20:40:31 -0700 | [diff] [blame] | 912 | virtual void gUnaryCmp(int op, Type* pResultType) { |
| 913 | LOG_API("gUnaryCmp(%d);\n", op); |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 914 | if (op != OP_LOGICAL_NOT) { |
| 915 | error("Unknown unary cmp %d", op); |
| 916 | } else { |
| 917 | Type* pR0Type = getR0Type(); |
| 918 | TypeTag tag = collapseType(pR0Type->tag); |
| 919 | switch(tag) { |
| 920 | case TY_INT: |
| 921 | o4(0xE3A01000); // mov r1, #0 |
Jack Palevich | a8f427f | 2009-07-13 18:40:08 -0700 | [diff] [blame] | 922 | o4(0xE1510000); // cmp r1, r0 |
| 923 | o4(0x03A00001); // moveq r0,#1 |
| 924 | o4(0x13A00000); // movne r0,#0 |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 925 | break; |
| 926 | case TY_FLOAT: |
| 927 | callRuntime((void*) runtime_is_zero_f); |
| 928 | break; |
| 929 | case TY_DOUBLE: |
| 930 | callRuntime((void*) runtime_is_zero_d); |
| 931 | break; |
| 932 | default: |
| 933 | error("gUnaryCmp unsupported type"); |
| 934 | break; |
| 935 | } |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 936 | } |
Jack Palevich | a39749f | 2009-07-08 20:40:31 -0700 | [diff] [blame] | 937 | setR0Type(pResultType); |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | virtual void genUnaryOp(int op) { |
| 941 | LOG_API("genOp(%d);\n", op); |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 942 | Type* pR0Type = getR0Type(); |
| 943 | TypeTag tag = collapseType(pR0Type->tag); |
| 944 | switch(tag) { |
| 945 | case TY_INT: |
| 946 | switch(op) { |
| 947 | case OP_MINUS: |
| 948 | o4(0xE3A01000); // mov r1, #0 |
| 949 | o4(0xE0410000); // sub r0,r1,r0 |
| 950 | break; |
| 951 | case OP_BIT_NOT: |
| 952 | o4(0xE1E00000); // mvn r0, r0 |
| 953 | break; |
| 954 | default: |
| 955 | error("Unknown unary op %d\n", op); |
| 956 | break; |
| 957 | } |
| 958 | break; |
| 959 | case TY_FLOAT: |
| 960 | case TY_DOUBLE: |
| 961 | switch (op) { |
| 962 | case OP_MINUS: |
| 963 | if (tag == TY_FLOAT) { |
| 964 | callRuntime((void*) runtime_op_neg_f); |
| 965 | } else { |
| 966 | callRuntime((void*) runtime_op_neg_d); |
| 967 | } |
| 968 | break; |
| 969 | case OP_BIT_NOT: |
| 970 | error("Can't apply '~' operator to a float or double."); |
| 971 | break; |
| 972 | default: |
| 973 | error("Unknown unary op %d\n", op); |
| 974 | break; |
| 975 | } |
| 976 | break; |
| 977 | default: |
| 978 | error("genUnaryOp unsupported type"); |
| 979 | break; |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 980 | } |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 981 | } |
| 982 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 983 | virtual void pushR0() { |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 984 | LOG_API("pushR0();\n"); |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 985 | Type* pR0Type = getR0Type(); |
| 986 | TypeTag r0ct = collapseType(pR0Type->tag); |
| 987 | if (r0ct != TY_DOUBLE) { |
| 988 | o4(0xE92D0001); // stmfd sp!,{r0} |
| 989 | mStackUse += 4; |
| 990 | } else { |
| 991 | o4(0xE92D0003); // stmfd sp!,{r0,r1} |
| 992 | mStackUse += 8; |
| 993 | } |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 994 | pushType(); |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 995 | LOG_STACK("pushR0: %d\n", mStackUse); |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 996 | } |
| 997 | |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 998 | virtual void storeR0ToTOS(Type* pPointerType) { |
| 999 | LOG_API("storeR0ToTOS(%d);\n", isInt); |
| 1000 | assert(pPointerType->tag == TY_POINTER); |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1001 | o4(0xE8BD0004); // ldmfd sp!,{r2} |
| 1002 | popType(); |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 1003 | mStackUse -= 4; |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 1004 | switch (pPointerType->pHead->tag) { |
| 1005 | case TY_INT: |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1006 | case TY_FLOAT: |
| 1007 | o4(0xE5820000); // str r0, [r2] |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 1008 | break; |
| 1009 | case TY_CHAR: |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1010 | o4(0xE5C20000); // strb r0, [r2] |
| 1011 | break; |
| 1012 | case TY_DOUBLE: |
| 1013 | o4(0xE1C200F0); // strd r0, [r2] |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 1014 | break; |
| 1015 | default: |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 1016 | error("storeR0ToTOS: unimplemented type"); |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 1017 | break; |
Jack Palevich | bd89490 | 2009-05-14 19:35:31 -0700 | [diff] [blame] | 1018 | } |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 1019 | } |
| 1020 | |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 1021 | virtual void loadR0FromR0(Type* pPointerType) { |
| 1022 | LOG_API("loadR0FromR0(%d);\n", pPointerType); |
| 1023 | assert(pPointerType->tag == TY_POINTER); |
| 1024 | switch (pPointerType->pHead->tag) { |
| 1025 | case TY_INT: |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1026 | case TY_FLOAT: |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 1027 | o4(0xE5900000); // ldr r0, [r0] |
| 1028 | break; |
| 1029 | case TY_CHAR: |
| 1030 | o4(0xE5D00000); // ldrb r0, [r0] |
| 1031 | break; |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1032 | case TY_DOUBLE: |
| 1033 | o4(0xE1C000D0); // ldrd r0, [r0] |
| 1034 | break; |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 1035 | default: |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 1036 | error("loadR0FromR0: unimplemented type"); |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 1037 | break; |
| 1038 | } |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 1039 | setR0Type(pPointerType->pHead); |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 1040 | } |
| 1041 | |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 1042 | virtual void leaR0(int ea, Type* pPointerType) { |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 1043 | LOG_API("leaR0(%d);\n", ea); |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1044 | if (ea > -LOCAL && ea < LOCAL) { |
Jack Palevich | 4d93f30 | 2009-05-15 13:30:00 -0700 | [diff] [blame] | 1045 | // Local, fp relative |
| 1046 | if (ea < -1023 || ea > 1023 || ((ea & 3) != 0)) { |
| 1047 | error("Offset out of range: %08x", ea); |
| 1048 | } |
| 1049 | if (ea < 0) { |
| 1050 | o4(0xE24B0F00 | (0xff & ((-ea) >> 2))); // sub r0, fp, #ea |
| 1051 | } else { |
| 1052 | o4(0xE28B0F00 | (0xff & (ea >> 2))); // add r0, fp, #ea |
| 1053 | } |
Jack Palevich | bd89490 | 2009-05-14 19:35:31 -0700 | [diff] [blame] | 1054 | } else { |
Jack Palevich | 4d93f30 | 2009-05-15 13:30:00 -0700 | [diff] [blame] | 1055 | // Global, absolute. |
| 1056 | o4(0xE59F0000); // ldr r0, .L1 |
| 1057 | o4(0xEA000000); // b .L99 |
| 1058 | o4(ea); // .L1: .word 0 |
| 1059 | // .L99: |
Jack Palevich | bd89490 | 2009-05-14 19:35:31 -0700 | [diff] [blame] | 1060 | } |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 1061 | setR0Type(pPointerType); |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 1062 | } |
| 1063 | |
Jack Palevich | 9cbd226 | 2009-07-08 16:48:41 -0700 | [diff] [blame] | 1064 | virtual void storeR0(int ea, Type* pType) { |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 1065 | LOG_API("storeR0(%d);\n", ea); |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1066 | TypeTag tag = pType->tag; |
| 1067 | switch (tag) { |
Jack Palevich | 25c0cca | 2009-07-13 16:56:28 -0700 | [diff] [blame] | 1068 | case TY_CHAR: |
| 1069 | if (ea > -LOCAL && ea < LOCAL) { |
| 1070 | // Local, fp relative |
| 1071 | if (ea < -4095 || ea > 4095) { |
| 1072 | error("Offset out of range: %08x", ea); |
| 1073 | } |
| 1074 | if (ea < 0) { |
| 1075 | o4(0xE54B0000 | (0xfff & (-ea))); // strb r0, [fp,#-ea] |
| 1076 | } else { |
| 1077 | o4(0xE5CB0000 | (0xfff & ea)); // strb r0, [fp,#ea] |
| 1078 | } |
| 1079 | } else{ |
| 1080 | // Global, absolute |
| 1081 | o4(0xE59F1000); // ldr r1, .L1 |
| 1082 | o4(0xEA000000); // b .L99 |
| 1083 | o4(ea); // .L1: .word 0 |
| 1084 | o4(0xE5C10000); // .L99: strb r0, [r1] |
| 1085 | } |
| 1086 | break; |
Jack Palevich | 45431bc | 2009-07-13 15:57:26 -0700 | [diff] [blame] | 1087 | case TY_POINTER: |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1088 | case TY_INT: |
| 1089 | case TY_FLOAT: |
| 1090 | if (ea > -LOCAL && ea < LOCAL) { |
| 1091 | // Local, fp relative |
| 1092 | if (ea < -4095 || ea > 4095) { |
| 1093 | error("Offset out of range: %08x", ea); |
| 1094 | } |
| 1095 | if (ea < 0) { |
| 1096 | o4(0xE50B0000 | (0xfff & (-ea))); // str r0, [fp,#-ea] |
| 1097 | } else { |
| 1098 | o4(0xE58B0000 | (0xfff & ea)); // str r0, [fp,#ea] |
| 1099 | } |
| 1100 | } else{ |
| 1101 | // Global, absolute |
| 1102 | o4(0xE59F1000); // ldr r1, .L1 |
| 1103 | o4(0xEA000000); // b .L99 |
| 1104 | o4(ea); // .L1: .word 0 |
| 1105 | o4(0xE5810000); // .L99: str r0, [r1] |
| 1106 | } |
| 1107 | break; |
| 1108 | case TY_DOUBLE: |
| 1109 | if ((ea & 0x7) != 0) { |
| 1110 | error("double address is not aligned: %d", ea); |
| 1111 | } |
| 1112 | if (ea > -LOCAL && ea < LOCAL) { |
| 1113 | // Local, fp relative |
| 1114 | if (ea < -4095 || ea > 4095) { |
| 1115 | error("Offset out of range: %08x", ea); |
| 1116 | } |
| 1117 | if (ea < 0) { |
| 1118 | o4(0xE50B0000 | (0xfff & (-ea))); // str r0, [fp,#-ea] |
| 1119 | o4(0xE50B1000 | (0xfff & (-ea + 4))); // str r1, [fp,#-ea+4] |
| 1120 | #if 0 |
| 1121 | // strd doesn't seem to work. Is encoding wrong? |
| 1122 | } else if (ea < 0) { |
| 1123 | o4(0xE1CB000F | ((0xff & (-ea)) << 4)); // strd r0, [fp,#-ea] |
| 1124 | } else if (ea < 256) { |
| 1125 | o4(0xE14B000F | ((0xff & ea) << 4)); // strd r0, [fp,#ea] |
| 1126 | #endif |
| 1127 | } else { |
| 1128 | o4(0xE58B0000 | (0xfff & ea)); // str r0, [fp,#ea] |
| 1129 | o4(0xE58B1000 | (0xfff & (ea + 4))); // str r1, [fp,#ea+4] |
| 1130 | } |
| 1131 | } else{ |
| 1132 | // Global, absolute |
| 1133 | o4(0xE59F2000); // ldr r2, .L1 |
| 1134 | o4(0xEA000000); // b .L99 |
| 1135 | o4(ea); // .L1: .word 0 |
| 1136 | o4(0xE1C200F0); // .L99: strd r0, [r2] |
| 1137 | } |
| 1138 | break; |
| 1139 | default: |
| 1140 | error("Unable to store to type %d", tag); |
| 1141 | break; |
Jack Palevich | 69796b6 | 2009-05-14 15:42:26 -0700 | [diff] [blame] | 1142 | } |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 1143 | } |
| 1144 | |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 1145 | virtual void loadR0(int ea, bool isIncDec, int op, Type* pType) { |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 1146 | LOG_API("loadR0(%d, %d, %d, %d);\n", ea, isIncDec, op, pType); |
Jack Palevich | 25c0cca | 2009-07-13 16:56:28 -0700 | [diff] [blame] | 1147 | TypeTag tag = pType->tag; |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1148 | switch (tag) { |
Jack Palevich | 25c0cca | 2009-07-13 16:56:28 -0700 | [diff] [blame] | 1149 | case TY_CHAR: |
| 1150 | if (ea < LOCAL) { |
| 1151 | // Local, fp relative |
| 1152 | if (ea < -4095 || ea > 4095) { |
| 1153 | error("Offset out of range: %08x", ea); |
| 1154 | } |
| 1155 | if (ea < 0) { |
| 1156 | o4(0xE55B0000 | (0xfff & (-ea))); // ldrb r0, [fp,#-ea] |
| 1157 | } else { |
| 1158 | o4(0xE5DB0000 | (0xfff & ea)); // ldrb r0, [fp,#ea] |
| 1159 | } |
| 1160 | } else { |
| 1161 | // Global, absolute |
| 1162 | o4(0xE59F2000); // ldr r2, .L1 |
| 1163 | o4(0xEA000000); // b .L99 |
| 1164 | o4(ea); // .L1: .word ea |
| 1165 | o4(0xE5D20000); // .L99: ldrb r0, [r2] |
| 1166 | } |
| 1167 | |
| 1168 | if (isIncDec) { |
| 1169 | error("inc/dec not implemented for char."); |
| 1170 | } |
| 1171 | break; |
| 1172 | case TY_POINTER: |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1173 | case TY_INT: |
| 1174 | case TY_FLOAT: |
| 1175 | if (ea < LOCAL) { |
| 1176 | // Local, fp relative |
| 1177 | if (ea < -4095 || ea > 4095) { |
| 1178 | error("Offset out of range: %08x", ea); |
| 1179 | } |
| 1180 | if (ea < 0) { |
| 1181 | o4(0xE51B0000 | (0xfff & (-ea))); // ldr r0, [fp,#-ea] |
| 1182 | } else { |
| 1183 | o4(0xE59B0000 | (0xfff & ea)); // ldr r0, [fp,#ea] |
| 1184 | } |
| 1185 | } else { |
| 1186 | // Global, absolute |
| 1187 | o4(0xE59F2000); // ldr r2, .L1 |
| 1188 | o4(0xEA000000); // b .L99 |
| 1189 | o4(ea); // .L1: .word ea |
| 1190 | o4(0xE5920000); // .L99: ldr r0, [r2] |
| 1191 | } |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 1192 | |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1193 | if (isIncDec) { |
| 1194 | if (tag == TY_INT) { |
| 1195 | switch (op) { |
| 1196 | case OP_INCREMENT: |
| 1197 | o4(0xE2801001); // add r1, r0, #1 |
| 1198 | break; |
| 1199 | case OP_DECREMENT: |
| 1200 | o4(0xE2401001); // sub r1, r0, #1 |
| 1201 | break; |
| 1202 | default: |
| 1203 | error("unknown opcode: %d", op); |
| 1204 | } |
| 1205 | if (ea < LOCAL) { |
| 1206 | // Local, fp relative |
| 1207 | // Don't need range check, was already checked above |
| 1208 | if (ea < 0) { |
| 1209 | o4(0xE50B1000 | (0xfff & (-ea))); // str r1, [fp,#-ea] |
| 1210 | } else { |
| 1211 | o4(0xE58B1000 | (0xfff & ea)); // str r1, [fp,#ea] |
| 1212 | } |
| 1213 | } else{ |
| 1214 | // Global, absolute |
| 1215 | // r2 is already set up from before. |
| 1216 | o4(0xE5821000); // str r1, [r2] |
| 1217 | } |
| 1218 | } |
| 1219 | else { |
| 1220 | error("inc/dec not implemented for float."); |
| 1221 | } |
| 1222 | } |
Jack Palevich | 4d93f30 | 2009-05-15 13:30:00 -0700 | [diff] [blame] | 1223 | break; |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1224 | case TY_DOUBLE: |
| 1225 | if ((ea & 0x7) != 0) { |
| 1226 | error("double address is not aligned: %d", ea); |
| 1227 | } |
| 1228 | if (ea < LOCAL) { |
| 1229 | // Local, fp relative |
| 1230 | if (ea < -4095 || ea > 4095) { |
| 1231 | error("Offset out of range: %08x", ea); |
| 1232 | } |
| 1233 | if (ea < 0) { |
| 1234 | o4(0xE51B0000 | (0xfff & (-ea))); // ldr r0, [fp,#-ea] |
| 1235 | o4(0xE51B1000 | (0xfff & (-ea+4))); // ldr r1, [fp,#-ea+4] |
| 1236 | } else { |
| 1237 | o4(0xE59B0000 | (0xfff & ea)); // ldr r0, [fp,#ea] |
| 1238 | o4(0xE59B1000 | (0xfff & (ea+4))); // ldr r0, [fp,#ea+4] |
| 1239 | } |
| 1240 | } else { |
| 1241 | // Global, absolute |
| 1242 | o4(0xE59F2000); // ldr r2, .L1 |
| 1243 | o4(0xEA000000); // b .L99 |
| 1244 | o4(ea); // .L1: .word ea |
| 1245 | o4(0xE1C200D0); // .L99: ldrd r0, [r2] |
| 1246 | } |
Jack Palevich | 4d93f30 | 2009-05-15 13:30:00 -0700 | [diff] [blame] | 1247 | break; |
| 1248 | default: |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1249 | error("Unable to load type %d", tag); |
| 1250 | break; |
Jack Palevich | bd89490 | 2009-05-14 19:35:31 -0700 | [diff] [blame] | 1251 | } |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 1252 | setR0Type(pType); |
| 1253 | } |
| 1254 | |
| 1255 | virtual void convertR0(Type* pType){ |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 1256 | Type* pR0Type = getR0Type(); |
| 1257 | if (bitsSame(pType, pR0Type)) { |
| 1258 | // do nothing special |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 1259 | } else { |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1260 | TypeTag r0Tag = collapseType(pR0Type->tag); |
| 1261 | TypeTag destTag = collapseType(pType->tag); |
| 1262 | if (r0Tag == TY_INT) { |
| 1263 | if (destTag == TY_FLOAT) { |
| 1264 | callRuntime((void*) runtime_int_to_float); |
| 1265 | } else { |
| 1266 | assert(destTag == TY_DOUBLE); |
| 1267 | callRuntime((void*) runtime_int_to_double); |
| 1268 | } |
| 1269 | } else if (r0Tag == TY_FLOAT) { |
| 1270 | if (destTag == TY_INT) { |
| 1271 | callRuntime((void*) runtime_float_to_int); |
| 1272 | } else { |
| 1273 | assert(destTag == TY_DOUBLE); |
| 1274 | callRuntime((void*) runtime_float_to_double); |
| 1275 | } |
| 1276 | } else { |
| 1277 | assert (r0Tag == TY_DOUBLE); |
| 1278 | if (destTag == TY_INT) { |
| 1279 | callRuntime((void*) runtime_double_to_int); |
| 1280 | } else { |
| 1281 | assert(destTag == TY_FLOAT); |
| 1282 | callRuntime((void*) runtime_double_to_float); |
| 1283 | } |
| 1284 | } |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 1285 | } |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 1286 | setR0Type(pType); |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 1287 | } |
| 1288 | |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 1289 | virtual int beginFunctionCallArguments() { |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 1290 | LOG_API("beginFunctionCallArguments();\n"); |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 1291 | return o4(0xE24DDF00); // Placeholder |
| 1292 | } |
| 1293 | |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 1294 | virtual size_t storeR0ToArg(int l) { |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 1295 | LOG_API("storeR0ToArg(%d);\n", l); |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1296 | Type* pR0Type = getR0Type(); |
| 1297 | TypeTag r0ct = collapseType(pR0Type->tag); |
| 1298 | switch(r0ct) { |
| 1299 | case TY_INT: |
| 1300 | case TY_FLOAT: |
| 1301 | if (l < 0 || l > 4096-4) { |
| 1302 | error("l out of range for stack offset: 0x%08x", l); |
| 1303 | } |
| 1304 | o4(0xE58D0000 + l); // str r0, [sp, #l] |
| 1305 | return 4; |
| 1306 | case TY_DOUBLE: { |
| 1307 | // Align to 8 byte boundary |
| 1308 | int l2 = (l + 7) & ~7; |
| 1309 | if (l2 < 0 || l2 > 4096-8) { |
| 1310 | error("l out of range for stack offset: 0x%08x", l); |
| 1311 | } |
| 1312 | o4(0xE58D0000 + l2); // str r0, [sp, #l] |
| 1313 | o4(0xE58D1000 + l2 + 4); // str r1, [sp, #l+4] |
| 1314 | return (l2 - l) + 8; |
| 1315 | } |
| 1316 | default: |
| 1317 | assert(false); |
| 1318 | return 0; |
Jack Palevich | 7810bc9 | 2009-05-15 14:31:47 -0700 | [diff] [blame] | 1319 | } |
Jack Palevich | 7810bc9 | 2009-05-15 14:31:47 -0700 | [diff] [blame] | 1320 | } |
| 1321 | |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1322 | virtual void endFunctionCallArguments(Type* pDecl, int a, int l) { |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 1323 | LOG_API("endFunctionCallArguments(0x%08x, %d);\n", a, l); |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 1324 | int argumentStackUse = l; |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1325 | // Have to calculate register arg count from actual stack size, |
| 1326 | // in order to properly handle ... functions. |
| 1327 | int regArgCount = l >> 2; |
| 1328 | if (regArgCount > 4) { |
| 1329 | regArgCount = 4; |
| 1330 | } |
| 1331 | if (regArgCount > 0) { |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 1332 | argumentStackUse -= regArgCount * 4; |
| 1333 | o4(0xE8BD0000 | ((1 << regArgCount) - 1)); // ldmfd sp!,{} |
| 1334 | } |
| 1335 | mStackUse += argumentStackUse; |
| 1336 | |
| 1337 | // Align stack. |
| 1338 | int missalignment = mStackUse - ((mStackUse / STACK_ALIGNMENT) |
| 1339 | * STACK_ALIGNMENT); |
| 1340 | mStackAlignmentAdjustment = 0; |
| 1341 | if (missalignment > 0) { |
| 1342 | mStackAlignmentAdjustment = STACK_ALIGNMENT - missalignment; |
| 1343 | } |
| 1344 | l += mStackAlignmentAdjustment; |
| 1345 | |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 1346 | if (l < 0 || l > 0x3FC) { |
| 1347 | error("L out of range for stack adjustment: 0x%08x", l); |
| 1348 | } |
| 1349 | * (int*) a = 0xE24DDF00 | (l >> 2); // sub sp, sp, #0 << 2 |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 1350 | mStackUse += mStackAlignmentAdjustment; |
| 1351 | LOG_STACK("endFunctionCallArguments mStackUse: %d, mStackAlignmentAdjustment %d\n", |
| 1352 | mStackUse, mStackAlignmentAdjustment); |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 1353 | } |
| 1354 | |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 1355 | virtual int callForward(int symbol, Type* pFunc) { |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 1356 | LOG_API("callForward(%d);\n", symbol); |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 1357 | setR0Type(pFunc->pHead); |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 1358 | // Forward calls are always short (local) |
| 1359 | return o4(0xEB000000 | encodeAddress(symbol)); |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 1360 | } |
| 1361 | |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 1362 | virtual void callRelative(int t, Type* pFunc) { |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 1363 | LOG_API("callRelative(%d);\n", t); |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 1364 | setR0Type(pFunc->pHead); |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 1365 | int abs = t + getPC() + jumpOffset(); |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 1366 | LOG_API("abs=%d (0x%08x)\n", abs, abs); |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 1367 | if (t >= - (1 << 25) && t < (1 << 25)) { |
| 1368 | o4(0xEB000000 | encodeAddress(t)); |
| 1369 | } else { |
| 1370 | // Long call. |
| 1371 | o4(0xE59FC000); // ldr r12, .L1 |
| 1372 | o4(0xEA000000); // b .L99 |
Jack Palevich | bd89490 | 2009-05-14 19:35:31 -0700 | [diff] [blame] | 1373 | o4(t - 12); // .L1: .word 0 |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 1374 | o4(0xE08CC00F); // .L99: add r12,pc |
| 1375 | o4(0xE12FFF3C); // blx r12 |
| 1376 | } |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 1377 | } |
| 1378 | |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 1379 | virtual void callIndirect(int l, Type* pFunc) { |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 1380 | LOG_API("callIndirect(%d);\n", l); |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 1381 | setR0Type(pFunc->pHead); |
Jack Palevich | 7810bc9 | 2009-05-15 14:31:47 -0700 | [diff] [blame] | 1382 | int argCount = l >> 2; |
| 1383 | int poppedArgs = argCount > 4 ? 4 : argCount; |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 1384 | int adjustedL = l - (poppedArgs << 2) + mStackAlignmentAdjustment; |
Jack Palevich | 7810bc9 | 2009-05-15 14:31:47 -0700 | [diff] [blame] | 1385 | if (adjustedL < 0 || adjustedL > 4096-4) { |
| 1386 | error("l out of range for stack offset: 0x%08x", l); |
| 1387 | } |
| 1388 | o4(0xE59DC000 | (0xfff & adjustedL)); // ldr r12, [sp,#adjustedL] |
| 1389 | o4(0xE12FFF3C); // blx r12 |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 1390 | } |
| 1391 | |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1392 | virtual void adjustStackAfterCall(Type* pDecl, int l, bool isIndirect) { |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 1393 | LOG_API("adjustStackAfterCall(%d, %d);\n", l, isIndirect); |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 1394 | int argCount = l >> 2; |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1395 | // Have to calculate register arg count from actual stack size, |
| 1396 | // in order to properly handle ... functions. |
| 1397 | int regArgCount = l >> 2; |
| 1398 | if (regArgCount > 4) { |
| 1399 | regArgCount = 4; |
| 1400 | } |
| 1401 | int stackArgs = argCount - regArgCount; |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 1402 | int stackUse = stackArgs + (isIndirect ? 1 : 0) |
| 1403 | + (mStackAlignmentAdjustment >> 2); |
Jack Palevich | 7810bc9 | 2009-05-15 14:31:47 -0700 | [diff] [blame] | 1404 | if (stackUse) { |
| 1405 | if (stackUse < 0 || stackUse > 255) { |
| 1406 | error("L out of range for stack adjustment: 0x%08x", l); |
| 1407 | } |
| 1408 | o4(0xE28DDF00 | stackUse); // add sp, sp, #stackUse << 2 |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 1409 | mStackUse -= stackUse * 4; |
| 1410 | LOG_STACK("adjustStackAfterCall: %d\n", mStackUse); |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 1411 | } |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 1412 | } |
| 1413 | |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 1414 | virtual int jumpOffset() { |
Jack Palevich | bd89490 | 2009-05-14 19:35:31 -0700 | [diff] [blame] | 1415 | return 8; |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 1416 | } |
| 1417 | |
| 1418 | /* output a symbol and patch all calls to it */ |
| 1419 | virtual void gsym(int t) { |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 1420 | LOG_API("gsym(0x%x)\n", t); |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 1421 | int n; |
| 1422 | int base = getBase(); |
| 1423 | int pc = getPC(); |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 1424 | LOG_API("pc = 0x%x\n", pc); |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 1425 | while (t) { |
| 1426 | int data = * (int*) t; |
| 1427 | int decodedOffset = ((BRANCH_REL_ADDRESS_MASK & data) << 2); |
| 1428 | if (decodedOffset == 0) { |
| 1429 | n = 0; |
| 1430 | } else { |
| 1431 | n = base + decodedOffset; /* next value */ |
| 1432 | } |
| 1433 | *(int *) t = (data & ~BRANCH_REL_ADDRESS_MASK) |
| 1434 | | encodeRelAddress(pc - t - 8); |
| 1435 | t = n; |
| 1436 | } |
| 1437 | } |
| 1438 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 1439 | virtual int finishCompile() { |
| 1440 | #if defined(__arm__) |
| 1441 | const long base = long(getBase()); |
| 1442 | const long curr = long(getPC()); |
| 1443 | int err = cacheflush(base, curr, 0); |
| 1444 | return err; |
| 1445 | #else |
| 1446 | return 0; |
| 1447 | #endif |
| 1448 | } |
| 1449 | |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 1450 | virtual int disassemble(FILE* out) { |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 1451 | #ifdef ENABLE_ARM_DISASSEMBLY |
| 1452 | disasmOut = out; |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 1453 | disasm_interface_t di; |
| 1454 | di.di_readword = disassemble_readword; |
| 1455 | di.di_printaddr = disassemble_printaddr; |
| 1456 | di.di_printf = disassemble_printf; |
| 1457 | |
| 1458 | int base = getBase(); |
| 1459 | int pc = getPC(); |
| 1460 | for(int i = base; i < pc; i += 4) { |
| 1461 | fprintf(out, "%08x: %08x ", i, *(int*) i); |
| 1462 | ::disasm(&di, i, 0); |
| 1463 | } |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 1464 | #endif |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 1465 | return 0; |
| 1466 | } |
Jack Palevich | 7810bc9 | 2009-05-15 14:31:47 -0700 | [diff] [blame] | 1467 | |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 1468 | /** |
Jack Palevich | 9cbd226 | 2009-07-08 16:48:41 -0700 | [diff] [blame] | 1469 | * alignment (in bytes) for this type of data |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 1470 | */ |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1471 | virtual size_t alignmentOf(Type* pType){ |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 1472 | switch(pType->tag) { |
| 1473 | case TY_DOUBLE: |
| 1474 | return 8; |
| 1475 | default: |
| 1476 | return 4; |
| 1477 | } |
| 1478 | } |
| 1479 | |
| 1480 | /** |
| 1481 | * Array element alignment (in bytes) for this type of data. |
| 1482 | */ |
| 1483 | virtual size_t sizeOf(Type* pType){ |
| 1484 | switch(pType->tag) { |
| 1485 | case TY_INT: |
| 1486 | return 4; |
| 1487 | case TY_CHAR: |
| 1488 | return 1; |
| 1489 | default: |
| 1490 | return 0; |
| 1491 | case TY_FLOAT: |
| 1492 | return 4; |
| 1493 | case TY_DOUBLE: |
| 1494 | return 8; |
| 1495 | case TY_POINTER: |
| 1496 | return 4; |
| 1497 | } |
| 1498 | } |
Jack Palevich | 9cbd226 | 2009-07-08 16:48:41 -0700 | [diff] [blame] | 1499 | |
| 1500 | virtual size_t stackSizeOf(Type* pType) { |
| 1501 | switch(pType->tag) { |
| 1502 | case TY_DOUBLE: |
| 1503 | return 8; |
| 1504 | default: |
| 1505 | return 4; |
| 1506 | } |
| 1507 | } |
| 1508 | |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 1509 | private: |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 1510 | static FILE* disasmOut; |
| 1511 | |
| 1512 | static u_int |
| 1513 | disassemble_readword(u_int address) |
| 1514 | { |
| 1515 | return(*((u_int *)address)); |
| 1516 | } |
| 1517 | |
| 1518 | static void |
| 1519 | disassemble_printaddr(u_int address) |
| 1520 | { |
| 1521 | fprintf(disasmOut, "0x%08x", address); |
| 1522 | } |
| 1523 | |
| 1524 | static void |
| 1525 | disassemble_printf(const char *fmt, ...) { |
| 1526 | va_list ap; |
| 1527 | va_start(ap, fmt); |
| 1528 | vfprintf(disasmOut, fmt, ap); |
| 1529 | va_end(ap); |
| 1530 | } |
| 1531 | |
| 1532 | static const int BRANCH_REL_ADDRESS_MASK = 0x00ffffff; |
| 1533 | |
| 1534 | /** Encode a relative address that might also be |
| 1535 | * a label. |
| 1536 | */ |
| 1537 | int encodeAddress(int value) { |
| 1538 | int base = getBase(); |
| 1539 | if (value >= base && value <= getPC() ) { |
| 1540 | // This is a label, encode it relative to the base. |
| 1541 | value = value - base; |
| 1542 | } |
| 1543 | return encodeRelAddress(value); |
| 1544 | } |
| 1545 | |
| 1546 | int encodeRelAddress(int value) { |
| 1547 | return BRANCH_REL_ADDRESS_MASK & (value >> 2); |
| 1548 | } |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 1549 | |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1550 | int calcRegArgCount(Type* pDecl) { |
| 1551 | int reg = 0; |
| 1552 | Type* pArgs = pDecl->pTail; |
| 1553 | while (pArgs && reg < 4) { |
| 1554 | Type* pArg = pArgs->pHead; |
| 1555 | if ( pArg->tag == TY_DOUBLE) { |
| 1556 | int evenReg = (reg + 1) & ~1; |
| 1557 | if (evenReg >= 4) { |
| 1558 | break; |
| 1559 | } |
| 1560 | reg = evenReg + 2; |
| 1561 | } else { |
| 1562 | reg++; |
| 1563 | } |
| 1564 | pArgs = pArgs->pTail; |
| 1565 | } |
| 1566 | return reg; |
| 1567 | } |
| 1568 | |
| 1569 | /* Pop TOS to R1 |
| 1570 | * Make sure both R0 and TOS are floats. (Could be ints) |
| 1571 | * We know that at least one of R0 and TOS is already a float |
| 1572 | */ |
| 1573 | void setupFloatArgs() { |
| 1574 | Type* pR0Type = getR0Type(); |
| 1575 | Type* pTOSType = getTOSType(); |
| 1576 | TypeTag tagR0 = collapseType(pR0Type->tag); |
| 1577 | TypeTag tagTOS = collapseType(pTOSType->tag); |
| 1578 | if (tagR0 != TY_FLOAT) { |
| 1579 | assert(tagR0 == TY_INT); |
| 1580 | callRuntime((void*) runtime_int_to_float); |
| 1581 | } |
| 1582 | if (tagTOS != TY_FLOAT) { |
| 1583 | assert(tagTOS == TY_INT); |
| 1584 | assert(tagR0 == TY_FLOAT); |
| 1585 | o4(0xE92D0001); // stmfd sp!,{r0} // push R0 |
| 1586 | o4(0xE59D0004); // ldr r0, [sp, #4] |
| 1587 | callRuntime((void*) runtime_int_to_float); |
| 1588 | o4(0xE1A01000); // mov r1, r0 |
| 1589 | o4(0xE8BD0001); // ldmfd sp!,{r0} // pop R0 |
| 1590 | o4(0xE28DD004); // add sp, sp, #4 // Pop sp |
| 1591 | } else { |
| 1592 | // Pop TOS |
| 1593 | o4(0xE8BD0002); // ldmfd sp!,{r1} |
| 1594 | } |
| 1595 | mStackUse -= 4; |
| 1596 | popType(); |
| 1597 | } |
| 1598 | |
| 1599 | /* Pop TOS into R2..R3 |
| 1600 | * Make sure both R0 and TOS are doubles. Could be floats or ints. |
| 1601 | * We know that at least one of R0 and TOS are already a double. |
| 1602 | */ |
| 1603 | |
| 1604 | void setupDoubleArgs() { |
| 1605 | Type* pR0Type = getR0Type(); |
| 1606 | Type* pTOSType = getTOSType(); |
| 1607 | TypeTag tagR0 = collapseType(pR0Type->tag); |
| 1608 | TypeTag tagTOS = collapseType(pTOSType->tag); |
| 1609 | if (tagR0 != TY_DOUBLE) { |
| 1610 | if (tagR0 == TY_INT) { |
| 1611 | callRuntime((void*) runtime_int_to_double); |
| 1612 | } else { |
| 1613 | assert(tagR0 == TY_FLOAT); |
| 1614 | callRuntime((void*) runtime_float_to_double); |
| 1615 | } |
| 1616 | } |
| 1617 | if (tagTOS != TY_DOUBLE) { |
| 1618 | o4(0xE92D0003); // stmfd sp!,{r0,r1} // push r0,r1 |
| 1619 | o4(0xE59D0008); // ldr r0, [sp, #8] |
| 1620 | if (tagTOS == TY_INT) { |
| 1621 | callRuntime((void*) runtime_int_to_double); |
| 1622 | } else { |
| 1623 | assert(tagTOS == TY_FLOAT); |
| 1624 | callRuntime((void*) runtime_float_to_double); |
| 1625 | } |
| 1626 | o4(0xE1A02000); // mov r2, r0 |
| 1627 | o4(0xE1A03001); // mov r3, r1 |
| 1628 | o4(0xE8BD0003); // ldmfd sp!,{r0, r1} // Restore R0 |
| 1629 | o4(0xE28DD004); // add sp, sp, #4 // Pop sp |
| 1630 | mStackUse -= 4; |
| 1631 | } else { |
| 1632 | o4(0xE8BD000C); // ldmfd sp!,{r2,r3} |
| 1633 | mStackUse -= 8; |
| 1634 | } |
| 1635 | popType(); |
| 1636 | } |
| 1637 | |
Jack Palevich | a8f427f | 2009-07-13 18:40:08 -0700 | [diff] [blame] | 1638 | void liReg(int t, int reg) { |
| 1639 | assert(reg >= 0 && reg < 16); |
| 1640 | int rN = (reg & 0xf) << 12; |
| 1641 | if (t >= 0 && t < 255) { |
| 1642 | o4((0xE3A00000 + t) | rN); // mov rN, #0 |
| 1643 | } else if (t >= -256 && t < 0) { |
| 1644 | // mvn means move constant ^ ~0 |
| 1645 | o4((0xE3E00001 - t) | rN); // mvn rN, #0 |
| 1646 | } else { |
| 1647 | o4(0xE51F0000 | rN); // ldr rN, .L3 |
| 1648 | o4(0xEA000000); // b .L99 |
| 1649 | o4(t); // .L3: .word 0 |
| 1650 | // .L99: |
| 1651 | } |
| 1652 | } |
| 1653 | |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1654 | void callRuntime(void* fn) { |
| 1655 | o4(0xE59FC000); // ldr r12, .L1 |
Jack Palevich | 3d474a7 | 2009-05-15 15:12:38 -0700 | [diff] [blame] | 1656 | o4(0xEA000000); // b .L99 |
| 1657 | o4((int) fn); //.L1: .word fn |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1658 | o4(0xE12FFF3C); //.L99: blx r12 |
Jack Palevich | 3d474a7 | 2009-05-15 15:12:38 -0700 | [diff] [blame] | 1659 | } |
| 1660 | |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1661 | // Integer math: |
| 1662 | |
| 1663 | static int runtime_DIV(int b, int a) { |
| 1664 | return a / b; |
Jack Palevich | 3d474a7 | 2009-05-15 15:12:38 -0700 | [diff] [blame] | 1665 | } |
| 1666 | |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1667 | static int runtime_MOD(int b, int a) { |
| 1668 | return a % b; |
| 1669 | } |
| 1670 | |
| 1671 | // Comparison to zero |
| 1672 | |
| 1673 | static int runtime_is_non_zero_f(float a) { |
| 1674 | return a != 0; |
| 1675 | } |
| 1676 | |
| 1677 | static int runtime_is_non_zero_d(double a) { |
| 1678 | return a != 0; |
| 1679 | } |
| 1680 | |
| 1681 | // Comparison to zero |
| 1682 | |
| 1683 | static int runtime_is_zero_f(float a) { |
| 1684 | return a == 0; |
| 1685 | } |
| 1686 | |
| 1687 | static int runtime_is_zero_d(double a) { |
| 1688 | return a == 0; |
| 1689 | } |
| 1690 | |
| 1691 | // Type conversion |
| 1692 | |
| 1693 | static int runtime_float_to_int(float a) { |
| 1694 | return (int) a; |
| 1695 | } |
| 1696 | |
| 1697 | static double runtime_float_to_double(float a) { |
| 1698 | return (double) a; |
| 1699 | } |
| 1700 | |
| 1701 | static int runtime_double_to_int(double a) { |
| 1702 | return (int) a; |
| 1703 | } |
| 1704 | |
| 1705 | static float runtime_double_to_float(double a) { |
| 1706 | return (float) a; |
| 1707 | } |
| 1708 | |
| 1709 | static float runtime_int_to_float(int a) { |
| 1710 | return (float) a; |
| 1711 | } |
| 1712 | |
| 1713 | static double runtime_int_to_double(int a) { |
| 1714 | return (double) a; |
| 1715 | } |
| 1716 | |
| 1717 | // Comparisons float |
| 1718 | |
| 1719 | static int runtime_cmp_eq_ff(float b, float a) { |
| 1720 | return a == b; |
| 1721 | } |
| 1722 | |
| 1723 | static int runtime_cmp_ne_ff(float b, float a) { |
| 1724 | return a != b; |
| 1725 | } |
| 1726 | |
| 1727 | static int runtime_cmp_lt_ff(float b, float a) { |
| 1728 | return a < b; |
| 1729 | } |
| 1730 | |
| 1731 | static int runtime_cmp_le_ff(float b, float a) { |
| 1732 | return a <= b; |
| 1733 | } |
| 1734 | |
| 1735 | static int runtime_cmp_ge_ff(float b, float a) { |
| 1736 | return a >= b; |
| 1737 | } |
| 1738 | |
| 1739 | static int runtime_cmp_gt_ff(float b, float a) { |
| 1740 | return a > b; |
| 1741 | } |
| 1742 | |
| 1743 | // Comparisons double |
| 1744 | |
| 1745 | static int runtime_cmp_eq_dd(double b, double a) { |
| 1746 | return a == b; |
| 1747 | } |
| 1748 | |
| 1749 | static int runtime_cmp_ne_dd(double b, double a) { |
| 1750 | return a != b; |
| 1751 | } |
| 1752 | |
| 1753 | static int runtime_cmp_lt_dd(double b, double a) { |
| 1754 | return a < b; |
| 1755 | } |
| 1756 | |
| 1757 | static int runtime_cmp_le_dd(double b, double a) { |
| 1758 | return a <= b; |
| 1759 | } |
| 1760 | |
| 1761 | static int runtime_cmp_ge_dd(double b, double a) { |
| 1762 | return a >= b; |
| 1763 | } |
| 1764 | |
| 1765 | static int runtime_cmp_gt_dd(double b, double a) { |
| 1766 | return a > b; |
| 1767 | } |
| 1768 | |
| 1769 | // Math float |
| 1770 | |
| 1771 | static float runtime_op_add_ff(float b, float a) { |
| 1772 | return a + b; |
| 1773 | } |
| 1774 | |
| 1775 | static float runtime_op_sub_ff(float b, float a) { |
| 1776 | return a - b; |
| 1777 | } |
| 1778 | |
| 1779 | static float runtime_op_mul_ff(float b, float a) { |
| 1780 | return a * b; |
| 1781 | } |
| 1782 | |
| 1783 | static float runtime_op_div_ff(float b, float a) { |
| 1784 | return a / b; |
| 1785 | } |
| 1786 | |
| 1787 | static float runtime_op_neg_f(float a) { |
| 1788 | return -a; |
| 1789 | } |
| 1790 | |
| 1791 | // Math double |
| 1792 | |
| 1793 | static double runtime_op_add_dd(double b, double a) { |
| 1794 | return a + b; |
| 1795 | } |
| 1796 | |
| 1797 | static double runtime_op_sub_dd(double b, double a) { |
| 1798 | return a - b; |
| 1799 | } |
| 1800 | |
| 1801 | static double runtime_op_mul_dd(double b, double a) { |
| 1802 | return a * b; |
| 1803 | } |
| 1804 | |
| 1805 | static double runtime_op_div_dd(double b, double a) { |
| 1806 | return a / b; |
| 1807 | } |
| 1808 | |
| 1809 | static double runtime_op_neg_d(double a) { |
| 1810 | return -a; |
Jack Palevich | 3d474a7 | 2009-05-15 15:12:38 -0700 | [diff] [blame] | 1811 | } |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 1812 | |
| 1813 | static const int STACK_ALIGNMENT = 8; |
| 1814 | int mStackUse; |
| 1815 | // This variable holds the amount we adjusted the stack in the most |
| 1816 | // recent endFunctionCallArguments call. It's examined by the |
| 1817 | // following adjustStackAfterCall call. |
| 1818 | int mStackAlignmentAdjustment; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 1819 | }; |
| 1820 | |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 1821 | #endif // PROVIDE_ARM_CODEGEN |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 1822 | |
| 1823 | #ifdef PROVIDE_X86_CODEGEN |
| 1824 | |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 1825 | class X86CodeGenerator : public CodeGenerator { |
| 1826 | public: |
| 1827 | X86CodeGenerator() {} |
| 1828 | virtual ~X86CodeGenerator() {} |
| 1829 | |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 1830 | /* returns address to patch with local variable size |
| 1831 | */ |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1832 | virtual int functionEntry(Type* pDecl) { |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 1833 | o(0xe58955); /* push %ebp, mov %esp, %ebp */ |
| 1834 | return oad(0xec81, 0); /* sub $xxx, %esp */ |
| 1835 | } |
| 1836 | |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1837 | virtual void functionExit(Type* pDecl, int localVariableAddress, int localVariableSize) { |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 1838 | o(0xc3c9); /* leave, ret */ |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 1839 | *(int *) localVariableAddress = localVariableSize; /* save local variables */ |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 1840 | } |
| 1841 | |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 1842 | /* load immediate value */ |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 1843 | virtual void li(int i, Type* pType) { |
Jack Palevich | 1aeb87b | 2009-07-06 18:33:20 -0700 | [diff] [blame] | 1844 | oad(0xb8, i); /* mov $xx, %eax */ |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 1845 | setR0Type(pType); |
Jack Palevich | 1aeb87b | 2009-07-06 18:33:20 -0700 | [diff] [blame] | 1846 | } |
| 1847 | |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 1848 | virtual void loadFloat(int address, Type* pType) { |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 1849 | setR0Type(pType); |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 1850 | switch (pType->tag) { |
| 1851 | case TY_FLOAT: |
| 1852 | oad(0x05D9, address); // flds |
| 1853 | break; |
| 1854 | case TY_DOUBLE: |
| 1855 | oad(0x05DD, address); // fldl |
| 1856 | break; |
| 1857 | default: |
| 1858 | assert(false); |
| 1859 | break; |
| 1860 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 1861 | } |
| 1862 | |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 1863 | virtual int gjmp(int t) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 1864 | return psym(0xe9, t); |
| 1865 | } |
| 1866 | |
| 1867 | /* l = 0: je, l == 1: jne */ |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 1868 | virtual int gtst(bool l, int t) { |
Jack Palevich | 2a4e1a9 | 2009-07-09 13:34:25 -0700 | [diff] [blame] | 1869 | Type* pR0Type = getR0Type(); |
| 1870 | TypeTag tagR0 = pR0Type->tag; |
| 1871 | bool isFloatR0 = isFloatTag(tagR0); |
| 1872 | if (isFloatR0) { |
| 1873 | o(0xeed9); // fldz |
| 1874 | o(0xe9da); // fucompp |
| 1875 | o(0xe0df); // fnstsw %ax |
| 1876 | o(0x9e); // sahf |
| 1877 | } else { |
| 1878 | o(0xc085); // test %eax, %eax |
| 1879 | } |
| 1880 | // Use two output statements to generate one instruction. |
| 1881 | o(0x0f); // je/jne xxx |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 1882 | return psym(0x84 + l, t); |
| 1883 | } |
| 1884 | |
Jack Palevich | a39749f | 2009-07-08 20:40:31 -0700 | [diff] [blame] | 1885 | virtual void gcmp(int op, Type* pResultType) { |
| 1886 | Type* pR0Type = getR0Type(); |
| 1887 | Type* pTOSType = getTOSType(); |
| 1888 | TypeTag tagR0 = pR0Type->tag; |
| 1889 | TypeTag tagTOS = pTOSType->tag; |
| 1890 | bool isFloatR0 = isFloatTag(tagR0); |
| 1891 | bool isFloatTOS = isFloatTag(tagTOS); |
| 1892 | if (!isFloatR0 && !isFloatTOS) { |
| 1893 | int t = decodeOp(op); |
| 1894 | o(0x59); /* pop %ecx */ |
| 1895 | o(0xc139); /* cmp %eax,%ecx */ |
| 1896 | li(0, NULL); |
| 1897 | o(0x0f); /* setxx %al */ |
| 1898 | o(t + 0x90); |
| 1899 | o(0xc0); |
| 1900 | popType(); |
| 1901 | } else { |
| 1902 | setupFloatOperands(); |
| 1903 | switch (op) { |
| 1904 | case OP_EQUALS: |
| 1905 | o(0xe9da); // fucompp |
| 1906 | o(0xe0df); // fnstsw %ax |
| 1907 | o(0x9e); // sahf |
| 1908 | o(0xc0940f); // sete %al |
| 1909 | o(0xc29b0f); // setnp %dl |
| 1910 | o(0xd021); // andl %edx, %eax |
| 1911 | break; |
| 1912 | case OP_NOT_EQUALS: |
| 1913 | o(0xe9da); // fucompp |
| 1914 | o(0xe0df); // fnstsw %ax |
| 1915 | o(0x9e); // sahf |
| 1916 | o(0xc0950f); // setne %al |
| 1917 | o(0xc29a0f); // setp %dl |
| 1918 | o(0xd009); // orl %edx, %eax |
| 1919 | break; |
| 1920 | case OP_GREATER_EQUAL: |
| 1921 | o(0xe9da); // fucompp |
| 1922 | o(0xe0df); // fnstsw %ax |
| 1923 | o(0x05c4f6); // testb $5, %ah |
| 1924 | o(0xc0940f); // sete %al |
| 1925 | break; |
| 1926 | case OP_LESS: |
| 1927 | o(0xc9d9); // fxch %st(1) |
| 1928 | o(0xe9da); // fucompp |
| 1929 | o(0xe0df); // fnstsw %ax |
| 1930 | o(0x9e); // sahf |
| 1931 | o(0xc0970f); // seta %al |
| 1932 | break; |
| 1933 | case OP_LESS_EQUAL: |
| 1934 | o(0xc9d9); // fxch %st(1) |
| 1935 | o(0xe9da); // fucompp |
| 1936 | o(0xe0df); // fnstsw %ax |
| 1937 | o(0x9e); // sahf |
| 1938 | o(0xc0930f); // setea %al |
| 1939 | break; |
| 1940 | case OP_GREATER: |
| 1941 | o(0xe9da); // fucompp |
| 1942 | o(0xe0df); // fnstsw %ax |
| 1943 | o(0x45c4f6); // testb $69, %ah |
| 1944 | o(0xc0940f); // sete %al |
| 1945 | break; |
| 1946 | default: |
| 1947 | error("Unknown comparison op"); |
| 1948 | } |
| 1949 | o(0xc0b60f); // movzbl %al, %eax |
| 1950 | } |
| 1951 | setR0Type(pResultType); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 1952 | } |
| 1953 | |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 1954 | virtual void genOp(int op) { |
Jack Palevich | a39749f | 2009-07-08 20:40:31 -0700 | [diff] [blame] | 1955 | Type* pR0Type = getR0Type(); |
| 1956 | Type* pTOSType = getTOSType(); |
| 1957 | TypeTag tagR0 = pR0Type->tag; |
| 1958 | TypeTag tagTOS = pTOSType->tag; |
| 1959 | bool isFloatR0 = isFloatTag(tagR0); |
| 1960 | bool isFloatTOS = isFloatTag(tagTOS); |
| 1961 | if (!isFloatR0 && !isFloatTOS) { |
Jack Palevich | a8f427f | 2009-07-13 18:40:08 -0700 | [diff] [blame] | 1962 | bool isPtrR0 = tagR0 == TY_POINTER; |
| 1963 | bool isPtrTOS = tagTOS == TY_POINTER; |
| 1964 | if (isPtrR0 || isPtrTOS) { |
| 1965 | if (isPtrR0 && isPtrTOS) { |
| 1966 | if (op != OP_MINUS) { |
| 1967 | error("Unsupported pointer-pointer operation %d.", op); |
| 1968 | } |
| 1969 | if (! typeEqual(pR0Type, pTOSType)) { |
| 1970 | error("Incompatible pointer types for subtraction."); |
| 1971 | } |
| 1972 | o(0x59); /* pop %ecx */ |
| 1973 | o(decodeOp(op)); |
| 1974 | popType(); |
| 1975 | setR0Type(mkpInt); |
| 1976 | int size = sizeOf(pR0Type->pHead); |
| 1977 | if (size != 1) { |
| 1978 | pushR0(); |
| 1979 | li(size, mkpInt); |
| 1980 | // TODO: Optimize for power-of-two. |
| 1981 | genOp(OP_DIV); |
| 1982 | } |
| 1983 | } else { |
| 1984 | if (! (op == OP_PLUS || (op == OP_MINUS && isPtrR0))) { |
| 1985 | error("Unsupported pointer-scalar operation %d", op); |
| 1986 | } |
| 1987 | Type* pPtrType = isPtrR0 ? pR0Type : pTOSType; |
| 1988 | o(0x59); /* pop %ecx */ |
| 1989 | int size = sizeOf(pPtrType->pHead); |
| 1990 | if (size != 1) { |
| 1991 | // TODO: Optimize for power-of-two. |
| 1992 | if (isPtrR0) { |
| 1993 | oad(0xC969, size); // imull $size, %ecx |
| 1994 | } else { |
| 1995 | oad(0xC069, size); // mul $size, %eax |
| 1996 | } |
| 1997 | } |
| 1998 | o(decodeOp(op)); |
| 1999 | popType(); |
| 2000 | setR0Type(pPtrType); |
| 2001 | } |
| 2002 | } else { |
| 2003 | o(0x59); /* pop %ecx */ |
| 2004 | o(decodeOp(op)); |
| 2005 | if (op == OP_MOD) |
| 2006 | o(0x92); /* xchg %edx, %eax */ |
| 2007 | popType(); |
| 2008 | } |
Jack Palevich | a39749f | 2009-07-08 20:40:31 -0700 | [diff] [blame] | 2009 | } else { |
| 2010 | Type* pResultType = tagR0 > tagTOS ? pR0Type : pTOSType; |
| 2011 | setupFloatOperands(); |
| 2012 | // Both float. x87 R0 == left hand, x87 R1 == right hand |
| 2013 | switch (op) { |
| 2014 | case OP_MUL: |
| 2015 | o(0xc9de); // fmulp |
| 2016 | break; |
| 2017 | case OP_DIV: |
| 2018 | o(0xf1de); // fdivp |
| 2019 | break; |
| 2020 | case OP_PLUS: |
| 2021 | o(0xc1de); // faddp |
| 2022 | break; |
| 2023 | case OP_MINUS: |
| 2024 | o(0xe1de); // fsubp |
| 2025 | break; |
| 2026 | default: |
| 2027 | error("Unsupported binary floating operation."); |
| 2028 | break; |
| 2029 | } |
Jack Palevich | a39749f | 2009-07-08 20:40:31 -0700 | [diff] [blame] | 2030 | setR0Type(pResultType); |
Jack Palevich | a39749f | 2009-07-08 20:40:31 -0700 | [diff] [blame] | 2031 | } |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 2032 | } |
| 2033 | |
Jack Palevich | a39749f | 2009-07-08 20:40:31 -0700 | [diff] [blame] | 2034 | virtual void gUnaryCmp(int op, Type* pResultType) { |
| 2035 | if (op != OP_LOGICAL_NOT) { |
| 2036 | error("Unknown unary cmp %d", op); |
| 2037 | } else { |
| 2038 | Type* pR0Type = getR0Type(); |
| 2039 | TypeTag tag = collapseType(pR0Type->tag); |
| 2040 | switch(tag) { |
| 2041 | case TY_INT: { |
| 2042 | oad(0xb9, 0); /* movl $0, %ecx */ |
| 2043 | int t = decodeOp(op); |
| 2044 | o(0xc139); /* cmp %eax,%ecx */ |
| 2045 | li(0, NULL); |
| 2046 | o(0x0f); /* setxx %al */ |
| 2047 | o(t + 0x90); |
| 2048 | o(0xc0); |
| 2049 | } |
| 2050 | break; |
| 2051 | case TY_FLOAT: |
| 2052 | case TY_DOUBLE: |
| 2053 | o(0xeed9); // fldz |
| 2054 | o(0xe9da); // fucompp |
| 2055 | o(0xe0df); // fnstsw %ax |
| 2056 | o(0x9e); // sahf |
| 2057 | o(0xc0950f); // setne %al |
| 2058 | o(0xc29a0f); // setp %dl |
| 2059 | o(0xd009); // orl %edx, %eax |
| 2060 | o(0xc0b60f); // movzbl %al, %eax |
| 2061 | o(0x01f083); // xorl $1, %eax |
| 2062 | break; |
| 2063 | default: |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 2064 | error("gUnaryCmp unsupported type"); |
Jack Palevich | a39749f | 2009-07-08 20:40:31 -0700 | [diff] [blame] | 2065 | break; |
| 2066 | } |
| 2067 | } |
| 2068 | setR0Type(pResultType); |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 2069 | } |
| 2070 | |
| 2071 | virtual void genUnaryOp(int op) { |
Jack Palevich | a39749f | 2009-07-08 20:40:31 -0700 | [diff] [blame] | 2072 | Type* pR0Type = getR0Type(); |
| 2073 | TypeTag tag = collapseType(pR0Type->tag); |
| 2074 | switch(tag) { |
| 2075 | case TY_INT: |
| 2076 | oad(0xb9, 0); /* movl $0, %ecx */ |
| 2077 | o(decodeOp(op)); |
| 2078 | break; |
| 2079 | case TY_FLOAT: |
| 2080 | case TY_DOUBLE: |
| 2081 | switch (op) { |
| 2082 | case OP_MINUS: |
| 2083 | o(0xe0d9); // fchs |
| 2084 | break; |
| 2085 | case OP_BIT_NOT: |
| 2086 | error("Can't apply '~' operator to a float or double."); |
| 2087 | break; |
| 2088 | default: |
| 2089 | error("Unknown unary op %d\n", op); |
| 2090 | break; |
| 2091 | } |
| 2092 | break; |
| 2093 | default: |
| 2094 | error("genUnaryOp unsupported type"); |
| 2095 | break; |
| 2096 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2097 | } |
| 2098 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 2099 | virtual void pushR0() { |
Jack Palevich | 9cbd226 | 2009-07-08 16:48:41 -0700 | [diff] [blame] | 2100 | Type* pR0Type = getR0Type(); |
| 2101 | TypeTag r0ct = collapseType(pR0Type->tag); |
| 2102 | switch(r0ct) { |
| 2103 | case TY_INT: |
| 2104 | o(0x50); /* push %eax */ |
| 2105 | break; |
| 2106 | case TY_FLOAT: |
| 2107 | o(0x50); /* push %eax */ |
| 2108 | o(0x241cd9); // fstps 0(%esp) |
| 2109 | break; |
| 2110 | case TY_DOUBLE: |
| 2111 | o(0x50); /* push %eax */ |
| 2112 | o(0x50); /* push %eax */ |
| 2113 | o(0x241cdd); // fstpl 0(%esp) |
| 2114 | break; |
| 2115 | default: |
Jack Palevich | 2a4e1a9 | 2009-07-09 13:34:25 -0700 | [diff] [blame] | 2116 | error("pushR0 unsupported type %d", r0ct); |
Jack Palevich | 9cbd226 | 2009-07-08 16:48:41 -0700 | [diff] [blame] | 2117 | break; |
| 2118 | } |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 2119 | pushType(); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2120 | } |
| 2121 | |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 2122 | virtual void storeR0ToTOS(Type* pPointerType) { |
| 2123 | assert(pPointerType->tag == TY_POINTER); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2124 | o(0x59); /* pop %ecx */ |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 2125 | popType(); |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 2126 | switch (pPointerType->pHead->tag) { |
| 2127 | case TY_INT: |
| 2128 | o(0x0189); /* movl %eax/%al, (%ecx) */ |
| 2129 | break; |
| 2130 | case TY_CHAR: |
| 2131 | o(0x0188); /* movl %eax/%al, (%ecx) */ |
| 2132 | break; |
Jack Palevich | 9cbd226 | 2009-07-08 16:48:41 -0700 | [diff] [blame] | 2133 | case TY_FLOAT: |
| 2134 | o(0x19d9); /* fstps (%ecx) */ |
| 2135 | break; |
| 2136 | case TY_DOUBLE: |
| 2137 | o(0x19dd); /* fstpl (%ecx) */ |
| 2138 | break; |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 2139 | default: |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 2140 | error("storeR0ToTOS: unsupported type"); |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 2141 | break; |
| 2142 | } |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 2143 | } |
| 2144 | |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 2145 | virtual void loadR0FromR0(Type* pPointerType) { |
| 2146 | assert(pPointerType->tag == TY_POINTER); |
| 2147 | switch (pPointerType->pHead->tag) { |
| 2148 | case TY_INT: |
Jack Palevich | 2a4e1a9 | 2009-07-09 13:34:25 -0700 | [diff] [blame] | 2149 | o2(0x008b); /* mov (%eax), %eax */ |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 2150 | break; |
| 2151 | case TY_CHAR: |
| 2152 | o(0xbe0f); /* movsbl (%eax), %eax */ |
Jack Palevich | 2a4e1a9 | 2009-07-09 13:34:25 -0700 | [diff] [blame] | 2153 | ob(0); /* add zero in code */ |
| 2154 | break; |
| 2155 | case TY_FLOAT: |
| 2156 | o2(0x00d9); // flds (%eax) |
| 2157 | break; |
| 2158 | case TY_DOUBLE: |
| 2159 | o2(0x00dd); // fldl (%eax) |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 2160 | break; |
| 2161 | default: |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 2162 | error("loadR0FromR0: unsupported type"); |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 2163 | break; |
| 2164 | } |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 2165 | setR0Type(pPointerType->pHead); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2166 | } |
| 2167 | |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 2168 | virtual void leaR0(int ea, Type* pPointerType) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2169 | gmov(10, ea); /* leal EA, %eax */ |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 2170 | setR0Type(pPointerType); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2171 | } |
| 2172 | |
Jack Palevich | 9cbd226 | 2009-07-08 16:48:41 -0700 | [diff] [blame] | 2173 | virtual void storeR0(int ea, Type* pType) { |
| 2174 | TypeTag tag = pType->tag; |
| 2175 | switch (tag) { |
Jack Palevich | 25c0cca | 2009-07-13 16:56:28 -0700 | [diff] [blame] | 2176 | case TY_CHAR: |
| 2177 | if (ea < -LOCAL || ea > LOCAL) { |
| 2178 | oad(0xa2, ea); // movb %al,ea |
| 2179 | } else { |
| 2180 | oad(0x8588, ea); // movb %al,ea(%ebp) |
| 2181 | } |
| 2182 | break; |
Jack Palevich | 9cbd226 | 2009-07-08 16:48:41 -0700 | [diff] [blame] | 2183 | case TY_INT: |
Jack Palevich | 45431bc | 2009-07-13 15:57:26 -0700 | [diff] [blame] | 2184 | case TY_POINTER: |
Jack Palevich | 9cbd226 | 2009-07-08 16:48:41 -0700 | [diff] [blame] | 2185 | gmov(6, ea); /* mov %eax, EA */ |
| 2186 | break; |
| 2187 | case TY_FLOAT: |
| 2188 | if (ea < -LOCAL || ea > LOCAL) { |
| 2189 | oad(0x1dd9, ea); // fstps ea |
| 2190 | } else { |
| 2191 | oad(0x9dd9, ea); // fstps ea(%ebp) |
| 2192 | } |
| 2193 | break; |
| 2194 | case TY_DOUBLE: |
| 2195 | if (ea < -LOCAL || ea > LOCAL) { |
| 2196 | oad(0x1ddd, ea); // fstpl ea |
| 2197 | } else { |
| 2198 | oad(0x9ddd, ea); // fstpl ea(%ebp) |
| 2199 | } |
| 2200 | break; |
| 2201 | default: |
| 2202 | error("Unable to store to type %d", tag); |
| 2203 | break; |
| 2204 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2205 | } |
| 2206 | |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 2207 | virtual void loadR0(int ea, bool isIncDec, int op, Type* pType) { |
Jack Palevich | 25c0cca | 2009-07-13 16:56:28 -0700 | [diff] [blame] | 2208 | TypeTag tag = pType->tag; |
Jack Palevich | 128ad2d | 2009-07-08 14:51:31 -0700 | [diff] [blame] | 2209 | switch (tag) { |
Jack Palevich | 25c0cca | 2009-07-13 16:56:28 -0700 | [diff] [blame] | 2210 | case TY_CHAR: |
| 2211 | if (ea < -LOCAL || ea > LOCAL) { |
| 2212 | oad(0x05BE0F, ea); // movsbl ea,%eax |
| 2213 | } else { |
| 2214 | oad(0x85BE0F, ea); // movsbl ea(%ebp),%eax |
| 2215 | } |
| 2216 | if (isIncDec) { |
| 2217 | error("inc/dec not implemented for char."); |
| 2218 | } |
| 2219 | break; |
Jack Palevich | 128ad2d | 2009-07-08 14:51:31 -0700 | [diff] [blame] | 2220 | case TY_INT: |
Jack Palevich | 25c0cca | 2009-07-13 16:56:28 -0700 | [diff] [blame] | 2221 | case TY_POINTER: |
| 2222 | if (tag == TY_CHAR) { |
| 2223 | } else { |
| 2224 | gmov(8, ea); /* mov EA, %eax */ |
| 2225 | } |
Jack Palevich | 128ad2d | 2009-07-08 14:51:31 -0700 | [diff] [blame] | 2226 | if (isIncDec) { |
| 2227 | /* Implement post-increment or post decrement. |
| 2228 | */ |
| 2229 | gmov(0, ea); /* 83 ADD */ |
| 2230 | o(decodeOp(op)); |
| 2231 | } |
| 2232 | break; |
| 2233 | case TY_FLOAT: |
| 2234 | if (ea < -LOCAL || ea > LOCAL) { |
| 2235 | oad(0x05d9, ea); // flds ea |
| 2236 | } else { |
| 2237 | oad(0x85d9, ea); // flds ea(%ebp) |
| 2238 | } |
| 2239 | if (isIncDec) { |
| 2240 | error("inc/dec not implemented for float."); |
| 2241 | } |
| 2242 | break; |
| 2243 | case TY_DOUBLE: |
| 2244 | if (ea < -LOCAL || ea > LOCAL) { |
| 2245 | oad(0x05dd, ea); // fldl ea |
| 2246 | } else { |
| 2247 | oad(0x85dd, ea); // fldl ea(%ebp) |
| 2248 | } |
| 2249 | if (isIncDec) { |
| 2250 | error("inc/dec not implemented for double."); |
| 2251 | } |
| 2252 | break; |
| 2253 | default: |
| 2254 | error("Unable to load type %d", tag); |
| 2255 | break; |
Jack Palevich | 4d93f30 | 2009-05-15 13:30:00 -0700 | [diff] [blame] | 2256 | } |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 2257 | setR0Type(pType); |
| 2258 | } |
| 2259 | |
| 2260 | virtual void convertR0(Type* pType){ |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 2261 | Type* pR0Type = getR0Type(); |
| 2262 | if (pR0Type == NULL) { |
Jack Palevich | 128ad2d | 2009-07-08 14:51:31 -0700 | [diff] [blame] | 2263 | assert(false); |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 2264 | setR0Type(pType); |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 2265 | return; |
| 2266 | } |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 2267 | if (bitsSame(pType, pR0Type)) { |
| 2268 | // do nothing special |
| 2269 | } else if (isFloatType(pType) && isFloatType(pR0Type)) { |
| 2270 | // do nothing special, both held in same register on x87. |
| 2271 | } else { |
Jack Palevich | 128ad2d | 2009-07-08 14:51:31 -0700 | [diff] [blame] | 2272 | TypeTag r0Tag = collapseType(pR0Type->tag); |
| 2273 | TypeTag destTag = collapseType(pType->tag); |
| 2274 | if (r0Tag == TY_INT && isFloatTag(destTag)) { |
| 2275 | // Convert R0 from int to float |
| 2276 | o(0x50); // push %eax |
| 2277 | o(0x2404DB); // fildl 0(%esp) |
| 2278 | o(0x58); // pop %eax |
| 2279 | } else if (isFloatTag(r0Tag) && destTag == TY_INT) { |
| 2280 | // Convert R0 from float to int. Complicated because |
| 2281 | // need to save and restore the rounding mode. |
| 2282 | o(0x50); // push %eax |
| 2283 | o(0x50); // push %eax |
| 2284 | o(0x02247cD9); // fnstcw 2(%esp) |
| 2285 | o(0x2444b70f); // movzwl 2(%esp), %eax |
| 2286 | o(0x02); |
| 2287 | o(0x0cb4); // movb $12, %ah |
| 2288 | o(0x24048966); // movw %ax, 0(%esp) |
| 2289 | o(0x242cd9); // fldcw 0(%esp) |
| 2290 | o(0x04245cdb); // fistpl 4(%esp) |
| 2291 | o(0x02246cd9); // fldcw 2(%esp) |
| 2292 | o(0x58); // pop %eax |
| 2293 | o(0x58); // pop %eax |
| 2294 | } else { |
| 2295 | error("Incompatible types old: %d new: %d", |
| 2296 | pR0Type->tag, pType->tag); |
| 2297 | } |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 2298 | } |
| 2299 | setR0Type(pType); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2300 | } |
| 2301 | |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 2302 | virtual int beginFunctionCallArguments() { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2303 | return oad(0xec81, 0); /* sub $xxx, %esp */ |
| 2304 | } |
| 2305 | |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 2306 | virtual size_t storeR0ToArg(int l) { |
| 2307 | Type* pR0Type = getR0Type(); |
| 2308 | TypeTag r0ct = collapseType(pR0Type->tag); |
| 2309 | switch(r0ct) { |
| 2310 | case TY_INT: |
| 2311 | oad(0x248489, l); /* movl %eax, xxx(%esp) */ |
| 2312 | return 4; |
| 2313 | case TY_FLOAT: |
| 2314 | oad(0x249CD9, l); /* fstps xxx(%esp) */ |
| 2315 | return 4; |
| 2316 | case TY_DOUBLE: |
| 2317 | oad(0x249CDD, l); /* fstpl xxx(%esp) */ |
| 2318 | return 8; |
| 2319 | default: |
| 2320 | assert(false); |
| 2321 | return 0; |
| 2322 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2323 | } |
| 2324 | |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 2325 | virtual void endFunctionCallArguments(Type* pDecl, int a, int l) { |
Jack Palevich | 7810bc9 | 2009-05-15 14:31:47 -0700 | [diff] [blame] | 2326 | * (int*) a = l; |
| 2327 | } |
| 2328 | |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 2329 | virtual int callForward(int symbol, Type* pFunc) { |
| 2330 | setR0Type(pFunc->pHead); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2331 | return psym(0xe8, symbol); /* call xxx */ |
| 2332 | } |
| 2333 | |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 2334 | virtual void callRelative(int t, Type* pFunc) { |
| 2335 | setR0Type(pFunc->pHead); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2336 | psym(0xe8, t); /* call xxx */ |
| 2337 | } |
| 2338 | |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 2339 | virtual void callIndirect(int l, Type* pFunc) { |
| 2340 | setR0Type(pFunc->pHead); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2341 | oad(0x2494ff, l); /* call *xxx(%esp) */ |
| 2342 | } |
| 2343 | |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 2344 | virtual void adjustStackAfterCall(Type* pDecl, int l, bool isIndirect) { |
Jack Palevich | 7810bc9 | 2009-05-15 14:31:47 -0700 | [diff] [blame] | 2345 | if (isIndirect) { |
| 2346 | l += 4; |
| 2347 | } |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 2348 | if (l > 0) { |
| 2349 | oad(0xc481, l); /* add $xxx, %esp */ |
| 2350 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2351 | } |
| 2352 | |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 2353 | virtual int jumpOffset() { |
| 2354 | return 5; |
| 2355 | } |
| 2356 | |
| 2357 | virtual int disassemble(FILE* out) { |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 2358 | return 0; |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 2359 | } |
| 2360 | |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 2361 | /* output a symbol and patch all calls to it */ |
| 2362 | virtual void gsym(int t) { |
| 2363 | int n; |
| 2364 | int pc = getPC(); |
| 2365 | while (t) { |
| 2366 | n = *(int *) t; /* next value */ |
| 2367 | *(int *) t = pc - t - 4; |
| 2368 | t = n; |
| 2369 | } |
| 2370 | } |
| 2371 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 2372 | virtual int finishCompile() { |
Jack Palevich | 8dc662e | 2009-06-09 22:53:47 +0000 | [diff] [blame] | 2373 | size_t pagesize = 4096; |
| 2374 | size_t base = (size_t) getBase() & ~ (pagesize - 1); |
| 2375 | size_t top = ((size_t) getPC() + pagesize - 1) & ~ (pagesize - 1); |
| 2376 | int err = mprotect((void*) base, top - base, PROT_READ | PROT_WRITE | PROT_EXEC); |
| 2377 | if (err) { |
| 2378 | error("mprotect() failed: %d", errno); |
| 2379 | } |
| 2380 | return err; |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 2381 | } |
| 2382 | |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 2383 | /** |
Jack Palevich | 9cbd226 | 2009-07-08 16:48:41 -0700 | [diff] [blame] | 2384 | * Alignment (in bytes) for this type of data |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 2385 | */ |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 2386 | virtual size_t alignmentOf(Type* pType){ |
| 2387 | return 4; |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 2388 | } |
| 2389 | |
| 2390 | /** |
| 2391 | * Array element alignment (in bytes) for this type of data. |
| 2392 | */ |
| 2393 | virtual size_t sizeOf(Type* pType){ |
| 2394 | switch(pType->tag) { |
| 2395 | case TY_INT: |
| 2396 | return 4; |
| 2397 | case TY_CHAR: |
| 2398 | return 1; |
| 2399 | default: |
| 2400 | return 0; |
| 2401 | case TY_FLOAT: |
| 2402 | return 4; |
| 2403 | case TY_DOUBLE: |
| 2404 | return 8; |
| 2405 | case TY_POINTER: |
| 2406 | return 4; |
| 2407 | } |
| 2408 | } |
| 2409 | |
Jack Palevich | 9cbd226 | 2009-07-08 16:48:41 -0700 | [diff] [blame] | 2410 | virtual size_t stackSizeOf(Type* pType) { |
| 2411 | switch(pType->tag) { |
| 2412 | case TY_DOUBLE: |
| 2413 | return 8; |
| 2414 | default: |
| 2415 | return 4; |
| 2416 | } |
| 2417 | } |
| 2418 | |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2419 | private: |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 2420 | |
| 2421 | /** Output 1 to 4 bytes. |
| 2422 | * |
| 2423 | */ |
| 2424 | void o(int n) { |
| 2425 | /* cannot use unsigned, so we must do a hack */ |
| 2426 | while (n && n != -1) { |
| 2427 | ob(n & 0xff); |
| 2428 | n = n >> 8; |
| 2429 | } |
| 2430 | } |
| 2431 | |
Jack Palevich | 2a4e1a9 | 2009-07-09 13:34:25 -0700 | [diff] [blame] | 2432 | /* Output exactly 2 bytes |
| 2433 | */ |
| 2434 | void o2(int n) { |
| 2435 | ob(n & 0xff); |
| 2436 | ob(0xff & (n >> 8)); |
| 2437 | } |
| 2438 | |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 2439 | /* psym is used to put an instruction with a data field which is a |
| 2440 | reference to a symbol. It is in fact the same as oad ! */ |
| 2441 | int psym(int n, int t) { |
| 2442 | return oad(n, t); |
| 2443 | } |
| 2444 | |
| 2445 | /* instruction + address */ |
| 2446 | int oad(int n, int t) { |
| 2447 | o(n); |
| 2448 | int result = getPC(); |
| 2449 | o4(t); |
| 2450 | return result; |
| 2451 | } |
| 2452 | |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 2453 | static const int operatorHelper[]; |
| 2454 | |
| 2455 | int decodeOp(int op) { |
| 2456 | if (op < 0 || op > OP_COUNT) { |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 2457 | error("Out-of-range operator: %d\n", op); |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 2458 | op = 0; |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 2459 | } |
| 2460 | return operatorHelper[op]; |
| 2461 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2462 | |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 2463 | void gmov(int l, int t) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2464 | o(l + 0x83); |
Jack Palevich | 8dc662e | 2009-06-09 22:53:47 +0000 | [diff] [blame] | 2465 | oad((t > -LOCAL && t < LOCAL) << 7 | 5, t); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2466 | } |
Jack Palevich | a39749f | 2009-07-08 20:40:31 -0700 | [diff] [blame] | 2467 | |
| 2468 | void setupFloatOperands() { |
| 2469 | Type* pR0Type = getR0Type(); |
| 2470 | Type* pTOSType = getTOSType(); |
| 2471 | TypeTag tagR0 = pR0Type->tag; |
| 2472 | TypeTag tagTOS = pTOSType->tag; |
| 2473 | bool isFloatR0 = isFloatTag(tagR0); |
| 2474 | bool isFloatTOS = isFloatTag(tagTOS); |
| 2475 | if (! isFloatR0) { |
| 2476 | // Convert R0 from int to float |
| 2477 | o(0x50); // push %eax |
| 2478 | o(0x2404DB); // fildl 0(%esp) |
| 2479 | o(0x58); // pop %eax |
| 2480 | } |
| 2481 | if (! isFloatTOS){ |
| 2482 | o(0x2404DB); // fildl 0(%esp); |
| 2483 | o(0x58); // pop %eax |
| 2484 | } else { |
| 2485 | if (tagTOS == TY_FLOAT) { |
| 2486 | o(0x2404d9); // flds (%esp) |
| 2487 | o(0x58); // pop %eax |
| 2488 | } else { |
| 2489 | o(0x2404dd); // fldl (%esp) |
| 2490 | o(0x58); // pop %eax |
| 2491 | o(0x58); // pop %eax |
| 2492 | } |
| 2493 | } |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 2494 | popType(); |
Jack Palevich | a39749f | 2009-07-08 20:40:31 -0700 | [diff] [blame] | 2495 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2496 | }; |
| 2497 | |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 2498 | #endif // PROVIDE_X86_CODEGEN |
| 2499 | |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 2500 | #ifdef PROVIDE_TRACE_CODEGEN |
| 2501 | class TraceCodeGenerator : public CodeGenerator { |
| 2502 | private: |
| 2503 | CodeGenerator* mpBase; |
| 2504 | |
| 2505 | public: |
| 2506 | TraceCodeGenerator(CodeGenerator* pBase) { |
| 2507 | mpBase = pBase; |
| 2508 | } |
| 2509 | |
| 2510 | virtual ~TraceCodeGenerator() { |
| 2511 | delete mpBase; |
| 2512 | } |
| 2513 | |
| 2514 | virtual void init(CodeBuf* pCodeBuf) { |
| 2515 | mpBase->init(pCodeBuf); |
| 2516 | } |
| 2517 | |
| 2518 | void setErrorSink(ErrorSink* pErrorSink) { |
| 2519 | mpBase->setErrorSink(pErrorSink); |
| 2520 | } |
| 2521 | |
| 2522 | /* returns address to patch with local variable size |
| 2523 | */ |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 2524 | virtual int functionEntry(Type* pDecl) { |
| 2525 | int result = mpBase->functionEntry(pDecl); |
| 2526 | fprintf(stderr, "functionEntry(pDecl) -> %d\n", result); |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 2527 | return result; |
| 2528 | } |
| 2529 | |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 2530 | virtual void functionExit(Type* pDecl, int localVariableAddress, int localVariableSize) { |
| 2531 | fprintf(stderr, "functionExit(pDecl, %d, %d)\n", |
| 2532 | localVariableAddress, localVariableSize); |
| 2533 | mpBase->functionExit(pDecl, localVariableAddress, localVariableSize); |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 2534 | } |
| 2535 | |
| 2536 | /* load immediate value */ |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 2537 | virtual void li(int t, Type* pType) { |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 2538 | fprintf(stderr, "li(%d)\n", t); |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 2539 | mpBase->li(t, pType); |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 2540 | } |
| 2541 | |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 2542 | virtual void loadFloat(int address, Type* pType) { |
| 2543 | fprintf(stderr, "loadFloat(%d, type)\n", address); |
| 2544 | mpBase->loadFloat(address, pType); |
Jack Palevich | 1aeb87b | 2009-07-06 18:33:20 -0700 | [diff] [blame] | 2545 | } |
| 2546 | |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 2547 | virtual int gjmp(int t) { |
| 2548 | int result = mpBase->gjmp(t); |
| 2549 | fprintf(stderr, "gjmp(%d) = %d\n", t, result); |
| 2550 | return result; |
| 2551 | } |
| 2552 | |
| 2553 | /* l = 0: je, l == 1: jne */ |
| 2554 | virtual int gtst(bool l, int t) { |
| 2555 | int result = mpBase->gtst(l, t); |
| 2556 | fprintf(stderr, "gtst(%d,%d) = %d\n", l, t, result); |
| 2557 | return result; |
| 2558 | } |
| 2559 | |
Jack Palevich | a39749f | 2009-07-08 20:40:31 -0700 | [diff] [blame] | 2560 | virtual void gcmp(int op, Type* pResultType) { |
| 2561 | fprintf(stderr, "gcmp(%d, pResultType)\n", op); |
| 2562 | mpBase->gcmp(op, pResultType); |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 2563 | } |
| 2564 | |
| 2565 | virtual void genOp(int op) { |
| 2566 | fprintf(stderr, "genOp(%d)\n", op); |
| 2567 | mpBase->genOp(op); |
| 2568 | } |
| 2569 | |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 2570 | |
Jack Palevich | a39749f | 2009-07-08 20:40:31 -0700 | [diff] [blame] | 2571 | virtual void gUnaryCmp(int op, Type* pResultType) { |
| 2572 | fprintf(stderr, "gUnaryCmp(%d, pResultType)\n", op); |
| 2573 | mpBase->gUnaryCmp(op, pResultType); |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 2574 | } |
| 2575 | |
| 2576 | virtual void genUnaryOp(int op) { |
| 2577 | fprintf(stderr, "genUnaryOp(%d)\n", op); |
| 2578 | mpBase->genUnaryOp(op); |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 2579 | } |
| 2580 | |
| 2581 | virtual void pushR0() { |
| 2582 | fprintf(stderr, "pushR0()\n"); |
| 2583 | mpBase->pushR0(); |
| 2584 | } |
| 2585 | |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 2586 | virtual void storeR0ToTOS(Type* pPointerType) { |
Jack Palevich | 1aeb87b | 2009-07-06 18:33:20 -0700 | [diff] [blame] | 2587 | fprintf(stderr, "storeR0ToTOS(%d)\n", pPointerType->pHead->tag); |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 2588 | mpBase->storeR0ToTOS(pPointerType); |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 2589 | } |
| 2590 | |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 2591 | virtual void loadR0FromR0(Type* pPointerType) { |
Jack Palevich | 1aeb87b | 2009-07-06 18:33:20 -0700 | [diff] [blame] | 2592 | fprintf(stderr, "loadR0FromR0(%d)\n", pPointerType->pHead->tag); |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 2593 | mpBase->loadR0FromR0(pPointerType); |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 2594 | } |
| 2595 | |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 2596 | virtual void leaR0(int ea, Type* pPointerType) { |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 2597 | fprintf(stderr, "leaR0(%d)\n", ea); |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 2598 | mpBase->leaR0(ea, pPointerType); |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 2599 | } |
| 2600 | |
Jack Palevich | 9cbd226 | 2009-07-08 16:48:41 -0700 | [diff] [blame] | 2601 | virtual void storeR0(int ea, Type* pType) { |
| 2602 | fprintf(stderr, "storeR0(%d, pType)\n", ea); |
| 2603 | mpBase->storeR0(ea, pType); |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 2604 | } |
| 2605 | |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 2606 | virtual void loadR0(int ea, bool isIncDec, int op, Type* pType) { |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 2607 | fprintf(stderr, "loadR0(%d, %d, %d, pType)\n", ea, isIncDec, op); |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 2608 | mpBase->loadR0(ea, isIncDec, op, pType); |
| 2609 | } |
| 2610 | |
| 2611 | virtual void convertR0(Type* pType){ |
Jack Palevich | 37c54bd | 2009-07-14 18:35:36 -0700 | [diff] [blame] | 2612 | fprintf(stderr, "convertR0(pType tag=%d)\n", pType->tag); |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 2613 | mpBase->convertR0(pType); |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 2614 | } |
| 2615 | |
| 2616 | virtual int beginFunctionCallArguments() { |
| 2617 | int result = mpBase->beginFunctionCallArguments(); |
| 2618 | fprintf(stderr, "beginFunctionCallArguments() = %d\n", result); |
| 2619 | return result; |
| 2620 | } |
| 2621 | |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 2622 | virtual size_t storeR0ToArg(int l) { |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 2623 | fprintf(stderr, "storeR0ToArg(%d)\n", l); |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 2624 | return mpBase->storeR0ToArg(l); |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 2625 | } |
| 2626 | |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 2627 | virtual void endFunctionCallArguments(Type* pDecl, int a, int l) { |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 2628 | fprintf(stderr, "endFunctionCallArguments(%d, %d)\n", a, l); |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 2629 | mpBase->endFunctionCallArguments(pDecl, a, l); |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 2630 | } |
| 2631 | |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 2632 | virtual int callForward(int symbol, Type* pFunc) { |
| 2633 | int result = mpBase->callForward(symbol, pFunc); |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 2634 | fprintf(stderr, "callForward(%d) = %d\n", symbol, result); |
| 2635 | return result; |
| 2636 | } |
| 2637 | |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 2638 | virtual void callRelative(int t, Type* pFunc) { |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 2639 | fprintf(stderr, "callRelative(%d)\n", t); |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 2640 | mpBase->callRelative(t, pFunc); |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 2641 | } |
| 2642 | |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 2643 | virtual void callIndirect(int l, Type* pFunc) { |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 2644 | fprintf(stderr, "callIndirect(%d)\n", l); |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 2645 | mpBase->callIndirect(l, pFunc); |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 2646 | } |
| 2647 | |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 2648 | virtual void adjustStackAfterCall(Type* pDecl, int l, bool isIndirect) { |
| 2649 | fprintf(stderr, "adjustStackAfterCall(pType, %d, %d)\n", l, isIndirect); |
| 2650 | mpBase->adjustStackAfterCall(pDecl, l, isIndirect); |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 2651 | } |
| 2652 | |
| 2653 | virtual int jumpOffset() { |
| 2654 | return mpBase->jumpOffset(); |
| 2655 | } |
| 2656 | |
| 2657 | virtual int disassemble(FILE* out) { |
| 2658 | return mpBase->disassemble(out); |
| 2659 | } |
| 2660 | |
| 2661 | /* output a symbol and patch all calls to it */ |
| 2662 | virtual void gsym(int t) { |
| 2663 | fprintf(stderr, "gsym(%d)\n", t); |
| 2664 | mpBase->gsym(t); |
| 2665 | } |
| 2666 | |
| 2667 | virtual int finishCompile() { |
| 2668 | int result = mpBase->finishCompile(); |
| 2669 | fprintf(stderr, "finishCompile() = %d\n", result); |
| 2670 | return result; |
| 2671 | } |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 2672 | |
| 2673 | /** |
Jack Palevich | 9cbd226 | 2009-07-08 16:48:41 -0700 | [diff] [blame] | 2674 | * Alignment (in bytes) for this type of data |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 2675 | */ |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 2676 | virtual size_t alignmentOf(Type* pType){ |
| 2677 | return mpBase->alignmentOf(pType); |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 2678 | } |
| 2679 | |
| 2680 | /** |
| 2681 | * Array element alignment (in bytes) for this type of data. |
| 2682 | */ |
| 2683 | virtual size_t sizeOf(Type* pType){ |
| 2684 | return mpBase->sizeOf(pType); |
| 2685 | } |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 2686 | |
Jack Palevich | 9cbd226 | 2009-07-08 16:48:41 -0700 | [diff] [blame] | 2687 | |
| 2688 | virtual size_t stackSizeOf(Type* pType) { |
| 2689 | return mpBase->stackSizeOf(pType); |
| 2690 | } |
| 2691 | |
| 2692 | |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 2693 | virtual Type* getR0Type() { |
| 2694 | return mpBase->getR0Type(); |
| 2695 | } |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 2696 | }; |
| 2697 | |
| 2698 | #endif // PROVIDE_TRACE_CODEGEN |
| 2699 | |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 2700 | class Arena { |
| 2701 | public: |
| 2702 | // Used to record a given allocation amount. |
| 2703 | // Used: |
| 2704 | // Mark mark = arena.mark(); |
| 2705 | // ... lots of arena.allocate() |
| 2706 | // arena.free(mark); |
| 2707 | |
| 2708 | struct Mark { |
| 2709 | size_t chunk; |
| 2710 | size_t offset; |
| 2711 | }; |
| 2712 | |
| 2713 | Arena() { |
| 2714 | mCurrentChunk = 0; |
| 2715 | Chunk start(CHUNK_SIZE); |
| 2716 | mData.push_back(start); |
| 2717 | } |
| 2718 | |
| 2719 | ~Arena() { |
| 2720 | for(size_t i = 0; i < mData.size(); i++) { |
| 2721 | mData[i].free(); |
| 2722 | } |
| 2723 | } |
| 2724 | |
| 2725 | // Alloc using the standard alignment size safe for any variable |
| 2726 | void* alloc(size_t size) { |
| 2727 | return alloc(size, 8); |
| 2728 | } |
| 2729 | |
| 2730 | Mark mark(){ |
| 2731 | Mark result; |
| 2732 | result.chunk = mCurrentChunk; |
| 2733 | result.offset = mData[mCurrentChunk].mOffset; |
| 2734 | return result; |
| 2735 | } |
| 2736 | |
| 2737 | void freeToMark(const Mark& mark) { |
| 2738 | mCurrentChunk = mark.chunk; |
| 2739 | mData[mCurrentChunk].mOffset = mark.offset; |
| 2740 | } |
| 2741 | |
| 2742 | private: |
| 2743 | // Allocate memory aligned to a given size |
| 2744 | // and a given power-of-two-sized alignment (e.g. 1,2,4,8,...) |
| 2745 | // Memory is not zero filled. |
| 2746 | |
| 2747 | void* alloc(size_t size, size_t alignment) { |
| 2748 | while (size > mData[mCurrentChunk].remainingCapacity(alignment)) { |
| 2749 | if (mCurrentChunk + 1 < mData.size()) { |
| 2750 | mCurrentChunk++; |
| 2751 | } else { |
| 2752 | size_t allocSize = CHUNK_SIZE; |
| 2753 | if (allocSize < size + alignment - 1) { |
| 2754 | allocSize = size + alignment - 1; |
| 2755 | } |
| 2756 | Chunk chunk(allocSize); |
| 2757 | mData.push_back(chunk); |
| 2758 | mCurrentChunk++; |
| 2759 | } |
| 2760 | } |
| 2761 | return mData[mCurrentChunk].allocate(size, alignment); |
| 2762 | } |
| 2763 | |
| 2764 | static const size_t CHUNK_SIZE = 128*1024; |
| 2765 | // Note: this class does not deallocate its |
| 2766 | // memory when it's destroyed. It depends upon |
| 2767 | // its parent to deallocate the memory. |
| 2768 | struct Chunk { |
| 2769 | Chunk() { |
| 2770 | mpData = 0; |
| 2771 | mSize = 0; |
| 2772 | mOffset = 0; |
| 2773 | } |
| 2774 | |
| 2775 | Chunk(size_t size) { |
| 2776 | mSize = size; |
| 2777 | mpData = (char*) malloc(size); |
| 2778 | mOffset = 0; |
| 2779 | } |
| 2780 | |
| 2781 | ~Chunk() { |
| 2782 | // Doesn't deallocate memory. |
| 2783 | } |
| 2784 | |
| 2785 | void* allocate(size_t size, size_t alignment) { |
| 2786 | size_t alignedOffset = aligned(mOffset, alignment); |
| 2787 | void* result = mpData + alignedOffset; |
| 2788 | mOffset = alignedOffset + size; |
| 2789 | return result; |
| 2790 | } |
| 2791 | |
| 2792 | void free() { |
| 2793 | if (mpData) { |
| 2794 | ::free(mpData); |
| 2795 | mpData = 0; |
| 2796 | } |
| 2797 | } |
| 2798 | |
| 2799 | size_t remainingCapacity(size_t alignment) { |
| 2800 | return aligned(mSize, alignment) - aligned(mOffset, alignment); |
| 2801 | } |
| 2802 | |
| 2803 | // Assume alignment is a power of two |
| 2804 | inline size_t aligned(size_t v, size_t alignment) { |
| 2805 | size_t mask = alignment-1; |
| 2806 | return (v + mask) & ~mask; |
| 2807 | } |
| 2808 | |
| 2809 | char* mpData; |
| 2810 | size_t mSize; |
| 2811 | size_t mOffset; |
| 2812 | }; |
| 2813 | |
| 2814 | size_t mCurrentChunk; |
| 2815 | |
| 2816 | Vector<Chunk> mData; |
| 2817 | }; |
| 2818 | |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 2819 | struct VariableInfo; |
| 2820 | |
| 2821 | struct Token { |
| 2822 | int hash; |
| 2823 | size_t length; |
| 2824 | char* pText; |
| 2825 | tokenid_t id; |
| 2826 | |
| 2827 | // Current values for the token |
| 2828 | char* mpMacroDefinition; |
| 2829 | VariableInfo* mpVariableInfo; |
| 2830 | }; |
| 2831 | |
| 2832 | class TokenTable { |
| 2833 | public: |
| 2834 | // Don't use 0..0xff, allows characters and operators to be tokens too. |
| 2835 | |
| 2836 | static const int TOKEN_BASE = 0x100; |
| 2837 | TokenTable() { |
| 2838 | mpMap = hashmapCreate(128, hashFn, equalsFn); |
| 2839 | } |
| 2840 | |
| 2841 | ~TokenTable() { |
| 2842 | hashmapFree(mpMap); |
| 2843 | } |
| 2844 | |
| 2845 | void setArena(Arena* pArena) { |
| 2846 | mpArena = pArena; |
| 2847 | } |
| 2848 | |
| 2849 | // Returns a token for a given string of characters. |
| 2850 | tokenid_t intern(const char* pText, size_t length) { |
| 2851 | Token probe; |
| 2852 | int hash = hashmapHash((void*) pText, length); |
| 2853 | { |
| 2854 | Token probe; |
| 2855 | probe.hash = hash; |
| 2856 | probe.length = length; |
| 2857 | probe.pText = (char*) pText; |
| 2858 | Token* pValue = (Token*) hashmapGet(mpMap, &probe); |
| 2859 | if (pValue) { |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 2860 | return pValue->id; |
| 2861 | } |
| 2862 | } |
| 2863 | |
| 2864 | Token* pToken = (Token*) mpArena->alloc(sizeof(Token)); |
| 2865 | memset(pToken, 0, sizeof(*pToken)); |
| 2866 | pToken->hash = hash; |
| 2867 | pToken->length = length; |
| 2868 | pToken->pText = (char*) mpArena->alloc(length + 1); |
| 2869 | memcpy(pToken->pText, pText, length); |
| 2870 | pToken->pText[length] = 0; |
| 2871 | pToken->id = mTokens.size() + TOKEN_BASE; |
| 2872 | mTokens.push_back(pToken); |
| 2873 | hashmapPut(mpMap, pToken, pToken); |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 2874 | return pToken->id; |
| 2875 | } |
| 2876 | |
| 2877 | // Return the Token for a given tokenid. |
| 2878 | Token& operator[](tokenid_t id) { |
| 2879 | return *mTokens[id - TOKEN_BASE]; |
| 2880 | } |
| 2881 | |
| 2882 | inline size_t size() { |
| 2883 | return mTokens.size(); |
| 2884 | } |
| 2885 | |
| 2886 | private: |
| 2887 | |
| 2888 | static int hashFn(void* pKey) { |
| 2889 | Token* pToken = (Token*) pKey; |
| 2890 | return pToken->hash; |
| 2891 | } |
| 2892 | |
| 2893 | static bool equalsFn(void* keyA, void* keyB) { |
| 2894 | Token* pTokenA = (Token*) keyA; |
| 2895 | Token* pTokenB = (Token*) keyB; |
| 2896 | // Don't need to compare hash values, they should always be equal |
| 2897 | return pTokenA->length == pTokenB->length |
| 2898 | && strcmp(pTokenA->pText, pTokenB->pText) == 0; |
| 2899 | } |
| 2900 | |
| 2901 | Hashmap* mpMap; |
| 2902 | Vector<Token*> mTokens; |
| 2903 | Arena* mpArena; |
| 2904 | }; |
| 2905 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 2906 | class InputStream { |
| 2907 | public: |
Marco Nelissen | eea5ae9 | 2009-07-08 16:59:18 -0700 | [diff] [blame] | 2908 | virtual ~InputStream() {} |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 2909 | int getChar() { |
| 2910 | if (bumpLine) { |
| 2911 | line++; |
| 2912 | bumpLine = false; |
| 2913 | } |
| 2914 | int ch = get(); |
| 2915 | if (ch == '\n') { |
| 2916 | bumpLine = true; |
| 2917 | } |
| 2918 | return ch; |
| 2919 | } |
| 2920 | int getLine() { |
| 2921 | return line; |
| 2922 | } |
| 2923 | protected: |
| 2924 | InputStream() : |
| 2925 | line(1), bumpLine(false) { |
| 2926 | } |
| 2927 | private: |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 2928 | virtual int get() = 0; |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 2929 | int line; |
| 2930 | bool bumpLine; |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 2931 | }; |
| 2932 | |
| 2933 | class FileInputStream : public InputStream { |
| 2934 | public: |
| 2935 | FileInputStream(FILE* in) : f(in) {} |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 2936 | private: |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 2937 | virtual int get() { return fgetc(f); } |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 2938 | FILE* f; |
| 2939 | }; |
| 2940 | |
| 2941 | class TextInputStream : public InputStream { |
| 2942 | public: |
| 2943 | TextInputStream(const char* text, size_t textLength) |
| 2944 | : pText(text), mTextLength(textLength), mPosition(0) { |
| 2945 | } |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 2946 | |
| 2947 | private: |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 2948 | virtual int get() { |
| 2949 | return mPosition < mTextLength ? pText[mPosition++] : EOF; |
| 2950 | } |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 2951 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 2952 | const char* pText; |
| 2953 | size_t mTextLength; |
| 2954 | size_t mPosition; |
| 2955 | }; |
| 2956 | |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 2957 | class String { |
| 2958 | public: |
| 2959 | String() { |
| 2960 | mpBase = 0; |
| 2961 | mUsed = 0; |
| 2962 | mSize = 0; |
| 2963 | } |
| 2964 | |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 2965 | String(const char* item, int len, bool adopt) { |
| 2966 | if (len < 0) { |
| 2967 | len = strlen(item); |
| 2968 | } |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 2969 | if (adopt) { |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 2970 | mpBase = (char*) item; |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 2971 | mUsed = len; |
| 2972 | mSize = len + 1; |
| 2973 | } else { |
| 2974 | mpBase = 0; |
| 2975 | mUsed = 0; |
| 2976 | mSize = 0; |
| 2977 | appendBytes(item, len); |
| 2978 | } |
| 2979 | } |
| 2980 | |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 2981 | String(const String& other) { |
| 2982 | mpBase = 0; |
| 2983 | mUsed = 0; |
| 2984 | mSize = 0; |
| 2985 | appendBytes(other.getUnwrapped(), other.len()); |
| 2986 | } |
| 2987 | |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 2988 | ~String() { |
| 2989 | if (mpBase) { |
| 2990 | free(mpBase); |
| 2991 | } |
| 2992 | } |
| 2993 | |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 2994 | String& operator=(const String& other) { |
| 2995 | clear(); |
| 2996 | appendBytes(other.getUnwrapped(), other.len()); |
| 2997 | return *this; |
| 2998 | } |
| 2999 | |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 3000 | inline char* getUnwrapped() const { |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 3001 | return mpBase; |
| 3002 | } |
| 3003 | |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 3004 | void clear() { |
| 3005 | mUsed = 0; |
| 3006 | if (mSize > 0) { |
| 3007 | mpBase[0] = 0; |
| 3008 | } |
| 3009 | } |
| 3010 | |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 3011 | void appendCStr(const char* s) { |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 3012 | appendBytes(s, strlen(s)); |
| 3013 | } |
| 3014 | |
| 3015 | void appendBytes(const char* s, int n) { |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 3016 | memcpy(ensure(n), s, n + 1); |
| 3017 | } |
| 3018 | |
| 3019 | void append(char c) { |
| 3020 | * ensure(1) = c; |
| 3021 | } |
| 3022 | |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 3023 | void append(String& other) { |
| 3024 | appendBytes(other.getUnwrapped(), other.len()); |
| 3025 | } |
| 3026 | |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 3027 | char* orphan() { |
| 3028 | char* result = mpBase; |
| 3029 | mpBase = 0; |
| 3030 | mUsed = 0; |
| 3031 | mSize = 0; |
| 3032 | return result; |
| 3033 | } |
| 3034 | |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 3035 | void printf(const char* fmt,...) { |
| 3036 | va_list ap; |
| 3037 | va_start(ap, fmt); |
| 3038 | vprintf(fmt, ap); |
| 3039 | va_end(ap); |
| 3040 | } |
| 3041 | |
| 3042 | void vprintf(const char* fmt, va_list ap) { |
| 3043 | char* temp; |
| 3044 | int numChars = vasprintf(&temp, fmt, ap); |
| 3045 | memcpy(ensure(numChars), temp, numChars+1); |
| 3046 | free(temp); |
| 3047 | } |
| 3048 | |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 3049 | inline size_t len() const { |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 3050 | return mUsed; |
| 3051 | } |
| 3052 | |
| 3053 | private: |
| 3054 | char* ensure(int n) { |
| 3055 | size_t newUsed = mUsed + n; |
| 3056 | if (newUsed > mSize) { |
| 3057 | size_t newSize = mSize * 2 + 10; |
| 3058 | if (newSize < newUsed) { |
| 3059 | newSize = newUsed; |
| 3060 | } |
| 3061 | mpBase = (char*) realloc(mpBase, newSize + 1); |
| 3062 | mSize = newSize; |
| 3063 | } |
| 3064 | mpBase[newUsed] = '\0'; |
| 3065 | char* result = mpBase + mUsed; |
| 3066 | mUsed = newUsed; |
| 3067 | return result; |
| 3068 | } |
| 3069 | |
| 3070 | char* mpBase; |
| 3071 | size_t mUsed; |
| 3072 | size_t mSize; |
| 3073 | }; |
| 3074 | |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3075 | void internKeywords() { |
| 3076 | // Note: order has to match TOK_ constants |
| 3077 | static const char* keywords[] = { |
| 3078 | "int", |
| 3079 | "char", |
| 3080 | "void", |
| 3081 | "if", |
| 3082 | "else", |
| 3083 | "while", |
| 3084 | "break", |
| 3085 | "return", |
| 3086 | "for", |
| 3087 | "pragma", |
| 3088 | "define", |
| 3089 | "auto", |
| 3090 | "case", |
| 3091 | "const", |
| 3092 | "continue", |
| 3093 | "default", |
| 3094 | "do", |
| 3095 | "double", |
| 3096 | "enum", |
| 3097 | "extern", |
| 3098 | "float", |
| 3099 | "goto", |
| 3100 | "long", |
| 3101 | "register", |
| 3102 | "short", |
| 3103 | "signed", |
| 3104 | "sizeof", |
| 3105 | "static", |
| 3106 | "struct", |
| 3107 | "switch", |
| 3108 | "typedef", |
| 3109 | "union", |
| 3110 | "unsigned", |
| 3111 | "volatile", |
| 3112 | "_Bool", |
| 3113 | "_Complex", |
| 3114 | "_Imaginary", |
| 3115 | "inline", |
| 3116 | "restrict", |
| 3117 | 0}; |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 3118 | |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3119 | for(int i = 0; keywords[i]; i++) { |
| 3120 | mTokenTable.intern(keywords[i], strlen(keywords[i])); |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 3121 | } |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3122 | } |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 3123 | |
Jack Palevich | 36d9414 | 2009-06-08 15:55:32 -0700 | [diff] [blame] | 3124 | struct InputState { |
| 3125 | InputStream* pStream; |
| 3126 | int oldCh; |
| 3127 | }; |
| 3128 | |
Jack Palevich | 2db168f | 2009-06-11 14:29:47 -0700 | [diff] [blame] | 3129 | struct VariableInfo { |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 3130 | void* pAddress; |
| 3131 | void* pForward; // For a forward direction, linked list of data to fix up |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3132 | tokenid_t tok; |
| 3133 | size_t level; |
| 3134 | VariableInfo* pOldDefinition; |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 3135 | Type* pType; |
Jack Palevich | 2db168f | 2009-06-11 14:29:47 -0700 | [diff] [blame] | 3136 | }; |
| 3137 | |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 3138 | class SymbolStack { |
| 3139 | public: |
| 3140 | SymbolStack() { |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3141 | mpArena = 0; |
| 3142 | mpTokenTable = 0; |
| 3143 | } |
| 3144 | |
| 3145 | void setArena(Arena* pArena) { |
| 3146 | mpArena = pArena; |
| 3147 | } |
| 3148 | |
| 3149 | void setTokenTable(TokenTable* pTokenTable) { |
| 3150 | mpTokenTable = pTokenTable; |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 3151 | } |
| 3152 | |
| 3153 | void pushLevel() { |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3154 | Mark mark; |
| 3155 | mark.mArenaMark = mpArena->mark(); |
| 3156 | mark.mSymbolHead = mStack.size(); |
| 3157 | mLevelStack.push_back(mark); |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 3158 | } |
| 3159 | |
| 3160 | void popLevel() { |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3161 | // Undo any shadowing that was done: |
| 3162 | Mark mark = mLevelStack.back(); |
| 3163 | mLevelStack.pop_back(); |
| 3164 | while (mStack.size() > mark.mSymbolHead) { |
| 3165 | VariableInfo* pV = mStack.back(); |
| 3166 | mStack.pop_back(); |
| 3167 | (*mpTokenTable)[pV->tok].mpVariableInfo = pV->pOldDefinition; |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 3168 | } |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3169 | mpArena->freeToMark(mark.mArenaMark); |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 3170 | } |
| 3171 | |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3172 | bool isDefinedAtCurrentLevel(tokenid_t tok) { |
| 3173 | VariableInfo* pV = (*mpTokenTable)[tok].mpVariableInfo; |
| 3174 | return pV && pV->level == level(); |
| 3175 | } |
| 3176 | |
| 3177 | VariableInfo* add(tokenid_t tok) { |
| 3178 | Token& token = (*mpTokenTable)[tok]; |
| 3179 | VariableInfo* pOldV = token.mpVariableInfo; |
| 3180 | VariableInfo* pNewV = |
| 3181 | (VariableInfo*) mpArena->alloc(sizeof(VariableInfo)); |
| 3182 | memset(pNewV, 0, sizeof(VariableInfo)); |
| 3183 | pNewV->tok = tok; |
| 3184 | pNewV->level = level(); |
| 3185 | pNewV->pOldDefinition = pOldV; |
| 3186 | token.mpVariableInfo = pNewV; |
| 3187 | mStack.push_back(pNewV); |
| 3188 | return pNewV; |
| 3189 | } |
| 3190 | |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 3191 | VariableInfo* add(Type* pType) { |
| 3192 | VariableInfo* pVI = add(pType->id); |
| 3193 | pVI->pType = pType; |
| 3194 | return pVI; |
| 3195 | } |
| 3196 | |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3197 | void forEach(bool (*fn)(VariableInfo*, void*), void* context) { |
| 3198 | for (size_t i = 0; i < mStack.size(); i++) { |
| 3199 | if (! fn(mStack[i], context)) { |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 3200 | break; |
| 3201 | } |
| 3202 | } |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 3203 | } |
| 3204 | |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 3205 | private: |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3206 | inline size_t level() { |
| 3207 | return mLevelStack.size(); |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 3208 | } |
| 3209 | |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3210 | struct Mark { |
| 3211 | Arena::Mark mArenaMark; |
| 3212 | size_t mSymbolHead; |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 3213 | }; |
| 3214 | |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3215 | Arena* mpArena; |
| 3216 | TokenTable* mpTokenTable; |
| 3217 | Vector<VariableInfo*> mStack; |
| 3218 | Vector<Mark> mLevelStack; |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 3219 | }; |
Jack Palevich | 36d9414 | 2009-06-08 15:55:32 -0700 | [diff] [blame] | 3220 | |
| 3221 | int ch; // Current input character, or EOF |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3222 | tokenid_t tok; // token |
Jack Palevich | 36d9414 | 2009-06-08 15:55:32 -0700 | [diff] [blame] | 3223 | intptr_t tokc; // token extra info |
Jack Palevich | 1aeb87b | 2009-07-06 18:33:20 -0700 | [diff] [blame] | 3224 | double tokd; // floating point constant value |
Jack Palevich | 36d9414 | 2009-06-08 15:55:32 -0700 | [diff] [blame] | 3225 | int tokl; // token operator level |
| 3226 | intptr_t rsym; // return symbol |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 3227 | Type* pReturnType; // type of the current function's return. |
Jack Palevich | 36d9414 | 2009-06-08 15:55:32 -0700 | [diff] [blame] | 3228 | intptr_t loc; // local variable index |
| 3229 | char* glo; // global variable index |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 3230 | String mTokenString; |
Jack Palevich | 36d9414 | 2009-06-08 15:55:32 -0700 | [diff] [blame] | 3231 | char* dptr; // Macro state: Points to macro text during macro playback. |
| 3232 | int dch; // Macro state: Saves old value of ch during a macro playback. |
Jack Palevich | 36d9414 | 2009-06-08 15:55:32 -0700 | [diff] [blame] | 3233 | char* pGlobalBase; |
Jack Palevich | 8c246a9 | 2009-07-14 21:14:10 -0700 | [diff] [blame] | 3234 | ACCSymbolLookupFn mpSymbolLookupFn; |
| 3235 | void* mpSymbolLookupContext; |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3236 | |
| 3237 | // Arena for the duration of the compile |
| 3238 | Arena mGlobalArena; |
| 3239 | // Arena for data that's only needed when compiling a single function |
| 3240 | Arena mLocalArena; |
| 3241 | |
| 3242 | TokenTable mTokenTable; |
| 3243 | SymbolStack mGlobals; |
| 3244 | SymbolStack mLocals; |
| 3245 | |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 3246 | // Prebuilt types, makes things slightly faster. |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 3247 | Type* mkpInt; // int |
| 3248 | Type* mkpChar; // char |
| 3249 | Type* mkpVoid; // void |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 3250 | Type* mkpFloat; |
| 3251 | Type* mkpDouble; |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 3252 | Type* mkpIntFn; |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 3253 | Type* mkpIntPtr; |
| 3254 | Type* mkpCharPtr; |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 3255 | Type* mkpFloatPtr; |
| 3256 | Type* mkpDoublePtr; |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 3257 | Type* mkpPtrIntFn; |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 3258 | |
Jack Palevich | 36d9414 | 2009-06-08 15:55:32 -0700 | [diff] [blame] | 3259 | InputStream* file; |
| 3260 | |
| 3261 | CodeBuf codeBuf; |
| 3262 | CodeGenerator* pGen; |
| 3263 | |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 3264 | String mErrorBuf; |
| 3265 | |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 3266 | String mPragmas; |
| 3267 | int mPragmaStringCount; |
Jack Palevich | ce105a9 | 2009-07-16 14:30:33 -0700 | [diff] [blame] | 3268 | int mCompileResult; |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 3269 | |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3270 | static const int ALLOC_SIZE = 99999; |
| 3271 | |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 3272 | static const int TOK_DUMMY = 1; |
| 3273 | static const int TOK_NUM = 2; |
Jack Palevich | 1aeb87b | 2009-07-06 18:33:20 -0700 | [diff] [blame] | 3274 | static const int TOK_NUM_FLOAT = 3; |
| 3275 | static const int TOK_NUM_DOUBLE = 4; |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 3276 | |
| 3277 | // 3..255 are character and/or operators |
| 3278 | |
Jack Palevich | 2db168f | 2009-06-11 14:29:47 -0700 | [diff] [blame] | 3279 | // Keywords start at 0x100 and increase by 1 |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3280 | // Order has to match string list in "internKeywords". |
| 3281 | enum { |
| 3282 | TOK_KEYWORD = TokenTable::TOKEN_BASE, |
| 3283 | TOK_INT = TOK_KEYWORD, |
| 3284 | TOK_CHAR, |
| 3285 | TOK_VOID, |
| 3286 | TOK_IF, |
| 3287 | TOK_ELSE, |
| 3288 | TOK_WHILE, |
| 3289 | TOK_BREAK, |
| 3290 | TOK_RETURN, |
| 3291 | TOK_FOR, |
| 3292 | TOK_PRAGMA, |
| 3293 | TOK_DEFINE, |
| 3294 | TOK_AUTO, |
| 3295 | TOK_CASE, |
| 3296 | TOK_CONST, |
| 3297 | TOK_CONTINUE, |
| 3298 | TOK_DEFAULT, |
| 3299 | TOK_DO, |
| 3300 | TOK_DOUBLE, |
| 3301 | TOK_ENUM, |
| 3302 | TOK_EXTERN, |
| 3303 | TOK_FLOAT, |
| 3304 | TOK_GOTO, |
| 3305 | TOK_LONG, |
| 3306 | TOK_REGISTER, |
| 3307 | TOK_SHORT, |
| 3308 | TOK_SIGNED, |
| 3309 | TOK_SIZEOF, |
| 3310 | TOK_STATIC, |
| 3311 | TOK_STRUCT, |
| 3312 | TOK_SWITCH, |
| 3313 | TOK_TYPEDEF, |
| 3314 | TOK_UNION, |
| 3315 | TOK_UNSIGNED, |
| 3316 | TOK_VOLATILE, |
| 3317 | TOK__BOOL, |
| 3318 | TOK__COMPLEX, |
| 3319 | TOK__IMAGINARY, |
| 3320 | TOK_INLINE, |
| 3321 | TOK_RESTRICT, |
| 3322 | // Symbols start after tokens |
| 3323 | TOK_SYMBOL |
| 3324 | }; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3325 | |
| 3326 | static const int LOCAL = 0x200; |
| 3327 | |
| 3328 | static const int SYM_FORWARD = 0; |
| 3329 | static const int SYM_DEFINE = 1; |
| 3330 | |
| 3331 | /* tokens in string heap */ |
| 3332 | static const int TAG_TOK = ' '; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3333 | |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 3334 | static const int OP_INCREMENT = 0; |
| 3335 | static const int OP_DECREMENT = 1; |
| 3336 | static const int OP_MUL = 2; |
| 3337 | static const int OP_DIV = 3; |
| 3338 | static const int OP_MOD = 4; |
| 3339 | static const int OP_PLUS = 5; |
| 3340 | static const int OP_MINUS = 6; |
| 3341 | static const int OP_SHIFT_LEFT = 7; |
| 3342 | static const int OP_SHIFT_RIGHT = 8; |
| 3343 | static const int OP_LESS_EQUAL = 9; |
| 3344 | static const int OP_GREATER_EQUAL = 10; |
| 3345 | static const int OP_LESS = 11; |
| 3346 | static const int OP_GREATER = 12; |
| 3347 | static const int OP_EQUALS = 13; |
| 3348 | static const int OP_NOT_EQUALS = 14; |
| 3349 | static const int OP_LOGICAL_AND = 15; |
| 3350 | static const int OP_LOGICAL_OR = 16; |
| 3351 | static const int OP_BIT_AND = 17; |
| 3352 | static const int OP_BIT_XOR = 18; |
| 3353 | static const int OP_BIT_OR = 19; |
| 3354 | static const int OP_BIT_NOT = 20; |
| 3355 | static const int OP_LOGICAL_NOT = 21; |
| 3356 | static const int OP_COUNT = 22; |
| 3357 | |
| 3358 | /* Operators are searched from front, the two-character operators appear |
| 3359 | * before the single-character operators with the same first character. |
| 3360 | * @ is used to pad out single-character operators. |
| 3361 | */ |
| 3362 | static const char* operatorChars; |
| 3363 | static const char operatorLevel[]; |
| 3364 | |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3365 | /* Called when we detect an internal problem. Does nothing in production. |
| 3366 | * |
| 3367 | */ |
| 3368 | void internalError() { |
| 3369 | * (char*) 0 = 0; |
| 3370 | } |
| 3371 | |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 3372 | void assert(bool isTrue) { |
| 3373 | if (!isTrue) { |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3374 | internalError(); |
| 3375 | } |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 3376 | } |
| 3377 | |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 3378 | bool isSymbol(tokenid_t t) { |
| 3379 | return t >= TOK_SYMBOL && |
| 3380 | ((size_t) (t-TOK_SYMBOL)) < mTokenTable.size(); |
| 3381 | } |
| 3382 | |
| 3383 | bool isSymbolOrKeyword(tokenid_t t) { |
| 3384 | return t >= TOK_KEYWORD && |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 3385 | ((size_t) (t-TOK_KEYWORD)) < mTokenTable.size(); |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 3386 | } |
| 3387 | |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 3388 | VariableInfo* VI(tokenid_t t) { |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 3389 | assert(isSymbol(t)); |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3390 | VariableInfo* pV = mTokenTable[t].mpVariableInfo; |
| 3391 | if (pV && pV->tok != t) { |
| 3392 | internalError(); |
| 3393 | } |
| 3394 | return pV; |
| 3395 | } |
| 3396 | |
| 3397 | inline bool isDefined(tokenid_t t) { |
| 3398 | return t >= TOK_SYMBOL && VI(t) != 0; |
| 3399 | } |
| 3400 | |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 3401 | const char* nameof(tokenid_t t) { |
| 3402 | assert(isSymbolOrKeyword(t)); |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3403 | return mTokenTable[t].pText; |
| 3404 | } |
| 3405 | |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3406 | void pdef(int t) { |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 3407 | mTokenString.append(t); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3408 | } |
| 3409 | |
| 3410 | void inp() { |
| 3411 | if (dptr) { |
Jack Palevich | 653f42d | 2009-05-28 17:15:32 -0700 | [diff] [blame] | 3412 | ch = *dptr++; |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 3413 | if (ch == 0) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3414 | dptr = 0; |
| 3415 | ch = dch; |
| 3416 | } |
| 3417 | } else |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 3418 | ch = file->getChar(); |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 3419 | #if 0 |
| 3420 | printf("ch='%c' 0x%x\n", ch, ch); |
| 3421 | #endif |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3422 | } |
| 3423 | |
| 3424 | int isid() { |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 3425 | return isalnum(ch) | (ch == '_'); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3426 | } |
| 3427 | |
Jack Palevich | 2aaf21f | 2009-07-15 16:16:37 -0700 | [diff] [blame] | 3428 | int decodeHex(int c) { |
| 3429 | if (isdigit(c)) { |
| 3430 | c -= '0'; |
| 3431 | } else if (c <= 'F') { |
| 3432 | c = c - 'A' + 10; |
| 3433 | } else { |
| 3434 | c =c - 'a' + 10; |
| 3435 | } |
| 3436 | return c; |
| 3437 | } |
| 3438 | |
Jack Palevich | b4758ff | 2009-06-12 12:49:14 -0700 | [diff] [blame] | 3439 | /* read a character constant, advances ch to after end of constant */ |
| 3440 | int getq() { |
| 3441 | int val = ch; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3442 | if (ch == '\\') { |
| 3443 | inp(); |
Jack Palevich | b4758ff | 2009-06-12 12:49:14 -0700 | [diff] [blame] | 3444 | if (isoctal(ch)) { |
| 3445 | // 1 to 3 octal characters. |
| 3446 | val = 0; |
| 3447 | for(int i = 0; i < 3; i++) { |
| 3448 | if (isoctal(ch)) { |
| 3449 | val = (val << 3) + ch - '0'; |
| 3450 | inp(); |
| 3451 | } |
| 3452 | } |
| 3453 | return val; |
| 3454 | } else if (ch == 'x' || ch == 'X') { |
| 3455 | // N hex chars |
| 3456 | inp(); |
| 3457 | if (! isxdigit(ch)) { |
| 3458 | error("'x' character escape requires at least one digit."); |
| 3459 | } else { |
| 3460 | val = 0; |
| 3461 | while (isxdigit(ch)) { |
Jack Palevich | 2aaf21f | 2009-07-15 16:16:37 -0700 | [diff] [blame] | 3462 | val = (val << 4) + decodeHex(ch); |
Jack Palevich | b4758ff | 2009-06-12 12:49:14 -0700 | [diff] [blame] | 3463 | inp(); |
| 3464 | } |
| 3465 | } |
| 3466 | } else { |
| 3467 | int val = ch; |
| 3468 | switch (ch) { |
| 3469 | case 'a': |
| 3470 | val = '\a'; |
| 3471 | break; |
| 3472 | case 'b': |
| 3473 | val = '\b'; |
| 3474 | break; |
| 3475 | case 'f': |
| 3476 | val = '\f'; |
| 3477 | break; |
| 3478 | case 'n': |
| 3479 | val = '\n'; |
| 3480 | break; |
| 3481 | case 'r': |
| 3482 | val = '\r'; |
| 3483 | break; |
| 3484 | case 't': |
| 3485 | val = '\t'; |
| 3486 | break; |
| 3487 | case 'v': |
| 3488 | val = '\v'; |
| 3489 | break; |
| 3490 | case '\\': |
| 3491 | val = '\\'; |
| 3492 | break; |
| 3493 | case '\'': |
| 3494 | val = '\''; |
| 3495 | break; |
| 3496 | case '"': |
| 3497 | val = '"'; |
| 3498 | break; |
| 3499 | case '?': |
| 3500 | val = '?'; |
| 3501 | break; |
| 3502 | default: |
| 3503 | error("Undefined character escape %c", ch); |
| 3504 | break; |
| 3505 | } |
| 3506 | inp(); |
| 3507 | return val; |
| 3508 | } |
| 3509 | } else { |
| 3510 | inp(); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3511 | } |
Jack Palevich | b4758ff | 2009-06-12 12:49:14 -0700 | [diff] [blame] | 3512 | return val; |
| 3513 | } |
| 3514 | |
| 3515 | static bool isoctal(int ch) { |
| 3516 | return ch >= '0' && ch <= '7'; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3517 | } |
| 3518 | |
Jack Palevich | 1aeb87b | 2009-07-06 18:33:20 -0700 | [diff] [blame] | 3519 | bool acceptCh(int c) { |
| 3520 | bool result = c == ch; |
| 3521 | if (result) { |
| 3522 | pdef(ch); |
| 3523 | inp(); |
| 3524 | } |
| 3525 | return result; |
| 3526 | } |
| 3527 | |
| 3528 | bool acceptDigitsCh() { |
| 3529 | bool result = false; |
| 3530 | while (isdigit(ch)) { |
| 3531 | result = true; |
| 3532 | pdef(ch); |
| 3533 | inp(); |
| 3534 | } |
| 3535 | return result; |
| 3536 | } |
| 3537 | |
| 3538 | void parseFloat() { |
| 3539 | tok = TOK_NUM_DOUBLE; |
| 3540 | // mTokenString already has the integral part of the number. |
Jack Palevich | 2aaf21f | 2009-07-15 16:16:37 -0700 | [diff] [blame] | 3541 | if(mTokenString.len() == 0) { |
| 3542 | mTokenString.append('0'); |
| 3543 | } |
Jack Palevich | 1aeb87b | 2009-07-06 18:33:20 -0700 | [diff] [blame] | 3544 | acceptCh('.'); |
| 3545 | acceptDigitsCh(); |
Jack Palevich | 1aeb87b | 2009-07-06 18:33:20 -0700 | [diff] [blame] | 3546 | if (acceptCh('e') || acceptCh('E')) { |
Jack Palevich | 2aaf21f | 2009-07-15 16:16:37 -0700 | [diff] [blame] | 3547 | acceptCh('-') || acceptCh('+'); |
| 3548 | acceptDigitsCh(); |
Jack Palevich | 1aeb87b | 2009-07-06 18:33:20 -0700 | [diff] [blame] | 3549 | } |
Jack Palevich | 2aaf21f | 2009-07-15 16:16:37 -0700 | [diff] [blame] | 3550 | if (ch == 'f' || ch == 'F') { |
| 3551 | tok = TOK_NUM_FLOAT; |
| 3552 | inp(); |
| 3553 | } else if (ch == 'l' || ch == 'L') { |
| 3554 | inp(); |
| 3555 | error("Long floating point constants not supported."); |
Jack Palevich | 1aeb87b | 2009-07-06 18:33:20 -0700 | [diff] [blame] | 3556 | } |
| 3557 | char* pText = mTokenString.getUnwrapped(); |
Jack Palevich | 2aaf21f | 2009-07-15 16:16:37 -0700 | [diff] [blame] | 3558 | char* pEnd = pText + strlen(pText); |
| 3559 | char* pEndPtr = 0; |
| 3560 | errno = 0; |
Jack Palevich | 1aeb87b | 2009-07-06 18:33:20 -0700 | [diff] [blame] | 3561 | if (tok == TOK_NUM_FLOAT) { |
Jack Palevich | 2aaf21f | 2009-07-15 16:16:37 -0700 | [diff] [blame] | 3562 | tokd = strtof(pText, &pEndPtr); |
Jack Palevich | 1aeb87b | 2009-07-06 18:33:20 -0700 | [diff] [blame] | 3563 | } else { |
Jack Palevich | 2aaf21f | 2009-07-15 16:16:37 -0700 | [diff] [blame] | 3564 | tokd = strtod(pText, &pEndPtr); |
Jack Palevich | 1aeb87b | 2009-07-06 18:33:20 -0700 | [diff] [blame] | 3565 | } |
Jack Palevich | 2aaf21f | 2009-07-15 16:16:37 -0700 | [diff] [blame] | 3566 | if (errno || pEndPtr != pEnd) { |
| 3567 | error("Can't parse constant: %s", pText); |
| 3568 | } |
| 3569 | // fprintf(stderr, "float constant: %s (%d) %g\n", pText, tok, tokd); |
Jack Palevich | 1aeb87b | 2009-07-06 18:33:20 -0700 | [diff] [blame] | 3570 | } |
| 3571 | |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3572 | void next() { |
| 3573 | int l, a; |
| 3574 | |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 3575 | while (isspace(ch) | (ch == '#')) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3576 | if (ch == '#') { |
| 3577 | inp(); |
| 3578 | next(); |
| 3579 | if (tok == TOK_DEFINE) { |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 3580 | doDefine(); |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 3581 | } else if (tok == TOK_PRAGMA) { |
| 3582 | doPragma(); |
| 3583 | } else { |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 3584 | error("Unsupported preprocessor directive \"%s\"", |
| 3585 | mTokenString.getUnwrapped()); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3586 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3587 | } |
| 3588 | inp(); |
| 3589 | } |
| 3590 | tokl = 0; |
| 3591 | tok = ch; |
| 3592 | /* encode identifiers & numbers */ |
Jack Palevich | 2aaf21f | 2009-07-15 16:16:37 -0700 | [diff] [blame] | 3593 | if (isdigit(ch) || ch == '.') { |
| 3594 | // Start of a numeric constant. Could be integer, float, or |
| 3595 | // double, won't know until we look further. |
| 3596 | mTokenString.clear(); |
| 3597 | pdef(ch); |
| 3598 | inp(); |
| 3599 | int base = 10; |
| 3600 | if (tok == '0') { |
| 3601 | if (ch == 'x' || ch == 'X') { |
| 3602 | base = 16; |
| 3603 | tok = TOK_NUM; |
| 3604 | tokc = 0; |
| 3605 | inp(); |
| 3606 | while ( isxdigit(ch) ) { |
| 3607 | tokc = (tokc << 4) + decodeHex(ch); |
| 3608 | inp(); |
| 3609 | } |
| 3610 | } else if (isoctal(ch)){ |
| 3611 | base = 8; |
| 3612 | tok = TOK_NUM; |
| 3613 | tokc = 0; |
| 3614 | while ( isoctal(ch) ) { |
| 3615 | tokc = (tokc << 3) + (ch - '0'); |
| 3616 | inp(); |
| 3617 | } |
| 3618 | } |
| 3619 | } else if (isdigit(tok)){ |
| 3620 | acceptDigitsCh(); |
| 3621 | } |
| 3622 | if (base == 10) { |
| 3623 | if (tok == '.' || ch == '.' || ch == 'e' || ch == 'E') { |
| 3624 | parseFloat(); |
| 3625 | } else { |
| 3626 | // It's an integer constant |
| 3627 | char* pText = mTokenString.getUnwrapped(); |
| 3628 | char* pEnd = pText + strlen(pText); |
| 3629 | char* pEndPtr = 0; |
| 3630 | errno = 0; |
| 3631 | tokc = strtol(pText, &pEndPtr, base); |
| 3632 | if (errno || pEndPtr != pEnd) { |
| 3633 | error("Can't parse constant: %s %d %d", pText, base, errno); |
| 3634 | } |
| 3635 | tok = TOK_NUM; |
| 3636 | } |
| 3637 | } |
| 3638 | } else if (isid()) { |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 3639 | mTokenString.clear(); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3640 | while (isid()) { |
Jack Palevich | e27bf3e | 2009-05-10 14:09:03 -0700 | [diff] [blame] | 3641 | pdef(ch); |
| 3642 | inp(); |
Jack Palevich | ae54f1f | 2009-05-08 14:54:15 -0700 | [diff] [blame] | 3643 | } |
Jack Palevich | 2aaf21f | 2009-07-15 16:16:37 -0700 | [diff] [blame] | 3644 | tok = mTokenTable.intern(mTokenString.getUnwrapped(), mTokenString.len()); |
| 3645 | // Is this a macro? |
| 3646 | char* pMacroDefinition = mTokenTable[tok].mpMacroDefinition; |
| 3647 | if (pMacroDefinition) { |
| 3648 | // Yes, it is a macro |
| 3649 | dptr = pMacroDefinition; |
| 3650 | dch = ch; |
| 3651 | inp(); |
| 3652 | next(); |
Jack Palevich | e27bf3e | 2009-05-10 14:09:03 -0700 | [diff] [blame] | 3653 | } |
Jack Palevich | e27bf3e | 2009-05-10 14:09:03 -0700 | [diff] [blame] | 3654 | } else { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3655 | inp(); |
| 3656 | if (tok == '\'') { |
| 3657 | tok = TOK_NUM; |
Jack Palevich | b4758ff | 2009-06-12 12:49:14 -0700 | [diff] [blame] | 3658 | tokc = getq(); |
| 3659 | if (ch != '\'') { |
| 3660 | error("Expected a ' character, got %c", ch); |
| 3661 | } else { |
| 3662 | inp(); |
| 3663 | } |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 3664 | } else if ((tok == '/') & (ch == '*')) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3665 | inp(); |
Jack Palevich | 22e3e8e | 2009-06-12 13:12:55 -0700 | [diff] [blame] | 3666 | while (ch && ch != EOF) { |
| 3667 | while (ch != '*' && ch != EOF) |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3668 | inp(); |
| 3669 | inp(); |
| 3670 | if (ch == '/') |
| 3671 | ch = 0; |
Jack Palevich | e27bf3e | 2009-05-10 14:09:03 -0700 | [diff] [blame] | 3672 | } |
Jack Palevich | 22e3e8e | 2009-06-12 13:12:55 -0700 | [diff] [blame] | 3673 | if (ch == EOF) { |
| 3674 | error("End of file inside comment."); |
| 3675 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3676 | inp(); |
Jack Palevich | e27bf3e | 2009-05-10 14:09:03 -0700 | [diff] [blame] | 3677 | next(); |
Jack Palevich | bd89490 | 2009-05-14 19:35:31 -0700 | [diff] [blame] | 3678 | } else if ((tok == '/') & (ch == '/')) { |
| 3679 | inp(); |
Jack Palevich | 22e3e8e | 2009-06-12 13:12:55 -0700 | [diff] [blame] | 3680 | while (ch && (ch != '\n') && (ch != EOF)) { |
Jack Palevich | bd89490 | 2009-05-14 19:35:31 -0700 | [diff] [blame] | 3681 | inp(); |
| 3682 | } |
| 3683 | inp(); |
| 3684 | next(); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3685 | } else { |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 3686 | const char* t = operatorChars; |
| 3687 | int opIndex = 0; |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 3688 | while ((l = *t++) != 0) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3689 | a = *t++; |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 3690 | tokl = operatorLevel[opIndex]; |
| 3691 | tokc = opIndex; |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 3692 | if ((l == tok) & ((a == ch) | (a == '@'))) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3693 | #if 0 |
| 3694 | printf("%c%c -> tokl=%d tokc=0x%x\n", |
| 3695 | l, a, tokl, tokc); |
| 3696 | #endif |
| 3697 | if (a == ch) { |
| 3698 | inp(); |
| 3699 | tok = TOK_DUMMY; /* dummy token for double tokens */ |
| 3700 | } |
| 3701 | break; |
| 3702 | } |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 3703 | opIndex++; |
| 3704 | } |
| 3705 | if (l == 0) { |
| 3706 | tokl = 0; |
| 3707 | tokc = 0; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3708 | } |
| 3709 | } |
| 3710 | } |
| 3711 | #if 0 |
| 3712 | { |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3713 | String buf; |
Jack Palevich | 37c54bd | 2009-07-14 18:35:36 -0700 | [diff] [blame] | 3714 | decodeToken(buf, tok, true); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 3715 | fprintf(stderr, "%s\n", buf.getUnwrapped()); |
| 3716 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3717 | #endif |
| 3718 | } |
| 3719 | |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 3720 | void doDefine() { |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3721 | next(); |
| 3722 | tokenid_t name = tok; |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 3723 | String* pName = new String(); |
| 3724 | while (isspace(ch)) { |
| 3725 | inp(); |
| 3726 | } |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 3727 | if (ch == '(') { |
| 3728 | delete pName; |
| 3729 | error("Defines with arguments not supported"); |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 3730 | return; |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 3731 | } |
| 3732 | while (isspace(ch)) { |
| 3733 | inp(); |
| 3734 | } |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3735 | String value; |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 3736 | while (ch != '\n' && ch != EOF) { |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3737 | value.append(ch); |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 3738 | inp(); |
| 3739 | } |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3740 | char* pDefn = (char*)mGlobalArena.alloc(value.len() + 1); |
| 3741 | memcpy(pDefn, value.getUnwrapped(), value.len()); |
| 3742 | pDefn[value.len()] = 0; |
| 3743 | mTokenTable[name].mpMacroDefinition = pDefn; |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 3744 | } |
| 3745 | |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 3746 | void doPragma() { |
| 3747 | // # pragma name(val) |
| 3748 | int state = 0; |
| 3749 | while(ch != EOF && ch != '\n' && state < 10) { |
| 3750 | switch(state) { |
| 3751 | case 0: |
| 3752 | if (isspace(ch)) { |
| 3753 | inp(); |
| 3754 | } else { |
| 3755 | state++; |
| 3756 | } |
| 3757 | break; |
| 3758 | case 1: |
| 3759 | if (isalnum(ch)) { |
| 3760 | mPragmas.append(ch); |
| 3761 | inp(); |
| 3762 | } else if (ch == '(') { |
| 3763 | mPragmas.append(0); |
| 3764 | inp(); |
| 3765 | state++; |
| 3766 | } else { |
| 3767 | state = 11; |
| 3768 | } |
| 3769 | break; |
| 3770 | case 2: |
| 3771 | if (isalnum(ch)) { |
| 3772 | mPragmas.append(ch); |
| 3773 | inp(); |
| 3774 | } else if (ch == ')') { |
| 3775 | mPragmas.append(0); |
| 3776 | inp(); |
| 3777 | state = 10; |
| 3778 | } else { |
| 3779 | state = 11; |
| 3780 | } |
| 3781 | break; |
| 3782 | } |
| 3783 | } |
| 3784 | if(state != 10) { |
| 3785 | error("Unexpected pragma syntax"); |
| 3786 | } |
| 3787 | mPragmaStringCount += 2; |
| 3788 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3789 | |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 3790 | virtual void verror(const char* fmt, va_list ap) { |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 3791 | mErrorBuf.printf("%ld: ", file->getLine()); |
| 3792 | mErrorBuf.vprintf(fmt, ap); |
| 3793 | mErrorBuf.printf("\n"); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3794 | } |
| 3795 | |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 3796 | void skip(intptr_t c) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3797 | if (tok != c) { |
| 3798 | error("'%c' expected", c); |
| 3799 | } |
| 3800 | next(); |
| 3801 | } |
| 3802 | |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 3803 | bool accept(intptr_t c) { |
| 3804 | if (tok == c) { |
| 3805 | next(); |
| 3806 | return true; |
| 3807 | } |
| 3808 | return false; |
| 3809 | } |
| 3810 | |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 3811 | bool acceptStringLiteral() { |
| 3812 | if (tok == '"') { |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 3813 | pGen->li((int) glo, mkpCharPtr); |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 3814 | // This while loop merges multiple adjacent string constants. |
| 3815 | while (tok == '"') { |
| 3816 | while (ch != '"' && ch != EOF) { |
Jack Palevich | 9cbd226 | 2009-07-08 16:48:41 -0700 | [diff] [blame] | 3817 | *allocGlobalSpace(1,1) = getq(); |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 3818 | } |
| 3819 | if (ch != '"') { |
| 3820 | error("Unterminated string constant."); |
| 3821 | } |
| 3822 | inp(); |
| 3823 | next(); |
Jack Palevich | b4758ff | 2009-06-12 12:49:14 -0700 | [diff] [blame] | 3824 | } |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 3825 | /* Null terminate */ |
Jack Palevich | 653f42d | 2009-05-28 17:15:32 -0700 | [diff] [blame] | 3826 | *glo = 0; |
Jack Palevich | f1f39cc | 2009-05-29 18:03:15 -0700 | [diff] [blame] | 3827 | /* align heap */ |
Jack Palevich | 9cbd226 | 2009-07-08 16:48:41 -0700 | [diff] [blame] | 3828 | allocGlobalSpace(1,(char*) (((intptr_t) glo + 4) & -4) - glo); |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 3829 | |
| 3830 | return true; |
| 3831 | } |
| 3832 | return false; |
| 3833 | } |
Jack Palevich | 8c246a9 | 2009-07-14 21:14:10 -0700 | [diff] [blame] | 3834 | |
Jack Palevich | b1544ca | 2009-07-16 15:09:20 -0700 | [diff] [blame^] | 3835 | void linkGlobal(tokenid_t t, bool isFunction) { |
| 3836 | VariableInfo* pVI = VI(t); |
| 3837 | void* n = NULL; |
| 3838 | if (mpSymbolLookupFn) { |
| 3839 | n = mpSymbolLookupFn(mpSymbolLookupContext, nameof(t)); |
| 3840 | } |
| 3841 | if (pVI->pType == NULL) { |
| 3842 | if (isFunction) { |
| 3843 | pVI->pType = mkpIntFn; |
| 3844 | } else { |
| 3845 | pVI->pType = mkpInt; |
| 3846 | } |
| 3847 | } |
| 3848 | pVI->pAddress = n; |
| 3849 | } |
| 3850 | |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 3851 | /* Parse and evaluate a unary expression. |
| 3852 | * allowAssignment is true if '=' parsing wanted (quick hack) |
| 3853 | */ |
| 3854 | void unary(bool allowAssignment) { |
Jack Palevich | b1544ca | 2009-07-16 15:09:20 -0700 | [diff] [blame^] | 3855 | tokenid_t t; |
| 3856 | intptr_t n, a; |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 3857 | t = 0; |
| 3858 | n = 1; /* type of expression 0 = forward, 1 = value, other = lvalue */ |
| 3859 | if (acceptStringLiteral()) { |
| 3860 | // Nothing else to do. |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3861 | } else { |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 3862 | int c = tokl; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3863 | a = tokc; |
Jack Palevich | 1aeb87b | 2009-07-06 18:33:20 -0700 | [diff] [blame] | 3864 | double ad = tokd; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3865 | t = tok; |
| 3866 | next(); |
| 3867 | if (t == TOK_NUM) { |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 3868 | pGen->li(a, mkpInt); |
Jack Palevich | 1aeb87b | 2009-07-06 18:33:20 -0700 | [diff] [blame] | 3869 | } else if (t == TOK_NUM_FLOAT) { |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 3870 | // Align to 4-byte boundary |
| 3871 | glo = (char*) (((intptr_t) glo + 3) & -4); |
| 3872 | * (float*) glo = (float) ad; |
| 3873 | pGen->loadFloat((int) glo, mkpFloat); |
| 3874 | glo += 4; |
Jack Palevich | 1aeb87b | 2009-07-06 18:33:20 -0700 | [diff] [blame] | 3875 | } else if (t == TOK_NUM_DOUBLE) { |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 3876 | // Align to 8-byte boundary |
| 3877 | glo = (char*) (((intptr_t) glo + 7) & -8); |
| 3878 | * (double*) glo = ad; |
| 3879 | pGen->loadFloat((int) glo, mkpDouble); |
| 3880 | glo += 8; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3881 | } else if (c == 2) { |
| 3882 | /* -, +, !, ~ */ |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 3883 | unary(false); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3884 | if (t == '!') |
Jack Palevich | a39749f | 2009-07-08 20:40:31 -0700 | [diff] [blame] | 3885 | pGen->gUnaryCmp(a, mkpInt); |
| 3886 | else if (t == '+') { |
| 3887 | // ignore unary plus. |
| 3888 | } else { |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 3889 | pGen->genUnaryOp(a); |
Jack Palevich | a39749f | 2009-07-08 20:40:31 -0700 | [diff] [blame] | 3890 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3891 | } else if (t == '(') { |
Jack Palevich | 45431bc | 2009-07-13 15:57:26 -0700 | [diff] [blame] | 3892 | // It's either a cast or an expression |
| 3893 | Type* pCast = acceptCastTypeDeclaration(mLocalArena); |
| 3894 | if (pCast) { |
| 3895 | skip(')'); |
| 3896 | unary(false); |
| 3897 | pGen->convertR0(pCast); |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 3898 | } else { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3899 | expr(); |
Jack Palevich | 45431bc | 2009-07-13 15:57:26 -0700 | [diff] [blame] | 3900 | skip(')'); |
| 3901 | } |
| 3902 | } else if (t == '*') { |
| 3903 | /* This is a pointer dereference. |
| 3904 | */ |
| 3905 | unary(false); |
| 3906 | Type* pR0Type = pGen->getR0Type(); |
| 3907 | if (pR0Type->tag != TY_POINTER) { |
| 3908 | error("Expected a pointer type."); |
| 3909 | } else { |
| 3910 | if (pR0Type->pHead->tag == TY_FUNC) { |
| 3911 | t = 0; |
| 3912 | } |
| 3913 | if (accept('=')) { |
| 3914 | pGen->pushR0(); |
| 3915 | expr(); |
| 3916 | pGen->storeR0ToTOS(pR0Type); |
| 3917 | } else if (t) { |
| 3918 | pGen->loadR0FromR0(pR0Type); |
| 3919 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3920 | } |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 3921 | // Else we fall through to the function call below, with |
| 3922 | // t == 0 to trigger an indirect function call. Hack! |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3923 | } else if (t == '&') { |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 3924 | VariableInfo* pVI = VI(tok); |
| 3925 | pGen->leaR0((int) pVI->pAddress, |
| 3926 | createPtrType(pVI->pType, mLocalArena)); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3927 | next(); |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 3928 | } else if (t == EOF ) { |
| 3929 | error("Unexpected EOF."); |
Jack Palevich | d1f57e6 | 2009-07-15 18:23:22 -0700 | [diff] [blame] | 3930 | } else if (t == ';') { |
| 3931 | error("Unexpected ';'"); |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 3932 | } else if (!checkSymbol(t)) { |
Jack Palevich | a1804dd | 2009-06-12 14:40:04 -0700 | [diff] [blame] | 3933 | // Don't have to do anything special here, the error |
| 3934 | // message was printed by checkSymbol() above. |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3935 | } else { |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3936 | if (!isDefined(t)) { |
| 3937 | mGlobals.add(t); |
| 3938 | // printf("Adding new global function %s\n", nameof(t)); |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 3939 | } |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 3940 | VariableInfo* pVI = VI(t); |
| 3941 | n = (intptr_t) pVI->pAddress; |
Jack Palevich | 8c246a9 | 2009-07-14 21:14:10 -0700 | [diff] [blame] | 3942 | /* forward reference: try our lookup function */ |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 3943 | if (!n) { |
Jack Palevich | b1544ca | 2009-07-16 15:09:20 -0700 | [diff] [blame^] | 3944 | linkGlobal(t, tok == '('); |
| 3945 | n = (intptr_t) pVI->pAddress; |
| 3946 | if (!n && tok != '(') { |
| 3947 | error("Undeclared variable %s\n", nameof(t)); |
Jack Palevich | 8c246a9 | 2009-07-14 21:14:10 -0700 | [diff] [blame] | 3948 | } |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 3949 | } |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 3950 | if ((tok == '=') & allowAssignment) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3951 | /* assignment */ |
| 3952 | next(); |
| 3953 | expr(); |
Jack Palevich | 9cbd226 | 2009-07-08 16:48:41 -0700 | [diff] [blame] | 3954 | pGen->storeR0(n, pVI->pType); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3955 | } else if (tok != '(') { |
| 3956 | /* variable */ |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 3957 | if (!n) { |
Jack Palevich | b1544ca | 2009-07-16 15:09:20 -0700 | [diff] [blame^] | 3958 | linkGlobal(t, false); |
| 3959 | n = (intptr_t) pVI->pAddress; |
| 3960 | if (!n) { |
| 3961 | error("Undeclared variable %s\n", nameof(t)); |
| 3962 | } |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 3963 | } |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 3964 | pGen->loadR0(n, tokl == 11, tokc, pVI->pType); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3965 | if (tokl == 11) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3966 | next(); |
| 3967 | } |
| 3968 | } |
| 3969 | } |
| 3970 | } |
| 3971 | |
| 3972 | /* function call */ |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 3973 | if (accept('(')) { |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 3974 | Type* pDecl = NULL; |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 3975 | VariableInfo* pVI = NULL; |
| 3976 | if (n == 1) { // Indirect function call, push address of fn. |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 3977 | pDecl = pGen->getR0Type(); |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 3978 | pGen->pushR0(); |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 3979 | } else { |
| 3980 | pVI = VI(t); |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 3981 | pDecl = pVI->pType; |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 3982 | } |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 3983 | Type* pArgList = pDecl->pTail; |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 3984 | bool varArgs = pArgList == NULL; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3985 | /* push args and invert order */ |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 3986 | a = pGen->beginFunctionCallArguments(); |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 3987 | int l = 0; |
Jack Palevich | 2a4e1a9 | 2009-07-09 13:34:25 -0700 | [diff] [blame] | 3988 | int argCount = 0; |
Jack Palevich | b4758ff | 2009-06-12 12:49:14 -0700 | [diff] [blame] | 3989 | while (tok != ')' && tok != EOF) { |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 3990 | if (! varArgs && !pArgList) { |
Jack Palevich | ce105a9 | 2009-07-16 14:30:33 -0700 | [diff] [blame] | 3991 | error("Unexpected argument."); |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 3992 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3993 | expr(); |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 3994 | Type* pTargetType; |
| 3995 | if (pArgList) { |
| 3996 | pTargetType = pArgList->pHead; |
| 3997 | pArgList = pArgList->pTail; |
| 3998 | } else { |
| 3999 | pTargetType = pGen->getR0Type(); |
| 4000 | if (pTargetType->tag == TY_FLOAT) { |
| 4001 | pTargetType = mkpDouble; |
| 4002 | } |
| 4003 | } |
Jack Palevich | 2a4e1a9 | 2009-07-09 13:34:25 -0700 | [diff] [blame] | 4004 | if (pTargetType->tag == TY_VOID) { |
| 4005 | error("Can't pass void value for argument %d", |
| 4006 | argCount + 1); |
| 4007 | } else { |
| 4008 | pGen->convertR0(pTargetType); |
| 4009 | l += pGen->storeR0ToArg(l); |
| 4010 | } |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 4011 | if (accept(',')) { |
| 4012 | // fine |
| 4013 | } else if ( tok != ')') { |
| 4014 | error("Expected ',' or ')'"); |
| 4015 | } |
Jack Palevich | 2a4e1a9 | 2009-07-09 13:34:25 -0700 | [diff] [blame] | 4016 | argCount += 1; |
Jack Palevich | e27bf3e | 2009-05-10 14:09:03 -0700 | [diff] [blame] | 4017 | } |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 4018 | if (! varArgs && pArgList) { |
Jack Palevich | ce105a9 | 2009-07-16 14:30:33 -0700 | [diff] [blame] | 4019 | error("Expected more argument(s). Saw %d", argCount); |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 4020 | } |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 4021 | pGen->endFunctionCallArguments(pDecl, a, l); |
Jack Palevich | b4758ff | 2009-06-12 12:49:14 -0700 | [diff] [blame] | 4022 | skip(')'); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4023 | if (!n) { |
| 4024 | /* forward reference */ |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 4025 | pVI->pForward = (void*) pGen->callForward((int) pVI->pForward, |
| 4026 | pVI->pType); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4027 | } else if (n == 1) { |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 4028 | pGen->callIndirect(l, mkpPtrIntFn->pHead); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4029 | } else { |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 4030 | pGen->callRelative(n - codeBuf.getPC() - pGen->jumpOffset(), |
| 4031 | VI(t)->pType); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4032 | } |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 4033 | pGen->adjustStackAfterCall(pDecl, l, n == 1); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4034 | } |
| 4035 | } |
| 4036 | |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 4037 | /* Recursive descent parser for binary operations. |
| 4038 | */ |
| 4039 | void binaryOp(int level) { |
Jack Palevich | 7ecc555 | 2009-07-14 16:24:55 -0700 | [diff] [blame] | 4040 | intptr_t t, a; |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 4041 | t = 0; |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 4042 | if (level-- == 1) |
| 4043 | unary(true); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4044 | else { |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 4045 | binaryOp(level); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4046 | a = 0; |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 4047 | while (level == tokl) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4048 | t = tokc; |
| 4049 | next(); |
| 4050 | |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 4051 | if (level > 8) { |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 4052 | a = pGen->gtst(t == OP_LOGICAL_OR, a); /* && and || output code generation */ |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 4053 | binaryOp(level); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4054 | } else { |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 4055 | pGen->pushR0(); |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 4056 | binaryOp(level); |
Jack Palevich | d1f57e6 | 2009-07-15 18:23:22 -0700 | [diff] [blame] | 4057 | // Check for syntax error. |
| 4058 | if (pGen->getR0Type() == NULL) { |
| 4059 | // We failed to parse a right-hand argument. |
| 4060 | // Push a dummy value so we don't fail |
| 4061 | pGen->li(0, mkpInt); |
| 4062 | } |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 4063 | if ((level == 4) | (level == 5)) { |
Jack Palevich | a39749f | 2009-07-08 20:40:31 -0700 | [diff] [blame] | 4064 | pGen->gcmp(t, mkpInt); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4065 | } else { |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 4066 | pGen->genOp(t); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4067 | } |
| 4068 | } |
| 4069 | } |
| 4070 | /* && and || output code generation */ |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 4071 | if (a && level > 8) { |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 4072 | a = pGen->gtst(t == OP_LOGICAL_OR, a); |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 4073 | pGen->li(t != OP_LOGICAL_OR, mkpInt); |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 4074 | pGen->gjmp(5); /* jmp $ + 5 (sizeof li, FIXME for ARM) */ |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 4075 | pGen->gsym(a); |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 4076 | pGen->li(t == OP_LOGICAL_OR, mkpInt); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4077 | } |
| 4078 | } |
| 4079 | } |
| 4080 | |
| 4081 | void expr() { |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 4082 | binaryOp(11); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4083 | } |
| 4084 | |
| 4085 | int test_expr() { |
| 4086 | expr(); |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 4087 | return pGen->gtst(0, 0); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4088 | } |
| 4089 | |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 4090 | void block(intptr_t l, bool outermostFunctionBlock) { |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 4091 | intptr_t a, n, t; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4092 | |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 4093 | Type* pBaseType; |
| 4094 | if ((pBaseType = acceptPrimitiveType(mLocalArena))) { |
Jack Palevich | a1804dd | 2009-06-12 14:40:04 -0700 | [diff] [blame] | 4095 | /* declarations */ |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 4096 | localDeclarations(pBaseType); |
Jack Palevich | a1804dd | 2009-06-12 14:40:04 -0700 | [diff] [blame] | 4097 | } else if (tok == TOK_IF) { |
Jack Palevich | e27bf3e | 2009-05-10 14:09:03 -0700 | [diff] [blame] | 4098 | next(); |
| 4099 | skip('('); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4100 | a = test_expr(); |
| 4101 | skip(')'); |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 4102 | block(l, false); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4103 | if (tok == TOK_ELSE) { |
Jack Palevich | e27bf3e | 2009-05-10 14:09:03 -0700 | [diff] [blame] | 4104 | next(); |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 4105 | n = pGen->gjmp(0); /* jmp */ |
| 4106 | pGen->gsym(a); |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 4107 | block(l, false); |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 4108 | pGen->gsym(n); /* patch else jmp */ |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4109 | } else { |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 4110 | pGen->gsym(a); /* patch if test */ |
Jack Palevich | e27bf3e | 2009-05-10 14:09:03 -0700 | [diff] [blame] | 4111 | } |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 4112 | } else if ((tok == TOK_WHILE) | (tok == TOK_FOR)) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4113 | t = tok; |
| 4114 | next(); |
| 4115 | skip('('); |
| 4116 | if (t == TOK_WHILE) { |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 4117 | n = codeBuf.getPC(); // top of loop, target of "next" iteration |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4118 | a = test_expr(); |
| 4119 | } else { |
| 4120 | if (tok != ';') |
| 4121 | expr(); |
| 4122 | skip(';'); |
| 4123 | n = codeBuf.getPC(); |
| 4124 | a = 0; |
| 4125 | if (tok != ';') |
| 4126 | a = test_expr(); |
| 4127 | skip(';'); |
| 4128 | if (tok != ')') { |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 4129 | t = pGen->gjmp(0); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4130 | expr(); |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 4131 | pGen->gjmp(n - codeBuf.getPC() - pGen->jumpOffset()); |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 4132 | pGen->gsym(t); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4133 | n = t + 4; |
| 4134 | } |
| 4135 | } |
| 4136 | skip(')'); |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 4137 | block((intptr_t) &a, false); |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 4138 | pGen->gjmp(n - codeBuf.getPC() - pGen->jumpOffset()); /* jmp */ |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 4139 | pGen->gsym(a); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4140 | } else if (tok == '{') { |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 4141 | if (! outermostFunctionBlock) { |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 4142 | mLocals.pushLevel(); |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 4143 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4144 | next(); |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 4145 | while (tok != '}' && tok != EOF) |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 4146 | block(l, false); |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 4147 | skip('}'); |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 4148 | if (! outermostFunctionBlock) { |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 4149 | mLocals.popLevel(); |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 4150 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4151 | } else { |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 4152 | if (accept(TOK_RETURN)) { |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 4153 | if (tok != ';') { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4154 | expr(); |
Jack Palevich | 2a4e1a9 | 2009-07-09 13:34:25 -0700 | [diff] [blame] | 4155 | if (pReturnType->tag == TY_VOID) { |
| 4156 | error("Must not return a value from a void function"); |
| 4157 | } else { |
| 4158 | pGen->convertR0(pReturnType); |
| 4159 | } |
| 4160 | } else { |
| 4161 | if (pReturnType->tag != TY_VOID) { |
| 4162 | error("Must specify a value here"); |
| 4163 | } |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 4164 | } |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 4165 | rsym = pGen->gjmp(rsym); /* jmp */ |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 4166 | } else if (accept(TOK_BREAK)) { |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 4167 | *(int *) l = pGen->gjmp(*(int *) l); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4168 | } else if (tok != ';') |
| 4169 | expr(); |
| 4170 | skip(';'); |
Jack Palevich | e27bf3e | 2009-05-10 14:09:03 -0700 | [diff] [blame] | 4171 | } |
| 4172 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4173 | |
Jack Palevich | a8f427f | 2009-07-13 18:40:08 -0700 | [diff] [blame] | 4174 | static bool typeEqual(Type* a, Type* b) { |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 4175 | if (a == b) { |
| 4176 | return true; |
| 4177 | } |
| 4178 | if (a == NULL || b == NULL) { |
| 4179 | return false; |
| 4180 | } |
| 4181 | TypeTag at = a->tag; |
| 4182 | if (at != b->tag) { |
| 4183 | return false; |
| 4184 | } |
| 4185 | if (at == TY_POINTER) { |
| 4186 | return typeEqual(a->pHead, b->pHead); |
| 4187 | } else if (at == TY_FUNC || at == TY_PARAM) { |
| 4188 | return typeEqual(a->pHead, b->pHead) |
| 4189 | && typeEqual(a->pTail, b->pTail); |
| 4190 | } |
| 4191 | return true; |
| 4192 | } |
| 4193 | |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4194 | Type* createType(TypeTag tag, Type* pHead, Type* pTail, Arena& arena) { |
| 4195 | assert(tag >= TY_INT && tag <= TY_PARAM); |
| 4196 | Type* pType = (Type*) arena.alloc(sizeof(Type)); |
| 4197 | memset(pType, 0, sizeof(*pType)); |
| 4198 | pType->tag = tag; |
| 4199 | pType->pHead = pHead; |
| 4200 | pType->pTail = pTail; |
| 4201 | return pType; |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 4202 | } |
| 4203 | |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 4204 | Type* createPtrType(Type* pType, Arena& arena) { |
| 4205 | return createType(TY_POINTER, pType, NULL, arena); |
| 4206 | } |
| 4207 | |
| 4208 | /** |
| 4209 | * Try to print a type in declaration order |
| 4210 | */ |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4211 | void decodeType(String& buffer, Type* pType) { |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 4212 | buffer.clear(); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4213 | if (pType == NULL) { |
| 4214 | buffer.appendCStr("null"); |
| 4215 | return; |
| 4216 | } |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 4217 | decodeTypeImp(buffer, pType); |
| 4218 | } |
| 4219 | |
| 4220 | void decodeTypeImp(String& buffer, Type* pType) { |
| 4221 | decodeTypeImpPrefix(buffer, pType); |
| 4222 | |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4223 | String temp; |
| 4224 | if (pType->id != 0) { |
Jack Palevich | 37c54bd | 2009-07-14 18:35:36 -0700 | [diff] [blame] | 4225 | decodeToken(temp, pType->id, false); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4226 | buffer.append(temp); |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 4227 | } |
| 4228 | |
| 4229 | decodeTypeImpPostfix(buffer, pType); |
| 4230 | } |
| 4231 | |
| 4232 | void decodeTypeImpPrefix(String& buffer, Type* pType) { |
| 4233 | TypeTag tag = pType->tag; |
| 4234 | |
Jack Palevich | 37c54bd | 2009-07-14 18:35:36 -0700 | [diff] [blame] | 4235 | if (tag >= TY_INT && tag <= TY_DOUBLE) { |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 4236 | switch (tag) { |
| 4237 | case TY_INT: |
| 4238 | buffer.appendCStr("int"); |
| 4239 | break; |
| 4240 | case TY_CHAR: |
| 4241 | buffer.appendCStr("char"); |
| 4242 | break; |
| 4243 | case TY_VOID: |
| 4244 | buffer.appendCStr("void"); |
| 4245 | break; |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 4246 | case TY_FLOAT: |
| 4247 | buffer.appendCStr("float"); |
| 4248 | break; |
| 4249 | case TY_DOUBLE: |
| 4250 | buffer.appendCStr("double"); |
| 4251 | break; |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 4252 | default: |
| 4253 | break; |
| 4254 | } |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4255 | buffer.append(' '); |
| 4256 | } |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 4257 | |
| 4258 | switch (tag) { |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4259 | case TY_INT: |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4260 | break; |
| 4261 | case TY_CHAR: |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4262 | break; |
| 4263 | case TY_VOID: |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 4264 | break; |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 4265 | case TY_FLOAT: |
| 4266 | break; |
| 4267 | case TY_DOUBLE: |
| 4268 | break; |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4269 | case TY_POINTER: |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 4270 | decodeTypeImpPrefix(buffer, pType->pHead); |
| 4271 | if(pType->pHead && pType->pHead->tag == TY_FUNC) { |
| 4272 | buffer.append('('); |
| 4273 | } |
| 4274 | buffer.append('*'); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4275 | break; |
| 4276 | case TY_FUNC: |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 4277 | decodeTypeImp(buffer, pType->pHead); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4278 | break; |
| 4279 | case TY_PARAM: |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 4280 | decodeTypeImp(buffer, pType->pHead); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4281 | break; |
| 4282 | default: |
| 4283 | String temp; |
| 4284 | temp.printf("Unknown tag %d", pType->tag); |
| 4285 | buffer.append(temp); |
| 4286 | break; |
| 4287 | } |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 4288 | } |
| 4289 | |
| 4290 | void decodeTypeImpPostfix(String& buffer, Type* pType) { |
| 4291 | TypeTag tag = pType->tag; |
| 4292 | |
| 4293 | switch(tag) { |
| 4294 | case TY_POINTER: |
| 4295 | if(pType->pHead && pType->pHead->tag == TY_FUNC) { |
| 4296 | buffer.append(')'); |
| 4297 | } |
| 4298 | decodeTypeImpPostfix(buffer, pType->pHead); |
| 4299 | break; |
| 4300 | case TY_FUNC: |
| 4301 | buffer.append('('); |
| 4302 | for(Type* pArg = pType->pTail; pArg; pArg = pArg->pTail) { |
| 4303 | decodeTypeImp(buffer, pArg); |
| 4304 | if (pArg->pTail) { |
| 4305 | buffer.appendCStr(", "); |
| 4306 | } |
| 4307 | } |
| 4308 | buffer.append(')'); |
| 4309 | break; |
| 4310 | default: |
| 4311 | break; |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4312 | } |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 4313 | } |
| 4314 | |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4315 | void printType(Type* pType) { |
| 4316 | String buffer; |
| 4317 | decodeType(buffer, pType); |
| 4318 | fprintf(stderr, "%s\n", buffer.getUnwrapped()); |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 4319 | } |
| 4320 | |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4321 | Type* acceptPrimitiveType(Arena& arena) { |
| 4322 | Type* pType; |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 4323 | if (tok == TOK_INT) { |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4324 | pType = mkpInt; |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 4325 | } else if (tok == TOK_CHAR) { |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4326 | pType = mkpChar; |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 4327 | } else if (tok == TOK_VOID) { |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4328 | pType = mkpVoid; |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 4329 | } else if (tok == TOK_FLOAT) { |
| 4330 | pType = mkpFloat; |
| 4331 | } else if (tok == TOK_DOUBLE) { |
| 4332 | pType = mkpDouble; |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 4333 | } else { |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4334 | return NULL; |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 4335 | } |
| 4336 | next(); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4337 | return pType; |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 4338 | } |
| 4339 | |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 4340 | Type* acceptDeclaration(Type* pType, bool nameAllowed, bool nameRequired, |
| 4341 | Arena& arena) { |
| 4342 | tokenid_t declName = 0; |
| 4343 | pType = acceptDecl2(pType, declName, nameAllowed, |
| 4344 | nameRequired, arena); |
| 4345 | if (declName) { |
| 4346 | // Clone the parent type so we can set a unique ID |
| 4347 | pType = createType(pType->tag, pType->pHead, |
| 4348 | pType->pTail, arena); |
| 4349 | |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4350 | pType->id = declName; |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4351 | } |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 4352 | // fprintf(stderr, "Parsed a declaration: "); |
| 4353 | // printType(pType); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4354 | return pType; |
| 4355 | } |
| 4356 | |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 4357 | Type* expectDeclaration(Type* pBaseType, Arena& arena) { |
| 4358 | Type* pType = acceptDeclaration(pBaseType, true, true, arena); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4359 | if (! pType) { |
| 4360 | error("Expected a declaration"); |
| 4361 | } |
| 4362 | return pType; |
| 4363 | } |
| 4364 | |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 4365 | /* Used for accepting types that appear in casts */ |
| 4366 | Type* acceptCastTypeDeclaration(Arena& arena) { |
| 4367 | Type* pType = acceptPrimitiveType(arena); |
| 4368 | if (pType) { |
| 4369 | pType = acceptDeclaration(pType, false, false, arena); |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 4370 | } |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4371 | return pType; |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 4372 | } |
| 4373 | |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 4374 | Type* expectCastTypeDeclaration(Arena& arena) { |
| 4375 | Type* pType = acceptCastTypeDeclaration(arena); |
| 4376 | if (! pType) { |
| 4377 | error("Expected a declaration"); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4378 | } |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 4379 | return pType; |
| 4380 | } |
| 4381 | |
| 4382 | Type* acceptDecl2(Type* pType, tokenid_t& declName, |
| 4383 | bool nameAllowed, bool nameRequired, Arena& arena) { |
| 4384 | int ptrCounter = 0; |
| 4385 | while (accept('*')) { |
| 4386 | ptrCounter++; |
| 4387 | } |
| 4388 | pType = acceptDecl3(pType, declName, nameAllowed, nameRequired, arena); |
| 4389 | while (ptrCounter-- > 0) { |
| 4390 | pType = createType(TY_POINTER, pType, NULL, arena); |
| 4391 | } |
| 4392 | return pType; |
| 4393 | } |
| 4394 | |
| 4395 | Type* acceptDecl3(Type* pType, tokenid_t& declName, |
| 4396 | bool nameAllowed, bool nameRequired, Arena& arena) { |
| 4397 | // direct-dcl : |
| 4398 | // name |
| 4399 | // (dcl) |
| 4400 | // direct-dcl() |
| 4401 | // direct-dcl[] |
| 4402 | Type* pNewHead = NULL; |
| 4403 | if (accept('(')) { |
| 4404 | pNewHead = acceptDecl2(pNewHead, declName, nameAllowed, |
| 4405 | nameRequired, arena); |
| 4406 | skip(')'); |
| 4407 | } else if ((declName = acceptSymbol()) != 0) { |
| 4408 | if (nameAllowed == false && declName) { |
| 4409 | error("Symbol %s not allowed here", nameof(declName)); |
| 4410 | } else if (nameRequired && ! declName) { |
| 4411 | String temp; |
Jack Palevich | 37c54bd | 2009-07-14 18:35:36 -0700 | [diff] [blame] | 4412 | decodeToken(temp, tok, true); |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 4413 | error("Expected symbol. Got %s", temp.getUnwrapped()); |
| 4414 | } |
| 4415 | } |
| 4416 | while (accept('(')) { |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4417 | // Function declaration |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 4418 | Type* pTail = acceptArgs(nameAllowed, arena); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4419 | pType = createType(TY_FUNC, pType, pTail, arena); |
| 4420 | skip(')'); |
| 4421 | } |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 4422 | |
| 4423 | if (pNewHead) { |
| 4424 | Type* pA = pNewHead; |
| 4425 | while (pA->pHead) { |
| 4426 | pA = pA->pHead; |
| 4427 | } |
| 4428 | pA->pHead = pType; |
| 4429 | pType = pNewHead; |
| 4430 | } |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4431 | return pType; |
| 4432 | } |
| 4433 | |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 4434 | Type* acceptArgs(bool nameAllowed, Arena& arena) { |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4435 | Type* pHead = NULL; |
| 4436 | Type* pTail = NULL; |
| 4437 | for(;;) { |
| 4438 | Type* pBaseArg = acceptPrimitiveType(arena); |
| 4439 | if (pBaseArg) { |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 4440 | Type* pArg = acceptDeclaration(pBaseArg, nameAllowed, false, |
| 4441 | arena); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4442 | if (pArg) { |
| 4443 | Type* pParam = createType(TY_PARAM, pArg, NULL, arena); |
| 4444 | if (!pHead) { |
| 4445 | pHead = pParam; |
| 4446 | pTail = pParam; |
| 4447 | } else { |
| 4448 | pTail->pTail = pParam; |
| 4449 | pTail = pParam; |
| 4450 | } |
| 4451 | } |
| 4452 | } |
| 4453 | if (! accept(',')) { |
| 4454 | break; |
| 4455 | } |
| 4456 | } |
| 4457 | return pHead; |
| 4458 | } |
| 4459 | |
| 4460 | Type* expectPrimitiveType(Arena& arena) { |
| 4461 | Type* pType = acceptPrimitiveType(arena); |
| 4462 | if (!pType) { |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 4463 | String buf; |
Jack Palevich | 37c54bd | 2009-07-14 18:35:36 -0700 | [diff] [blame] | 4464 | decodeToken(buf, tok, true); |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 4465 | error("Expected a type, got %s", buf.getUnwrapped()); |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 4466 | } |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4467 | return pType; |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 4468 | } |
| 4469 | |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4470 | void addGlobalSymbol(Type* pDecl) { |
| 4471 | tokenid_t t = pDecl->id; |
| 4472 | VariableInfo* pVI = VI(t); |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 4473 | if(pVI && pVI->pAddress) { |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4474 | reportDuplicate(t); |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 4475 | } |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4476 | mGlobals.add(pDecl); |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 4477 | } |
| 4478 | |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4479 | void reportDuplicate(tokenid_t t) { |
| 4480 | error("Duplicate definition of %s", nameof(t)); |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 4481 | } |
| 4482 | |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4483 | void addLocalSymbol(Type* pDecl) { |
| 4484 | tokenid_t t = pDecl->id; |
| 4485 | if (mLocals.isDefinedAtCurrentLevel(t)) { |
| 4486 | reportDuplicate(t); |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 4487 | } |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4488 | mLocals.add(pDecl); |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 4489 | } |
| 4490 | |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 4491 | void localDeclarations(Type* pBaseType) { |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 4492 | intptr_t a; |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 4493 | |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 4494 | while (pBaseType) { |
Jack Palevich | 22e3e8e | 2009-06-12 13:12:55 -0700 | [diff] [blame] | 4495 | while (tok != ';' && tok != EOF) { |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4496 | Type* pDecl = expectDeclaration(pBaseType, mLocalArena); |
| 4497 | if (!pDecl) { |
| 4498 | break; |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 4499 | } |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4500 | int variableAddress = 0; |
| 4501 | addLocalSymbol(pDecl); |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 4502 | size_t alignment = pGen->alignmentOf(pDecl); |
| 4503 | loc = (loc + alignment - 1) & ~ (alignment-1); |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 4504 | loc = loc + pGen->sizeOf(pDecl); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4505 | variableAddress = -loc; |
| 4506 | VI(pDecl->id)->pAddress = (void*) variableAddress; |
| 4507 | if (accept('=')) { |
Jack Palevich | d7461a7 | 2009-06-12 14:26:58 -0700 | [diff] [blame] | 4508 | /* assignment */ |
Jack Palevich | d7461a7 | 2009-06-12 14:26:58 -0700 | [diff] [blame] | 4509 | expr(); |
Jack Palevich | 9cbd226 | 2009-07-08 16:48:41 -0700 | [diff] [blame] | 4510 | pGen->storeR0(variableAddress, pDecl); |
Jack Palevich | d7461a7 | 2009-06-12 14:26:58 -0700 | [diff] [blame] | 4511 | } |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 4512 | if (tok == ',') |
| 4513 | next(); |
| 4514 | } |
| 4515 | skip(';'); |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 4516 | pBaseType = acceptPrimitiveType(mLocalArena); |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 4517 | } |
| 4518 | } |
| 4519 | |
Jack Palevich | f1728be | 2009-06-12 13:53:51 -0700 | [diff] [blame] | 4520 | bool checkSymbol() { |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 4521 | return checkSymbol(tok); |
Jack Palevich | a1804dd | 2009-06-12 14:40:04 -0700 | [diff] [blame] | 4522 | } |
| 4523 | |
Jack Palevich | 37c54bd | 2009-07-14 18:35:36 -0700 | [diff] [blame] | 4524 | void decodeToken(String& buffer, tokenid_t token, bool quote) { |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 4525 | if (token == EOF ) { |
| 4526 | buffer.printf("EOF"); |
| 4527 | } else if (token == TOK_NUM) { |
| 4528 | buffer.printf("numeric constant"); |
| 4529 | } else if (token >= 0 && token < 256) { |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4530 | if (token < 32) { |
| 4531 | buffer.printf("'\\x%02x'", token); |
| 4532 | } else { |
| 4533 | buffer.printf("'%c'", token); |
| 4534 | } |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 4535 | } else { |
Jack Palevich | 37c54bd | 2009-07-14 18:35:36 -0700 | [diff] [blame] | 4536 | if (quote) { |
| 4537 | if (token >= TOK_KEYWORD && token < TOK_SYMBOL) { |
| 4538 | buffer.printf("keyword \"%s\"", nameof(token)); |
| 4539 | } else { |
| 4540 | buffer.printf("symbol \"%s\"", nameof(token)); |
| 4541 | } |
| 4542 | } else { |
| 4543 | buffer.printf("%s", nameof(token)); |
| 4544 | } |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 4545 | } |
| 4546 | } |
| 4547 | |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 4548 | bool checkSymbol(tokenid_t token) { |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 4549 | bool result = token >= TOK_SYMBOL; |
Jack Palevich | f1728be | 2009-06-12 13:53:51 -0700 | [diff] [blame] | 4550 | if (!result) { |
| 4551 | String temp; |
Jack Palevich | 37c54bd | 2009-07-14 18:35:36 -0700 | [diff] [blame] | 4552 | decodeToken(temp, token, true); |
Jack Palevich | f1728be | 2009-06-12 13:53:51 -0700 | [diff] [blame] | 4553 | error("Expected symbol. Got %s", temp.getUnwrapped()); |
| 4554 | } |
| 4555 | return result; |
| 4556 | } |
| 4557 | |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4558 | tokenid_t acceptSymbol() { |
| 4559 | tokenid_t result = 0; |
| 4560 | if (tok >= TOK_SYMBOL) { |
| 4561 | result = tok; |
| 4562 | next(); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4563 | } |
| 4564 | return result; |
| 4565 | } |
| 4566 | |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 4567 | void globalDeclarations() { |
| 4568 | while (tok != EOF) { |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4569 | Type* pBaseType = expectPrimitiveType(mGlobalArena); |
| 4570 | if (!pBaseType) { |
Jack Palevich | f1728be | 2009-06-12 13:53:51 -0700 | [diff] [blame] | 4571 | break; |
| 4572 | } |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4573 | Type* pDecl = expectDeclaration(pBaseType, mGlobalArena); |
| 4574 | if (!pDecl) { |
| 4575 | break; |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 4576 | } |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4577 | if (! isDefined(pDecl->id)) { |
| 4578 | addGlobalSymbol(pDecl); |
| 4579 | } |
| 4580 | VariableInfo* name = VI(pDecl->id); |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 4581 | if (name && name->pAddress) { |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4582 | error("Already defined global %s", nameof(pDecl->id)); |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 4583 | } |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4584 | if (pDecl->tag < TY_FUNC) { |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 4585 | // it's a variable declaration |
| 4586 | for(;;) { |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4587 | if (name && !name->pAddress) { |
Jack Palevich | 9cbd226 | 2009-07-08 16:48:41 -0700 | [diff] [blame] | 4588 | name->pAddress = (int*) allocGlobalSpace( |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 4589 | pGen->alignmentOf(name->pType), |
Jack Palevich | 9cbd226 | 2009-07-08 16:48:41 -0700 | [diff] [blame] | 4590 | pGen->sizeOf(name->pType)); |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 4591 | } |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4592 | if (accept('=')) { |
Jack Palevich | d7461a7 | 2009-06-12 14:26:58 -0700 | [diff] [blame] | 4593 | if (tok == TOK_NUM) { |
| 4594 | if (name) { |
| 4595 | * (int*) name->pAddress = tokc; |
| 4596 | } |
| 4597 | next(); |
| 4598 | } else { |
| 4599 | error("Expected an integer constant"); |
| 4600 | } |
| 4601 | } |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4602 | if (!accept(',')) { |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 4603 | break; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4604 | } |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4605 | pDecl = expectDeclaration(pBaseType, mGlobalArena); |
| 4606 | if (!pDecl) { |
| 4607 | break; |
| 4608 | } |
| 4609 | if (! isDefined(pDecl->id)) { |
| 4610 | addGlobalSymbol(pDecl); |
| 4611 | } |
| 4612 | name = VI(pDecl->id); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4613 | } |
| 4614 | skip(';'); |
| 4615 | } else { |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4616 | // Function declaration |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 4617 | if (accept(';')) { |
| 4618 | // forward declaration. |
Jack Palevich | d1f57e6 | 2009-07-15 18:23:22 -0700 | [diff] [blame] | 4619 | } else if (tok != '{') { |
| 4620 | error("expected '{'"); |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 4621 | } else { |
| 4622 | if (name) { |
| 4623 | /* patch forward references (XXX: does not work for function |
| 4624 | pointers) */ |
| 4625 | pGen->gsym((int) name->pForward); |
| 4626 | /* put function address */ |
| 4627 | name->pAddress = (void*) codeBuf.getPC(); |
| 4628 | } |
| 4629 | // Calculate stack offsets for parameters |
| 4630 | mLocals.pushLevel(); |
| 4631 | intptr_t a = 8; |
| 4632 | int argCount = 0; |
| 4633 | for (Type* pP = pDecl->pTail; pP; pP = pP->pTail) { |
| 4634 | Type* pArg = pP->pHead; |
| 4635 | addLocalSymbol(pArg); |
| 4636 | /* read param name and compute offset */ |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 4637 | size_t alignment = pGen->alignmentOf(pArg); |
| 4638 | a = (a + alignment - 1) & ~ (alignment-1); |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 4639 | VI(pArg->id)->pAddress = (void*) a; |
Jack Palevich | 9cbd226 | 2009-07-08 16:48:41 -0700 | [diff] [blame] | 4640 | a = a + pGen->stackSizeOf(pArg); |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 4641 | argCount++; |
| 4642 | } |
| 4643 | rsym = loc = 0; |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 4644 | pReturnType = pDecl->pHead; |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 4645 | a = pGen->functionEntry(pDecl); |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 4646 | block(0, true); |
| 4647 | pGen->gsym(rsym); |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 4648 | pGen->functionExit(pDecl, a, loc); |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 4649 | mLocals.popLevel(); |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 4650 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4651 | } |
| 4652 | } |
| 4653 | } |
| 4654 | |
Jack Palevich | 9cbd226 | 2009-07-08 16:48:41 -0700 | [diff] [blame] | 4655 | char* allocGlobalSpace(size_t alignment, size_t bytes) { |
| 4656 | size_t base = (((size_t) glo) + alignment - 1) & ~(alignment-1); |
| 4657 | size_t end = base + bytes; |
Jack Palevich | a39749f | 2009-07-08 20:40:31 -0700 | [diff] [blame] | 4658 | if ((end - (size_t) pGlobalBase) > (size_t) ALLOC_SIZE) { |
Jack Palevich | f1f39cc | 2009-05-29 18:03:15 -0700 | [diff] [blame] | 4659 | error("Global space exhausted"); |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 4660 | return NULL; |
Jack Palevich | f1f39cc | 2009-05-29 18:03:15 -0700 | [diff] [blame] | 4661 | } |
Jack Palevich | 9cbd226 | 2009-07-08 16:48:41 -0700 | [diff] [blame] | 4662 | char* result = (char*) base; |
| 4663 | glo = (char*) end; |
Jack Palevich | f1f39cc | 2009-05-29 18:03:15 -0700 | [diff] [blame] | 4664 | return result; |
| 4665 | } |
| 4666 | |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4667 | void cleanup() { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4668 | if (pGlobalBase != 0) { |
Jack Palevich | f1f39cc | 2009-05-29 18:03:15 -0700 | [diff] [blame] | 4669 | free(pGlobalBase); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4670 | pGlobalBase = 0; |
| 4671 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4672 | if (pGen) { |
| 4673 | delete pGen; |
| 4674 | pGen = 0; |
| 4675 | } |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 4676 | if (file) { |
| 4677 | delete file; |
| 4678 | file = 0; |
| 4679 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4680 | } |
| 4681 | |
Jack Palevich | 8c246a9 | 2009-07-14 21:14:10 -0700 | [diff] [blame] | 4682 | // One-time initialization, when class is constructed. |
| 4683 | void init() { |
| 4684 | mpSymbolLookupFn = 0; |
| 4685 | mpSymbolLookupContext = 0; |
| 4686 | } |
| 4687 | |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4688 | void clear() { |
| 4689 | tok = 0; |
| 4690 | tokc = 0; |
| 4691 | tokl = 0; |
| 4692 | ch = 0; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4693 | rsym = 0; |
| 4694 | loc = 0; |
| 4695 | glo = 0; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4696 | dptr = 0; |
| 4697 | dch = 0; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4698 | file = 0; |
| 4699 | pGlobalBase = 0; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4700 | pGen = 0; |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 4701 | mPragmaStringCount = 0; |
Jack Palevich | ce105a9 | 2009-07-16 14:30:33 -0700 | [diff] [blame] | 4702 | mCompileResult = 0; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4703 | } |
Jack Palevich | e27bf3e | 2009-05-10 14:09:03 -0700 | [diff] [blame] | 4704 | |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 4705 | void setArchitecture(const char* architecture) { |
| 4706 | delete pGen; |
| 4707 | pGen = 0; |
| 4708 | |
| 4709 | if (architecture != NULL) { |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 4710 | #ifdef PROVIDE_ARM_CODEGEN |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 4711 | if (! pGen && strcmp(architecture, "arm") == 0) { |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 4712 | pGen = new ARMCodeGenerator(); |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 4713 | } |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 4714 | #endif |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 4715 | #ifdef PROVIDE_X86_CODEGEN |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 4716 | if (! pGen && strcmp(architecture, "x86") == 0) { |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 4717 | pGen = new X86CodeGenerator(); |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 4718 | } |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 4719 | #endif |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 4720 | if (!pGen ) { |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 4721 | error("Unknown architecture %s\n", architecture); |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 4722 | } |
| 4723 | } |
| 4724 | |
| 4725 | if (pGen == NULL) { |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 4726 | #if defined(DEFAULT_ARM_CODEGEN) |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 4727 | pGen = new ARMCodeGenerator(); |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 4728 | #elif defined(DEFAULT_X86_CODEGEN) |
| 4729 | pGen = new X86CodeGenerator(); |
| 4730 | #endif |
| 4731 | } |
| 4732 | if (pGen == NULL) { |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 4733 | error("No code generator defined."); |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 4734 | } else { |
| 4735 | pGen->setErrorSink(this); |
Jack Palevich | a8f427f | 2009-07-13 18:40:08 -0700 | [diff] [blame] | 4736 | pGen->setTypes(mkpInt); |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 4737 | } |
| 4738 | } |
| 4739 | |
Jack Palevich | 77ae76e | 2009-05-10 19:59:24 -0700 | [diff] [blame] | 4740 | public: |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 4741 | struct args { |
| 4742 | args() { |
| 4743 | architecture = 0; |
| 4744 | } |
| 4745 | const char* architecture; |
| 4746 | }; |
| 4747 | |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 4748 | Compiler() { |
Jack Palevich | 8c246a9 | 2009-07-14 21:14:10 -0700 | [diff] [blame] | 4749 | init(); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4750 | clear(); |
Jack Palevich | e27bf3e | 2009-05-10 14:09:03 -0700 | [diff] [blame] | 4751 | } |
Jack Palevich | bbf8ab5 | 2009-05-11 11:54:30 -0700 | [diff] [blame] | 4752 | |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 4753 | ~Compiler() { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4754 | cleanup(); |
| 4755 | } |
| 4756 | |
Jack Palevich | 8c246a9 | 2009-07-14 21:14:10 -0700 | [diff] [blame] | 4757 | void registerSymbolCallback(ACCSymbolLookupFn pFn, ACCvoid* pContext) { |
| 4758 | mpSymbolLookupFn = pFn; |
| 4759 | mpSymbolLookupContext = pContext; |
| 4760 | } |
| 4761 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 4762 | int compile(const char* text, size_t textLength) { |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 4763 | int result; |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 4764 | |
Jack Palevich | a8f427f | 2009-07-13 18:40:08 -0700 | [diff] [blame] | 4765 | createPrimitiveTypes(); |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 4766 | cleanup(); |
| 4767 | clear(); |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 4768 | mTokenTable.setArena(&mGlobalArena); |
| 4769 | mGlobals.setArena(&mGlobalArena); |
| 4770 | mGlobals.setTokenTable(&mTokenTable); |
| 4771 | mLocals.setArena(&mLocalArena); |
| 4772 | mLocals.setTokenTable(&mTokenTable); |
| 4773 | |
| 4774 | internKeywords(); |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 4775 | codeBuf.init(ALLOC_SIZE); |
| 4776 | setArchitecture(NULL); |
| 4777 | if (!pGen) { |
| 4778 | return -1; |
| 4779 | } |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 4780 | #ifdef PROVIDE_TRACE_CODEGEN |
| 4781 | pGen = new TraceCodeGenerator(pGen); |
| 4782 | #endif |
| 4783 | pGen->setErrorSink(this); |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 4784 | pGen->init(&codeBuf); |
| 4785 | file = new TextInputStream(text, textLength); |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 4786 | pGlobalBase = (char*) calloc(1, ALLOC_SIZE); |
| 4787 | glo = pGlobalBase; |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 4788 | inp(); |
| 4789 | next(); |
| 4790 | globalDeclarations(); |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 4791 | checkForUndefinedForwardReferences(); |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 4792 | result = pGen->finishCompile(); |
| 4793 | if (result == 0) { |
| 4794 | if (mErrorBuf.len()) { |
| 4795 | result = -2; |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 4796 | } |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 4797 | } |
Jack Palevich | ce105a9 | 2009-07-16 14:30:33 -0700 | [diff] [blame] | 4798 | mCompileResult = result; |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 4799 | return result; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4800 | } |
| 4801 | |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4802 | void createPrimitiveTypes() { |
| 4803 | mkpInt = createType(TY_INT, NULL, NULL, mGlobalArena); |
| 4804 | mkpChar = createType(TY_CHAR, NULL, NULL, mGlobalArena); |
| 4805 | mkpVoid = createType(TY_VOID, NULL, NULL, mGlobalArena); |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 4806 | mkpFloat = createType(TY_FLOAT, NULL, NULL, mGlobalArena); |
| 4807 | mkpDouble = createType(TY_DOUBLE, NULL, NULL, mGlobalArena); |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 4808 | mkpIntFn = createType(TY_FUNC, mkpInt, NULL, mGlobalArena); |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 4809 | mkpIntPtr = createPtrType(mkpInt, mGlobalArena); |
| 4810 | mkpCharPtr = createPtrType(mkpChar, mGlobalArena); |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 4811 | mkpFloatPtr = createPtrType(mkpFloat, mGlobalArena); |
| 4812 | mkpDoublePtr = createPtrType(mkpDouble, mGlobalArena); |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 4813 | mkpPtrIntFn = createPtrType(mkpIntFn, mGlobalArena); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4814 | } |
| 4815 | |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 4816 | void checkForUndefinedForwardReferences() { |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 4817 | mGlobals.forEach(static_ufrcFn, this); |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 4818 | } |
| 4819 | |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 4820 | static bool static_ufrcFn(VariableInfo* value, void* context) { |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 4821 | Compiler* pCompiler = (Compiler*) context; |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 4822 | return pCompiler->undefinedForwardReferenceCheck(value); |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 4823 | } |
| 4824 | |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 4825 | bool undefinedForwardReferenceCheck(VariableInfo* value) { |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 4826 | if (!value->pAddress && value->pForward) { |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 4827 | error("Undefined forward reference: %s", |
| 4828 | mTokenTable[value->tok].pText); |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 4829 | } |
| 4830 | return true; |
| 4831 | } |
| 4832 | |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4833 | int dump(FILE* out) { |
| 4834 | fwrite(codeBuf.getBase(), 1, codeBuf.getSize(), out); |
| 4835 | return 0; |
| 4836 | } |
Jack Palevich | 77ae76e | 2009-05-10 19:59:24 -0700 | [diff] [blame] | 4837 | |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 4838 | int disassemble(FILE* out) { |
| 4839 | return pGen->disassemble(out); |
| 4840 | } |
| 4841 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 4842 | /* Look through the symbol table to find a symbol. |
| 4843 | * If found, return its value. |
| 4844 | */ |
| 4845 | void* lookup(const char* name) { |
Jack Palevich | ce105a9 | 2009-07-16 14:30:33 -0700 | [diff] [blame] | 4846 | if (mCompileResult == 0) { |
| 4847 | tokenid_t tok = mTokenTable.intern(name, strlen(name)); |
| 4848 | VariableInfo* pVariableInfo = VI(tok); |
| 4849 | if (pVariableInfo) { |
| 4850 | return pVariableInfo->pAddress; |
| 4851 | } |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 4852 | } |
| 4853 | return NULL; |
| 4854 | } |
| 4855 | |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 4856 | void getPragmas(ACCsizei* actualStringCount, |
| 4857 | ACCsizei maxStringCount, ACCchar** strings) { |
| 4858 | int stringCount = mPragmaStringCount; |
| 4859 | if (actualStringCount) { |
| 4860 | *actualStringCount = stringCount; |
| 4861 | } |
| 4862 | if (stringCount > maxStringCount) { |
| 4863 | stringCount = maxStringCount; |
| 4864 | } |
| 4865 | if (strings) { |
| 4866 | char* pPragmas = mPragmas.getUnwrapped(); |
| 4867 | while (stringCount-- > 0) { |
| 4868 | *strings++ = pPragmas; |
| 4869 | pPragmas += strlen(pPragmas) + 1; |
| 4870 | } |
| 4871 | } |
| 4872 | } |
| 4873 | |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 4874 | char* getErrorMessage() { |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 4875 | return mErrorBuf.getUnwrapped(); |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 4876 | } |
| 4877 | |
Jack Palevich | 77ae76e | 2009-05-10 19:59:24 -0700 | [diff] [blame] | 4878 | }; |
| 4879 | |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 4880 | const char* Compiler::operatorChars = |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 4881 | "++--*@/@%@+@-@<<>><=>=<@>@==!=&&||&@^@|@~@!@"; |
| 4882 | |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 4883 | const char Compiler::operatorLevel[] = |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 4884 | {11, 11, 1, 1, 1, 2, 2, 3, 3, 4, 4, 4, 4, |
| 4885 | 5, 5, /* ==, != */ |
| 4886 | 9, 10, /* &&, || */ |
| 4887 | 6, 7, 8, /* & ^ | */ |
| 4888 | 2, 2 /* ~ ! */ |
| 4889 | }; |
| 4890 | |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 4891 | #ifdef PROVIDE_ARM_CODEGEN |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 4892 | FILE* Compiler::ARMCodeGenerator::disasmOut; |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 4893 | #endif |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 4894 | |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 4895 | #ifdef PROVIDE_X86_CODEGEN |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 4896 | const int Compiler::X86CodeGenerator::operatorHelper[] = { |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 4897 | 0x1, // ++ |
| 4898 | 0xff, // -- |
| 4899 | 0xc1af0f, // * |
| 4900 | 0xf9f79991, // / |
| 4901 | 0xf9f79991, // % (With manual assist to swap results) |
| 4902 | 0xc801, // + |
| 4903 | 0xd8f7c829, // - |
| 4904 | 0xe0d391, // << |
| 4905 | 0xf8d391, // >> |
| 4906 | 0xe, // <= |
| 4907 | 0xd, // >= |
| 4908 | 0xc, // < |
| 4909 | 0xf, // > |
| 4910 | 0x4, // == |
| 4911 | 0x5, // != |
| 4912 | 0x0, // && |
| 4913 | 0x1, // || |
| 4914 | 0xc821, // & |
| 4915 | 0xc831, // ^ |
| 4916 | 0xc809, // | |
| 4917 | 0xd0f7, // ~ |
| 4918 | 0x4 // ! |
| 4919 | }; |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 4920 | #endif |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 4921 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 4922 | struct ACCscript { |
| 4923 | ACCscript() { |
| 4924 | text = 0; |
| 4925 | textLength = 0; |
| 4926 | accError = ACC_NO_ERROR; |
| 4927 | } |
Jack Palevich | bbf8ab5 | 2009-05-11 11:54:30 -0700 | [diff] [blame] | 4928 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 4929 | ~ACCscript() { |
| 4930 | delete text; |
| 4931 | } |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 4932 | |
Jack Palevich | 8c246a9 | 2009-07-14 21:14:10 -0700 | [diff] [blame] | 4933 | void registerSymbolCallback(ACCSymbolLookupFn pFn, ACCvoid* pContext) { |
| 4934 | compiler.registerSymbolCallback(pFn, pContext); |
| 4935 | } |
| 4936 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 4937 | void setError(ACCenum error) { |
| 4938 | if (accError == ACC_NO_ERROR && error != ACC_NO_ERROR) { |
| 4939 | accError = error; |
Jack Palevich | bbf8ab5 | 2009-05-11 11:54:30 -0700 | [diff] [blame] | 4940 | } |
| 4941 | } |
| 4942 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 4943 | ACCenum getError() { |
| 4944 | ACCenum result = accError; |
| 4945 | accError = ACC_NO_ERROR; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 4946 | return result; |
Jack Palevich | bbf8ab5 | 2009-05-11 11:54:30 -0700 | [diff] [blame] | 4947 | } |
| 4948 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 4949 | Compiler compiler; |
| 4950 | char* text; |
| 4951 | int textLength; |
| 4952 | ACCenum accError; |
| 4953 | }; |
| 4954 | |
| 4955 | |
| 4956 | extern "C" |
| 4957 | ACCscript* accCreateScript() { |
| 4958 | return new ACCscript(); |
Jack Palevich | bbf8ab5 | 2009-05-11 11:54:30 -0700 | [diff] [blame] | 4959 | } |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 4960 | |
| 4961 | extern "C" |
| 4962 | ACCenum accGetError( ACCscript* script ) { |
| 4963 | return script->getError(); |
| 4964 | } |
| 4965 | |
| 4966 | extern "C" |
| 4967 | void accDeleteScript(ACCscript* script) { |
| 4968 | delete script; |
| 4969 | } |
| 4970 | |
| 4971 | extern "C" |
Jack Palevich | 8c246a9 | 2009-07-14 21:14:10 -0700 | [diff] [blame] | 4972 | void accRegisterSymbolCallback(ACCscript* script, ACCSymbolLookupFn pFn, |
| 4973 | ACCvoid* pContext) { |
| 4974 | script->registerSymbolCallback(pFn, pContext); |
| 4975 | } |
| 4976 | |
| 4977 | extern "C" |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 4978 | void accScriptSource(ACCscript* script, |
| 4979 | ACCsizei count, |
| 4980 | const ACCchar ** string, |
| 4981 | const ACCint * length) { |
| 4982 | int totalLength = 0; |
| 4983 | for(int i = 0; i < count; i++) { |
| 4984 | int len = -1; |
| 4985 | const ACCchar* s = string[i]; |
| 4986 | if (length) { |
| 4987 | len = length[i]; |
| 4988 | } |
| 4989 | if (len < 0) { |
| 4990 | len = strlen(s); |
| 4991 | } |
| 4992 | totalLength += len; |
| 4993 | } |
| 4994 | delete script->text; |
| 4995 | char* text = new char[totalLength + 1]; |
| 4996 | script->text = text; |
| 4997 | script->textLength = totalLength; |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 4998 | char* dest = text; |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 4999 | for(int i = 0; i < count; i++) { |
| 5000 | int len = -1; |
| 5001 | const ACCchar* s = string[i]; |
| 5002 | if (length) { |
| 5003 | len = length[i]; |
| 5004 | } |
| 5005 | if (len < 0) { |
| 5006 | len = strlen(s); |
| 5007 | } |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 5008 | memcpy(dest, s, len); |
| 5009 | dest += len; |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 5010 | } |
| 5011 | text[totalLength] = '\0'; |
| 5012 | } |
| 5013 | |
| 5014 | extern "C" |
| 5015 | void accCompileScript(ACCscript* script) { |
| 5016 | int result = script->compiler.compile(script->text, script->textLength); |
| 5017 | if (result) { |
| 5018 | script->setError(ACC_INVALID_OPERATION); |
| 5019 | } |
| 5020 | } |
| 5021 | |
| 5022 | extern "C" |
| 5023 | void accGetScriptiv(ACCscript* script, |
| 5024 | ACCenum pname, |
| 5025 | ACCint * params) { |
| 5026 | switch (pname) { |
| 5027 | case ACC_INFO_LOG_LENGTH: |
| 5028 | *params = 0; |
| 5029 | break; |
| 5030 | } |
| 5031 | } |
| 5032 | |
| 5033 | extern "C" |
| 5034 | void accGetScriptInfoLog(ACCscript* script, |
| 5035 | ACCsizei maxLength, |
| 5036 | ACCsizei * length, |
| 5037 | ACCchar * infoLog) { |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 5038 | char* message = script->compiler.getErrorMessage(); |
| 5039 | int messageLength = strlen(message) + 1; |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 5040 | if (length) { |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 5041 | *length = messageLength; |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 5042 | } |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 5043 | if (infoLog && maxLength > 0) { |
| 5044 | int trimmedLength = maxLength < messageLength ? |
| 5045 | maxLength : messageLength; |
| 5046 | memcpy(infoLog, message, trimmedLength); |
| 5047 | infoLog[trimmedLength] = 0; |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 5048 | } |
| 5049 | } |
| 5050 | |
| 5051 | extern "C" |
| 5052 | void accGetScriptLabel(ACCscript* script, const ACCchar * name, |
| 5053 | ACCvoid ** address) { |
| 5054 | void* value = script->compiler.lookup(name); |
| 5055 | if (value) { |
| 5056 | *address = value; |
| 5057 | } else { |
| 5058 | script->setError(ACC_INVALID_VALUE); |
| 5059 | } |
| 5060 | } |
| 5061 | |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 5062 | extern "C" |
| 5063 | void accGetPragmas(ACCscript* script, ACCsizei* actualStringCount, |
| 5064 | ACCsizei maxStringCount, ACCchar** strings){ |
| 5065 | script->compiler.getPragmas(actualStringCount, maxStringCount, strings); |
| 5066 | } |
| 5067 | |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 5068 | extern "C" |
| 5069 | void accDisassemble(ACCscript* script) { |
| 5070 | script->compiler.disassemble(stderr); |
| 5071 | } |
| 5072 | |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 5073 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 5074 | } // namespace acc |
| 5075 | |