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 | 61de31f | 2009-09-08 11:06:40 -0700 | [diff] [blame] | 16 | #include <limits.h> |
Jack Palevich | e27bf3e | 2009-05-10 14:09:03 -0700 | [diff] [blame] | 17 | #include <stdarg.h> |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 18 | #include <stdint.h> |
Jack Palevich | ae54f1f | 2009-05-08 14:54:15 -0700 | [diff] [blame] | 19 | #include <stdio.h> |
Jack Palevich | f6b5a53 | 2009-05-10 19:16:42 -0700 | [diff] [blame] | 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
Jack Palevich | 61de31f | 2009-09-08 11:06:40 -0700 | [diff] [blame] | 22 | #include <unistd.h> |
| 23 | |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 24 | #include <cutils/hashmap.h> |
Jack Palevich | ae54f1f | 2009-05-08 14:54:15 -0700 | [diff] [blame] | 25 | |
Jack Palevich | 8dc662e | 2009-06-09 22:53:47 +0000 | [diff] [blame] | 26 | #if defined(__i386__) |
| 27 | #include <sys/mman.h> |
| 28 | #endif |
| 29 | |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 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 | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 42 | #if (defined(__VFP_FP__) && !defined(__SOFTFP__)) |
| 43 | #define ARM_USE_VFP |
| 44 | #endif |
| 45 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 46 | #include <acc/acc.h> |
| 47 | |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 48 | #define LOG_API(...) do {} while(0) |
| 49 | // #define LOG_API(...) fprintf (stderr, __VA_ARGS__) |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 50 | |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 51 | #define LOG_STACK(...) do {} while(0) |
| 52 | // #define LOG_STACK(...) fprintf (stderr, __VA_ARGS__) |
| 53 | |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 54 | // #define PROVIDE_TRACE_CODEGEN |
| 55 | |
Jack Palevich | 61de31f | 2009-09-08 11:06:40 -0700 | [diff] [blame] | 56 | // Uncomment to save input to a text file in DEBUG_DUMP_PATTERN |
| 57 | // #define DEBUG_SAVE_INPUT_TO_FILE |
| 58 | |
Jack Palevich | 9116bc4 | 2009-09-08 11:46:42 -0700 | [diff] [blame] | 59 | #ifdef DEBUG_SAVE_INPUT_TO_FILE |
Jack Palevich | 61de31f | 2009-09-08 11:06:40 -0700 | [diff] [blame] | 60 | #ifdef ARM_USE_VFP |
Jack Palevich | 9116bc4 | 2009-09-08 11:46:42 -0700 | [diff] [blame] | 61 | #define DEBUG_DUMP_PATTERN "/data/misc/acc_dump/%d.c" |
Jack Palevich | 61de31f | 2009-09-08 11:06:40 -0700 | [diff] [blame] | 62 | #else |
| 63 | #define DEBUG_DUMP_PATTERN "/tmp/acc_dump/%d.c" |
| 64 | #endif |
Jack Palevich | 9116bc4 | 2009-09-08 11:46:42 -0700 | [diff] [blame] | 65 | #endif |
Jack Palevich | 61de31f | 2009-09-08 11:06:40 -0700 | [diff] [blame] | 66 | |
Jack Palevich | 7f5b1a2 | 2009-08-17 16:54:56 -0700 | [diff] [blame] | 67 | #define assert(b) assertImpl(b, __LINE__) |
| 68 | |
Jack Palevich | bbf8ab5 | 2009-05-11 11:54:30 -0700 | [diff] [blame] | 69 | namespace acc { |
| 70 | |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 71 | // Subset of STL vector. |
| 72 | template<class E> class Vector { |
| 73 | public: |
| 74 | Vector() { |
| 75 | mpBase = 0; |
| 76 | mUsed = 0; |
| 77 | mSize = 0; |
| 78 | } |
| 79 | |
| 80 | ~Vector() { |
| 81 | if (mpBase) { |
| 82 | for(size_t i = 0; i < mUsed; i++) { |
| 83 | mpBase[mUsed].~E(); |
| 84 | } |
| 85 | free(mpBase); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | inline E& operator[](size_t i) { |
| 90 | return mpBase[i]; |
| 91 | } |
| 92 | |
| 93 | inline E& front() { |
| 94 | return mpBase[0]; |
| 95 | } |
| 96 | |
| 97 | inline E& back() { |
| 98 | return mpBase[mUsed - 1]; |
| 99 | } |
| 100 | |
| 101 | void pop_back() { |
| 102 | mUsed -= 1; |
| 103 | mpBase[mUsed].~E(); |
| 104 | } |
| 105 | |
| 106 | void push_back(const E& item) { |
| 107 | * ensure(1) = item; |
| 108 | } |
| 109 | |
| 110 | size_t size() { |
| 111 | return mUsed; |
| 112 | } |
| 113 | |
| 114 | private: |
| 115 | E* ensure(int n) { |
| 116 | size_t newUsed = mUsed + n; |
| 117 | if (newUsed > mSize) { |
| 118 | size_t newSize = mSize * 2 + 10; |
| 119 | if (newSize < newUsed) { |
| 120 | newSize = newUsed; |
| 121 | } |
| 122 | mpBase = (E*) realloc(mpBase, sizeof(E) * newSize); |
| 123 | mSize = newSize; |
| 124 | } |
| 125 | E* result = mpBase + mUsed; |
| 126 | mUsed = newUsed; |
| 127 | return result; |
| 128 | } |
| 129 | |
| 130 | E* mpBase; |
| 131 | size_t mUsed; |
| 132 | size_t mSize; |
| 133 | }; |
| 134 | |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 135 | class ErrorSink { |
| 136 | public: |
| 137 | void error(const char *fmt, ...) { |
| 138 | va_list ap; |
| 139 | va_start(ap, fmt); |
| 140 | verror(fmt, ap); |
| 141 | va_end(ap); |
| 142 | } |
| 143 | |
Marco Nelissen | eea5ae9 | 2009-07-08 16:59:18 -0700 | [diff] [blame] | 144 | virtual ~ErrorSink() {} |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 145 | virtual void verror(const char* fmt, va_list ap) = 0; |
| 146 | }; |
| 147 | |
| 148 | class Compiler : public ErrorSink { |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 149 | typedef int tokenid_t; |
| 150 | enum TypeTag { |
Jack Palevich | c9b8ffc | 2009-08-03 14:42:57 -0700 | [diff] [blame] | 151 | TY_INT, // 0 |
| 152 | TY_CHAR, // 1 |
| 153 | TY_SHORT, // 2 |
| 154 | TY_VOID, // 3 |
| 155 | TY_FLOAT, // 4 |
| 156 | TY_DOUBLE, // 5 |
Jack Palevich | b615450 | 2009-08-04 14:56:09 -0700 | [diff] [blame] | 157 | TY_POINTER, // 6 |
| 158 | TY_ARRAY, // 7 |
| 159 | TY_STRUCT, // 8 |
| 160 | TY_FUNC, // 9 |
| 161 | TY_PARAM // 10 |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 162 | }; |
| 163 | |
| 164 | struct Type { |
| 165 | TypeTag tag; |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 166 | tokenid_t id; // For function arguments, global vars, local vars, struct elements |
| 167 | tokenid_t structTag; // For structs the name of the struct |
| 168 | int length; // length of array, offset of struct element. -1 means struct is forward defined |
| 169 | int alignment; // for structs only |
| 170 | Type* pHead; // For a struct this is the prototype struct. |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 171 | Type* pTail; |
| 172 | }; |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 173 | |
Jack Palevich | ba929a4 | 2009-07-17 10:20:32 -0700 | [diff] [blame] | 174 | enum ExpressionType { |
| 175 | ET_RVALUE, |
| 176 | ET_LVALUE |
| 177 | }; |
| 178 | |
| 179 | struct ExpressionValue { |
| 180 | ExpressionValue() { |
| 181 | et = ET_RVALUE; |
| 182 | pType = NULL; |
| 183 | } |
| 184 | ExpressionType et; |
| 185 | Type* pType; |
| 186 | }; |
| 187 | |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 188 | class CodeBuf { |
Jack Palevich | 653f42d | 2009-05-28 17:15:32 -0700 | [diff] [blame] | 189 | char* ind; // Output code pointer |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 190 | char* pProgramBase; |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 191 | ErrorSink* mErrorSink; |
| 192 | int mSize; |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 193 | bool mOverflowed; |
Jack Palevich | f0cbc92 | 2009-05-08 16:35:13 -0700 | [diff] [blame] | 194 | |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 195 | void release() { |
| 196 | if (pProgramBase != 0) { |
| 197 | free(pProgramBase); |
| 198 | pProgramBase = 0; |
Jack Palevich | ae54f1f | 2009-05-08 14:54:15 -0700 | [diff] [blame] | 199 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 202 | bool check(int n) { |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 203 | int newSize = ind - pProgramBase + n; |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 204 | bool overflow = newSize > mSize; |
| 205 | if (overflow && !mOverflowed) { |
| 206 | mOverflowed = true; |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 207 | if (mErrorSink) { |
| 208 | mErrorSink->error("Code too large: %d bytes", newSize); |
| 209 | } |
| 210 | } |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 211 | return overflow; |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 214 | public: |
| 215 | CodeBuf() { |
| 216 | pProgramBase = 0; |
| 217 | ind = 0; |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 218 | mErrorSink = 0; |
| 219 | mSize = 0; |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 220 | mOverflowed = false; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | ~CodeBuf() { |
| 224 | release(); |
| 225 | } |
| 226 | |
| 227 | void init(int size) { |
| 228 | release(); |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 229 | mSize = size; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 230 | pProgramBase = (char*) calloc(1, size); |
| 231 | ind = pProgramBase; |
| 232 | } |
| 233 | |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 234 | void setErrorSink(ErrorSink* pErrorSink) { |
| 235 | mErrorSink = pErrorSink; |
| 236 | } |
| 237 | |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 238 | int o4(int n) { |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 239 | if(check(4)) { |
| 240 | return 0; |
| 241 | } |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 242 | intptr_t result = (intptr_t) ind; |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 243 | * (int*) ind = n; |
| 244 | ind += 4; |
| 245 | return result; |
| 246 | } |
| 247 | |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 248 | /* |
| 249 | * Output a byte. Handles all values, 0..ff. |
| 250 | */ |
| 251 | void ob(int n) { |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 252 | if(check(1)) { |
| 253 | return; |
| 254 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 255 | *ind++ = n; |
| 256 | } |
| 257 | |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 258 | inline void* getBase() { |
| 259 | return (void*) pProgramBase; |
| 260 | } |
| 261 | |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 262 | intptr_t getSize() { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 263 | return ind - pProgramBase; |
| 264 | } |
| 265 | |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 266 | intptr_t getPC() { |
| 267 | return (intptr_t) ind; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 268 | } |
| 269 | }; |
| 270 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 271 | /** |
| 272 | * A code generator creates an in-memory program, generating the code on |
| 273 | * the fly. There is one code generator implementation for each supported |
| 274 | * architecture. |
| 275 | * |
| 276 | * The code generator implements the following abstract machine: |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 277 | * R0 - the accumulator. |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 278 | * FP - a frame pointer for accessing function arguments and local |
| 279 | * variables. |
| 280 | * SP - a stack pointer for storing intermediate results while evaluating |
| 281 | * expressions. The stack pointer grows downwards. |
| 282 | * |
| 283 | * The function calling convention is that all arguments are placed on the |
| 284 | * stack such that the first argument has the lowest address. |
| 285 | * After the call, the result is in R0. The caller is responsible for |
| 286 | * removing the arguments from the stack. |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 287 | * The R0 register is not saved across function calls. The |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 288 | * FP and SP registers are saved. |
| 289 | */ |
| 290 | |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 291 | class CodeGenerator { |
| 292 | public: |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 293 | CodeGenerator() { |
| 294 | mErrorSink = 0; |
| 295 | pCodeBuf = 0; |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 296 | pushType(); |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 297 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 298 | virtual ~CodeGenerator() {} |
| 299 | |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 300 | virtual void init(CodeBuf* pCodeBuf) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 301 | this->pCodeBuf = pCodeBuf; |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 302 | pCodeBuf->setErrorSink(mErrorSink); |
| 303 | } |
| 304 | |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 305 | virtual void setErrorSink(ErrorSink* pErrorSink) { |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 306 | mErrorSink = pErrorSink; |
| 307 | if (pCodeBuf) { |
| 308 | pCodeBuf->setErrorSink(mErrorSink); |
| 309 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Jack Palevich | 58c30ee | 2009-07-17 16:35:23 -0700 | [diff] [blame] | 312 | /* Give the code generator some utility types so it can |
| 313 | * use its own types as needed for the results of some |
| 314 | * operations like gcmp. |
| 315 | */ |
| 316 | |
Jack Palevich | a8f427f | 2009-07-13 18:40:08 -0700 | [diff] [blame] | 317 | void setTypes(Type* pInt) { |
| 318 | mkpInt = pInt; |
| 319 | } |
| 320 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 321 | /* Emit a function prolog. |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 322 | * pDecl is the function declaration, which gives the arguments. |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 323 | * Save the old value of the FP. |
| 324 | * Set the new value of the FP. |
| 325 | * Convert from the native platform calling convention to |
| 326 | * our stack-based calling convention. This may require |
| 327 | * pushing arguments from registers to the stack. |
| 328 | * Allocate "N" bytes of stack space. N isn't known yet, so |
| 329 | * just emit the instructions for adjusting the stack, and return |
| 330 | * the address to patch up. The patching will be done in |
| 331 | * functionExit(). |
| 332 | * returns address to patch with local variable size. |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 333 | */ |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 334 | virtual int functionEntry(Type* pDecl) = 0; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 335 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 336 | /* Emit a function epilog. |
| 337 | * Restore the old SP and FP register values. |
| 338 | * Return to the calling function. |
| 339 | * argCount - the number of arguments to the function. |
| 340 | * localVariableAddress - returned from functionEntry() |
| 341 | * localVariableSize - the size in bytes of the local variables. |
| 342 | */ |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 343 | virtual void functionExit(Type* pDecl, int localVariableAddress, |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 344 | int localVariableSize) = 0; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 345 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 346 | /* load immediate value to R0 */ |
Jack Palevich | 58c30ee | 2009-07-17 16:35:23 -0700 | [diff] [blame] | 347 | virtual void li(int i) = 0; |
Jack Palevich | 1aeb87b | 2009-07-06 18:33:20 -0700 | [diff] [blame] | 348 | |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 349 | /* Load floating point value from global address. */ |
| 350 | virtual void loadFloat(int address, Type* pType) = 0; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 351 | |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 352 | /* Add the struct offset in bytes to R0, change the type to pType */ |
| 353 | virtual void addStructOffsetR0(int offset, Type* pType) = 0; |
| 354 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 355 | /* Jump to a target, and return the address of the word that |
| 356 | * holds the target data, in case it needs to be fixed up later. |
| 357 | */ |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 358 | virtual int gjmp(int t) = 0; |
| 359 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 360 | /* Test R0 and jump to a target if the test succeeds. |
| 361 | * l = 0: je, l == 1: jne |
| 362 | * Return the address of the word that holds the targed data, in |
| 363 | * case it needs to be fixed up later. |
| 364 | */ |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 365 | virtual int gtst(bool l, int t) = 0; |
| 366 | |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 367 | /* Compare TOS against R0, and store the boolean result in R0. |
| 368 | * Pops TOS. |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 369 | * op specifies the comparison. |
| 370 | */ |
Jack Palevich | 58c30ee | 2009-07-17 16:35:23 -0700 | [diff] [blame] | 371 | virtual void gcmp(int op) = 0; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 372 | |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 373 | /* Perform the arithmetic op specified by op. TOS is the |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 374 | * left argument, R0 is the right argument. |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 375 | * Pops TOS. |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 376 | */ |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 377 | virtual void genOp(int op) = 0; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 378 | |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 379 | /* Compare 0 against R0, and store the boolean result in R0. |
| 380 | * op specifies the comparison. |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 381 | */ |
Jack Palevich | 58c30ee | 2009-07-17 16:35:23 -0700 | [diff] [blame] | 382 | virtual void gUnaryCmp(int op) = 0; |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 383 | |
| 384 | /* Perform the arithmetic op specified by op. 0 is the |
| 385 | * left argument, R0 is the right argument. |
| 386 | */ |
| 387 | virtual void genUnaryOp(int op) = 0; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 388 | |
Jack Palevich | ddf7c9c | 2009-07-29 10:28:18 -0700 | [diff] [blame] | 389 | /* Push R0 onto the stack. (Also known as "dup" for duplicate.) |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 390 | */ |
| 391 | virtual void pushR0() = 0; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 392 | |
Jack Palevich | ddf7c9c | 2009-07-29 10:28:18 -0700 | [diff] [blame] | 393 | /* Turn R0, TOS into R0 TOS R0 */ |
| 394 | |
| 395 | virtual void over() = 0; |
| 396 | |
| 397 | /* Pop R0 from the stack. (Also known as "drop") |
Jack Palevich | 58c30ee | 2009-07-17 16:35:23 -0700 | [diff] [blame] | 398 | */ |
| 399 | virtual void popR0() = 0; |
| 400 | |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 401 | /* Store R0 to the address stored in TOS. |
| 402 | * The TOS is popped. |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 403 | */ |
Jack Palevich | 58c30ee | 2009-07-17 16:35:23 -0700 | [diff] [blame] | 404 | virtual void storeR0ToTOS() = 0; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 405 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 406 | /* Load R0 from the address stored in R0. |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 407 | */ |
Jack Palevich | 58c30ee | 2009-07-17 16:35:23 -0700 | [diff] [blame] | 408 | virtual void loadR0FromR0() = 0; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 409 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 410 | /* Load the absolute address of a variable to R0. |
| 411 | * If ea <= LOCAL, then this is a local variable, or an |
| 412 | * argument, addressed relative to FP. |
| 413 | * else it is an absolute global address. |
Jack Palevich | 9f51a26 | 2009-07-29 16:22:26 -0700 | [diff] [blame] | 414 | * |
Jack Palevich | b5e3331 | 2009-07-30 19:06:34 -0700 | [diff] [blame] | 415 | * et is ET_RVALUE for things like string constants, ET_LVALUE for |
| 416 | * variables. |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 417 | */ |
Jack Palevich | b5e3331 | 2009-07-30 19:06:34 -0700 | [diff] [blame] | 418 | virtual void leaR0(int ea, Type* pPointerType, ExpressionType et) = 0; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 419 | |
Jack Palevich | 9f51a26 | 2009-07-29 16:22:26 -0700 | [diff] [blame] | 420 | /* Load the pc-relative address of a forward-referenced variable to R0. |
| 421 | * Return the address of the 4-byte constant so that it can be filled |
| 422 | * in later. |
| 423 | */ |
| 424 | virtual int leaForward(int ea, Type* pPointerType) = 0; |
| 425 | |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 426 | /** |
| 427 | * Convert R0 to the given type. |
| 428 | */ |
Jack Palevich | b615450 | 2009-08-04 14:56:09 -0700 | [diff] [blame] | 429 | |
| 430 | void convertR0(Type* pType) { |
| 431 | convertR0Imp(pType, false); |
| 432 | } |
| 433 | |
| 434 | void castR0(Type* pType) { |
| 435 | convertR0Imp(pType, true); |
| 436 | } |
| 437 | |
| 438 | virtual void convertR0Imp(Type* pType, bool isCast) = 0; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 439 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 440 | /* Emit code to adjust the stack for a function call. Return the |
| 441 | * label for the address of the instruction that adjusts the |
| 442 | * stack size. This will be passed as argument "a" to |
| 443 | * endFunctionCallArguments. |
| 444 | */ |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 445 | virtual int beginFunctionCallArguments() = 0; |
| 446 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 447 | /* Emit code to store R0 to the stack at byte offset l. |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 448 | * Returns stack size of object (typically 4 or 8 bytes) |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 449 | */ |
Jack Palevich | 8148c5b | 2009-07-16 18:24:47 -0700 | [diff] [blame] | 450 | virtual size_t storeR0ToArg(int l, Type* pArgType) = 0; |
Jack Palevich | 7810bc9 | 2009-05-15 14:31:47 -0700 | [diff] [blame] | 451 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 452 | /* Patch the function call preamble. |
| 453 | * a is the address returned from beginFunctionCallArguments |
| 454 | * l is the number of bytes the arguments took on the stack. |
| 455 | * Typically you would also emit code to convert the argument |
| 456 | * list into whatever the native function calling convention is. |
| 457 | * On ARM for example you would pop the first 5 arguments into |
| 458 | * R0..R4 |
| 459 | */ |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 460 | virtual void endFunctionCallArguments(Type* pDecl, int a, int l) = 0; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 461 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 462 | /* Emit a call to an unknown function. The argument "symbol" needs to |
| 463 | * be stored in the location where the address should go. It forms |
| 464 | * a chain. The address will be patched later. |
| 465 | * Return the address of the word that has to be patched. |
| 466 | */ |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 467 | virtual int callForward(int symbol, Type* pFunc) = 0; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 468 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 469 | /* Call a function pointer. L is the number of bytes the arguments |
| 470 | * take on the stack. The address of the function is stored at |
| 471 | * location SP + l. |
| 472 | */ |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 473 | virtual void callIndirect(int l, Type* pFunc) = 0; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 474 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 475 | /* Adjust SP after returning from a function call. l is the |
| 476 | * number of bytes of arguments stored on the stack. isIndirect |
| 477 | * is true if this was an indirect call. (In which case the |
| 478 | * address of the function is stored at location SP + l.) |
| 479 | */ |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 480 | virtual void adjustStackAfterCall(Type* pDecl, int l, bool isIndirect) = 0; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 481 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 482 | /* Generate a symbol at the current PC. t is the head of a |
| 483 | * linked list of addresses to patch. |
| 484 | */ |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 485 | virtual void gsym(int t) = 0; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 486 | |
Jack Palevich | 9f51a26 | 2009-07-29 16:22:26 -0700 | [diff] [blame] | 487 | /* Resolve a forward reference function at the current PC. |
| 488 | * t is the head of a |
| 489 | * linked list of addresses to patch. |
| 490 | * (Like gsym, but using absolute address, not PC relative address.) |
| 491 | */ |
| 492 | virtual void resolveForward(int t) = 0; |
| 493 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 494 | /* |
| 495 | * Do any cleanup work required at the end of a compile. |
| 496 | * For example, an instruction cache might need to be |
| 497 | * invalidated. |
| 498 | * Return non-zero if there is an error. |
| 499 | */ |
| 500 | virtual int finishCompile() = 0; |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 501 | |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 502 | /** |
| 503 | * Adjust relative branches by this amount. |
| 504 | */ |
| 505 | virtual int jumpOffset() = 0; |
| 506 | |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 507 | /** |
Jack Palevich | 9cbd226 | 2009-07-08 16:48:41 -0700 | [diff] [blame] | 508 | * Memory alignment (in bytes) for this type of data |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 509 | */ |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 510 | virtual size_t alignmentOf(Type* type) = 0; |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 511 | |
| 512 | /** |
| 513 | * Array element alignment (in bytes) for this type of data. |
| 514 | */ |
| 515 | virtual size_t sizeOf(Type* type) = 0; |
| 516 | |
Jack Palevich | ddf7c9c | 2009-07-29 10:28:18 -0700 | [diff] [blame] | 517 | virtual Type* getR0Type() { |
Jack Palevich | ba929a4 | 2009-07-17 10:20:32 -0700 | [diff] [blame] | 518 | return mExpressionStack.back().pType; |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 519 | } |
| 520 | |
Jack Palevich | ddf7c9c | 2009-07-29 10:28:18 -0700 | [diff] [blame] | 521 | virtual ExpressionType getR0ExpressionType() { |
Jack Palevich | 7fcdf1c | 2009-07-23 18:56:20 -0700 | [diff] [blame] | 522 | return mExpressionStack.back().et; |
| 523 | } |
| 524 | |
Jack Palevich | ddf7c9c | 2009-07-29 10:28:18 -0700 | [diff] [blame] | 525 | virtual void setR0ExpressionType(ExpressionType et) { |
Jack Palevich | 7fcdf1c | 2009-07-23 18:56:20 -0700 | [diff] [blame] | 526 | mExpressionStack.back().et = et; |
| 527 | } |
| 528 | |
Jack Palevich | ddf7c9c | 2009-07-29 10:28:18 -0700 | [diff] [blame] | 529 | virtual size_t getExpressionStackDepth() { |
| 530 | return mExpressionStack.size(); |
| 531 | } |
| 532 | |
Jack Palevich | b5e3331 | 2009-07-30 19:06:34 -0700 | [diff] [blame] | 533 | virtual void forceR0RVal() { |
| 534 | if (getR0ExpressionType() == ET_LVALUE) { |
| 535 | loadR0FromR0(); |
| 536 | } |
| 537 | } |
| 538 | |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 539 | protected: |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 540 | /* |
| 541 | * Output a byte. Handles all values, 0..ff. |
| 542 | */ |
| 543 | void ob(int n) { |
| 544 | pCodeBuf->ob(n); |
| 545 | } |
| 546 | |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 547 | intptr_t o4(int data) { |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 548 | return pCodeBuf->o4(data); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 549 | } |
| 550 | |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 551 | intptr_t getBase() { |
| 552 | return (intptr_t) pCodeBuf->getBase(); |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 553 | } |
| 554 | |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 555 | intptr_t getPC() { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 556 | return pCodeBuf->getPC(); |
| 557 | } |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 558 | |
| 559 | intptr_t getSize() { |
| 560 | return pCodeBuf->getSize(); |
| 561 | } |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 562 | |
| 563 | void error(const char* fmt,...) { |
| 564 | va_list ap; |
| 565 | va_start(ap, fmt); |
| 566 | mErrorSink->verror(fmt, ap); |
| 567 | va_end(ap); |
| 568 | } |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 569 | |
Jack Palevich | 7f5b1a2 | 2009-08-17 16:54:56 -0700 | [diff] [blame] | 570 | void assertImpl(bool test, int line) { |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 571 | if (!test) { |
Jack Palevich | 7f5b1a2 | 2009-08-17 16:54:56 -0700 | [diff] [blame] | 572 | error("code generator assertion failed at line %s:%d.", __FILE__, line); |
| 573 | LOGD("code generator assertion failed at line %s:%d.", __FILE__, line); |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 574 | * (char*) 0 = 0; |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 575 | } |
| 576 | } |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 577 | |
| 578 | void setR0Type(Type* pType) { |
Jack Palevich | ddf7c9c | 2009-07-29 10:28:18 -0700 | [diff] [blame] | 579 | assert(pType != NULL); |
Jack Palevich | ba929a4 | 2009-07-17 10:20:32 -0700 | [diff] [blame] | 580 | mExpressionStack.back().pType = pType; |
Jack Palevich | b5e3331 | 2009-07-30 19:06:34 -0700 | [diff] [blame] | 581 | mExpressionStack.back().et = ET_RVALUE; |
| 582 | } |
| 583 | |
| 584 | void setR0Type(Type* pType, ExpressionType et) { |
| 585 | assert(pType != NULL); |
| 586 | mExpressionStack.back().pType = pType; |
| 587 | mExpressionStack.back().et = et; |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 588 | } |
| 589 | |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 590 | Type* getTOSType() { |
Jack Palevich | ba929a4 | 2009-07-17 10:20:32 -0700 | [diff] [blame] | 591 | return mExpressionStack[mExpressionStack.size()-2].pType; |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | void pushType() { |
Jack Palevich | ba929a4 | 2009-07-17 10:20:32 -0700 | [diff] [blame] | 595 | if (mExpressionStack.size()) { |
| 596 | mExpressionStack.push_back(mExpressionStack.back()); |
| 597 | } else { |
| 598 | mExpressionStack.push_back(ExpressionValue()); |
| 599 | } |
| 600 | |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 601 | } |
| 602 | |
Jack Palevich | ddf7c9c | 2009-07-29 10:28:18 -0700 | [diff] [blame] | 603 | void overType() { |
| 604 | size_t size = mExpressionStack.size(); |
| 605 | if (size >= 2) { |
| 606 | mExpressionStack.push_back(mExpressionStack.back()); |
| 607 | mExpressionStack[size-1] = mExpressionStack[size-2]; |
| 608 | mExpressionStack[size-2] = mExpressionStack[size]; |
| 609 | } |
| 610 | } |
| 611 | |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 612 | void popType() { |
| 613 | mExpressionStack.pop_back(); |
| 614 | } |
| 615 | |
| 616 | bool bitsSame(Type* pA, Type* pB) { |
| 617 | return collapseType(pA->tag) == collapseType(pB->tag); |
| 618 | } |
| 619 | |
| 620 | TypeTag collapseType(TypeTag tag) { |
| 621 | static const TypeTag collapsedTag[] = { |
Jack Palevich | c9b8ffc | 2009-08-03 14:42:57 -0700 | [diff] [blame] | 622 | TY_INT, |
| 623 | TY_INT, |
| 624 | TY_INT, |
| 625 | TY_VOID, |
| 626 | TY_FLOAT, |
| 627 | TY_DOUBLE, |
| 628 | TY_INT, |
| 629 | TY_INT, |
| 630 | TY_VOID, |
| 631 | TY_VOID, |
| 632 | TY_VOID |
| 633 | }; |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 634 | return collapsedTag[tag]; |
| 635 | } |
| 636 | |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 637 | TypeTag collapseTypeR0() { |
| 638 | return collapseType(getR0Type()->tag); |
| 639 | } |
| 640 | |
Jack Palevich | b615450 | 2009-08-04 14:56:09 -0700 | [diff] [blame] | 641 | static bool isFloatType(Type* pType) { |
Jack Palevich | 128ad2d | 2009-07-08 14:51:31 -0700 | [diff] [blame] | 642 | return isFloatTag(pType->tag); |
| 643 | } |
| 644 | |
Jack Palevich | b615450 | 2009-08-04 14:56:09 -0700 | [diff] [blame] | 645 | static bool isFloatTag(TypeTag tag) { |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 646 | return tag == TY_FLOAT || tag == TY_DOUBLE; |
| 647 | } |
| 648 | |
Jack Palevich | b615450 | 2009-08-04 14:56:09 -0700 | [diff] [blame] | 649 | static bool isPointerType(Type* pType) { |
| 650 | return isPointerTag(pType->tag); |
| 651 | } |
| 652 | |
| 653 | static bool isPointerTag(TypeTag tag) { |
| 654 | return tag == TY_POINTER || tag == TY_ARRAY; |
| 655 | } |
| 656 | |
| 657 | Type* getPointerArithmeticResultType(Type* a, Type* b) { |
| 658 | TypeTag aTag = a->tag; |
| 659 | TypeTag bTag = b->tag; |
| 660 | if (aTag == TY_POINTER) { |
| 661 | return a; |
| 662 | } |
| 663 | if (bTag == TY_POINTER) { |
| 664 | return b; |
| 665 | } |
| 666 | if (aTag == TY_ARRAY) { |
| 667 | return a->pTail; |
| 668 | } |
| 669 | if (bTag == TY_ARRAY) { |
| 670 | return b->pTail; |
| 671 | } |
| 672 | return NULL; |
| 673 | } |
Jack Palevich | a8f427f | 2009-07-13 18:40:08 -0700 | [diff] [blame] | 674 | Type* mkpInt; |
| 675 | |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 676 | private: |
Jack Palevich | ba929a4 | 2009-07-17 10:20:32 -0700 | [diff] [blame] | 677 | Vector<ExpressionValue> mExpressionStack; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 678 | CodeBuf* pCodeBuf; |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 679 | ErrorSink* mErrorSink; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 680 | }; |
| 681 | |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 682 | #ifdef PROVIDE_ARM_CODEGEN |
| 683 | |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 684 | class ARMCodeGenerator : public CodeGenerator { |
| 685 | public: |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 686 | ARMCodeGenerator() { |
| 687 | #ifdef ARM_USE_VFP |
Jack Palevich | d531557 | 2009-09-09 13:19:34 -0700 | [diff] [blame^] | 688 | // LOGD("Using ARM VFP hardware floating point."); |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 689 | #else |
Jack Palevich | d531557 | 2009-09-09 13:19:34 -0700 | [diff] [blame^] | 690 | // LOGD("Using ARM soft floating point."); |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 691 | #endif |
| 692 | } |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 693 | |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 694 | virtual ~ARMCodeGenerator() {} |
| 695 | |
| 696 | /* returns address to patch with local variable size |
| 697 | */ |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 698 | virtual int functionEntry(Type* pDecl) { |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 699 | mStackUse = 0; |
Jack Palevich | 69796b6 | 2009-05-14 15:42:26 -0700 | [diff] [blame] | 700 | // sp -> arg4 arg5 ... |
| 701 | // Push our register-based arguments back on the stack |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 702 | int regArgCount = calcRegArgCount(pDecl); |
| 703 | if (regArgCount > 0) { |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 704 | mStackUse += regArgCount * 4; |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 705 | o4(0xE92D0000 | ((1 << regArgCount) - 1)); // stmfd sp!, {} |
Jack Palevich | 69796b6 | 2009-05-14 15:42:26 -0700 | [diff] [blame] | 706 | } |
| 707 | // sp -> arg0 arg1 ... |
| 708 | o4(0xE92D4800); // stmfd sp!, {fp, lr} |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 709 | mStackUse += 2 * 4; |
Jack Palevich | 69796b6 | 2009-05-14 15:42:26 -0700 | [diff] [blame] | 710 | // sp, fp -> oldfp, retadr, arg0 arg1 .... |
| 711 | o4(0xE1A0B00D); // mov fp, sp |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 712 | LOG_STACK("functionEntry: %d\n", mStackUse); |
Jack Palevich | 69796b6 | 2009-05-14 15:42:26 -0700 | [diff] [blame] | 713 | return o4(0xE24DD000); // sub sp, sp, # <local variables> |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 714 | // We don't know how many local variables we are going to use, |
| 715 | // but we will round the allocation up to a multiple of |
| 716 | // STACK_ALIGNMENT, so it won't affect the stack alignment. |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 717 | } |
| 718 | |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 719 | virtual void functionExit(Type* pDecl, int localVariableAddress, int localVariableSize) { |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 720 | // Round local variable size up to a multiple of stack alignment |
| 721 | localVariableSize = ((localVariableSize + STACK_ALIGNMENT - 1) / |
| 722 | STACK_ALIGNMENT) * STACK_ALIGNMENT; |
Jack Palevich | 69796b6 | 2009-05-14 15:42:26 -0700 | [diff] [blame] | 723 | // Patch local variable allocation code: |
| 724 | if (localVariableSize < 0 || localVariableSize > 255) { |
Jack Palevich | 8de461d | 2009-05-14 17:21:45 -0700 | [diff] [blame] | 725 | error("localVariables out of range: %d", localVariableSize); |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 726 | } |
Jack Palevich | 69796b6 | 2009-05-14 15:42:26 -0700 | [diff] [blame] | 727 | *(char*) (localVariableAddress) = localVariableSize; |
| 728 | |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 729 | #ifdef ARM_USE_VFP |
| 730 | { |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 731 | Type* pReturnType = pDecl->pHead; |
| 732 | switch(pReturnType->tag) { |
| 733 | case TY_FLOAT: |
| 734 | o4(0xEE170A90); // fmrs r0, s15 |
| 735 | break; |
| 736 | case TY_DOUBLE: |
| 737 | o4(0xEC510B17); // fmrrd r0, r1, d7 |
| 738 | break; |
| 739 | default: |
| 740 | break; |
| 741 | } |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 742 | } |
| 743 | #endif |
| 744 | |
Jack Palevich | 69796b6 | 2009-05-14 15:42:26 -0700 | [diff] [blame] | 745 | // sp -> locals .... fp -> oldfp, retadr, arg0, arg1, ... |
| 746 | o4(0xE1A0E00B); // mov lr, fp |
| 747 | o4(0xE59BB000); // ldr fp, [fp] |
| 748 | o4(0xE28ED004); // add sp, lr, #4 |
| 749 | // sp -> retadr, arg0, ... |
| 750 | o4(0xE8BD4000); // ldmfd sp!, {lr} |
| 751 | // sp -> arg0 .... |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 752 | |
| 753 | // We store the PC into the lr so we can adjust the sp before |
| 754 | // returning. We need to pull off the registers we pushed |
| 755 | // earlier. We don't need to actually store them anywhere, |
| 756 | // just adjust the stack. |
| 757 | int regArgCount = calcRegArgCount(pDecl); |
| 758 | if (regArgCount) { |
Jack Palevich | 69796b6 | 2009-05-14 15:42:26 -0700 | [diff] [blame] | 759 | o4(0xE28DD000 | (regArgCount << 2)); // add sp, sp, #argCount << 2 |
| 760 | } |
| 761 | o4(0xE12FFF1E); // bx lr |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | /* load immediate value */ |
Jack Palevich | 58c30ee | 2009-07-17 16:35:23 -0700 | [diff] [blame] | 765 | virtual void li(int t) { |
Jack Palevich | a8f427f | 2009-07-13 18:40:08 -0700 | [diff] [blame] | 766 | liReg(t, 0); |
Jack Palevich | 58c30ee | 2009-07-17 16:35:23 -0700 | [diff] [blame] | 767 | setR0Type(mkpInt); |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 768 | } |
| 769 | |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 770 | virtual void loadFloat(int address, Type* pType) { |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 771 | setR0Type(pType); |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 772 | // Global, absolute address |
| 773 | o4(0xE59F0000); // ldr r0, .L1 |
| 774 | o4(0xEA000000); // b .L99 |
| 775 | o4(address); // .L1: .word ea |
| 776 | // .L99: |
| 777 | |
| 778 | switch (pType->tag) { |
| 779 | case TY_FLOAT: |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 780 | #ifdef ARM_USE_VFP |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 781 | o4(0xEDD07A00); // flds s15, [r0] |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 782 | #else |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 783 | o4(0xE5900000); // ldr r0, [r0] |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 784 | #endif |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 785 | break; |
| 786 | case TY_DOUBLE: |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 787 | #ifdef ARM_USE_VFP |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 788 | o4(0xED907B00); // fldd d7, [r0] |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 789 | #else |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 790 | o4(0xE1C000D0); // ldrd r0, [r0] |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 791 | #endif |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 792 | break; |
| 793 | default: |
| 794 | assert(false); |
| 795 | break; |
| 796 | } |
Jack Palevich | 1aeb87b | 2009-07-06 18:33:20 -0700 | [diff] [blame] | 797 | } |
| 798 | |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 799 | |
| 800 | virtual void addStructOffsetR0(int offset, Type* pType) { |
| 801 | if (offset) { |
| 802 | size_t immediate = 0; |
| 803 | if (encode12BitImmediate(offset, &immediate)) { |
| 804 | o4(0xE2800000 | immediate); // add r0, r0, #offset |
| 805 | } else { |
| 806 | error("structure offset out of range: %d", offset); |
| 807 | } |
| 808 | } |
| 809 | setR0Type(pType, ET_LVALUE); |
| 810 | } |
| 811 | |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 812 | virtual int gjmp(int t) { |
Jack Palevich | 8de461d | 2009-05-14 17:21:45 -0700 | [diff] [blame] | 813 | return o4(0xEA000000 | encodeAddress(t)); // b .L33 |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | /* l = 0: je, l == 1: jne */ |
| 817 | virtual int gtst(bool l, int t) { |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 818 | Type* pR0Type = getR0Type(); |
| 819 | TypeTag tagR0 = pR0Type->tag; |
| 820 | switch(tagR0) { |
| 821 | case TY_FLOAT: |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 822 | #ifdef ARM_USE_VFP |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 823 | o4(0xEEF57A40); // fcmpzs s15 |
| 824 | o4(0xEEF1FA10); // fmstat |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 825 | #else |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 826 | callRuntime((void*) runtime_is_non_zero_f); |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 827 | o4(0xE3500000); // cmp r0,#0 |
| 828 | #endif |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 829 | break; |
| 830 | case TY_DOUBLE: |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 831 | #ifdef ARM_USE_VFP |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 832 | o4(0xEEB57B40); // fcmpzd d7 |
| 833 | o4(0xEEF1FA10); // fmstat |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 834 | #else |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 835 | callRuntime((void*) runtime_is_non_zero_d); |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 836 | o4(0xE3500000); // cmp r0,#0 |
| 837 | #endif |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 838 | break; |
| 839 | default: |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 840 | o4(0xE3500000); // cmp r0,#0 |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 841 | break; |
| 842 | } |
Jack Palevich | 8de461d | 2009-05-14 17:21:45 -0700 | [diff] [blame] | 843 | int branch = l ? 0x1A000000 : 0x0A000000; // bne : beq |
| 844 | return o4(branch | encodeAddress(t)); |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 845 | } |
| 846 | |
Jack Palevich | 58c30ee | 2009-07-17 16:35:23 -0700 | [diff] [blame] | 847 | virtual void gcmp(int op) { |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 848 | Type* pR0Type = getR0Type(); |
| 849 | Type* pTOSType = getTOSType(); |
| 850 | TypeTag tagR0 = collapseType(pR0Type->tag); |
| 851 | TypeTag tagTOS = collapseType(pTOSType->tag); |
| 852 | if (tagR0 == TY_INT && tagTOS == TY_INT) { |
Jack Palevich | 58c30ee | 2009-07-17 16:35:23 -0700 | [diff] [blame] | 853 | setupIntPtrArgs(); |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 854 | o4(0xE1510000); // cmp r1, r1 |
| 855 | switch(op) { |
| 856 | case OP_EQUALS: |
| 857 | o4(0x03A00001); // moveq r0,#1 |
| 858 | o4(0x13A00000); // movne r0,#0 |
| 859 | break; |
| 860 | case OP_NOT_EQUALS: |
| 861 | o4(0x03A00000); // moveq r0,#0 |
| 862 | o4(0x13A00001); // movne r0,#1 |
| 863 | break; |
| 864 | case OP_LESS_EQUAL: |
| 865 | o4(0xD3A00001); // movle r0,#1 |
| 866 | o4(0xC3A00000); // movgt r0,#0 |
| 867 | break; |
| 868 | case OP_GREATER: |
| 869 | o4(0xD3A00000); // movle r0,#0 |
| 870 | o4(0xC3A00001); // movgt r0,#1 |
| 871 | break; |
| 872 | case OP_GREATER_EQUAL: |
| 873 | o4(0xA3A00001); // movge r0,#1 |
| 874 | o4(0xB3A00000); // movlt r0,#0 |
| 875 | break; |
| 876 | case OP_LESS: |
| 877 | o4(0xA3A00000); // movge r0,#0 |
| 878 | o4(0xB3A00001); // movlt r0,#1 |
| 879 | break; |
| 880 | default: |
| 881 | error("Unknown comparison op %d", op); |
| 882 | break; |
| 883 | } |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 884 | } else if (tagR0 == TY_DOUBLE || tagTOS == TY_DOUBLE) { |
| 885 | setupDoubleArgs(); |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 886 | #ifdef ARM_USE_VFP |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 887 | o4(0xEEB46BC7); // fcmped d6, d7 |
| 888 | o4(0xEEF1FA10); // fmstat |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 889 | switch(op) { |
| 890 | case OP_EQUALS: |
| 891 | o4(0x03A00001); // moveq r0,#1 |
| 892 | o4(0x13A00000); // movne r0,#0 |
| 893 | break; |
| 894 | case OP_NOT_EQUALS: |
| 895 | o4(0x03A00000); // moveq r0,#0 |
| 896 | o4(0x13A00001); // movne r0,#1 |
| 897 | break; |
| 898 | case OP_LESS_EQUAL: |
| 899 | o4(0xD3A00001); // movle r0,#1 |
| 900 | o4(0xC3A00000); // movgt r0,#0 |
| 901 | break; |
| 902 | case OP_GREATER: |
| 903 | o4(0xD3A00000); // movle r0,#0 |
| 904 | o4(0xC3A00001); // movgt r0,#1 |
| 905 | break; |
| 906 | case OP_GREATER_EQUAL: |
| 907 | o4(0xA3A00001); // movge r0,#1 |
| 908 | o4(0xB3A00000); // movlt r0,#0 |
| 909 | break; |
| 910 | case OP_LESS: |
| 911 | o4(0xA3A00000); // movge r0,#0 |
| 912 | o4(0xB3A00001); // movlt r0,#1 |
| 913 | break; |
| 914 | default: |
| 915 | error("Unknown comparison op %d", op); |
| 916 | break; |
| 917 | } |
| 918 | #else |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 919 | switch(op) { |
| 920 | case OP_EQUALS: |
| 921 | callRuntime((void*) runtime_cmp_eq_dd); |
| 922 | break; |
| 923 | case OP_NOT_EQUALS: |
| 924 | callRuntime((void*) runtime_cmp_ne_dd); |
| 925 | break; |
| 926 | case OP_LESS_EQUAL: |
| 927 | callRuntime((void*) runtime_cmp_le_dd); |
| 928 | break; |
| 929 | case OP_GREATER: |
| 930 | callRuntime((void*) runtime_cmp_gt_dd); |
| 931 | break; |
| 932 | case OP_GREATER_EQUAL: |
| 933 | callRuntime((void*) runtime_cmp_ge_dd); |
| 934 | break; |
| 935 | case OP_LESS: |
| 936 | callRuntime((void*) runtime_cmp_lt_dd); |
| 937 | break; |
| 938 | default: |
| 939 | error("Unknown comparison op %d", op); |
| 940 | break; |
| 941 | } |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 942 | #endif |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 943 | } else { |
| 944 | setupFloatArgs(); |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 945 | #ifdef ARM_USE_VFP |
| 946 | o4(0xEEB47AE7); // fcmpes s14, s15 |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 947 | o4(0xEEF1FA10); // fmstat |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 948 | switch(op) { |
| 949 | case OP_EQUALS: |
| 950 | o4(0x03A00001); // moveq r0,#1 |
| 951 | o4(0x13A00000); // movne r0,#0 |
| 952 | break; |
| 953 | case OP_NOT_EQUALS: |
| 954 | o4(0x03A00000); // moveq r0,#0 |
| 955 | o4(0x13A00001); // movne r0,#1 |
| 956 | break; |
| 957 | case OP_LESS_EQUAL: |
| 958 | o4(0xD3A00001); // movle r0,#1 |
| 959 | o4(0xC3A00000); // movgt r0,#0 |
| 960 | break; |
| 961 | case OP_GREATER: |
| 962 | o4(0xD3A00000); // movle r0,#0 |
| 963 | o4(0xC3A00001); // movgt r0,#1 |
| 964 | break; |
| 965 | case OP_GREATER_EQUAL: |
| 966 | o4(0xA3A00001); // movge r0,#1 |
| 967 | o4(0xB3A00000); // movlt r0,#0 |
| 968 | break; |
| 969 | case OP_LESS: |
| 970 | o4(0xA3A00000); // movge r0,#0 |
| 971 | o4(0xB3A00001); // movlt r0,#1 |
| 972 | break; |
| 973 | default: |
| 974 | error("Unknown comparison op %d", op); |
| 975 | break; |
| 976 | } |
| 977 | #else |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 978 | switch(op) { |
| 979 | case OP_EQUALS: |
| 980 | callRuntime((void*) runtime_cmp_eq_ff); |
| 981 | break; |
| 982 | case OP_NOT_EQUALS: |
| 983 | callRuntime((void*) runtime_cmp_ne_ff); |
| 984 | break; |
| 985 | case OP_LESS_EQUAL: |
| 986 | callRuntime((void*) runtime_cmp_le_ff); |
| 987 | break; |
| 988 | case OP_GREATER: |
| 989 | callRuntime((void*) runtime_cmp_gt_ff); |
| 990 | break; |
| 991 | case OP_GREATER_EQUAL: |
| 992 | callRuntime((void*) runtime_cmp_ge_ff); |
| 993 | break; |
| 994 | case OP_LESS: |
| 995 | callRuntime((void*) runtime_cmp_lt_ff); |
| 996 | break; |
| 997 | default: |
| 998 | error("Unknown comparison op %d", op); |
| 999 | break; |
| 1000 | } |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1001 | #endif |
Jack Palevich | 8de461d | 2009-05-14 17:21:45 -0700 | [diff] [blame] | 1002 | } |
Jack Palevich | 58c30ee | 2009-07-17 16:35:23 -0700 | [diff] [blame] | 1003 | setR0Type(mkpInt); |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 1004 | } |
| 1005 | |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 1006 | virtual void genOp(int op) { |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1007 | Type* pR0Type = getR0Type(); |
| 1008 | Type* pTOSType = getTOSType(); |
Jack Palevich | a8f427f | 2009-07-13 18:40:08 -0700 | [diff] [blame] | 1009 | TypeTag tagR0 = pR0Type->tag; |
| 1010 | TypeTag tagTOS = pTOSType->tag; |
| 1011 | bool isFloatR0 = isFloatTag(tagR0); |
| 1012 | bool isFloatTOS = isFloatTag(tagTOS); |
| 1013 | if (!isFloatR0 && !isFloatTOS) { |
Jack Palevich | 58c30ee | 2009-07-17 16:35:23 -0700 | [diff] [blame] | 1014 | setupIntPtrArgs(); |
Jack Palevich | b615450 | 2009-08-04 14:56:09 -0700 | [diff] [blame] | 1015 | bool isPtrR0 = isPointerTag(tagR0); |
| 1016 | bool isPtrTOS = isPointerTag(tagTOS); |
Jack Palevich | a8f427f | 2009-07-13 18:40:08 -0700 | [diff] [blame] | 1017 | if (isPtrR0 || isPtrTOS) { |
| 1018 | if (isPtrR0 && isPtrTOS) { |
| 1019 | if (op != OP_MINUS) { |
| 1020 | error("Unsupported pointer-pointer operation %d.", op); |
| 1021 | } |
| 1022 | if (! typeEqual(pR0Type, pTOSType)) { |
| 1023 | error("Incompatible pointer types for subtraction."); |
| 1024 | } |
Jack Palevich | a8f427f | 2009-07-13 18:40:08 -0700 | [diff] [blame] | 1025 | o4(0xE0410000); // sub r0,r1,r0 |
Jack Palevich | a8f427f | 2009-07-13 18:40:08 -0700 | [diff] [blame] | 1026 | setR0Type(mkpInt); |
| 1027 | int size = sizeOf(pR0Type->pHead); |
| 1028 | if (size != 1) { |
| 1029 | pushR0(); |
Jack Palevich | 58c30ee | 2009-07-17 16:35:23 -0700 | [diff] [blame] | 1030 | li(size); |
Jack Palevich | a8f427f | 2009-07-13 18:40:08 -0700 | [diff] [blame] | 1031 | // TODO: Optimize for power-of-two. |
| 1032 | genOp(OP_DIV); |
| 1033 | } |
| 1034 | } else { |
| 1035 | if (! (op == OP_PLUS || (op == OP_MINUS && isPtrR0))) { |
| 1036 | error("Unsupported pointer-scalar operation %d", op); |
| 1037 | } |
Jack Palevich | b615450 | 2009-08-04 14:56:09 -0700 | [diff] [blame] | 1038 | Type* pPtrType = getPointerArithmeticResultType( |
| 1039 | pR0Type, pTOSType); |
Jack Palevich | a8f427f | 2009-07-13 18:40:08 -0700 | [diff] [blame] | 1040 | int size = sizeOf(pPtrType->pHead); |
| 1041 | if (size != 1) { |
| 1042 | // TODO: Optimize for power-of-two. |
| 1043 | liReg(size, 2); |
| 1044 | if (isPtrR0) { |
| 1045 | o4(0x0E0010192); // mul r1,r2,r1 |
| 1046 | } else { |
| 1047 | o4(0x0E0000092); // mul r0,r2,r0 |
| 1048 | } |
| 1049 | } |
| 1050 | switch(op) { |
| 1051 | case OP_PLUS: |
| 1052 | o4(0xE0810000); // add r0,r1,r0 |
| 1053 | break; |
| 1054 | case OP_MINUS: |
| 1055 | o4(0xE0410000); // sub r0,r1,r0 |
| 1056 | break; |
| 1057 | } |
Jack Palevich | a8f427f | 2009-07-13 18:40:08 -0700 | [diff] [blame] | 1058 | setR0Type(pPtrType); |
| 1059 | } |
| 1060 | } else { |
Jack Palevich | a8f427f | 2009-07-13 18:40:08 -0700 | [diff] [blame] | 1061 | switch(op) { |
| 1062 | case OP_MUL: |
| 1063 | o4(0x0E0000091); // mul r0,r1,r0 |
| 1064 | break; |
| 1065 | case OP_DIV: |
| 1066 | callRuntime((void*) runtime_DIV); |
| 1067 | break; |
| 1068 | case OP_MOD: |
| 1069 | callRuntime((void*) runtime_MOD); |
| 1070 | break; |
| 1071 | case OP_PLUS: |
| 1072 | o4(0xE0810000); // add r0,r1,r0 |
| 1073 | break; |
| 1074 | case OP_MINUS: |
| 1075 | o4(0xE0410000); // sub r0,r1,r0 |
| 1076 | break; |
| 1077 | case OP_SHIFT_LEFT: |
| 1078 | o4(0xE1A00011); // lsl r0,r1,r0 |
| 1079 | break; |
| 1080 | case OP_SHIFT_RIGHT: |
| 1081 | o4(0xE1A00051); // asr r0,r1,r0 |
| 1082 | break; |
| 1083 | case OP_BIT_AND: |
| 1084 | o4(0xE0010000); // and r0,r1,r0 |
| 1085 | break; |
| 1086 | case OP_BIT_XOR: |
| 1087 | o4(0xE0210000); // eor r0,r1,r0 |
| 1088 | break; |
| 1089 | case OP_BIT_OR: |
| 1090 | o4(0xE1810000); // orr r0,r1,r0 |
| 1091 | break; |
| 1092 | case OP_BIT_NOT: |
| 1093 | o4(0xE1E00000); // mvn r0, r0 |
| 1094 | break; |
| 1095 | default: |
| 1096 | error("Unimplemented op %d\n", op); |
| 1097 | break; |
| 1098 | } |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1099 | } |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1100 | } else { |
| 1101 | Type* pResultType = tagR0 > tagTOS ? pR0Type : pTOSType; |
| 1102 | if (pResultType->tag == TY_DOUBLE) { |
| 1103 | setupDoubleArgs(); |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1104 | |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1105 | switch(op) { |
| 1106 | case OP_MUL: |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1107 | #ifdef ARM_USE_VFP |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 1108 | o4(0xEE267B07); // fmuld d7, d6, d7 |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1109 | #else |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 1110 | callRuntime((void*) runtime_op_mul_dd); |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1111 | #endif |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1112 | break; |
| 1113 | case OP_DIV: |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1114 | #ifdef ARM_USE_VFP |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 1115 | o4(0xEE867B07); // fdivd d7, d6, d7 |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1116 | #else |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1117 | callRuntime((void*) runtime_op_div_dd); |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1118 | #endif |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1119 | break; |
| 1120 | case OP_PLUS: |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1121 | #ifdef ARM_USE_VFP |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 1122 | o4(0xEE367B07); // faddd d7, d6, d7 |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1123 | #else |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1124 | callRuntime((void*) runtime_op_add_dd); |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1125 | #endif |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1126 | break; |
| 1127 | case OP_MINUS: |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1128 | #ifdef ARM_USE_VFP |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 1129 | o4(0xEE367B47); // fsubd d7, d6, d7 |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1130 | #else |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1131 | callRuntime((void*) runtime_op_sub_dd); |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1132 | #endif |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1133 | break; |
| 1134 | default: |
| 1135 | error("Unsupported binary floating operation %d\n", op); |
| 1136 | break; |
| 1137 | } |
| 1138 | } else { |
| 1139 | setupFloatArgs(); |
| 1140 | switch(op) { |
| 1141 | case OP_MUL: |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1142 | #ifdef ARM_USE_VFP |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 1143 | o4(0xEE677A27); // fmuls s15, s14, s15 |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1144 | #else |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1145 | callRuntime((void*) runtime_op_mul_ff); |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1146 | #endif |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1147 | break; |
| 1148 | case OP_DIV: |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1149 | #ifdef ARM_USE_VFP |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 1150 | o4(0xEEC77A27); // fdivs s15, s14, s15 |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1151 | #else |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1152 | callRuntime((void*) runtime_op_div_ff); |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1153 | #endif |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1154 | break; |
| 1155 | case OP_PLUS: |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1156 | #ifdef ARM_USE_VFP |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 1157 | o4(0xEE777A27); // fadds s15, s14, s15 |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1158 | #else |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1159 | callRuntime((void*) runtime_op_add_ff); |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1160 | #endif |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1161 | break; |
| 1162 | case OP_MINUS: |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1163 | #ifdef ARM_USE_VFP |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 1164 | o4(0xEE777A67); // fsubs s15, s14, s15 |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1165 | #else |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1166 | callRuntime((void*) runtime_op_sub_ff); |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1167 | #endif |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1168 | break; |
| 1169 | default: |
| 1170 | error("Unsupported binary floating operation %d\n", op); |
| 1171 | break; |
| 1172 | } |
| 1173 | } |
| 1174 | setR0Type(pResultType); |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 1175 | } |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 1176 | } |
| 1177 | |
Jack Palevich | 58c30ee | 2009-07-17 16:35:23 -0700 | [diff] [blame] | 1178 | virtual void gUnaryCmp(int op) { |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1179 | if (op != OP_LOGICAL_NOT) { |
| 1180 | error("Unknown unary cmp %d", op); |
| 1181 | } else { |
| 1182 | Type* pR0Type = getR0Type(); |
| 1183 | TypeTag tag = collapseType(pR0Type->tag); |
| 1184 | switch(tag) { |
| 1185 | case TY_INT: |
| 1186 | o4(0xE3A01000); // mov r1, #0 |
Jack Palevich | a8f427f | 2009-07-13 18:40:08 -0700 | [diff] [blame] | 1187 | o4(0xE1510000); // cmp r1, r0 |
| 1188 | o4(0x03A00001); // moveq r0,#1 |
| 1189 | o4(0x13A00000); // movne r0,#0 |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1190 | break; |
| 1191 | case TY_FLOAT: |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1192 | #ifdef ARM_USE_VFP |
| 1193 | o4(0xEEF57A40); // fcmpzs s15 |
| 1194 | o4(0xEEF1FA10); // fmstat |
| 1195 | o4(0x03A00001); // moveq r0,#1 |
| 1196 | o4(0x13A00000); // movne r0,#0 |
| 1197 | #else |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 1198 | callRuntime((void*) runtime_is_zero_f); |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1199 | #endif |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1200 | break; |
| 1201 | case TY_DOUBLE: |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1202 | #ifdef ARM_USE_VFP |
| 1203 | o4(0xEEB57B40); // fcmpzd d7 |
| 1204 | o4(0xEEF1FA10); // fmstat |
| 1205 | o4(0x03A00001); // moveq r0,#1 |
| 1206 | o4(0x13A00000); // movne r0,#0 |
| 1207 | #else |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 1208 | callRuntime((void*) runtime_is_zero_d); |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1209 | #endif |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1210 | break; |
| 1211 | default: |
| 1212 | error("gUnaryCmp unsupported type"); |
| 1213 | break; |
| 1214 | } |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 1215 | } |
Jack Palevich | 58c30ee | 2009-07-17 16:35:23 -0700 | [diff] [blame] | 1216 | setR0Type(mkpInt); |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | virtual void genUnaryOp(int op) { |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1220 | Type* pR0Type = getR0Type(); |
| 1221 | TypeTag tag = collapseType(pR0Type->tag); |
| 1222 | switch(tag) { |
| 1223 | case TY_INT: |
| 1224 | switch(op) { |
| 1225 | case OP_MINUS: |
| 1226 | o4(0xE3A01000); // mov r1, #0 |
| 1227 | o4(0xE0410000); // sub r0,r1,r0 |
| 1228 | break; |
| 1229 | case OP_BIT_NOT: |
| 1230 | o4(0xE1E00000); // mvn r0, r0 |
| 1231 | break; |
| 1232 | default: |
| 1233 | error("Unknown unary op %d\n", op); |
| 1234 | break; |
| 1235 | } |
| 1236 | break; |
| 1237 | case TY_FLOAT: |
| 1238 | case TY_DOUBLE: |
| 1239 | switch (op) { |
| 1240 | case OP_MINUS: |
| 1241 | if (tag == TY_FLOAT) { |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1242 | #ifdef ARM_USE_VFP |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 1243 | o4(0xEEF17A67); // fnegs s15, s15 |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1244 | #else |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1245 | callRuntime((void*) runtime_op_neg_f); |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1246 | #endif |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1247 | } else { |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1248 | #ifdef ARM_USE_VFP |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 1249 | o4(0xEEB17B47); // fnegd d7, d7 |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1250 | #else |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1251 | callRuntime((void*) runtime_op_neg_d); |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1252 | #endif |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1253 | } |
| 1254 | break; |
| 1255 | case OP_BIT_NOT: |
| 1256 | error("Can't apply '~' operator to a float or double."); |
| 1257 | break; |
| 1258 | default: |
| 1259 | error("Unknown unary op %d\n", op); |
| 1260 | break; |
| 1261 | } |
| 1262 | break; |
| 1263 | default: |
| 1264 | error("genUnaryOp unsupported type"); |
| 1265 | break; |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 1266 | } |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 1267 | } |
| 1268 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 1269 | virtual void pushR0() { |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1270 | Type* pR0Type = getR0Type(); |
| 1271 | TypeTag r0ct = collapseType(pR0Type->tag); |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1272 | |
| 1273 | #ifdef ARM_USE_VFP |
| 1274 | switch (r0ct ) { |
| 1275 | case TY_FLOAT: |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 1276 | o4(0xED6D7A01); // fstmfds sp!,{s15} |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1277 | mStackUse += 4; |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 1278 | break; |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1279 | case TY_DOUBLE: |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 1280 | o4(0xED2D7B02); // fstmfdd sp!,{d7} |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1281 | mStackUse += 8; |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 1282 | break; |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1283 | default: |
| 1284 | o4(0xE92D0001); // stmfd sp!,{r0} |
| 1285 | mStackUse += 4; |
| 1286 | } |
| 1287 | #else |
| 1288 | |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1289 | if (r0ct != TY_DOUBLE) { |
| 1290 | o4(0xE92D0001); // stmfd sp!,{r0} |
| 1291 | mStackUse += 4; |
| 1292 | } else { |
| 1293 | o4(0xE92D0003); // stmfd sp!,{r0,r1} |
| 1294 | mStackUse += 8; |
| 1295 | } |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1296 | #endif |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 1297 | pushType(); |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 1298 | LOG_STACK("pushR0: %d\n", mStackUse); |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 1299 | } |
| 1300 | |
Jack Palevich | ddf7c9c | 2009-07-29 10:28:18 -0700 | [diff] [blame] | 1301 | virtual void over() { |
| 1302 | // We know it's only used for int-ptr ops (++/--) |
| 1303 | |
| 1304 | Type* pR0Type = getR0Type(); |
| 1305 | TypeTag r0ct = collapseType(pR0Type->tag); |
| 1306 | |
| 1307 | Type* pTOSType = getTOSType(); |
| 1308 | TypeTag tosct = collapseType(pTOSType->tag); |
| 1309 | |
| 1310 | assert (r0ct == TY_INT && tosct == TY_INT); |
| 1311 | |
| 1312 | o4(0xE8BD0002); // ldmfd sp!,{r1} |
| 1313 | o4(0xE92D0001); // stmfd sp!,{r0} |
| 1314 | o4(0xE92D0002); // stmfd sp!,{r1} |
| 1315 | overType(); |
| 1316 | mStackUse += 4; |
| 1317 | } |
| 1318 | |
Jack Palevich | 58c30ee | 2009-07-17 16:35:23 -0700 | [diff] [blame] | 1319 | virtual void popR0() { |
| 1320 | Type* pTOSType = getTOSType(); |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1321 | TypeTag tosct = collapseType(pTOSType->tag); |
| 1322 | #ifdef ARM_USE_VFP |
| 1323 | if (tosct == TY_FLOAT || tosct == TY_DOUBLE) { |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 1324 | error("Unsupported popR0 float/double"); |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1325 | } |
| 1326 | #endif |
| 1327 | switch (tosct){ |
Jack Palevich | 58c30ee | 2009-07-17 16:35:23 -0700 | [diff] [blame] | 1328 | case TY_INT: |
| 1329 | case TY_FLOAT: |
| 1330 | o4(0xE8BD0001); // ldmfd sp!,{r0} |
| 1331 | mStackUse -= 4; |
| 1332 | break; |
| 1333 | case TY_DOUBLE: |
| 1334 | o4(0xE8BD0003); // ldmfd sp!,{r0, r1} // Restore R0 |
| 1335 | mStackUse -= 8; |
| 1336 | break; |
| 1337 | default: |
| 1338 | error("Can't pop this type."); |
| 1339 | break; |
| 1340 | } |
| 1341 | popType(); |
| 1342 | LOG_STACK("popR0: %d\n", mStackUse); |
| 1343 | } |
| 1344 | |
| 1345 | virtual void storeR0ToTOS() { |
| 1346 | Type* pPointerType = getTOSType(); |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 1347 | assert(pPointerType->tag == TY_POINTER); |
Jack Palevich | 8968e8e | 2009-07-30 16:57:33 -0700 | [diff] [blame] | 1348 | Type* pDestType = pPointerType->pHead; |
| 1349 | convertR0(pDestType); |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1350 | o4(0xE8BD0004); // ldmfd sp!,{r2} |
| 1351 | popType(); |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 1352 | mStackUse -= 4; |
Jack Palevich | 8968e8e | 2009-07-30 16:57:33 -0700 | [diff] [blame] | 1353 | switch (pDestType->tag) { |
| 1354 | case TY_POINTER: |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 1355 | case TY_INT: |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1356 | o4(0xE5820000); // str r0, [r2] |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 1357 | break; |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1358 | case TY_FLOAT: |
| 1359 | #ifdef ARM_USE_VFP |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 1360 | o4(0xEDC27A00); // fsts s15, [r2, #0] |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1361 | #else |
| 1362 | o4(0xE5820000); // str r0, [r2] |
| 1363 | #endif |
| 1364 | break; |
Jack Palevich | c9b8ffc | 2009-08-03 14:42:57 -0700 | [diff] [blame] | 1365 | case TY_SHORT: |
| 1366 | o4(0xE1C200B0); // strh r0, [r2] |
| 1367 | break; |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 1368 | case TY_CHAR: |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1369 | o4(0xE5C20000); // strb r0, [r2] |
| 1370 | break; |
| 1371 | case TY_DOUBLE: |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1372 | #ifdef ARM_USE_VFP |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 1373 | o4(0xED827B00); // fstd d7, [r2, #0] |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1374 | #else |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1375 | o4(0xE1C200F0); // strd r0, [r2] |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1376 | #endif |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 1377 | break; |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 1378 | case TY_STRUCT: |
| 1379 | { |
| 1380 | int size = sizeOf(pDestType); |
| 1381 | if (size > 0) { |
| 1382 | liReg(size, 1); |
| 1383 | callRuntime((void*) runtime_structCopy); |
| 1384 | } |
| 1385 | } |
| 1386 | break; |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 1387 | default: |
Jack Palevich | b5e3331 | 2009-07-30 19:06:34 -0700 | [diff] [blame] | 1388 | error("storeR0ToTOS: unimplemented type %d", |
| 1389 | pDestType->tag); |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 1390 | break; |
Jack Palevich | bd89490 | 2009-05-14 19:35:31 -0700 | [diff] [blame] | 1391 | } |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 1392 | } |
| 1393 | |
Jack Palevich | 58c30ee | 2009-07-17 16:35:23 -0700 | [diff] [blame] | 1394 | virtual void loadR0FromR0() { |
| 1395 | Type* pPointerType = getR0Type(); |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 1396 | assert(pPointerType->tag == TY_POINTER); |
Jack Palevich | 80e4972 | 2009-08-04 15:39:49 -0700 | [diff] [blame] | 1397 | Type* pNewType = pPointerType->pHead; |
| 1398 | TypeTag tag = pNewType->tag; |
Jack Palevich | b615450 | 2009-08-04 14:56:09 -0700 | [diff] [blame] | 1399 | switch (tag) { |
Jack Palevich | a7813bd | 2009-07-29 11:36:04 -0700 | [diff] [blame] | 1400 | case TY_POINTER: |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 1401 | case TY_INT: |
| 1402 | o4(0xE5900000); // ldr r0, [r0] |
| 1403 | break; |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1404 | case TY_FLOAT: |
| 1405 | #ifdef ARM_USE_VFP |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 1406 | o4(0xEDD07A00); // flds s15, [r0, #0] |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1407 | #else |
| 1408 | o4(0xE5900000); // ldr r0, [r0] |
| 1409 | #endif |
| 1410 | break; |
Jack Palevich | c9b8ffc | 2009-08-03 14:42:57 -0700 | [diff] [blame] | 1411 | case TY_SHORT: |
| 1412 | o4(0xE1D000F0); // ldrsh r0, [r0] |
| 1413 | break; |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 1414 | case TY_CHAR: |
| 1415 | o4(0xE5D00000); // ldrb r0, [r0] |
| 1416 | break; |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1417 | case TY_DOUBLE: |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1418 | #ifdef ARM_USE_VFP |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 1419 | o4(0xED907B00); // fldd d7, [r0, #0] |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1420 | #else |
Jack Palevich | a7813bd | 2009-07-29 11:36:04 -0700 | [diff] [blame] | 1421 | o4(0xE1C000D0); // ldrd r0, [r0] |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1422 | #endif |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1423 | break; |
Jack Palevich | 80e4972 | 2009-08-04 15:39:49 -0700 | [diff] [blame] | 1424 | case TY_ARRAY: |
| 1425 | pNewType = pNewType->pTail; |
| 1426 | break; |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 1427 | case TY_STRUCT: |
| 1428 | break; |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 1429 | default: |
Jack Palevich | b615450 | 2009-08-04 14:56:09 -0700 | [diff] [blame] | 1430 | error("loadR0FromR0: unimplemented type %d", tag); |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 1431 | break; |
| 1432 | } |
Jack Palevich | 80e4972 | 2009-08-04 15:39:49 -0700 | [diff] [blame] | 1433 | setR0Type(pNewType); |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 1434 | } |
| 1435 | |
Jack Palevich | b5e3331 | 2009-07-30 19:06:34 -0700 | [diff] [blame] | 1436 | virtual void leaR0(int ea, Type* pPointerType, ExpressionType et) { |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1437 | if (ea > -LOCAL && ea < LOCAL) { |
Jack Palevich | 4d93f30 | 2009-05-15 13:30:00 -0700 | [diff] [blame] | 1438 | // Local, fp relative |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 1439 | |
| 1440 | size_t immediate = 0; |
| 1441 | bool inRange = false; |
Jack Palevich | 4d93f30 | 2009-05-15 13:30:00 -0700 | [diff] [blame] | 1442 | if (ea < 0) { |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 1443 | inRange = encode12BitImmediate(-ea, &immediate); |
| 1444 | o4(0xE24B0000 | immediate); // sub r0, fp, #ea |
Jack Palevich | 4d93f30 | 2009-05-15 13:30:00 -0700 | [diff] [blame] | 1445 | } else { |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 1446 | inRange = encode12BitImmediate(ea, &immediate); |
| 1447 | o4(0xE28B0000 | immediate); // add r0, fp, #ea |
| 1448 | } |
| 1449 | if (! inRange) { |
| 1450 | error("Offset out of range: %08x", ea); |
Jack Palevich | 4d93f30 | 2009-05-15 13:30:00 -0700 | [diff] [blame] | 1451 | } |
Jack Palevich | bd89490 | 2009-05-14 19:35:31 -0700 | [diff] [blame] | 1452 | } else { |
Jack Palevich | 4d93f30 | 2009-05-15 13:30:00 -0700 | [diff] [blame] | 1453 | // Global, absolute. |
| 1454 | o4(0xE59F0000); // ldr r0, .L1 |
| 1455 | o4(0xEA000000); // b .L99 |
| 1456 | o4(ea); // .L1: .word 0 |
| 1457 | // .L99: |
Jack Palevich | bd89490 | 2009-05-14 19:35:31 -0700 | [diff] [blame] | 1458 | } |
Jack Palevich | b5e3331 | 2009-07-30 19:06:34 -0700 | [diff] [blame] | 1459 | setR0Type(pPointerType, et); |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 1460 | } |
| 1461 | |
Jack Palevich | 9f51a26 | 2009-07-29 16:22:26 -0700 | [diff] [blame] | 1462 | virtual int leaForward(int ea, Type* pPointerType) { |
| 1463 | setR0Type(pPointerType); |
| 1464 | int result = ea; |
| 1465 | int pc = getPC(); |
| 1466 | int offset = 0; |
| 1467 | if (ea) { |
| 1468 | offset = (pc - ea - 8) >> 2; |
| 1469 | if ((offset & 0xffff) != offset) { |
| 1470 | error("function forward reference out of bounds"); |
| 1471 | } |
| 1472 | } else { |
| 1473 | offset = 0; |
| 1474 | } |
| 1475 | o4(0xE59F0000 | offset); // ldr r0, .L1 |
| 1476 | |
| 1477 | if (ea == 0) { |
| 1478 | o4(0xEA000000); // b .L99 |
| 1479 | result = o4(ea); // .L1: .word 0 |
| 1480 | // .L99: |
| 1481 | } |
| 1482 | return result; |
| 1483 | } |
| 1484 | |
Jack Palevich | b615450 | 2009-08-04 14:56:09 -0700 | [diff] [blame] | 1485 | virtual void convertR0Imp(Type* pType, bool isCast){ |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 1486 | Type* pR0Type = getR0Type(); |
Jack Palevich | b615450 | 2009-08-04 14:56:09 -0700 | [diff] [blame] | 1487 | if (isPointerType(pType) && isPointerType(pR0Type)) { |
| 1488 | Type* pA = pR0Type; |
| 1489 | Type* pB = pType; |
| 1490 | // Array decays to pointer |
| 1491 | if (pA->tag == TY_ARRAY && pB->tag == TY_POINTER) { |
| 1492 | pA = pA->pTail; |
| 1493 | } |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 1494 | if (! (typeEqual(pA, pB) |
| 1495 | || pB->pHead->tag == TY_VOID |
| 1496 | || (pA->tag == TY_POINTER && pB->tag == TY_POINTER && isCast) |
| 1497 | )) { |
| 1498 | error("Incompatible pointer or array types"); |
Jack Palevich | b615450 | 2009-08-04 14:56:09 -0700 | [diff] [blame] | 1499 | } |
Jack Palevich | b615450 | 2009-08-04 14:56:09 -0700 | [diff] [blame] | 1500 | } else if (bitsSame(pType, pR0Type)) { |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 1501 | // do nothing special |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 1502 | } else { |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1503 | TypeTag r0Tag = collapseType(pR0Type->tag); |
| 1504 | TypeTag destTag = collapseType(pType->tag); |
| 1505 | if (r0Tag == TY_INT) { |
| 1506 | if (destTag == TY_FLOAT) { |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1507 | #ifdef ARM_USE_VFP |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 1508 | o4(0xEE070A90); // fmsr s15, r0 |
| 1509 | o4(0xEEF87AE7); // fsitos s15, s15 |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1510 | |
| 1511 | #else |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1512 | callRuntime((void*) runtime_int_to_float); |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1513 | #endif |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1514 | } else { |
| 1515 | assert(destTag == TY_DOUBLE); |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1516 | #ifdef ARM_USE_VFP |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 1517 | o4(0xEE070A90); // fmsr s15, r0 |
| 1518 | o4(0xEEB87BE7); // fsitod d7, s15 |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1519 | |
| 1520 | #else |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1521 | callRuntime((void*) runtime_int_to_double); |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1522 | #endif |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1523 | } |
| 1524 | } else if (r0Tag == TY_FLOAT) { |
| 1525 | if (destTag == TY_INT) { |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1526 | #ifdef ARM_USE_VFP |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 1527 | o4(0xEEFD7AE7); // ftosizs s15, s15 |
| 1528 | o4(0xEE170A90); // fmrs r0, s15 |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1529 | #else |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1530 | callRuntime((void*) runtime_float_to_int); |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1531 | #endif |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1532 | } else { |
| 1533 | assert(destTag == TY_DOUBLE); |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1534 | #ifdef ARM_USE_VFP |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 1535 | o4(0xEEB77AE7); // fcvtds d7, s15 |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1536 | #else |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1537 | callRuntime((void*) runtime_float_to_double); |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1538 | #endif |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1539 | } |
| 1540 | } else { |
Jack Palevich | c408bbf | 2009-09-08 12:07:32 -0700 | [diff] [blame] | 1541 | if (r0Tag == TY_DOUBLE) { |
| 1542 | if (destTag == TY_INT) { |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1543 | #ifdef ARM_USE_VFP |
Jack Palevich | c408bbf | 2009-09-08 12:07:32 -0700 | [diff] [blame] | 1544 | o4(0xEEFD7BC7); // ftosizd s15, d7 |
| 1545 | o4(0xEE170A90); // fmrs r0, s15 |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1546 | #else |
Jack Palevich | c408bbf | 2009-09-08 12:07:32 -0700 | [diff] [blame] | 1547 | callRuntime((void*) runtime_double_to_int); |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1548 | #endif |
Jack Palevich | c408bbf | 2009-09-08 12:07:32 -0700 | [diff] [blame] | 1549 | } else { |
| 1550 | if(destTag == TY_FLOAT) { |
| 1551 | #ifdef ARM_USE_VFP |
| 1552 | o4(0xEEF77BC7); // fcvtsd s15, d7 |
| 1553 | #else |
| 1554 | callRuntime((void*) runtime_double_to_float); |
| 1555 | #endif |
| 1556 | } else { |
| 1557 | incompatibleTypes(pR0Type, pType); |
| 1558 | } |
| 1559 | } |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1560 | } else { |
Jack Palevich | c408bbf | 2009-09-08 12:07:32 -0700 | [diff] [blame] | 1561 | incompatibleTypes(pR0Type, pType); |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1562 | } |
| 1563 | } |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 1564 | } |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 1565 | setR0Type(pType); |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 1566 | } |
| 1567 | |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 1568 | virtual int beginFunctionCallArguments() { |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 1569 | return o4(0xE24DDF00); // Placeholder |
| 1570 | } |
| 1571 | |
Jack Palevich | 8148c5b | 2009-07-16 18:24:47 -0700 | [diff] [blame] | 1572 | virtual size_t storeR0ToArg(int l, Type* pArgType) { |
Jack Palevich | 8148c5b | 2009-07-16 18:24:47 -0700 | [diff] [blame] | 1573 | convertR0(pArgType); |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1574 | Type* pR0Type = getR0Type(); |
| 1575 | TypeTag r0ct = collapseType(pR0Type->tag); |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1576 | #ifdef ARM_USE_VFP |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1577 | switch(r0ct) { |
| 1578 | case TY_INT: |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1579 | if (l < 0 || l > 4096-4) { |
| 1580 | error("l out of range for stack offset: 0x%08x", l); |
| 1581 | } |
| 1582 | o4(0xE58D0000 | l); // str r0, [sp, #l] |
| 1583 | return 4; |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 1584 | case TY_FLOAT: |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1585 | if (l < 0 || l > 1020 || (l & 3)) { |
| 1586 | error("l out of range for stack offset: 0x%08x", l); |
| 1587 | } |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 1588 | o4(0xEDCD7A00 | (l >> 2)); // fsts s15, [sp, #l] |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1589 | return 4; |
| 1590 | case TY_DOUBLE: { |
| 1591 | // Align to 8 byte boundary |
| 1592 | int l2 = (l + 7) & ~7; |
| 1593 | if (l2 < 0 || l2 > 1020 || (l2 & 3)) { |
| 1594 | error("l out of range for stack offset: 0x%08x", l); |
| 1595 | } |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 1596 | o4(0xED8D7B00 | (l2 >> 2)); // fstd d7, [sp, #l2] |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1597 | return (l2 - l) + 8; |
| 1598 | } |
| 1599 | default: |
| 1600 | assert(false); |
| 1601 | return 0; |
| 1602 | } |
| 1603 | #else |
| 1604 | switch(r0ct) { |
| 1605 | case TY_INT: |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 1606 | case TY_FLOAT: |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1607 | if (l < 0 || l > 4096-4) { |
| 1608 | error("l out of range for stack offset: 0x%08x", l); |
| 1609 | } |
| 1610 | o4(0xE58D0000 + l); // str r0, [sp, #l] |
| 1611 | return 4; |
| 1612 | case TY_DOUBLE: { |
| 1613 | // Align to 8 byte boundary |
| 1614 | int l2 = (l + 7) & ~7; |
| 1615 | if (l2 < 0 || l2 > 4096-8) { |
| 1616 | error("l out of range for stack offset: 0x%08x", l); |
| 1617 | } |
| 1618 | o4(0xE58D0000 + l2); // str r0, [sp, #l] |
| 1619 | o4(0xE58D1000 + l2 + 4); // str r1, [sp, #l+4] |
| 1620 | return (l2 - l) + 8; |
| 1621 | } |
| 1622 | default: |
| 1623 | assert(false); |
| 1624 | return 0; |
Jack Palevich | 7810bc9 | 2009-05-15 14:31:47 -0700 | [diff] [blame] | 1625 | } |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1626 | #endif |
Jack Palevich | 7810bc9 | 2009-05-15 14:31:47 -0700 | [diff] [blame] | 1627 | } |
| 1628 | |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1629 | virtual void endFunctionCallArguments(Type* pDecl, int a, int l) { |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 1630 | int argumentStackUse = l; |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1631 | // Have to calculate register arg count from actual stack size, |
| 1632 | // in order to properly handle ... functions. |
| 1633 | int regArgCount = l >> 2; |
| 1634 | if (regArgCount > 4) { |
| 1635 | regArgCount = 4; |
| 1636 | } |
| 1637 | if (regArgCount > 0) { |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 1638 | argumentStackUse -= regArgCount * 4; |
| 1639 | o4(0xE8BD0000 | ((1 << regArgCount) - 1)); // ldmfd sp!,{} |
| 1640 | } |
| 1641 | mStackUse += argumentStackUse; |
| 1642 | |
| 1643 | // Align stack. |
| 1644 | int missalignment = mStackUse - ((mStackUse / STACK_ALIGNMENT) |
| 1645 | * STACK_ALIGNMENT); |
| 1646 | mStackAlignmentAdjustment = 0; |
| 1647 | if (missalignment > 0) { |
| 1648 | mStackAlignmentAdjustment = STACK_ALIGNMENT - missalignment; |
| 1649 | } |
| 1650 | l += mStackAlignmentAdjustment; |
| 1651 | |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 1652 | if (l < 0 || l > 0x3FC) { |
| 1653 | error("L out of range for stack adjustment: 0x%08x", l); |
| 1654 | } |
| 1655 | * (int*) a = 0xE24DDF00 | (l >> 2); // sub sp, sp, #0 << 2 |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 1656 | mStackUse += mStackAlignmentAdjustment; |
| 1657 | LOG_STACK("endFunctionCallArguments mStackUse: %d, mStackAlignmentAdjustment %d\n", |
| 1658 | mStackUse, mStackAlignmentAdjustment); |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 1659 | } |
| 1660 | |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 1661 | virtual int callForward(int symbol, Type* pFunc) { |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 1662 | setR0Type(pFunc->pHead); |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 1663 | // Forward calls are always short (local) |
| 1664 | return o4(0xEB000000 | encodeAddress(symbol)); |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 1665 | } |
| 1666 | |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 1667 | virtual void callIndirect(int l, Type* pFunc) { |
Jack Palevich | 9f51a26 | 2009-07-29 16:22:26 -0700 | [diff] [blame] | 1668 | assert(pFunc->tag == TY_FUNC); |
| 1669 | popType(); // Get rid of indirect fn pointer type |
Jack Palevich | 7810bc9 | 2009-05-15 14:31:47 -0700 | [diff] [blame] | 1670 | int argCount = l >> 2; |
| 1671 | int poppedArgs = argCount > 4 ? 4 : argCount; |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 1672 | int adjustedL = l - (poppedArgs << 2) + mStackAlignmentAdjustment; |
Jack Palevich | 7810bc9 | 2009-05-15 14:31:47 -0700 | [diff] [blame] | 1673 | if (adjustedL < 0 || adjustedL > 4096-4) { |
| 1674 | error("l out of range for stack offset: 0x%08x", l); |
| 1675 | } |
| 1676 | o4(0xE59DC000 | (0xfff & adjustedL)); // ldr r12, [sp,#adjustedL] |
| 1677 | o4(0xE12FFF3C); // blx r12 |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1678 | Type* pReturnType = pFunc->pHead; |
| 1679 | setR0Type(pReturnType); |
| 1680 | #ifdef ARM_USE_VFP |
| 1681 | switch(pReturnType->tag) { |
| 1682 | case TY_FLOAT: |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 1683 | o4(0xEE070A90); // fmsr s15, r0 |
| 1684 | break; |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1685 | case TY_DOUBLE: |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 1686 | o4(0xEC410B17); // fmdrr d7, r0, r1 |
| 1687 | break; |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1688 | default: |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 1689 | break; |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1690 | } |
| 1691 | #endif |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 1692 | } |
| 1693 | |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1694 | virtual void adjustStackAfterCall(Type* pDecl, int l, bool isIndirect) { |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 1695 | int argCount = l >> 2; |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1696 | // Have to calculate register arg count from actual stack size, |
| 1697 | // in order to properly handle ... functions. |
| 1698 | int regArgCount = l >> 2; |
| 1699 | if (regArgCount > 4) { |
| 1700 | regArgCount = 4; |
| 1701 | } |
| 1702 | int stackArgs = argCount - regArgCount; |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 1703 | int stackUse = stackArgs + (isIndirect ? 1 : 0) |
| 1704 | + (mStackAlignmentAdjustment >> 2); |
Jack Palevich | 7810bc9 | 2009-05-15 14:31:47 -0700 | [diff] [blame] | 1705 | if (stackUse) { |
| 1706 | if (stackUse < 0 || stackUse > 255) { |
| 1707 | error("L out of range for stack adjustment: 0x%08x", l); |
| 1708 | } |
| 1709 | o4(0xE28DDF00 | stackUse); // add sp, sp, #stackUse << 2 |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 1710 | mStackUse -= stackUse * 4; |
| 1711 | LOG_STACK("adjustStackAfterCall: %d\n", mStackUse); |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 1712 | } |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 1713 | } |
| 1714 | |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 1715 | virtual int jumpOffset() { |
Jack Palevich | bd89490 | 2009-05-14 19:35:31 -0700 | [diff] [blame] | 1716 | return 8; |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 1717 | } |
| 1718 | |
| 1719 | /* output a symbol and patch all calls to it */ |
| 1720 | virtual void gsym(int t) { |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 1721 | int n; |
| 1722 | int base = getBase(); |
| 1723 | int pc = getPC(); |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 1724 | while (t) { |
| 1725 | int data = * (int*) t; |
| 1726 | int decodedOffset = ((BRANCH_REL_ADDRESS_MASK & data) << 2); |
| 1727 | if (decodedOffset == 0) { |
| 1728 | n = 0; |
| 1729 | } else { |
| 1730 | n = base + decodedOffset; /* next value */ |
| 1731 | } |
| 1732 | *(int *) t = (data & ~BRANCH_REL_ADDRESS_MASK) |
| 1733 | | encodeRelAddress(pc - t - 8); |
| 1734 | t = n; |
| 1735 | } |
| 1736 | } |
| 1737 | |
Jack Palevich | 9f51a26 | 2009-07-29 16:22:26 -0700 | [diff] [blame] | 1738 | /* output a symbol and patch all calls to it */ |
| 1739 | virtual void resolveForward(int t) { |
| 1740 | if (t) { |
| 1741 | int pc = getPC(); |
| 1742 | *(int *) t = pc; |
| 1743 | } |
| 1744 | } |
| 1745 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 1746 | virtual int finishCompile() { |
| 1747 | #if defined(__arm__) |
| 1748 | const long base = long(getBase()); |
| 1749 | const long curr = long(getPC()); |
| 1750 | int err = cacheflush(base, curr, 0); |
| 1751 | return err; |
| 1752 | #else |
| 1753 | return 0; |
| 1754 | #endif |
| 1755 | } |
| 1756 | |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 1757 | /** |
Jack Palevich | 9cbd226 | 2009-07-08 16:48:41 -0700 | [diff] [blame] | 1758 | * alignment (in bytes) for this type of data |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 1759 | */ |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1760 | virtual size_t alignmentOf(Type* pType){ |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 1761 | switch(pType->tag) { |
Jack Palevich | 7fcdf1c | 2009-07-23 18:56:20 -0700 | [diff] [blame] | 1762 | case TY_CHAR: |
| 1763 | return 1; |
Jack Palevich | c9b8ffc | 2009-08-03 14:42:57 -0700 | [diff] [blame] | 1764 | case TY_SHORT: |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 1765 | return 2; |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 1766 | case TY_DOUBLE: |
| 1767 | return 8; |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 1768 | case TY_ARRAY: |
| 1769 | return alignmentOf(pType->pHead); |
| 1770 | case TY_STRUCT: |
| 1771 | return pType->pHead->alignment & 0x7fffffff; |
| 1772 | case TY_FUNC: |
| 1773 | error("alignment of func not supported"); |
| 1774 | return 1; |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 1775 | default: |
| 1776 | return 4; |
| 1777 | } |
| 1778 | } |
| 1779 | |
| 1780 | /** |
| 1781 | * Array element alignment (in bytes) for this type of data. |
| 1782 | */ |
| 1783 | virtual size_t sizeOf(Type* pType){ |
| 1784 | switch(pType->tag) { |
| 1785 | case TY_INT: |
| 1786 | return 4; |
Jack Palevich | c9b8ffc | 2009-08-03 14:42:57 -0700 | [diff] [blame] | 1787 | case TY_SHORT: |
| 1788 | return 2; |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 1789 | case TY_CHAR: |
| 1790 | return 1; |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 1791 | case TY_FLOAT: |
| 1792 | return 4; |
| 1793 | case TY_DOUBLE: |
| 1794 | return 8; |
| 1795 | case TY_POINTER: |
| 1796 | return 4; |
Jack Palevich | b615450 | 2009-08-04 14:56:09 -0700 | [diff] [blame] | 1797 | case TY_ARRAY: |
| 1798 | return pType->length * sizeOf(pType->pHead); |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 1799 | case TY_STRUCT: |
| 1800 | return pType->pHead->length; |
Jack Palevich | b615450 | 2009-08-04 14:56:09 -0700 | [diff] [blame] | 1801 | default: |
| 1802 | error("Unsupported type %d", pType->tag); |
| 1803 | return 0; |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 1804 | } |
| 1805 | } |
Jack Palevich | 9cbd226 | 2009-07-08 16:48:41 -0700 | [diff] [blame] | 1806 | |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 1807 | private: |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 1808 | |
| 1809 | static const int BRANCH_REL_ADDRESS_MASK = 0x00ffffff; |
| 1810 | |
| 1811 | /** Encode a relative address that might also be |
| 1812 | * a label. |
| 1813 | */ |
| 1814 | int encodeAddress(int value) { |
| 1815 | int base = getBase(); |
| 1816 | if (value >= base && value <= getPC() ) { |
| 1817 | // This is a label, encode it relative to the base. |
| 1818 | value = value - base; |
| 1819 | } |
| 1820 | return encodeRelAddress(value); |
| 1821 | } |
| 1822 | |
| 1823 | int encodeRelAddress(int value) { |
| 1824 | return BRANCH_REL_ADDRESS_MASK & (value >> 2); |
| 1825 | } |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 1826 | |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1827 | int calcRegArgCount(Type* pDecl) { |
| 1828 | int reg = 0; |
| 1829 | Type* pArgs = pDecl->pTail; |
| 1830 | while (pArgs && reg < 4) { |
| 1831 | Type* pArg = pArgs->pHead; |
| 1832 | if ( pArg->tag == TY_DOUBLE) { |
| 1833 | int evenReg = (reg + 1) & ~1; |
| 1834 | if (evenReg >= 4) { |
| 1835 | break; |
| 1836 | } |
| 1837 | reg = evenReg + 2; |
| 1838 | } else { |
| 1839 | reg++; |
| 1840 | } |
| 1841 | pArgs = pArgs->pTail; |
| 1842 | } |
| 1843 | return reg; |
| 1844 | } |
| 1845 | |
Jack Palevich | 58c30ee | 2009-07-17 16:35:23 -0700 | [diff] [blame] | 1846 | void setupIntPtrArgs() { |
| 1847 | o4(0xE8BD0002); // ldmfd sp!,{r1} |
| 1848 | mStackUse -= 4; |
| 1849 | popType(); |
| 1850 | } |
| 1851 | |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1852 | /* Pop TOS to R1 (use s14 if VFP) |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1853 | * Make sure both R0 and TOS are floats. (Could be ints) |
| 1854 | * We know that at least one of R0 and TOS is already a float |
| 1855 | */ |
| 1856 | void setupFloatArgs() { |
| 1857 | Type* pR0Type = getR0Type(); |
| 1858 | Type* pTOSType = getTOSType(); |
| 1859 | TypeTag tagR0 = collapseType(pR0Type->tag); |
| 1860 | TypeTag tagTOS = collapseType(pTOSType->tag); |
| 1861 | if (tagR0 != TY_FLOAT) { |
| 1862 | assert(tagR0 == TY_INT); |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1863 | #ifdef ARM_USE_VFP |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 1864 | o4(0xEE070A90); // fmsr s15, r0 |
| 1865 | o4(0xEEF87AE7); // fsitos s15, s15 |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1866 | #else |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1867 | callRuntime((void*) runtime_int_to_float); |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1868 | #endif |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1869 | } |
| 1870 | if (tagTOS != TY_FLOAT) { |
| 1871 | assert(tagTOS == TY_INT); |
| 1872 | assert(tagR0 == TY_FLOAT); |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1873 | #ifdef ARM_USE_VFP |
| 1874 | o4(0xECBD7A01); // fldmfds sp!, {s14} |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 1875 | o4(0xEEB87AC7); // fsitos s14, s14 |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1876 | #else |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1877 | o4(0xE92D0001); // stmfd sp!,{r0} // push R0 |
| 1878 | o4(0xE59D0004); // ldr r0, [sp, #4] |
| 1879 | callRuntime((void*) runtime_int_to_float); |
| 1880 | o4(0xE1A01000); // mov r1, r0 |
| 1881 | o4(0xE8BD0001); // ldmfd sp!,{r0} // pop R0 |
| 1882 | o4(0xE28DD004); // add sp, sp, #4 // Pop sp |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1883 | #endif |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1884 | } else { |
| 1885 | // Pop TOS |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1886 | #ifdef ARM_USE_VFP |
| 1887 | o4(0xECBD7A01); // fldmfds sp!, {s14} |
| 1888 | |
| 1889 | #else |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1890 | o4(0xE8BD0002); // ldmfd sp!,{r1} |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1891 | #endif |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1892 | } |
| 1893 | mStackUse -= 4; |
| 1894 | popType(); |
| 1895 | } |
| 1896 | |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1897 | /* Pop TOS into R2..R3 (use D6 if VFP) |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1898 | * Make sure both R0 and TOS are doubles. Could be floats or ints. |
| 1899 | * We know that at least one of R0 and TOS are already a double. |
| 1900 | */ |
| 1901 | |
| 1902 | void setupDoubleArgs() { |
| 1903 | Type* pR0Type = getR0Type(); |
| 1904 | Type* pTOSType = getTOSType(); |
| 1905 | TypeTag tagR0 = collapseType(pR0Type->tag); |
| 1906 | TypeTag tagTOS = collapseType(pTOSType->tag); |
| 1907 | if (tagR0 != TY_DOUBLE) { |
| 1908 | if (tagR0 == TY_INT) { |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1909 | #ifdef ARM_USE_VFP |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 1910 | o4(0xEE070A90); // fmsr s15, r0 |
| 1911 | o4(0xEEB87BE7); // fsitod d7, s15 |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1912 | |
| 1913 | #else |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1914 | callRuntime((void*) runtime_int_to_double); |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1915 | #endif |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1916 | } else { |
| 1917 | assert(tagR0 == TY_FLOAT); |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1918 | #ifdef ARM_USE_VFP |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 1919 | o4(0xEEB77AE7); // fcvtds d7, s15 |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1920 | #else |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1921 | callRuntime((void*) runtime_float_to_double); |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1922 | #endif |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1923 | } |
| 1924 | } |
| 1925 | if (tagTOS != TY_DOUBLE) { |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1926 | #ifdef ARM_USE_VFP |
| 1927 | if (tagTOS == TY_INT) { |
| 1928 | o4(0xECFD6A01); // fldmfds sp!,{s13} |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 1929 | o4(0xEEB86BE6); // fsitod d6, s13 |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1930 | } else { |
| 1931 | assert(tagTOS == TY_FLOAT); |
| 1932 | o4(0xECFD6A01); // fldmfds sp!,{s13} |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 1933 | o4(0xEEB76AE6); // fcvtds d6, s13 |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1934 | } |
| 1935 | #else |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1936 | o4(0xE92D0003); // stmfd sp!,{r0,r1} // push r0,r1 |
| 1937 | o4(0xE59D0008); // ldr r0, [sp, #8] |
| 1938 | if (tagTOS == TY_INT) { |
| 1939 | callRuntime((void*) runtime_int_to_double); |
| 1940 | } else { |
| 1941 | assert(tagTOS == TY_FLOAT); |
| 1942 | callRuntime((void*) runtime_float_to_double); |
| 1943 | } |
| 1944 | o4(0xE1A02000); // mov r2, r0 |
| 1945 | o4(0xE1A03001); // mov r3, r1 |
| 1946 | o4(0xE8BD0003); // ldmfd sp!,{r0, r1} // Restore R0 |
| 1947 | o4(0xE28DD004); // add sp, sp, #4 // Pop sp |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1948 | #endif |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1949 | mStackUse -= 4; |
| 1950 | } else { |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1951 | #ifdef ARM_USE_VFP |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 1952 | o4(0xECBD6B02); // fldmfdd sp!, {d6} |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1953 | #else |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1954 | o4(0xE8BD000C); // ldmfd sp!,{r2,r3} |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 1955 | #endif |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 1956 | mStackUse -= 8; |
| 1957 | } |
| 1958 | popType(); |
| 1959 | } |
| 1960 | |
Jack Palevich | a8f427f | 2009-07-13 18:40:08 -0700 | [diff] [blame] | 1961 | void liReg(int t, int reg) { |
| 1962 | assert(reg >= 0 && reg < 16); |
| 1963 | int rN = (reg & 0xf) << 12; |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 1964 | size_t encodedImmediate; |
| 1965 | if (encode12BitImmediate(t, &encodedImmediate)) { |
| 1966 | o4(0xE3A00000 | encodedImmediate | rN); // mov rN, #0 |
| 1967 | } else if (encode12BitImmediate(-(t+1), &encodedImmediate)) { |
Jack Palevich | a8f427f | 2009-07-13 18:40:08 -0700 | [diff] [blame] | 1968 | // mvn means move constant ^ ~0 |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 1969 | o4(0xE3E00000 | encodedImmediate | rN); // mvn rN, #0 |
Jack Palevich | a8f427f | 2009-07-13 18:40:08 -0700 | [diff] [blame] | 1970 | } else { |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 1971 | o4(0xE51F0000 | rN); // ldr rN, .L3 |
| 1972 | o4(0xEA000000); // b .L99 |
| 1973 | o4(t); // .L3: .word 0 |
Jack Palevich | a8f427f | 2009-07-13 18:40:08 -0700 | [diff] [blame] | 1974 | // .L99: |
| 1975 | } |
| 1976 | } |
| 1977 | |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 1978 | bool encode12BitImmediate(size_t immediate, size_t* pResult) { |
| 1979 | for(size_t i = 0; i < 16; i++) { |
| 1980 | size_t rotate = i * 2; |
| 1981 | size_t mask = rotateRight(0xff, rotate); |
| 1982 | if ((immediate | mask) == mask) { |
| 1983 | size_t bits8 = rotateLeft(immediate, rotate); |
| 1984 | assert(bits8 <= 0xff); |
| 1985 | *pResult = (i << 8) | bits8; |
| 1986 | return true; |
| 1987 | } |
| 1988 | } |
| 1989 | return false; |
| 1990 | } |
| 1991 | |
Jack Palevich | c408bbf | 2009-09-08 12:07:32 -0700 | [diff] [blame] | 1992 | void incompatibleTypes(Type* pR0Type, Type* pType) { |
| 1993 | error("Incompatible types old: %d new: %d", pR0Type->tag, pType->tag); |
| 1994 | } |
| 1995 | |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 1996 | size_t rotateRight(size_t n, size_t rotate) { |
| 1997 | return (n >> rotate) | (n << (32 - rotate)); |
| 1998 | } |
| 1999 | |
| 2000 | size_t rotateLeft(size_t n, size_t rotate) { |
| 2001 | return (n << rotate) | (n >> (32 - rotate)); |
| 2002 | } |
| 2003 | |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 2004 | void callRuntime(void* fn) { |
| 2005 | o4(0xE59FC000); // ldr r12, .L1 |
Jack Palevich | 3d474a7 | 2009-05-15 15:12:38 -0700 | [diff] [blame] | 2006 | o4(0xEA000000); // b .L99 |
| 2007 | o4((int) fn); //.L1: .word fn |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 2008 | o4(0xE12FFF3C); //.L99: blx r12 |
Jack Palevich | 3d474a7 | 2009-05-15 15:12:38 -0700 | [diff] [blame] | 2009 | } |
| 2010 | |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 2011 | // Integer math: |
| 2012 | |
| 2013 | static int runtime_DIV(int b, int a) { |
| 2014 | return a / b; |
Jack Palevich | 3d474a7 | 2009-05-15 15:12:38 -0700 | [diff] [blame] | 2015 | } |
| 2016 | |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 2017 | static int runtime_MOD(int b, int a) { |
| 2018 | return a % b; |
| 2019 | } |
| 2020 | |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 2021 | static void runtime_structCopy(void* src, size_t size, void* dest) { |
| 2022 | memcpy(dest, src, size); |
| 2023 | } |
| 2024 | |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 2025 | #ifndef ARM_USE_VFP |
| 2026 | |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 2027 | // Comparison to zero |
| 2028 | |
| 2029 | static int runtime_is_non_zero_f(float a) { |
| 2030 | return a != 0; |
| 2031 | } |
| 2032 | |
| 2033 | static int runtime_is_non_zero_d(double a) { |
| 2034 | return a != 0; |
| 2035 | } |
| 2036 | |
| 2037 | // Comparison to zero |
| 2038 | |
| 2039 | static int runtime_is_zero_f(float a) { |
| 2040 | return a == 0; |
| 2041 | } |
| 2042 | |
| 2043 | static int runtime_is_zero_d(double a) { |
| 2044 | return a == 0; |
| 2045 | } |
| 2046 | |
| 2047 | // Type conversion |
| 2048 | |
| 2049 | static int runtime_float_to_int(float a) { |
| 2050 | return (int) a; |
| 2051 | } |
| 2052 | |
| 2053 | static double runtime_float_to_double(float a) { |
| 2054 | return (double) a; |
| 2055 | } |
| 2056 | |
| 2057 | static int runtime_double_to_int(double a) { |
| 2058 | return (int) a; |
| 2059 | } |
| 2060 | |
| 2061 | static float runtime_double_to_float(double a) { |
| 2062 | return (float) a; |
| 2063 | } |
| 2064 | |
| 2065 | static float runtime_int_to_float(int a) { |
| 2066 | return (float) a; |
| 2067 | } |
| 2068 | |
| 2069 | static double runtime_int_to_double(int a) { |
| 2070 | return (double) a; |
| 2071 | } |
| 2072 | |
| 2073 | // Comparisons float |
| 2074 | |
| 2075 | static int runtime_cmp_eq_ff(float b, float a) { |
| 2076 | return a == b; |
| 2077 | } |
| 2078 | |
| 2079 | static int runtime_cmp_ne_ff(float b, float a) { |
| 2080 | return a != b; |
| 2081 | } |
| 2082 | |
| 2083 | static int runtime_cmp_lt_ff(float b, float a) { |
| 2084 | return a < b; |
| 2085 | } |
| 2086 | |
| 2087 | static int runtime_cmp_le_ff(float b, float a) { |
| 2088 | return a <= b; |
| 2089 | } |
| 2090 | |
| 2091 | static int runtime_cmp_ge_ff(float b, float a) { |
| 2092 | return a >= b; |
| 2093 | } |
| 2094 | |
| 2095 | static int runtime_cmp_gt_ff(float b, float a) { |
| 2096 | return a > b; |
| 2097 | } |
| 2098 | |
| 2099 | // Comparisons double |
| 2100 | |
| 2101 | static int runtime_cmp_eq_dd(double b, double a) { |
| 2102 | return a == b; |
| 2103 | } |
| 2104 | |
| 2105 | static int runtime_cmp_ne_dd(double b, double a) { |
| 2106 | return a != b; |
| 2107 | } |
| 2108 | |
| 2109 | static int runtime_cmp_lt_dd(double b, double a) { |
| 2110 | return a < b; |
| 2111 | } |
| 2112 | |
| 2113 | static int runtime_cmp_le_dd(double b, double a) { |
| 2114 | return a <= b; |
| 2115 | } |
| 2116 | |
| 2117 | static int runtime_cmp_ge_dd(double b, double a) { |
| 2118 | return a >= b; |
| 2119 | } |
| 2120 | |
| 2121 | static int runtime_cmp_gt_dd(double b, double a) { |
| 2122 | return a > b; |
| 2123 | } |
| 2124 | |
| 2125 | // Math float |
| 2126 | |
| 2127 | static float runtime_op_add_ff(float b, float a) { |
| 2128 | return a + b; |
| 2129 | } |
| 2130 | |
| 2131 | static float runtime_op_sub_ff(float b, float a) { |
| 2132 | return a - b; |
| 2133 | } |
| 2134 | |
| 2135 | static float runtime_op_mul_ff(float b, float a) { |
| 2136 | return a * b; |
| 2137 | } |
| 2138 | |
| 2139 | static float runtime_op_div_ff(float b, float a) { |
| 2140 | return a / b; |
| 2141 | } |
| 2142 | |
| 2143 | static float runtime_op_neg_f(float a) { |
| 2144 | return -a; |
| 2145 | } |
| 2146 | |
| 2147 | // Math double |
| 2148 | |
| 2149 | static double runtime_op_add_dd(double b, double a) { |
| 2150 | return a + b; |
| 2151 | } |
| 2152 | |
| 2153 | static double runtime_op_sub_dd(double b, double a) { |
| 2154 | return a - b; |
| 2155 | } |
| 2156 | |
| 2157 | static double runtime_op_mul_dd(double b, double a) { |
| 2158 | return a * b; |
| 2159 | } |
| 2160 | |
| 2161 | static double runtime_op_div_dd(double b, double a) { |
| 2162 | return a / b; |
| 2163 | } |
| 2164 | |
| 2165 | static double runtime_op_neg_d(double a) { |
| 2166 | return -a; |
Jack Palevich | 3d474a7 | 2009-05-15 15:12:38 -0700 | [diff] [blame] | 2167 | } |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 2168 | |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 2169 | #endif |
| 2170 | |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 2171 | static const int STACK_ALIGNMENT = 8; |
| 2172 | int mStackUse; |
| 2173 | // This variable holds the amount we adjusted the stack in the most |
| 2174 | // recent endFunctionCallArguments call. It's examined by the |
| 2175 | // following adjustStackAfterCall call. |
| 2176 | int mStackAlignmentAdjustment; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 2177 | }; |
| 2178 | |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 2179 | #endif // PROVIDE_ARM_CODEGEN |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 2180 | |
| 2181 | #ifdef PROVIDE_X86_CODEGEN |
| 2182 | |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2183 | class X86CodeGenerator : public CodeGenerator { |
| 2184 | public: |
| 2185 | X86CodeGenerator() {} |
| 2186 | virtual ~X86CodeGenerator() {} |
| 2187 | |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 2188 | /* returns address to patch with local variable size |
| 2189 | */ |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 2190 | virtual int functionEntry(Type* pDecl) { |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 2191 | o(0xe58955); /* push %ebp, mov %esp, %ebp */ |
| 2192 | return oad(0xec81, 0); /* sub $xxx, %esp */ |
| 2193 | } |
| 2194 | |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 2195 | virtual void functionExit(Type* pDecl, int localVariableAddress, int localVariableSize) { |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 2196 | o(0xc3c9); /* leave, ret */ |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 2197 | *(int *) localVariableAddress = localVariableSize; /* save local variables */ |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 2198 | } |
| 2199 | |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2200 | /* load immediate value */ |
Jack Palevich | 58c30ee | 2009-07-17 16:35:23 -0700 | [diff] [blame] | 2201 | virtual void li(int i) { |
Jack Palevich | 1aeb87b | 2009-07-06 18:33:20 -0700 | [diff] [blame] | 2202 | oad(0xb8, i); /* mov $xx, %eax */ |
Jack Palevich | 58c30ee | 2009-07-17 16:35:23 -0700 | [diff] [blame] | 2203 | setR0Type(mkpInt); |
Jack Palevich | 1aeb87b | 2009-07-06 18:33:20 -0700 | [diff] [blame] | 2204 | } |
| 2205 | |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 2206 | virtual void loadFloat(int address, Type* pType) { |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 2207 | setR0Type(pType); |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 2208 | switch (pType->tag) { |
| 2209 | case TY_FLOAT: |
| 2210 | oad(0x05D9, address); // flds |
| 2211 | break; |
| 2212 | case TY_DOUBLE: |
| 2213 | oad(0x05DD, address); // fldl |
| 2214 | break; |
| 2215 | default: |
| 2216 | assert(false); |
| 2217 | break; |
| 2218 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2219 | } |
| 2220 | |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 2221 | virtual void addStructOffsetR0(int offset, Type* pType) { |
| 2222 | if (offset) { |
| 2223 | oad(0x05, offset); // addl offset, %eax |
| 2224 | } |
| 2225 | setR0Type(pType, ET_LVALUE); |
| 2226 | } |
| 2227 | |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 2228 | virtual int gjmp(int t) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2229 | return psym(0xe9, t); |
| 2230 | } |
| 2231 | |
| 2232 | /* l = 0: je, l == 1: jne */ |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 2233 | virtual int gtst(bool l, int t) { |
Jack Palevich | 2a4e1a9 | 2009-07-09 13:34:25 -0700 | [diff] [blame] | 2234 | Type* pR0Type = getR0Type(); |
| 2235 | TypeTag tagR0 = pR0Type->tag; |
| 2236 | bool isFloatR0 = isFloatTag(tagR0); |
| 2237 | if (isFloatR0) { |
| 2238 | o(0xeed9); // fldz |
| 2239 | o(0xe9da); // fucompp |
| 2240 | o(0xe0df); // fnstsw %ax |
| 2241 | o(0x9e); // sahf |
| 2242 | } else { |
| 2243 | o(0xc085); // test %eax, %eax |
| 2244 | } |
| 2245 | // Use two output statements to generate one instruction. |
| 2246 | o(0x0f); // je/jne xxx |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2247 | return psym(0x84 + l, t); |
| 2248 | } |
| 2249 | |
Jack Palevich | 58c30ee | 2009-07-17 16:35:23 -0700 | [diff] [blame] | 2250 | virtual void gcmp(int op) { |
Jack Palevich | a39749f | 2009-07-08 20:40:31 -0700 | [diff] [blame] | 2251 | Type* pR0Type = getR0Type(); |
| 2252 | Type* pTOSType = getTOSType(); |
| 2253 | TypeTag tagR0 = pR0Type->tag; |
| 2254 | TypeTag tagTOS = pTOSType->tag; |
| 2255 | bool isFloatR0 = isFloatTag(tagR0); |
| 2256 | bool isFloatTOS = isFloatTag(tagTOS); |
| 2257 | if (!isFloatR0 && !isFloatTOS) { |
| 2258 | int t = decodeOp(op); |
| 2259 | o(0x59); /* pop %ecx */ |
| 2260 | o(0xc139); /* cmp %eax,%ecx */ |
Jack Palevich | 58c30ee | 2009-07-17 16:35:23 -0700 | [diff] [blame] | 2261 | li(0); |
Jack Palevich | a39749f | 2009-07-08 20:40:31 -0700 | [diff] [blame] | 2262 | o(0x0f); /* setxx %al */ |
| 2263 | o(t + 0x90); |
| 2264 | o(0xc0); |
| 2265 | popType(); |
| 2266 | } else { |
| 2267 | setupFloatOperands(); |
| 2268 | switch (op) { |
| 2269 | case OP_EQUALS: |
| 2270 | o(0xe9da); // fucompp |
| 2271 | o(0xe0df); // fnstsw %ax |
| 2272 | o(0x9e); // sahf |
| 2273 | o(0xc0940f); // sete %al |
| 2274 | o(0xc29b0f); // setnp %dl |
| 2275 | o(0xd021); // andl %edx, %eax |
| 2276 | break; |
| 2277 | case OP_NOT_EQUALS: |
| 2278 | o(0xe9da); // fucompp |
| 2279 | o(0xe0df); // fnstsw %ax |
| 2280 | o(0x9e); // sahf |
| 2281 | o(0xc0950f); // setne %al |
| 2282 | o(0xc29a0f); // setp %dl |
| 2283 | o(0xd009); // orl %edx, %eax |
| 2284 | break; |
| 2285 | case OP_GREATER_EQUAL: |
| 2286 | o(0xe9da); // fucompp |
| 2287 | o(0xe0df); // fnstsw %ax |
| 2288 | o(0x05c4f6); // testb $5, %ah |
| 2289 | o(0xc0940f); // sete %al |
| 2290 | break; |
| 2291 | case OP_LESS: |
| 2292 | o(0xc9d9); // fxch %st(1) |
| 2293 | o(0xe9da); // fucompp |
| 2294 | o(0xe0df); // fnstsw %ax |
| 2295 | o(0x9e); // sahf |
| 2296 | o(0xc0970f); // seta %al |
| 2297 | break; |
| 2298 | case OP_LESS_EQUAL: |
| 2299 | o(0xc9d9); // fxch %st(1) |
| 2300 | o(0xe9da); // fucompp |
| 2301 | o(0xe0df); // fnstsw %ax |
| 2302 | o(0x9e); // sahf |
| 2303 | o(0xc0930f); // setea %al |
| 2304 | break; |
| 2305 | case OP_GREATER: |
| 2306 | o(0xe9da); // fucompp |
| 2307 | o(0xe0df); // fnstsw %ax |
| 2308 | o(0x45c4f6); // testb $69, %ah |
| 2309 | o(0xc0940f); // sete %al |
| 2310 | break; |
| 2311 | default: |
| 2312 | error("Unknown comparison op"); |
| 2313 | } |
| 2314 | o(0xc0b60f); // movzbl %al, %eax |
| 2315 | } |
Jack Palevich | 58c30ee | 2009-07-17 16:35:23 -0700 | [diff] [blame] | 2316 | setR0Type(mkpInt); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2317 | } |
| 2318 | |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 2319 | virtual void genOp(int op) { |
Jack Palevich | a39749f | 2009-07-08 20:40:31 -0700 | [diff] [blame] | 2320 | Type* pR0Type = getR0Type(); |
| 2321 | Type* pTOSType = getTOSType(); |
| 2322 | TypeTag tagR0 = pR0Type->tag; |
| 2323 | TypeTag tagTOS = pTOSType->tag; |
| 2324 | bool isFloatR0 = isFloatTag(tagR0); |
| 2325 | bool isFloatTOS = isFloatTag(tagTOS); |
| 2326 | if (!isFloatR0 && !isFloatTOS) { |
Jack Palevich | b615450 | 2009-08-04 14:56:09 -0700 | [diff] [blame] | 2327 | bool isPtrR0 = isPointerTag(tagR0); |
| 2328 | bool isPtrTOS = isPointerTag(tagTOS); |
Jack Palevich | a8f427f | 2009-07-13 18:40:08 -0700 | [diff] [blame] | 2329 | if (isPtrR0 || isPtrTOS) { |
| 2330 | if (isPtrR0 && isPtrTOS) { |
| 2331 | if (op != OP_MINUS) { |
| 2332 | error("Unsupported pointer-pointer operation %d.", op); |
| 2333 | } |
| 2334 | if (! typeEqual(pR0Type, pTOSType)) { |
| 2335 | error("Incompatible pointer types for subtraction."); |
| 2336 | } |
| 2337 | o(0x59); /* pop %ecx */ |
| 2338 | o(decodeOp(op)); |
| 2339 | popType(); |
| 2340 | setR0Type(mkpInt); |
| 2341 | int size = sizeOf(pR0Type->pHead); |
| 2342 | if (size != 1) { |
| 2343 | pushR0(); |
Jack Palevich | 58c30ee | 2009-07-17 16:35:23 -0700 | [diff] [blame] | 2344 | li(size); |
Jack Palevich | a8f427f | 2009-07-13 18:40:08 -0700 | [diff] [blame] | 2345 | // TODO: Optimize for power-of-two. |
| 2346 | genOp(OP_DIV); |
| 2347 | } |
| 2348 | } else { |
| 2349 | if (! (op == OP_PLUS || (op == OP_MINUS && isPtrR0))) { |
| 2350 | error("Unsupported pointer-scalar operation %d", op); |
| 2351 | } |
Jack Palevich | b615450 | 2009-08-04 14:56:09 -0700 | [diff] [blame] | 2352 | Type* pPtrType = getPointerArithmeticResultType( |
| 2353 | pR0Type, pTOSType); |
Jack Palevich | a8f427f | 2009-07-13 18:40:08 -0700 | [diff] [blame] | 2354 | o(0x59); /* pop %ecx */ |
| 2355 | int size = sizeOf(pPtrType->pHead); |
| 2356 | if (size != 1) { |
| 2357 | // TODO: Optimize for power-of-two. |
| 2358 | if (isPtrR0) { |
| 2359 | oad(0xC969, size); // imull $size, %ecx |
| 2360 | } else { |
| 2361 | oad(0xC069, size); // mul $size, %eax |
| 2362 | } |
| 2363 | } |
| 2364 | o(decodeOp(op)); |
| 2365 | popType(); |
| 2366 | setR0Type(pPtrType); |
| 2367 | } |
| 2368 | } else { |
| 2369 | o(0x59); /* pop %ecx */ |
| 2370 | o(decodeOp(op)); |
| 2371 | if (op == OP_MOD) |
| 2372 | o(0x92); /* xchg %edx, %eax */ |
| 2373 | popType(); |
| 2374 | } |
Jack Palevich | a39749f | 2009-07-08 20:40:31 -0700 | [diff] [blame] | 2375 | } else { |
| 2376 | Type* pResultType = tagR0 > tagTOS ? pR0Type : pTOSType; |
| 2377 | setupFloatOperands(); |
| 2378 | // Both float. x87 R0 == left hand, x87 R1 == right hand |
| 2379 | switch (op) { |
| 2380 | case OP_MUL: |
| 2381 | o(0xc9de); // fmulp |
| 2382 | break; |
| 2383 | case OP_DIV: |
| 2384 | o(0xf1de); // fdivp |
| 2385 | break; |
| 2386 | case OP_PLUS: |
| 2387 | o(0xc1de); // faddp |
| 2388 | break; |
| 2389 | case OP_MINUS: |
| 2390 | o(0xe1de); // fsubp |
| 2391 | break; |
| 2392 | default: |
| 2393 | error("Unsupported binary floating operation."); |
| 2394 | break; |
| 2395 | } |
Jack Palevich | a39749f | 2009-07-08 20:40:31 -0700 | [diff] [blame] | 2396 | setR0Type(pResultType); |
Jack Palevich | a39749f | 2009-07-08 20:40:31 -0700 | [diff] [blame] | 2397 | } |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 2398 | } |
| 2399 | |
Jack Palevich | 58c30ee | 2009-07-17 16:35:23 -0700 | [diff] [blame] | 2400 | virtual void gUnaryCmp(int op) { |
Jack Palevich | a39749f | 2009-07-08 20:40:31 -0700 | [diff] [blame] | 2401 | if (op != OP_LOGICAL_NOT) { |
| 2402 | error("Unknown unary cmp %d", op); |
| 2403 | } else { |
| 2404 | Type* pR0Type = getR0Type(); |
| 2405 | TypeTag tag = collapseType(pR0Type->tag); |
| 2406 | switch(tag) { |
| 2407 | case TY_INT: { |
| 2408 | oad(0xb9, 0); /* movl $0, %ecx */ |
| 2409 | int t = decodeOp(op); |
| 2410 | o(0xc139); /* cmp %eax,%ecx */ |
Jack Palevich | 58c30ee | 2009-07-17 16:35:23 -0700 | [diff] [blame] | 2411 | li(0); |
Jack Palevich | a39749f | 2009-07-08 20:40:31 -0700 | [diff] [blame] | 2412 | o(0x0f); /* setxx %al */ |
| 2413 | o(t + 0x90); |
| 2414 | o(0xc0); |
| 2415 | } |
| 2416 | break; |
| 2417 | case TY_FLOAT: |
| 2418 | case TY_DOUBLE: |
| 2419 | o(0xeed9); // fldz |
| 2420 | o(0xe9da); // fucompp |
| 2421 | o(0xe0df); // fnstsw %ax |
| 2422 | o(0x9e); // sahf |
| 2423 | o(0xc0950f); // setne %al |
| 2424 | o(0xc29a0f); // setp %dl |
| 2425 | o(0xd009); // orl %edx, %eax |
| 2426 | o(0xc0b60f); // movzbl %al, %eax |
| 2427 | o(0x01f083); // xorl $1, %eax |
| 2428 | break; |
| 2429 | default: |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 2430 | error("gUnaryCmp unsupported type"); |
Jack Palevich | a39749f | 2009-07-08 20:40:31 -0700 | [diff] [blame] | 2431 | break; |
| 2432 | } |
| 2433 | } |
Jack Palevich | 58c30ee | 2009-07-17 16:35:23 -0700 | [diff] [blame] | 2434 | setR0Type(mkpInt); |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 2435 | } |
| 2436 | |
| 2437 | virtual void genUnaryOp(int op) { |
Jack Palevich | a39749f | 2009-07-08 20:40:31 -0700 | [diff] [blame] | 2438 | Type* pR0Type = getR0Type(); |
| 2439 | TypeTag tag = collapseType(pR0Type->tag); |
| 2440 | switch(tag) { |
| 2441 | case TY_INT: |
| 2442 | oad(0xb9, 0); /* movl $0, %ecx */ |
| 2443 | o(decodeOp(op)); |
| 2444 | break; |
| 2445 | case TY_FLOAT: |
| 2446 | case TY_DOUBLE: |
| 2447 | switch (op) { |
| 2448 | case OP_MINUS: |
| 2449 | o(0xe0d9); // fchs |
| 2450 | break; |
| 2451 | case OP_BIT_NOT: |
| 2452 | error("Can't apply '~' operator to a float or double."); |
| 2453 | break; |
| 2454 | default: |
| 2455 | error("Unknown unary op %d\n", op); |
| 2456 | break; |
| 2457 | } |
| 2458 | break; |
| 2459 | default: |
| 2460 | error("genUnaryOp unsupported type"); |
| 2461 | break; |
| 2462 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2463 | } |
| 2464 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 2465 | virtual void pushR0() { |
Jack Palevich | 9cbd226 | 2009-07-08 16:48:41 -0700 | [diff] [blame] | 2466 | Type* pR0Type = getR0Type(); |
| 2467 | TypeTag r0ct = collapseType(pR0Type->tag); |
| 2468 | switch(r0ct) { |
| 2469 | case TY_INT: |
| 2470 | o(0x50); /* push %eax */ |
| 2471 | break; |
| 2472 | case TY_FLOAT: |
| 2473 | o(0x50); /* push %eax */ |
| 2474 | o(0x241cd9); // fstps 0(%esp) |
| 2475 | break; |
| 2476 | case TY_DOUBLE: |
| 2477 | o(0x50); /* push %eax */ |
| 2478 | o(0x50); /* push %eax */ |
| 2479 | o(0x241cdd); // fstpl 0(%esp) |
| 2480 | break; |
| 2481 | default: |
Jack Palevich | 2a4e1a9 | 2009-07-09 13:34:25 -0700 | [diff] [blame] | 2482 | error("pushR0 unsupported type %d", r0ct); |
Jack Palevich | 9cbd226 | 2009-07-08 16:48:41 -0700 | [diff] [blame] | 2483 | break; |
| 2484 | } |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 2485 | pushType(); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2486 | } |
| 2487 | |
Jack Palevich | ddf7c9c | 2009-07-29 10:28:18 -0700 | [diff] [blame] | 2488 | virtual void over() { |
| 2489 | // We know it's only used for int-ptr ops (++/--) |
| 2490 | |
| 2491 | Type* pR0Type = getR0Type(); |
| 2492 | TypeTag r0ct = collapseType(pR0Type->tag); |
| 2493 | |
| 2494 | Type* pTOSType = getTOSType(); |
| 2495 | TypeTag tosct = collapseType(pTOSType->tag); |
| 2496 | |
| 2497 | assert (r0ct == TY_INT && tosct == TY_INT); |
| 2498 | |
| 2499 | o(0x59); /* pop %ecx */ |
| 2500 | o(0x50); /* push %eax */ |
| 2501 | o(0x51); /* push %ecx */ |
| 2502 | |
| 2503 | overType(); |
| 2504 | } |
| 2505 | |
Jack Palevich | 58c30ee | 2009-07-17 16:35:23 -0700 | [diff] [blame] | 2506 | virtual void popR0() { |
| 2507 | Type* pR0Type = getR0Type(); |
| 2508 | TypeTag r0ct = collapseType(pR0Type->tag); |
| 2509 | switch(r0ct) { |
| 2510 | case TY_INT: |
| 2511 | o(0x58); /* popl %eax */ |
| 2512 | break; |
| 2513 | case TY_FLOAT: |
| 2514 | o(0x2404d9); // flds (%esp) |
| 2515 | o(0x58); /* popl %eax */ |
| 2516 | break; |
| 2517 | case TY_DOUBLE: |
| 2518 | o(0x2404dd); // fldl (%esp) |
| 2519 | o(0x58); /* popl %eax */ |
| 2520 | o(0x58); /* popl %eax */ |
| 2521 | break; |
| 2522 | default: |
Jack Palevich | ddf7c9c | 2009-07-29 10:28:18 -0700 | [diff] [blame] | 2523 | error("popR0 unsupported type %d", r0ct); |
Jack Palevich | 58c30ee | 2009-07-17 16:35:23 -0700 | [diff] [blame] | 2524 | break; |
| 2525 | } |
| 2526 | popType(); |
| 2527 | } |
| 2528 | |
| 2529 | virtual void storeR0ToTOS() { |
| 2530 | Type* pPointerType = getTOSType(); |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 2531 | assert(pPointerType->tag == TY_POINTER); |
Jack Palevich | 8148c5b | 2009-07-16 18:24:47 -0700 | [diff] [blame] | 2532 | Type* pTargetType = pPointerType->pHead; |
| 2533 | convertR0(pTargetType); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2534 | o(0x59); /* pop %ecx */ |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 2535 | popType(); |
Jack Palevich | 8148c5b | 2009-07-16 18:24:47 -0700 | [diff] [blame] | 2536 | switch (pTargetType->tag) { |
Jack Palevich | 8968e8e | 2009-07-30 16:57:33 -0700 | [diff] [blame] | 2537 | case TY_POINTER: |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 2538 | case TY_INT: |
| 2539 | o(0x0189); /* movl %eax/%al, (%ecx) */ |
| 2540 | break; |
Jack Palevich | c9b8ffc | 2009-08-03 14:42:57 -0700 | [diff] [blame] | 2541 | case TY_SHORT: |
| 2542 | o(0x018966); /* movw %ax, (%ecx) */ |
| 2543 | break; |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 2544 | case TY_CHAR: |
| 2545 | o(0x0188); /* movl %eax/%al, (%ecx) */ |
| 2546 | break; |
Jack Palevich | 9cbd226 | 2009-07-08 16:48:41 -0700 | [diff] [blame] | 2547 | case TY_FLOAT: |
| 2548 | o(0x19d9); /* fstps (%ecx) */ |
| 2549 | break; |
| 2550 | case TY_DOUBLE: |
| 2551 | o(0x19dd); /* fstpl (%ecx) */ |
| 2552 | break; |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 2553 | case TY_STRUCT: |
| 2554 | { |
| 2555 | // TODO: use alignment information to use movsw/movsl instead of movsb |
| 2556 | int size = sizeOf(pTargetType); |
| 2557 | if (size > 0) { |
| 2558 | o(0x9c); // pushf |
| 2559 | o(0x57); // pushl %edi |
| 2560 | o(0x56); // pushl %esi |
| 2561 | o(0xcf89); // movl %ecx, %edi |
| 2562 | o(0xc689); // movl %eax, %esi |
| 2563 | oad(0xb9, size); // mov #size, %ecx |
| 2564 | o(0xfc); // cld |
| 2565 | o(0xf3); // rep |
| 2566 | o(0xa4); // movsb |
| 2567 | o(0x5e); // popl %esi |
| 2568 | o(0x5f); // popl %edi |
| 2569 | o(0x9d); // popf |
| 2570 | } |
| 2571 | } |
| 2572 | break; |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 2573 | default: |
Jack Palevich | b5e3331 | 2009-07-30 19:06:34 -0700 | [diff] [blame] | 2574 | error("storeR0ToTOS: unsupported type %d", |
| 2575 | pTargetType->tag); |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 2576 | break; |
| 2577 | } |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 2578 | } |
| 2579 | |
Jack Palevich | 58c30ee | 2009-07-17 16:35:23 -0700 | [diff] [blame] | 2580 | virtual void loadR0FromR0() { |
| 2581 | Type* pPointerType = getR0Type(); |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 2582 | assert(pPointerType->tag == TY_POINTER); |
Jack Palevich | 80e4972 | 2009-08-04 15:39:49 -0700 | [diff] [blame] | 2583 | Type* pNewType = pPointerType->pHead; |
| 2584 | TypeTag tag = pNewType->tag; |
Jack Palevich | b615450 | 2009-08-04 14:56:09 -0700 | [diff] [blame] | 2585 | switch (tag) { |
Jack Palevich | a7813bd | 2009-07-29 11:36:04 -0700 | [diff] [blame] | 2586 | case TY_POINTER: |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 2587 | case TY_INT: |
Jack Palevich | 2a4e1a9 | 2009-07-09 13:34:25 -0700 | [diff] [blame] | 2588 | o2(0x008b); /* mov (%eax), %eax */ |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 2589 | break; |
Jack Palevich | c9b8ffc | 2009-08-03 14:42:57 -0700 | [diff] [blame] | 2590 | case TY_SHORT: |
| 2591 | o(0xbf0f); /* movswl (%eax), %eax */ |
| 2592 | ob(0); |
| 2593 | break; |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 2594 | case TY_CHAR: |
| 2595 | o(0xbe0f); /* movsbl (%eax), %eax */ |
Jack Palevich | 2a4e1a9 | 2009-07-09 13:34:25 -0700 | [diff] [blame] | 2596 | ob(0); /* add zero in code */ |
| 2597 | break; |
| 2598 | case TY_FLOAT: |
| 2599 | o2(0x00d9); // flds (%eax) |
| 2600 | break; |
| 2601 | case TY_DOUBLE: |
| 2602 | o2(0x00dd); // fldl (%eax) |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 2603 | break; |
Jack Palevich | 80e4972 | 2009-08-04 15:39:49 -0700 | [diff] [blame] | 2604 | case TY_ARRAY: |
| 2605 | pNewType = pNewType->pTail; |
| 2606 | break; |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 2607 | case TY_STRUCT: |
| 2608 | break; |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 2609 | default: |
Jack Palevich | b615450 | 2009-08-04 14:56:09 -0700 | [diff] [blame] | 2610 | error("loadR0FromR0: unsupported type %d", tag); |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 2611 | break; |
| 2612 | } |
Jack Palevich | 80e4972 | 2009-08-04 15:39:49 -0700 | [diff] [blame] | 2613 | setR0Type(pNewType); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2614 | } |
| 2615 | |
Jack Palevich | b5e3331 | 2009-07-30 19:06:34 -0700 | [diff] [blame] | 2616 | virtual void leaR0(int ea, Type* pPointerType, ExpressionType et) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2617 | gmov(10, ea); /* leal EA, %eax */ |
Jack Palevich | b5e3331 | 2009-07-30 19:06:34 -0700 | [diff] [blame] | 2618 | setR0Type(pPointerType, et); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2619 | } |
| 2620 | |
Jack Palevich | 9f51a26 | 2009-07-29 16:22:26 -0700 | [diff] [blame] | 2621 | virtual int leaForward(int ea, Type* pPointerType) { |
| 2622 | oad(0xb8, ea); /* mov $xx, %eax */ |
| 2623 | setR0Type(pPointerType); |
| 2624 | return getPC() - 4; |
| 2625 | } |
| 2626 | |
Jack Palevich | b615450 | 2009-08-04 14:56:09 -0700 | [diff] [blame] | 2627 | virtual void convertR0Imp(Type* pType, bool isCast){ |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 2628 | Type* pR0Type = getR0Type(); |
| 2629 | if (pR0Type == NULL) { |
Jack Palevich | 128ad2d | 2009-07-08 14:51:31 -0700 | [diff] [blame] | 2630 | assert(false); |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 2631 | setR0Type(pType); |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 2632 | return; |
| 2633 | } |
Jack Palevich | b615450 | 2009-08-04 14:56:09 -0700 | [diff] [blame] | 2634 | if (isPointerType(pType) && isPointerType(pR0Type)) { |
| 2635 | Type* pA = pR0Type; |
| 2636 | Type* pB = pType; |
| 2637 | // Array decays to pointer |
| 2638 | if (pA->tag == TY_ARRAY && pB->tag == TY_POINTER) { |
| 2639 | pA = pA->pTail; |
| 2640 | } |
Jack Palevich | c0f2533 | 2009-08-25 12:23:43 -0700 | [diff] [blame] | 2641 | if (! (typeEqual(pA, pB) |
| 2642 | || pB->pHead->tag == TY_VOID |
| 2643 | || (pA->tag == TY_POINTER && pB->tag == TY_POINTER && isCast) |
| 2644 | )) { |
| 2645 | error("Incompatible pointer or array types"); |
Jack Palevich | b615450 | 2009-08-04 14:56:09 -0700 | [diff] [blame] | 2646 | } |
Jack Palevich | b615450 | 2009-08-04 14:56:09 -0700 | [diff] [blame] | 2647 | } else if (bitsSame(pType, pR0Type)) { |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 2648 | // do nothing special |
| 2649 | } else if (isFloatType(pType) && isFloatType(pR0Type)) { |
| 2650 | // do nothing special, both held in same register on x87. |
| 2651 | } else { |
Jack Palevich | 128ad2d | 2009-07-08 14:51:31 -0700 | [diff] [blame] | 2652 | TypeTag r0Tag = collapseType(pR0Type->tag); |
| 2653 | TypeTag destTag = collapseType(pType->tag); |
| 2654 | if (r0Tag == TY_INT && isFloatTag(destTag)) { |
| 2655 | // Convert R0 from int to float |
| 2656 | o(0x50); // push %eax |
| 2657 | o(0x2404DB); // fildl 0(%esp) |
| 2658 | o(0x58); // pop %eax |
| 2659 | } else if (isFloatTag(r0Tag) && destTag == TY_INT) { |
| 2660 | // Convert R0 from float to int. Complicated because |
| 2661 | // need to save and restore the rounding mode. |
| 2662 | o(0x50); // push %eax |
| 2663 | o(0x50); // push %eax |
| 2664 | o(0x02247cD9); // fnstcw 2(%esp) |
| 2665 | o(0x2444b70f); // movzwl 2(%esp), %eax |
| 2666 | o(0x02); |
| 2667 | o(0x0cb4); // movb $12, %ah |
| 2668 | o(0x24048966); // movw %ax, 0(%esp) |
| 2669 | o(0x242cd9); // fldcw 0(%esp) |
| 2670 | o(0x04245cdb); // fistpl 4(%esp) |
| 2671 | o(0x02246cd9); // fldcw 2(%esp) |
| 2672 | o(0x58); // pop %eax |
| 2673 | o(0x58); // pop %eax |
| 2674 | } else { |
| 2675 | error("Incompatible types old: %d new: %d", |
| 2676 | pR0Type->tag, pType->tag); |
| 2677 | } |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 2678 | } |
| 2679 | setR0Type(pType); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2680 | } |
| 2681 | |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 2682 | virtual int beginFunctionCallArguments() { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2683 | return oad(0xec81, 0); /* sub $xxx, %esp */ |
| 2684 | } |
| 2685 | |
Jack Palevich | 8148c5b | 2009-07-16 18:24:47 -0700 | [diff] [blame] | 2686 | virtual size_t storeR0ToArg(int l, Type* pArgType) { |
| 2687 | convertR0(pArgType); |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 2688 | Type* pR0Type = getR0Type(); |
| 2689 | TypeTag r0ct = collapseType(pR0Type->tag); |
| 2690 | switch(r0ct) { |
| 2691 | case TY_INT: |
| 2692 | oad(0x248489, l); /* movl %eax, xxx(%esp) */ |
| 2693 | return 4; |
| 2694 | case TY_FLOAT: |
| 2695 | oad(0x249CD9, l); /* fstps xxx(%esp) */ |
| 2696 | return 4; |
| 2697 | case TY_DOUBLE: |
| 2698 | oad(0x249CDD, l); /* fstpl xxx(%esp) */ |
| 2699 | return 8; |
| 2700 | default: |
| 2701 | assert(false); |
| 2702 | return 0; |
| 2703 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2704 | } |
| 2705 | |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 2706 | virtual void endFunctionCallArguments(Type* pDecl, int a, int l) { |
Jack Palevich | 7810bc9 | 2009-05-15 14:31:47 -0700 | [diff] [blame] | 2707 | * (int*) a = l; |
| 2708 | } |
| 2709 | |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 2710 | virtual int callForward(int symbol, Type* pFunc) { |
Jack Palevich | 9f51a26 | 2009-07-29 16:22:26 -0700 | [diff] [blame] | 2711 | assert(pFunc->tag == TY_FUNC); |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 2712 | setR0Type(pFunc->pHead); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2713 | return psym(0xe8, symbol); /* call xxx */ |
| 2714 | } |
| 2715 | |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 2716 | virtual void callIndirect(int l, Type* pFunc) { |
Jack Palevich | 9f51a26 | 2009-07-29 16:22:26 -0700 | [diff] [blame] | 2717 | assert(pFunc->tag == TY_FUNC); |
Jack Palevich | b5e3331 | 2009-07-30 19:06:34 -0700 | [diff] [blame] | 2718 | popType(); // Get rid of indirect fn pointer type |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 2719 | setR0Type(pFunc->pHead); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2720 | oad(0x2494ff, l); /* call *xxx(%esp) */ |
| 2721 | } |
| 2722 | |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 2723 | virtual void adjustStackAfterCall(Type* pDecl, int l, bool isIndirect) { |
Jack Palevich | 9f51a26 | 2009-07-29 16:22:26 -0700 | [diff] [blame] | 2724 | assert(pDecl->tag == TY_FUNC); |
Jack Palevich | 7810bc9 | 2009-05-15 14:31:47 -0700 | [diff] [blame] | 2725 | if (isIndirect) { |
| 2726 | l += 4; |
| 2727 | } |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 2728 | if (l > 0) { |
| 2729 | oad(0xc481, l); /* add $xxx, %esp */ |
| 2730 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2731 | } |
| 2732 | |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 2733 | virtual int jumpOffset() { |
| 2734 | return 5; |
| 2735 | } |
| 2736 | |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 2737 | /* output a symbol and patch all calls to it */ |
| 2738 | virtual void gsym(int t) { |
| 2739 | int n; |
| 2740 | int pc = getPC(); |
| 2741 | while (t) { |
| 2742 | n = *(int *) t; /* next value */ |
| 2743 | *(int *) t = pc - t - 4; |
| 2744 | t = n; |
| 2745 | } |
| 2746 | } |
| 2747 | |
Jack Palevich | 9f51a26 | 2009-07-29 16:22:26 -0700 | [diff] [blame] | 2748 | /* output a symbol and patch all calls to it, using absolute address */ |
| 2749 | virtual void resolveForward(int t) { |
| 2750 | int n; |
| 2751 | int pc = getPC(); |
| 2752 | while (t) { |
| 2753 | n = *(int *) t; /* next value */ |
| 2754 | *(int *) t = pc; |
| 2755 | t = n; |
| 2756 | } |
| 2757 | } |
| 2758 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 2759 | virtual int finishCompile() { |
Jack Palevich | 8dc662e | 2009-06-09 22:53:47 +0000 | [diff] [blame] | 2760 | size_t pagesize = 4096; |
| 2761 | size_t base = (size_t) getBase() & ~ (pagesize - 1); |
| 2762 | size_t top = ((size_t) getPC() + pagesize - 1) & ~ (pagesize - 1); |
| 2763 | int err = mprotect((void*) base, top - base, PROT_READ | PROT_WRITE | PROT_EXEC); |
| 2764 | if (err) { |
| 2765 | error("mprotect() failed: %d", errno); |
| 2766 | } |
| 2767 | return err; |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 2768 | } |
| 2769 | |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 2770 | /** |
Jack Palevich | 9cbd226 | 2009-07-08 16:48:41 -0700 | [diff] [blame] | 2771 | * Alignment (in bytes) for this type of data |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 2772 | */ |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 2773 | virtual size_t alignmentOf(Type* pType){ |
Jack Palevich | 7fcdf1c | 2009-07-23 18:56:20 -0700 | [diff] [blame] | 2774 | switch (pType->tag) { |
| 2775 | case TY_CHAR: |
| 2776 | return 1; |
Jack Palevich | c9b8ffc | 2009-08-03 14:42:57 -0700 | [diff] [blame] | 2777 | case TY_SHORT: |
| 2778 | return 2; |
Jack Palevich | b615450 | 2009-08-04 14:56:09 -0700 | [diff] [blame] | 2779 | case TY_ARRAY: |
| 2780 | return alignmentOf(pType->pHead); |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 2781 | case TY_STRUCT: |
| 2782 | return pType->pHead->alignment & 0x7fffffff; |
Jack Palevich | b615450 | 2009-08-04 14:56:09 -0700 | [diff] [blame] | 2783 | case TY_FUNC: |
| 2784 | error("alignment of func not supported"); |
| 2785 | return 1; |
Jack Palevich | 7fcdf1c | 2009-07-23 18:56:20 -0700 | [diff] [blame] | 2786 | default: |
| 2787 | return 4; |
| 2788 | } |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 2789 | } |
| 2790 | |
| 2791 | /** |
| 2792 | * Array element alignment (in bytes) for this type of data. |
| 2793 | */ |
| 2794 | virtual size_t sizeOf(Type* pType){ |
| 2795 | switch(pType->tag) { |
| 2796 | case TY_INT: |
| 2797 | return 4; |
Jack Palevich | c9b8ffc | 2009-08-03 14:42:57 -0700 | [diff] [blame] | 2798 | case TY_SHORT: |
| 2799 | return 2; |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 2800 | case TY_CHAR: |
| 2801 | return 1; |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 2802 | case TY_FLOAT: |
| 2803 | return 4; |
| 2804 | case TY_DOUBLE: |
| 2805 | return 8; |
| 2806 | case TY_POINTER: |
| 2807 | return 4; |
Jack Palevich | b615450 | 2009-08-04 14:56:09 -0700 | [diff] [blame] | 2808 | case TY_ARRAY: |
| 2809 | return pType->length * sizeOf(pType->pHead); |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 2810 | case TY_STRUCT: |
| 2811 | return pType->pHead->length; |
Jack Palevich | b615450 | 2009-08-04 14:56:09 -0700 | [diff] [blame] | 2812 | default: |
| 2813 | error("Unsupported type %d", pType->tag); |
| 2814 | return 0; |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 2815 | } |
| 2816 | } |
| 2817 | |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2818 | private: |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 2819 | |
| 2820 | /** Output 1 to 4 bytes. |
| 2821 | * |
| 2822 | */ |
| 2823 | void o(int n) { |
| 2824 | /* cannot use unsigned, so we must do a hack */ |
| 2825 | while (n && n != -1) { |
| 2826 | ob(n & 0xff); |
| 2827 | n = n >> 8; |
| 2828 | } |
| 2829 | } |
| 2830 | |
Jack Palevich | 2a4e1a9 | 2009-07-09 13:34:25 -0700 | [diff] [blame] | 2831 | /* Output exactly 2 bytes |
| 2832 | */ |
| 2833 | void o2(int n) { |
| 2834 | ob(n & 0xff); |
| 2835 | ob(0xff & (n >> 8)); |
| 2836 | } |
| 2837 | |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 2838 | /* psym is used to put an instruction with a data field which is a |
| 2839 | reference to a symbol. It is in fact the same as oad ! */ |
| 2840 | int psym(int n, int t) { |
| 2841 | return oad(n, t); |
| 2842 | } |
| 2843 | |
| 2844 | /* instruction + address */ |
| 2845 | int oad(int n, int t) { |
| 2846 | o(n); |
| 2847 | int result = getPC(); |
| 2848 | o4(t); |
| 2849 | return result; |
| 2850 | } |
| 2851 | |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 2852 | static const int operatorHelper[]; |
| 2853 | |
| 2854 | int decodeOp(int op) { |
| 2855 | if (op < 0 || op > OP_COUNT) { |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 2856 | error("Out-of-range operator: %d\n", op); |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 2857 | op = 0; |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 2858 | } |
| 2859 | return operatorHelper[op]; |
| 2860 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2861 | |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 2862 | void gmov(int l, int t) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2863 | o(l + 0x83); |
Jack Palevich | 8dc662e | 2009-06-09 22:53:47 +0000 | [diff] [blame] | 2864 | oad((t > -LOCAL && t < LOCAL) << 7 | 5, t); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2865 | } |
Jack Palevich | a39749f | 2009-07-08 20:40:31 -0700 | [diff] [blame] | 2866 | |
| 2867 | void setupFloatOperands() { |
| 2868 | Type* pR0Type = getR0Type(); |
| 2869 | Type* pTOSType = getTOSType(); |
| 2870 | TypeTag tagR0 = pR0Type->tag; |
| 2871 | TypeTag tagTOS = pTOSType->tag; |
| 2872 | bool isFloatR0 = isFloatTag(tagR0); |
| 2873 | bool isFloatTOS = isFloatTag(tagTOS); |
| 2874 | if (! isFloatR0) { |
| 2875 | // Convert R0 from int to float |
| 2876 | o(0x50); // push %eax |
| 2877 | o(0x2404DB); // fildl 0(%esp) |
| 2878 | o(0x58); // pop %eax |
| 2879 | } |
| 2880 | if (! isFloatTOS){ |
| 2881 | o(0x2404DB); // fildl 0(%esp); |
| 2882 | o(0x58); // pop %eax |
| 2883 | } else { |
| 2884 | if (tagTOS == TY_FLOAT) { |
| 2885 | o(0x2404d9); // flds (%esp) |
| 2886 | o(0x58); // pop %eax |
| 2887 | } else { |
| 2888 | o(0x2404dd); // fldl (%esp) |
| 2889 | o(0x58); // pop %eax |
| 2890 | o(0x58); // pop %eax |
| 2891 | } |
| 2892 | } |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 2893 | popType(); |
Jack Palevich | a39749f | 2009-07-08 20:40:31 -0700 | [diff] [blame] | 2894 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 2895 | }; |
| 2896 | |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 2897 | #endif // PROVIDE_X86_CODEGEN |
| 2898 | |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 2899 | #ifdef PROVIDE_TRACE_CODEGEN |
| 2900 | class TraceCodeGenerator : public CodeGenerator { |
| 2901 | private: |
| 2902 | CodeGenerator* mpBase; |
| 2903 | |
| 2904 | public: |
| 2905 | TraceCodeGenerator(CodeGenerator* pBase) { |
| 2906 | mpBase = pBase; |
| 2907 | } |
| 2908 | |
| 2909 | virtual ~TraceCodeGenerator() { |
| 2910 | delete mpBase; |
| 2911 | } |
| 2912 | |
| 2913 | virtual void init(CodeBuf* pCodeBuf) { |
| 2914 | mpBase->init(pCodeBuf); |
| 2915 | } |
| 2916 | |
| 2917 | void setErrorSink(ErrorSink* pErrorSink) { |
| 2918 | mpBase->setErrorSink(pErrorSink); |
| 2919 | } |
| 2920 | |
| 2921 | /* returns address to patch with local variable size |
| 2922 | */ |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 2923 | virtual int functionEntry(Type* pDecl) { |
| 2924 | int result = mpBase->functionEntry(pDecl); |
| 2925 | fprintf(stderr, "functionEntry(pDecl) -> %d\n", result); |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 2926 | return result; |
| 2927 | } |
| 2928 | |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 2929 | virtual void functionExit(Type* pDecl, int localVariableAddress, int localVariableSize) { |
| 2930 | fprintf(stderr, "functionExit(pDecl, %d, %d)\n", |
| 2931 | localVariableAddress, localVariableSize); |
| 2932 | mpBase->functionExit(pDecl, localVariableAddress, localVariableSize); |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 2933 | } |
| 2934 | |
| 2935 | /* load immediate value */ |
Jack Palevich | 58c30ee | 2009-07-17 16:35:23 -0700 | [diff] [blame] | 2936 | virtual void li(int t) { |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 2937 | fprintf(stderr, "li(%d)\n", t); |
Jack Palevich | 58c30ee | 2009-07-17 16:35:23 -0700 | [diff] [blame] | 2938 | mpBase->li(t); |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 2939 | } |
| 2940 | |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 2941 | virtual void loadFloat(int address, Type* pType) { |
Jack Palevich | 8148c5b | 2009-07-16 18:24:47 -0700 | [diff] [blame] | 2942 | fprintf(stderr, "loadFloat(%d, type=%d)\n", address, pType->tag); |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 2943 | mpBase->loadFloat(address, pType); |
Jack Palevich | 1aeb87b | 2009-07-06 18:33:20 -0700 | [diff] [blame] | 2944 | } |
| 2945 | |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 2946 | virtual void addStructOffsetR0(int offset, Type* pType) { |
| 2947 | fprintf(stderr, "addStructOffsetR0(%d, type=%d)\n", offset, pType->tag); |
| 2948 | mpBase->addStructOffsetR0(offset, pType); |
| 2949 | } |
| 2950 | |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 2951 | virtual int gjmp(int t) { |
| 2952 | int result = mpBase->gjmp(t); |
| 2953 | fprintf(stderr, "gjmp(%d) = %d\n", t, result); |
| 2954 | return result; |
| 2955 | } |
| 2956 | |
| 2957 | /* l = 0: je, l == 1: jne */ |
| 2958 | virtual int gtst(bool l, int t) { |
| 2959 | int result = mpBase->gtst(l, t); |
| 2960 | fprintf(stderr, "gtst(%d,%d) = %d\n", l, t, result); |
| 2961 | return result; |
| 2962 | } |
| 2963 | |
Jack Palevich | 58c30ee | 2009-07-17 16:35:23 -0700 | [diff] [blame] | 2964 | virtual void gcmp(int op) { |
| 2965 | fprintf(stderr, "gcmp(%d)\n", op); |
| 2966 | mpBase->gcmp(op); |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 2967 | } |
| 2968 | |
| 2969 | virtual void genOp(int op) { |
| 2970 | fprintf(stderr, "genOp(%d)\n", op); |
| 2971 | mpBase->genOp(op); |
| 2972 | } |
| 2973 | |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 2974 | |
Jack Palevich | 58c30ee | 2009-07-17 16:35:23 -0700 | [diff] [blame] | 2975 | virtual void gUnaryCmp(int op) { |
| 2976 | fprintf(stderr, "gUnaryCmp(%d)\n", op); |
| 2977 | mpBase->gUnaryCmp(op); |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 2978 | } |
| 2979 | |
| 2980 | virtual void genUnaryOp(int op) { |
| 2981 | fprintf(stderr, "genUnaryOp(%d)\n", op); |
| 2982 | mpBase->genUnaryOp(op); |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 2983 | } |
| 2984 | |
| 2985 | virtual void pushR0() { |
| 2986 | fprintf(stderr, "pushR0()\n"); |
| 2987 | mpBase->pushR0(); |
| 2988 | } |
| 2989 | |
Jack Palevich | ddf7c9c | 2009-07-29 10:28:18 -0700 | [diff] [blame] | 2990 | virtual void over() { |
| 2991 | fprintf(stderr, "over()\n"); |
| 2992 | mpBase->over(); |
| 2993 | } |
| 2994 | |
Jack Palevich | 58c30ee | 2009-07-17 16:35:23 -0700 | [diff] [blame] | 2995 | virtual void popR0() { |
| 2996 | fprintf(stderr, "popR0()\n"); |
| 2997 | mpBase->popR0(); |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 2998 | } |
| 2999 | |
Jack Palevich | 58c30ee | 2009-07-17 16:35:23 -0700 | [diff] [blame] | 3000 | virtual void storeR0ToTOS() { |
| 3001 | fprintf(stderr, "storeR0ToTOS()\n"); |
| 3002 | mpBase->storeR0ToTOS(); |
| 3003 | } |
| 3004 | |
| 3005 | virtual void loadR0FromR0() { |
| 3006 | fprintf(stderr, "loadR0FromR0()\n"); |
| 3007 | mpBase->loadR0FromR0(); |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 3008 | } |
| 3009 | |
Jack Palevich | b5e3331 | 2009-07-30 19:06:34 -0700 | [diff] [blame] | 3010 | virtual void leaR0(int ea, Type* pPointerType, ExpressionType et) { |
| 3011 | fprintf(stderr, "leaR0(%d, %d, %d)\n", ea, |
| 3012 | pPointerType->pHead->tag, et); |
| 3013 | mpBase->leaR0(ea, pPointerType, et); |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 3014 | } |
| 3015 | |
Jack Palevich | 9f51a26 | 2009-07-29 16:22:26 -0700 | [diff] [blame] | 3016 | virtual int leaForward(int ea, Type* pPointerType) { |
| 3017 | fprintf(stderr, "leaForward(%d)\n", ea); |
| 3018 | return mpBase->leaForward(ea, pPointerType); |
| 3019 | } |
| 3020 | |
Jack Palevich | 30321cb | 2009-08-20 15:34:23 -0700 | [diff] [blame] | 3021 | virtual void convertR0Imp(Type* pType, bool isCast){ |
| 3022 | fprintf(stderr, "convertR0(pType tag=%d, %d)\n", pType->tag, isCast); |
| 3023 | mpBase->convertR0Imp(pType, isCast); |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 3024 | } |
| 3025 | |
| 3026 | virtual int beginFunctionCallArguments() { |
| 3027 | int result = mpBase->beginFunctionCallArguments(); |
| 3028 | fprintf(stderr, "beginFunctionCallArguments() = %d\n", result); |
| 3029 | return result; |
| 3030 | } |
| 3031 | |
Jack Palevich | 8148c5b | 2009-07-16 18:24:47 -0700 | [diff] [blame] | 3032 | virtual size_t storeR0ToArg(int l, Type* pArgType) { |
| 3033 | fprintf(stderr, "storeR0ToArg(%d, pArgType=%d)\n", l, |
| 3034 | pArgType->tag); |
| 3035 | return mpBase->storeR0ToArg(l, pArgType); |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 3036 | } |
| 3037 | |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 3038 | virtual void endFunctionCallArguments(Type* pDecl, int a, int l) { |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 3039 | fprintf(stderr, "endFunctionCallArguments(%d, %d)\n", a, l); |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 3040 | mpBase->endFunctionCallArguments(pDecl, a, l); |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 3041 | } |
| 3042 | |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 3043 | virtual int callForward(int symbol, Type* pFunc) { |
| 3044 | int result = mpBase->callForward(symbol, pFunc); |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 3045 | fprintf(stderr, "callForward(%d) = %d\n", symbol, result); |
| 3046 | return result; |
| 3047 | } |
| 3048 | |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 3049 | virtual void callIndirect(int l, Type* pFunc) { |
Jack Palevich | 9f51a26 | 2009-07-29 16:22:26 -0700 | [diff] [blame] | 3050 | fprintf(stderr, "callIndirect(%d returntype = %d)\n", l, |
| 3051 | pFunc->pHead->tag); |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 3052 | mpBase->callIndirect(l, pFunc); |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 3053 | } |
| 3054 | |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 3055 | virtual void adjustStackAfterCall(Type* pDecl, int l, bool isIndirect) { |
| 3056 | fprintf(stderr, "adjustStackAfterCall(pType, %d, %d)\n", l, isIndirect); |
| 3057 | mpBase->adjustStackAfterCall(pDecl, l, isIndirect); |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 3058 | } |
| 3059 | |
| 3060 | virtual int jumpOffset() { |
| 3061 | return mpBase->jumpOffset(); |
| 3062 | } |
| 3063 | |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 3064 | /* output a symbol and patch all calls to it */ |
| 3065 | virtual void gsym(int t) { |
| 3066 | fprintf(stderr, "gsym(%d)\n", t); |
| 3067 | mpBase->gsym(t); |
| 3068 | } |
| 3069 | |
Jack Palevich | 9f51a26 | 2009-07-29 16:22:26 -0700 | [diff] [blame] | 3070 | virtual void resolveForward(int t) { |
| 3071 | mpBase->resolveForward(t); |
| 3072 | } |
| 3073 | |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 3074 | virtual int finishCompile() { |
| 3075 | int result = mpBase->finishCompile(); |
| 3076 | fprintf(stderr, "finishCompile() = %d\n", result); |
| 3077 | return result; |
| 3078 | } |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 3079 | |
| 3080 | /** |
Jack Palevich | 9cbd226 | 2009-07-08 16:48:41 -0700 | [diff] [blame] | 3081 | * Alignment (in bytes) for this type of data |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 3082 | */ |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 3083 | virtual size_t alignmentOf(Type* pType){ |
| 3084 | return mpBase->alignmentOf(pType); |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 3085 | } |
| 3086 | |
| 3087 | /** |
| 3088 | * Array element alignment (in bytes) for this type of data. |
| 3089 | */ |
| 3090 | virtual size_t sizeOf(Type* pType){ |
| 3091 | return mpBase->sizeOf(pType); |
| 3092 | } |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 3093 | |
| 3094 | virtual Type* getR0Type() { |
| 3095 | return mpBase->getR0Type(); |
| 3096 | } |
Jack Palevich | ddf7c9c | 2009-07-29 10:28:18 -0700 | [diff] [blame] | 3097 | |
| 3098 | virtual ExpressionType getR0ExpressionType() { |
| 3099 | return mpBase->getR0ExpressionType(); |
| 3100 | } |
| 3101 | |
| 3102 | virtual void setR0ExpressionType(ExpressionType et) { |
| 3103 | mpBase->setR0ExpressionType(et); |
| 3104 | } |
| 3105 | |
| 3106 | virtual size_t getExpressionStackDepth() { |
| 3107 | return mpBase->getExpressionStackDepth(); |
| 3108 | } |
Jack Palevich | b5e3331 | 2009-07-30 19:06:34 -0700 | [diff] [blame] | 3109 | |
| 3110 | virtual void forceR0RVal() { |
| 3111 | return mpBase->forceR0RVal(); |
| 3112 | } |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 3113 | }; |
| 3114 | |
| 3115 | #endif // PROVIDE_TRACE_CODEGEN |
| 3116 | |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3117 | class Arena { |
| 3118 | public: |
| 3119 | // Used to record a given allocation amount. |
| 3120 | // Used: |
| 3121 | // Mark mark = arena.mark(); |
| 3122 | // ... lots of arena.allocate() |
| 3123 | // arena.free(mark); |
| 3124 | |
| 3125 | struct Mark { |
| 3126 | size_t chunk; |
| 3127 | size_t offset; |
| 3128 | }; |
| 3129 | |
| 3130 | Arena() { |
| 3131 | mCurrentChunk = 0; |
| 3132 | Chunk start(CHUNK_SIZE); |
| 3133 | mData.push_back(start); |
| 3134 | } |
| 3135 | |
| 3136 | ~Arena() { |
| 3137 | for(size_t i = 0; i < mData.size(); i++) { |
| 3138 | mData[i].free(); |
| 3139 | } |
| 3140 | } |
| 3141 | |
| 3142 | // Alloc using the standard alignment size safe for any variable |
| 3143 | void* alloc(size_t size) { |
| 3144 | return alloc(size, 8); |
| 3145 | } |
| 3146 | |
| 3147 | Mark mark(){ |
| 3148 | Mark result; |
| 3149 | result.chunk = mCurrentChunk; |
| 3150 | result.offset = mData[mCurrentChunk].mOffset; |
| 3151 | return result; |
| 3152 | } |
| 3153 | |
| 3154 | void freeToMark(const Mark& mark) { |
| 3155 | mCurrentChunk = mark.chunk; |
| 3156 | mData[mCurrentChunk].mOffset = mark.offset; |
| 3157 | } |
| 3158 | |
| 3159 | private: |
| 3160 | // Allocate memory aligned to a given size |
| 3161 | // and a given power-of-two-sized alignment (e.g. 1,2,4,8,...) |
| 3162 | // Memory is not zero filled. |
| 3163 | |
| 3164 | void* alloc(size_t size, size_t alignment) { |
| 3165 | while (size > mData[mCurrentChunk].remainingCapacity(alignment)) { |
| 3166 | if (mCurrentChunk + 1 < mData.size()) { |
| 3167 | mCurrentChunk++; |
| 3168 | } else { |
| 3169 | size_t allocSize = CHUNK_SIZE; |
| 3170 | if (allocSize < size + alignment - 1) { |
| 3171 | allocSize = size + alignment - 1; |
| 3172 | } |
| 3173 | Chunk chunk(allocSize); |
| 3174 | mData.push_back(chunk); |
| 3175 | mCurrentChunk++; |
| 3176 | } |
| 3177 | } |
| 3178 | return mData[mCurrentChunk].allocate(size, alignment); |
| 3179 | } |
| 3180 | |
| 3181 | static const size_t CHUNK_SIZE = 128*1024; |
| 3182 | // Note: this class does not deallocate its |
| 3183 | // memory when it's destroyed. It depends upon |
| 3184 | // its parent to deallocate the memory. |
| 3185 | struct Chunk { |
| 3186 | Chunk() { |
| 3187 | mpData = 0; |
| 3188 | mSize = 0; |
| 3189 | mOffset = 0; |
| 3190 | } |
| 3191 | |
| 3192 | Chunk(size_t size) { |
| 3193 | mSize = size; |
| 3194 | mpData = (char*) malloc(size); |
| 3195 | mOffset = 0; |
| 3196 | } |
| 3197 | |
| 3198 | ~Chunk() { |
| 3199 | // Doesn't deallocate memory. |
| 3200 | } |
| 3201 | |
| 3202 | void* allocate(size_t size, size_t alignment) { |
| 3203 | size_t alignedOffset = aligned(mOffset, alignment); |
| 3204 | void* result = mpData + alignedOffset; |
| 3205 | mOffset = alignedOffset + size; |
| 3206 | return result; |
| 3207 | } |
| 3208 | |
| 3209 | void free() { |
| 3210 | if (mpData) { |
| 3211 | ::free(mpData); |
| 3212 | mpData = 0; |
| 3213 | } |
| 3214 | } |
| 3215 | |
| 3216 | size_t remainingCapacity(size_t alignment) { |
| 3217 | return aligned(mSize, alignment) - aligned(mOffset, alignment); |
| 3218 | } |
| 3219 | |
| 3220 | // Assume alignment is a power of two |
| 3221 | inline size_t aligned(size_t v, size_t alignment) { |
| 3222 | size_t mask = alignment-1; |
| 3223 | return (v + mask) & ~mask; |
| 3224 | } |
| 3225 | |
| 3226 | char* mpData; |
| 3227 | size_t mSize; |
| 3228 | size_t mOffset; |
| 3229 | }; |
| 3230 | |
| 3231 | size_t mCurrentChunk; |
| 3232 | |
| 3233 | Vector<Chunk> mData; |
| 3234 | }; |
| 3235 | |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3236 | struct VariableInfo; |
| 3237 | |
| 3238 | struct Token { |
| 3239 | int hash; |
| 3240 | size_t length; |
| 3241 | char* pText; |
| 3242 | tokenid_t id; |
| 3243 | |
| 3244 | // Current values for the token |
| 3245 | char* mpMacroDefinition; |
| 3246 | VariableInfo* mpVariableInfo; |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 3247 | VariableInfo* mpStructInfo; |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3248 | }; |
| 3249 | |
| 3250 | class TokenTable { |
| 3251 | public: |
| 3252 | // Don't use 0..0xff, allows characters and operators to be tokens too. |
| 3253 | |
| 3254 | static const int TOKEN_BASE = 0x100; |
| 3255 | TokenTable() { |
| 3256 | mpMap = hashmapCreate(128, hashFn, equalsFn); |
| 3257 | } |
| 3258 | |
| 3259 | ~TokenTable() { |
| 3260 | hashmapFree(mpMap); |
| 3261 | } |
| 3262 | |
| 3263 | void setArena(Arena* pArena) { |
| 3264 | mpArena = pArena; |
| 3265 | } |
| 3266 | |
| 3267 | // Returns a token for a given string of characters. |
| 3268 | tokenid_t intern(const char* pText, size_t length) { |
| 3269 | Token probe; |
| 3270 | int hash = hashmapHash((void*) pText, length); |
| 3271 | { |
| 3272 | Token probe; |
| 3273 | probe.hash = hash; |
| 3274 | probe.length = length; |
| 3275 | probe.pText = (char*) pText; |
| 3276 | Token* pValue = (Token*) hashmapGet(mpMap, &probe); |
| 3277 | if (pValue) { |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3278 | return pValue->id; |
| 3279 | } |
| 3280 | } |
| 3281 | |
| 3282 | Token* pToken = (Token*) mpArena->alloc(sizeof(Token)); |
| 3283 | memset(pToken, 0, sizeof(*pToken)); |
| 3284 | pToken->hash = hash; |
| 3285 | pToken->length = length; |
| 3286 | pToken->pText = (char*) mpArena->alloc(length + 1); |
| 3287 | memcpy(pToken->pText, pText, length); |
| 3288 | pToken->pText[length] = 0; |
| 3289 | pToken->id = mTokens.size() + TOKEN_BASE; |
| 3290 | mTokens.push_back(pToken); |
| 3291 | hashmapPut(mpMap, pToken, pToken); |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3292 | return pToken->id; |
| 3293 | } |
| 3294 | |
| 3295 | // Return the Token for a given tokenid. |
| 3296 | Token& operator[](tokenid_t id) { |
| 3297 | return *mTokens[id - TOKEN_BASE]; |
| 3298 | } |
| 3299 | |
| 3300 | inline size_t size() { |
| 3301 | return mTokens.size(); |
| 3302 | } |
| 3303 | |
| 3304 | private: |
| 3305 | |
| 3306 | static int hashFn(void* pKey) { |
| 3307 | Token* pToken = (Token*) pKey; |
| 3308 | return pToken->hash; |
| 3309 | } |
| 3310 | |
| 3311 | static bool equalsFn(void* keyA, void* keyB) { |
| 3312 | Token* pTokenA = (Token*) keyA; |
| 3313 | Token* pTokenB = (Token*) keyB; |
| 3314 | // Don't need to compare hash values, they should always be equal |
| 3315 | return pTokenA->length == pTokenB->length |
| 3316 | && strcmp(pTokenA->pText, pTokenB->pText) == 0; |
| 3317 | } |
| 3318 | |
| 3319 | Hashmap* mpMap; |
| 3320 | Vector<Token*> mTokens; |
| 3321 | Arena* mpArena; |
| 3322 | }; |
| 3323 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 3324 | class InputStream { |
| 3325 | public: |
Marco Nelissen | eea5ae9 | 2009-07-08 16:59:18 -0700 | [diff] [blame] | 3326 | virtual ~InputStream() {} |
Jack Palevich | dc45646 | 2009-07-16 16:50:56 -0700 | [diff] [blame] | 3327 | virtual int getChar() = 0; |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 3328 | }; |
| 3329 | |
| 3330 | class TextInputStream : public InputStream { |
| 3331 | public: |
| 3332 | TextInputStream(const char* text, size_t textLength) |
| 3333 | : pText(text), mTextLength(textLength), mPosition(0) { |
| 3334 | } |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 3335 | |
Jack Palevich | dc45646 | 2009-07-16 16:50:56 -0700 | [diff] [blame] | 3336 | virtual int getChar() { |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 3337 | return mPosition < mTextLength ? pText[mPosition++] : EOF; |
| 3338 | } |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 3339 | |
Jack Palevich | dc45646 | 2009-07-16 16:50:56 -0700 | [diff] [blame] | 3340 | private: |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 3341 | const char* pText; |
| 3342 | size_t mTextLength; |
| 3343 | size_t mPosition; |
| 3344 | }; |
| 3345 | |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 3346 | class String { |
| 3347 | public: |
| 3348 | String() { |
| 3349 | mpBase = 0; |
| 3350 | mUsed = 0; |
| 3351 | mSize = 0; |
| 3352 | } |
| 3353 | |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 3354 | String(const char* item, int len, bool adopt) { |
| 3355 | if (len < 0) { |
| 3356 | len = strlen(item); |
| 3357 | } |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 3358 | if (adopt) { |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 3359 | mpBase = (char*) item; |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 3360 | mUsed = len; |
| 3361 | mSize = len + 1; |
| 3362 | } else { |
| 3363 | mpBase = 0; |
| 3364 | mUsed = 0; |
| 3365 | mSize = 0; |
| 3366 | appendBytes(item, len); |
| 3367 | } |
| 3368 | } |
| 3369 | |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 3370 | String(const String& other) { |
| 3371 | mpBase = 0; |
| 3372 | mUsed = 0; |
| 3373 | mSize = 0; |
| 3374 | appendBytes(other.getUnwrapped(), other.len()); |
| 3375 | } |
| 3376 | |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 3377 | ~String() { |
| 3378 | if (mpBase) { |
| 3379 | free(mpBase); |
| 3380 | } |
| 3381 | } |
| 3382 | |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 3383 | String& operator=(const String& other) { |
| 3384 | clear(); |
| 3385 | appendBytes(other.getUnwrapped(), other.len()); |
| 3386 | return *this; |
| 3387 | } |
| 3388 | |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 3389 | inline char* getUnwrapped() const { |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 3390 | return mpBase; |
| 3391 | } |
| 3392 | |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 3393 | void clear() { |
| 3394 | mUsed = 0; |
| 3395 | if (mSize > 0) { |
| 3396 | mpBase[0] = 0; |
| 3397 | } |
| 3398 | } |
| 3399 | |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 3400 | void appendCStr(const char* s) { |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 3401 | appendBytes(s, strlen(s)); |
| 3402 | } |
| 3403 | |
| 3404 | void appendBytes(const char* s, int n) { |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 3405 | memcpy(ensure(n), s, n + 1); |
| 3406 | } |
| 3407 | |
| 3408 | void append(char c) { |
| 3409 | * ensure(1) = c; |
| 3410 | } |
| 3411 | |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 3412 | void append(String& other) { |
| 3413 | appendBytes(other.getUnwrapped(), other.len()); |
| 3414 | } |
| 3415 | |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 3416 | char* orphan() { |
| 3417 | char* result = mpBase; |
| 3418 | mpBase = 0; |
| 3419 | mUsed = 0; |
| 3420 | mSize = 0; |
| 3421 | return result; |
| 3422 | } |
| 3423 | |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 3424 | void printf(const char* fmt,...) { |
| 3425 | va_list ap; |
| 3426 | va_start(ap, fmt); |
| 3427 | vprintf(fmt, ap); |
| 3428 | va_end(ap); |
| 3429 | } |
| 3430 | |
| 3431 | void vprintf(const char* fmt, va_list ap) { |
| 3432 | char* temp; |
| 3433 | int numChars = vasprintf(&temp, fmt, ap); |
| 3434 | memcpy(ensure(numChars), temp, numChars+1); |
| 3435 | free(temp); |
| 3436 | } |
| 3437 | |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 3438 | inline size_t len() const { |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 3439 | return mUsed; |
| 3440 | } |
| 3441 | |
| 3442 | private: |
| 3443 | char* ensure(int n) { |
| 3444 | size_t newUsed = mUsed + n; |
| 3445 | if (newUsed > mSize) { |
| 3446 | size_t newSize = mSize * 2 + 10; |
| 3447 | if (newSize < newUsed) { |
| 3448 | newSize = newUsed; |
| 3449 | } |
| 3450 | mpBase = (char*) realloc(mpBase, newSize + 1); |
| 3451 | mSize = newSize; |
| 3452 | } |
| 3453 | mpBase[newUsed] = '\0'; |
| 3454 | char* result = mpBase + mUsed; |
| 3455 | mUsed = newUsed; |
| 3456 | return result; |
| 3457 | } |
| 3458 | |
| 3459 | char* mpBase; |
| 3460 | size_t mUsed; |
| 3461 | size_t mSize; |
| 3462 | }; |
| 3463 | |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3464 | void internKeywords() { |
| 3465 | // Note: order has to match TOK_ constants |
| 3466 | static const char* keywords[] = { |
| 3467 | "int", |
| 3468 | "char", |
| 3469 | "void", |
| 3470 | "if", |
| 3471 | "else", |
| 3472 | "while", |
| 3473 | "break", |
| 3474 | "return", |
| 3475 | "for", |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3476 | "auto", |
| 3477 | "case", |
| 3478 | "const", |
| 3479 | "continue", |
| 3480 | "default", |
| 3481 | "do", |
| 3482 | "double", |
| 3483 | "enum", |
| 3484 | "extern", |
| 3485 | "float", |
| 3486 | "goto", |
| 3487 | "long", |
| 3488 | "register", |
| 3489 | "short", |
| 3490 | "signed", |
| 3491 | "sizeof", |
| 3492 | "static", |
| 3493 | "struct", |
| 3494 | "switch", |
| 3495 | "typedef", |
| 3496 | "union", |
| 3497 | "unsigned", |
| 3498 | "volatile", |
| 3499 | "_Bool", |
| 3500 | "_Complex", |
| 3501 | "_Imaginary", |
| 3502 | "inline", |
| 3503 | "restrict", |
Jack Palevich | dc45646 | 2009-07-16 16:50:56 -0700 | [diff] [blame] | 3504 | |
| 3505 | // predefined tokens that can also be symbols start here: |
| 3506 | "pragma", |
| 3507 | "define", |
| 3508 | "line", |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3509 | 0}; |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 3510 | |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3511 | for(int i = 0; keywords[i]; i++) { |
| 3512 | mTokenTable.intern(keywords[i], strlen(keywords[i])); |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 3513 | } |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3514 | } |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 3515 | |
Jack Palevich | 36d9414 | 2009-06-08 15:55:32 -0700 | [diff] [blame] | 3516 | struct InputState { |
| 3517 | InputStream* pStream; |
| 3518 | int oldCh; |
| 3519 | }; |
| 3520 | |
Jack Palevich | 2db168f | 2009-06-11 14:29:47 -0700 | [diff] [blame] | 3521 | struct VariableInfo { |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 3522 | void* pAddress; |
| 3523 | 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] | 3524 | tokenid_t tok; |
| 3525 | size_t level; |
| 3526 | VariableInfo* pOldDefinition; |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 3527 | Type* pType; |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 3528 | bool isStructTag; |
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(); |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 3560 | if (pV->isStructTag) { |
| 3561 | (*mpTokenTable)[pV->tok].mpStructInfo = pV->pOldDefinition; |
| 3562 | } else { |
| 3563 | (*mpTokenTable)[pV->tok].mpVariableInfo = pV->pOldDefinition; |
| 3564 | } |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 3565 | } |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3566 | mpArena->freeToMark(mark.mArenaMark); |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 3567 | } |
| 3568 | |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3569 | bool isDefinedAtCurrentLevel(tokenid_t tok) { |
| 3570 | VariableInfo* pV = (*mpTokenTable)[tok].mpVariableInfo; |
| 3571 | return pV && pV->level == level(); |
| 3572 | } |
| 3573 | |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 3574 | bool isStructTagDefinedAtCurrentLevel(tokenid_t tok) { |
| 3575 | VariableInfo* pV = (*mpTokenTable)[tok].mpStructInfo; |
| 3576 | return pV && pV->level == level(); |
| 3577 | } |
| 3578 | |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3579 | VariableInfo* add(tokenid_t tok) { |
| 3580 | Token& token = (*mpTokenTable)[tok]; |
| 3581 | VariableInfo* pOldV = token.mpVariableInfo; |
| 3582 | VariableInfo* pNewV = |
| 3583 | (VariableInfo*) mpArena->alloc(sizeof(VariableInfo)); |
| 3584 | memset(pNewV, 0, sizeof(VariableInfo)); |
| 3585 | pNewV->tok = tok; |
| 3586 | pNewV->level = level(); |
| 3587 | pNewV->pOldDefinition = pOldV; |
| 3588 | token.mpVariableInfo = pNewV; |
| 3589 | mStack.push_back(pNewV); |
| 3590 | return pNewV; |
| 3591 | } |
| 3592 | |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 3593 | VariableInfo* addStructTag(tokenid_t tok) { |
| 3594 | Token& token = (*mpTokenTable)[tok]; |
| 3595 | VariableInfo* pOldS = token.mpStructInfo; |
| 3596 | VariableInfo* pNewS = |
| 3597 | (VariableInfo*) mpArena->alloc(sizeof(VariableInfo)); |
| 3598 | memset(pNewS, 0, sizeof(VariableInfo)); |
| 3599 | pNewS->tok = tok; |
| 3600 | pNewS->level = level(); |
| 3601 | pNewS->isStructTag = true; |
| 3602 | pNewS->pOldDefinition = pOldS; |
| 3603 | token.mpStructInfo = pNewS; |
| 3604 | mStack.push_back(pNewS); |
| 3605 | return pNewS; |
| 3606 | } |
| 3607 | |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 3608 | VariableInfo* add(Type* pType) { |
| 3609 | VariableInfo* pVI = add(pType->id); |
| 3610 | pVI->pType = pType; |
| 3611 | return pVI; |
| 3612 | } |
| 3613 | |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3614 | void forEach(bool (*fn)(VariableInfo*, void*), void* context) { |
| 3615 | for (size_t i = 0; i < mStack.size(); i++) { |
| 3616 | if (! fn(mStack[i], context)) { |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 3617 | break; |
| 3618 | } |
| 3619 | } |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 3620 | } |
| 3621 | |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 3622 | private: |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3623 | inline size_t level() { |
| 3624 | return mLevelStack.size(); |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 3625 | } |
| 3626 | |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3627 | struct Mark { |
| 3628 | Arena::Mark mArenaMark; |
| 3629 | size_t mSymbolHead; |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 3630 | }; |
| 3631 | |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3632 | Arena* mpArena; |
| 3633 | TokenTable* mpTokenTable; |
| 3634 | Vector<VariableInfo*> mStack; |
| 3635 | Vector<Mark> mLevelStack; |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 3636 | }; |
Jack Palevich | 36d9414 | 2009-06-08 15:55:32 -0700 | [diff] [blame] | 3637 | |
| 3638 | int ch; // Current input character, or EOF |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3639 | tokenid_t tok; // token |
Jack Palevich | 36d9414 | 2009-06-08 15:55:32 -0700 | [diff] [blame] | 3640 | intptr_t tokc; // token extra info |
Jack Palevich | 1aeb87b | 2009-07-06 18:33:20 -0700 | [diff] [blame] | 3641 | double tokd; // floating point constant value |
Jack Palevich | 36d9414 | 2009-06-08 15:55:32 -0700 | [diff] [blame] | 3642 | int tokl; // token operator level |
| 3643 | intptr_t rsym; // return symbol |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 3644 | Type* pReturnType; // type of the current function's return. |
Jack Palevich | 36d9414 | 2009-06-08 15:55:32 -0700 | [diff] [blame] | 3645 | intptr_t loc; // local variable index |
| 3646 | char* glo; // global variable index |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 3647 | String mTokenString; |
Jack Palevich | 815d8b8 | 2009-08-18 18:25:56 -0700 | [diff] [blame] | 3648 | bool mbSuppressMacroExpansion; |
Jack Palevich | 36d9414 | 2009-06-08 15:55:32 -0700 | [diff] [blame] | 3649 | char* dptr; // Macro state: Points to macro text during macro playback. |
| 3650 | 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] | 3651 | char* pGlobalBase; |
Jack Palevich | 8c246a9 | 2009-07-14 21:14:10 -0700 | [diff] [blame] | 3652 | ACCSymbolLookupFn mpSymbolLookupFn; |
| 3653 | void* mpSymbolLookupContext; |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3654 | |
| 3655 | // Arena for the duration of the compile |
| 3656 | Arena mGlobalArena; |
| 3657 | // Arena for data that's only needed when compiling a single function |
| 3658 | Arena mLocalArena; |
| 3659 | |
Jack Palevich | 2ff5c22 | 2009-07-23 15:11:22 -0700 | [diff] [blame] | 3660 | Arena* mpCurrentArena; |
| 3661 | |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3662 | TokenTable mTokenTable; |
| 3663 | SymbolStack mGlobals; |
| 3664 | SymbolStack mLocals; |
| 3665 | |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 3666 | SymbolStack* mpCurrentSymbolStack; |
| 3667 | |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 3668 | // Prebuilt types, makes things slightly faster. |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 3669 | Type* mkpInt; // int |
Jack Palevich | c9b8ffc | 2009-08-03 14:42:57 -0700 | [diff] [blame] | 3670 | Type* mkpShort; // short |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 3671 | Type* mkpChar; // char |
| 3672 | Type* mkpVoid; // void |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 3673 | Type* mkpFloat; |
| 3674 | Type* mkpDouble; |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 3675 | Type* mkpIntFn; |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 3676 | Type* mkpIntPtr; |
| 3677 | Type* mkpCharPtr; |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 3678 | Type* mkpFloatPtr; |
| 3679 | Type* mkpDoublePtr; |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 3680 | Type* mkpPtrIntFn; |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 3681 | |
Jack Palevich | 36d9414 | 2009-06-08 15:55:32 -0700 | [diff] [blame] | 3682 | InputStream* file; |
Jack Palevich | dc45646 | 2009-07-16 16:50:56 -0700 | [diff] [blame] | 3683 | int mLineNumber; |
| 3684 | bool mbBumpLine; |
Jack Palevich | 36d9414 | 2009-06-08 15:55:32 -0700 | [diff] [blame] | 3685 | |
| 3686 | CodeBuf codeBuf; |
| 3687 | CodeGenerator* pGen; |
| 3688 | |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 3689 | String mErrorBuf; |
| 3690 | |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 3691 | String mPragmas; |
| 3692 | int mPragmaStringCount; |
Jack Palevich | ce105a9 | 2009-07-16 14:30:33 -0700 | [diff] [blame] | 3693 | int mCompileResult; |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 3694 | |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3695 | static const int ALLOC_SIZE = 99999; |
| 3696 | |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 3697 | static const int TOK_DUMMY = 1; |
| 3698 | static const int TOK_NUM = 2; |
Jack Palevich | 1aeb87b | 2009-07-06 18:33:20 -0700 | [diff] [blame] | 3699 | static const int TOK_NUM_FLOAT = 3; |
| 3700 | static const int TOK_NUM_DOUBLE = 4; |
Jack Palevich | 0c01774 | 2009-07-31 12:00:39 -0700 | [diff] [blame] | 3701 | static const int TOK_OP_ASSIGNMENT = 5; |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 3702 | static const int TOK_OP_ARROW = 6; |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 3703 | |
| 3704 | // 3..255 are character and/or operators |
| 3705 | |
Jack Palevich | 2db168f | 2009-06-11 14:29:47 -0700 | [diff] [blame] | 3706 | // Keywords start at 0x100 and increase by 1 |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3707 | // Order has to match string list in "internKeywords". |
| 3708 | enum { |
| 3709 | TOK_KEYWORD = TokenTable::TOKEN_BASE, |
| 3710 | TOK_INT = TOK_KEYWORD, |
| 3711 | TOK_CHAR, |
| 3712 | TOK_VOID, |
| 3713 | TOK_IF, |
| 3714 | TOK_ELSE, |
| 3715 | TOK_WHILE, |
| 3716 | TOK_BREAK, |
| 3717 | TOK_RETURN, |
| 3718 | TOK_FOR, |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3719 | TOK_AUTO, |
| 3720 | TOK_CASE, |
| 3721 | TOK_CONST, |
| 3722 | TOK_CONTINUE, |
| 3723 | TOK_DEFAULT, |
| 3724 | TOK_DO, |
| 3725 | TOK_DOUBLE, |
| 3726 | TOK_ENUM, |
| 3727 | TOK_EXTERN, |
| 3728 | TOK_FLOAT, |
| 3729 | TOK_GOTO, |
| 3730 | TOK_LONG, |
| 3731 | TOK_REGISTER, |
| 3732 | TOK_SHORT, |
| 3733 | TOK_SIGNED, |
| 3734 | TOK_SIZEOF, |
| 3735 | TOK_STATIC, |
| 3736 | TOK_STRUCT, |
| 3737 | TOK_SWITCH, |
| 3738 | TOK_TYPEDEF, |
| 3739 | TOK_UNION, |
| 3740 | TOK_UNSIGNED, |
| 3741 | TOK_VOLATILE, |
| 3742 | TOK__BOOL, |
| 3743 | TOK__COMPLEX, |
| 3744 | TOK__IMAGINARY, |
| 3745 | TOK_INLINE, |
| 3746 | TOK_RESTRICT, |
Jack Palevich | dc45646 | 2009-07-16 16:50:56 -0700 | [diff] [blame] | 3747 | |
| 3748 | // Symbols start after keywords |
| 3749 | |
| 3750 | TOK_SYMBOL, |
| 3751 | TOK_PRAGMA = TOK_SYMBOL, |
| 3752 | TOK_DEFINE, |
| 3753 | TOK_LINE |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3754 | }; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3755 | |
| 3756 | static const int LOCAL = 0x200; |
| 3757 | |
| 3758 | static const int SYM_FORWARD = 0; |
| 3759 | static const int SYM_DEFINE = 1; |
| 3760 | |
| 3761 | /* tokens in string heap */ |
| 3762 | static const int TAG_TOK = ' '; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3763 | |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 3764 | static const int OP_INCREMENT = 0; |
| 3765 | static const int OP_DECREMENT = 1; |
| 3766 | static const int OP_MUL = 2; |
| 3767 | static const int OP_DIV = 3; |
| 3768 | static const int OP_MOD = 4; |
| 3769 | static const int OP_PLUS = 5; |
| 3770 | static const int OP_MINUS = 6; |
| 3771 | static const int OP_SHIFT_LEFT = 7; |
| 3772 | static const int OP_SHIFT_RIGHT = 8; |
| 3773 | static const int OP_LESS_EQUAL = 9; |
| 3774 | static const int OP_GREATER_EQUAL = 10; |
| 3775 | static const int OP_LESS = 11; |
| 3776 | static const int OP_GREATER = 12; |
| 3777 | static const int OP_EQUALS = 13; |
| 3778 | static const int OP_NOT_EQUALS = 14; |
| 3779 | static const int OP_LOGICAL_AND = 15; |
| 3780 | static const int OP_LOGICAL_OR = 16; |
| 3781 | static const int OP_BIT_AND = 17; |
| 3782 | static const int OP_BIT_XOR = 18; |
| 3783 | static const int OP_BIT_OR = 19; |
| 3784 | static const int OP_BIT_NOT = 20; |
| 3785 | static const int OP_LOGICAL_NOT = 21; |
| 3786 | static const int OP_COUNT = 22; |
| 3787 | |
| 3788 | /* Operators are searched from front, the two-character operators appear |
| 3789 | * before the single-character operators with the same first character. |
| 3790 | * @ is used to pad out single-character operators. |
| 3791 | */ |
| 3792 | static const char* operatorChars; |
| 3793 | static const char operatorLevel[]; |
| 3794 | |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3795 | /* Called when we detect an internal problem. Does nothing in production. |
| 3796 | * |
| 3797 | */ |
| 3798 | void internalError() { |
| 3799 | * (char*) 0 = 0; |
| 3800 | } |
| 3801 | |
Jack Palevich | 7f5b1a2 | 2009-08-17 16:54:56 -0700 | [diff] [blame] | 3802 | void assertImpl(bool isTrue, int line) { |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 3803 | if (!isTrue) { |
Joe Onorato | ecfd8e7 | 2009-08-28 09:26:31 -0700 | [diff] [blame] | 3804 | LOGD("%d: assertion failed at line %s:%d.", mLineNumber, __FILE__, line); |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3805 | internalError(); |
| 3806 | } |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 3807 | } |
| 3808 | |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 3809 | bool isSymbol(tokenid_t t) { |
| 3810 | return t >= TOK_SYMBOL && |
| 3811 | ((size_t) (t-TOK_SYMBOL)) < mTokenTable.size(); |
| 3812 | } |
| 3813 | |
| 3814 | bool isSymbolOrKeyword(tokenid_t t) { |
| 3815 | return t >= TOK_KEYWORD && |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 3816 | ((size_t) (t-TOK_KEYWORD)) < mTokenTable.size(); |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 3817 | } |
| 3818 | |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 3819 | VariableInfo* VI(tokenid_t t) { |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 3820 | assert(isSymbol(t)); |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3821 | VariableInfo* pV = mTokenTable[t].mpVariableInfo; |
| 3822 | if (pV && pV->tok != t) { |
| 3823 | internalError(); |
| 3824 | } |
| 3825 | return pV; |
| 3826 | } |
| 3827 | |
| 3828 | inline bool isDefined(tokenid_t t) { |
| 3829 | return t >= TOK_SYMBOL && VI(t) != 0; |
| 3830 | } |
| 3831 | |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 3832 | const char* nameof(tokenid_t t) { |
| 3833 | assert(isSymbolOrKeyword(t)); |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 3834 | return mTokenTable[t].pText; |
| 3835 | } |
| 3836 | |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3837 | void pdef(int t) { |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 3838 | mTokenString.append(t); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3839 | } |
| 3840 | |
| 3841 | void inp() { |
| 3842 | if (dptr) { |
Jack Palevich | 653f42d | 2009-05-28 17:15:32 -0700 | [diff] [blame] | 3843 | ch = *dptr++; |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 3844 | if (ch == 0) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3845 | dptr = 0; |
| 3846 | ch = dch; |
| 3847 | } |
Jack Palevich | dc45646 | 2009-07-16 16:50:56 -0700 | [diff] [blame] | 3848 | } else { |
| 3849 | if (mbBumpLine) { |
| 3850 | mLineNumber++; |
| 3851 | mbBumpLine = false; |
| 3852 | } |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 3853 | ch = file->getChar(); |
Jack Palevich | dc45646 | 2009-07-16 16:50:56 -0700 | [diff] [blame] | 3854 | if (ch == '\n') { |
| 3855 | mbBumpLine = true; |
| 3856 | } |
| 3857 | } |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 3858 | #if 0 |
| 3859 | printf("ch='%c' 0x%x\n", ch, ch); |
| 3860 | #endif |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3861 | } |
| 3862 | |
| 3863 | int isid() { |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 3864 | return isalnum(ch) | (ch == '_'); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3865 | } |
| 3866 | |
Jack Palevich | 2aaf21f | 2009-07-15 16:16:37 -0700 | [diff] [blame] | 3867 | int decodeHex(int c) { |
| 3868 | if (isdigit(c)) { |
| 3869 | c -= '0'; |
| 3870 | } else if (c <= 'F') { |
| 3871 | c = c - 'A' + 10; |
| 3872 | } else { |
| 3873 | c =c - 'a' + 10; |
| 3874 | } |
| 3875 | return c; |
| 3876 | } |
| 3877 | |
Jack Palevich | b4758ff | 2009-06-12 12:49:14 -0700 | [diff] [blame] | 3878 | /* read a character constant, advances ch to after end of constant */ |
| 3879 | int getq() { |
| 3880 | int val = ch; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3881 | if (ch == '\\') { |
| 3882 | inp(); |
Jack Palevich | b4758ff | 2009-06-12 12:49:14 -0700 | [diff] [blame] | 3883 | if (isoctal(ch)) { |
| 3884 | // 1 to 3 octal characters. |
| 3885 | val = 0; |
| 3886 | for(int i = 0; i < 3; i++) { |
| 3887 | if (isoctal(ch)) { |
| 3888 | val = (val << 3) + ch - '0'; |
| 3889 | inp(); |
| 3890 | } |
| 3891 | } |
| 3892 | return val; |
| 3893 | } else if (ch == 'x' || ch == 'X') { |
| 3894 | // N hex chars |
| 3895 | inp(); |
| 3896 | if (! isxdigit(ch)) { |
| 3897 | error("'x' character escape requires at least one digit."); |
| 3898 | } else { |
| 3899 | val = 0; |
| 3900 | while (isxdigit(ch)) { |
Jack Palevich | 2aaf21f | 2009-07-15 16:16:37 -0700 | [diff] [blame] | 3901 | val = (val << 4) + decodeHex(ch); |
Jack Palevich | b4758ff | 2009-06-12 12:49:14 -0700 | [diff] [blame] | 3902 | inp(); |
| 3903 | } |
| 3904 | } |
| 3905 | } else { |
| 3906 | int val = ch; |
| 3907 | switch (ch) { |
| 3908 | case 'a': |
| 3909 | val = '\a'; |
| 3910 | break; |
| 3911 | case 'b': |
| 3912 | val = '\b'; |
| 3913 | break; |
| 3914 | case 'f': |
| 3915 | val = '\f'; |
| 3916 | break; |
| 3917 | case 'n': |
| 3918 | val = '\n'; |
| 3919 | break; |
| 3920 | case 'r': |
| 3921 | val = '\r'; |
| 3922 | break; |
| 3923 | case 't': |
| 3924 | val = '\t'; |
| 3925 | break; |
| 3926 | case 'v': |
| 3927 | val = '\v'; |
| 3928 | break; |
| 3929 | case '\\': |
| 3930 | val = '\\'; |
| 3931 | break; |
| 3932 | case '\'': |
| 3933 | val = '\''; |
| 3934 | break; |
| 3935 | case '"': |
| 3936 | val = '"'; |
| 3937 | break; |
| 3938 | case '?': |
| 3939 | val = '?'; |
| 3940 | break; |
| 3941 | default: |
| 3942 | error("Undefined character escape %c", ch); |
| 3943 | break; |
| 3944 | } |
| 3945 | inp(); |
| 3946 | return val; |
| 3947 | } |
| 3948 | } else { |
| 3949 | inp(); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3950 | } |
Jack Palevich | b4758ff | 2009-06-12 12:49:14 -0700 | [diff] [blame] | 3951 | return val; |
| 3952 | } |
| 3953 | |
| 3954 | static bool isoctal(int ch) { |
| 3955 | return ch >= '0' && ch <= '7'; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 3956 | } |
| 3957 | |
Jack Palevich | 1aeb87b | 2009-07-06 18:33:20 -0700 | [diff] [blame] | 3958 | bool acceptCh(int c) { |
| 3959 | bool result = c == ch; |
| 3960 | if (result) { |
| 3961 | pdef(ch); |
| 3962 | inp(); |
| 3963 | } |
| 3964 | return result; |
| 3965 | } |
| 3966 | |
| 3967 | bool acceptDigitsCh() { |
| 3968 | bool result = false; |
| 3969 | while (isdigit(ch)) { |
| 3970 | result = true; |
| 3971 | pdef(ch); |
| 3972 | inp(); |
| 3973 | } |
| 3974 | return result; |
| 3975 | } |
| 3976 | |
| 3977 | void parseFloat() { |
| 3978 | tok = TOK_NUM_DOUBLE; |
| 3979 | // mTokenString already has the integral part of the number. |
Jack Palevich | 2aaf21f | 2009-07-15 16:16:37 -0700 | [diff] [blame] | 3980 | if(mTokenString.len() == 0) { |
| 3981 | mTokenString.append('0'); |
| 3982 | } |
Jack Palevich | 1aeb87b | 2009-07-06 18:33:20 -0700 | [diff] [blame] | 3983 | acceptCh('.'); |
| 3984 | acceptDigitsCh(); |
Jack Palevich | 1aeb87b | 2009-07-06 18:33:20 -0700 | [diff] [blame] | 3985 | if (acceptCh('e') || acceptCh('E')) { |
Jack Palevich | 2aaf21f | 2009-07-15 16:16:37 -0700 | [diff] [blame] | 3986 | acceptCh('-') || acceptCh('+'); |
| 3987 | acceptDigitsCh(); |
Jack Palevich | 1aeb87b | 2009-07-06 18:33:20 -0700 | [diff] [blame] | 3988 | } |
Jack Palevich | 2aaf21f | 2009-07-15 16:16:37 -0700 | [diff] [blame] | 3989 | if (ch == 'f' || ch == 'F') { |
| 3990 | tok = TOK_NUM_FLOAT; |
| 3991 | inp(); |
| 3992 | } else if (ch == 'l' || ch == 'L') { |
| 3993 | inp(); |
| 3994 | error("Long floating point constants not supported."); |
Jack Palevich | 1aeb87b | 2009-07-06 18:33:20 -0700 | [diff] [blame] | 3995 | } |
| 3996 | char* pText = mTokenString.getUnwrapped(); |
Jack Palevich | 2aaf21f | 2009-07-15 16:16:37 -0700 | [diff] [blame] | 3997 | char* pEnd = pText + strlen(pText); |
| 3998 | char* pEndPtr = 0; |
| 3999 | errno = 0; |
Jack Palevich | 1aeb87b | 2009-07-06 18:33:20 -0700 | [diff] [blame] | 4000 | if (tok == TOK_NUM_FLOAT) { |
Jack Palevich | 2aaf21f | 2009-07-15 16:16:37 -0700 | [diff] [blame] | 4001 | tokd = strtof(pText, &pEndPtr); |
Jack Palevich | 1aeb87b | 2009-07-06 18:33:20 -0700 | [diff] [blame] | 4002 | } else { |
Jack Palevich | 2aaf21f | 2009-07-15 16:16:37 -0700 | [diff] [blame] | 4003 | tokd = strtod(pText, &pEndPtr); |
Jack Palevich | 1aeb87b | 2009-07-06 18:33:20 -0700 | [diff] [blame] | 4004 | } |
Jack Palevich | 2aaf21f | 2009-07-15 16:16:37 -0700 | [diff] [blame] | 4005 | if (errno || pEndPtr != pEnd) { |
| 4006 | error("Can't parse constant: %s", pText); |
| 4007 | } |
| 4008 | // fprintf(stderr, "float constant: %s (%d) %g\n", pText, tok, tokd); |
Jack Palevich | 1aeb87b | 2009-07-06 18:33:20 -0700 | [diff] [blame] | 4009 | } |
| 4010 | |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4011 | void next() { |
| 4012 | int l, a; |
| 4013 | |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 4014 | while (isspace(ch) | (ch == '#')) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4015 | if (ch == '#') { |
| 4016 | inp(); |
| 4017 | next(); |
| 4018 | if (tok == TOK_DEFINE) { |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 4019 | doDefine(); |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 4020 | } else if (tok == TOK_PRAGMA) { |
| 4021 | doPragma(); |
Jack Palevich | dc45646 | 2009-07-16 16:50:56 -0700 | [diff] [blame] | 4022 | } else if (tok == TOK_LINE) { |
| 4023 | doLine(); |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 4024 | } else { |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 4025 | error("Unsupported preprocessor directive \"%s\"", |
| 4026 | mTokenString.getUnwrapped()); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4027 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4028 | } |
| 4029 | inp(); |
| 4030 | } |
| 4031 | tokl = 0; |
| 4032 | tok = ch; |
| 4033 | /* encode identifiers & numbers */ |
Jack Palevich | 2aaf21f | 2009-07-15 16:16:37 -0700 | [diff] [blame] | 4034 | if (isdigit(ch) || ch == '.') { |
| 4035 | // Start of a numeric constant. Could be integer, float, or |
| 4036 | // double, won't know until we look further. |
| 4037 | mTokenString.clear(); |
| 4038 | pdef(ch); |
| 4039 | inp(); |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 4040 | if (tok == '.' && !isdigit(ch)) { |
| 4041 | goto done; |
| 4042 | } |
Jack Palevich | 2aaf21f | 2009-07-15 16:16:37 -0700 | [diff] [blame] | 4043 | int base = 10; |
| 4044 | if (tok == '0') { |
| 4045 | if (ch == 'x' || ch == 'X') { |
| 4046 | base = 16; |
| 4047 | tok = TOK_NUM; |
| 4048 | tokc = 0; |
| 4049 | inp(); |
| 4050 | while ( isxdigit(ch) ) { |
| 4051 | tokc = (tokc << 4) + decodeHex(ch); |
| 4052 | inp(); |
| 4053 | } |
| 4054 | } else if (isoctal(ch)){ |
| 4055 | base = 8; |
| 4056 | tok = TOK_NUM; |
| 4057 | tokc = 0; |
| 4058 | while ( isoctal(ch) ) { |
| 4059 | tokc = (tokc << 3) + (ch - '0'); |
| 4060 | inp(); |
| 4061 | } |
| 4062 | } |
| 4063 | } else if (isdigit(tok)){ |
| 4064 | acceptDigitsCh(); |
| 4065 | } |
| 4066 | if (base == 10) { |
| 4067 | if (tok == '.' || ch == '.' || ch == 'e' || ch == 'E') { |
| 4068 | parseFloat(); |
| 4069 | } else { |
| 4070 | // It's an integer constant |
| 4071 | char* pText = mTokenString.getUnwrapped(); |
| 4072 | char* pEnd = pText + strlen(pText); |
| 4073 | char* pEndPtr = 0; |
| 4074 | errno = 0; |
| 4075 | tokc = strtol(pText, &pEndPtr, base); |
| 4076 | if (errno || pEndPtr != pEnd) { |
| 4077 | error("Can't parse constant: %s %d %d", pText, base, errno); |
| 4078 | } |
| 4079 | tok = TOK_NUM; |
| 4080 | } |
| 4081 | } |
| 4082 | } else if (isid()) { |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 4083 | mTokenString.clear(); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4084 | while (isid()) { |
Jack Palevich | e27bf3e | 2009-05-10 14:09:03 -0700 | [diff] [blame] | 4085 | pdef(ch); |
| 4086 | inp(); |
Jack Palevich | ae54f1f | 2009-05-08 14:54:15 -0700 | [diff] [blame] | 4087 | } |
Jack Palevich | 2aaf21f | 2009-07-15 16:16:37 -0700 | [diff] [blame] | 4088 | tok = mTokenTable.intern(mTokenString.getUnwrapped(), mTokenString.len()); |
Jack Palevich | 815d8b8 | 2009-08-18 18:25:56 -0700 | [diff] [blame] | 4089 | if (! mbSuppressMacroExpansion) { |
| 4090 | // Is this a macro? |
| 4091 | char* pMacroDefinition = mTokenTable[tok].mpMacroDefinition; |
| 4092 | if (pMacroDefinition) { |
| 4093 | // Yes, it is a macro |
| 4094 | dptr = pMacroDefinition; |
| 4095 | dch = ch; |
| 4096 | inp(); |
| 4097 | next(); |
| 4098 | } |
Jack Palevich | e27bf3e | 2009-05-10 14:09:03 -0700 | [diff] [blame] | 4099 | } |
Jack Palevich | e27bf3e | 2009-05-10 14:09:03 -0700 | [diff] [blame] | 4100 | } else { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4101 | inp(); |
| 4102 | if (tok == '\'') { |
| 4103 | tok = TOK_NUM; |
Jack Palevich | b4758ff | 2009-06-12 12:49:14 -0700 | [diff] [blame] | 4104 | tokc = getq(); |
| 4105 | if (ch != '\'') { |
| 4106 | error("Expected a ' character, got %c", ch); |
| 4107 | } else { |
| 4108 | inp(); |
| 4109 | } |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 4110 | } else if ((tok == '/') & (ch == '*')) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4111 | inp(); |
Jack Palevich | 22e3e8e | 2009-06-12 13:12:55 -0700 | [diff] [blame] | 4112 | while (ch && ch != EOF) { |
| 4113 | while (ch != '*' && ch != EOF) |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4114 | inp(); |
| 4115 | inp(); |
| 4116 | if (ch == '/') |
| 4117 | ch = 0; |
Jack Palevich | e27bf3e | 2009-05-10 14:09:03 -0700 | [diff] [blame] | 4118 | } |
Jack Palevich | 22e3e8e | 2009-06-12 13:12:55 -0700 | [diff] [blame] | 4119 | if (ch == EOF) { |
| 4120 | error("End of file inside comment."); |
| 4121 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4122 | inp(); |
Jack Palevich | e27bf3e | 2009-05-10 14:09:03 -0700 | [diff] [blame] | 4123 | next(); |
Jack Palevich | bd89490 | 2009-05-14 19:35:31 -0700 | [diff] [blame] | 4124 | } else if ((tok == '/') & (ch == '/')) { |
| 4125 | inp(); |
Jack Palevich | 22e3e8e | 2009-06-12 13:12:55 -0700 | [diff] [blame] | 4126 | while (ch && (ch != '\n') && (ch != EOF)) { |
Jack Palevich | bd89490 | 2009-05-14 19:35:31 -0700 | [diff] [blame] | 4127 | inp(); |
| 4128 | } |
| 4129 | inp(); |
| 4130 | next(); |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 4131 | } else if ((tok == '-') & (ch == '>')) { |
| 4132 | inp(); |
| 4133 | tok = TOK_OP_ARROW; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4134 | } else { |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 4135 | const char* t = operatorChars; |
| 4136 | int opIndex = 0; |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 4137 | while ((l = *t++) != 0) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4138 | a = *t++; |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 4139 | tokl = operatorLevel[opIndex]; |
| 4140 | tokc = opIndex; |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 4141 | if ((l == tok) & ((a == ch) | (a == '@'))) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4142 | #if 0 |
| 4143 | printf("%c%c -> tokl=%d tokc=0x%x\n", |
| 4144 | l, a, tokl, tokc); |
| 4145 | #endif |
| 4146 | if (a == ch) { |
| 4147 | inp(); |
| 4148 | tok = TOK_DUMMY; /* dummy token for double tokens */ |
| 4149 | } |
Jack Palevich | 0c01774 | 2009-07-31 12:00:39 -0700 | [diff] [blame] | 4150 | /* check for op=, valid for * / % + - << >> & ^ | */ |
| 4151 | if (ch == '=' && |
| 4152 | ((tokl >= 1 && tokl <= 3) |
Jack Palevich | 47cbea9 | 2009-07-31 15:25:53 -0700 | [diff] [blame] | 4153 | || (tokl >=6 && tokl <= 8)) ) { |
Jack Palevich | 0c01774 | 2009-07-31 12:00:39 -0700 | [diff] [blame] | 4154 | inp(); |
| 4155 | tok = TOK_OP_ASSIGNMENT; |
| 4156 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4157 | break; |
| 4158 | } |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 4159 | opIndex++; |
| 4160 | } |
| 4161 | if (l == 0) { |
| 4162 | tokl = 0; |
| 4163 | tokc = 0; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4164 | } |
| 4165 | } |
| 4166 | } |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 4167 | |
| 4168 | done: ; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4169 | #if 0 |
| 4170 | { |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 4171 | String buf; |
Jack Palevich | 37c54bd | 2009-07-14 18:35:36 -0700 | [diff] [blame] | 4172 | decodeToken(buf, tok, true); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4173 | fprintf(stderr, "%s\n", buf.getUnwrapped()); |
| 4174 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4175 | #endif |
| 4176 | } |
| 4177 | |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 4178 | void doDefine() { |
Jack Palevich | 815d8b8 | 2009-08-18 18:25:56 -0700 | [diff] [blame] | 4179 | mbSuppressMacroExpansion = true; |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 4180 | next(); |
Jack Palevich | 815d8b8 | 2009-08-18 18:25:56 -0700 | [diff] [blame] | 4181 | mbSuppressMacroExpansion = false; |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 4182 | tokenid_t name = tok; |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 4183 | String* pName = new String(); |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 4184 | if (ch == '(') { |
| 4185 | delete pName; |
| 4186 | error("Defines with arguments not supported"); |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 4187 | return; |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 4188 | } |
| 4189 | while (isspace(ch)) { |
| 4190 | inp(); |
| 4191 | } |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 4192 | String value; |
Jack Palevich | 0b1827a | 2009-08-18 17:44:12 -0700 | [diff] [blame] | 4193 | bool appendToValue = true; |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 4194 | while (ch != '\n' && ch != EOF) { |
Jack Palevich | 0b1827a | 2009-08-18 17:44:12 -0700 | [diff] [blame] | 4195 | // Check for '//' comments. |
| 4196 | if (appendToValue && ch == '/') { |
| 4197 | inp(); |
| 4198 | if (ch == '/') { |
| 4199 | appendToValue = false; |
| 4200 | } else { |
| 4201 | value.append('/'); |
| 4202 | } |
| 4203 | } |
| 4204 | if (appendToValue && ch != EOF) { |
| 4205 | value.append(ch); |
| 4206 | } |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 4207 | inp(); |
| 4208 | } |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 4209 | char* pDefn = (char*)mGlobalArena.alloc(value.len() + 1); |
| 4210 | memcpy(pDefn, value.getUnwrapped(), value.len()); |
| 4211 | pDefn[value.len()] = 0; |
| 4212 | mTokenTable[name].mpMacroDefinition = pDefn; |
Jack Palevich | 2d11dfb | 2009-06-08 14:34:26 -0700 | [diff] [blame] | 4213 | } |
| 4214 | |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 4215 | void doPragma() { |
| 4216 | // # pragma name(val) |
| 4217 | int state = 0; |
| 4218 | while(ch != EOF && ch != '\n' && state < 10) { |
| 4219 | switch(state) { |
| 4220 | case 0: |
| 4221 | if (isspace(ch)) { |
| 4222 | inp(); |
| 4223 | } else { |
| 4224 | state++; |
| 4225 | } |
| 4226 | break; |
| 4227 | case 1: |
| 4228 | if (isalnum(ch)) { |
| 4229 | mPragmas.append(ch); |
| 4230 | inp(); |
| 4231 | } else if (ch == '(') { |
| 4232 | mPragmas.append(0); |
| 4233 | inp(); |
| 4234 | state++; |
| 4235 | } else { |
| 4236 | state = 11; |
| 4237 | } |
| 4238 | break; |
| 4239 | case 2: |
| 4240 | if (isalnum(ch)) { |
| 4241 | mPragmas.append(ch); |
| 4242 | inp(); |
| 4243 | } else if (ch == ')') { |
| 4244 | mPragmas.append(0); |
| 4245 | inp(); |
| 4246 | state = 10; |
| 4247 | } else { |
| 4248 | state = 11; |
| 4249 | } |
| 4250 | break; |
| 4251 | } |
| 4252 | } |
| 4253 | if(state != 10) { |
| 4254 | error("Unexpected pragma syntax"); |
| 4255 | } |
| 4256 | mPragmaStringCount += 2; |
| 4257 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4258 | |
Jack Palevich | dc45646 | 2009-07-16 16:50:56 -0700 | [diff] [blame] | 4259 | void doLine() { |
| 4260 | // # line number { "filename "} |
| 4261 | next(); |
| 4262 | if (tok != TOK_NUM) { |
| 4263 | error("Expected a line-number"); |
| 4264 | } else { |
| 4265 | mLineNumber = tokc-1; // The end-of-line will increment it. |
| 4266 | } |
| 4267 | while(ch != EOF && ch != '\n') { |
| 4268 | inp(); |
| 4269 | } |
| 4270 | } |
| 4271 | |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 4272 | virtual void verror(const char* fmt, va_list ap) { |
Jack Palevich | dc45646 | 2009-07-16 16:50:56 -0700 | [diff] [blame] | 4273 | mErrorBuf.printf("%ld: ", mLineNumber); |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 4274 | mErrorBuf.vprintf(fmt, ap); |
| 4275 | mErrorBuf.printf("\n"); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4276 | } |
| 4277 | |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 4278 | void skip(intptr_t c) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4279 | if (tok != c) { |
| 4280 | error("'%c' expected", c); |
| 4281 | } |
| 4282 | next(); |
| 4283 | } |
| 4284 | |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4285 | bool accept(intptr_t c) { |
| 4286 | if (tok == c) { |
| 4287 | next(); |
| 4288 | return true; |
| 4289 | } |
| 4290 | return false; |
| 4291 | } |
| 4292 | |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 4293 | bool acceptStringLiteral() { |
| 4294 | if (tok == '"') { |
Jack Palevich | b5e3331 | 2009-07-30 19:06:34 -0700 | [diff] [blame] | 4295 | pGen->leaR0((int) glo, mkpCharPtr, ET_RVALUE); |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 4296 | // This while loop merges multiple adjacent string constants. |
| 4297 | while (tok == '"') { |
| 4298 | while (ch != '"' && ch != EOF) { |
Jack Palevich | 9cbd226 | 2009-07-08 16:48:41 -0700 | [diff] [blame] | 4299 | *allocGlobalSpace(1,1) = getq(); |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 4300 | } |
| 4301 | if (ch != '"') { |
| 4302 | error("Unterminated string constant."); |
| 4303 | } |
| 4304 | inp(); |
| 4305 | next(); |
Jack Palevich | b4758ff | 2009-06-12 12:49:14 -0700 | [diff] [blame] | 4306 | } |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 4307 | /* Null terminate */ |
Jack Palevich | 653f42d | 2009-05-28 17:15:32 -0700 | [diff] [blame] | 4308 | *glo = 0; |
Jack Palevich | f1f39cc | 2009-05-29 18:03:15 -0700 | [diff] [blame] | 4309 | /* align heap */ |
Jack Palevich | 9cbd226 | 2009-07-08 16:48:41 -0700 | [diff] [blame] | 4310 | allocGlobalSpace(1,(char*) (((intptr_t) glo + 4) & -4) - glo); |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 4311 | |
| 4312 | return true; |
| 4313 | } |
| 4314 | return false; |
| 4315 | } |
Jack Palevich | 8c246a9 | 2009-07-14 21:14:10 -0700 | [diff] [blame] | 4316 | |
Jack Palevich | b1544ca | 2009-07-16 15:09:20 -0700 | [diff] [blame] | 4317 | void linkGlobal(tokenid_t t, bool isFunction) { |
| 4318 | VariableInfo* pVI = VI(t); |
| 4319 | void* n = NULL; |
| 4320 | if (mpSymbolLookupFn) { |
| 4321 | n = mpSymbolLookupFn(mpSymbolLookupContext, nameof(t)); |
| 4322 | } |
| 4323 | if (pVI->pType == NULL) { |
| 4324 | if (isFunction) { |
| 4325 | pVI->pType = mkpIntFn; |
| 4326 | } else { |
| 4327 | pVI->pType = mkpInt; |
| 4328 | } |
| 4329 | } |
| 4330 | pVI->pAddress = n; |
| 4331 | } |
| 4332 | |
Jack Palevich | 29daf57 | 2009-07-30 19:38:55 -0700 | [diff] [blame] | 4333 | void unaryOrAssignment() { |
| 4334 | unary(); |
| 4335 | if (accept('=')) { |
| 4336 | checkLVal(); |
| 4337 | pGen->pushR0(); |
| 4338 | expr(); |
| 4339 | pGen->forceR0RVal(); |
| 4340 | pGen->storeR0ToTOS(); |
Jack Palevich | 0c01774 | 2009-07-31 12:00:39 -0700 | [diff] [blame] | 4341 | } else if (tok == TOK_OP_ASSIGNMENT) { |
| 4342 | int t = tokc; |
| 4343 | next(); |
| 4344 | checkLVal(); |
| 4345 | pGen->pushR0(); |
| 4346 | pGen->forceR0RVal(); |
| 4347 | pGen->pushR0(); |
| 4348 | expr(); |
| 4349 | pGen->forceR0RVal(); |
| 4350 | pGen->genOp(t); |
| 4351 | pGen->storeR0ToTOS(); |
Jack Palevich | 29daf57 | 2009-07-30 19:38:55 -0700 | [diff] [blame] | 4352 | } |
| 4353 | } |
| 4354 | |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 4355 | /* Parse and evaluate a unary expression. |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 4356 | */ |
Jack Palevich | 29daf57 | 2009-07-30 19:38:55 -0700 | [diff] [blame] | 4357 | void unary() { |
Jack Palevich | b1544ca | 2009-07-16 15:09:20 -0700 | [diff] [blame] | 4358 | tokenid_t t; |
Jack Palevich | 5b65909 | 2009-07-31 14:55:07 -0700 | [diff] [blame] | 4359 | intptr_t a; |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 4360 | t = 0; |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 4361 | if (acceptStringLiteral()) { |
| 4362 | // Nothing else to do. |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4363 | } else { |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 4364 | int c = tokl; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4365 | a = tokc; |
Jack Palevich | 1aeb87b | 2009-07-06 18:33:20 -0700 | [diff] [blame] | 4366 | double ad = tokd; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4367 | t = tok; |
| 4368 | next(); |
| 4369 | if (t == TOK_NUM) { |
Jack Palevich | 58c30ee | 2009-07-17 16:35:23 -0700 | [diff] [blame] | 4370 | pGen->li(a); |
Jack Palevich | 1aeb87b | 2009-07-06 18:33:20 -0700 | [diff] [blame] | 4371 | } else if (t == TOK_NUM_FLOAT) { |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 4372 | // Align to 4-byte boundary |
| 4373 | glo = (char*) (((intptr_t) glo + 3) & -4); |
| 4374 | * (float*) glo = (float) ad; |
| 4375 | pGen->loadFloat((int) glo, mkpFloat); |
| 4376 | glo += 4; |
Jack Palevich | 1aeb87b | 2009-07-06 18:33:20 -0700 | [diff] [blame] | 4377 | } else if (t == TOK_NUM_DOUBLE) { |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 4378 | // Align to 8-byte boundary |
| 4379 | glo = (char*) (((intptr_t) glo + 7) & -8); |
| 4380 | * (double*) glo = ad; |
| 4381 | pGen->loadFloat((int) glo, mkpDouble); |
| 4382 | glo += 8; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4383 | } else if (c == 2) { |
| 4384 | /* -, +, !, ~ */ |
Jack Palevich | 29daf57 | 2009-07-30 19:38:55 -0700 | [diff] [blame] | 4385 | unary(); |
Jack Palevich | b5e3331 | 2009-07-30 19:06:34 -0700 | [diff] [blame] | 4386 | pGen->forceR0RVal(); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4387 | if (t == '!') |
Jack Palevich | 58c30ee | 2009-07-17 16:35:23 -0700 | [diff] [blame] | 4388 | pGen->gUnaryCmp(a); |
Jack Palevich | a39749f | 2009-07-08 20:40:31 -0700 | [diff] [blame] | 4389 | else if (t == '+') { |
| 4390 | // ignore unary plus. |
| 4391 | } else { |
Jack Palevich | 9eed7a2 | 2009-07-06 17:24:34 -0700 | [diff] [blame] | 4392 | pGen->genUnaryOp(a); |
Jack Palevich | a39749f | 2009-07-08 20:40:31 -0700 | [diff] [blame] | 4393 | } |
Jack Palevich | aaac928 | 2009-07-31 14:34:34 -0700 | [diff] [blame] | 4394 | } else if (c == 11) { |
| 4395 | // pre increment / pre decrement |
| 4396 | unary(); |
| 4397 | doIncDec(a == OP_INCREMENT, 0); |
| 4398 | } |
| 4399 | else if (t == '(') { |
Jack Palevich | 45431bc | 2009-07-13 15:57:26 -0700 | [diff] [blame] | 4400 | // It's either a cast or an expression |
Jack Palevich | 2ff5c22 | 2009-07-23 15:11:22 -0700 | [diff] [blame] | 4401 | Type* pCast = acceptCastTypeDeclaration(); |
Jack Palevich | 45431bc | 2009-07-13 15:57:26 -0700 | [diff] [blame] | 4402 | if (pCast) { |
| 4403 | skip(')'); |
Jack Palevich | 29daf57 | 2009-07-30 19:38:55 -0700 | [diff] [blame] | 4404 | unary(); |
Jack Palevich | b5e3331 | 2009-07-30 19:06:34 -0700 | [diff] [blame] | 4405 | pGen->forceR0RVal(); |
Jack Palevich | b615450 | 2009-08-04 14:56:09 -0700 | [diff] [blame] | 4406 | pGen->castR0(pCast); |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 4407 | } else { |
Jack Palevich | 43aaee3 | 2009-07-31 14:01:37 -0700 | [diff] [blame] | 4408 | commaExpr(); |
Jack Palevich | 45431bc | 2009-07-13 15:57:26 -0700 | [diff] [blame] | 4409 | skip(')'); |
| 4410 | } |
| 4411 | } else if (t == '*') { |
| 4412 | /* This is a pointer dereference. |
| 4413 | */ |
Jack Palevich | 29daf57 | 2009-07-30 19:38:55 -0700 | [diff] [blame] | 4414 | unary(); |
Jack Palevich | 47cbea9 | 2009-07-31 15:25:53 -0700 | [diff] [blame] | 4415 | doPointer(); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4416 | } else if (t == '&') { |
Jack Palevich | 5fd66ae | 2009-09-04 15:24:23 -0700 | [diff] [blame] | 4417 | unary(); |
| 4418 | doAddressOf(); |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 4419 | } else if (t == EOF ) { |
| 4420 | error("Unexpected EOF."); |
Jack Palevich | d1f57e6 | 2009-07-15 18:23:22 -0700 | [diff] [blame] | 4421 | } else if (t == ';') { |
| 4422 | error("Unexpected ';'"); |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 4423 | } else if (!checkSymbol(t)) { |
Jack Palevich | a1804dd | 2009-06-12 14:40:04 -0700 | [diff] [blame] | 4424 | // Don't have to do anything special here, the error |
| 4425 | // message was printed by checkSymbol() above. |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4426 | } else { |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 4427 | if (!isDefined(t)) { |
| 4428 | mGlobals.add(t); |
| 4429 | // printf("Adding new global function %s\n", nameof(t)); |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 4430 | } |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 4431 | VariableInfo* pVI = VI(t); |
Jack Palevich | 5b65909 | 2009-07-31 14:55:07 -0700 | [diff] [blame] | 4432 | int n = (intptr_t) pVI->pAddress; |
Jack Palevich | 8c246a9 | 2009-07-14 21:14:10 -0700 | [diff] [blame] | 4433 | /* forward reference: try our lookup function */ |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 4434 | if (!n) { |
Jack Palevich | b1544ca | 2009-07-16 15:09:20 -0700 | [diff] [blame] | 4435 | linkGlobal(t, tok == '('); |
| 4436 | n = (intptr_t) pVI->pAddress; |
| 4437 | if (!n && tok != '(') { |
| 4438 | error("Undeclared variable %s\n", nameof(t)); |
Jack Palevich | 8c246a9 | 2009-07-14 21:14:10 -0700 | [diff] [blame] | 4439 | } |
Jack Palevich | cb1c9ef | 2009-05-14 11:38:49 -0700 | [diff] [blame] | 4440 | } |
Jack Palevich | 29daf57 | 2009-07-30 19:38:55 -0700 | [diff] [blame] | 4441 | if (tok != '(') { |
Jack Palevich | 5b65909 | 2009-07-31 14:55:07 -0700 | [diff] [blame] | 4442 | /* variable or function name */ |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 4443 | if (!n) { |
Jack Palevich | b1544ca | 2009-07-16 15:09:20 -0700 | [diff] [blame] | 4444 | linkGlobal(t, false); |
| 4445 | n = (intptr_t) pVI->pAddress; |
| 4446 | if (!n) { |
| 4447 | error("Undeclared variable %s\n", nameof(t)); |
| 4448 | } |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 4449 | } |
Jack Palevich | 5b65909 | 2009-07-31 14:55:07 -0700 | [diff] [blame] | 4450 | } |
| 4451 | // load a variable |
Jack Palevich | b615450 | 2009-08-04 14:56:09 -0700 | [diff] [blame] | 4452 | Type* pVal; |
| 4453 | ExpressionType et; |
| 4454 | if (pVI->pType->tag == TY_ARRAY) { |
| 4455 | pVal = pVI->pType; |
| 4456 | et = ET_RVALUE; |
| 4457 | } else { |
| 4458 | pVal = createPtrType(pVI->pType); |
| 4459 | et = ET_LVALUE; |
| 4460 | } |
Jack Palevich | 5b65909 | 2009-07-31 14:55:07 -0700 | [diff] [blame] | 4461 | if (n) { |
Jack Palevich | b615450 | 2009-08-04 14:56:09 -0700 | [diff] [blame] | 4462 | int tag = pVal->pHead->tag; |
| 4463 | if (tag == TY_FUNC) { |
Jack Palevich | 5b65909 | 2009-07-31 14:55:07 -0700 | [diff] [blame] | 4464 | et = ET_RVALUE; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4465 | } |
Jack Palevich | 5b65909 | 2009-07-31 14:55:07 -0700 | [diff] [blame] | 4466 | pGen->leaR0(n, pVal, et); |
| 4467 | } else { |
| 4468 | pVI->pForward = (void*) pGen->leaForward( |
| 4469 | (int) pVI->pForward, pVal); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4470 | } |
| 4471 | } |
| 4472 | } |
| 4473 | |
Jack Palevich | 5b65909 | 2009-07-31 14:55:07 -0700 | [diff] [blame] | 4474 | /* Now handle postfix operators */ |
| 4475 | for(;;) { |
| 4476 | if (tokl == 11) { |
| 4477 | // post inc / post dec |
| 4478 | doIncDec(tokc == OP_INCREMENT, true); |
| 4479 | next(); |
Jack Palevich | 47cbea9 | 2009-07-31 15:25:53 -0700 | [diff] [blame] | 4480 | } else if (accept('[')) { |
| 4481 | // Array reference |
| 4482 | pGen->forceR0RVal(); |
| 4483 | pGen->pushR0(); |
| 4484 | commaExpr(); |
| 4485 | pGen->forceR0RVal(); |
| 4486 | pGen->genOp(OP_PLUS); |
| 4487 | doPointer(); |
| 4488 | skip(']'); |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 4489 | } else if (accept('.')) { |
| 4490 | // struct element |
| 4491 | pGen->forceR0RVal(); |
| 4492 | Type* pStruct = pGen->getR0Type(); |
| 4493 | if (pStruct->tag == TY_STRUCT) { |
Jack Palevich | 8fe5dca | 2009-09-04 15:34:21 -0700 | [diff] [blame] | 4494 | doStructMember(pStruct, true); |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 4495 | } else { |
| 4496 | error("expected a struct value to the left of '.'"); |
| 4497 | } |
| 4498 | } else if (accept(TOK_OP_ARROW)) { |
| 4499 | pGen->forceR0RVal(); |
| 4500 | Type* pPtr = pGen->getR0Type(); |
| 4501 | if (pPtr->tag == TY_POINTER && pPtr->pHead->tag == TY_STRUCT) { |
| 4502 | pGen->loadR0FromR0(); |
Jack Palevich | 8fe5dca | 2009-09-04 15:34:21 -0700 | [diff] [blame] | 4503 | doStructMember(pPtr->pHead, false); |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 4504 | } else { |
| 4505 | error("Expected a pointer to a struct to the left of '->'"); |
| 4506 | } |
Jack Palevich | 5b65909 | 2009-07-31 14:55:07 -0700 | [diff] [blame] | 4507 | } else if (accept('(')) { |
| 4508 | /* function call */ |
| 4509 | Type* pDecl = NULL; |
| 4510 | VariableInfo* pVI = NULL; |
Jack Palevich | 9f51a26 | 2009-07-29 16:22:26 -0700 | [diff] [blame] | 4511 | Type* pFn = pGen->getR0Type(); |
| 4512 | assert(pFn->tag == TY_POINTER); |
| 4513 | assert(pFn->pHead->tag == TY_FUNC); |
| 4514 | pDecl = pFn->pHead; |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 4515 | pGen->pushR0(); |
Jack Palevich | 5b65909 | 2009-07-31 14:55:07 -0700 | [diff] [blame] | 4516 | Type* pArgList = pDecl->pTail; |
| 4517 | bool varArgs = pArgList == NULL; |
| 4518 | /* push args and invert order */ |
| 4519 | a = pGen->beginFunctionCallArguments(); |
| 4520 | int l = 0; |
| 4521 | int argCount = 0; |
| 4522 | while (tok != ')' && tok != EOF) { |
| 4523 | if (! varArgs && !pArgList) { |
| 4524 | error("Unexpected argument."); |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 4525 | } |
Jack Palevich | 5b65909 | 2009-07-31 14:55:07 -0700 | [diff] [blame] | 4526 | expr(); |
| 4527 | pGen->forceR0RVal(); |
| 4528 | Type* pTargetType; |
| 4529 | if (pArgList) { |
| 4530 | pTargetType = pArgList->pHead; |
| 4531 | pArgList = pArgList->pTail; |
| 4532 | } else { |
| 4533 | // This is a ... function, just pass arguments in their |
| 4534 | // natural type. |
| 4535 | pTargetType = pGen->getR0Type(); |
| 4536 | if (pTargetType->tag == TY_FLOAT) { |
| 4537 | pTargetType = mkpDouble; |
Jack Palevich | 80e4972 | 2009-08-04 15:39:49 -0700 | [diff] [blame] | 4538 | } else if (pTargetType->tag == TY_ARRAY) { |
| 4539 | // Pass arrays by pointer. |
| 4540 | pTargetType = pTargetType->pTail; |
Jack Palevich | 5b65909 | 2009-07-31 14:55:07 -0700 | [diff] [blame] | 4541 | } |
| 4542 | } |
| 4543 | if (pTargetType->tag == TY_VOID) { |
| 4544 | error("Can't pass void value for argument %d", |
| 4545 | argCount + 1); |
| 4546 | } else { |
| 4547 | l += pGen->storeR0ToArg(l, pTargetType); |
| 4548 | } |
| 4549 | if (accept(',')) { |
| 4550 | // fine |
| 4551 | } else if ( tok != ')') { |
| 4552 | error("Expected ',' or ')'"); |
| 4553 | } |
| 4554 | argCount += 1; |
Jack Palevich | 1a539db | 2009-07-08 13:04:41 -0700 | [diff] [blame] | 4555 | } |
Jack Palevich | 5b65909 | 2009-07-31 14:55:07 -0700 | [diff] [blame] | 4556 | if (! varArgs && pArgList) { |
| 4557 | error("Expected more argument(s). Saw %d", argCount); |
Jack Palevich | 2a4e1a9 | 2009-07-09 13:34:25 -0700 | [diff] [blame] | 4558 | } |
Jack Palevich | 5b65909 | 2009-07-31 14:55:07 -0700 | [diff] [blame] | 4559 | pGen->endFunctionCallArguments(pDecl, a, l); |
| 4560 | skip(')'); |
| 4561 | pGen->callIndirect(l, pDecl); |
| 4562 | pGen->adjustStackAfterCall(pDecl, l, true); |
| 4563 | } else { |
| 4564 | break; |
Jack Palevich | e27bf3e | 2009-05-10 14:09:03 -0700 | [diff] [blame] | 4565 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4566 | } |
| 4567 | } |
| 4568 | |
Jack Palevich | 8fe5dca | 2009-09-04 15:34:21 -0700 | [diff] [blame] | 4569 | void doStructMember(Type* pStruct, bool isDot) { |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 4570 | Type* pStructElement = lookupStructMember(pStruct, tok); |
| 4571 | if (pStructElement) { |
| 4572 | next(); |
| 4573 | pGen->addStructOffsetR0(pStructElement->length, createPtrType(pStructElement->pHead)); |
| 4574 | } else { |
| 4575 | String buf; |
| 4576 | decodeToken(buf, tok, true); |
Jack Palevich | 8fe5dca | 2009-09-04 15:34:21 -0700 | [diff] [blame] | 4577 | error("Expected a struct member to the right of '%s', got %s", |
| 4578 | isDot ? "." : "->", buf.getUnwrapped()); |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 4579 | } |
| 4580 | } |
| 4581 | |
Jack Palevich | aaac928 | 2009-07-31 14:34:34 -0700 | [diff] [blame] | 4582 | void doIncDec(int isInc, int isPost) { |
| 4583 | // R0 already has the lval |
| 4584 | checkLVal(); |
| 4585 | int lit = isInc ? 1 : -1; |
| 4586 | pGen->pushR0(); |
| 4587 | pGen->loadR0FromR0(); |
| 4588 | int tag = pGen->getR0Type()->tag; |
Jack Palevich | c9b8ffc | 2009-08-03 14:42:57 -0700 | [diff] [blame] | 4589 | if (!(tag == TY_INT || tag == TY_SHORT || tag == TY_CHAR || |
| 4590 | tag == TY_POINTER)) { |
Jack Palevich | aaac928 | 2009-07-31 14:34:34 -0700 | [diff] [blame] | 4591 | error("++/-- illegal for this type. %d", tag); |
| 4592 | } |
| 4593 | if (isPost) { |
| 4594 | pGen->over(); |
| 4595 | pGen->pushR0(); |
| 4596 | pGen->li(lit); |
| 4597 | pGen->genOp(OP_PLUS); |
| 4598 | pGen->storeR0ToTOS(); |
| 4599 | pGen->popR0(); |
| 4600 | } else { |
| 4601 | pGen->pushR0(); |
| 4602 | pGen->li(lit); |
| 4603 | pGen->genOp(OP_PLUS); |
| 4604 | pGen->over(); |
| 4605 | pGen->storeR0ToTOS(); |
| 4606 | pGen->popR0(); |
| 4607 | } |
| 4608 | } |
| 4609 | |
Jack Palevich | 47cbea9 | 2009-07-31 15:25:53 -0700 | [diff] [blame] | 4610 | void doPointer() { |
| 4611 | pGen->forceR0RVal(); |
| 4612 | Type* pR0Type = pGen->getR0Type(); |
| 4613 | if (pR0Type->tag != TY_POINTER) { |
| 4614 | error("Expected a pointer type."); |
| 4615 | } else { |
| 4616 | if (pR0Type->pHead->tag != TY_FUNC) { |
| 4617 | pGen->setR0ExpressionType(ET_LVALUE); |
| 4618 | } |
| 4619 | } |
| 4620 | } |
| 4621 | |
Jack Palevich | 5fd66ae | 2009-09-04 15:24:23 -0700 | [diff] [blame] | 4622 | void doAddressOf() { |
| 4623 | Type* pR0 = pGen->getR0Type(); |
| 4624 | bool isFuncPtr = pR0->tag == TY_POINTER && pR0->pHead->tag == TY_FUNC; |
| 4625 | if ((! isFuncPtr) && pGen->getR0ExpressionType() != ET_LVALUE) { |
| 4626 | error("Expected an lvalue"); |
| 4627 | } |
| 4628 | Type* pR0Type = pGen->getR0Type(); |
| 4629 | pGen->setR0ExpressionType(ET_RVALUE); |
| 4630 | } |
| 4631 | |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 4632 | /* Recursive descent parser for binary operations. |
| 4633 | */ |
| 4634 | void binaryOp(int level) { |
Jack Palevich | 7ecc555 | 2009-07-14 16:24:55 -0700 | [diff] [blame] | 4635 | intptr_t t, a; |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 4636 | t = 0; |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 4637 | if (level-- == 1) |
Jack Palevich | 29daf57 | 2009-07-30 19:38:55 -0700 | [diff] [blame] | 4638 | unaryOrAssignment(); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4639 | else { |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 4640 | binaryOp(level); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4641 | a = 0; |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 4642 | while (level == tokl) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4643 | t = tokc; |
| 4644 | next(); |
Jack Palevich | b5e3331 | 2009-07-30 19:06:34 -0700 | [diff] [blame] | 4645 | pGen->forceR0RVal(); |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 4646 | if (level > 8) { |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 4647 | a = pGen->gtst(t == OP_LOGICAL_OR, a); /* && and || output code generation */ |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 4648 | binaryOp(level); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4649 | } else { |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 4650 | pGen->pushR0(); |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 4651 | binaryOp(level); |
Jack Palevich | d1f57e6 | 2009-07-15 18:23:22 -0700 | [diff] [blame] | 4652 | // Check for syntax error. |
| 4653 | if (pGen->getR0Type() == NULL) { |
| 4654 | // We failed to parse a right-hand argument. |
| 4655 | // Push a dummy value so we don't fail |
Jack Palevich | 58c30ee | 2009-07-17 16:35:23 -0700 | [diff] [blame] | 4656 | pGen->li(0); |
Jack Palevich | d1f57e6 | 2009-07-15 18:23:22 -0700 | [diff] [blame] | 4657 | } |
Jack Palevich | b5e3331 | 2009-07-30 19:06:34 -0700 | [diff] [blame] | 4658 | pGen->forceR0RVal(); |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 4659 | if ((level == 4) | (level == 5)) { |
Jack Palevich | 58c30ee | 2009-07-17 16:35:23 -0700 | [diff] [blame] | 4660 | pGen->gcmp(t); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4661 | } else { |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 4662 | pGen->genOp(t); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4663 | } |
| 4664 | } |
| 4665 | } |
| 4666 | /* && and || output code generation */ |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 4667 | if (a && level > 8) { |
Jack Palevich | b5e3331 | 2009-07-30 19:06:34 -0700 | [diff] [blame] | 4668 | pGen->forceR0RVal(); |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 4669 | a = pGen->gtst(t == OP_LOGICAL_OR, a); |
Jack Palevich | 58c30ee | 2009-07-17 16:35:23 -0700 | [diff] [blame] | 4670 | pGen->li(t != OP_LOGICAL_OR); |
Jack Palevich | 8f361fa | 2009-07-30 16:19:43 -0700 | [diff] [blame] | 4671 | int b = pGen->gjmp(0); |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 4672 | pGen->gsym(a); |
Jack Palevich | 58c30ee | 2009-07-17 16:35:23 -0700 | [diff] [blame] | 4673 | pGen->li(t == OP_LOGICAL_OR); |
Jack Palevich | 8f361fa | 2009-07-30 16:19:43 -0700 | [diff] [blame] | 4674 | pGen->gsym(b); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4675 | } |
| 4676 | } |
| 4677 | } |
| 4678 | |
Jack Palevich | 43aaee3 | 2009-07-31 14:01:37 -0700 | [diff] [blame] | 4679 | void commaExpr() { |
| 4680 | for(;;) { |
| 4681 | expr(); |
| 4682 | if (!accept(',')) { |
| 4683 | break; |
| 4684 | } |
| 4685 | } |
| 4686 | } |
| 4687 | |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4688 | void expr() { |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 4689 | binaryOp(11); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4690 | } |
| 4691 | |
| 4692 | int test_expr() { |
Jack Palevich | 43aaee3 | 2009-07-31 14:01:37 -0700 | [diff] [blame] | 4693 | commaExpr(); |
Jack Palevich | b5e3331 | 2009-07-30 19:06:34 -0700 | [diff] [blame] | 4694 | pGen->forceR0RVal(); |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 4695 | return pGen->gtst(0, 0); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4696 | } |
| 4697 | |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 4698 | void block(intptr_t l, bool outermostFunctionBlock) { |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 4699 | intptr_t a, n, t; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4700 | |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 4701 | Type* pBaseType; |
Jack Palevich | 2ff5c22 | 2009-07-23 15:11:22 -0700 | [diff] [blame] | 4702 | if ((pBaseType = acceptPrimitiveType())) { |
Jack Palevich | a1804dd | 2009-06-12 14:40:04 -0700 | [diff] [blame] | 4703 | /* declarations */ |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 4704 | localDeclarations(pBaseType); |
Jack Palevich | a1804dd | 2009-06-12 14:40:04 -0700 | [diff] [blame] | 4705 | } else if (tok == TOK_IF) { |
Jack Palevich | e27bf3e | 2009-05-10 14:09:03 -0700 | [diff] [blame] | 4706 | next(); |
| 4707 | skip('('); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4708 | a = test_expr(); |
| 4709 | skip(')'); |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 4710 | block(l, false); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4711 | if (tok == TOK_ELSE) { |
Jack Palevich | e27bf3e | 2009-05-10 14:09:03 -0700 | [diff] [blame] | 4712 | next(); |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 4713 | n = pGen->gjmp(0); /* jmp */ |
| 4714 | pGen->gsym(a); |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 4715 | block(l, false); |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 4716 | pGen->gsym(n); /* patch else jmp */ |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4717 | } else { |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 4718 | pGen->gsym(a); /* patch if test */ |
Jack Palevich | e27bf3e | 2009-05-10 14:09:03 -0700 | [diff] [blame] | 4719 | } |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 4720 | } else if ((tok == TOK_WHILE) | (tok == TOK_FOR)) { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4721 | t = tok; |
| 4722 | next(); |
| 4723 | skip('('); |
| 4724 | if (t == TOK_WHILE) { |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 4725 | n = codeBuf.getPC(); // top of loop, target of "next" iteration |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4726 | a = test_expr(); |
| 4727 | } else { |
| 4728 | if (tok != ';') |
Jack Palevich | 43aaee3 | 2009-07-31 14:01:37 -0700 | [diff] [blame] | 4729 | commaExpr(); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4730 | skip(';'); |
| 4731 | n = codeBuf.getPC(); |
| 4732 | a = 0; |
| 4733 | if (tok != ';') |
| 4734 | a = test_expr(); |
| 4735 | skip(';'); |
| 4736 | if (tok != ')') { |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 4737 | t = pGen->gjmp(0); |
Jack Palevich | 43aaee3 | 2009-07-31 14:01:37 -0700 | [diff] [blame] | 4738 | commaExpr(); |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 4739 | pGen->gjmp(n - codeBuf.getPC() - pGen->jumpOffset()); |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 4740 | pGen->gsym(t); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4741 | n = t + 4; |
| 4742 | } |
| 4743 | } |
| 4744 | skip(')'); |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 4745 | block((intptr_t) &a, false); |
Jack Palevich | a653561 | 2009-05-13 16:24:17 -0700 | [diff] [blame] | 4746 | pGen->gjmp(n - codeBuf.getPC() - pGen->jumpOffset()); /* jmp */ |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 4747 | pGen->gsym(a); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4748 | } else if (tok == '{') { |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 4749 | if (! outermostFunctionBlock) { |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 4750 | mLocals.pushLevel(); |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 4751 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4752 | next(); |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 4753 | while (tok != '}' && tok != EOF) |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 4754 | block(l, false); |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 4755 | skip('}'); |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 4756 | if (! outermostFunctionBlock) { |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 4757 | mLocals.popLevel(); |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 4758 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4759 | } else { |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 4760 | if (accept(TOK_RETURN)) { |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 4761 | if (tok != ';') { |
Jack Palevich | 43aaee3 | 2009-07-31 14:01:37 -0700 | [diff] [blame] | 4762 | commaExpr(); |
Jack Palevich | b5e3331 | 2009-07-30 19:06:34 -0700 | [diff] [blame] | 4763 | pGen->forceR0RVal(); |
Jack Palevich | 2a4e1a9 | 2009-07-09 13:34:25 -0700 | [diff] [blame] | 4764 | if (pReturnType->tag == TY_VOID) { |
| 4765 | error("Must not return a value from a void function"); |
| 4766 | } else { |
| 4767 | pGen->convertR0(pReturnType); |
| 4768 | } |
| 4769 | } else { |
| 4770 | if (pReturnType->tag != TY_VOID) { |
| 4771 | error("Must specify a value here"); |
| 4772 | } |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 4773 | } |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 4774 | rsym = pGen->gjmp(rsym); /* jmp */ |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 4775 | } else if (accept(TOK_BREAK)) { |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 4776 | *(int *) l = pGen->gjmp(*(int *) l); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4777 | } else if (tok != ';') |
Jack Palevich | 43aaee3 | 2009-07-31 14:01:37 -0700 | [diff] [blame] | 4778 | commaExpr(); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4779 | skip(';'); |
Jack Palevich | e27bf3e | 2009-05-10 14:09:03 -0700 | [diff] [blame] | 4780 | } |
| 4781 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 4782 | |
Jack Palevich | a8f427f | 2009-07-13 18:40:08 -0700 | [diff] [blame] | 4783 | static bool typeEqual(Type* a, Type* b) { |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 4784 | if (a == b) { |
| 4785 | return true; |
| 4786 | } |
| 4787 | if (a == NULL || b == NULL) { |
| 4788 | return false; |
| 4789 | } |
| 4790 | TypeTag at = a->tag; |
| 4791 | if (at != b->tag) { |
| 4792 | return false; |
| 4793 | } |
| 4794 | if (at == TY_POINTER) { |
| 4795 | return typeEqual(a->pHead, b->pHead); |
Jack Palevich | b615450 | 2009-08-04 14:56:09 -0700 | [diff] [blame] | 4796 | } else if (at == TY_ARRAY) { |
| 4797 | return a->length == b->length && typeEqual(a->pHead, b->pHead); |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 4798 | } else if (at == TY_FUNC || at == TY_PARAM) { |
| 4799 | return typeEqual(a->pHead, b->pHead) |
| 4800 | && typeEqual(a->pTail, b->pTail); |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 4801 | } else if (at == TY_STRUCT) { |
| 4802 | return a->pHead == b->pHead; |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 4803 | } |
| 4804 | return true; |
| 4805 | } |
| 4806 | |
Jack Palevich | 2ff5c22 | 2009-07-23 15:11:22 -0700 | [diff] [blame] | 4807 | Type* createType(TypeTag tag, Type* pHead, Type* pTail) { |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4808 | assert(tag >= TY_INT && tag <= TY_PARAM); |
Jack Palevich | 2ff5c22 | 2009-07-23 15:11:22 -0700 | [diff] [blame] | 4809 | Type* pType = (Type*) mpCurrentArena->alloc(sizeof(Type)); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4810 | memset(pType, 0, sizeof(*pType)); |
| 4811 | pType->tag = tag; |
| 4812 | pType->pHead = pHead; |
| 4813 | pType->pTail = pTail; |
| 4814 | return pType; |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 4815 | } |
| 4816 | |
Jack Palevich | 2ff5c22 | 2009-07-23 15:11:22 -0700 | [diff] [blame] | 4817 | Type* createPtrType(Type* pType) { |
| 4818 | return createType(TY_POINTER, pType, NULL); |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 4819 | } |
| 4820 | |
| 4821 | /** |
| 4822 | * Try to print a type in declaration order |
| 4823 | */ |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4824 | void decodeType(String& buffer, Type* pType) { |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 4825 | buffer.clear(); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4826 | if (pType == NULL) { |
| 4827 | buffer.appendCStr("null"); |
| 4828 | return; |
| 4829 | } |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 4830 | decodeTypeImp(buffer, pType); |
| 4831 | } |
| 4832 | |
| 4833 | void decodeTypeImp(String& buffer, Type* pType) { |
| 4834 | decodeTypeImpPrefix(buffer, pType); |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 4835 | decodeId(buffer, pType->id); |
| 4836 | decodeTypeImpPostfix(buffer, pType); |
| 4837 | } |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 4838 | |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 4839 | void decodeId(String& buffer, tokenid_t id) { |
| 4840 | if (id) { |
| 4841 | String temp; |
| 4842 | decodeToken(temp, id, false); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4843 | buffer.append(temp); |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 4844 | } |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 4845 | } |
| 4846 | |
| 4847 | void decodeTypeImpPrefix(String& buffer, Type* pType) { |
| 4848 | TypeTag tag = pType->tag; |
| 4849 | |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 4850 | if ((tag >= TY_INT && tag <= TY_DOUBLE) || tag == TY_STRUCT) { |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 4851 | switch (tag) { |
| 4852 | case TY_INT: |
| 4853 | buffer.appendCStr("int"); |
| 4854 | break; |
Jack Palevich | c9b8ffc | 2009-08-03 14:42:57 -0700 | [diff] [blame] | 4855 | case TY_SHORT: |
| 4856 | buffer.appendCStr("short"); |
| 4857 | break; |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 4858 | case TY_CHAR: |
| 4859 | buffer.appendCStr("char"); |
| 4860 | break; |
| 4861 | case TY_VOID: |
| 4862 | buffer.appendCStr("void"); |
| 4863 | break; |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 4864 | case TY_FLOAT: |
| 4865 | buffer.appendCStr("float"); |
| 4866 | break; |
| 4867 | case TY_DOUBLE: |
| 4868 | buffer.appendCStr("double"); |
| 4869 | break; |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 4870 | case TY_STRUCT: |
| 4871 | { |
| 4872 | bool isStruct = (pType->pHead->alignment & 0x80000000) != 0; |
| 4873 | buffer.appendCStr(isStruct ? "struct" : "union"); |
| 4874 | if (pType->pHead && pType->pHead->structTag) { |
| 4875 | buffer.append(' '); |
| 4876 | decodeId(buffer, pType->pHead->structTag); |
| 4877 | } |
| 4878 | } |
| 4879 | break; |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 4880 | default: |
| 4881 | break; |
| 4882 | } |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4883 | buffer.append(' '); |
| 4884 | } |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 4885 | |
| 4886 | switch (tag) { |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4887 | case TY_INT: |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4888 | break; |
Jack Palevich | c9b8ffc | 2009-08-03 14:42:57 -0700 | [diff] [blame] | 4889 | case TY_SHORT: |
| 4890 | break; |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4891 | case TY_CHAR: |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4892 | break; |
| 4893 | case TY_VOID: |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 4894 | break; |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 4895 | case TY_FLOAT: |
| 4896 | break; |
| 4897 | case TY_DOUBLE: |
| 4898 | break; |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4899 | case TY_POINTER: |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 4900 | decodeTypeImpPrefix(buffer, pType->pHead); |
| 4901 | if(pType->pHead && pType->pHead->tag == TY_FUNC) { |
| 4902 | buffer.append('('); |
| 4903 | } |
| 4904 | buffer.append('*'); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4905 | break; |
Jack Palevich | b615450 | 2009-08-04 14:56:09 -0700 | [diff] [blame] | 4906 | case TY_ARRAY: |
| 4907 | decodeTypeImpPrefix(buffer, pType->pHead); |
| 4908 | break; |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 4909 | case TY_STRUCT: |
| 4910 | break; |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4911 | case TY_FUNC: |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 4912 | decodeTypeImp(buffer, pType->pHead); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4913 | break; |
| 4914 | case TY_PARAM: |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 4915 | decodeTypeImp(buffer, pType->pHead); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4916 | break; |
| 4917 | default: |
| 4918 | String temp; |
| 4919 | temp.printf("Unknown tag %d", pType->tag); |
| 4920 | buffer.append(temp); |
| 4921 | break; |
| 4922 | } |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 4923 | } |
| 4924 | |
| 4925 | void decodeTypeImpPostfix(String& buffer, Type* pType) { |
| 4926 | TypeTag tag = pType->tag; |
| 4927 | |
| 4928 | switch(tag) { |
| 4929 | case TY_POINTER: |
| 4930 | if(pType->pHead && pType->pHead->tag == TY_FUNC) { |
| 4931 | buffer.append(')'); |
| 4932 | } |
| 4933 | decodeTypeImpPostfix(buffer, pType->pHead); |
| 4934 | break; |
Jack Palevich | b615450 | 2009-08-04 14:56:09 -0700 | [diff] [blame] | 4935 | case TY_ARRAY: |
| 4936 | { |
| 4937 | String temp; |
| 4938 | temp.printf("[%d]", pType->length); |
| 4939 | buffer.append(temp); |
| 4940 | } |
| 4941 | break; |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 4942 | case TY_STRUCT: |
| 4943 | if (pType->pHead->length >= 0) { |
| 4944 | buffer.appendCStr(" {"); |
| 4945 | for(Type* pArg = pType->pTail; pArg; pArg = pArg->pTail) { |
| 4946 | decodeTypeImp(buffer, pArg->pHead); |
| 4947 | buffer.appendCStr(";"); |
| 4948 | } |
| 4949 | buffer.append('}'); |
| 4950 | } |
| 4951 | break; |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 4952 | case TY_FUNC: |
| 4953 | buffer.append('('); |
| 4954 | for(Type* pArg = pType->pTail; pArg; pArg = pArg->pTail) { |
| 4955 | decodeTypeImp(buffer, pArg); |
| 4956 | if (pArg->pTail) { |
| 4957 | buffer.appendCStr(", "); |
| 4958 | } |
| 4959 | } |
| 4960 | buffer.append(')'); |
| 4961 | break; |
| 4962 | default: |
| 4963 | break; |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4964 | } |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 4965 | } |
| 4966 | |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4967 | void printType(Type* pType) { |
| 4968 | String buffer; |
| 4969 | decodeType(buffer, pType); |
| 4970 | fprintf(stderr, "%s\n", buffer.getUnwrapped()); |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 4971 | } |
| 4972 | |
Jack Palevich | 2ff5c22 | 2009-07-23 15:11:22 -0700 | [diff] [blame] | 4973 | Type* acceptPrimitiveType() { |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4974 | Type* pType; |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 4975 | if (tok == TOK_INT) { |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4976 | pType = mkpInt; |
Jack Palevich | c9b8ffc | 2009-08-03 14:42:57 -0700 | [diff] [blame] | 4977 | } else if (tok == TOK_SHORT) { |
| 4978 | pType = mkpShort; |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 4979 | } else if (tok == TOK_CHAR) { |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4980 | pType = mkpChar; |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 4981 | } else if (tok == TOK_VOID) { |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4982 | pType = mkpVoid; |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 4983 | } else if (tok == TOK_FLOAT) { |
| 4984 | pType = mkpFloat; |
| 4985 | } else if (tok == TOK_DOUBLE) { |
| 4986 | pType = mkpDouble; |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 4987 | } else if (tok == TOK_STRUCT || tok == TOK_UNION) { |
| 4988 | return acceptStruct(); |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 4989 | } else { |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4990 | return NULL; |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 4991 | } |
| 4992 | next(); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 4993 | return pType; |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 4994 | } |
| 4995 | |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 4996 | Type* acceptStruct() { |
| 4997 | assert(tok == TOK_STRUCT || tok == TOK_UNION); |
| 4998 | bool isStruct = tok == TOK_STRUCT; |
| 4999 | next(); |
| 5000 | tokenid_t structTag = acceptSymbol(); |
| 5001 | bool isDeclaration = accept('{'); |
| 5002 | bool fail = false; |
| 5003 | |
| 5004 | Type* pStructType = createType(TY_STRUCT, NULL, NULL); |
| 5005 | if (structTag) { |
| 5006 | Token* pToken = &mTokenTable[structTag]; |
| 5007 | VariableInfo* pStructInfo = pToken->mpStructInfo; |
| 5008 | bool needToDeclare = !pStructInfo; |
| 5009 | if (pStructInfo) { |
| 5010 | if (isDeclaration) { |
| 5011 | if (mpCurrentSymbolStack->isStructTagDefinedAtCurrentLevel(structTag)) { |
| 5012 | if (pStructInfo->pType->pHead->length == -1) { |
| 5013 | // we're filling in a forward declaration. |
| 5014 | needToDeclare = false; |
| 5015 | } else { |
| 5016 | error("A struct with the same name is already defined at this level."); |
| 5017 | fail = true; |
| 5018 | } |
| 5019 | } else { |
| 5020 | needToDeclare = true; |
| 5021 | } |
| 5022 | } |
| 5023 | if (!fail) { |
| 5024 | assert(pStructInfo->isStructTag); |
| 5025 | pStructType->pHead = pStructInfo->pType; |
| 5026 | pStructType->pTail = pStructType->pHead->pTail; |
| 5027 | } |
| 5028 | } |
| 5029 | |
| 5030 | if (needToDeclare) { |
| 5031 | // This is a new struct name |
| 5032 | pToken->mpStructInfo = mpCurrentSymbolStack->addStructTag(structTag); |
| 5033 | pStructType = createType(TY_STRUCT, NULL, NULL); |
| 5034 | pStructType->structTag = structTag; |
| 5035 | pStructType->pHead = pStructType; |
| 5036 | if (! isDeclaration) { |
| 5037 | // A forward declaration |
| 5038 | pStructType->length = -1; |
| 5039 | } |
| 5040 | pToken->mpStructInfo->pType = pStructType; |
| 5041 | } |
| 5042 | } else { |
| 5043 | // An anonymous struct |
| 5044 | pStructType->pHead = pStructType; |
| 5045 | } |
| 5046 | |
| 5047 | if (isDeclaration) { |
| 5048 | size_t offset = 0; |
| 5049 | size_t structSize = 0; |
| 5050 | size_t structAlignment = 0; |
| 5051 | Type** pParamHolder = & pStructType->pHead->pTail; |
| 5052 | while (tok != '}' && tok != EOF) { |
| 5053 | Type* pPrimitiveType = expectPrimitiveType(); |
| 5054 | if (pPrimitiveType) { |
| 5055 | while (tok != ';' && tok != EOF) { |
| 5056 | Type* pItem = acceptDeclaration(pPrimitiveType, true, false); |
| 5057 | if (!pItem) { |
| 5058 | break; |
| 5059 | } |
| 5060 | if (lookupStructMember(pStructType, pItem->id)) { |
| 5061 | String buf; |
| 5062 | decodeToken(buf, pItem->id, false); |
| 5063 | error("Duplicate struct member %s", buf.getUnwrapped()); |
| 5064 | } |
| 5065 | Type* pStructElement = createType(TY_PARAM, pItem, NULL); |
| 5066 | size_t alignment = pGen->alignmentOf(pItem); |
| 5067 | if (alignment > structAlignment) { |
| 5068 | structAlignment = alignment; |
| 5069 | } |
| 5070 | size_t alignmentMask = alignment - 1; |
| 5071 | offset = (offset + alignmentMask) & ~alignmentMask; |
| 5072 | pStructElement->length = offset; |
| 5073 | size_t size = pGen->sizeOf(pItem); |
| 5074 | if (isStruct) { |
| 5075 | offset += size; |
| 5076 | structSize = offset; |
| 5077 | } else { |
| 5078 | if (size >= structSize) { |
| 5079 | structSize = size; |
| 5080 | } |
| 5081 | } |
| 5082 | *pParamHolder = pStructElement; |
| 5083 | pParamHolder = &pStructElement->pTail; |
| 5084 | accept(','); |
| 5085 | } |
| 5086 | skip(';'); |
| 5087 | } else { |
| 5088 | // Some sort of syntax error, skip token and keep trying |
| 5089 | next(); |
| 5090 | } |
| 5091 | } |
| 5092 | if (!fail) { |
| 5093 | pStructType->pHead->length = structSize; |
| 5094 | pStructType->pHead->alignment = structAlignment | (isStruct << 31); |
| 5095 | } |
| 5096 | skip('}'); |
| 5097 | } |
| 5098 | if (fail) { |
| 5099 | pStructType = NULL; |
| 5100 | } |
| 5101 | return pStructType; |
| 5102 | } |
| 5103 | |
| 5104 | Type* lookupStructMember(Type* pStruct, tokenid_t memberId) { |
| 5105 | for(Type* pStructElement = pStruct->pHead->pTail; pStructElement; pStructElement = pStructElement->pTail) { |
| 5106 | if (pStructElement->pHead->id == memberId) { |
| 5107 | return pStructElement; |
| 5108 | } |
| 5109 | } |
| 5110 | return NULL; |
| 5111 | } |
| 5112 | |
Jack Palevich | 2ff5c22 | 2009-07-23 15:11:22 -0700 | [diff] [blame] | 5113 | Type* acceptDeclaration(Type* pType, bool nameAllowed, bool nameRequired) { |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 5114 | tokenid_t declName = 0; |
Jack Palevich | 3377bfd | 2009-07-16 19:05:07 -0700 | [diff] [blame] | 5115 | bool reportFailure = false; |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 5116 | pType = acceptDecl2(pType, declName, nameAllowed, |
Jack Palevich | 2ff5c22 | 2009-07-23 15:11:22 -0700 | [diff] [blame] | 5117 | nameRequired, reportFailure); |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 5118 | if (declName) { |
| 5119 | // Clone the parent type so we can set a unique ID |
Jack Palevich | b615450 | 2009-08-04 14:56:09 -0700 | [diff] [blame] | 5120 | Type* pOldType = pType; |
Jack Palevich | 2ff5c22 | 2009-07-23 15:11:22 -0700 | [diff] [blame] | 5121 | pType = createType(pType->tag, pType->pHead, pType->pTail); |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 5122 | *pType = *pOldType; |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 5123 | pType->id = declName; |
Jack Palevich | b615450 | 2009-08-04 14:56:09 -0700 | [diff] [blame] | 5124 | } else if (nameRequired) { |
| 5125 | error("Expected a variable name"); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 5126 | } |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 5127 | #if 0 |
| 5128 | fprintf(stderr, "Parsed a declaration: "); |
| 5129 | printType(pType); |
| 5130 | #endif |
Jack Palevich | 3377bfd | 2009-07-16 19:05:07 -0700 | [diff] [blame] | 5131 | if (reportFailure) { |
| 5132 | return NULL; |
| 5133 | } |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 5134 | return pType; |
| 5135 | } |
| 5136 | |
Jack Palevich | 2ff5c22 | 2009-07-23 15:11:22 -0700 | [diff] [blame] | 5137 | Type* expectDeclaration(Type* pBaseType) { |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 5138 | bool nameRequired = pBaseType->tag != TY_STRUCT; |
| 5139 | Type* pType = acceptDeclaration(pBaseType, true, nameRequired); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 5140 | if (! pType) { |
| 5141 | error("Expected a declaration"); |
| 5142 | } |
| 5143 | return pType; |
| 5144 | } |
| 5145 | |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 5146 | /* Used for accepting types that appear in casts */ |
Jack Palevich | 2ff5c22 | 2009-07-23 15:11:22 -0700 | [diff] [blame] | 5147 | Type* acceptCastTypeDeclaration() { |
| 5148 | Type* pType = acceptPrimitiveType(); |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 5149 | if (pType) { |
Jack Palevich | 2ff5c22 | 2009-07-23 15:11:22 -0700 | [diff] [blame] | 5150 | pType = acceptDeclaration(pType, false, false); |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 5151 | } |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 5152 | return pType; |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 5153 | } |
| 5154 | |
Jack Palevich | 2ff5c22 | 2009-07-23 15:11:22 -0700 | [diff] [blame] | 5155 | Type* expectCastTypeDeclaration() { |
| 5156 | Type* pType = acceptCastTypeDeclaration(); |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 5157 | if (! pType) { |
| 5158 | error("Expected a declaration"); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 5159 | } |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 5160 | return pType; |
| 5161 | } |
| 5162 | |
| 5163 | Type* acceptDecl2(Type* pType, tokenid_t& declName, |
Jack Palevich | 2ff5c22 | 2009-07-23 15:11:22 -0700 | [diff] [blame] | 5164 | bool nameAllowed, bool nameRequired, |
Jack Palevich | 3377bfd | 2009-07-16 19:05:07 -0700 | [diff] [blame] | 5165 | bool& reportFailure) { |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 5166 | while (accept('*')) { |
Jack Palevich | 9613899 | 2009-07-31 15:58:19 -0700 | [diff] [blame] | 5167 | pType = createType(TY_POINTER, pType, NULL); |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 5168 | } |
Jack Palevich | 2ff5c22 | 2009-07-23 15:11:22 -0700 | [diff] [blame] | 5169 | pType = acceptDecl3(pType, declName, nameAllowed, nameRequired, |
Jack Palevich | 3377bfd | 2009-07-16 19:05:07 -0700 | [diff] [blame] | 5170 | reportFailure); |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 5171 | return pType; |
| 5172 | } |
| 5173 | |
| 5174 | Type* acceptDecl3(Type* pType, tokenid_t& declName, |
Jack Palevich | 2ff5c22 | 2009-07-23 15:11:22 -0700 | [diff] [blame] | 5175 | bool nameAllowed, bool nameRequired, |
Jack Palevich | 3377bfd | 2009-07-16 19:05:07 -0700 | [diff] [blame] | 5176 | bool& reportFailure) { |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 5177 | // direct-dcl : |
| 5178 | // name |
| 5179 | // (dcl) |
| 5180 | // direct-dcl() |
| 5181 | // direct-dcl[] |
| 5182 | Type* pNewHead = NULL; |
| 5183 | if (accept('(')) { |
| 5184 | pNewHead = acceptDecl2(pNewHead, declName, nameAllowed, |
Jack Palevich | 2ff5c22 | 2009-07-23 15:11:22 -0700 | [diff] [blame] | 5185 | nameRequired, reportFailure); |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 5186 | skip(')'); |
| 5187 | } else if ((declName = acceptSymbol()) != 0) { |
| 5188 | if (nameAllowed == false && declName) { |
| 5189 | error("Symbol %s not allowed here", nameof(declName)); |
Jack Palevich | 3377bfd | 2009-07-16 19:05:07 -0700 | [diff] [blame] | 5190 | reportFailure = true; |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 5191 | } |
Jack Palevich | 3377bfd | 2009-07-16 19:05:07 -0700 | [diff] [blame] | 5192 | } else if (nameRequired && ! declName) { |
| 5193 | String temp; |
| 5194 | decodeToken(temp, tok, true); |
| 5195 | error("Expected name. Got %s", temp.getUnwrapped()); |
| 5196 | reportFailure = true; |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 5197 | } |
Jack Palevich | b615450 | 2009-08-04 14:56:09 -0700 | [diff] [blame] | 5198 | for(;;) { |
| 5199 | if (accept('(')) { |
| 5200 | // Function declaration |
| 5201 | Type* pTail = acceptArgs(nameAllowed); |
| 5202 | pType = createType(TY_FUNC, pType, pTail); |
| 5203 | skip(')'); |
| 5204 | } if (accept('[')) { |
| 5205 | if (tok != ']') { |
| 5206 | if (tok != TOK_NUM || tokc <= 0) { |
| 5207 | error("Expected positive integer constant"); |
| 5208 | } else { |
| 5209 | Type* pDecayType = createPtrType(pType); |
| 5210 | pType = createType(TY_ARRAY, pType, pDecayType); |
| 5211 | pType->length = tokc; |
| 5212 | } |
| 5213 | next(); |
| 5214 | } |
| 5215 | skip(']'); |
| 5216 | } else { |
| 5217 | break; |
| 5218 | } |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 5219 | } |
Jack Palevich | 3f22649 | 2009-07-02 14:46:19 -0700 | [diff] [blame] | 5220 | |
| 5221 | if (pNewHead) { |
| 5222 | Type* pA = pNewHead; |
| 5223 | while (pA->pHead) { |
| 5224 | pA = pA->pHead; |
| 5225 | } |
| 5226 | pA->pHead = pType; |
| 5227 | pType = pNewHead; |
| 5228 | } |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 5229 | return pType; |
| 5230 | } |
| 5231 | |
Jack Palevich | 2ff5c22 | 2009-07-23 15:11:22 -0700 | [diff] [blame] | 5232 | Type* acceptArgs(bool nameAllowed) { |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 5233 | Type* pHead = NULL; |
| 5234 | Type* pTail = NULL; |
| 5235 | for(;;) { |
Jack Palevich | 2ff5c22 | 2009-07-23 15:11:22 -0700 | [diff] [blame] | 5236 | Type* pBaseArg = acceptPrimitiveType(); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 5237 | if (pBaseArg) { |
Jack Palevich | 2ff5c22 | 2009-07-23 15:11:22 -0700 | [diff] [blame] | 5238 | Type* pArg = acceptDeclaration(pBaseArg, nameAllowed, false); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 5239 | if (pArg) { |
Jack Palevich | 2ff5c22 | 2009-07-23 15:11:22 -0700 | [diff] [blame] | 5240 | Type* pParam = createType(TY_PARAM, pArg, NULL); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 5241 | if (!pHead) { |
| 5242 | pHead = pParam; |
| 5243 | pTail = pParam; |
| 5244 | } else { |
| 5245 | pTail->pTail = pParam; |
| 5246 | pTail = pParam; |
| 5247 | } |
| 5248 | } |
| 5249 | } |
| 5250 | if (! accept(',')) { |
| 5251 | break; |
| 5252 | } |
| 5253 | } |
| 5254 | return pHead; |
| 5255 | } |
| 5256 | |
Jack Palevich | 2ff5c22 | 2009-07-23 15:11:22 -0700 | [diff] [blame] | 5257 | Type* expectPrimitiveType() { |
| 5258 | Type* pType = acceptPrimitiveType(); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 5259 | if (!pType) { |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 5260 | String buf; |
Jack Palevich | 37c54bd | 2009-07-14 18:35:36 -0700 | [diff] [blame] | 5261 | decodeToken(buf, tok, true); |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 5262 | error("Expected a type, got %s", buf.getUnwrapped()); |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 5263 | } |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 5264 | return pType; |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 5265 | } |
| 5266 | |
Jack Palevich | b5e3331 | 2009-07-30 19:06:34 -0700 | [diff] [blame] | 5267 | void checkLVal() { |
| 5268 | if (pGen->getR0ExpressionType() != ET_LVALUE) { |
Jack Palevich | 5fd66ae | 2009-09-04 15:24:23 -0700 | [diff] [blame] | 5269 | error("Expected an lvalue"); |
Jack Palevich | b5e3331 | 2009-07-30 19:06:34 -0700 | [diff] [blame] | 5270 | } |
| 5271 | } |
| 5272 | |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 5273 | void addGlobalSymbol(Type* pDecl) { |
| 5274 | tokenid_t t = pDecl->id; |
| 5275 | VariableInfo* pVI = VI(t); |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 5276 | if(pVI && pVI->pAddress) { |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 5277 | reportDuplicate(t); |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 5278 | } |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 5279 | mGlobals.add(pDecl); |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 5280 | } |
| 5281 | |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 5282 | void reportDuplicate(tokenid_t t) { |
| 5283 | error("Duplicate definition of %s", nameof(t)); |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 5284 | } |
| 5285 | |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 5286 | void addLocalSymbol(Type* pDecl) { |
| 5287 | tokenid_t t = pDecl->id; |
| 5288 | if (mLocals.isDefinedAtCurrentLevel(t)) { |
| 5289 | reportDuplicate(t); |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 5290 | } |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 5291 | mLocals.add(pDecl); |
Jack Palevich | 303d8ff | 2009-06-11 19:06:24 -0700 | [diff] [blame] | 5292 | } |
| 5293 | |
Jack Palevich | 61de31f | 2009-09-08 11:06:40 -0700 | [diff] [blame] | 5294 | bool checkUndeclaredStruct(Type* pBaseType) { |
| 5295 | if (pBaseType->tag == TY_STRUCT && pBaseType->length < 0) { |
| 5296 | String temp; |
| 5297 | decodeToken(temp, pBaseType->structTag, false); |
| 5298 | error("Undeclared struct %s", temp.getUnwrapped()); |
| 5299 | return true; |
| 5300 | } |
| 5301 | return false; |
| 5302 | } |
| 5303 | |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 5304 | void localDeclarations(Type* pBaseType) { |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 5305 | intptr_t a; |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 5306 | |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 5307 | while (pBaseType) { |
Jack Palevich | 22e3e8e | 2009-06-12 13:12:55 -0700 | [diff] [blame] | 5308 | while (tok != ';' && tok != EOF) { |
Jack Palevich | 2ff5c22 | 2009-07-23 15:11:22 -0700 | [diff] [blame] | 5309 | Type* pDecl = expectDeclaration(pBaseType); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 5310 | if (!pDecl) { |
| 5311 | break; |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 5312 | } |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 5313 | if (!pDecl->id) { |
| 5314 | break; |
| 5315 | } |
Jack Palevich | 61de31f | 2009-09-08 11:06:40 -0700 | [diff] [blame] | 5316 | if (checkUndeclaredStruct(pDecl)) { |
| 5317 | break; |
| 5318 | } |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 5319 | int variableAddress = 0; |
| 5320 | addLocalSymbol(pDecl); |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 5321 | size_t alignment = pGen->alignmentOf(pDecl); |
| 5322 | assert(alignment > 0); |
Jack Palevich | 7fcdf1c | 2009-07-23 18:56:20 -0700 | [diff] [blame] | 5323 | size_t alignmentMask = ~ (alignment - 1); |
| 5324 | size_t sizeOf = pGen->sizeOf(pDecl); |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 5325 | assert(sizeOf > 0); |
Jack Palevich | 7fcdf1c | 2009-07-23 18:56:20 -0700 | [diff] [blame] | 5326 | loc = (loc + alignment - 1) & alignmentMask; |
| 5327 | size_t alignedSize = (sizeOf + alignment - 1) & alignmentMask; |
| 5328 | loc = loc + alignedSize; |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 5329 | variableAddress = -loc; |
| 5330 | VI(pDecl->id)->pAddress = (void*) variableAddress; |
| 5331 | if (accept('=')) { |
Jack Palevich | d7461a7 | 2009-06-12 14:26:58 -0700 | [diff] [blame] | 5332 | /* assignment */ |
Jack Palevich | b5e3331 | 2009-07-30 19:06:34 -0700 | [diff] [blame] | 5333 | pGen->leaR0(variableAddress, createPtrType(pDecl), ET_LVALUE); |
Jack Palevich | 8968e8e | 2009-07-30 16:57:33 -0700 | [diff] [blame] | 5334 | pGen->pushR0(); |
Jack Palevich | d7461a7 | 2009-06-12 14:26:58 -0700 | [diff] [blame] | 5335 | expr(); |
Jack Palevich | b5e3331 | 2009-07-30 19:06:34 -0700 | [diff] [blame] | 5336 | pGen->forceR0RVal(); |
Jack Palevich | 8968e8e | 2009-07-30 16:57:33 -0700 | [diff] [blame] | 5337 | pGen->storeR0ToTOS(); |
Jack Palevich | d7461a7 | 2009-06-12 14:26:58 -0700 | [diff] [blame] | 5338 | } |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 5339 | if (tok == ',') |
| 5340 | next(); |
| 5341 | } |
| 5342 | skip(';'); |
Jack Palevich | 2ff5c22 | 2009-07-23 15:11:22 -0700 | [diff] [blame] | 5343 | pBaseType = acceptPrimitiveType(); |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 5344 | } |
| 5345 | } |
| 5346 | |
Jack Palevich | f1728be | 2009-06-12 13:53:51 -0700 | [diff] [blame] | 5347 | bool checkSymbol() { |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 5348 | return checkSymbol(tok); |
Jack Palevich | a1804dd | 2009-06-12 14:40:04 -0700 | [diff] [blame] | 5349 | } |
| 5350 | |
Jack Palevich | 37c54bd | 2009-07-14 18:35:36 -0700 | [diff] [blame] | 5351 | void decodeToken(String& buffer, tokenid_t token, bool quote) { |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 5352 | if (token == EOF ) { |
| 5353 | buffer.printf("EOF"); |
| 5354 | } else if (token == TOK_NUM) { |
| 5355 | buffer.printf("numeric constant"); |
| 5356 | } else if (token >= 0 && token < 256) { |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 5357 | if (token < 32) { |
| 5358 | buffer.printf("'\\x%02x'", token); |
| 5359 | } else { |
| 5360 | buffer.printf("'%c'", token); |
| 5361 | } |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 5362 | } else { |
Jack Palevich | 37c54bd | 2009-07-14 18:35:36 -0700 | [diff] [blame] | 5363 | if (quote) { |
| 5364 | if (token >= TOK_KEYWORD && token < TOK_SYMBOL) { |
| 5365 | buffer.printf("keyword \"%s\"", nameof(token)); |
| 5366 | } else { |
| 5367 | buffer.printf("symbol \"%s\"", nameof(token)); |
| 5368 | } |
| 5369 | } else { |
| 5370 | buffer.printf("%s", nameof(token)); |
| 5371 | } |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 5372 | } |
| 5373 | } |
| 5374 | |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 5375 | void printToken(tokenid_t token) { |
| 5376 | String buffer; |
| 5377 | decodeToken(buffer, token, true); |
| 5378 | fprintf(stderr, "%s\n", buffer.getUnwrapped()); |
| 5379 | } |
| 5380 | |
Jack Palevich | 40600de | 2009-07-01 15:32:35 -0700 | [diff] [blame] | 5381 | bool checkSymbol(tokenid_t token) { |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 5382 | bool result = token >= TOK_SYMBOL; |
Jack Palevich | f1728be | 2009-06-12 13:53:51 -0700 | [diff] [blame] | 5383 | if (!result) { |
| 5384 | String temp; |
Jack Palevich | 37c54bd | 2009-07-14 18:35:36 -0700 | [diff] [blame] | 5385 | decodeToken(temp, token, true); |
Jack Palevich | f1728be | 2009-06-12 13:53:51 -0700 | [diff] [blame] | 5386 | error("Expected symbol. Got %s", temp.getUnwrapped()); |
| 5387 | } |
| 5388 | return result; |
| 5389 | } |
| 5390 | |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 5391 | tokenid_t acceptSymbol() { |
| 5392 | tokenid_t result = 0; |
| 5393 | if (tok >= TOK_SYMBOL) { |
| 5394 | result = tok; |
| 5395 | next(); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 5396 | } |
| 5397 | return result; |
| 5398 | } |
| 5399 | |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 5400 | void globalDeclarations() { |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 5401 | mpCurrentSymbolStack = &mGlobals; |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 5402 | while (tok != EOF) { |
Jack Palevich | 2ff5c22 | 2009-07-23 15:11:22 -0700 | [diff] [blame] | 5403 | Type* pBaseType = expectPrimitiveType(); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 5404 | if (!pBaseType) { |
Jack Palevich | f1728be | 2009-06-12 13:53:51 -0700 | [diff] [blame] | 5405 | break; |
| 5406 | } |
Jack Palevich | 2ff5c22 | 2009-07-23 15:11:22 -0700 | [diff] [blame] | 5407 | Type* pDecl = expectDeclaration(pBaseType); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 5408 | if (!pDecl) { |
| 5409 | break; |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 5410 | } |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 5411 | if (!pDecl->id) { |
| 5412 | skip(';'); |
| 5413 | continue; |
| 5414 | } |
| 5415 | |
Jack Palevich | 61de31f | 2009-09-08 11:06:40 -0700 | [diff] [blame] | 5416 | if (checkUndeclaredStruct(pDecl)) { |
| 5417 | skip(';'); |
| 5418 | continue; |
| 5419 | } |
| 5420 | |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 5421 | if (! isDefined(pDecl->id)) { |
| 5422 | addGlobalSymbol(pDecl); |
| 5423 | } |
| 5424 | VariableInfo* name = VI(pDecl->id); |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 5425 | if (name && name->pAddress) { |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 5426 | error("Already defined global %s", nameof(pDecl->id)); |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 5427 | } |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 5428 | if (pDecl->tag < TY_FUNC) { |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 5429 | // it's a variable declaration |
| 5430 | for(;;) { |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 5431 | if (name && !name->pAddress) { |
Jack Palevich | 9cbd226 | 2009-07-08 16:48:41 -0700 | [diff] [blame] | 5432 | name->pAddress = (int*) allocGlobalSpace( |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 5433 | pGen->alignmentOf(name->pType), |
Jack Palevich | 9cbd226 | 2009-07-08 16:48:41 -0700 | [diff] [blame] | 5434 | pGen->sizeOf(name->pType)); |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 5435 | } |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 5436 | if (accept('=')) { |
Jack Palevich | d7461a7 | 2009-06-12 14:26:58 -0700 | [diff] [blame] | 5437 | if (tok == TOK_NUM) { |
| 5438 | if (name) { |
| 5439 | * (int*) name->pAddress = tokc; |
| 5440 | } |
| 5441 | next(); |
| 5442 | } else { |
| 5443 | error("Expected an integer constant"); |
| 5444 | } |
| 5445 | } |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 5446 | if (!accept(',')) { |
Jack Palevich | b7c81e9 | 2009-06-04 19:56:13 -0700 | [diff] [blame] | 5447 | break; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 5448 | } |
Jack Palevich | 2ff5c22 | 2009-07-23 15:11:22 -0700 | [diff] [blame] | 5449 | pDecl = expectDeclaration(pBaseType); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 5450 | if (!pDecl) { |
| 5451 | break; |
| 5452 | } |
| 5453 | if (! isDefined(pDecl->id)) { |
| 5454 | addGlobalSymbol(pDecl); |
| 5455 | } |
| 5456 | name = VI(pDecl->id); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 5457 | } |
| 5458 | skip(';'); |
| 5459 | } else { |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 5460 | // Function declaration |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 5461 | if (accept(';')) { |
| 5462 | // forward declaration. |
Jack Palevich | d1f57e6 | 2009-07-15 18:23:22 -0700 | [diff] [blame] | 5463 | } else if (tok != '{') { |
| 5464 | error("expected '{'"); |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 5465 | } else { |
Jack Palevich | 2ff5c22 | 2009-07-23 15:11:22 -0700 | [diff] [blame] | 5466 | mpCurrentArena = &mLocalArena; |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 5467 | mpCurrentSymbolStack = &mLocals; |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 5468 | if (name) { |
Jack Palevich | 9f51a26 | 2009-07-29 16:22:26 -0700 | [diff] [blame] | 5469 | /* patch forward references */ |
| 5470 | pGen->resolveForward((int) name->pForward); |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 5471 | /* put function address */ |
| 5472 | name->pAddress = (void*) codeBuf.getPC(); |
| 5473 | } |
| 5474 | // Calculate stack offsets for parameters |
| 5475 | mLocals.pushLevel(); |
| 5476 | intptr_t a = 8; |
| 5477 | int argCount = 0; |
| 5478 | for (Type* pP = pDecl->pTail; pP; pP = pP->pTail) { |
| 5479 | Type* pArg = pP->pHead; |
Jack Palevich | 0a01a5d | 2009-08-19 10:53:43 -0700 | [diff] [blame] | 5480 | if (pArg->id) { |
| 5481 | addLocalSymbol(pArg); |
| 5482 | } |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 5483 | /* read param name and compute offset */ |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 5484 | Type* pPassingType = passingType(pArg); |
| 5485 | size_t alignment = pGen->alignmentOf(pPassingType); |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 5486 | a = (a + alignment - 1) & ~ (alignment-1); |
Jack Palevich | 0a01a5d | 2009-08-19 10:53:43 -0700 | [diff] [blame] | 5487 | if (pArg->id) { |
| 5488 | VI(pArg->id)->pAddress = (void*) a; |
| 5489 | } |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 5490 | a = a + pGen->sizeOf(pPassingType); |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 5491 | argCount++; |
| 5492 | } |
| 5493 | rsym = loc = 0; |
Jack Palevich | 8df4619 | 2009-07-07 14:48:51 -0700 | [diff] [blame] | 5494 | pReturnType = pDecl->pHead; |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 5495 | a = pGen->functionEntry(pDecl); |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 5496 | block(0, true); |
| 5497 | pGen->gsym(rsym); |
Jack Palevich | b7718b9 | 2009-07-09 22:00:24 -0700 | [diff] [blame] | 5498 | pGen->functionExit(pDecl, a, loc); |
Jack Palevich | 95727a0 | 2009-07-06 12:07:15 -0700 | [diff] [blame] | 5499 | mLocals.popLevel(); |
Jack Palevich | 2ff5c22 | 2009-07-23 15:11:22 -0700 | [diff] [blame] | 5500 | mpCurrentArena = &mGlobalArena; |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 5501 | mpCurrentSymbolStack = &mGlobals; |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 5502 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 5503 | } |
| 5504 | } |
| 5505 | } |
| 5506 | |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 5507 | Type* passingType(Type* pType) { |
| 5508 | switch (pType->tag) { |
| 5509 | case TY_CHAR: |
| 5510 | case TY_SHORT: |
| 5511 | return mkpInt; |
| 5512 | default: |
| 5513 | return pType; |
| 5514 | } |
| 5515 | } |
| 5516 | |
Jack Palevich | 9cbd226 | 2009-07-08 16:48:41 -0700 | [diff] [blame] | 5517 | char* allocGlobalSpace(size_t alignment, size_t bytes) { |
| 5518 | size_t base = (((size_t) glo) + alignment - 1) & ~(alignment-1); |
| 5519 | size_t end = base + bytes; |
Jack Palevich | a39749f | 2009-07-08 20:40:31 -0700 | [diff] [blame] | 5520 | if ((end - (size_t) pGlobalBase) > (size_t) ALLOC_SIZE) { |
Jack Palevich | f1f39cc | 2009-05-29 18:03:15 -0700 | [diff] [blame] | 5521 | error("Global space exhausted"); |
Jack Palevich | 9221bcc | 2009-08-26 16:15:07 -0700 | [diff] [blame] | 5522 | assert(false); |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 5523 | return NULL; |
Jack Palevich | f1f39cc | 2009-05-29 18:03:15 -0700 | [diff] [blame] | 5524 | } |
Jack Palevich | 9cbd226 | 2009-07-08 16:48:41 -0700 | [diff] [blame] | 5525 | char* result = (char*) base; |
| 5526 | glo = (char*) end; |
Jack Palevich | f1f39cc | 2009-05-29 18:03:15 -0700 | [diff] [blame] | 5527 | return result; |
| 5528 | } |
| 5529 | |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 5530 | void cleanup() { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 5531 | if (pGlobalBase != 0) { |
Jack Palevich | f1f39cc | 2009-05-29 18:03:15 -0700 | [diff] [blame] | 5532 | free(pGlobalBase); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 5533 | pGlobalBase = 0; |
| 5534 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 5535 | if (pGen) { |
| 5536 | delete pGen; |
| 5537 | pGen = 0; |
| 5538 | } |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 5539 | if (file) { |
| 5540 | delete file; |
| 5541 | file = 0; |
| 5542 | } |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 5543 | } |
| 5544 | |
Jack Palevich | 8c246a9 | 2009-07-14 21:14:10 -0700 | [diff] [blame] | 5545 | // One-time initialization, when class is constructed. |
| 5546 | void init() { |
| 5547 | mpSymbolLookupFn = 0; |
| 5548 | mpSymbolLookupContext = 0; |
| 5549 | } |
| 5550 | |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 5551 | void clear() { |
| 5552 | tok = 0; |
| 5553 | tokc = 0; |
| 5554 | tokl = 0; |
| 5555 | ch = 0; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 5556 | rsym = 0; |
| 5557 | loc = 0; |
| 5558 | glo = 0; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 5559 | dptr = 0; |
| 5560 | dch = 0; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 5561 | file = 0; |
| 5562 | pGlobalBase = 0; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 5563 | pGen = 0; |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 5564 | mPragmaStringCount = 0; |
Jack Palevich | ce105a9 | 2009-07-16 14:30:33 -0700 | [diff] [blame] | 5565 | mCompileResult = 0; |
Jack Palevich | dc45646 | 2009-07-16 16:50:56 -0700 | [diff] [blame] | 5566 | mLineNumber = 1; |
| 5567 | mbBumpLine = false; |
Jack Palevich | 815d8b8 | 2009-08-18 18:25:56 -0700 | [diff] [blame] | 5568 | mbSuppressMacroExpansion = false; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 5569 | } |
Jack Palevich | e27bf3e | 2009-05-10 14:09:03 -0700 | [diff] [blame] | 5570 | |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 5571 | void setArchitecture(const char* architecture) { |
| 5572 | delete pGen; |
| 5573 | pGen = 0; |
| 5574 | |
| 5575 | if (architecture != NULL) { |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 5576 | #ifdef PROVIDE_ARM_CODEGEN |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 5577 | if (! pGen && strcmp(architecture, "arm") == 0) { |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 5578 | pGen = new ARMCodeGenerator(); |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 5579 | } |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 5580 | #endif |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 5581 | #ifdef PROVIDE_X86_CODEGEN |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 5582 | if (! pGen && strcmp(architecture, "x86") == 0) { |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 5583 | pGen = new X86CodeGenerator(); |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 5584 | } |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 5585 | #endif |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 5586 | if (!pGen ) { |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 5587 | error("Unknown architecture %s\n", architecture); |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 5588 | } |
| 5589 | } |
| 5590 | |
| 5591 | if (pGen == NULL) { |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 5592 | #if defined(DEFAULT_ARM_CODEGEN) |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 5593 | pGen = new ARMCodeGenerator(); |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 5594 | #elif defined(DEFAULT_X86_CODEGEN) |
| 5595 | pGen = new X86CodeGenerator(); |
| 5596 | #endif |
| 5597 | } |
| 5598 | if (pGen == NULL) { |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 5599 | error("No code generator defined."); |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 5600 | } else { |
| 5601 | pGen->setErrorSink(this); |
Jack Palevich | a8f427f | 2009-07-13 18:40:08 -0700 | [diff] [blame] | 5602 | pGen->setTypes(mkpInt); |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 5603 | } |
| 5604 | } |
| 5605 | |
Jack Palevich | 77ae76e | 2009-05-10 19:59:24 -0700 | [diff] [blame] | 5606 | public: |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 5607 | struct args { |
| 5608 | args() { |
| 5609 | architecture = 0; |
| 5610 | } |
| 5611 | const char* architecture; |
| 5612 | }; |
| 5613 | |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 5614 | Compiler() { |
Jack Palevich | 8c246a9 | 2009-07-14 21:14:10 -0700 | [diff] [blame] | 5615 | init(); |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 5616 | clear(); |
Jack Palevich | e27bf3e | 2009-05-10 14:09:03 -0700 | [diff] [blame] | 5617 | } |
Jack Palevich | bbf8ab5 | 2009-05-11 11:54:30 -0700 | [diff] [blame] | 5618 | |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 5619 | ~Compiler() { |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 5620 | cleanup(); |
| 5621 | } |
| 5622 | |
Jack Palevich | 8c246a9 | 2009-07-14 21:14:10 -0700 | [diff] [blame] | 5623 | void registerSymbolCallback(ACCSymbolLookupFn pFn, ACCvoid* pContext) { |
| 5624 | mpSymbolLookupFn = pFn; |
| 5625 | mpSymbolLookupContext = pContext; |
| 5626 | } |
| 5627 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 5628 | int compile(const char* text, size_t textLength) { |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 5629 | int result; |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 5630 | |
Jack Palevich | 2ff5c22 | 2009-07-23 15:11:22 -0700 | [diff] [blame] | 5631 | mpCurrentArena = &mGlobalArena; |
Jack Palevich | a8f427f | 2009-07-13 18:40:08 -0700 | [diff] [blame] | 5632 | createPrimitiveTypes(); |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 5633 | cleanup(); |
| 5634 | clear(); |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 5635 | mTokenTable.setArena(&mGlobalArena); |
| 5636 | mGlobals.setArena(&mGlobalArena); |
| 5637 | mGlobals.setTokenTable(&mTokenTable); |
| 5638 | mLocals.setArena(&mLocalArena); |
| 5639 | mLocals.setTokenTable(&mTokenTable); |
| 5640 | |
| 5641 | internKeywords(); |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 5642 | codeBuf.init(ALLOC_SIZE); |
| 5643 | setArchitecture(NULL); |
| 5644 | if (!pGen) { |
| 5645 | return -1; |
| 5646 | } |
Jack Palevich | b67b18f | 2009-06-11 21:12:23 -0700 | [diff] [blame] | 5647 | #ifdef PROVIDE_TRACE_CODEGEN |
| 5648 | pGen = new TraceCodeGenerator(pGen); |
| 5649 | #endif |
| 5650 | pGen->setErrorSink(this); |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 5651 | pGen->init(&codeBuf); |
| 5652 | file = new TextInputStream(text, textLength); |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 5653 | pGlobalBase = (char*) calloc(1, ALLOC_SIZE); |
| 5654 | glo = pGlobalBase; |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 5655 | inp(); |
| 5656 | next(); |
| 5657 | globalDeclarations(); |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 5658 | checkForUndefinedForwardReferences(); |
Jack Palevich | 0a280a0 | 2009-06-11 10:53:51 -0700 | [diff] [blame] | 5659 | result = pGen->finishCompile(); |
| 5660 | if (result == 0) { |
| 5661 | if (mErrorBuf.len()) { |
| 5662 | result = -2; |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 5663 | } |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 5664 | } |
Jack Palevich | ce105a9 | 2009-07-16 14:30:33 -0700 | [diff] [blame] | 5665 | mCompileResult = result; |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 5666 | return result; |
Jack Palevich | 21a15a2 | 2009-05-11 14:49:29 -0700 | [diff] [blame] | 5667 | } |
| 5668 | |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 5669 | void createPrimitiveTypes() { |
Jack Palevich | 2ff5c22 | 2009-07-23 15:11:22 -0700 | [diff] [blame] | 5670 | mkpInt = createType(TY_INT, NULL, NULL); |
Jack Palevich | c9b8ffc | 2009-08-03 14:42:57 -0700 | [diff] [blame] | 5671 | mkpShort = createType(TY_SHORT, NULL, NULL); |
Jack Palevich | 2ff5c22 | 2009-07-23 15:11:22 -0700 | [diff] [blame] | 5672 | mkpChar = createType(TY_CHAR, NULL, NULL); |
| 5673 | mkpVoid = createType(TY_VOID, NULL, NULL); |
| 5674 | mkpFloat = createType(TY_FLOAT, NULL, NULL); |
| 5675 | mkpDouble = createType(TY_DOUBLE, NULL, NULL); |
| 5676 | mkpIntFn = createType(TY_FUNC, mkpInt, NULL); |
| 5677 | mkpIntPtr = createPtrType(mkpInt); |
| 5678 | mkpCharPtr = createPtrType(mkpChar); |
| 5679 | mkpFloatPtr = createPtrType(mkpFloat); |
| 5680 | mkpDoublePtr = createPtrType(mkpDouble); |
| 5681 | mkpPtrIntFn = createPtrType(mkpIntFn); |
Jack Palevich | 8635198 | 2009-06-30 18:09:56 -0700 | [diff] [blame] | 5682 | } |
| 5683 | |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 5684 | void checkForUndefinedForwardReferences() { |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 5685 | mGlobals.forEach(static_ufrcFn, this); |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 5686 | } |
| 5687 | |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 5688 | static bool static_ufrcFn(VariableInfo* value, void* context) { |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 5689 | Compiler* pCompiler = (Compiler*) context; |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 5690 | return pCompiler->undefinedForwardReferenceCheck(value); |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 5691 | } |
| 5692 | |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 5693 | bool undefinedForwardReferenceCheck(VariableInfo* value) { |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 5694 | if (!value->pAddress && value->pForward) { |
Jack Palevich | 569f135 | 2009-06-29 14:29:08 -0700 | [diff] [blame] | 5695 | error("Undefined forward reference: %s", |
| 5696 | mTokenTable[value->tok].pText); |
Jack Palevich | a6baa23 | 2009-06-12 11:25:59 -0700 | [diff] [blame] | 5697 | } |
| 5698 | return true; |
| 5699 | } |
| 5700 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 5701 | /* Look through the symbol table to find a symbol. |
| 5702 | * If found, return its value. |
| 5703 | */ |
| 5704 | void* lookup(const char* name) { |
Jack Palevich | ce105a9 | 2009-07-16 14:30:33 -0700 | [diff] [blame] | 5705 | if (mCompileResult == 0) { |
| 5706 | tokenid_t tok = mTokenTable.intern(name, strlen(name)); |
| 5707 | VariableInfo* pVariableInfo = VI(tok); |
| 5708 | if (pVariableInfo) { |
| 5709 | return pVariableInfo->pAddress; |
| 5710 | } |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 5711 | } |
| 5712 | return NULL; |
| 5713 | } |
| 5714 | |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 5715 | void getPragmas(ACCsizei* actualStringCount, |
| 5716 | ACCsizei maxStringCount, ACCchar** strings) { |
| 5717 | int stringCount = mPragmaStringCount; |
| 5718 | if (actualStringCount) { |
| 5719 | *actualStringCount = stringCount; |
| 5720 | } |
| 5721 | if (stringCount > maxStringCount) { |
| 5722 | stringCount = maxStringCount; |
| 5723 | } |
| 5724 | if (strings) { |
| 5725 | char* pPragmas = mPragmas.getUnwrapped(); |
| 5726 | while (stringCount-- > 0) { |
| 5727 | *strings++ = pPragmas; |
| 5728 | pPragmas += strlen(pPragmas) + 1; |
| 5729 | } |
| 5730 | } |
| 5731 | } |
| 5732 | |
Jack Palevich | d531557 | 2009-09-09 13:19:34 -0700 | [diff] [blame^] | 5733 | void getProgramBinary(ACCvoid** base, ACCsizei* length) { |
| 5734 | *base = codeBuf.getBase(); |
| 5735 | *length = (ACCsizei) codeBuf.getSize(); |
| 5736 | } |
| 5737 | |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 5738 | char* getErrorMessage() { |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 5739 | return mErrorBuf.getUnwrapped(); |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 5740 | } |
Jack Palevich | 77ae76e | 2009-05-10 19:59:24 -0700 | [diff] [blame] | 5741 | }; |
| 5742 | |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 5743 | const char* Compiler::operatorChars = |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 5744 | "++--*@/@%@+@-@<<>><=>=<@>@==!=&&||&@^@|@~@!@"; |
| 5745 | |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 5746 | const char Compiler::operatorLevel[] = |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 5747 | {11, 11, 1, 1, 1, 2, 2, 3, 3, 4, 4, 4, 4, |
| 5748 | 5, 5, /* ==, != */ |
| 5749 | 9, 10, /* &&, || */ |
| 5750 | 6, 7, 8, /* & ^ | */ |
| 5751 | 2, 2 /* ~ ! */ |
| 5752 | }; |
| 5753 | |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 5754 | #ifdef PROVIDE_X86_CODEGEN |
Jack Palevich | e7b5906 | 2009-05-19 17:12:17 -0700 | [diff] [blame] | 5755 | const int Compiler::X86CodeGenerator::operatorHelper[] = { |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 5756 | 0x1, // ++ |
| 5757 | 0xff, // -- |
| 5758 | 0xc1af0f, // * |
| 5759 | 0xf9f79991, // / |
| 5760 | 0xf9f79991, // % (With manual assist to swap results) |
| 5761 | 0xc801, // + |
| 5762 | 0xd8f7c829, // - |
| 5763 | 0xe0d391, // << |
| 5764 | 0xf8d391, // >> |
| 5765 | 0xe, // <= |
| 5766 | 0xd, // >= |
| 5767 | 0xc, // < |
| 5768 | 0xf, // > |
| 5769 | 0x4, // == |
| 5770 | 0x5, // != |
| 5771 | 0x0, // && |
| 5772 | 0x1, // || |
| 5773 | 0xc821, // & |
| 5774 | 0xc831, // ^ |
| 5775 | 0xc809, // | |
| 5776 | 0xd0f7, // ~ |
| 5777 | 0x4 // ! |
| 5778 | }; |
Jack Palevich | 8b0624c | 2009-05-20 12:12:06 -0700 | [diff] [blame] | 5779 | #endif |
Jack Palevich | bf42c9c | 2009-05-12 12:48:35 -0700 | [diff] [blame] | 5780 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 5781 | struct ACCscript { |
| 5782 | ACCscript() { |
| 5783 | text = 0; |
| 5784 | textLength = 0; |
| 5785 | accError = ACC_NO_ERROR; |
| 5786 | } |
Jack Palevich | bbf8ab5 | 2009-05-11 11:54:30 -0700 | [diff] [blame] | 5787 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 5788 | ~ACCscript() { |
| 5789 | delete text; |
| 5790 | } |
Jack Palevich | 546b224 | 2009-05-13 15:10:04 -0700 | [diff] [blame] | 5791 | |
Jack Palevich | 8c246a9 | 2009-07-14 21:14:10 -0700 | [diff] [blame] | 5792 | void registerSymbolCallback(ACCSymbolLookupFn pFn, ACCvoid* pContext) { |
| 5793 | compiler.registerSymbolCallback(pFn, pContext); |
| 5794 | } |
| 5795 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 5796 | void setError(ACCenum error) { |
| 5797 | if (accError == ACC_NO_ERROR && error != ACC_NO_ERROR) { |
| 5798 | accError = error; |
Jack Palevich | bbf8ab5 | 2009-05-11 11:54:30 -0700 | [diff] [blame] | 5799 | } |
| 5800 | } |
| 5801 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 5802 | ACCenum getError() { |
| 5803 | ACCenum result = accError; |
| 5804 | accError = ACC_NO_ERROR; |
Jack Palevich | 2230513 | 2009-05-13 10:58:45 -0700 | [diff] [blame] | 5805 | return result; |
Jack Palevich | bbf8ab5 | 2009-05-11 11:54:30 -0700 | [diff] [blame] | 5806 | } |
| 5807 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 5808 | Compiler compiler; |
| 5809 | char* text; |
| 5810 | int textLength; |
| 5811 | ACCenum accError; |
| 5812 | }; |
| 5813 | |
| 5814 | |
| 5815 | extern "C" |
| 5816 | ACCscript* accCreateScript() { |
| 5817 | return new ACCscript(); |
Jack Palevich | bbf8ab5 | 2009-05-11 11:54:30 -0700 | [diff] [blame] | 5818 | } |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 5819 | |
| 5820 | extern "C" |
| 5821 | ACCenum accGetError( ACCscript* script ) { |
| 5822 | return script->getError(); |
| 5823 | } |
| 5824 | |
| 5825 | extern "C" |
| 5826 | void accDeleteScript(ACCscript* script) { |
| 5827 | delete script; |
| 5828 | } |
| 5829 | |
| 5830 | extern "C" |
Jack Palevich | 8c246a9 | 2009-07-14 21:14:10 -0700 | [diff] [blame] | 5831 | void accRegisterSymbolCallback(ACCscript* script, ACCSymbolLookupFn pFn, |
| 5832 | ACCvoid* pContext) { |
| 5833 | script->registerSymbolCallback(pFn, pContext); |
| 5834 | } |
| 5835 | |
| 5836 | extern "C" |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 5837 | void accScriptSource(ACCscript* script, |
| 5838 | ACCsizei count, |
| 5839 | const ACCchar ** string, |
| 5840 | const ACCint * length) { |
| 5841 | int totalLength = 0; |
| 5842 | for(int i = 0; i < count; i++) { |
| 5843 | int len = -1; |
| 5844 | const ACCchar* s = string[i]; |
| 5845 | if (length) { |
| 5846 | len = length[i]; |
| 5847 | } |
| 5848 | if (len < 0) { |
| 5849 | len = strlen(s); |
| 5850 | } |
| 5851 | totalLength += len; |
| 5852 | } |
| 5853 | delete script->text; |
| 5854 | char* text = new char[totalLength + 1]; |
| 5855 | script->text = text; |
| 5856 | script->textLength = totalLength; |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 5857 | char* dest = text; |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 5858 | for(int i = 0; i < count; i++) { |
| 5859 | int len = -1; |
| 5860 | const ACCchar* s = string[i]; |
| 5861 | if (length) { |
| 5862 | len = length[i]; |
| 5863 | } |
| 5864 | if (len < 0) { |
| 5865 | len = strlen(s); |
| 5866 | } |
Jack Palevich | 09555c7 | 2009-05-27 12:25:55 -0700 | [diff] [blame] | 5867 | memcpy(dest, s, len); |
| 5868 | dest += len; |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 5869 | } |
| 5870 | text[totalLength] = '\0'; |
Jack Palevich | 61de31f | 2009-09-08 11:06:40 -0700 | [diff] [blame] | 5871 | |
| 5872 | #ifdef DEBUG_SAVE_INPUT_TO_FILE |
Jack Palevich | 9116bc4 | 2009-09-08 11:46:42 -0700 | [diff] [blame] | 5873 | LOGD("Saving input to file..."); |
Jack Palevich | 61de31f | 2009-09-08 11:06:40 -0700 | [diff] [blame] | 5874 | int counter; |
| 5875 | char path[PATH_MAX]; |
| 5876 | for (counter = 0; counter < 4096; counter++) { |
| 5877 | sprintf(path, DEBUG_DUMP_PATTERN, counter); |
| 5878 | if(access(path, F_OK) != 0) { |
| 5879 | break; |
| 5880 | } |
| 5881 | } |
| 5882 | if (counter < 4096) { |
Jack Palevich | 9116bc4 | 2009-09-08 11:46:42 -0700 | [diff] [blame] | 5883 | LOGD("Saving input to file %s", path); |
Jack Palevich | 61de31f | 2009-09-08 11:06:40 -0700 | [diff] [blame] | 5884 | FILE* fd = fopen(path, "w"); |
| 5885 | if (fd) { |
| 5886 | fwrite(text, totalLength, 1, fd); |
| 5887 | fclose(fd); |
Jack Palevich | 9116bc4 | 2009-09-08 11:46:42 -0700 | [diff] [blame] | 5888 | LOGD("Saved input to file %s", path); |
| 5889 | } else { |
| 5890 | LOGD("Could not save. errno: %d", errno); |
Jack Palevich | 61de31f | 2009-09-08 11:06:40 -0700 | [diff] [blame] | 5891 | } |
| 5892 | } |
| 5893 | #endif |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 5894 | } |
| 5895 | |
| 5896 | extern "C" |
| 5897 | void accCompileScript(ACCscript* script) { |
| 5898 | int result = script->compiler.compile(script->text, script->textLength); |
| 5899 | if (result) { |
| 5900 | script->setError(ACC_INVALID_OPERATION); |
| 5901 | } |
| 5902 | } |
| 5903 | |
| 5904 | extern "C" |
| 5905 | void accGetScriptiv(ACCscript* script, |
| 5906 | ACCenum pname, |
| 5907 | ACCint * params) { |
| 5908 | switch (pname) { |
| 5909 | case ACC_INFO_LOG_LENGTH: |
| 5910 | *params = 0; |
| 5911 | break; |
| 5912 | } |
| 5913 | } |
| 5914 | |
| 5915 | extern "C" |
| 5916 | void accGetScriptInfoLog(ACCscript* script, |
| 5917 | ACCsizei maxLength, |
| 5918 | ACCsizei * length, |
| 5919 | ACCchar * infoLog) { |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 5920 | char* message = script->compiler.getErrorMessage(); |
| 5921 | int messageLength = strlen(message) + 1; |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 5922 | if (length) { |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 5923 | *length = messageLength; |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 5924 | } |
Jack Palevich | ac0e95e | 2009-05-29 13:53:44 -0700 | [diff] [blame] | 5925 | if (infoLog && maxLength > 0) { |
| 5926 | int trimmedLength = maxLength < messageLength ? |
| 5927 | maxLength : messageLength; |
| 5928 | memcpy(infoLog, message, trimmedLength); |
| 5929 | infoLog[trimmedLength] = 0; |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 5930 | } |
| 5931 | } |
| 5932 | |
| 5933 | extern "C" |
| 5934 | void accGetScriptLabel(ACCscript* script, const ACCchar * name, |
| 5935 | ACCvoid ** address) { |
| 5936 | void* value = script->compiler.lookup(name); |
| 5937 | if (value) { |
| 5938 | *address = value; |
| 5939 | } else { |
| 5940 | script->setError(ACC_INVALID_VALUE); |
| 5941 | } |
| 5942 | } |
| 5943 | |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 5944 | extern "C" |
| 5945 | void accGetPragmas(ACCscript* script, ACCsizei* actualStringCount, |
| 5946 | ACCsizei maxStringCount, ACCchar** strings){ |
| 5947 | script->compiler.getPragmas(actualStringCount, maxStringCount, strings); |
| 5948 | } |
| 5949 | |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 5950 | extern "C" |
Jack Palevich | d531557 | 2009-09-09 13:19:34 -0700 | [diff] [blame^] | 5951 | void accGetProgramBinary(ACCscript* script, |
| 5952 | ACCvoid** base, ACCsizei* length) { |
| 5953 | script->compiler.getProgramBinary(base, length); |
-b master | 422972c | 2009-06-17 19:13:52 -0700 | [diff] [blame] | 5954 | } |
| 5955 | |
Jack Palevich | eedf9d2 | 2009-06-04 16:23:40 -0700 | [diff] [blame] | 5956 | |
Jack Palevich | 1cdef20 | 2009-05-22 12:06:27 -0700 | [diff] [blame] | 5957 | } // namespace acc |
| 5958 | |