blob: 45f0c732e2ec0b1ac57da5605a39596fbd873d57 [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 Paleviche27bf3e2009-05-10 14:09:03 -070013#include <stdarg.h>
Jack Palevich8b0624c2009-05-20 12:12:06 -070014#include <stdint.h>
Jack Palevichae54f1f2009-05-08 14:54:15 -070015#include <stdio.h>
Jack Palevichf6b5a532009-05-10 19:16:42 -070016#include <stdlib.h>
17#include <string.h>
Jack Palevichae54f1f2009-05-08 14:54:15 -070018
Jack Palevich546b2242009-05-13 15:10:04 -070019#if defined(__arm__)
20#include <unistd.h>
21#endif
22
Jack Paleviche7b59062009-05-19 17:12:17 -070023#if defined(__arm__)
24#define DEFAULT_ARM_CODEGEN
Jack Palevich8b0624c2009-05-20 12:12:06 -070025#define PROVIDE_ARM_CODEGEN
Jack Paleviche7b59062009-05-19 17:12:17 -070026#elif defined(__i386__)
27#define DEFAULT_X86_CODEGEN
Jack Palevich8b0624c2009-05-20 12:12:06 -070028#define PROVIDE_X86_CODEGEN
Jack Paleviche7b59062009-05-19 17:12:17 -070029#elif defined(__x86_64__)
30#define DEFAULT_X64_CODEGEN
Jack Palevich8b0624c2009-05-20 12:12:06 -070031#define PROVIDE_X64_CODEGEN
Jack Paleviche7b59062009-05-19 17:12:17 -070032#endif
33
Jack Paleviche7b59062009-05-19 17:12:17 -070034
35#ifdef PROVIDE_ARM_CODEGEN
Jack Palevicha6535612009-05-13 16:24:17 -070036#include "disassem.h"
Jack Paleviche7b59062009-05-19 17:12:17 -070037#endif
Jack Palevicha6535612009-05-13 16:24:17 -070038
Jack Palevich1cdef202009-05-22 12:06:27 -070039#include <acc/acc.h>
40
Jack Palevich09555c72009-05-27 12:25:55 -070041#define LOG_API(...) do {} while(0)
42// #define LOG_API(...) fprintf (stderr, __VA_ARGS__)
43
44// #define ENABLE_ARM_DISASSEMBLY
45
Jack Palevichbbf8ab52009-05-11 11:54:30 -070046namespace acc {
47
Jack Paleviche7b59062009-05-19 17:12:17 -070048class Compiler {
Jack Palevich21a15a22009-05-11 14:49:29 -070049 class CodeBuf {
Jack Palevich653f42d2009-05-28 17:15:32 -070050 char* ind; // Output code pointer
Jack Palevich21a15a22009-05-11 14:49:29 -070051 char* pProgramBase;
Jack Palevichf0cbc922009-05-08 16:35:13 -070052
Jack Palevich21a15a22009-05-11 14:49:29 -070053 void release() {
54 if (pProgramBase != 0) {
55 free(pProgramBase);
56 pProgramBase = 0;
Jack Palevichae54f1f2009-05-08 14:54:15 -070057 }
Jack Palevich21a15a22009-05-11 14:49:29 -070058 }
59
60 public:
61 CodeBuf() {
62 pProgramBase = 0;
63 ind = 0;
64 }
65
66 ~CodeBuf() {
67 release();
68 }
69
70 void init(int size) {
71 release();
72 pProgramBase = (char*) calloc(1, size);
73 ind = pProgramBase;
74 }
75
Jack Palevich546b2242009-05-13 15:10:04 -070076 int o4(int n) {
Jack Palevich8b0624c2009-05-20 12:12:06 -070077 intptr_t result = (intptr_t) ind;
Jack Palevich546b2242009-05-13 15:10:04 -070078 * (int*) ind = n;
79 ind += 4;
80 return result;
81 }
82
Jack Palevich21a15a22009-05-11 14:49:29 -070083 /*
84 * Output a byte. Handles all values, 0..ff.
85 */
86 void ob(int n) {
87 *ind++ = n;
88 }
89
Jack Palevich21a15a22009-05-11 14:49:29 -070090 inline void* getBase() {
91 return (void*) pProgramBase;
92 }
93
Jack Palevich8b0624c2009-05-20 12:12:06 -070094 intptr_t getSize() {
Jack Palevich21a15a22009-05-11 14:49:29 -070095 return ind - pProgramBase;
96 }
97
Jack Palevich8b0624c2009-05-20 12:12:06 -070098 intptr_t getPC() {
99 return (intptr_t) ind;
Jack Palevich21a15a22009-05-11 14:49:29 -0700100 }
101 };
102
Jack Palevich1cdef202009-05-22 12:06:27 -0700103 /**
104 * A code generator creates an in-memory program, generating the code on
105 * the fly. There is one code generator implementation for each supported
106 * architecture.
107 *
108 * The code generator implements the following abstract machine:
109 * R0 - the main accumulator.
110 * R1 - the secondary accumulator.
111 * FP - a frame pointer for accessing function arguments and local
112 * variables.
113 * SP - a stack pointer for storing intermediate results while evaluating
114 * expressions. The stack pointer grows downwards.
115 *
116 * The function calling convention is that all arguments are placed on the
117 * stack such that the first argument has the lowest address.
118 * After the call, the result is in R0. The caller is responsible for
119 * removing the arguments from the stack.
120 * The R0 and R1 registers are not saved across function calls. The
121 * FP and SP registers are saved.
122 */
123
Jack Palevich21a15a22009-05-11 14:49:29 -0700124 class CodeGenerator {
125 public:
126 CodeGenerator() {}
127 virtual ~CodeGenerator() {}
128
Jack Palevich22305132009-05-13 10:58:45 -0700129 virtual void init(CodeBuf* pCodeBuf) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700130 this->pCodeBuf = pCodeBuf;
131 }
132
Jack Palevich1cdef202009-05-22 12:06:27 -0700133 /* Emit a function prolog.
134 * argCount is the number of arguments.
135 * Save the old value of the FP.
136 * Set the new value of the FP.
137 * Convert from the native platform calling convention to
138 * our stack-based calling convention. This may require
139 * pushing arguments from registers to the stack.
140 * Allocate "N" bytes of stack space. N isn't known yet, so
141 * just emit the instructions for adjusting the stack, and return
142 * the address to patch up. The patching will be done in
143 * functionExit().
144 * returns address to patch with local variable size.
Jack Palevich22305132009-05-13 10:58:45 -0700145 */
Jack Palevich546b2242009-05-13 15:10:04 -0700146 virtual int functionEntry(int argCount) = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700147
Jack Palevich1cdef202009-05-22 12:06:27 -0700148 /* Emit a function epilog.
149 * Restore the old SP and FP register values.
150 * Return to the calling function.
151 * argCount - the number of arguments to the function.
152 * localVariableAddress - returned from functionEntry()
153 * localVariableSize - the size in bytes of the local variables.
154 */
155 virtual void functionExit(int argCount, int localVariableAddress,
156 int localVariableSize) = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700157
Jack Palevich1cdef202009-05-22 12:06:27 -0700158 /* load immediate value to R0 */
Jack Palevich546b2242009-05-13 15:10:04 -0700159 virtual void li(int t) = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700160
Jack Palevich1cdef202009-05-22 12:06:27 -0700161 /* Jump to a target, and return the address of the word that
162 * holds the target data, in case it needs to be fixed up later.
163 */
Jack Palevich22305132009-05-13 10:58:45 -0700164 virtual int gjmp(int t) = 0;
165
Jack Palevich1cdef202009-05-22 12:06:27 -0700166 /* Test R0 and jump to a target if the test succeeds.
167 * l = 0: je, l == 1: jne
168 * Return the address of the word that holds the targed data, in
169 * case it needs to be fixed up later.
170 */
Jack Palevich22305132009-05-13 10:58:45 -0700171 virtual int gtst(bool l, int t) = 0;
172
Jack Palevich1cdef202009-05-22 12:06:27 -0700173 /* Compare R1 against R0, and store the boolean result in R0.
174 * op specifies the comparison.
175 */
Jack Palevich22305132009-05-13 10:58:45 -0700176 virtual void gcmp(int op) = 0;
177
Jack Palevich1cdef202009-05-22 12:06:27 -0700178 /* Perform the arithmetic op specified by op. R1 is the
179 * left argument, R0 is the right argument.
180 */
Jack Palevich546b2242009-05-13 15:10:04 -0700181 virtual void genOp(int op) = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700182
Jack Palevich1cdef202009-05-22 12:06:27 -0700183 /* Set R1 to 0.
184 */
185 virtual void clearR1() = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700186
Jack Palevich1cdef202009-05-22 12:06:27 -0700187 /* Push R0 onto the stack.
188 */
189 virtual void pushR0() = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700190
Jack Palevich1cdef202009-05-22 12:06:27 -0700191 /* Pop R1 off of the stack.
192 */
193 virtual void popR1() = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700194
Jack Palevich1cdef202009-05-22 12:06:27 -0700195 /* Store R0 to the address stored in R1.
196 * isInt is true if a whole 4-byte integer value
197 * should be stored, otherwise a 1-byte character
198 * value should be stored.
199 */
200 virtual void storeR0ToR1(bool isInt) = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700201
Jack Palevich1cdef202009-05-22 12:06:27 -0700202 /* Load R0 from the address stored in R0.
203 * isInt is true if a whole 4-byte integer value
204 * should be loaded, otherwise a 1-byte character
205 * value should be loaded.
206 */
207 virtual void loadR0FromR0(bool isInt) = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700208
Jack Palevich1cdef202009-05-22 12:06:27 -0700209 /* Load the absolute address of a variable to R0.
210 * If ea <= LOCAL, then this is a local variable, or an
211 * argument, addressed relative to FP.
212 * else it is an absolute global address.
213 */
214 virtual void leaR0(int ea) = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700215
Jack Palevich1cdef202009-05-22 12:06:27 -0700216 /* Store R0 to a variable.
217 * If ea <= LOCAL, then this is a local variable, or an
218 * argument, addressed relative to FP.
219 * else it is an absolute global address.
220 */
221 virtual void storeR0(int ea) = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700222
Jack Palevich1cdef202009-05-22 12:06:27 -0700223 /* load R0 from a variable.
224 * If ea <= LOCAL, then this is a local variable, or an
225 * argument, addressed relative to FP.
226 * else it is an absolute global address.
227 * If isIncDec is true, then the stored variable's value
228 * should be post-incremented or post-decremented, based
229 * on the value of op.
230 */
231 virtual void loadR0(int ea, bool isIncDec, int op) = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700232
Jack Palevich1cdef202009-05-22 12:06:27 -0700233 /* Emit code to adjust the stack for a function call. Return the
234 * label for the address of the instruction that adjusts the
235 * stack size. This will be passed as argument "a" to
236 * endFunctionCallArguments.
237 */
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700238 virtual int beginFunctionCallArguments() = 0;
239
Jack Palevich1cdef202009-05-22 12:06:27 -0700240 /* Emit code to store R0 to the stack at byte offset l.
241 */
242 virtual void storeR0ToArg(int l) = 0;
Jack Palevich7810bc92009-05-15 14:31:47 -0700243
Jack Palevich1cdef202009-05-22 12:06:27 -0700244 /* Patch the function call preamble.
245 * a is the address returned from beginFunctionCallArguments
246 * l is the number of bytes the arguments took on the stack.
247 * Typically you would also emit code to convert the argument
248 * list into whatever the native function calling convention is.
249 * On ARM for example you would pop the first 5 arguments into
250 * R0..R4
251 */
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700252 virtual void endFunctionCallArguments(int a, int l) = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700253
Jack Palevich1cdef202009-05-22 12:06:27 -0700254 /* Emit a call to an unknown function. The argument "symbol" needs to
255 * be stored in the location where the address should go. It forms
256 * a chain. The address will be patched later.
257 * Return the address of the word that has to be patched.
258 */
Jack Palevich22305132009-05-13 10:58:45 -0700259 virtual int callForward(int symbol) = 0;
260
Jack Palevich1cdef202009-05-22 12:06:27 -0700261 /* Call a function using PC-relative addressing. t is the PC-relative
262 * address of the function. It has already been adjusted for the
263 * architectural jump offset, so just store it as-is.
264 */
Jack Palevich22305132009-05-13 10:58:45 -0700265 virtual void callRelative(int t) = 0;
266
Jack Palevich1cdef202009-05-22 12:06:27 -0700267 /* Call a function pointer. L is the number of bytes the arguments
268 * take on the stack. The address of the function is stored at
269 * location SP + l.
270 */
Jack Palevich22305132009-05-13 10:58:45 -0700271 virtual void callIndirect(int l) = 0;
272
Jack Palevich1cdef202009-05-22 12:06:27 -0700273 /* Adjust SP after returning from a function call. l is the
274 * number of bytes of arguments stored on the stack. isIndirect
275 * is true if this was an indirect call. (In which case the
276 * address of the function is stored at location SP + l.)
277 */
Jack Palevich7810bc92009-05-15 14:31:47 -0700278 virtual void adjustStackAfterCall(int l, bool isIndirect) = 0;
Jack Palevich22305132009-05-13 10:58:45 -0700279
Jack Palevich1cdef202009-05-22 12:06:27 -0700280 /* Print a disassembly of the assembled code to out. Return
281 * non-zero if there is an error.
282 */
Jack Palevicha6535612009-05-13 16:24:17 -0700283 virtual int disassemble(FILE* out) = 0;
284
Jack Palevich1cdef202009-05-22 12:06:27 -0700285 /* Generate a symbol at the current PC. t is the head of a
286 * linked list of addresses to patch.
287 */
Jack Paleviche7b59062009-05-19 17:12:17 -0700288 virtual void gsym(int t) = 0;
Jack Palevich21a15a22009-05-11 14:49:29 -0700289
Jack Palevich1cdef202009-05-22 12:06:27 -0700290 /*
291 * Do any cleanup work required at the end of a compile.
292 * For example, an instruction cache might need to be
293 * invalidated.
294 * Return non-zero if there is an error.
295 */
296 virtual int finishCompile() = 0;
Jack Palevich546b2242009-05-13 15:10:04 -0700297
Jack Palevicha6535612009-05-13 16:24:17 -0700298 /**
299 * Adjust relative branches by this amount.
300 */
301 virtual int jumpOffset() = 0;
302
Jack Palevich21a15a22009-05-11 14:49:29 -0700303 protected:
Jack Palevich21a15a22009-05-11 14:49:29 -0700304 /*
305 * Output a byte. Handles all values, 0..ff.
306 */
307 void ob(int n) {
308 pCodeBuf->ob(n);
309 }
310
Jack Palevich8b0624c2009-05-20 12:12:06 -0700311 intptr_t o4(int data) {
Jack Paleviche7b59062009-05-19 17:12:17 -0700312 return pCodeBuf->o4(data);
Jack Palevich21a15a22009-05-11 14:49:29 -0700313 }
314
Jack Palevich8b0624c2009-05-20 12:12:06 -0700315 intptr_t getBase() {
316 return (intptr_t) pCodeBuf->getBase();
Jack Palevicha6535612009-05-13 16:24:17 -0700317 }
318
Jack Palevich8b0624c2009-05-20 12:12:06 -0700319 intptr_t getPC() {
Jack Palevich21a15a22009-05-11 14:49:29 -0700320 return pCodeBuf->getPC();
321 }
Jack Palevich1cdef202009-05-22 12:06:27 -0700322
323 intptr_t getSize() {
324 return pCodeBuf->getSize();
325 }
Jack Palevich21a15a22009-05-11 14:49:29 -0700326 private:
327 CodeBuf* pCodeBuf;
328 };
329
Jack Paleviche7b59062009-05-19 17:12:17 -0700330#ifdef PROVIDE_ARM_CODEGEN
331
Jack Palevich22305132009-05-13 10:58:45 -0700332 class ARMCodeGenerator : public CodeGenerator {
333 public:
334 ARMCodeGenerator() {}
335 virtual ~ARMCodeGenerator() {}
336
337 /* returns address to patch with local variable size
338 */
Jack Palevich546b2242009-05-13 15:10:04 -0700339 virtual int functionEntry(int argCount) {
Jack Palevich09555c72009-05-27 12:25:55 -0700340 LOG_API(stderr, "functionEntry(%d);\n", argCount);
Jack Palevich69796b62009-05-14 15:42:26 -0700341 // sp -> arg4 arg5 ...
342 // Push our register-based arguments back on the stack
343 if (argCount > 0) {
344 int regArgCount = argCount <= 4 ? argCount : 4;
345 o4(0xE92D0000 | ((1 << argCount) - 1)); // stmfd sp!, {}
346 }
347 // sp -> arg0 arg1 ...
348 o4(0xE92D4800); // stmfd sp!, {fp, lr}
349 // sp, fp -> oldfp, retadr, arg0 arg1 ....
350 o4(0xE1A0B00D); // mov fp, sp
351 return o4(0xE24DD000); // sub sp, sp, # <local variables>
Jack Palevich22305132009-05-13 10:58:45 -0700352 }
353
Jack Palevich546b2242009-05-13 15:10:04 -0700354 virtual void functionExit(int argCount, int localVariableAddress, int localVariableSize) {
Jack Palevich09555c72009-05-27 12:25:55 -0700355 LOG_API("functionExit(%d, %d, %d);\n", argCount, localVariableAddress, localVariableSize);
Jack Palevich69796b62009-05-14 15:42:26 -0700356 // Patch local variable allocation code:
357 if (localVariableSize < 0 || localVariableSize > 255) {
Jack Palevich8de461d2009-05-14 17:21:45 -0700358 error("localVariables out of range: %d", localVariableSize);
Jack Palevich546b2242009-05-13 15:10:04 -0700359 }
Jack Palevich69796b62009-05-14 15:42:26 -0700360 *(char*) (localVariableAddress) = localVariableSize;
361
362 // sp -> locals .... fp -> oldfp, retadr, arg0, arg1, ...
363 o4(0xE1A0E00B); // mov lr, fp
364 o4(0xE59BB000); // ldr fp, [fp]
365 o4(0xE28ED004); // add sp, lr, #4
366 // sp -> retadr, arg0, ...
367 o4(0xE8BD4000); // ldmfd sp!, {lr}
368 // sp -> arg0 ....
369 if (argCount > 0) {
370 // We store the PC into the lr so we can adjust the sp before
Jack Palevich8de461d2009-05-14 17:21:45 -0700371 // returning. We need to pull off the registers we pushed
Jack Palevich69796b62009-05-14 15:42:26 -0700372 // earlier. We don't need to actually store them anywhere,
373 // just adjust the stack.
374 int regArgCount = argCount <= 4 ? argCount : 4;
375 o4(0xE28DD000 | (regArgCount << 2)); // add sp, sp, #argCount << 2
376 }
377 o4(0xE12FFF1E); // bx lr
Jack Palevich22305132009-05-13 10:58:45 -0700378 }
379
380 /* load immediate value */
Jack Palevich546b2242009-05-13 15:10:04 -0700381 virtual void li(int t) {
Jack Palevich09555c72009-05-27 12:25:55 -0700382 LOG_API("li(%d);\n", t);
Jack Palevicha6535612009-05-13 16:24:17 -0700383 if (t >= 0 && t < 255) {
Jack Palevich69796b62009-05-14 15:42:26 -0700384 o4(0xE3A00000 + t); // mov r0, #0
Jack Palevicha6535612009-05-13 16:24:17 -0700385 } else if (t >= -256 && t < 0) {
386 // mvn means move constant ^ ~0
Jack Palevich69796b62009-05-14 15:42:26 -0700387 o4(0xE3E00001 - t); // mvn r0, #0
Jack Palevicha6535612009-05-13 16:24:17 -0700388 } else {
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700389 o4(0xE51F0000); // ldr r0, .L3
390 o4(0xEA000000); // b .L99
391 o4(t); // .L3: .word 0
392 // .L99:
Jack Palevicha6535612009-05-13 16:24:17 -0700393 }
Jack Palevich22305132009-05-13 10:58:45 -0700394 }
395
396 virtual int gjmp(int t) {
Jack Palevich09555c72009-05-27 12:25:55 -0700397 LOG_API("gjmp(%d);\n", t);
Jack Palevich8de461d2009-05-14 17:21:45 -0700398 return o4(0xEA000000 | encodeAddress(t)); // b .L33
Jack Palevich22305132009-05-13 10:58:45 -0700399 }
400
401 /* l = 0: je, l == 1: jne */
402 virtual int gtst(bool l, int t) {
Jack Palevich09555c72009-05-27 12:25:55 -0700403 LOG_API("gtst(%d, %d);\n", l, t);
Jack Palevich8de461d2009-05-14 17:21:45 -0700404 o4(0xE3500000); // cmp r0,#0
405 int branch = l ? 0x1A000000 : 0x0A000000; // bne : beq
406 return o4(branch | encodeAddress(t));
Jack Palevich22305132009-05-13 10:58:45 -0700407 }
408
409 virtual void gcmp(int op) {
Jack Palevich09555c72009-05-27 12:25:55 -0700410 LOG_API("gcmp(%d);\n", op);
Jack Palevich8de461d2009-05-14 17:21:45 -0700411 o4(0xE1510000); // cmp r1, r1
412 switch(op) {
413 case OP_EQUALS:
414 o4(0x03A00001); // moveq r0,#1
415 o4(0x13A00000); // movne r0,#0
416 break;
417 case OP_NOT_EQUALS:
418 o4(0x03A00000); // moveq r0,#0
419 o4(0x13A00001); // movne r0,#1
420 break;
421 case OP_LESS_EQUAL:
422 o4(0xD3A00001); // movle r0,#1
423 o4(0xC3A00000); // movgt r0,#0
424 break;
425 case OP_GREATER:
426 o4(0xD3A00000); // movle r0,#0
427 o4(0xC3A00001); // movgt r0,#1
428 break;
429 case OP_GREATER_EQUAL:
430 o4(0xA3A00001); // movge r0,#1
431 o4(0xB3A00000); // movlt r0,#0
432 break;
433 case OP_LESS:
434 o4(0xA3A00000); // movge r0,#0
435 o4(0xB3A00001); // movlt r0,#1
436 break;
437 default:
438 error("Unknown comparison op %d", op);
439 break;
440 }
Jack Palevich22305132009-05-13 10:58:45 -0700441 }
442
Jack Palevich546b2242009-05-13 15:10:04 -0700443 virtual void genOp(int op) {
Jack Palevich09555c72009-05-27 12:25:55 -0700444 LOG_API("genOp(%d);\n", op);
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700445 switch(op) {
446 case OP_MUL:
447 o4(0x0E0000091); // mul r0,r1,r0
448 break;
Jack Palevich3d474a72009-05-15 15:12:38 -0700449 case OP_DIV:
450 callRuntime(runtime_DIV);
451 break;
452 case OP_MOD:
453 callRuntime(runtime_MOD);
454 break;
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700455 case OP_PLUS:
456 o4(0xE0810000); // add r0,r1,r0
457 break;
458 case OP_MINUS:
459 o4(0xE0410000); // sub r0,r1,r0
460 break;
461 case OP_SHIFT_LEFT:
462 o4(0xE1A00011); // lsl r0,r1,r0
463 break;
464 case OP_SHIFT_RIGHT:
465 o4(0xE1A00051); // asr r0,r1,r0
466 break;
467 case OP_BIT_AND:
468 o4(0xE0010000); // and r0,r1,r0
469 break;
470 case OP_BIT_XOR:
471 o4(0xE0210000); // eor r0,r1,r0
472 break;
473 case OP_BIT_OR:
474 o4(0xE1810000); // orr r0,r1,r0
475 break;
476 case OP_BIT_NOT:
477 o4(0xE1E00000); // mvn r0, r0
478 break;
479 default:
Jack Palevich69796b62009-05-14 15:42:26 -0700480 error("Unimplemented op %d\n", op);
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700481 break;
482 }
Jack Palevich22305132009-05-13 10:58:45 -0700483#if 0
484 o(decodeOp(op));
485 if (op == OP_MOD)
486 o(0x92); /* xchg %edx, %eax */
487#endif
488 }
489
Jack Palevich1cdef202009-05-22 12:06:27 -0700490 virtual void clearR1() {
Jack Palevich09555c72009-05-27 12:25:55 -0700491 LOG_API("clearR1();\n");
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700492 o4(0xE3A01000); // mov r1, #0
Jack Palevich22305132009-05-13 10:58:45 -0700493 }
494
Jack Palevich1cdef202009-05-22 12:06:27 -0700495 virtual void pushR0() {
Jack Palevich09555c72009-05-27 12:25:55 -0700496 LOG_API("pushR0();\n");
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700497 o4(0xE92D0001); // stmfd sp!,{r0}
Jack Palevich22305132009-05-13 10:58:45 -0700498 }
499
Jack Palevich1cdef202009-05-22 12:06:27 -0700500 virtual void popR1() {
Jack Palevich09555c72009-05-27 12:25:55 -0700501 LOG_API("popR1();\n");
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700502 o4(0xE8BD0002); // ldmfd sp!,{r1}
Jack Palevich22305132009-05-13 10:58:45 -0700503 }
504
Jack Palevich1cdef202009-05-22 12:06:27 -0700505 virtual void storeR0ToR1(bool isInt) {
Jack Palevich09555c72009-05-27 12:25:55 -0700506 LOG_API("storeR0ToR1(%d);\n", isInt);
Jack Palevichbd894902009-05-14 19:35:31 -0700507 if (isInt) {
508 o4(0xE5810000); // str r0, [r1]
509 } else {
510 o4(0xE5C10000); // strb r0, [r1]
511 }
Jack Palevich22305132009-05-13 10:58:45 -0700512 }
513
Jack Palevich1cdef202009-05-22 12:06:27 -0700514 virtual void loadR0FromR0(bool isInt) {
Jack Palevich09555c72009-05-27 12:25:55 -0700515 LOG_API("loadR0FromR0(%d);\n", isInt);
Jack Palevich22305132009-05-13 10:58:45 -0700516 if (isInt)
Jack Palevich69796b62009-05-14 15:42:26 -0700517 o4(0xE5900000); // ldr r0, [r0]
Jack Palevich22305132009-05-13 10:58:45 -0700518 else
Jack Palevich69796b62009-05-14 15:42:26 -0700519 o4(0xE5D00000); // ldrb r0, [r0]
Jack Palevich22305132009-05-13 10:58:45 -0700520 }
521
Jack Palevich1cdef202009-05-22 12:06:27 -0700522 virtual void leaR0(int ea) {
Jack Palevich09555c72009-05-27 12:25:55 -0700523 LOG_API("leaR0(%d);\n", ea);
Jack Palevich4d93f302009-05-15 13:30:00 -0700524 if (ea < LOCAL) {
525 // Local, fp relative
526 if (ea < -1023 || ea > 1023 || ((ea & 3) != 0)) {
527 error("Offset out of range: %08x", ea);
528 }
529 if (ea < 0) {
530 o4(0xE24B0F00 | (0xff & ((-ea) >> 2))); // sub r0, fp, #ea
531 } else {
532 o4(0xE28B0F00 | (0xff & (ea >> 2))); // add r0, fp, #ea
533 }
Jack Palevichbd894902009-05-14 19:35:31 -0700534 } else {
Jack Palevich4d93f302009-05-15 13:30:00 -0700535 // Global, absolute.
536 o4(0xE59F0000); // ldr r0, .L1
537 o4(0xEA000000); // b .L99
538 o4(ea); // .L1: .word 0
539 // .L99:
Jack Palevichbd894902009-05-14 19:35:31 -0700540 }
Jack Palevich22305132009-05-13 10:58:45 -0700541 }
542
Jack Palevich1cdef202009-05-22 12:06:27 -0700543 virtual void storeR0(int ea) {
Jack Palevich09555c72009-05-27 12:25:55 -0700544 LOG_API("storeR0(%d);\n", ea);
Jack Palevich4d93f302009-05-15 13:30:00 -0700545 if (ea < LOCAL) {
546 // Local, fp relative
547 if (ea < -4095 || ea > 4095) {
548 error("Offset out of range: %08x", ea);
549 }
550 if (ea < 0) {
551 o4(0xE50B0000 | (0xfff & (-ea))); // str r0, [fp,#-ea]
552 } else {
553 o4(0xE58B0000 | (0xfff & ea)); // str r0, [fp,#ea]
554 }
555 } else{
556 // Global, absolute
557 o4(0xE59F1000); // ldr r1, .L1
558 o4(0xEA000000); // b .L99
559 o4(ea); // .L1: .word 0
560 o4(0xE5810000); // .L99: str r0, [r1]
Jack Palevich69796b62009-05-14 15:42:26 -0700561 }
Jack Palevich22305132009-05-13 10:58:45 -0700562 }
563
Jack Palevich1cdef202009-05-22 12:06:27 -0700564 virtual void loadR0(int ea, bool isIncDec, int op) {
Jack Palevich09555c72009-05-27 12:25:55 -0700565 LOG_API("loadR0(%d, %d, %d);\n", ea, isIncDec, op);
Jack Palevich4d93f302009-05-15 13:30:00 -0700566 if (ea < LOCAL) {
567 // Local, fp relative
568 if (ea < -4095 || ea > 4095) {
569 error("Offset out of range: %08x", ea);
570 }
571 if (ea < 0) {
572 o4(0xE51B0000 | (0xfff & (-ea))); // ldr r0, [fp,#-ea]
573 } else {
574 o4(0xE59B0000 | (0xfff & ea)); // ldr r0, [fp,#ea]
575 }
Jack Palevich69796b62009-05-14 15:42:26 -0700576 } else {
Jack Palevich4d93f302009-05-15 13:30:00 -0700577 // Global, absolute
578 o4(0xE59F2000); // ldr r2, .L1
579 o4(0xEA000000); // b .L99
580 o4(ea); // .L1: .word ea
581 o4(0xE5920000); // .L99: ldr r0, [r2]
Jack Palevich69796b62009-05-14 15:42:26 -0700582 }
Jack Palevich22305132009-05-13 10:58:45 -0700583
Jack Palevich4d93f302009-05-15 13:30:00 -0700584 if (isIncDec) {
585 switch (op) {
586 case OP_INCREMENT:
587 o4(0xE2801001); // add r1, r0, #1
588 break;
589 case OP_DECREMENT:
590 o4(0xE2401001); // sub r1, r0, #1
591 break;
592 default:
593 error("unknown opcode: %d", op);
594 }
595 if (ea < LOCAL) {
596 // Local, fp relative
597 // Don't need range check, was already checked above
598 if (ea < 0) {
599 o4(0xE50B1000 | (0xfff & (-ea))); // str r1, [fp,#-ea]
600 } else {
601 o4(0xE58B1000 | (0xfff & ea)); // str r1, [fp,#ea]
602 }
603 } else{
604 // Global, absolute
605 // r2 is already set up from before.
606 o4(0xE5821000); // str r1, [r2]
607 }
Jack Palevichbd894902009-05-14 19:35:31 -0700608 }
Jack Palevich22305132009-05-13 10:58:45 -0700609 }
610
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700611 virtual int beginFunctionCallArguments() {
Jack Palevich09555c72009-05-27 12:25:55 -0700612 LOG_API("beginFunctionCallArguments();\n");
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700613 return o4(0xE24DDF00); // Placeholder
614 }
615
Jack Palevich1cdef202009-05-22 12:06:27 -0700616 virtual void storeR0ToArg(int l) {
Jack Palevich09555c72009-05-27 12:25:55 -0700617 LOG_API("storeR0ToArg(%d);\n", l);
Jack Palevich7810bc92009-05-15 14:31:47 -0700618 if (l < 0 || l > 4096-4) {
619 error("l out of range for stack offset: 0x%08x", l);
620 }
621 o4(0xE58D0000 + l); // str r0, [sp, #4]
622 }
623
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700624 virtual void endFunctionCallArguments(int a, int l) {
Jack Palevich09555c72009-05-27 12:25:55 -0700625 LOG_API("endFunctionCallArguments(0x%08x, %d);\n", a, l);
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700626 if (l < 0 || l > 0x3FC) {
627 error("L out of range for stack adjustment: 0x%08x", l);
628 }
629 * (int*) a = 0xE24DDF00 | (l >> 2); // sub sp, sp, #0 << 2
630 int argCount = l >> 2;
631 if (argCount > 0) {
632 int regArgCount = argCount > 4 ? 4 : argCount;
633 o4(0xE8BD0000 | ((1 << regArgCount) - 1)); // ldmfd sp!,{}
634 }
Jack Palevich22305132009-05-13 10:58:45 -0700635 }
636
Jack Palevich22305132009-05-13 10:58:45 -0700637 virtual int callForward(int symbol) {
Jack Palevich09555c72009-05-27 12:25:55 -0700638 LOG_API("callForward(%d);\n", symbol);
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700639 // Forward calls are always short (local)
640 return o4(0xEB000000 | encodeAddress(symbol));
Jack Palevich22305132009-05-13 10:58:45 -0700641 }
642
643 virtual void callRelative(int t) {
Jack Palevich09555c72009-05-27 12:25:55 -0700644 LOG_API("callRelative(%d);\n", t);
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700645 int abs = t + getPC() + jumpOffset();
Jack Palevichbd894902009-05-14 19:35:31 -0700646 fprintf(stderr, "abs=%d (0x%08x)\n", abs, abs);
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700647 if (t >= - (1 << 25) && t < (1 << 25)) {
648 o4(0xEB000000 | encodeAddress(t));
649 } else {
650 // Long call.
651 o4(0xE59FC000); // ldr r12, .L1
652 o4(0xEA000000); // b .L99
Jack Palevichbd894902009-05-14 19:35:31 -0700653 o4(t - 12); // .L1: .word 0
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700654 o4(0xE08CC00F); // .L99: add r12,pc
655 o4(0xE12FFF3C); // blx r12
656 }
Jack Palevich22305132009-05-13 10:58:45 -0700657 }
658
659 virtual void callIndirect(int l) {
Jack Palevich09555c72009-05-27 12:25:55 -0700660 LOG_API("callIndirect(%d);\n", l);
Jack Palevich7810bc92009-05-15 14:31:47 -0700661 int argCount = l >> 2;
662 int poppedArgs = argCount > 4 ? 4 : argCount;
663 int adjustedL = l - (poppedArgs << 2);
664 if (adjustedL < 0 || adjustedL > 4096-4) {
665 error("l out of range for stack offset: 0x%08x", l);
666 }
667 o4(0xE59DC000 | (0xfff & adjustedL)); // ldr r12, [sp,#adjustedL]
668 o4(0xE12FFF3C); // blx r12
Jack Palevich22305132009-05-13 10:58:45 -0700669 }
670
Jack Palevich7810bc92009-05-15 14:31:47 -0700671 virtual void adjustStackAfterCall(int l, bool isIndirect) {
Jack Palevich09555c72009-05-27 12:25:55 -0700672 LOG_API("adjustStackAfterCall(%d, %d);\n", l, isIndirect);
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700673 int argCount = l >> 2;
Jack Palevich7810bc92009-05-15 14:31:47 -0700674 int stackArgs = argCount > 4 ? argCount - 4 : 0;
675 int stackUse = stackArgs + (isIndirect ? 1 : 0);
676 if (stackUse) {
677 if (stackUse < 0 || stackUse > 255) {
678 error("L out of range for stack adjustment: 0x%08x", l);
679 }
680 o4(0xE28DDF00 | stackUse); // add sp, sp, #stackUse << 2
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700681 }
Jack Palevich22305132009-05-13 10:58:45 -0700682 }
683
Jack Palevicha6535612009-05-13 16:24:17 -0700684 virtual int jumpOffset() {
Jack Palevichbd894902009-05-14 19:35:31 -0700685 return 8;
Jack Palevicha6535612009-05-13 16:24:17 -0700686 }
687
688 /* output a symbol and patch all calls to it */
689 virtual void gsym(int t) {
Jack Palevich09555c72009-05-27 12:25:55 -0700690 LOG_API("gsym(0x%x)\n", t);
Jack Palevicha6535612009-05-13 16:24:17 -0700691 int n;
692 int base = getBase();
693 int pc = getPC();
Jack Palevich09555c72009-05-27 12:25:55 -0700694 LOG_API("pc = 0x%x\n", pc);
Jack Palevicha6535612009-05-13 16:24:17 -0700695 while (t) {
696 int data = * (int*) t;
697 int decodedOffset = ((BRANCH_REL_ADDRESS_MASK & data) << 2);
698 if (decodedOffset == 0) {
699 n = 0;
700 } else {
701 n = base + decodedOffset; /* next value */
702 }
703 *(int *) t = (data & ~BRANCH_REL_ADDRESS_MASK)
704 | encodeRelAddress(pc - t - 8);
705 t = n;
706 }
707 }
708
Jack Palevich1cdef202009-05-22 12:06:27 -0700709 virtual int finishCompile() {
710#if defined(__arm__)
711 const long base = long(getBase());
712 const long curr = long(getPC());
713 int err = cacheflush(base, curr, 0);
714 return err;
715#else
716 return 0;
717#endif
718 }
719
Jack Palevicha6535612009-05-13 16:24:17 -0700720 virtual int disassemble(FILE* out) {
Jack Palevich09555c72009-05-27 12:25:55 -0700721#ifdef ENABLE_ARM_DISASSEMBLY
722 disasmOut = out;
Jack Palevicha6535612009-05-13 16:24:17 -0700723 disasm_interface_t di;
724 di.di_readword = disassemble_readword;
725 di.di_printaddr = disassemble_printaddr;
726 di.di_printf = disassemble_printf;
727
728 int base = getBase();
729 int pc = getPC();
730 for(int i = base; i < pc; i += 4) {
731 fprintf(out, "%08x: %08x ", i, *(int*) i);
732 ::disasm(&di, i, 0);
733 }
Jack Palevich09555c72009-05-27 12:25:55 -0700734#endif
Jack Palevicha6535612009-05-13 16:24:17 -0700735 return 0;
736 }
Jack Palevich7810bc92009-05-15 14:31:47 -0700737
Jack Palevich22305132009-05-13 10:58:45 -0700738 private:
Jack Palevicha6535612009-05-13 16:24:17 -0700739 static FILE* disasmOut;
740
741 static u_int
742 disassemble_readword(u_int address)
743 {
744 return(*((u_int *)address));
745 }
746
747 static void
748 disassemble_printaddr(u_int address)
749 {
750 fprintf(disasmOut, "0x%08x", address);
751 }
752
753 static void
754 disassemble_printf(const char *fmt, ...) {
755 va_list ap;
756 va_start(ap, fmt);
757 vfprintf(disasmOut, fmt, ap);
758 va_end(ap);
759 }
760
761 static const int BRANCH_REL_ADDRESS_MASK = 0x00ffffff;
762
763 /** Encode a relative address that might also be
764 * a label.
765 */
766 int encodeAddress(int value) {
767 int base = getBase();
768 if (value >= base && value <= getPC() ) {
769 // This is a label, encode it relative to the base.
770 value = value - base;
771 }
772 return encodeRelAddress(value);
773 }
774
775 int encodeRelAddress(int value) {
776 return BRANCH_REL_ADDRESS_MASK & (value >> 2);
777 }
Jack Palevich22305132009-05-13 10:58:45 -0700778
Jack Palevich3d474a72009-05-15 15:12:38 -0700779 typedef int (*int2FnPtr)(int a, int b);
780 void callRuntime(int2FnPtr fn) {
781 o4(0xE59F2000); // ldr r2, .L1
782 o4(0xEA000000); // b .L99
783 o4((int) fn); //.L1: .word fn
784 o4(0xE12FFF32); //.L99: blx r2
785 }
786
787 static int runtime_DIV(int a, int b) {
788 return b / a;
789 }
790
791 static int runtime_MOD(int a, int b) {
792 return b % a;
793 }
794
Jack Palevich546b2242009-05-13 15:10:04 -0700795 void error(const char* fmt,...) {
796 va_list ap;
797 va_start(ap, fmt);
798 vfprintf(stderr, fmt, ap);
799 va_end(ap);
800 exit(12);
801 }
Jack Palevich22305132009-05-13 10:58:45 -0700802 };
803
Jack Palevich09555c72009-05-27 12:25:55 -0700804#endif // PROVIDE_ARM_CODEGEN
Jack Paleviche7b59062009-05-19 17:12:17 -0700805
806#ifdef PROVIDE_X86_CODEGEN
807
Jack Palevich21a15a22009-05-11 14:49:29 -0700808 class X86CodeGenerator : public CodeGenerator {
809 public:
810 X86CodeGenerator() {}
811 virtual ~X86CodeGenerator() {}
812
Jack Palevichbf42c9c2009-05-12 12:48:35 -0700813 /* returns address to patch with local variable size
814 */
Jack Palevich546b2242009-05-13 15:10:04 -0700815 virtual int functionEntry(int argCount) {
Jack Palevichbf42c9c2009-05-12 12:48:35 -0700816 o(0xe58955); /* push %ebp, mov %esp, %ebp */
817 return oad(0xec81, 0); /* sub $xxx, %esp */
818 }
819
Jack Palevich546b2242009-05-13 15:10:04 -0700820 virtual void functionExit(int argCount, int localVariableAddress, int localVariableSize) {
Jack Palevichbf42c9c2009-05-12 12:48:35 -0700821 o(0xc3c9); /* leave, ret */
Jack Palevich546b2242009-05-13 15:10:04 -0700822 *(int *) localVariableAddress = localVariableSize; /* save local variables */
Jack Palevichbf42c9c2009-05-12 12:48:35 -0700823 }
824
Jack Palevich21a15a22009-05-11 14:49:29 -0700825 /* load immediate value */
Jack Palevich546b2242009-05-13 15:10:04 -0700826 virtual void li(int t) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700827 oad(0xb8, t); /* mov $xx, %eax */
828 }
829
Jack Palevich22305132009-05-13 10:58:45 -0700830 virtual int gjmp(int t) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700831 return psym(0xe9, t);
832 }
833
834 /* l = 0: je, l == 1: jne */
Jack Palevich22305132009-05-13 10:58:45 -0700835 virtual int gtst(bool l, int t) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700836 o(0x0fc085); /* test %eax, %eax, je/jne xxx */
837 return psym(0x84 + l, t);
838 }
839
Jack Palevich22305132009-05-13 10:58:45 -0700840 virtual void gcmp(int op) {
Jack Palevichbf42c9c2009-05-12 12:48:35 -0700841 int t = decodeOp(op);
Jack Palevich21a15a22009-05-11 14:49:29 -0700842 o(0xc139); /* cmp %eax,%ecx */
843 li(0);
844 o(0x0f); /* setxx %al */
845 o(t + 0x90);
846 o(0xc0);
847 }
848
Jack Palevich546b2242009-05-13 15:10:04 -0700849 virtual void genOp(int op) {
Jack Palevichbf42c9c2009-05-12 12:48:35 -0700850 o(decodeOp(op));
851 if (op == OP_MOD)
852 o(0x92); /* xchg %edx, %eax */
853 }
854
Jack Palevich1cdef202009-05-22 12:06:27 -0700855 virtual void clearR1() {
Jack Palevich21a15a22009-05-11 14:49:29 -0700856 oad(0xb9, 0); /* movl $0, %ecx */
857 }
858
Jack Palevich1cdef202009-05-22 12:06:27 -0700859 virtual void pushR0() {
Jack Palevich21a15a22009-05-11 14:49:29 -0700860 o(0x50); /* push %eax */
861 }
862
Jack Palevich1cdef202009-05-22 12:06:27 -0700863 virtual void popR1() {
Jack Palevich21a15a22009-05-11 14:49:29 -0700864 o(0x59); /* pop %ecx */
Jack Palevichbf42c9c2009-05-12 12:48:35 -0700865 }
866
Jack Palevich1cdef202009-05-22 12:06:27 -0700867 virtual void storeR0ToR1(bool isInt) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700868 o(0x0188 + isInt); /* movl %eax/%al, (%ecx) */
869 }
870
Jack Palevich1cdef202009-05-22 12:06:27 -0700871 virtual void loadR0FromR0(bool isInt) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700872 if (isInt)
873 o(0x8b); /* mov (%eax), %eax */
874 else
875 o(0xbe0f); /* movsbl (%eax), %eax */
876 ob(0); /* add zero in code */
877 }
878
Jack Palevich1cdef202009-05-22 12:06:27 -0700879 virtual void leaR0(int ea) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700880 gmov(10, ea); /* leal EA, %eax */
881 }
882
Jack Palevich1cdef202009-05-22 12:06:27 -0700883 virtual void storeR0(int ea) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700884 gmov(6, ea); /* mov %eax, EA */
885 }
886
Jack Palevich1cdef202009-05-22 12:06:27 -0700887 virtual void loadR0(int ea, bool isIncDec, int op) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700888 gmov(8, ea); /* mov EA, %eax */
Jack Palevich4d93f302009-05-15 13:30:00 -0700889 if (isIncDec) {
890 /* Implement post-increment or post decrement.
891 */
892 gmov(0, ea); /* 83 ADD */
893 o(decodeOp(op));
894 }
Jack Palevich21a15a22009-05-11 14:49:29 -0700895 }
896
Jack Palevichcb1c9ef2009-05-14 11:38:49 -0700897 virtual int beginFunctionCallArguments() {
Jack Palevich21a15a22009-05-11 14:49:29 -0700898 return oad(0xec81, 0); /* sub $xxx, %esp */
899 }
900
Jack Palevich1cdef202009-05-22 12:06:27 -0700901 virtual void storeR0ToArg(int l) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700902 oad(0x248489, l); /* movl %eax, xxx(%esp) */
903 }
904
Jack Palevich7810bc92009-05-15 14:31:47 -0700905 virtual void endFunctionCallArguments(int a, int l) {
906 * (int*) a = l;
907 }
908
Jack Palevich22305132009-05-13 10:58:45 -0700909 virtual int callForward(int symbol) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700910 return psym(0xe8, symbol); /* call xxx */
911 }
912
Jack Palevich22305132009-05-13 10:58:45 -0700913 virtual void callRelative(int t) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700914 psym(0xe8, t); /* call xxx */
915 }
916
Jack Palevich22305132009-05-13 10:58:45 -0700917 virtual void callIndirect(int l) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700918 oad(0x2494ff, l); /* call *xxx(%esp) */
919 }
920
Jack Palevich7810bc92009-05-15 14:31:47 -0700921 virtual void adjustStackAfterCall(int l, bool isIndirect) {
922 if (isIndirect) {
923 l += 4;
924 }
Jack Palevich21a15a22009-05-11 14:49:29 -0700925 oad(0xc481, l); /* add $xxx, %esp */
926 }
927
Jack Palevicha6535612009-05-13 16:24:17 -0700928 virtual int jumpOffset() {
929 return 5;
930 }
931
932 virtual int disassemble(FILE* out) {
Jack Palevich1cdef202009-05-22 12:06:27 -0700933 return 0;
Jack Palevicha6535612009-05-13 16:24:17 -0700934 }
935
Jack Paleviche7b59062009-05-19 17:12:17 -0700936 /* output a symbol and patch all calls to it */
937 virtual void gsym(int t) {
938 int n;
939 int pc = getPC();
940 while (t) {
941 n = *(int *) t; /* next value */
942 *(int *) t = pc - t - 4;
943 t = n;
944 }
945 }
946
Jack Palevich1cdef202009-05-22 12:06:27 -0700947 virtual int finishCompile() {
948 return 0;
949 }
950
Jack Palevich21a15a22009-05-11 14:49:29 -0700951 private:
Jack Paleviche7b59062009-05-19 17:12:17 -0700952
953 /** Output 1 to 4 bytes.
954 *
955 */
956 void o(int n) {
957 /* cannot use unsigned, so we must do a hack */
958 while (n && n != -1) {
959 ob(n & 0xff);
960 n = n >> 8;
961 }
962 }
963
964 /* psym is used to put an instruction with a data field which is a
965 reference to a symbol. It is in fact the same as oad ! */
966 int psym(int n, int t) {
967 return oad(n, t);
968 }
969
970 /* instruction + address */
971 int oad(int n, int t) {
972 o(n);
973 int result = getPC();
974 o4(t);
975 return result;
976 }
977
978
Jack Palevichbf42c9c2009-05-12 12:48:35 -0700979 static const int operatorHelper[];
980
981 int decodeOp(int op) {
982 if (op < 0 || op > OP_COUNT) {
983 fprintf(stderr, "Out-of-range operator: %d\n", op);
984 exit(1);
985 }
986 return operatorHelper[op];
987 }
Jack Palevich21a15a22009-05-11 14:49:29 -0700988
Jack Palevich546b2242009-05-13 15:10:04 -0700989 void gmov(int l, int t) {
Jack Palevich21a15a22009-05-11 14:49:29 -0700990 o(l + 0x83);
991 oad((t < LOCAL) << 7 | 5, t);
992 }
993 };
994
Jack Paleviche7b59062009-05-19 17:12:17 -0700995#endif // PROVIDE_X86_CODEGEN
996
Jack Palevich1cdef202009-05-22 12:06:27 -0700997 class InputStream {
998 public:
999 virtual int get() = 0;
1000 virtual long tell() = 0;
1001 };
1002
1003 class FileInputStream : public InputStream {
1004 public:
1005 FileInputStream(FILE* in) : f(in) {}
1006 virtual int get() { return fgetc(f); }
1007 virtual long tell() { return ftell(f); }
1008 private:
1009 FILE* f;
1010 };
1011
1012 class TextInputStream : public InputStream {
1013 public:
1014 TextInputStream(const char* text, size_t textLength)
1015 : pText(text), mTextLength(textLength), mPosition(0) {
1016 }
1017 virtual int get() {
1018 return mPosition < mTextLength ? pText[mPosition++] : EOF;
1019 }
1020 virtual long tell() {
1021 return mPosition;
1022 }
1023
1024 private:
1025 const char* pText;
1026 size_t mTextLength;
1027 size_t mPosition;
1028 };
1029
Jack Palevich653f42d2009-05-28 17:15:32 -07001030 int ch; // Current input character, or EOF
1031 intptr_t tok; // token
1032 intptr_t tokc; // token extra info
1033 int tokl; // token operator level
1034 intptr_t rsym; // return symbol
1035 intptr_t loc; // local variable index
1036 char* glo; // global variable index
1037 char* sym_stk;
1038 char* dstk; // Define stack
1039 char* dptr; // Macro state: Points to macro text during macro playback.
1040 int dch; // Macro state: Saves old value of ch during a macro playback.
1041 char* last_id;
Jack Palevich21a15a22009-05-11 14:49:29 -07001042 void* pSymbolBase;
1043 void* pGlobalBase;
Jack Palevich653f42d2009-05-28 17:15:32 -07001044 char* pVarsBase; // Value of variables
Jack Palevich1cdef202009-05-22 12:06:27 -07001045
1046 InputStream* file;
Jack Palevich21a15a22009-05-11 14:49:29 -07001047
1048 CodeBuf codeBuf;
Jack Palevich22305132009-05-13 10:58:45 -07001049 CodeGenerator* pGen;
Jack Palevich21a15a22009-05-11 14:49:29 -07001050
1051 static const int ALLOC_SIZE = 99999;
1052
1053 /* depends on the init string */
1054 static const int TOK_STR_SIZE = 48;
1055 static const int TOK_IDENT = 0x100;
1056 static const int TOK_INT = 0x100;
1057 static const int TOK_IF = 0x120;
1058 static const int TOK_ELSE = 0x138;
1059 static const int TOK_WHILE = 0x160;
1060 static const int TOK_BREAK = 0x190;
1061 static const int TOK_RETURN = 0x1c0;
1062 static const int TOK_FOR = 0x1f8;
1063 static const int TOK_DEFINE = 0x218;
1064 static const int TOK_MAIN = 0x250;
1065
1066 static const int TOK_DUMMY = 1;
1067 static const int TOK_NUM = 2;
1068
1069 static const int LOCAL = 0x200;
1070
1071 static const int SYM_FORWARD = 0;
1072 static const int SYM_DEFINE = 1;
1073
1074 /* tokens in string heap */
1075 static const int TAG_TOK = ' ';
1076 static const int TAG_MACRO = 2;
1077
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001078 static const int OP_INCREMENT = 0;
1079 static const int OP_DECREMENT = 1;
1080 static const int OP_MUL = 2;
1081 static const int OP_DIV = 3;
1082 static const int OP_MOD = 4;
1083 static const int OP_PLUS = 5;
1084 static const int OP_MINUS = 6;
1085 static const int OP_SHIFT_LEFT = 7;
1086 static const int OP_SHIFT_RIGHT = 8;
1087 static const int OP_LESS_EQUAL = 9;
1088 static const int OP_GREATER_EQUAL = 10;
1089 static const int OP_LESS = 11;
1090 static const int OP_GREATER = 12;
1091 static const int OP_EQUALS = 13;
1092 static const int OP_NOT_EQUALS = 14;
1093 static const int OP_LOGICAL_AND = 15;
1094 static const int OP_LOGICAL_OR = 16;
1095 static const int OP_BIT_AND = 17;
1096 static const int OP_BIT_XOR = 18;
1097 static const int OP_BIT_OR = 19;
1098 static const int OP_BIT_NOT = 20;
1099 static const int OP_LOGICAL_NOT = 21;
1100 static const int OP_COUNT = 22;
1101
1102 /* Operators are searched from front, the two-character operators appear
1103 * before the single-character operators with the same first character.
1104 * @ is used to pad out single-character operators.
1105 */
1106 static const char* operatorChars;
1107 static const char operatorLevel[];
1108
Jack Palevich21a15a22009-05-11 14:49:29 -07001109 void pdef(int t) {
Jack Palevich653f42d2009-05-28 17:15:32 -07001110 *dstk++ = t;
Jack Palevich21a15a22009-05-11 14:49:29 -07001111 }
1112
1113 void inp() {
1114 if (dptr) {
Jack Palevich653f42d2009-05-28 17:15:32 -07001115 ch = *dptr++;
Jack Palevich21a15a22009-05-11 14:49:29 -07001116 if (ch == TAG_MACRO) {
1117 dptr = 0;
1118 ch = dch;
1119 }
1120 } else
Jack Palevich1cdef202009-05-22 12:06:27 -07001121 ch = file->get();
Jack Palevich21a15a22009-05-11 14:49:29 -07001122 /* printf("ch=%c 0x%x\n", ch, ch); */
1123 }
1124
1125 int isid() {
Jack Palevich546b2242009-05-13 15:10:04 -07001126 return isalnum(ch) | (ch == '_');
Jack Palevich21a15a22009-05-11 14:49:29 -07001127 }
1128
1129 /* read a character constant */
1130 void getq() {
1131 if (ch == '\\') {
1132 inp();
1133 if (ch == 'n')
1134 ch = '\n';
1135 }
1136 }
1137
1138 void next() {
1139 int l, a;
1140
Jack Palevich546b2242009-05-13 15:10:04 -07001141 while (isspace(ch) | (ch == '#')) {
Jack Palevich21a15a22009-05-11 14:49:29 -07001142 if (ch == '#') {
1143 inp();
1144 next();
1145 if (tok == TOK_DEFINE) {
1146 next();
1147 pdef(TAG_TOK); /* fill last ident tag */
1148 *(int *) tok = SYM_DEFINE;
Jack Palevich653f42d2009-05-28 17:15:32 -07001149 *(char* *) (tok + 4) = dstk; /* define stack */
Jack Palevich21a15a22009-05-11 14:49:29 -07001150 }
1151 /* well we always save the values ! */
1152 while (ch != '\n') {
1153 pdef(ch);
1154 inp();
1155 }
1156 pdef(ch);
1157 pdef(TAG_MACRO);
1158 }
1159 inp();
1160 }
1161 tokl = 0;
1162 tok = ch;
1163 /* encode identifiers & numbers */
1164 if (isid()) {
1165 pdef(TAG_TOK);
1166 last_id = dstk;
1167 while (isid()) {
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001168 pdef(ch);
1169 inp();
Jack Palevichae54f1f2009-05-08 14:54:15 -07001170 }
Jack Palevich21a15a22009-05-11 14:49:29 -07001171 if (isdigit(tok)) {
Jack Palevich653f42d2009-05-28 17:15:32 -07001172 tokc = strtol(last_id, 0, 0);
Jack Palevich21a15a22009-05-11 14:49:29 -07001173 tok = TOK_NUM;
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001174 } else {
Jack Palevich653f42d2009-05-28 17:15:32 -07001175 * dstk = TAG_TOK; /* no need to mark end of string (we
Jack Palevich21a15a22009-05-11 14:49:29 -07001176 suppose data is initialized to zero by calloc) */
Jack Palevich653f42d2009-05-28 17:15:32 -07001177 tok = (intptr_t) (strstr(sym_stk, (last_id - 1))
Jack Palevich21a15a22009-05-11 14:49:29 -07001178 - sym_stk);
Jack Palevich653f42d2009-05-28 17:15:32 -07001179 * dstk = 0; /* mark real end of ident for dlsym() */
Jack Palevich21a15a22009-05-11 14:49:29 -07001180 tok = tok * 8 + TOK_IDENT;
1181 if (tok > TOK_DEFINE) {
Jack Palevich653f42d2009-05-28 17:15:32 -07001182 tok = (intptr_t) (pVarsBase + tok);
Jack Palevich21a15a22009-05-11 14:49:29 -07001183 /* printf("tok=%s %x\n", last_id, tok); */
1184 /* define handling */
1185 if (*(int *) tok == SYM_DEFINE) {
Jack Palevich653f42d2009-05-28 17:15:32 -07001186 dptr = *(char* *) (tok + 4);
Jack Palevich21a15a22009-05-11 14:49:29 -07001187 dch = ch;
1188 inp();
1189 next();
1190 }
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001191 }
1192 }
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001193 } else {
Jack Palevich21a15a22009-05-11 14:49:29 -07001194 inp();
1195 if (tok == '\'') {
1196 tok = TOK_NUM;
1197 getq();
1198 tokc = ch;
1199 inp();
1200 inp();
Jack Palevich546b2242009-05-13 15:10:04 -07001201 } else if ((tok == '/') & (ch == '*')) {
Jack Palevich21a15a22009-05-11 14:49:29 -07001202 inp();
1203 while (ch) {
1204 while (ch != '*')
1205 inp();
1206 inp();
1207 if (ch == '/')
1208 ch = 0;
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001209 }
Jack Palevich21a15a22009-05-11 14:49:29 -07001210 inp();
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001211 next();
Jack Palevichbd894902009-05-14 19:35:31 -07001212 } else if ((tok == '/') & (ch == '/')) {
1213 inp();
1214 while (ch && (ch != '\n')) {
1215 inp();
1216 }
1217 inp();
1218 next();
Jack Palevich21a15a22009-05-11 14:49:29 -07001219 } else {
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001220 const char* t = operatorChars;
1221 int opIndex = 0;
Jack Palevich546b2242009-05-13 15:10:04 -07001222 while ((l = *t++) != 0) {
Jack Palevich21a15a22009-05-11 14:49:29 -07001223 a = *t++;
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001224 tokl = operatorLevel[opIndex];
1225 tokc = opIndex;
Jack Palevich546b2242009-05-13 15:10:04 -07001226 if ((l == tok) & ((a == ch) | (a == '@'))) {
Jack Palevich21a15a22009-05-11 14:49:29 -07001227#if 0
1228 printf("%c%c -> tokl=%d tokc=0x%x\n",
1229 l, a, tokl, tokc);
1230#endif
1231 if (a == ch) {
1232 inp();
1233 tok = TOK_DUMMY; /* dummy token for double tokens */
1234 }
1235 break;
1236 }
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001237 opIndex++;
1238 }
1239 if (l == 0) {
1240 tokl = 0;
1241 tokc = 0;
Jack Palevich21a15a22009-05-11 14:49:29 -07001242 }
1243 }
1244 }
1245#if 0
1246 {
Jack Palevich653f42d2009-05-28 17:15:32 -07001247 char* p;
Jack Palevich21a15a22009-05-11 14:49:29 -07001248
1249 printf("tok=0x%x ", tok);
1250 if (tok >= TOK_IDENT) {
1251 printf("'");
1252 if (tok> TOK_DEFINE)
Jack Palevich653f42d2009-05-28 17:15:32 -07001253 p = sym_stk + 1 + ((char*) tok - pVarsBase - TOK_IDENT) / 8;
Jack Palevich21a15a22009-05-11 14:49:29 -07001254 else
1255 p = sym_stk + 1 + (tok - TOK_IDENT) / 8;
Jack Palevich653f42d2009-05-28 17:15:32 -07001256 while (*p != TAG_TOK && *p)
1257 printf("%c", *p++);
Jack Palevich21a15a22009-05-11 14:49:29 -07001258 printf("'\n");
1259 } else if (tok == TOK_NUM) {
1260 printf("%d\n", tokc);
1261 } else {
1262 printf("'%c'\n", tok);
1263 }
1264 }
1265#endif
1266 }
1267
1268 void error(const char *fmt, ...) {
1269 va_list ap;
1270
1271 va_start(ap, fmt);
Jack Palevich1cdef202009-05-22 12:06:27 -07001272 fprintf(stderr, "%ld: ", file->tell());
Jack Palevich21a15a22009-05-11 14:49:29 -07001273 vfprintf(stderr, fmt, ap);
1274 fprintf(stderr, "\n");
1275 va_end(ap);
1276 exit(1);
1277 }
1278
Jack Palevich8b0624c2009-05-20 12:12:06 -07001279 void skip(intptr_t c) {
Jack Palevich21a15a22009-05-11 14:49:29 -07001280 if (tok != c) {
1281 error("'%c' expected", c);
1282 }
1283 next();
1284 }
1285
Jack Palevich21a15a22009-05-11 14:49:29 -07001286 /* l is one if '=' parsing wanted (quick hack) */
Jack Palevich8b0624c2009-05-20 12:12:06 -07001287 void unary(intptr_t l) {
Jack Palevich653f42d2009-05-28 17:15:32 -07001288 intptr_t n, t, a;
1289 int c;
Jack Palevich546b2242009-05-13 15:10:04 -07001290 t = 0;
Jack Palevich21a15a22009-05-11 14:49:29 -07001291 n = 1; /* type of expression 0 = forward, 1 = value, other =
1292 lvalue */
1293 if (tok == '\"') {
Jack Palevich653f42d2009-05-28 17:15:32 -07001294 pGen->li((int) glo);
Jack Palevich21a15a22009-05-11 14:49:29 -07001295 while (ch != '\"') {
1296 getq();
Jack Palevich653f42d2009-05-28 17:15:32 -07001297 *glo++ = ch;
Jack Palevich21a15a22009-05-11 14:49:29 -07001298 inp();
1299 }
Jack Palevich653f42d2009-05-28 17:15:32 -07001300 *glo = 0;
1301 glo = (char*) (((intptr_t) glo + 4) & -4); /* align heap */
Jack Palevich21a15a22009-05-11 14:49:29 -07001302 inp();
1303 next();
1304 } else {
1305 c = tokl;
1306 a = tokc;
1307 t = tok;
1308 next();
1309 if (t == TOK_NUM) {
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001310 pGen->li(a);
Jack Palevich21a15a22009-05-11 14:49:29 -07001311 } else if (c == 2) {
1312 /* -, +, !, ~ */
1313 unary(0);
Jack Palevich1cdef202009-05-22 12:06:27 -07001314 pGen->clearR1();
Jack Palevich21a15a22009-05-11 14:49:29 -07001315 if (t == '!')
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001316 pGen->gcmp(a);
Jack Palevich21a15a22009-05-11 14:49:29 -07001317 else
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001318 pGen->genOp(a);
Jack Palevich21a15a22009-05-11 14:49:29 -07001319 } else if (t == '(') {
1320 expr();
1321 skip(')');
1322 } else if (t == '*') {
1323 /* parse cast */
1324 skip('(');
1325 t = tok; /* get type */
1326 next(); /* skip int/char/void */
1327 next(); /* skip '*' or '(' */
1328 if (tok == '*') {
1329 /* function type */
1330 skip('*');
1331 skip(')');
1332 skip('(');
1333 skip(')');
1334 t = 0;
1335 }
1336 skip(')');
1337 unary(0);
1338 if (tok == '=') {
1339 next();
Jack Palevich1cdef202009-05-22 12:06:27 -07001340 pGen->pushR0();
Jack Palevich21a15a22009-05-11 14:49:29 -07001341 expr();
Jack Palevich1cdef202009-05-22 12:06:27 -07001342 pGen->popR1();
1343 pGen->storeR0ToR1(t == TOK_INT);
Jack Palevich21a15a22009-05-11 14:49:29 -07001344 } else if (t) {
Jack Palevich1cdef202009-05-22 12:06:27 -07001345 pGen->loadR0FromR0(t == TOK_INT);
Jack Palevich21a15a22009-05-11 14:49:29 -07001346 }
1347 } else if (t == '&') {
Jack Palevich1cdef202009-05-22 12:06:27 -07001348 pGen->leaR0(*(int *) tok);
Jack Palevich21a15a22009-05-11 14:49:29 -07001349 next();
1350 } else {
1351 n = *(int *) t;
1352 /* forward reference: try dlsym */
Jack Palevichcb1c9ef2009-05-14 11:38:49 -07001353 if (!n) {
Jack Palevich653f42d2009-05-28 17:15:32 -07001354 n = (intptr_t) dlsym(RTLD_DEFAULT, last_id);
Jack Palevichcb1c9ef2009-05-14 11:38:49 -07001355 }
Jack Palevich546b2242009-05-13 15:10:04 -07001356 if ((tok == '=') & l) {
Jack Palevich21a15a22009-05-11 14:49:29 -07001357 /* assignment */
1358 next();
1359 expr();
Jack Palevich1cdef202009-05-22 12:06:27 -07001360 pGen->storeR0(n);
Jack Palevich21a15a22009-05-11 14:49:29 -07001361 } else if (tok != '(') {
1362 /* variable */
Jack Palevich1cdef202009-05-22 12:06:27 -07001363 pGen->loadR0(n, tokl == 11, tokc);
Jack Palevich21a15a22009-05-11 14:49:29 -07001364 if (tokl == 11) {
Jack Palevich21a15a22009-05-11 14:49:29 -07001365 next();
1366 }
1367 }
1368 }
1369 }
1370
1371 /* function call */
1372 if (tok == '(') {
1373 if (n == 1)
Jack Palevich1cdef202009-05-22 12:06:27 -07001374 pGen->pushR0();
Jack Palevich21a15a22009-05-11 14:49:29 -07001375
1376 /* push args and invert order */
Jack Palevichcb1c9ef2009-05-14 11:38:49 -07001377 a = pGen->beginFunctionCallArguments();
Jack Palevich21a15a22009-05-11 14:49:29 -07001378 next();
1379 l = 0;
1380 while (tok != ')') {
1381 expr();
Jack Palevich1cdef202009-05-22 12:06:27 -07001382 pGen->storeR0ToArg(l);
Jack Palevichbbf8ab52009-05-11 11:54:30 -07001383 if (tok == ',')
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001384 next();
Jack Palevich21a15a22009-05-11 14:49:29 -07001385 l = l + 4;
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001386 }
Jack Palevichcb1c9ef2009-05-14 11:38:49 -07001387 pGen->endFunctionCallArguments(a, l);
Jack Palevich21a15a22009-05-11 14:49:29 -07001388 next();
1389 if (!n) {
1390 /* forward reference */
1391 t = t + 4;
1392 *(int *) t = pGen->callForward(*(int *) t);
1393 } else if (n == 1) {
1394 pGen->callIndirect(l);
Jack Palevich21a15a22009-05-11 14:49:29 -07001395 } else {
Jack Palevich7810bc92009-05-15 14:31:47 -07001396 pGen->callRelative(n - codeBuf.getPC() - pGen->jumpOffset());
Jack Palevich21a15a22009-05-11 14:49:29 -07001397 }
Jack Palevich3d474a72009-05-15 15:12:38 -07001398 if (l | (n == 1))
Jack Palevich7810bc92009-05-15 14:31:47 -07001399 pGen->adjustStackAfterCall(l, n == 1);
Jack Palevich21a15a22009-05-11 14:49:29 -07001400 }
1401 }
1402
Jack Palevich653f42d2009-05-28 17:15:32 -07001403 void sum(int l) {
Jack Palevich8b0624c2009-05-20 12:12:06 -07001404 intptr_t t, n, a;
Jack Palevich546b2242009-05-13 15:10:04 -07001405 t = 0;
Jack Palevich21a15a22009-05-11 14:49:29 -07001406 if (l-- == 1)
1407 unary(1);
1408 else {
1409 sum(l);
1410 a = 0;
1411 while (l == tokl) {
1412 n = tok;
1413 t = tokc;
1414 next();
1415
1416 if (l > 8) {
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001417 a = pGen->gtst(t == OP_LOGICAL_OR, a); /* && and || output code generation */
Jack Palevich21a15a22009-05-11 14:49:29 -07001418 sum(l);
1419 } else {
Jack Palevich1cdef202009-05-22 12:06:27 -07001420 pGen->pushR0();
Jack Palevich21a15a22009-05-11 14:49:29 -07001421 sum(l);
Jack Palevich1cdef202009-05-22 12:06:27 -07001422 pGen->popR1();
Jack Palevich21a15a22009-05-11 14:49:29 -07001423
Jack Palevich546b2242009-05-13 15:10:04 -07001424 if ((l == 4) | (l == 5)) {
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001425 pGen->gcmp(t);
Jack Palevich21a15a22009-05-11 14:49:29 -07001426 } else {
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001427 pGen->genOp(t);
Jack Palevich21a15a22009-05-11 14:49:29 -07001428 }
1429 }
1430 }
1431 /* && and || output code generation */
1432 if (a && l > 8) {
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001433 a = pGen->gtst(t == OP_LOGICAL_OR, a);
1434 pGen->li(t != OP_LOGICAL_OR);
Jack Palevicha6535612009-05-13 16:24:17 -07001435 pGen->gjmp(5); /* jmp $ + 5 (sizeof li, FIXME for ARM) */
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001436 pGen->gsym(a);
1437 pGen->li(t == OP_LOGICAL_OR);
Jack Palevich21a15a22009-05-11 14:49:29 -07001438 }
1439 }
1440 }
1441
1442 void expr() {
1443 sum(11);
1444 }
1445
1446 int test_expr() {
1447 expr();
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001448 return pGen->gtst(0, 0);
Jack Palevich21a15a22009-05-11 14:49:29 -07001449 }
1450
Jack Palevich8b0624c2009-05-20 12:12:06 -07001451 void block(intptr_t l) {
1452 intptr_t a, n, t;
Jack Palevich21a15a22009-05-11 14:49:29 -07001453
1454 if (tok == TOK_IF) {
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001455 next();
1456 skip('(');
Jack Palevich21a15a22009-05-11 14:49:29 -07001457 a = test_expr();
1458 skip(')');
1459 block(l);
1460 if (tok == TOK_ELSE) {
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001461 next();
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001462 n = pGen->gjmp(0); /* jmp */
1463 pGen->gsym(a);
Jack Palevich21a15a22009-05-11 14:49:29 -07001464 block(l);
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001465 pGen->gsym(n); /* patch else jmp */
Jack Palevich21a15a22009-05-11 14:49:29 -07001466 } else {
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001467 pGen->gsym(a); /* patch if test */
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001468 }
Jack Palevich546b2242009-05-13 15:10:04 -07001469 } else if ((tok == TOK_WHILE) | (tok == TOK_FOR)) {
Jack Palevich21a15a22009-05-11 14:49:29 -07001470 t = tok;
1471 next();
1472 skip('(');
1473 if (t == TOK_WHILE) {
Jack Palevicha6535612009-05-13 16:24:17 -07001474 n = codeBuf.getPC(); // top of loop, target of "next" iteration
Jack Palevich21a15a22009-05-11 14:49:29 -07001475 a = test_expr();
1476 } else {
1477 if (tok != ';')
1478 expr();
1479 skip(';');
1480 n = codeBuf.getPC();
1481 a = 0;
1482 if (tok != ';')
1483 a = test_expr();
1484 skip(';');
1485 if (tok != ')') {
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001486 t = pGen->gjmp(0);
Jack Palevich21a15a22009-05-11 14:49:29 -07001487 expr();
Jack Palevicha6535612009-05-13 16:24:17 -07001488 pGen->gjmp(n - codeBuf.getPC() - pGen->jumpOffset());
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001489 pGen->gsym(t);
Jack Palevich21a15a22009-05-11 14:49:29 -07001490 n = t + 4;
1491 }
1492 }
1493 skip(')');
Jack Palevich8b0624c2009-05-20 12:12:06 -07001494 block((intptr_t) &a);
Jack Palevicha6535612009-05-13 16:24:17 -07001495 pGen->gjmp(n - codeBuf.getPC() - pGen->jumpOffset()); /* jmp */
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001496 pGen->gsym(a);
Jack Palevich21a15a22009-05-11 14:49:29 -07001497 } else if (tok == '{') {
1498 next();
1499 /* declarations */
1500 decl(1);
1501 while (tok != '}')
1502 block(l);
1503 next();
1504 } else {
1505 if (tok == TOK_RETURN) {
1506 next();
1507 if (tok != ';')
1508 expr();
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001509 rsym = pGen->gjmp(rsym); /* jmp */
Jack Palevich21a15a22009-05-11 14:49:29 -07001510 } else if (tok == TOK_BREAK) {
1511 next();
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001512 *(int *) l = pGen->gjmp(*(int *) l);
Jack Palevich21a15a22009-05-11 14:49:29 -07001513 } else if (tok != ';')
1514 expr();
1515 skip(';');
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001516 }
1517 }
Jack Palevich21a15a22009-05-11 14:49:29 -07001518
1519 /* 'l' is true if local declarations */
Jack Palevich8b0624c2009-05-20 12:12:06 -07001520 void decl(bool l) {
1521 intptr_t a;
Jack Palevich21a15a22009-05-11 14:49:29 -07001522
Jack Palevich653f42d2009-05-28 17:15:32 -07001523 while ((tok == TOK_INT) | ((tok != EOF) & (!l))) {
Jack Palevich21a15a22009-05-11 14:49:29 -07001524 if (tok == TOK_INT) {
1525 next();
1526 while (tok != ';') {
1527 if (l) {
1528 loc = loc + 4;
1529 *(int *) tok = -loc;
1530 } else {
Jack Palevich653f42d2009-05-28 17:15:32 -07001531 *(int* *) tok = (int*) glo;
Jack Palevich21a15a22009-05-11 14:49:29 -07001532 glo = glo + 4;
1533 }
1534 next();
1535 if (tok == ',')
1536 next();
1537 }
1538 skip(';');
1539 } else {
1540 /* patch forward references (XXX: do not work for function
1541 pointers) */
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001542 pGen->gsym(*(int *) (tok + 4));
Jack Palevich21a15a22009-05-11 14:49:29 -07001543 /* put function address */
1544 *(int *) tok = codeBuf.getPC();
1545 next();
1546 skip('(');
1547 a = 8;
Jack Palevich546b2242009-05-13 15:10:04 -07001548 int argCount = 0;
Jack Palevich21a15a22009-05-11 14:49:29 -07001549 while (tok != ')') {
1550 /* read param name and compute offset */
1551 *(int *) tok = a;
1552 a = a + 4;
1553 next();
1554 if (tok == ',')
1555 next();
Jack Palevich546b2242009-05-13 15:10:04 -07001556 argCount++;
Jack Palevich21a15a22009-05-11 14:49:29 -07001557 }
1558 next(); /* skip ')' */
1559 rsym = loc = 0;
Jack Palevich546b2242009-05-13 15:10:04 -07001560 a = pGen->functionEntry(argCount);
Jack Palevich21a15a22009-05-11 14:49:29 -07001561 block(0);
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001562 pGen->gsym(rsym);
Jack Palevich546b2242009-05-13 15:10:04 -07001563 pGen->functionExit(argCount, a, loc);
Jack Palevich21a15a22009-05-11 14:49:29 -07001564 }
1565 }
1566 }
1567
1568 void cleanup() {
1569 if (sym_stk != 0) {
Jack Palevich653f42d2009-05-28 17:15:32 -07001570 free(sym_stk);
Jack Palevich21a15a22009-05-11 14:49:29 -07001571 sym_stk = 0;
1572 }
1573 if (pGlobalBase != 0) {
1574 free((void*) pGlobalBase);
1575 pGlobalBase = 0;
1576 }
1577 if (pVarsBase != 0) {
1578 free(pVarsBase);
1579 pVarsBase = 0;
1580 }
1581 if (pGen) {
1582 delete pGen;
1583 pGen = 0;
1584 }
Jack Palevich1cdef202009-05-22 12:06:27 -07001585 if (file) {
1586 delete file;
1587 file = 0;
1588 }
Jack Palevich21a15a22009-05-11 14:49:29 -07001589 }
1590
1591 void clear() {
1592 tok = 0;
1593 tokc = 0;
1594 tokl = 0;
1595 ch = 0;
Jack Palevich653f42d2009-05-28 17:15:32 -07001596 pVarsBase = 0;
Jack Palevich21a15a22009-05-11 14:49:29 -07001597 rsym = 0;
1598 loc = 0;
1599 glo = 0;
1600 sym_stk = 0;
1601 dstk = 0;
1602 dptr = 0;
1603 dch = 0;
1604 last_id = 0;
1605 file = 0;
1606 pGlobalBase = 0;
1607 pVarsBase = 0;
1608 pGen = 0;
1609 }
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001610
Jack Palevich22305132009-05-13 10:58:45 -07001611 void setArchitecture(const char* architecture) {
1612 delete pGen;
1613 pGen = 0;
1614
1615 if (architecture != NULL) {
Jack Paleviche7b59062009-05-19 17:12:17 -07001616#ifdef PROVIDE_ARM_CODEGEN
Jack Palevich8b0624c2009-05-20 12:12:06 -07001617 if (! pGen && strcmp(architecture, "arm") == 0) {
Jack Palevich22305132009-05-13 10:58:45 -07001618 pGen = new ARMCodeGenerator();
Jack Palevich8b0624c2009-05-20 12:12:06 -07001619 }
Jack Paleviche7b59062009-05-19 17:12:17 -07001620#endif
Jack Paleviche7b59062009-05-19 17:12:17 -07001621#ifdef PROVIDE_X86_CODEGEN
Jack Palevich8b0624c2009-05-20 12:12:06 -07001622 if (! pGen && strcmp(architecture, "x86") == 0) {
Jack Palevich22305132009-05-13 10:58:45 -07001623 pGen = new X86CodeGenerator();
Jack Palevich8b0624c2009-05-20 12:12:06 -07001624 }
Jack Paleviche7b59062009-05-19 17:12:17 -07001625#endif
Jack Palevich8b0624c2009-05-20 12:12:06 -07001626 if (!pGen ) {
1627 fprintf(stderr, "Unknown architecture %s\n", architecture);
Jack Palevich22305132009-05-13 10:58:45 -07001628 }
1629 }
1630
1631 if (pGen == NULL) {
Jack Paleviche7b59062009-05-19 17:12:17 -07001632#if defined(DEFAULT_ARM_CODEGEN)
Jack Palevich22305132009-05-13 10:58:45 -07001633 pGen = new ARMCodeGenerator();
Jack Paleviche7b59062009-05-19 17:12:17 -07001634#elif defined(DEFAULT_X86_CODEGEN)
1635 pGen = new X86CodeGenerator();
1636#endif
1637 }
1638 if (pGen == NULL) {
Jack Palevich1cdef202009-05-22 12:06:27 -07001639 fprintf(stderr, "No code generator defined.\n");
Jack Palevich22305132009-05-13 10:58:45 -07001640 }
1641 }
1642
Jack Palevich77ae76e2009-05-10 19:59:24 -07001643public:
Jack Palevich22305132009-05-13 10:58:45 -07001644 struct args {
1645 args() {
1646 architecture = 0;
1647 }
1648 const char* architecture;
1649 };
1650
Jack Paleviche7b59062009-05-19 17:12:17 -07001651 Compiler() {
Jack Palevich21a15a22009-05-11 14:49:29 -07001652 clear();
Jack Paleviche27bf3e2009-05-10 14:09:03 -07001653 }
Jack Palevichbbf8ab52009-05-11 11:54:30 -07001654
Jack Paleviche7b59062009-05-19 17:12:17 -07001655 ~Compiler() {
Jack Palevich21a15a22009-05-11 14:49:29 -07001656 cleanup();
1657 }
1658
Jack Palevich1cdef202009-05-22 12:06:27 -07001659 int compile(const char* text, size_t textLength) {
Jack Palevich21a15a22009-05-11 14:49:29 -07001660 cleanup();
1661 clear();
1662 codeBuf.init(ALLOC_SIZE);
Jack Palevich1cdef202009-05-22 12:06:27 -07001663 setArchitecture(NULL);
Jack Palevich8b0624c2009-05-20 12:12:06 -07001664 if (!pGen) {
1665 return -1;
1666 }
Jack Palevich21a15a22009-05-11 14:49:29 -07001667 pGen->init(&codeBuf);
Jack Palevich1cdef202009-05-22 12:06:27 -07001668 file = new TextInputStream(text, textLength);
Jack Palevich653f42d2009-05-28 17:15:32 -07001669 sym_stk = (char*) calloc(1, ALLOC_SIZE);
1670 dstk = strcpy(sym_stk,
Jack Palevich21a15a22009-05-11 14:49:29 -07001671 " int if else while break return for define main ")
1672 + TOK_STR_SIZE;
1673 pGlobalBase = calloc(1, ALLOC_SIZE);
Jack Palevich653f42d2009-05-28 17:15:32 -07001674 glo = (char*) pGlobalBase;
1675 pVarsBase = (char*) calloc(1, ALLOC_SIZE);
Jack Palevich21a15a22009-05-11 14:49:29 -07001676 inp();
1677 next();
1678 decl(0);
Jack Palevich546b2242009-05-13 15:10:04 -07001679 pGen->finishCompile();
Jack Palevich21a15a22009-05-11 14:49:29 -07001680 return 0;
1681 }
1682
1683 int run(int argc, char** argv) {
1684 typedef int (*mainPtr)(int argc, char** argv);
Jack Palevich653f42d2009-05-28 17:15:32 -07001685 mainPtr aMain = (mainPtr) *(int*) (pVarsBase + TOK_MAIN);
Jack Palevich21a15a22009-05-11 14:49:29 -07001686 if (!aMain) {
1687 fprintf(stderr, "Could not find function \"main\".\n");
1688 return -1;
1689 }
1690 return aMain(argc, argv);
1691 }
1692
1693 int dump(FILE* out) {
1694 fwrite(codeBuf.getBase(), 1, codeBuf.getSize(), out);
1695 return 0;
1696 }
Jack Palevich77ae76e2009-05-10 19:59:24 -07001697
Jack Palevicha6535612009-05-13 16:24:17 -07001698 int disassemble(FILE* out) {
1699 return pGen->disassemble(out);
1700 }
1701
Jack Palevich1cdef202009-05-22 12:06:27 -07001702 /* Look through the symbol table to find a symbol.
1703 * If found, return its value.
1704 */
1705 void* lookup(const char* name) {
1706 if (!sym_stk) {
1707 return NULL;
1708 }
1709 size_t nameLen = strlen(name);
Jack Palevich653f42d2009-05-28 17:15:32 -07001710 char* pSym = sym_stk;
Jack Palevich1cdef202009-05-22 12:06:27 -07001711 char c;
1712 for(;;) {
1713 c = *pSym++;
1714 if (c == 0) {
1715 break;
1716 }
1717 if (c == TAG_TOK) {
1718 if (memcmp(pSym, name, nameLen) == 0
1719 && pSym[nameLen] == TAG_TOK) {
Jack Palevich653f42d2009-05-28 17:15:32 -07001720 int tok = pSym - 1 - sym_stk;
Jack Palevich1cdef202009-05-22 12:06:27 -07001721 tok = tok * 8 + TOK_IDENT;
1722 if (tok <= TOK_DEFINE) {
1723 return 0;
1724 } else {
Jack Palevich653f42d2009-05-28 17:15:32 -07001725 tok = (intptr_t) (pVarsBase + tok);
Jack Palevich1cdef202009-05-22 12:06:27 -07001726 return * (void**) tok;
1727 }
1728 }
1729 }
1730 }
1731 return NULL;
1732 }
1733
Jack Palevich77ae76e2009-05-10 19:59:24 -07001734};
1735
Jack Paleviche7b59062009-05-19 17:12:17 -07001736const char* Compiler::operatorChars =
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001737 "++--*@/@%@+@-@<<>><=>=<@>@==!=&&||&@^@|@~@!@";
1738
Jack Paleviche7b59062009-05-19 17:12:17 -07001739const char Compiler::operatorLevel[] =
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001740 {11, 11, 1, 1, 1, 2, 2, 3, 3, 4, 4, 4, 4,
1741 5, 5, /* ==, != */
1742 9, 10, /* &&, || */
1743 6, 7, 8, /* & ^ | */
1744 2, 2 /* ~ ! */
1745 };
1746
Jack Palevich8b0624c2009-05-20 12:12:06 -07001747#ifdef PROVIDE_ARM_CODEGEN
Jack Paleviche7b59062009-05-19 17:12:17 -07001748FILE* Compiler::ARMCodeGenerator::disasmOut;
Jack Palevich8b0624c2009-05-20 12:12:06 -07001749#endif
Jack Palevicha6535612009-05-13 16:24:17 -07001750
Jack Palevich8b0624c2009-05-20 12:12:06 -07001751#ifdef PROVIDE_X86_CODEGEN
Jack Paleviche7b59062009-05-19 17:12:17 -07001752const int Compiler::X86CodeGenerator::operatorHelper[] = {
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001753 0x1, // ++
1754 0xff, // --
1755 0xc1af0f, // *
1756 0xf9f79991, // /
1757 0xf9f79991, // % (With manual assist to swap results)
1758 0xc801, // +
1759 0xd8f7c829, // -
1760 0xe0d391, // <<
1761 0xf8d391, // >>
1762 0xe, // <=
1763 0xd, // >=
1764 0xc, // <
1765 0xf, // >
1766 0x4, // ==
1767 0x5, // !=
1768 0x0, // &&
1769 0x1, // ||
1770 0xc821, // &
1771 0xc831, // ^
1772 0xc809, // |
1773 0xd0f7, // ~
1774 0x4 // !
1775};
Jack Palevich8b0624c2009-05-20 12:12:06 -07001776#endif
Jack Palevichbf42c9c2009-05-12 12:48:35 -07001777
Jack Palevich1cdef202009-05-22 12:06:27 -07001778struct ACCscript {
1779 ACCscript() {
1780 text = 0;
1781 textLength = 0;
1782 accError = ACC_NO_ERROR;
1783 }
Jack Palevichbbf8ab52009-05-11 11:54:30 -07001784
Jack Palevich1cdef202009-05-22 12:06:27 -07001785 ~ACCscript() {
1786 delete text;
1787 }
Jack Palevich546b2242009-05-13 15:10:04 -07001788
Jack Palevich1cdef202009-05-22 12:06:27 -07001789 void setError(ACCenum error) {
1790 if (accError == ACC_NO_ERROR && error != ACC_NO_ERROR) {
1791 accError = error;
Jack Palevichbbf8ab52009-05-11 11:54:30 -07001792 }
1793 }
1794
Jack Palevich1cdef202009-05-22 12:06:27 -07001795 ACCenum getError() {
1796 ACCenum result = accError;
1797 accError = ACC_NO_ERROR;
Jack Palevich22305132009-05-13 10:58:45 -07001798 return result;
Jack Palevichbbf8ab52009-05-11 11:54:30 -07001799 }
1800
Jack Palevich1cdef202009-05-22 12:06:27 -07001801 Compiler compiler;
1802 char* text;
1803 int textLength;
1804 ACCenum accError;
1805};
1806
1807
1808extern "C"
1809ACCscript* accCreateScript() {
1810 return new ACCscript();
Jack Palevichbbf8ab52009-05-11 11:54:30 -07001811}
Jack Palevich1cdef202009-05-22 12:06:27 -07001812
1813extern "C"
1814ACCenum accGetError( ACCscript* script ) {
1815 return script->getError();
1816}
1817
1818extern "C"
1819void accDeleteScript(ACCscript* script) {
1820 delete script;
1821}
1822
1823extern "C"
1824void accScriptSource(ACCscript* script,
1825 ACCsizei count,
1826 const ACCchar ** string,
1827 const ACCint * length) {
1828 int totalLength = 0;
1829 for(int i = 0; i < count; i++) {
1830 int len = -1;
1831 const ACCchar* s = string[i];
1832 if (length) {
1833 len = length[i];
1834 }
1835 if (len < 0) {
1836 len = strlen(s);
1837 }
1838 totalLength += len;
1839 }
1840 delete script->text;
1841 char* text = new char[totalLength + 1];
1842 script->text = text;
1843 script->textLength = totalLength;
Jack Palevich09555c72009-05-27 12:25:55 -07001844 char* dest = text;
Jack Palevich1cdef202009-05-22 12:06:27 -07001845 for(int i = 0; i < count; i++) {
1846 int len = -1;
1847 const ACCchar* s = string[i];
1848 if (length) {
1849 len = length[i];
1850 }
1851 if (len < 0) {
1852 len = strlen(s);
1853 }
Jack Palevich09555c72009-05-27 12:25:55 -07001854 memcpy(dest, s, len);
1855 dest += len;
Jack Palevich1cdef202009-05-22 12:06:27 -07001856 }
1857 text[totalLength] = '\0';
1858}
1859
1860extern "C"
1861void accCompileScript(ACCscript* script) {
1862 int result = script->compiler.compile(script->text, script->textLength);
1863 if (result) {
1864 script->setError(ACC_INVALID_OPERATION);
1865 }
1866}
1867
1868extern "C"
1869void accGetScriptiv(ACCscript* script,
1870 ACCenum pname,
1871 ACCint * params) {
1872 switch (pname) {
1873 case ACC_INFO_LOG_LENGTH:
1874 *params = 0;
1875 break;
1876 }
1877}
1878
1879extern "C"
1880void accGetScriptInfoLog(ACCscript* script,
1881 ACCsizei maxLength,
1882 ACCsizei * length,
1883 ACCchar * infoLog) {
1884 if (length) {
1885 *length = 0;
1886 }
1887 if (maxLength > 0 && infoLog) {
1888 *infoLog = 0;
1889 }
1890}
1891
1892extern "C"
1893void accGetScriptLabel(ACCscript* script, const ACCchar * name,
1894 ACCvoid ** address) {
1895 void* value = script->compiler.lookup(name);
1896 if (value) {
1897 *address = value;
1898 } else {
1899 script->setError(ACC_INVALID_VALUE);
1900 }
1901}
1902
1903} // namespace acc
1904