2009-05-08 23:54:15 +02:00
|
|
|
/*
|
2009-05-11 23:49:29 +02:00
|
|
|
Obfuscated Tiny C Compiler
|
2009-05-08 23:54:15 +02:00
|
|
|
|
2009-05-11 23:49:29 +02:00
|
|
|
Copyright (C) 2001-2003 Fabrice Bellard
|
2009-05-10 23:09:03 +02:00
|
|
|
|
2009-05-11 23:49:29 +02:00
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
|
|
arising from the use of this software.
|
2009-05-10 23:09:03 +02:00
|
|
|
|
2009-05-11 23:49:29 +02:00
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it
|
|
|
|
freely, subject to the following restrictions:
|
2009-05-10 23:09:03 +02:00
|
|
|
|
2009-05-11 23:49:29 +02:00
|
|
|
1. The origin of this software must not be misrepresented; you must not
|
|
|
|
claim that you wrote the original software. If you use this software
|
|
|
|
in a product, an acknowledgment in the product and its documentation
|
|
|
|
*is* required.
|
|
|
|
2. Altered source versions must be plainly marked as such, and must not be
|
|
|
|
misrepresented as being the original software.
|
|
|
|
3. This notice may not be removed or altered from any source distribution.
|
|
|
|
*/
|
2009-05-10 23:09:03 +02:00
|
|
|
|
2009-05-11 04:59:24 +02:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <dlfcn.h>
|
2009-05-10 23:09:03 +02:00
|
|
|
#include <stdarg.h>
|
2009-05-08 22:57:37 +02:00
|
|
|
#include <stdio.h>
|
2009-05-11 04:16:42 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2009-05-10 23:09:03 +02:00
|
|
|
|
2009-05-14 00:10:04 +02:00
|
|
|
#if defined(__arm__)
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2009-05-14 01:24:17 +02:00
|
|
|
#include "disassem.h"
|
|
|
|
|
2009-05-11 20:54:30 +02:00
|
|
|
namespace acc {
|
|
|
|
|
2009-05-11 04:59:24 +02:00
|
|
|
class compiler {
|
2009-05-11 23:49:29 +02:00
|
|
|
class CodeBuf {
|
|
|
|
char* ind;
|
|
|
|
char* pProgramBase;
|
2009-05-08 23:54:15 +02:00
|
|
|
|
2009-05-11 23:49:29 +02:00
|
|
|
void release() {
|
|
|
|
if (pProgramBase != 0) {
|
|
|
|
free(pProgramBase);
|
|
|
|
pProgramBase = 0;
|
|
|
|
}
|
|
|
|
}
|
2009-05-08 23:54:15 +02:00
|
|
|
|
2009-05-11 23:49:29 +02:00
|
|
|
public:
|
|
|
|
CodeBuf() {
|
|
|
|
pProgramBase = 0;
|
|
|
|
ind = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
~CodeBuf() {
|
|
|
|
release();
|
|
|
|
}
|
2009-05-08 23:54:15 +02:00
|
|
|
|
2009-05-11 23:49:29 +02:00
|
|
|
void init(int size) {
|
|
|
|
release();
|
|
|
|
pProgramBase = (char*) calloc(1, size);
|
|
|
|
ind = pProgramBase;
|
|
|
|
}
|
2009-05-10 23:09:03 +02:00
|
|
|
|
2009-05-11 23:49:29 +02:00
|
|
|
void o(int n) {
|
|
|
|
/* cannot use unsigned, so we must do a hack */
|
|
|
|
while (n && n != -1) {
|
|
|
|
*ind++ = n;
|
|
|
|
n = n >> 8;
|
2009-05-08 23:54:15 +02:00
|
|
|
}
|
2009-05-11 23:49:29 +02:00
|
|
|
}
|
|
|
|
|
2009-05-14 00:10:04 +02:00
|
|
|
int o4(int n) {
|
|
|
|
int result = (int) ind;
|
|
|
|
* (int*) ind = n;
|
|
|
|
ind += 4;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-05-11 23:49:29 +02:00
|
|
|
/*
|
|
|
|
* Output a byte. Handles all values, 0..ff.
|
|
|
|
*/
|
|
|
|
void ob(int n) {
|
|
|
|
*ind++ = n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* output a symbol and patch all calls to it */
|
|
|
|
void gsym(int t) {
|
|
|
|
int n;
|
|
|
|
while (t) {
|
|
|
|
n = *(int *) t; /* next value */
|
|
|
|
*(int *) t = ((int) ind) - t - 4;
|
|
|
|
t = n;
|
2009-05-08 23:54:15 +02:00
|
|
|
}
|
|
|
|
}
|
2009-05-11 23:49:29 +02:00
|
|
|
|
|
|
|
/* psym is used to put an instruction with a data field which is a
|
|
|
|
reference to a symbol. It is in fact the same as oad ! */
|
|
|
|
int psym(int n, int t) {
|
|
|
|
return oad(n, t);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* instruction + address */
|
|
|
|
int oad(int n, int t) {
|
|
|
|
o(n);
|
|
|
|
*(int *) ind = t;
|
|
|
|
t = (int) ind;
|
|
|
|
ind = ind + 4;
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void* getBase() {
|
|
|
|
return (void*) pProgramBase;
|
|
|
|
}
|
|
|
|
|
|
|
|
int getSize() {
|
|
|
|
return ind - pProgramBase;
|
|
|
|
}
|
|
|
|
|
|
|
|
int getPC() {
|
|
|
|
return (int) ind;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class CodeGenerator {
|
|
|
|
public:
|
|
|
|
CodeGenerator() {}
|
|
|
|
virtual ~CodeGenerator() {}
|
|
|
|
|
2009-05-13 19:58:45 +02:00
|
|
|
virtual void init(CodeBuf* pCodeBuf) {
|
2009-05-11 23:49:29 +02:00
|
|
|
this->pCodeBuf = pCodeBuf;
|
|
|
|
}
|
|
|
|
|
2009-05-13 19:58:45 +02:00
|
|
|
/* returns address to patch with local variable size
|
|
|
|
*/
|
2009-05-14 00:10:04 +02:00
|
|
|
virtual int functionEntry(int argCount) = 0;
|
2009-05-13 19:58:45 +02:00
|
|
|
|
2009-05-14 00:10:04 +02:00
|
|
|
virtual void functionExit(int argCount, int localVariableAddress, int localVariableSize) = 0;
|
2009-05-13 19:58:45 +02:00
|
|
|
|
|
|
|
/* load immediate value */
|
2009-05-14 00:10:04 +02:00
|
|
|
virtual void li(int t) = 0;
|
2009-05-13 19:58:45 +02:00
|
|
|
|
|
|
|
virtual int gjmp(int t) = 0;
|
|
|
|
|
|
|
|
/* l = 0: je, l == 1: jne */
|
|
|
|
virtual int gtst(bool l, int t) = 0;
|
|
|
|
|
|
|
|
virtual void gcmp(int op) = 0;
|
|
|
|
|
2009-05-14 00:10:04 +02:00
|
|
|
virtual void genOp(int op) = 0;
|
2009-05-13 19:58:45 +02:00
|
|
|
|
|
|
|
virtual void clearECX() = 0;
|
|
|
|
|
|
|
|
virtual void pushEAX() = 0;
|
|
|
|
|
|
|
|
virtual void popECX() = 0;
|
|
|
|
|
|
|
|
virtual void storeEAXToAddressECX(bool isInt) = 0;
|
|
|
|
|
|
|
|
virtual void loadEAXIndirect(bool isInt) = 0;
|
|
|
|
|
|
|
|
virtual void leaEAX(int ea) = 0;
|
|
|
|
|
|
|
|
virtual void storeEAX(int ea) = 0;
|
|
|
|
|
|
|
|
virtual void loadEAX(int ea) = 0;
|
|
|
|
|
|
|
|
virtual void postIncrementOrDecrement(int n, int op) = 0;
|
|
|
|
|
2009-05-14 20:38:49 +02:00
|
|
|
virtual int beginFunctionCallArguments() = 0;
|
|
|
|
|
|
|
|
virtual void endFunctionCallArguments(int a, int l) = 0;
|
2009-05-13 19:58:45 +02:00
|
|
|
|
|
|
|
virtual void storeEAToArg(int l) = 0;
|
|
|
|
|
|
|
|
virtual int callForward(int symbol) = 0;
|
|
|
|
|
|
|
|
virtual void callRelative(int t) = 0;
|
|
|
|
|
|
|
|
virtual void callIndirect(int l) = 0;
|
|
|
|
|
|
|
|
virtual void adjustStackAfterCall(int l) = 0;
|
|
|
|
|
2009-05-14 01:24:17 +02:00
|
|
|
virtual int disassemble(FILE* out) = 0;
|
|
|
|
|
2009-05-11 23:49:29 +02:00
|
|
|
/* output a symbol and patch all calls to it */
|
2009-05-13 19:58:45 +02:00
|
|
|
virtual void gsym(int t) {
|
2009-05-11 23:49:29 +02:00
|
|
|
pCodeBuf->gsym(t);
|
|
|
|
}
|
|
|
|
|
2009-05-14 00:10:04 +02:00
|
|
|
virtual int finishCompile() {
|
|
|
|
#if defined(__arm__)
|
2009-05-14 01:24:17 +02:00
|
|
|
const long base = long(pCodeBuf->getBase());
|
|
|
|
const long curr = base + long(pCodeBuf->getSize());
|
|
|
|
int err = cacheflush(base, curr, 0);
|
|
|
|
return err;
|
2009-05-14 00:10:04 +02:00
|
|
|
#else
|
2009-05-14 01:24:17 +02:00
|
|
|
return 0;
|
2009-05-14 00:10:04 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-05-14 01:24:17 +02:00
|
|
|
/**
|
|
|
|
* Adjust relative branches by this amount.
|
|
|
|
*/
|
|
|
|
virtual int jumpOffset() = 0;
|
|
|
|
|
2009-05-11 23:49:29 +02:00
|
|
|
protected:
|
|
|
|
void o(int n) {
|
|
|
|
pCodeBuf->o(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Output a byte. Handles all values, 0..ff.
|
|
|
|
*/
|
|
|
|
void ob(int n) {
|
|
|
|
pCodeBuf->ob(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* psym is used to put an instruction with a data field which is a
|
|
|
|
reference to a symbol. It is in fact the same as oad ! */
|
|
|
|
int psym(int n, int t) {
|
|
|
|
return oad(n, t);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* instruction + address */
|
|
|
|
int oad(int n, int t) {
|
|
|
|
return pCodeBuf->oad(n,t);
|
|
|
|
}
|
|
|
|
|
2009-05-14 01:24:17 +02:00
|
|
|
int getBase() {
|
|
|
|
return (int) pCodeBuf->getBase();
|
|
|
|
}
|
|
|
|
|
2009-05-11 23:49:29 +02:00
|
|
|
int getPC() {
|
|
|
|
return pCodeBuf->getPC();
|
|
|
|
}
|
|
|
|
|
2009-05-14 00:10:04 +02:00
|
|
|
int o4(int data) {
|
|
|
|
return pCodeBuf->o4(data);
|
|
|
|
}
|
2009-05-11 23:49:29 +02:00
|
|
|
private:
|
|
|
|
CodeBuf* pCodeBuf;
|
|
|
|
};
|
|
|
|
|
2009-05-13 19:58:45 +02:00
|
|
|
class ARMCodeGenerator : public CodeGenerator {
|
|
|
|
public:
|
|
|
|
ARMCodeGenerator() {}
|
|
|
|
virtual ~ARMCodeGenerator() {}
|
|
|
|
|
|
|
|
/* returns address to patch with local variable size
|
|
|
|
*/
|
2009-05-14 00:10:04 +02:00
|
|
|
virtual int functionEntry(int argCount) {
|
|
|
|
fprintf(stderr, "functionEntry(%d);\n", argCount);
|
2009-05-15 00:42:26 +02:00
|
|
|
// sp -> arg4 arg5 ...
|
|
|
|
// Push our register-based arguments back on the stack
|
|
|
|
if (argCount > 0) {
|
|
|
|
int regArgCount = argCount <= 4 ? argCount : 4;
|
|
|
|
o4(0xE92D0000 | ((1 << argCount) - 1)); // stmfd sp!, {}
|
|
|
|
}
|
|
|
|
// sp -> arg0 arg1 ...
|
|
|
|
o4(0xE92D4800); // stmfd sp!, {fp, lr}
|
|
|
|
// sp, fp -> oldfp, retadr, arg0 arg1 ....
|
|
|
|
o4(0xE1A0B00D); // mov fp, sp
|
|
|
|
return o4(0xE24DD000); // sub sp, sp, # <local variables>
|
2009-05-14 00:10:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void functionExit(int argCount, int localVariableAddress, int localVariableSize) {
|
|
|
|
fprintf(stderr, "functionExit(%d, %d, %d);\n", argCount, localVariableAddress, localVariableSize);
|
2009-05-15 00:42:26 +02:00
|
|
|
// Patch local variable allocation code:
|
|
|
|
if (localVariableSize < 0 || localVariableSize > 255) {
|
2009-05-15 02:21:45 +02:00
|
|
|
error("localVariables out of range: %d", localVariableSize);
|
2009-05-14 00:10:04 +02:00
|
|
|
}
|
2009-05-15 00:42:26 +02:00
|
|
|
*(char*) (localVariableAddress) = localVariableSize;
|
|
|
|
|
|
|
|
// sp -> locals .... fp -> oldfp, retadr, arg0, arg1, ...
|
|
|
|
o4(0xE1A0E00B); // mov lr, fp
|
|
|
|
o4(0xE59BB000); // ldr fp, [fp]
|
|
|
|
o4(0xE28ED004); // add sp, lr, #4
|
|
|
|
// sp -> retadr, arg0, ...
|
|
|
|
o4(0xE8BD4000); // ldmfd sp!, {lr}
|
|
|
|
// sp -> arg0 ....
|
|
|
|
if (argCount > 0) {
|
|
|
|
// We store the PC into the lr so we can adjust the sp before
|
2009-05-15 02:21:45 +02:00
|
|
|
// returning. We need to pull off the registers we pushed
|
2009-05-15 00:42:26 +02:00
|
|
|
// earlier. We don't need to actually store them anywhere,
|
|
|
|
// just adjust the stack.
|
|
|
|
int regArgCount = argCount <= 4 ? argCount : 4;
|
|
|
|
o4(0xE28DD000 | (regArgCount << 2)); // add sp, sp, #argCount << 2
|
|
|
|
}
|
|
|
|
o4(0xE12FFF1E); // bx lr
|
2009-05-13 19:58:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* load immediate value */
|
2009-05-14 00:10:04 +02:00
|
|
|
virtual void li(int t) {
|
2009-05-13 19:58:45 +02:00
|
|
|
fprintf(stderr, "li(%d);\n", t);
|
2009-05-14 01:24:17 +02:00
|
|
|
if (t >= 0 && t < 255) {
|
2009-05-15 00:42:26 +02:00
|
|
|
o4(0xE3A00000 + t); // mov r0, #0
|
2009-05-14 01:24:17 +02:00
|
|
|
} else if (t >= -256 && t < 0) {
|
|
|
|
// mvn means move constant ^ ~0
|
2009-05-15 00:42:26 +02:00
|
|
|
o4(0xE3E00001 - t); // mvn r0, #0
|
2009-05-14 01:24:17 +02:00
|
|
|
} else {
|
2009-05-14 20:38:49 +02:00
|
|
|
o4(0xE51F0000); // ldr r0, .L3
|
|
|
|
o4(0xEA000000); // b .L99
|
|
|
|
o4(t); // .L3: .word 0
|
|
|
|
// .L99:
|
2009-05-14 01:24:17 +02:00
|
|
|
}
|
2009-05-13 19:58:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual int gjmp(int t) {
|
|
|
|
fprintf(stderr, "gjmp(%d);\n", t);
|
2009-05-15 02:21:45 +02:00
|
|
|
return o4(0xEA000000 | encodeAddress(t)); // b .L33
|
2009-05-13 19:58:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* l = 0: je, l == 1: jne */
|
|
|
|
virtual int gtst(bool l, int t) {
|
|
|
|
fprintf(stderr, "gtst(%d, %d);\n", l, t);
|
2009-05-15 02:21:45 +02:00
|
|
|
o4(0xE3500000); // cmp r0,#0
|
|
|
|
int branch = l ? 0x1A000000 : 0x0A000000; // bne : beq
|
|
|
|
return o4(branch | encodeAddress(t));
|
2009-05-13 19:58:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void gcmp(int op) {
|
|
|
|
fprintf(stderr, "gcmp(%d);\n", op);
|
2009-05-15 02:21:45 +02:00
|
|
|
o4(0xE1510000); // cmp r1, r1
|
|
|
|
switch(op) {
|
|
|
|
case OP_EQUALS:
|
|
|
|
o4(0x03A00001); // moveq r0,#1
|
|
|
|
o4(0x13A00000); // movne r0,#0
|
|
|
|
break;
|
|
|
|
case OP_NOT_EQUALS:
|
|
|
|
o4(0x03A00000); // moveq r0,#0
|
|
|
|
o4(0x13A00001); // movne r0,#1
|
|
|
|
break;
|
|
|
|
case OP_LESS_EQUAL:
|
|
|
|
o4(0xD3A00001); // movle r0,#1
|
|
|
|
o4(0xC3A00000); // movgt r0,#0
|
|
|
|
break;
|
|
|
|
case OP_GREATER:
|
|
|
|
o4(0xD3A00000); // movle r0,#0
|
|
|
|
o4(0xC3A00001); // movgt r0,#1
|
|
|
|
break;
|
|
|
|
case OP_GREATER_EQUAL:
|
|
|
|
o4(0xA3A00001); // movge r0,#1
|
|
|
|
o4(0xB3A00000); // movlt r0,#0
|
|
|
|
break;
|
|
|
|
case OP_LESS:
|
|
|
|
o4(0xA3A00000); // movge r0,#0
|
|
|
|
o4(0xB3A00001); // movlt r0,#1
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error("Unknown comparison op %d", op);
|
|
|
|
break;
|
|
|
|
}
|
2009-05-13 19:58:45 +02:00
|
|
|
}
|
|
|
|
|
2009-05-14 00:10:04 +02:00
|
|
|
virtual void genOp(int op) {
|
2009-05-13 19:58:45 +02:00
|
|
|
fprintf(stderr, "genOp(%d);\n", op);
|
2009-05-14 20:38:49 +02:00
|
|
|
switch(op) {
|
|
|
|
case OP_MUL:
|
|
|
|
o4(0x0E0000091); // mul r0,r1,r0
|
|
|
|
break;
|
|
|
|
case OP_PLUS:
|
|
|
|
o4(0xE0810000); // add r0,r1,r0
|
|
|
|
break;
|
|
|
|
case OP_MINUS:
|
|
|
|
o4(0xE0410000); // sub r0,r1,r0
|
|
|
|
break;
|
|
|
|
case OP_SHIFT_LEFT:
|
|
|
|
o4(0xE1A00011); // lsl r0,r1,r0
|
|
|
|
break;
|
|
|
|
case OP_SHIFT_RIGHT:
|
|
|
|
o4(0xE1A00051); // asr r0,r1,r0
|
|
|
|
break;
|
|
|
|
case OP_BIT_AND:
|
|
|
|
o4(0xE0010000); // and r0,r1,r0
|
|
|
|
break;
|
|
|
|
case OP_BIT_XOR:
|
|
|
|
o4(0xE0210000); // eor r0,r1,r0
|
|
|
|
break;
|
|
|
|
case OP_BIT_OR:
|
|
|
|
o4(0xE1810000); // orr r0,r1,r0
|
|
|
|
break;
|
|
|
|
case OP_BIT_NOT:
|
|
|
|
o4(0xE1E00000); // mvn r0, r0
|
|
|
|
break;
|
|
|
|
default:
|
2009-05-15 00:42:26 +02:00
|
|
|
error("Unimplemented op %d\n", op);
|
2009-05-14 20:38:49 +02:00
|
|
|
break;
|
|
|
|
}
|
2009-05-13 19:58:45 +02:00
|
|
|
#if 0
|
|
|
|
o(decodeOp(op));
|
|
|
|
if (op == OP_MOD)
|
|
|
|
o(0x92); /* xchg %edx, %eax */
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void clearECX() {
|
|
|
|
fprintf(stderr, "clearECX();\n");
|
2009-05-14 20:38:49 +02:00
|
|
|
o4(0xE3A01000); // mov r1, #0
|
2009-05-13 19:58:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void pushEAX() {
|
|
|
|
fprintf(stderr, "pushEAX();\n");
|
2009-05-14 20:38:49 +02:00
|
|
|
o4(0xE92D0001); // stmfd sp!,{r0}
|
2009-05-13 19:58:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void popECX() {
|
|
|
|
fprintf(stderr, "popECX();\n");
|
2009-05-14 20:38:49 +02:00
|
|
|
o4(0xE8BD0002); // ldmfd sp!,{r1}
|
2009-05-13 19:58:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void storeEAXToAddressECX(bool isInt) {
|
|
|
|
fprintf(stderr, "storeEAXToAddressECX(%d);\n", isInt);
|
2009-05-14 20:38:49 +02:00
|
|
|
o4(0x0188 + isInt); /* movl %eax/%al, (%ecx) */
|
2009-05-13 19:58:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void loadEAXIndirect(bool isInt) {
|
|
|
|
fprintf(stderr, "loadEAXIndirect(%d);\n", isInt);
|
|
|
|
if (isInt)
|
2009-05-15 00:42:26 +02:00
|
|
|
o4(0xE5900000); // ldr r0, [r0]
|
2009-05-13 19:58:45 +02:00
|
|
|
else
|
2009-05-15 00:42:26 +02:00
|
|
|
o4(0xE5D00000); // ldrb r0, [r0]
|
2009-05-13 19:58:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void leaEAX(int ea) {
|
2009-05-15 00:42:26 +02:00
|
|
|
fprintf(stderr, "[!!! fixme !!!] leaEAX(%d);\n", ea);
|
|
|
|
error("Unimplemented");
|
|
|
|
if (ea < -4095 || ea > 4095) {
|
|
|
|
error("Offset out of range: %08x", ea);
|
|
|
|
}
|
|
|
|
o4(0xE59B0000 | (0x1fff & ea)); //ldr r0, [fp,#ea]
|
2009-05-13 19:58:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void storeEAX(int ea) {
|
|
|
|
fprintf(stderr, "storeEAX(%d);\n", ea);
|
2009-05-15 00:42:26 +02:00
|
|
|
int fpOffset = ea;
|
|
|
|
if (fpOffset < -4095 || fpOffset > 4095) {
|
|
|
|
error("Offset out of range: %08x", ea);
|
|
|
|
}
|
|
|
|
if (fpOffset < 0) {
|
|
|
|
o4(0xE50B0000 | (0xfff & (-fpOffset))); // str r0, [fp,#-ea]
|
|
|
|
} else {
|
|
|
|
o4(0xE58B0000 | (0xfff & fpOffset)); // str r0, [fp,#ea]
|
|
|
|
}
|
2009-05-13 19:58:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void loadEAX(int ea) {
|
|
|
|
fprintf(stderr, "loadEAX(%d);\n", ea);
|
2009-05-15 00:42:26 +02:00
|
|
|
int fpOffset = ea;
|
|
|
|
if (fpOffset < -4095 || fpOffset > 4095) {
|
|
|
|
error("Offset out of range: %08x", ea);
|
|
|
|
}
|
|
|
|
if (fpOffset < 0) {
|
|
|
|
o4(0xE51B0000 | (0xfff & (-fpOffset))); // ldr r0, [fp,#-ea]
|
|
|
|
} else {
|
|
|
|
o4(0xE59B0000 | (0xfff & fpOffset)); //ldr r0, [fp,#ea]
|
|
|
|
}
|
2009-05-13 19:58:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void postIncrementOrDecrement(int n, int op) {
|
|
|
|
fprintf(stderr, "postIncrementOrDecrement(%d, %d);\n", n, op);
|
|
|
|
/* Implement post-increment or post decrement.
|
|
|
|
*/
|
2009-05-15 00:42:26 +02:00
|
|
|
|
|
|
|
error("Unimplemented");
|
2009-05-13 19:58:45 +02:00
|
|
|
#if 0
|
|
|
|
gmov(0, n); /* 83 ADD */
|
|
|
|
o(decodeOp(op));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-05-14 20:38:49 +02:00
|
|
|
virtual int beginFunctionCallArguments() {
|
|
|
|
fprintf(stderr, "beginFunctionCallArguments();\n");
|
|
|
|
return o4(0xE24DDF00); // Placeholder
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void endFunctionCallArguments(int a, int l) {
|
|
|
|
fprintf(stderr, "endFunctionCallArguments(0x%08x, %d);\n", a, l);
|
|
|
|
if (l < 0 || l > 0x3FC) {
|
|
|
|
error("L out of range for stack adjustment: 0x%08x", l);
|
|
|
|
}
|
|
|
|
* (int*) a = 0xE24DDF00 | (l >> 2); // sub sp, sp, #0 << 2
|
|
|
|
int argCount = l >> 2;
|
|
|
|
if (argCount > 0) {
|
|
|
|
int regArgCount = argCount > 4 ? 4 : argCount;
|
|
|
|
o4(0xE8BD0000 | ((1 << regArgCount) - 1)); // ldmfd sp!,{}
|
|
|
|
}
|
2009-05-13 19:58:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void storeEAToArg(int l) {
|
|
|
|
fprintf(stderr, "storeEAToArg(%d);\n", l);
|
2009-05-14 20:38:49 +02:00
|
|
|
if (l < 0 || l > 4096-4) {
|
|
|
|
error("l out of range for stack offset: 0x%08x", l);
|
|
|
|
}
|
|
|
|
o4(0xE58D0000 + l); // str r0, [sp, #4]
|
2009-05-13 19:58:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual int callForward(int symbol) {
|
|
|
|
fprintf(stderr, "callForward(%d);\n", symbol);
|
2009-05-14 20:38:49 +02:00
|
|
|
// Forward calls are always short (local)
|
|
|
|
return o4(0xEB000000 | encodeAddress(symbol));
|
2009-05-13 19:58:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void callRelative(int t) {
|
|
|
|
fprintf(stderr, "callRelative(%d);\n", t);
|
2009-05-14 20:38:49 +02:00
|
|
|
int abs = t + getPC() + jumpOffset();
|
2009-05-15 00:42:26 +02:00
|
|
|
fprintf(stderr, "abs=%d (0x08%x)\n", abs, abs);
|
2009-05-14 20:38:49 +02:00
|
|
|
if (t >= - (1 << 25) && t < (1 << 25)) {
|
|
|
|
o4(0xEB000000 | encodeAddress(t));
|
|
|
|
} else {
|
|
|
|
// Long call.
|
|
|
|
o4(0xE59FC000); // ldr r12, .L1
|
|
|
|
o4(0xEA000000); // b .L99
|
|
|
|
o4(t - 16); // .L1: .word 0
|
|
|
|
o4(0xE08CC00F); // .L99: add r12,pc
|
|
|
|
o4(0xE12FFF3C); // blx r12
|
|
|
|
}
|
2009-05-13 19:58:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void callIndirect(int l) {
|
|
|
|
fprintf(stderr, "callIndirect(%d);\n", l);
|
|
|
|
oad(0x2494ff, l); /* call *xxx(%esp) */
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void adjustStackAfterCall(int l) {
|
|
|
|
fprintf(stderr, "adjustStackAfterCall(%d);\n", l);
|
2009-05-14 20:38:49 +02:00
|
|
|
if (l < 0 || l > 0x3FC) {
|
|
|
|
error("L out of range for stack adjustment: 0x%08x", l);
|
|
|
|
}
|
|
|
|
int argCount = l >> 2;
|
|
|
|
if (argCount > 4) {
|
|
|
|
int remainingArgs = argCount - 4;
|
|
|
|
o4(0xE28DDF00 | remainingArgs); // add sp, sp, #0x3fc
|
|
|
|
}
|
|
|
|
|
2009-05-13 19:58:45 +02:00
|
|
|
}
|
|
|
|
|
2009-05-14 01:24:17 +02:00
|
|
|
virtual int jumpOffset() {
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* output a symbol and patch all calls to it */
|
|
|
|
virtual void gsym(int t) {
|
|
|
|
fprintf(stderr, "gsym(0x%x)\n", t);
|
|
|
|
int n;
|
|
|
|
int base = getBase();
|
|
|
|
int pc = getPC();
|
|
|
|
fprintf(stderr, "pc = 0x%x\n", pc);
|
|
|
|
while (t) {
|
|
|
|
int data = * (int*) t;
|
|
|
|
int decodedOffset = ((BRANCH_REL_ADDRESS_MASK & data) << 2);
|
|
|
|
if (decodedOffset == 0) {
|
|
|
|
n = 0;
|
|
|
|
} else {
|
|
|
|
n = base + decodedOffset; /* next value */
|
|
|
|
}
|
|
|
|
*(int *) t = (data & ~BRANCH_REL_ADDRESS_MASK)
|
|
|
|
| encodeRelAddress(pc - t - 8);
|
|
|
|
t = n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual int disassemble(FILE* out) {
|
|
|
|
disasmOut = out;
|
|
|
|
disasm_interface_t di;
|
|
|
|
di.di_readword = disassemble_readword;
|
|
|
|
di.di_printaddr = disassemble_printaddr;
|
|
|
|
di.di_printf = disassemble_printf;
|
|
|
|
|
|
|
|
int base = getBase();
|
|
|
|
int pc = getPC();
|
|
|
|
for(int i = base; i < pc; i += 4) {
|
|
|
|
fprintf(out, "%08x: %08x ", i, *(int*) i);
|
|
|
|
::disasm(&di, i, 0);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2009-05-13 19:58:45 +02:00
|
|
|
private:
|
2009-05-14 01:24:17 +02:00
|
|
|
static FILE* disasmOut;
|
|
|
|
|
|
|
|
static u_int
|
|
|
|
disassemble_readword(u_int address)
|
|
|
|
{
|
|
|
|
return(*((u_int *)address));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
disassemble_printaddr(u_int address)
|
|
|
|
{
|
|
|
|
fprintf(disasmOut, "0x%08x", address);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
disassemble_printf(const char *fmt, ...) {
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vfprintf(disasmOut, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const int BRANCH_REL_ADDRESS_MASK = 0x00ffffff;
|
|
|
|
|
|
|
|
/** Encode a relative address that might also be
|
|
|
|
* a label.
|
|
|
|
*/
|
|
|
|
int encodeAddress(int value) {
|
|
|
|
int base = getBase();
|
|
|
|
if (value >= base && value <= getPC() ) {
|
|
|
|
// This is a label, encode it relative to the base.
|
|
|
|
value = value - base;
|
|
|
|
}
|
|
|
|
return encodeRelAddress(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
int encodeRelAddress(int value) {
|
|
|
|
return BRANCH_REL_ADDRESS_MASK & (value >> 2);
|
|
|
|
}
|
2009-05-13 19:58:45 +02:00
|
|
|
|
2009-05-14 00:10:04 +02:00
|
|
|
void error(const char* fmt,...) {
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vfprintf(stderr, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
exit(12);
|
|
|
|
}
|
2009-05-13 19:58:45 +02:00
|
|
|
};
|
|
|
|
|
2009-05-11 23:49:29 +02:00
|
|
|
class X86CodeGenerator : public CodeGenerator {
|
|
|
|
public:
|
|
|
|
X86CodeGenerator() {}
|
|
|
|
virtual ~X86CodeGenerator() {}
|
|
|
|
|
2009-05-12 21:48:35 +02:00
|
|
|
/* returns address to patch with local variable size
|
|
|
|
*/
|
2009-05-14 00:10:04 +02:00
|
|
|
virtual int functionEntry(int argCount) {
|
2009-05-12 21:48:35 +02:00
|
|
|
o(0xe58955); /* push %ebp, mov %esp, %ebp */
|
|
|
|
return oad(0xec81, 0); /* sub $xxx, %esp */
|
|
|
|
}
|
|
|
|
|
2009-05-14 00:10:04 +02:00
|
|
|
virtual void functionExit(int argCount, int localVariableAddress, int localVariableSize) {
|
2009-05-12 21:48:35 +02:00
|
|
|
o(0xc3c9); /* leave, ret */
|
2009-05-14 00:10:04 +02:00
|
|
|
*(int *) localVariableAddress = localVariableSize; /* save local variables */
|
2009-05-12 21:48:35 +02:00
|
|
|
}
|
|
|
|
|
2009-05-11 23:49:29 +02:00
|
|
|
/* load immediate value */
|
2009-05-14 00:10:04 +02:00
|
|
|
virtual void li(int t) {
|
2009-05-11 23:49:29 +02:00
|
|
|
oad(0xb8, t); /* mov $xx, %eax */
|
|
|
|
}
|
|
|
|
|
2009-05-13 19:58:45 +02:00
|
|
|
virtual int gjmp(int t) {
|
2009-05-11 23:49:29 +02:00
|
|
|
return psym(0xe9, t);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* l = 0: je, l == 1: jne */
|
2009-05-13 19:58:45 +02:00
|
|
|
virtual int gtst(bool l, int t) {
|
2009-05-11 23:49:29 +02:00
|
|
|
o(0x0fc085); /* test %eax, %eax, je/jne xxx */
|
|
|
|
return psym(0x84 + l, t);
|
|
|
|
}
|
|
|
|
|
2009-05-13 19:58:45 +02:00
|
|
|
virtual void gcmp(int op) {
|
2009-05-12 21:48:35 +02:00
|
|
|
int t = decodeOp(op);
|
2009-05-11 23:49:29 +02:00
|
|
|
o(0xc139); /* cmp %eax,%ecx */
|
|
|
|
li(0);
|
|
|
|
o(0x0f); /* setxx %al */
|
|
|
|
o(t + 0x90);
|
|
|
|
o(0xc0);
|
|
|
|
}
|
|
|
|
|
2009-05-14 00:10:04 +02:00
|
|
|
virtual void genOp(int op) {
|
2009-05-12 21:48:35 +02:00
|
|
|
o(decodeOp(op));
|
|
|
|
if (op == OP_MOD)
|
|
|
|
o(0x92); /* xchg %edx, %eax */
|
|
|
|
}
|
|
|
|
|
2009-05-13 19:58:45 +02:00
|
|
|
virtual void clearECX() {
|
2009-05-11 23:49:29 +02:00
|
|
|
oad(0xb9, 0); /* movl $0, %ecx */
|
|
|
|
}
|
|
|
|
|
2009-05-13 19:58:45 +02:00
|
|
|
virtual void pushEAX() {
|
2009-05-11 23:49:29 +02:00
|
|
|
o(0x50); /* push %eax */
|
|
|
|
}
|
|
|
|
|
2009-05-13 19:58:45 +02:00
|
|
|
virtual void popECX() {
|
2009-05-11 23:49:29 +02:00
|
|
|
o(0x59); /* pop %ecx */
|
2009-05-12 21:48:35 +02:00
|
|
|
}
|
|
|
|
|
2009-05-13 19:58:45 +02:00
|
|
|
virtual void storeEAXToAddressECX(bool isInt) {
|
2009-05-11 23:49:29 +02:00
|
|
|
o(0x0188 + isInt); /* movl %eax/%al, (%ecx) */
|
|
|
|
}
|
|
|
|
|
2009-05-13 19:58:45 +02:00
|
|
|
virtual void loadEAXIndirect(bool isInt) {
|
2009-05-11 23:49:29 +02:00
|
|
|
if (isInt)
|
|
|
|
o(0x8b); /* mov (%eax), %eax */
|
|
|
|
else
|
|
|
|
o(0xbe0f); /* movsbl (%eax), %eax */
|
|
|
|
ob(0); /* add zero in code */
|
|
|
|
}
|
|
|
|
|
2009-05-13 19:58:45 +02:00
|
|
|
virtual void leaEAX(int ea) {
|
2009-05-11 23:49:29 +02:00
|
|
|
gmov(10, ea); /* leal EA, %eax */
|
|
|
|
}
|
|
|
|
|
2009-05-13 19:58:45 +02:00
|
|
|
virtual void storeEAX(int ea) {
|
2009-05-11 23:49:29 +02:00
|
|
|
gmov(6, ea); /* mov %eax, EA */
|
|
|
|
}
|
|
|
|
|
2009-05-13 19:58:45 +02:00
|
|
|
virtual void loadEAX(int ea) {
|
2009-05-11 23:49:29 +02:00
|
|
|
gmov(8, ea); /* mov EA, %eax */
|
|
|
|
}
|
|
|
|
|
2009-05-13 19:58:45 +02:00
|
|
|
virtual void postIncrementOrDecrement(int n, int op) {
|
2009-05-12 21:48:35 +02:00
|
|
|
/* Implement post-increment or post decrement.
|
2009-05-11 23:49:29 +02:00
|
|
|
*/
|
|
|
|
gmov(0, n); /* 83 ADD */
|
2009-05-12 21:48:35 +02:00
|
|
|
o(decodeOp(op));
|
2009-05-11 23:49:29 +02:00
|
|
|
}
|
|
|
|
|
2009-05-14 20:38:49 +02:00
|
|
|
virtual int beginFunctionCallArguments() {
|
2009-05-11 23:49:29 +02:00
|
|
|
return oad(0xec81, 0); /* sub $xxx, %esp */
|
|
|
|
}
|
|
|
|
|
2009-05-14 20:38:49 +02:00
|
|
|
virtual void endFunctionCallArguments(int a, int l) {
|
|
|
|
* (int*) a = l;
|
|
|
|
}
|
|
|
|
|
2009-05-13 19:58:45 +02:00
|
|
|
virtual void storeEAToArg(int l) {
|
2009-05-11 23:49:29 +02:00
|
|
|
oad(0x248489, l); /* movl %eax, xxx(%esp) */
|
|
|
|
}
|
|
|
|
|
2009-05-13 19:58:45 +02:00
|
|
|
virtual int callForward(int symbol) {
|
2009-05-11 23:49:29 +02:00
|
|
|
return psym(0xe8, symbol); /* call xxx */
|
|
|
|
}
|
|
|
|
|
2009-05-13 19:58:45 +02:00
|
|
|
virtual void callRelative(int t) {
|
2009-05-11 23:49:29 +02:00
|
|
|
psym(0xe8, t); /* call xxx */
|
|
|
|
}
|
|
|
|
|
2009-05-13 19:58:45 +02:00
|
|
|
virtual void callIndirect(int l) {
|
2009-05-11 23:49:29 +02:00
|
|
|
oad(0x2494ff, l); /* call *xxx(%esp) */
|
|
|
|
}
|
|
|
|
|
2009-05-13 19:58:45 +02:00
|
|
|
virtual void adjustStackAfterCall(int l) {
|
2009-05-11 23:49:29 +02:00
|
|
|
oad(0xc481, l); /* add $xxx, %esp */
|
|
|
|
}
|
|
|
|
|
2009-05-14 01:24:17 +02:00
|
|
|
virtual int jumpOffset() {
|
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual int disassemble(FILE* out) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-05-12 21:48:35 +02:00
|
|
|
private:
|
|
|
|
static const int operatorHelper[];
|
2009-05-11 23:49:29 +02:00
|
|
|
|
2009-05-12 21:48:35 +02:00
|
|
|
int decodeOp(int op) {
|
|
|
|
if (op < 0 || op > OP_COUNT) {
|
|
|
|
fprintf(stderr, "Out-of-range operator: %d\n", op);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return operatorHelper[op];
|
2009-05-11 23:49:29 +02:00
|
|
|
}
|
|
|
|
|
2009-05-14 00:10:04 +02:00
|
|
|
void gmov(int l, int t) {
|
2009-05-11 23:49:29 +02:00
|
|
|
o(l + 0x83);
|
|
|
|
oad((t < LOCAL) << 7 | 5, t);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* vars: value of variables
|
|
|
|
loc : local variable index
|
|
|
|
glo : global variable index
|
|
|
|
ind : output code ptr
|
|
|
|
rsym: return symbol
|
|
|
|
prog: output code
|
|
|
|
dstk: define stack
|
|
|
|
dptr, dch: macro state
|
|
|
|
*/
|
|
|
|
int tok, tokc, tokl, ch, vars, rsym, loc, glo, sym_stk, dstk,
|
|
|
|
dptr, dch, last_id;
|
|
|
|
void* pSymbolBase;
|
|
|
|
void* pGlobalBase;
|
|
|
|
void* pVarsBase;
|
|
|
|
FILE* file;
|
|
|
|
|
|
|
|
CodeBuf codeBuf;
|
2009-05-13 19:58:45 +02:00
|
|
|
CodeGenerator* pGen;
|
2009-05-11 23:49:29 +02:00
|
|
|
|
|
|
|
static const int ALLOC_SIZE = 99999;
|
|
|
|
|
|
|
|
/* depends on the init string */
|
|
|
|
static const int TOK_STR_SIZE = 48;
|
|
|
|
static const int TOK_IDENT = 0x100;
|
|
|
|
static const int TOK_INT = 0x100;
|
|
|
|
static const int TOK_IF = 0x120;
|
|
|
|
static const int TOK_ELSE = 0x138;
|
|
|
|
static const int TOK_WHILE = 0x160;
|
|
|
|
static const int TOK_BREAK = 0x190;
|
|
|
|
static const int TOK_RETURN = 0x1c0;
|
|
|
|
static const int TOK_FOR = 0x1f8;
|
|
|
|
static const int TOK_DEFINE = 0x218;
|
|
|
|
static const int TOK_MAIN = 0x250;
|
|
|
|
|
|
|
|
static const int TOK_DUMMY = 1;
|
|
|
|
static const int TOK_NUM = 2;
|
|
|
|
|
|
|
|
static const int LOCAL = 0x200;
|
|
|
|
|
|
|
|
static const int SYM_FORWARD = 0;
|
|
|
|
static const int SYM_DEFINE = 1;
|
|
|
|
|
|
|
|
/* tokens in string heap */
|
|
|
|
static const int TAG_TOK = ' ';
|
|
|
|
static const int TAG_MACRO = 2;
|
|
|
|
|
2009-05-12 21:48:35 +02:00
|
|
|
static const int OP_INCREMENT = 0;
|
|
|
|
static const int OP_DECREMENT = 1;
|
|
|
|
static const int OP_MUL = 2;
|
|
|
|
static const int OP_DIV = 3;
|
|
|
|
static const int OP_MOD = 4;
|
|
|
|
static const int OP_PLUS = 5;
|
|
|
|
static const int OP_MINUS = 6;
|
|
|
|
static const int OP_SHIFT_LEFT = 7;
|
|
|
|
static const int OP_SHIFT_RIGHT = 8;
|
|
|
|
static const int OP_LESS_EQUAL = 9;
|
|
|
|
static const int OP_GREATER_EQUAL = 10;
|
|
|
|
static const int OP_LESS = 11;
|
|
|
|
static const int OP_GREATER = 12;
|
|
|
|
static const int OP_EQUALS = 13;
|
|
|
|
static const int OP_NOT_EQUALS = 14;
|
|
|
|
static const int OP_LOGICAL_AND = 15;
|
|
|
|
static const int OP_LOGICAL_OR = 16;
|
|
|
|
static const int OP_BIT_AND = 17;
|
|
|
|
static const int OP_BIT_XOR = 18;
|
|
|
|
static const int OP_BIT_OR = 19;
|
|
|
|
static const int OP_BIT_NOT = 20;
|
|
|
|
static const int OP_LOGICAL_NOT = 21;
|
|
|
|
static const int OP_COUNT = 22;
|
|
|
|
|
|
|
|
/* Operators are searched from front, the two-character operators appear
|
|
|
|
* before the single-character operators with the same first character.
|
|
|
|
* @ is used to pad out single-character operators.
|
|
|
|
*/
|
|
|
|
static const char* operatorChars;
|
|
|
|
static const char operatorLevel[];
|
|
|
|
|
2009-05-11 23:49:29 +02:00
|
|
|
void pdef(int t) {
|
|
|
|
*(char *) dstk++ = t;
|
|
|
|
}
|
|
|
|
|
|
|
|
void inp() {
|
|
|
|
if (dptr) {
|
|
|
|
ch = *(char *) dptr++;
|
|
|
|
if (ch == TAG_MACRO) {
|
|
|
|
dptr = 0;
|
|
|
|
ch = dch;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
ch = fgetc(file);
|
|
|
|
/* printf("ch=%c 0x%x\n", ch, ch); */
|
|
|
|
}
|
|
|
|
|
|
|
|
int isid() {
|
2009-05-14 00:10:04 +02:00
|
|
|
return isalnum(ch) | (ch == '_');
|
2009-05-08 23:54:15 +02:00
|
|
|
}
|
2009-05-11 23:49:29 +02:00
|
|
|
|
|
|
|
/* read a character constant */
|
|
|
|
void getq() {
|
|
|
|
if (ch == '\\') {
|
2009-05-10 23:09:03 +02:00
|
|
|
inp();
|
2009-05-11 23:49:29 +02:00
|
|
|
if (ch == 'n')
|
|
|
|
ch = '\n';
|
2009-05-08 23:54:15 +02:00
|
|
|
}
|
2009-05-11 23:49:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void next() {
|
|
|
|
int l, a;
|
|
|
|
|
2009-05-14 00:10:04 +02:00
|
|
|
while (isspace(ch) | (ch == '#')) {
|
2009-05-11 23:49:29 +02:00
|
|
|
if (ch == '#') {
|
|
|
|
inp();
|
|
|
|
next();
|
|
|
|
if (tok == TOK_DEFINE) {
|
2009-05-10 23:09:03 +02:00
|
|
|
next();
|
2009-05-11 23:49:29 +02:00
|
|
|
pdef(TAG_TOK); /* fill last ident tag */
|
|
|
|
*(int *) tok = SYM_DEFINE;
|
|
|
|
*(int *) (tok + 4) = dstk; /* define stack */
|
|
|
|
}
|
|
|
|
/* well we always save the values ! */
|
|
|
|
while (ch != '\n') {
|
|
|
|
pdef(ch);
|
|
|
|
inp();
|
2009-05-08 23:54:15 +02:00
|
|
|
}
|
2009-05-11 23:49:29 +02:00
|
|
|
pdef(ch);
|
|
|
|
pdef(TAG_MACRO);
|
2009-05-08 23:54:15 +02:00
|
|
|
}
|
2009-05-10 23:09:03 +02:00
|
|
|
inp();
|
2009-05-11 23:49:29 +02:00
|
|
|
}
|
|
|
|
tokl = 0;
|
|
|
|
tok = ch;
|
|
|
|
/* encode identifiers & numbers */
|
|
|
|
if (isid()) {
|
|
|
|
pdef(TAG_TOK);
|
|
|
|
last_id = dstk;
|
|
|
|
while (isid()) {
|
|
|
|
pdef(ch);
|
2009-05-10 23:09:03 +02:00
|
|
|
inp();
|
2009-05-08 23:54:15 +02:00
|
|
|
}
|
2009-05-11 23:49:29 +02:00
|
|
|
if (isdigit(tok)) {
|
|
|
|
tokc = strtol((char*) last_id, 0, 0);
|
|
|
|
tok = TOK_NUM;
|
|
|
|
} else {
|
|
|
|
*(char *) dstk = TAG_TOK; /* no need to mark end of string (we
|
|
|
|
suppose data is initialized to zero by calloc) */
|
|
|
|
tok = (int) (strstr((char*) sym_stk, (char*) (last_id - 1))
|
|
|
|
- sym_stk);
|
|
|
|
*(char *) dstk = 0; /* mark real end of ident for dlsym() */
|
|
|
|
tok = tok * 8 + TOK_IDENT;
|
|
|
|
if (tok > TOK_DEFINE) {
|
|
|
|
tok = vars + tok;
|
|
|
|
/* printf("tok=%s %x\n", last_id, tok); */
|
|
|
|
/* define handling */
|
|
|
|
if (*(int *) tok == SYM_DEFINE) {
|
|
|
|
dptr = *(int *) (tok + 4);
|
|
|
|
dch = ch;
|
|
|
|
inp();
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2009-05-10 23:09:03 +02:00
|
|
|
inp();
|
2009-05-11 23:49:29 +02:00
|
|
|
if (tok == '\'') {
|
|
|
|
tok = TOK_NUM;
|
|
|
|
getq();
|
|
|
|
tokc = ch;
|
|
|
|
inp();
|
|
|
|
inp();
|
2009-05-14 00:10:04 +02:00
|
|
|
} else if ((tok == '/') & (ch == '*')) {
|
2009-05-11 23:49:29 +02:00
|
|
|
inp();
|
|
|
|
while (ch) {
|
|
|
|
while (ch != '*')
|
|
|
|
inp();
|
|
|
|
inp();
|
|
|
|
if (ch == '/')
|
|
|
|
ch = 0;
|
|
|
|
}
|
|
|
|
inp();
|
|
|
|
next();
|
|
|
|
} else {
|
2009-05-12 21:48:35 +02:00
|
|
|
const char* t = operatorChars;
|
|
|
|
int opIndex = 0;
|
2009-05-14 00:10:04 +02:00
|
|
|
while ((l = *t++) != 0) {
|
2009-05-11 23:49:29 +02:00
|
|
|
a = *t++;
|
2009-05-12 21:48:35 +02:00
|
|
|
tokl = operatorLevel[opIndex];
|
|
|
|
tokc = opIndex;
|
2009-05-14 00:10:04 +02:00
|
|
|
if ((l == tok) & ((a == ch) | (a == '@'))) {
|
2009-05-10 23:09:03 +02:00
|
|
|
#if 0
|
2009-05-11 23:49:29 +02:00
|
|
|
printf("%c%c -> tokl=%d tokc=0x%x\n",
|
|
|
|
l, a, tokl, tokc);
|
2009-05-10 23:09:03 +02:00
|
|
|
#endif
|
2009-05-11 23:49:29 +02:00
|
|
|
if (a == ch) {
|
|
|
|
inp();
|
|
|
|
tok = TOK_DUMMY; /* dummy token for double tokens */
|
|
|
|
}
|
|
|
|
break;
|
2009-05-08 23:54:15 +02:00
|
|
|
}
|
2009-05-12 21:48:35 +02:00
|
|
|
opIndex++;
|
|
|
|
}
|
|
|
|
if (l == 0) {
|
|
|
|
tokl = 0;
|
|
|
|
tokc = 0;
|
2009-05-08 23:54:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-05-10 23:09:03 +02:00
|
|
|
#if 0
|
2009-05-11 23:49:29 +02:00
|
|
|
{
|
|
|
|
int p;
|
2009-05-10 23:09:03 +02:00
|
|
|
|
2009-05-11 23:49:29 +02:00
|
|
|
printf("tok=0x%x ", tok);
|
|
|
|
if (tok >= TOK_IDENT) {
|
|
|
|
printf("'");
|
|
|
|
if (tok> TOK_DEFINE)
|
2009-05-10 23:09:03 +02:00
|
|
|
p = sym_stk + 1 + (tok - vars - TOK_IDENT) / 8;
|
2009-05-11 23:49:29 +02:00
|
|
|
else
|
2009-05-10 23:09:03 +02:00
|
|
|
p = sym_stk + 1 + (tok - TOK_IDENT) / 8;
|
2009-05-11 23:49:29 +02:00
|
|
|
while (*(char *)p != TAG_TOK && *(char *)p)
|
2009-05-10 23:09:03 +02:00
|
|
|
printf("%c", *(char *)p++);
|
2009-05-11 23:49:29 +02:00
|
|
|
printf("'\n");
|
|
|
|
} else if (tok == TOK_NUM) {
|
|
|
|
printf("%d\n", tokc);
|
|
|
|
} else {
|
|
|
|
printf("'%c'\n", tok);
|
|
|
|
}
|
2009-05-10 23:09:03 +02:00
|
|
|
}
|
|
|
|
#endif
|
2009-05-11 23:49:29 +02:00
|
|
|
}
|
2009-05-08 23:54:15 +02:00
|
|
|
|
2009-05-11 23:49:29 +02:00
|
|
|
void error(const char *fmt, ...) {
|
|
|
|
va_list ap;
|
2009-05-10 23:09:03 +02:00
|
|
|
|
2009-05-11 23:49:29 +02:00
|
|
|
va_start(ap, fmt);
|
|
|
|
fprintf(stderr, "%ld: ", ftell((FILE *) file));
|
|
|
|
vfprintf(stderr, fmt, ap);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
va_end(ap);
|
|
|
|
exit(1);
|
|
|
|
}
|
2009-05-10 23:09:03 +02:00
|
|
|
|
2009-05-11 23:49:29 +02:00
|
|
|
void skip(int c) {
|
|
|
|
if (tok != c) {
|
|
|
|
error("'%c' expected", c);
|
|
|
|
}
|
|
|
|
next();
|
2009-05-08 23:54:15 +02:00
|
|
|
}
|
|
|
|
|
2009-05-11 23:49:29 +02:00
|
|
|
/* l is one if '=' parsing wanted (quick hack) */
|
|
|
|
void unary(int l) {
|
|
|
|
int n, t, a, c;
|
2009-05-14 00:10:04 +02:00
|
|
|
t = 0;
|
2009-05-11 23:49:29 +02:00
|
|
|
n = 1; /* type of expression 0 = forward, 1 = value, other =
|
|
|
|
lvalue */
|
|
|
|
if (tok == '\"') {
|
2009-05-12 21:48:35 +02:00
|
|
|
pGen->li(glo);
|
2009-05-11 23:49:29 +02:00
|
|
|
while (ch != '\"') {
|
|
|
|
getq();
|
|
|
|
*(char *) glo++ = ch;
|
|
|
|
inp();
|
|
|
|
}
|
|
|
|
*(char *) glo = 0;
|
2009-05-14 00:10:04 +02:00
|
|
|
glo = (glo + 4) & -4; /* align heap */
|
2009-05-10 23:09:03 +02:00
|
|
|
inp();
|
2009-05-11 23:49:29 +02:00
|
|
|
next();
|
|
|
|
} else {
|
|
|
|
c = tokl;
|
|
|
|
a = tokc;
|
|
|
|
t = tok;
|
|
|
|
next();
|
|
|
|
if (t == TOK_NUM) {
|
2009-05-12 21:48:35 +02:00
|
|
|
pGen->li(a);
|
2009-05-11 23:49:29 +02:00
|
|
|
} else if (c == 2) {
|
|
|
|
/* -, +, !, ~ */
|
|
|
|
unary(0);
|
2009-05-12 21:48:35 +02:00
|
|
|
pGen->clearECX();
|
2009-05-11 23:49:29 +02:00
|
|
|
if (t == '!')
|
2009-05-12 21:48:35 +02:00
|
|
|
pGen->gcmp(a);
|
2009-05-11 23:49:29 +02:00
|
|
|
else
|
2009-05-12 21:48:35 +02:00
|
|
|
pGen->genOp(a);
|
2009-05-11 23:49:29 +02:00
|
|
|
} else if (t == '(') {
|
|
|
|
expr();
|
2009-05-10 23:09:03 +02:00
|
|
|
skip(')');
|
2009-05-11 23:49:29 +02:00
|
|
|
} else if (t == '*') {
|
|
|
|
/* parse cast */
|
2009-05-10 23:09:03 +02:00
|
|
|
skip('(');
|
2009-05-11 23:49:29 +02:00
|
|
|
t = tok; /* get type */
|
|
|
|
next(); /* skip int/char/void */
|
|
|
|
next(); /* skip '*' or '(' */
|
|
|
|
if (tok == '*') {
|
|
|
|
/* function type */
|
|
|
|
skip('*');
|
|
|
|
skip(')');
|
|
|
|
skip('(');
|
|
|
|
skip(')');
|
|
|
|
t = 0;
|
|
|
|
}
|
2009-05-10 23:09:03 +02:00
|
|
|
skip(')');
|
2009-05-11 23:49:29 +02:00
|
|
|
unary(0);
|
|
|
|
if (tok == '=') {
|
|
|
|
next();
|
|
|
|
pGen->pushEAX();
|
|
|
|
expr();
|
2009-05-12 21:48:35 +02:00
|
|
|
pGen->popECX();
|
|
|
|
pGen->storeEAXToAddressECX(t == TOK_INT);
|
2009-05-11 23:49:29 +02:00
|
|
|
} else if (t) {
|
2009-05-12 21:48:35 +02:00
|
|
|
pGen->loadEAXIndirect(t == TOK_INT);
|
2009-05-11 23:49:29 +02:00
|
|
|
}
|
|
|
|
} else if (t == '&') {
|
2009-05-12 21:48:35 +02:00
|
|
|
pGen->leaEAX(*(int *) tok);
|
2009-05-10 23:09:03 +02:00
|
|
|
next();
|
2009-05-11 23:49:29 +02:00
|
|
|
} else {
|
|
|
|
n = *(int *) t;
|
|
|
|
/* forward reference: try dlsym */
|
2009-05-14 20:38:49 +02:00
|
|
|
if (!n) {
|
|
|
|
n = (int) dlsym(RTLD_DEFAULT, (char*) last_id);
|
|
|
|
}
|
2009-05-14 00:10:04 +02:00
|
|
|
if ((tok == '=') & l) {
|
2009-05-11 23:49:29 +02:00
|
|
|
/* assignment */
|
2009-05-10 23:09:03 +02:00
|
|
|
next();
|
2009-05-11 23:49:29 +02:00
|
|
|
expr();
|
|
|
|
pGen->storeEAX(n);
|
|
|
|
} else if (tok != '(') {
|
|
|
|
/* variable */
|
|
|
|
pGen->loadEAX(n);
|
|
|
|
if (tokl == 11) {
|
2009-05-12 21:48:35 +02:00
|
|
|
pGen->postIncrementOrDecrement(n, tokc);
|
2009-05-11 23:49:29 +02:00
|
|
|
next();
|
|
|
|
}
|
2009-05-08 23:54:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-05-10 23:09:03 +02:00
|
|
|
|
2009-05-11 23:49:29 +02:00
|
|
|
/* function call */
|
|
|
|
if (tok == '(') {
|
|
|
|
if (n == 1)
|
|
|
|
pGen->pushEAX();
|
2009-05-10 23:09:03 +02:00
|
|
|
|
2009-05-11 23:49:29 +02:00
|
|
|
/* push args and invert order */
|
2009-05-14 20:38:49 +02:00
|
|
|
a = pGen->beginFunctionCallArguments();
|
2009-05-11 23:49:29 +02:00
|
|
|
next();
|
|
|
|
l = 0;
|
|
|
|
while (tok != ')') {
|
|
|
|
expr();
|
|
|
|
pGen->storeEAToArg(l);
|
|
|
|
if (tok == ',')
|
|
|
|
next();
|
|
|
|
l = l + 4;
|
|
|
|
}
|
2009-05-14 20:38:49 +02:00
|
|
|
pGen->endFunctionCallArguments(a, l);
|
2009-05-11 23:49:29 +02:00
|
|
|
next();
|
|
|
|
if (!n) {
|
|
|
|
/* forward reference */
|
|
|
|
t = t + 4;
|
|
|
|
*(int *) t = pGen->callForward(*(int *) t);
|
|
|
|
} else if (n == 1) {
|
|
|
|
pGen->callIndirect(l);
|
|
|
|
l = l + 4;
|
|
|
|
} else {
|
2009-05-14 01:24:17 +02:00
|
|
|
pGen->callRelative(n - codeBuf.getPC() - pGen->jumpOffset()); /* call xxx */
|
2009-05-11 23:49:29 +02:00
|
|
|
}
|
|
|
|
if (l)
|
|
|
|
pGen->adjustStackAfterCall(l);
|
2009-05-08 23:54:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-11 23:49:29 +02:00
|
|
|
void sum(int l) {
|
|
|
|
int t, n, a;
|
2009-05-14 00:10:04 +02:00
|
|
|
t = 0;
|
2009-05-11 23:49:29 +02:00
|
|
|
if (l-- == 1)
|
|
|
|
unary(1);
|
|
|
|
else {
|
|
|
|
sum(l);
|
|
|
|
a = 0;
|
|
|
|
while (l == tokl) {
|
|
|
|
n = tok;
|
|
|
|
t = tokc;
|
|
|
|
next();
|
2009-05-11 20:54:30 +02:00
|
|
|
|
2009-05-11 23:49:29 +02:00
|
|
|
if (l > 8) {
|
2009-05-12 21:48:35 +02:00
|
|
|
a = pGen->gtst(t == OP_LOGICAL_OR, a); /* && and || output code generation */
|
2009-05-11 23:49:29 +02:00
|
|
|
sum(l);
|
2009-05-10 23:09:03 +02:00
|
|
|
} else {
|
2009-05-12 21:48:35 +02:00
|
|
|
pGen->pushEAX();
|
2009-05-11 23:49:29 +02:00
|
|
|
sum(l);
|
2009-05-12 21:48:35 +02:00
|
|
|
pGen->popECX();
|
2009-05-11 23:49:29 +02:00
|
|
|
|
2009-05-14 00:10:04 +02:00
|
|
|
if ((l == 4) | (l == 5)) {
|
2009-05-12 21:48:35 +02:00
|
|
|
pGen->gcmp(t);
|
2009-05-11 23:49:29 +02:00
|
|
|
} else {
|
2009-05-12 21:48:35 +02:00
|
|
|
pGen->genOp(t);
|
2009-05-11 23:49:29 +02:00
|
|
|
}
|
2009-05-08 23:54:15 +02:00
|
|
|
}
|
|
|
|
}
|
2009-05-11 23:49:29 +02:00
|
|
|
/* && and || output code generation */
|
|
|
|
if (a && l > 8) {
|
2009-05-12 21:48:35 +02:00
|
|
|
a = pGen->gtst(t == OP_LOGICAL_OR, a);
|
|
|
|
pGen->li(t != OP_LOGICAL_OR);
|
2009-05-14 01:24:17 +02:00
|
|
|
pGen->gjmp(5); /* jmp $ + 5 (sizeof li, FIXME for ARM) */
|
2009-05-12 21:48:35 +02:00
|
|
|
pGen->gsym(a);
|
|
|
|
pGen->li(t == OP_LOGICAL_OR);
|
2009-05-11 23:49:29 +02:00
|
|
|
}
|
2009-05-08 23:54:15 +02:00
|
|
|
}
|
|
|
|
}
|
2009-05-10 23:09:03 +02:00
|
|
|
|
2009-05-11 23:49:29 +02:00
|
|
|
void expr() {
|
|
|
|
sum(11);
|
|
|
|
}
|
2009-05-08 23:54:15 +02:00
|
|
|
|
2009-05-11 23:49:29 +02:00
|
|
|
int test_expr() {
|
|
|
|
expr();
|
2009-05-12 21:48:35 +02:00
|
|
|
return pGen->gtst(0, 0);
|
2009-05-11 23:49:29 +02:00
|
|
|
}
|
2009-05-11 04:59:24 +02:00
|
|
|
|
2009-05-11 23:49:29 +02:00
|
|
|
void block(int l) {
|
|
|
|
int a, n, t;
|
2009-05-10 23:09:03 +02:00
|
|
|
|
2009-05-11 23:49:29 +02:00
|
|
|
if (tok == TOK_IF) {
|
2009-05-10 23:09:03 +02:00
|
|
|
next();
|
2009-05-11 23:49:29 +02:00
|
|
|
skip('(');
|
2009-05-10 23:09:03 +02:00
|
|
|
a = test_expr();
|
2009-05-11 23:49:29 +02:00
|
|
|
skip(')');
|
|
|
|
block(l);
|
|
|
|
if (tok == TOK_ELSE) {
|
|
|
|
next();
|
2009-05-12 21:48:35 +02:00
|
|
|
n = pGen->gjmp(0); /* jmp */
|
|
|
|
pGen->gsym(a);
|
2009-05-11 23:49:29 +02:00
|
|
|
block(l);
|
2009-05-12 21:48:35 +02:00
|
|
|
pGen->gsym(n); /* patch else jmp */
|
2009-05-11 23:49:29 +02:00
|
|
|
} else {
|
2009-05-12 21:48:35 +02:00
|
|
|
pGen->gsym(a); /* patch if test */
|
2009-05-11 23:49:29 +02:00
|
|
|
}
|
2009-05-14 00:10:04 +02:00
|
|
|
} else if ((tok == TOK_WHILE) | (tok == TOK_FOR)) {
|
2009-05-11 23:49:29 +02:00
|
|
|
t = tok;
|
|
|
|
next();
|
|
|
|
skip('(');
|
|
|
|
if (t == TOK_WHILE) {
|
2009-05-14 01:24:17 +02:00
|
|
|
n = codeBuf.getPC(); // top of loop, target of "next" iteration
|
2009-05-10 23:09:03 +02:00
|
|
|
a = test_expr();
|
2009-05-11 23:49:29 +02:00
|
|
|
} else {
|
|
|
|
if (tok != ';')
|
|
|
|
expr();
|
|
|
|
skip(';');
|
|
|
|
n = codeBuf.getPC();
|
|
|
|
a = 0;
|
|
|
|
if (tok != ';')
|
|
|
|
a = test_expr();
|
|
|
|
skip(';');
|
|
|
|
if (tok != ')') {
|
2009-05-12 21:48:35 +02:00
|
|
|
t = pGen->gjmp(0);
|
2009-05-11 23:49:29 +02:00
|
|
|
expr();
|
2009-05-14 01:24:17 +02:00
|
|
|
pGen->gjmp(n - codeBuf.getPC() - pGen->jumpOffset());
|
2009-05-12 21:48:35 +02:00
|
|
|
pGen->gsym(t);
|
2009-05-11 23:49:29 +02:00
|
|
|
n = t + 4;
|
|
|
|
}
|
2009-05-08 23:54:15 +02:00
|
|
|
}
|
2009-05-11 23:49:29 +02:00
|
|
|
skip(')');
|
|
|
|
block((int) &a);
|
2009-05-14 01:24:17 +02:00
|
|
|
pGen->gjmp(n - codeBuf.getPC() - pGen->jumpOffset()); /* jmp */
|
2009-05-12 21:48:35 +02:00
|
|
|
pGen->gsym(a);
|
2009-05-11 23:49:29 +02:00
|
|
|
} else if (tok == '{') {
|
2009-05-10 23:09:03 +02:00
|
|
|
next();
|
2009-05-11 23:49:29 +02:00
|
|
|
/* declarations */
|
|
|
|
decl(1);
|
|
|
|
while (tok != '}')
|
|
|
|
block(l);
|
2009-05-10 23:09:03 +02:00
|
|
|
next();
|
2009-05-11 23:49:29 +02:00
|
|
|
} else {
|
|
|
|
if (tok == TOK_RETURN) {
|
|
|
|
next();
|
|
|
|
if (tok != ';')
|
|
|
|
expr();
|
2009-05-12 21:48:35 +02:00
|
|
|
rsym = pGen->gjmp(rsym); /* jmp */
|
2009-05-11 23:49:29 +02:00
|
|
|
} else if (tok == TOK_BREAK) {
|
|
|
|
next();
|
2009-05-12 21:48:35 +02:00
|
|
|
*(int *) l = pGen->gjmp(*(int *) l);
|
2009-05-11 23:49:29 +02:00
|
|
|
} else if (tok != ';')
|
|
|
|
expr();
|
|
|
|
skip(';');
|
|
|
|
}
|
2009-05-08 23:54:15 +02:00
|
|
|
}
|
|
|
|
|
2009-05-11 23:49:29 +02:00
|
|
|
/* 'l' is true if local declarations */
|
|
|
|
void decl(int l) {
|
|
|
|
int a;
|
2009-05-10 23:09:03 +02:00
|
|
|
|
2009-05-14 00:10:04 +02:00
|
|
|
while ((tok == TOK_INT) | ((tok != -1) & (!l))) {
|
2009-05-11 23:49:29 +02:00
|
|
|
if (tok == TOK_INT) {
|
2009-05-10 23:09:03 +02:00
|
|
|
next();
|
2009-05-11 23:49:29 +02:00
|
|
|
while (tok != ';') {
|
|
|
|
if (l) {
|
|
|
|
loc = loc + 4;
|
|
|
|
*(int *) tok = -loc;
|
|
|
|
} else {
|
|
|
|
*(int *) tok = glo;
|
|
|
|
glo = glo + 4;
|
|
|
|
}
|
2009-05-10 23:09:03 +02:00
|
|
|
next();
|
2009-05-11 23:49:29 +02:00
|
|
|
if (tok == ',')
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
skip(';');
|
|
|
|
} else {
|
|
|
|
/* patch forward references (XXX: do not work for function
|
|
|
|
pointers) */
|
2009-05-12 21:48:35 +02:00
|
|
|
pGen->gsym(*(int *) (tok + 4));
|
2009-05-11 23:49:29 +02:00
|
|
|
/* put function address */
|
|
|
|
*(int *) tok = codeBuf.getPC();
|
2009-05-10 23:09:03 +02:00
|
|
|
next();
|
2009-05-11 23:49:29 +02:00
|
|
|
skip('(');
|
|
|
|
a = 8;
|
2009-05-14 00:10:04 +02:00
|
|
|
int argCount = 0;
|
2009-05-11 23:49:29 +02:00
|
|
|
while (tok != ')') {
|
|
|
|
/* read param name and compute offset */
|
|
|
|
*(int *) tok = a;
|
|
|
|
a = a + 4;
|
2009-05-10 23:09:03 +02:00
|
|
|
next();
|
2009-05-11 23:49:29 +02:00
|
|
|
if (tok == ',')
|
|
|
|
next();
|
2009-05-14 00:10:04 +02:00
|
|
|
argCount++;
|
2009-05-11 23:49:29 +02:00
|
|
|
}
|
|
|
|
next(); /* skip ')' */
|
|
|
|
rsym = loc = 0;
|
2009-05-14 00:10:04 +02:00
|
|
|
a = pGen->functionEntry(argCount);
|
2009-05-11 23:49:29 +02:00
|
|
|
block(0);
|
2009-05-12 21:48:35 +02:00
|
|
|
pGen->gsym(rsym);
|
2009-05-14 00:10:04 +02:00
|
|
|
pGen->functionExit(argCount, a, loc);
|
2009-05-08 23:54:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-05-11 23:49:29 +02:00
|
|
|
|
|
|
|
void cleanup() {
|
|
|
|
if (sym_stk != 0) {
|
|
|
|
free((void*) sym_stk);
|
|
|
|
sym_stk = 0;
|
|
|
|
}
|
|
|
|
if (pGlobalBase != 0) {
|
|
|
|
free((void*) pGlobalBase);
|
|
|
|
pGlobalBase = 0;
|
|
|
|
}
|
|
|
|
if (pVarsBase != 0) {
|
|
|
|
free(pVarsBase);
|
|
|
|
pVarsBase = 0;
|
|
|
|
}
|
|
|
|
if (pGen) {
|
|
|
|
delete pGen;
|
|
|
|
pGen = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear() {
|
|
|
|
tok = 0;
|
|
|
|
tokc = 0;
|
|
|
|
tokl = 0;
|
|
|
|
ch = 0;
|
|
|
|
vars = 0;
|
|
|
|
rsym = 0;
|
|
|
|
loc = 0;
|
|
|
|
glo = 0;
|
|
|
|
sym_stk = 0;
|
|
|
|
dstk = 0;
|
|
|
|
dptr = 0;
|
|
|
|
dch = 0;
|
|
|
|
last_id = 0;
|
|
|
|
file = 0;
|
|
|
|
pGlobalBase = 0;
|
|
|
|
pVarsBase = 0;
|
|
|
|
pGen = 0;
|
|
|
|
}
|
2009-05-08 23:54:15 +02:00
|
|
|
|
2009-05-13 19:58:45 +02:00
|
|
|
void setArchitecture(const char* architecture) {
|
|
|
|
delete pGen;
|
|
|
|
pGen = 0;
|
|
|
|
|
|
|
|
if (architecture != NULL) {
|
|
|
|
if (strcmp(architecture, "arm") == 0) {
|
|
|
|
pGen = new ARMCodeGenerator();
|
|
|
|
} else if (strcmp(architecture, "x86") == 0) {
|
|
|
|
pGen = new X86CodeGenerator();
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Unknown architecture %s", architecture);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pGen == NULL) {
|
|
|
|
pGen = new ARMCodeGenerator();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-11 04:59:24 +02:00
|
|
|
public:
|
2009-05-13 19:58:45 +02:00
|
|
|
struct args {
|
|
|
|
args() {
|
|
|
|
architecture = 0;
|
|
|
|
}
|
|
|
|
const char* architecture;
|
|
|
|
};
|
|
|
|
|
2009-05-11 23:49:29 +02:00
|
|
|
compiler() {
|
|
|
|
clear();
|
|
|
|
}
|
2009-05-11 20:54:30 +02:00
|
|
|
|
2009-05-11 23:49:29 +02:00
|
|
|
~compiler() {
|
|
|
|
cleanup();
|
|
|
|
}
|
2009-05-11 20:54:30 +02:00
|
|
|
|
2009-05-13 19:58:45 +02:00
|
|
|
int compile(FILE* in, args& args) {
|
2009-05-11 23:49:29 +02:00
|
|
|
cleanup();
|
|
|
|
clear();
|
|
|
|
codeBuf.init(ALLOC_SIZE);
|
2009-05-13 19:58:45 +02:00
|
|
|
setArchitecture(args.architecture);
|
2009-05-11 23:49:29 +02:00
|
|
|
pGen->init(&codeBuf);
|
|
|
|
file = in;
|
|
|
|
sym_stk = (int) calloc(1, ALLOC_SIZE);
|
|
|
|
dstk = (int) strcpy((char*) sym_stk,
|
|
|
|
" int if else while break return for define main ")
|
|
|
|
+ TOK_STR_SIZE;
|
|
|
|
pGlobalBase = calloc(1, ALLOC_SIZE);
|
|
|
|
glo = (int) pGlobalBase;
|
|
|
|
pVarsBase = calloc(1, ALLOC_SIZE);
|
|
|
|
vars = (int) pVarsBase;
|
|
|
|
inp();
|
|
|
|
next();
|
|
|
|
decl(0);
|
2009-05-14 00:10:04 +02:00
|
|
|
pGen->finishCompile();
|
2009-05-11 23:49:29 +02:00
|
|
|
return 0;
|
2009-05-08 23:54:15 +02:00
|
|
|
}
|
2009-05-11 20:54:30 +02:00
|
|
|
|
2009-05-11 23:49:29 +02:00
|
|
|
int run(int argc, char** argv) {
|
|
|
|
typedef int (*mainPtr)(int argc, char** argv);
|
|
|
|
mainPtr aMain = (mainPtr) *(int*) (vars + TOK_MAIN);
|
|
|
|
if (!aMain) {
|
|
|
|
fprintf(stderr, "Could not find function \"main\".\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return aMain(argc, argv);
|
|
|
|
}
|
2009-05-11 04:59:24 +02:00
|
|
|
|
2009-05-11 23:49:29 +02:00
|
|
|
int dump(FILE* out) {
|
|
|
|
fwrite(codeBuf.getBase(), 1, codeBuf.getSize(), out);
|
|
|
|
return 0;
|
|
|
|
}
|
2009-05-11 04:59:24 +02:00
|
|
|
|
2009-05-14 01:24:17 +02:00
|
|
|
int disassemble(FILE* out) {
|
|
|
|
return pGen->disassemble(out);
|
|
|
|
}
|
|
|
|
|
2009-05-11 23:49:29 +02:00
|
|
|
};
|
2009-05-11 20:54:30 +02:00
|
|
|
|
2009-05-12 21:48:35 +02:00
|
|
|
const char* compiler::operatorChars =
|
|
|
|
"++--*@/@%@+@-@<<>><=>=<@>@==!=&&||&@^@|@~@!@";
|
|
|
|
|
|
|
|
const char compiler::operatorLevel[] =
|
|
|
|
{11, 11, 1, 1, 1, 2, 2, 3, 3, 4, 4, 4, 4,
|
|
|
|
5, 5, /* ==, != */
|
|
|
|
9, 10, /* &&, || */
|
|
|
|
6, 7, 8, /* & ^ | */
|
|
|
|
2, 2 /* ~ ! */
|
|
|
|
};
|
|
|
|
|
2009-05-14 01:24:17 +02:00
|
|
|
FILE* compiler::ARMCodeGenerator::disasmOut;
|
|
|
|
|
2009-05-12 21:48:35 +02:00
|
|
|
const int compiler::X86CodeGenerator::operatorHelper[] = {
|
|
|
|
0x1, // ++
|
|
|
|
0xff, // --
|
|
|
|
0xc1af0f, // *
|
|
|
|
0xf9f79991, // /
|
|
|
|
0xf9f79991, // % (With manual assist to swap results)
|
|
|
|
0xc801, // +
|
|
|
|
0xd8f7c829, // -
|
|
|
|
0xe0d391, // <<
|
|
|
|
0xf8d391, // >>
|
|
|
|
0xe, // <=
|
|
|
|
0xd, // >=
|
|
|
|
0xc, // <
|
|
|
|
0xf, // >
|
|
|
|
0x4, // ==
|
|
|
|
0x5, // !=
|
|
|
|
0x0, // &&
|
|
|
|
0x1, // ||
|
|
|
|
0xc821, // &
|
|
|
|
0xc831, // ^
|
|
|
|
0xc809, // |
|
|
|
|
0xd0f7, // ~
|
|
|
|
0x4 // !
|
|
|
|
};
|
|
|
|
|
2009-05-11 20:54:30 +02:00
|
|
|
} // namespace acc
|
|
|
|
|
2009-05-14 00:10:04 +02:00
|
|
|
// This is a separate function so it can easily be set by breakpoint in gdb.
|
|
|
|
int run(acc::compiler& c, int argc, char** argv) {
|
|
|
|
return c.run(argc, argv);
|
|
|
|
}
|
|
|
|
|
2009-05-11 04:59:24 +02:00
|
|
|
int main(int argc, char** argv) {
|
2009-05-13 19:58:45 +02:00
|
|
|
bool doDump = false;
|
2009-05-14 01:24:17 +02:00
|
|
|
bool doDisassemble = false;
|
2009-05-11 20:54:30 +02:00
|
|
|
const char* inFile = NULL;
|
|
|
|
const char* outFile = NULL;
|
2009-05-13 19:58:45 +02:00
|
|
|
const char* architecture = "arm";
|
2009-05-11 20:54:30 +02:00
|
|
|
int i;
|
2009-05-11 23:49:29 +02:00
|
|
|
for (i = 1; i < argc; i++) {
|
2009-05-11 20:54:30 +02:00
|
|
|
char* arg = argv[i];
|
|
|
|
if (arg[0] == '-') {
|
|
|
|
switch (arg[1]) {
|
2009-05-13 19:58:45 +02:00
|
|
|
case 'a':
|
|
|
|
if (i + 1 >= argc) {
|
|
|
|
fprintf(stderr, "Expected architecture after -a\n");
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
architecture = argv[i+1];
|
|
|
|
i += 1;
|
|
|
|
break;
|
|
|
|
case 'd':
|
2009-05-11 20:54:30 +02:00
|
|
|
if (i + 1 >= argc) {
|
2009-05-13 19:58:45 +02:00
|
|
|
fprintf(stderr, "Expected filename after -d\n");
|
2009-05-11 20:54:30 +02:00
|
|
|
return 2;
|
|
|
|
}
|
2009-05-13 19:58:45 +02:00
|
|
|
doDump = true;
|
2009-05-11 20:54:30 +02:00
|
|
|
outFile = argv[i + 1];
|
|
|
|
i += 1;
|
|
|
|
break;
|
2009-05-14 01:24:17 +02:00
|
|
|
case 'S':
|
|
|
|
doDisassemble = true;
|
|
|
|
break;
|
2009-05-11 20:54:30 +02:00
|
|
|
default:
|
|
|
|
fprintf(stderr, "Unrecognized flag %s\n", arg);
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
} else if (inFile == NULL) {
|
|
|
|
inFile = arg;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE* in = stdin;
|
|
|
|
if (inFile) {
|
|
|
|
in = fopen(inFile, "r");
|
2009-05-11 23:49:29 +02:00
|
|
|
if (!in) {
|
2009-05-11 20:54:30 +02:00
|
|
|
fprintf(stderr, "Could not open input file %s\n", inFile);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
acc::compiler compiler;
|
2009-05-13 19:58:45 +02:00
|
|
|
acc::compiler::args args;
|
|
|
|
args.architecture = architecture;
|
|
|
|
int compileResult = compiler.compile(in, args);
|
2009-05-11 20:54:30 +02:00
|
|
|
if (in != stdin) {
|
|
|
|
fclose(in);
|
|
|
|
}
|
|
|
|
if (compileResult) {
|
|
|
|
fprintf(stderr, "Compile failed: %d\n", compileResult);
|
|
|
|
return 6;
|
|
|
|
}
|
2009-05-14 01:24:17 +02:00
|
|
|
if (doDisassemble) {
|
|
|
|
compiler.disassemble(stderr);
|
|
|
|
}
|
2009-05-13 19:58:45 +02:00
|
|
|
if (doDump) {
|
2009-05-11 20:54:30 +02:00
|
|
|
FILE* save = fopen(outFile, "w");
|
2009-05-11 23:49:29 +02:00
|
|
|
if (!save) {
|
2009-05-11 20:54:30 +02:00
|
|
|
fprintf(stderr, "Could not open output file %s\n", outFile);
|
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
compiler.dump(save);
|
|
|
|
fclose(save);
|
|
|
|
} else {
|
2009-05-12 21:48:35 +02:00
|
|
|
fprintf(stderr, "Executing compiled code:\n");
|
2009-05-11 23:49:29 +02:00
|
|
|
int codeArgc = argc - i + 1;
|
|
|
|
char** codeArgv = argv + i - 1;
|
2009-05-11 20:54:30 +02:00
|
|
|
codeArgv[0] = (char*) (inFile ? inFile : "stdin");
|
2009-05-14 00:10:04 +02:00
|
|
|
int result = run(compiler, codeArgc, codeArgv);
|
2009-05-13 19:58:45 +02:00
|
|
|
fprintf(stderr, "result: %d\n", result);
|
|
|
|
return result;
|
2009-05-11 20:54:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|