blob: e7f95b036585ed3734cdbb7ffa499eb29d551e9c [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// #define ENABLE_ARM_DISASSEMBLY
50
Jack Palevichbbf8ab52009-05-11 11:54:30 -070051namespace acc {
52
Jack Palevichac0e95e2009-05-29 13:53:44 -070053class ErrorSink {
54public:
55 void error(const char *fmt, ...) {
56 va_list ap;
57 va_start(ap, fmt);
58 verror(fmt, ap);
59 va_end(ap);
60 }
61
62 virtual void verror(const char* fmt, va_list ap) = 0;
63};
64
65class Compiler : public ErrorSink {
Jack Palevich21a15a22009-05-11 14:49:29 -070066 class CodeBuf {
Jack Palevich653f42d2009-05-28 17:15:32 -070067 char* ind; // Output code pointer
Jack Palevich21a15a22009-05-11 14:49:29 -070068 char* pProgramBase;
Jack Palevichac0e95e2009-05-29 13:53:44 -070069 ErrorSink* mErrorSink;
70 int mSize;
Jack Palevich0a280a02009-06-11 10:53:51 -070071 bool mOverflowed;
Jack Palevichf0cbc922009-05-08 16:35:13 -070072
Jack Palevich21a15a22009-05-11 14:49:29 -070073 void release() {
74 if (pProgramBase != 0) {
75 free(pProgramBase);
76 pProgramBase = 0;
Jack Palevichae54f1f2009-05-08 14:54:15 -070077 }
Jack Palevich21a15a22009-05-11 14:49:29 -070078 }
79
Jack Palevich0a280a02009-06-11 10:53:51 -070080 bool check(int n) {
Jack Palevichac0e95e2009-05-29 13:53:44 -070081 int newSize = ind - pProgramBase + n;
Jack Palevich0a280a02009-06-11 10:53:51 -070082 bool overflow = newSize > mSize;
83 if (overflow && !mOverflowed) {
84 mOverflowed = true;
Jack Palevichac0e95e2009-05-29 13:53:44 -070085 if (mErrorSink) {
86 mErrorSink->error("Code too large: %d bytes", newSize);
87 }
88 }
Jack Palevich0a280a02009-06-11 10:53:51 -070089 return overflow;
Jack Palevichac0e95e2009-05-29 13:53:44 -070090 }
91
Jack Palevich21a15a22009-05-11 14:49:29 -070092 public:
93 CodeBuf() {
94 pProgramBase = 0;
95 ind = 0;
Jack Palevichac0e95e2009-05-29 13:53:44 -070096 mErrorSink = 0;
97 mSize = 0;
Jack Palevich0a280a02009-06-11 10:53:51 -070098 mOverflowed = false;
Jack Palevich21a15a22009-05-11 14:49:29 -070099 }
100
101 ~CodeBuf() {
102 release();
103 }
104
105 void init(int size) {
106 release();
Jack Palevichac0e95e2009-05-29 13:53:44 -0700107 mSize = size;
Jack Palevich21a15a22009-05-11 14:49:29 -0700108 pProgramBase = (char*) calloc(1, size);
109 ind = pProgramBase;
110 }
111
Jack Palevichac0e95e2009-05-29 13:53:44 -0700112 void setErrorSink(ErrorSink* pErrorSink) {
113 mErrorSink = pErrorSink;
114 }
115
Jack Palevich546b2242009-05-13 15:10:04 -0700116 int o4(int n) {
Jack Palevich0a280a02009-06-11 10:53:51 -0700117 if(check(4)) {
118 return 0;
119 }
Jack Palevich8b0624c2009-05-20 12:12:06 -0700120 intptr_t result = (intptr_t) ind;
Jack Palevich546b2242009-05-13 15:10:04 -0700121 * (int*) ind = n;
122 ind += 4;
123 return result;
124 }
125
Jack Palevich21a15a22009-05-11 14:49:29 -0700126 /*
127 * Output a byte. Handles all values, 0..ff.
128 */
129 void ob(int n) {
Jack Palevich0a280a02009-06-11 10:53:51 -0700130 if(check(1)) {
131 return;
132 }
Jack Palevich21a15a22009-05-11 14:49:29 -0700133 *ind++ = n;
134 }
135
Jack Palevich21a15a22009-05-11 14:49:29 -0700136 inline void* getBase() {
137 return (void*) pProgramBase;
138 }
139
Jack Palevich8b0624c2009-05-20 12:12:06 -0700140 intptr_t getSize() {
Jack Palevich21a15a22009-05-11 14:49:29 -0700141 return ind - pProgramBase;
142 }
143
Jack Palevich8b0624c2009-05-20 12:12:06 -0700144 intptr_t getPC() {
145 return (intptr_t) ind;
Jack Palevich21a15a22009-05-11 14:49:29 -0700146 }
147 };
148
Jack Palevich1cdef202009-05-22 12:06:27 -0700149 /**
150 * A code generator creates an in-memory program, generating the code on
151 * the fly. There is one code generator implementation for each supported
152 * architecture.
153 *
154 * The code generator implements the following abstract machine:
155 * R0 - the main accumulator.
156 * R1 - the secondary accumulator.
157 * FP - a frame pointer for accessing function arguments and local
158 * variables.
159 * SP - a stack pointer for storing intermediate results while evaluating
160 * expressions. The stack pointer grows downwards.
161 *
162 * The function calling convention is that all arguments are placed on the
163 * stack such that the first argument has the lowest address.
164 * After the call, the result is in R0. The caller is responsible for
165 * removing the arguments from the stack.
166 * The R0 and R1 registers are not saved across function calls. The
167 * FP and SP registers are saved.
168 */
169
Jack Palevich21a15a22009-05-11 14:49:29 -0700170 class CodeGenerator {
171 public:
Jack Palevichac0e95e2009-05-29 13:53:44 -0700172 CodeGenerator() {
173 mErrorSink = 0;
174 pCodeBuf = 0;
175 }
Jack Palevich21a15a22009-05-11 14:49:29 -0700176 virtual ~CodeGenerator() {}
177
Jack Palevich22305132009-05-13 10:58:45 -0700178 virtual void init(CodeBuf* pCodeBuf) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700179 this->pCodeBuf = pCodeBuf;
Jack Palevichac0e95e2009-05-29 13:53:44 -0700180 pCodeBuf->setErrorSink(mErrorSink);
181 }
182
183 void setErrorSink(ErrorSink* pErrorSink) {
184 mErrorSink = pErrorSink;
185 if (pCodeBuf) {
186 pCodeBuf->setErrorSink(mErrorSink);
187 }
Jack Palevich21a15a22009-05-11 14:49:29 -0700188 }
189
Jack Palevich1cdef202009-05-22 12:06:27 -0700190 /* Emit a function prolog.
191 * argCount is the number of arguments.
192 * Save the old value of the FP.
193 * Set the new value of the FP.
194 * Convert from the native platform calling convention to
195 * our stack-based calling convention. This may require
196 * pushing arguments from registers to the stack.
197 * Allocate "N" bytes of stack space. N isn't known yet, so
198 * just emit the instructions for adjusting the stack, and return
199 * the address to patch up. The patching will be done in
200 * functionExit().
201 * returns address to patch with local variable size.
Jack Palevich22305132009-05-13 10:58:45 -0700202 */
Jack Palevich546b2242009-05-13 15:10:04 -0700203 virtual int functionEntry(int argCount) = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700204
Jack Palevich1cdef202009-05-22 12:06:27 -0700205 /* Emit a function epilog.
206 * Restore the old SP and FP register values.
207 * Return to the calling function.
208 * argCount - the number of arguments to the function.
209 * localVariableAddress - returned from functionEntry()
210 * localVariableSize - the size in bytes of the local variables.
211 */
212 virtual void functionExit(int argCount, int localVariableAddress,
213 int localVariableSize) = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700214
Jack Palevich1cdef202009-05-22 12:06:27 -0700215 /* load immediate value to R0 */
Jack Palevich546b2242009-05-13 15:10:04 -0700216 virtual void li(int t) = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700217
Jack Palevich1cdef202009-05-22 12:06:27 -0700218 /* Jump to a target, and return the address of the word that
219 * holds the target data, in case it needs to be fixed up later.
220 */
Jack Palevich22305132009-05-13 10:58:45 -0700221 virtual int gjmp(int t) = 0;
222
Jack Palevich1cdef202009-05-22 12:06:27 -0700223 /* Test R0 and jump to a target if the test succeeds.
224 * l = 0: je, l == 1: jne
225 * Return the address of the word that holds the targed data, in
226 * case it needs to be fixed up later.
227 */
Jack Palevich22305132009-05-13 10:58:45 -0700228 virtual int gtst(bool l, int t) = 0;
229
Jack Palevich1cdef202009-05-22 12:06:27 -0700230 /* Compare R1 against R0, and store the boolean result in R0.
231 * op specifies the comparison.
232 */
Jack Palevich22305132009-05-13 10:58:45 -0700233 virtual void gcmp(int op) = 0;
234
Jack Palevich1cdef202009-05-22 12:06:27 -0700235 /* Perform the arithmetic op specified by op. R1 is the
236 * left argument, R0 is the right argument.
237 */
Jack Palevich546b2242009-05-13 15:10:04 -0700238 virtual void genOp(int op) = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700239
Jack Palevich1cdef202009-05-22 12:06:27 -0700240 /* Set R1 to 0.
241 */
242 virtual void clearR1() = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700243
Jack Palevich1cdef202009-05-22 12:06:27 -0700244 /* Push R0 onto the stack.
245 */
246 virtual void pushR0() = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700247
Jack Palevich1cdef202009-05-22 12:06:27 -0700248 /* Pop R1 off of the stack.
249 */
250 virtual void popR1() = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700251
Jack Palevich1cdef202009-05-22 12:06:27 -0700252 /* Store R0 to the address stored in R1.
253 * isInt is true if a whole 4-byte integer value
254 * should be stored, otherwise a 1-byte character
255 * value should be stored.
256 */
257 virtual void storeR0ToR1(bool isInt) = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700258
Jack Palevich1cdef202009-05-22 12:06:27 -0700259 /* Load R0 from the address stored in R0.
260 * isInt is true if a whole 4-byte integer value
261 * should be loaded, otherwise a 1-byte character
262 * value should be loaded.
263 */
264 virtual void loadR0FromR0(bool isInt) = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700265
Jack Palevich1cdef202009-05-22 12:06:27 -0700266 /* Load the absolute address of a variable to R0.
267 * If ea <= LOCAL, then this is a local variable, or an
268 * argument, addressed relative to FP.
269 * else it is an absolute global address.
270 */
271 virtual void leaR0(int ea) = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700272
Jack Palevich1cdef202009-05-22 12:06:27 -0700273 /* Store R0 to a variable.
274 * If ea <= LOCAL, then this is a local variable, or an
275 * argument, addressed relative to FP.
276 * else it is an absolute global address.
277 */
278 virtual void storeR0(int ea) = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700279
Jack Palevich1cdef202009-05-22 12:06:27 -0700280 /* load R0 from a variable.
281 * If ea <= LOCAL, then this is a local variable, or an
282 * argument, addressed relative to FP.
283 * else it is an absolute global address.
284 * If isIncDec is true, then the stored variable's value
285 * should be post-incremented or post-decremented, based
286 * on the value of op.
287 */
288 virtual void loadR0(int ea, bool isIncDec, int op) = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700289
Jack Palevich1cdef202009-05-22 12:06:27 -0700290 /* Emit code to adjust the stack for a function call. Return the
291 * label for the address of the instruction that adjusts the
292 * stack size. This will be passed as argument "a" to
293 * endFunctionCallArguments.
294 */
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700295 virtual int beginFunctionCallArguments() = 0;
296
Jack Palevich1cdef202009-05-22 12:06:27 -0700297 /* Emit code to store R0 to the stack at byte offset l.
298 */
299 virtual void storeR0ToArg(int l) = 0;
Jack Palevich7810bc92009-05-15 14:31:47 -0700300
Jack Palevich1cdef202009-05-22 12:06:27 -0700301 /* Patch the function call preamble.
302 * a is the address returned from beginFunctionCallArguments
303 * l is the number of bytes the arguments took on the stack.
304 * Typically you would also emit code to convert the argument
305 * list into whatever the native function calling convention is.
306 * On ARM for example you would pop the first 5 arguments into
307 * R0..R4
308 */
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700309 virtual void endFunctionCallArguments(int a, int l) = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700310
Jack Palevich1cdef202009-05-22 12:06:27 -0700311 /* Emit a call to an unknown function. The argument "symbol" needs to
312 * be stored in the location where the address should go. It forms
313 * a chain. The address will be patched later.
314 * Return the address of the word that has to be patched.
315 */
Jack Palevich22305132009-05-13 10:58:45 -0700316 virtual int callForward(int symbol) = 0;
317
Jack Palevich1cdef202009-05-22 12:06:27 -0700318 /* Call a function using PC-relative addressing. t is the PC-relative
319 * address of the function. It has already been adjusted for the
320 * architectural jump offset, so just store it as-is.
321 */
Jack Palevich22305132009-05-13 10:58:45 -0700322 virtual void callRelative(int t) = 0;
323
Jack Palevich1cdef202009-05-22 12:06:27 -0700324 /* Call a function pointer. L is the number of bytes the arguments
325 * take on the stack. The address of the function is stored at
326 * location SP + l.
327 */
Jack Palevich22305132009-05-13 10:58:45 -0700328 virtual void callIndirect(int l) = 0;
329
Jack Palevich1cdef202009-05-22 12:06:27 -0700330 /* Adjust SP after returning from a function call. l is the
331 * number of bytes of arguments stored on the stack. isIndirect
332 * is true if this was an indirect call. (In which case the
333 * address of the function is stored at location SP + l.)
334 */
Jack Palevich7810bc92009-05-15 14:31:47 -0700335 virtual void adjustStackAfterCall(int l, bool isIndirect) = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700336
Jack Palevich1cdef202009-05-22 12:06:27 -0700337 /* Print a disassembly of the assembled code to out. Return
338 * non-zero if there is an error.
339 */
Jack Palevicha6535612009-05-13 16:24:17 -0700340 virtual int disassemble(FILE* out) = 0;
341
Jack Palevich1cdef202009-05-22 12:06:27 -0700342 /* Generate a symbol at the current PC. t is the head of a
343 * linked list of addresses to patch.
344 */
Jack Paleviche7b59062009-05-19 17:12:17 -0700345 virtual void gsym(int t) = 0;
Jack Palevich21a15a22009-05-11 14:49:29 -0700346
Jack Palevich1cdef202009-05-22 12:06:27 -0700347 /*
348 * Do any cleanup work required at the end of a compile.
349 * For example, an instruction cache might need to be
350 * invalidated.
351 * Return non-zero if there is an error.
352 */
353 virtual int finishCompile() = 0;
Jack Palevich546b2242009-05-13 15:10:04 -0700354
Jack Palevicha6535612009-05-13 16:24:17 -0700355 /**
356 * Adjust relative branches by this amount.
357 */
358 virtual int jumpOffset() = 0;
359
Jack Palevich21a15a22009-05-11 14:49:29 -0700360 protected:
Jack Palevich21a15a22009-05-11 14:49:29 -0700361 /*
362 * Output a byte. Handles all values, 0..ff.
363 */
364 void ob(int n) {
365 pCodeBuf->ob(n);
366 }
367
Jack Palevich8b0624c2009-05-20 12:12:06 -0700368 intptr_t o4(int data) {
Jack Paleviche7b59062009-05-19 17:12:17 -0700369 return pCodeBuf->o4(data);
Jack Palevich21a15a22009-05-11 14:49:29 -0700370 }
371
Jack Palevich8b0624c2009-05-20 12:12:06 -0700372 intptr_t getBase() {
373 return (intptr_t) pCodeBuf->getBase();
Jack Palevicha6535612009-05-13 16:24:17 -0700374 }
375
Jack Palevich8b0624c2009-05-20 12:12:06 -0700376 intptr_t getPC() {
Jack Palevich21a15a22009-05-11 14:49:29 -0700377 return pCodeBuf->getPC();
378 }
Jack Palevich1cdef202009-05-22 12:06:27 -0700379
380 intptr_t getSize() {
381 return pCodeBuf->getSize();
382 }
Jack Palevichac0e95e2009-05-29 13:53:44 -0700383
384 void error(const char* fmt,...) {
385 va_list ap;
386 va_start(ap, fmt);
387 mErrorSink->verror(fmt, ap);
388 va_end(ap);
389 }
Jack Palevich21a15a22009-05-11 14:49:29 -0700390 private:
391 CodeBuf* pCodeBuf;
Jack Palevichac0e95e2009-05-29 13:53:44 -0700392 ErrorSink* mErrorSink;
Jack Palevich21a15a22009-05-11 14:49:29 -0700393 };
394
Jack Paleviche7b59062009-05-19 17:12:17 -0700395#ifdef PROVIDE_ARM_CODEGEN
396
Jack Palevich22305132009-05-13 10:58:45 -0700397 class ARMCodeGenerator : public CodeGenerator {
398 public:
399 ARMCodeGenerator() {}
400 virtual ~ARMCodeGenerator() {}
401
402 /* returns address to patch with local variable size
403 */
Jack Palevich546b2242009-05-13 15:10:04 -0700404 virtual int functionEntry(int argCount) {
Jack Palevichb7c81e92009-06-04 19:56:13 -0700405 LOG_API("functionEntry(%d);\n", argCount);
Jack Palevich69796b62009-05-14 15:42:26 -0700406 // sp -> arg4 arg5 ...
407 // Push our register-based arguments back on the stack
408 if (argCount > 0) {
409 int regArgCount = argCount <= 4 ? argCount : 4;
410 o4(0xE92D0000 | ((1 << argCount) - 1)); // stmfd sp!, {}
411 }
412 // sp -> arg0 arg1 ...
413 o4(0xE92D4800); // stmfd sp!, {fp, lr}
414 // sp, fp -> oldfp, retadr, arg0 arg1 ....
415 o4(0xE1A0B00D); // mov fp, sp
416 return o4(0xE24DD000); // sub sp, sp, # <local variables>
Jack Palevich22305132009-05-13 10:58:45 -0700417 }
418
Jack Palevich546b2242009-05-13 15:10:04 -0700419 virtual void functionExit(int argCount, int localVariableAddress, int localVariableSize) {
Jack Palevich09555c72009-05-27 12:25:55 -0700420 LOG_API("functionExit(%d, %d, %d);\n", argCount, localVariableAddress, localVariableSize);
Jack Palevich69796b62009-05-14 15:42:26 -0700421 // Patch local variable allocation code:
422 if (localVariableSize < 0 || localVariableSize > 255) {
Jack Palevich8de461d2009-05-14 17:21:45 -0700423 error("localVariables out of range: %d", localVariableSize);
Jack Palevich546b2242009-05-13 15:10:04 -0700424 }
Jack Palevich69796b62009-05-14 15:42:26 -0700425 *(char*) (localVariableAddress) = localVariableSize;
426
427 // sp -> locals .... fp -> oldfp, retadr, arg0, arg1, ...
428 o4(0xE1A0E00B); // mov lr, fp
429 o4(0xE59BB000); // ldr fp, [fp]
430 o4(0xE28ED004); // add sp, lr, #4
431 // sp -> retadr, arg0, ...
432 o4(0xE8BD4000); // ldmfd sp!, {lr}
433 // sp -> arg0 ....
434 if (argCount > 0) {
435 // We store the PC into the lr so we can adjust the sp before
Jack Palevich8de461d2009-05-14 17:21:45 -0700436 // returning. We need to pull off the registers we pushed
Jack Palevich69796b62009-05-14 15:42:26 -0700437 // earlier. We don't need to actually store them anywhere,
438 // just adjust the stack.
439 int regArgCount = argCount <= 4 ? argCount : 4;
440 o4(0xE28DD000 | (regArgCount << 2)); // add sp, sp, #argCount << 2
441 }
442 o4(0xE12FFF1E); // bx lr
Jack Palevich22305132009-05-13 10:58:45 -0700443 }
444
445 /* load immediate value */
Jack Palevich546b2242009-05-13 15:10:04 -0700446 virtual void li(int t) {
Jack Palevich09555c72009-05-27 12:25:55 -0700447 LOG_API("li(%d);\n", t);
Jack Palevicha6535612009-05-13 16:24:17 -0700448 if (t >= 0 && t < 255) {
Jack Palevich69796b62009-05-14 15:42:26 -0700449 o4(0xE3A00000 + t); // mov r0, #0
Jack Palevicha6535612009-05-13 16:24:17 -0700450 } else if (t >= -256 && t < 0) {
451 // mvn means move constant ^ ~0
Jack Palevich69796b62009-05-14 15:42:26 -0700452 o4(0xE3E00001 - t); // mvn r0, #0
Jack Palevicha6535612009-05-13 16:24:17 -0700453 } else {
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700454 o4(0xE51F0000); // ldr r0, .L3
455 o4(0xEA000000); // b .L99
456 o4(t); // .L3: .word 0
457 // .L99:
Jack Palevicha6535612009-05-13 16:24:17 -0700458 }
Jack Palevich22305132009-05-13 10:58:45 -0700459 }
460
461 virtual int gjmp(int t) {
Jack Palevich09555c72009-05-27 12:25:55 -0700462 LOG_API("gjmp(%d);\n", t);
Jack Palevich8de461d2009-05-14 17:21:45 -0700463 return o4(0xEA000000 | encodeAddress(t)); // b .L33
Jack Palevich22305132009-05-13 10:58:45 -0700464 }
465
466 /* l = 0: je, l == 1: jne */
467 virtual int gtst(bool l, int t) {
Jack Palevich09555c72009-05-27 12:25:55 -0700468 LOG_API("gtst(%d, %d);\n", l, t);
Jack Palevich8de461d2009-05-14 17:21:45 -0700469 o4(0xE3500000); // cmp r0,#0
470 int branch = l ? 0x1A000000 : 0x0A000000; // bne : beq
471 return o4(branch | encodeAddress(t));
Jack Palevich22305132009-05-13 10:58:45 -0700472 }
473
474 virtual void gcmp(int op) {
Jack Palevich09555c72009-05-27 12:25:55 -0700475 LOG_API("gcmp(%d);\n", op);
Jack Palevich8de461d2009-05-14 17:21:45 -0700476 o4(0xE1510000); // cmp r1, r1
477 switch(op) {
478 case OP_EQUALS:
479 o4(0x03A00001); // moveq r0,#1
480 o4(0x13A00000); // movne r0,#0
481 break;
482 case OP_NOT_EQUALS:
483 o4(0x03A00000); // moveq r0,#0
484 o4(0x13A00001); // movne r0,#1
485 break;
486 case OP_LESS_EQUAL:
487 o4(0xD3A00001); // movle r0,#1
488 o4(0xC3A00000); // movgt r0,#0
489 break;
490 case OP_GREATER:
491 o4(0xD3A00000); // movle r0,#0
492 o4(0xC3A00001); // movgt r0,#1
493 break;
494 case OP_GREATER_EQUAL:
495 o4(0xA3A00001); // movge r0,#1
496 o4(0xB3A00000); // movlt r0,#0
497 break;
498 case OP_LESS:
499 o4(0xA3A00000); // movge r0,#0
500 o4(0xB3A00001); // movlt r0,#1
501 break;
502 default:
503 error("Unknown comparison op %d", op);
504 break;
505 }
Jack Palevich22305132009-05-13 10:58:45 -0700506 }
507
Jack Palevich546b2242009-05-13 15:10:04 -0700508 virtual void genOp(int op) {
Jack Palevich09555c72009-05-27 12:25:55 -0700509 LOG_API("genOp(%d);\n", op);
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700510 switch(op) {
511 case OP_MUL:
512 o4(0x0E0000091); // mul r0,r1,r0
513 break;
Jack Palevich3d474a72009-05-15 15:12:38 -0700514 case OP_DIV:
515 callRuntime(runtime_DIV);
516 break;
517 case OP_MOD:
518 callRuntime(runtime_MOD);
519 break;
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700520 case OP_PLUS:
521 o4(0xE0810000); // add r0,r1,r0
522 break;
523 case OP_MINUS:
524 o4(0xE0410000); // sub r0,r1,r0
525 break;
526 case OP_SHIFT_LEFT:
527 o4(0xE1A00011); // lsl r0,r1,r0
528 break;
529 case OP_SHIFT_RIGHT:
530 o4(0xE1A00051); // asr r0,r1,r0
531 break;
532 case OP_BIT_AND:
533 o4(0xE0010000); // and r0,r1,r0
534 break;
535 case OP_BIT_XOR:
536 o4(0xE0210000); // eor r0,r1,r0
537 break;
538 case OP_BIT_OR:
539 o4(0xE1810000); // orr r0,r1,r0
540 break;
541 case OP_BIT_NOT:
542 o4(0xE1E00000); // mvn r0, r0
543 break;
544 default:
Jack Palevich69796b62009-05-14 15:42:26 -0700545 error("Unimplemented op %d\n", op);
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700546 break;
547 }
Jack Palevich22305132009-05-13 10:58:45 -0700548#if 0
549 o(decodeOp(op));
550 if (op == OP_MOD)
551 o(0x92); /* xchg %edx, %eax */
552#endif
553 }
554
Jack Palevich1cdef202009-05-22 12:06:27 -0700555 virtual void clearR1() {
Jack Palevich09555c72009-05-27 12:25:55 -0700556 LOG_API("clearR1();\n");
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700557 o4(0xE3A01000); // mov r1, #0
Jack Palevich22305132009-05-13 10:58:45 -0700558 }
559
Jack Palevich1cdef202009-05-22 12:06:27 -0700560 virtual void pushR0() {
Jack Palevich09555c72009-05-27 12:25:55 -0700561 LOG_API("pushR0();\n");
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700562 o4(0xE92D0001); // stmfd sp!,{r0}
Jack Palevich22305132009-05-13 10:58:45 -0700563 }
564
Jack Palevich1cdef202009-05-22 12:06:27 -0700565 virtual void popR1() {
Jack Palevich09555c72009-05-27 12:25:55 -0700566 LOG_API("popR1();\n");
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700567 o4(0xE8BD0002); // ldmfd sp!,{r1}
Jack Palevich22305132009-05-13 10:58:45 -0700568 }
569
Jack Palevich1cdef202009-05-22 12:06:27 -0700570 virtual void storeR0ToR1(bool isInt) {
Jack Palevich09555c72009-05-27 12:25:55 -0700571 LOG_API("storeR0ToR1(%d);\n", isInt);
Jack Palevichbd894902009-05-14 19:35:31 -0700572 if (isInt) {
573 o4(0xE5810000); // str r0, [r1]
574 } else {
575 o4(0xE5C10000); // strb r0, [r1]
576 }
Jack Palevich22305132009-05-13 10:58:45 -0700577 }
578
Jack Palevich1cdef202009-05-22 12:06:27 -0700579 virtual void loadR0FromR0(bool isInt) {
Jack Palevich09555c72009-05-27 12:25:55 -0700580 LOG_API("loadR0FromR0(%d);\n", isInt);
Jack Palevich22305132009-05-13 10:58:45 -0700581 if (isInt)
Jack Palevich69796b62009-05-14 15:42:26 -0700582 o4(0xE5900000); // ldr r0, [r0]
Jack Palevich22305132009-05-13 10:58:45 -0700583 else
Jack Palevich69796b62009-05-14 15:42:26 -0700584 o4(0xE5D00000); // ldrb r0, [r0]
Jack Palevich22305132009-05-13 10:58:45 -0700585 }
586
Jack Palevich1cdef202009-05-22 12:06:27 -0700587 virtual void leaR0(int ea) {
Jack Palevich09555c72009-05-27 12:25:55 -0700588 LOG_API("leaR0(%d);\n", ea);
Jack Palevich4d93f302009-05-15 13:30:00 -0700589 if (ea < LOCAL) {
590 // Local, fp relative
591 if (ea < -1023 || ea > 1023 || ((ea & 3) != 0)) {
592 error("Offset out of range: %08x", ea);
593 }
594 if (ea < 0) {
595 o4(0xE24B0F00 | (0xff & ((-ea) >> 2))); // sub r0, fp, #ea
596 } else {
597 o4(0xE28B0F00 | (0xff & (ea >> 2))); // add r0, fp, #ea
598 }
Jack Palevichbd894902009-05-14 19:35:31 -0700599 } else {
Jack Palevich4d93f302009-05-15 13:30:00 -0700600 // Global, absolute.
601 o4(0xE59F0000); // ldr r0, .L1
602 o4(0xEA000000); // b .L99
603 o4(ea); // .L1: .word 0
604 // .L99:
Jack Palevichbd894902009-05-14 19:35:31 -0700605 }
Jack Palevich22305132009-05-13 10:58:45 -0700606 }
607
Jack Palevich1cdef202009-05-22 12:06:27 -0700608 virtual void storeR0(int ea) {
Jack Palevich09555c72009-05-27 12:25:55 -0700609 LOG_API("storeR0(%d);\n", ea);
Jack Palevich4d93f302009-05-15 13:30:00 -0700610 if (ea < LOCAL) {
611 // Local, fp relative
612 if (ea < -4095 || ea > 4095) {
613 error("Offset out of range: %08x", ea);
614 }
615 if (ea < 0) {
616 o4(0xE50B0000 | (0xfff & (-ea))); // str r0, [fp,#-ea]
617 } else {
618 o4(0xE58B0000 | (0xfff & ea)); // str r0, [fp,#ea]
619 }
620 } else{
621 // Global, absolute
622 o4(0xE59F1000); // ldr r1, .L1
623 o4(0xEA000000); // b .L99
624 o4(ea); // .L1: .word 0
625 o4(0xE5810000); // .L99: str r0, [r1]
Jack Palevich69796b62009-05-14 15:42:26 -0700626 }
Jack Palevich22305132009-05-13 10:58:45 -0700627 }
628
Jack Palevich1cdef202009-05-22 12:06:27 -0700629 virtual void loadR0(int ea, bool isIncDec, int op) {
Jack Palevich09555c72009-05-27 12:25:55 -0700630 LOG_API("loadR0(%d, %d, %d);\n", ea, isIncDec, op);
Jack Palevich4d93f302009-05-15 13:30:00 -0700631 if (ea < LOCAL) {
632 // Local, fp relative
633 if (ea < -4095 || ea > 4095) {
634 error("Offset out of range: %08x", ea);
635 }
636 if (ea < 0) {
637 o4(0xE51B0000 | (0xfff & (-ea))); // ldr r0, [fp,#-ea]
638 } else {
639 o4(0xE59B0000 | (0xfff & ea)); // ldr r0, [fp,#ea]
640 }
Jack Palevich69796b62009-05-14 15:42:26 -0700641 } else {
Jack Palevich4d93f302009-05-15 13:30:00 -0700642 // Global, absolute
643 o4(0xE59F2000); // ldr r2, .L1
644 o4(0xEA000000); // b .L99
645 o4(ea); // .L1: .word ea
646 o4(0xE5920000); // .L99: ldr r0, [r2]
Jack Palevich69796b62009-05-14 15:42:26 -0700647 }
Jack Palevich22305132009-05-13 10:58:45 -0700648
Jack Palevich4d93f302009-05-15 13:30:00 -0700649 if (isIncDec) {
650 switch (op) {
651 case OP_INCREMENT:
652 o4(0xE2801001); // add r1, r0, #1
653 break;
654 case OP_DECREMENT:
655 o4(0xE2401001); // sub r1, r0, #1
656 break;
657 default:
658 error("unknown opcode: %d", op);
659 }
660 if (ea < LOCAL) {
661 // Local, fp relative
662 // Don't need range check, was already checked above
663 if (ea < 0) {
664 o4(0xE50B1000 | (0xfff & (-ea))); // str r1, [fp,#-ea]
665 } else {
666 o4(0xE58B1000 | (0xfff & ea)); // str r1, [fp,#ea]
667 }
668 } else{
669 // Global, absolute
670 // r2 is already set up from before.
671 o4(0xE5821000); // str r1, [r2]
672 }
Jack Palevichbd894902009-05-14 19:35:31 -0700673 }
Jack Palevich22305132009-05-13 10:58:45 -0700674 }
675
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700676 virtual int beginFunctionCallArguments() {
Jack Palevich09555c72009-05-27 12:25:55 -0700677 LOG_API("beginFunctionCallArguments();\n");
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700678 return o4(0xE24DDF00); // Placeholder
679 }
680
Jack Palevich1cdef202009-05-22 12:06:27 -0700681 virtual void storeR0ToArg(int l) {
Jack Palevich09555c72009-05-27 12:25:55 -0700682 LOG_API("storeR0ToArg(%d);\n", l);
Jack Palevich7810bc92009-05-15 14:31:47 -0700683 if (l < 0 || l > 4096-4) {
684 error("l out of range for stack offset: 0x%08x", l);
685 }
686 o4(0xE58D0000 + l); // str r0, [sp, #4]
687 }
688
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700689 virtual void endFunctionCallArguments(int a, int l) {
Jack Palevich09555c72009-05-27 12:25:55 -0700690 LOG_API("endFunctionCallArguments(0x%08x, %d);\n", a, l);
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700691 if (l < 0 || l > 0x3FC) {
692 error("L out of range for stack adjustment: 0x%08x", l);
693 }
694 * (int*) a = 0xE24DDF00 | (l >> 2); // sub sp, sp, #0 << 2
695 int argCount = l >> 2;
696 if (argCount > 0) {
697 int regArgCount = argCount > 4 ? 4 : argCount;
698 o4(0xE8BD0000 | ((1 << regArgCount) - 1)); // ldmfd sp!,{}
699 }
Jack Palevich22305132009-05-13 10:58:45 -0700700 }
701
Jack Palevich22305132009-05-13 10:58:45 -0700702 virtual int callForward(int symbol) {
Jack Palevich09555c72009-05-27 12:25:55 -0700703 LOG_API("callForward(%d);\n", symbol);
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700704 // Forward calls are always short (local)
705 return o4(0xEB000000 | encodeAddress(symbol));
Jack Palevich22305132009-05-13 10:58:45 -0700706 }
707
708 virtual void callRelative(int t) {
Jack Palevich09555c72009-05-27 12:25:55 -0700709 LOG_API("callRelative(%d);\n", t);
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700710 int abs = t + getPC() + jumpOffset();
Jack Palevichac0e95e2009-05-29 13:53:44 -0700711 LOG_API("abs=%d (0x%08x)\n", abs, abs);
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700712 if (t >= - (1 << 25) && t < (1 << 25)) {
713 o4(0xEB000000 | encodeAddress(t));
714 } else {
715 // Long call.
716 o4(0xE59FC000); // ldr r12, .L1
717 o4(0xEA000000); // b .L99
Jack Palevichbd894902009-05-14 19:35:31 -0700718 o4(t - 12); // .L1: .word 0
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700719 o4(0xE08CC00F); // .L99: add r12,pc
720 o4(0xE12FFF3C); // blx r12
721 }
Jack Palevich22305132009-05-13 10:58:45 -0700722 }
723
724 virtual void callIndirect(int l) {
Jack Palevich09555c72009-05-27 12:25:55 -0700725 LOG_API("callIndirect(%d);\n", l);
Jack Palevich7810bc92009-05-15 14:31:47 -0700726 int argCount = l >> 2;
727 int poppedArgs = argCount > 4 ? 4 : argCount;
728 int adjustedL = l - (poppedArgs << 2);
729 if (adjustedL < 0 || adjustedL > 4096-4) {
730 error("l out of range for stack offset: 0x%08x", l);
731 }
732 o4(0xE59DC000 | (0xfff & adjustedL)); // ldr r12, [sp,#adjustedL]
733 o4(0xE12FFF3C); // blx r12
Jack Palevich22305132009-05-13 10:58:45 -0700734 }
735
Jack Palevich7810bc92009-05-15 14:31:47 -0700736 virtual void adjustStackAfterCall(int l, bool isIndirect) {
Jack Palevich09555c72009-05-27 12:25:55 -0700737 LOG_API("adjustStackAfterCall(%d, %d);\n", l, isIndirect);
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700738 int argCount = l >> 2;
Jack Palevich7810bc92009-05-15 14:31:47 -0700739 int stackArgs = argCount > 4 ? argCount - 4 : 0;
740 int stackUse = stackArgs + (isIndirect ? 1 : 0);
741 if (stackUse) {
742 if (stackUse < 0 || stackUse > 255) {
743 error("L out of range for stack adjustment: 0x%08x", l);
744 }
745 o4(0xE28DDF00 | stackUse); // add sp, sp, #stackUse << 2
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700746 }
Jack Palevich22305132009-05-13 10:58:45 -0700747 }
748
Jack Palevicha6535612009-05-13 16:24:17 -0700749 virtual int jumpOffset() {
Jack Palevichbd894902009-05-14 19:35:31 -0700750 return 8;
Jack Palevicha6535612009-05-13 16:24:17 -0700751 }
752
753 /* output a symbol and patch all calls to it */
754 virtual void gsym(int t) {
Jack Palevich09555c72009-05-27 12:25:55 -0700755 LOG_API("gsym(0x%x)\n", t);
Jack Palevicha6535612009-05-13 16:24:17 -0700756 int n;
757 int base = getBase();
758 int pc = getPC();
Jack Palevich09555c72009-05-27 12:25:55 -0700759 LOG_API("pc = 0x%x\n", pc);
Jack Palevicha6535612009-05-13 16:24:17 -0700760 while (t) {
761 int data = * (int*) t;
762 int decodedOffset = ((BRANCH_REL_ADDRESS_MASK & data) << 2);
763 if (decodedOffset == 0) {
764 n = 0;
765 } else {
766 n = base + decodedOffset; /* next value */
767 }
768 *(int *) t = (data & ~BRANCH_REL_ADDRESS_MASK)
769 | encodeRelAddress(pc - t - 8);
770 t = n;
771 }
772 }
773
Jack Palevich1cdef202009-05-22 12:06:27 -0700774 virtual int finishCompile() {
775#if defined(__arm__)
776 const long base = long(getBase());
777 const long curr = long(getPC());
778 int err = cacheflush(base, curr, 0);
779 return err;
780#else
781 return 0;
782#endif
783 }
784
Jack Palevicha6535612009-05-13 16:24:17 -0700785 virtual int disassemble(FILE* out) {
Jack Palevich09555c72009-05-27 12:25:55 -0700786#ifdef ENABLE_ARM_DISASSEMBLY
787 disasmOut = out;
Jack Palevicha6535612009-05-13 16:24:17 -0700788 disasm_interface_t di;
789 di.di_readword = disassemble_readword;
790 di.di_printaddr = disassemble_printaddr;
791 di.di_printf = disassemble_printf;
792
793 int base = getBase();
794 int pc = getPC();
795 for(int i = base; i < pc; i += 4) {
796 fprintf(out, "%08x: %08x ", i, *(int*) i);
797 ::disasm(&di, i, 0);
798 }
Jack Palevich09555c72009-05-27 12:25:55 -0700799#endif
Jack Palevicha6535612009-05-13 16:24:17 -0700800 return 0;
801 }
Jack Palevich7810bc92009-05-15 14:31:47 -0700802
Jack Palevich22305132009-05-13 10:58:45 -0700803 private:
Jack Palevicha6535612009-05-13 16:24:17 -0700804 static FILE* disasmOut;
805
806 static u_int
807 disassemble_readword(u_int address)
808 {
809 return(*((u_int *)address));
810 }
811
812 static void
813 disassemble_printaddr(u_int address)
814 {
815 fprintf(disasmOut, "0x%08x", address);
816 }
817
818 static void
819 disassemble_printf(const char *fmt, ...) {
820 va_list ap;
821 va_start(ap, fmt);
822 vfprintf(disasmOut, fmt, ap);
823 va_end(ap);
824 }
825
826 static const int BRANCH_REL_ADDRESS_MASK = 0x00ffffff;
827
828 /** Encode a relative address that might also be
829 * a label.
830 */
831 int encodeAddress(int value) {
832 int base = getBase();
833 if (value >= base && value <= getPC() ) {
834 // This is a label, encode it relative to the base.
835 value = value - base;
836 }
837 return encodeRelAddress(value);
838 }
839
840 int encodeRelAddress(int value) {
841 return BRANCH_REL_ADDRESS_MASK & (value >> 2);
842 }
Jack Palevich22305132009-05-13 10:58:45 -0700843
Jack Palevich3d474a72009-05-15 15:12:38 -0700844 typedef int (*int2FnPtr)(int a, int b);
845 void callRuntime(int2FnPtr fn) {
846 o4(0xE59F2000); // ldr r2, .L1
847 o4(0xEA000000); // b .L99
848 o4((int) fn); //.L1: .word fn
849 o4(0xE12FFF32); //.L99: blx r2
850 }
851
852 static int runtime_DIV(int a, int b) {
853 return b / a;
854 }
855
856 static int runtime_MOD(int a, int b) {
857 return b % a;
858 }
Jack Palevich22305132009-05-13 10:58:45 -0700859 };
860
Jack Palevich09555c72009-05-27 12:25:55 -0700861#endif // PROVIDE_ARM_CODEGEN
Jack Paleviche7b59062009-05-19 17:12:17 -0700862
863#ifdef PROVIDE_X86_CODEGEN
864
Jack Palevich21a15a22009-05-11 14:49:29 -0700865 class X86CodeGenerator : public CodeGenerator {
866 public:
867 X86CodeGenerator() {}
868 virtual ~X86CodeGenerator() {}
869
Jack Palevichbf42c9c2009-05-12 12:48:35 -0700870 /* returns address to patch with local variable size
871 */
Jack Palevich546b2242009-05-13 15:10:04 -0700872 virtual int functionEntry(int argCount) {
Jack Palevichbf42c9c2009-05-12 12:48:35 -0700873 o(0xe58955); /* push %ebp, mov %esp, %ebp */
874 return oad(0xec81, 0); /* sub $xxx, %esp */
875 }
876
Jack Palevich546b2242009-05-13 15:10:04 -0700877 virtual void functionExit(int argCount, int localVariableAddress, int localVariableSize) {
Jack Palevichbf42c9c2009-05-12 12:48:35 -0700878 o(0xc3c9); /* leave, ret */
Jack Palevich546b2242009-05-13 15:10:04 -0700879 *(int *) localVariableAddress = localVariableSize; /* save local variables */
Jack Palevichbf42c9c2009-05-12 12:48:35 -0700880 }
881
Jack Palevich21a15a22009-05-11 14:49:29 -0700882 /* load immediate value */
Jack Palevich546b2242009-05-13 15:10:04 -0700883 virtual void li(int t) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700884 oad(0xb8, t); /* mov $xx, %eax */
885 }
886
Jack Palevich22305132009-05-13 10:58:45 -0700887 virtual int gjmp(int t) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700888 return psym(0xe9, t);
889 }
890
891 /* l = 0: je, l == 1: jne */
Jack Palevich22305132009-05-13 10:58:45 -0700892 virtual int gtst(bool l, int t) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700893 o(0x0fc085); /* test %eax, %eax, je/jne xxx */
894 return psym(0x84 + l, t);
895 }
896
Jack Palevich22305132009-05-13 10:58:45 -0700897 virtual void gcmp(int op) {
Jack Palevichbf42c9c2009-05-12 12:48:35 -0700898 int t = decodeOp(op);
Jack Palevich21a15a22009-05-11 14:49:29 -0700899 o(0xc139); /* cmp %eax,%ecx */
900 li(0);
901 o(0x0f); /* setxx %al */
902 o(t + 0x90);
903 o(0xc0);
904 }
905
Jack Palevich546b2242009-05-13 15:10:04 -0700906 virtual void genOp(int op) {
Jack Palevichbf42c9c2009-05-12 12:48:35 -0700907 o(decodeOp(op));
908 if (op == OP_MOD)
909 o(0x92); /* xchg %edx, %eax */
910 }
911
Jack Palevich1cdef202009-05-22 12:06:27 -0700912 virtual void clearR1() {
Jack Palevich21a15a22009-05-11 14:49:29 -0700913 oad(0xb9, 0); /* movl $0, %ecx */
914 }
915
Jack Palevich1cdef202009-05-22 12:06:27 -0700916 virtual void pushR0() {
Jack Palevich21a15a22009-05-11 14:49:29 -0700917 o(0x50); /* push %eax */
918 }
919
Jack Palevich1cdef202009-05-22 12:06:27 -0700920 virtual void popR1() {
Jack Palevich21a15a22009-05-11 14:49:29 -0700921 o(0x59); /* pop %ecx */
Jack Palevichbf42c9c2009-05-12 12:48:35 -0700922 }
923
Jack Palevich1cdef202009-05-22 12:06:27 -0700924 virtual void storeR0ToR1(bool isInt) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700925 o(0x0188 + isInt); /* movl %eax/%al, (%ecx) */
926 }
927
Jack Palevich1cdef202009-05-22 12:06:27 -0700928 virtual void loadR0FromR0(bool isInt) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700929 if (isInt)
930 o(0x8b); /* mov (%eax), %eax */
931 else
932 o(0xbe0f); /* movsbl (%eax), %eax */
933 ob(0); /* add zero in code */
934 }
935
Jack Palevich1cdef202009-05-22 12:06:27 -0700936 virtual void leaR0(int ea) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700937 gmov(10, ea); /* leal EA, %eax */
938 }
939
Jack Palevich1cdef202009-05-22 12:06:27 -0700940 virtual void storeR0(int ea) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700941 gmov(6, ea); /* mov %eax, EA */
942 }
943
Jack Palevich1cdef202009-05-22 12:06:27 -0700944 virtual void loadR0(int ea, bool isIncDec, int op) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700945 gmov(8, ea); /* mov EA, %eax */
Jack Palevich4d93f302009-05-15 13:30:00 -0700946 if (isIncDec) {
947 /* Implement post-increment or post decrement.
948 */
949 gmov(0, ea); /* 83 ADD */
950 o(decodeOp(op));
951 }
Jack Palevich21a15a22009-05-11 14:49:29 -0700952 }
953
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700954 virtual int beginFunctionCallArguments() {
Jack Palevich21a15a22009-05-11 14:49:29 -0700955 return oad(0xec81, 0); /* sub $xxx, %esp */
956 }
957
Jack Palevich1cdef202009-05-22 12:06:27 -0700958 virtual void storeR0ToArg(int l) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700959 oad(0x248489, l); /* movl %eax, xxx(%esp) */
960 }
961
Jack Palevich7810bc92009-05-15 14:31:47 -0700962 virtual void endFunctionCallArguments(int a, int l) {
963 * (int*) a = l;
964 }
965
Jack Palevich22305132009-05-13 10:58:45 -0700966 virtual int callForward(int symbol) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700967 return psym(0xe8, symbol); /* call xxx */
968 }
969
Jack Palevich22305132009-05-13 10:58:45 -0700970 virtual void callRelative(int t) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700971 psym(0xe8, t); /* call xxx */
972 }
973
Jack Palevich22305132009-05-13 10:58:45 -0700974 virtual void callIndirect(int l) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700975 oad(0x2494ff, l); /* call *xxx(%esp) */
976 }
977
Jack Palevich7810bc92009-05-15 14:31:47 -0700978 virtual void adjustStackAfterCall(int l, bool isIndirect) {
979 if (isIndirect) {
980 l += 4;
981 }
Jack Palevich21a15a22009-05-11 14:49:29 -0700982 oad(0xc481, l); /* add $xxx, %esp */
983 }
984
Jack Palevicha6535612009-05-13 16:24:17 -0700985 virtual int jumpOffset() {
986 return 5;
987 }
988
989 virtual int disassemble(FILE* out) {
Jack Palevich1cdef202009-05-22 12:06:27 -0700990 return 0;
Jack Palevicha6535612009-05-13 16:24:17 -0700991 }
992
Jack Paleviche7b59062009-05-19 17:12:17 -0700993 /* output a symbol and patch all calls to it */
994 virtual void gsym(int t) {
995 int n;
996 int pc = getPC();
997 while (t) {
998 n = *(int *) t; /* next value */
999 *(int *) t = pc - t - 4;
1000 t = n;
1001 }
1002 }
1003
Jack Palevich1cdef202009-05-22 12:06:27 -07001004 virtual int finishCompile() {
Jack Palevich8dc662e2009-06-09 22:53:47 +00001005 size_t pagesize = 4096;
1006 size_t base = (size_t) getBase() & ~ (pagesize - 1);
1007 size_t top = ((size_t) getPC() + pagesize - 1) & ~ (pagesize - 1);
1008 int err = mprotect((void*) base, top - base, PROT_READ | PROT_WRITE | PROT_EXEC);
1009 if (err) {
1010 error("mprotect() failed: %d", errno);
1011 }
1012 return err;
Jack Palevich1cdef202009-05-22 12:06:27 -07001013 }
1014
Jack Palevich21a15a22009-05-11 14:49:29 -07001015 private:
Jack Paleviche7b59062009-05-19 17:12:17 -07001016
1017 /** Output 1 to 4 bytes.
1018 *
1019 */
1020 void o(int n) {
1021 /* cannot use unsigned, so we must do a hack */
1022 while (n && n != -1) {
1023 ob(n & 0xff);
1024 n = n >> 8;
1025 }
1026 }
1027
1028 /* psym is used to put an instruction with a data field which is a
1029 reference to a symbol. It is in fact the same as oad ! */
1030 int psym(int n, int t) {
1031 return oad(n, t);
1032 }
1033
1034 /* instruction + address */
1035 int oad(int n, int t) {
1036 o(n);
1037 int result = getPC();
1038 o4(t);
1039 return result;
1040 }
1041
1042
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001043 static const int operatorHelper[];
1044
1045 int decodeOp(int op) {
1046 if (op < 0 || op > OP_COUNT) {
Jack Palevichac0e95e2009-05-29 13:53:44 -07001047 error("Out-of-range operator: %d\n", op);
Jack Palevich0a280a02009-06-11 10:53:51 -07001048 op = 0;
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001049 }
1050 return operatorHelper[op];
1051 }
Jack Palevich21a15a22009-05-11 14:49:29 -07001052
Jack Palevich546b2242009-05-13 15:10:04 -07001053 void gmov(int l, int t) {
Jack Palevich21a15a22009-05-11 14:49:29 -07001054 o(l + 0x83);
Jack Palevich8dc662e2009-06-09 22:53:47 +00001055 oad((t > -LOCAL && t < LOCAL) << 7 | 5, t);
Jack Palevich21a15a22009-05-11 14:49:29 -07001056 }
1057 };
1058
Jack Paleviche7b59062009-05-19 17:12:17 -07001059#endif // PROVIDE_X86_CODEGEN
1060
Jack Palevich1cdef202009-05-22 12:06:27 -07001061 class InputStream {
1062 public:
Jack Palevicheedf9d22009-06-04 16:23:40 -07001063 int getChar() {
1064 if (bumpLine) {
1065 line++;
1066 bumpLine = false;
1067 }
1068 int ch = get();
1069 if (ch == '\n') {
1070 bumpLine = true;
1071 }
1072 return ch;
1073 }
1074 int getLine() {
1075 return line;
1076 }
1077 protected:
1078 InputStream() :
1079 line(1), bumpLine(false) {
1080 }
1081 private:
Jack Palevich1cdef202009-05-22 12:06:27 -07001082 virtual int get() = 0;
Jack Palevicheedf9d22009-06-04 16:23:40 -07001083 int line;
1084 bool bumpLine;
Jack Palevich1cdef202009-05-22 12:06:27 -07001085 };
1086
1087 class FileInputStream : public InputStream {
1088 public:
1089 FileInputStream(FILE* in) : f(in) {}
Jack Palevich1cdef202009-05-22 12:06:27 -07001090 private:
Jack Palevicheedf9d22009-06-04 16:23:40 -07001091 virtual int get() { return fgetc(f); }
Jack Palevich1cdef202009-05-22 12:06:27 -07001092 FILE* f;
1093 };
1094
1095 class TextInputStream : public InputStream {
1096 public:
1097 TextInputStream(const char* text, size_t textLength)
1098 : pText(text), mTextLength(textLength), mPosition(0) {
1099 }
Jack Palevicheedf9d22009-06-04 16:23:40 -07001100
1101 private:
Jack Palevich1cdef202009-05-22 12:06:27 -07001102 virtual int get() {
1103 return mPosition < mTextLength ? pText[mPosition++] : EOF;
1104 }
Jack Palevich1cdef202009-05-22 12:06:27 -07001105
Jack Palevich1cdef202009-05-22 12:06:27 -07001106 const char* pText;
1107 size_t mTextLength;
1108 size_t mPosition;
1109 };
1110
Jack Palevicheedf9d22009-06-04 16:23:40 -07001111 class String {
1112 public:
1113 String() {
1114 mpBase = 0;
1115 mUsed = 0;
1116 mSize = 0;
1117 }
1118
Jack Palevich2d11dfb2009-06-08 14:34:26 -07001119 String(char* item, int len, bool adopt) {
1120 if (adopt) {
1121 mpBase = item;
1122 mUsed = len;
1123 mSize = len + 1;
1124 } else {
1125 mpBase = 0;
1126 mUsed = 0;
1127 mSize = 0;
1128 appendBytes(item, len);
1129 }
1130 }
1131
Jack Palevicheedf9d22009-06-04 16:23:40 -07001132 ~String() {
1133 if (mpBase) {
1134 free(mpBase);
1135 }
1136 }
1137
Jack Palevich2d11dfb2009-06-08 14:34:26 -07001138 inline char* getUnwrapped() {
Jack Palevicheedf9d22009-06-04 16:23:40 -07001139 return mpBase;
1140 }
1141
1142 void appendCStr(const char* s) {
Jack Palevich2d11dfb2009-06-08 14:34:26 -07001143 appendBytes(s, strlen(s));
1144 }
1145
1146 void appendBytes(const char* s, int n) {
Jack Palevicheedf9d22009-06-04 16:23:40 -07001147 memcpy(ensure(n), s, n + 1);
1148 }
1149
1150 void append(char c) {
1151 * ensure(1) = c;
1152 }
1153
Jack Palevich2d11dfb2009-06-08 14:34:26 -07001154 char* orphan() {
1155 char* result = mpBase;
1156 mpBase = 0;
1157 mUsed = 0;
1158 mSize = 0;
1159 return result;
1160 }
1161
Jack Palevicheedf9d22009-06-04 16:23:40 -07001162 void printf(const char* fmt,...) {
1163 va_list ap;
1164 va_start(ap, fmt);
1165 vprintf(fmt, ap);
1166 va_end(ap);
1167 }
1168
1169 void vprintf(const char* fmt, va_list ap) {
1170 char* temp;
1171 int numChars = vasprintf(&temp, fmt, ap);
1172 memcpy(ensure(numChars), temp, numChars+1);
1173 free(temp);
1174 }
1175
Jack Palevich2d11dfb2009-06-08 14:34:26 -07001176 inline size_t len() {
Jack Palevicheedf9d22009-06-04 16:23:40 -07001177 return mUsed;
1178 }
1179
1180 private:
1181 char* ensure(int n) {
1182 size_t newUsed = mUsed + n;
1183 if (newUsed > mSize) {
1184 size_t newSize = mSize * 2 + 10;
1185 if (newSize < newUsed) {
1186 newSize = newUsed;
1187 }
1188 mpBase = (char*) realloc(mpBase, newSize + 1);
1189 mSize = newSize;
1190 }
1191 mpBase[newUsed] = '\0';
1192 char* result = mpBase + mUsed;
1193 mUsed = newUsed;
1194 return result;
1195 }
1196
1197 char* mpBase;
1198 size_t mUsed;
1199 size_t mSize;
1200 };
1201
Jack Palevich2d11dfb2009-06-08 14:34:26 -07001202 /**
1203 * Wrap an externally allocated string for use as a hash key.
1204 */
1205 class FakeString : public String {
1206 public:
1207 FakeString(char* string, size_t length) :
1208 String(string, length, true) {}
1209
1210 ~FakeString() {
1211 orphan();
1212 }
1213 };
1214
1215 template<class V> class StringTable {
1216 public:
1217 StringTable(size_t initialCapacity) {
1218 mpMap = hashmapCreate(initialCapacity, hashFn, equalsFn);
1219 }
1220
1221 ~StringTable() {
1222 clear();
1223 }
1224
1225 void clear() {
1226 hashmapForEach(mpMap, freeKeyValue, this);
1227 }
1228
1229 bool contains(String* pKey) {
1230 bool result = hashmapContainsKey(mpMap, pKey);
1231 return result;
1232 }
1233
1234 V* get(String* pKey) {
1235 V* result = (V*) hashmapGet(mpMap, pKey);
1236 return result;
1237 }
1238
1239 V* remove(String* pKey) {
1240 V* result = (V*) hashmapRemove(mpMap, pKey);
1241 return result;
1242 }
1243
1244 V* put(String* pKey, V* value) {
1245 V* result = (V*) hashmapPut(mpMap, pKey, value);
1246 if (result) {
1247 // The key was not adopted by the map, so delete it here.
1248 delete pKey;
1249 }
1250 return result;
1251 }
1252
1253 protected:
1254 static int hashFn(void* pKey) {
1255 String* pString = (String*) pKey;
1256 return hashmapHash(pString->getUnwrapped(), pString->len());
1257 }
1258
1259 static bool equalsFn(void* keyA, void* keyB) {
1260 String* pStringA = (String*) keyA;
1261 String* pStringB = (String*) keyB;
1262 return pStringA->len() == pStringB->len()
1263 && strcmp(pStringA->getUnwrapped(), pStringB->getUnwrapped())
1264 == 0;
1265 }
1266
1267 static bool freeKeyValue(void* key, void* value, void* context) {
1268 delete (String*) key;
1269 delete (V*) value;
1270 return true;
1271 }
1272
1273 Hashmap* mpMap;
1274 };
1275
1276 class MacroTable : public StringTable<String> {
1277 public:
1278 MacroTable() : StringTable<String>(10) {}
1279 };
1280
1281 template<class E> class Array {
1282 public:
1283 Array() {
1284 mpBase = 0;
1285 mUsed = 0;
1286 mSize = 0;
1287 }
1288
1289 ~Array() {
1290 if (mpBase) {
1291 free(mpBase);
1292 }
1293 }
1294
1295 E get(int i) {
1296 if (i < 0 || i > mUsed) {
1297 error("internal error: Index out of range");
1298 return E();
1299 }
1300 return mpBase[i];
1301 }
1302
1303 void set(int i, E val) {
1304 mpBase[i] = val;
1305 }
1306
1307 void pop() {
1308 if (mUsed > 0) {
1309 mUsed -= 1;
Jack Palevich36d94142009-06-08 15:55:32 -07001310 } else {
1311 error("internal error: Popped empty stack.");
Jack Palevich2d11dfb2009-06-08 14:34:26 -07001312 }
1313 }
1314
1315 void push(E item) {
1316 * ensure(1) = item;
1317 }
1318
1319 size_t len() {
1320 return mUsed;
1321 }
1322
1323 private:
1324 E* ensure(int n) {
1325 size_t newUsed = mUsed + n;
1326 if (newUsed > mSize) {
1327 size_t newSize = mSize * 2 + 10;
1328 if (newSize < newUsed) {
1329 newSize = newUsed;
1330 }
1331 mpBase = (E*) realloc(mpBase, sizeof(E) * newSize);
1332 mSize = newSize;
1333 }
1334 E* result = mpBase + mUsed;
1335 mUsed = newUsed;
1336 return result;
1337 }
1338
1339 E* mpBase;
1340 size_t mUsed;
1341 size_t mSize;
1342 };
1343
Jack Palevich36d94142009-06-08 15:55:32 -07001344 struct InputState {
1345 InputStream* pStream;
1346 int oldCh;
1347 };
1348
1349
1350 int ch; // Current input character, or EOF
1351 intptr_t tok; // token
1352 intptr_t tokc; // token extra info
1353 int tokl; // token operator level
1354 intptr_t rsym; // return symbol
1355 intptr_t loc; // local variable index
1356 char* glo; // global variable index
1357 char* sym_stk;
1358 char* dstk; // Define stack
1359 char* dptr; // Macro state: Points to macro text during macro playback.
1360 int dch; // Macro state: Saves old value of ch during a macro playback.
1361 char* last_id;
1362 char* pGlobalBase;
1363 char* pVarsBase; // Value of variables
1364
1365 InputStream* file;
1366
1367 CodeBuf codeBuf;
1368 CodeGenerator* pGen;
1369
Jack Palevich2d11dfb2009-06-08 14:34:26 -07001370 MacroTable mMacros;
Jack Palevich36d94142009-06-08 15:55:32 -07001371 Array<InputState> mInputStateStack;
Jack Palevich2d11dfb2009-06-08 14:34:26 -07001372
Jack Palevicheedf9d22009-06-04 16:23:40 -07001373 String mErrorBuf;
1374
Jack Palevicheedf9d22009-06-04 16:23:40 -07001375 String mPragmas;
1376 int mPragmaStringCount;
1377
Jack Palevich21a15a22009-05-11 14:49:29 -07001378 static const int ALLOC_SIZE = 99999;
1379
Jack Palevicheedf9d22009-06-04 16:23:40 -07001380 // Indentifiers start at 0x100 and increase by # (chars + 1) * 8
Jack Palevich21a15a22009-05-11 14:49:29 -07001381 static const int TOK_IDENT = 0x100;
1382 static const int TOK_INT = 0x100;
Jack Palevichb7c81e92009-06-04 19:56:13 -07001383 static const int TOK_CHAR = TOK_INT + 4*8;
1384 static const int TOK_VOID = TOK_CHAR + 5*8;
1385 static const int TOK_IF = TOK_VOID + 5*8;
1386 static const int TOK_ELSE = TOK_IF + 3*8;
1387 static const int TOK_WHILE = TOK_ELSE + 5*8;
1388 static const int TOK_BREAK = TOK_WHILE + 6*8;
1389 static const int TOK_RETURN = TOK_BREAK + 6*8;
1390 static const int TOK_FOR = TOK_RETURN + 7*8;
1391 static const int TOK_PRAGMA = TOK_FOR + 4*8;
1392 static const int TOK_DEFINE = TOK_PRAGMA + 7*8;
1393 static const int TOK_MAIN = TOK_DEFINE + 7*8;
Jack Palevich21a15a22009-05-11 14:49:29 -07001394
1395 static const int TOK_DUMMY = 1;
1396 static const int TOK_NUM = 2;
1397
1398 static const int LOCAL = 0x200;
1399
1400 static const int SYM_FORWARD = 0;
1401 static const int SYM_DEFINE = 1;
1402
1403 /* tokens in string heap */
1404 static const int TAG_TOK = ' ';
Jack Palevich21a15a22009-05-11 14:49:29 -07001405
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001406 static const int OP_INCREMENT = 0;
1407 static const int OP_DECREMENT = 1;
1408 static const int OP_MUL = 2;
1409 static const int OP_DIV = 3;
1410 static const int OP_MOD = 4;
1411 static const int OP_PLUS = 5;
1412 static const int OP_MINUS = 6;
1413 static const int OP_SHIFT_LEFT = 7;
1414 static const int OP_SHIFT_RIGHT = 8;
1415 static const int OP_LESS_EQUAL = 9;
1416 static const int OP_GREATER_EQUAL = 10;
1417 static const int OP_LESS = 11;
1418 static const int OP_GREATER = 12;
1419 static const int OP_EQUALS = 13;
1420 static const int OP_NOT_EQUALS = 14;
1421 static const int OP_LOGICAL_AND = 15;
1422 static const int OP_LOGICAL_OR = 16;
1423 static const int OP_BIT_AND = 17;
1424 static const int OP_BIT_XOR = 18;
1425 static const int OP_BIT_OR = 19;
1426 static const int OP_BIT_NOT = 20;
1427 static const int OP_LOGICAL_NOT = 21;
1428 static const int OP_COUNT = 22;
1429
1430 /* Operators are searched from front, the two-character operators appear
1431 * before the single-character operators with the same first character.
1432 * @ is used to pad out single-character operators.
1433 */
1434 static const char* operatorChars;
1435 static const char operatorLevel[];
1436
Jack Palevich21a15a22009-05-11 14:49:29 -07001437 void pdef(int t) {
Jack Palevichf1f39cc2009-05-29 18:03:15 -07001438 if (dstk - sym_stk >= ALLOC_SIZE) {
1439 error("Symbol table exhausted");
Jack Palevich0a280a02009-06-11 10:53:51 -07001440 return;
Jack Palevichf1f39cc2009-05-29 18:03:15 -07001441 }
Jack Palevich653f42d2009-05-28 17:15:32 -07001442 *dstk++ = t;
Jack Palevich21a15a22009-05-11 14:49:29 -07001443 }
1444
1445 void inp() {
1446 if (dptr) {
Jack Palevich653f42d2009-05-28 17:15:32 -07001447 ch = *dptr++;
Jack Palevich2d11dfb2009-06-08 14:34:26 -07001448 if (ch == 0) {
Jack Palevich21a15a22009-05-11 14:49:29 -07001449 dptr = 0;
1450 ch = dch;
1451 }
1452 } else
Jack Palevicheedf9d22009-06-04 16:23:40 -07001453 ch = file->getChar();
Jack Palevichb7c81e92009-06-04 19:56:13 -07001454#if 0
1455 printf("ch='%c' 0x%x\n", ch, ch);
1456#endif
Jack Palevich21a15a22009-05-11 14:49:29 -07001457 }
1458
1459 int isid() {
Jack Palevich546b2242009-05-13 15:10:04 -07001460 return isalnum(ch) | (ch == '_');
Jack Palevich21a15a22009-05-11 14:49:29 -07001461 }
1462
1463 /* read a character constant */
1464 void getq() {
1465 if (ch == '\\') {
1466 inp();
1467 if (ch == 'n')
1468 ch = '\n';
1469 }
1470 }
1471
1472 void next() {
1473 int l, a;
1474
Jack Palevich546b2242009-05-13 15:10:04 -07001475 while (isspace(ch) | (ch == '#')) {
Jack Palevich21a15a22009-05-11 14:49:29 -07001476 if (ch == '#') {
1477 inp();
1478 next();
1479 if (tok == TOK_DEFINE) {
Jack Palevich2d11dfb2009-06-08 14:34:26 -07001480 doDefine();
Jack Palevicheedf9d22009-06-04 16:23:40 -07001481 } else if (tok == TOK_PRAGMA) {
1482 doPragma();
1483 } else {
1484 error("Unsupported preprocessor directive \"%s\"", last_id);
Jack Palevich21a15a22009-05-11 14:49:29 -07001485 }
Jack Palevich21a15a22009-05-11 14:49:29 -07001486 }
1487 inp();
1488 }
1489 tokl = 0;
1490 tok = ch;
1491 /* encode identifiers & numbers */
1492 if (isid()) {
1493 pdef(TAG_TOK);
1494 last_id = dstk;
1495 while (isid()) {
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001496 pdef(ch);
1497 inp();
Jack Palevichae54f1f2009-05-08 14:54:15 -07001498 }
Jack Palevich21a15a22009-05-11 14:49:29 -07001499 if (isdigit(tok)) {
Jack Palevich653f42d2009-05-28 17:15:32 -07001500 tokc = strtol(last_id, 0, 0);
Jack Palevich21a15a22009-05-11 14:49:29 -07001501 tok = TOK_NUM;
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001502 } else {
Jack Palevichf1f39cc2009-05-29 18:03:15 -07001503 if (dstk - sym_stk + 1 > ALLOC_SIZE) {
1504 error("symbol stack overflow");
1505 }
Jack Palevich2d11dfb2009-06-08 14:34:26 -07001506 FakeString token(last_id, dstk-last_id);
1507 // Is this a macro?
1508 String* pValue = mMacros.get(&token);
1509 if (pValue) {
1510 // Yes, it is a macro
1511 dstk = last_id-1;
1512 dptr = pValue->getUnwrapped();
1513 dch = ch;
1514 inp();
1515 next();
1516 } else {
1517 * dstk = TAG_TOK; /* no need to mark end of string (we
1518 suppose data is initialized to zero by calloc) */
1519 tok = (intptr_t) (strstr(sym_stk, (last_id - 1))
1520 - sym_stk);
1521 * dstk = 0; /* mark real end of ident for dlsym() */
1522 tok = tok * 8 + TOK_IDENT;
1523 if (tok > TOK_DEFINE) {
1524 if (tok + 8 > ALLOC_SIZE) {
1525 error("Variable Table overflow.");
1526 }
1527 tok = (intptr_t) (pVarsBase + tok);
1528 /* printf("tok=%s %x\n", last_id, tok); */
Jack Palevich21a15a22009-05-11 14:49:29 -07001529 }
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001530 }
1531 }
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001532 } else {
Jack Palevich21a15a22009-05-11 14:49:29 -07001533 inp();
1534 if (tok == '\'') {
1535 tok = TOK_NUM;
1536 getq();
1537 tokc = ch;
1538 inp();
1539 inp();
Jack Palevich546b2242009-05-13 15:10:04 -07001540 } else if ((tok == '/') & (ch == '*')) {
Jack Palevich21a15a22009-05-11 14:49:29 -07001541 inp();
1542 while (ch) {
1543 while (ch != '*')
1544 inp();
1545 inp();
1546 if (ch == '/')
1547 ch = 0;
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001548 }
Jack Palevich21a15a22009-05-11 14:49:29 -07001549 inp();
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001550 next();
Jack Palevichbd894902009-05-14 19:35:31 -07001551 } else if ((tok == '/') & (ch == '/')) {
1552 inp();
1553 while (ch && (ch != '\n')) {
1554 inp();
1555 }
1556 inp();
1557 next();
Jack Palevich21a15a22009-05-11 14:49:29 -07001558 } else {
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001559 const char* t = operatorChars;
1560 int opIndex = 0;
Jack Palevich546b2242009-05-13 15:10:04 -07001561 while ((l = *t++) != 0) {
Jack Palevich21a15a22009-05-11 14:49:29 -07001562 a = *t++;
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001563 tokl = operatorLevel[opIndex];
1564 tokc = opIndex;
Jack Palevich546b2242009-05-13 15:10:04 -07001565 if ((l == tok) & ((a == ch) | (a == '@'))) {
Jack Palevich21a15a22009-05-11 14:49:29 -07001566#if 0
1567 printf("%c%c -> tokl=%d tokc=0x%x\n",
1568 l, a, tokl, tokc);
1569#endif
1570 if (a == ch) {
1571 inp();
1572 tok = TOK_DUMMY; /* dummy token for double tokens */
1573 }
1574 break;
1575 }
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001576 opIndex++;
1577 }
1578 if (l == 0) {
1579 tokl = 0;
1580 tokc = 0;
Jack Palevich21a15a22009-05-11 14:49:29 -07001581 }
1582 }
1583 }
1584#if 0
1585 {
Jack Palevich653f42d2009-05-28 17:15:32 -07001586 char* p;
Jack Palevich21a15a22009-05-11 14:49:29 -07001587
1588 printf("tok=0x%x ", tok);
1589 if (tok >= TOK_IDENT) {
1590 printf("'");
1591 if (tok> TOK_DEFINE)
Jack Palevich653f42d2009-05-28 17:15:32 -07001592 p = sym_stk + 1 + ((char*) tok - pVarsBase - TOK_IDENT) / 8;
Jack Palevich21a15a22009-05-11 14:49:29 -07001593 else
1594 p = sym_stk + 1 + (tok - TOK_IDENT) / 8;
Jack Palevich653f42d2009-05-28 17:15:32 -07001595 while (*p != TAG_TOK && *p)
1596 printf("%c", *p++);
Jack Palevich21a15a22009-05-11 14:49:29 -07001597 printf("'\n");
1598 } else if (tok == TOK_NUM) {
1599 printf("%d\n", tokc);
1600 } else {
1601 printf("'%c'\n", tok);
1602 }
1603 }
1604#endif
1605 }
1606
Jack Palevich2d11dfb2009-06-08 14:34:26 -07001607 void doDefine() {
1608 String* pName = new String();
1609 while (isspace(ch)) {
1610 inp();
1611 }
1612 while (isid()) {
1613 pName->append(ch);
1614 inp();
1615 }
1616 if (ch == '(') {
1617 delete pName;
1618 error("Defines with arguments not supported");
Jack Palevich0a280a02009-06-11 10:53:51 -07001619 return;
Jack Palevich2d11dfb2009-06-08 14:34:26 -07001620 }
1621 while (isspace(ch)) {
1622 inp();
1623 }
1624 String* pValue = new String();
1625 while (ch != '\n' && ch != EOF) {
1626 pValue->append(ch);
1627 inp();
1628 }
1629 delete mMacros.put(pName, pValue);
1630 }
1631
Jack Palevicheedf9d22009-06-04 16:23:40 -07001632 void doPragma() {
1633 // # pragma name(val)
1634 int state = 0;
1635 while(ch != EOF && ch != '\n' && state < 10) {
1636 switch(state) {
1637 case 0:
1638 if (isspace(ch)) {
1639 inp();
1640 } else {
1641 state++;
1642 }
1643 break;
1644 case 1:
1645 if (isalnum(ch)) {
1646 mPragmas.append(ch);
1647 inp();
1648 } else if (ch == '(') {
1649 mPragmas.append(0);
1650 inp();
1651 state++;
1652 } else {
1653 state = 11;
1654 }
1655 break;
1656 case 2:
1657 if (isalnum(ch)) {
1658 mPragmas.append(ch);
1659 inp();
1660 } else if (ch == ')') {
1661 mPragmas.append(0);
1662 inp();
1663 state = 10;
1664 } else {
1665 state = 11;
1666 }
1667 break;
1668 }
1669 }
1670 if(state != 10) {
1671 error("Unexpected pragma syntax");
1672 }
1673 mPragmaStringCount += 2;
1674 }
Jack Palevich21a15a22009-05-11 14:49:29 -07001675
Jack Palevichac0e95e2009-05-29 13:53:44 -07001676 virtual void verror(const char* fmt, va_list ap) {
Jack Palevicheedf9d22009-06-04 16:23:40 -07001677 mErrorBuf.printf("%ld: ", file->getLine());
1678 mErrorBuf.vprintf(fmt, ap);
1679 mErrorBuf.printf("\n");
Jack Palevich21a15a22009-05-11 14:49:29 -07001680 }
1681
Jack Palevich8b0624c2009-05-20 12:12:06 -07001682 void skip(intptr_t c) {
Jack Palevich21a15a22009-05-11 14:49:29 -07001683 if (tok != c) {
1684 error("'%c' expected", c);
1685 }
1686 next();
1687 }
1688
Jack Palevich21a15a22009-05-11 14:49:29 -07001689 /* l is one if '=' parsing wanted (quick hack) */
Jack Palevich8b0624c2009-05-20 12:12:06 -07001690 void unary(intptr_t l) {
Jack Palevich653f42d2009-05-28 17:15:32 -07001691 intptr_t n, t, a;
1692 int c;
Jack Palevich546b2242009-05-13 15:10:04 -07001693 t = 0;
Jack Palevich21a15a22009-05-11 14:49:29 -07001694 n = 1; /* type of expression 0 = forward, 1 = value, other =
1695 lvalue */
1696 if (tok == '\"') {
Jack Palevich653f42d2009-05-28 17:15:32 -07001697 pGen->li((int) glo);
Jack Palevich21a15a22009-05-11 14:49:29 -07001698 while (ch != '\"') {
1699 getq();
Jack Palevichf1f39cc2009-05-29 18:03:15 -07001700 *allocGlobalSpace(1) = ch;
Jack Palevich21a15a22009-05-11 14:49:29 -07001701 inp();
1702 }
Jack Palevich653f42d2009-05-28 17:15:32 -07001703 *glo = 0;
Jack Palevichf1f39cc2009-05-29 18:03:15 -07001704 /* align heap */
1705 allocGlobalSpace((char*) (((intptr_t) glo + 4) & -4) - glo);
Jack Palevich21a15a22009-05-11 14:49:29 -07001706 inp();
1707 next();
1708 } else {
1709 c = tokl;
1710 a = tokc;
1711 t = tok;
1712 next();
1713 if (t == TOK_NUM) {
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001714 pGen->li(a);
Jack Palevich21a15a22009-05-11 14:49:29 -07001715 } else if (c == 2) {
1716 /* -, +, !, ~ */
1717 unary(0);
Jack Palevich1cdef202009-05-22 12:06:27 -07001718 pGen->clearR1();
Jack Palevich21a15a22009-05-11 14:49:29 -07001719 if (t == '!')
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001720 pGen->gcmp(a);
Jack Palevich21a15a22009-05-11 14:49:29 -07001721 else
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001722 pGen->genOp(a);
Jack Palevich21a15a22009-05-11 14:49:29 -07001723 } else if (t == '(') {
1724 expr();
1725 skip(')');
1726 } else if (t == '*') {
1727 /* parse cast */
1728 skip('(');
1729 t = tok; /* get type */
1730 next(); /* skip int/char/void */
1731 next(); /* skip '*' or '(' */
1732 if (tok == '*') {
1733 /* function type */
1734 skip('*');
1735 skip(')');
1736 skip('(');
1737 skip(')');
1738 t = 0;
1739 }
1740 skip(')');
1741 unary(0);
1742 if (tok == '=') {
1743 next();
Jack Palevich1cdef202009-05-22 12:06:27 -07001744 pGen->pushR0();
Jack Palevich21a15a22009-05-11 14:49:29 -07001745 expr();
Jack Palevich1cdef202009-05-22 12:06:27 -07001746 pGen->popR1();
1747 pGen->storeR0ToR1(t == TOK_INT);
Jack Palevich21a15a22009-05-11 14:49:29 -07001748 } else if (t) {
Jack Palevich1cdef202009-05-22 12:06:27 -07001749 pGen->loadR0FromR0(t == TOK_INT);
Jack Palevich21a15a22009-05-11 14:49:29 -07001750 }
1751 } else if (t == '&') {
Jack Palevich1cdef202009-05-22 12:06:27 -07001752 pGen->leaR0(*(int *) tok);
Jack Palevich21a15a22009-05-11 14:49:29 -07001753 next();
1754 } else {
1755 n = *(int *) t;
1756 /* forward reference: try dlsym */
Jack Palevichcb1c9ef2009-05-14 11:38:49 -07001757 if (!n) {
Jack Palevich653f42d2009-05-28 17:15:32 -07001758 n = (intptr_t) dlsym(RTLD_DEFAULT, last_id);
Jack Palevichcb1c9ef2009-05-14 11:38:49 -07001759 }
Jack Palevich546b2242009-05-13 15:10:04 -07001760 if ((tok == '=') & l) {
Jack Palevich21a15a22009-05-11 14:49:29 -07001761 /* assignment */
1762 next();
1763 expr();
Jack Palevich1cdef202009-05-22 12:06:27 -07001764 pGen->storeR0(n);
Jack Palevich21a15a22009-05-11 14:49:29 -07001765 } else if (tok != '(') {
1766 /* variable */
Jack Palevich1cdef202009-05-22 12:06:27 -07001767 pGen->loadR0(n, tokl == 11, tokc);
Jack Palevich21a15a22009-05-11 14:49:29 -07001768 if (tokl == 11) {
Jack Palevich21a15a22009-05-11 14:49:29 -07001769 next();
1770 }
1771 }
1772 }
1773 }
1774
1775 /* function call */
1776 if (tok == '(') {
1777 if (n == 1)
Jack Palevich1cdef202009-05-22 12:06:27 -07001778 pGen->pushR0();
Jack Palevich21a15a22009-05-11 14:49:29 -07001779
1780 /* push args and invert order */
Jack Palevichcb1c9ef2009-05-14 11:38:49 -07001781 a = pGen->beginFunctionCallArguments();
Jack Palevich21a15a22009-05-11 14:49:29 -07001782 next();
1783 l = 0;
1784 while (tok != ')') {
1785 expr();
Jack Palevich1cdef202009-05-22 12:06:27 -07001786 pGen->storeR0ToArg(l);
Jack Palevichbbf8ab52009-05-11 11:54:30 -07001787 if (tok == ',')
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001788 next();
Jack Palevich21a15a22009-05-11 14:49:29 -07001789 l = l + 4;
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001790 }
Jack Palevichcb1c9ef2009-05-14 11:38:49 -07001791 pGen->endFunctionCallArguments(a, l);
Jack Palevich21a15a22009-05-11 14:49:29 -07001792 next();
1793 if (!n) {
1794 /* forward reference */
1795 t = t + 4;
1796 *(int *) t = pGen->callForward(*(int *) t);
1797 } else if (n == 1) {
1798 pGen->callIndirect(l);
Jack Palevich21a15a22009-05-11 14:49:29 -07001799 } else {
Jack Palevich7810bc92009-05-15 14:31:47 -07001800 pGen->callRelative(n - codeBuf.getPC() - pGen->jumpOffset());
Jack Palevich21a15a22009-05-11 14:49:29 -07001801 }
Jack Palevich3d474a72009-05-15 15:12:38 -07001802 if (l | (n == 1))
Jack Palevich7810bc92009-05-15 14:31:47 -07001803 pGen->adjustStackAfterCall(l, n == 1);
Jack Palevich21a15a22009-05-11 14:49:29 -07001804 }
1805 }
1806
Jack Palevich653f42d2009-05-28 17:15:32 -07001807 void sum(int l) {
Jack Palevich8b0624c2009-05-20 12:12:06 -07001808 intptr_t t, n, a;
Jack Palevich546b2242009-05-13 15:10:04 -07001809 t = 0;
Jack Palevich21a15a22009-05-11 14:49:29 -07001810 if (l-- == 1)
1811 unary(1);
1812 else {
1813 sum(l);
1814 a = 0;
1815 while (l == tokl) {
1816 n = tok;
1817 t = tokc;
1818 next();
1819
1820 if (l > 8) {
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001821 a = pGen->gtst(t == OP_LOGICAL_OR, a); /* && and || output code generation */
Jack Palevich21a15a22009-05-11 14:49:29 -07001822 sum(l);
1823 } else {
Jack Palevich1cdef202009-05-22 12:06:27 -07001824 pGen->pushR0();
Jack Palevich21a15a22009-05-11 14:49:29 -07001825 sum(l);
Jack Palevich1cdef202009-05-22 12:06:27 -07001826 pGen->popR1();
Jack Palevich21a15a22009-05-11 14:49:29 -07001827
Jack Palevich546b2242009-05-13 15:10:04 -07001828 if ((l == 4) | (l == 5)) {
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001829 pGen->gcmp(t);
Jack Palevich21a15a22009-05-11 14:49:29 -07001830 } else {
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001831 pGen->genOp(t);
Jack Palevich21a15a22009-05-11 14:49:29 -07001832 }
1833 }
1834 }
1835 /* && and || output code generation */
1836 if (a && l > 8) {
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001837 a = pGen->gtst(t == OP_LOGICAL_OR, a);
1838 pGen->li(t != OP_LOGICAL_OR);
Jack Palevicha6535612009-05-13 16:24:17 -07001839 pGen->gjmp(5); /* jmp $ + 5 (sizeof li, FIXME for ARM) */
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001840 pGen->gsym(a);
1841 pGen->li(t == OP_LOGICAL_OR);
Jack Palevich21a15a22009-05-11 14:49:29 -07001842 }
1843 }
1844 }
1845
1846 void expr() {
1847 sum(11);
1848 }
1849
1850 int test_expr() {
1851 expr();
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001852 return pGen->gtst(0, 0);
Jack Palevich21a15a22009-05-11 14:49:29 -07001853 }
1854
Jack Palevich8b0624c2009-05-20 12:12:06 -07001855 void block(intptr_t l) {
1856 intptr_t a, n, t;
Jack Palevich21a15a22009-05-11 14:49:29 -07001857
1858 if (tok == TOK_IF) {
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001859 next();
1860 skip('(');
Jack Palevich21a15a22009-05-11 14:49:29 -07001861 a = test_expr();
1862 skip(')');
1863 block(l);
1864 if (tok == TOK_ELSE) {
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001865 next();
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001866 n = pGen->gjmp(0); /* jmp */
1867 pGen->gsym(a);
Jack Palevich21a15a22009-05-11 14:49:29 -07001868 block(l);
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001869 pGen->gsym(n); /* patch else jmp */
Jack Palevich21a15a22009-05-11 14:49:29 -07001870 } else {
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001871 pGen->gsym(a); /* patch if test */
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001872 }
Jack Palevich546b2242009-05-13 15:10:04 -07001873 } else if ((tok == TOK_WHILE) | (tok == TOK_FOR)) {
Jack Palevich21a15a22009-05-11 14:49:29 -07001874 t = tok;
1875 next();
1876 skip('(');
1877 if (t == TOK_WHILE) {
Jack Palevicha6535612009-05-13 16:24:17 -07001878 n = codeBuf.getPC(); // top of loop, target of "next" iteration
Jack Palevich21a15a22009-05-11 14:49:29 -07001879 a = test_expr();
1880 } else {
1881 if (tok != ';')
1882 expr();
1883 skip(';');
1884 n = codeBuf.getPC();
1885 a = 0;
1886 if (tok != ';')
1887 a = test_expr();
1888 skip(';');
1889 if (tok != ')') {
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001890 t = pGen->gjmp(0);
Jack Palevich21a15a22009-05-11 14:49:29 -07001891 expr();
Jack Palevicha6535612009-05-13 16:24:17 -07001892 pGen->gjmp(n - codeBuf.getPC() - pGen->jumpOffset());
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001893 pGen->gsym(t);
Jack Palevich21a15a22009-05-11 14:49:29 -07001894 n = t + 4;
1895 }
1896 }
1897 skip(')');
Jack Palevich8b0624c2009-05-20 12:12:06 -07001898 block((intptr_t) &a);
Jack Palevicha6535612009-05-13 16:24:17 -07001899 pGen->gjmp(n - codeBuf.getPC() - pGen->jumpOffset()); /* jmp */
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001900 pGen->gsym(a);
Jack Palevich21a15a22009-05-11 14:49:29 -07001901 } else if (tok == '{') {
1902 next();
1903 /* declarations */
Jack Palevichb7c81e92009-06-04 19:56:13 -07001904 localDeclarations();
Jack Palevich21a15a22009-05-11 14:49:29 -07001905 while (tok != '}')
1906 block(l);
1907 next();
1908 } else {
1909 if (tok == TOK_RETURN) {
1910 next();
1911 if (tok != ';')
1912 expr();
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001913 rsym = pGen->gjmp(rsym); /* jmp */
Jack Palevich21a15a22009-05-11 14:49:29 -07001914 } else if (tok == TOK_BREAK) {
1915 next();
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001916 *(int *) l = pGen->gjmp(*(int *) l);
Jack Palevich21a15a22009-05-11 14:49:29 -07001917 } else if (tok != ';')
1918 expr();
1919 skip(';');
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001920 }
1921 }
Jack Palevich21a15a22009-05-11 14:49:29 -07001922
Jack Palevichb7c81e92009-06-04 19:56:13 -07001923 typedef int Type;
1924 static const Type TY_UNKNOWN = 0;
1925 static const Type TY_INT = 1;
1926 static const Type TY_CHAR = 2;
1927 static const Type TY_VOID = 3;
1928 static const int TY_BASE_TYPE_MASK = 0xf;
1929 static const int TY_INDIRECTION_MASK = 0xf0;
1930 static const int TY_INDIRECTION_SHIFT = 4;
1931 static const int MAX_INDIRECTION_COUNT = 15;
Jack Palevich21a15a22009-05-11 14:49:29 -07001932
Jack Palevichb7c81e92009-06-04 19:56:13 -07001933 Type getBaseType(Type t) {
1934 return t & TY_BASE_TYPE_MASK;
1935 }
1936
1937 int getIndirectionCount(Type t) {
1938 return (TY_INDIRECTION_MASK & t) >> TY_INDIRECTION_SHIFT;
1939 }
1940
1941 void setIndirectionCount(Type& t, int count) {
1942 t = ((TY_INDIRECTION_MASK & (count << TY_INDIRECTION_SHIFT))
1943 | (t & ~TY_INDIRECTION_MASK));
1944 }
1945
1946 bool acceptType(Type& t) {
1947 t = TY_UNKNOWN;
1948 if (tok == TOK_INT) {
1949 t = TY_INT;
1950 } else if (tok == TOK_CHAR) {
1951 t = TY_CHAR;
1952 } else if (tok == TOK_VOID) {
1953 t = TY_VOID;
1954 } else {
1955 return false;
1956 }
1957 next();
1958 return true;
1959 }
1960
1961 Type acceptPointerDeclaration(Type& base) {
1962 Type t = base;
1963 int indirectionCount = 0;
1964 while (tok == '*' && indirectionCount <= MAX_INDIRECTION_COUNT) {
1965 next();
1966 indirectionCount++;
1967 }
1968 if (indirectionCount > MAX_INDIRECTION_COUNT) {
1969 error("Too many levels of pointer. Max %d", MAX_INDIRECTION_COUNT);
1970 }
1971 setIndirectionCount(t, indirectionCount);
1972 return t;
1973 }
1974
1975 void expectType(Type& t) {
1976 if (!acceptType(t)) {
1977 error("Expected a type.");
1978 }
1979 }
1980
1981 void checkSymbol() {
1982 if (tok <= TOK_DEFINE) {
1983 error("Expected a symbol");
1984 }
1985 }
1986
1987 void localDeclarations() {
1988 intptr_t a;
1989 Type base;
1990
1991 while (acceptType(base)) {
1992 while (tok != ';') {
1993 Type t = acceptPointerDeclaration(t);
1994 checkSymbol();
1995 loc = loc + 4;
1996 *(int *) tok = -loc;
1997
Jack Palevich21a15a22009-05-11 14:49:29 -07001998 next();
Jack Palevichb7c81e92009-06-04 19:56:13 -07001999 if (tok == ',')
2000 next();
2001 }
2002 skip(';');
2003 }
2004 }
2005
2006 void globalDeclarations() {
2007 while (tok != EOF) {
2008 Type base;
2009 expectType(base);
2010 Type t = acceptPointerDeclaration(t);
2011 checkSymbol();
2012 int name = tok;
2013 next();
2014 if (tok == ',' || tok == ';') {
2015 // it's a variable declaration
2016 for(;;) {
2017 *(int* *) name = (int*) allocGlobalSpace(4);
2018 if (tok != ',') {
2019 break;
Jack Palevich21a15a22009-05-11 14:49:29 -07002020 }
2021 next();
Jack Palevichb7c81e92009-06-04 19:56:13 -07002022 t = acceptPointerDeclaration(t);
2023 checkSymbol();
2024 name = tok;
2025 next();
Jack Palevich21a15a22009-05-11 14:49:29 -07002026 }
2027 skip(';');
2028 } else {
Jack Palevichb7c81e92009-06-04 19:56:13 -07002029 /* patch forward references (XXX: does not work for function
Jack Palevich21a15a22009-05-11 14:49:29 -07002030 pointers) */
Jack Palevichb7c81e92009-06-04 19:56:13 -07002031 pGen->gsym(*(int *) (name + 4));
Jack Palevich21a15a22009-05-11 14:49:29 -07002032 /* put function address */
Jack Palevichb7c81e92009-06-04 19:56:13 -07002033 *(int *) name = codeBuf.getPC();
Jack Palevich21a15a22009-05-11 14:49:29 -07002034 skip('(');
Jack Palevichb7c81e92009-06-04 19:56:13 -07002035 intptr_t a = 8;
Jack Palevich546b2242009-05-13 15:10:04 -07002036 int argCount = 0;
Jack Palevich21a15a22009-05-11 14:49:29 -07002037 while (tok != ')') {
Jack Palevichb7c81e92009-06-04 19:56:13 -07002038 Type aType;
2039 expectType(aType);
2040 aType = acceptPointerDeclaration(aType);
2041 checkSymbol();
Jack Palevich21a15a22009-05-11 14:49:29 -07002042 /* read param name and compute offset */
2043 *(int *) tok = a;
2044 a = a + 4;
2045 next();
2046 if (tok == ',')
2047 next();
Jack Palevich546b2242009-05-13 15:10:04 -07002048 argCount++;
Jack Palevich21a15a22009-05-11 14:49:29 -07002049 }
Jack Palevichb7c81e92009-06-04 19:56:13 -07002050 skip(')'); /* skip ')' */
Jack Palevich21a15a22009-05-11 14:49:29 -07002051 rsym = loc = 0;
Jack Palevich546b2242009-05-13 15:10:04 -07002052 a = pGen->functionEntry(argCount);
Jack Palevich21a15a22009-05-11 14:49:29 -07002053 block(0);
Jack Palevichbf42c9c2009-05-12 12:48:35 -07002054 pGen->gsym(rsym);
Jack Palevich546b2242009-05-13 15:10:04 -07002055 pGen->functionExit(argCount, a, loc);
Jack Palevich21a15a22009-05-11 14:49:29 -07002056 }
2057 }
2058 }
2059
Jack Palevichf1f39cc2009-05-29 18:03:15 -07002060 char* allocGlobalSpace(int bytes) {
2061 if (glo - pGlobalBase + bytes > ALLOC_SIZE) {
2062 error("Global space exhausted");
Jack Palevich0a280a02009-06-11 10:53:51 -07002063 return NULL;
Jack Palevichf1f39cc2009-05-29 18:03:15 -07002064 }
2065 char* result = glo;
2066 glo += bytes;
2067 return result;
2068 }
2069
Jack Palevich21a15a22009-05-11 14:49:29 -07002070 void cleanup() {
2071 if (sym_stk != 0) {
Jack Palevich653f42d2009-05-28 17:15:32 -07002072 free(sym_stk);
Jack Palevich21a15a22009-05-11 14:49:29 -07002073 sym_stk = 0;
2074 }
2075 if (pGlobalBase != 0) {
Jack Palevichf1f39cc2009-05-29 18:03:15 -07002076 free(pGlobalBase);
Jack Palevich21a15a22009-05-11 14:49:29 -07002077 pGlobalBase = 0;
2078 }
2079 if (pVarsBase != 0) {
2080 free(pVarsBase);
2081 pVarsBase = 0;
2082 }
2083 if (pGen) {
2084 delete pGen;
2085 pGen = 0;
2086 }
Jack Palevich1cdef202009-05-22 12:06:27 -07002087 if (file) {
2088 delete file;
2089 file = 0;
2090 }
Jack Palevich21a15a22009-05-11 14:49:29 -07002091 }
2092
2093 void clear() {
2094 tok = 0;
2095 tokc = 0;
2096 tokl = 0;
2097 ch = 0;
Jack Palevich653f42d2009-05-28 17:15:32 -07002098 pVarsBase = 0;
Jack Palevich21a15a22009-05-11 14:49:29 -07002099 rsym = 0;
2100 loc = 0;
2101 glo = 0;
2102 sym_stk = 0;
2103 dstk = 0;
2104 dptr = 0;
2105 dch = 0;
2106 last_id = 0;
2107 file = 0;
2108 pGlobalBase = 0;
2109 pVarsBase = 0;
2110 pGen = 0;
Jack Palevicheedf9d22009-06-04 16:23:40 -07002111 mPragmaStringCount = 0;
Jack Palevich21a15a22009-05-11 14:49:29 -07002112 }
Jack Paleviche27bf3e2009-05-10 14:09:03 -07002113
Jack Palevich22305132009-05-13 10:58:45 -07002114 void setArchitecture(const char* architecture) {
2115 delete pGen;
2116 pGen = 0;
2117
2118 if (architecture != NULL) {
Jack Paleviche7b59062009-05-19 17:12:17 -07002119#ifdef PROVIDE_ARM_CODEGEN
Jack Palevich8b0624c2009-05-20 12:12:06 -07002120 if (! pGen && strcmp(architecture, "arm") == 0) {
Jack Palevich22305132009-05-13 10:58:45 -07002121 pGen = new ARMCodeGenerator();
Jack Palevich8b0624c2009-05-20 12:12:06 -07002122 }
Jack Paleviche7b59062009-05-19 17:12:17 -07002123#endif
Jack Paleviche7b59062009-05-19 17:12:17 -07002124#ifdef PROVIDE_X86_CODEGEN
Jack Palevich8b0624c2009-05-20 12:12:06 -07002125 if (! pGen && strcmp(architecture, "x86") == 0) {
Jack Palevich22305132009-05-13 10:58:45 -07002126 pGen = new X86CodeGenerator();
Jack Palevich8b0624c2009-05-20 12:12:06 -07002127 }
Jack Paleviche7b59062009-05-19 17:12:17 -07002128#endif
Jack Palevich8b0624c2009-05-20 12:12:06 -07002129 if (!pGen ) {
Jack Palevichac0e95e2009-05-29 13:53:44 -07002130 error("Unknown architecture %s\n", architecture);
Jack Palevich22305132009-05-13 10:58:45 -07002131 }
2132 }
2133
2134 if (pGen == NULL) {
Jack Paleviche7b59062009-05-19 17:12:17 -07002135#if defined(DEFAULT_ARM_CODEGEN)
Jack Palevich22305132009-05-13 10:58:45 -07002136 pGen = new ARMCodeGenerator();
Jack Paleviche7b59062009-05-19 17:12:17 -07002137#elif defined(DEFAULT_X86_CODEGEN)
2138 pGen = new X86CodeGenerator();
2139#endif
2140 }
2141 if (pGen == NULL) {
Jack Palevichac0e95e2009-05-29 13:53:44 -07002142 error("No code generator defined.");
Jack Palevich0a280a02009-06-11 10:53:51 -07002143 } else {
2144 pGen->setErrorSink(this);
Jack Palevich22305132009-05-13 10:58:45 -07002145 }
2146 }
2147
Jack Palevich77ae76e2009-05-10 19:59:24 -07002148public:
Jack Palevich22305132009-05-13 10:58:45 -07002149 struct args {
2150 args() {
2151 architecture = 0;
2152 }
2153 const char* architecture;
2154 };
2155
Jack Paleviche7b59062009-05-19 17:12:17 -07002156 Compiler() {
Jack Palevich21a15a22009-05-11 14:49:29 -07002157 clear();
Jack Paleviche27bf3e2009-05-10 14:09:03 -07002158 }
Jack Palevichbbf8ab52009-05-11 11:54:30 -07002159
Jack Paleviche7b59062009-05-19 17:12:17 -07002160 ~Compiler() {
Jack Palevich21a15a22009-05-11 14:49:29 -07002161 cleanup();
2162 }
2163
Jack Palevich1cdef202009-05-22 12:06:27 -07002164 int compile(const char* text, size_t textLength) {
Jack Palevichac0e95e2009-05-29 13:53:44 -07002165 int result;
Jack Palevich0a280a02009-06-11 10:53:51 -07002166
2167 cleanup();
2168 clear();
2169 codeBuf.init(ALLOC_SIZE);
2170 setArchitecture(NULL);
2171 if (!pGen) {
2172 return -1;
2173 }
2174 pGen->init(&codeBuf);
2175 file = new TextInputStream(text, textLength);
2176 sym_stk = (char*) calloc(1, ALLOC_SIZE);
2177 static const char* predefinedSymbols =
2178 " int char void"
2179 " if else while break return for"
2180 " pragma define main ";
2181 dstk = strcpy(sym_stk, predefinedSymbols)
2182 + strlen(predefinedSymbols);
2183 pGlobalBase = (char*) calloc(1, ALLOC_SIZE);
2184 glo = pGlobalBase;
2185 pVarsBase = (char*) calloc(1, ALLOC_SIZE);
2186 inp();
2187 next();
2188 globalDeclarations();
2189 result = pGen->finishCompile();
2190 if (result == 0) {
2191 if (mErrorBuf.len()) {
2192 result = -2;
Jack Palevichac0e95e2009-05-29 13:53:44 -07002193 }
Jack Palevich8b0624c2009-05-20 12:12:06 -07002194 }
Jack Palevichac0e95e2009-05-29 13:53:44 -07002195 return result;
Jack Palevich21a15a22009-05-11 14:49:29 -07002196 }
2197
2198 int run(int argc, char** argv) {
2199 typedef int (*mainPtr)(int argc, char** argv);
Jack Palevich653f42d2009-05-28 17:15:32 -07002200 mainPtr aMain = (mainPtr) *(int*) (pVarsBase + TOK_MAIN);
Jack Palevich21a15a22009-05-11 14:49:29 -07002201 if (!aMain) {
2202 fprintf(stderr, "Could not find function \"main\".\n");
2203 return -1;
2204 }
2205 return aMain(argc, argv);
2206 }
2207
2208 int dump(FILE* out) {
2209 fwrite(codeBuf.getBase(), 1, codeBuf.getSize(), out);
2210 return 0;
2211 }
Jack Palevich77ae76e2009-05-10 19:59:24 -07002212
Jack Palevicha6535612009-05-13 16:24:17 -07002213 int disassemble(FILE* out) {
2214 return pGen->disassemble(out);
2215 }
2216
Jack Palevich1cdef202009-05-22 12:06:27 -07002217 /* Look through the symbol table to find a symbol.
2218 * If found, return its value.
2219 */
2220 void* lookup(const char* name) {
2221 if (!sym_stk) {
2222 return NULL;
2223 }
2224 size_t nameLen = strlen(name);
Jack Palevich653f42d2009-05-28 17:15:32 -07002225 char* pSym = sym_stk;
Jack Palevich1cdef202009-05-22 12:06:27 -07002226 char c;
2227 for(;;) {
2228 c = *pSym++;
2229 if (c == 0) {
2230 break;
2231 }
2232 if (c == TAG_TOK) {
2233 if (memcmp(pSym, name, nameLen) == 0
2234 && pSym[nameLen] == TAG_TOK) {
Jack Palevich653f42d2009-05-28 17:15:32 -07002235 int tok = pSym - 1 - sym_stk;
Jack Palevich1cdef202009-05-22 12:06:27 -07002236 tok = tok * 8 + TOK_IDENT;
2237 if (tok <= TOK_DEFINE) {
2238 return 0;
2239 } else {
Jack Palevich653f42d2009-05-28 17:15:32 -07002240 tok = (intptr_t) (pVarsBase + tok);
Jack Palevich1cdef202009-05-22 12:06:27 -07002241 return * (void**) tok;
2242 }
2243 }
2244 }
2245 }
2246 return NULL;
2247 }
2248
Jack Palevicheedf9d22009-06-04 16:23:40 -07002249 void getPragmas(ACCsizei* actualStringCount,
2250 ACCsizei maxStringCount, ACCchar** strings) {
2251 int stringCount = mPragmaStringCount;
2252 if (actualStringCount) {
2253 *actualStringCount = stringCount;
2254 }
2255 if (stringCount > maxStringCount) {
2256 stringCount = maxStringCount;
2257 }
2258 if (strings) {
2259 char* pPragmas = mPragmas.getUnwrapped();
2260 while (stringCount-- > 0) {
2261 *strings++ = pPragmas;
2262 pPragmas += strlen(pPragmas) + 1;
2263 }
2264 }
2265 }
2266
Jack Palevichac0e95e2009-05-29 13:53:44 -07002267 char* getErrorMessage() {
Jack Palevicheedf9d22009-06-04 16:23:40 -07002268 return mErrorBuf.getUnwrapped();
Jack Palevichac0e95e2009-05-29 13:53:44 -07002269 }
2270
Jack Palevich77ae76e2009-05-10 19:59:24 -07002271};
2272
Jack Paleviche7b59062009-05-19 17:12:17 -07002273const char* Compiler::operatorChars =
Jack Palevichbf42c9c2009-05-12 12:48:35 -07002274 "++--*@/@%@+@-@<<>><=>=<@>@==!=&&||&@^@|@~@!@";
2275
Jack Paleviche7b59062009-05-19 17:12:17 -07002276const char Compiler::operatorLevel[] =
Jack Palevichbf42c9c2009-05-12 12:48:35 -07002277 {11, 11, 1, 1, 1, 2, 2, 3, 3, 4, 4, 4, 4,
2278 5, 5, /* ==, != */
2279 9, 10, /* &&, || */
2280 6, 7, 8, /* & ^ | */
2281 2, 2 /* ~ ! */
2282 };
2283
Jack Palevich8b0624c2009-05-20 12:12:06 -07002284#ifdef PROVIDE_ARM_CODEGEN
Jack Paleviche7b59062009-05-19 17:12:17 -07002285FILE* Compiler::ARMCodeGenerator::disasmOut;
Jack Palevich8b0624c2009-05-20 12:12:06 -07002286#endif
Jack Palevicha6535612009-05-13 16:24:17 -07002287
Jack Palevich8b0624c2009-05-20 12:12:06 -07002288#ifdef PROVIDE_X86_CODEGEN
Jack Paleviche7b59062009-05-19 17:12:17 -07002289const int Compiler::X86CodeGenerator::operatorHelper[] = {
Jack Palevichbf42c9c2009-05-12 12:48:35 -07002290 0x1, // ++
2291 0xff, // --
2292 0xc1af0f, // *
2293 0xf9f79991, // /
2294 0xf9f79991, // % (With manual assist to swap results)
2295 0xc801, // +
2296 0xd8f7c829, // -
2297 0xe0d391, // <<
2298 0xf8d391, // >>
2299 0xe, // <=
2300 0xd, // >=
2301 0xc, // <
2302 0xf, // >
2303 0x4, // ==
2304 0x5, // !=
2305 0x0, // &&
2306 0x1, // ||
2307 0xc821, // &
2308 0xc831, // ^
2309 0xc809, // |
2310 0xd0f7, // ~
2311 0x4 // !
2312};
Jack Palevich8b0624c2009-05-20 12:12:06 -07002313#endif
Jack Palevichbf42c9c2009-05-12 12:48:35 -07002314
Jack Palevich1cdef202009-05-22 12:06:27 -07002315struct ACCscript {
2316 ACCscript() {
2317 text = 0;
2318 textLength = 0;
2319 accError = ACC_NO_ERROR;
2320 }
Jack Palevichbbf8ab52009-05-11 11:54:30 -07002321
Jack Palevich1cdef202009-05-22 12:06:27 -07002322 ~ACCscript() {
2323 delete text;
2324 }
Jack Palevich546b2242009-05-13 15:10:04 -07002325
Jack Palevich1cdef202009-05-22 12:06:27 -07002326 void setError(ACCenum error) {
2327 if (accError == ACC_NO_ERROR && error != ACC_NO_ERROR) {
2328 accError = error;
Jack Palevichbbf8ab52009-05-11 11:54:30 -07002329 }
2330 }
2331
Jack Palevich1cdef202009-05-22 12:06:27 -07002332 ACCenum getError() {
2333 ACCenum result = accError;
2334 accError = ACC_NO_ERROR;
Jack Palevich22305132009-05-13 10:58:45 -07002335 return result;
Jack Palevichbbf8ab52009-05-11 11:54:30 -07002336 }
2337
Jack Palevich1cdef202009-05-22 12:06:27 -07002338 Compiler compiler;
2339 char* text;
2340 int textLength;
2341 ACCenum accError;
2342};
2343
2344
2345extern "C"
2346ACCscript* accCreateScript() {
2347 return new ACCscript();
Jack Palevichbbf8ab52009-05-11 11:54:30 -07002348}
Jack Palevich1cdef202009-05-22 12:06:27 -07002349
2350extern "C"
2351ACCenum accGetError( ACCscript* script ) {
2352 return script->getError();
2353}
2354
2355extern "C"
2356void accDeleteScript(ACCscript* script) {
2357 delete script;
2358}
2359
2360extern "C"
2361void accScriptSource(ACCscript* script,
2362 ACCsizei count,
2363 const ACCchar ** string,
2364 const ACCint * length) {
2365 int totalLength = 0;
2366 for(int i = 0; i < count; i++) {
2367 int len = -1;
2368 const ACCchar* s = string[i];
2369 if (length) {
2370 len = length[i];
2371 }
2372 if (len < 0) {
2373 len = strlen(s);
2374 }
2375 totalLength += len;
2376 }
2377 delete script->text;
2378 char* text = new char[totalLength + 1];
2379 script->text = text;
2380 script->textLength = totalLength;
Jack Palevich09555c72009-05-27 12:25:55 -07002381 char* dest = text;
Jack Palevich1cdef202009-05-22 12:06:27 -07002382 for(int i = 0; i < count; i++) {
2383 int len = -1;
2384 const ACCchar* s = string[i];
2385 if (length) {
2386 len = length[i];
2387 }
2388 if (len < 0) {
2389 len = strlen(s);
2390 }
Jack Palevich09555c72009-05-27 12:25:55 -07002391 memcpy(dest, s, len);
2392 dest += len;
Jack Palevich1cdef202009-05-22 12:06:27 -07002393 }
2394 text[totalLength] = '\0';
2395}
2396
2397extern "C"
2398void accCompileScript(ACCscript* script) {
2399 int result = script->compiler.compile(script->text, script->textLength);
2400 if (result) {
2401 script->setError(ACC_INVALID_OPERATION);
2402 }
2403}
2404
2405extern "C"
2406void accGetScriptiv(ACCscript* script,
2407 ACCenum pname,
2408 ACCint * params) {
2409 switch (pname) {
2410 case ACC_INFO_LOG_LENGTH:
2411 *params = 0;
2412 break;
2413 }
2414}
2415
2416extern "C"
2417void accGetScriptInfoLog(ACCscript* script,
2418 ACCsizei maxLength,
2419 ACCsizei * length,
2420 ACCchar * infoLog) {
Jack Palevichac0e95e2009-05-29 13:53:44 -07002421 char* message = script->compiler.getErrorMessage();
2422 int messageLength = strlen(message) + 1;
Jack Palevich1cdef202009-05-22 12:06:27 -07002423 if (length) {
Jack Palevichac0e95e2009-05-29 13:53:44 -07002424 *length = messageLength;
Jack Palevich1cdef202009-05-22 12:06:27 -07002425 }
Jack Palevichac0e95e2009-05-29 13:53:44 -07002426 if (infoLog && maxLength > 0) {
2427 int trimmedLength = maxLength < messageLength ?
2428 maxLength : messageLength;
2429 memcpy(infoLog, message, trimmedLength);
2430 infoLog[trimmedLength] = 0;
Jack Palevich1cdef202009-05-22 12:06:27 -07002431 }
2432}
2433
2434extern "C"
2435void accGetScriptLabel(ACCscript* script, const ACCchar * name,
2436 ACCvoid ** address) {
2437 void* value = script->compiler.lookup(name);
2438 if (value) {
2439 *address = value;
2440 } else {
2441 script->setError(ACC_INVALID_VALUE);
2442 }
2443}
2444
Jack Palevicheedf9d22009-06-04 16:23:40 -07002445extern "C"
2446void accGetPragmas(ACCscript* script, ACCsizei* actualStringCount,
2447 ACCsizei maxStringCount, ACCchar** strings){
2448 script->compiler.getPragmas(actualStringCount, maxStringCount, strings);
2449}
2450
2451
Jack Palevich1cdef202009-05-22 12:06:27 -07002452} // namespace acc
2453