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