blob: fb4f4f68927ee636a22cde85439f8023cc695c12 [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:
Jack Palevich2db168f2009-06-11 14:29:47 -07001207 FakeString(const char* string, size_t length) :
1208 String((char*) string, length, true) {}
Jack Palevich2d11dfb2009-06-08 14:34:26 -07001209
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();
Jack Palevich2db168f2009-06-11 14:29:47 -07001223 hashmapFree(mpMap);
Jack Palevich2d11dfb2009-06-08 14:34:26 -07001224 }
1225
1226 void clear() {
1227 hashmapForEach(mpMap, freeKeyValue, this);
1228 }
1229
1230 bool contains(String* pKey) {
1231 bool result = hashmapContainsKey(mpMap, pKey);
1232 return result;
1233 }
1234
1235 V* get(String* pKey) {
1236 V* result = (V*) hashmapGet(mpMap, pKey);
1237 return result;
1238 }
1239
1240 V* remove(String* pKey) {
1241 V* result = (V*) hashmapRemove(mpMap, pKey);
1242 return result;
1243 }
1244
1245 V* put(String* pKey, V* value) {
1246 V* result = (V*) hashmapPut(mpMap, pKey, value);
1247 if (result) {
1248 // The key was not adopted by the map, so delete it here.
1249 delete pKey;
1250 }
1251 return result;
1252 }
1253
1254 protected:
1255 static int hashFn(void* pKey) {
1256 String* pString = (String*) pKey;
1257 return hashmapHash(pString->getUnwrapped(), pString->len());
1258 }
1259
1260 static bool equalsFn(void* keyA, void* keyB) {
1261 String* pStringA = (String*) keyA;
1262 String* pStringB = (String*) keyB;
1263 return pStringA->len() == pStringB->len()
1264 && strcmp(pStringA->getUnwrapped(), pStringB->getUnwrapped())
1265 == 0;
1266 }
1267
1268 static bool freeKeyValue(void* key, void* value, void* context) {
1269 delete (String*) key;
1270 delete (V*) value;
1271 return true;
1272 }
1273
1274 Hashmap* mpMap;
1275 };
1276
1277 class MacroTable : public StringTable<String> {
1278 public:
1279 MacroTable() : StringTable<String>(10) {}
1280 };
1281
Jack Palevich2db168f2009-06-11 14:29:47 -07001282 class KeywordTable {
1283 public:
1284
1285 KeywordTable(){
1286 mpMap = hashmapCreate(40, hashFn, equalsFn);
1287 put("int", TOK_INT);
1288 put("char", TOK_CHAR);
1289 put("void", TOK_VOID);
1290 put("if", TOK_IF);
1291 put("else", TOK_ELSE);
1292 put("while", TOK_WHILE);
1293 put("break", TOK_BREAK);
1294 put("return", TOK_RETURN);
1295 put("for", TOK_FOR);
1296 put("pragma", TOK_PRAGMA);
1297 }
1298
1299 ~KeywordTable() {
1300 hashmapFree(mpMap);
1301 }
1302
1303 int get(char* key) {
1304 return (int) hashmapGet(mpMap, key);
1305 }
1306
1307 const char* lookupKeyFor(int value) {
1308 FindValContext context;
1309 context.key = 0;
1310 hashmapForEach(mpMap, findKeyFn, &context);
1311 return context.key;
1312 }
1313
1314 private:
1315 void put(const char* kw, int val) {
1316 hashmapPut(mpMap, (void*) kw, (void*) val);
1317 }
1318
1319 static int hashFn(void* pKey) {
1320 char* pString = (char*) pKey;
1321 return hashmapHash(pString, strlen(pString));
1322 }
1323
1324 static bool equalsFn(void* keyA, void* keyB) {
1325 const char* pStringA = (const char*) keyA;
1326 const char* pStringB = (const char*) keyB;
1327 return strcmp(pStringA, pStringB) == 0;
1328 }
1329
1330 struct FindValContext {
1331 char* key;
1332 int value;
1333 };
1334
1335 static bool findKeyFn(void* key, void* value, void* context) {
1336 FindValContext* pContext = (FindValContext*) context;
1337 if ((int) value == pContext->value) {
1338 pContext->key = (char*) key;
1339 return false;
1340 }
1341 return true;
1342 }
1343
1344 Hashmap* mpMap;
1345 };
1346
Jack Palevich2d11dfb2009-06-08 14:34:26 -07001347 template<class E> class Array {
1348 public:
1349 Array() {
1350 mpBase = 0;
1351 mUsed = 0;
1352 mSize = 0;
1353 }
1354
1355 ~Array() {
1356 if (mpBase) {
1357 free(mpBase);
1358 }
1359 }
1360
1361 E get(int i) {
1362 if (i < 0 || i > mUsed) {
1363 error("internal error: Index out of range");
1364 return E();
1365 }
1366 return mpBase[i];
1367 }
1368
1369 void set(int i, E val) {
1370 mpBase[i] = val;
1371 }
1372
1373 void pop() {
1374 if (mUsed > 0) {
1375 mUsed -= 1;
Jack Palevich36d94142009-06-08 15:55:32 -07001376 } else {
1377 error("internal error: Popped empty stack.");
Jack Palevich2d11dfb2009-06-08 14:34:26 -07001378 }
1379 }
1380
1381 void push(E item) {
1382 * ensure(1) = item;
1383 }
1384
1385 size_t len() {
1386 return mUsed;
1387 }
1388
1389 private:
1390 E* ensure(int n) {
1391 size_t newUsed = mUsed + n;
1392 if (newUsed > mSize) {
1393 size_t newSize = mSize * 2 + 10;
1394 if (newSize < newUsed) {
1395 newSize = newUsed;
1396 }
1397 mpBase = (E*) realloc(mpBase, sizeof(E) * newSize);
1398 mSize = newSize;
1399 }
1400 E* result = mpBase + mUsed;
1401 mUsed = newUsed;
1402 return result;
1403 }
1404
1405 E* mpBase;
1406 size_t mUsed;
1407 size_t mSize;
1408 };
1409
Jack Palevich36d94142009-06-08 15:55:32 -07001410 struct InputState {
1411 InputStream* pStream;
1412 int oldCh;
1413 };
1414
Jack Palevich2db168f2009-06-11 14:29:47 -07001415 struct VariableInfo {
1416 void* pA;
1417 void* pB;
1418 };
1419
Jack Palevich36d94142009-06-08 15:55:32 -07001420
1421 int ch; // Current input character, or EOF
1422 intptr_t tok; // token
1423 intptr_t tokc; // token extra info
1424 int tokl; // token operator level
1425 intptr_t rsym; // return symbol
1426 intptr_t loc; // local variable index
1427 char* glo; // global variable index
1428 char* sym_stk;
1429 char* dstk; // Define stack
1430 char* dptr; // Macro state: Points to macro text during macro playback.
1431 int dch; // Macro state: Saves old value of ch during a macro playback.
1432 char* last_id;
1433 char* pGlobalBase;
Jack Palevich2db168f2009-06-11 14:29:47 -07001434 VariableInfo* pVarsBase; // Value of variables
1435 KeywordTable mKeywords;
Jack Palevich36d94142009-06-08 15:55:32 -07001436
1437 InputStream* file;
1438
1439 CodeBuf codeBuf;
1440 CodeGenerator* pGen;
1441
Jack Palevich2d11dfb2009-06-08 14:34:26 -07001442 MacroTable mMacros;
Jack Palevich36d94142009-06-08 15:55:32 -07001443 Array<InputState> mInputStateStack;
Jack Palevich2d11dfb2009-06-08 14:34:26 -07001444
Jack Palevicheedf9d22009-06-04 16:23:40 -07001445 String mErrorBuf;
1446
Jack Palevicheedf9d22009-06-04 16:23:40 -07001447 String mPragmas;
1448 int mPragmaStringCount;
1449
Jack Palevich21a15a22009-05-11 14:49:29 -07001450 static const int ALLOC_SIZE = 99999;
1451
Jack Palevich2db168f2009-06-11 14:29:47 -07001452 // Keywords start at 0x100 and increase by 1
1453 static const int TOK_KEYWORD = 0x100;
1454 static const int TOK_INT = TOK_KEYWORD + 0;
1455 static const int TOK_CHAR = TOK_KEYWORD + 1;
1456 static const int TOK_VOID = TOK_KEYWORD + 2;
1457 static const int TOK_IF = TOK_KEYWORD + 3;
1458 static const int TOK_ELSE = TOK_KEYWORD + 4;
1459 static const int TOK_WHILE = TOK_KEYWORD + 5;
1460 static const int TOK_BREAK = TOK_KEYWORD + 6;
1461 static const int TOK_RETURN = TOK_KEYWORD + 7;
1462 static const int TOK_FOR = TOK_KEYWORD + 8;
1463 static const int TOK_PRAGMA = TOK_KEYWORD + 9;
1464 static const int TOK_DEFINE = TOK_KEYWORD + 10;
1465
1466 // Symbols start at 0x200
1467 static const int TOK_SYMBOL = 0x200;
Jack Palevich21a15a22009-05-11 14:49:29 -07001468
1469 static const int TOK_DUMMY = 1;
1470 static const int TOK_NUM = 2;
1471
1472 static const int LOCAL = 0x200;
1473
1474 static const int SYM_FORWARD = 0;
1475 static const int SYM_DEFINE = 1;
1476
1477 /* tokens in string heap */
1478 static const int TAG_TOK = ' ';
Jack Palevich21a15a22009-05-11 14:49:29 -07001479
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001480 static const int OP_INCREMENT = 0;
1481 static const int OP_DECREMENT = 1;
1482 static const int OP_MUL = 2;
1483 static const int OP_DIV = 3;
1484 static const int OP_MOD = 4;
1485 static const int OP_PLUS = 5;
1486 static const int OP_MINUS = 6;
1487 static const int OP_SHIFT_LEFT = 7;
1488 static const int OP_SHIFT_RIGHT = 8;
1489 static const int OP_LESS_EQUAL = 9;
1490 static const int OP_GREATER_EQUAL = 10;
1491 static const int OP_LESS = 11;
1492 static const int OP_GREATER = 12;
1493 static const int OP_EQUALS = 13;
1494 static const int OP_NOT_EQUALS = 14;
1495 static const int OP_LOGICAL_AND = 15;
1496 static const int OP_LOGICAL_OR = 16;
1497 static const int OP_BIT_AND = 17;
1498 static const int OP_BIT_XOR = 18;
1499 static const int OP_BIT_OR = 19;
1500 static const int OP_BIT_NOT = 20;
1501 static const int OP_LOGICAL_NOT = 21;
1502 static const int OP_COUNT = 22;
1503
1504 /* Operators are searched from front, the two-character operators appear
1505 * before the single-character operators with the same first character.
1506 * @ is used to pad out single-character operators.
1507 */
1508 static const char* operatorChars;
1509 static const char operatorLevel[];
1510
Jack Palevich21a15a22009-05-11 14:49:29 -07001511 void pdef(int t) {
Jack Palevichf1f39cc2009-05-29 18:03:15 -07001512 if (dstk - sym_stk >= ALLOC_SIZE) {
1513 error("Symbol table exhausted");
Jack Palevich0a280a02009-06-11 10:53:51 -07001514 return;
Jack Palevichf1f39cc2009-05-29 18:03:15 -07001515 }
Jack Palevich653f42d2009-05-28 17:15:32 -07001516 *dstk++ = t;
Jack Palevich21a15a22009-05-11 14:49:29 -07001517 }
1518
1519 void inp() {
1520 if (dptr) {
Jack Palevich653f42d2009-05-28 17:15:32 -07001521 ch = *dptr++;
Jack Palevich2d11dfb2009-06-08 14:34:26 -07001522 if (ch == 0) {
Jack Palevich21a15a22009-05-11 14:49:29 -07001523 dptr = 0;
1524 ch = dch;
1525 }
1526 } else
Jack Palevicheedf9d22009-06-04 16:23:40 -07001527 ch = file->getChar();
Jack Palevichb7c81e92009-06-04 19:56:13 -07001528#if 0
1529 printf("ch='%c' 0x%x\n", ch, ch);
1530#endif
Jack Palevich21a15a22009-05-11 14:49:29 -07001531 }
1532
1533 int isid() {
Jack Palevich546b2242009-05-13 15:10:04 -07001534 return isalnum(ch) | (ch == '_');
Jack Palevich21a15a22009-05-11 14:49:29 -07001535 }
1536
1537 /* read a character constant */
1538 void getq() {
1539 if (ch == '\\') {
1540 inp();
1541 if (ch == 'n')
1542 ch = '\n';
1543 }
1544 }
1545
1546 void next() {
1547 int l, a;
1548
Jack Palevich546b2242009-05-13 15:10:04 -07001549 while (isspace(ch) | (ch == '#')) {
Jack Palevich21a15a22009-05-11 14:49:29 -07001550 if (ch == '#') {
1551 inp();
1552 next();
1553 if (tok == TOK_DEFINE) {
Jack Palevich2d11dfb2009-06-08 14:34:26 -07001554 doDefine();
Jack Palevicheedf9d22009-06-04 16:23:40 -07001555 } else if (tok == TOK_PRAGMA) {
1556 doPragma();
1557 } else {
1558 error("Unsupported preprocessor directive \"%s\"", last_id);
Jack Palevich21a15a22009-05-11 14:49:29 -07001559 }
Jack Palevich21a15a22009-05-11 14:49:29 -07001560 }
1561 inp();
1562 }
1563 tokl = 0;
1564 tok = ch;
1565 /* encode identifiers & numbers */
1566 if (isid()) {
1567 pdef(TAG_TOK);
1568 last_id = dstk;
1569 while (isid()) {
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001570 pdef(ch);
1571 inp();
Jack Palevichae54f1f2009-05-08 14:54:15 -07001572 }
Jack Palevich21a15a22009-05-11 14:49:29 -07001573 if (isdigit(tok)) {
Jack Palevich653f42d2009-05-28 17:15:32 -07001574 tokc = strtol(last_id, 0, 0);
Jack Palevich21a15a22009-05-11 14:49:29 -07001575 tok = TOK_NUM;
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001576 } else {
Jack Palevichf1f39cc2009-05-29 18:03:15 -07001577 if (dstk - sym_stk + 1 > ALLOC_SIZE) {
1578 error("symbol stack overflow");
1579 }
Jack Palevich2d11dfb2009-06-08 14:34:26 -07001580 FakeString token(last_id, dstk-last_id);
1581 // Is this a macro?
1582 String* pValue = mMacros.get(&token);
1583 if (pValue) {
1584 // Yes, it is a macro
1585 dstk = last_id-1;
1586 dptr = pValue->getUnwrapped();
1587 dch = ch;
1588 inp();
1589 next();
1590 } else {
Jack Palevich2db168f2009-06-11 14:29:47 -07001591 // Is this a keyword?
1592 * dstk = 0;
1593 int kwtok = mKeywords.get(last_id);
1594 if (kwtok) {
1595 tok = kwtok;
1596 // fprintf(stderr, "tok= keyword %s %x\n", last_id, tok);
1597 } else {
1598 * dstk = TAG_TOK; /* no need to mark end of string (we
1599 suppose data is initialized to zero by calloc) */
1600 tok = (intptr_t) (strstr(sym_stk, (last_id - 1))
1601 - sym_stk);
1602 * dstk = 0; /* mark real end of ident for dlsym() */
1603 tok = (intptr_t) & (pVarsBase[tok]);
1604 // fprintf(stderr, "tok= symbol %s %x\n", last_id, tok);
Jack Palevich21a15a22009-05-11 14:49:29 -07001605 }
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001606 }
1607 }
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001608 } else {
Jack Palevich21a15a22009-05-11 14:49:29 -07001609 inp();
1610 if (tok == '\'') {
1611 tok = TOK_NUM;
1612 getq();
1613 tokc = ch;
1614 inp();
1615 inp();
Jack Palevich546b2242009-05-13 15:10:04 -07001616 } else if ((tok == '/') & (ch == '*')) {
Jack Palevich21a15a22009-05-11 14:49:29 -07001617 inp();
1618 while (ch) {
1619 while (ch != '*')
1620 inp();
1621 inp();
1622 if (ch == '/')
1623 ch = 0;
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001624 }
Jack Palevich21a15a22009-05-11 14:49:29 -07001625 inp();
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001626 next();
Jack Palevichbd894902009-05-14 19:35:31 -07001627 } else if ((tok == '/') & (ch == '/')) {
1628 inp();
1629 while (ch && (ch != '\n')) {
1630 inp();
1631 }
1632 inp();
1633 next();
Jack Palevich21a15a22009-05-11 14:49:29 -07001634 } else {
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001635 const char* t = operatorChars;
1636 int opIndex = 0;
Jack Palevich546b2242009-05-13 15:10:04 -07001637 while ((l = *t++) != 0) {
Jack Palevich21a15a22009-05-11 14:49:29 -07001638 a = *t++;
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001639 tokl = operatorLevel[opIndex];
1640 tokc = opIndex;
Jack Palevich546b2242009-05-13 15:10:04 -07001641 if ((l == tok) & ((a == ch) | (a == '@'))) {
Jack Palevich21a15a22009-05-11 14:49:29 -07001642#if 0
1643 printf("%c%c -> tokl=%d tokc=0x%x\n",
1644 l, a, tokl, tokc);
1645#endif
1646 if (a == ch) {
1647 inp();
1648 tok = TOK_DUMMY; /* dummy token for double tokens */
1649 }
1650 break;
1651 }
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001652 opIndex++;
1653 }
1654 if (l == 0) {
1655 tokl = 0;
1656 tokc = 0;
Jack Palevich21a15a22009-05-11 14:49:29 -07001657 }
1658 }
1659 }
1660#if 0
1661 {
Jack Palevich2db168f2009-06-11 14:29:47 -07001662 const char* p;
Jack Palevich21a15a22009-05-11 14:49:29 -07001663
1664 printf("tok=0x%x ", tok);
Jack Palevich2db168f2009-06-11 14:29:47 -07001665 if (tok >= TOK_KEYWORD) {
Jack Palevich21a15a22009-05-11 14:49:29 -07001666 printf("'");
Jack Palevich2db168f2009-06-11 14:29:47 -07001667 if (tok>= TOK_SYMBOL)
1668 p = sym_stk + 1 + ((char*) tok - (char*) pVarsBase) / 8;
1669 else {
1670 p = mKeywords.lookupKeyFor(tok);
1671 if (!p) {
1672 p = "unknown keyword";
1673 }
1674 }
Jack Palevich653f42d2009-05-28 17:15:32 -07001675 while (*p != TAG_TOK && *p)
1676 printf("%c", *p++);
Jack Palevich21a15a22009-05-11 14:49:29 -07001677 printf("'\n");
1678 } else if (tok == TOK_NUM) {
1679 printf("%d\n", tokc);
1680 } else {
1681 printf("'%c'\n", tok);
1682 }
1683 }
1684#endif
1685 }
1686
Jack Palevich2d11dfb2009-06-08 14:34:26 -07001687 void doDefine() {
1688 String* pName = new String();
1689 while (isspace(ch)) {
1690 inp();
1691 }
1692 while (isid()) {
1693 pName->append(ch);
1694 inp();
1695 }
1696 if (ch == '(') {
1697 delete pName;
1698 error("Defines with arguments not supported");
Jack Palevich0a280a02009-06-11 10:53:51 -07001699 return;
Jack Palevich2d11dfb2009-06-08 14:34:26 -07001700 }
1701 while (isspace(ch)) {
1702 inp();
1703 }
1704 String* pValue = new String();
1705 while (ch != '\n' && ch != EOF) {
1706 pValue->append(ch);
1707 inp();
1708 }
1709 delete mMacros.put(pName, pValue);
1710 }
1711
Jack Palevicheedf9d22009-06-04 16:23:40 -07001712 void doPragma() {
1713 // # pragma name(val)
1714 int state = 0;
1715 while(ch != EOF && ch != '\n' && state < 10) {
1716 switch(state) {
1717 case 0:
1718 if (isspace(ch)) {
1719 inp();
1720 } else {
1721 state++;
1722 }
1723 break;
1724 case 1:
1725 if (isalnum(ch)) {
1726 mPragmas.append(ch);
1727 inp();
1728 } else if (ch == '(') {
1729 mPragmas.append(0);
1730 inp();
1731 state++;
1732 } else {
1733 state = 11;
1734 }
1735 break;
1736 case 2:
1737 if (isalnum(ch)) {
1738 mPragmas.append(ch);
1739 inp();
1740 } else if (ch == ')') {
1741 mPragmas.append(0);
1742 inp();
1743 state = 10;
1744 } else {
1745 state = 11;
1746 }
1747 break;
1748 }
1749 }
1750 if(state != 10) {
1751 error("Unexpected pragma syntax");
1752 }
1753 mPragmaStringCount += 2;
1754 }
Jack Palevich21a15a22009-05-11 14:49:29 -07001755
Jack Palevichac0e95e2009-05-29 13:53:44 -07001756 virtual void verror(const char* fmt, va_list ap) {
Jack Palevicheedf9d22009-06-04 16:23:40 -07001757 mErrorBuf.printf("%ld: ", file->getLine());
1758 mErrorBuf.vprintf(fmt, ap);
1759 mErrorBuf.printf("\n");
Jack Palevich21a15a22009-05-11 14:49:29 -07001760 }
1761
Jack Palevich8b0624c2009-05-20 12:12:06 -07001762 void skip(intptr_t c) {
Jack Palevich21a15a22009-05-11 14:49:29 -07001763 if (tok != c) {
1764 error("'%c' expected", c);
1765 }
1766 next();
1767 }
1768
Jack Palevich21a15a22009-05-11 14:49:29 -07001769 /* l is one if '=' parsing wanted (quick hack) */
Jack Palevich8b0624c2009-05-20 12:12:06 -07001770 void unary(intptr_t l) {
Jack Palevich653f42d2009-05-28 17:15:32 -07001771 intptr_t n, t, a;
1772 int c;
Jack Palevich546b2242009-05-13 15:10:04 -07001773 t = 0;
Jack Palevich21a15a22009-05-11 14:49:29 -07001774 n = 1; /* type of expression 0 = forward, 1 = value, other =
1775 lvalue */
1776 if (tok == '\"') {
Jack Palevich653f42d2009-05-28 17:15:32 -07001777 pGen->li((int) glo);
Jack Palevich21a15a22009-05-11 14:49:29 -07001778 while (ch != '\"') {
1779 getq();
Jack Palevichf1f39cc2009-05-29 18:03:15 -07001780 *allocGlobalSpace(1) = ch;
Jack Palevich21a15a22009-05-11 14:49:29 -07001781 inp();
1782 }
Jack Palevich653f42d2009-05-28 17:15:32 -07001783 *glo = 0;
Jack Palevichf1f39cc2009-05-29 18:03:15 -07001784 /* align heap */
1785 allocGlobalSpace((char*) (((intptr_t) glo + 4) & -4) - glo);
Jack Palevich21a15a22009-05-11 14:49:29 -07001786 inp();
1787 next();
1788 } else {
1789 c = tokl;
1790 a = tokc;
1791 t = tok;
1792 next();
1793 if (t == TOK_NUM) {
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001794 pGen->li(a);
Jack Palevich21a15a22009-05-11 14:49:29 -07001795 } else if (c == 2) {
1796 /* -, +, !, ~ */
1797 unary(0);
Jack Palevich1cdef202009-05-22 12:06:27 -07001798 pGen->clearR1();
Jack Palevich21a15a22009-05-11 14:49:29 -07001799 if (t == '!')
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001800 pGen->gcmp(a);
Jack Palevich21a15a22009-05-11 14:49:29 -07001801 else
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001802 pGen->genOp(a);
Jack Palevich21a15a22009-05-11 14:49:29 -07001803 } else if (t == '(') {
1804 expr();
1805 skip(')');
1806 } else if (t == '*') {
1807 /* parse cast */
1808 skip('(');
1809 t = tok; /* get type */
1810 next(); /* skip int/char/void */
1811 next(); /* skip '*' or '(' */
1812 if (tok == '*') {
1813 /* function type */
1814 skip('*');
1815 skip(')');
1816 skip('(');
1817 skip(')');
1818 t = 0;
1819 }
1820 skip(')');
1821 unary(0);
1822 if (tok == '=') {
1823 next();
Jack Palevich1cdef202009-05-22 12:06:27 -07001824 pGen->pushR0();
Jack Palevich21a15a22009-05-11 14:49:29 -07001825 expr();
Jack Palevich1cdef202009-05-22 12:06:27 -07001826 pGen->popR1();
1827 pGen->storeR0ToR1(t == TOK_INT);
Jack Palevich21a15a22009-05-11 14:49:29 -07001828 } else if (t) {
Jack Palevich1cdef202009-05-22 12:06:27 -07001829 pGen->loadR0FromR0(t == TOK_INT);
Jack Palevich21a15a22009-05-11 14:49:29 -07001830 }
1831 } else if (t == '&') {
Jack Palevich1cdef202009-05-22 12:06:27 -07001832 pGen->leaR0(*(int *) tok);
Jack Palevich21a15a22009-05-11 14:49:29 -07001833 next();
1834 } else {
1835 n = *(int *) t;
1836 /* forward reference: try dlsym */
Jack Palevichcb1c9ef2009-05-14 11:38:49 -07001837 if (!n) {
Jack Palevich653f42d2009-05-28 17:15:32 -07001838 n = (intptr_t) dlsym(RTLD_DEFAULT, last_id);
Jack Palevichcb1c9ef2009-05-14 11:38:49 -07001839 }
Jack Palevich546b2242009-05-13 15:10:04 -07001840 if ((tok == '=') & l) {
Jack Palevich21a15a22009-05-11 14:49:29 -07001841 /* assignment */
1842 next();
1843 expr();
Jack Palevich1cdef202009-05-22 12:06:27 -07001844 pGen->storeR0(n);
Jack Palevich21a15a22009-05-11 14:49:29 -07001845 } else if (tok != '(') {
1846 /* variable */
Jack Palevich1cdef202009-05-22 12:06:27 -07001847 pGen->loadR0(n, tokl == 11, tokc);
Jack Palevich21a15a22009-05-11 14:49:29 -07001848 if (tokl == 11) {
Jack Palevich21a15a22009-05-11 14:49:29 -07001849 next();
1850 }
1851 }
1852 }
1853 }
1854
1855 /* function call */
1856 if (tok == '(') {
1857 if (n == 1)
Jack Palevich1cdef202009-05-22 12:06:27 -07001858 pGen->pushR0();
Jack Palevich21a15a22009-05-11 14:49:29 -07001859
1860 /* push args and invert order */
Jack Palevichcb1c9ef2009-05-14 11:38:49 -07001861 a = pGen->beginFunctionCallArguments();
Jack Palevich21a15a22009-05-11 14:49:29 -07001862 next();
1863 l = 0;
1864 while (tok != ')') {
1865 expr();
Jack Palevich1cdef202009-05-22 12:06:27 -07001866 pGen->storeR0ToArg(l);
Jack Palevichbbf8ab52009-05-11 11:54:30 -07001867 if (tok == ',')
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001868 next();
Jack Palevich21a15a22009-05-11 14:49:29 -07001869 l = l + 4;
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001870 }
Jack Palevichcb1c9ef2009-05-14 11:38:49 -07001871 pGen->endFunctionCallArguments(a, l);
Jack Palevich21a15a22009-05-11 14:49:29 -07001872 next();
1873 if (!n) {
1874 /* forward reference */
1875 t = t + 4;
1876 *(int *) t = pGen->callForward(*(int *) t);
1877 } else if (n == 1) {
1878 pGen->callIndirect(l);
Jack Palevich21a15a22009-05-11 14:49:29 -07001879 } else {
Jack Palevich7810bc92009-05-15 14:31:47 -07001880 pGen->callRelative(n - codeBuf.getPC() - pGen->jumpOffset());
Jack Palevich21a15a22009-05-11 14:49:29 -07001881 }
Jack Palevich3d474a72009-05-15 15:12:38 -07001882 if (l | (n == 1))
Jack Palevich7810bc92009-05-15 14:31:47 -07001883 pGen->adjustStackAfterCall(l, n == 1);
Jack Palevich21a15a22009-05-11 14:49:29 -07001884 }
1885 }
1886
Jack Palevich653f42d2009-05-28 17:15:32 -07001887 void sum(int l) {
Jack Palevich8b0624c2009-05-20 12:12:06 -07001888 intptr_t t, n, a;
Jack Palevich546b2242009-05-13 15:10:04 -07001889 t = 0;
Jack Palevich21a15a22009-05-11 14:49:29 -07001890 if (l-- == 1)
1891 unary(1);
1892 else {
1893 sum(l);
1894 a = 0;
1895 while (l == tokl) {
1896 n = tok;
1897 t = tokc;
1898 next();
1899
1900 if (l > 8) {
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001901 a = pGen->gtst(t == OP_LOGICAL_OR, a); /* && and || output code generation */
Jack Palevich21a15a22009-05-11 14:49:29 -07001902 sum(l);
1903 } else {
Jack Palevich1cdef202009-05-22 12:06:27 -07001904 pGen->pushR0();
Jack Palevich21a15a22009-05-11 14:49:29 -07001905 sum(l);
Jack Palevich1cdef202009-05-22 12:06:27 -07001906 pGen->popR1();
Jack Palevich21a15a22009-05-11 14:49:29 -07001907
Jack Palevich546b2242009-05-13 15:10:04 -07001908 if ((l == 4) | (l == 5)) {
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001909 pGen->gcmp(t);
Jack Palevich21a15a22009-05-11 14:49:29 -07001910 } else {
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001911 pGen->genOp(t);
Jack Palevich21a15a22009-05-11 14:49:29 -07001912 }
1913 }
1914 }
1915 /* && and || output code generation */
1916 if (a && l > 8) {
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001917 a = pGen->gtst(t == OP_LOGICAL_OR, a);
1918 pGen->li(t != OP_LOGICAL_OR);
Jack Palevicha6535612009-05-13 16:24:17 -07001919 pGen->gjmp(5); /* jmp $ + 5 (sizeof li, FIXME for ARM) */
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001920 pGen->gsym(a);
1921 pGen->li(t == OP_LOGICAL_OR);
Jack Palevich21a15a22009-05-11 14:49:29 -07001922 }
1923 }
1924 }
1925
1926 void expr() {
1927 sum(11);
1928 }
1929
1930 int test_expr() {
1931 expr();
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001932 return pGen->gtst(0, 0);
Jack Palevich21a15a22009-05-11 14:49:29 -07001933 }
1934
Jack Palevich8b0624c2009-05-20 12:12:06 -07001935 void block(intptr_t l) {
1936 intptr_t a, n, t;
Jack Palevich21a15a22009-05-11 14:49:29 -07001937
1938 if (tok == TOK_IF) {
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001939 next();
1940 skip('(');
Jack Palevich21a15a22009-05-11 14:49:29 -07001941 a = test_expr();
1942 skip(')');
1943 block(l);
1944 if (tok == TOK_ELSE) {
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001945 next();
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001946 n = pGen->gjmp(0); /* jmp */
1947 pGen->gsym(a);
Jack Palevich21a15a22009-05-11 14:49:29 -07001948 block(l);
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001949 pGen->gsym(n); /* patch else jmp */
Jack Palevich21a15a22009-05-11 14:49:29 -07001950 } else {
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001951 pGen->gsym(a); /* patch if test */
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001952 }
Jack Palevich546b2242009-05-13 15:10:04 -07001953 } else if ((tok == TOK_WHILE) | (tok == TOK_FOR)) {
Jack Palevich21a15a22009-05-11 14:49:29 -07001954 t = tok;
1955 next();
1956 skip('(');
1957 if (t == TOK_WHILE) {
Jack Palevicha6535612009-05-13 16:24:17 -07001958 n = codeBuf.getPC(); // top of loop, target of "next" iteration
Jack Palevich21a15a22009-05-11 14:49:29 -07001959 a = test_expr();
1960 } else {
1961 if (tok != ';')
1962 expr();
1963 skip(';');
1964 n = codeBuf.getPC();
1965 a = 0;
1966 if (tok != ';')
1967 a = test_expr();
1968 skip(';');
1969 if (tok != ')') {
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001970 t = pGen->gjmp(0);
Jack Palevich21a15a22009-05-11 14:49:29 -07001971 expr();
Jack Palevicha6535612009-05-13 16:24:17 -07001972 pGen->gjmp(n - codeBuf.getPC() - pGen->jumpOffset());
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001973 pGen->gsym(t);
Jack Palevich21a15a22009-05-11 14:49:29 -07001974 n = t + 4;
1975 }
1976 }
1977 skip(')');
Jack Palevich8b0624c2009-05-20 12:12:06 -07001978 block((intptr_t) &a);
Jack Palevicha6535612009-05-13 16:24:17 -07001979 pGen->gjmp(n - codeBuf.getPC() - pGen->jumpOffset()); /* jmp */
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001980 pGen->gsym(a);
Jack Palevich21a15a22009-05-11 14:49:29 -07001981 } else if (tok == '{') {
1982 next();
1983 /* declarations */
Jack Palevichb7c81e92009-06-04 19:56:13 -07001984 localDeclarations();
Jack Palevich21a15a22009-05-11 14:49:29 -07001985 while (tok != '}')
1986 block(l);
1987 next();
1988 } else {
1989 if (tok == TOK_RETURN) {
1990 next();
1991 if (tok != ';')
1992 expr();
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001993 rsym = pGen->gjmp(rsym); /* jmp */
Jack Palevich21a15a22009-05-11 14:49:29 -07001994 } else if (tok == TOK_BREAK) {
1995 next();
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001996 *(int *) l = pGen->gjmp(*(int *) l);
Jack Palevich21a15a22009-05-11 14:49:29 -07001997 } else if (tok != ';')
1998 expr();
1999 skip(';');
Jack Paleviche27bf3e2009-05-10 14:09:03 -07002000 }
2001 }
Jack Palevich21a15a22009-05-11 14:49:29 -07002002
Jack Palevichb7c81e92009-06-04 19:56:13 -07002003 typedef int Type;
2004 static const Type TY_UNKNOWN = 0;
2005 static const Type TY_INT = 1;
2006 static const Type TY_CHAR = 2;
2007 static const Type TY_VOID = 3;
2008 static const int TY_BASE_TYPE_MASK = 0xf;
2009 static const int TY_INDIRECTION_MASK = 0xf0;
2010 static const int TY_INDIRECTION_SHIFT = 4;
2011 static const int MAX_INDIRECTION_COUNT = 15;
Jack Palevich21a15a22009-05-11 14:49:29 -07002012
Jack Palevichb7c81e92009-06-04 19:56:13 -07002013 Type getBaseType(Type t) {
2014 return t & TY_BASE_TYPE_MASK;
2015 }
2016
2017 int getIndirectionCount(Type t) {
2018 return (TY_INDIRECTION_MASK & t) >> TY_INDIRECTION_SHIFT;
2019 }
2020
2021 void setIndirectionCount(Type& t, int count) {
2022 t = ((TY_INDIRECTION_MASK & (count << TY_INDIRECTION_SHIFT))
2023 | (t & ~TY_INDIRECTION_MASK));
2024 }
2025
2026 bool acceptType(Type& t) {
2027 t = TY_UNKNOWN;
2028 if (tok == TOK_INT) {
2029 t = TY_INT;
2030 } else if (tok == TOK_CHAR) {
2031 t = TY_CHAR;
2032 } else if (tok == TOK_VOID) {
2033 t = TY_VOID;
2034 } else {
2035 return false;
2036 }
2037 next();
2038 return true;
2039 }
2040
2041 Type acceptPointerDeclaration(Type& base) {
2042 Type t = base;
2043 int indirectionCount = 0;
2044 while (tok == '*' && indirectionCount <= MAX_INDIRECTION_COUNT) {
2045 next();
2046 indirectionCount++;
2047 }
2048 if (indirectionCount > MAX_INDIRECTION_COUNT) {
2049 error("Too many levels of pointer. Max %d", MAX_INDIRECTION_COUNT);
2050 }
2051 setIndirectionCount(t, indirectionCount);
2052 return t;
2053 }
2054
2055 void expectType(Type& t) {
2056 if (!acceptType(t)) {
2057 error("Expected a type.");
2058 }
2059 }
2060
2061 void checkSymbol() {
Jack Palevich2db168f2009-06-11 14:29:47 -07002062 if (tok < TOK_SYMBOL) {
Jack Palevichb7c81e92009-06-04 19:56:13 -07002063 error("Expected a symbol");
2064 }
2065 }
2066
2067 void localDeclarations() {
2068 intptr_t a;
2069 Type base;
2070
2071 while (acceptType(base)) {
2072 while (tok != ';') {
2073 Type t = acceptPointerDeclaration(t);
2074 checkSymbol();
2075 loc = loc + 4;
2076 *(int *) tok = -loc;
2077
Jack Palevich21a15a22009-05-11 14:49:29 -07002078 next();
Jack Palevichb7c81e92009-06-04 19:56:13 -07002079 if (tok == ',')
2080 next();
2081 }
2082 skip(';');
2083 }
2084 }
2085
2086 void globalDeclarations() {
2087 while (tok != EOF) {
2088 Type base;
2089 expectType(base);
2090 Type t = acceptPointerDeclaration(t);
2091 checkSymbol();
2092 int name = tok;
2093 next();
2094 if (tok == ',' || tok == ';') {
2095 // it's a variable declaration
2096 for(;;) {
2097 *(int* *) name = (int*) allocGlobalSpace(4);
2098 if (tok != ',') {
2099 break;
Jack Palevich21a15a22009-05-11 14:49:29 -07002100 }
2101 next();
Jack Palevichb7c81e92009-06-04 19:56:13 -07002102 t = acceptPointerDeclaration(t);
2103 checkSymbol();
2104 name = tok;
2105 next();
Jack Palevich21a15a22009-05-11 14:49:29 -07002106 }
2107 skip(';');
2108 } else {
Jack Palevichb7c81e92009-06-04 19:56:13 -07002109 /* patch forward references (XXX: does not work for function
Jack Palevich21a15a22009-05-11 14:49:29 -07002110 pointers) */
Jack Palevichb7c81e92009-06-04 19:56:13 -07002111 pGen->gsym(*(int *) (name + 4));
Jack Palevich21a15a22009-05-11 14:49:29 -07002112 /* put function address */
Jack Palevichb7c81e92009-06-04 19:56:13 -07002113 *(int *) name = codeBuf.getPC();
Jack Palevich21a15a22009-05-11 14:49:29 -07002114 skip('(');
Jack Palevichb7c81e92009-06-04 19:56:13 -07002115 intptr_t a = 8;
Jack Palevich546b2242009-05-13 15:10:04 -07002116 int argCount = 0;
Jack Palevich21a15a22009-05-11 14:49:29 -07002117 while (tok != ')') {
Jack Palevichb7c81e92009-06-04 19:56:13 -07002118 Type aType;
2119 expectType(aType);
2120 aType = acceptPointerDeclaration(aType);
2121 checkSymbol();
Jack Palevich21a15a22009-05-11 14:49:29 -07002122 /* read param name and compute offset */
2123 *(int *) tok = a;
2124 a = a + 4;
2125 next();
2126 if (tok == ',')
2127 next();
Jack Palevich546b2242009-05-13 15:10:04 -07002128 argCount++;
Jack Palevich21a15a22009-05-11 14:49:29 -07002129 }
Jack Palevichb7c81e92009-06-04 19:56:13 -07002130 skip(')'); /* skip ')' */
Jack Palevich21a15a22009-05-11 14:49:29 -07002131 rsym = loc = 0;
Jack Palevich546b2242009-05-13 15:10:04 -07002132 a = pGen->functionEntry(argCount);
Jack Palevich21a15a22009-05-11 14:49:29 -07002133 block(0);
Jack Palevichbf42c9c2009-05-12 12:48:35 -07002134 pGen->gsym(rsym);
Jack Palevich546b2242009-05-13 15:10:04 -07002135 pGen->functionExit(argCount, a, loc);
Jack Palevich21a15a22009-05-11 14:49:29 -07002136 }
2137 }
2138 }
2139
Jack Palevichf1f39cc2009-05-29 18:03:15 -07002140 char* allocGlobalSpace(int bytes) {
2141 if (glo - pGlobalBase + bytes > ALLOC_SIZE) {
2142 error("Global space exhausted");
Jack Palevich0a280a02009-06-11 10:53:51 -07002143 return NULL;
Jack Palevichf1f39cc2009-05-29 18:03:15 -07002144 }
2145 char* result = glo;
2146 glo += bytes;
2147 return result;
2148 }
2149
Jack Palevich21a15a22009-05-11 14:49:29 -07002150 void cleanup() {
2151 if (sym_stk != 0) {
Jack Palevich653f42d2009-05-28 17:15:32 -07002152 free(sym_stk);
Jack Palevich21a15a22009-05-11 14:49:29 -07002153 sym_stk = 0;
2154 }
2155 if (pGlobalBase != 0) {
Jack Palevichf1f39cc2009-05-29 18:03:15 -07002156 free(pGlobalBase);
Jack Palevich21a15a22009-05-11 14:49:29 -07002157 pGlobalBase = 0;
2158 }
2159 if (pVarsBase != 0) {
2160 free(pVarsBase);
2161 pVarsBase = 0;
2162 }
2163 if (pGen) {
2164 delete pGen;
2165 pGen = 0;
2166 }
Jack Palevich1cdef202009-05-22 12:06:27 -07002167 if (file) {
2168 delete file;
2169 file = 0;
2170 }
Jack Palevich21a15a22009-05-11 14:49:29 -07002171 }
2172
2173 void clear() {
2174 tok = 0;
2175 tokc = 0;
2176 tokl = 0;
2177 ch = 0;
Jack Palevich653f42d2009-05-28 17:15:32 -07002178 pVarsBase = 0;
Jack Palevich21a15a22009-05-11 14:49:29 -07002179 rsym = 0;
2180 loc = 0;
2181 glo = 0;
2182 sym_stk = 0;
2183 dstk = 0;
2184 dptr = 0;
2185 dch = 0;
2186 last_id = 0;
2187 file = 0;
2188 pGlobalBase = 0;
2189 pVarsBase = 0;
2190 pGen = 0;
Jack Palevicheedf9d22009-06-04 16:23:40 -07002191 mPragmaStringCount = 0;
Jack Palevich21a15a22009-05-11 14:49:29 -07002192 }
Jack Paleviche27bf3e2009-05-10 14:09:03 -07002193
Jack Palevich22305132009-05-13 10:58:45 -07002194 void setArchitecture(const char* architecture) {
2195 delete pGen;
2196 pGen = 0;
2197
2198 if (architecture != NULL) {
Jack Paleviche7b59062009-05-19 17:12:17 -07002199#ifdef PROVIDE_ARM_CODEGEN
Jack Palevich8b0624c2009-05-20 12:12:06 -07002200 if (! pGen && strcmp(architecture, "arm") == 0) {
Jack Palevich22305132009-05-13 10:58:45 -07002201 pGen = new ARMCodeGenerator();
Jack Palevich8b0624c2009-05-20 12:12:06 -07002202 }
Jack Paleviche7b59062009-05-19 17:12:17 -07002203#endif
Jack Paleviche7b59062009-05-19 17:12:17 -07002204#ifdef PROVIDE_X86_CODEGEN
Jack Palevich8b0624c2009-05-20 12:12:06 -07002205 if (! pGen && strcmp(architecture, "x86") == 0) {
Jack Palevich22305132009-05-13 10:58:45 -07002206 pGen = new X86CodeGenerator();
Jack Palevich8b0624c2009-05-20 12:12:06 -07002207 }
Jack Paleviche7b59062009-05-19 17:12:17 -07002208#endif
Jack Palevich8b0624c2009-05-20 12:12:06 -07002209 if (!pGen ) {
Jack Palevichac0e95e2009-05-29 13:53:44 -07002210 error("Unknown architecture %s\n", architecture);
Jack Palevich22305132009-05-13 10:58:45 -07002211 }
2212 }
2213
2214 if (pGen == NULL) {
Jack Paleviche7b59062009-05-19 17:12:17 -07002215#if defined(DEFAULT_ARM_CODEGEN)
Jack Palevich22305132009-05-13 10:58:45 -07002216 pGen = new ARMCodeGenerator();
Jack Paleviche7b59062009-05-19 17:12:17 -07002217#elif defined(DEFAULT_X86_CODEGEN)
2218 pGen = new X86CodeGenerator();
2219#endif
2220 }
2221 if (pGen == NULL) {
Jack Palevichac0e95e2009-05-29 13:53:44 -07002222 error("No code generator defined.");
Jack Palevich0a280a02009-06-11 10:53:51 -07002223 } else {
2224 pGen->setErrorSink(this);
Jack Palevich22305132009-05-13 10:58:45 -07002225 }
2226 }
2227
Jack Palevich77ae76e2009-05-10 19:59:24 -07002228public:
Jack Palevich22305132009-05-13 10:58:45 -07002229 struct args {
2230 args() {
2231 architecture = 0;
2232 }
2233 const char* architecture;
2234 };
2235
Jack Paleviche7b59062009-05-19 17:12:17 -07002236 Compiler() {
Jack Palevich21a15a22009-05-11 14:49:29 -07002237 clear();
Jack Paleviche27bf3e2009-05-10 14:09:03 -07002238 }
Jack Palevichbbf8ab52009-05-11 11:54:30 -07002239
Jack Paleviche7b59062009-05-19 17:12:17 -07002240 ~Compiler() {
Jack Palevich21a15a22009-05-11 14:49:29 -07002241 cleanup();
2242 }
2243
Jack Palevich1cdef202009-05-22 12:06:27 -07002244 int compile(const char* text, size_t textLength) {
Jack Palevichac0e95e2009-05-29 13:53:44 -07002245 int result;
Jack Palevich0a280a02009-06-11 10:53:51 -07002246
2247 cleanup();
2248 clear();
2249 codeBuf.init(ALLOC_SIZE);
2250 setArchitecture(NULL);
2251 if (!pGen) {
2252 return -1;
2253 }
2254 pGen->init(&codeBuf);
2255 file = new TextInputStream(text, textLength);
2256 sym_stk = (char*) calloc(1, ALLOC_SIZE);
Jack Palevich2db168f2009-06-11 14:29:47 -07002257 dstk = sym_stk;
Jack Palevich0a280a02009-06-11 10:53:51 -07002258 pGlobalBase = (char*) calloc(1, ALLOC_SIZE);
2259 glo = pGlobalBase;
Jack Palevich2db168f2009-06-11 14:29:47 -07002260 pVarsBase = (VariableInfo*) calloc(1, ALLOC_SIZE);
Jack Palevich0a280a02009-06-11 10:53:51 -07002261 inp();
2262 next();
2263 globalDeclarations();
2264 result = pGen->finishCompile();
2265 if (result == 0) {
2266 if (mErrorBuf.len()) {
2267 result = -2;
Jack Palevichac0e95e2009-05-29 13:53:44 -07002268 }
Jack Palevich8b0624c2009-05-20 12:12:06 -07002269 }
Jack Palevichac0e95e2009-05-29 13:53:44 -07002270 return result;
Jack Palevich21a15a22009-05-11 14:49:29 -07002271 }
2272
Jack Palevich21a15a22009-05-11 14:49:29 -07002273 int dump(FILE* out) {
2274 fwrite(codeBuf.getBase(), 1, codeBuf.getSize(), out);
2275 return 0;
2276 }
Jack Palevich77ae76e2009-05-10 19:59:24 -07002277
Jack Palevicha6535612009-05-13 16:24:17 -07002278 int disassemble(FILE* out) {
2279 return pGen->disassemble(out);
2280 }
2281
Jack Palevich1cdef202009-05-22 12:06:27 -07002282 /* Look through the symbol table to find a symbol.
2283 * If found, return its value.
2284 */
2285 void* lookup(const char* name) {
2286 if (!sym_stk) {
2287 return NULL;
2288 }
2289 size_t nameLen = strlen(name);
Jack Palevich653f42d2009-05-28 17:15:32 -07002290 char* pSym = sym_stk;
Jack Palevich1cdef202009-05-22 12:06:27 -07002291 char c;
2292 for(;;) {
2293 c = *pSym++;
2294 if (c == 0) {
2295 break;
2296 }
2297 if (c == TAG_TOK) {
2298 if (memcmp(pSym, name, nameLen) == 0
2299 && pSym[nameLen] == TAG_TOK) {
Jack Palevich653f42d2009-05-28 17:15:32 -07002300 int tok = pSym - 1 - sym_stk;
Jack Palevich2db168f2009-06-11 14:29:47 -07002301 tok = tok * sizeof(VariableInfo);
2302 tok = (intptr_t) ((intptr_t) pVarsBase + tok);
2303 return * (void**) tok;
Jack Palevich1cdef202009-05-22 12:06:27 -07002304 }
2305 }
2306 }
2307 return NULL;
2308 }
2309
Jack Palevicheedf9d22009-06-04 16:23:40 -07002310 void getPragmas(ACCsizei* actualStringCount,
2311 ACCsizei maxStringCount, ACCchar** strings) {
2312 int stringCount = mPragmaStringCount;
2313 if (actualStringCount) {
2314 *actualStringCount = stringCount;
2315 }
2316 if (stringCount > maxStringCount) {
2317 stringCount = maxStringCount;
2318 }
2319 if (strings) {
2320 char* pPragmas = mPragmas.getUnwrapped();
2321 while (stringCount-- > 0) {
2322 *strings++ = pPragmas;
2323 pPragmas += strlen(pPragmas) + 1;
2324 }
2325 }
2326 }
2327
Jack Palevichac0e95e2009-05-29 13:53:44 -07002328 char* getErrorMessage() {
Jack Palevicheedf9d22009-06-04 16:23:40 -07002329 return mErrorBuf.getUnwrapped();
Jack Palevichac0e95e2009-05-29 13:53:44 -07002330 }
2331
Jack Palevich77ae76e2009-05-10 19:59:24 -07002332};
2333
Jack Paleviche7b59062009-05-19 17:12:17 -07002334const char* Compiler::operatorChars =
Jack Palevichbf42c9c2009-05-12 12:48:35 -07002335 "++--*@/@%@+@-@<<>><=>=<@>@==!=&&||&@^@|@~@!@";
2336
Jack Paleviche7b59062009-05-19 17:12:17 -07002337const char Compiler::operatorLevel[] =
Jack Palevichbf42c9c2009-05-12 12:48:35 -07002338 {11, 11, 1, 1, 1, 2, 2, 3, 3, 4, 4, 4, 4,
2339 5, 5, /* ==, != */
2340 9, 10, /* &&, || */
2341 6, 7, 8, /* & ^ | */
2342 2, 2 /* ~ ! */
2343 };
2344
Jack Palevich8b0624c2009-05-20 12:12:06 -07002345#ifdef PROVIDE_ARM_CODEGEN
Jack Paleviche7b59062009-05-19 17:12:17 -07002346FILE* Compiler::ARMCodeGenerator::disasmOut;
Jack Palevich8b0624c2009-05-20 12:12:06 -07002347#endif
Jack Palevicha6535612009-05-13 16:24:17 -07002348
Jack Palevich8b0624c2009-05-20 12:12:06 -07002349#ifdef PROVIDE_X86_CODEGEN
Jack Paleviche7b59062009-05-19 17:12:17 -07002350const int Compiler::X86CodeGenerator::operatorHelper[] = {
Jack Palevichbf42c9c2009-05-12 12:48:35 -07002351 0x1, // ++
2352 0xff, // --
2353 0xc1af0f, // *
2354 0xf9f79991, // /
2355 0xf9f79991, // % (With manual assist to swap results)
2356 0xc801, // +
2357 0xd8f7c829, // -
2358 0xe0d391, // <<
2359 0xf8d391, // >>
2360 0xe, // <=
2361 0xd, // >=
2362 0xc, // <
2363 0xf, // >
2364 0x4, // ==
2365 0x5, // !=
2366 0x0, // &&
2367 0x1, // ||
2368 0xc821, // &
2369 0xc831, // ^
2370 0xc809, // |
2371 0xd0f7, // ~
2372 0x4 // !
2373};
Jack Palevich8b0624c2009-05-20 12:12:06 -07002374#endif
Jack Palevichbf42c9c2009-05-12 12:48:35 -07002375
Jack Palevich1cdef202009-05-22 12:06:27 -07002376struct ACCscript {
2377 ACCscript() {
2378 text = 0;
2379 textLength = 0;
2380 accError = ACC_NO_ERROR;
2381 }
Jack Palevichbbf8ab52009-05-11 11:54:30 -07002382
Jack Palevich1cdef202009-05-22 12:06:27 -07002383 ~ACCscript() {
2384 delete text;
2385 }
Jack Palevich546b2242009-05-13 15:10:04 -07002386
Jack Palevich1cdef202009-05-22 12:06:27 -07002387 void setError(ACCenum error) {
2388 if (accError == ACC_NO_ERROR && error != ACC_NO_ERROR) {
2389 accError = error;
Jack Palevichbbf8ab52009-05-11 11:54:30 -07002390 }
2391 }
2392
Jack Palevich1cdef202009-05-22 12:06:27 -07002393 ACCenum getError() {
2394 ACCenum result = accError;
2395 accError = ACC_NO_ERROR;
Jack Palevich22305132009-05-13 10:58:45 -07002396 return result;
Jack Palevichbbf8ab52009-05-11 11:54:30 -07002397 }
2398
Jack Palevich1cdef202009-05-22 12:06:27 -07002399 Compiler compiler;
2400 char* text;
2401 int textLength;
2402 ACCenum accError;
2403};
2404
2405
2406extern "C"
2407ACCscript* accCreateScript() {
2408 return new ACCscript();
Jack Palevichbbf8ab52009-05-11 11:54:30 -07002409}
Jack Palevich1cdef202009-05-22 12:06:27 -07002410
2411extern "C"
2412ACCenum accGetError( ACCscript* script ) {
2413 return script->getError();
2414}
2415
2416extern "C"
2417void accDeleteScript(ACCscript* script) {
2418 delete script;
2419}
2420
2421extern "C"
2422void accScriptSource(ACCscript* script,
2423 ACCsizei count,
2424 const ACCchar ** string,
2425 const ACCint * length) {
2426 int totalLength = 0;
2427 for(int i = 0; i < count; i++) {
2428 int len = -1;
2429 const ACCchar* s = string[i];
2430 if (length) {
2431 len = length[i];
2432 }
2433 if (len < 0) {
2434 len = strlen(s);
2435 }
2436 totalLength += len;
2437 }
2438 delete script->text;
2439 char* text = new char[totalLength + 1];
2440 script->text = text;
2441 script->textLength = totalLength;
Jack Palevich09555c72009-05-27 12:25:55 -07002442 char* dest = text;
Jack Palevich1cdef202009-05-22 12:06:27 -07002443 for(int i = 0; i < count; i++) {
2444 int len = -1;
2445 const ACCchar* s = string[i];
2446 if (length) {
2447 len = length[i];
2448 }
2449 if (len < 0) {
2450 len = strlen(s);
2451 }
Jack Palevich09555c72009-05-27 12:25:55 -07002452 memcpy(dest, s, len);
2453 dest += len;
Jack Palevich1cdef202009-05-22 12:06:27 -07002454 }
2455 text[totalLength] = '\0';
2456}
2457
2458extern "C"
2459void accCompileScript(ACCscript* script) {
2460 int result = script->compiler.compile(script->text, script->textLength);
2461 if (result) {
2462 script->setError(ACC_INVALID_OPERATION);
2463 }
2464}
2465
2466extern "C"
2467void accGetScriptiv(ACCscript* script,
2468 ACCenum pname,
2469 ACCint * params) {
2470 switch (pname) {
2471 case ACC_INFO_LOG_LENGTH:
2472 *params = 0;
2473 break;
2474 }
2475}
2476
2477extern "C"
2478void accGetScriptInfoLog(ACCscript* script,
2479 ACCsizei maxLength,
2480 ACCsizei * length,
2481 ACCchar * infoLog) {
Jack Palevichac0e95e2009-05-29 13:53:44 -07002482 char* message = script->compiler.getErrorMessage();
2483 int messageLength = strlen(message) + 1;
Jack Palevich1cdef202009-05-22 12:06:27 -07002484 if (length) {
Jack Palevichac0e95e2009-05-29 13:53:44 -07002485 *length = messageLength;
Jack Palevich1cdef202009-05-22 12:06:27 -07002486 }
Jack Palevichac0e95e2009-05-29 13:53:44 -07002487 if (infoLog && maxLength > 0) {
2488 int trimmedLength = maxLength < messageLength ?
2489 maxLength : messageLength;
2490 memcpy(infoLog, message, trimmedLength);
2491 infoLog[trimmedLength] = 0;
Jack Palevich1cdef202009-05-22 12:06:27 -07002492 }
2493}
2494
2495extern "C"
2496void accGetScriptLabel(ACCscript* script, const ACCchar * name,
2497 ACCvoid ** address) {
2498 void* value = script->compiler.lookup(name);
2499 if (value) {
2500 *address = value;
2501 } else {
2502 script->setError(ACC_INVALID_VALUE);
2503 }
2504}
2505
Jack Palevicheedf9d22009-06-04 16:23:40 -07002506extern "C"
2507void accGetPragmas(ACCscript* script, ACCsizei* actualStringCount,
2508 ACCsizei maxStringCount, ACCchar** strings){
2509 script->compiler.getPragmas(actualStringCount, maxStringCount, strings);
2510}
2511
2512
Jack Palevich1cdef202009-05-22 12:06:27 -07002513} // namespace acc
2514