blob: 5731f3151c1e79c479cfef5f0190626bc1c10fab [file] [log] [blame]
Jack Palevichae54f1f2009-05-08 14:54:15 -07001/*
Jack Paleviche7b59062009-05-19 17:12:17 -07002 * 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 Palevich1cdef202009-05-22 12:06:27 -07007 * Obfuscated Tiny C compiler, see the file LICENSE for details.
Jack Paleviche7b59062009-05-19 17:12:17 -07008 *
9 */
10
Jack Palevich77ae76e2009-05-10 19:59:24 -070011#include <ctype.h>
12#include <dlfcn.h>
Jack Palevich8dc662e2009-06-09 22:53:47 +000013#include <errno.h>
Jack Paleviche27bf3e2009-05-10 14:09:03 -070014#include <stdarg.h>
Jack Palevich8b0624c2009-05-20 12:12:06 -070015#include <stdint.h>
Jack Palevichae54f1f2009-05-08 14:54:15 -070016#include <stdio.h>
Jack Palevichf6b5a532009-05-10 19:16:42 -070017#include <stdlib.h>
18#include <string.h>
Jack Palevich2d11dfb2009-06-08 14:34:26 -070019#include <cutils/hashmap.h>
Jack Palevichae54f1f2009-05-08 14:54:15 -070020
Jack Palevich8dc662e2009-06-09 22:53:47 +000021#if defined(__i386__)
22#include <sys/mman.h>
23#endif
24
Jack Palevich546b2242009-05-13 15:10:04 -070025#if defined(__arm__)
26#include <unistd.h>
27#endif
28
Jack Paleviche7b59062009-05-19 17:12:17 -070029#if defined(__arm__)
30#define DEFAULT_ARM_CODEGEN
Jack Palevich8b0624c2009-05-20 12:12:06 -070031#define PROVIDE_ARM_CODEGEN
Jack Paleviche7b59062009-05-19 17:12:17 -070032#elif defined(__i386__)
33#define DEFAULT_X86_CODEGEN
Jack Palevich8b0624c2009-05-20 12:12:06 -070034#define PROVIDE_X86_CODEGEN
Jack Paleviche7b59062009-05-19 17:12:17 -070035#elif defined(__x86_64__)
36#define DEFAULT_X64_CODEGEN
Jack Palevich8b0624c2009-05-20 12:12:06 -070037#define PROVIDE_X64_CODEGEN
Jack Paleviche7b59062009-05-19 17:12:17 -070038#endif
39
Jack Paleviche7b59062009-05-19 17:12:17 -070040
41#ifdef PROVIDE_ARM_CODEGEN
Jack Palevicha6535612009-05-13 16:24:17 -070042#include "disassem.h"
Jack Paleviche7b59062009-05-19 17:12:17 -070043#endif
Jack Palevicha6535612009-05-13 16:24:17 -070044
Jack Palevich1cdef202009-05-22 12:06:27 -070045#include <acc/acc.h>
46
Jack Palevich09555c72009-05-27 12:25:55 -070047#define LOG_API(...) do {} while(0)
48// #define LOG_API(...) fprintf (stderr, __VA_ARGS__)
Jack Palevich09555c72009-05-27 12:25:55 -070049
-b master422972c2009-06-17 19:13:52 -070050#define LOG_STACK(...) do {} while(0)
51// #define LOG_STACK(...) fprintf (stderr, __VA_ARGS__)
52
53// #define ENABLE_ARM_DISASSEMBLY
Jack Palevichb67b18f2009-06-11 21:12:23 -070054// #define PROVIDE_TRACE_CODEGEN
55
Jack Palevichbbf8ab52009-05-11 11:54:30 -070056namespace acc {
57
Jack Palevichac0e95e2009-05-29 13:53:44 -070058class ErrorSink {
59public:
60 void error(const char *fmt, ...) {
61 va_list ap;
62 va_start(ap, fmt);
63 verror(fmt, ap);
64 va_end(ap);
65 }
66
67 virtual void verror(const char* fmt, va_list ap) = 0;
68};
69
70class Compiler : public ErrorSink {
Jack Palevich21a15a22009-05-11 14:49:29 -070071 class CodeBuf {
Jack Palevich653f42d2009-05-28 17:15:32 -070072 char* ind; // Output code pointer
Jack Palevich21a15a22009-05-11 14:49:29 -070073 char* pProgramBase;
Jack Palevichac0e95e2009-05-29 13:53:44 -070074 ErrorSink* mErrorSink;
75 int mSize;
Jack Palevich0a280a02009-06-11 10:53:51 -070076 bool mOverflowed;
Jack Palevichf0cbc922009-05-08 16:35:13 -070077
Jack Palevich21a15a22009-05-11 14:49:29 -070078 void release() {
79 if (pProgramBase != 0) {
80 free(pProgramBase);
81 pProgramBase = 0;
Jack Palevichae54f1f2009-05-08 14:54:15 -070082 }
Jack Palevich21a15a22009-05-11 14:49:29 -070083 }
84
Jack Palevich0a280a02009-06-11 10:53:51 -070085 bool check(int n) {
Jack Palevichac0e95e2009-05-29 13:53:44 -070086 int newSize = ind - pProgramBase + n;
Jack Palevich0a280a02009-06-11 10:53:51 -070087 bool overflow = newSize > mSize;
88 if (overflow && !mOverflowed) {
89 mOverflowed = true;
Jack Palevichac0e95e2009-05-29 13:53:44 -070090 if (mErrorSink) {
91 mErrorSink->error("Code too large: %d bytes", newSize);
92 }
93 }
Jack Palevich0a280a02009-06-11 10:53:51 -070094 return overflow;
Jack Palevichac0e95e2009-05-29 13:53:44 -070095 }
96
Jack Palevich21a15a22009-05-11 14:49:29 -070097 public:
98 CodeBuf() {
99 pProgramBase = 0;
100 ind = 0;
Jack Palevichac0e95e2009-05-29 13:53:44 -0700101 mErrorSink = 0;
102 mSize = 0;
Jack Palevich0a280a02009-06-11 10:53:51 -0700103 mOverflowed = false;
Jack Palevich21a15a22009-05-11 14:49:29 -0700104 }
105
106 ~CodeBuf() {
107 release();
108 }
109
110 void init(int size) {
111 release();
Jack Palevichac0e95e2009-05-29 13:53:44 -0700112 mSize = size;
Jack Palevich21a15a22009-05-11 14:49:29 -0700113 pProgramBase = (char*) calloc(1, size);
114 ind = pProgramBase;
115 }
116
Jack Palevichac0e95e2009-05-29 13:53:44 -0700117 void setErrorSink(ErrorSink* pErrorSink) {
118 mErrorSink = pErrorSink;
119 }
120
Jack Palevich546b2242009-05-13 15:10:04 -0700121 int o4(int n) {
Jack Palevich0a280a02009-06-11 10:53:51 -0700122 if(check(4)) {
123 return 0;
124 }
Jack Palevich8b0624c2009-05-20 12:12:06 -0700125 intptr_t result = (intptr_t) ind;
Jack Palevich546b2242009-05-13 15:10:04 -0700126 * (int*) ind = n;
127 ind += 4;
128 return result;
129 }
130
Jack Palevich21a15a22009-05-11 14:49:29 -0700131 /*
132 * Output a byte. Handles all values, 0..ff.
133 */
134 void ob(int n) {
Jack Palevich0a280a02009-06-11 10:53:51 -0700135 if(check(1)) {
136 return;
137 }
Jack Palevich21a15a22009-05-11 14:49:29 -0700138 *ind++ = n;
139 }
140
Jack Palevich21a15a22009-05-11 14:49:29 -0700141 inline void* getBase() {
142 return (void*) pProgramBase;
143 }
144
Jack Palevich8b0624c2009-05-20 12:12:06 -0700145 intptr_t getSize() {
Jack Palevich21a15a22009-05-11 14:49:29 -0700146 return ind - pProgramBase;
147 }
148
Jack Palevich8b0624c2009-05-20 12:12:06 -0700149 intptr_t getPC() {
150 return (intptr_t) ind;
Jack Palevich21a15a22009-05-11 14:49:29 -0700151 }
152 };
153
Jack Palevich1cdef202009-05-22 12:06:27 -0700154 /**
155 * A code generator creates an in-memory program, generating the code on
156 * the fly. There is one code generator implementation for each supported
157 * architecture.
158 *
159 * The code generator implements the following abstract machine:
160 * R0 - the main accumulator.
161 * R1 - the secondary accumulator.
162 * FP - a frame pointer for accessing function arguments and local
163 * variables.
164 * SP - a stack pointer for storing intermediate results while evaluating
165 * expressions. The stack pointer grows downwards.
166 *
167 * The function calling convention is that all arguments are placed on the
168 * stack such that the first argument has the lowest address.
169 * After the call, the result is in R0. The caller is responsible for
170 * removing the arguments from the stack.
171 * The R0 and R1 registers are not saved across function calls. The
172 * FP and SP registers are saved.
173 */
174
Jack Palevich21a15a22009-05-11 14:49:29 -0700175 class CodeGenerator {
176 public:
Jack Palevichac0e95e2009-05-29 13:53:44 -0700177 CodeGenerator() {
178 mErrorSink = 0;
179 pCodeBuf = 0;
180 }
Jack Palevich21a15a22009-05-11 14:49:29 -0700181 virtual ~CodeGenerator() {}
182
Jack Palevich22305132009-05-13 10:58:45 -0700183 virtual void init(CodeBuf* pCodeBuf) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700184 this->pCodeBuf = pCodeBuf;
Jack Palevichac0e95e2009-05-29 13:53:44 -0700185 pCodeBuf->setErrorSink(mErrorSink);
186 }
187
Jack Palevichb67b18f2009-06-11 21:12:23 -0700188 virtual void setErrorSink(ErrorSink* pErrorSink) {
Jack Palevichac0e95e2009-05-29 13:53:44 -0700189 mErrorSink = pErrorSink;
190 if (pCodeBuf) {
191 pCodeBuf->setErrorSink(mErrorSink);
192 }
Jack Palevich21a15a22009-05-11 14:49:29 -0700193 }
194
Jack Palevich1cdef202009-05-22 12:06:27 -0700195 /* Emit a function prolog.
196 * argCount is the number of arguments.
197 * Save the old value of the FP.
198 * Set the new value of the FP.
199 * Convert from the native platform calling convention to
200 * our stack-based calling convention. This may require
201 * pushing arguments from registers to the stack.
202 * Allocate "N" bytes of stack space. N isn't known yet, so
203 * just emit the instructions for adjusting the stack, and return
204 * the address to patch up. The patching will be done in
205 * functionExit().
206 * returns address to patch with local variable size.
Jack Palevich22305132009-05-13 10:58:45 -0700207 */
Jack Palevich546b2242009-05-13 15:10:04 -0700208 virtual int functionEntry(int argCount) = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700209
Jack Palevich1cdef202009-05-22 12:06:27 -0700210 /* Emit a function epilog.
211 * Restore the old SP and FP register values.
212 * Return to the calling function.
213 * argCount - the number of arguments to the function.
214 * localVariableAddress - returned from functionEntry()
215 * localVariableSize - the size in bytes of the local variables.
216 */
217 virtual void functionExit(int argCount, int localVariableAddress,
218 int localVariableSize) = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700219
Jack Palevich1cdef202009-05-22 12:06:27 -0700220 /* load immediate value to R0 */
Jack Palevich546b2242009-05-13 15:10:04 -0700221 virtual void li(int t) = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700222
Jack Palevich1cdef202009-05-22 12:06:27 -0700223 /* Jump to a target, and return the address of the word that
224 * holds the target data, in case it needs to be fixed up later.
225 */
Jack Palevich22305132009-05-13 10:58:45 -0700226 virtual int gjmp(int t) = 0;
227
Jack Palevich1cdef202009-05-22 12:06:27 -0700228 /* Test R0 and jump to a target if the test succeeds.
229 * l = 0: je, l == 1: jne
230 * Return the address of the word that holds the targed data, in
231 * case it needs to be fixed up later.
232 */
Jack Palevich22305132009-05-13 10:58:45 -0700233 virtual int gtst(bool l, int t) = 0;
234
Jack Palevich1cdef202009-05-22 12:06:27 -0700235 /* Compare R1 against R0, and store the boolean result in R0.
236 * op specifies the comparison.
237 */
Jack Palevich22305132009-05-13 10:58:45 -0700238 virtual void gcmp(int op) = 0;
239
Jack Palevich1cdef202009-05-22 12:06:27 -0700240 /* Perform the arithmetic op specified by op. R1 is the
241 * left argument, R0 is the right argument.
242 */
Jack Palevich546b2242009-05-13 15:10:04 -0700243 virtual void genOp(int op) = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700244
Jack Palevich1cdef202009-05-22 12:06:27 -0700245 /* Set R1 to 0.
246 */
247 virtual void clearR1() = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700248
Jack Palevich1cdef202009-05-22 12:06:27 -0700249 /* Push R0 onto the stack.
250 */
251 virtual void pushR0() = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700252
Jack Palevich1cdef202009-05-22 12:06:27 -0700253 /* Pop R1 off of the stack.
254 */
255 virtual void popR1() = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700256
Jack Palevich1cdef202009-05-22 12:06:27 -0700257 /* Store R0 to the address stored in R1.
258 * isInt is true if a whole 4-byte integer value
259 * should be stored, otherwise a 1-byte character
260 * value should be stored.
261 */
262 virtual void storeR0ToR1(bool isInt) = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700263
Jack Palevich1cdef202009-05-22 12:06:27 -0700264 /* Load R0 from the address stored in R0.
265 * isInt is true if a whole 4-byte integer value
266 * should be loaded, otherwise a 1-byte character
267 * value should be loaded.
268 */
269 virtual void loadR0FromR0(bool isInt) = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700270
Jack Palevich1cdef202009-05-22 12:06:27 -0700271 /* Load the absolute address of a variable to R0.
272 * If ea <= LOCAL, then this is a local variable, or an
273 * argument, addressed relative to FP.
274 * else it is an absolute global address.
275 */
276 virtual void leaR0(int ea) = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700277
Jack Palevich1cdef202009-05-22 12:06:27 -0700278 /* Store R0 to a variable.
279 * If ea <= LOCAL, then this is a local variable, or an
280 * argument, addressed relative to FP.
281 * else it is an absolute global address.
282 */
283 virtual void storeR0(int ea) = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700284
Jack Palevich1cdef202009-05-22 12:06:27 -0700285 /* load R0 from a variable.
286 * If ea <= LOCAL, then this is a local variable, or an
287 * argument, addressed relative to FP.
288 * else it is an absolute global address.
289 * If isIncDec is true, then the stored variable's value
290 * should be post-incremented or post-decremented, based
291 * on the value of op.
292 */
293 virtual void loadR0(int ea, bool isIncDec, int op) = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700294
Jack Palevich1cdef202009-05-22 12:06:27 -0700295 /* Emit code to adjust the stack for a function call. Return the
296 * label for the address of the instruction that adjusts the
297 * stack size. This will be passed as argument "a" to
298 * endFunctionCallArguments.
299 */
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700300 virtual int beginFunctionCallArguments() = 0;
301
Jack Palevich1cdef202009-05-22 12:06:27 -0700302 /* Emit code to store R0 to the stack at byte offset l.
303 */
304 virtual void storeR0ToArg(int l) = 0;
Jack Palevich7810bc92009-05-15 14:31:47 -0700305
Jack Palevich1cdef202009-05-22 12:06:27 -0700306 /* Patch the function call preamble.
307 * a is the address returned from beginFunctionCallArguments
308 * l is the number of bytes the arguments took on the stack.
309 * Typically you would also emit code to convert the argument
310 * list into whatever the native function calling convention is.
311 * On ARM for example you would pop the first 5 arguments into
312 * R0..R4
313 */
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700314 virtual void endFunctionCallArguments(int a, int l) = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700315
Jack Palevich1cdef202009-05-22 12:06:27 -0700316 /* Emit a call to an unknown function. The argument "symbol" needs to
317 * be stored in the location where the address should go. It forms
318 * a chain. The address will be patched later.
319 * Return the address of the word that has to be patched.
320 */
Jack Palevich22305132009-05-13 10:58:45 -0700321 virtual int callForward(int symbol) = 0;
322
Jack Palevich1cdef202009-05-22 12:06:27 -0700323 /* Call a function using PC-relative addressing. t is the PC-relative
324 * address of the function. It has already been adjusted for the
325 * architectural jump offset, so just store it as-is.
326 */
Jack Palevich22305132009-05-13 10:58:45 -0700327 virtual void callRelative(int t) = 0;
328
Jack Palevich1cdef202009-05-22 12:06:27 -0700329 /* Call a function pointer. L is the number of bytes the arguments
330 * take on the stack. The address of the function is stored at
331 * location SP + l.
332 */
Jack Palevich22305132009-05-13 10:58:45 -0700333 virtual void callIndirect(int l) = 0;
334
Jack Palevich1cdef202009-05-22 12:06:27 -0700335 /* Adjust SP after returning from a function call. l is the
336 * number of bytes of arguments stored on the stack. isIndirect
337 * is true if this was an indirect call. (In which case the
338 * address of the function is stored at location SP + l.)
339 */
Jack Palevich7810bc92009-05-15 14:31:47 -0700340 virtual void adjustStackAfterCall(int l, bool isIndirect) = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700341
Jack Palevich1cdef202009-05-22 12:06:27 -0700342 /* Print a disassembly of the assembled code to out. Return
343 * non-zero if there is an error.
344 */
Jack Palevicha6535612009-05-13 16:24:17 -0700345 virtual int disassemble(FILE* out) = 0;
346
Jack Palevich1cdef202009-05-22 12:06:27 -0700347 /* Generate a symbol at the current PC. t is the head of a
348 * linked list of addresses to patch.
349 */
Jack Paleviche7b59062009-05-19 17:12:17 -0700350 virtual void gsym(int t) = 0;
Jack Palevich21a15a22009-05-11 14:49:29 -0700351
Jack Palevich1cdef202009-05-22 12:06:27 -0700352 /*
353 * Do any cleanup work required at the end of a compile.
354 * For example, an instruction cache might need to be
355 * invalidated.
356 * Return non-zero if there is an error.
357 */
358 virtual int finishCompile() = 0;
Jack Palevich546b2242009-05-13 15:10:04 -0700359
Jack Palevicha6535612009-05-13 16:24:17 -0700360 /**
361 * Adjust relative branches by this amount.
362 */
363 virtual int jumpOffset() = 0;
364
Jack Palevich21a15a22009-05-11 14:49:29 -0700365 protected:
Jack Palevich21a15a22009-05-11 14:49:29 -0700366 /*
367 * Output a byte. Handles all values, 0..ff.
368 */
369 void ob(int n) {
370 pCodeBuf->ob(n);
371 }
372
Jack Palevich8b0624c2009-05-20 12:12:06 -0700373 intptr_t o4(int data) {
Jack Paleviche7b59062009-05-19 17:12:17 -0700374 return pCodeBuf->o4(data);
Jack Palevich21a15a22009-05-11 14:49:29 -0700375 }
376
Jack Palevich8b0624c2009-05-20 12:12:06 -0700377 intptr_t getBase() {
378 return (intptr_t) pCodeBuf->getBase();
Jack Palevicha6535612009-05-13 16:24:17 -0700379 }
380
Jack Palevich8b0624c2009-05-20 12:12:06 -0700381 intptr_t getPC() {
Jack Palevich21a15a22009-05-11 14:49:29 -0700382 return pCodeBuf->getPC();
383 }
Jack Palevich1cdef202009-05-22 12:06:27 -0700384
385 intptr_t getSize() {
386 return pCodeBuf->getSize();
387 }
Jack Palevichac0e95e2009-05-29 13:53:44 -0700388
389 void error(const char* fmt,...) {
390 va_list ap;
391 va_start(ap, fmt);
392 mErrorSink->verror(fmt, ap);
393 va_end(ap);
394 }
Jack Palevich21a15a22009-05-11 14:49:29 -0700395 private:
396 CodeBuf* pCodeBuf;
Jack Palevichac0e95e2009-05-29 13:53:44 -0700397 ErrorSink* mErrorSink;
Jack Palevich21a15a22009-05-11 14:49:29 -0700398 };
399
Jack Paleviche7b59062009-05-19 17:12:17 -0700400#ifdef PROVIDE_ARM_CODEGEN
401
Jack Palevich22305132009-05-13 10:58:45 -0700402 class ARMCodeGenerator : public CodeGenerator {
403 public:
404 ARMCodeGenerator() {}
-b master422972c2009-06-17 19:13:52 -0700405
Jack Palevich22305132009-05-13 10:58:45 -0700406 virtual ~ARMCodeGenerator() {}
407
408 /* returns address to patch with local variable size
409 */
Jack Palevich546b2242009-05-13 15:10:04 -0700410 virtual int functionEntry(int argCount) {
Jack Palevichb7c81e92009-06-04 19:56:13 -0700411 LOG_API("functionEntry(%d);\n", argCount);
-b master422972c2009-06-17 19:13:52 -0700412 mStackUse = 0;
Jack Palevich69796b62009-05-14 15:42:26 -0700413 // sp -> arg4 arg5 ...
414 // Push our register-based arguments back on the stack
415 if (argCount > 0) {
416 int regArgCount = argCount <= 4 ? argCount : 4;
417 o4(0xE92D0000 | ((1 << argCount) - 1)); // stmfd sp!, {}
-b master422972c2009-06-17 19:13:52 -0700418 mStackUse += regArgCount * 4;
Jack Palevich69796b62009-05-14 15:42:26 -0700419 }
420 // sp -> arg0 arg1 ...
421 o4(0xE92D4800); // stmfd sp!, {fp, lr}
-b master422972c2009-06-17 19:13:52 -0700422 mStackUse += 2 * 4;
Jack Palevich69796b62009-05-14 15:42:26 -0700423 // sp, fp -> oldfp, retadr, arg0 arg1 ....
424 o4(0xE1A0B00D); // mov fp, sp
-b master422972c2009-06-17 19:13:52 -0700425 LOG_STACK("functionEntry: %d\n", mStackUse);
Jack Palevich69796b62009-05-14 15:42:26 -0700426 return o4(0xE24DD000); // sub sp, sp, # <local variables>
-b master422972c2009-06-17 19:13:52 -0700427 // We don't know how many local variables we are going to use,
428 // but we will round the allocation up to a multiple of
429 // STACK_ALIGNMENT, so it won't affect the stack alignment.
Jack Palevich22305132009-05-13 10:58:45 -0700430 }
431
Jack Palevich546b2242009-05-13 15:10:04 -0700432 virtual void functionExit(int argCount, int localVariableAddress, int localVariableSize) {
Jack Palevich09555c72009-05-27 12:25:55 -0700433 LOG_API("functionExit(%d, %d, %d);\n", argCount, localVariableAddress, localVariableSize);
-b master422972c2009-06-17 19:13:52 -0700434 // Round local variable size up to a multiple of stack alignment
435 localVariableSize = ((localVariableSize + STACK_ALIGNMENT - 1) /
436 STACK_ALIGNMENT) * STACK_ALIGNMENT;
Jack Palevich69796b62009-05-14 15:42:26 -0700437 // Patch local variable allocation code:
438 if (localVariableSize < 0 || localVariableSize > 255) {
Jack Palevich8de461d2009-05-14 17:21:45 -0700439 error("localVariables out of range: %d", localVariableSize);
Jack Palevich546b2242009-05-13 15:10:04 -0700440 }
Jack Palevich69796b62009-05-14 15:42:26 -0700441 *(char*) (localVariableAddress) = localVariableSize;
442
443 // sp -> locals .... fp -> oldfp, retadr, arg0, arg1, ...
444 o4(0xE1A0E00B); // mov lr, fp
445 o4(0xE59BB000); // ldr fp, [fp]
446 o4(0xE28ED004); // add sp, lr, #4
447 // sp -> retadr, arg0, ...
448 o4(0xE8BD4000); // ldmfd sp!, {lr}
449 // sp -> arg0 ....
450 if (argCount > 0) {
451 // We store the PC into the lr so we can adjust the sp before
Jack Palevich8de461d2009-05-14 17:21:45 -0700452 // returning. We need to pull off the registers we pushed
Jack Palevich69796b62009-05-14 15:42:26 -0700453 // earlier. We don't need to actually store them anywhere,
454 // just adjust the stack.
455 int regArgCount = argCount <= 4 ? argCount : 4;
456 o4(0xE28DD000 | (regArgCount << 2)); // add sp, sp, #argCount << 2
457 }
458 o4(0xE12FFF1E); // bx lr
Jack Palevich22305132009-05-13 10:58:45 -0700459 }
460
461 /* load immediate value */
Jack Palevich546b2242009-05-13 15:10:04 -0700462 virtual void li(int t) {
Jack Palevich09555c72009-05-27 12:25:55 -0700463 LOG_API("li(%d);\n", t);
Jack Palevicha6535612009-05-13 16:24:17 -0700464 if (t >= 0 && t < 255) {
Jack Palevich69796b62009-05-14 15:42:26 -0700465 o4(0xE3A00000 + t); // mov r0, #0
Jack Palevicha6535612009-05-13 16:24:17 -0700466 } else if (t >= -256 && t < 0) {
467 // mvn means move constant ^ ~0
Jack Palevich69796b62009-05-14 15:42:26 -0700468 o4(0xE3E00001 - t); // mvn r0, #0
Jack Palevicha6535612009-05-13 16:24:17 -0700469 } else {
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700470 o4(0xE51F0000); // ldr r0, .L3
471 o4(0xEA000000); // b .L99
472 o4(t); // .L3: .word 0
473 // .L99:
Jack Palevicha6535612009-05-13 16:24:17 -0700474 }
Jack Palevich22305132009-05-13 10:58:45 -0700475 }
476
477 virtual int gjmp(int t) {
Jack Palevich09555c72009-05-27 12:25:55 -0700478 LOG_API("gjmp(%d);\n", t);
Jack Palevich8de461d2009-05-14 17:21:45 -0700479 return o4(0xEA000000 | encodeAddress(t)); // b .L33
Jack Palevich22305132009-05-13 10:58:45 -0700480 }
481
482 /* l = 0: je, l == 1: jne */
483 virtual int gtst(bool l, int t) {
Jack Palevich09555c72009-05-27 12:25:55 -0700484 LOG_API("gtst(%d, %d);\n", l, t);
Jack Palevich8de461d2009-05-14 17:21:45 -0700485 o4(0xE3500000); // cmp r0,#0
486 int branch = l ? 0x1A000000 : 0x0A000000; // bne : beq
487 return o4(branch | encodeAddress(t));
Jack Palevich22305132009-05-13 10:58:45 -0700488 }
489
490 virtual void gcmp(int op) {
Jack Palevich09555c72009-05-27 12:25:55 -0700491 LOG_API("gcmp(%d);\n", op);
Jack Palevich8de461d2009-05-14 17:21:45 -0700492 o4(0xE1510000); // cmp r1, r1
493 switch(op) {
494 case OP_EQUALS:
495 o4(0x03A00001); // moveq r0,#1
496 o4(0x13A00000); // movne r0,#0
497 break;
498 case OP_NOT_EQUALS:
499 o4(0x03A00000); // moveq r0,#0
500 o4(0x13A00001); // movne r0,#1
501 break;
502 case OP_LESS_EQUAL:
503 o4(0xD3A00001); // movle r0,#1
504 o4(0xC3A00000); // movgt r0,#0
505 break;
506 case OP_GREATER:
507 o4(0xD3A00000); // movle r0,#0
508 o4(0xC3A00001); // movgt r0,#1
509 break;
510 case OP_GREATER_EQUAL:
511 o4(0xA3A00001); // movge r0,#1
512 o4(0xB3A00000); // movlt r0,#0
513 break;
514 case OP_LESS:
515 o4(0xA3A00000); // movge r0,#0
516 o4(0xB3A00001); // movlt r0,#1
517 break;
518 default:
519 error("Unknown comparison op %d", op);
520 break;
521 }
Jack Palevich22305132009-05-13 10:58:45 -0700522 }
523
Jack Palevich546b2242009-05-13 15:10:04 -0700524 virtual void genOp(int op) {
Jack Palevich09555c72009-05-27 12:25:55 -0700525 LOG_API("genOp(%d);\n", op);
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700526 switch(op) {
527 case OP_MUL:
528 o4(0x0E0000091); // mul r0,r1,r0
529 break;
Jack Palevich3d474a72009-05-15 15:12:38 -0700530 case OP_DIV:
531 callRuntime(runtime_DIV);
532 break;
533 case OP_MOD:
534 callRuntime(runtime_MOD);
535 break;
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700536 case OP_PLUS:
537 o4(0xE0810000); // add r0,r1,r0
538 break;
539 case OP_MINUS:
540 o4(0xE0410000); // sub r0,r1,r0
541 break;
542 case OP_SHIFT_LEFT:
543 o4(0xE1A00011); // lsl r0,r1,r0
544 break;
545 case OP_SHIFT_RIGHT:
546 o4(0xE1A00051); // asr r0,r1,r0
547 break;
548 case OP_BIT_AND:
549 o4(0xE0010000); // and r0,r1,r0
550 break;
551 case OP_BIT_XOR:
552 o4(0xE0210000); // eor r0,r1,r0
553 break;
554 case OP_BIT_OR:
555 o4(0xE1810000); // orr r0,r1,r0
556 break;
557 case OP_BIT_NOT:
558 o4(0xE1E00000); // mvn r0, r0
559 break;
560 default:
Jack Palevich69796b62009-05-14 15:42:26 -0700561 error("Unimplemented op %d\n", op);
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700562 break;
563 }
Jack Palevich22305132009-05-13 10:58:45 -0700564 }
565
Jack Palevich1cdef202009-05-22 12:06:27 -0700566 virtual void clearR1() {
Jack Palevich09555c72009-05-27 12:25:55 -0700567 LOG_API("clearR1();\n");
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700568 o4(0xE3A01000); // mov r1, #0
Jack Palevich22305132009-05-13 10:58:45 -0700569 }
570
Jack Palevich1cdef202009-05-22 12:06:27 -0700571 virtual void pushR0() {
Jack Palevich09555c72009-05-27 12:25:55 -0700572 LOG_API("pushR0();\n");
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700573 o4(0xE92D0001); // stmfd sp!,{r0}
-b master422972c2009-06-17 19:13:52 -0700574 mStackUse += 4;
575 LOG_STACK("pushR0: %d\n", mStackUse);
Jack Palevich22305132009-05-13 10:58:45 -0700576 }
577
Jack Palevich1cdef202009-05-22 12:06:27 -0700578 virtual void popR1() {
Jack Palevich09555c72009-05-27 12:25:55 -0700579 LOG_API("popR1();\n");
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700580 o4(0xE8BD0002); // ldmfd sp!,{r1}
-b master422972c2009-06-17 19:13:52 -0700581 mStackUse -= 4;
582 LOG_STACK("popR1: %d\n", mStackUse);
Jack Palevich22305132009-05-13 10:58:45 -0700583 }
584
Jack Palevich1cdef202009-05-22 12:06:27 -0700585 virtual void storeR0ToR1(bool isInt) {
Jack Palevich09555c72009-05-27 12:25:55 -0700586 LOG_API("storeR0ToR1(%d);\n", isInt);
Jack Palevichbd894902009-05-14 19:35:31 -0700587 if (isInt) {
588 o4(0xE5810000); // str r0, [r1]
589 } else {
590 o4(0xE5C10000); // strb r0, [r1]
591 }
Jack Palevich22305132009-05-13 10:58:45 -0700592 }
593
Jack Palevich1cdef202009-05-22 12:06:27 -0700594 virtual void loadR0FromR0(bool isInt) {
Jack Palevich09555c72009-05-27 12:25:55 -0700595 LOG_API("loadR0FromR0(%d);\n", isInt);
Jack Palevich22305132009-05-13 10:58:45 -0700596 if (isInt)
Jack Palevich69796b62009-05-14 15:42:26 -0700597 o4(0xE5900000); // ldr r0, [r0]
Jack Palevich22305132009-05-13 10:58:45 -0700598 else
Jack Palevich69796b62009-05-14 15:42:26 -0700599 o4(0xE5D00000); // ldrb r0, [r0]
Jack Palevich22305132009-05-13 10:58:45 -0700600 }
601
Jack Palevich1cdef202009-05-22 12:06:27 -0700602 virtual void leaR0(int ea) {
Jack Palevich09555c72009-05-27 12:25:55 -0700603 LOG_API("leaR0(%d);\n", ea);
Jack Palevich4d93f302009-05-15 13:30:00 -0700604 if (ea < LOCAL) {
605 // Local, fp relative
606 if (ea < -1023 || ea > 1023 || ((ea & 3) != 0)) {
607 error("Offset out of range: %08x", ea);
608 }
609 if (ea < 0) {
610 o4(0xE24B0F00 | (0xff & ((-ea) >> 2))); // sub r0, fp, #ea
611 } else {
612 o4(0xE28B0F00 | (0xff & (ea >> 2))); // add r0, fp, #ea
613 }
Jack Palevichbd894902009-05-14 19:35:31 -0700614 } else {
Jack Palevich4d93f302009-05-15 13:30:00 -0700615 // Global, absolute.
616 o4(0xE59F0000); // ldr r0, .L1
617 o4(0xEA000000); // b .L99
618 o4(ea); // .L1: .word 0
619 // .L99:
Jack Palevichbd894902009-05-14 19:35:31 -0700620 }
Jack Palevich22305132009-05-13 10:58:45 -0700621 }
622
Jack Palevich1cdef202009-05-22 12:06:27 -0700623 virtual void storeR0(int ea) {
Jack Palevich09555c72009-05-27 12:25:55 -0700624 LOG_API("storeR0(%d);\n", ea);
Jack Palevich4d93f302009-05-15 13:30:00 -0700625 if (ea < LOCAL) {
626 // Local, fp relative
627 if (ea < -4095 || ea > 4095) {
628 error("Offset out of range: %08x", ea);
629 }
630 if (ea < 0) {
631 o4(0xE50B0000 | (0xfff & (-ea))); // str r0, [fp,#-ea]
632 } else {
633 o4(0xE58B0000 | (0xfff & ea)); // str r0, [fp,#ea]
634 }
635 } else{
636 // Global, absolute
637 o4(0xE59F1000); // ldr r1, .L1
638 o4(0xEA000000); // b .L99
639 o4(ea); // .L1: .word 0
640 o4(0xE5810000); // .L99: str r0, [r1]
Jack Palevich69796b62009-05-14 15:42:26 -0700641 }
Jack Palevich22305132009-05-13 10:58:45 -0700642 }
643
Jack Palevich1cdef202009-05-22 12:06:27 -0700644 virtual void loadR0(int ea, bool isIncDec, int op) {
Jack Palevich09555c72009-05-27 12:25:55 -0700645 LOG_API("loadR0(%d, %d, %d);\n", ea, isIncDec, op);
Jack Palevich4d93f302009-05-15 13:30:00 -0700646 if (ea < LOCAL) {
647 // Local, fp relative
648 if (ea < -4095 || ea > 4095) {
649 error("Offset out of range: %08x", ea);
650 }
651 if (ea < 0) {
652 o4(0xE51B0000 | (0xfff & (-ea))); // ldr r0, [fp,#-ea]
653 } else {
654 o4(0xE59B0000 | (0xfff & ea)); // ldr r0, [fp,#ea]
655 }
Jack Palevich69796b62009-05-14 15:42:26 -0700656 } else {
Jack Palevich4d93f302009-05-15 13:30:00 -0700657 // Global, absolute
658 o4(0xE59F2000); // ldr r2, .L1
659 o4(0xEA000000); // b .L99
660 o4(ea); // .L1: .word ea
661 o4(0xE5920000); // .L99: ldr r0, [r2]
Jack Palevich69796b62009-05-14 15:42:26 -0700662 }
Jack Palevich22305132009-05-13 10:58:45 -0700663
Jack Palevich4d93f302009-05-15 13:30:00 -0700664 if (isIncDec) {
665 switch (op) {
666 case OP_INCREMENT:
667 o4(0xE2801001); // add r1, r0, #1
668 break;
669 case OP_DECREMENT:
670 o4(0xE2401001); // sub r1, r0, #1
671 break;
672 default:
673 error("unknown opcode: %d", op);
674 }
675 if (ea < LOCAL) {
676 // Local, fp relative
677 // Don't need range check, was already checked above
678 if (ea < 0) {
679 o4(0xE50B1000 | (0xfff & (-ea))); // str r1, [fp,#-ea]
680 } else {
681 o4(0xE58B1000 | (0xfff & ea)); // str r1, [fp,#ea]
682 }
683 } else{
684 // Global, absolute
685 // r2 is already set up from before.
686 o4(0xE5821000); // str r1, [r2]
687 }
Jack Palevichbd894902009-05-14 19:35:31 -0700688 }
Jack Palevich22305132009-05-13 10:58:45 -0700689 }
690
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700691 virtual int beginFunctionCallArguments() {
Jack Palevich09555c72009-05-27 12:25:55 -0700692 LOG_API("beginFunctionCallArguments();\n");
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700693 return o4(0xE24DDF00); // Placeholder
694 }
695
Jack Palevich1cdef202009-05-22 12:06:27 -0700696 virtual void storeR0ToArg(int l) {
Jack Palevich09555c72009-05-27 12:25:55 -0700697 LOG_API("storeR0ToArg(%d);\n", l);
Jack Palevich7810bc92009-05-15 14:31:47 -0700698 if (l < 0 || l > 4096-4) {
699 error("l out of range for stack offset: 0x%08x", l);
700 }
701 o4(0xE58D0000 + l); // str r0, [sp, #4]
702 }
703
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700704 virtual void endFunctionCallArguments(int a, int l) {
Jack Palevich09555c72009-05-27 12:25:55 -0700705 LOG_API("endFunctionCallArguments(0x%08x, %d);\n", a, l);
-b master422972c2009-06-17 19:13:52 -0700706 int argCount = l >> 2;
707 int argumentStackUse = l;
708 if (argCount > 0) {
709 int regArgCount = argCount > 4 ? 4 : argCount;
710 argumentStackUse -= regArgCount * 4;
711 o4(0xE8BD0000 | ((1 << regArgCount) - 1)); // ldmfd sp!,{}
712 }
713 mStackUse += argumentStackUse;
714
715 // Align stack.
716 int missalignment = mStackUse - ((mStackUse / STACK_ALIGNMENT)
717 * STACK_ALIGNMENT);
718 mStackAlignmentAdjustment = 0;
719 if (missalignment > 0) {
720 mStackAlignmentAdjustment = STACK_ALIGNMENT - missalignment;
721 }
722 l += mStackAlignmentAdjustment;
723
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700724 if (l < 0 || l > 0x3FC) {
725 error("L out of range for stack adjustment: 0x%08x", l);
726 }
727 * (int*) a = 0xE24DDF00 | (l >> 2); // sub sp, sp, #0 << 2
-b master422972c2009-06-17 19:13:52 -0700728 mStackUse += mStackAlignmentAdjustment;
729 LOG_STACK("endFunctionCallArguments mStackUse: %d, mStackAlignmentAdjustment %d\n",
730 mStackUse, mStackAlignmentAdjustment);
Jack Palevich22305132009-05-13 10:58:45 -0700731 }
732
Jack Palevich22305132009-05-13 10:58:45 -0700733 virtual int callForward(int symbol) {
Jack Palevich09555c72009-05-27 12:25:55 -0700734 LOG_API("callForward(%d);\n", symbol);
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700735 // Forward calls are always short (local)
736 return o4(0xEB000000 | encodeAddress(symbol));
Jack Palevich22305132009-05-13 10:58:45 -0700737 }
738
739 virtual void callRelative(int t) {
Jack Palevich09555c72009-05-27 12:25:55 -0700740 LOG_API("callRelative(%d);\n", t);
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700741 int abs = t + getPC() + jumpOffset();
Jack Palevichac0e95e2009-05-29 13:53:44 -0700742 LOG_API("abs=%d (0x%08x)\n", abs, abs);
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700743 if (t >= - (1 << 25) && t < (1 << 25)) {
744 o4(0xEB000000 | encodeAddress(t));
745 } else {
746 // Long call.
747 o4(0xE59FC000); // ldr r12, .L1
748 o4(0xEA000000); // b .L99
Jack Palevichbd894902009-05-14 19:35:31 -0700749 o4(t - 12); // .L1: .word 0
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700750 o4(0xE08CC00F); // .L99: add r12,pc
751 o4(0xE12FFF3C); // blx r12
752 }
Jack Palevich22305132009-05-13 10:58:45 -0700753 }
754
755 virtual void callIndirect(int l) {
Jack Palevich09555c72009-05-27 12:25:55 -0700756 LOG_API("callIndirect(%d);\n", l);
Jack Palevich7810bc92009-05-15 14:31:47 -0700757 int argCount = l >> 2;
758 int poppedArgs = argCount > 4 ? 4 : argCount;
-b master422972c2009-06-17 19:13:52 -0700759 int adjustedL = l - (poppedArgs << 2) + mStackAlignmentAdjustment;
Jack Palevich7810bc92009-05-15 14:31:47 -0700760 if (adjustedL < 0 || adjustedL > 4096-4) {
761 error("l out of range for stack offset: 0x%08x", l);
762 }
763 o4(0xE59DC000 | (0xfff & adjustedL)); // ldr r12, [sp,#adjustedL]
764 o4(0xE12FFF3C); // blx r12
Jack Palevich22305132009-05-13 10:58:45 -0700765 }
766
Jack Palevich7810bc92009-05-15 14:31:47 -0700767 virtual void adjustStackAfterCall(int l, bool isIndirect) {
Jack Palevich09555c72009-05-27 12:25:55 -0700768 LOG_API("adjustStackAfterCall(%d, %d);\n", l, isIndirect);
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700769 int argCount = l >> 2;
Jack Palevich7810bc92009-05-15 14:31:47 -0700770 int stackArgs = argCount > 4 ? argCount - 4 : 0;
-b master422972c2009-06-17 19:13:52 -0700771 int stackUse = stackArgs + (isIndirect ? 1 : 0)
772 + (mStackAlignmentAdjustment >> 2);
Jack Palevich7810bc92009-05-15 14:31:47 -0700773 if (stackUse) {
774 if (stackUse < 0 || stackUse > 255) {
775 error("L out of range for stack adjustment: 0x%08x", l);
776 }
777 o4(0xE28DDF00 | stackUse); // add sp, sp, #stackUse << 2
-b master422972c2009-06-17 19:13:52 -0700778 mStackUse -= stackUse * 4;
779 LOG_STACK("adjustStackAfterCall: %d\n", mStackUse);
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700780 }
Jack Palevich22305132009-05-13 10:58:45 -0700781 }
782
Jack Palevicha6535612009-05-13 16:24:17 -0700783 virtual int jumpOffset() {
Jack Palevichbd894902009-05-14 19:35:31 -0700784 return 8;
Jack Palevicha6535612009-05-13 16:24:17 -0700785 }
786
787 /* output a symbol and patch all calls to it */
788 virtual void gsym(int t) {
Jack Palevich09555c72009-05-27 12:25:55 -0700789 LOG_API("gsym(0x%x)\n", t);
Jack Palevicha6535612009-05-13 16:24:17 -0700790 int n;
791 int base = getBase();
792 int pc = getPC();
Jack Palevich09555c72009-05-27 12:25:55 -0700793 LOG_API("pc = 0x%x\n", pc);
Jack Palevicha6535612009-05-13 16:24:17 -0700794 while (t) {
795 int data = * (int*) t;
796 int decodedOffset = ((BRANCH_REL_ADDRESS_MASK & data) << 2);
797 if (decodedOffset == 0) {
798 n = 0;
799 } else {
800 n = base + decodedOffset; /* next value */
801 }
802 *(int *) t = (data & ~BRANCH_REL_ADDRESS_MASK)
803 | encodeRelAddress(pc - t - 8);
804 t = n;
805 }
806 }
807
Jack Palevich1cdef202009-05-22 12:06:27 -0700808 virtual int finishCompile() {
809#if defined(__arm__)
810 const long base = long(getBase());
811 const long curr = long(getPC());
812 int err = cacheflush(base, curr, 0);
813 return err;
814#else
815 return 0;
816#endif
817 }
818
Jack Palevicha6535612009-05-13 16:24:17 -0700819 virtual int disassemble(FILE* out) {
Jack Palevich09555c72009-05-27 12:25:55 -0700820#ifdef ENABLE_ARM_DISASSEMBLY
821 disasmOut = out;
Jack Palevicha6535612009-05-13 16:24:17 -0700822 disasm_interface_t di;
823 di.di_readword = disassemble_readword;
824 di.di_printaddr = disassemble_printaddr;
825 di.di_printf = disassemble_printf;
826
827 int base = getBase();
828 int pc = getPC();
829 for(int i = base; i < pc; i += 4) {
830 fprintf(out, "%08x: %08x ", i, *(int*) i);
831 ::disasm(&di, i, 0);
832 }
Jack Palevich09555c72009-05-27 12:25:55 -0700833#endif
Jack Palevicha6535612009-05-13 16:24:17 -0700834 return 0;
835 }
Jack Palevich7810bc92009-05-15 14:31:47 -0700836
Jack Palevich22305132009-05-13 10:58:45 -0700837 private:
Jack Palevicha6535612009-05-13 16:24:17 -0700838 static FILE* disasmOut;
839
840 static u_int
841 disassemble_readword(u_int address)
842 {
843 return(*((u_int *)address));
844 }
845
846 static void
847 disassemble_printaddr(u_int address)
848 {
849 fprintf(disasmOut, "0x%08x", address);
850 }
851
852 static void
853 disassemble_printf(const char *fmt, ...) {
854 va_list ap;
855 va_start(ap, fmt);
856 vfprintf(disasmOut, fmt, ap);
857 va_end(ap);
858 }
859
860 static const int BRANCH_REL_ADDRESS_MASK = 0x00ffffff;
861
862 /** Encode a relative address that might also be
863 * a label.
864 */
865 int encodeAddress(int value) {
866 int base = getBase();
867 if (value >= base && value <= getPC() ) {
868 // This is a label, encode it relative to the base.
869 value = value - base;
870 }
871 return encodeRelAddress(value);
872 }
873
874 int encodeRelAddress(int value) {
875 return BRANCH_REL_ADDRESS_MASK & (value >> 2);
876 }
Jack Palevich22305132009-05-13 10:58:45 -0700877
Jack Palevich3d474a72009-05-15 15:12:38 -0700878 typedef int (*int2FnPtr)(int a, int b);
879 void callRuntime(int2FnPtr fn) {
880 o4(0xE59F2000); // ldr r2, .L1
881 o4(0xEA000000); // b .L99
882 o4((int) fn); //.L1: .word fn
883 o4(0xE12FFF32); //.L99: blx r2
884 }
885
886 static int runtime_DIV(int a, int b) {
887 return b / a;
888 }
889
890 static int runtime_MOD(int a, int b) {
891 return b % a;
892 }
-b master422972c2009-06-17 19:13:52 -0700893
894 static const int STACK_ALIGNMENT = 8;
895 int mStackUse;
896 // This variable holds the amount we adjusted the stack in the most
897 // recent endFunctionCallArguments call. It's examined by the
898 // following adjustStackAfterCall call.
899 int mStackAlignmentAdjustment;
Jack Palevich22305132009-05-13 10:58:45 -0700900 };
901
Jack Palevich09555c72009-05-27 12:25:55 -0700902#endif // PROVIDE_ARM_CODEGEN
Jack Paleviche7b59062009-05-19 17:12:17 -0700903
904#ifdef PROVIDE_X86_CODEGEN
905
Jack Palevich21a15a22009-05-11 14:49:29 -0700906 class X86CodeGenerator : public CodeGenerator {
907 public:
908 X86CodeGenerator() {}
909 virtual ~X86CodeGenerator() {}
910
Jack Palevichbf42c9c2009-05-12 12:48:35 -0700911 /* returns address to patch with local variable size
912 */
Jack Palevich546b2242009-05-13 15:10:04 -0700913 virtual int functionEntry(int argCount) {
Jack Palevichbf42c9c2009-05-12 12:48:35 -0700914 o(0xe58955); /* push %ebp, mov %esp, %ebp */
915 return oad(0xec81, 0); /* sub $xxx, %esp */
916 }
917
Jack Palevich546b2242009-05-13 15:10:04 -0700918 virtual void functionExit(int argCount, int localVariableAddress, int localVariableSize) {
Jack Palevichbf42c9c2009-05-12 12:48:35 -0700919 o(0xc3c9); /* leave, ret */
Jack Palevich546b2242009-05-13 15:10:04 -0700920 *(int *) localVariableAddress = localVariableSize; /* save local variables */
Jack Palevichbf42c9c2009-05-12 12:48:35 -0700921 }
922
Jack Palevich21a15a22009-05-11 14:49:29 -0700923 /* load immediate value */
Jack Palevich546b2242009-05-13 15:10:04 -0700924 virtual void li(int t) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700925 oad(0xb8, t); /* mov $xx, %eax */
926 }
927
Jack Palevich22305132009-05-13 10:58:45 -0700928 virtual int gjmp(int t) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700929 return psym(0xe9, t);
930 }
931
932 /* l = 0: je, l == 1: jne */
Jack Palevich22305132009-05-13 10:58:45 -0700933 virtual int gtst(bool l, int t) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700934 o(0x0fc085); /* test %eax, %eax, je/jne xxx */
935 return psym(0x84 + l, t);
936 }
937
Jack Palevich22305132009-05-13 10:58:45 -0700938 virtual void gcmp(int op) {
Jack Palevichbf42c9c2009-05-12 12:48:35 -0700939 int t = decodeOp(op);
Jack Palevich21a15a22009-05-11 14:49:29 -0700940 o(0xc139); /* cmp %eax,%ecx */
941 li(0);
942 o(0x0f); /* setxx %al */
943 o(t + 0x90);
944 o(0xc0);
945 }
946
Jack Palevich546b2242009-05-13 15:10:04 -0700947 virtual void genOp(int op) {
Jack Palevichbf42c9c2009-05-12 12:48:35 -0700948 o(decodeOp(op));
949 if (op == OP_MOD)
950 o(0x92); /* xchg %edx, %eax */
951 }
952
Jack Palevich1cdef202009-05-22 12:06:27 -0700953 virtual void clearR1() {
Jack Palevich21a15a22009-05-11 14:49:29 -0700954 oad(0xb9, 0); /* movl $0, %ecx */
955 }
956
Jack Palevich1cdef202009-05-22 12:06:27 -0700957 virtual void pushR0() {
Jack Palevich21a15a22009-05-11 14:49:29 -0700958 o(0x50); /* push %eax */
959 }
960
Jack Palevich1cdef202009-05-22 12:06:27 -0700961 virtual void popR1() {
Jack Palevich21a15a22009-05-11 14:49:29 -0700962 o(0x59); /* pop %ecx */
Jack Palevichbf42c9c2009-05-12 12:48:35 -0700963 }
964
Jack Palevich1cdef202009-05-22 12:06:27 -0700965 virtual void storeR0ToR1(bool isInt) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700966 o(0x0188 + isInt); /* movl %eax/%al, (%ecx) */
967 }
968
Jack Palevich1cdef202009-05-22 12:06:27 -0700969 virtual void loadR0FromR0(bool isInt) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700970 if (isInt)
971 o(0x8b); /* mov (%eax), %eax */
972 else
973 o(0xbe0f); /* movsbl (%eax), %eax */
974 ob(0); /* add zero in code */
975 }
976
Jack Palevich1cdef202009-05-22 12:06:27 -0700977 virtual void leaR0(int ea) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700978 gmov(10, ea); /* leal EA, %eax */
979 }
980
Jack Palevich1cdef202009-05-22 12:06:27 -0700981 virtual void storeR0(int ea) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700982 gmov(6, ea); /* mov %eax, EA */
983 }
984
Jack Palevich1cdef202009-05-22 12:06:27 -0700985 virtual void loadR0(int ea, bool isIncDec, int op) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700986 gmov(8, ea); /* mov EA, %eax */
Jack Palevich4d93f302009-05-15 13:30:00 -0700987 if (isIncDec) {
988 /* Implement post-increment or post decrement.
989 */
990 gmov(0, ea); /* 83 ADD */
991 o(decodeOp(op));
992 }
Jack Palevich21a15a22009-05-11 14:49:29 -0700993 }
994
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700995 virtual int beginFunctionCallArguments() {
Jack Palevich21a15a22009-05-11 14:49:29 -0700996 return oad(0xec81, 0); /* sub $xxx, %esp */
997 }
998
Jack Palevich1cdef202009-05-22 12:06:27 -0700999 virtual void storeR0ToArg(int l) {
Jack Palevich21a15a22009-05-11 14:49:29 -07001000 oad(0x248489, l); /* movl %eax, xxx(%esp) */
1001 }
1002
Jack Palevich7810bc92009-05-15 14:31:47 -07001003 virtual void endFunctionCallArguments(int a, int l) {
1004 * (int*) a = l;
1005 }
1006
Jack Palevich22305132009-05-13 10:58:45 -07001007 virtual int callForward(int symbol) {
Jack Palevich21a15a22009-05-11 14:49:29 -07001008 return psym(0xe8, symbol); /* call xxx */
1009 }
1010
Jack Palevich22305132009-05-13 10:58:45 -07001011 virtual void callRelative(int t) {
Jack Palevich21a15a22009-05-11 14:49:29 -07001012 psym(0xe8, t); /* call xxx */
1013 }
1014
Jack Palevich22305132009-05-13 10:58:45 -07001015 virtual void callIndirect(int l) {
Jack Palevich21a15a22009-05-11 14:49:29 -07001016 oad(0x2494ff, l); /* call *xxx(%esp) */
1017 }
1018
Jack Palevich7810bc92009-05-15 14:31:47 -07001019 virtual void adjustStackAfterCall(int l, bool isIndirect) {
1020 if (isIndirect) {
1021 l += 4;
1022 }
-b master422972c2009-06-17 19:13:52 -07001023 if (l > 0) {
1024 oad(0xc481, l); /* add $xxx, %esp */
1025 }
Jack Palevich21a15a22009-05-11 14:49:29 -07001026 }
1027
Jack Palevicha6535612009-05-13 16:24:17 -07001028 virtual int jumpOffset() {
1029 return 5;
1030 }
1031
1032 virtual int disassemble(FILE* out) {
Jack Palevich1cdef202009-05-22 12:06:27 -07001033 return 0;
Jack Palevicha6535612009-05-13 16:24:17 -07001034 }
1035
Jack Paleviche7b59062009-05-19 17:12:17 -07001036 /* output a symbol and patch all calls to it */
1037 virtual void gsym(int t) {
1038 int n;
1039 int pc = getPC();
1040 while (t) {
1041 n = *(int *) t; /* next value */
1042 *(int *) t = pc - t - 4;
1043 t = n;
1044 }
1045 }
1046
Jack Palevich1cdef202009-05-22 12:06:27 -07001047 virtual int finishCompile() {
Jack Palevich8dc662e2009-06-09 22:53:47 +00001048 size_t pagesize = 4096;
1049 size_t base = (size_t) getBase() & ~ (pagesize - 1);
1050 size_t top = ((size_t) getPC() + pagesize - 1) & ~ (pagesize - 1);
1051 int err = mprotect((void*) base, top - base, PROT_READ | PROT_WRITE | PROT_EXEC);
1052 if (err) {
1053 error("mprotect() failed: %d", errno);
1054 }
1055 return err;
Jack Palevich1cdef202009-05-22 12:06:27 -07001056 }
1057
Jack Palevich21a15a22009-05-11 14:49:29 -07001058 private:
Jack Paleviche7b59062009-05-19 17:12:17 -07001059
1060 /** Output 1 to 4 bytes.
1061 *
1062 */
1063 void o(int n) {
1064 /* cannot use unsigned, so we must do a hack */
1065 while (n && n != -1) {
1066 ob(n & 0xff);
1067 n = n >> 8;
1068 }
1069 }
1070
1071 /* psym is used to put an instruction with a data field which is a
1072 reference to a symbol. It is in fact the same as oad ! */
1073 int psym(int n, int t) {
1074 return oad(n, t);
1075 }
1076
1077 /* instruction + address */
1078 int oad(int n, int t) {
1079 o(n);
1080 int result = getPC();
1081 o4(t);
1082 return result;
1083 }
1084
1085
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001086 static const int operatorHelper[];
1087
1088 int decodeOp(int op) {
1089 if (op < 0 || op > OP_COUNT) {
Jack Palevichac0e95e2009-05-29 13:53:44 -07001090 error("Out-of-range operator: %d\n", op);
Jack Palevich0a280a02009-06-11 10:53:51 -07001091 op = 0;
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001092 }
1093 return operatorHelper[op];
1094 }
Jack Palevich21a15a22009-05-11 14:49:29 -07001095
Jack Palevich546b2242009-05-13 15:10:04 -07001096 void gmov(int l, int t) {
Jack Palevich21a15a22009-05-11 14:49:29 -07001097 o(l + 0x83);
Jack Palevich8dc662e2009-06-09 22:53:47 +00001098 oad((t > -LOCAL && t < LOCAL) << 7 | 5, t);
Jack Palevich21a15a22009-05-11 14:49:29 -07001099 }
1100 };
1101
Jack Paleviche7b59062009-05-19 17:12:17 -07001102#endif // PROVIDE_X86_CODEGEN
1103
Jack Palevichb67b18f2009-06-11 21:12:23 -07001104#ifdef PROVIDE_TRACE_CODEGEN
1105 class TraceCodeGenerator : public CodeGenerator {
1106 private:
1107 CodeGenerator* mpBase;
1108
1109 public:
1110 TraceCodeGenerator(CodeGenerator* pBase) {
1111 mpBase = pBase;
1112 }
1113
1114 virtual ~TraceCodeGenerator() {
1115 delete mpBase;
1116 }
1117
1118 virtual void init(CodeBuf* pCodeBuf) {
1119 mpBase->init(pCodeBuf);
1120 }
1121
1122 void setErrorSink(ErrorSink* pErrorSink) {
1123 mpBase->setErrorSink(pErrorSink);
1124 }
1125
1126 /* returns address to patch with local variable size
1127 */
1128 virtual int functionEntry(int argCount) {
1129 int result = mpBase->functionEntry(argCount);
1130 fprintf(stderr, "functionEntry(%d) -> %d\n", argCount, result);
1131 return result;
1132 }
1133
1134 virtual void functionExit(int argCount, int localVariableAddress, int localVariableSize) {
1135 fprintf(stderr, "functionExit(%d, %d, %d)\n",
1136 argCount, localVariableAddress, localVariableSize);
1137 mpBase->functionExit(argCount, localVariableAddress, localVariableSize);
1138 }
1139
1140 /* load immediate value */
1141 virtual void li(int t) {
1142 fprintf(stderr, "li(%d)\n", t);
1143 mpBase->li(t);
1144 }
1145
1146 virtual int gjmp(int t) {
1147 int result = mpBase->gjmp(t);
1148 fprintf(stderr, "gjmp(%d) = %d\n", t, result);
1149 return result;
1150 }
1151
1152 /* l = 0: je, l == 1: jne */
1153 virtual int gtst(bool l, int t) {
1154 int result = mpBase->gtst(l, t);
1155 fprintf(stderr, "gtst(%d,%d) = %d\n", l, t, result);
1156 return result;
1157 }
1158
1159 virtual void gcmp(int op) {
1160 fprintf(stderr, "gcmp(%d)\n", op);
1161 mpBase->gcmp(op);
1162 }
1163
1164 virtual void genOp(int op) {
1165 fprintf(stderr, "genOp(%d)\n", op);
1166 mpBase->genOp(op);
1167 }
1168
1169 virtual void clearR1() {
1170 fprintf(stderr, "clearR1()\n");
1171 mpBase->clearR1();
1172 }
1173
1174 virtual void pushR0() {
1175 fprintf(stderr, "pushR0()\n");
1176 mpBase->pushR0();
1177 }
1178
1179 virtual void popR1() {
1180 fprintf(stderr, "popR1()\n");
1181 mpBase->popR1();
1182 }
1183
1184 virtual void storeR0ToR1(bool isInt) {
1185 fprintf(stderr, "storeR0ToR1(%d)\n", isInt);
1186 mpBase->storeR0ToR1(isInt);
1187 }
1188
1189 virtual void loadR0FromR0(bool isInt) {
1190 fprintf(stderr, "loadR0FromR0(%d)\n", isInt);
1191 mpBase->loadR0FromR0(isInt);
1192 }
1193
1194 virtual void leaR0(int ea) {
1195 fprintf(stderr, "leaR0(%d)\n", ea);
1196 mpBase->leaR0(ea);
1197 }
1198
1199 virtual void storeR0(int ea) {
1200 fprintf(stderr, "storeR0(%d)\n", ea);
1201 mpBase->storeR0(ea);
1202 }
1203
1204 virtual void loadR0(int ea, bool isIncDec, int op) {
1205 fprintf(stderr, "loadR0(%d, %d, %d)\n", ea, isIncDec, op);
1206 mpBase->loadR0(ea, isIncDec, op);
1207 }
1208
1209 virtual int beginFunctionCallArguments() {
1210 int result = mpBase->beginFunctionCallArguments();
1211 fprintf(stderr, "beginFunctionCallArguments() = %d\n", result);
1212 return result;
1213 }
1214
1215 virtual void storeR0ToArg(int l) {
1216 fprintf(stderr, "storeR0ToArg(%d)\n", l);
1217 mpBase->storeR0ToArg(l);
1218 }
1219
1220 virtual void endFunctionCallArguments(int a, int l) {
1221 fprintf(stderr, "endFunctionCallArguments(%d, %d)\n", a, l);
1222 mpBase->endFunctionCallArguments(a, l);
1223 }
1224
1225 virtual int callForward(int symbol) {
1226 int result = mpBase->callForward(symbol);
1227 fprintf(stderr, "callForward(%d) = %d\n", symbol, result);
1228 return result;
1229 }
1230
1231 virtual void callRelative(int t) {
1232 fprintf(stderr, "callRelative(%d)\n", t);
1233 mpBase->callRelative(t);
1234 }
1235
1236 virtual void callIndirect(int l) {
1237 fprintf(stderr, "callIndirect(%d)\n", l);
1238 mpBase->callIndirect(l);
1239 }
1240
1241 virtual void adjustStackAfterCall(int l, bool isIndirect) {
1242 fprintf(stderr, "adjustStackAfterCall(%d, %d)\n", l, isIndirect);
1243 mpBase->adjustStackAfterCall(l, isIndirect);
1244 }
1245
1246 virtual int jumpOffset() {
1247 return mpBase->jumpOffset();
1248 }
1249
1250 virtual int disassemble(FILE* out) {
1251 return mpBase->disassemble(out);
1252 }
1253
1254 /* output a symbol and patch all calls to it */
1255 virtual void gsym(int t) {
1256 fprintf(stderr, "gsym(%d)\n", t);
1257 mpBase->gsym(t);
1258 }
1259
1260 virtual int finishCompile() {
1261 int result = mpBase->finishCompile();
1262 fprintf(stderr, "finishCompile() = %d\n", result);
1263 return result;
1264 }
1265 };
1266
1267#endif // PROVIDE_TRACE_CODEGEN
1268
Jack Palevich569f1352009-06-29 14:29:08 -07001269
1270 // Subset of STL vector.
1271 template<class E> class Vector {
1272 public:
1273 Vector() {
1274 mpBase = 0;
1275 mUsed = 0;
1276 mSize = 0;
1277 }
1278
1279 ~Vector() {
1280 if (mpBase) {
1281 for(size_t i = 0; i < mUsed; i++) {
1282 mpBase[mUsed].~E();
1283 }
1284 free(mpBase);
1285 }
1286 }
1287
1288 inline E& operator[](size_t i) {
1289 return mpBase[i];
1290 }
1291
1292 inline E& front() {
1293 return mpBase[0];
1294 }
1295
1296 inline E& back() {
1297 return mpBase[mUsed - 1];
1298 }
1299
1300 void pop_back() {
1301 mUsed -= 1;
1302 mpBase[mUsed].~E();
1303 }
1304
1305 void push_back(const E& item) {
1306 * ensure(1) = item;
1307 }
1308
1309 size_t size() {
1310 return mUsed;
1311 }
1312
1313 private:
1314 E* ensure(int n) {
1315 size_t newUsed = mUsed + n;
1316 if (newUsed > mSize) {
1317 size_t newSize = mSize * 2 + 10;
1318 if (newSize < newUsed) {
1319 newSize = newUsed;
1320 }
1321 mpBase = (E*) realloc(mpBase, sizeof(E) * newSize);
1322 mSize = newSize;
1323 }
1324 E* result = mpBase + mUsed;
1325 mUsed = newUsed;
1326 return result;
1327 }
1328
1329 E* mpBase;
1330 size_t mUsed;
1331 size_t mSize;
1332 };
1333
1334 class Arena {
1335 public:
1336 // Used to record a given allocation amount.
1337 // Used:
1338 // Mark mark = arena.mark();
1339 // ... lots of arena.allocate()
1340 // arena.free(mark);
1341
1342 struct Mark {
1343 size_t chunk;
1344 size_t offset;
1345 };
1346
1347 Arena() {
1348 mCurrentChunk = 0;
1349 Chunk start(CHUNK_SIZE);
1350 mData.push_back(start);
1351 }
1352
1353 ~Arena() {
1354 for(size_t i = 0; i < mData.size(); i++) {
1355 mData[i].free();
1356 }
1357 }
1358
1359 // Alloc using the standard alignment size safe for any variable
1360 void* alloc(size_t size) {
1361 return alloc(size, 8);
1362 }
1363
1364 Mark mark(){
1365 Mark result;
1366 result.chunk = mCurrentChunk;
1367 result.offset = mData[mCurrentChunk].mOffset;
1368 return result;
1369 }
1370
1371 void freeToMark(const Mark& mark) {
1372 mCurrentChunk = mark.chunk;
1373 mData[mCurrentChunk].mOffset = mark.offset;
1374 }
1375
1376 private:
1377 // Allocate memory aligned to a given size
1378 // and a given power-of-two-sized alignment (e.g. 1,2,4,8,...)
1379 // Memory is not zero filled.
1380
1381 void* alloc(size_t size, size_t alignment) {
1382 while (size > mData[mCurrentChunk].remainingCapacity(alignment)) {
1383 if (mCurrentChunk + 1 < mData.size()) {
1384 mCurrentChunk++;
1385 } else {
1386 size_t allocSize = CHUNK_SIZE;
1387 if (allocSize < size + alignment - 1) {
1388 allocSize = size + alignment - 1;
1389 }
1390 Chunk chunk(allocSize);
1391 mData.push_back(chunk);
1392 mCurrentChunk++;
1393 }
1394 }
1395 return mData[mCurrentChunk].allocate(size, alignment);
1396 }
1397
1398 static const size_t CHUNK_SIZE = 128*1024;
1399 // Note: this class does not deallocate its
1400 // memory when it's destroyed. It depends upon
1401 // its parent to deallocate the memory.
1402 struct Chunk {
1403 Chunk() {
1404 mpData = 0;
1405 mSize = 0;
1406 mOffset = 0;
1407 }
1408
1409 Chunk(size_t size) {
1410 mSize = size;
1411 mpData = (char*) malloc(size);
1412 mOffset = 0;
1413 }
1414
1415 ~Chunk() {
1416 // Doesn't deallocate memory.
1417 }
1418
1419 void* allocate(size_t size, size_t alignment) {
1420 size_t alignedOffset = aligned(mOffset, alignment);
1421 void* result = mpData + alignedOffset;
1422 mOffset = alignedOffset + size;
1423 return result;
1424 }
1425
1426 void free() {
1427 if (mpData) {
1428 ::free(mpData);
1429 mpData = 0;
1430 }
1431 }
1432
1433 size_t remainingCapacity(size_t alignment) {
1434 return aligned(mSize, alignment) - aligned(mOffset, alignment);
1435 }
1436
1437 // Assume alignment is a power of two
1438 inline size_t aligned(size_t v, size_t alignment) {
1439 size_t mask = alignment-1;
1440 return (v + mask) & ~mask;
1441 }
1442
1443 char* mpData;
1444 size_t mSize;
1445 size_t mOffset;
1446 };
1447
1448 size_t mCurrentChunk;
1449
1450 Vector<Chunk> mData;
1451 };
1452
1453 typedef int tokenid_t;
1454 struct VariableInfo;
1455
1456 struct Token {
1457 int hash;
1458 size_t length;
1459 char* pText;
1460 tokenid_t id;
1461
1462 // Current values for the token
1463 char* mpMacroDefinition;
1464 VariableInfo* mpVariableInfo;
1465 };
1466
1467 class TokenTable {
1468 public:
1469 // Don't use 0..0xff, allows characters and operators to be tokens too.
1470
1471 static const int TOKEN_BASE = 0x100;
1472 TokenTable() {
1473 mpMap = hashmapCreate(128, hashFn, equalsFn);
1474 }
1475
1476 ~TokenTable() {
1477 hashmapFree(mpMap);
1478 }
1479
1480 void setArena(Arena* pArena) {
1481 mpArena = pArena;
1482 }
1483
1484 // Returns a token for a given string of characters.
1485 tokenid_t intern(const char* pText, size_t length) {
1486 Token probe;
1487 int hash = hashmapHash((void*) pText, length);
1488 {
1489 Token probe;
1490 probe.hash = hash;
1491 probe.length = length;
1492 probe.pText = (char*) pText;
1493 Token* pValue = (Token*) hashmapGet(mpMap, &probe);
1494 if (pValue) {
Jack Palevich569f1352009-06-29 14:29:08 -07001495 return pValue->id;
1496 }
1497 }
1498
1499 Token* pToken = (Token*) mpArena->alloc(sizeof(Token));
1500 memset(pToken, 0, sizeof(*pToken));
1501 pToken->hash = hash;
1502 pToken->length = length;
1503 pToken->pText = (char*) mpArena->alloc(length + 1);
1504 memcpy(pToken->pText, pText, length);
1505 pToken->pText[length] = 0;
1506 pToken->id = mTokens.size() + TOKEN_BASE;
1507 mTokens.push_back(pToken);
1508 hashmapPut(mpMap, pToken, pToken);
Jack Palevich569f1352009-06-29 14:29:08 -07001509 return pToken->id;
1510 }
1511
1512 // Return the Token for a given tokenid.
1513 Token& operator[](tokenid_t id) {
1514 return *mTokens[id - TOKEN_BASE];
1515 }
1516
1517 inline size_t size() {
1518 return mTokens.size();
1519 }
1520
1521 private:
1522
1523 static int hashFn(void* pKey) {
1524 Token* pToken = (Token*) pKey;
1525 return pToken->hash;
1526 }
1527
1528 static bool equalsFn(void* keyA, void* keyB) {
1529 Token* pTokenA = (Token*) keyA;
1530 Token* pTokenB = (Token*) keyB;
1531 // Don't need to compare hash values, they should always be equal
1532 return pTokenA->length == pTokenB->length
1533 && strcmp(pTokenA->pText, pTokenB->pText) == 0;
1534 }
1535
1536 Hashmap* mpMap;
1537 Vector<Token*> mTokens;
1538 Arena* mpArena;
1539 };
1540
Jack Palevich1cdef202009-05-22 12:06:27 -07001541 class InputStream {
1542 public:
Jack Palevicheedf9d22009-06-04 16:23:40 -07001543 int getChar() {
1544 if (bumpLine) {
1545 line++;
1546 bumpLine = false;
1547 }
1548 int ch = get();
1549 if (ch == '\n') {
1550 bumpLine = true;
1551 }
1552 return ch;
1553 }
1554 int getLine() {
1555 return line;
1556 }
1557 protected:
1558 InputStream() :
1559 line(1), bumpLine(false) {
1560 }
1561 private:
Jack Palevich1cdef202009-05-22 12:06:27 -07001562 virtual int get() = 0;
Jack Palevicheedf9d22009-06-04 16:23:40 -07001563 int line;
1564 bool bumpLine;
Jack Palevich1cdef202009-05-22 12:06:27 -07001565 };
1566
1567 class FileInputStream : public InputStream {
1568 public:
1569 FileInputStream(FILE* in) : f(in) {}
Jack Palevich1cdef202009-05-22 12:06:27 -07001570 private:
Jack Palevicheedf9d22009-06-04 16:23:40 -07001571 virtual int get() { return fgetc(f); }
Jack Palevich1cdef202009-05-22 12:06:27 -07001572 FILE* f;
1573 };
1574
1575 class TextInputStream : public InputStream {
1576 public:
1577 TextInputStream(const char* text, size_t textLength)
1578 : pText(text), mTextLength(textLength), mPosition(0) {
1579 }
Jack Palevicheedf9d22009-06-04 16:23:40 -07001580
1581 private:
Jack Palevich1cdef202009-05-22 12:06:27 -07001582 virtual int get() {
1583 return mPosition < mTextLength ? pText[mPosition++] : EOF;
1584 }
Jack Palevich1cdef202009-05-22 12:06:27 -07001585
Jack Palevich1cdef202009-05-22 12:06:27 -07001586 const char* pText;
1587 size_t mTextLength;
1588 size_t mPosition;
1589 };
1590
Jack Palevicheedf9d22009-06-04 16:23:40 -07001591 class String {
1592 public:
1593 String() {
1594 mpBase = 0;
1595 mUsed = 0;
1596 mSize = 0;
1597 }
1598
Jack Palevich303d8ff2009-06-11 19:06:24 -07001599 String(const char* item, int len, bool adopt) {
1600 if (len < 0) {
1601 len = strlen(item);
1602 }
Jack Palevich2d11dfb2009-06-08 14:34:26 -07001603 if (adopt) {
Jack Palevich303d8ff2009-06-11 19:06:24 -07001604 mpBase = (char*) item;
Jack Palevich2d11dfb2009-06-08 14:34:26 -07001605 mUsed = len;
1606 mSize = len + 1;
1607 } else {
1608 mpBase = 0;
1609 mUsed = 0;
1610 mSize = 0;
1611 appendBytes(item, len);
1612 }
1613 }
1614
Jack Palevich303d8ff2009-06-11 19:06:24 -07001615 String(const String& other) {
1616 mpBase = 0;
1617 mUsed = 0;
1618 mSize = 0;
1619 appendBytes(other.getUnwrapped(), other.len());
1620 }
1621
Jack Palevicheedf9d22009-06-04 16:23:40 -07001622 ~String() {
1623 if (mpBase) {
1624 free(mpBase);
1625 }
1626 }
1627
Jack Palevicha6baa232009-06-12 11:25:59 -07001628 String& operator=(const String& other) {
1629 clear();
1630 appendBytes(other.getUnwrapped(), other.len());
1631 return *this;
1632 }
1633
Jack Palevich303d8ff2009-06-11 19:06:24 -07001634 inline char* getUnwrapped() const {
Jack Palevicheedf9d22009-06-04 16:23:40 -07001635 return mpBase;
1636 }
1637
Jack Palevich303d8ff2009-06-11 19:06:24 -07001638 void clear() {
1639 mUsed = 0;
1640 if (mSize > 0) {
1641 mpBase[0] = 0;
1642 }
1643 }
1644
Jack Palevicheedf9d22009-06-04 16:23:40 -07001645 void appendCStr(const char* s) {
Jack Palevich2d11dfb2009-06-08 14:34:26 -07001646 appendBytes(s, strlen(s));
1647 }
1648
1649 void appendBytes(const char* s, int n) {
Jack Palevicheedf9d22009-06-04 16:23:40 -07001650 memcpy(ensure(n), s, n + 1);
1651 }
1652
1653 void append(char c) {
1654 * ensure(1) = c;
1655 }
1656
Jack Palevich86351982009-06-30 18:09:56 -07001657 void append(String& other) {
1658 appendBytes(other.getUnwrapped(), other.len());
1659 }
1660
Jack Palevich2d11dfb2009-06-08 14:34:26 -07001661 char* orphan() {
1662 char* result = mpBase;
1663 mpBase = 0;
1664 mUsed = 0;
1665 mSize = 0;
1666 return result;
1667 }
1668
Jack Palevicheedf9d22009-06-04 16:23:40 -07001669 void printf(const char* fmt,...) {
1670 va_list ap;
1671 va_start(ap, fmt);
1672 vprintf(fmt, ap);
1673 va_end(ap);
1674 }
1675
1676 void vprintf(const char* fmt, va_list ap) {
1677 char* temp;
1678 int numChars = vasprintf(&temp, fmt, ap);
1679 memcpy(ensure(numChars), temp, numChars+1);
1680 free(temp);
1681 }
1682
Jack Palevich303d8ff2009-06-11 19:06:24 -07001683 inline size_t len() const {
Jack Palevicheedf9d22009-06-04 16:23:40 -07001684 return mUsed;
1685 }
1686
1687 private:
1688 char* ensure(int n) {
1689 size_t newUsed = mUsed + n;
1690 if (newUsed > mSize) {
1691 size_t newSize = mSize * 2 + 10;
1692 if (newSize < newUsed) {
1693 newSize = newUsed;
1694 }
1695 mpBase = (char*) realloc(mpBase, newSize + 1);
1696 mSize = newSize;
1697 }
1698 mpBase[newUsed] = '\0';
1699 char* result = mpBase + mUsed;
1700 mUsed = newUsed;
1701 return result;
1702 }
1703
1704 char* mpBase;
1705 size_t mUsed;
1706 size_t mSize;
1707 };
1708
Jack Palevich569f1352009-06-29 14:29:08 -07001709 void internKeywords() {
1710 // Note: order has to match TOK_ constants
1711 static const char* keywords[] = {
1712 "int",
1713 "char",
1714 "void",
1715 "if",
1716 "else",
1717 "while",
1718 "break",
1719 "return",
1720 "for",
1721 "pragma",
1722 "define",
1723 "auto",
1724 "case",
1725 "const",
1726 "continue",
1727 "default",
1728 "do",
1729 "double",
1730 "enum",
1731 "extern",
1732 "float",
1733 "goto",
1734 "long",
1735 "register",
1736 "short",
1737 "signed",
1738 "sizeof",
1739 "static",
1740 "struct",
1741 "switch",
1742 "typedef",
1743 "union",
1744 "unsigned",
1745 "volatile",
1746 "_Bool",
1747 "_Complex",
1748 "_Imaginary",
1749 "inline",
1750 "restrict",
1751 0};
Jack Palevich2d11dfb2009-06-08 14:34:26 -07001752
Jack Palevich569f1352009-06-29 14:29:08 -07001753 for(int i = 0; keywords[i]; i++) {
1754 mTokenTable.intern(keywords[i], strlen(keywords[i]));
Jack Palevich2d11dfb2009-06-08 14:34:26 -07001755 }
Jack Palevich569f1352009-06-29 14:29:08 -07001756 }
Jack Palevich2d11dfb2009-06-08 14:34:26 -07001757
Jack Palevich36d94142009-06-08 15:55:32 -07001758 struct InputState {
1759 InputStream* pStream;
1760 int oldCh;
1761 };
1762
Jack Palevich86351982009-06-30 18:09:56 -07001763 struct Type;
1764
Jack Palevich2db168f2009-06-11 14:29:47 -07001765 struct VariableInfo {
Jack Palevich303d8ff2009-06-11 19:06:24 -07001766 void* pAddress;
1767 void* pForward; // For a forward direction, linked list of data to fix up
Jack Palevich569f1352009-06-29 14:29:08 -07001768 tokenid_t tok;
1769 size_t level;
1770 VariableInfo* pOldDefinition;
Jack Palevich86351982009-06-30 18:09:56 -07001771 Type* pType;
Jack Palevich2db168f2009-06-11 14:29:47 -07001772 };
1773
Jack Palevich303d8ff2009-06-11 19:06:24 -07001774 class SymbolStack {
1775 public:
1776 SymbolStack() {
Jack Palevich569f1352009-06-29 14:29:08 -07001777 mpArena = 0;
1778 mpTokenTable = 0;
1779 }
1780
1781 void setArena(Arena* pArena) {
1782 mpArena = pArena;
1783 }
1784
1785 void setTokenTable(TokenTable* pTokenTable) {
1786 mpTokenTable = pTokenTable;
Jack Palevich303d8ff2009-06-11 19:06:24 -07001787 }
1788
1789 void pushLevel() {
Jack Palevich569f1352009-06-29 14:29:08 -07001790 Mark mark;
1791 mark.mArenaMark = mpArena->mark();
1792 mark.mSymbolHead = mStack.size();
1793 mLevelStack.push_back(mark);
Jack Palevich303d8ff2009-06-11 19:06:24 -07001794 }
1795
1796 void popLevel() {
Jack Palevich569f1352009-06-29 14:29:08 -07001797 // Undo any shadowing that was done:
1798 Mark mark = mLevelStack.back();
1799 mLevelStack.pop_back();
1800 while (mStack.size() > mark.mSymbolHead) {
1801 VariableInfo* pV = mStack.back();
1802 mStack.pop_back();
1803 (*mpTokenTable)[pV->tok].mpVariableInfo = pV->pOldDefinition;
Jack Palevich303d8ff2009-06-11 19:06:24 -07001804 }
Jack Palevich569f1352009-06-29 14:29:08 -07001805 mpArena->freeToMark(mark.mArenaMark);
Jack Palevich303d8ff2009-06-11 19:06:24 -07001806 }
1807
Jack Palevich569f1352009-06-29 14:29:08 -07001808 bool isDefinedAtCurrentLevel(tokenid_t tok) {
1809 VariableInfo* pV = (*mpTokenTable)[tok].mpVariableInfo;
1810 return pV && pV->level == level();
1811 }
1812
1813 VariableInfo* add(tokenid_t tok) {
1814 Token& token = (*mpTokenTable)[tok];
1815 VariableInfo* pOldV = token.mpVariableInfo;
1816 VariableInfo* pNewV =
1817 (VariableInfo*) mpArena->alloc(sizeof(VariableInfo));
1818 memset(pNewV, 0, sizeof(VariableInfo));
1819 pNewV->tok = tok;
1820 pNewV->level = level();
1821 pNewV->pOldDefinition = pOldV;
1822 token.mpVariableInfo = pNewV;
1823 mStack.push_back(pNewV);
1824 return pNewV;
1825 }
1826
Jack Palevich86351982009-06-30 18:09:56 -07001827 VariableInfo* add(Type* pType) {
1828 VariableInfo* pVI = add(pType->id);
1829 pVI->pType = pType;
1830 return pVI;
1831 }
1832
Jack Palevich569f1352009-06-29 14:29:08 -07001833 void forEach(bool (*fn)(VariableInfo*, void*), void* context) {
1834 for (size_t i = 0; i < mStack.size(); i++) {
1835 if (! fn(mStack[i], context)) {
Jack Palevich303d8ff2009-06-11 19:06:24 -07001836 break;
1837 }
1838 }
Jack Palevicha6baa232009-06-12 11:25:59 -07001839 }
1840
Jack Palevich303d8ff2009-06-11 19:06:24 -07001841 private:
Jack Palevich569f1352009-06-29 14:29:08 -07001842 inline size_t level() {
1843 return mLevelStack.size();
Jack Palevich303d8ff2009-06-11 19:06:24 -07001844 }
1845
Jack Palevich569f1352009-06-29 14:29:08 -07001846 struct Mark {
1847 Arena::Mark mArenaMark;
1848 size_t mSymbolHead;
Jack Palevich303d8ff2009-06-11 19:06:24 -07001849 };
1850
Jack Palevich569f1352009-06-29 14:29:08 -07001851 Arena* mpArena;
1852 TokenTable* mpTokenTable;
1853 Vector<VariableInfo*> mStack;
1854 Vector<Mark> mLevelStack;
Jack Palevich303d8ff2009-06-11 19:06:24 -07001855 };
Jack Palevich36d94142009-06-08 15:55:32 -07001856
Jack Palevich40600de2009-07-01 15:32:35 -07001857 struct Value {
1858 Type* pType;
1859 bool mLValue; // This is the L-value (true means the lvalue)
1860 };
1861
Jack Palevich36d94142009-06-08 15:55:32 -07001862 int ch; // Current input character, or EOF
Jack Palevich569f1352009-06-29 14:29:08 -07001863 tokenid_t tok; // token
Jack Palevich36d94142009-06-08 15:55:32 -07001864 intptr_t tokc; // token extra info
1865 int tokl; // token operator level
1866 intptr_t rsym; // return symbol
1867 intptr_t loc; // local variable index
1868 char* glo; // global variable index
Jack Palevich303d8ff2009-06-11 19:06:24 -07001869 String mTokenString;
Jack Palevich36d94142009-06-08 15:55:32 -07001870 char* dptr; // Macro state: Points to macro text during macro playback.
1871 int dch; // Macro state: Saves old value of ch during a macro playback.
Jack Palevich36d94142009-06-08 15:55:32 -07001872 char* pGlobalBase;
Jack Palevich569f1352009-06-29 14:29:08 -07001873
1874 // Arena for the duration of the compile
1875 Arena mGlobalArena;
1876 // Arena for data that's only needed when compiling a single function
1877 Arena mLocalArena;
1878
1879 TokenTable mTokenTable;
1880 SymbolStack mGlobals;
1881 SymbolStack mLocals;
1882
Jack Palevich40600de2009-07-01 15:32:35 -07001883 // Prebuilt types, makes things slightly faster.
Jack Palevich86351982009-06-30 18:09:56 -07001884 Type* mkpInt;
1885 Type* mkpChar;
1886 Type* mkpVoid;
1887
Jack Palevich40600de2009-07-01 15:32:35 -07001888 // Track what's on the expression stack
1889 Vector<Value> mValueStack;
1890
Jack Palevich36d94142009-06-08 15:55:32 -07001891 InputStream* file;
1892
1893 CodeBuf codeBuf;
1894 CodeGenerator* pGen;
1895
Jack Palevicheedf9d22009-06-04 16:23:40 -07001896 String mErrorBuf;
1897
Jack Palevicheedf9d22009-06-04 16:23:40 -07001898 String mPragmas;
1899 int mPragmaStringCount;
1900
Jack Palevich21a15a22009-05-11 14:49:29 -07001901 static const int ALLOC_SIZE = 99999;
1902
Jack Palevich303d8ff2009-06-11 19:06:24 -07001903 static const int TOK_DUMMY = 1;
1904 static const int TOK_NUM = 2;
1905
1906 // 3..255 are character and/or operators
1907
Jack Palevich2db168f2009-06-11 14:29:47 -07001908 // Keywords start at 0x100 and increase by 1
Jack Palevich569f1352009-06-29 14:29:08 -07001909 // Order has to match string list in "internKeywords".
1910 enum {
1911 TOK_KEYWORD = TokenTable::TOKEN_BASE,
1912 TOK_INT = TOK_KEYWORD,
1913 TOK_CHAR,
1914 TOK_VOID,
1915 TOK_IF,
1916 TOK_ELSE,
1917 TOK_WHILE,
1918 TOK_BREAK,
1919 TOK_RETURN,
1920 TOK_FOR,
1921 TOK_PRAGMA,
1922 TOK_DEFINE,
1923 TOK_AUTO,
1924 TOK_CASE,
1925 TOK_CONST,
1926 TOK_CONTINUE,
1927 TOK_DEFAULT,
1928 TOK_DO,
1929 TOK_DOUBLE,
1930 TOK_ENUM,
1931 TOK_EXTERN,
1932 TOK_FLOAT,
1933 TOK_GOTO,
1934 TOK_LONG,
1935 TOK_REGISTER,
1936 TOK_SHORT,
1937 TOK_SIGNED,
1938 TOK_SIZEOF,
1939 TOK_STATIC,
1940 TOK_STRUCT,
1941 TOK_SWITCH,
1942 TOK_TYPEDEF,
1943 TOK_UNION,
1944 TOK_UNSIGNED,
1945 TOK_VOLATILE,
1946 TOK__BOOL,
1947 TOK__COMPLEX,
1948 TOK__IMAGINARY,
1949 TOK_INLINE,
1950 TOK_RESTRICT,
1951 // Symbols start after tokens
1952 TOK_SYMBOL
1953 };
Jack Palevich21a15a22009-05-11 14:49:29 -07001954
1955 static const int LOCAL = 0x200;
1956
1957 static const int SYM_FORWARD = 0;
1958 static const int SYM_DEFINE = 1;
1959
1960 /* tokens in string heap */
1961 static const int TAG_TOK = ' ';
Jack Palevich21a15a22009-05-11 14:49:29 -07001962
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001963 static const int OP_INCREMENT = 0;
1964 static const int OP_DECREMENT = 1;
1965 static const int OP_MUL = 2;
1966 static const int OP_DIV = 3;
1967 static const int OP_MOD = 4;
1968 static const int OP_PLUS = 5;
1969 static const int OP_MINUS = 6;
1970 static const int OP_SHIFT_LEFT = 7;
1971 static const int OP_SHIFT_RIGHT = 8;
1972 static const int OP_LESS_EQUAL = 9;
1973 static const int OP_GREATER_EQUAL = 10;
1974 static const int OP_LESS = 11;
1975 static const int OP_GREATER = 12;
1976 static const int OP_EQUALS = 13;
1977 static const int OP_NOT_EQUALS = 14;
1978 static const int OP_LOGICAL_AND = 15;
1979 static const int OP_LOGICAL_OR = 16;
1980 static const int OP_BIT_AND = 17;
1981 static const int OP_BIT_XOR = 18;
1982 static const int OP_BIT_OR = 19;
1983 static const int OP_BIT_NOT = 20;
1984 static const int OP_LOGICAL_NOT = 21;
1985 static const int OP_COUNT = 22;
1986
1987 /* Operators are searched from front, the two-character operators appear
1988 * before the single-character operators with the same first character.
1989 * @ is used to pad out single-character operators.
1990 */
1991 static const char* operatorChars;
1992 static const char operatorLevel[];
1993
Jack Palevich569f1352009-06-29 14:29:08 -07001994 /* Called when we detect an internal problem. Does nothing in production.
1995 *
1996 */
1997 void internalError() {
1998 * (char*) 0 = 0;
1999 }
2000
Jack Palevich86351982009-06-30 18:09:56 -07002001 void assert(bool isTrue) {
2002 if (!isTrue) {
Jack Palevich569f1352009-06-29 14:29:08 -07002003 internalError();
2004 }
Jack Palevich86351982009-06-30 18:09:56 -07002005 }
2006
Jack Palevich40600de2009-07-01 15:32:35 -07002007 bool isSymbol(tokenid_t t) {
2008 return t >= TOK_SYMBOL &&
2009 ((size_t) (t-TOK_SYMBOL)) < mTokenTable.size();
2010 }
2011
2012 bool isSymbolOrKeyword(tokenid_t t) {
2013 return t >= TOK_KEYWORD &&
2014 ((size_t) (t-TOK_SYMBOL)) < mTokenTable.size();
2015 }
2016
Jack Palevich86351982009-06-30 18:09:56 -07002017 VariableInfo* VI(tokenid_t t) {
Jack Palevich40600de2009-07-01 15:32:35 -07002018 assert(isSymbol(t));
Jack Palevich569f1352009-06-29 14:29:08 -07002019 VariableInfo* pV = mTokenTable[t].mpVariableInfo;
2020 if (pV && pV->tok != t) {
2021 internalError();
2022 }
2023 return pV;
2024 }
2025
2026 inline bool isDefined(tokenid_t t) {
2027 return t >= TOK_SYMBOL && VI(t) != 0;
2028 }
2029
Jack Palevich40600de2009-07-01 15:32:35 -07002030 const char* nameof(tokenid_t t) {
2031 assert(isSymbolOrKeyword(t));
Jack Palevich569f1352009-06-29 14:29:08 -07002032 return mTokenTable[t].pText;
2033 }
2034
Jack Palevich21a15a22009-05-11 14:49:29 -07002035 void pdef(int t) {
Jack Palevich303d8ff2009-06-11 19:06:24 -07002036 mTokenString.append(t);
Jack Palevich21a15a22009-05-11 14:49:29 -07002037 }
2038
2039 void inp() {
2040 if (dptr) {
Jack Palevich653f42d2009-05-28 17:15:32 -07002041 ch = *dptr++;
Jack Palevich2d11dfb2009-06-08 14:34:26 -07002042 if (ch == 0) {
Jack Palevich21a15a22009-05-11 14:49:29 -07002043 dptr = 0;
2044 ch = dch;
2045 }
2046 } else
Jack Palevicheedf9d22009-06-04 16:23:40 -07002047 ch = file->getChar();
Jack Palevichb7c81e92009-06-04 19:56:13 -07002048#if 0
2049 printf("ch='%c' 0x%x\n", ch, ch);
2050#endif
Jack Palevich21a15a22009-05-11 14:49:29 -07002051 }
2052
2053 int isid() {
Jack Palevich546b2242009-05-13 15:10:04 -07002054 return isalnum(ch) | (ch == '_');
Jack Palevich21a15a22009-05-11 14:49:29 -07002055 }
2056
Jack Palevichb4758ff2009-06-12 12:49:14 -07002057 /* read a character constant, advances ch to after end of constant */
2058 int getq() {
2059 int val = ch;
Jack Palevich21a15a22009-05-11 14:49:29 -07002060 if (ch == '\\') {
2061 inp();
Jack Palevichb4758ff2009-06-12 12:49:14 -07002062 if (isoctal(ch)) {
2063 // 1 to 3 octal characters.
2064 val = 0;
2065 for(int i = 0; i < 3; i++) {
2066 if (isoctal(ch)) {
2067 val = (val << 3) + ch - '0';
2068 inp();
2069 }
2070 }
2071 return val;
2072 } else if (ch == 'x' || ch == 'X') {
2073 // N hex chars
2074 inp();
2075 if (! isxdigit(ch)) {
2076 error("'x' character escape requires at least one digit.");
2077 } else {
2078 val = 0;
2079 while (isxdigit(ch)) {
2080 int d = ch;
2081 if (isdigit(d)) {
2082 d -= '0';
2083 } else if (d <= 'F') {
2084 d = d - 'A' + 10;
2085 } else {
2086 d = d - 'a' + 10;
2087 }
2088 val = (val << 4) + d;
2089 inp();
2090 }
2091 }
2092 } else {
2093 int val = ch;
2094 switch (ch) {
2095 case 'a':
2096 val = '\a';
2097 break;
2098 case 'b':
2099 val = '\b';
2100 break;
2101 case 'f':
2102 val = '\f';
2103 break;
2104 case 'n':
2105 val = '\n';
2106 break;
2107 case 'r':
2108 val = '\r';
2109 break;
2110 case 't':
2111 val = '\t';
2112 break;
2113 case 'v':
2114 val = '\v';
2115 break;
2116 case '\\':
2117 val = '\\';
2118 break;
2119 case '\'':
2120 val = '\'';
2121 break;
2122 case '"':
2123 val = '"';
2124 break;
2125 case '?':
2126 val = '?';
2127 break;
2128 default:
2129 error("Undefined character escape %c", ch);
2130 break;
2131 }
2132 inp();
2133 return val;
2134 }
2135 } else {
2136 inp();
Jack Palevich21a15a22009-05-11 14:49:29 -07002137 }
Jack Palevichb4758ff2009-06-12 12:49:14 -07002138 return val;
2139 }
2140
2141 static bool isoctal(int ch) {
2142 return ch >= '0' && ch <= '7';
Jack Palevich21a15a22009-05-11 14:49:29 -07002143 }
2144
2145 void next() {
2146 int l, a;
2147
Jack Palevich546b2242009-05-13 15:10:04 -07002148 while (isspace(ch) | (ch == '#')) {
Jack Palevich21a15a22009-05-11 14:49:29 -07002149 if (ch == '#') {
2150 inp();
2151 next();
2152 if (tok == TOK_DEFINE) {
Jack Palevich2d11dfb2009-06-08 14:34:26 -07002153 doDefine();
Jack Palevicheedf9d22009-06-04 16:23:40 -07002154 } else if (tok == TOK_PRAGMA) {
2155 doPragma();
2156 } else {
Jack Palevich303d8ff2009-06-11 19:06:24 -07002157 error("Unsupported preprocessor directive \"%s\"",
2158 mTokenString.getUnwrapped());
Jack Palevich21a15a22009-05-11 14:49:29 -07002159 }
Jack Palevich21a15a22009-05-11 14:49:29 -07002160 }
2161 inp();
2162 }
2163 tokl = 0;
2164 tok = ch;
2165 /* encode identifiers & numbers */
2166 if (isid()) {
Jack Palevich303d8ff2009-06-11 19:06:24 -07002167 mTokenString.clear();
Jack Palevich21a15a22009-05-11 14:49:29 -07002168 while (isid()) {
Jack Paleviche27bf3e2009-05-10 14:09:03 -07002169 pdef(ch);
2170 inp();
Jack Palevichae54f1f2009-05-08 14:54:15 -07002171 }
Jack Palevich21a15a22009-05-11 14:49:29 -07002172 if (isdigit(tok)) {
Jack Palevich303d8ff2009-06-11 19:06:24 -07002173 tokc = strtol(mTokenString.getUnwrapped(), 0, 0);
Jack Palevich21a15a22009-05-11 14:49:29 -07002174 tok = TOK_NUM;
Jack Paleviche27bf3e2009-05-10 14:09:03 -07002175 } else {
Jack Palevich569f1352009-06-29 14:29:08 -07002176 tok = mTokenTable.intern(mTokenString.getUnwrapped(),
2177 mTokenString.len());
Jack Palevich2d11dfb2009-06-08 14:34:26 -07002178 // Is this a macro?
Jack Palevich569f1352009-06-29 14:29:08 -07002179 char* pMacroDefinition = mTokenTable[tok].mpMacroDefinition;
2180 if(pMacroDefinition) {
Jack Palevich2d11dfb2009-06-08 14:34:26 -07002181 // Yes, it is a macro
Jack Palevich569f1352009-06-29 14:29:08 -07002182 dptr = pMacroDefinition;
Jack Palevich2d11dfb2009-06-08 14:34:26 -07002183 dch = ch;
2184 inp();
2185 next();
Jack Paleviche27bf3e2009-05-10 14:09:03 -07002186 }
2187 }
Jack Paleviche27bf3e2009-05-10 14:09:03 -07002188 } else {
Jack Palevich21a15a22009-05-11 14:49:29 -07002189 inp();
2190 if (tok == '\'') {
2191 tok = TOK_NUM;
Jack Palevichb4758ff2009-06-12 12:49:14 -07002192 tokc = getq();
2193 if (ch != '\'') {
2194 error("Expected a ' character, got %c", ch);
2195 } else {
2196 inp();
2197 }
Jack Palevich546b2242009-05-13 15:10:04 -07002198 } else if ((tok == '/') & (ch == '*')) {
Jack Palevich21a15a22009-05-11 14:49:29 -07002199 inp();
Jack Palevich22e3e8e2009-06-12 13:12:55 -07002200 while (ch && ch != EOF) {
2201 while (ch != '*' && ch != EOF)
Jack Palevich21a15a22009-05-11 14:49:29 -07002202 inp();
2203 inp();
2204 if (ch == '/')
2205 ch = 0;
Jack Paleviche27bf3e2009-05-10 14:09:03 -07002206 }
Jack Palevich22e3e8e2009-06-12 13:12:55 -07002207 if (ch == EOF) {
2208 error("End of file inside comment.");
2209 }
Jack Palevich21a15a22009-05-11 14:49:29 -07002210 inp();
Jack Paleviche27bf3e2009-05-10 14:09:03 -07002211 next();
Jack Palevichbd894902009-05-14 19:35:31 -07002212 } else if ((tok == '/') & (ch == '/')) {
2213 inp();
Jack Palevich22e3e8e2009-06-12 13:12:55 -07002214 while (ch && (ch != '\n') && (ch != EOF)) {
Jack Palevichbd894902009-05-14 19:35:31 -07002215 inp();
2216 }
2217 inp();
2218 next();
Jack Palevich21a15a22009-05-11 14:49:29 -07002219 } else {
Jack Palevichbf42c9c2009-05-12 12:48:35 -07002220 const char* t = operatorChars;
2221 int opIndex = 0;
Jack Palevich546b2242009-05-13 15:10:04 -07002222 while ((l = *t++) != 0) {
Jack Palevich21a15a22009-05-11 14:49:29 -07002223 a = *t++;
Jack Palevichbf42c9c2009-05-12 12:48:35 -07002224 tokl = operatorLevel[opIndex];
2225 tokc = opIndex;
Jack Palevich546b2242009-05-13 15:10:04 -07002226 if ((l == tok) & ((a == ch) | (a == '@'))) {
Jack Palevich21a15a22009-05-11 14:49:29 -07002227#if 0
2228 printf("%c%c -> tokl=%d tokc=0x%x\n",
2229 l, a, tokl, tokc);
2230#endif
2231 if (a == ch) {
2232 inp();
2233 tok = TOK_DUMMY; /* dummy token for double tokens */
2234 }
2235 break;
2236 }
Jack Palevichbf42c9c2009-05-12 12:48:35 -07002237 opIndex++;
2238 }
2239 if (l == 0) {
2240 tokl = 0;
2241 tokc = 0;
Jack Palevich21a15a22009-05-11 14:49:29 -07002242 }
2243 }
2244 }
2245#if 0
2246 {
Jack Palevich569f1352009-06-29 14:29:08 -07002247 String buf;
2248 decodeToken(buf, tok);
Jack Palevich86351982009-06-30 18:09:56 -07002249 fprintf(stderr, "%s\n", buf.getUnwrapped());
2250 }
Jack Palevich21a15a22009-05-11 14:49:29 -07002251#endif
2252 }
2253
Jack Palevich2d11dfb2009-06-08 14:34:26 -07002254 void doDefine() {
Jack Palevich569f1352009-06-29 14:29:08 -07002255 next();
2256 tokenid_t name = tok;
Jack Palevich2d11dfb2009-06-08 14:34:26 -07002257 String* pName = new String();
2258 while (isspace(ch)) {
2259 inp();
2260 }
Jack Palevich2d11dfb2009-06-08 14:34:26 -07002261 if (ch == '(') {
2262 delete pName;
2263 error("Defines with arguments not supported");
Jack Palevich0a280a02009-06-11 10:53:51 -07002264 return;
Jack Palevich2d11dfb2009-06-08 14:34:26 -07002265 }
2266 while (isspace(ch)) {
2267 inp();
2268 }
Jack Palevich569f1352009-06-29 14:29:08 -07002269 String value;
Jack Palevich2d11dfb2009-06-08 14:34:26 -07002270 while (ch != '\n' && ch != EOF) {
Jack Palevich569f1352009-06-29 14:29:08 -07002271 value.append(ch);
Jack Palevich2d11dfb2009-06-08 14:34:26 -07002272 inp();
2273 }
Jack Palevich569f1352009-06-29 14:29:08 -07002274 char* pDefn = (char*)mGlobalArena.alloc(value.len() + 1);
2275 memcpy(pDefn, value.getUnwrapped(), value.len());
2276 pDefn[value.len()] = 0;
2277 mTokenTable[name].mpMacroDefinition = pDefn;
Jack Palevich2d11dfb2009-06-08 14:34:26 -07002278 }
2279
Jack Palevicheedf9d22009-06-04 16:23:40 -07002280 void doPragma() {
2281 // # pragma name(val)
2282 int state = 0;
2283 while(ch != EOF && ch != '\n' && state < 10) {
2284 switch(state) {
2285 case 0:
2286 if (isspace(ch)) {
2287 inp();
2288 } else {
2289 state++;
2290 }
2291 break;
2292 case 1:
2293 if (isalnum(ch)) {
2294 mPragmas.append(ch);
2295 inp();
2296 } else if (ch == '(') {
2297 mPragmas.append(0);
2298 inp();
2299 state++;
2300 } else {
2301 state = 11;
2302 }
2303 break;
2304 case 2:
2305 if (isalnum(ch)) {
2306 mPragmas.append(ch);
2307 inp();
2308 } else if (ch == ')') {
2309 mPragmas.append(0);
2310 inp();
2311 state = 10;
2312 } else {
2313 state = 11;
2314 }
2315 break;
2316 }
2317 }
2318 if(state != 10) {
2319 error("Unexpected pragma syntax");
2320 }
2321 mPragmaStringCount += 2;
2322 }
Jack Palevich21a15a22009-05-11 14:49:29 -07002323
Jack Palevichac0e95e2009-05-29 13:53:44 -07002324 virtual void verror(const char* fmt, va_list ap) {
Jack Palevicheedf9d22009-06-04 16:23:40 -07002325 mErrorBuf.printf("%ld: ", file->getLine());
2326 mErrorBuf.vprintf(fmt, ap);
2327 mErrorBuf.printf("\n");
Jack Palevich21a15a22009-05-11 14:49:29 -07002328 }
2329
Jack Palevich8b0624c2009-05-20 12:12:06 -07002330 void skip(intptr_t c) {
Jack Palevich21a15a22009-05-11 14:49:29 -07002331 if (tok != c) {
2332 error("'%c' expected", c);
2333 }
2334 next();
2335 }
2336
Jack Palevich86351982009-06-30 18:09:56 -07002337 bool accept(intptr_t c) {
2338 if (tok == c) {
2339 next();
2340 return true;
2341 }
2342 return false;
2343 }
2344
Jack Palevich40600de2009-07-01 15:32:35 -07002345 bool acceptStringLiteral() {
2346 if (tok == '"') {
Jack Palevich653f42d2009-05-28 17:15:32 -07002347 pGen->li((int) glo);
Jack Palevich40600de2009-07-01 15:32:35 -07002348 // This while loop merges multiple adjacent string constants.
2349 while (tok == '"') {
2350 while (ch != '"' && ch != EOF) {
2351 *allocGlobalSpace(1) = getq();
2352 }
2353 if (ch != '"') {
2354 error("Unterminated string constant.");
2355 }
2356 inp();
2357 next();
Jack Palevichb4758ff2009-06-12 12:49:14 -07002358 }
Jack Palevich40600de2009-07-01 15:32:35 -07002359 /* Null terminate */
Jack Palevich653f42d2009-05-28 17:15:32 -07002360 *glo = 0;
Jack Palevichf1f39cc2009-05-29 18:03:15 -07002361 /* align heap */
2362 allocGlobalSpace((char*) (((intptr_t) glo + 4) & -4) - glo);
Jack Palevich40600de2009-07-01 15:32:35 -07002363
2364 return true;
2365 }
2366 return false;
2367 }
2368 /* Parse and evaluate a unary expression.
2369 * allowAssignment is true if '=' parsing wanted (quick hack)
2370 */
2371 void unary(bool allowAssignment) {
2372 intptr_t n, t, a;
2373 t = 0;
2374 n = 1; /* type of expression 0 = forward, 1 = value, other = lvalue */
2375 if (acceptStringLiteral()) {
2376 // Nothing else to do.
Jack Palevich21a15a22009-05-11 14:49:29 -07002377 } else {
Jack Palevich40600de2009-07-01 15:32:35 -07002378 int c = tokl;
Jack Palevich21a15a22009-05-11 14:49:29 -07002379 a = tokc;
2380 t = tok;
2381 next();
2382 if (t == TOK_NUM) {
Jack Palevichbf42c9c2009-05-12 12:48:35 -07002383 pGen->li(a);
Jack Palevich21a15a22009-05-11 14:49:29 -07002384 } else if (c == 2) {
2385 /* -, +, !, ~ */
Jack Palevich40600de2009-07-01 15:32:35 -07002386 unary(false);
Jack Palevich1cdef202009-05-22 12:06:27 -07002387 pGen->clearR1();
Jack Palevich21a15a22009-05-11 14:49:29 -07002388 if (t == '!')
Jack Palevichbf42c9c2009-05-12 12:48:35 -07002389 pGen->gcmp(a);
Jack Palevich21a15a22009-05-11 14:49:29 -07002390 else
Jack Palevichbf42c9c2009-05-12 12:48:35 -07002391 pGen->genOp(a);
Jack Palevich21a15a22009-05-11 14:49:29 -07002392 } else if (t == '(') {
2393 expr();
2394 skip(')');
2395 } else if (t == '*') {
2396 /* parse cast */
2397 skip('(');
2398 t = tok; /* get type */
2399 next(); /* skip int/char/void */
2400 next(); /* skip '*' or '(' */
2401 if (tok == '*') {
2402 /* function type */
2403 skip('*');
2404 skip(')');
2405 skip('(');
2406 skip(')');
2407 t = 0;
2408 }
2409 skip(')');
Jack Palevich40600de2009-07-01 15:32:35 -07002410 unary(false);
Jack Palevich21a15a22009-05-11 14:49:29 -07002411 if (tok == '=') {
2412 next();
Jack Palevich1cdef202009-05-22 12:06:27 -07002413 pGen->pushR0();
Jack Palevich21a15a22009-05-11 14:49:29 -07002414 expr();
Jack Palevich1cdef202009-05-22 12:06:27 -07002415 pGen->popR1();
2416 pGen->storeR0ToR1(t == TOK_INT);
Jack Palevich21a15a22009-05-11 14:49:29 -07002417 } else if (t) {
Jack Palevich1cdef202009-05-22 12:06:27 -07002418 pGen->loadR0FromR0(t == TOK_INT);
Jack Palevich21a15a22009-05-11 14:49:29 -07002419 }
2420 } else if (t == '&') {
Jack Palevich569f1352009-06-29 14:29:08 -07002421 pGen->leaR0((int) VI(tok)->pAddress);
Jack Palevich21a15a22009-05-11 14:49:29 -07002422 next();
Jack Palevich303d8ff2009-06-11 19:06:24 -07002423 } else if (t == EOF ) {
2424 error("Unexpected EOF.");
Jack Palevich40600de2009-07-01 15:32:35 -07002425 } else if (!checkSymbol(t)) {
Jack Palevicha1804dd2009-06-12 14:40:04 -07002426 // Don't have to do anything special here, the error
2427 // message was printed by checkSymbol() above.
Jack Palevich21a15a22009-05-11 14:49:29 -07002428 } else {
Jack Palevich569f1352009-06-29 14:29:08 -07002429 if (!isDefined(t)) {
2430 mGlobals.add(t);
2431 // printf("Adding new global function %s\n", nameof(t));
Jack Palevich303d8ff2009-06-11 19:06:24 -07002432 }
2433
Jack Palevich569f1352009-06-29 14:29:08 -07002434 n = (intptr_t) VI(t)->pAddress;
Jack Palevich21a15a22009-05-11 14:49:29 -07002435 /* forward reference: try dlsym */
Jack Palevichcb1c9ef2009-05-14 11:38:49 -07002436 if (!n) {
Jack Palevich40600de2009-07-01 15:32:35 -07002437 n = (intptr_t) dlsym(RTLD_DEFAULT, nameof(t));
Jack Palevich569f1352009-06-29 14:29:08 -07002438 VI(t)->pAddress = (void*) n;
Jack Palevichcb1c9ef2009-05-14 11:38:49 -07002439 }
Jack Palevich40600de2009-07-01 15:32:35 -07002440 if ((tok == '=') & allowAssignment) {
Jack Palevich21a15a22009-05-11 14:49:29 -07002441 /* assignment */
2442 next();
2443 expr();
Jack Palevich1cdef202009-05-22 12:06:27 -07002444 pGen->storeR0(n);
Jack Palevich21a15a22009-05-11 14:49:29 -07002445 } else if (tok != '(') {
2446 /* variable */
Jack Palevicha6baa232009-06-12 11:25:59 -07002447 if (!n) {
Jack Palevich40600de2009-07-01 15:32:35 -07002448 error("Undefined variable %s", nameof(t));
Jack Palevicha6baa232009-06-12 11:25:59 -07002449 }
Jack Palevich1cdef202009-05-22 12:06:27 -07002450 pGen->loadR0(n, tokl == 11, tokc);
Jack Palevich21a15a22009-05-11 14:49:29 -07002451 if (tokl == 11) {
Jack Palevich21a15a22009-05-11 14:49:29 -07002452 next();
2453 }
2454 }
2455 }
2456 }
2457
2458 /* function call */
2459 if (tok == '(') {
2460 if (n == 1)
Jack Palevich1cdef202009-05-22 12:06:27 -07002461 pGen->pushR0();
Jack Palevich21a15a22009-05-11 14:49:29 -07002462
2463 /* push args and invert order */
Jack Palevichcb1c9ef2009-05-14 11:38:49 -07002464 a = pGen->beginFunctionCallArguments();
Jack Palevich21a15a22009-05-11 14:49:29 -07002465 next();
Jack Palevich40600de2009-07-01 15:32:35 -07002466 int l = 0;
Jack Palevichb4758ff2009-06-12 12:49:14 -07002467 while (tok != ')' && tok != EOF) {
Jack Palevich21a15a22009-05-11 14:49:29 -07002468 expr();
Jack Palevich1cdef202009-05-22 12:06:27 -07002469 pGen->storeR0ToArg(l);
Jack Palevichbbf8ab52009-05-11 11:54:30 -07002470 if (tok == ',')
Jack Paleviche27bf3e2009-05-10 14:09:03 -07002471 next();
Jack Palevich21a15a22009-05-11 14:49:29 -07002472 l = l + 4;
Jack Paleviche27bf3e2009-05-10 14:09:03 -07002473 }
Jack Palevichcb1c9ef2009-05-14 11:38:49 -07002474 pGen->endFunctionCallArguments(a, l);
Jack Palevichb4758ff2009-06-12 12:49:14 -07002475 skip(')');
Jack Palevich21a15a22009-05-11 14:49:29 -07002476 if (!n) {
2477 /* forward reference */
Jack Palevich569f1352009-06-29 14:29:08 -07002478 VariableInfo* pVI = VI(t);
2479 pVI->pForward = (void*) pGen->callForward((int) pVI->pForward);
Jack Palevich21a15a22009-05-11 14:49:29 -07002480 } else if (n == 1) {
2481 pGen->callIndirect(l);
Jack Palevich21a15a22009-05-11 14:49:29 -07002482 } else {
Jack Palevich7810bc92009-05-15 14:31:47 -07002483 pGen->callRelative(n - codeBuf.getPC() - pGen->jumpOffset());
Jack Palevich21a15a22009-05-11 14:49:29 -07002484 }
-b master422972c2009-06-17 19:13:52 -07002485 pGen->adjustStackAfterCall(l, n == 1);
Jack Palevich21a15a22009-05-11 14:49:29 -07002486 }
2487 }
2488
Jack Palevich40600de2009-07-01 15:32:35 -07002489 /* Recursive descent parser for binary operations.
2490 */
2491 void binaryOp(int level) {
Jack Palevich8b0624c2009-05-20 12:12:06 -07002492 intptr_t t, n, a;
Jack Palevich546b2242009-05-13 15:10:04 -07002493 t = 0;
Jack Palevich40600de2009-07-01 15:32:35 -07002494 if (level-- == 1)
2495 unary(true);
Jack Palevich21a15a22009-05-11 14:49:29 -07002496 else {
Jack Palevich40600de2009-07-01 15:32:35 -07002497 binaryOp(level);
Jack Palevich21a15a22009-05-11 14:49:29 -07002498 a = 0;
Jack Palevich40600de2009-07-01 15:32:35 -07002499 while (level == tokl) {
Jack Palevich21a15a22009-05-11 14:49:29 -07002500 n = tok;
2501 t = tokc;
2502 next();
2503
Jack Palevich40600de2009-07-01 15:32:35 -07002504 if (level > 8) {
Jack Palevichbf42c9c2009-05-12 12:48:35 -07002505 a = pGen->gtst(t == OP_LOGICAL_OR, a); /* && and || output code generation */
Jack Palevich40600de2009-07-01 15:32:35 -07002506 binaryOp(level);
Jack Palevich21a15a22009-05-11 14:49:29 -07002507 } else {
Jack Palevich1cdef202009-05-22 12:06:27 -07002508 pGen->pushR0();
Jack Palevich40600de2009-07-01 15:32:35 -07002509 binaryOp(level);
Jack Palevich1cdef202009-05-22 12:06:27 -07002510 pGen->popR1();
Jack Palevich21a15a22009-05-11 14:49:29 -07002511
Jack Palevich40600de2009-07-01 15:32:35 -07002512 if ((level == 4) | (level == 5)) {
Jack Palevichbf42c9c2009-05-12 12:48:35 -07002513 pGen->gcmp(t);
Jack Palevich21a15a22009-05-11 14:49:29 -07002514 } else {
Jack Palevichbf42c9c2009-05-12 12:48:35 -07002515 pGen->genOp(t);
Jack Palevich21a15a22009-05-11 14:49:29 -07002516 }
2517 }
2518 }
2519 /* && and || output code generation */
Jack Palevich40600de2009-07-01 15:32:35 -07002520 if (a && level > 8) {
Jack Palevichbf42c9c2009-05-12 12:48:35 -07002521 a = pGen->gtst(t == OP_LOGICAL_OR, a);
2522 pGen->li(t != OP_LOGICAL_OR);
Jack Palevicha6535612009-05-13 16:24:17 -07002523 pGen->gjmp(5); /* jmp $ + 5 (sizeof li, FIXME for ARM) */
Jack Palevichbf42c9c2009-05-12 12:48:35 -07002524 pGen->gsym(a);
2525 pGen->li(t == OP_LOGICAL_OR);
Jack Palevich21a15a22009-05-11 14:49:29 -07002526 }
2527 }
2528 }
2529
2530 void expr() {
Jack Palevich40600de2009-07-01 15:32:35 -07002531 binaryOp(11);
Jack Palevich21a15a22009-05-11 14:49:29 -07002532 }
2533
2534 int test_expr() {
2535 expr();
Jack Palevichbf42c9c2009-05-12 12:48:35 -07002536 return pGen->gtst(0, 0);
Jack Palevich21a15a22009-05-11 14:49:29 -07002537 }
2538
Jack Palevicha6baa232009-06-12 11:25:59 -07002539 void block(intptr_t l, bool outermostFunctionBlock) {
Jack Palevich8b0624c2009-05-20 12:12:06 -07002540 intptr_t a, n, t;
Jack Palevich21a15a22009-05-11 14:49:29 -07002541
Jack Palevicha1804dd2009-06-12 14:40:04 -07002542 if (tok == TOK_INT || tok == TOK_CHAR) {
2543 /* declarations */
2544 localDeclarations();
2545 } else if (tok == TOK_IF) {
Jack Paleviche27bf3e2009-05-10 14:09:03 -07002546 next();
2547 skip('(');
Jack Palevich21a15a22009-05-11 14:49:29 -07002548 a = test_expr();
2549 skip(')');
Jack Palevicha6baa232009-06-12 11:25:59 -07002550 block(l, false);
Jack Palevich21a15a22009-05-11 14:49:29 -07002551 if (tok == TOK_ELSE) {
Jack Paleviche27bf3e2009-05-10 14:09:03 -07002552 next();
Jack Palevichbf42c9c2009-05-12 12:48:35 -07002553 n = pGen->gjmp(0); /* jmp */
2554 pGen->gsym(a);
Jack Palevicha6baa232009-06-12 11:25:59 -07002555 block(l, false);
Jack Palevichbf42c9c2009-05-12 12:48:35 -07002556 pGen->gsym(n); /* patch else jmp */
Jack Palevich21a15a22009-05-11 14:49:29 -07002557 } else {
Jack Palevichbf42c9c2009-05-12 12:48:35 -07002558 pGen->gsym(a); /* patch if test */
Jack Paleviche27bf3e2009-05-10 14:09:03 -07002559 }
Jack Palevich546b2242009-05-13 15:10:04 -07002560 } else if ((tok == TOK_WHILE) | (tok == TOK_FOR)) {
Jack Palevich21a15a22009-05-11 14:49:29 -07002561 t = tok;
2562 next();
2563 skip('(');
2564 if (t == TOK_WHILE) {
Jack Palevicha6535612009-05-13 16:24:17 -07002565 n = codeBuf.getPC(); // top of loop, target of "next" iteration
Jack Palevich21a15a22009-05-11 14:49:29 -07002566 a = test_expr();
2567 } else {
2568 if (tok != ';')
2569 expr();
2570 skip(';');
2571 n = codeBuf.getPC();
2572 a = 0;
2573 if (tok != ';')
2574 a = test_expr();
2575 skip(';');
2576 if (tok != ')') {
Jack Palevichbf42c9c2009-05-12 12:48:35 -07002577 t = pGen->gjmp(0);
Jack Palevich21a15a22009-05-11 14:49:29 -07002578 expr();
Jack Palevicha6535612009-05-13 16:24:17 -07002579 pGen->gjmp(n - codeBuf.getPC() - pGen->jumpOffset());
Jack Palevichbf42c9c2009-05-12 12:48:35 -07002580 pGen->gsym(t);
Jack Palevich21a15a22009-05-11 14:49:29 -07002581 n = t + 4;
2582 }
2583 }
2584 skip(')');
Jack Palevicha6baa232009-06-12 11:25:59 -07002585 block((intptr_t) &a, false);
Jack Palevicha6535612009-05-13 16:24:17 -07002586 pGen->gjmp(n - codeBuf.getPC() - pGen->jumpOffset()); /* jmp */
Jack Palevichbf42c9c2009-05-12 12:48:35 -07002587 pGen->gsym(a);
Jack Palevich21a15a22009-05-11 14:49:29 -07002588 } else if (tok == '{') {
Jack Palevicha6baa232009-06-12 11:25:59 -07002589 if (! outermostFunctionBlock) {
Jack Palevich569f1352009-06-29 14:29:08 -07002590 mLocals.pushLevel();
Jack Palevicha6baa232009-06-12 11:25:59 -07002591 }
Jack Palevich21a15a22009-05-11 14:49:29 -07002592 next();
Jack Palevich303d8ff2009-06-11 19:06:24 -07002593 while (tok != '}' && tok != EOF)
Jack Palevicha6baa232009-06-12 11:25:59 -07002594 block(l, false);
Jack Palevich303d8ff2009-06-11 19:06:24 -07002595 skip('}');
Jack Palevicha6baa232009-06-12 11:25:59 -07002596 if (! outermostFunctionBlock) {
Jack Palevich569f1352009-06-29 14:29:08 -07002597 mLocals.popLevel();
Jack Palevicha6baa232009-06-12 11:25:59 -07002598 }
Jack Palevich21a15a22009-05-11 14:49:29 -07002599 } else {
2600 if (tok == TOK_RETURN) {
2601 next();
2602 if (tok != ';')
2603 expr();
Jack Palevichbf42c9c2009-05-12 12:48:35 -07002604 rsym = pGen->gjmp(rsym); /* jmp */
Jack Palevich21a15a22009-05-11 14:49:29 -07002605 } else if (tok == TOK_BREAK) {
2606 next();
Jack Palevichbf42c9c2009-05-12 12:48:35 -07002607 *(int *) l = pGen->gjmp(*(int *) l);
Jack Palevich21a15a22009-05-11 14:49:29 -07002608 } else if (tok != ';')
2609 expr();
2610 skip(';');
Jack Paleviche27bf3e2009-05-10 14:09:03 -07002611 }
2612 }
Jack Palevich21a15a22009-05-11 14:49:29 -07002613
Jack Palevich86351982009-06-30 18:09:56 -07002614 enum TypeTag {
2615 TY_INT, TY_CHAR, TY_VOID, TY_POINTER, TY_FUNC, TY_PARAM
2616 };
Jack Palevich21a15a22009-05-11 14:49:29 -07002617
Jack Palevich86351982009-06-30 18:09:56 -07002618 struct Type {
2619 TypeTag tag;
2620 tokenid_t id; // For function arguments
2621 Type* pHead;
2622 Type* pTail;
2623 };
2624
2625 Type* createType(TypeTag tag, Type* pHead, Type* pTail, Arena& arena) {
2626 assert(tag >= TY_INT && tag <= TY_PARAM);
2627 Type* pType = (Type*) arena.alloc(sizeof(Type));
2628 memset(pType, 0, sizeof(*pType));
2629 pType->tag = tag;
2630 pType->pHead = pHead;
2631 pType->pTail = pTail;
2632 return pType;
Jack Palevichb7c81e92009-06-04 19:56:13 -07002633 }
2634
Jack Palevich86351982009-06-30 18:09:56 -07002635 void decodeType(String& buffer, Type* pType) {
2636 if (pType == NULL) {
2637 buffer.appendCStr("null");
2638 return;
2639 }
2640 buffer.append('(');
2641 String temp;
2642 if (pType->id != 0) {
2643 decodeToken(temp, pType->id);
2644 buffer.append(temp);
2645 buffer.append(' ');
2646 }
2647 bool printHead = false;
2648 bool printTail = false;
2649 switch (pType->tag) {
2650 case TY_INT:
2651 buffer.appendCStr("int");
2652 break;
2653 case TY_CHAR:
2654 buffer.appendCStr("char");
2655 break;
2656 case TY_VOID:
2657 buffer.appendCStr("void");
2658 break;
2659 case TY_POINTER:
2660 buffer.appendCStr("*");
2661 printHead = true;
2662 break;
2663 case TY_FUNC:
2664 buffer.appendCStr("func");
2665 printHead = true;
2666 printTail = true;
2667 break;
2668 case TY_PARAM:
2669 buffer.appendCStr("param");
2670 printHead = true;
2671 printTail = true;
2672 break;
2673 default:
2674 String temp;
2675 temp.printf("Unknown tag %d", pType->tag);
2676 buffer.append(temp);
2677 break;
2678 }
2679 if (printHead) {
2680 buffer.append(' ');
2681 decodeType(buffer, pType->pHead);
2682 }
2683 if (printTail) {
2684 buffer.append(' ');
2685 decodeType(buffer, pType->pTail);
2686 }
2687 buffer.append(')');
Jack Palevichb7c81e92009-06-04 19:56:13 -07002688 }
2689
Jack Palevich86351982009-06-30 18:09:56 -07002690 void printType(Type* pType) {
2691 String buffer;
2692 decodeType(buffer, pType);
2693 fprintf(stderr, "%s\n", buffer.getUnwrapped());
Jack Palevichb7c81e92009-06-04 19:56:13 -07002694 }
2695
Jack Palevich86351982009-06-30 18:09:56 -07002696 Type* acceptPrimitiveType(Arena& arena) {
2697 Type* pType;
Jack Palevichb7c81e92009-06-04 19:56:13 -07002698 if (tok == TOK_INT) {
Jack Palevich86351982009-06-30 18:09:56 -07002699 pType = mkpInt;
Jack Palevichb7c81e92009-06-04 19:56:13 -07002700 } else if (tok == TOK_CHAR) {
Jack Palevich86351982009-06-30 18:09:56 -07002701 pType = mkpChar;
Jack Palevichb7c81e92009-06-04 19:56:13 -07002702 } else if (tok == TOK_VOID) {
Jack Palevich86351982009-06-30 18:09:56 -07002703 pType = mkpVoid;
Jack Palevichb7c81e92009-06-04 19:56:13 -07002704 } else {
Jack Palevich86351982009-06-30 18:09:56 -07002705 return NULL;
Jack Palevichb7c81e92009-06-04 19:56:13 -07002706 }
2707 next();
Jack Palevich86351982009-06-30 18:09:56 -07002708 return pType;
Jack Palevichb7c81e92009-06-04 19:56:13 -07002709 }
2710
Jack Palevich86351982009-06-30 18:09:56 -07002711 Type* acceptDeclaration(const Type* pBaseType, Arena& arena) {
2712 Type* pType = createType(pBaseType->tag, pBaseType->pHead,
2713 pBaseType->pTail, arena);
2714 tokenid_t declName;
2715 if (pType) {
2716 pType = acceptDecl2(pType, declName, arena);
2717 pType->id = declName;
2718 // fprintf(stderr, "Parsed a declaration: ");
2719 // printType(pType);
2720 }
2721 return pType;
2722 }
2723
2724 Type* expectDeclaration(const Type* pBaseType, Arena& arena) {
2725 Type* pType = acceptDeclaration(pBaseType, arena);
2726 if (! pType) {
2727 error("Expected a declaration");
2728 }
2729 return pType;
2730 }
2731
2732 Type* acceptDecl2(Type* pType, tokenid_t& declName, Arena& arena) {
2733 while (tok == '*') {
2734 pType = createType(TY_POINTER, pType, NULL, arena);
Jack Palevichb7c81e92009-06-04 19:56:13 -07002735 next();
Jack Palevichb7c81e92009-06-04 19:56:13 -07002736 }
Jack Palevich86351982009-06-30 18:09:56 -07002737 pType = acceptDecl3(pType, declName, arena);
2738 return pType;
Jack Palevichb7c81e92009-06-04 19:56:13 -07002739 }
2740
Jack Palevich86351982009-06-30 18:09:56 -07002741 Type* acceptDecl3(Type* pType, tokenid_t& declName, Arena& arena) {
2742 if (accept('(')) {
2743 pType = acceptDecl2(pType, declName, arena);
2744 skip(')');
2745 } else {
2746 declName = acceptSymbol();
2747 }
2748 while (tok == '(') {
2749 // Function declaration
2750 skip('(');
2751 Type* pTail = acceptArgs(arena);
2752 pType = createType(TY_FUNC, pType, pTail, arena);
2753 skip(')');
2754 }
2755 return pType;
2756 }
2757
2758 Type* acceptArgs(Arena& arena) {
2759 Type* pHead = NULL;
2760 Type* pTail = NULL;
2761 for(;;) {
2762 Type* pBaseArg = acceptPrimitiveType(arena);
2763 if (pBaseArg) {
2764 Type* pArg = acceptDeclaration(pBaseArg, arena);
2765 if (pArg) {
2766 Type* pParam = createType(TY_PARAM, pArg, NULL, arena);
2767 if (!pHead) {
2768 pHead = pParam;
2769 pTail = pParam;
2770 } else {
2771 pTail->pTail = pParam;
2772 pTail = pParam;
2773 }
2774 }
2775 }
2776 if (! accept(',')) {
2777 break;
2778 }
2779 }
2780 return pHead;
2781 }
2782
2783 Type* expectPrimitiveType(Arena& arena) {
2784 Type* pType = acceptPrimitiveType(arena);
2785 if (!pType) {
Jack Palevich569f1352009-06-29 14:29:08 -07002786 String buf;
2787 decodeToken(buf, tok);
2788 error("Expected a type, got %s", buf.getUnwrapped());
Jack Palevichb7c81e92009-06-04 19:56:13 -07002789 }
Jack Palevich86351982009-06-30 18:09:56 -07002790 return pType;
Jack Palevichb7c81e92009-06-04 19:56:13 -07002791 }
2792
Jack Palevich86351982009-06-30 18:09:56 -07002793 void addGlobalSymbol(Type* pDecl) {
2794 tokenid_t t = pDecl->id;
2795 VariableInfo* pVI = VI(t);
Jack Palevich569f1352009-06-29 14:29:08 -07002796 if(pVI && pVI->pAddress) {
Jack Palevich86351982009-06-30 18:09:56 -07002797 reportDuplicate(t);
Jack Palevich569f1352009-06-29 14:29:08 -07002798 }
Jack Palevich86351982009-06-30 18:09:56 -07002799 mGlobals.add(pDecl);
Jack Palevicha6baa232009-06-12 11:25:59 -07002800 }
2801
Jack Palevich86351982009-06-30 18:09:56 -07002802 void reportDuplicate(tokenid_t t) {
2803 error("Duplicate definition of %s", nameof(t));
Jack Palevich303d8ff2009-06-11 19:06:24 -07002804 }
2805
Jack Palevich86351982009-06-30 18:09:56 -07002806 void addLocalSymbol(Type* pDecl) {
2807 tokenid_t t = pDecl->id;
2808 if (mLocals.isDefinedAtCurrentLevel(t)) {
2809 reportDuplicate(t);
Jack Palevich569f1352009-06-29 14:29:08 -07002810 }
Jack Palevich86351982009-06-30 18:09:56 -07002811 mLocals.add(pDecl);
Jack Palevich303d8ff2009-06-11 19:06:24 -07002812 }
2813
Jack Palevichb7c81e92009-06-04 19:56:13 -07002814 void localDeclarations() {
2815 intptr_t a;
Jack Palevich86351982009-06-30 18:09:56 -07002816 Type* pBaseType;
Jack Palevichb7c81e92009-06-04 19:56:13 -07002817
Jack Palevich86351982009-06-30 18:09:56 -07002818 while ((pBaseType = acceptPrimitiveType(mLocalArena)) != NULL) {
Jack Palevich22e3e8e2009-06-12 13:12:55 -07002819 while (tok != ';' && tok != EOF) {
Jack Palevich86351982009-06-30 18:09:56 -07002820 Type* pDecl = expectDeclaration(pBaseType, mLocalArena);
2821 if (!pDecl) {
2822 break;
Jack Palevicha6baa232009-06-12 11:25:59 -07002823 }
Jack Palevich86351982009-06-30 18:09:56 -07002824 int variableAddress = 0;
2825 addLocalSymbol(pDecl);
2826 loc = loc + 4;
2827 variableAddress = -loc;
2828 VI(pDecl->id)->pAddress = (void*) variableAddress;
2829 if (accept('=')) {
Jack Palevichd7461a72009-06-12 14:26:58 -07002830 /* assignment */
Jack Palevichd7461a72009-06-12 14:26:58 -07002831 expr();
2832 pGen->storeR0(variableAddress);
2833 }
Jack Palevichb7c81e92009-06-04 19:56:13 -07002834 if (tok == ',')
2835 next();
2836 }
2837 skip(';');
2838 }
2839 }
2840
Jack Palevichf1728be2009-06-12 13:53:51 -07002841 bool checkSymbol() {
Jack Palevich40600de2009-07-01 15:32:35 -07002842 return checkSymbol(tok);
Jack Palevicha1804dd2009-06-12 14:40:04 -07002843 }
2844
Jack Palevich569f1352009-06-29 14:29:08 -07002845 void decodeToken(String& buffer, tokenid_t token) {
2846 if (token == EOF ) {
2847 buffer.printf("EOF");
2848 } else if (token == TOK_NUM) {
2849 buffer.printf("numeric constant");
2850 } else if (token >= 0 && token < 256) {
Jack Palevich86351982009-06-30 18:09:56 -07002851 if (token < 32) {
2852 buffer.printf("'\\x%02x'", token);
2853 } else {
2854 buffer.printf("'%c'", token);
2855 }
Jack Palevich569f1352009-06-29 14:29:08 -07002856 } else if (token >= TOK_KEYWORD && token < TOK_SYMBOL) {
2857 buffer.printf("keyword \"%s\"", nameof(token));
2858 } else {
2859 buffer.printf("symbol \"%s\"", nameof(token));
2860 }
2861 }
2862
Jack Palevich40600de2009-07-01 15:32:35 -07002863 bool checkSymbol(tokenid_t token) {
Jack Palevich569f1352009-06-29 14:29:08 -07002864 bool result = token >= TOK_SYMBOL;
Jack Palevichf1728be2009-06-12 13:53:51 -07002865 if (!result) {
2866 String temp;
Jack Palevich569f1352009-06-29 14:29:08 -07002867 decodeToken(temp, token);
Jack Palevichf1728be2009-06-12 13:53:51 -07002868 error("Expected symbol. Got %s", temp.getUnwrapped());
2869 }
2870 return result;
2871 }
2872
Jack Palevich86351982009-06-30 18:09:56 -07002873 tokenid_t acceptSymbol() {
2874 tokenid_t result = 0;
2875 if (tok >= TOK_SYMBOL) {
2876 result = tok;
2877 next();
2878 } else {
2879 String temp;
2880 decodeToken(temp, tok);
2881 error("Expected symbol. Got %s", temp.getUnwrapped());
2882 }
2883 return result;
2884 }
2885
Jack Palevichb7c81e92009-06-04 19:56:13 -07002886 void globalDeclarations() {
2887 while (tok != EOF) {
Jack Palevich86351982009-06-30 18:09:56 -07002888 Type* pBaseType = expectPrimitiveType(mGlobalArena);
2889 if (!pBaseType) {
Jack Palevichf1728be2009-06-12 13:53:51 -07002890 break;
2891 }
Jack Palevich86351982009-06-30 18:09:56 -07002892 Type* pDecl = expectDeclaration(pBaseType, mGlobalArena);
2893 if (!pDecl) {
2894 break;
Jack Palevicha6baa232009-06-12 11:25:59 -07002895 }
Jack Palevich86351982009-06-30 18:09:56 -07002896 if (! isDefined(pDecl->id)) {
2897 addGlobalSymbol(pDecl);
2898 }
2899 VariableInfo* name = VI(pDecl->id);
Jack Palevicha6baa232009-06-12 11:25:59 -07002900 if (name && name->pAddress) {
Jack Palevich86351982009-06-30 18:09:56 -07002901 error("Already defined global %s", nameof(pDecl->id));
Jack Palevicha6baa232009-06-12 11:25:59 -07002902 }
Jack Palevich86351982009-06-30 18:09:56 -07002903 if (pDecl->tag < TY_FUNC) {
Jack Palevichb7c81e92009-06-04 19:56:13 -07002904 // it's a variable declaration
2905 for(;;) {
Jack Palevich86351982009-06-30 18:09:56 -07002906 if (name && !name->pAddress) {
Jack Palevicha6baa232009-06-12 11:25:59 -07002907 name->pAddress = (int*) allocGlobalSpace(4);
2908 }
Jack Palevich86351982009-06-30 18:09:56 -07002909 if (accept('=')) {
Jack Palevichd7461a72009-06-12 14:26:58 -07002910 if (tok == TOK_NUM) {
2911 if (name) {
2912 * (int*) name->pAddress = tokc;
2913 }
2914 next();
2915 } else {
2916 error("Expected an integer constant");
2917 }
2918 }
Jack Palevich86351982009-06-30 18:09:56 -07002919 if (!accept(',')) {
Jack Palevichb7c81e92009-06-04 19:56:13 -07002920 break;
Jack Palevich21a15a22009-05-11 14:49:29 -07002921 }
Jack Palevich86351982009-06-30 18:09:56 -07002922 pDecl = expectDeclaration(pBaseType, mGlobalArena);
2923 if (!pDecl) {
2924 break;
2925 }
2926 if (! isDefined(pDecl->id)) {
2927 addGlobalSymbol(pDecl);
2928 }
2929 name = VI(pDecl->id);
Jack Palevich21a15a22009-05-11 14:49:29 -07002930 }
2931 skip(';');
2932 } else {
Jack Palevich86351982009-06-30 18:09:56 -07002933 // Function declaration
Jack Palevicha6baa232009-06-12 11:25:59 -07002934 if (name) {
2935 /* patch forward references (XXX: does not work for function
2936 pointers) */
2937 pGen->gsym((int) name->pForward);
2938 /* put function address */
2939 name->pAddress = (void*) codeBuf.getPC();
2940 }
Jack Palevich86351982009-06-30 18:09:56 -07002941 // Calculate stack offsets for parameters
Jack Palevich569f1352009-06-29 14:29:08 -07002942 mLocals.pushLevel();
Jack Palevichb7c81e92009-06-04 19:56:13 -07002943 intptr_t a = 8;
Jack Palevich546b2242009-05-13 15:10:04 -07002944 int argCount = 0;
Jack Palevich86351982009-06-30 18:09:56 -07002945 for (Type* pP = pDecl->pTail; pP; pP = pP->pTail) {
2946 Type* pArg = pP->pHead;
2947 addLocalSymbol(pArg);
2948 /* read param name and compute offset */
2949 VI(pArg->id)->pAddress = (void*) a;
2950 a = a + 4;
Jack Palevich546b2242009-05-13 15:10:04 -07002951 argCount++;
Jack Palevich21a15a22009-05-11 14:49:29 -07002952 }
Jack Palevich21a15a22009-05-11 14:49:29 -07002953 rsym = loc = 0;
Jack Palevich546b2242009-05-13 15:10:04 -07002954 a = pGen->functionEntry(argCount);
Jack Palevicha6baa232009-06-12 11:25:59 -07002955 block(0, true);
Jack Palevichbf42c9c2009-05-12 12:48:35 -07002956 pGen->gsym(rsym);
Jack Palevich546b2242009-05-13 15:10:04 -07002957 pGen->functionExit(argCount, a, loc);
Jack Palevich569f1352009-06-29 14:29:08 -07002958 mLocals.popLevel();
Jack Palevich21a15a22009-05-11 14:49:29 -07002959 }
2960 }
2961 }
2962
Jack Palevichf1f39cc2009-05-29 18:03:15 -07002963 char* allocGlobalSpace(int bytes) {
2964 if (glo - pGlobalBase + bytes > ALLOC_SIZE) {
2965 error("Global space exhausted");
Jack Palevich0a280a02009-06-11 10:53:51 -07002966 return NULL;
Jack Palevichf1f39cc2009-05-29 18:03:15 -07002967 }
2968 char* result = glo;
2969 glo += bytes;
2970 return result;
2971 }
2972
Jack Palevich21a15a22009-05-11 14:49:29 -07002973 void cleanup() {
Jack Palevich21a15a22009-05-11 14:49:29 -07002974 if (pGlobalBase != 0) {
Jack Palevichf1f39cc2009-05-29 18:03:15 -07002975 free(pGlobalBase);
Jack Palevich21a15a22009-05-11 14:49:29 -07002976 pGlobalBase = 0;
2977 }
Jack Palevich21a15a22009-05-11 14:49:29 -07002978 if (pGen) {
2979 delete pGen;
2980 pGen = 0;
2981 }
Jack Palevich1cdef202009-05-22 12:06:27 -07002982 if (file) {
2983 delete file;
2984 file = 0;
2985 }
Jack Palevich21a15a22009-05-11 14:49:29 -07002986 }
2987
2988 void clear() {
2989 tok = 0;
2990 tokc = 0;
2991 tokl = 0;
2992 ch = 0;
Jack Palevich21a15a22009-05-11 14:49:29 -07002993 rsym = 0;
2994 loc = 0;
2995 glo = 0;
Jack Palevich21a15a22009-05-11 14:49:29 -07002996 dptr = 0;
2997 dch = 0;
Jack Palevich21a15a22009-05-11 14:49:29 -07002998 file = 0;
2999 pGlobalBase = 0;
Jack Palevich21a15a22009-05-11 14:49:29 -07003000 pGen = 0;
Jack Palevicheedf9d22009-06-04 16:23:40 -07003001 mPragmaStringCount = 0;
Jack Palevich21a15a22009-05-11 14:49:29 -07003002 }
Jack Paleviche27bf3e2009-05-10 14:09:03 -07003003
Jack Palevich22305132009-05-13 10:58:45 -07003004 void setArchitecture(const char* architecture) {
3005 delete pGen;
3006 pGen = 0;
3007
3008 if (architecture != NULL) {
Jack Paleviche7b59062009-05-19 17:12:17 -07003009#ifdef PROVIDE_ARM_CODEGEN
Jack Palevich8b0624c2009-05-20 12:12:06 -07003010 if (! pGen && strcmp(architecture, "arm") == 0) {
Jack Palevich22305132009-05-13 10:58:45 -07003011 pGen = new ARMCodeGenerator();
Jack Palevich8b0624c2009-05-20 12:12:06 -07003012 }
Jack Paleviche7b59062009-05-19 17:12:17 -07003013#endif
Jack Paleviche7b59062009-05-19 17:12:17 -07003014#ifdef PROVIDE_X86_CODEGEN
Jack Palevich8b0624c2009-05-20 12:12:06 -07003015 if (! pGen && strcmp(architecture, "x86") == 0) {
Jack Palevich22305132009-05-13 10:58:45 -07003016 pGen = new X86CodeGenerator();
Jack Palevich8b0624c2009-05-20 12:12:06 -07003017 }
Jack Paleviche7b59062009-05-19 17:12:17 -07003018#endif
Jack Palevich8b0624c2009-05-20 12:12:06 -07003019 if (!pGen ) {
Jack Palevichac0e95e2009-05-29 13:53:44 -07003020 error("Unknown architecture %s\n", architecture);
Jack Palevich22305132009-05-13 10:58:45 -07003021 }
3022 }
3023
3024 if (pGen == NULL) {
Jack Paleviche7b59062009-05-19 17:12:17 -07003025#if defined(DEFAULT_ARM_CODEGEN)
Jack Palevich22305132009-05-13 10:58:45 -07003026 pGen = new ARMCodeGenerator();
Jack Paleviche7b59062009-05-19 17:12:17 -07003027#elif defined(DEFAULT_X86_CODEGEN)
3028 pGen = new X86CodeGenerator();
3029#endif
3030 }
3031 if (pGen == NULL) {
Jack Palevichac0e95e2009-05-29 13:53:44 -07003032 error("No code generator defined.");
Jack Palevich0a280a02009-06-11 10:53:51 -07003033 } else {
3034 pGen->setErrorSink(this);
Jack Palevich22305132009-05-13 10:58:45 -07003035 }
3036 }
3037
Jack Palevich77ae76e2009-05-10 19:59:24 -07003038public:
Jack Palevich22305132009-05-13 10:58:45 -07003039 struct args {
3040 args() {
3041 architecture = 0;
3042 }
3043 const char* architecture;
3044 };
3045
Jack Paleviche7b59062009-05-19 17:12:17 -07003046 Compiler() {
Jack Palevich21a15a22009-05-11 14:49:29 -07003047 clear();
Jack Paleviche27bf3e2009-05-10 14:09:03 -07003048 }
Jack Palevichbbf8ab52009-05-11 11:54:30 -07003049
Jack Paleviche7b59062009-05-19 17:12:17 -07003050 ~Compiler() {
Jack Palevich21a15a22009-05-11 14:49:29 -07003051 cleanup();
3052 }
3053
Jack Palevich1cdef202009-05-22 12:06:27 -07003054 int compile(const char* text, size_t textLength) {
Jack Palevichac0e95e2009-05-29 13:53:44 -07003055 int result;
Jack Palevich0a280a02009-06-11 10:53:51 -07003056
3057 cleanup();
3058 clear();
Jack Palevich569f1352009-06-29 14:29:08 -07003059 mTokenTable.setArena(&mGlobalArena);
3060 mGlobals.setArena(&mGlobalArena);
3061 mGlobals.setTokenTable(&mTokenTable);
3062 mLocals.setArena(&mLocalArena);
3063 mLocals.setTokenTable(&mTokenTable);
3064
3065 internKeywords();
Jack Palevich86351982009-06-30 18:09:56 -07003066 createPrimitiveTypes();
Jack Palevich0a280a02009-06-11 10:53:51 -07003067 codeBuf.init(ALLOC_SIZE);
3068 setArchitecture(NULL);
3069 if (!pGen) {
3070 return -1;
3071 }
Jack Palevichb67b18f2009-06-11 21:12:23 -07003072#ifdef PROVIDE_TRACE_CODEGEN
3073 pGen = new TraceCodeGenerator(pGen);
3074#endif
3075 pGen->setErrorSink(this);
Jack Palevich0a280a02009-06-11 10:53:51 -07003076 pGen->init(&codeBuf);
3077 file = new TextInputStream(text, textLength);
Jack Palevich0a280a02009-06-11 10:53:51 -07003078 pGlobalBase = (char*) calloc(1, ALLOC_SIZE);
3079 glo = pGlobalBase;
Jack Palevich0a280a02009-06-11 10:53:51 -07003080 inp();
3081 next();
3082 globalDeclarations();
Jack Palevicha6baa232009-06-12 11:25:59 -07003083 checkForUndefinedForwardReferences();
Jack Palevich0a280a02009-06-11 10:53:51 -07003084 result = pGen->finishCompile();
3085 if (result == 0) {
3086 if (mErrorBuf.len()) {
3087 result = -2;
Jack Palevichac0e95e2009-05-29 13:53:44 -07003088 }
Jack Palevich8b0624c2009-05-20 12:12:06 -07003089 }
Jack Palevichac0e95e2009-05-29 13:53:44 -07003090 return result;
Jack Palevich21a15a22009-05-11 14:49:29 -07003091 }
3092
Jack Palevich86351982009-06-30 18:09:56 -07003093 void createPrimitiveTypes() {
3094 mkpInt = createType(TY_INT, NULL, NULL, mGlobalArena);
3095 mkpChar = createType(TY_CHAR, NULL, NULL, mGlobalArena);
3096 mkpVoid = createType(TY_VOID, NULL, NULL, mGlobalArena);
3097 }
3098
Jack Palevicha6baa232009-06-12 11:25:59 -07003099 void checkForUndefinedForwardReferences() {
Jack Palevich569f1352009-06-29 14:29:08 -07003100 mGlobals.forEach(static_ufrcFn, this);
Jack Palevicha6baa232009-06-12 11:25:59 -07003101 }
3102
Jack Palevich569f1352009-06-29 14:29:08 -07003103 static bool static_ufrcFn(VariableInfo* value, void* context) {
Jack Palevicha6baa232009-06-12 11:25:59 -07003104 Compiler* pCompiler = (Compiler*) context;
Jack Palevich569f1352009-06-29 14:29:08 -07003105 return pCompiler->undefinedForwardReferenceCheck(value);
Jack Palevicha6baa232009-06-12 11:25:59 -07003106 }
3107
Jack Palevich569f1352009-06-29 14:29:08 -07003108 bool undefinedForwardReferenceCheck(VariableInfo* value) {
Jack Palevicha6baa232009-06-12 11:25:59 -07003109 if (!value->pAddress && value->pForward) {
Jack Palevich569f1352009-06-29 14:29:08 -07003110 error("Undefined forward reference: %s",
3111 mTokenTable[value->tok].pText);
Jack Palevicha6baa232009-06-12 11:25:59 -07003112 }
3113 return true;
3114 }
3115
Jack Palevich21a15a22009-05-11 14:49:29 -07003116 int dump(FILE* out) {
3117 fwrite(codeBuf.getBase(), 1, codeBuf.getSize(), out);
3118 return 0;
3119 }
Jack Palevich77ae76e2009-05-10 19:59:24 -07003120
Jack Palevicha6535612009-05-13 16:24:17 -07003121 int disassemble(FILE* out) {
3122 return pGen->disassemble(out);
3123 }
3124
Jack Palevich1cdef202009-05-22 12:06:27 -07003125 /* Look through the symbol table to find a symbol.
3126 * If found, return its value.
3127 */
3128 void* lookup(const char* name) {
Jack Palevich569f1352009-06-29 14:29:08 -07003129 tokenid_t tok = mTokenTable.intern(name, strlen(name));
3130 VariableInfo* pVariableInfo = VI(tok);
Jack Palevich303d8ff2009-06-11 19:06:24 -07003131 if (pVariableInfo) {
3132 return pVariableInfo->pAddress;
Jack Palevich1cdef202009-05-22 12:06:27 -07003133 }
3134 return NULL;
3135 }
3136
Jack Palevicheedf9d22009-06-04 16:23:40 -07003137 void getPragmas(ACCsizei* actualStringCount,
3138 ACCsizei maxStringCount, ACCchar** strings) {
3139 int stringCount = mPragmaStringCount;
3140 if (actualStringCount) {
3141 *actualStringCount = stringCount;
3142 }
3143 if (stringCount > maxStringCount) {
3144 stringCount = maxStringCount;
3145 }
3146 if (strings) {
3147 char* pPragmas = mPragmas.getUnwrapped();
3148 while (stringCount-- > 0) {
3149 *strings++ = pPragmas;
3150 pPragmas += strlen(pPragmas) + 1;
3151 }
3152 }
3153 }
3154
Jack Palevichac0e95e2009-05-29 13:53:44 -07003155 char* getErrorMessage() {
Jack Palevicheedf9d22009-06-04 16:23:40 -07003156 return mErrorBuf.getUnwrapped();
Jack Palevichac0e95e2009-05-29 13:53:44 -07003157 }
3158
Jack Palevich77ae76e2009-05-10 19:59:24 -07003159};
3160
Jack Paleviche7b59062009-05-19 17:12:17 -07003161const char* Compiler::operatorChars =
Jack Palevichbf42c9c2009-05-12 12:48:35 -07003162 "++--*@/@%@+@-@<<>><=>=<@>@==!=&&||&@^@|@~@!@";
3163
Jack Paleviche7b59062009-05-19 17:12:17 -07003164const char Compiler::operatorLevel[] =
Jack Palevichbf42c9c2009-05-12 12:48:35 -07003165 {11, 11, 1, 1, 1, 2, 2, 3, 3, 4, 4, 4, 4,
3166 5, 5, /* ==, != */
3167 9, 10, /* &&, || */
3168 6, 7, 8, /* & ^ | */
3169 2, 2 /* ~ ! */
3170 };
3171
Jack Palevich8b0624c2009-05-20 12:12:06 -07003172#ifdef PROVIDE_ARM_CODEGEN
Jack Paleviche7b59062009-05-19 17:12:17 -07003173FILE* Compiler::ARMCodeGenerator::disasmOut;
Jack Palevich8b0624c2009-05-20 12:12:06 -07003174#endif
Jack Palevicha6535612009-05-13 16:24:17 -07003175
Jack Palevich8b0624c2009-05-20 12:12:06 -07003176#ifdef PROVIDE_X86_CODEGEN
Jack Paleviche7b59062009-05-19 17:12:17 -07003177const int Compiler::X86CodeGenerator::operatorHelper[] = {
Jack Palevichbf42c9c2009-05-12 12:48:35 -07003178 0x1, // ++
3179 0xff, // --
3180 0xc1af0f, // *
3181 0xf9f79991, // /
3182 0xf9f79991, // % (With manual assist to swap results)
3183 0xc801, // +
3184 0xd8f7c829, // -
3185 0xe0d391, // <<
3186 0xf8d391, // >>
3187 0xe, // <=
3188 0xd, // >=
3189 0xc, // <
3190 0xf, // >
3191 0x4, // ==
3192 0x5, // !=
3193 0x0, // &&
3194 0x1, // ||
3195 0xc821, // &
3196 0xc831, // ^
3197 0xc809, // |
3198 0xd0f7, // ~
3199 0x4 // !
3200};
Jack Palevich8b0624c2009-05-20 12:12:06 -07003201#endif
Jack Palevichbf42c9c2009-05-12 12:48:35 -07003202
Jack Palevich1cdef202009-05-22 12:06:27 -07003203struct ACCscript {
3204 ACCscript() {
3205 text = 0;
3206 textLength = 0;
3207 accError = ACC_NO_ERROR;
3208 }
Jack Palevichbbf8ab52009-05-11 11:54:30 -07003209
Jack Palevich1cdef202009-05-22 12:06:27 -07003210 ~ACCscript() {
3211 delete text;
3212 }
Jack Palevich546b2242009-05-13 15:10:04 -07003213
Jack Palevich1cdef202009-05-22 12:06:27 -07003214 void setError(ACCenum error) {
3215 if (accError == ACC_NO_ERROR && error != ACC_NO_ERROR) {
3216 accError = error;
Jack Palevichbbf8ab52009-05-11 11:54:30 -07003217 }
3218 }
3219
Jack Palevich1cdef202009-05-22 12:06:27 -07003220 ACCenum getError() {
3221 ACCenum result = accError;
3222 accError = ACC_NO_ERROR;
Jack Palevich22305132009-05-13 10:58:45 -07003223 return result;
Jack Palevichbbf8ab52009-05-11 11:54:30 -07003224 }
3225
Jack Palevich1cdef202009-05-22 12:06:27 -07003226 Compiler compiler;
3227 char* text;
3228 int textLength;
3229 ACCenum accError;
3230};
3231
3232
3233extern "C"
3234ACCscript* accCreateScript() {
3235 return new ACCscript();
Jack Palevichbbf8ab52009-05-11 11:54:30 -07003236}
Jack Palevich1cdef202009-05-22 12:06:27 -07003237
3238extern "C"
3239ACCenum accGetError( ACCscript* script ) {
3240 return script->getError();
3241}
3242
3243extern "C"
3244void accDeleteScript(ACCscript* script) {
3245 delete script;
3246}
3247
3248extern "C"
3249void accScriptSource(ACCscript* script,
3250 ACCsizei count,
3251 const ACCchar ** string,
3252 const ACCint * length) {
3253 int totalLength = 0;
3254 for(int i = 0; i < count; i++) {
3255 int len = -1;
3256 const ACCchar* s = string[i];
3257 if (length) {
3258 len = length[i];
3259 }
3260 if (len < 0) {
3261 len = strlen(s);
3262 }
3263 totalLength += len;
3264 }
3265 delete script->text;
3266 char* text = new char[totalLength + 1];
3267 script->text = text;
3268 script->textLength = totalLength;
Jack Palevich09555c72009-05-27 12:25:55 -07003269 char* dest = text;
Jack Palevich1cdef202009-05-22 12:06:27 -07003270 for(int i = 0; i < count; i++) {
3271 int len = -1;
3272 const ACCchar* s = string[i];
3273 if (length) {
3274 len = length[i];
3275 }
3276 if (len < 0) {
3277 len = strlen(s);
3278 }
Jack Palevich09555c72009-05-27 12:25:55 -07003279 memcpy(dest, s, len);
3280 dest += len;
Jack Palevich1cdef202009-05-22 12:06:27 -07003281 }
3282 text[totalLength] = '\0';
3283}
3284
3285extern "C"
3286void accCompileScript(ACCscript* script) {
3287 int result = script->compiler.compile(script->text, script->textLength);
3288 if (result) {
3289 script->setError(ACC_INVALID_OPERATION);
3290 }
3291}
3292
3293extern "C"
3294void accGetScriptiv(ACCscript* script,
3295 ACCenum pname,
3296 ACCint * params) {
3297 switch (pname) {
3298 case ACC_INFO_LOG_LENGTH:
3299 *params = 0;
3300 break;
3301 }
3302}
3303
3304extern "C"
3305void accGetScriptInfoLog(ACCscript* script,
3306 ACCsizei maxLength,
3307 ACCsizei * length,
3308 ACCchar * infoLog) {
Jack Palevichac0e95e2009-05-29 13:53:44 -07003309 char* message = script->compiler.getErrorMessage();
3310 int messageLength = strlen(message) + 1;
Jack Palevich1cdef202009-05-22 12:06:27 -07003311 if (length) {
Jack Palevichac0e95e2009-05-29 13:53:44 -07003312 *length = messageLength;
Jack Palevich1cdef202009-05-22 12:06:27 -07003313 }
Jack Palevichac0e95e2009-05-29 13:53:44 -07003314 if (infoLog && maxLength > 0) {
3315 int trimmedLength = maxLength < messageLength ?
3316 maxLength : messageLength;
3317 memcpy(infoLog, message, trimmedLength);
3318 infoLog[trimmedLength] = 0;
Jack Palevich1cdef202009-05-22 12:06:27 -07003319 }
3320}
3321
3322extern "C"
3323void accGetScriptLabel(ACCscript* script, const ACCchar * name,
3324 ACCvoid ** address) {
3325 void* value = script->compiler.lookup(name);
3326 if (value) {
3327 *address = value;
3328 } else {
3329 script->setError(ACC_INVALID_VALUE);
3330 }
3331}
3332
Jack Palevicheedf9d22009-06-04 16:23:40 -07003333extern "C"
3334void accGetPragmas(ACCscript* script, ACCsizei* actualStringCount,
3335 ACCsizei maxStringCount, ACCchar** strings){
3336 script->compiler.getPragmas(actualStringCount, maxStringCount, strings);
3337}
3338
-b master422972c2009-06-17 19:13:52 -07003339extern "C"
3340void accDisassemble(ACCscript* script) {
3341 script->compiler.disassemble(stderr);
3342}
3343
Jack Palevicheedf9d22009-06-04 16:23:40 -07003344
Jack Palevich1cdef202009-05-22 12:06:27 -07003345} // namespace acc
3346