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