2009-05-20 02:12:17 +02:00
|
|
|
/*
|
|
|
|
* Android "Almost" C Compiler.
|
|
|
|
* This is a compiler for a small subset of the C language, intended for use
|
|
|
|
* in scripting environments where speed and memory footprint are important.
|
|
|
|
*
|
|
|
|
* This code is based upon the "unobfuscated" version of the
|
2009-05-22 21:06:27 +02:00
|
|
|
* Obfuscated Tiny C compiler, see the file LICENSE for details.
|
2009-05-20 02:12:17 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2009-05-11 04:59:24 +02:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <dlfcn.h>
|
2009-06-10 00:53:47 +02:00
|
|
|
#include <errno.h>
|
2009-05-10 23:09:03 +02:00
|
|
|
#include <stdarg.h>
|
2009-05-20 21:12:06 +02:00
|
|
|
#include <stdint.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-06-08 23:34:26 +02:00
|
|
|
#include <cutils/hashmap.h>
|
2009-05-10 23:09:03 +02:00
|
|
|
|
2009-06-10 00:53:47 +02:00
|
|
|
#if defined(__i386__)
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#endif
|
|
|
|
|
2009-05-14 00:10:04 +02:00
|
|
|
#if defined(__arm__)
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2009-05-20 02:12:17 +02:00
|
|
|
#if defined(__arm__)
|
|
|
|
#define DEFAULT_ARM_CODEGEN
|
2009-05-20 21:12:06 +02:00
|
|
|
#define PROVIDE_ARM_CODEGEN
|
2009-05-20 02:12:17 +02:00
|
|
|
#elif defined(__i386__)
|
|
|
|
#define DEFAULT_X86_CODEGEN
|
2009-05-20 21:12:06 +02:00
|
|
|
#define PROVIDE_X86_CODEGEN
|
2009-05-20 02:12:17 +02:00
|
|
|
#elif defined(__x86_64__)
|
|
|
|
#define DEFAULT_X64_CODEGEN
|
2009-05-20 21:12:06 +02:00
|
|
|
#define PROVIDE_X64_CODEGEN
|
2009-05-20 02:12:17 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef PROVIDE_ARM_CODEGEN
|
2009-05-14 01:24:17 +02:00
|
|
|
#include "disassem.h"
|
2009-05-20 02:12:17 +02:00
|
|
|
#endif
|
2009-05-14 01:24:17 +02:00
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
#include <acc/acc.h>
|
|
|
|
|
2009-05-27 21:25:55 +02:00
|
|
|
#define LOG_API(...) do {} while(0)
|
|
|
|
// #define LOG_API(...) fprintf (stderr, __VA_ARGS__)
|
|
|
|
// #define ENABLE_ARM_DISASSEMBLY
|
|
|
|
|
2009-06-12 06:12:23 +02:00
|
|
|
// #define PROVIDE_TRACE_CODEGEN
|
|
|
|
|
2009-05-11 20:54:30 +02:00
|
|
|
namespace acc {
|
|
|
|
|
2009-05-29 22:53:44 +02:00
|
|
|
class ErrorSink {
|
|
|
|
public:
|
|
|
|
void error(const char *fmt, ...) {
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
verror(fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void verror(const char* fmt, va_list ap) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Compiler : public ErrorSink {
|
2009-05-11 23:49:29 +02:00
|
|
|
class CodeBuf {
|
2009-05-29 02:15:32 +02:00
|
|
|
char* ind; // Output code pointer
|
2009-05-11 23:49:29 +02:00
|
|
|
char* pProgramBase;
|
2009-05-29 22:53:44 +02:00
|
|
|
ErrorSink* mErrorSink;
|
|
|
|
int mSize;
|
2009-06-11 19:53:51 +02:00
|
|
|
bool mOverflowed;
|
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-06-11 19:53:51 +02:00
|
|
|
bool check(int n) {
|
2009-05-29 22:53:44 +02:00
|
|
|
int newSize = ind - pProgramBase + n;
|
2009-06-11 19:53:51 +02:00
|
|
|
bool overflow = newSize > mSize;
|
|
|
|
if (overflow && !mOverflowed) {
|
|
|
|
mOverflowed = true;
|
2009-05-29 22:53:44 +02:00
|
|
|
if (mErrorSink) {
|
|
|
|
mErrorSink->error("Code too large: %d bytes", newSize);
|
|
|
|
}
|
|
|
|
}
|
2009-06-11 19:53:51 +02:00
|
|
|
return overflow;
|
2009-05-29 22:53:44 +02:00
|
|
|
}
|
|
|
|
|
2009-05-11 23:49:29 +02:00
|
|
|
public:
|
|
|
|
CodeBuf() {
|
|
|
|
pProgramBase = 0;
|
|
|
|
ind = 0;
|
2009-05-29 22:53:44 +02:00
|
|
|
mErrorSink = 0;
|
|
|
|
mSize = 0;
|
2009-06-11 19:53:51 +02:00
|
|
|
mOverflowed = false;
|
2009-05-11 23:49:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
~CodeBuf() {
|
|
|
|
release();
|
|
|
|
}
|
2009-05-08 23:54:15 +02:00
|
|
|
|
2009-05-11 23:49:29 +02:00
|
|
|
void init(int size) {
|
|
|
|
release();
|
2009-05-29 22:53:44 +02:00
|
|
|
mSize = size;
|
2009-05-11 23:49:29 +02:00
|
|
|
pProgramBase = (char*) calloc(1, size);
|
|
|
|
ind = pProgramBase;
|
|
|
|
}
|
2009-05-10 23:09:03 +02:00
|
|
|
|
2009-05-29 22:53:44 +02:00
|
|
|
void setErrorSink(ErrorSink* pErrorSink) {
|
|
|
|
mErrorSink = pErrorSink;
|
|
|
|
}
|
|
|
|
|
2009-05-14 00:10:04 +02:00
|
|
|
int o4(int n) {
|
2009-06-11 19:53:51 +02:00
|
|
|
if(check(4)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2009-05-20 21:12:06 +02:00
|
|
|
intptr_t result = (intptr_t) ind;
|
2009-05-14 00:10:04 +02:00
|
|
|
* (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) {
|
2009-06-11 19:53:51 +02:00
|
|
|
if(check(1)) {
|
|
|
|
return;
|
|
|
|
}
|
2009-05-11 23:49:29 +02:00
|
|
|
*ind++ = n;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void* getBase() {
|
|
|
|
return (void*) pProgramBase;
|
|
|
|
}
|
|
|
|
|
2009-05-20 21:12:06 +02:00
|
|
|
intptr_t getSize() {
|
2009-05-11 23:49:29 +02:00
|
|
|
return ind - pProgramBase;
|
|
|
|
}
|
|
|
|
|
2009-05-20 21:12:06 +02:00
|
|
|
intptr_t getPC() {
|
|
|
|
return (intptr_t) ind;
|
2009-05-11 23:49:29 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
/**
|
|
|
|
* A code generator creates an in-memory program, generating the code on
|
|
|
|
* the fly. There is one code generator implementation for each supported
|
|
|
|
* architecture.
|
|
|
|
*
|
|
|
|
* The code generator implements the following abstract machine:
|
|
|
|
* R0 - the main accumulator.
|
|
|
|
* R1 - the secondary accumulator.
|
|
|
|
* FP - a frame pointer for accessing function arguments and local
|
|
|
|
* variables.
|
|
|
|
* SP - a stack pointer for storing intermediate results while evaluating
|
|
|
|
* expressions. The stack pointer grows downwards.
|
|
|
|
*
|
|
|
|
* The function calling convention is that all arguments are placed on the
|
|
|
|
* stack such that the first argument has the lowest address.
|
|
|
|
* After the call, the result is in R0. The caller is responsible for
|
|
|
|
* removing the arguments from the stack.
|
|
|
|
* The R0 and R1 registers are not saved across function calls. The
|
|
|
|
* FP and SP registers are saved.
|
|
|
|
*/
|
|
|
|
|
2009-05-11 23:49:29 +02:00
|
|
|
class CodeGenerator {
|
|
|
|
public:
|
2009-05-29 22:53:44 +02:00
|
|
|
CodeGenerator() {
|
|
|
|
mErrorSink = 0;
|
|
|
|
pCodeBuf = 0;
|
|
|
|
}
|
2009-05-11 23:49:29 +02:00
|
|
|
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-29 22:53:44 +02:00
|
|
|
pCodeBuf->setErrorSink(mErrorSink);
|
|
|
|
}
|
|
|
|
|
2009-06-12 06:12:23 +02:00
|
|
|
virtual void setErrorSink(ErrorSink* pErrorSink) {
|
2009-05-29 22:53:44 +02:00
|
|
|
mErrorSink = pErrorSink;
|
|
|
|
if (pCodeBuf) {
|
|
|
|
pCodeBuf->setErrorSink(mErrorSink);
|
|
|
|
}
|
2009-05-11 23:49:29 +02:00
|
|
|
}
|
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
/* Emit a function prolog.
|
|
|
|
* argCount is the number of arguments.
|
|
|
|
* Save the old value of the FP.
|
|
|
|
* Set the new value of the FP.
|
|
|
|
* Convert from the native platform calling convention to
|
|
|
|
* our stack-based calling convention. This may require
|
|
|
|
* pushing arguments from registers to the stack.
|
|
|
|
* Allocate "N" bytes of stack space. N isn't known yet, so
|
|
|
|
* just emit the instructions for adjusting the stack, and return
|
|
|
|
* the address to patch up. The patching will be done in
|
|
|
|
* functionExit().
|
|
|
|
* returns address to patch with local variable size.
|
2009-05-13 19:58:45 +02:00
|
|
|
*/
|
2009-05-14 00:10:04 +02:00
|
|
|
virtual int functionEntry(int argCount) = 0;
|
2009-05-13 19:58:45 +02:00
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
/* Emit a function epilog.
|
|
|
|
* Restore the old SP and FP register values.
|
|
|
|
* Return to the calling function.
|
|
|
|
* argCount - the number of arguments to the function.
|
|
|
|
* localVariableAddress - returned from functionEntry()
|
|
|
|
* localVariableSize - the size in bytes of the local variables.
|
|
|
|
*/
|
|
|
|
virtual void functionExit(int argCount, int localVariableAddress,
|
|
|
|
int localVariableSize) = 0;
|
2009-05-13 19:58:45 +02:00
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
/* load immediate value to R0 */
|
2009-05-14 00:10:04 +02:00
|
|
|
virtual void li(int t) = 0;
|
2009-05-13 19:58:45 +02:00
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
/* Jump to a target, and return the address of the word that
|
|
|
|
* holds the target data, in case it needs to be fixed up later.
|
|
|
|
*/
|
2009-05-13 19:58:45 +02:00
|
|
|
virtual int gjmp(int t) = 0;
|
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
/* Test R0 and jump to a target if the test succeeds.
|
|
|
|
* l = 0: je, l == 1: jne
|
|
|
|
* Return the address of the word that holds the targed data, in
|
|
|
|
* case it needs to be fixed up later.
|
|
|
|
*/
|
2009-05-13 19:58:45 +02:00
|
|
|
virtual int gtst(bool l, int t) = 0;
|
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
/* Compare R1 against R0, and store the boolean result in R0.
|
|
|
|
* op specifies the comparison.
|
|
|
|
*/
|
2009-05-13 19:58:45 +02:00
|
|
|
virtual void gcmp(int op) = 0;
|
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
/* Perform the arithmetic op specified by op. R1 is the
|
|
|
|
* left argument, R0 is the right argument.
|
|
|
|
*/
|
2009-05-14 00:10:04 +02:00
|
|
|
virtual void genOp(int op) = 0;
|
2009-05-13 19:58:45 +02:00
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
/* Set R1 to 0.
|
|
|
|
*/
|
|
|
|
virtual void clearR1() = 0;
|
2009-05-13 19:58:45 +02:00
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
/* Push R0 onto the stack.
|
|
|
|
*/
|
|
|
|
virtual void pushR0() = 0;
|
2009-05-13 19:58:45 +02:00
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
/* Pop R1 off of the stack.
|
|
|
|
*/
|
|
|
|
virtual void popR1() = 0;
|
2009-05-13 19:58:45 +02:00
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
/* Store R0 to the address stored in R1.
|
|
|
|
* isInt is true if a whole 4-byte integer value
|
|
|
|
* should be stored, otherwise a 1-byte character
|
|
|
|
* value should be stored.
|
|
|
|
*/
|
|
|
|
virtual void storeR0ToR1(bool isInt) = 0;
|
2009-05-13 19:58:45 +02:00
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
/* Load R0 from the address stored in R0.
|
|
|
|
* isInt is true if a whole 4-byte integer value
|
|
|
|
* should be loaded, otherwise a 1-byte character
|
|
|
|
* value should be loaded.
|
|
|
|
*/
|
|
|
|
virtual void loadR0FromR0(bool isInt) = 0;
|
2009-05-13 19:58:45 +02:00
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
/* Load the absolute address of a variable to R0.
|
|
|
|
* If ea <= LOCAL, then this is a local variable, or an
|
|
|
|
* argument, addressed relative to FP.
|
|
|
|
* else it is an absolute global address.
|
|
|
|
*/
|
|
|
|
virtual void leaR0(int ea) = 0;
|
2009-05-13 19:58:45 +02:00
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
/* Store R0 to a variable.
|
|
|
|
* If ea <= LOCAL, then this is a local variable, or an
|
|
|
|
* argument, addressed relative to FP.
|
|
|
|
* else it is an absolute global address.
|
|
|
|
*/
|
|
|
|
virtual void storeR0(int ea) = 0;
|
|
|
|
|
|
|
|
/* load R0 from a variable.
|
|
|
|
* If ea <= LOCAL, then this is a local variable, or an
|
|
|
|
* argument, addressed relative to FP.
|
|
|
|
* else it is an absolute global address.
|
|
|
|
* If isIncDec is true, then the stored variable's value
|
|
|
|
* should be post-incremented or post-decremented, based
|
|
|
|
* on the value of op.
|
|
|
|
*/
|
|
|
|
virtual void loadR0(int ea, bool isIncDec, int op) = 0;
|
2009-05-13 19:58:45 +02:00
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
/* Emit code to adjust the stack for a function call. Return the
|
|
|
|
* label for the address of the instruction that adjusts the
|
|
|
|
* stack size. This will be passed as argument "a" to
|
|
|
|
* endFunctionCallArguments.
|
|
|
|
*/
|
2009-05-14 20:38:49 +02:00
|
|
|
virtual int beginFunctionCallArguments() = 0;
|
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
/* Emit code to store R0 to the stack at byte offset l.
|
|
|
|
*/
|
|
|
|
virtual void storeR0ToArg(int l) = 0;
|
|
|
|
|
|
|
|
/* Patch the function call preamble.
|
|
|
|
* a is the address returned from beginFunctionCallArguments
|
|
|
|
* l is the number of bytes the arguments took on the stack.
|
|
|
|
* Typically you would also emit code to convert the argument
|
|
|
|
* list into whatever the native function calling convention is.
|
|
|
|
* On ARM for example you would pop the first 5 arguments into
|
|
|
|
* R0..R4
|
|
|
|
*/
|
2009-05-14 20:38:49 +02:00
|
|
|
virtual void endFunctionCallArguments(int a, int l) = 0;
|
2009-05-13 19:58:45 +02:00
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
/* Emit a call to an unknown function. The argument "symbol" needs to
|
|
|
|
* be stored in the location where the address should go. It forms
|
|
|
|
* a chain. The address will be patched later.
|
|
|
|
* Return the address of the word that has to be patched.
|
|
|
|
*/
|
2009-05-13 19:58:45 +02:00
|
|
|
virtual int callForward(int symbol) = 0;
|
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
/* Call a function using PC-relative addressing. t is the PC-relative
|
|
|
|
* address of the function. It has already been adjusted for the
|
|
|
|
* architectural jump offset, so just store it as-is.
|
|
|
|
*/
|
2009-05-13 19:58:45 +02:00
|
|
|
virtual void callRelative(int t) = 0;
|
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
/* Call a function pointer. L is the number of bytes the arguments
|
|
|
|
* take on the stack. The address of the function is stored at
|
|
|
|
* location SP + l.
|
|
|
|
*/
|
2009-05-13 19:58:45 +02:00
|
|
|
virtual void callIndirect(int l) = 0;
|
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
/* Adjust SP after returning from a function call. l is the
|
|
|
|
* number of bytes of arguments stored on the stack. isIndirect
|
|
|
|
* is true if this was an indirect call. (In which case the
|
|
|
|
* address of the function is stored at location SP + l.)
|
|
|
|
*/
|
2009-05-15 23:31:47 +02:00
|
|
|
virtual void adjustStackAfterCall(int l, bool isIndirect) = 0;
|
2009-05-13 19:58:45 +02:00
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
/* Print a disassembly of the assembled code to out. Return
|
|
|
|
* non-zero if there is an error.
|
|
|
|
*/
|
2009-05-14 01:24:17 +02:00
|
|
|
virtual int disassemble(FILE* out) = 0;
|
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
/* Generate a symbol at the current PC. t is the head of a
|
|
|
|
* linked list of addresses to patch.
|
|
|
|
*/
|
2009-05-20 02:12:17 +02:00
|
|
|
virtual void gsym(int t) = 0;
|
2009-05-11 23:49:29 +02:00
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
/*
|
|
|
|
* Do any cleanup work required at the end of a compile.
|
|
|
|
* For example, an instruction cache might need to be
|
|
|
|
* invalidated.
|
|
|
|
* Return non-zero if there is an error.
|
|
|
|
*/
|
|
|
|
virtual int finishCompile() = 0;
|
2009-05-14 00:10:04 +02:00
|
|
|
|
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:
|
|
|
|
/*
|
|
|
|
* Output a byte. Handles all values, 0..ff.
|
|
|
|
*/
|
|
|
|
void ob(int n) {
|
|
|
|
pCodeBuf->ob(n);
|
|
|
|
}
|
|
|
|
|
2009-05-20 21:12:06 +02:00
|
|
|
intptr_t o4(int data) {
|
2009-05-20 02:12:17 +02:00
|
|
|
return pCodeBuf->o4(data);
|
2009-05-11 23:49:29 +02:00
|
|
|
}
|
|
|
|
|
2009-05-20 21:12:06 +02:00
|
|
|
intptr_t getBase() {
|
|
|
|
return (intptr_t) pCodeBuf->getBase();
|
2009-05-14 01:24:17 +02:00
|
|
|
}
|
|
|
|
|
2009-05-20 21:12:06 +02:00
|
|
|
intptr_t getPC() {
|
2009-05-11 23:49:29 +02:00
|
|
|
return pCodeBuf->getPC();
|
|
|
|
}
|
2009-05-22 21:06:27 +02:00
|
|
|
|
|
|
|
intptr_t getSize() {
|
|
|
|
return pCodeBuf->getSize();
|
|
|
|
}
|
2009-05-29 22:53:44 +02:00
|
|
|
|
|
|
|
void error(const char* fmt,...) {
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
mErrorSink->verror(fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
2009-05-11 23:49:29 +02:00
|
|
|
private:
|
|
|
|
CodeBuf* pCodeBuf;
|
2009-05-29 22:53:44 +02:00
|
|
|
ErrorSink* mErrorSink;
|
2009-05-11 23:49:29 +02:00
|
|
|
};
|
|
|
|
|
2009-05-20 02:12:17 +02:00
|
|
|
#ifdef PROVIDE_ARM_CODEGEN
|
|
|
|
|
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) {
|
2009-06-05 04:56:13 +02:00
|
|
|
LOG_API("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) {
|
2009-05-27 21:25:55 +02:00
|
|
|
LOG_API("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-27 21:25:55 +02:00
|
|
|
LOG_API("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) {
|
2009-05-27 21:25:55 +02:00
|
|
|
LOG_API("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) {
|
2009-05-27 21:25:55 +02:00
|
|
|
LOG_API("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) {
|
2009-05-27 21:25:55 +02:00
|
|
|
LOG_API("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-27 21:25:55 +02:00
|
|
|
LOG_API("genOp(%d);\n", op);
|
2009-05-14 20:38:49 +02:00
|
|
|
switch(op) {
|
|
|
|
case OP_MUL:
|
|
|
|
o4(0x0E0000091); // mul r0,r1,r0
|
|
|
|
break;
|
2009-05-16 00:12:38 +02:00
|
|
|
case OP_DIV:
|
|
|
|
callRuntime(runtime_DIV);
|
|
|
|
break;
|
|
|
|
case OP_MOD:
|
|
|
|
callRuntime(runtime_MOD);
|
|
|
|
break;
|
2009-05-14 20:38:49 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
virtual void clearR1() {
|
2009-05-27 21:25:55 +02:00
|
|
|
LOG_API("clearR1();\n");
|
2009-05-14 20:38:49 +02:00
|
|
|
o4(0xE3A01000); // mov r1, #0
|
2009-05-13 19:58:45 +02:00
|
|
|
}
|
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
virtual void pushR0() {
|
2009-05-27 21:25:55 +02:00
|
|
|
LOG_API("pushR0();\n");
|
2009-05-14 20:38:49 +02:00
|
|
|
o4(0xE92D0001); // stmfd sp!,{r0}
|
2009-05-13 19:58:45 +02:00
|
|
|
}
|
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
virtual void popR1() {
|
2009-05-27 21:25:55 +02:00
|
|
|
LOG_API("popR1();\n");
|
2009-05-14 20:38:49 +02:00
|
|
|
o4(0xE8BD0002); // ldmfd sp!,{r1}
|
2009-05-13 19:58:45 +02:00
|
|
|
}
|
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
virtual void storeR0ToR1(bool isInt) {
|
2009-05-27 21:25:55 +02:00
|
|
|
LOG_API("storeR0ToR1(%d);\n", isInt);
|
2009-05-15 04:35:31 +02:00
|
|
|
if (isInt) {
|
|
|
|
o4(0xE5810000); // str r0, [r1]
|
|
|
|
} else {
|
|
|
|
o4(0xE5C10000); // strb r0, [r1]
|
|
|
|
}
|
2009-05-13 19:58:45 +02:00
|
|
|
}
|
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
virtual void loadR0FromR0(bool isInt) {
|
2009-05-27 21:25:55 +02:00
|
|
|
LOG_API("loadR0FromR0(%d);\n", isInt);
|
2009-05-13 19:58:45 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
virtual void leaR0(int ea) {
|
2009-05-27 21:25:55 +02:00
|
|
|
LOG_API("leaR0(%d);\n", ea);
|
2009-05-15 22:30:00 +02:00
|
|
|
if (ea < LOCAL) {
|
|
|
|
// Local, fp relative
|
|
|
|
if (ea < -1023 || ea > 1023 || ((ea & 3) != 0)) {
|
|
|
|
error("Offset out of range: %08x", ea);
|
|
|
|
}
|
|
|
|
if (ea < 0) {
|
|
|
|
o4(0xE24B0F00 | (0xff & ((-ea) >> 2))); // sub r0, fp, #ea
|
|
|
|
} else {
|
|
|
|
o4(0xE28B0F00 | (0xff & (ea >> 2))); // add r0, fp, #ea
|
|
|
|
}
|
2009-05-15 04:35:31 +02:00
|
|
|
} else {
|
2009-05-15 22:30:00 +02:00
|
|
|
// Global, absolute.
|
|
|
|
o4(0xE59F0000); // ldr r0, .L1
|
|
|
|
o4(0xEA000000); // b .L99
|
|
|
|
o4(ea); // .L1: .word 0
|
|
|
|
// .L99:
|
2009-05-15 04:35:31 +02:00
|
|
|
}
|
2009-05-13 19:58:45 +02:00
|
|
|
}
|
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
virtual void storeR0(int ea) {
|
2009-05-27 21:25:55 +02:00
|
|
|
LOG_API("storeR0(%d);\n", ea);
|
2009-05-15 22:30:00 +02:00
|
|
|
if (ea < LOCAL) {
|
|
|
|
// Local, fp relative
|
|
|
|
if (ea < -4095 || ea > 4095) {
|
|
|
|
error("Offset out of range: %08x", ea);
|
|
|
|
}
|
|
|
|
if (ea < 0) {
|
|
|
|
o4(0xE50B0000 | (0xfff & (-ea))); // str r0, [fp,#-ea]
|
|
|
|
} else {
|
|
|
|
o4(0xE58B0000 | (0xfff & ea)); // str r0, [fp,#ea]
|
|
|
|
}
|
|
|
|
} else{
|
|
|
|
// Global, absolute
|
|
|
|
o4(0xE59F1000); // ldr r1, .L1
|
|
|
|
o4(0xEA000000); // b .L99
|
|
|
|
o4(ea); // .L1: .word 0
|
|
|
|
o4(0xE5810000); // .L99: str r0, [r1]
|
2009-05-15 00:42:26 +02:00
|
|
|
}
|
2009-05-13 19:58:45 +02:00
|
|
|
}
|
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
virtual void loadR0(int ea, bool isIncDec, int op) {
|
2009-05-27 21:25:55 +02:00
|
|
|
LOG_API("loadR0(%d, %d, %d);\n", ea, isIncDec, op);
|
2009-05-15 22:30:00 +02:00
|
|
|
if (ea < LOCAL) {
|
|
|
|
// Local, fp relative
|
|
|
|
if (ea < -4095 || ea > 4095) {
|
|
|
|
error("Offset out of range: %08x", ea);
|
|
|
|
}
|
|
|
|
if (ea < 0) {
|
|
|
|
o4(0xE51B0000 | (0xfff & (-ea))); // ldr r0, [fp,#-ea]
|
|
|
|
} else {
|
|
|
|
o4(0xE59B0000 | (0xfff & ea)); // ldr r0, [fp,#ea]
|
|
|
|
}
|
2009-05-15 00:42:26 +02:00
|
|
|
} else {
|
2009-05-15 22:30:00 +02:00
|
|
|
// Global, absolute
|
|
|
|
o4(0xE59F2000); // ldr r2, .L1
|
|
|
|
o4(0xEA000000); // b .L99
|
|
|
|
o4(ea); // .L1: .word ea
|
|
|
|
o4(0xE5920000); // .L99: ldr r0, [r2]
|
2009-05-15 00:42:26 +02:00
|
|
|
}
|
2009-05-13 19:58:45 +02:00
|
|
|
|
2009-05-15 22:30:00 +02:00
|
|
|
if (isIncDec) {
|
|
|
|
switch (op) {
|
|
|
|
case OP_INCREMENT:
|
|
|
|
o4(0xE2801001); // add r1, r0, #1
|
|
|
|
break;
|
|
|
|
case OP_DECREMENT:
|
|
|
|
o4(0xE2401001); // sub r1, r0, #1
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error("unknown opcode: %d", op);
|
|
|
|
}
|
|
|
|
if (ea < LOCAL) {
|
|
|
|
// Local, fp relative
|
|
|
|
// Don't need range check, was already checked above
|
|
|
|
if (ea < 0) {
|
|
|
|
o4(0xE50B1000 | (0xfff & (-ea))); // str r1, [fp,#-ea]
|
|
|
|
} else {
|
|
|
|
o4(0xE58B1000 | (0xfff & ea)); // str r1, [fp,#ea]
|
|
|
|
}
|
|
|
|
} else{
|
|
|
|
// Global, absolute
|
|
|
|
// r2 is already set up from before.
|
|
|
|
o4(0xE5821000); // str r1, [r2]
|
|
|
|
}
|
2009-05-15 04:35:31 +02:00
|
|
|
}
|
2009-05-13 19:58:45 +02:00
|
|
|
}
|
|
|
|
|
2009-05-14 20:38:49 +02:00
|
|
|
virtual int beginFunctionCallArguments() {
|
2009-05-27 21:25:55 +02:00
|
|
|
LOG_API("beginFunctionCallArguments();\n");
|
2009-05-14 20:38:49 +02:00
|
|
|
return o4(0xE24DDF00); // Placeholder
|
|
|
|
}
|
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
virtual void storeR0ToArg(int l) {
|
2009-05-27 21:25:55 +02:00
|
|
|
LOG_API("storeR0ToArg(%d);\n", l);
|
2009-05-15 23:31:47 +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-14 20:38:49 +02:00
|
|
|
virtual void endFunctionCallArguments(int a, int l) {
|
2009-05-27 21:25:55 +02:00
|
|
|
LOG_API("endFunctionCallArguments(0x%08x, %d);\n", a, 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*) 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 int callForward(int symbol) {
|
2009-05-27 21:25:55 +02:00
|
|
|
LOG_API("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) {
|
2009-05-27 21:25:55 +02:00
|
|
|
LOG_API("callRelative(%d);\n", t);
|
2009-05-14 20:38:49 +02:00
|
|
|
int abs = t + getPC() + jumpOffset();
|
2009-05-29 22:53:44 +02:00
|
|
|
LOG_API("abs=%d (0x%08x)\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
|
2009-05-15 04:35:31 +02:00
|
|
|
o4(t - 12); // .L1: .word 0
|
2009-05-14 20:38:49 +02:00
|
|
|
o4(0xE08CC00F); // .L99: add r12,pc
|
|
|
|
o4(0xE12FFF3C); // blx r12
|
|
|
|
}
|
2009-05-13 19:58:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void callIndirect(int l) {
|
2009-05-27 21:25:55 +02:00
|
|
|
LOG_API("callIndirect(%d);\n", l);
|
2009-05-15 23:31:47 +02:00
|
|
|
int argCount = l >> 2;
|
|
|
|
int poppedArgs = argCount > 4 ? 4 : argCount;
|
|
|
|
int adjustedL = l - (poppedArgs << 2);
|
|
|
|
if (adjustedL < 0 || adjustedL > 4096-4) {
|
|
|
|
error("l out of range for stack offset: 0x%08x", l);
|
|
|
|
}
|
|
|
|
o4(0xE59DC000 | (0xfff & adjustedL)); // ldr r12, [sp,#adjustedL]
|
|
|
|
o4(0xE12FFF3C); // blx r12
|
2009-05-13 19:58:45 +02:00
|
|
|
}
|
|
|
|
|
2009-05-15 23:31:47 +02:00
|
|
|
virtual void adjustStackAfterCall(int l, bool isIndirect) {
|
2009-05-27 21:25:55 +02:00
|
|
|
LOG_API("adjustStackAfterCall(%d, %d);\n", l, isIndirect);
|
2009-05-14 20:38:49 +02:00
|
|
|
int argCount = l >> 2;
|
2009-05-15 23:31:47 +02:00
|
|
|
int stackArgs = argCount > 4 ? argCount - 4 : 0;
|
|
|
|
int stackUse = stackArgs + (isIndirect ? 1 : 0);
|
|
|
|
if (stackUse) {
|
|
|
|
if (stackUse < 0 || stackUse > 255) {
|
|
|
|
error("L out of range for stack adjustment: 0x%08x", l);
|
|
|
|
}
|
|
|
|
o4(0xE28DDF00 | stackUse); // add sp, sp, #stackUse << 2
|
2009-05-14 20:38:49 +02:00
|
|
|
}
|
2009-05-13 19:58:45 +02:00
|
|
|
}
|
|
|
|
|
2009-05-14 01:24:17 +02:00
|
|
|
virtual int jumpOffset() {
|
2009-05-15 04:35:31 +02:00
|
|
|
return 8;
|
2009-05-14 01:24:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* output a symbol and patch all calls to it */
|
|
|
|
virtual void gsym(int t) {
|
2009-05-27 21:25:55 +02:00
|
|
|
LOG_API("gsym(0x%x)\n", t);
|
2009-05-14 01:24:17 +02:00
|
|
|
int n;
|
|
|
|
int base = getBase();
|
|
|
|
int pc = getPC();
|
2009-05-27 21:25:55 +02:00
|
|
|
LOG_API("pc = 0x%x\n", pc);
|
2009-05-14 01:24:17 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
virtual int finishCompile() {
|
|
|
|
#if defined(__arm__)
|
|
|
|
const long base = long(getBase());
|
|
|
|
const long curr = long(getPC());
|
|
|
|
int err = cacheflush(base, curr, 0);
|
|
|
|
return err;
|
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-05-14 01:24:17 +02:00
|
|
|
virtual int disassemble(FILE* out) {
|
2009-05-27 21:25:55 +02:00
|
|
|
#ifdef ENABLE_ARM_DISASSEMBLY
|
|
|
|
disasmOut = out;
|
2009-05-14 01:24:17 +02:00
|
|
|
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);
|
|
|
|
}
|
2009-05-27 21:25:55 +02:00
|
|
|
#endif
|
2009-05-14 01:24:17 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2009-05-15 23:31:47 +02:00
|
|
|
|
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-16 00:12:38 +02:00
|
|
|
typedef int (*int2FnPtr)(int a, int b);
|
|
|
|
void callRuntime(int2FnPtr fn) {
|
|
|
|
o4(0xE59F2000); // ldr r2, .L1
|
|
|
|
o4(0xEA000000); // b .L99
|
|
|
|
o4((int) fn); //.L1: .word fn
|
|
|
|
o4(0xE12FFF32); //.L99: blx r2
|
|
|
|
}
|
|
|
|
|
|
|
|
static int runtime_DIV(int a, int b) {
|
|
|
|
return b / a;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int runtime_MOD(int a, int b) {
|
|
|
|
return b % a;
|
|
|
|
}
|
2009-05-13 19:58:45 +02:00
|
|
|
};
|
|
|
|
|
2009-05-27 21:25:55 +02:00
|
|
|
#endif // PROVIDE_ARM_CODEGEN
|
2009-05-20 02:12:17 +02:00
|
|
|
|
|
|
|
#ifdef PROVIDE_X86_CODEGEN
|
|
|
|
|
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-22 21:06:27 +02:00
|
|
|
virtual void clearR1() {
|
2009-05-11 23:49:29 +02:00
|
|
|
oad(0xb9, 0); /* movl $0, %ecx */
|
|
|
|
}
|
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
virtual void pushR0() {
|
2009-05-11 23:49:29 +02:00
|
|
|
o(0x50); /* push %eax */
|
|
|
|
}
|
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
virtual void popR1() {
|
2009-05-11 23:49:29 +02:00
|
|
|
o(0x59); /* pop %ecx */
|
2009-05-12 21:48:35 +02:00
|
|
|
}
|
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
virtual void storeR0ToR1(bool isInt) {
|
2009-05-11 23:49:29 +02:00
|
|
|
o(0x0188 + isInt); /* movl %eax/%al, (%ecx) */
|
|
|
|
}
|
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
virtual void loadR0FromR0(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-22 21:06:27 +02:00
|
|
|
virtual void leaR0(int ea) {
|
2009-05-11 23:49:29 +02:00
|
|
|
gmov(10, ea); /* leal EA, %eax */
|
|
|
|
}
|
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
virtual void storeR0(int ea) {
|
2009-05-11 23:49:29 +02:00
|
|
|
gmov(6, ea); /* mov %eax, EA */
|
|
|
|
}
|
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
virtual void loadR0(int ea, bool isIncDec, int op) {
|
2009-05-11 23:49:29 +02:00
|
|
|
gmov(8, ea); /* mov EA, %eax */
|
2009-05-15 22:30:00 +02:00
|
|
|
if (isIncDec) {
|
|
|
|
/* Implement post-increment or post decrement.
|
|
|
|
*/
|
|
|
|
gmov(0, ea); /* 83 ADD */
|
|
|
|
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-22 21:06:27 +02:00
|
|
|
virtual void storeR0ToArg(int l) {
|
2009-05-11 23:49:29 +02:00
|
|
|
oad(0x248489, l); /* movl %eax, xxx(%esp) */
|
|
|
|
}
|
|
|
|
|
2009-05-15 23:31:47 +02:00
|
|
|
virtual void endFunctionCallArguments(int a, int l) {
|
|
|
|
* (int*) a = l;
|
|
|
|
}
|
|
|
|
|
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-15 23:31:47 +02:00
|
|
|
virtual void adjustStackAfterCall(int l, bool isIndirect) {
|
|
|
|
if (isIndirect) {
|
|
|
|
l += 4;
|
|
|
|
}
|
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) {
|
2009-05-22 21:06:27 +02:00
|
|
|
return 0;
|
2009-05-14 01:24:17 +02:00
|
|
|
}
|
|
|
|
|
2009-05-20 02:12:17 +02:00
|
|
|
/* output a symbol and patch all calls to it */
|
|
|
|
virtual void gsym(int t) {
|
|
|
|
int n;
|
|
|
|
int pc = getPC();
|
|
|
|
while (t) {
|
|
|
|
n = *(int *) t; /* next value */
|
|
|
|
*(int *) t = pc - t - 4;
|
|
|
|
t = n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
virtual int finishCompile() {
|
2009-06-10 00:53:47 +02:00
|
|
|
size_t pagesize = 4096;
|
|
|
|
size_t base = (size_t) getBase() & ~ (pagesize - 1);
|
|
|
|
size_t top = ((size_t) getPC() + pagesize - 1) & ~ (pagesize - 1);
|
|
|
|
int err = mprotect((void*) base, top - base, PROT_READ | PROT_WRITE | PROT_EXEC);
|
|
|
|
if (err) {
|
|
|
|
error("mprotect() failed: %d", errno);
|
|
|
|
}
|
|
|
|
return err;
|
2009-05-22 21:06:27 +02:00
|
|
|
}
|
|
|
|
|
2009-05-12 21:48:35 +02:00
|
|
|
private:
|
2009-05-20 02:12:17 +02:00
|
|
|
|
|
|
|
/** Output 1 to 4 bytes.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void o(int n) {
|
|
|
|
/* cannot use unsigned, so we must do a hack */
|
|
|
|
while (n && n != -1) {
|
|
|
|
ob(n & 0xff);
|
|
|
|
n = n >> 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 result = getPC();
|
|
|
|
o4(t);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-12 21:48:35 +02:00
|
|
|
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) {
|
2009-05-29 22:53:44 +02:00
|
|
|
error("Out-of-range operator: %d\n", op);
|
2009-06-11 19:53:51 +02:00
|
|
|
op = 0;
|
2009-05-12 21:48:35 +02:00
|
|
|
}
|
|
|
|
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);
|
2009-06-10 00:53:47 +02:00
|
|
|
oad((t > -LOCAL && t < LOCAL) << 7 | 5, t);
|
2009-05-11 23:49:29 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-05-20 02:12:17 +02:00
|
|
|
#endif // PROVIDE_X86_CODEGEN
|
|
|
|
|
2009-06-12 06:12:23 +02:00
|
|
|
#ifdef PROVIDE_TRACE_CODEGEN
|
|
|
|
class TraceCodeGenerator : public CodeGenerator {
|
|
|
|
private:
|
|
|
|
CodeGenerator* mpBase;
|
|
|
|
|
|
|
|
public:
|
|
|
|
TraceCodeGenerator(CodeGenerator* pBase) {
|
|
|
|
mpBase = pBase;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~TraceCodeGenerator() {
|
|
|
|
delete mpBase;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void init(CodeBuf* pCodeBuf) {
|
|
|
|
mpBase->init(pCodeBuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setErrorSink(ErrorSink* pErrorSink) {
|
|
|
|
mpBase->setErrorSink(pErrorSink);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* returns address to patch with local variable size
|
|
|
|
*/
|
|
|
|
virtual int functionEntry(int argCount) {
|
|
|
|
int result = mpBase->functionEntry(argCount);
|
|
|
|
fprintf(stderr, "functionEntry(%d) -> %d\n", argCount, result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void functionExit(int argCount, int localVariableAddress, int localVariableSize) {
|
|
|
|
fprintf(stderr, "functionExit(%d, %d, %d)\n",
|
|
|
|
argCount, localVariableAddress, localVariableSize);
|
|
|
|
mpBase->functionExit(argCount, localVariableAddress, localVariableSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* load immediate value */
|
|
|
|
virtual void li(int t) {
|
|
|
|
fprintf(stderr, "li(%d)\n", t);
|
|
|
|
mpBase->li(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual int gjmp(int t) {
|
|
|
|
int result = mpBase->gjmp(t);
|
|
|
|
fprintf(stderr, "gjmp(%d) = %d\n", t, result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* l = 0: je, l == 1: jne */
|
|
|
|
virtual int gtst(bool l, int t) {
|
|
|
|
int result = mpBase->gtst(l, t);
|
|
|
|
fprintf(stderr, "gtst(%d,%d) = %d\n", l, t, result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void gcmp(int op) {
|
|
|
|
fprintf(stderr, "gcmp(%d)\n", op);
|
|
|
|
mpBase->gcmp(op);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void genOp(int op) {
|
|
|
|
fprintf(stderr, "genOp(%d)\n", op);
|
|
|
|
mpBase->genOp(op);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void clearR1() {
|
|
|
|
fprintf(stderr, "clearR1()\n");
|
|
|
|
mpBase->clearR1();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void pushR0() {
|
|
|
|
fprintf(stderr, "pushR0()\n");
|
|
|
|
mpBase->pushR0();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void popR1() {
|
|
|
|
fprintf(stderr, "popR1()\n");
|
|
|
|
mpBase->popR1();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void storeR0ToR1(bool isInt) {
|
|
|
|
fprintf(stderr, "storeR0ToR1(%d)\n", isInt);
|
|
|
|
mpBase->storeR0ToR1(isInt);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void loadR0FromR0(bool isInt) {
|
|
|
|
fprintf(stderr, "loadR0FromR0(%d)\n", isInt);
|
|
|
|
mpBase->loadR0FromR0(isInt);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void leaR0(int ea) {
|
|
|
|
fprintf(stderr, "leaR0(%d)\n", ea);
|
|
|
|
mpBase->leaR0(ea);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void storeR0(int ea) {
|
|
|
|
fprintf(stderr, "storeR0(%d)\n", ea);
|
|
|
|
mpBase->storeR0(ea);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void loadR0(int ea, bool isIncDec, int op) {
|
|
|
|
fprintf(stderr, "loadR0(%d, %d, %d)\n", ea, isIncDec, op);
|
|
|
|
mpBase->loadR0(ea, isIncDec, op);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual int beginFunctionCallArguments() {
|
|
|
|
int result = mpBase->beginFunctionCallArguments();
|
|
|
|
fprintf(stderr, "beginFunctionCallArguments() = %d\n", result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void storeR0ToArg(int l) {
|
|
|
|
fprintf(stderr, "storeR0ToArg(%d)\n", l);
|
|
|
|
mpBase->storeR0ToArg(l);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void endFunctionCallArguments(int a, int l) {
|
|
|
|
fprintf(stderr, "endFunctionCallArguments(%d, %d)\n", a, l);
|
|
|
|
mpBase->endFunctionCallArguments(a, l);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual int callForward(int symbol) {
|
|
|
|
int result = mpBase->callForward(symbol);
|
|
|
|
fprintf(stderr, "callForward(%d) = %d\n", symbol, result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void callRelative(int t) {
|
|
|
|
fprintf(stderr, "callRelative(%d)\n", t);
|
|
|
|
mpBase->callRelative(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void callIndirect(int l) {
|
|
|
|
fprintf(stderr, "callIndirect(%d)\n", l);
|
|
|
|
mpBase->callIndirect(l);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void adjustStackAfterCall(int l, bool isIndirect) {
|
|
|
|
fprintf(stderr, "adjustStackAfterCall(%d, %d)\n", l, isIndirect);
|
|
|
|
mpBase->adjustStackAfterCall(l, isIndirect);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual int jumpOffset() {
|
|
|
|
return mpBase->jumpOffset();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual int disassemble(FILE* out) {
|
|
|
|
return mpBase->disassemble(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* output a symbol and patch all calls to it */
|
|
|
|
virtual void gsym(int t) {
|
|
|
|
fprintf(stderr, "gsym(%d)\n", t);
|
|
|
|
mpBase->gsym(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual int finishCompile() {
|
|
|
|
int result = mpBase->finishCompile();
|
|
|
|
fprintf(stderr, "finishCompile() = %d\n", result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // PROVIDE_TRACE_CODEGEN
|
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
class InputStream {
|
|
|
|
public:
|
2009-06-05 01:23:40 +02:00
|
|
|
int getChar() {
|
|
|
|
if (bumpLine) {
|
|
|
|
line++;
|
|
|
|
bumpLine = false;
|
|
|
|
}
|
|
|
|
int ch = get();
|
|
|
|
if (ch == '\n') {
|
|
|
|
bumpLine = true;
|
|
|
|
}
|
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
int getLine() {
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
protected:
|
|
|
|
InputStream() :
|
|
|
|
line(1), bumpLine(false) {
|
|
|
|
}
|
|
|
|
private:
|
2009-05-22 21:06:27 +02:00
|
|
|
virtual int get() = 0;
|
2009-06-05 01:23:40 +02:00
|
|
|
int line;
|
|
|
|
bool bumpLine;
|
2009-05-22 21:06:27 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class FileInputStream : public InputStream {
|
|
|
|
public:
|
|
|
|
FileInputStream(FILE* in) : f(in) {}
|
|
|
|
private:
|
2009-06-05 01:23:40 +02:00
|
|
|
virtual int get() { return fgetc(f); }
|
2009-05-22 21:06:27 +02:00
|
|
|
FILE* f;
|
|
|
|
};
|
|
|
|
|
|
|
|
class TextInputStream : public InputStream {
|
|
|
|
public:
|
|
|
|
TextInputStream(const char* text, size_t textLength)
|
|
|
|
: pText(text), mTextLength(textLength), mPosition(0) {
|
|
|
|
}
|
2009-06-05 01:23:40 +02:00
|
|
|
|
|
|
|
private:
|
2009-05-22 21:06:27 +02:00
|
|
|
virtual int get() {
|
|
|
|
return mPosition < mTextLength ? pText[mPosition++] : EOF;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* pText;
|
|
|
|
size_t mTextLength;
|
|
|
|
size_t mPosition;
|
|
|
|
};
|
|
|
|
|
2009-06-05 01:23:40 +02:00
|
|
|
class String {
|
|
|
|
public:
|
|
|
|
String() {
|
|
|
|
mpBase = 0;
|
|
|
|
mUsed = 0;
|
|
|
|
mSize = 0;
|
|
|
|
}
|
|
|
|
|
2009-06-12 04:06:24 +02:00
|
|
|
String(const char* item, int len, bool adopt) {
|
|
|
|
if (len < 0) {
|
|
|
|
len = strlen(item);
|
|
|
|
}
|
2009-06-08 23:34:26 +02:00
|
|
|
if (adopt) {
|
2009-06-12 04:06:24 +02:00
|
|
|
mpBase = (char*) item;
|
2009-06-08 23:34:26 +02:00
|
|
|
mUsed = len;
|
|
|
|
mSize = len + 1;
|
|
|
|
} else {
|
|
|
|
mpBase = 0;
|
|
|
|
mUsed = 0;
|
|
|
|
mSize = 0;
|
|
|
|
appendBytes(item, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-12 04:06:24 +02:00
|
|
|
String(const String& other) {
|
|
|
|
mpBase = 0;
|
|
|
|
mUsed = 0;
|
|
|
|
mSize = 0;
|
|
|
|
appendBytes(other.getUnwrapped(), other.len());
|
|
|
|
}
|
|
|
|
|
2009-06-05 01:23:40 +02:00
|
|
|
~String() {
|
|
|
|
if (mpBase) {
|
|
|
|
free(mpBase);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-12 20:25:59 +02:00
|
|
|
String& operator=(const String& other) {
|
|
|
|
clear();
|
|
|
|
appendBytes(other.getUnwrapped(), other.len());
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2009-06-12 04:06:24 +02:00
|
|
|
inline char* getUnwrapped() const {
|
2009-06-05 01:23:40 +02:00
|
|
|
return mpBase;
|
|
|
|
}
|
|
|
|
|
2009-06-12 04:06:24 +02:00
|
|
|
void clear() {
|
|
|
|
mUsed = 0;
|
|
|
|
if (mSize > 0) {
|
|
|
|
mpBase[0] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-05 01:23:40 +02:00
|
|
|
void appendCStr(const char* s) {
|
2009-06-08 23:34:26 +02:00
|
|
|
appendBytes(s, strlen(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
void appendBytes(const char* s, int n) {
|
2009-06-05 01:23:40 +02:00
|
|
|
memcpy(ensure(n), s, n + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void append(char c) {
|
|
|
|
* ensure(1) = c;
|
|
|
|
}
|
|
|
|
|
2009-06-08 23:34:26 +02:00
|
|
|
char* orphan() {
|
|
|
|
char* result = mpBase;
|
|
|
|
mpBase = 0;
|
|
|
|
mUsed = 0;
|
|
|
|
mSize = 0;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-06-05 01:23:40 +02:00
|
|
|
void printf(const char* fmt,...) {
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vprintf(fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
void vprintf(const char* fmt, va_list ap) {
|
|
|
|
char* temp;
|
|
|
|
int numChars = vasprintf(&temp, fmt, ap);
|
|
|
|
memcpy(ensure(numChars), temp, numChars+1);
|
|
|
|
free(temp);
|
|
|
|
}
|
|
|
|
|
2009-06-12 04:06:24 +02:00
|
|
|
inline size_t len() const {
|
2009-06-05 01:23:40 +02:00
|
|
|
return mUsed;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
char* ensure(int n) {
|
|
|
|
size_t newUsed = mUsed + n;
|
|
|
|
if (newUsed > mSize) {
|
|
|
|
size_t newSize = mSize * 2 + 10;
|
|
|
|
if (newSize < newUsed) {
|
|
|
|
newSize = newUsed;
|
|
|
|
}
|
|
|
|
mpBase = (char*) realloc(mpBase, newSize + 1);
|
|
|
|
mSize = newSize;
|
|
|
|
}
|
|
|
|
mpBase[newUsed] = '\0';
|
|
|
|
char* result = mpBase + mUsed;
|
|
|
|
mUsed = newUsed;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* mpBase;
|
|
|
|
size_t mUsed;
|
|
|
|
size_t mSize;
|
|
|
|
};
|
|
|
|
|
2009-06-08 23:34:26 +02:00
|
|
|
/**
|
|
|
|
* Wrap an externally allocated string for use as a hash key.
|
|
|
|
*/
|
|
|
|
class FakeString : public String {
|
|
|
|
public:
|
2009-06-11 23:29:47 +02:00
|
|
|
FakeString(const char* string, size_t length) :
|
|
|
|
String((char*) string, length, true) {}
|
2009-06-08 23:34:26 +02:00
|
|
|
|
|
|
|
~FakeString() {
|
|
|
|
orphan();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class V> class StringTable {
|
|
|
|
public:
|
2009-06-12 04:06:24 +02:00
|
|
|
StringTable() {
|
|
|
|
init(10);
|
|
|
|
}
|
|
|
|
|
2009-06-08 23:34:26 +02:00
|
|
|
StringTable(size_t initialCapacity) {
|
2009-06-12 04:06:24 +02:00
|
|
|
init(initialCapacity);
|
2009-06-08 23:34:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
~StringTable() {
|
|
|
|
clear();
|
2009-06-11 23:29:47 +02:00
|
|
|
hashmapFree(mpMap);
|
2009-06-08 23:34:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void clear() {
|
|
|
|
hashmapForEach(mpMap, freeKeyValue, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool contains(String* pKey) {
|
|
|
|
bool result = hashmapContainsKey(mpMap, pKey);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
V* get(String* pKey) {
|
|
|
|
V* result = (V*) hashmapGet(mpMap, pKey);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
V* remove(String* pKey) {
|
|
|
|
V* result = (V*) hashmapRemove(mpMap, pKey);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
V* put(String* pKey, V* value) {
|
|
|
|
V* result = (V*) hashmapPut(mpMap, pKey, value);
|
|
|
|
if (result) {
|
|
|
|
// The key was not adopted by the map, so delete it here.
|
|
|
|
delete pKey;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-06-12 20:25:59 +02:00
|
|
|
void forEach(bool (*callback)(String* key, V* value, void* context),
|
|
|
|
void* context) {
|
|
|
|
hashmapForEach(mpMap, (bool (*)(void*, void*, void*)) callback,
|
|
|
|
context);
|
|
|
|
}
|
|
|
|
|
2009-06-08 23:34:26 +02:00
|
|
|
protected:
|
2009-06-12 04:06:24 +02:00
|
|
|
|
|
|
|
void init(size_t initialCapacity) {
|
|
|
|
mpMap = hashmapCreate(initialCapacity, hashFn, equalsFn);
|
|
|
|
}
|
|
|
|
|
2009-06-08 23:34:26 +02:00
|
|
|
static int hashFn(void* pKey) {
|
|
|
|
String* pString = (String*) pKey;
|
|
|
|
return hashmapHash(pString->getUnwrapped(), pString->len());
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool equalsFn(void* keyA, void* keyB) {
|
|
|
|
String* pStringA = (String*) keyA;
|
|
|
|
String* pStringB = (String*) keyB;
|
|
|
|
return pStringA->len() == pStringB->len()
|
|
|
|
&& strcmp(pStringA->getUnwrapped(), pStringB->getUnwrapped())
|
|
|
|
== 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool freeKeyValue(void* key, void* value, void* context) {
|
|
|
|
delete (String*) key;
|
|
|
|
delete (V*) value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Hashmap* mpMap;
|
|
|
|
};
|
|
|
|
|
|
|
|
class MacroTable : public StringTable<String> {
|
|
|
|
public:
|
|
|
|
MacroTable() : StringTable<String>(10) {}
|
|
|
|
};
|
|
|
|
|
2009-06-11 23:29:47 +02:00
|
|
|
class KeywordTable {
|
|
|
|
public:
|
|
|
|
|
|
|
|
KeywordTable(){
|
|
|
|
mpMap = hashmapCreate(40, hashFn, equalsFn);
|
|
|
|
put("int", TOK_INT);
|
|
|
|
put("char", TOK_CHAR);
|
|
|
|
put("void", TOK_VOID);
|
|
|
|
put("if", TOK_IF);
|
|
|
|
put("else", TOK_ELSE);
|
|
|
|
put("while", TOK_WHILE);
|
|
|
|
put("break", TOK_BREAK);
|
|
|
|
put("return", TOK_RETURN);
|
|
|
|
put("for", TOK_FOR);
|
2009-06-12 20:53:07 +02:00
|
|
|
// TODO: remove these preprocessor-specific keywords. You should
|
|
|
|
// be able to have symbols named pragma or define.
|
2009-06-11 23:29:47 +02:00
|
|
|
put("pragma", TOK_PRAGMA);
|
2009-06-12 20:53:07 +02:00
|
|
|
put("define", TOK_DEFINE);
|
2009-06-11 23:29:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
~KeywordTable() {
|
|
|
|
hashmapFree(mpMap);
|
|
|
|
}
|
|
|
|
|
2009-06-12 04:06:24 +02:00
|
|
|
int get(String* key) {
|
|
|
|
return (int) hashmapGet(mpMap, key->getUnwrapped());
|
2009-06-11 23:29:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* lookupKeyFor(int value) {
|
|
|
|
FindValContext context;
|
|
|
|
context.key = 0;
|
|
|
|
hashmapForEach(mpMap, findKeyFn, &context);
|
|
|
|
return context.key;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void put(const char* kw, int val) {
|
|
|
|
hashmapPut(mpMap, (void*) kw, (void*) val);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hashFn(void* pKey) {
|
|
|
|
char* pString = (char*) pKey;
|
|
|
|
return hashmapHash(pString, strlen(pString));
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool equalsFn(void* keyA, void* keyB) {
|
|
|
|
const char* pStringA = (const char*) keyA;
|
|
|
|
const char* pStringB = (const char*) keyB;
|
|
|
|
return strcmp(pStringA, pStringB) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct FindValContext {
|
|
|
|
char* key;
|
|
|
|
int value;
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool findKeyFn(void* key, void* value, void* context) {
|
|
|
|
FindValContext* pContext = (FindValContext*) context;
|
|
|
|
if ((int) value == pContext->value) {
|
|
|
|
pContext->key = (char*) key;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Hashmap* mpMap;
|
|
|
|
};
|
|
|
|
|
2009-06-08 23:34:26 +02:00
|
|
|
template<class E> class Array {
|
|
|
|
public:
|
|
|
|
Array() {
|
|
|
|
mpBase = 0;
|
|
|
|
mUsed = 0;
|
|
|
|
mSize = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
~Array() {
|
|
|
|
if (mpBase) {
|
|
|
|
free(mpBase);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
E get(int i) {
|
2009-06-12 04:06:24 +02:00
|
|
|
if (i < 0 || i > (int) mUsed) {
|
|
|
|
// error("internal error: Index out of range");
|
2009-06-08 23:34:26 +02:00
|
|
|
return E();
|
|
|
|
}
|
|
|
|
return mpBase[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
void set(int i, E val) {
|
|
|
|
mpBase[i] = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
void pop() {
|
|
|
|
if (mUsed > 0) {
|
|
|
|
mUsed -= 1;
|
2009-06-09 00:55:32 +02:00
|
|
|
} else {
|
2009-06-12 04:06:24 +02:00
|
|
|
// error("internal error: Popped empty stack.");
|
2009-06-08 23:34:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void push(E item) {
|
|
|
|
* ensure(1) = item;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t len() {
|
|
|
|
return mUsed;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
E* ensure(int n) {
|
|
|
|
size_t newUsed = mUsed + n;
|
|
|
|
if (newUsed > mSize) {
|
|
|
|
size_t newSize = mSize * 2 + 10;
|
|
|
|
if (newSize < newUsed) {
|
|
|
|
newSize = newUsed;
|
|
|
|
}
|
|
|
|
mpBase = (E*) realloc(mpBase, sizeof(E) * newSize);
|
|
|
|
mSize = newSize;
|
|
|
|
}
|
|
|
|
E* result = mpBase + mUsed;
|
|
|
|
mUsed = newUsed;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
E* mpBase;
|
|
|
|
size_t mUsed;
|
|
|
|
size_t mSize;
|
|
|
|
};
|
|
|
|
|
2009-06-09 00:55:32 +02:00
|
|
|
struct InputState {
|
|
|
|
InputStream* pStream;
|
|
|
|
int oldCh;
|
|
|
|
};
|
|
|
|
|
2009-06-11 23:29:47 +02:00
|
|
|
struct VariableInfo {
|
2009-06-12 04:06:24 +02:00
|
|
|
VariableInfo() {
|
|
|
|
pAddress = 0;
|
|
|
|
pForward = 0;
|
|
|
|
}
|
|
|
|
void* pAddress;
|
|
|
|
void* pForward; // For a forward direction, linked list of data to fix up
|
2009-06-11 23:29:47 +02:00
|
|
|
};
|
|
|
|
|
2009-06-12 04:06:24 +02:00
|
|
|
typedef StringTable<VariableInfo> SymbolTable;
|
|
|
|
|
|
|
|
class SymbolStack {
|
|
|
|
public:
|
|
|
|
SymbolStack() {
|
|
|
|
mLevel = 0;
|
|
|
|
addEntry();
|
|
|
|
}
|
|
|
|
|
|
|
|
void pushLevel() {
|
|
|
|
mLevel++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void popLevel() {
|
|
|
|
mLevel--;
|
|
|
|
Entry e = mStack.get(mStack.len()-1);
|
|
|
|
if (mLevel < e.level) {
|
|
|
|
mStack.pop();
|
|
|
|
delete e.pTable;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VariableInfo* get(String* pName) {
|
|
|
|
int len = mStack.len();
|
|
|
|
VariableInfo* v = NULL;
|
|
|
|
int level = -1;
|
|
|
|
for (int i = len - 1; i >= 0; i--) {
|
|
|
|
Entry e = mStack.get(i);
|
|
|
|
v = e.pTable->get(pName);
|
|
|
|
if (v) {
|
|
|
|
level = e.level;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#if 0
|
|
|
|
fprintf(stderr, "Lookup %s %08x level %d\n", pName->getUnwrapped(), v, level);
|
|
|
|
if (v) {
|
|
|
|
fprintf(stderr, " %08x %08x\n", v->pAddress, v->pForward);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
VariableInfo* addLocal(String* pName) {
|
|
|
|
int len = mStack.len();
|
|
|
|
if (mStack.get(len-1).level != mLevel) {
|
|
|
|
addEntry();
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
return addImp(len-1, pName);
|
|
|
|
}
|
|
|
|
|
|
|
|
VariableInfo* addGlobal(String* pName) {
|
|
|
|
return addImp(0, pName);
|
|
|
|
}
|
|
|
|
|
2009-06-12 20:25:59 +02:00
|
|
|
void forEachGlobal(
|
|
|
|
bool (*callback)(String* key, VariableInfo* value, void* context),
|
|
|
|
void* context) {
|
|
|
|
mStack.get(0).pTable->forEach(callback, context);
|
|
|
|
}
|
|
|
|
|
2009-06-12 04:06:24 +02:00
|
|
|
private:
|
|
|
|
VariableInfo* addImp(int entryIndex, String* pName) {
|
|
|
|
Entry e = mStack.get(entryIndex);
|
|
|
|
SymbolTable* pTable = e.pTable;
|
2009-06-12 20:25:59 +02:00
|
|
|
if (pTable->contains(pName)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-06-12 04:06:24 +02:00
|
|
|
VariableInfo* v = new VariableInfo();
|
2009-06-12 20:25:59 +02:00
|
|
|
|
2009-06-12 04:06:24 +02:00
|
|
|
delete pTable->put(pName, v);
|
|
|
|
#if 0
|
|
|
|
fprintf(stderr, "Add \"%s\" %08x level %d\n", pName->getUnwrapped(), v, e.level);
|
|
|
|
#endif
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
void addEntry() {
|
|
|
|
Entry e;
|
|
|
|
e.level = mLevel;
|
|
|
|
e.pTable = new SymbolTable();
|
|
|
|
mStack.push(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Entry {
|
|
|
|
Entry() {
|
|
|
|
level = 0;
|
|
|
|
pTable = NULL;
|
|
|
|
}
|
|
|
|
int level;
|
|
|
|
SymbolTable* pTable;
|
|
|
|
};
|
|
|
|
|
|
|
|
int mLevel;
|
|
|
|
Array<Entry> mStack;
|
|
|
|
};
|
2009-06-09 00:55:32 +02:00
|
|
|
|
|
|
|
int ch; // Current input character, or EOF
|
|
|
|
intptr_t tok; // token
|
|
|
|
intptr_t tokc; // token extra info
|
|
|
|
int tokl; // token operator level
|
|
|
|
intptr_t rsym; // return symbol
|
|
|
|
intptr_t loc; // local variable index
|
|
|
|
char* glo; // global variable index
|
2009-06-12 04:06:24 +02:00
|
|
|
String mTokenString;
|
2009-06-09 00:55:32 +02:00
|
|
|
char* dptr; // Macro state: Points to macro text during macro playback.
|
|
|
|
int dch; // Macro state: Saves old value of ch during a macro playback.
|
|
|
|
char* pGlobalBase;
|
2009-06-11 23:29:47 +02:00
|
|
|
KeywordTable mKeywords;
|
2009-06-12 04:06:24 +02:00
|
|
|
SymbolStack mSymbolTable;
|
2009-06-09 00:55:32 +02:00
|
|
|
InputStream* file;
|
|
|
|
|
|
|
|
CodeBuf codeBuf;
|
|
|
|
CodeGenerator* pGen;
|
|
|
|
|
2009-06-08 23:34:26 +02:00
|
|
|
MacroTable mMacros;
|
2009-06-09 00:55:32 +02:00
|
|
|
Array<InputState> mInputStateStack;
|
2009-06-08 23:34:26 +02:00
|
|
|
|
2009-06-05 01:23:40 +02:00
|
|
|
String mErrorBuf;
|
|
|
|
|
|
|
|
String mPragmas;
|
|
|
|
int mPragmaStringCount;
|
|
|
|
|
2009-05-11 23:49:29 +02:00
|
|
|
static const int ALLOC_SIZE = 99999;
|
|
|
|
|
2009-06-12 04:06:24 +02:00
|
|
|
static const int TOK_DUMMY = 1;
|
|
|
|
static const int TOK_NUM = 2;
|
|
|
|
|
|
|
|
// 3..255 are character and/or operators
|
|
|
|
|
2009-06-11 23:29:47 +02:00
|
|
|
// Keywords start at 0x100 and increase by 1
|
|
|
|
static const int TOK_KEYWORD = 0x100;
|
|
|
|
static const int TOK_INT = TOK_KEYWORD + 0;
|
|
|
|
static const int TOK_CHAR = TOK_KEYWORD + 1;
|
|
|
|
static const int TOK_VOID = TOK_KEYWORD + 2;
|
|
|
|
static const int TOK_IF = TOK_KEYWORD + 3;
|
|
|
|
static const int TOK_ELSE = TOK_KEYWORD + 4;
|
|
|
|
static const int TOK_WHILE = TOK_KEYWORD + 5;
|
|
|
|
static const int TOK_BREAK = TOK_KEYWORD + 6;
|
|
|
|
static const int TOK_RETURN = TOK_KEYWORD + 7;
|
|
|
|
static const int TOK_FOR = TOK_KEYWORD + 8;
|
|
|
|
static const int TOK_PRAGMA = TOK_KEYWORD + 9;
|
|
|
|
static const int TOK_DEFINE = TOK_KEYWORD + 10;
|
|
|
|
|
2009-06-12 04:06:24 +02:00
|
|
|
static const int TOK_UNDEFINED_SYMBOL = 0x200;
|
|
|
|
|
|
|
|
// Symbols start at 0x300, but are really pointers to VariableInfo structs.
|
|
|
|
static const int TOK_SYMBOL = 0x300;
|
2009-05-11 23:49:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
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 = ' ';
|
|
|
|
|
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) {
|
2009-06-12 04:06:24 +02:00
|
|
|
mTokenString.append(t);
|
2009-05-11 23:49:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void inp() {
|
|
|
|
if (dptr) {
|
2009-05-29 02:15:32 +02:00
|
|
|
ch = *dptr++;
|
2009-06-08 23:34:26 +02:00
|
|
|
if (ch == 0) {
|
2009-05-11 23:49:29 +02:00
|
|
|
dptr = 0;
|
|
|
|
ch = dch;
|
|
|
|
}
|
|
|
|
} else
|
2009-06-05 01:23:40 +02:00
|
|
|
ch = file->getChar();
|
2009-06-05 04:56:13 +02:00
|
|
|
#if 0
|
|
|
|
printf("ch='%c' 0x%x\n", ch, ch);
|
|
|
|
#endif
|
2009-05-11 23:49:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2009-06-12 21:49:14 +02:00
|
|
|
/* read a character constant, advances ch to after end of constant */
|
|
|
|
int getq() {
|
|
|
|
int val = ch;
|
2009-05-11 23:49:29 +02:00
|
|
|
if (ch == '\\') {
|
2009-05-10 23:09:03 +02:00
|
|
|
inp();
|
2009-06-12 21:49:14 +02:00
|
|
|
if (isoctal(ch)) {
|
|
|
|
// 1 to 3 octal characters.
|
|
|
|
val = 0;
|
|
|
|
for(int i = 0; i < 3; i++) {
|
|
|
|
if (isoctal(ch)) {
|
|
|
|
val = (val << 3) + ch - '0';
|
|
|
|
inp();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
} else if (ch == 'x' || ch == 'X') {
|
|
|
|
// N hex chars
|
|
|
|
inp();
|
|
|
|
if (! isxdigit(ch)) {
|
|
|
|
error("'x' character escape requires at least one digit.");
|
|
|
|
} else {
|
|
|
|
val = 0;
|
|
|
|
while (isxdigit(ch)) {
|
|
|
|
int d = ch;
|
|
|
|
if (isdigit(d)) {
|
|
|
|
d -= '0';
|
|
|
|
} else if (d <= 'F') {
|
|
|
|
d = d - 'A' + 10;
|
|
|
|
} else {
|
|
|
|
d = d - 'a' + 10;
|
|
|
|
}
|
|
|
|
val = (val << 4) + d;
|
|
|
|
inp();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int val = ch;
|
|
|
|
switch (ch) {
|
|
|
|
case 'a':
|
|
|
|
val = '\a';
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
val = '\b';
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
val = '\f';
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
val = '\n';
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
val = '\r';
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
val = '\t';
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
val = '\v';
|
|
|
|
break;
|
|
|
|
case '\\':
|
|
|
|
val = '\\';
|
|
|
|
break;
|
|
|
|
case '\'':
|
|
|
|
val = '\'';
|
|
|
|
break;
|
|
|
|
case '"':
|
|
|
|
val = '"';
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
val = '?';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error("Undefined character escape %c", ch);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
inp();
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
inp();
|
2009-05-08 23:54:15 +02:00
|
|
|
}
|
2009-06-12 21:49:14 +02:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isoctal(int ch) {
|
|
|
|
return ch >= '0' && ch <= '7';
|
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-06-08 23:34:26 +02:00
|
|
|
doDefine();
|
2009-06-05 01:23:40 +02:00
|
|
|
} else if (tok == TOK_PRAGMA) {
|
|
|
|
doPragma();
|
|
|
|
} else {
|
2009-06-12 04:06:24 +02:00
|
|
|
error("Unsupported preprocessor directive \"%s\"",
|
|
|
|
mTokenString.getUnwrapped());
|
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()) {
|
2009-06-12 04:06:24 +02:00
|
|
|
mTokenString.clear();
|
2009-05-11 23:49:29 +02:00
|
|
|
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)) {
|
2009-06-12 04:06:24 +02:00
|
|
|
tokc = strtol(mTokenString.getUnwrapped(), 0, 0);
|
2009-05-11 23:49:29 +02:00
|
|
|
tok = TOK_NUM;
|
|
|
|
} else {
|
2009-06-08 23:34:26 +02:00
|
|
|
// Is this a macro?
|
2009-06-12 04:06:24 +02:00
|
|
|
String* pValue = mMacros.get(&mTokenString);
|
2009-06-08 23:34:26 +02:00
|
|
|
if (pValue) {
|
|
|
|
// Yes, it is a macro
|
|
|
|
dptr = pValue->getUnwrapped();
|
|
|
|
dch = ch;
|
|
|
|
inp();
|
|
|
|
next();
|
|
|
|
} else {
|
2009-06-11 23:29:47 +02:00
|
|
|
// Is this a keyword?
|
2009-06-12 04:06:24 +02:00
|
|
|
int kwtok = mKeywords.get(&mTokenString);
|
2009-06-11 23:29:47 +02:00
|
|
|
if (kwtok) {
|
|
|
|
tok = kwtok;
|
|
|
|
// fprintf(stderr, "tok= keyword %s %x\n", last_id, tok);
|
|
|
|
} else {
|
2009-06-12 04:06:24 +02:00
|
|
|
tok = (intptr_t) mSymbolTable.get(&mTokenString);
|
|
|
|
if (!tok) {
|
|
|
|
tok = TOK_UNDEFINED_SYMBOL;
|
|
|
|
}
|
2009-06-11 23:29:47 +02:00
|
|
|
// fprintf(stderr, "tok= symbol %s %x\n", last_id, tok);
|
2009-05-11 23:49:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2009-05-10 23:09:03 +02:00
|
|
|
inp();
|
2009-05-11 23:49:29 +02:00
|
|
|
if (tok == '\'') {
|
|
|
|
tok = TOK_NUM;
|
2009-06-12 21:49:14 +02:00
|
|
|
tokc = getq();
|
|
|
|
if (ch != '\'') {
|
|
|
|
error("Expected a ' character, got %c", ch);
|
|
|
|
} else {
|
|
|
|
inp();
|
|
|
|
}
|
2009-05-14 00:10:04 +02:00
|
|
|
} else if ((tok == '/') & (ch == '*')) {
|
2009-05-11 23:49:29 +02:00
|
|
|
inp();
|
2009-06-12 22:12:55 +02:00
|
|
|
while (ch && ch != EOF) {
|
|
|
|
while (ch != '*' && ch != EOF)
|
2009-05-11 23:49:29 +02:00
|
|
|
inp();
|
|
|
|
inp();
|
|
|
|
if (ch == '/')
|
|
|
|
ch = 0;
|
|
|
|
}
|
2009-06-12 22:12:55 +02:00
|
|
|
if (ch == EOF) {
|
|
|
|
error("End of file inside comment.");
|
|
|
|
}
|
2009-05-11 23:49:29 +02:00
|
|
|
inp();
|
|
|
|
next();
|
2009-05-15 04:35:31 +02:00
|
|
|
} else if ((tok == '/') & (ch == '/')) {
|
|
|
|
inp();
|
2009-06-12 22:12:55 +02:00
|
|
|
while (ch && (ch != '\n') && (ch != EOF)) {
|
2009-05-15 04:35:31 +02:00
|
|
|
inp();
|
|
|
|
}
|
|
|
|
inp();
|
|
|
|
next();
|
2009-05-11 23:49:29 +02:00
|
|
|
} 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
|
|
|
{
|
2009-06-11 23:29:47 +02:00
|
|
|
const char* p;
|
2009-05-10 23:09:03 +02:00
|
|
|
|
2009-05-11 23:49:29 +02:00
|
|
|
printf("tok=0x%x ", tok);
|
2009-06-11 23:29:47 +02:00
|
|
|
if (tok >= TOK_KEYWORD) {
|
2009-05-11 23:49:29 +02:00
|
|
|
printf("'");
|
2009-06-11 23:29:47 +02:00
|
|
|
if (tok>= TOK_SYMBOL)
|
|
|
|
p = sym_stk + 1 + ((char*) tok - (char*) pVarsBase) / 8;
|
|
|
|
else {
|
|
|
|
p = mKeywords.lookupKeyFor(tok);
|
|
|
|
if (!p) {
|
|
|
|
p = "unknown keyword";
|
|
|
|
}
|
|
|
|
}
|
2009-05-29 02:15:32 +02:00
|
|
|
while (*p != TAG_TOK && *p)
|
|
|
|
printf("%c", *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-06-08 23:34:26 +02:00
|
|
|
void doDefine() {
|
|
|
|
String* pName = new String();
|
|
|
|
while (isspace(ch)) {
|
|
|
|
inp();
|
|
|
|
}
|
|
|
|
while (isid()) {
|
|
|
|
pName->append(ch);
|
|
|
|
inp();
|
|
|
|
}
|
|
|
|
if (ch == '(') {
|
|
|
|
delete pName;
|
|
|
|
error("Defines with arguments not supported");
|
2009-06-11 19:53:51 +02:00
|
|
|
return;
|
2009-06-08 23:34:26 +02:00
|
|
|
}
|
|
|
|
while (isspace(ch)) {
|
|
|
|
inp();
|
|
|
|
}
|
|
|
|
String* pValue = new String();
|
|
|
|
while (ch != '\n' && ch != EOF) {
|
|
|
|
pValue->append(ch);
|
|
|
|
inp();
|
|
|
|
}
|
|
|
|
delete mMacros.put(pName, pValue);
|
|
|
|
}
|
|
|
|
|
2009-06-05 01:23:40 +02:00
|
|
|
void doPragma() {
|
|
|
|
// # pragma name(val)
|
|
|
|
int state = 0;
|
|
|
|
while(ch != EOF && ch != '\n' && state < 10) {
|
|
|
|
switch(state) {
|
|
|
|
case 0:
|
|
|
|
if (isspace(ch)) {
|
|
|
|
inp();
|
|
|
|
} else {
|
|
|
|
state++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
if (isalnum(ch)) {
|
|
|
|
mPragmas.append(ch);
|
|
|
|
inp();
|
|
|
|
} else if (ch == '(') {
|
|
|
|
mPragmas.append(0);
|
|
|
|
inp();
|
|
|
|
state++;
|
|
|
|
} else {
|
|
|
|
state = 11;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
if (isalnum(ch)) {
|
|
|
|
mPragmas.append(ch);
|
|
|
|
inp();
|
|
|
|
} else if (ch == ')') {
|
|
|
|
mPragmas.append(0);
|
|
|
|
inp();
|
|
|
|
state = 10;
|
|
|
|
} else {
|
|
|
|
state = 11;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(state != 10) {
|
|
|
|
error("Unexpected pragma syntax");
|
|
|
|
}
|
|
|
|
mPragmaStringCount += 2;
|
|
|
|
}
|
2009-05-10 23:09:03 +02:00
|
|
|
|
2009-05-29 22:53:44 +02:00
|
|
|
virtual void verror(const char* fmt, va_list ap) {
|
2009-06-05 01:23:40 +02:00
|
|
|
mErrorBuf.printf("%ld: ", file->getLine());
|
|
|
|
mErrorBuf.vprintf(fmt, ap);
|
|
|
|
mErrorBuf.printf("\n");
|
2009-05-11 23:49:29 +02:00
|
|
|
}
|
2009-05-10 23:09:03 +02:00
|
|
|
|
2009-05-20 21:12:06 +02:00
|
|
|
void skip(intptr_t c) {
|
2009-05-11 23:49:29 +02:00
|
|
|
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) */
|
2009-05-20 21:12:06 +02:00
|
|
|
void unary(intptr_t l) {
|
2009-05-29 02:15:32 +02:00
|
|
|
intptr_t n, t, a;
|
|
|
|
int c;
|
2009-06-12 20:25:59 +02:00
|
|
|
String tString;
|
2009-05-14 00:10:04 +02:00
|
|
|
t = 0;
|
2009-06-12 21:49:14 +02:00
|
|
|
n = 1; /* type of expression 0 = forward, 1 = value, other = lvalue */
|
2009-05-11 23:49:29 +02:00
|
|
|
if (tok == '\"') {
|
2009-05-29 02:15:32 +02:00
|
|
|
pGen->li((int) glo);
|
2009-06-12 21:49:14 +02:00
|
|
|
while (ch != '\"' && ch != EOF) {
|
|
|
|
*allocGlobalSpace(1) = getq();
|
|
|
|
}
|
|
|
|
if (ch != '\"') {
|
|
|
|
error("Unterminated string constant.");
|
2009-05-11 23:49:29 +02:00
|
|
|
}
|
2009-05-29 02:15:32 +02:00
|
|
|
*glo = 0;
|
2009-05-30 03:03:15 +02:00
|
|
|
/* align heap */
|
|
|
|
allocGlobalSpace((char*) (((intptr_t) glo + 4) & -4) - glo);
|
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;
|
2009-06-12 20:25:59 +02:00
|
|
|
tString = mTokenString;
|
2009-05-11 23:49:29 +02:00
|
|
|
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-22 21:06:27 +02:00
|
|
|
pGen->clearR1();
|
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();
|
2009-05-22 21:06:27 +02:00
|
|
|
pGen->pushR0();
|
2009-05-11 23:49:29 +02:00
|
|
|
expr();
|
2009-05-22 21:06:27 +02:00
|
|
|
pGen->popR1();
|
|
|
|
pGen->storeR0ToR1(t == TOK_INT);
|
2009-05-11 23:49:29 +02:00
|
|
|
} else if (t) {
|
2009-05-22 21:06:27 +02:00
|
|
|
pGen->loadR0FromR0(t == TOK_INT);
|
2009-05-11 23:49:29 +02:00
|
|
|
}
|
|
|
|
} else if (t == '&') {
|
2009-05-22 21:06:27 +02:00
|
|
|
pGen->leaR0(*(int *) tok);
|
2009-05-10 23:09:03 +02:00
|
|
|
next();
|
2009-06-12 04:06:24 +02:00
|
|
|
} else if (t == EOF ) {
|
|
|
|
error("Unexpected EOF.");
|
|
|
|
} else if (t < TOK_UNDEFINED_SYMBOL) {
|
|
|
|
error("Unexpected symbol or keyword");
|
2009-05-11 23:49:29 +02:00
|
|
|
} else {
|
2009-06-12 04:06:24 +02:00
|
|
|
if (t == TOK_UNDEFINED_SYMBOL) {
|
|
|
|
t = (intptr_t) mSymbolTable.addGlobal(
|
2009-06-12 20:25:59 +02:00
|
|
|
new String(tString));
|
2009-06-12 04:06:24 +02:00
|
|
|
}
|
|
|
|
|
2009-06-12 20:25:59 +02:00
|
|
|
n = (intptr_t) ((VariableInfo*) t)->pAddress;
|
2009-05-11 23:49:29 +02:00
|
|
|
/* forward reference: try dlsym */
|
2009-05-14 20:38:49 +02:00
|
|
|
if (!n) {
|
2009-06-12 04:06:24 +02:00
|
|
|
n = (intptr_t) dlsym(RTLD_DEFAULT,
|
2009-06-12 20:25:59 +02:00
|
|
|
tString.getUnwrapped());
|
|
|
|
((VariableInfo*) t)->pAddress = (void*) n;
|
2009-05-14 20:38:49 +02:00
|
|
|
}
|
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();
|
2009-05-22 21:06:27 +02:00
|
|
|
pGen->storeR0(n);
|
2009-05-11 23:49:29 +02:00
|
|
|
} else if (tok != '(') {
|
|
|
|
/* variable */
|
2009-06-12 20:25:59 +02:00
|
|
|
if (!n) {
|
|
|
|
error("Undefined variable %s", tString.getUnwrapped());
|
|
|
|
}
|
2009-05-22 21:06:27 +02:00
|
|
|
pGen->loadR0(n, tokl == 11, tokc);
|
2009-05-11 23:49:29 +02:00
|
|
|
if (tokl == 11) {
|
|
|
|
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)
|
2009-05-22 21:06:27 +02:00
|
|
|
pGen->pushR0();
|
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;
|
2009-06-12 21:49:14 +02:00
|
|
|
while (tok != ')' && tok != EOF) {
|
2009-05-11 23:49:29 +02:00
|
|
|
expr();
|
2009-05-22 21:06:27 +02:00
|
|
|
pGen->storeR0ToArg(l);
|
2009-05-11 23:49:29 +02:00
|
|
|
if (tok == ',')
|
|
|
|
next();
|
|
|
|
l = l + 4;
|
|
|
|
}
|
2009-05-14 20:38:49 +02:00
|
|
|
pGen->endFunctionCallArguments(a, l);
|
2009-06-12 21:49:14 +02:00
|
|
|
skip(')');
|
2009-05-11 23:49:29 +02:00
|
|
|
if (!n) {
|
|
|
|
/* forward reference */
|
|
|
|
t = t + 4;
|
|
|
|
*(int *) t = pGen->callForward(*(int *) t);
|
|
|
|
} else if (n == 1) {
|
|
|
|
pGen->callIndirect(l);
|
|
|
|
} else {
|
2009-05-15 23:31:47 +02:00
|
|
|
pGen->callRelative(n - codeBuf.getPC() - pGen->jumpOffset());
|
2009-05-11 23:49:29 +02:00
|
|
|
}
|
2009-05-16 00:12:38 +02:00
|
|
|
if (l | (n == 1))
|
2009-05-15 23:31:47 +02:00
|
|
|
pGen->adjustStackAfterCall(l, n == 1);
|
2009-05-08 23:54:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-29 02:15:32 +02:00
|
|
|
void sum(int l) {
|
2009-05-20 21:12:06 +02:00
|
|
|
intptr_t 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-22 21:06:27 +02:00
|
|
|
pGen->pushR0();
|
2009-05-11 23:49:29 +02:00
|
|
|
sum(l);
|
2009-05-22 21:06:27 +02:00
|
|
|
pGen->popR1();
|
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-06-12 20:25:59 +02:00
|
|
|
void block(intptr_t l, bool outermostFunctionBlock) {
|
2009-05-20 21:12:06 +02:00
|
|
|
intptr_t 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(')');
|
2009-06-12 20:25:59 +02:00
|
|
|
block(l, false);
|
2009-05-11 23:49:29 +02:00
|
|
|
if (tok == TOK_ELSE) {
|
|
|
|
next();
|
2009-05-12 21:48:35 +02:00
|
|
|
n = pGen->gjmp(0); /* jmp */
|
|
|
|
pGen->gsym(a);
|
2009-06-12 20:25:59 +02:00
|
|
|
block(l, false);
|
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(')');
|
2009-06-12 20:25:59 +02:00
|
|
|
block((intptr_t) &a, false);
|
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-06-12 20:25:59 +02:00
|
|
|
if (! outermostFunctionBlock) {
|
|
|
|
mSymbolTable.pushLevel();
|
|
|
|
}
|
2009-05-10 23:09:03 +02:00
|
|
|
next();
|
2009-05-11 23:49:29 +02:00
|
|
|
/* declarations */
|
2009-06-05 04:56:13 +02:00
|
|
|
localDeclarations();
|
2009-06-12 04:06:24 +02:00
|
|
|
while (tok != '}' && tok != EOF)
|
2009-06-12 20:25:59 +02:00
|
|
|
block(l, false);
|
2009-06-12 04:06:24 +02:00
|
|
|
skip('}');
|
2009-06-12 20:25:59 +02:00
|
|
|
if (! outermostFunctionBlock) {
|
|
|
|
mSymbolTable.popLevel();
|
|
|
|
}
|
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-06-05 04:56:13 +02:00
|
|
|
typedef int Type;
|
|
|
|
static const Type TY_UNKNOWN = 0;
|
|
|
|
static const Type TY_INT = 1;
|
|
|
|
static const Type TY_CHAR = 2;
|
|
|
|
static const Type TY_VOID = 3;
|
|
|
|
static const int TY_BASE_TYPE_MASK = 0xf;
|
|
|
|
static const int TY_INDIRECTION_MASK = 0xf0;
|
|
|
|
static const int TY_INDIRECTION_SHIFT = 4;
|
|
|
|
static const int MAX_INDIRECTION_COUNT = 15;
|
|
|
|
|
|
|
|
Type getBaseType(Type t) {
|
|
|
|
return t & TY_BASE_TYPE_MASK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int getIndirectionCount(Type t) {
|
|
|
|
return (TY_INDIRECTION_MASK & t) >> TY_INDIRECTION_SHIFT;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setIndirectionCount(Type& t, int count) {
|
|
|
|
t = ((TY_INDIRECTION_MASK & (count << TY_INDIRECTION_SHIFT))
|
|
|
|
| (t & ~TY_INDIRECTION_MASK));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool acceptType(Type& t) {
|
|
|
|
t = TY_UNKNOWN;
|
|
|
|
if (tok == TOK_INT) {
|
|
|
|
t = TY_INT;
|
|
|
|
} else if (tok == TOK_CHAR) {
|
|
|
|
t = TY_CHAR;
|
|
|
|
} else if (tok == TOK_VOID) {
|
|
|
|
t = TY_VOID;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Type acceptPointerDeclaration(Type& base) {
|
|
|
|
Type t = base;
|
|
|
|
int indirectionCount = 0;
|
|
|
|
while (tok == '*' && indirectionCount <= MAX_INDIRECTION_COUNT) {
|
|
|
|
next();
|
|
|
|
indirectionCount++;
|
|
|
|
}
|
|
|
|
if (indirectionCount > MAX_INDIRECTION_COUNT) {
|
|
|
|
error("Too many levels of pointer. Max %d", MAX_INDIRECTION_COUNT);
|
|
|
|
}
|
|
|
|
setIndirectionCount(t, indirectionCount);
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
void expectType(Type& t) {
|
|
|
|
if (!acceptType(t)) {
|
|
|
|
error("Expected a type.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void checkSymbol() {
|
2009-06-11 23:29:47 +02:00
|
|
|
if (tok < TOK_SYMBOL) {
|
2009-06-05 04:56:13 +02:00
|
|
|
error("Expected a symbol");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-12 20:25:59 +02:00
|
|
|
void addGlobalSymbol() {
|
|
|
|
tok = (intptr_t) mSymbolTable.addGlobal(
|
|
|
|
new String(mTokenString));
|
|
|
|
reportIfDuplicate();
|
|
|
|
}
|
|
|
|
|
|
|
|
void reportIfDuplicate() {
|
|
|
|
if (!tok) {
|
|
|
|
error("Duplicate definition of %s", mTokenString.getUnwrapped());
|
2009-06-12 04:06:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-12 20:25:59 +02:00
|
|
|
void addLocalSymbol() {
|
2009-06-12 04:06:24 +02:00
|
|
|
tok = (intptr_t) mSymbolTable.addLocal(
|
|
|
|
new String(mTokenString));
|
2009-06-12 20:25:59 +02:00
|
|
|
reportIfDuplicate();
|
2009-06-12 04:06:24 +02:00
|
|
|
}
|
|
|
|
|
2009-06-05 04:56:13 +02:00
|
|
|
void localDeclarations() {
|
2009-05-20 21:12:06 +02:00
|
|
|
intptr_t a;
|
2009-06-05 04:56:13 +02:00
|
|
|
Type base;
|
|
|
|
|
|
|
|
while (acceptType(base)) {
|
2009-06-12 22:12:55 +02:00
|
|
|
while (tok != ';' && tok != EOF) {
|
2009-06-05 04:56:13 +02:00
|
|
|
Type t = acceptPointerDeclaration(t);
|
2009-06-12 20:25:59 +02:00
|
|
|
addLocalSymbol();
|
|
|
|
if (tok) {
|
|
|
|
loc = loc + 4;
|
|
|
|
*(int *) tok = -loc;
|
|
|
|
}
|
2009-05-10 23:09:03 +02:00
|
|
|
next();
|
2009-06-05 04:56:13 +02:00
|
|
|
if (tok == ',')
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
skip(';');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void globalDeclarations() {
|
|
|
|
while (tok != EOF) {
|
|
|
|
Type base;
|
|
|
|
expectType(base);
|
|
|
|
Type t = acceptPointerDeclaration(t);
|
2009-06-12 20:25:59 +02:00
|
|
|
if (tok == TOK_UNDEFINED_SYMBOL) {
|
|
|
|
addGlobalSymbol();
|
|
|
|
}
|
2009-06-12 04:06:24 +02:00
|
|
|
VariableInfo* name = (VariableInfo*) tok;
|
2009-06-12 20:25:59 +02:00
|
|
|
if (name && name->pAddress) {
|
|
|
|
error("Already defined global %s",
|
|
|
|
mTokenString.getUnwrapped());
|
|
|
|
}
|
2009-06-05 04:56:13 +02:00
|
|
|
next();
|
|
|
|
if (tok == ',' || tok == ';') {
|
|
|
|
// it's a variable declaration
|
|
|
|
for(;;) {
|
2009-06-12 20:25:59 +02:00
|
|
|
if (name) {
|
|
|
|
name->pAddress = (int*) allocGlobalSpace(4);
|
|
|
|
}
|
2009-06-05 04:56:13 +02:00
|
|
|
if (tok != ',') {
|
|
|
|
break;
|
2009-05-11 23:49:29 +02:00
|
|
|
}
|
2009-06-12 04:06:24 +02:00
|
|
|
skip(',');
|
2009-06-05 04:56:13 +02:00
|
|
|
t = acceptPointerDeclaration(t);
|
2009-06-12 20:25:59 +02:00
|
|
|
addGlobalSymbol();
|
2009-06-12 04:06:24 +02:00
|
|
|
name = (VariableInfo*) tok;
|
2009-06-05 04:56:13 +02:00
|
|
|
next();
|
2009-05-11 23:49:29 +02:00
|
|
|
}
|
|
|
|
skip(';');
|
|
|
|
} else {
|
2009-06-12 20:25:59 +02:00
|
|
|
if (name) {
|
|
|
|
/* patch forward references (XXX: does not work for function
|
|
|
|
pointers) */
|
|
|
|
pGen->gsym((int) name->pForward);
|
|
|
|
/* put function address */
|
|
|
|
name->pAddress = (void*) codeBuf.getPC();
|
|
|
|
}
|
2009-05-11 23:49:29 +02:00
|
|
|
skip('(');
|
2009-06-12 04:06:24 +02:00
|
|
|
mSymbolTable.pushLevel();
|
2009-06-05 04:56:13 +02:00
|
|
|
intptr_t a = 8;
|
2009-05-14 00:10:04 +02:00
|
|
|
int argCount = 0;
|
2009-06-12 04:06:24 +02:00
|
|
|
while (tok != ')' && tok != EOF) {
|
2009-06-05 04:56:13 +02:00
|
|
|
Type aType;
|
|
|
|
expectType(aType);
|
|
|
|
aType = acceptPointerDeclaration(aType);
|
2009-06-12 20:25:59 +02:00
|
|
|
addLocalSymbol();
|
|
|
|
if (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
|
|
|
}
|
2009-06-12 04:06:24 +02:00
|
|
|
skip(')');
|
2009-05-11 23:49:29 +02:00
|
|
|
rsym = loc = 0;
|
2009-05-14 00:10:04 +02:00
|
|
|
a = pGen->functionEntry(argCount);
|
2009-06-12 20:25:59 +02:00
|
|
|
block(0, true);
|
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-06-12 04:06:24 +02:00
|
|
|
mSymbolTable.popLevel();
|
2009-05-08 23:54:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-05-11 23:49:29 +02:00
|
|
|
|
2009-05-30 03:03:15 +02:00
|
|
|
char* allocGlobalSpace(int bytes) {
|
|
|
|
if (glo - pGlobalBase + bytes > ALLOC_SIZE) {
|
|
|
|
error("Global space exhausted");
|
2009-06-11 19:53:51 +02:00
|
|
|
return NULL;
|
2009-05-30 03:03:15 +02:00
|
|
|
}
|
|
|
|
char* result = glo;
|
|
|
|
glo += bytes;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-05-11 23:49:29 +02:00
|
|
|
void cleanup() {
|
|
|
|
if (pGlobalBase != 0) {
|
2009-05-30 03:03:15 +02:00
|
|
|
free(pGlobalBase);
|
2009-05-11 23:49:29 +02:00
|
|
|
pGlobalBase = 0;
|
|
|
|
}
|
|
|
|
if (pGen) {
|
|
|
|
delete pGen;
|
|
|
|
pGen = 0;
|
|
|
|
}
|
2009-05-22 21:06:27 +02:00
|
|
|
if (file) {
|
|
|
|
delete file;
|
|
|
|
file = 0;
|
|
|
|
}
|
2009-05-11 23:49:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void clear() {
|
|
|
|
tok = 0;
|
|
|
|
tokc = 0;
|
|
|
|
tokl = 0;
|
|
|
|
ch = 0;
|
|
|
|
rsym = 0;
|
|
|
|
loc = 0;
|
|
|
|
glo = 0;
|
|
|
|
dptr = 0;
|
|
|
|
dch = 0;
|
|
|
|
file = 0;
|
|
|
|
pGlobalBase = 0;
|
|
|
|
pGen = 0;
|
2009-06-05 01:23:40 +02:00
|
|
|
mPragmaStringCount = 0;
|
2009-05-11 23:49:29 +02:00
|
|
|
}
|
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) {
|
2009-05-20 02:12:17 +02:00
|
|
|
#ifdef PROVIDE_ARM_CODEGEN
|
2009-05-20 21:12:06 +02:00
|
|
|
if (! pGen && strcmp(architecture, "arm") == 0) {
|
2009-05-13 19:58:45 +02:00
|
|
|
pGen = new ARMCodeGenerator();
|
2009-05-20 21:12:06 +02:00
|
|
|
}
|
2009-05-20 02:12:17 +02:00
|
|
|
#endif
|
|
|
|
#ifdef PROVIDE_X86_CODEGEN
|
2009-05-20 21:12:06 +02:00
|
|
|
if (! pGen && strcmp(architecture, "x86") == 0) {
|
2009-05-13 19:58:45 +02:00
|
|
|
pGen = new X86CodeGenerator();
|
2009-05-20 21:12:06 +02:00
|
|
|
}
|
2009-05-20 02:12:17 +02:00
|
|
|
#endif
|
2009-05-20 21:12:06 +02:00
|
|
|
if (!pGen ) {
|
2009-05-29 22:53:44 +02:00
|
|
|
error("Unknown architecture %s\n", architecture);
|
2009-05-13 19:58:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pGen == NULL) {
|
2009-05-20 02:12:17 +02:00
|
|
|
#if defined(DEFAULT_ARM_CODEGEN)
|
2009-05-13 19:58:45 +02:00
|
|
|
pGen = new ARMCodeGenerator();
|
2009-05-20 02:12:17 +02:00
|
|
|
#elif defined(DEFAULT_X86_CODEGEN)
|
|
|
|
pGen = new X86CodeGenerator();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
if (pGen == NULL) {
|
2009-05-29 22:53:44 +02:00
|
|
|
error("No code generator defined.");
|
2009-06-11 19:53:51 +02:00
|
|
|
} else {
|
|
|
|
pGen->setErrorSink(this);
|
2009-05-13 19:58:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-20 02:12:17 +02:00
|
|
|
Compiler() {
|
2009-05-11 23:49:29 +02:00
|
|
|
clear();
|
|
|
|
}
|
2009-05-11 20:54:30 +02:00
|
|
|
|
2009-05-20 02:12:17 +02:00
|
|
|
~Compiler() {
|
2009-05-11 23:49:29 +02:00
|
|
|
cleanup();
|
|
|
|
}
|
2009-05-11 20:54:30 +02:00
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
int compile(const char* text, size_t textLength) {
|
2009-05-29 22:53:44 +02:00
|
|
|
int result;
|
2009-06-11 19:53:51 +02:00
|
|
|
|
|
|
|
cleanup();
|
|
|
|
clear();
|
|
|
|
codeBuf.init(ALLOC_SIZE);
|
|
|
|
setArchitecture(NULL);
|
|
|
|
if (!pGen) {
|
|
|
|
return -1;
|
|
|
|
}
|
2009-06-12 06:12:23 +02:00
|
|
|
#ifdef PROVIDE_TRACE_CODEGEN
|
|
|
|
pGen = new TraceCodeGenerator(pGen);
|
|
|
|
#endif
|
|
|
|
pGen->setErrorSink(this);
|
2009-06-11 19:53:51 +02:00
|
|
|
pGen->init(&codeBuf);
|
|
|
|
file = new TextInputStream(text, textLength);
|
|
|
|
pGlobalBase = (char*) calloc(1, ALLOC_SIZE);
|
|
|
|
glo = pGlobalBase;
|
|
|
|
inp();
|
|
|
|
next();
|
|
|
|
globalDeclarations();
|
2009-06-12 20:25:59 +02:00
|
|
|
checkForUndefinedForwardReferences();
|
2009-06-11 19:53:51 +02:00
|
|
|
result = pGen->finishCompile();
|
|
|
|
if (result == 0) {
|
|
|
|
if (mErrorBuf.len()) {
|
|
|
|
result = -2;
|
2009-05-29 22:53:44 +02:00
|
|
|
}
|
2009-05-20 21:12:06 +02:00
|
|
|
}
|
2009-05-29 22:53:44 +02:00
|
|
|
return result;
|
2009-05-08 23:54:15 +02:00
|
|
|
}
|
2009-05-11 20:54:30 +02:00
|
|
|
|
2009-06-12 20:25:59 +02:00
|
|
|
void checkForUndefinedForwardReferences() {
|
|
|
|
mSymbolTable.forEachGlobal(static_ufrcFn, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool static_ufrcFn(String* key, VariableInfo* value,
|
|
|
|
void* context) {
|
|
|
|
Compiler* pCompiler = (Compiler*) context;
|
|
|
|
return pCompiler->undefinedForwardReferenceCheck(key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool undefinedForwardReferenceCheck(String* key, VariableInfo* value) {
|
|
|
|
#if 0
|
|
|
|
fprintf(stderr, "%s 0x%8x 0x%08x\n", key->getUnwrapped(),
|
|
|
|
value->pAddress, value->pForward);
|
|
|
|
#endif
|
|
|
|
if (!value->pAddress && value->pForward) {
|
|
|
|
error("Undefined forward reference: %s", key->getUnwrapped());
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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-22 21:06:27 +02:00
|
|
|
/* Look through the symbol table to find a symbol.
|
|
|
|
* If found, return its value.
|
|
|
|
*/
|
|
|
|
void* lookup(const char* name) {
|
2009-06-12 04:06:24 +02:00
|
|
|
String string(name, -1, false);
|
|
|
|
VariableInfo* pVariableInfo = mSymbolTable.get(&string);
|
|
|
|
if (pVariableInfo) {
|
|
|
|
return pVariableInfo->pAddress;
|
2009-05-22 21:06:27 +02:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-06-05 01:23:40 +02:00
|
|
|
void getPragmas(ACCsizei* actualStringCount,
|
|
|
|
ACCsizei maxStringCount, ACCchar** strings) {
|
|
|
|
int stringCount = mPragmaStringCount;
|
|
|
|
if (actualStringCount) {
|
|
|
|
*actualStringCount = stringCount;
|
|
|
|
}
|
|
|
|
if (stringCount > maxStringCount) {
|
|
|
|
stringCount = maxStringCount;
|
|
|
|
}
|
|
|
|
if (strings) {
|
|
|
|
char* pPragmas = mPragmas.getUnwrapped();
|
|
|
|
while (stringCount-- > 0) {
|
|
|
|
*strings++ = pPragmas;
|
|
|
|
pPragmas += strlen(pPragmas) + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-29 22:53:44 +02:00
|
|
|
char* getErrorMessage() {
|
2009-06-05 01:23:40 +02:00
|
|
|
return mErrorBuf.getUnwrapped();
|
2009-05-29 22:53:44 +02:00
|
|
|
}
|
|
|
|
|
2009-05-11 23:49:29 +02:00
|
|
|
};
|
2009-05-11 20:54:30 +02:00
|
|
|
|
2009-05-20 02:12:17 +02:00
|
|
|
const char* Compiler::operatorChars =
|
2009-05-12 21:48:35 +02:00
|
|
|
"++--*@/@%@+@-@<<>><=>=<@>@==!=&&||&@^@|@~@!@";
|
|
|
|
|
2009-05-20 02:12:17 +02:00
|
|
|
const char Compiler::operatorLevel[] =
|
2009-05-12 21:48:35 +02:00
|
|
|
{11, 11, 1, 1, 1, 2, 2, 3, 3, 4, 4, 4, 4,
|
|
|
|
5, 5, /* ==, != */
|
|
|
|
9, 10, /* &&, || */
|
|
|
|
6, 7, 8, /* & ^ | */
|
|
|
|
2, 2 /* ~ ! */
|
|
|
|
};
|
|
|
|
|
2009-05-20 21:12:06 +02:00
|
|
|
#ifdef PROVIDE_ARM_CODEGEN
|
2009-05-20 02:12:17 +02:00
|
|
|
FILE* Compiler::ARMCodeGenerator::disasmOut;
|
2009-05-20 21:12:06 +02:00
|
|
|
#endif
|
2009-05-14 01:24:17 +02:00
|
|
|
|
2009-05-20 21:12:06 +02:00
|
|
|
#ifdef PROVIDE_X86_CODEGEN
|
2009-05-20 02:12:17 +02:00
|
|
|
const int Compiler::X86CodeGenerator::operatorHelper[] = {
|
2009-05-12 21:48:35 +02:00
|
|
|
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-20 21:12:06 +02:00
|
|
|
#endif
|
2009-05-12 21:48:35 +02:00
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
struct ACCscript {
|
|
|
|
ACCscript() {
|
|
|
|
text = 0;
|
|
|
|
textLength = 0;
|
|
|
|
accError = ACC_NO_ERROR;
|
|
|
|
}
|
2009-05-11 20:54:30 +02:00
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
~ACCscript() {
|
|
|
|
delete text;
|
|
|
|
}
|
2009-05-14 00:10:04 +02:00
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
void setError(ACCenum error) {
|
|
|
|
if (accError == ACC_NO_ERROR && error != ACC_NO_ERROR) {
|
|
|
|
accError = error;
|
2009-05-11 20:54:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
ACCenum getError() {
|
|
|
|
ACCenum result = accError;
|
|
|
|
accError = ACC_NO_ERROR;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Compiler compiler;
|
|
|
|
char* text;
|
|
|
|
int textLength;
|
|
|
|
ACCenum accError;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
ACCscript* accCreateScript() {
|
|
|
|
return new ACCscript();
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
ACCenum accGetError( ACCscript* script ) {
|
|
|
|
return script->getError();
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
void accDeleteScript(ACCscript* script) {
|
|
|
|
delete script;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
void accScriptSource(ACCscript* script,
|
|
|
|
ACCsizei count,
|
|
|
|
const ACCchar ** string,
|
|
|
|
const ACCint * length) {
|
|
|
|
int totalLength = 0;
|
|
|
|
for(int i = 0; i < count; i++) {
|
|
|
|
int len = -1;
|
|
|
|
const ACCchar* s = string[i];
|
|
|
|
if (length) {
|
|
|
|
len = length[i];
|
|
|
|
}
|
|
|
|
if (len < 0) {
|
|
|
|
len = strlen(s);
|
|
|
|
}
|
|
|
|
totalLength += len;
|
2009-05-11 20:54:30 +02:00
|
|
|
}
|
2009-05-22 21:06:27 +02:00
|
|
|
delete script->text;
|
|
|
|
char* text = new char[totalLength + 1];
|
|
|
|
script->text = text;
|
|
|
|
script->textLength = totalLength;
|
2009-05-27 21:25:55 +02:00
|
|
|
char* dest = text;
|
2009-05-22 21:06:27 +02:00
|
|
|
for(int i = 0; i < count; i++) {
|
|
|
|
int len = -1;
|
|
|
|
const ACCchar* s = string[i];
|
|
|
|
if (length) {
|
|
|
|
len = length[i];
|
|
|
|
}
|
|
|
|
if (len < 0) {
|
|
|
|
len = strlen(s);
|
|
|
|
}
|
2009-05-27 21:25:55 +02:00
|
|
|
memcpy(dest, s, len);
|
|
|
|
dest += len;
|
2009-05-20 02:12:17 +02:00
|
|
|
}
|
2009-05-22 21:06:27 +02:00
|
|
|
text[totalLength] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
void accCompileScript(ACCscript* script) {
|
|
|
|
int result = script->compiler.compile(script->text, script->textLength);
|
|
|
|
if (result) {
|
|
|
|
script->setError(ACC_INVALID_OPERATION);
|
2009-05-11 20:54:30 +02:00
|
|
|
}
|
2009-05-22 21:06:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
void accGetScriptiv(ACCscript* script,
|
|
|
|
ACCenum pname,
|
|
|
|
ACCint * params) {
|
|
|
|
switch (pname) {
|
|
|
|
case ACC_INFO_LOG_LENGTH:
|
|
|
|
*params = 0;
|
|
|
|
break;
|
2009-05-11 20:54:30 +02:00
|
|
|
}
|
2009-05-22 21:06:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
void accGetScriptInfoLog(ACCscript* script,
|
|
|
|
ACCsizei maxLength,
|
|
|
|
ACCsizei * length,
|
|
|
|
ACCchar * infoLog) {
|
2009-05-29 22:53:44 +02:00
|
|
|
char* message = script->compiler.getErrorMessage();
|
|
|
|
int messageLength = strlen(message) + 1;
|
2009-05-22 21:06:27 +02:00
|
|
|
if (length) {
|
2009-05-29 22:53:44 +02:00
|
|
|
*length = messageLength;
|
2009-05-14 01:24:17 +02:00
|
|
|
}
|
2009-05-29 22:53:44 +02:00
|
|
|
if (infoLog && maxLength > 0) {
|
|
|
|
int trimmedLength = maxLength < messageLength ?
|
|
|
|
maxLength : messageLength;
|
|
|
|
memcpy(infoLog, message, trimmedLength);
|
|
|
|
infoLog[trimmedLength] = 0;
|
2009-05-11 20:54:30 +02:00
|
|
|
}
|
2009-05-22 21:06:27 +02:00
|
|
|
}
|
2009-05-11 20:54:30 +02:00
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
extern "C"
|
|
|
|
void accGetScriptLabel(ACCscript* script, const ACCchar * name,
|
|
|
|
ACCvoid ** address) {
|
|
|
|
void* value = script->compiler.lookup(name);
|
|
|
|
if (value) {
|
|
|
|
*address = value;
|
|
|
|
} else {
|
|
|
|
script->setError(ACC_INVALID_VALUE);
|
|
|
|
}
|
2009-05-11 20:54:30 +02:00
|
|
|
}
|
2009-05-22 21:06:27 +02:00
|
|
|
|
2009-06-05 01:23:40 +02:00
|
|
|
extern "C"
|
|
|
|
void accGetPragmas(ACCscript* script, ACCsizei* actualStringCount,
|
|
|
|
ACCsizei maxStringCount, ACCchar** strings){
|
|
|
|
script->compiler.getPragmas(actualStringCount, maxStringCount, strings);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
} // namespace acc
|
|
|
|
|