blob: 096500dd804dc8dd5b445d776122a6a6b8e71a5c [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 Palevichac0e95e2009-05-29 13:53:44 -070013#include <setjmp.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 Palevichae54f1f2009-05-08 14:54:15 -070019
Jack Palevich546b2242009-05-13 15:10:04 -070020#if defined(__arm__)
21#include <unistd.h>
22#endif
23
Jack Paleviche7b59062009-05-19 17:12:17 -070024#if defined(__arm__)
25#define DEFAULT_ARM_CODEGEN
Jack Palevich8b0624c2009-05-20 12:12:06 -070026#define PROVIDE_ARM_CODEGEN
Jack Paleviche7b59062009-05-19 17:12:17 -070027#elif defined(__i386__)
28#define DEFAULT_X86_CODEGEN
Jack Palevich8b0624c2009-05-20 12:12:06 -070029#define PROVIDE_X86_CODEGEN
Jack Paleviche7b59062009-05-19 17:12:17 -070030#elif defined(__x86_64__)
31#define DEFAULT_X64_CODEGEN
Jack Palevich8b0624c2009-05-20 12:12:06 -070032#define PROVIDE_X64_CODEGEN
Jack Paleviche7b59062009-05-19 17:12:17 -070033#endif
34
Jack Paleviche7b59062009-05-19 17:12:17 -070035
36#ifdef PROVIDE_ARM_CODEGEN
Jack Palevicha6535612009-05-13 16:24:17 -070037#include "disassem.h"
Jack Paleviche7b59062009-05-19 17:12:17 -070038#endif
Jack Palevicha6535612009-05-13 16:24:17 -070039
Jack Palevich1cdef202009-05-22 12:06:27 -070040#include <acc/acc.h>
41
Jack Palevich09555c72009-05-27 12:25:55 -070042#define LOG_API(...) do {} while(0)
43// #define LOG_API(...) fprintf (stderr, __VA_ARGS__)
44
45// #define ENABLE_ARM_DISASSEMBLY
46
Jack Palevichbbf8ab52009-05-11 11:54:30 -070047namespace acc {
48
Jack Palevichac0e95e2009-05-29 13:53:44 -070049class ErrorSink {
50public:
51 void error(const char *fmt, ...) {
52 va_list ap;
53 va_start(ap, fmt);
54 verror(fmt, ap);
55 va_end(ap);
56 }
57
58 virtual void verror(const char* fmt, va_list ap) = 0;
59};
60
61class Compiler : public ErrorSink {
Jack Palevich21a15a22009-05-11 14:49:29 -070062 class CodeBuf {
Jack Palevich653f42d2009-05-28 17:15:32 -070063 char* ind; // Output code pointer
Jack Palevich21a15a22009-05-11 14:49:29 -070064 char* pProgramBase;
Jack Palevichac0e95e2009-05-29 13:53:44 -070065 ErrorSink* mErrorSink;
66 int mSize;
Jack Palevichf0cbc922009-05-08 16:35:13 -070067
Jack Palevich21a15a22009-05-11 14:49:29 -070068 void release() {
69 if (pProgramBase != 0) {
70 free(pProgramBase);
71 pProgramBase = 0;
Jack Palevichae54f1f2009-05-08 14:54:15 -070072 }
Jack Palevich21a15a22009-05-11 14:49:29 -070073 }
74
Jack Palevichac0e95e2009-05-29 13:53:44 -070075 void check(int n) {
76 int newSize = ind - pProgramBase + n;
77 if (newSize > mSize) {
78 if (mErrorSink) {
79 mErrorSink->error("Code too large: %d bytes", newSize);
80 }
81 }
82 }
83
Jack Palevich21a15a22009-05-11 14:49:29 -070084 public:
85 CodeBuf() {
86 pProgramBase = 0;
87 ind = 0;
Jack Palevichac0e95e2009-05-29 13:53:44 -070088 mErrorSink = 0;
89 mSize = 0;
Jack Palevich21a15a22009-05-11 14:49:29 -070090 }
91
92 ~CodeBuf() {
93 release();
94 }
95
96 void init(int size) {
97 release();
Jack Palevichac0e95e2009-05-29 13:53:44 -070098 mSize = size;
Jack Palevich21a15a22009-05-11 14:49:29 -070099 pProgramBase = (char*) calloc(1, size);
100 ind = pProgramBase;
101 }
102
Jack Palevichac0e95e2009-05-29 13:53:44 -0700103 void setErrorSink(ErrorSink* pErrorSink) {
104 mErrorSink = pErrorSink;
105 }
106
Jack Palevich546b2242009-05-13 15:10:04 -0700107 int o4(int n) {
Jack Palevichac0e95e2009-05-29 13:53:44 -0700108 check(4);
Jack Palevich8b0624c2009-05-20 12:12:06 -0700109 intptr_t result = (intptr_t) ind;
Jack Palevich546b2242009-05-13 15:10:04 -0700110 * (int*) ind = n;
111 ind += 4;
112 return result;
113 }
114
Jack Palevich21a15a22009-05-11 14:49:29 -0700115 /*
116 * Output a byte. Handles all values, 0..ff.
117 */
118 void ob(int n) {
Jack Palevichac0e95e2009-05-29 13:53:44 -0700119 check(1);
Jack Palevich21a15a22009-05-11 14:49:29 -0700120 *ind++ = n;
121 }
122
Jack Palevich21a15a22009-05-11 14:49:29 -0700123 inline void* getBase() {
124 return (void*) pProgramBase;
125 }
126
Jack Palevich8b0624c2009-05-20 12:12:06 -0700127 intptr_t getSize() {
Jack Palevich21a15a22009-05-11 14:49:29 -0700128 return ind - pProgramBase;
129 }
130
Jack Palevich8b0624c2009-05-20 12:12:06 -0700131 intptr_t getPC() {
132 return (intptr_t) ind;
Jack Palevich21a15a22009-05-11 14:49:29 -0700133 }
134 };
135
Jack Palevich1cdef202009-05-22 12:06:27 -0700136 /**
137 * A code generator creates an in-memory program, generating the code on
138 * the fly. There is one code generator implementation for each supported
139 * architecture.
140 *
141 * The code generator implements the following abstract machine:
142 * R0 - the main accumulator.
143 * R1 - the secondary accumulator.
144 * FP - a frame pointer for accessing function arguments and local
145 * variables.
146 * SP - a stack pointer for storing intermediate results while evaluating
147 * expressions. The stack pointer grows downwards.
148 *
149 * The function calling convention is that all arguments are placed on the
150 * stack such that the first argument has the lowest address.
151 * After the call, the result is in R0. The caller is responsible for
152 * removing the arguments from the stack.
153 * The R0 and R1 registers are not saved across function calls. The
154 * FP and SP registers are saved.
155 */
156
Jack Palevich21a15a22009-05-11 14:49:29 -0700157 class CodeGenerator {
158 public:
Jack Palevichac0e95e2009-05-29 13:53:44 -0700159 CodeGenerator() {
160 mErrorSink = 0;
161 pCodeBuf = 0;
162 }
Jack Palevich21a15a22009-05-11 14:49:29 -0700163 virtual ~CodeGenerator() {}
164
Jack Palevich22305132009-05-13 10:58:45 -0700165 virtual void init(CodeBuf* pCodeBuf) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700166 this->pCodeBuf = pCodeBuf;
Jack Palevichac0e95e2009-05-29 13:53:44 -0700167 pCodeBuf->setErrorSink(mErrorSink);
168 }
169
170 void setErrorSink(ErrorSink* pErrorSink) {
171 mErrorSink = pErrorSink;
172 if (pCodeBuf) {
173 pCodeBuf->setErrorSink(mErrorSink);
174 }
Jack Palevich21a15a22009-05-11 14:49:29 -0700175 }
176
Jack Palevich1cdef202009-05-22 12:06:27 -0700177 /* Emit a function prolog.
178 * argCount is the number of arguments.
179 * Save the old value of the FP.
180 * Set the new value of the FP.
181 * Convert from the native platform calling convention to
182 * our stack-based calling convention. This may require
183 * pushing arguments from registers to the stack.
184 * Allocate "N" bytes of stack space. N isn't known yet, so
185 * just emit the instructions for adjusting the stack, and return
186 * the address to patch up. The patching will be done in
187 * functionExit().
188 * returns address to patch with local variable size.
Jack Palevich22305132009-05-13 10:58:45 -0700189 */
Jack Palevich546b2242009-05-13 15:10:04 -0700190 virtual int functionEntry(int argCount) = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700191
Jack Palevich1cdef202009-05-22 12:06:27 -0700192 /* Emit a function epilog.
193 * Restore the old SP and FP register values.
194 * Return to the calling function.
195 * argCount - the number of arguments to the function.
196 * localVariableAddress - returned from functionEntry()
197 * localVariableSize - the size in bytes of the local variables.
198 */
199 virtual void functionExit(int argCount, int localVariableAddress,
200 int localVariableSize) = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700201
Jack Palevich1cdef202009-05-22 12:06:27 -0700202 /* load immediate value to R0 */
Jack Palevich546b2242009-05-13 15:10:04 -0700203 virtual void li(int t) = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700204
Jack Palevich1cdef202009-05-22 12:06:27 -0700205 /* Jump to a target, and return the address of the word that
206 * holds the target data, in case it needs to be fixed up later.
207 */
Jack Palevich22305132009-05-13 10:58:45 -0700208 virtual int gjmp(int t) = 0;
209
Jack Palevich1cdef202009-05-22 12:06:27 -0700210 /* Test R0 and jump to a target if the test succeeds.
211 * l = 0: je, l == 1: jne
212 * Return the address of the word that holds the targed data, in
213 * case it needs to be fixed up later.
214 */
Jack Palevich22305132009-05-13 10:58:45 -0700215 virtual int gtst(bool l, int t) = 0;
216
Jack Palevich1cdef202009-05-22 12:06:27 -0700217 /* Compare R1 against R0, and store the boolean result in R0.
218 * op specifies the comparison.
219 */
Jack Palevich22305132009-05-13 10:58:45 -0700220 virtual void gcmp(int op) = 0;
221
Jack Palevich1cdef202009-05-22 12:06:27 -0700222 /* Perform the arithmetic op specified by op. R1 is the
223 * left argument, R0 is the right argument.
224 */
Jack Palevich546b2242009-05-13 15:10:04 -0700225 virtual void genOp(int op) = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700226
Jack Palevich1cdef202009-05-22 12:06:27 -0700227 /* Set R1 to 0.
228 */
229 virtual void clearR1() = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700230
Jack Palevich1cdef202009-05-22 12:06:27 -0700231 /* Push R0 onto the stack.
232 */
233 virtual void pushR0() = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700234
Jack Palevich1cdef202009-05-22 12:06:27 -0700235 /* Pop R1 off of the stack.
236 */
237 virtual void popR1() = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700238
Jack Palevich1cdef202009-05-22 12:06:27 -0700239 /* Store R0 to the address stored in R1.
240 * isInt is true if a whole 4-byte integer value
241 * should be stored, otherwise a 1-byte character
242 * value should be stored.
243 */
244 virtual void storeR0ToR1(bool isInt) = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700245
Jack Palevich1cdef202009-05-22 12:06:27 -0700246 /* Load R0 from the address stored in R0.
247 * isInt is true if a whole 4-byte integer value
248 * should be loaded, otherwise a 1-byte character
249 * value should be loaded.
250 */
251 virtual void loadR0FromR0(bool isInt) = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700252
Jack Palevich1cdef202009-05-22 12:06:27 -0700253 /* Load the absolute address of a variable to R0.
254 * If ea <= LOCAL, then this is a local variable, or an
255 * argument, addressed relative to FP.
256 * else it is an absolute global address.
257 */
258 virtual void leaR0(int ea) = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700259
Jack Palevich1cdef202009-05-22 12:06:27 -0700260 /* Store R0 to a variable.
261 * If ea <= LOCAL, then this is a local variable, or an
262 * argument, addressed relative to FP.
263 * else it is an absolute global address.
264 */
265 virtual void storeR0(int ea) = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700266
Jack Palevich1cdef202009-05-22 12:06:27 -0700267 /* load R0 from a variable.
268 * If ea <= LOCAL, then this is a local variable, or an
269 * argument, addressed relative to FP.
270 * else it is an absolute global address.
271 * If isIncDec is true, then the stored variable's value
272 * should be post-incremented or post-decremented, based
273 * on the value of op.
274 */
275 virtual void loadR0(int ea, bool isIncDec, int op) = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700276
Jack Palevich1cdef202009-05-22 12:06:27 -0700277 /* Emit code to adjust the stack for a function call. Return the
278 * label for the address of the instruction that adjusts the
279 * stack size. This will be passed as argument "a" to
280 * endFunctionCallArguments.
281 */
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700282 virtual int beginFunctionCallArguments() = 0;
283
Jack Palevich1cdef202009-05-22 12:06:27 -0700284 /* Emit code to store R0 to the stack at byte offset l.
285 */
286 virtual void storeR0ToArg(int l) = 0;
Jack Palevich7810bc92009-05-15 14:31:47 -0700287
Jack Palevich1cdef202009-05-22 12:06:27 -0700288 /* Patch the function call preamble.
289 * a is the address returned from beginFunctionCallArguments
290 * l is the number of bytes the arguments took on the stack.
291 * Typically you would also emit code to convert the argument
292 * list into whatever the native function calling convention is.
293 * On ARM for example you would pop the first 5 arguments into
294 * R0..R4
295 */
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700296 virtual void endFunctionCallArguments(int a, int l) = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700297
Jack Palevich1cdef202009-05-22 12:06:27 -0700298 /* Emit a call to an unknown function. The argument "symbol" needs to
299 * be stored in the location where the address should go. It forms
300 * a chain. The address will be patched later.
301 * Return the address of the word that has to be patched.
302 */
Jack Palevich22305132009-05-13 10:58:45 -0700303 virtual int callForward(int symbol) = 0;
304
Jack Palevich1cdef202009-05-22 12:06:27 -0700305 /* Call a function using PC-relative addressing. t is the PC-relative
306 * address of the function. It has already been adjusted for the
307 * architectural jump offset, so just store it as-is.
308 */
Jack Palevich22305132009-05-13 10:58:45 -0700309 virtual void callRelative(int t) = 0;
310
Jack Palevich1cdef202009-05-22 12:06:27 -0700311 /* Call a function pointer. L is the number of bytes the arguments
312 * take on the stack. The address of the function is stored at
313 * location SP + l.
314 */
Jack Palevich22305132009-05-13 10:58:45 -0700315 virtual void callIndirect(int l) = 0;
316
Jack Palevich1cdef202009-05-22 12:06:27 -0700317 /* Adjust SP after returning from a function call. l is the
318 * number of bytes of arguments stored on the stack. isIndirect
319 * is true if this was an indirect call. (In which case the
320 * address of the function is stored at location SP + l.)
321 */
Jack Palevich7810bc92009-05-15 14:31:47 -0700322 virtual void adjustStackAfterCall(int l, bool isIndirect) = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700323
Jack Palevich1cdef202009-05-22 12:06:27 -0700324 /* Print a disassembly of the assembled code to out. Return
325 * non-zero if there is an error.
326 */
Jack Palevicha6535612009-05-13 16:24:17 -0700327 virtual int disassemble(FILE* out) = 0;
328
Jack Palevich1cdef202009-05-22 12:06:27 -0700329 /* Generate a symbol at the current PC. t is the head of a
330 * linked list of addresses to patch.
331 */
Jack Paleviche7b59062009-05-19 17:12:17 -0700332 virtual void gsym(int t) = 0;
Jack Palevich21a15a22009-05-11 14:49:29 -0700333
Jack Palevich1cdef202009-05-22 12:06:27 -0700334 /*
335 * Do any cleanup work required at the end of a compile.
336 * For example, an instruction cache might need to be
337 * invalidated.
338 * Return non-zero if there is an error.
339 */
340 virtual int finishCompile() = 0;
Jack Palevich546b2242009-05-13 15:10:04 -0700341
Jack Palevicha6535612009-05-13 16:24:17 -0700342 /**
343 * Adjust relative branches by this amount.
344 */
345 virtual int jumpOffset() = 0;
346
Jack Palevich21a15a22009-05-11 14:49:29 -0700347 protected:
Jack Palevich21a15a22009-05-11 14:49:29 -0700348 /*
349 * Output a byte. Handles all values, 0..ff.
350 */
351 void ob(int n) {
352 pCodeBuf->ob(n);
353 }
354
Jack Palevich8b0624c2009-05-20 12:12:06 -0700355 intptr_t o4(int data) {
Jack Paleviche7b59062009-05-19 17:12:17 -0700356 return pCodeBuf->o4(data);
Jack Palevich21a15a22009-05-11 14:49:29 -0700357 }
358
Jack Palevich8b0624c2009-05-20 12:12:06 -0700359 intptr_t getBase() {
360 return (intptr_t) pCodeBuf->getBase();
Jack Palevicha6535612009-05-13 16:24:17 -0700361 }
362
Jack Palevich8b0624c2009-05-20 12:12:06 -0700363 intptr_t getPC() {
Jack Palevich21a15a22009-05-11 14:49:29 -0700364 return pCodeBuf->getPC();
365 }
Jack Palevich1cdef202009-05-22 12:06:27 -0700366
367 intptr_t getSize() {
368 return pCodeBuf->getSize();
369 }
Jack Palevichac0e95e2009-05-29 13:53:44 -0700370
371 void error(const char* fmt,...) {
372 va_list ap;
373 va_start(ap, fmt);
374 mErrorSink->verror(fmt, ap);
375 va_end(ap);
376 }
Jack Palevich21a15a22009-05-11 14:49:29 -0700377 private:
378 CodeBuf* pCodeBuf;
Jack Palevichac0e95e2009-05-29 13:53:44 -0700379 ErrorSink* mErrorSink;
Jack Palevich21a15a22009-05-11 14:49:29 -0700380 };
381
Jack Paleviche7b59062009-05-19 17:12:17 -0700382#ifdef PROVIDE_ARM_CODEGEN
383
Jack Palevich22305132009-05-13 10:58:45 -0700384 class ARMCodeGenerator : public CodeGenerator {
385 public:
386 ARMCodeGenerator() {}
387 virtual ~ARMCodeGenerator() {}
388
389 /* returns address to patch with local variable size
390 */
Jack Palevich546b2242009-05-13 15:10:04 -0700391 virtual int functionEntry(int argCount) {
Jack Palevich09555c72009-05-27 12:25:55 -0700392 LOG_API(stderr, "functionEntry(%d);\n", argCount);
Jack Palevich69796b62009-05-14 15:42:26 -0700393 // sp -> arg4 arg5 ...
394 // Push our register-based arguments back on the stack
395 if (argCount > 0) {
396 int regArgCount = argCount <= 4 ? argCount : 4;
397 o4(0xE92D0000 | ((1 << argCount) - 1)); // stmfd sp!, {}
398 }
399 // sp -> arg0 arg1 ...
400 o4(0xE92D4800); // stmfd sp!, {fp, lr}
401 // sp, fp -> oldfp, retadr, arg0 arg1 ....
402 o4(0xE1A0B00D); // mov fp, sp
403 return o4(0xE24DD000); // sub sp, sp, # <local variables>
Jack Palevich22305132009-05-13 10:58:45 -0700404 }
405
Jack Palevich546b2242009-05-13 15:10:04 -0700406 virtual void functionExit(int argCount, int localVariableAddress, int localVariableSize) {
Jack Palevich09555c72009-05-27 12:25:55 -0700407 LOG_API("functionExit(%d, %d, %d);\n", argCount, localVariableAddress, localVariableSize);
Jack Palevich69796b62009-05-14 15:42:26 -0700408 // Patch local variable allocation code:
409 if (localVariableSize < 0 || localVariableSize > 255) {
Jack Palevich8de461d2009-05-14 17:21:45 -0700410 error("localVariables out of range: %d", localVariableSize);
Jack Palevich546b2242009-05-13 15:10:04 -0700411 }
Jack Palevich69796b62009-05-14 15:42:26 -0700412 *(char*) (localVariableAddress) = localVariableSize;
413
414 // sp -> locals .... fp -> oldfp, retadr, arg0, arg1, ...
415 o4(0xE1A0E00B); // mov lr, fp
416 o4(0xE59BB000); // ldr fp, [fp]
417 o4(0xE28ED004); // add sp, lr, #4
418 // sp -> retadr, arg0, ...
419 o4(0xE8BD4000); // ldmfd sp!, {lr}
420 // sp -> arg0 ....
421 if (argCount > 0) {
422 // We store the PC into the lr so we can adjust the sp before
Jack Palevich8de461d2009-05-14 17:21:45 -0700423 // returning. We need to pull off the registers we pushed
Jack Palevich69796b62009-05-14 15:42:26 -0700424 // earlier. We don't need to actually store them anywhere,
425 // just adjust the stack.
426 int regArgCount = argCount <= 4 ? argCount : 4;
427 o4(0xE28DD000 | (regArgCount << 2)); // add sp, sp, #argCount << 2
428 }
429 o4(0xE12FFF1E); // bx lr
Jack Palevich22305132009-05-13 10:58:45 -0700430 }
431
432 /* load immediate value */
Jack Palevich546b2242009-05-13 15:10:04 -0700433 virtual void li(int t) {
Jack Palevich09555c72009-05-27 12:25:55 -0700434 LOG_API("li(%d);\n", t);
Jack Palevicha6535612009-05-13 16:24:17 -0700435 if (t >= 0 && t < 255) {
Jack Palevich69796b62009-05-14 15:42:26 -0700436 o4(0xE3A00000 + t); // mov r0, #0
Jack Palevicha6535612009-05-13 16:24:17 -0700437 } else if (t >= -256 && t < 0) {
438 // mvn means move constant ^ ~0
Jack Palevich69796b62009-05-14 15:42:26 -0700439 o4(0xE3E00001 - t); // mvn r0, #0
Jack Palevicha6535612009-05-13 16:24:17 -0700440 } else {
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700441 o4(0xE51F0000); // ldr r0, .L3
442 o4(0xEA000000); // b .L99
443 o4(t); // .L3: .word 0
444 // .L99:
Jack Palevicha6535612009-05-13 16:24:17 -0700445 }
Jack Palevich22305132009-05-13 10:58:45 -0700446 }
447
448 virtual int gjmp(int t) {
Jack Palevich09555c72009-05-27 12:25:55 -0700449 LOG_API("gjmp(%d);\n", t);
Jack Palevich8de461d2009-05-14 17:21:45 -0700450 return o4(0xEA000000 | encodeAddress(t)); // b .L33
Jack Palevich22305132009-05-13 10:58:45 -0700451 }
452
453 /* l = 0: je, l == 1: jne */
454 virtual int gtst(bool l, int t) {
Jack Palevich09555c72009-05-27 12:25:55 -0700455 LOG_API("gtst(%d, %d);\n", l, t);
Jack Palevich8de461d2009-05-14 17:21:45 -0700456 o4(0xE3500000); // cmp r0,#0
457 int branch = l ? 0x1A000000 : 0x0A000000; // bne : beq
458 return o4(branch | encodeAddress(t));
Jack Palevich22305132009-05-13 10:58:45 -0700459 }
460
461 virtual void gcmp(int op) {
Jack Palevich09555c72009-05-27 12:25:55 -0700462 LOG_API("gcmp(%d);\n", op);
Jack Palevich8de461d2009-05-14 17:21:45 -0700463 o4(0xE1510000); // cmp r1, r1
464 switch(op) {
465 case OP_EQUALS:
466 o4(0x03A00001); // moveq r0,#1
467 o4(0x13A00000); // movne r0,#0
468 break;
469 case OP_NOT_EQUALS:
470 o4(0x03A00000); // moveq r0,#0
471 o4(0x13A00001); // movne r0,#1
472 break;
473 case OP_LESS_EQUAL:
474 o4(0xD3A00001); // movle r0,#1
475 o4(0xC3A00000); // movgt r0,#0
476 break;
477 case OP_GREATER:
478 o4(0xD3A00000); // movle r0,#0
479 o4(0xC3A00001); // movgt r0,#1
480 break;
481 case OP_GREATER_EQUAL:
482 o4(0xA3A00001); // movge r0,#1
483 o4(0xB3A00000); // movlt r0,#0
484 break;
485 case OP_LESS:
486 o4(0xA3A00000); // movge r0,#0
487 o4(0xB3A00001); // movlt r0,#1
488 break;
489 default:
490 error("Unknown comparison op %d", op);
491 break;
492 }
Jack Palevich22305132009-05-13 10:58:45 -0700493 }
494
Jack Palevich546b2242009-05-13 15:10:04 -0700495 virtual void genOp(int op) {
Jack Palevich09555c72009-05-27 12:25:55 -0700496 LOG_API("genOp(%d);\n", op);
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700497 switch(op) {
498 case OP_MUL:
499 o4(0x0E0000091); // mul r0,r1,r0
500 break;
Jack Palevich3d474a72009-05-15 15:12:38 -0700501 case OP_DIV:
502 callRuntime(runtime_DIV);
503 break;
504 case OP_MOD:
505 callRuntime(runtime_MOD);
506 break;
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700507 case OP_PLUS:
508 o4(0xE0810000); // add r0,r1,r0
509 break;
510 case OP_MINUS:
511 o4(0xE0410000); // sub r0,r1,r0
512 break;
513 case OP_SHIFT_LEFT:
514 o4(0xE1A00011); // lsl r0,r1,r0
515 break;
516 case OP_SHIFT_RIGHT:
517 o4(0xE1A00051); // asr r0,r1,r0
518 break;
519 case OP_BIT_AND:
520 o4(0xE0010000); // and r0,r1,r0
521 break;
522 case OP_BIT_XOR:
523 o4(0xE0210000); // eor r0,r1,r0
524 break;
525 case OP_BIT_OR:
526 o4(0xE1810000); // orr r0,r1,r0
527 break;
528 case OP_BIT_NOT:
529 o4(0xE1E00000); // mvn r0, r0
530 break;
531 default:
Jack Palevich69796b62009-05-14 15:42:26 -0700532 error("Unimplemented op %d\n", op);
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700533 break;
534 }
Jack Palevich22305132009-05-13 10:58:45 -0700535#if 0
536 o(decodeOp(op));
537 if (op == OP_MOD)
538 o(0x92); /* xchg %edx, %eax */
539#endif
540 }
541
Jack Palevich1cdef202009-05-22 12:06:27 -0700542 virtual void clearR1() {
Jack Palevich09555c72009-05-27 12:25:55 -0700543 LOG_API("clearR1();\n");
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700544 o4(0xE3A01000); // mov r1, #0
Jack Palevich22305132009-05-13 10:58:45 -0700545 }
546
Jack Palevich1cdef202009-05-22 12:06:27 -0700547 virtual void pushR0() {
Jack Palevich09555c72009-05-27 12:25:55 -0700548 LOG_API("pushR0();\n");
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700549 o4(0xE92D0001); // stmfd sp!,{r0}
Jack Palevich22305132009-05-13 10:58:45 -0700550 }
551
Jack Palevich1cdef202009-05-22 12:06:27 -0700552 virtual void popR1() {
Jack Palevich09555c72009-05-27 12:25:55 -0700553 LOG_API("popR1();\n");
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700554 o4(0xE8BD0002); // ldmfd sp!,{r1}
Jack Palevich22305132009-05-13 10:58:45 -0700555 }
556
Jack Palevich1cdef202009-05-22 12:06:27 -0700557 virtual void storeR0ToR1(bool isInt) {
Jack Palevich09555c72009-05-27 12:25:55 -0700558 LOG_API("storeR0ToR1(%d);\n", isInt);
Jack Palevichbd894902009-05-14 19:35:31 -0700559 if (isInt) {
560 o4(0xE5810000); // str r0, [r1]
561 } else {
562 o4(0xE5C10000); // strb r0, [r1]
563 }
Jack Palevich22305132009-05-13 10:58:45 -0700564 }
565
Jack Palevich1cdef202009-05-22 12:06:27 -0700566 virtual void loadR0FromR0(bool isInt) {
Jack Palevich09555c72009-05-27 12:25:55 -0700567 LOG_API("loadR0FromR0(%d);\n", isInt);
Jack Palevich22305132009-05-13 10:58:45 -0700568 if (isInt)
Jack Palevich69796b62009-05-14 15:42:26 -0700569 o4(0xE5900000); // ldr r0, [r0]
Jack Palevich22305132009-05-13 10:58:45 -0700570 else
Jack Palevich69796b62009-05-14 15:42:26 -0700571 o4(0xE5D00000); // ldrb r0, [r0]
Jack Palevich22305132009-05-13 10:58:45 -0700572 }
573
Jack Palevich1cdef202009-05-22 12:06:27 -0700574 virtual void leaR0(int ea) {
Jack Palevich09555c72009-05-27 12:25:55 -0700575 LOG_API("leaR0(%d);\n", ea);
Jack Palevich4d93f302009-05-15 13:30:00 -0700576 if (ea < LOCAL) {
577 // Local, fp relative
578 if (ea < -1023 || ea > 1023 || ((ea & 3) != 0)) {
579 error("Offset out of range: %08x", ea);
580 }
581 if (ea < 0) {
582 o4(0xE24B0F00 | (0xff & ((-ea) >> 2))); // sub r0, fp, #ea
583 } else {
584 o4(0xE28B0F00 | (0xff & (ea >> 2))); // add r0, fp, #ea
585 }
Jack Palevichbd894902009-05-14 19:35:31 -0700586 } else {
Jack Palevich4d93f302009-05-15 13:30:00 -0700587 // Global, absolute.
588 o4(0xE59F0000); // ldr r0, .L1
589 o4(0xEA000000); // b .L99
590 o4(ea); // .L1: .word 0
591 // .L99:
Jack Palevichbd894902009-05-14 19:35:31 -0700592 }
Jack Palevich22305132009-05-13 10:58:45 -0700593 }
594
Jack Palevich1cdef202009-05-22 12:06:27 -0700595 virtual void storeR0(int ea) {
Jack Palevich09555c72009-05-27 12:25:55 -0700596 LOG_API("storeR0(%d);\n", ea);
Jack Palevich4d93f302009-05-15 13:30:00 -0700597 if (ea < LOCAL) {
598 // Local, fp relative
599 if (ea < -4095 || ea > 4095) {
600 error("Offset out of range: %08x", ea);
601 }
602 if (ea < 0) {
603 o4(0xE50B0000 | (0xfff & (-ea))); // str r0, [fp,#-ea]
604 } else {
605 o4(0xE58B0000 | (0xfff & ea)); // str r0, [fp,#ea]
606 }
607 } else{
608 // Global, absolute
609 o4(0xE59F1000); // ldr r1, .L1
610 o4(0xEA000000); // b .L99
611 o4(ea); // .L1: .word 0
612 o4(0xE5810000); // .L99: str r0, [r1]
Jack Palevich69796b62009-05-14 15:42:26 -0700613 }
Jack Palevich22305132009-05-13 10:58:45 -0700614 }
615
Jack Palevich1cdef202009-05-22 12:06:27 -0700616 virtual void loadR0(int ea, bool isIncDec, int op) {
Jack Palevich09555c72009-05-27 12:25:55 -0700617 LOG_API("loadR0(%d, %d, %d);\n", ea, isIncDec, op);
Jack Palevich4d93f302009-05-15 13:30:00 -0700618 if (ea < LOCAL) {
619 // Local, fp relative
620 if (ea < -4095 || ea > 4095) {
621 error("Offset out of range: %08x", ea);
622 }
623 if (ea < 0) {
624 o4(0xE51B0000 | (0xfff & (-ea))); // ldr r0, [fp,#-ea]
625 } else {
626 o4(0xE59B0000 | (0xfff & ea)); // ldr r0, [fp,#ea]
627 }
Jack Palevich69796b62009-05-14 15:42:26 -0700628 } else {
Jack Palevich4d93f302009-05-15 13:30:00 -0700629 // Global, absolute
630 o4(0xE59F2000); // ldr r2, .L1
631 o4(0xEA000000); // b .L99
632 o4(ea); // .L1: .word ea
633 o4(0xE5920000); // .L99: ldr r0, [r2]
Jack Palevich69796b62009-05-14 15:42:26 -0700634 }
Jack Palevich22305132009-05-13 10:58:45 -0700635
Jack Palevich4d93f302009-05-15 13:30:00 -0700636 if (isIncDec) {
637 switch (op) {
638 case OP_INCREMENT:
639 o4(0xE2801001); // add r1, r0, #1
640 break;
641 case OP_DECREMENT:
642 o4(0xE2401001); // sub r1, r0, #1
643 break;
644 default:
645 error("unknown opcode: %d", op);
646 }
647 if (ea < LOCAL) {
648 // Local, fp relative
649 // Don't need range check, was already checked above
650 if (ea < 0) {
651 o4(0xE50B1000 | (0xfff & (-ea))); // str r1, [fp,#-ea]
652 } else {
653 o4(0xE58B1000 | (0xfff & ea)); // str r1, [fp,#ea]
654 }
655 } else{
656 // Global, absolute
657 // r2 is already set up from before.
658 o4(0xE5821000); // str r1, [r2]
659 }
Jack Palevichbd894902009-05-14 19:35:31 -0700660 }
Jack Palevich22305132009-05-13 10:58:45 -0700661 }
662
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700663 virtual int beginFunctionCallArguments() {
Jack Palevich09555c72009-05-27 12:25:55 -0700664 LOG_API("beginFunctionCallArguments();\n");
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700665 return o4(0xE24DDF00); // Placeholder
666 }
667
Jack Palevich1cdef202009-05-22 12:06:27 -0700668 virtual void storeR0ToArg(int l) {
Jack Palevich09555c72009-05-27 12:25:55 -0700669 LOG_API("storeR0ToArg(%d);\n", l);
Jack Palevich7810bc92009-05-15 14:31:47 -0700670 if (l < 0 || l > 4096-4) {
671 error("l out of range for stack offset: 0x%08x", l);
672 }
673 o4(0xE58D0000 + l); // str r0, [sp, #4]
674 }
675
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700676 virtual void endFunctionCallArguments(int a, int l) {
Jack Palevich09555c72009-05-27 12:25:55 -0700677 LOG_API("endFunctionCallArguments(0x%08x, %d);\n", a, l);
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700678 if (l < 0 || l > 0x3FC) {
679 error("L out of range for stack adjustment: 0x%08x", l);
680 }
681 * (int*) a = 0xE24DDF00 | (l >> 2); // sub sp, sp, #0 << 2
682 int argCount = l >> 2;
683 if (argCount > 0) {
684 int regArgCount = argCount > 4 ? 4 : argCount;
685 o4(0xE8BD0000 | ((1 << regArgCount) - 1)); // ldmfd sp!,{}
686 }
Jack Palevich22305132009-05-13 10:58:45 -0700687 }
688
Jack Palevich22305132009-05-13 10:58:45 -0700689 virtual int callForward(int symbol) {
Jack Palevich09555c72009-05-27 12:25:55 -0700690 LOG_API("callForward(%d);\n", symbol);
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700691 // Forward calls are always short (local)
692 return o4(0xEB000000 | encodeAddress(symbol));
Jack Palevich22305132009-05-13 10:58:45 -0700693 }
694
695 virtual void callRelative(int t) {
Jack Palevich09555c72009-05-27 12:25:55 -0700696 LOG_API("callRelative(%d);\n", t);
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700697 int abs = t + getPC() + jumpOffset();
Jack Palevichac0e95e2009-05-29 13:53:44 -0700698 LOG_API("abs=%d (0x%08x)\n", abs, abs);
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700699 if (t >= - (1 << 25) && t < (1 << 25)) {
700 o4(0xEB000000 | encodeAddress(t));
701 } else {
702 // Long call.
703 o4(0xE59FC000); // ldr r12, .L1
704 o4(0xEA000000); // b .L99
Jack Palevichbd894902009-05-14 19:35:31 -0700705 o4(t - 12); // .L1: .word 0
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700706 o4(0xE08CC00F); // .L99: add r12,pc
707 o4(0xE12FFF3C); // blx r12
708 }
Jack Palevich22305132009-05-13 10:58:45 -0700709 }
710
711 virtual void callIndirect(int l) {
Jack Palevich09555c72009-05-27 12:25:55 -0700712 LOG_API("callIndirect(%d);\n", l);
Jack Palevich7810bc92009-05-15 14:31:47 -0700713 int argCount = l >> 2;
714 int poppedArgs = argCount > 4 ? 4 : argCount;
715 int adjustedL = l - (poppedArgs << 2);
716 if (adjustedL < 0 || adjustedL > 4096-4) {
717 error("l out of range for stack offset: 0x%08x", l);
718 }
719 o4(0xE59DC000 | (0xfff & adjustedL)); // ldr r12, [sp,#adjustedL]
720 o4(0xE12FFF3C); // blx r12
Jack Palevich22305132009-05-13 10:58:45 -0700721 }
722
Jack Palevich7810bc92009-05-15 14:31:47 -0700723 virtual void adjustStackAfterCall(int l, bool isIndirect) {
Jack Palevich09555c72009-05-27 12:25:55 -0700724 LOG_API("adjustStackAfterCall(%d, %d);\n", l, isIndirect);
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700725 int argCount = l >> 2;
Jack Palevich7810bc92009-05-15 14:31:47 -0700726 int stackArgs = argCount > 4 ? argCount - 4 : 0;
727 int stackUse = stackArgs + (isIndirect ? 1 : 0);
728 if (stackUse) {
729 if (stackUse < 0 || stackUse > 255) {
730 error("L out of range for stack adjustment: 0x%08x", l);
731 }
732 o4(0xE28DDF00 | stackUse); // add sp, sp, #stackUse << 2
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700733 }
Jack Palevich22305132009-05-13 10:58:45 -0700734 }
735
Jack Palevicha6535612009-05-13 16:24:17 -0700736 virtual int jumpOffset() {
Jack Palevichbd894902009-05-14 19:35:31 -0700737 return 8;
Jack Palevicha6535612009-05-13 16:24:17 -0700738 }
739
740 /* output a symbol and patch all calls to it */
741 virtual void gsym(int t) {
Jack Palevich09555c72009-05-27 12:25:55 -0700742 LOG_API("gsym(0x%x)\n", t);
Jack Palevicha6535612009-05-13 16:24:17 -0700743 int n;
744 int base = getBase();
745 int pc = getPC();
Jack Palevich09555c72009-05-27 12:25:55 -0700746 LOG_API("pc = 0x%x\n", pc);
Jack Palevicha6535612009-05-13 16:24:17 -0700747 while (t) {
748 int data = * (int*) t;
749 int decodedOffset = ((BRANCH_REL_ADDRESS_MASK & data) << 2);
750 if (decodedOffset == 0) {
751 n = 0;
752 } else {
753 n = base + decodedOffset; /* next value */
754 }
755 *(int *) t = (data & ~BRANCH_REL_ADDRESS_MASK)
756 | encodeRelAddress(pc - t - 8);
757 t = n;
758 }
759 }
760
Jack Palevich1cdef202009-05-22 12:06:27 -0700761 virtual int finishCompile() {
762#if defined(__arm__)
763 const long base = long(getBase());
764 const long curr = long(getPC());
765 int err = cacheflush(base, curr, 0);
766 return err;
767#else
768 return 0;
769#endif
770 }
771
Jack Palevicha6535612009-05-13 16:24:17 -0700772 virtual int disassemble(FILE* out) {
Jack Palevich09555c72009-05-27 12:25:55 -0700773#ifdef ENABLE_ARM_DISASSEMBLY
774 disasmOut = out;
Jack Palevicha6535612009-05-13 16:24:17 -0700775 disasm_interface_t di;
776 di.di_readword = disassemble_readword;
777 di.di_printaddr = disassemble_printaddr;
778 di.di_printf = disassemble_printf;
779
780 int base = getBase();
781 int pc = getPC();
782 for(int i = base; i < pc; i += 4) {
783 fprintf(out, "%08x: %08x ", i, *(int*) i);
784 ::disasm(&di, i, 0);
785 }
Jack Palevich09555c72009-05-27 12:25:55 -0700786#endif
Jack Palevicha6535612009-05-13 16:24:17 -0700787 return 0;
788 }
Jack Palevich7810bc92009-05-15 14:31:47 -0700789
Jack Palevich22305132009-05-13 10:58:45 -0700790 private:
Jack Palevicha6535612009-05-13 16:24:17 -0700791 static FILE* disasmOut;
792
793 static u_int
794 disassemble_readword(u_int address)
795 {
796 return(*((u_int *)address));
797 }
798
799 static void
800 disassemble_printaddr(u_int address)
801 {
802 fprintf(disasmOut, "0x%08x", address);
803 }
804
805 static void
806 disassemble_printf(const char *fmt, ...) {
807 va_list ap;
808 va_start(ap, fmt);
809 vfprintf(disasmOut, fmt, ap);
810 va_end(ap);
811 }
812
813 static const int BRANCH_REL_ADDRESS_MASK = 0x00ffffff;
814
815 /** Encode a relative address that might also be
816 * a label.
817 */
818 int encodeAddress(int value) {
819 int base = getBase();
820 if (value >= base && value <= getPC() ) {
821 // This is a label, encode it relative to the base.
822 value = value - base;
823 }
824 return encodeRelAddress(value);
825 }
826
827 int encodeRelAddress(int value) {
828 return BRANCH_REL_ADDRESS_MASK & (value >> 2);
829 }
Jack Palevich22305132009-05-13 10:58:45 -0700830
Jack Palevich3d474a72009-05-15 15:12:38 -0700831 typedef int (*int2FnPtr)(int a, int b);
832 void callRuntime(int2FnPtr fn) {
833 o4(0xE59F2000); // ldr r2, .L1
834 o4(0xEA000000); // b .L99
835 o4((int) fn); //.L1: .word fn
836 o4(0xE12FFF32); //.L99: blx r2
837 }
838
839 static int runtime_DIV(int a, int b) {
840 return b / a;
841 }
842
843 static int runtime_MOD(int a, int b) {
844 return b % a;
845 }
Jack Palevich22305132009-05-13 10:58:45 -0700846 };
847
Jack Palevich09555c72009-05-27 12:25:55 -0700848#endif // PROVIDE_ARM_CODEGEN
Jack Paleviche7b59062009-05-19 17:12:17 -0700849
850#ifdef PROVIDE_X86_CODEGEN
851
Jack Palevich21a15a22009-05-11 14:49:29 -0700852 class X86CodeGenerator : public CodeGenerator {
853 public:
854 X86CodeGenerator() {}
855 virtual ~X86CodeGenerator() {}
856
Jack Palevichbf42c9c2009-05-12 12:48:35 -0700857 /* returns address to patch with local variable size
858 */
Jack Palevich546b2242009-05-13 15:10:04 -0700859 virtual int functionEntry(int argCount) {
Jack Palevichbf42c9c2009-05-12 12:48:35 -0700860 o(0xe58955); /* push %ebp, mov %esp, %ebp */
861 return oad(0xec81, 0); /* sub $xxx, %esp */
862 }
863
Jack Palevich546b2242009-05-13 15:10:04 -0700864 virtual void functionExit(int argCount, int localVariableAddress, int localVariableSize) {
Jack Palevichbf42c9c2009-05-12 12:48:35 -0700865 o(0xc3c9); /* leave, ret */
Jack Palevich546b2242009-05-13 15:10:04 -0700866 *(int *) localVariableAddress = localVariableSize; /* save local variables */
Jack Palevichbf42c9c2009-05-12 12:48:35 -0700867 }
868
Jack Palevich21a15a22009-05-11 14:49:29 -0700869 /* load immediate value */
Jack Palevich546b2242009-05-13 15:10:04 -0700870 virtual void li(int t) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700871 oad(0xb8, t); /* mov $xx, %eax */
872 }
873
Jack Palevich22305132009-05-13 10:58:45 -0700874 virtual int gjmp(int t) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700875 return psym(0xe9, t);
876 }
877
878 /* l = 0: je, l == 1: jne */
Jack Palevich22305132009-05-13 10:58:45 -0700879 virtual int gtst(bool l, int t) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700880 o(0x0fc085); /* test %eax, %eax, je/jne xxx */
881 return psym(0x84 + l, t);
882 }
883
Jack Palevich22305132009-05-13 10:58:45 -0700884 virtual void gcmp(int op) {
Jack Palevichbf42c9c2009-05-12 12:48:35 -0700885 int t = decodeOp(op);
Jack Palevich21a15a22009-05-11 14:49:29 -0700886 o(0xc139); /* cmp %eax,%ecx */
887 li(0);
888 o(0x0f); /* setxx %al */
889 o(t + 0x90);
890 o(0xc0);
891 }
892
Jack Palevich546b2242009-05-13 15:10:04 -0700893 virtual void genOp(int op) {
Jack Palevichbf42c9c2009-05-12 12:48:35 -0700894 o(decodeOp(op));
895 if (op == OP_MOD)
896 o(0x92); /* xchg %edx, %eax */
897 }
898
Jack Palevich1cdef202009-05-22 12:06:27 -0700899 virtual void clearR1() {
Jack Palevich21a15a22009-05-11 14:49:29 -0700900 oad(0xb9, 0); /* movl $0, %ecx */
901 }
902
Jack Palevich1cdef202009-05-22 12:06:27 -0700903 virtual void pushR0() {
Jack Palevich21a15a22009-05-11 14:49:29 -0700904 o(0x50); /* push %eax */
905 }
906
Jack Palevich1cdef202009-05-22 12:06:27 -0700907 virtual void popR1() {
Jack Palevich21a15a22009-05-11 14:49:29 -0700908 o(0x59); /* pop %ecx */
Jack Palevichbf42c9c2009-05-12 12:48:35 -0700909 }
910
Jack Palevich1cdef202009-05-22 12:06:27 -0700911 virtual void storeR0ToR1(bool isInt) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700912 o(0x0188 + isInt); /* movl %eax/%al, (%ecx) */
913 }
914
Jack Palevich1cdef202009-05-22 12:06:27 -0700915 virtual void loadR0FromR0(bool isInt) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700916 if (isInt)
917 o(0x8b); /* mov (%eax), %eax */
918 else
919 o(0xbe0f); /* movsbl (%eax), %eax */
920 ob(0); /* add zero in code */
921 }
922
Jack Palevich1cdef202009-05-22 12:06:27 -0700923 virtual void leaR0(int ea) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700924 gmov(10, ea); /* leal EA, %eax */
925 }
926
Jack Palevich1cdef202009-05-22 12:06:27 -0700927 virtual void storeR0(int ea) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700928 gmov(6, ea); /* mov %eax, EA */
929 }
930
Jack Palevich1cdef202009-05-22 12:06:27 -0700931 virtual void loadR0(int ea, bool isIncDec, int op) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700932 gmov(8, ea); /* mov EA, %eax */
Jack Palevich4d93f302009-05-15 13:30:00 -0700933 if (isIncDec) {
934 /* Implement post-increment or post decrement.
935 */
936 gmov(0, ea); /* 83 ADD */
937 o(decodeOp(op));
938 }
Jack Palevich21a15a22009-05-11 14:49:29 -0700939 }
940
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700941 virtual int beginFunctionCallArguments() {
Jack Palevich21a15a22009-05-11 14:49:29 -0700942 return oad(0xec81, 0); /* sub $xxx, %esp */
943 }
944
Jack Palevich1cdef202009-05-22 12:06:27 -0700945 virtual void storeR0ToArg(int l) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700946 oad(0x248489, l); /* movl %eax, xxx(%esp) */
947 }
948
Jack Palevich7810bc92009-05-15 14:31:47 -0700949 virtual void endFunctionCallArguments(int a, int l) {
950 * (int*) a = l;
951 }
952
Jack Palevich22305132009-05-13 10:58:45 -0700953 virtual int callForward(int symbol) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700954 return psym(0xe8, symbol); /* call xxx */
955 }
956
Jack Palevich22305132009-05-13 10:58:45 -0700957 virtual void callRelative(int t) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700958 psym(0xe8, t); /* call xxx */
959 }
960
Jack Palevich22305132009-05-13 10:58:45 -0700961 virtual void callIndirect(int l) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700962 oad(0x2494ff, l); /* call *xxx(%esp) */
963 }
964
Jack Palevich7810bc92009-05-15 14:31:47 -0700965 virtual void adjustStackAfterCall(int l, bool isIndirect) {
966 if (isIndirect) {
967 l += 4;
968 }
Jack Palevich21a15a22009-05-11 14:49:29 -0700969 oad(0xc481, l); /* add $xxx, %esp */
970 }
971
Jack Palevicha6535612009-05-13 16:24:17 -0700972 virtual int jumpOffset() {
973 return 5;
974 }
975
976 virtual int disassemble(FILE* out) {
Jack Palevich1cdef202009-05-22 12:06:27 -0700977 return 0;
Jack Palevicha6535612009-05-13 16:24:17 -0700978 }
979
Jack Paleviche7b59062009-05-19 17:12:17 -0700980 /* output a symbol and patch all calls to it */
981 virtual void gsym(int t) {
982 int n;
983 int pc = getPC();
984 while (t) {
985 n = *(int *) t; /* next value */
986 *(int *) t = pc - t - 4;
987 t = n;
988 }
989 }
990
Jack Palevich1cdef202009-05-22 12:06:27 -0700991 virtual int finishCompile() {
992 return 0;
993 }
994
Jack Palevich21a15a22009-05-11 14:49:29 -0700995 private:
Jack Paleviche7b59062009-05-19 17:12:17 -0700996
997 /** Output 1 to 4 bytes.
998 *
999 */
1000 void o(int n) {
1001 /* cannot use unsigned, so we must do a hack */
1002 while (n && n != -1) {
1003 ob(n & 0xff);
1004 n = n >> 8;
1005 }
1006 }
1007
1008 /* psym is used to put an instruction with a data field which is a
1009 reference to a symbol. It is in fact the same as oad ! */
1010 int psym(int n, int t) {
1011 return oad(n, t);
1012 }
1013
1014 /* instruction + address */
1015 int oad(int n, int t) {
1016 o(n);
1017 int result = getPC();
1018 o4(t);
1019 return result;
1020 }
1021
1022
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001023 static const int operatorHelper[];
1024
1025 int decodeOp(int op) {
1026 if (op < 0 || op > OP_COUNT) {
Jack Palevichac0e95e2009-05-29 13:53:44 -07001027 error("Out-of-range operator: %d\n", op);
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001028 }
1029 return operatorHelper[op];
1030 }
Jack Palevich21a15a22009-05-11 14:49:29 -07001031
Jack Palevich546b2242009-05-13 15:10:04 -07001032 void gmov(int l, int t) {
Jack Palevich21a15a22009-05-11 14:49:29 -07001033 o(l + 0x83);
1034 oad((t < LOCAL) << 7 | 5, t);
1035 }
1036 };
1037
Jack Paleviche7b59062009-05-19 17:12:17 -07001038#endif // PROVIDE_X86_CODEGEN
1039
Jack Palevich1cdef202009-05-22 12:06:27 -07001040 class InputStream {
1041 public:
Jack Palevicheedf9d22009-06-04 16:23:40 -07001042 int getChar() {
1043 if (bumpLine) {
1044 line++;
1045 bumpLine = false;
1046 }
1047 int ch = get();
1048 if (ch == '\n') {
1049 bumpLine = true;
1050 }
1051 return ch;
1052 }
1053 int getLine() {
1054 return line;
1055 }
1056 protected:
1057 InputStream() :
1058 line(1), bumpLine(false) {
1059 }
1060 private:
Jack Palevich1cdef202009-05-22 12:06:27 -07001061 virtual int get() = 0;
Jack Palevicheedf9d22009-06-04 16:23:40 -07001062 int line;
1063 bool bumpLine;
Jack Palevich1cdef202009-05-22 12:06:27 -07001064 };
1065
1066 class FileInputStream : public InputStream {
1067 public:
1068 FileInputStream(FILE* in) : f(in) {}
Jack Palevich1cdef202009-05-22 12:06:27 -07001069 private:
Jack Palevicheedf9d22009-06-04 16:23:40 -07001070 virtual int get() { return fgetc(f); }
Jack Palevich1cdef202009-05-22 12:06:27 -07001071 FILE* f;
1072 };
1073
1074 class TextInputStream : public InputStream {
1075 public:
1076 TextInputStream(const char* text, size_t textLength)
1077 : pText(text), mTextLength(textLength), mPosition(0) {
1078 }
Jack Palevicheedf9d22009-06-04 16:23:40 -07001079
1080 private:
Jack Palevich1cdef202009-05-22 12:06:27 -07001081 virtual int get() {
1082 return mPosition < mTextLength ? pText[mPosition++] : EOF;
1083 }
Jack Palevich1cdef202009-05-22 12:06:27 -07001084
Jack Palevich1cdef202009-05-22 12:06:27 -07001085 const char* pText;
1086 size_t mTextLength;
1087 size_t mPosition;
1088 };
1089
Jack Palevich653f42d2009-05-28 17:15:32 -07001090 int ch; // Current input character, or EOF
1091 intptr_t tok; // token
1092 intptr_t tokc; // token extra info
1093 int tokl; // token operator level
1094 intptr_t rsym; // return symbol
1095 intptr_t loc; // local variable index
1096 char* glo; // global variable index
1097 char* sym_stk;
1098 char* dstk; // Define stack
1099 char* dptr; // Macro state: Points to macro text during macro playback.
1100 int dch; // Macro state: Saves old value of ch during a macro playback.
1101 char* last_id;
Jack Palevich21a15a22009-05-11 14:49:29 -07001102 void* pSymbolBase;
Jack Palevichf1f39cc2009-05-29 18:03:15 -07001103 char* pGlobalBase;
Jack Palevich653f42d2009-05-28 17:15:32 -07001104 char* pVarsBase; // Value of variables
Jack Palevich1cdef202009-05-22 12:06:27 -07001105
1106 InputStream* file;
Jack Palevich21a15a22009-05-11 14:49:29 -07001107
1108 CodeBuf codeBuf;
Jack Palevich22305132009-05-13 10:58:45 -07001109 CodeGenerator* pGen;
Jack Palevich21a15a22009-05-11 14:49:29 -07001110
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
1119 ~String() {
1120 if (mpBase) {
1121 free(mpBase);
1122 }
1123 }
1124
1125 char* getUnwrapped() {
1126 return mpBase;
1127 }
1128
1129 void appendCStr(const char* s) {
1130 int n = strlen(s);
1131 memcpy(ensure(n), s, n + 1);
1132 }
1133
1134 void append(char c) {
1135 * ensure(1) = c;
1136 }
1137
1138 void printf(const char* fmt,...) {
1139 va_list ap;
1140 va_start(ap, fmt);
1141 vprintf(fmt, ap);
1142 va_end(ap);
1143 }
1144
1145 void vprintf(const char* fmt, va_list ap) {
1146 char* temp;
1147 int numChars = vasprintf(&temp, fmt, ap);
1148 memcpy(ensure(numChars), temp, numChars+1);
1149 free(temp);
1150 }
1151
1152 size_t len() {
1153 return mUsed;
1154 }
1155
1156 private:
1157 char* ensure(int n) {
1158 size_t newUsed = mUsed + n;
1159 if (newUsed > mSize) {
1160 size_t newSize = mSize * 2 + 10;
1161 if (newSize < newUsed) {
1162 newSize = newUsed;
1163 }
1164 mpBase = (char*) realloc(mpBase, newSize + 1);
1165 mSize = newSize;
1166 }
1167 mpBase[newUsed] = '\0';
1168 char* result = mpBase + mUsed;
1169 mUsed = newUsed;
1170 return result;
1171 }
1172
1173 char* mpBase;
1174 size_t mUsed;
1175 size_t mSize;
1176 };
1177
1178 String mErrorBuf;
1179
Jack Palevichac0e95e2009-05-29 13:53:44 -07001180 jmp_buf mErrorRecoveryJumpBuf;
1181
Jack Palevicheedf9d22009-06-04 16:23:40 -07001182 String mPragmas;
1183 int mPragmaStringCount;
1184
Jack Palevich21a15a22009-05-11 14:49:29 -07001185 static const int ALLOC_SIZE = 99999;
1186
Jack Palevicheedf9d22009-06-04 16:23:40 -07001187 // Indentifiers start at 0x100 and increase by # (chars + 1) * 8
Jack Palevich21a15a22009-05-11 14:49:29 -07001188 static const int TOK_IDENT = 0x100;
1189 static const int TOK_INT = 0x100;
1190 static const int TOK_IF = 0x120;
1191 static const int TOK_ELSE = 0x138;
1192 static const int TOK_WHILE = 0x160;
1193 static const int TOK_BREAK = 0x190;
1194 static const int TOK_RETURN = 0x1c0;
1195 static const int TOK_FOR = 0x1f8;
Jack Palevicheedf9d22009-06-04 16:23:40 -07001196 static const int TOK_PRAGMA = 0x218;
1197 static const int TOK_DEFINE = TOK_PRAGMA + (7*8);
1198 static const int TOK_MAIN = TOK_DEFINE + (7*8);
Jack Palevich21a15a22009-05-11 14:49:29 -07001199
1200 static const int TOK_DUMMY = 1;
1201 static const int TOK_NUM = 2;
1202
1203 static const int LOCAL = 0x200;
1204
1205 static const int SYM_FORWARD = 0;
1206 static const int SYM_DEFINE = 1;
1207
1208 /* tokens in string heap */
1209 static const int TAG_TOK = ' ';
1210 static const int TAG_MACRO = 2;
1211
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001212 static const int OP_INCREMENT = 0;
1213 static const int OP_DECREMENT = 1;
1214 static const int OP_MUL = 2;
1215 static const int OP_DIV = 3;
1216 static const int OP_MOD = 4;
1217 static const int OP_PLUS = 5;
1218 static const int OP_MINUS = 6;
1219 static const int OP_SHIFT_LEFT = 7;
1220 static const int OP_SHIFT_RIGHT = 8;
1221 static const int OP_LESS_EQUAL = 9;
1222 static const int OP_GREATER_EQUAL = 10;
1223 static const int OP_LESS = 11;
1224 static const int OP_GREATER = 12;
1225 static const int OP_EQUALS = 13;
1226 static const int OP_NOT_EQUALS = 14;
1227 static const int OP_LOGICAL_AND = 15;
1228 static const int OP_LOGICAL_OR = 16;
1229 static const int OP_BIT_AND = 17;
1230 static const int OP_BIT_XOR = 18;
1231 static const int OP_BIT_OR = 19;
1232 static const int OP_BIT_NOT = 20;
1233 static const int OP_LOGICAL_NOT = 21;
1234 static const int OP_COUNT = 22;
1235
1236 /* Operators are searched from front, the two-character operators appear
1237 * before the single-character operators with the same first character.
1238 * @ is used to pad out single-character operators.
1239 */
1240 static const char* operatorChars;
1241 static const char operatorLevel[];
1242
Jack Palevich21a15a22009-05-11 14:49:29 -07001243 void pdef(int t) {
Jack Palevichf1f39cc2009-05-29 18:03:15 -07001244 if (dstk - sym_stk >= ALLOC_SIZE) {
1245 error("Symbol table exhausted");
1246 }
Jack Palevich653f42d2009-05-28 17:15:32 -07001247 *dstk++ = t;
Jack Palevich21a15a22009-05-11 14:49:29 -07001248 }
1249
1250 void inp() {
1251 if (dptr) {
Jack Palevich653f42d2009-05-28 17:15:32 -07001252 ch = *dptr++;
Jack Palevich21a15a22009-05-11 14:49:29 -07001253 if (ch == TAG_MACRO) {
1254 dptr = 0;
1255 ch = dch;
1256 }
1257 } else
Jack Palevicheedf9d22009-06-04 16:23:40 -07001258 ch = file->getChar();
Jack Palevich21a15a22009-05-11 14:49:29 -07001259 /* printf("ch=%c 0x%x\n", ch, ch); */
1260 }
1261
1262 int isid() {
Jack Palevich546b2242009-05-13 15:10:04 -07001263 return isalnum(ch) | (ch == '_');
Jack Palevich21a15a22009-05-11 14:49:29 -07001264 }
1265
1266 /* read a character constant */
1267 void getq() {
1268 if (ch == '\\') {
1269 inp();
1270 if (ch == 'n')
1271 ch = '\n';
1272 }
1273 }
1274
1275 void next() {
1276 int l, a;
1277
Jack Palevich546b2242009-05-13 15:10:04 -07001278 while (isspace(ch) | (ch == '#')) {
Jack Palevich21a15a22009-05-11 14:49:29 -07001279 if (ch == '#') {
1280 inp();
1281 next();
1282 if (tok == TOK_DEFINE) {
1283 next();
1284 pdef(TAG_TOK); /* fill last ident tag */
1285 *(int *) tok = SYM_DEFINE;
Jack Palevich653f42d2009-05-28 17:15:32 -07001286 *(char* *) (tok + 4) = dstk; /* define stack */
Jack Palevicheedf9d22009-06-04 16:23:40 -07001287 while (ch != '\n') {
1288 pdef(ch);
1289 inp();
1290 }
Jack Palevich21a15a22009-05-11 14:49:29 -07001291 pdef(ch);
Jack Palevicheedf9d22009-06-04 16:23:40 -07001292 pdef(TAG_MACRO);
1293 } else if (tok == TOK_PRAGMA) {
1294 doPragma();
1295 } else {
1296 error("Unsupported preprocessor directive \"%s\"", last_id);
Jack Palevich21a15a22009-05-11 14:49:29 -07001297 }
Jack Palevicheedf9d22009-06-04 16:23:40 -07001298
Jack Palevich21a15a22009-05-11 14:49:29 -07001299 }
1300 inp();
1301 }
1302 tokl = 0;
1303 tok = ch;
1304 /* encode identifiers & numbers */
1305 if (isid()) {
1306 pdef(TAG_TOK);
1307 last_id = dstk;
1308 while (isid()) {
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001309 pdef(ch);
1310 inp();
Jack Palevichae54f1f2009-05-08 14:54:15 -07001311 }
Jack Palevich21a15a22009-05-11 14:49:29 -07001312 if (isdigit(tok)) {
Jack Palevich653f42d2009-05-28 17:15:32 -07001313 tokc = strtol(last_id, 0, 0);
Jack Palevich21a15a22009-05-11 14:49:29 -07001314 tok = TOK_NUM;
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001315 } else {
Jack Palevichf1f39cc2009-05-29 18:03:15 -07001316 if (dstk - sym_stk + 1 > ALLOC_SIZE) {
1317 error("symbol stack overflow");
1318 }
Jack Palevich653f42d2009-05-28 17:15:32 -07001319 * dstk = TAG_TOK; /* no need to mark end of string (we
Jack Palevich21a15a22009-05-11 14:49:29 -07001320 suppose data is initialized to zero by calloc) */
Jack Palevich653f42d2009-05-28 17:15:32 -07001321 tok = (intptr_t) (strstr(sym_stk, (last_id - 1))
Jack Palevich21a15a22009-05-11 14:49:29 -07001322 - sym_stk);
Jack Palevich653f42d2009-05-28 17:15:32 -07001323 * dstk = 0; /* mark real end of ident for dlsym() */
Jack Palevich21a15a22009-05-11 14:49:29 -07001324 tok = tok * 8 + TOK_IDENT;
1325 if (tok > TOK_DEFINE) {
Jack Palevichf1f39cc2009-05-29 18:03:15 -07001326 if (tok + 8 > ALLOC_SIZE) {
1327 error("Variable Table overflow.");
1328 }
Jack Palevich653f42d2009-05-28 17:15:32 -07001329 tok = (intptr_t) (pVarsBase + tok);
Jack Palevich21a15a22009-05-11 14:49:29 -07001330 /* printf("tok=%s %x\n", last_id, tok); */
1331 /* define handling */
1332 if (*(int *) tok == SYM_DEFINE) {
Jack Palevich653f42d2009-05-28 17:15:32 -07001333 dptr = *(char* *) (tok + 4);
Jack Palevich21a15a22009-05-11 14:49:29 -07001334 dch = ch;
1335 inp();
1336 next();
1337 }
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001338 }
1339 }
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001340 } else {
Jack Palevich21a15a22009-05-11 14:49:29 -07001341 inp();
1342 if (tok == '\'') {
1343 tok = TOK_NUM;
1344 getq();
1345 tokc = ch;
1346 inp();
1347 inp();
Jack Palevich546b2242009-05-13 15:10:04 -07001348 } else if ((tok == '/') & (ch == '*')) {
Jack Palevich21a15a22009-05-11 14:49:29 -07001349 inp();
1350 while (ch) {
1351 while (ch != '*')
1352 inp();
1353 inp();
1354 if (ch == '/')
1355 ch = 0;
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001356 }
Jack Palevich21a15a22009-05-11 14:49:29 -07001357 inp();
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001358 next();
Jack Palevichbd894902009-05-14 19:35:31 -07001359 } else if ((tok == '/') & (ch == '/')) {
1360 inp();
1361 while (ch && (ch != '\n')) {
1362 inp();
1363 }
1364 inp();
1365 next();
Jack Palevich21a15a22009-05-11 14:49:29 -07001366 } else {
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001367 const char* t = operatorChars;
1368 int opIndex = 0;
Jack Palevich546b2242009-05-13 15:10:04 -07001369 while ((l = *t++) != 0) {
Jack Palevich21a15a22009-05-11 14:49:29 -07001370 a = *t++;
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001371 tokl = operatorLevel[opIndex];
1372 tokc = opIndex;
Jack Palevich546b2242009-05-13 15:10:04 -07001373 if ((l == tok) & ((a == ch) | (a == '@'))) {
Jack Palevich21a15a22009-05-11 14:49:29 -07001374#if 0
1375 printf("%c%c -> tokl=%d tokc=0x%x\n",
1376 l, a, tokl, tokc);
1377#endif
1378 if (a == ch) {
1379 inp();
1380 tok = TOK_DUMMY; /* dummy token for double tokens */
1381 }
1382 break;
1383 }
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001384 opIndex++;
1385 }
1386 if (l == 0) {
1387 tokl = 0;
1388 tokc = 0;
Jack Palevich21a15a22009-05-11 14:49:29 -07001389 }
1390 }
1391 }
1392#if 0
1393 {
Jack Palevich653f42d2009-05-28 17:15:32 -07001394 char* p;
Jack Palevich21a15a22009-05-11 14:49:29 -07001395
1396 printf("tok=0x%x ", tok);
1397 if (tok >= TOK_IDENT) {
1398 printf("'");
1399 if (tok> TOK_DEFINE)
Jack Palevich653f42d2009-05-28 17:15:32 -07001400 p = sym_stk + 1 + ((char*) tok - pVarsBase - TOK_IDENT) / 8;
Jack Palevich21a15a22009-05-11 14:49:29 -07001401 else
1402 p = sym_stk + 1 + (tok - TOK_IDENT) / 8;
Jack Palevich653f42d2009-05-28 17:15:32 -07001403 while (*p != TAG_TOK && *p)
1404 printf("%c", *p++);
Jack Palevich21a15a22009-05-11 14:49:29 -07001405 printf("'\n");
1406 } else if (tok == TOK_NUM) {
1407 printf("%d\n", tokc);
1408 } else {
1409 printf("'%c'\n", tok);
1410 }
1411 }
1412#endif
1413 }
1414
Jack Palevicheedf9d22009-06-04 16:23:40 -07001415 void doPragma() {
1416 // # pragma name(val)
1417 int state = 0;
1418 while(ch != EOF && ch != '\n' && state < 10) {
1419 switch(state) {
1420 case 0:
1421 if (isspace(ch)) {
1422 inp();
1423 } else {
1424 state++;
1425 }
1426 break;
1427 case 1:
1428 if (isalnum(ch)) {
1429 mPragmas.append(ch);
1430 inp();
1431 } else if (ch == '(') {
1432 mPragmas.append(0);
1433 inp();
1434 state++;
1435 } else {
1436 state = 11;
1437 }
1438 break;
1439 case 2:
1440 if (isalnum(ch)) {
1441 mPragmas.append(ch);
1442 inp();
1443 } else if (ch == ')') {
1444 mPragmas.append(0);
1445 inp();
1446 state = 10;
1447 } else {
1448 state = 11;
1449 }
1450 break;
1451 }
1452 }
1453 if(state != 10) {
1454 error("Unexpected pragma syntax");
1455 }
1456 mPragmaStringCount += 2;
1457 }
Jack Palevich21a15a22009-05-11 14:49:29 -07001458
Jack Palevichac0e95e2009-05-29 13:53:44 -07001459 virtual void verror(const char* fmt, va_list ap) {
Jack Palevicheedf9d22009-06-04 16:23:40 -07001460 mErrorBuf.printf("%ld: ", file->getLine());
1461 mErrorBuf.vprintf(fmt, ap);
1462 mErrorBuf.printf("\n");
Jack Palevichac0e95e2009-05-29 13:53:44 -07001463 longjmp(mErrorRecoveryJumpBuf, 1);
Jack Palevich21a15a22009-05-11 14:49:29 -07001464 }
1465
Jack Palevich8b0624c2009-05-20 12:12:06 -07001466 void skip(intptr_t c) {
Jack Palevich21a15a22009-05-11 14:49:29 -07001467 if (tok != c) {
1468 error("'%c' expected", c);
1469 }
1470 next();
1471 }
1472
Jack Palevich21a15a22009-05-11 14:49:29 -07001473 /* l is one if '=' parsing wanted (quick hack) */
Jack Palevich8b0624c2009-05-20 12:12:06 -07001474 void unary(intptr_t l) {
Jack Palevich653f42d2009-05-28 17:15:32 -07001475 intptr_t n, t, a;
1476 int c;
Jack Palevich546b2242009-05-13 15:10:04 -07001477 t = 0;
Jack Palevich21a15a22009-05-11 14:49:29 -07001478 n = 1; /* type of expression 0 = forward, 1 = value, other =
1479 lvalue */
1480 if (tok == '\"') {
Jack Palevich653f42d2009-05-28 17:15:32 -07001481 pGen->li((int) glo);
Jack Palevich21a15a22009-05-11 14:49:29 -07001482 while (ch != '\"') {
1483 getq();
Jack Palevichf1f39cc2009-05-29 18:03:15 -07001484 *allocGlobalSpace(1) = ch;
Jack Palevich21a15a22009-05-11 14:49:29 -07001485 inp();
1486 }
Jack Palevich653f42d2009-05-28 17:15:32 -07001487 *glo = 0;
Jack Palevichf1f39cc2009-05-29 18:03:15 -07001488 /* align heap */
1489 allocGlobalSpace((char*) (((intptr_t) glo + 4) & -4) - glo);
Jack Palevich21a15a22009-05-11 14:49:29 -07001490 inp();
1491 next();
1492 } else {
1493 c = tokl;
1494 a = tokc;
1495 t = tok;
1496 next();
1497 if (t == TOK_NUM) {
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001498 pGen->li(a);
Jack Palevich21a15a22009-05-11 14:49:29 -07001499 } else if (c == 2) {
1500 /* -, +, !, ~ */
1501 unary(0);
Jack Palevich1cdef202009-05-22 12:06:27 -07001502 pGen->clearR1();
Jack Palevich21a15a22009-05-11 14:49:29 -07001503 if (t == '!')
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001504 pGen->gcmp(a);
Jack Palevich21a15a22009-05-11 14:49:29 -07001505 else
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001506 pGen->genOp(a);
Jack Palevich21a15a22009-05-11 14:49:29 -07001507 } else if (t == '(') {
1508 expr();
1509 skip(')');
1510 } else if (t == '*') {
1511 /* parse cast */
1512 skip('(');
1513 t = tok; /* get type */
1514 next(); /* skip int/char/void */
1515 next(); /* skip '*' or '(' */
1516 if (tok == '*') {
1517 /* function type */
1518 skip('*');
1519 skip(')');
1520 skip('(');
1521 skip(')');
1522 t = 0;
1523 }
1524 skip(')');
1525 unary(0);
1526 if (tok == '=') {
1527 next();
Jack Palevich1cdef202009-05-22 12:06:27 -07001528 pGen->pushR0();
Jack Palevich21a15a22009-05-11 14:49:29 -07001529 expr();
Jack Palevich1cdef202009-05-22 12:06:27 -07001530 pGen->popR1();
1531 pGen->storeR0ToR1(t == TOK_INT);
Jack Palevich21a15a22009-05-11 14:49:29 -07001532 } else if (t) {
Jack Palevich1cdef202009-05-22 12:06:27 -07001533 pGen->loadR0FromR0(t == TOK_INT);
Jack Palevich21a15a22009-05-11 14:49:29 -07001534 }
1535 } else if (t == '&') {
Jack Palevich1cdef202009-05-22 12:06:27 -07001536 pGen->leaR0(*(int *) tok);
Jack Palevich21a15a22009-05-11 14:49:29 -07001537 next();
1538 } else {
1539 n = *(int *) t;
1540 /* forward reference: try dlsym */
Jack Palevichcb1c9ef2009-05-14 11:38:49 -07001541 if (!n) {
Jack Palevich653f42d2009-05-28 17:15:32 -07001542 n = (intptr_t) dlsym(RTLD_DEFAULT, last_id);
Jack Palevichcb1c9ef2009-05-14 11:38:49 -07001543 }
Jack Palevich546b2242009-05-13 15:10:04 -07001544 if ((tok == '=') & l) {
Jack Palevich21a15a22009-05-11 14:49:29 -07001545 /* assignment */
1546 next();
1547 expr();
Jack Palevich1cdef202009-05-22 12:06:27 -07001548 pGen->storeR0(n);
Jack Palevich21a15a22009-05-11 14:49:29 -07001549 } else if (tok != '(') {
1550 /* variable */
Jack Palevich1cdef202009-05-22 12:06:27 -07001551 pGen->loadR0(n, tokl == 11, tokc);
Jack Palevich21a15a22009-05-11 14:49:29 -07001552 if (tokl == 11) {
Jack Palevich21a15a22009-05-11 14:49:29 -07001553 next();
1554 }
1555 }
1556 }
1557 }
1558
1559 /* function call */
1560 if (tok == '(') {
1561 if (n == 1)
Jack Palevich1cdef202009-05-22 12:06:27 -07001562 pGen->pushR0();
Jack Palevich21a15a22009-05-11 14:49:29 -07001563
1564 /* push args and invert order */
Jack Palevichcb1c9ef2009-05-14 11:38:49 -07001565 a = pGen->beginFunctionCallArguments();
Jack Palevich21a15a22009-05-11 14:49:29 -07001566 next();
1567 l = 0;
1568 while (tok != ')') {
1569 expr();
Jack Palevich1cdef202009-05-22 12:06:27 -07001570 pGen->storeR0ToArg(l);
Jack Palevichbbf8ab52009-05-11 11:54:30 -07001571 if (tok == ',')
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001572 next();
Jack Palevich21a15a22009-05-11 14:49:29 -07001573 l = l + 4;
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001574 }
Jack Palevichcb1c9ef2009-05-14 11:38:49 -07001575 pGen->endFunctionCallArguments(a, l);
Jack Palevich21a15a22009-05-11 14:49:29 -07001576 next();
1577 if (!n) {
1578 /* forward reference */
1579 t = t + 4;
1580 *(int *) t = pGen->callForward(*(int *) t);
1581 } else if (n == 1) {
1582 pGen->callIndirect(l);
Jack Palevich21a15a22009-05-11 14:49:29 -07001583 } else {
Jack Palevich7810bc92009-05-15 14:31:47 -07001584 pGen->callRelative(n - codeBuf.getPC() - pGen->jumpOffset());
Jack Palevich21a15a22009-05-11 14:49:29 -07001585 }
Jack Palevich3d474a72009-05-15 15:12:38 -07001586 if (l | (n == 1))
Jack Palevich7810bc92009-05-15 14:31:47 -07001587 pGen->adjustStackAfterCall(l, n == 1);
Jack Palevich21a15a22009-05-11 14:49:29 -07001588 }
1589 }
1590
Jack Palevich653f42d2009-05-28 17:15:32 -07001591 void sum(int l) {
Jack Palevich8b0624c2009-05-20 12:12:06 -07001592 intptr_t t, n, a;
Jack Palevich546b2242009-05-13 15:10:04 -07001593 t = 0;
Jack Palevich21a15a22009-05-11 14:49:29 -07001594 if (l-- == 1)
1595 unary(1);
1596 else {
1597 sum(l);
1598 a = 0;
1599 while (l == tokl) {
1600 n = tok;
1601 t = tokc;
1602 next();
1603
1604 if (l > 8) {
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001605 a = pGen->gtst(t == OP_LOGICAL_OR, a); /* && and || output code generation */
Jack Palevich21a15a22009-05-11 14:49:29 -07001606 sum(l);
1607 } else {
Jack Palevich1cdef202009-05-22 12:06:27 -07001608 pGen->pushR0();
Jack Palevich21a15a22009-05-11 14:49:29 -07001609 sum(l);
Jack Palevich1cdef202009-05-22 12:06:27 -07001610 pGen->popR1();
Jack Palevich21a15a22009-05-11 14:49:29 -07001611
Jack Palevich546b2242009-05-13 15:10:04 -07001612 if ((l == 4) | (l == 5)) {
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001613 pGen->gcmp(t);
Jack Palevich21a15a22009-05-11 14:49:29 -07001614 } else {
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001615 pGen->genOp(t);
Jack Palevich21a15a22009-05-11 14:49:29 -07001616 }
1617 }
1618 }
1619 /* && and || output code generation */
1620 if (a && l > 8) {
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001621 a = pGen->gtst(t == OP_LOGICAL_OR, a);
1622 pGen->li(t != OP_LOGICAL_OR);
Jack Palevicha6535612009-05-13 16:24:17 -07001623 pGen->gjmp(5); /* jmp $ + 5 (sizeof li, FIXME for ARM) */
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001624 pGen->gsym(a);
1625 pGen->li(t == OP_LOGICAL_OR);
Jack Palevich21a15a22009-05-11 14:49:29 -07001626 }
1627 }
1628 }
1629
1630 void expr() {
1631 sum(11);
1632 }
1633
1634 int test_expr() {
1635 expr();
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001636 return pGen->gtst(0, 0);
Jack Palevich21a15a22009-05-11 14:49:29 -07001637 }
1638
Jack Palevich8b0624c2009-05-20 12:12:06 -07001639 void block(intptr_t l) {
1640 intptr_t a, n, t;
Jack Palevich21a15a22009-05-11 14:49:29 -07001641
1642 if (tok == TOK_IF) {
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001643 next();
1644 skip('(');
Jack Palevich21a15a22009-05-11 14:49:29 -07001645 a = test_expr();
1646 skip(')');
1647 block(l);
1648 if (tok == TOK_ELSE) {
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001649 next();
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001650 n = pGen->gjmp(0); /* jmp */
1651 pGen->gsym(a);
Jack Palevich21a15a22009-05-11 14:49:29 -07001652 block(l);
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001653 pGen->gsym(n); /* patch else jmp */
Jack Palevich21a15a22009-05-11 14:49:29 -07001654 } else {
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001655 pGen->gsym(a); /* patch if test */
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001656 }
Jack Palevich546b2242009-05-13 15:10:04 -07001657 } else if ((tok == TOK_WHILE) | (tok == TOK_FOR)) {
Jack Palevich21a15a22009-05-11 14:49:29 -07001658 t = tok;
1659 next();
1660 skip('(');
1661 if (t == TOK_WHILE) {
Jack Palevicha6535612009-05-13 16:24:17 -07001662 n = codeBuf.getPC(); // top of loop, target of "next" iteration
Jack Palevich21a15a22009-05-11 14:49:29 -07001663 a = test_expr();
1664 } else {
1665 if (tok != ';')
1666 expr();
1667 skip(';');
1668 n = codeBuf.getPC();
1669 a = 0;
1670 if (tok != ';')
1671 a = test_expr();
1672 skip(';');
1673 if (tok != ')') {
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001674 t = pGen->gjmp(0);
Jack Palevich21a15a22009-05-11 14:49:29 -07001675 expr();
Jack Palevicha6535612009-05-13 16:24:17 -07001676 pGen->gjmp(n - codeBuf.getPC() - pGen->jumpOffset());
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001677 pGen->gsym(t);
Jack Palevich21a15a22009-05-11 14:49:29 -07001678 n = t + 4;
1679 }
1680 }
1681 skip(')');
Jack Palevich8b0624c2009-05-20 12:12:06 -07001682 block((intptr_t) &a);
Jack Palevicha6535612009-05-13 16:24:17 -07001683 pGen->gjmp(n - codeBuf.getPC() - pGen->jumpOffset()); /* jmp */
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001684 pGen->gsym(a);
Jack Palevich21a15a22009-05-11 14:49:29 -07001685 } else if (tok == '{') {
1686 next();
1687 /* declarations */
1688 decl(1);
1689 while (tok != '}')
1690 block(l);
1691 next();
1692 } else {
1693 if (tok == TOK_RETURN) {
1694 next();
1695 if (tok != ';')
1696 expr();
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001697 rsym = pGen->gjmp(rsym); /* jmp */
Jack Palevich21a15a22009-05-11 14:49:29 -07001698 } else if (tok == TOK_BREAK) {
1699 next();
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001700 *(int *) l = pGen->gjmp(*(int *) l);
Jack Palevich21a15a22009-05-11 14:49:29 -07001701 } else if (tok != ';')
1702 expr();
1703 skip(';');
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001704 }
1705 }
Jack Palevich21a15a22009-05-11 14:49:29 -07001706
1707 /* 'l' is true if local declarations */
Jack Palevich8b0624c2009-05-20 12:12:06 -07001708 void decl(bool l) {
1709 intptr_t a;
Jack Palevich21a15a22009-05-11 14:49:29 -07001710
Jack Palevich653f42d2009-05-28 17:15:32 -07001711 while ((tok == TOK_INT) | ((tok != EOF) & (!l))) {
Jack Palevich21a15a22009-05-11 14:49:29 -07001712 if (tok == TOK_INT) {
1713 next();
1714 while (tok != ';') {
1715 if (l) {
1716 loc = loc + 4;
1717 *(int *) tok = -loc;
1718 } else {
Jack Palevichf1f39cc2009-05-29 18:03:15 -07001719 *(int* *) tok = (int*) allocGlobalSpace(4);
Jack Palevich21a15a22009-05-11 14:49:29 -07001720 }
1721 next();
1722 if (tok == ',')
1723 next();
1724 }
1725 skip(';');
1726 } else {
1727 /* patch forward references (XXX: do not work for function
1728 pointers) */
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001729 pGen->gsym(*(int *) (tok + 4));
Jack Palevich21a15a22009-05-11 14:49:29 -07001730 /* put function address */
1731 *(int *) tok = codeBuf.getPC();
1732 next();
1733 skip('(');
1734 a = 8;
Jack Palevich546b2242009-05-13 15:10:04 -07001735 int argCount = 0;
Jack Palevich21a15a22009-05-11 14:49:29 -07001736 while (tok != ')') {
1737 /* read param name and compute offset */
1738 *(int *) tok = a;
1739 a = a + 4;
1740 next();
1741 if (tok == ',')
1742 next();
Jack Palevich546b2242009-05-13 15:10:04 -07001743 argCount++;
Jack Palevich21a15a22009-05-11 14:49:29 -07001744 }
1745 next(); /* skip ')' */
1746 rsym = loc = 0;
Jack Palevich546b2242009-05-13 15:10:04 -07001747 a = pGen->functionEntry(argCount);
Jack Palevich21a15a22009-05-11 14:49:29 -07001748 block(0);
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001749 pGen->gsym(rsym);
Jack Palevich546b2242009-05-13 15:10:04 -07001750 pGen->functionExit(argCount, a, loc);
Jack Palevich21a15a22009-05-11 14:49:29 -07001751 }
1752 }
1753 }
1754
Jack Palevichf1f39cc2009-05-29 18:03:15 -07001755 char* allocGlobalSpace(int bytes) {
1756 if (glo - pGlobalBase + bytes > ALLOC_SIZE) {
1757 error("Global space exhausted");
1758 }
1759 char* result = glo;
1760 glo += bytes;
1761 return result;
1762 }
1763
Jack Palevich21a15a22009-05-11 14:49:29 -07001764 void cleanup() {
1765 if (sym_stk != 0) {
Jack Palevich653f42d2009-05-28 17:15:32 -07001766 free(sym_stk);
Jack Palevich21a15a22009-05-11 14:49:29 -07001767 sym_stk = 0;
1768 }
1769 if (pGlobalBase != 0) {
Jack Palevichf1f39cc2009-05-29 18:03:15 -07001770 free(pGlobalBase);
Jack Palevich21a15a22009-05-11 14:49:29 -07001771 pGlobalBase = 0;
1772 }
1773 if (pVarsBase != 0) {
1774 free(pVarsBase);
1775 pVarsBase = 0;
1776 }
1777 if (pGen) {
1778 delete pGen;
1779 pGen = 0;
1780 }
Jack Palevich1cdef202009-05-22 12:06:27 -07001781 if (file) {
1782 delete file;
1783 file = 0;
1784 }
Jack Palevich21a15a22009-05-11 14:49:29 -07001785 }
1786
1787 void clear() {
1788 tok = 0;
1789 tokc = 0;
1790 tokl = 0;
1791 ch = 0;
Jack Palevich653f42d2009-05-28 17:15:32 -07001792 pVarsBase = 0;
Jack Palevich21a15a22009-05-11 14:49:29 -07001793 rsym = 0;
1794 loc = 0;
1795 glo = 0;
1796 sym_stk = 0;
1797 dstk = 0;
1798 dptr = 0;
1799 dch = 0;
1800 last_id = 0;
1801 file = 0;
1802 pGlobalBase = 0;
1803 pVarsBase = 0;
1804 pGen = 0;
Jack Palevicheedf9d22009-06-04 16:23:40 -07001805 mPragmaStringCount = 0;
Jack Palevich21a15a22009-05-11 14:49:29 -07001806 }
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001807
Jack Palevich22305132009-05-13 10:58:45 -07001808 void setArchitecture(const char* architecture) {
1809 delete pGen;
1810 pGen = 0;
1811
1812 if (architecture != NULL) {
Jack Paleviche7b59062009-05-19 17:12:17 -07001813#ifdef PROVIDE_ARM_CODEGEN
Jack Palevich8b0624c2009-05-20 12:12:06 -07001814 if (! pGen && strcmp(architecture, "arm") == 0) {
Jack Palevich22305132009-05-13 10:58:45 -07001815 pGen = new ARMCodeGenerator();
Jack Palevich8b0624c2009-05-20 12:12:06 -07001816 }
Jack Paleviche7b59062009-05-19 17:12:17 -07001817#endif
Jack Paleviche7b59062009-05-19 17:12:17 -07001818#ifdef PROVIDE_X86_CODEGEN
Jack Palevich8b0624c2009-05-20 12:12:06 -07001819 if (! pGen && strcmp(architecture, "x86") == 0) {
Jack Palevich22305132009-05-13 10:58:45 -07001820 pGen = new X86CodeGenerator();
Jack Palevich8b0624c2009-05-20 12:12:06 -07001821 }
Jack Paleviche7b59062009-05-19 17:12:17 -07001822#endif
Jack Palevich8b0624c2009-05-20 12:12:06 -07001823 if (!pGen ) {
Jack Palevichac0e95e2009-05-29 13:53:44 -07001824 error("Unknown architecture %s\n", architecture);
Jack Palevich22305132009-05-13 10:58:45 -07001825 }
1826 }
1827
1828 if (pGen == NULL) {
Jack Paleviche7b59062009-05-19 17:12:17 -07001829#if defined(DEFAULT_ARM_CODEGEN)
Jack Palevich22305132009-05-13 10:58:45 -07001830 pGen = new ARMCodeGenerator();
Jack Paleviche7b59062009-05-19 17:12:17 -07001831#elif defined(DEFAULT_X86_CODEGEN)
1832 pGen = new X86CodeGenerator();
1833#endif
1834 }
1835 if (pGen == NULL) {
Jack Palevichac0e95e2009-05-29 13:53:44 -07001836 error("No code generator defined.");
Jack Palevich22305132009-05-13 10:58:45 -07001837 }
Jack Palevichac0e95e2009-05-29 13:53:44 -07001838 pGen->setErrorSink(this);
Jack Palevich22305132009-05-13 10:58:45 -07001839 }
1840
Jack Palevich77ae76e2009-05-10 19:59:24 -07001841public:
Jack Palevich22305132009-05-13 10:58:45 -07001842 struct args {
1843 args() {
1844 architecture = 0;
1845 }
1846 const char* architecture;
1847 };
1848
Jack Paleviche7b59062009-05-19 17:12:17 -07001849 Compiler() {
Jack Palevich21a15a22009-05-11 14:49:29 -07001850 clear();
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001851 }
Jack Palevichbbf8ab52009-05-11 11:54:30 -07001852
Jack Paleviche7b59062009-05-19 17:12:17 -07001853 ~Compiler() {
Jack Palevich21a15a22009-05-11 14:49:29 -07001854 cleanup();
1855 }
1856
Jack Palevich1cdef202009-05-22 12:06:27 -07001857 int compile(const char* text, size_t textLength) {
Jack Palevichac0e95e2009-05-29 13:53:44 -07001858 int result;
1859 if (! (result = setjmp(mErrorRecoveryJumpBuf))) {
1860 cleanup();
1861 clear();
1862 codeBuf.init(ALLOC_SIZE);
1863 setArchitecture(NULL);
1864 if (!pGen) {
1865 return -1;
1866 }
1867 pGen->init(&codeBuf);
1868 file = new TextInputStream(text, textLength);
1869 sym_stk = (char*) calloc(1, ALLOC_SIZE);
Jack Palevicheedf9d22009-06-04 16:23:40 -07001870 static const char* predefinedSymbols =
1871 " int if else while break return for pragma define main ";
1872 dstk = strcpy(sym_stk, predefinedSymbols)
1873 + strlen(predefinedSymbols);
Jack Palevichf1f39cc2009-05-29 18:03:15 -07001874 pGlobalBase = (char*) calloc(1, ALLOC_SIZE);
1875 glo = pGlobalBase;
Jack Palevichac0e95e2009-05-29 13:53:44 -07001876 pVarsBase = (char*) calloc(1, ALLOC_SIZE);
1877 inp();
1878 next();
1879 decl(0);
1880 pGen->finishCompile();
Jack Palevich8b0624c2009-05-20 12:12:06 -07001881 }
Jack Palevichac0e95e2009-05-29 13:53:44 -07001882 return result;
Jack Palevich21a15a22009-05-11 14:49:29 -07001883 }
1884
1885 int run(int argc, char** argv) {
1886 typedef int (*mainPtr)(int argc, char** argv);
Jack Palevich653f42d2009-05-28 17:15:32 -07001887 mainPtr aMain = (mainPtr) *(int*) (pVarsBase + TOK_MAIN);
Jack Palevich21a15a22009-05-11 14:49:29 -07001888 if (!aMain) {
1889 fprintf(stderr, "Could not find function \"main\".\n");
1890 return -1;
1891 }
1892 return aMain(argc, argv);
1893 }
1894
1895 int dump(FILE* out) {
1896 fwrite(codeBuf.getBase(), 1, codeBuf.getSize(), out);
1897 return 0;
1898 }
Jack Palevich77ae76e2009-05-10 19:59:24 -07001899
Jack Palevicha6535612009-05-13 16:24:17 -07001900 int disassemble(FILE* out) {
1901 return pGen->disassemble(out);
1902 }
1903
Jack Palevich1cdef202009-05-22 12:06:27 -07001904 /* Look through the symbol table to find a symbol.
1905 * If found, return its value.
1906 */
1907 void* lookup(const char* name) {
1908 if (!sym_stk) {
1909 return NULL;
1910 }
1911 size_t nameLen = strlen(name);
Jack Palevich653f42d2009-05-28 17:15:32 -07001912 char* pSym = sym_stk;
Jack Palevich1cdef202009-05-22 12:06:27 -07001913 char c;
1914 for(;;) {
1915 c = *pSym++;
1916 if (c == 0) {
1917 break;
1918 }
1919 if (c == TAG_TOK) {
1920 if (memcmp(pSym, name, nameLen) == 0
1921 && pSym[nameLen] == TAG_TOK) {
Jack Palevich653f42d2009-05-28 17:15:32 -07001922 int tok = pSym - 1 - sym_stk;
Jack Palevich1cdef202009-05-22 12:06:27 -07001923 tok = tok * 8 + TOK_IDENT;
1924 if (tok <= TOK_DEFINE) {
1925 return 0;
1926 } else {
Jack Palevich653f42d2009-05-28 17:15:32 -07001927 tok = (intptr_t) (pVarsBase + tok);
Jack Palevich1cdef202009-05-22 12:06:27 -07001928 return * (void**) tok;
1929 }
1930 }
1931 }
1932 }
1933 return NULL;
1934 }
1935
Jack Palevicheedf9d22009-06-04 16:23:40 -07001936 void getPragmas(ACCsizei* actualStringCount,
1937 ACCsizei maxStringCount, ACCchar** strings) {
1938 int stringCount = mPragmaStringCount;
1939 if (actualStringCount) {
1940 *actualStringCount = stringCount;
1941 }
1942 if (stringCount > maxStringCount) {
1943 stringCount = maxStringCount;
1944 }
1945 if (strings) {
1946 char* pPragmas = mPragmas.getUnwrapped();
1947 while (stringCount-- > 0) {
1948 *strings++ = pPragmas;
1949 pPragmas += strlen(pPragmas) + 1;
1950 }
1951 }
1952 }
1953
Jack Palevichac0e95e2009-05-29 13:53:44 -07001954 char* getErrorMessage() {
Jack Palevicheedf9d22009-06-04 16:23:40 -07001955 return mErrorBuf.getUnwrapped();
Jack Palevichac0e95e2009-05-29 13:53:44 -07001956 }
1957
Jack Palevich77ae76e2009-05-10 19:59:24 -07001958};
1959
Jack Paleviche7b59062009-05-19 17:12:17 -07001960const char* Compiler::operatorChars =
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001961 "++--*@/@%@+@-@<<>><=>=<@>@==!=&&||&@^@|@~@!@";
1962
Jack Paleviche7b59062009-05-19 17:12:17 -07001963const char Compiler::operatorLevel[] =
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001964 {11, 11, 1, 1, 1, 2, 2, 3, 3, 4, 4, 4, 4,
1965 5, 5, /* ==, != */
1966 9, 10, /* &&, || */
1967 6, 7, 8, /* & ^ | */
1968 2, 2 /* ~ ! */
1969 };
1970
Jack Palevich8b0624c2009-05-20 12:12:06 -07001971#ifdef PROVIDE_ARM_CODEGEN
Jack Paleviche7b59062009-05-19 17:12:17 -07001972FILE* Compiler::ARMCodeGenerator::disasmOut;
Jack Palevich8b0624c2009-05-20 12:12:06 -07001973#endif
Jack Palevicha6535612009-05-13 16:24:17 -07001974
Jack Palevich8b0624c2009-05-20 12:12:06 -07001975#ifdef PROVIDE_X86_CODEGEN
Jack Paleviche7b59062009-05-19 17:12:17 -07001976const int Compiler::X86CodeGenerator::operatorHelper[] = {
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001977 0x1, // ++
1978 0xff, // --
1979 0xc1af0f, // *
1980 0xf9f79991, // /
1981 0xf9f79991, // % (With manual assist to swap results)
1982 0xc801, // +
1983 0xd8f7c829, // -
1984 0xe0d391, // <<
1985 0xf8d391, // >>
1986 0xe, // <=
1987 0xd, // >=
1988 0xc, // <
1989 0xf, // >
1990 0x4, // ==
1991 0x5, // !=
1992 0x0, // &&
1993 0x1, // ||
1994 0xc821, // &
1995 0xc831, // ^
1996 0xc809, // |
1997 0xd0f7, // ~
1998 0x4 // !
1999};
Jack Palevich8b0624c2009-05-20 12:12:06 -07002000#endif
Jack Palevichbf42c9c2009-05-12 12:48:35 -07002001
Jack Palevich1cdef202009-05-22 12:06:27 -07002002struct ACCscript {
2003 ACCscript() {
2004 text = 0;
2005 textLength = 0;
2006 accError = ACC_NO_ERROR;
2007 }
Jack Palevichbbf8ab52009-05-11 11:54:30 -07002008
Jack Palevich1cdef202009-05-22 12:06:27 -07002009 ~ACCscript() {
2010 delete text;
2011 }
Jack Palevich546b2242009-05-13 15:10:04 -07002012
Jack Palevich1cdef202009-05-22 12:06:27 -07002013 void setError(ACCenum error) {
2014 if (accError == ACC_NO_ERROR && error != ACC_NO_ERROR) {
2015 accError = error;
Jack Palevichbbf8ab52009-05-11 11:54:30 -07002016 }
2017 }
2018
Jack Palevich1cdef202009-05-22 12:06:27 -07002019 ACCenum getError() {
2020 ACCenum result = accError;
2021 accError = ACC_NO_ERROR;
Jack Palevich22305132009-05-13 10:58:45 -07002022 return result;
Jack Palevichbbf8ab52009-05-11 11:54:30 -07002023 }
2024
Jack Palevich1cdef202009-05-22 12:06:27 -07002025 Compiler compiler;
2026 char* text;
2027 int textLength;
2028 ACCenum accError;
2029};
2030
2031
2032extern "C"
2033ACCscript* accCreateScript() {
2034 return new ACCscript();
Jack Palevichbbf8ab52009-05-11 11:54:30 -07002035}
Jack Palevich1cdef202009-05-22 12:06:27 -07002036
2037extern "C"
2038ACCenum accGetError( ACCscript* script ) {
2039 return script->getError();
2040}
2041
2042extern "C"
2043void accDeleteScript(ACCscript* script) {
2044 delete script;
2045}
2046
2047extern "C"
2048void accScriptSource(ACCscript* script,
2049 ACCsizei count,
2050 const ACCchar ** string,
2051 const ACCint * length) {
2052 int totalLength = 0;
2053 for(int i = 0; i < count; i++) {
2054 int len = -1;
2055 const ACCchar* s = string[i];
2056 if (length) {
2057 len = length[i];
2058 }
2059 if (len < 0) {
2060 len = strlen(s);
2061 }
2062 totalLength += len;
2063 }
2064 delete script->text;
2065 char* text = new char[totalLength + 1];
2066 script->text = text;
2067 script->textLength = totalLength;
Jack Palevich09555c72009-05-27 12:25:55 -07002068 char* dest = text;
Jack Palevich1cdef202009-05-22 12:06:27 -07002069 for(int i = 0; i < count; i++) {
2070 int len = -1;
2071 const ACCchar* s = string[i];
2072 if (length) {
2073 len = length[i];
2074 }
2075 if (len < 0) {
2076 len = strlen(s);
2077 }
Jack Palevich09555c72009-05-27 12:25:55 -07002078 memcpy(dest, s, len);
2079 dest += len;
Jack Palevich1cdef202009-05-22 12:06:27 -07002080 }
2081 text[totalLength] = '\0';
2082}
2083
2084extern "C"
2085void accCompileScript(ACCscript* script) {
2086 int result = script->compiler.compile(script->text, script->textLength);
2087 if (result) {
2088 script->setError(ACC_INVALID_OPERATION);
2089 }
2090}
2091
2092extern "C"
2093void accGetScriptiv(ACCscript* script,
2094 ACCenum pname,
2095 ACCint * params) {
2096 switch (pname) {
2097 case ACC_INFO_LOG_LENGTH:
2098 *params = 0;
2099 break;
2100 }
2101}
2102
2103extern "C"
2104void accGetScriptInfoLog(ACCscript* script,
2105 ACCsizei maxLength,
2106 ACCsizei * length,
2107 ACCchar * infoLog) {
Jack Palevichac0e95e2009-05-29 13:53:44 -07002108 char* message = script->compiler.getErrorMessage();
2109 int messageLength = strlen(message) + 1;
Jack Palevich1cdef202009-05-22 12:06:27 -07002110 if (length) {
Jack Palevichac0e95e2009-05-29 13:53:44 -07002111 *length = messageLength;
Jack Palevich1cdef202009-05-22 12:06:27 -07002112 }
Jack Palevichac0e95e2009-05-29 13:53:44 -07002113 if (infoLog && maxLength > 0) {
2114 int trimmedLength = maxLength < messageLength ?
2115 maxLength : messageLength;
2116 memcpy(infoLog, message, trimmedLength);
2117 infoLog[trimmedLength] = 0;
Jack Palevich1cdef202009-05-22 12:06:27 -07002118 }
2119}
2120
2121extern "C"
2122void accGetScriptLabel(ACCscript* script, const ACCchar * name,
2123 ACCvoid ** address) {
2124 void* value = script->compiler.lookup(name);
2125 if (value) {
2126 *address = value;
2127 } else {
2128 script->setError(ACC_INVALID_VALUE);
2129 }
2130}
2131
Jack Palevicheedf9d22009-06-04 16:23:40 -07002132extern "C"
2133void accGetPragmas(ACCscript* script, ACCsizei* actualStringCount,
2134 ACCsizei maxStringCount, ACCchar** strings){
2135 script->compiler.getPragmas(actualStringCount, maxStringCount, strings);
2136}
2137
2138
Jack Palevich1cdef202009-05-22 12:06:27 -07002139} // namespace acc
2140