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-08-18 01:54:56 +02:00
|
|
|
#define LOG_TAG "acc"
|
|
|
|
#include <cutils/log.h>
|
|
|
|
|
2009-05-11 04:59:24 +02:00
|
|
|
#include <ctype.h>
|
2009-06-10 00:53:47 +02:00
|
|
|
#include <errno.h>
|
2009-09-08 20:06:40 +02:00
|
|
|
#include <limits.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-09-08 20:06:40 +02:00
|
|
|
#include <unistd.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
|
|
|
|
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
|
|
|
|
|
2009-08-21 00:34:23 +02:00
|
|
|
#if (defined(__VFP_FP__) && !defined(__SOFTFP__))
|
|
|
|
#define ARM_USE_VFP
|
|
|
|
#endif
|
|
|
|
|
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__)
|
|
|
|
|
2009-06-18 04:13:52 +02:00
|
|
|
#define LOG_STACK(...) do {} while(0)
|
|
|
|
// #define LOG_STACK(...) fprintf (stderr, __VA_ARGS__)
|
|
|
|
|
2009-06-12 06:12:23 +02:00
|
|
|
// #define PROVIDE_TRACE_CODEGEN
|
|
|
|
|
2009-09-10 04:08:54 +02:00
|
|
|
// Uncomment to disable ARM peephole optimizations
|
|
|
|
// #define DISABLE_ARM_PEEPHOLE
|
|
|
|
|
2009-09-08 20:06:40 +02:00
|
|
|
// Uncomment to save input to a text file in DEBUG_DUMP_PATTERN
|
|
|
|
// #define DEBUG_SAVE_INPUT_TO_FILE
|
|
|
|
|
2009-09-08 20:46:42 +02:00
|
|
|
#ifdef DEBUG_SAVE_INPUT_TO_FILE
|
2009-09-08 20:06:40 +02:00
|
|
|
#ifdef ARM_USE_VFP
|
2009-09-08 20:46:42 +02:00
|
|
|
#define DEBUG_DUMP_PATTERN "/data/misc/acc_dump/%d.c"
|
2009-09-08 20:06:40 +02:00
|
|
|
#else
|
|
|
|
#define DEBUG_DUMP_PATTERN "/tmp/acc_dump/%d.c"
|
|
|
|
#endif
|
2009-09-08 20:46:42 +02:00
|
|
|
#endif
|
2009-09-08 20:06:40 +02:00
|
|
|
|
2009-08-18 01:54:56 +02:00
|
|
|
#define assert(b) assertImpl(b, __LINE__)
|
|
|
|
|
2009-05-11 20:54:30 +02:00
|
|
|
namespace acc {
|
|
|
|
|
2009-07-07 23:48:51 +02:00
|
|
|
// Subset of STL vector.
|
|
|
|
template<class E> class Vector {
|
|
|
|
public:
|
|
|
|
Vector() {
|
|
|
|
mpBase = 0;
|
|
|
|
mUsed = 0;
|
|
|
|
mSize = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
~Vector() {
|
|
|
|
if (mpBase) {
|
|
|
|
for(size_t i = 0; i < mUsed; i++) {
|
|
|
|
mpBase[mUsed].~E();
|
|
|
|
}
|
|
|
|
free(mpBase);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline E& operator[](size_t i) {
|
|
|
|
return mpBase[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
inline E& front() {
|
|
|
|
return mpBase[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
inline E& back() {
|
|
|
|
return mpBase[mUsed - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
void pop_back() {
|
|
|
|
mUsed -= 1;
|
|
|
|
mpBase[mUsed].~E();
|
|
|
|
}
|
|
|
|
|
|
|
|
void push_back(const E& item) {
|
|
|
|
* ensure(1) = item;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t size() {
|
|
|
|
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-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);
|
|
|
|
}
|
|
|
|
|
2009-07-09 01:59:18 +02:00
|
|
|
virtual ~ErrorSink() {}
|
2009-05-29 22:53:44 +02:00
|
|
|
virtual void verror(const char* fmt, va_list ap) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Compiler : public ErrorSink {
|
2009-07-07 23:48:51 +02:00
|
|
|
typedef int tokenid_t;
|
|
|
|
enum TypeTag {
|
2009-10-29 00:10:17 +01:00
|
|
|
TY_UNKNOWN = -1,
|
2009-08-03 23:42:57 +02:00
|
|
|
TY_INT, // 0
|
|
|
|
TY_CHAR, // 1
|
|
|
|
TY_SHORT, // 2
|
|
|
|
TY_VOID, // 3
|
|
|
|
TY_FLOAT, // 4
|
|
|
|
TY_DOUBLE, // 5
|
2009-08-04 23:56:09 +02:00
|
|
|
TY_POINTER, // 6
|
|
|
|
TY_ARRAY, // 7
|
|
|
|
TY_STRUCT, // 8
|
|
|
|
TY_FUNC, // 9
|
|
|
|
TY_PARAM // 10
|
2009-07-07 23:48:51 +02:00
|
|
|
};
|
|
|
|
|
2009-10-29 00:10:17 +01:00
|
|
|
enum StorageClass {
|
|
|
|
SC_DEFAULT, // 0
|
|
|
|
SC_AUTO, // 1
|
|
|
|
SC_REGISTER, // 2
|
|
|
|
SC_STATIC, // 3
|
|
|
|
SC_EXTERN, // 4
|
|
|
|
SC_TYPEDEF // 5
|
|
|
|
};
|
|
|
|
|
2009-07-07 23:48:51 +02:00
|
|
|
struct Type {
|
|
|
|
TypeTag tag;
|
2009-10-29 00:10:17 +01:00
|
|
|
StorageClass storageClass;
|
2009-08-27 01:15:07 +02:00
|
|
|
tokenid_t id; // For function arguments, global vars, local vars, struct elements
|
|
|
|
tokenid_t structTag; // For structs the name of the struct
|
|
|
|
int length; // length of array, offset of struct element. -1 means struct is forward defined
|
|
|
|
int alignment; // for structs only
|
|
|
|
Type* pHead; // For a struct this is the prototype struct.
|
2009-07-07 23:48:51 +02:00
|
|
|
Type* pTail;
|
|
|
|
};
|
2009-07-07 02:24:34 +02:00
|
|
|
|
2009-07-17 19:20:32 +02:00
|
|
|
enum ExpressionType {
|
|
|
|
ET_RVALUE,
|
|
|
|
ET_LVALUE
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ExpressionValue {
|
|
|
|
ExpressionValue() {
|
|
|
|
et = ET_RVALUE;
|
|
|
|
pType = NULL;
|
|
|
|
}
|
|
|
|
ExpressionType et;
|
|
|
|
Type* pType;
|
|
|
|
};
|
|
|
|
|
2009-09-10 04:08:54 +02:00
|
|
|
class ICodeBuf {
|
|
|
|
public:
|
|
|
|
virtual ~ICodeBuf() {}
|
|
|
|
virtual void init(int size) = 0;
|
|
|
|
virtual void setErrorSink(ErrorSink* pErrorSink) = 0;
|
|
|
|
virtual void o4(int n) = 0;
|
|
|
|
virtual void ob(int n) = 0;
|
|
|
|
virtual void* getBase() = 0;
|
|
|
|
virtual intptr_t getSize() = 0;
|
|
|
|
virtual intptr_t getPC() = 0;
|
|
|
|
// Call this before trying to modify code in the buffer.
|
|
|
|
virtual void flush() = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class CodeBuf : public ICodeBuf {
|
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
|
|
|
}
|
|
|
|
|
2009-09-10 04:08:54 +02:00
|
|
|
virtual ~CodeBuf() {
|
2009-05-11 23:49:29 +02:00
|
|
|
release();
|
|
|
|
}
|
2009-05-08 23:54:15 +02:00
|
|
|
|
2009-09-10 04:08:54 +02:00
|
|
|
virtual void init(int size) {
|
2009-05-11 23:49:29 +02:00
|
|
|
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-09-10 04:08:54 +02:00
|
|
|
virtual void setErrorSink(ErrorSink* pErrorSink) {
|
2009-05-29 22:53:44 +02:00
|
|
|
mErrorSink = pErrorSink;
|
|
|
|
}
|
|
|
|
|
2009-09-10 04:08:54 +02:00
|
|
|
virtual void o4(int n) {
|
2009-06-11 19:53:51 +02:00
|
|
|
if(check(4)) {
|
2009-09-10 04:08:54 +02:00
|
|
|
return;
|
2009-06-11 19:53:51 +02:00
|
|
|
}
|
2009-05-14 00:10:04 +02:00
|
|
|
* (int*) ind = n;
|
|
|
|
ind += 4;
|
|
|
|
}
|
|
|
|
|
2009-05-11 23:49:29 +02:00
|
|
|
/*
|
|
|
|
* Output a byte. Handles all values, 0..ff.
|
|
|
|
*/
|
2009-09-10 04:08:54 +02:00
|
|
|
virtual 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;
|
|
|
|
}
|
|
|
|
|
2009-09-10 04:08:54 +02:00
|
|
|
virtual void* getBase() {
|
2009-05-11 23:49:29 +02:00
|
|
|
return (void*) pProgramBase;
|
|
|
|
}
|
|
|
|
|
2009-09-10 04:08:54 +02:00
|
|
|
virtual intptr_t getSize() {
|
2009-05-11 23:49:29 +02:00
|
|
|
return ind - pProgramBase;
|
|
|
|
}
|
|
|
|
|
2009-09-10 04:08:54 +02:00
|
|
|
virtual intptr_t getPC() {
|
2009-05-20 21:12:06 +02:00
|
|
|
return (intptr_t) ind;
|
2009-05-11 23:49:29 +02:00
|
|
|
}
|
2009-09-10 04:08:54 +02:00
|
|
|
|
|
|
|
virtual void flush() {}
|
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:
|
2009-07-07 02:24:34 +02:00
|
|
|
* R0 - the accumulator.
|
2009-05-22 21:06:27 +02:00
|
|
|
* 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.
|
2009-07-07 02:24:34 +02:00
|
|
|
* The R0 register is not saved across function calls. The
|
2009-05-22 21:06:27 +02:00
|
|
|
* 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-07-07 23:48:51 +02:00
|
|
|
pushType();
|
2009-05-29 22:53:44 +02:00
|
|
|
}
|
2009-05-11 23:49:29 +02:00
|
|
|
virtual ~CodeGenerator() {}
|
|
|
|
|
2009-09-10 04:08:54 +02:00
|
|
|
virtual void init(ICodeBuf* 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-07-18 01:35:23 +02:00
|
|
|
/* Give the code generator some utility types so it can
|
|
|
|
* use its own types as needed for the results of some
|
|
|
|
* operations like gcmp.
|
|
|
|
*/
|
|
|
|
|
2009-07-14 03:40:08 +02:00
|
|
|
void setTypes(Type* pInt) {
|
|
|
|
mkpInt = pInt;
|
|
|
|
}
|
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
/* Emit a function prolog.
|
2009-07-10 07:00:24 +02:00
|
|
|
* pDecl is the function declaration, which gives the arguments.
|
2009-05-22 21:06:27 +02:00
|
|
|
* 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-07-10 07:00:24 +02:00
|
|
|
virtual int functionEntry(Type* pDecl) = 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.
|
|
|
|
*/
|
2009-07-10 07:00:24 +02:00
|
|
|
virtual void functionExit(Type* pDecl, int localVariableAddress,
|
2009-05-22 21:06:27 +02:00
|
|
|
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-07-18 01:35:23 +02:00
|
|
|
virtual void li(int i) = 0;
|
2009-07-07 03:33:20 +02:00
|
|
|
|
2009-07-08 22:04:41 +02:00
|
|
|
/* Load floating point value from global address. */
|
|
|
|
virtual void loadFloat(int address, Type* pType) = 0;
|
2009-05-13 19:58:45 +02:00
|
|
|
|
2009-08-27 01:15:07 +02:00
|
|
|
/* Add the struct offset in bytes to R0, change the type to pType */
|
|
|
|
virtual void addStructOffsetR0(int offset, Type* pType) = 0;
|
|
|
|
|
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-07-07 02:24:34 +02:00
|
|
|
/* Compare TOS against R0, and store the boolean result in R0.
|
|
|
|
* Pops TOS.
|
2009-05-22 21:06:27 +02:00
|
|
|
* op specifies the comparison.
|
|
|
|
*/
|
2009-07-18 01:35:23 +02:00
|
|
|
virtual void gcmp(int op) = 0;
|
2009-05-13 19:58:45 +02:00
|
|
|
|
2009-07-07 02:24:34 +02:00
|
|
|
/* Perform the arithmetic op specified by op. TOS is the
|
2009-05-22 21:06:27 +02:00
|
|
|
* left argument, R0 is the right argument.
|
2009-07-07 02:24:34 +02:00
|
|
|
* Pops TOS.
|
2009-05-22 21:06:27 +02:00
|
|
|
*/
|
2009-05-14 00:10:04 +02:00
|
|
|
virtual void genOp(int op) = 0;
|
2009-05-13 19:58:45 +02:00
|
|
|
|
2009-07-07 02:24:34 +02:00
|
|
|
/* Compare 0 against R0, and store the boolean result in R0.
|
|
|
|
* op specifies the comparison.
|
2009-05-22 21:06:27 +02:00
|
|
|
*/
|
2009-07-18 01:35:23 +02:00
|
|
|
virtual void gUnaryCmp(int op) = 0;
|
2009-05-13 19:58:45 +02:00
|
|
|
|
2009-07-07 02:24:34 +02:00
|
|
|
/* Perform the arithmetic op specified by op. 0 is the
|
|
|
|
* left argument, R0 is the right argument.
|
2009-05-22 21:06:27 +02:00
|
|
|
*/
|
2009-07-07 02:24:34 +02:00
|
|
|
virtual void genUnaryOp(int op) = 0;
|
2009-05-13 19:58:45 +02:00
|
|
|
|
2009-07-29 19:28:18 +02:00
|
|
|
/* Push R0 onto the stack. (Also known as "dup" for duplicate.)
|
2009-05-22 21:06:27 +02:00
|
|
|
*/
|
2009-07-07 02:24:34 +02:00
|
|
|
virtual void pushR0() = 0;
|
2009-05-13 19:58:45 +02:00
|
|
|
|
2009-07-29 19:28:18 +02:00
|
|
|
/* Turn R0, TOS into R0 TOS R0 */
|
|
|
|
|
|
|
|
virtual void over() = 0;
|
|
|
|
|
|
|
|
/* Pop R0 from the stack. (Also known as "drop")
|
2009-07-18 01:35:23 +02:00
|
|
|
*/
|
|
|
|
virtual void popR0() = 0;
|
|
|
|
|
2009-07-07 02:24:34 +02:00
|
|
|
/* Store R0 to the address stored in TOS.
|
|
|
|
* The TOS is popped.
|
2009-05-22 21:06:27 +02:00
|
|
|
*/
|
2009-07-18 01:35:23 +02:00
|
|
|
virtual void storeR0ToTOS() = 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.
|
|
|
|
*/
|
2009-07-18 01:35:23 +02:00
|
|
|
virtual void loadR0FromR0() = 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.
|
2009-07-30 01:22:26 +02:00
|
|
|
*
|
2009-07-31 04:06:34 +02:00
|
|
|
* et is ET_RVALUE for things like string constants, ET_LVALUE for
|
|
|
|
* variables.
|
2009-05-22 21:06:27 +02:00
|
|
|
*/
|
2009-07-31 04:06:34 +02:00
|
|
|
virtual void leaR0(int ea, Type* pPointerType, ExpressionType et) = 0;
|
2009-05-13 19:58:45 +02:00
|
|
|
|
2009-07-30 01:22:26 +02:00
|
|
|
/* Load the pc-relative address of a forward-referenced variable to R0.
|
|
|
|
* Return the address of the 4-byte constant so that it can be filled
|
|
|
|
* in later.
|
|
|
|
*/
|
|
|
|
virtual int leaForward(int ea, Type* pPointerType) = 0;
|
|
|
|
|
2009-07-07 23:48:51 +02:00
|
|
|
/**
|
|
|
|
* Convert R0 to the given type.
|
|
|
|
*/
|
2009-08-04 23:56:09 +02:00
|
|
|
|
|
|
|
void convertR0(Type* pType) {
|
|
|
|
convertR0Imp(pType, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void castR0(Type* pType) {
|
|
|
|
convertR0Imp(pType, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void convertR0Imp(Type* pType, bool isCast) = 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.
|
2009-07-08 22:04:41 +02:00
|
|
|
* Returns stack size of object (typically 4 or 8 bytes)
|
2009-05-22 21:06:27 +02:00
|
|
|
*/
|
2009-07-17 03:24:47 +02:00
|
|
|
virtual size_t storeR0ToArg(int l, Type* pArgType) = 0;
|
2009-05-22 21:06:27 +02:00
|
|
|
|
|
|
|
/* 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-07-10 07:00:24 +02:00
|
|
|
virtual void endFunctionCallArguments(Type* pDecl, 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-07-07 23:48:51 +02:00
|
|
|
virtual int callForward(int symbol, Type* pFunc) = 0;
|
2009-05-13 19:58:45 +02:00
|
|
|
|
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-07-07 23:48:51 +02:00
|
|
|
virtual void callIndirect(int l, Type* pFunc) = 0;
|
2009-05-13 19:58:45 +02:00
|
|
|
|
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-07-10 07:00:24 +02:00
|
|
|
virtual void adjustStackAfterCall(Type* pDecl, int l, bool isIndirect) = 0;
|
2009-05-13 19:58:45 +02:00
|
|
|
|
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-07-30 01:22:26 +02:00
|
|
|
/* Resolve a forward reference function at the current PC.
|
|
|
|
* t is the head of a
|
|
|
|
* linked list of addresses to patch.
|
|
|
|
* (Like gsym, but using absolute address, not PC relative address.)
|
|
|
|
*/
|
|
|
|
virtual void resolveForward(int t) = 0;
|
|
|
|
|
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-07-07 02:24:34 +02:00
|
|
|
/**
|
2009-07-09 01:48:41 +02:00
|
|
|
* Memory alignment (in bytes) for this type of data
|
2009-07-07 02:24:34 +02:00
|
|
|
*/
|
2009-07-10 07:00:24 +02:00
|
|
|
virtual size_t alignmentOf(Type* type) = 0;
|
2009-07-07 02:24:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Array element alignment (in bytes) for this type of data.
|
|
|
|
*/
|
|
|
|
virtual size_t sizeOf(Type* type) = 0;
|
|
|
|
|
2009-07-29 19:28:18 +02:00
|
|
|
virtual Type* getR0Type() {
|
2009-07-17 19:20:32 +02:00
|
|
|
return mExpressionStack.back().pType;
|
2009-07-08 22:04:41 +02:00
|
|
|
}
|
|
|
|
|
2009-07-29 19:28:18 +02:00
|
|
|
virtual ExpressionType getR0ExpressionType() {
|
2009-07-24 03:56:20 +02:00
|
|
|
return mExpressionStack.back().et;
|
|
|
|
}
|
|
|
|
|
2009-07-29 19:28:18 +02:00
|
|
|
virtual void setR0ExpressionType(ExpressionType et) {
|
2009-07-24 03:56:20 +02:00
|
|
|
mExpressionStack.back().et = et;
|
|
|
|
}
|
|
|
|
|
2009-07-29 19:28:18 +02:00
|
|
|
virtual size_t getExpressionStackDepth() {
|
|
|
|
return mExpressionStack.size();
|
|
|
|
}
|
|
|
|
|
2009-07-31 04:06:34 +02:00
|
|
|
virtual void forceR0RVal() {
|
|
|
|
if (getR0ExpressionType() == ET_LVALUE) {
|
|
|
|
loadR0FromR0();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-09-10 04:08:54 +02:00
|
|
|
void o4(int data) {
|
|
|
|
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
|
|
|
|
2009-09-10 04:08:54 +02:00
|
|
|
void flush() {
|
|
|
|
pCodeBuf->flush();
|
|
|
|
}
|
|
|
|
|
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-07-07 02:24:34 +02:00
|
|
|
|
2009-08-18 01:54:56 +02:00
|
|
|
void assertImpl(bool test, int line) {
|
2009-07-07 02:24:34 +02:00
|
|
|
if (!test) {
|
2009-08-18 01:54:56 +02:00
|
|
|
error("code generator assertion failed at line %s:%d.", __FILE__, line);
|
|
|
|
LOGD("code generator assertion failed at line %s:%d.", __FILE__, line);
|
2009-07-08 22:04:41 +02:00
|
|
|
* (char*) 0 = 0;
|
2009-07-07 02:24:34 +02:00
|
|
|
}
|
|
|
|
}
|
2009-07-07 23:48:51 +02:00
|
|
|
|
|
|
|
void setR0Type(Type* pType) {
|
2009-07-29 19:28:18 +02:00
|
|
|
assert(pType != NULL);
|
2009-07-17 19:20:32 +02:00
|
|
|
mExpressionStack.back().pType = pType;
|
2009-07-31 04:06:34 +02:00
|
|
|
mExpressionStack.back().et = ET_RVALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setR0Type(Type* pType, ExpressionType et) {
|
|
|
|
assert(pType != NULL);
|
|
|
|
mExpressionStack.back().pType = pType;
|
|
|
|
mExpressionStack.back().et = et;
|
2009-07-07 23:48:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Type* getTOSType() {
|
2009-07-17 19:20:32 +02:00
|
|
|
return mExpressionStack[mExpressionStack.size()-2].pType;
|
2009-07-07 23:48:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void pushType() {
|
2009-07-17 19:20:32 +02:00
|
|
|
if (mExpressionStack.size()) {
|
|
|
|
mExpressionStack.push_back(mExpressionStack.back());
|
|
|
|
} else {
|
|
|
|
mExpressionStack.push_back(ExpressionValue());
|
|
|
|
}
|
|
|
|
|
2009-07-07 23:48:51 +02:00
|
|
|
}
|
|
|
|
|
2009-07-29 19:28:18 +02:00
|
|
|
void overType() {
|
|
|
|
size_t size = mExpressionStack.size();
|
|
|
|
if (size >= 2) {
|
|
|
|
mExpressionStack.push_back(mExpressionStack.back());
|
|
|
|
mExpressionStack[size-1] = mExpressionStack[size-2];
|
|
|
|
mExpressionStack[size-2] = mExpressionStack[size];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-07 23:48:51 +02:00
|
|
|
void popType() {
|
|
|
|
mExpressionStack.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool bitsSame(Type* pA, Type* pB) {
|
|
|
|
return collapseType(pA->tag) == collapseType(pB->tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
TypeTag collapseType(TypeTag tag) {
|
|
|
|
static const TypeTag collapsedTag[] = {
|
2009-08-03 23:42:57 +02:00
|
|
|
TY_INT,
|
|
|
|
TY_INT,
|
|
|
|
TY_INT,
|
|
|
|
TY_VOID,
|
|
|
|
TY_FLOAT,
|
|
|
|
TY_DOUBLE,
|
|
|
|
TY_INT,
|
|
|
|
TY_INT,
|
|
|
|
TY_VOID,
|
|
|
|
TY_VOID,
|
|
|
|
TY_VOID
|
|
|
|
};
|
2009-07-07 23:48:51 +02:00
|
|
|
return collapsedTag[tag];
|
|
|
|
}
|
|
|
|
|
2009-07-08 22:04:41 +02:00
|
|
|
TypeTag collapseTypeR0() {
|
|
|
|
return collapseType(getR0Type()->tag);
|
|
|
|
}
|
|
|
|
|
2009-08-04 23:56:09 +02:00
|
|
|
static bool isFloatType(Type* pType) {
|
2009-07-08 23:51:31 +02:00
|
|
|
return isFloatTag(pType->tag);
|
|
|
|
}
|
|
|
|
|
2009-08-04 23:56:09 +02:00
|
|
|
static bool isFloatTag(TypeTag tag) {
|
2009-07-08 22:04:41 +02:00
|
|
|
return tag == TY_FLOAT || tag == TY_DOUBLE;
|
|
|
|
}
|
|
|
|
|
2009-08-04 23:56:09 +02:00
|
|
|
static bool isPointerType(Type* pType) {
|
|
|
|
return isPointerTag(pType->tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isPointerTag(TypeTag tag) {
|
|
|
|
return tag == TY_POINTER || tag == TY_ARRAY;
|
|
|
|
}
|
|
|
|
|
|
|
|
Type* getPointerArithmeticResultType(Type* a, Type* b) {
|
|
|
|
TypeTag aTag = a->tag;
|
|
|
|
TypeTag bTag = b->tag;
|
|
|
|
if (aTag == TY_POINTER) {
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
if (bTag == TY_POINTER) {
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
if (aTag == TY_ARRAY) {
|
|
|
|
return a->pTail;
|
|
|
|
}
|
|
|
|
if (bTag == TY_ARRAY) {
|
|
|
|
return b->pTail;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-07-14 03:40:08 +02:00
|
|
|
Type* mkpInt;
|
|
|
|
|
2009-05-11 23:49:29 +02:00
|
|
|
private:
|
2009-07-17 19:20:32 +02:00
|
|
|
Vector<ExpressionValue> mExpressionStack;
|
2009-09-10 04:08:54 +02:00
|
|
|
ICodeBuf* 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-09-10 04:08:54 +02:00
|
|
|
static size_t rotateRight(size_t n, size_t rotate) {
|
|
|
|
return (n >> rotate) | (n << (32 - rotate));
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t rotateLeft(size_t n, size_t rotate) {
|
|
|
|
return (n << rotate) | (n >> (32 - rotate));
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool encode12BitImmediate(size_t immediate, size_t* pResult) {
|
|
|
|
for(size_t i = 0; i < 16; i++) {
|
|
|
|
size_t rotate = i * 2;
|
|
|
|
size_t mask = rotateRight(0xff, rotate);
|
|
|
|
if ((immediate | mask) == mask) {
|
|
|
|
size_t bits8 = rotateLeft(immediate, rotate);
|
|
|
|
// assert(bits8 <= 0xff);
|
|
|
|
*pResult = (i << 8) | bits8;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t decode12BitImmediate(size_t immediate) {
|
|
|
|
size_t data = immediate & 0xff;
|
|
|
|
size_t rotate = 2 * ((immediate >> 8) & 0xf);
|
|
|
|
return rotateRight(data, rotate);
|
|
|
|
}
|
|
|
|
|
2009-09-10 23:01:58 +02:00
|
|
|
static bool isPowerOfTwo(size_t n) {
|
|
|
|
return (n != 0) & ((n & (n-1)) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t log2(size_t n) {
|
|
|
|
int result = 0;
|
|
|
|
while (n >>= 1) {
|
|
|
|
result++;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-09-10 04:08:54 +02:00
|
|
|
class ARMCodeBuf : public ICodeBuf {
|
|
|
|
ICodeBuf* mpBase;
|
|
|
|
ErrorSink* mErrorSink;
|
|
|
|
|
|
|
|
class CircularQueue {
|
|
|
|
static const int SIZE = 16; // Must be power of 2
|
|
|
|
static const int MASK = SIZE-1;
|
|
|
|
unsigned int mBuf[SIZE];
|
|
|
|
int mHead;
|
|
|
|
int mCount;
|
|
|
|
|
|
|
|
public:
|
|
|
|
CircularQueue() {
|
|
|
|
mHead = 0;
|
|
|
|
mCount = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void pushBack(unsigned int data) {
|
|
|
|
mBuf[(mHead + mCount) & MASK] = data;
|
|
|
|
mCount += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int popFront() {
|
|
|
|
unsigned int result = mBuf[mHead];
|
|
|
|
mHead = (mHead + 1) & MASK;
|
|
|
|
mCount -= 1;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void popBack(int n) {
|
|
|
|
mCount -= n;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int count() {
|
|
|
|
return mCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool empty() {
|
|
|
|
return mCount == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool full() {
|
|
|
|
return mCount == SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The valid indexes are 1 - count() to 0
|
|
|
|
unsigned int operator[](int i) {
|
|
|
|
return mBuf[(mHead + mCount + i) & MASK];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
CircularQueue mQ;
|
|
|
|
|
|
|
|
void error(const char* fmt,...) {
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
mErrorSink->verror(fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
void flush() {
|
|
|
|
while (!mQ.empty()) {
|
|
|
|
mpBase->o4(mQ.popFront());
|
|
|
|
}
|
|
|
|
mpBase->flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
ARMCodeBuf(ICodeBuf* pBase) {
|
|
|
|
mpBase = pBase;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~ARMCodeBuf() {
|
|
|
|
delete mpBase;
|
|
|
|
}
|
|
|
|
|
|
|
|
void init(int size) {
|
|
|
|
mpBase->init(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setErrorSink(ErrorSink* pErrorSink) {
|
|
|
|
mErrorSink = pErrorSink;
|
|
|
|
mpBase->setErrorSink(pErrorSink);
|
|
|
|
}
|
|
|
|
|
|
|
|
void o4(int n) {
|
|
|
|
if (mQ.full()) {
|
|
|
|
mpBase->o4(mQ.popFront());
|
|
|
|
}
|
|
|
|
mQ.pushBack(n);
|
|
|
|
|
|
|
|
#ifndef DISABLE_ARM_PEEPHOLE
|
|
|
|
// Peephole check
|
|
|
|
bool didPeep;
|
|
|
|
do {
|
2009-09-10 23:01:58 +02:00
|
|
|
static const unsigned int opMask = 0x01e00000;
|
|
|
|
static const unsigned int immediateMask = 0x00000fff;
|
|
|
|
static const unsigned int BMask = 0x00400000;
|
2009-09-10 04:08:54 +02:00
|
|
|
didPeep = false;
|
|
|
|
if (mQ.count() >= 4) {
|
|
|
|
|
|
|
|
// Operand by a small constant
|
|
|
|
// push;mov #imm;pop;op ==> op #imm
|
|
|
|
|
2009-09-19 00:03:03 +02:00
|
|
|
if (mQ[-4] == 0xe92d0001 && // stmfd r13!, {r0}
|
|
|
|
(mQ[-3] & ~immediateMask) == 0xe3a00000 && // mov r0, #X
|
|
|
|
mQ[-2] == 0xe8bd0002 && // ldmea r13!, {r1}
|
|
|
|
(mQ[-1] & ~opMask) == (0xe0810000 & ~opMask)) { // OP r0, r1, r0
|
2009-09-10 04:08:54 +02:00
|
|
|
unsigned int movConst = mQ[-3];
|
|
|
|
unsigned int op = mQ[-1];
|
|
|
|
unsigned int combined = 0xe2000000 | (op & opMask) | (movConst & immediateMask);
|
|
|
|
// fprintf(stderr, "op %x movConst %x combined %x\n", op, movConst, combined);
|
|
|
|
if (! (combined == 0xe2800000 || combined == 0xe2400000)) { // add/sub #0
|
|
|
|
mQ.popBack(4);
|
|
|
|
mQ.pushBack(combined);
|
|
|
|
didPeep = true;
|
|
|
|
} else {
|
|
|
|
mQ.popBack(4);
|
|
|
|
didPeep = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load local variable
|
2009-09-10 23:01:58 +02:00
|
|
|
// sub r0,r11,#imm;ldr/ldrb r0,[r0] ==> ldr/ldrb r0, [r11,#-imm]
|
2009-09-10 04:08:54 +02:00
|
|
|
if (mQ.count() >= 2) {
|
2009-09-10 23:01:58 +02:00
|
|
|
if ((mQ[-2] & ~immediateMask) == 0xe24b0000) { // sub r0,r11,#imm
|
|
|
|
const unsigned int encodedImmediate = mQ[-2] & immediateMask;
|
|
|
|
const unsigned int ld = mQ[-1];
|
|
|
|
if ((ld & ~BMask) == 0xe5900000) { // ldr{b} r0, [r0]
|
|
|
|
unsigned int combined = encodedImmediate | (0xE51B0000 | (ld & BMask)); // ldr r0, [r11, #-0]
|
|
|
|
mQ.popBack(2);
|
|
|
|
mQ.pushBack(combined);
|
|
|
|
didPeep = true;
|
2009-09-19 00:03:03 +02:00
|
|
|
} else if (ld == 0xedd07a00) { // ldcl p10, c7, [r0, #0x000]
|
2009-09-10 23:01:58 +02:00
|
|
|
unsigned int decodedImmediate = decode12BitImmediate(encodedImmediate);
|
|
|
|
if (decodedImmediate <= 1020 && ((decodedImmediate & 3) == 0)) {
|
2009-09-19 00:03:03 +02:00
|
|
|
unsigned int combined = (decodedImmediate >> 2) | 0xed5b7a00; // ldcl p10, c7, [r11, #-0]
|
2009-09-10 23:01:58 +02:00
|
|
|
mQ.popBack(2);
|
|
|
|
mQ.pushBack(combined);
|
|
|
|
didPeep = true;
|
|
|
|
}
|
|
|
|
}
|
2009-09-10 04:08:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Constant array lookup
|
|
|
|
|
|
|
|
if (mQ.count() >= 6 &&
|
|
|
|
mQ[-6] == 0xe92d0001 && // stmfd r13!, {r0}
|
|
|
|
(mQ[-5] & ~immediateMask)== 0xe3a00000 && // mov r0, #0x00000001
|
|
|
|
mQ[-4] == 0xe8bd0002 && // ldmea r13!, {r1}
|
|
|
|
(mQ[-3] & ~immediateMask)== 0xe3a02000 && // mov r2, #0x00000004
|
|
|
|
mQ[-2] == 0xe0000092 && // mul r0, r2, r0
|
|
|
|
mQ[-1] == 0xe0810000) { // add r0, r1, r0
|
|
|
|
unsigned int mov1 = mQ[-5];
|
|
|
|
unsigned int mov2 = mQ[-3];
|
|
|
|
unsigned int const1 = decode12BitImmediate(mov1);
|
|
|
|
unsigned int const2 = decode12BitImmediate(mov2);
|
|
|
|
unsigned int comboConst = const1 * const2;
|
|
|
|
size_t immediate = 0;
|
|
|
|
if (encode12BitImmediate(comboConst, &immediate)) {
|
|
|
|
mQ.popBack(6);
|
|
|
|
unsigned int add = immediate | 0xE2800000; // add r0, r0, #n
|
|
|
|
if (comboConst) {
|
|
|
|
mQ.pushBack(add);
|
|
|
|
}
|
|
|
|
didPeep = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-10 23:01:58 +02:00
|
|
|
// Pointer arithmetic with a stride that is a power of two
|
|
|
|
|
|
|
|
if (mQ.count() >= 3 &&
|
2009-09-19 00:03:03 +02:00
|
|
|
(mQ[-3] & ~ immediateMask) == 0xe3a02000 && // mov r2, #stride
|
|
|
|
mQ[-2] == 0xe0000092 && // mul r0, r2, r0
|
2009-09-10 23:01:58 +02:00
|
|
|
mQ[-1] == 0xe0810000) { // add r0, r1, r0
|
|
|
|
int stride = decode12BitImmediate(mQ[-3]);
|
|
|
|
if (isPowerOfTwo(stride)) {
|
|
|
|
mQ.popBack(3);
|
|
|
|
unsigned int add = 0xe0810000 | (log2(stride) << 7); // add r0, r1, r0, LSL #log2(stride)
|
|
|
|
mQ.pushBack(add);
|
|
|
|
didPeep = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-10 04:08:54 +02:00
|
|
|
} while (didPeep);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void ob(int n) {
|
|
|
|
error("ob() not supported.");
|
|
|
|
}
|
|
|
|
|
|
|
|
void* getBase() {
|
|
|
|
flush();
|
|
|
|
return mpBase->getBase();
|
|
|
|
}
|
|
|
|
|
|
|
|
intptr_t getSize() {
|
|
|
|
flush();
|
|
|
|
return mpBase->getSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
intptr_t getPC() {
|
|
|
|
flush();
|
|
|
|
return mpBase->getPC();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-05-13 19:58:45 +02:00
|
|
|
class ARMCodeGenerator : public CodeGenerator {
|
|
|
|
public:
|
2009-08-21 00:34:23 +02:00
|
|
|
ARMCodeGenerator() {
|
|
|
|
#ifdef ARM_USE_VFP
|
2009-09-09 22:19:34 +02:00
|
|
|
// LOGD("Using ARM VFP hardware floating point.");
|
2009-08-21 00:34:23 +02:00
|
|
|
#else
|
2009-09-09 22:19:34 +02:00
|
|
|
// LOGD("Using ARM soft floating point.");
|
2009-08-21 00:34:23 +02:00
|
|
|
#endif
|
|
|
|
}
|
2009-06-18 04:13:52 +02:00
|
|
|
|
2009-05-13 19:58:45 +02:00
|
|
|
virtual ~ARMCodeGenerator() {}
|
|
|
|
|
|
|
|
/* returns address to patch with local variable size
|
|
|
|
*/
|
2009-07-10 07:00:24 +02:00
|
|
|
virtual int functionEntry(Type* pDecl) {
|
2009-06-18 04:13:52 +02:00
|
|
|
mStackUse = 0;
|
2009-05-15 00:42:26 +02:00
|
|
|
// sp -> arg4 arg5 ...
|
|
|
|
// Push our register-based arguments back on the stack
|
2009-07-10 07:00:24 +02:00
|
|
|
int regArgCount = calcRegArgCount(pDecl);
|
|
|
|
if (regArgCount > 0) {
|
2009-06-18 04:13:52 +02:00
|
|
|
mStackUse += regArgCount * 4;
|
2009-07-10 07:00:24 +02:00
|
|
|
o4(0xE92D0000 | ((1 << regArgCount) - 1)); // stmfd sp!, {}
|
2009-05-15 00:42:26 +02:00
|
|
|
}
|
|
|
|
// sp -> arg0 arg1 ...
|
|
|
|
o4(0xE92D4800); // stmfd sp!, {fp, lr}
|
2009-06-18 04:13:52 +02:00
|
|
|
mStackUse += 2 * 4;
|
2009-05-15 00:42:26 +02:00
|
|
|
// sp, fp -> oldfp, retadr, arg0 arg1 ....
|
|
|
|
o4(0xE1A0B00D); // mov fp, sp
|
2009-06-18 04:13:52 +02:00
|
|
|
LOG_STACK("functionEntry: %d\n", mStackUse);
|
2009-09-10 04:08:54 +02:00
|
|
|
int pc = getPC();
|
|
|
|
o4(0xE24DD000); // sub sp, sp, # <local variables>
|
2009-06-18 04:13:52 +02:00
|
|
|
// We don't know how many local variables we are going to use,
|
|
|
|
// but we will round the allocation up to a multiple of
|
|
|
|
// STACK_ALIGNMENT, so it won't affect the stack alignment.
|
2009-09-10 04:08:54 +02:00
|
|
|
return pc;
|
2009-05-14 00:10:04 +02:00
|
|
|
}
|
|
|
|
|
2009-07-10 07:00:24 +02:00
|
|
|
virtual void functionExit(Type* pDecl, int localVariableAddress, int localVariableSize) {
|
2009-06-18 04:13:52 +02:00
|
|
|
// Round local variable size up to a multiple of stack alignment
|
|
|
|
localVariableSize = ((localVariableSize + STACK_ALIGNMENT - 1) /
|
|
|
|
STACK_ALIGNMENT) * STACK_ALIGNMENT;
|
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;
|
|
|
|
|
2009-08-21 00:34:23 +02:00
|
|
|
#ifdef ARM_USE_VFP
|
|
|
|
{
|
2009-08-25 21:23:43 +02:00
|
|
|
Type* pReturnType = pDecl->pHead;
|
|
|
|
switch(pReturnType->tag) {
|
|
|
|
case TY_FLOAT:
|
|
|
|
o4(0xEE170A90); // fmrs r0, s15
|
|
|
|
break;
|
|
|
|
case TY_DOUBLE:
|
|
|
|
o4(0xEC510B17); // fmrrd r0, r1, d7
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2009-08-21 00:34:23 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-05-15 00:42:26 +02:00
|
|
|
// 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 ....
|
2009-07-10 07:00:24 +02:00
|
|
|
|
|
|
|
// We store the PC into the lr so we can adjust the sp before
|
|
|
|
// returning. We need to pull off the registers we pushed
|
|
|
|
// earlier. We don't need to actually store them anywhere,
|
|
|
|
// just adjust the stack.
|
|
|
|
int regArgCount = calcRegArgCount(pDecl);
|
|
|
|
if (regArgCount) {
|
2009-05-15 00:42:26 +02:00
|
|
|
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-07-18 01:35:23 +02:00
|
|
|
virtual void li(int t) {
|
2009-07-14 03:40:08 +02:00
|
|
|
liReg(t, 0);
|
2009-07-18 01:35:23 +02:00
|
|
|
setR0Type(mkpInt);
|
2009-05-13 19:58:45 +02:00
|
|
|
}
|
|
|
|
|
2009-07-08 22:04:41 +02:00
|
|
|
virtual void loadFloat(int address, Type* pType) {
|
2009-07-07 23:48:51 +02:00
|
|
|
setR0Type(pType);
|
2009-07-10 07:00:24 +02:00
|
|
|
// Global, absolute address
|
|
|
|
o4(0xE59F0000); // ldr r0, .L1
|
|
|
|
o4(0xEA000000); // b .L99
|
|
|
|
o4(address); // .L1: .word ea
|
|
|
|
// .L99:
|
|
|
|
|
|
|
|
switch (pType->tag) {
|
|
|
|
case TY_FLOAT:
|
2009-08-21 00:34:23 +02:00
|
|
|
#ifdef ARM_USE_VFP
|
2009-08-25 21:23:43 +02:00
|
|
|
o4(0xEDD07A00); // flds s15, [r0]
|
2009-08-21 00:34:23 +02:00
|
|
|
#else
|
2009-07-10 07:00:24 +02:00
|
|
|
o4(0xE5900000); // ldr r0, [r0]
|
2009-08-21 00:34:23 +02:00
|
|
|
#endif
|
2009-07-10 07:00:24 +02:00
|
|
|
break;
|
|
|
|
case TY_DOUBLE:
|
2009-08-21 00:34:23 +02:00
|
|
|
#ifdef ARM_USE_VFP
|
2009-08-25 21:23:43 +02:00
|
|
|
o4(0xED907B00); // fldd d7, [r0]
|
2009-08-21 00:34:23 +02:00
|
|
|
#else
|
2009-07-10 07:00:24 +02:00
|
|
|
o4(0xE1C000D0); // ldrd r0, [r0]
|
2009-08-21 00:34:23 +02:00
|
|
|
#endif
|
2009-07-10 07:00:24 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
break;
|
|
|
|
}
|
2009-07-07 03:33:20 +02:00
|
|
|
}
|
|
|
|
|
2009-08-27 01:15:07 +02:00
|
|
|
|
|
|
|
virtual void addStructOffsetR0(int offset, Type* pType) {
|
|
|
|
if (offset) {
|
|
|
|
size_t immediate = 0;
|
|
|
|
if (encode12BitImmediate(offset, &immediate)) {
|
|
|
|
o4(0xE2800000 | immediate); // add r0, r0, #offset
|
|
|
|
} else {
|
|
|
|
error("structure offset out of range: %d", offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setR0Type(pType, ET_LVALUE);
|
|
|
|
}
|
|
|
|
|
2009-05-13 19:58:45 +02:00
|
|
|
virtual int gjmp(int t) {
|
2009-09-10 04:08:54 +02:00
|
|
|
int pc = getPC();
|
|
|
|
o4(0xEA000000 | encodeAddress(t)); // b .L33
|
|
|
|
return pc;
|
2009-05-13 19:58:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* l = 0: je, l == 1: jne */
|
|
|
|
virtual int gtst(bool l, int t) {
|
2009-07-10 07:00:24 +02:00
|
|
|
Type* pR0Type = getR0Type();
|
|
|
|
TypeTag tagR0 = pR0Type->tag;
|
|
|
|
switch(tagR0) {
|
|
|
|
case TY_FLOAT:
|
2009-08-21 00:34:23 +02:00
|
|
|
#ifdef ARM_USE_VFP
|
2009-08-25 21:23:43 +02:00
|
|
|
o4(0xEEF57A40); // fcmpzs s15
|
|
|
|
o4(0xEEF1FA10); // fmstat
|
2009-08-21 00:34:23 +02:00
|
|
|
#else
|
2009-07-10 07:00:24 +02:00
|
|
|
callRuntime((void*) runtime_is_non_zero_f);
|
2009-08-21 00:34:23 +02:00
|
|
|
o4(0xE3500000); // cmp r0,#0
|
|
|
|
#endif
|
2009-07-10 07:00:24 +02:00
|
|
|
break;
|
|
|
|
case TY_DOUBLE:
|
2009-08-21 00:34:23 +02:00
|
|
|
#ifdef ARM_USE_VFP
|
2009-08-25 21:23:43 +02:00
|
|
|
o4(0xEEB57B40); // fcmpzd d7
|
|
|
|
o4(0xEEF1FA10); // fmstat
|
2009-08-21 00:34:23 +02:00
|
|
|
#else
|
2009-07-10 07:00:24 +02:00
|
|
|
callRuntime((void*) runtime_is_non_zero_d);
|
2009-08-21 00:34:23 +02:00
|
|
|
o4(0xE3500000); // cmp r0,#0
|
|
|
|
#endif
|
2009-07-10 07:00:24 +02:00
|
|
|
break;
|
|
|
|
default:
|
2009-08-21 00:34:23 +02:00
|
|
|
o4(0xE3500000); // cmp r0,#0
|
2009-07-10 07:00:24 +02:00
|
|
|
break;
|
|
|
|
}
|
2009-05-15 02:21:45 +02:00
|
|
|
int branch = l ? 0x1A000000 : 0x0A000000; // bne : beq
|
2009-09-10 04:08:54 +02:00
|
|
|
int pc = getPC();
|
|
|
|
o4(branch | encodeAddress(t));
|
|
|
|
return pc;
|
2009-05-13 19:58:45 +02:00
|
|
|
}
|
|
|
|
|
2009-07-18 01:35:23 +02:00
|
|
|
virtual void gcmp(int op) {
|
2009-07-10 07:00:24 +02:00
|
|
|
Type* pR0Type = getR0Type();
|
|
|
|
Type* pTOSType = getTOSType();
|
|
|
|
TypeTag tagR0 = collapseType(pR0Type->tag);
|
|
|
|
TypeTag tagTOS = collapseType(pTOSType->tag);
|
|
|
|
if (tagR0 == TY_INT && tagTOS == TY_INT) {
|
2009-07-18 01:35:23 +02:00
|
|
|
setupIntPtrArgs();
|
2009-07-10 07:00:24 +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;
|
|
|
|
}
|
|
|
|
} else if (tagR0 == TY_DOUBLE || tagTOS == TY_DOUBLE) {
|
|
|
|
setupDoubleArgs();
|
2009-08-21 00:34:23 +02:00
|
|
|
#ifdef ARM_USE_VFP
|
2009-08-25 21:23:43 +02:00
|
|
|
o4(0xEEB46BC7); // fcmped d6, d7
|
|
|
|
o4(0xEEF1FA10); // fmstat
|
2009-08-21 00:34:23 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
#else
|
2009-07-10 07:00:24 +02:00
|
|
|
switch(op) {
|
|
|
|
case OP_EQUALS:
|
|
|
|
callRuntime((void*) runtime_cmp_eq_dd);
|
|
|
|
break;
|
|
|
|
case OP_NOT_EQUALS:
|
|
|
|
callRuntime((void*) runtime_cmp_ne_dd);
|
|
|
|
break;
|
|
|
|
case OP_LESS_EQUAL:
|
|
|
|
callRuntime((void*) runtime_cmp_le_dd);
|
|
|
|
break;
|
|
|
|
case OP_GREATER:
|
|
|
|
callRuntime((void*) runtime_cmp_gt_dd);
|
|
|
|
break;
|
|
|
|
case OP_GREATER_EQUAL:
|
|
|
|
callRuntime((void*) runtime_cmp_ge_dd);
|
|
|
|
break;
|
|
|
|
case OP_LESS:
|
|
|
|
callRuntime((void*) runtime_cmp_lt_dd);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error("Unknown comparison op %d", op);
|
|
|
|
break;
|
|
|
|
}
|
2009-08-21 00:34:23 +02:00
|
|
|
#endif
|
2009-07-10 07:00:24 +02:00
|
|
|
} else {
|
|
|
|
setupFloatArgs();
|
2009-08-21 00:34:23 +02:00
|
|
|
#ifdef ARM_USE_VFP
|
|
|
|
o4(0xEEB47AE7); // fcmpes s14, s15
|
2009-08-25 21:23:43 +02:00
|
|
|
o4(0xEEF1FA10); // fmstat
|
2009-08-21 00:34:23 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
#else
|
2009-07-10 07:00:24 +02:00
|
|
|
switch(op) {
|
|
|
|
case OP_EQUALS:
|
|
|
|
callRuntime((void*) runtime_cmp_eq_ff);
|
|
|
|
break;
|
|
|
|
case OP_NOT_EQUALS:
|
|
|
|
callRuntime((void*) runtime_cmp_ne_ff);
|
|
|
|
break;
|
|
|
|
case OP_LESS_EQUAL:
|
|
|
|
callRuntime((void*) runtime_cmp_le_ff);
|
|
|
|
break;
|
|
|
|
case OP_GREATER:
|
|
|
|
callRuntime((void*) runtime_cmp_gt_ff);
|
|
|
|
break;
|
|
|
|
case OP_GREATER_EQUAL:
|
|
|
|
callRuntime((void*) runtime_cmp_ge_ff);
|
|
|
|
break;
|
|
|
|
case OP_LESS:
|
|
|
|
callRuntime((void*) runtime_cmp_lt_ff);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error("Unknown comparison op %d", op);
|
|
|
|
break;
|
|
|
|
}
|
2009-08-21 00:34:23 +02:00
|
|
|
#endif
|
2009-05-15 02:21:45 +02:00
|
|
|
}
|
2009-07-18 01:35:23 +02:00
|
|
|
setR0Type(mkpInt);
|
2009-05-13 19:58:45 +02:00
|
|
|
}
|
|
|
|
|
2009-05-14 00:10:04 +02:00
|
|
|
virtual void genOp(int op) {
|
2009-07-10 07:00:24 +02:00
|
|
|
Type* pR0Type = getR0Type();
|
|
|
|
Type* pTOSType = getTOSType();
|
2009-07-14 03:40:08 +02:00
|
|
|
TypeTag tagR0 = pR0Type->tag;
|
|
|
|
TypeTag tagTOS = pTOSType->tag;
|
|
|
|
bool isFloatR0 = isFloatTag(tagR0);
|
|
|
|
bool isFloatTOS = isFloatTag(tagTOS);
|
|
|
|
if (!isFloatR0 && !isFloatTOS) {
|
2009-07-18 01:35:23 +02:00
|
|
|
setupIntPtrArgs();
|
2009-08-04 23:56:09 +02:00
|
|
|
bool isPtrR0 = isPointerTag(tagR0);
|
|
|
|
bool isPtrTOS = isPointerTag(tagTOS);
|
2009-07-14 03:40:08 +02:00
|
|
|
if (isPtrR0 || isPtrTOS) {
|
|
|
|
if (isPtrR0 && isPtrTOS) {
|
|
|
|
if (op != OP_MINUS) {
|
|
|
|
error("Unsupported pointer-pointer operation %d.", op);
|
|
|
|
}
|
|
|
|
if (! typeEqual(pR0Type, pTOSType)) {
|
|
|
|
error("Incompatible pointer types for subtraction.");
|
|
|
|
}
|
|
|
|
o4(0xE0410000); // sub r0,r1,r0
|
|
|
|
setR0Type(mkpInt);
|
|
|
|
int size = sizeOf(pR0Type->pHead);
|
|
|
|
if (size != 1) {
|
|
|
|
pushR0();
|
2009-07-18 01:35:23 +02:00
|
|
|
li(size);
|
2009-07-14 03:40:08 +02:00
|
|
|
// TODO: Optimize for power-of-two.
|
|
|
|
genOp(OP_DIV);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (! (op == OP_PLUS || (op == OP_MINUS && isPtrR0))) {
|
|
|
|
error("Unsupported pointer-scalar operation %d", op);
|
|
|
|
}
|
2009-08-04 23:56:09 +02:00
|
|
|
Type* pPtrType = getPointerArithmeticResultType(
|
|
|
|
pR0Type, pTOSType);
|
2009-07-14 03:40:08 +02:00
|
|
|
int size = sizeOf(pPtrType->pHead);
|
|
|
|
if (size != 1) {
|
|
|
|
// TODO: Optimize for power-of-two.
|
|
|
|
liReg(size, 2);
|
|
|
|
if (isPtrR0) {
|
|
|
|
o4(0x0E0010192); // mul r1,r2,r1
|
|
|
|
} else {
|
|
|
|
o4(0x0E0000092); // mul r0,r2,r0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch(op) {
|
|
|
|
case OP_PLUS:
|
|
|
|
o4(0xE0810000); // add r0,r1,r0
|
|
|
|
break;
|
|
|
|
case OP_MINUS:
|
|
|
|
o4(0xE0410000); // sub r0,r1,r0
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
setR0Type(pPtrType);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch(op) {
|
|
|
|
case OP_MUL:
|
|
|
|
o4(0x0E0000091); // mul r0,r1,r0
|
|
|
|
break;
|
|
|
|
case OP_DIV:
|
|
|
|
callRuntime((void*) runtime_DIV);
|
|
|
|
break;
|
|
|
|
case OP_MOD:
|
|
|
|
callRuntime((void*) runtime_MOD);
|
|
|
|
break;
|
|
|
|
case OP_PLUS:
|
|
|
|
o4(0xE0810000); // add r0,r1,r0
|
|
|
|
break;
|
|
|
|
case OP_MINUS:
|
|
|
|
o4(0xE0410000); // sub r0,r1,r0
|
|
|
|
break;
|
|
|
|
case OP_SHIFT_LEFT:
|
|
|
|
o4(0xE1A00011); // lsl r0,r1,r0
|
|
|
|
break;
|
|
|
|
case OP_SHIFT_RIGHT:
|
|
|
|
o4(0xE1A00051); // asr r0,r1,r0
|
|
|
|
break;
|
|
|
|
case OP_BIT_AND:
|
|
|
|
o4(0xE0010000); // and r0,r1,r0
|
|
|
|
break;
|
|
|
|
case OP_BIT_XOR:
|
|
|
|
o4(0xE0210000); // eor r0,r1,r0
|
|
|
|
break;
|
|
|
|
case OP_BIT_OR:
|
|
|
|
o4(0xE1810000); // orr r0,r1,r0
|
|
|
|
break;
|
|
|
|
case OP_BIT_NOT:
|
|
|
|
o4(0xE1E00000); // mvn r0, r0
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error("Unimplemented op %d\n", op);
|
|
|
|
break;
|
|
|
|
}
|
2009-07-10 07:00:24 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Type* pResultType = tagR0 > tagTOS ? pR0Type : pTOSType;
|
|
|
|
if (pResultType->tag == TY_DOUBLE) {
|
|
|
|
setupDoubleArgs();
|
2009-08-21 00:34:23 +02:00
|
|
|
|
2009-07-10 07:00:24 +02:00
|
|
|
switch(op) {
|
|
|
|
case OP_MUL:
|
2009-08-21 00:34:23 +02:00
|
|
|
#ifdef ARM_USE_VFP
|
2009-08-25 21:23:43 +02:00
|
|
|
o4(0xEE267B07); // fmuld d7, d6, d7
|
2009-08-21 00:34:23 +02:00
|
|
|
#else
|
2009-08-25 21:23:43 +02:00
|
|
|
callRuntime((void*) runtime_op_mul_dd);
|
2009-08-21 00:34:23 +02:00
|
|
|
#endif
|
2009-07-10 07:00:24 +02:00
|
|
|
break;
|
|
|
|
case OP_DIV:
|
2009-08-21 00:34:23 +02:00
|
|
|
#ifdef ARM_USE_VFP
|
2009-08-25 21:23:43 +02:00
|
|
|
o4(0xEE867B07); // fdivd d7, d6, d7
|
2009-08-21 00:34:23 +02:00
|
|
|
#else
|
2009-07-10 07:00:24 +02:00
|
|
|
callRuntime((void*) runtime_op_div_dd);
|
2009-08-21 00:34:23 +02:00
|
|
|
#endif
|
2009-07-10 07:00:24 +02:00
|
|
|
break;
|
|
|
|
case OP_PLUS:
|
2009-08-21 00:34:23 +02:00
|
|
|
#ifdef ARM_USE_VFP
|
2009-08-25 21:23:43 +02:00
|
|
|
o4(0xEE367B07); // faddd d7, d6, d7
|
2009-08-21 00:34:23 +02:00
|
|
|
#else
|
2009-07-10 07:00:24 +02:00
|
|
|
callRuntime((void*) runtime_op_add_dd);
|
2009-08-21 00:34:23 +02:00
|
|
|
#endif
|
2009-07-10 07:00:24 +02:00
|
|
|
break;
|
|
|
|
case OP_MINUS:
|
2009-08-21 00:34:23 +02:00
|
|
|
#ifdef ARM_USE_VFP
|
2009-08-25 21:23:43 +02:00
|
|
|
o4(0xEE367B47); // fsubd d7, d6, d7
|
2009-08-21 00:34:23 +02:00
|
|
|
#else
|
2009-07-10 07:00:24 +02:00
|
|
|
callRuntime((void*) runtime_op_sub_dd);
|
2009-08-21 00:34:23 +02:00
|
|
|
#endif
|
2009-07-10 07:00:24 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error("Unsupported binary floating operation %d\n", op);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
setupFloatArgs();
|
|
|
|
switch(op) {
|
|
|
|
case OP_MUL:
|
2009-08-21 00:34:23 +02:00
|
|
|
#ifdef ARM_USE_VFP
|
2009-08-25 21:23:43 +02:00
|
|
|
o4(0xEE677A27); // fmuls s15, s14, s15
|
2009-08-21 00:34:23 +02:00
|
|
|
#else
|
2009-07-10 07:00:24 +02:00
|
|
|
callRuntime((void*) runtime_op_mul_ff);
|
2009-08-21 00:34:23 +02:00
|
|
|
#endif
|
2009-07-10 07:00:24 +02:00
|
|
|
break;
|
|
|
|
case OP_DIV:
|
2009-08-21 00:34:23 +02:00
|
|
|
#ifdef ARM_USE_VFP
|
2009-08-25 21:23:43 +02:00
|
|
|
o4(0xEEC77A27); // fdivs s15, s14, s15
|
2009-08-21 00:34:23 +02:00
|
|
|
#else
|
2009-07-10 07:00:24 +02:00
|
|
|
callRuntime((void*) runtime_op_div_ff);
|
2009-08-21 00:34:23 +02:00
|
|
|
#endif
|
2009-07-10 07:00:24 +02:00
|
|
|
break;
|
|
|
|
case OP_PLUS:
|
2009-08-21 00:34:23 +02:00
|
|
|
#ifdef ARM_USE_VFP
|
2009-08-25 21:23:43 +02:00
|
|
|
o4(0xEE777A27); // fadds s15, s14, s15
|
2009-08-21 00:34:23 +02:00
|
|
|
#else
|
2009-07-10 07:00:24 +02:00
|
|
|
callRuntime((void*) runtime_op_add_ff);
|
2009-08-21 00:34:23 +02:00
|
|
|
#endif
|
2009-07-10 07:00:24 +02:00
|
|
|
break;
|
|
|
|
case OP_MINUS:
|
2009-08-21 00:34:23 +02:00
|
|
|
#ifdef ARM_USE_VFP
|
2009-08-25 21:23:43 +02:00
|
|
|
o4(0xEE777A67); // fsubs s15, s14, s15
|
2009-08-21 00:34:23 +02:00
|
|
|
#else
|
2009-07-10 07:00:24 +02:00
|
|
|
callRuntime((void*) runtime_op_sub_ff);
|
2009-08-21 00:34:23 +02:00
|
|
|
#endif
|
2009-07-10 07:00:24 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error("Unsupported binary floating operation %d\n", op);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setR0Type(pResultType);
|
2009-05-14 20:38:49 +02:00
|
|
|
}
|
2009-05-13 19:58:45 +02:00
|
|
|
}
|
|
|
|
|
2009-07-18 01:35:23 +02:00
|
|
|
virtual void gUnaryCmp(int op) {
|
2009-07-10 07:00:24 +02:00
|
|
|
if (op != OP_LOGICAL_NOT) {
|
|
|
|
error("Unknown unary cmp %d", op);
|
|
|
|
} else {
|
|
|
|
Type* pR0Type = getR0Type();
|
|
|
|
TypeTag tag = collapseType(pR0Type->tag);
|
|
|
|
switch(tag) {
|
|
|
|
case TY_INT:
|
|
|
|
o4(0xE3A01000); // mov r1, #0
|
2009-07-14 03:40:08 +02:00
|
|
|
o4(0xE1510000); // cmp r1, r0
|
|
|
|
o4(0x03A00001); // moveq r0,#1
|
|
|
|
o4(0x13A00000); // movne r0,#0
|
2009-07-10 07:00:24 +02:00
|
|
|
break;
|
|
|
|
case TY_FLOAT:
|
2009-08-21 00:34:23 +02:00
|
|
|
#ifdef ARM_USE_VFP
|
|
|
|
o4(0xEEF57A40); // fcmpzs s15
|
|
|
|
o4(0xEEF1FA10); // fmstat
|
|
|
|
o4(0x03A00001); // moveq r0,#1
|
|
|
|
o4(0x13A00000); // movne r0,#0
|
|
|
|
#else
|
2009-08-25 21:23:43 +02:00
|
|
|
callRuntime((void*) runtime_is_zero_f);
|
2009-08-21 00:34:23 +02:00
|
|
|
#endif
|
2009-07-10 07:00:24 +02:00
|
|
|
break;
|
|
|
|
case TY_DOUBLE:
|
2009-08-21 00:34:23 +02:00
|
|
|
#ifdef ARM_USE_VFP
|
|
|
|
o4(0xEEB57B40); // fcmpzd d7
|
|
|
|
o4(0xEEF1FA10); // fmstat
|
|
|
|
o4(0x03A00001); // moveq r0,#1
|
|
|
|
o4(0x13A00000); // movne r0,#0
|
|
|
|
#else
|
2009-08-25 21:23:43 +02:00
|
|
|
callRuntime((void*) runtime_is_zero_d);
|
2009-08-21 00:34:23 +02:00
|
|
|
#endif
|
2009-07-10 07:00:24 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error("gUnaryCmp unsupported type");
|
|
|
|
break;
|
|
|
|
}
|
2009-07-07 02:24:34 +02:00
|
|
|
}
|
2009-07-18 01:35:23 +02:00
|
|
|
setR0Type(mkpInt);
|
2009-07-07 02:24:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void genUnaryOp(int op) {
|
2009-07-10 07:00:24 +02:00
|
|
|
Type* pR0Type = getR0Type();
|
|
|
|
TypeTag tag = collapseType(pR0Type->tag);
|
|
|
|
switch(tag) {
|
|
|
|
case TY_INT:
|
|
|
|
switch(op) {
|
|
|
|
case OP_MINUS:
|
|
|
|
o4(0xE3A01000); // mov r1, #0
|
|
|
|
o4(0xE0410000); // sub r0,r1,r0
|
|
|
|
break;
|
|
|
|
case OP_BIT_NOT:
|
|
|
|
o4(0xE1E00000); // mvn r0, r0
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error("Unknown unary op %d\n", op);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TY_FLOAT:
|
|
|
|
case TY_DOUBLE:
|
|
|
|
switch (op) {
|
|
|
|
case OP_MINUS:
|
|
|
|
if (tag == TY_FLOAT) {
|
2009-08-21 00:34:23 +02:00
|
|
|
#ifdef ARM_USE_VFP
|
2009-08-25 21:23:43 +02:00
|
|
|
o4(0xEEF17A67); // fnegs s15, s15
|
2009-08-21 00:34:23 +02:00
|
|
|
#else
|
2009-07-10 07:00:24 +02:00
|
|
|
callRuntime((void*) runtime_op_neg_f);
|
2009-08-21 00:34:23 +02:00
|
|
|
#endif
|
2009-07-10 07:00:24 +02:00
|
|
|
} else {
|
2009-08-21 00:34:23 +02:00
|
|
|
#ifdef ARM_USE_VFP
|
2009-08-25 21:23:43 +02:00
|
|
|
o4(0xEEB17B47); // fnegd d7, d7
|
2009-08-21 00:34:23 +02:00
|
|
|
#else
|
2009-07-10 07:00:24 +02:00
|
|
|
callRuntime((void*) runtime_op_neg_d);
|
2009-08-21 00:34:23 +02:00
|
|
|
#endif
|
2009-07-10 07:00:24 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case OP_BIT_NOT:
|
|
|
|
error("Can't apply '~' operator to a float or double.");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error("Unknown unary op %d\n", op);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error("genUnaryOp unsupported type");
|
|
|
|
break;
|
2009-07-07 02:24:34 +02:00
|
|
|
}
|
2009-05-13 19:58:45 +02:00
|
|
|
}
|
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
virtual void pushR0() {
|
2009-07-10 07:00:24 +02:00
|
|
|
Type* pR0Type = getR0Type();
|
|
|
|
TypeTag r0ct = collapseType(pR0Type->tag);
|
2009-08-21 00:34:23 +02:00
|
|
|
|
|
|
|
#ifdef ARM_USE_VFP
|
|
|
|
switch (r0ct ) {
|
|
|
|
case TY_FLOAT:
|
2009-08-25 21:23:43 +02:00
|
|
|
o4(0xED6D7A01); // fstmfds sp!,{s15}
|
2009-08-21 00:34:23 +02:00
|
|
|
mStackUse += 4;
|
2009-08-25 21:23:43 +02:00
|
|
|
break;
|
2009-08-21 00:34:23 +02:00
|
|
|
case TY_DOUBLE:
|
2009-08-25 21:23:43 +02:00
|
|
|
o4(0xED2D7B02); // fstmfdd sp!,{d7}
|
2009-08-21 00:34:23 +02:00
|
|
|
mStackUse += 8;
|
2009-08-25 21:23:43 +02:00
|
|
|
break;
|
2009-08-21 00:34:23 +02:00
|
|
|
default:
|
|
|
|
o4(0xE92D0001); // stmfd sp!,{r0}
|
|
|
|
mStackUse += 4;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
|
2009-07-10 07:00:24 +02:00
|
|
|
if (r0ct != TY_DOUBLE) {
|
|
|
|
o4(0xE92D0001); // stmfd sp!,{r0}
|
|
|
|
mStackUse += 4;
|
|
|
|
} else {
|
|
|
|
o4(0xE92D0003); // stmfd sp!,{r0,r1}
|
|
|
|
mStackUse += 8;
|
|
|
|
}
|
2009-08-21 00:34:23 +02:00
|
|
|
#endif
|
2009-07-07 23:48:51 +02:00
|
|
|
pushType();
|
2009-06-18 04:13:52 +02:00
|
|
|
LOG_STACK("pushR0: %d\n", mStackUse);
|
2009-05-13 19:58:45 +02:00
|
|
|
}
|
|
|
|
|
2009-07-29 19:28:18 +02:00
|
|
|
virtual void over() {
|
|
|
|
// We know it's only used for int-ptr ops (++/--)
|
|
|
|
|
|
|
|
Type* pR0Type = getR0Type();
|
|
|
|
TypeTag r0ct = collapseType(pR0Type->tag);
|
|
|
|
|
|
|
|
Type* pTOSType = getTOSType();
|
|
|
|
TypeTag tosct = collapseType(pTOSType->tag);
|
|
|
|
|
|
|
|
assert (r0ct == TY_INT && tosct == TY_INT);
|
|
|
|
|
|
|
|
o4(0xE8BD0002); // ldmfd sp!,{r1}
|
|
|
|
o4(0xE92D0001); // stmfd sp!,{r0}
|
|
|
|
o4(0xE92D0002); // stmfd sp!,{r1}
|
|
|
|
overType();
|
|
|
|
mStackUse += 4;
|
|
|
|
}
|
|
|
|
|
2009-07-18 01:35:23 +02:00
|
|
|
virtual void popR0() {
|
|
|
|
Type* pTOSType = getTOSType();
|
2009-08-21 00:34:23 +02:00
|
|
|
TypeTag tosct = collapseType(pTOSType->tag);
|
|
|
|
#ifdef ARM_USE_VFP
|
|
|
|
if (tosct == TY_FLOAT || tosct == TY_DOUBLE) {
|
2009-08-25 21:23:43 +02:00
|
|
|
error("Unsupported popR0 float/double");
|
2009-08-21 00:34:23 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
switch (tosct){
|
2009-07-18 01:35:23 +02:00
|
|
|
case TY_INT:
|
|
|
|
case TY_FLOAT:
|
|
|
|
o4(0xE8BD0001); // ldmfd sp!,{r0}
|
|
|
|
mStackUse -= 4;
|
|
|
|
break;
|
|
|
|
case TY_DOUBLE:
|
|
|
|
o4(0xE8BD0003); // ldmfd sp!,{r0, r1} // Restore R0
|
|
|
|
mStackUse -= 8;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error("Can't pop this type.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
popType();
|
|
|
|
LOG_STACK("popR0: %d\n", mStackUse);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void storeR0ToTOS() {
|
|
|
|
Type* pPointerType = getTOSType();
|
2009-07-07 02:24:34 +02:00
|
|
|
assert(pPointerType->tag == TY_POINTER);
|
2009-07-31 01:57:33 +02:00
|
|
|
Type* pDestType = pPointerType->pHead;
|
|
|
|
convertR0(pDestType);
|
2009-07-10 07:00:24 +02:00
|
|
|
o4(0xE8BD0004); // ldmfd sp!,{r2}
|
|
|
|
popType();
|
2009-06-18 04:13:52 +02:00
|
|
|
mStackUse -= 4;
|
2009-07-31 01:57:33 +02:00
|
|
|
switch (pDestType->tag) {
|
|
|
|
case TY_POINTER:
|
2009-07-07 02:24:34 +02:00
|
|
|
case TY_INT:
|
2009-08-21 00:34:23 +02:00
|
|
|
o4(0xE5820000); // str r0, [r2]
|
|
|
|
break;
|
2009-07-10 07:00:24 +02:00
|
|
|
case TY_FLOAT:
|
2009-08-21 00:34:23 +02:00
|
|
|
#ifdef ARM_USE_VFP
|
2009-08-25 21:23:43 +02:00
|
|
|
o4(0xEDC27A00); // fsts s15, [r2, #0]
|
2009-08-21 00:34:23 +02:00
|
|
|
#else
|
2009-07-10 07:00:24 +02:00
|
|
|
o4(0xE5820000); // str r0, [r2]
|
2009-08-21 00:34:23 +02:00
|
|
|
#endif
|
2009-07-07 02:24:34 +02:00
|
|
|
break;
|
2009-08-03 23:42:57 +02:00
|
|
|
case TY_SHORT:
|
|
|
|
o4(0xE1C200B0); // strh r0, [r2]
|
|
|
|
break;
|
2009-07-07 02:24:34 +02:00
|
|
|
case TY_CHAR:
|
2009-07-10 07:00:24 +02:00
|
|
|
o4(0xE5C20000); // strb r0, [r2]
|
|
|
|
break;
|
|
|
|
case TY_DOUBLE:
|
2009-08-21 00:34:23 +02:00
|
|
|
#ifdef ARM_USE_VFP
|
2009-08-25 21:23:43 +02:00
|
|
|
o4(0xED827B00); // fstd d7, [r2, #0]
|
2009-08-21 00:34:23 +02:00
|
|
|
#else
|
2009-07-10 07:00:24 +02:00
|
|
|
o4(0xE1C200F0); // strd r0, [r2]
|
2009-08-21 00:34:23 +02:00
|
|
|
#endif
|
2009-07-07 02:24:34 +02:00
|
|
|
break;
|
2009-08-27 01:15:07 +02:00
|
|
|
case TY_STRUCT:
|
|
|
|
{
|
|
|
|
int size = sizeOf(pDestType);
|
|
|
|
if (size > 0) {
|
|
|
|
liReg(size, 1);
|
|
|
|
callRuntime((void*) runtime_structCopy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2009-07-07 02:24:34 +02:00
|
|
|
default:
|
2009-07-31 04:06:34 +02:00
|
|
|
error("storeR0ToTOS: unimplemented type %d",
|
|
|
|
pDestType->tag);
|
2009-07-07 02:24:34 +02:00
|
|
|
break;
|
2009-05-15 04:35:31 +02:00
|
|
|
}
|
2009-05-13 19:58:45 +02:00
|
|
|
}
|
|
|
|
|
2009-07-18 01:35:23 +02:00
|
|
|
virtual void loadR0FromR0() {
|
|
|
|
Type* pPointerType = getR0Type();
|
2009-07-07 02:24:34 +02:00
|
|
|
assert(pPointerType->tag == TY_POINTER);
|
2009-08-05 00:39:49 +02:00
|
|
|
Type* pNewType = pPointerType->pHead;
|
|
|
|
TypeTag tag = pNewType->tag;
|
2009-08-04 23:56:09 +02:00
|
|
|
switch (tag) {
|
2009-07-29 20:36:04 +02:00
|
|
|
case TY_POINTER:
|
2009-07-07 02:24:34 +02:00
|
|
|
case TY_INT:
|
2009-08-21 00:34:23 +02:00
|
|
|
o4(0xE5900000); // ldr r0, [r0]
|
|
|
|
break;
|
2009-07-10 07:00:24 +02:00
|
|
|
case TY_FLOAT:
|
2009-08-21 00:34:23 +02:00
|
|
|
#ifdef ARM_USE_VFP
|
2009-08-25 21:23:43 +02:00
|
|
|
o4(0xEDD07A00); // flds s15, [r0, #0]
|
2009-08-21 00:34:23 +02:00
|
|
|
#else
|
2009-07-07 02:24:34 +02:00
|
|
|
o4(0xE5900000); // ldr r0, [r0]
|
2009-08-21 00:34:23 +02:00
|
|
|
#endif
|
2009-07-07 02:24:34 +02:00
|
|
|
break;
|
2009-08-03 23:42:57 +02:00
|
|
|
case TY_SHORT:
|
|
|
|
o4(0xE1D000F0); // ldrsh r0, [r0]
|
|
|
|
break;
|
2009-07-07 02:24:34 +02:00
|
|
|
case TY_CHAR:
|
|
|
|
o4(0xE5D00000); // ldrb r0, [r0]
|
|
|
|
break;
|
2009-07-10 07:00:24 +02:00
|
|
|
case TY_DOUBLE:
|
2009-08-21 00:34:23 +02:00
|
|
|
#ifdef ARM_USE_VFP
|
2009-08-25 21:23:43 +02:00
|
|
|
o4(0xED907B00); // fldd d7, [r0, #0]
|
2009-08-21 00:34:23 +02:00
|
|
|
#else
|
2009-07-29 20:36:04 +02:00
|
|
|
o4(0xE1C000D0); // ldrd r0, [r0]
|
2009-08-21 00:34:23 +02:00
|
|
|
#endif
|
2009-07-10 07:00:24 +02:00
|
|
|
break;
|
2009-08-05 00:39:49 +02:00
|
|
|
case TY_ARRAY:
|
|
|
|
pNewType = pNewType->pTail;
|
|
|
|
break;
|
2009-08-27 01:15:07 +02:00
|
|
|
case TY_STRUCT:
|
|
|
|
break;
|
2009-07-07 02:24:34 +02:00
|
|
|
default:
|
2009-08-04 23:56:09 +02:00
|
|
|
error("loadR0FromR0: unimplemented type %d", tag);
|
2009-07-07 02:24:34 +02:00
|
|
|
break;
|
|
|
|
}
|
2009-08-05 00:39:49 +02:00
|
|
|
setR0Type(pNewType);
|
2009-05-13 19:58:45 +02:00
|
|
|
}
|
|
|
|
|
2009-07-31 04:06:34 +02:00
|
|
|
virtual void leaR0(int ea, Type* pPointerType, ExpressionType et) {
|
2009-07-10 07:00:24 +02:00
|
|
|
if (ea > -LOCAL && ea < LOCAL) {
|
2009-05-15 22:30:00 +02:00
|
|
|
// Local, fp relative
|
2009-08-27 01:15:07 +02:00
|
|
|
|
|
|
|
size_t immediate = 0;
|
|
|
|
bool inRange = false;
|
2009-05-15 22:30:00 +02:00
|
|
|
if (ea < 0) {
|
2009-08-27 01:15:07 +02:00
|
|
|
inRange = encode12BitImmediate(-ea, &immediate);
|
|
|
|
o4(0xE24B0000 | immediate); // sub r0, fp, #ea
|
2009-05-15 22:30:00 +02:00
|
|
|
} else {
|
2009-08-27 01:15:07 +02:00
|
|
|
inRange = encode12BitImmediate(ea, &immediate);
|
|
|
|
o4(0xE28B0000 | immediate); // add r0, fp, #ea
|
|
|
|
}
|
|
|
|
if (! inRange) {
|
|
|
|
error("Offset out of range: %08x", ea);
|
2009-05-15 22:30:00 +02:00
|
|
|
}
|
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-07-31 04:06:34 +02:00
|
|
|
setR0Type(pPointerType, et);
|
2009-05-13 19:58:45 +02:00
|
|
|
}
|
|
|
|
|
2009-07-30 01:22:26 +02:00
|
|
|
virtual int leaForward(int ea, Type* pPointerType) {
|
|
|
|
setR0Type(pPointerType);
|
|
|
|
int result = ea;
|
|
|
|
int pc = getPC();
|
|
|
|
int offset = 0;
|
|
|
|
if (ea) {
|
|
|
|
offset = (pc - ea - 8) >> 2;
|
|
|
|
if ((offset & 0xffff) != offset) {
|
|
|
|
error("function forward reference out of bounds");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
offset = 0;
|
|
|
|
}
|
|
|
|
o4(0xE59F0000 | offset); // ldr r0, .L1
|
|
|
|
|
|
|
|
if (ea == 0) {
|
|
|
|
o4(0xEA000000); // b .L99
|
2009-09-10 04:08:54 +02:00
|
|
|
result = getPC();
|
|
|
|
o4(ea); // .L1: .word 0
|
2009-07-30 01:22:26 +02:00
|
|
|
// .L99:
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-08-04 23:56:09 +02:00
|
|
|
virtual void convertR0Imp(Type* pType, bool isCast){
|
2009-07-08 22:04:41 +02:00
|
|
|
Type* pR0Type = getR0Type();
|
2009-08-04 23:56:09 +02:00
|
|
|
if (isPointerType(pType) && isPointerType(pR0Type)) {
|
|
|
|
Type* pA = pR0Type;
|
|
|
|
Type* pB = pType;
|
|
|
|
// Array decays to pointer
|
|
|
|
if (pA->tag == TY_ARRAY && pB->tag == TY_POINTER) {
|
|
|
|
pA = pA->pTail;
|
|
|
|
}
|
2009-08-25 21:23:43 +02:00
|
|
|
if (! (typeEqual(pA, pB)
|
|
|
|
|| pB->pHead->tag == TY_VOID
|
|
|
|
|| (pA->tag == TY_POINTER && pB->tag == TY_POINTER && isCast)
|
|
|
|
)) {
|
|
|
|
error("Incompatible pointer or array types");
|
2009-08-04 23:56:09 +02:00
|
|
|
}
|
|
|
|
} else if (bitsSame(pType, pR0Type)) {
|
2009-07-08 22:04:41 +02:00
|
|
|
// do nothing special
|
|
|
|
} else {
|
2009-07-10 07:00:24 +02:00
|
|
|
TypeTag r0Tag = collapseType(pR0Type->tag);
|
|
|
|
TypeTag destTag = collapseType(pType->tag);
|
|
|
|
if (r0Tag == TY_INT) {
|
|
|
|
if (destTag == TY_FLOAT) {
|
2009-08-21 00:34:23 +02:00
|
|
|
#ifdef ARM_USE_VFP
|
2009-08-25 21:23:43 +02:00
|
|
|
o4(0xEE070A90); // fmsr s15, r0
|
|
|
|
o4(0xEEF87AE7); // fsitos s15, s15
|
2009-08-21 00:34:23 +02:00
|
|
|
|
|
|
|
#else
|
2009-07-10 07:00:24 +02:00
|
|
|
callRuntime((void*) runtime_int_to_float);
|
2009-08-21 00:34:23 +02:00
|
|
|
#endif
|
2009-07-10 07:00:24 +02:00
|
|
|
} else {
|
|
|
|
assert(destTag == TY_DOUBLE);
|
2009-08-21 00:34:23 +02:00
|
|
|
#ifdef ARM_USE_VFP
|
2009-08-25 21:23:43 +02:00
|
|
|
o4(0xEE070A90); // fmsr s15, r0
|
|
|
|
o4(0xEEB87BE7); // fsitod d7, s15
|
2009-08-21 00:34:23 +02:00
|
|
|
|
|
|
|
#else
|
2009-07-10 07:00:24 +02:00
|
|
|
callRuntime((void*) runtime_int_to_double);
|
2009-08-21 00:34:23 +02:00
|
|
|
#endif
|
2009-07-10 07:00:24 +02:00
|
|
|
}
|
|
|
|
} else if (r0Tag == TY_FLOAT) {
|
|
|
|
if (destTag == TY_INT) {
|
2009-08-21 00:34:23 +02:00
|
|
|
#ifdef ARM_USE_VFP
|
2009-08-25 21:23:43 +02:00
|
|
|
o4(0xEEFD7AE7); // ftosizs s15, s15
|
|
|
|
o4(0xEE170A90); // fmrs r0, s15
|
2009-08-21 00:34:23 +02:00
|
|
|
#else
|
2009-07-10 07:00:24 +02:00
|
|
|
callRuntime((void*) runtime_float_to_int);
|
2009-08-21 00:34:23 +02:00
|
|
|
#endif
|
2009-07-10 07:00:24 +02:00
|
|
|
} else {
|
|
|
|
assert(destTag == TY_DOUBLE);
|
2009-08-21 00:34:23 +02:00
|
|
|
#ifdef ARM_USE_VFP
|
2009-08-25 21:23:43 +02:00
|
|
|
o4(0xEEB77AE7); // fcvtds d7, s15
|
2009-08-21 00:34:23 +02:00
|
|
|
#else
|
2009-07-10 07:00:24 +02:00
|
|
|
callRuntime((void*) runtime_float_to_double);
|
2009-08-21 00:34:23 +02:00
|
|
|
#endif
|
2009-07-10 07:00:24 +02:00
|
|
|
}
|
|
|
|
} else {
|
2009-09-08 21:07:32 +02:00
|
|
|
if (r0Tag == TY_DOUBLE) {
|
|
|
|
if (destTag == TY_INT) {
|
2009-08-21 00:34:23 +02:00
|
|
|
#ifdef ARM_USE_VFP
|
2009-09-08 21:07:32 +02:00
|
|
|
o4(0xEEFD7BC7); // ftosizd s15, d7
|
|
|
|
o4(0xEE170A90); // fmrs r0, s15
|
2009-08-21 00:34:23 +02:00
|
|
|
#else
|
2009-09-08 21:07:32 +02:00
|
|
|
callRuntime((void*) runtime_double_to_int);
|
2009-08-21 00:34:23 +02:00
|
|
|
#endif
|
2009-09-08 21:07:32 +02:00
|
|
|
} else {
|
|
|
|
if(destTag == TY_FLOAT) {
|
2009-08-21 00:34:23 +02:00
|
|
|
#ifdef ARM_USE_VFP
|
2009-09-08 21:07:32 +02:00
|
|
|
o4(0xEEF77BC7); // fcvtsd s15, d7
|
2009-08-21 00:34:23 +02:00
|
|
|
#else
|
2009-09-08 21:07:32 +02:00
|
|
|
callRuntime((void*) runtime_double_to_float);
|
2009-08-21 00:34:23 +02:00
|
|
|
#endif
|
2009-09-08 21:07:32 +02:00
|
|
|
} else {
|
|
|
|
incompatibleTypes(pR0Type, pType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
incompatibleTypes(pR0Type, pType);
|
2009-07-10 07:00:24 +02:00
|
|
|
}
|
|
|
|
}
|
2009-07-07 23:48:51 +02:00
|
|
|
}
|
2009-07-08 22:04:41 +02:00
|
|
|
setR0Type(pType);
|
2009-05-13 19:58:45 +02:00
|
|
|
}
|
|
|
|
|
2009-05-14 20:38:49 +02:00
|
|
|
virtual int beginFunctionCallArguments() {
|
2009-09-10 04:08:54 +02:00
|
|
|
int pc = getPC();
|
|
|
|
o4(0xE24DDF00); // Placeholder sub sp, sp, #0
|
|
|
|
return pc;
|
2009-05-14 20:38:49 +02:00
|
|
|
}
|
|
|
|
|
2009-07-17 03:24:47 +02:00
|
|
|
virtual size_t storeR0ToArg(int l, Type* pArgType) {
|
|
|
|
convertR0(pArgType);
|
2009-07-10 07:00:24 +02:00
|
|
|
Type* pR0Type = getR0Type();
|
|
|
|
TypeTag r0ct = collapseType(pR0Type->tag);
|
2009-08-21 00:34:23 +02:00
|
|
|
#ifdef ARM_USE_VFP
|
2009-07-10 07:00:24 +02:00
|
|
|
switch(r0ct) {
|
|
|
|
case TY_INT:
|
2009-08-21 00:34:23 +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, #l]
|
|
|
|
return 4;
|
2009-08-25 21:23:43 +02:00
|
|
|
case TY_FLOAT:
|
2009-08-21 00:34:23 +02:00
|
|
|
if (l < 0 || l > 1020 || (l & 3)) {
|
|
|
|
error("l out of range for stack offset: 0x%08x", l);
|
|
|
|
}
|
2009-08-25 21:23:43 +02:00
|
|
|
o4(0xEDCD7A00 | (l >> 2)); // fsts s15, [sp, #l]
|
2009-08-21 00:34:23 +02:00
|
|
|
return 4;
|
|
|
|
case TY_DOUBLE: {
|
|
|
|
// Align to 8 byte boundary
|
|
|
|
int l2 = (l + 7) & ~7;
|
|
|
|
if (l2 < 0 || l2 > 1020 || (l2 & 3)) {
|
|
|
|
error("l out of range for stack offset: 0x%08x", l);
|
|
|
|
}
|
2009-08-25 21:23:43 +02:00
|
|
|
o4(0xED8D7B00 | (l2 >> 2)); // fstd d7, [sp, #l2]
|
2009-08-21 00:34:23 +02:00
|
|
|
return (l2 - l) + 8;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
switch(r0ct) {
|
|
|
|
case TY_INT:
|
2009-08-25 21:23:43 +02:00
|
|
|
case TY_FLOAT:
|
2009-07-10 07:00:24 +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, #l]
|
|
|
|
return 4;
|
|
|
|
case TY_DOUBLE: {
|
|
|
|
// Align to 8 byte boundary
|
|
|
|
int l2 = (l + 7) & ~7;
|
|
|
|
if (l2 < 0 || l2 > 4096-8) {
|
|
|
|
error("l out of range for stack offset: 0x%08x", l);
|
|
|
|
}
|
|
|
|
o4(0xE58D0000 + l2); // str r0, [sp, #l]
|
|
|
|
o4(0xE58D1000 + l2 + 4); // str r1, [sp, #l+4]
|
|
|
|
return (l2 - l) + 8;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
return 0;
|
2009-05-15 23:31:47 +02:00
|
|
|
}
|
2009-08-21 00:34:23 +02:00
|
|
|
#endif
|
2009-05-15 23:31:47 +02:00
|
|
|
}
|
|
|
|
|
2009-07-10 07:00:24 +02:00
|
|
|
virtual void endFunctionCallArguments(Type* pDecl, int a, int l) {
|
2009-06-18 04:13:52 +02:00
|
|
|
int argumentStackUse = l;
|
2009-07-10 07:00:24 +02:00
|
|
|
// Have to calculate register arg count from actual stack size,
|
|
|
|
// in order to properly handle ... functions.
|
|
|
|
int regArgCount = l >> 2;
|
|
|
|
if (regArgCount > 4) {
|
|
|
|
regArgCount = 4;
|
|
|
|
}
|
|
|
|
if (regArgCount > 0) {
|
2009-06-18 04:13:52 +02:00
|
|
|
argumentStackUse -= regArgCount * 4;
|
2009-05-14 20:38:49 +02:00
|
|
|
o4(0xE8BD0000 | ((1 << regArgCount) - 1)); // ldmfd sp!,{}
|
|
|
|
}
|
2009-06-18 04:13:52 +02:00
|
|
|
mStackUse += argumentStackUse;
|
|
|
|
|
|
|
|
// Align stack.
|
|
|
|
int missalignment = mStackUse - ((mStackUse / STACK_ALIGNMENT)
|
|
|
|
* STACK_ALIGNMENT);
|
|
|
|
mStackAlignmentAdjustment = 0;
|
|
|
|
if (missalignment > 0) {
|
|
|
|
mStackAlignmentAdjustment = STACK_ALIGNMENT - missalignment;
|
|
|
|
}
|
|
|
|
l += mStackAlignmentAdjustment;
|
|
|
|
|
|
|
|
if (l < 0 || l > 0x3FC) {
|
|
|
|
error("L out of range for stack adjustment: 0x%08x", l);
|
|
|
|
}
|
2009-09-10 04:08:54 +02:00
|
|
|
flush();
|
2009-06-18 04:13:52 +02:00
|
|
|
* (int*) a = 0xE24DDF00 | (l >> 2); // sub sp, sp, #0 << 2
|
|
|
|
mStackUse += mStackAlignmentAdjustment;
|
|
|
|
LOG_STACK("endFunctionCallArguments mStackUse: %d, mStackAlignmentAdjustment %d\n",
|
|
|
|
mStackUse, mStackAlignmentAdjustment);
|
2009-05-13 19:58:45 +02:00
|
|
|
}
|
|
|
|
|
2009-07-07 23:48:51 +02:00
|
|
|
virtual int callForward(int symbol, Type* pFunc) {
|
|
|
|
setR0Type(pFunc->pHead);
|
2009-05-14 20:38:49 +02:00
|
|
|
// Forward calls are always short (local)
|
2009-09-10 04:08:54 +02:00
|
|
|
int pc = getPC();
|
|
|
|
o4(0xEB000000 | encodeAddress(symbol));
|
|
|
|
return pc;
|
2009-05-13 19:58:45 +02:00
|
|
|
}
|
|
|
|
|
2009-07-07 23:48:51 +02:00
|
|
|
virtual void callIndirect(int l, Type* pFunc) {
|
2009-07-30 01:22:26 +02:00
|
|
|
assert(pFunc->tag == TY_FUNC);
|
|
|
|
popType(); // Get rid of indirect fn pointer type
|
2009-05-15 23:31:47 +02:00
|
|
|
int argCount = l >> 2;
|
|
|
|
int poppedArgs = argCount > 4 ? 4 : argCount;
|
2009-06-18 04:13:52 +02:00
|
|
|
int adjustedL = l - (poppedArgs << 2) + mStackAlignmentAdjustment;
|
2009-05-15 23:31:47 +02:00
|
|
|
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-08-21 00:34:23 +02:00
|
|
|
Type* pReturnType = pFunc->pHead;
|
|
|
|
setR0Type(pReturnType);
|
|
|
|
#ifdef ARM_USE_VFP
|
|
|
|
switch(pReturnType->tag) {
|
|
|
|
case TY_FLOAT:
|
2009-08-25 21:23:43 +02:00
|
|
|
o4(0xEE070A90); // fmsr s15, r0
|
|
|
|
break;
|
2009-08-21 00:34:23 +02:00
|
|
|
case TY_DOUBLE:
|
2009-08-25 21:23:43 +02:00
|
|
|
o4(0xEC410B17); // fmdrr d7, r0, r1
|
|
|
|
break;
|
2009-08-21 00:34:23 +02:00
|
|
|
default:
|
2009-08-25 21:23:43 +02:00
|
|
|
break;
|
2009-08-21 00:34:23 +02:00
|
|
|
}
|
|
|
|
#endif
|
2009-05-13 19:58:45 +02:00
|
|
|
}
|
|
|
|
|
2009-07-10 07:00:24 +02:00
|
|
|
virtual void adjustStackAfterCall(Type* pDecl, int l, bool isIndirect) {
|
2009-05-14 20:38:49 +02:00
|
|
|
int argCount = l >> 2;
|
2009-07-10 07:00:24 +02:00
|
|
|
// Have to calculate register arg count from actual stack size,
|
|
|
|
// in order to properly handle ... functions.
|
|
|
|
int regArgCount = l >> 2;
|
|
|
|
if (regArgCount > 4) {
|
|
|
|
regArgCount = 4;
|
|
|
|
}
|
|
|
|
int stackArgs = argCount - regArgCount;
|
2009-06-18 04:13:52 +02:00
|
|
|
int stackUse = stackArgs + (isIndirect ? 1 : 0)
|
|
|
|
+ (mStackAlignmentAdjustment >> 2);
|
2009-05-15 23:31:47 +02:00
|
|
|
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-06-18 04:13:52 +02:00
|
|
|
mStackUse -= stackUse * 4;
|
|
|
|
LOG_STACK("adjustStackAfterCall: %d\n", mStackUse);
|
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) {
|
|
|
|
int n;
|
|
|
|
int base = getBase();
|
|
|
|
int pc = getPC();
|
|
|
|
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-07-30 01:22:26 +02:00
|
|
|
/* output a symbol and patch all calls to it */
|
|
|
|
virtual void resolveForward(int t) {
|
|
|
|
if (t) {
|
|
|
|
int pc = getPC();
|
|
|
|
*(int *) t = pc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-07-07 02:24:34 +02:00
|
|
|
/**
|
2009-07-09 01:48:41 +02:00
|
|
|
* alignment (in bytes) for this type of data
|
2009-07-07 02:24:34 +02:00
|
|
|
*/
|
2009-07-10 07:00:24 +02:00
|
|
|
virtual size_t alignmentOf(Type* pType){
|
2009-07-07 02:24:34 +02:00
|
|
|
switch(pType->tag) {
|
2009-07-24 03:56:20 +02:00
|
|
|
case TY_CHAR:
|
|
|
|
return 1;
|
2009-08-03 23:42:57 +02:00
|
|
|
case TY_SHORT:
|
2009-08-27 01:15:07 +02:00
|
|
|
return 2;
|
2009-07-07 02:24:34 +02:00
|
|
|
case TY_DOUBLE:
|
|
|
|
return 8;
|
2009-08-27 01:15:07 +02:00
|
|
|
case TY_ARRAY:
|
|
|
|
return alignmentOf(pType->pHead);
|
|
|
|
case TY_STRUCT:
|
|
|
|
return pType->pHead->alignment & 0x7fffffff;
|
|
|
|
case TY_FUNC:
|
|
|
|
error("alignment of func not supported");
|
|
|
|
return 1;
|
2009-07-07 02:24:34 +02:00
|
|
|
default:
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Array element alignment (in bytes) for this type of data.
|
|
|
|
*/
|
|
|
|
virtual size_t sizeOf(Type* pType){
|
|
|
|
switch(pType->tag) {
|
|
|
|
case TY_INT:
|
|
|
|
return 4;
|
2009-08-03 23:42:57 +02:00
|
|
|
case TY_SHORT:
|
|
|
|
return 2;
|
2009-07-07 02:24:34 +02:00
|
|
|
case TY_CHAR:
|
|
|
|
return 1;
|
|
|
|
case TY_FLOAT:
|
|
|
|
return 4;
|
|
|
|
case TY_DOUBLE:
|
|
|
|
return 8;
|
|
|
|
case TY_POINTER:
|
|
|
|
return 4;
|
2009-08-04 23:56:09 +02:00
|
|
|
case TY_ARRAY:
|
|
|
|
return pType->length * sizeOf(pType->pHead);
|
2009-08-27 01:15:07 +02:00
|
|
|
case TY_STRUCT:
|
|
|
|
return pType->pHead->length;
|
2009-08-04 23:56:09 +02:00
|
|
|
default:
|
|
|
|
error("Unsupported type %d", pType->tag);
|
|
|
|
return 0;
|
2009-07-07 02:24:34 +02:00
|
|
|
}
|
|
|
|
}
|
2009-07-09 01:48:41 +02:00
|
|
|
|
2009-05-13 19:58:45 +02:00
|
|
|
private:
|
2009-05-14 01:24:17 +02:00
|
|
|
|
|
|
|
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-07-10 07:00:24 +02:00
|
|
|
int calcRegArgCount(Type* pDecl) {
|
|
|
|
int reg = 0;
|
|
|
|
Type* pArgs = pDecl->pTail;
|
|
|
|
while (pArgs && reg < 4) {
|
|
|
|
Type* pArg = pArgs->pHead;
|
|
|
|
if ( pArg->tag == TY_DOUBLE) {
|
|
|
|
int evenReg = (reg + 1) & ~1;
|
|
|
|
if (evenReg >= 4) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
reg = evenReg + 2;
|
|
|
|
} else {
|
|
|
|
reg++;
|
|
|
|
}
|
|
|
|
pArgs = pArgs->pTail;
|
|
|
|
}
|
|
|
|
return reg;
|
|
|
|
}
|
|
|
|
|
2009-07-18 01:35:23 +02:00
|
|
|
void setupIntPtrArgs() {
|
|
|
|
o4(0xE8BD0002); // ldmfd sp!,{r1}
|
|
|
|
mStackUse -= 4;
|
|
|
|
popType();
|
|
|
|
}
|
|
|
|
|
2009-08-21 00:34:23 +02:00
|
|
|
/* Pop TOS to R1 (use s14 if VFP)
|
2009-07-10 07:00:24 +02:00
|
|
|
* Make sure both R0 and TOS are floats. (Could be ints)
|
|
|
|
* We know that at least one of R0 and TOS is already a float
|
|
|
|
*/
|
|
|
|
void setupFloatArgs() {
|
|
|
|
Type* pR0Type = getR0Type();
|
|
|
|
Type* pTOSType = getTOSType();
|
|
|
|
TypeTag tagR0 = collapseType(pR0Type->tag);
|
|
|
|
TypeTag tagTOS = collapseType(pTOSType->tag);
|
|
|
|
if (tagR0 != TY_FLOAT) {
|
|
|
|
assert(tagR0 == TY_INT);
|
2009-08-21 00:34:23 +02:00
|
|
|
#ifdef ARM_USE_VFP
|
2009-08-25 21:23:43 +02:00
|
|
|
o4(0xEE070A90); // fmsr s15, r0
|
|
|
|
o4(0xEEF87AE7); // fsitos s15, s15
|
2009-08-21 00:34:23 +02:00
|
|
|
#else
|
2009-07-10 07:00:24 +02:00
|
|
|
callRuntime((void*) runtime_int_to_float);
|
2009-08-21 00:34:23 +02:00
|
|
|
#endif
|
2009-07-10 07:00:24 +02:00
|
|
|
}
|
|
|
|
if (tagTOS != TY_FLOAT) {
|
|
|
|
assert(tagTOS == TY_INT);
|
|
|
|
assert(tagR0 == TY_FLOAT);
|
2009-08-21 00:34:23 +02:00
|
|
|
#ifdef ARM_USE_VFP
|
|
|
|
o4(0xECBD7A01); // fldmfds sp!, {s14}
|
2009-08-25 21:23:43 +02:00
|
|
|
o4(0xEEB87AC7); // fsitos s14, s14
|
2009-08-21 00:34:23 +02:00
|
|
|
#else
|
2009-07-10 07:00:24 +02:00
|
|
|
o4(0xE92D0001); // stmfd sp!,{r0} // push R0
|
|
|
|
o4(0xE59D0004); // ldr r0, [sp, #4]
|
|
|
|
callRuntime((void*) runtime_int_to_float);
|
|
|
|
o4(0xE1A01000); // mov r1, r0
|
|
|
|
o4(0xE8BD0001); // ldmfd sp!,{r0} // pop R0
|
|
|
|
o4(0xE28DD004); // add sp, sp, #4 // Pop sp
|
2009-08-21 00:34:23 +02:00
|
|
|
#endif
|
2009-07-10 07:00:24 +02:00
|
|
|
} else {
|
|
|
|
// Pop TOS
|
2009-08-21 00:34:23 +02:00
|
|
|
#ifdef ARM_USE_VFP
|
|
|
|
o4(0xECBD7A01); // fldmfds sp!, {s14}
|
|
|
|
|
|
|
|
#else
|
2009-07-10 07:00:24 +02:00
|
|
|
o4(0xE8BD0002); // ldmfd sp!,{r1}
|
2009-08-21 00:34:23 +02:00
|
|
|
#endif
|
2009-07-10 07:00:24 +02:00
|
|
|
}
|
|
|
|
mStackUse -= 4;
|
|
|
|
popType();
|
|
|
|
}
|
|
|
|
|
2009-08-21 00:34:23 +02:00
|
|
|
/* Pop TOS into R2..R3 (use D6 if VFP)
|
2009-07-10 07:00:24 +02:00
|
|
|
* Make sure both R0 and TOS are doubles. Could be floats or ints.
|
|
|
|
* We know that at least one of R0 and TOS are already a double.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void setupDoubleArgs() {
|
|
|
|
Type* pR0Type = getR0Type();
|
|
|
|
Type* pTOSType = getTOSType();
|
|
|
|
TypeTag tagR0 = collapseType(pR0Type->tag);
|
|
|
|
TypeTag tagTOS = collapseType(pTOSType->tag);
|
|
|
|
if (tagR0 != TY_DOUBLE) {
|
|
|
|
if (tagR0 == TY_INT) {
|
2009-08-21 00:34:23 +02:00
|
|
|
#ifdef ARM_USE_VFP
|
2009-08-25 21:23:43 +02:00
|
|
|
o4(0xEE070A90); // fmsr s15, r0
|
|
|
|
o4(0xEEB87BE7); // fsitod d7, s15
|
2009-08-21 00:34:23 +02:00
|
|
|
|
|
|
|
#else
|
2009-07-10 07:00:24 +02:00
|
|
|
callRuntime((void*) runtime_int_to_double);
|
2009-08-21 00:34:23 +02:00
|
|
|
#endif
|
2009-07-10 07:00:24 +02:00
|
|
|
} else {
|
|
|
|
assert(tagR0 == TY_FLOAT);
|
2009-08-21 00:34:23 +02:00
|
|
|
#ifdef ARM_USE_VFP
|
2009-08-25 21:23:43 +02:00
|
|
|
o4(0xEEB77AE7); // fcvtds d7, s15
|
2009-08-21 00:34:23 +02:00
|
|
|
#else
|
2009-07-10 07:00:24 +02:00
|
|
|
callRuntime((void*) runtime_float_to_double);
|
2009-08-21 00:34:23 +02:00
|
|
|
#endif
|
2009-07-10 07:00:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (tagTOS != TY_DOUBLE) {
|
2009-08-21 00:34:23 +02:00
|
|
|
#ifdef ARM_USE_VFP
|
|
|
|
if (tagTOS == TY_INT) {
|
|
|
|
o4(0xECFD6A01); // fldmfds sp!,{s13}
|
2009-08-25 21:23:43 +02:00
|
|
|
o4(0xEEB86BE6); // fsitod d6, s13
|
2009-08-21 00:34:23 +02:00
|
|
|
} else {
|
|
|
|
assert(tagTOS == TY_FLOAT);
|
|
|
|
o4(0xECFD6A01); // fldmfds sp!,{s13}
|
2009-08-25 21:23:43 +02:00
|
|
|
o4(0xEEB76AE6); // fcvtds d6, s13
|
2009-08-21 00:34:23 +02:00
|
|
|
}
|
|
|
|
#else
|
2009-07-10 07:00:24 +02:00
|
|
|
o4(0xE92D0003); // stmfd sp!,{r0,r1} // push r0,r1
|
|
|
|
o4(0xE59D0008); // ldr r0, [sp, #8]
|
|
|
|
if (tagTOS == TY_INT) {
|
|
|
|
callRuntime((void*) runtime_int_to_double);
|
|
|
|
} else {
|
|
|
|
assert(tagTOS == TY_FLOAT);
|
|
|
|
callRuntime((void*) runtime_float_to_double);
|
|
|
|
}
|
|
|
|
o4(0xE1A02000); // mov r2, r0
|
|
|
|
o4(0xE1A03001); // mov r3, r1
|
|
|
|
o4(0xE8BD0003); // ldmfd sp!,{r0, r1} // Restore R0
|
|
|
|
o4(0xE28DD004); // add sp, sp, #4 // Pop sp
|
2009-08-21 00:34:23 +02:00
|
|
|
#endif
|
2009-07-10 07:00:24 +02:00
|
|
|
mStackUse -= 4;
|
|
|
|
} else {
|
2009-08-21 00:34:23 +02:00
|
|
|
#ifdef ARM_USE_VFP
|
2009-08-25 21:23:43 +02:00
|
|
|
o4(0xECBD6B02); // fldmfdd sp!, {d6}
|
2009-08-21 00:34:23 +02:00
|
|
|
#else
|
2009-07-10 07:00:24 +02:00
|
|
|
o4(0xE8BD000C); // ldmfd sp!,{r2,r3}
|
2009-08-21 00:34:23 +02:00
|
|
|
#endif
|
2009-07-10 07:00:24 +02:00
|
|
|
mStackUse -= 8;
|
|
|
|
}
|
|
|
|
popType();
|
|
|
|
}
|
|
|
|
|
2009-07-14 03:40:08 +02:00
|
|
|
void liReg(int t, int reg) {
|
|
|
|
assert(reg >= 0 && reg < 16);
|
|
|
|
int rN = (reg & 0xf) << 12;
|
2009-08-27 01:15:07 +02:00
|
|
|
size_t encodedImmediate;
|
|
|
|
if (encode12BitImmediate(t, &encodedImmediate)) {
|
|
|
|
o4(0xE3A00000 | encodedImmediate | rN); // mov rN, #0
|
|
|
|
} else if (encode12BitImmediate(-(t+1), &encodedImmediate)) {
|
2009-07-14 03:40:08 +02:00
|
|
|
// mvn means move constant ^ ~0
|
2009-08-27 01:15:07 +02:00
|
|
|
o4(0xE3E00000 | encodedImmediate | rN); // mvn rN, #0
|
2009-07-14 03:40:08 +02:00
|
|
|
} else {
|
2009-08-27 01:15:07 +02:00
|
|
|
o4(0xE51F0000 | rN); // ldr rN, .L3
|
|
|
|
o4(0xEA000000); // b .L99
|
|
|
|
o4(t); // .L3: .word 0
|
2009-07-14 03:40:08 +02:00
|
|
|
// .L99:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-08 21:07:32 +02:00
|
|
|
void incompatibleTypes(Type* pR0Type, Type* pType) {
|
|
|
|
error("Incompatible types old: %d new: %d", pR0Type->tag, pType->tag);
|
|
|
|
}
|
|
|
|
|
2009-07-10 07:00:24 +02:00
|
|
|
void callRuntime(void* fn) {
|
|
|
|
o4(0xE59FC000); // ldr r12, .L1
|
2009-05-16 00:12:38 +02:00
|
|
|
o4(0xEA000000); // b .L99
|
|
|
|
o4((int) fn); //.L1: .word fn
|
2009-07-10 07:00:24 +02:00
|
|
|
o4(0xE12FFF3C); //.L99: blx r12
|
|
|
|
}
|
|
|
|
|
|
|
|
// Integer math:
|
|
|
|
|
|
|
|
static int runtime_DIV(int b, int a) {
|
|
|
|
return a / b;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int runtime_MOD(int b, int a) {
|
|
|
|
return a % b;
|
|
|
|
}
|
|
|
|
|
2009-08-27 01:15:07 +02:00
|
|
|
static void runtime_structCopy(void* src, size_t size, void* dest) {
|
|
|
|
memcpy(dest, src, size);
|
|
|
|
}
|
|
|
|
|
2009-08-21 00:34:23 +02:00
|
|
|
#ifndef ARM_USE_VFP
|
|
|
|
|
2009-07-10 07:00:24 +02:00
|
|
|
// Comparison to zero
|
|
|
|
|
|
|
|
static int runtime_is_non_zero_f(float a) {
|
|
|
|
return a != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int runtime_is_non_zero_d(double a) {
|
|
|
|
return a != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Comparison to zero
|
|
|
|
|
|
|
|
static int runtime_is_zero_f(float a) {
|
|
|
|
return a == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int runtime_is_zero_d(double a) {
|
|
|
|
return a == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type conversion
|
|
|
|
|
|
|
|
static int runtime_float_to_int(float a) {
|
|
|
|
return (int) a;
|
|
|
|
}
|
|
|
|
|
|
|
|
static double runtime_float_to_double(float a) {
|
|
|
|
return (double) a;
|
2009-05-16 00:12:38 +02:00
|
|
|
}
|
|
|
|
|
2009-07-10 07:00:24 +02:00
|
|
|
static int runtime_double_to_int(double a) {
|
|
|
|
return (int) a;
|
2009-05-16 00:12:38 +02:00
|
|
|
}
|
|
|
|
|
2009-07-10 07:00:24 +02:00
|
|
|
static float runtime_double_to_float(double a) {
|
|
|
|
return (float) a;
|
|
|
|
}
|
|
|
|
|
|
|
|
static float runtime_int_to_float(int a) {
|
|
|
|
return (float) a;
|
|
|
|
}
|
|
|
|
|
|
|
|
static double runtime_int_to_double(int a) {
|
|
|
|
return (double) a;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Comparisons float
|
|
|
|
|
|
|
|
static int runtime_cmp_eq_ff(float b, float a) {
|
|
|
|
return a == b;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int runtime_cmp_ne_ff(float b, float a) {
|
|
|
|
return a != b;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int runtime_cmp_lt_ff(float b, float a) {
|
|
|
|
return a < b;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int runtime_cmp_le_ff(float b, float a) {
|
|
|
|
return a <= b;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int runtime_cmp_ge_ff(float b, float a) {
|
|
|
|
return a >= b;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int runtime_cmp_gt_ff(float b, float a) {
|
|
|
|
return a > b;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Comparisons double
|
|
|
|
|
|
|
|
static int runtime_cmp_eq_dd(double b, double a) {
|
|
|
|
return a == b;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int runtime_cmp_ne_dd(double b, double a) {
|
|
|
|
return a != b;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int runtime_cmp_lt_dd(double b, double a) {
|
|
|
|
return a < b;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int runtime_cmp_le_dd(double b, double a) {
|
|
|
|
return a <= b;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int runtime_cmp_ge_dd(double b, double a) {
|
|
|
|
return a >= b;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int runtime_cmp_gt_dd(double b, double a) {
|
|
|
|
return a > b;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Math float
|
|
|
|
|
|
|
|
static float runtime_op_add_ff(float b, float a) {
|
|
|
|
return a + b;
|
|
|
|
}
|
|
|
|
|
|
|
|
static float runtime_op_sub_ff(float b, float a) {
|
|
|
|
return a - b;
|
|
|
|
}
|
|
|
|
|
|
|
|
static float runtime_op_mul_ff(float b, float a) {
|
|
|
|
return a * b;
|
|
|
|
}
|
|
|
|
|
|
|
|
static float runtime_op_div_ff(float b, float a) {
|
|
|
|
return a / b;
|
|
|
|
}
|
|
|
|
|
|
|
|
static float runtime_op_neg_f(float a) {
|
|
|
|
return -a;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Math double
|
|
|
|
|
|
|
|
static double runtime_op_add_dd(double b, double a) {
|
|
|
|
return a + b;
|
|
|
|
}
|
|
|
|
|
|
|
|
static double runtime_op_sub_dd(double b, double a) {
|
|
|
|
return a - b;
|
|
|
|
}
|
|
|
|
|
|
|
|
static double runtime_op_mul_dd(double b, double a) {
|
|
|
|
return a * b;
|
|
|
|
}
|
|
|
|
|
|
|
|
static double runtime_op_div_dd(double b, double a) {
|
|
|
|
return a / b;
|
|
|
|
}
|
|
|
|
|
|
|
|
static double runtime_op_neg_d(double a) {
|
|
|
|
return -a;
|
2009-05-16 00:12:38 +02:00
|
|
|
}
|
2009-06-18 04:13:52 +02:00
|
|
|
|
2009-08-21 00:34:23 +02:00
|
|
|
#endif
|
|
|
|
|
2009-06-18 04:13:52 +02:00
|
|
|
static const int STACK_ALIGNMENT = 8;
|
|
|
|
int mStackUse;
|
|
|
|
// This variable holds the amount we adjusted the stack in the most
|
|
|
|
// recent endFunctionCallArguments call. It's examined by the
|
|
|
|
// following adjustStackAfterCall call.
|
|
|
|
int mStackAlignmentAdjustment;
|
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-07-10 07:00:24 +02:00
|
|
|
virtual int functionEntry(Type* pDecl) {
|
2009-05-12 21:48:35 +02:00
|
|
|
o(0xe58955); /* push %ebp, mov %esp, %ebp */
|
|
|
|
return oad(0xec81, 0); /* sub $xxx, %esp */
|
|
|
|
}
|
|
|
|
|
2009-07-10 07:00:24 +02:00
|
|
|
virtual void functionExit(Type* pDecl, 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-07-18 01:35:23 +02:00
|
|
|
virtual void li(int i) {
|
2009-07-07 03:33:20 +02:00
|
|
|
oad(0xb8, i); /* mov $xx, %eax */
|
2009-07-18 01:35:23 +02:00
|
|
|
setR0Type(mkpInt);
|
2009-07-07 03:33:20 +02:00
|
|
|
}
|
|
|
|
|
2009-07-08 22:04:41 +02:00
|
|
|
virtual void loadFloat(int address, Type* pType) {
|
2009-07-07 23:48:51 +02:00
|
|
|
setR0Type(pType);
|
2009-07-08 22:04:41 +02:00
|
|
|
switch (pType->tag) {
|
|
|
|
case TY_FLOAT:
|
|
|
|
oad(0x05D9, address); // flds
|
|
|
|
break;
|
|
|
|
case TY_DOUBLE:
|
|
|
|
oad(0x05DD, address); // fldl
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
break;
|
|
|
|
}
|
2009-05-11 23:49:29 +02:00
|
|
|
}
|
|
|
|
|
2009-08-27 01:15:07 +02:00
|
|
|
virtual void addStructOffsetR0(int offset, Type* pType) {
|
|
|
|
if (offset) {
|
|
|
|
oad(0x05, offset); // addl offset, %eax
|
|
|
|
}
|
|
|
|
setR0Type(pType, ET_LVALUE);
|
|
|
|
}
|
|
|
|
|
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-07-09 22:34:25 +02:00
|
|
|
Type* pR0Type = getR0Type();
|
|
|
|
TypeTag tagR0 = pR0Type->tag;
|
|
|
|
bool isFloatR0 = isFloatTag(tagR0);
|
|
|
|
if (isFloatR0) {
|
|
|
|
o(0xeed9); // fldz
|
|
|
|
o(0xe9da); // fucompp
|
|
|
|
o(0xe0df); // fnstsw %ax
|
|
|
|
o(0x9e); // sahf
|
|
|
|
} else {
|
|
|
|
o(0xc085); // test %eax, %eax
|
|
|
|
}
|
|
|
|
// Use two output statements to generate one instruction.
|
|
|
|
o(0x0f); // je/jne xxx
|
2009-05-11 23:49:29 +02:00
|
|
|
return psym(0x84 + l, t);
|
|
|
|
}
|
|
|
|
|
2009-07-18 01:35:23 +02:00
|
|
|
virtual void gcmp(int op) {
|
2009-07-09 05:40:31 +02:00
|
|
|
Type* pR0Type = getR0Type();
|
|
|
|
Type* pTOSType = getTOSType();
|
|
|
|
TypeTag tagR0 = pR0Type->tag;
|
|
|
|
TypeTag tagTOS = pTOSType->tag;
|
|
|
|
bool isFloatR0 = isFloatTag(tagR0);
|
|
|
|
bool isFloatTOS = isFloatTag(tagTOS);
|
|
|
|
if (!isFloatR0 && !isFloatTOS) {
|
|
|
|
int t = decodeOp(op);
|
|
|
|
o(0x59); /* pop %ecx */
|
|
|
|
o(0xc139); /* cmp %eax,%ecx */
|
2009-07-18 01:35:23 +02:00
|
|
|
li(0);
|
2009-07-09 05:40:31 +02:00
|
|
|
o(0x0f); /* setxx %al */
|
|
|
|
o(t + 0x90);
|
|
|
|
o(0xc0);
|
|
|
|
popType();
|
|
|
|
} else {
|
|
|
|
setupFloatOperands();
|
|
|
|
switch (op) {
|
|
|
|
case OP_EQUALS:
|
|
|
|
o(0xe9da); // fucompp
|
|
|
|
o(0xe0df); // fnstsw %ax
|
|
|
|
o(0x9e); // sahf
|
|
|
|
o(0xc0940f); // sete %al
|
|
|
|
o(0xc29b0f); // setnp %dl
|
|
|
|
o(0xd021); // andl %edx, %eax
|
|
|
|
break;
|
|
|
|
case OP_NOT_EQUALS:
|
|
|
|
o(0xe9da); // fucompp
|
|
|
|
o(0xe0df); // fnstsw %ax
|
|
|
|
o(0x9e); // sahf
|
|
|
|
o(0xc0950f); // setne %al
|
|
|
|
o(0xc29a0f); // setp %dl
|
|
|
|
o(0xd009); // orl %edx, %eax
|
|
|
|
break;
|
|
|
|
case OP_GREATER_EQUAL:
|
|
|
|
o(0xe9da); // fucompp
|
|
|
|
o(0xe0df); // fnstsw %ax
|
|
|
|
o(0x05c4f6); // testb $5, %ah
|
|
|
|
o(0xc0940f); // sete %al
|
|
|
|
break;
|
|
|
|
case OP_LESS:
|
|
|
|
o(0xc9d9); // fxch %st(1)
|
|
|
|
o(0xe9da); // fucompp
|
|
|
|
o(0xe0df); // fnstsw %ax
|
|
|
|
o(0x9e); // sahf
|
|
|
|
o(0xc0970f); // seta %al
|
|
|
|
break;
|
|
|
|
case OP_LESS_EQUAL:
|
|
|
|
o(0xc9d9); // fxch %st(1)
|
|
|
|
o(0xe9da); // fucompp
|
|
|
|
o(0xe0df); // fnstsw %ax
|
|
|
|
o(0x9e); // sahf
|
|
|
|
o(0xc0930f); // setea %al
|
|
|
|
break;
|
|
|
|
case OP_GREATER:
|
|
|
|
o(0xe9da); // fucompp
|
|
|
|
o(0xe0df); // fnstsw %ax
|
|
|
|
o(0x45c4f6); // testb $69, %ah
|
|
|
|
o(0xc0940f); // sete %al
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error("Unknown comparison op");
|
|
|
|
}
|
|
|
|
o(0xc0b60f); // movzbl %al, %eax
|
|
|
|
}
|
2009-07-18 01:35:23 +02:00
|
|
|
setR0Type(mkpInt);
|
2009-05-11 23:49:29 +02:00
|
|
|
}
|
|
|
|
|
2009-05-14 00:10:04 +02:00
|
|
|
virtual void genOp(int op) {
|
2009-07-09 05:40:31 +02:00
|
|
|
Type* pR0Type = getR0Type();
|
|
|
|
Type* pTOSType = getTOSType();
|
|
|
|
TypeTag tagR0 = pR0Type->tag;
|
|
|
|
TypeTag tagTOS = pTOSType->tag;
|
|
|
|
bool isFloatR0 = isFloatTag(tagR0);
|
|
|
|
bool isFloatTOS = isFloatTag(tagTOS);
|
|
|
|
if (!isFloatR0 && !isFloatTOS) {
|
2009-08-04 23:56:09 +02:00
|
|
|
bool isPtrR0 = isPointerTag(tagR0);
|
|
|
|
bool isPtrTOS = isPointerTag(tagTOS);
|
2009-07-14 03:40:08 +02:00
|
|
|
if (isPtrR0 || isPtrTOS) {
|
|
|
|
if (isPtrR0 && isPtrTOS) {
|
|
|
|
if (op != OP_MINUS) {
|
|
|
|
error("Unsupported pointer-pointer operation %d.", op);
|
|
|
|
}
|
|
|
|
if (! typeEqual(pR0Type, pTOSType)) {
|
|
|
|
error("Incompatible pointer types for subtraction.");
|
|
|
|
}
|
|
|
|
o(0x59); /* pop %ecx */
|
|
|
|
o(decodeOp(op));
|
|
|
|
popType();
|
|
|
|
setR0Type(mkpInt);
|
|
|
|
int size = sizeOf(pR0Type->pHead);
|
|
|
|
if (size != 1) {
|
|
|
|
pushR0();
|
2009-07-18 01:35:23 +02:00
|
|
|
li(size);
|
2009-07-14 03:40:08 +02:00
|
|
|
// TODO: Optimize for power-of-two.
|
|
|
|
genOp(OP_DIV);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (! (op == OP_PLUS || (op == OP_MINUS && isPtrR0))) {
|
|
|
|
error("Unsupported pointer-scalar operation %d", op);
|
|
|
|
}
|
2009-08-04 23:56:09 +02:00
|
|
|
Type* pPtrType = getPointerArithmeticResultType(
|
|
|
|
pR0Type, pTOSType);
|
2009-07-14 03:40:08 +02:00
|
|
|
o(0x59); /* pop %ecx */
|
|
|
|
int size = sizeOf(pPtrType->pHead);
|
|
|
|
if (size != 1) {
|
|
|
|
// TODO: Optimize for power-of-two.
|
|
|
|
if (isPtrR0) {
|
|
|
|
oad(0xC969, size); // imull $size, %ecx
|
|
|
|
} else {
|
|
|
|
oad(0xC069, size); // mul $size, %eax
|
|
|
|
}
|
|
|
|
}
|
|
|
|
o(decodeOp(op));
|
|
|
|
popType();
|
|
|
|
setR0Type(pPtrType);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
o(0x59); /* pop %ecx */
|
|
|
|
o(decodeOp(op));
|
|
|
|
if (op == OP_MOD)
|
|
|
|
o(0x92); /* xchg %edx, %eax */
|
|
|
|
popType();
|
|
|
|
}
|
2009-07-09 05:40:31 +02:00
|
|
|
} else {
|
|
|
|
Type* pResultType = tagR0 > tagTOS ? pR0Type : pTOSType;
|
|
|
|
setupFloatOperands();
|
|
|
|
// Both float. x87 R0 == left hand, x87 R1 == right hand
|
|
|
|
switch (op) {
|
|
|
|
case OP_MUL:
|
|
|
|
o(0xc9de); // fmulp
|
|
|
|
break;
|
|
|
|
case OP_DIV:
|
|
|
|
o(0xf1de); // fdivp
|
|
|
|
break;
|
|
|
|
case OP_PLUS:
|
|
|
|
o(0xc1de); // faddp
|
|
|
|
break;
|
|
|
|
case OP_MINUS:
|
|
|
|
o(0xe1de); // fsubp
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error("Unsupported binary floating operation.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
setR0Type(pResultType);
|
|
|
|
}
|
2009-05-12 21:48:35 +02:00
|
|
|
}
|
|
|
|
|
2009-07-18 01:35:23 +02:00
|
|
|
virtual void gUnaryCmp(int op) {
|
2009-07-09 05:40:31 +02:00
|
|
|
if (op != OP_LOGICAL_NOT) {
|
|
|
|
error("Unknown unary cmp %d", op);
|
|
|
|
} else {
|
|
|
|
Type* pR0Type = getR0Type();
|
|
|
|
TypeTag tag = collapseType(pR0Type->tag);
|
|
|
|
switch(tag) {
|
|
|
|
case TY_INT: {
|
|
|
|
oad(0xb9, 0); /* movl $0, %ecx */
|
|
|
|
int t = decodeOp(op);
|
|
|
|
o(0xc139); /* cmp %eax,%ecx */
|
2009-07-18 01:35:23 +02:00
|
|
|
li(0);
|
2009-07-09 05:40:31 +02:00
|
|
|
o(0x0f); /* setxx %al */
|
|
|
|
o(t + 0x90);
|
|
|
|
o(0xc0);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TY_FLOAT:
|
|
|
|
case TY_DOUBLE:
|
|
|
|
o(0xeed9); // fldz
|
|
|
|
o(0xe9da); // fucompp
|
|
|
|
o(0xe0df); // fnstsw %ax
|
|
|
|
o(0x9e); // sahf
|
|
|
|
o(0xc0950f); // setne %al
|
|
|
|
o(0xc29a0f); // setp %dl
|
|
|
|
o(0xd009); // orl %edx, %eax
|
|
|
|
o(0xc0b60f); // movzbl %al, %eax
|
|
|
|
o(0x01f083); // xorl $1, %eax
|
|
|
|
break;
|
|
|
|
default:
|
2009-07-10 07:00:24 +02:00
|
|
|
error("gUnaryCmp unsupported type");
|
2009-07-09 05:40:31 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-07-18 01:35:23 +02:00
|
|
|
setR0Type(mkpInt);
|
2009-07-07 02:24:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void genUnaryOp(int op) {
|
2009-07-09 05:40:31 +02:00
|
|
|
Type* pR0Type = getR0Type();
|
|
|
|
TypeTag tag = collapseType(pR0Type->tag);
|
|
|
|
switch(tag) {
|
|
|
|
case TY_INT:
|
|
|
|
oad(0xb9, 0); /* movl $0, %ecx */
|
|
|
|
o(decodeOp(op));
|
|
|
|
break;
|
|
|
|
case TY_FLOAT:
|
|
|
|
case TY_DOUBLE:
|
|
|
|
switch (op) {
|
|
|
|
case OP_MINUS:
|
|
|
|
o(0xe0d9); // fchs
|
|
|
|
break;
|
|
|
|
case OP_BIT_NOT:
|
|
|
|
error("Can't apply '~' operator to a float or double.");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error("Unknown unary op %d\n", op);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error("genUnaryOp unsupported type");
|
|
|
|
break;
|
|
|
|
}
|
2009-05-11 23:49:29 +02:00
|
|
|
}
|
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
virtual void pushR0() {
|
2009-07-09 01:48:41 +02:00
|
|
|
Type* pR0Type = getR0Type();
|
|
|
|
TypeTag r0ct = collapseType(pR0Type->tag);
|
|
|
|
switch(r0ct) {
|
|
|
|
case TY_INT:
|
|
|
|
o(0x50); /* push %eax */
|
|
|
|
break;
|
|
|
|
case TY_FLOAT:
|
|
|
|
o(0x50); /* push %eax */
|
|
|
|
o(0x241cd9); // fstps 0(%esp)
|
|
|
|
break;
|
|
|
|
case TY_DOUBLE:
|
|
|
|
o(0x50); /* push %eax */
|
|
|
|
o(0x50); /* push %eax */
|
|
|
|
o(0x241cdd); // fstpl 0(%esp)
|
|
|
|
break;
|
|
|
|
default:
|
2009-07-09 22:34:25 +02:00
|
|
|
error("pushR0 unsupported type %d", r0ct);
|
2009-07-09 01:48:41 +02:00
|
|
|
break;
|
|
|
|
}
|
2009-07-07 23:48:51 +02:00
|
|
|
pushType();
|
2009-05-11 23:49:29 +02:00
|
|
|
}
|
|
|
|
|
2009-07-29 19:28:18 +02:00
|
|
|
virtual void over() {
|
|
|
|
// We know it's only used for int-ptr ops (++/--)
|
|
|
|
|
|
|
|
Type* pR0Type = getR0Type();
|
|
|
|
TypeTag r0ct = collapseType(pR0Type->tag);
|
|
|
|
|
|
|
|
Type* pTOSType = getTOSType();
|
|
|
|
TypeTag tosct = collapseType(pTOSType->tag);
|
|
|
|
|
|
|
|
assert (r0ct == TY_INT && tosct == TY_INT);
|
|
|
|
|
|
|
|
o(0x59); /* pop %ecx */
|
|
|
|
o(0x50); /* push %eax */
|
|
|
|
o(0x51); /* push %ecx */
|
|
|
|
|
|
|
|
overType();
|
|
|
|
}
|
|
|
|
|
2009-07-18 01:35:23 +02:00
|
|
|
virtual void popR0() {
|
|
|
|
Type* pR0Type = getR0Type();
|
|
|
|
TypeTag r0ct = collapseType(pR0Type->tag);
|
|
|
|
switch(r0ct) {
|
|
|
|
case TY_INT:
|
|
|
|
o(0x58); /* popl %eax */
|
|
|
|
break;
|
|
|
|
case TY_FLOAT:
|
|
|
|
o(0x2404d9); // flds (%esp)
|
|
|
|
o(0x58); /* popl %eax */
|
|
|
|
break;
|
|
|
|
case TY_DOUBLE:
|
|
|
|
o(0x2404dd); // fldl (%esp)
|
|
|
|
o(0x58); /* popl %eax */
|
|
|
|
o(0x58); /* popl %eax */
|
|
|
|
break;
|
|
|
|
default:
|
2009-07-29 19:28:18 +02:00
|
|
|
error("popR0 unsupported type %d", r0ct);
|
2009-07-18 01:35:23 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
popType();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void storeR0ToTOS() {
|
|
|
|
Type* pPointerType = getTOSType();
|
2009-07-07 02:24:34 +02:00
|
|
|
assert(pPointerType->tag == TY_POINTER);
|
2009-07-17 03:24:47 +02:00
|
|
|
Type* pTargetType = pPointerType->pHead;
|
|
|
|
convertR0(pTargetType);
|
2009-05-11 23:49:29 +02:00
|
|
|
o(0x59); /* pop %ecx */
|
2009-07-07 23:48:51 +02:00
|
|
|
popType();
|
2009-07-17 03:24:47 +02:00
|
|
|
switch (pTargetType->tag) {
|
2009-07-31 01:57:33 +02:00
|
|
|
case TY_POINTER:
|
2009-07-07 02:24:34 +02:00
|
|
|
case TY_INT:
|
|
|
|
o(0x0189); /* movl %eax/%al, (%ecx) */
|
|
|
|
break;
|
2009-08-03 23:42:57 +02:00
|
|
|
case TY_SHORT:
|
|
|
|
o(0x018966); /* movw %ax, (%ecx) */
|
|
|
|
break;
|
2009-07-07 02:24:34 +02:00
|
|
|
case TY_CHAR:
|
|
|
|
o(0x0188); /* movl %eax/%al, (%ecx) */
|
|
|
|
break;
|
2009-07-09 01:48:41 +02:00
|
|
|
case TY_FLOAT:
|
|
|
|
o(0x19d9); /* fstps (%ecx) */
|
|
|
|
break;
|
|
|
|
case TY_DOUBLE:
|
|
|
|
o(0x19dd); /* fstpl (%ecx) */
|
|
|
|
break;
|
2009-08-27 01:15:07 +02:00
|
|
|
case TY_STRUCT:
|
|
|
|
{
|
|
|
|
// TODO: use alignment information to use movsw/movsl instead of movsb
|
|
|
|
int size = sizeOf(pTargetType);
|
|
|
|
if (size > 0) {
|
|
|
|
o(0x9c); // pushf
|
|
|
|
o(0x57); // pushl %edi
|
|
|
|
o(0x56); // pushl %esi
|
|
|
|
o(0xcf89); // movl %ecx, %edi
|
|
|
|
o(0xc689); // movl %eax, %esi
|
|
|
|
oad(0xb9, size); // mov #size, %ecx
|
|
|
|
o(0xfc); // cld
|
|
|
|
o(0xf3); // rep
|
|
|
|
o(0xa4); // movsb
|
|
|
|
o(0x5e); // popl %esi
|
|
|
|
o(0x5f); // popl %edi
|
|
|
|
o(0x9d); // popf
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2009-07-07 02:24:34 +02:00
|
|
|
default:
|
2009-07-31 04:06:34 +02:00
|
|
|
error("storeR0ToTOS: unsupported type %d",
|
|
|
|
pTargetType->tag);
|
2009-07-07 02:24:34 +02:00
|
|
|
break;
|
|
|
|
}
|
2009-05-12 21:48:35 +02:00
|
|
|
}
|
|
|
|
|
2009-07-18 01:35:23 +02:00
|
|
|
virtual void loadR0FromR0() {
|
|
|
|
Type* pPointerType = getR0Type();
|
2009-07-07 02:24:34 +02:00
|
|
|
assert(pPointerType->tag == TY_POINTER);
|
2009-08-05 00:39:49 +02:00
|
|
|
Type* pNewType = pPointerType->pHead;
|
|
|
|
TypeTag tag = pNewType->tag;
|
2009-08-04 23:56:09 +02:00
|
|
|
switch (tag) {
|
2009-07-29 20:36:04 +02:00
|
|
|
case TY_POINTER:
|
2009-07-07 02:24:34 +02:00
|
|
|
case TY_INT:
|
2009-07-09 22:34:25 +02:00
|
|
|
o2(0x008b); /* mov (%eax), %eax */
|
2009-07-07 02:24:34 +02:00
|
|
|
break;
|
2009-08-03 23:42:57 +02:00
|
|
|
case TY_SHORT:
|
|
|
|
o(0xbf0f); /* movswl (%eax), %eax */
|
|
|
|
ob(0);
|
|
|
|
break;
|
2009-07-07 02:24:34 +02:00
|
|
|
case TY_CHAR:
|
|
|
|
o(0xbe0f); /* movsbl (%eax), %eax */
|
2009-07-09 22:34:25 +02:00
|
|
|
ob(0); /* add zero in code */
|
|
|
|
break;
|
|
|
|
case TY_FLOAT:
|
|
|
|
o2(0x00d9); // flds (%eax)
|
|
|
|
break;
|
|
|
|
case TY_DOUBLE:
|
|
|
|
o2(0x00dd); // fldl (%eax)
|
2009-07-07 02:24:34 +02:00
|
|
|
break;
|
2009-08-05 00:39:49 +02:00
|
|
|
case TY_ARRAY:
|
|
|
|
pNewType = pNewType->pTail;
|
|
|
|
break;
|
2009-08-27 01:15:07 +02:00
|
|
|
case TY_STRUCT:
|
|
|
|
break;
|
2009-07-07 02:24:34 +02:00
|
|
|
default:
|
2009-08-04 23:56:09 +02:00
|
|
|
error("loadR0FromR0: unsupported type %d", tag);
|
2009-07-07 02:24:34 +02:00
|
|
|
break;
|
|
|
|
}
|
2009-08-05 00:39:49 +02:00
|
|
|
setR0Type(pNewType);
|
2009-05-11 23:49:29 +02:00
|
|
|
}
|
|
|
|
|
2009-07-31 04:06:34 +02:00
|
|
|
virtual void leaR0(int ea, Type* pPointerType, ExpressionType et) {
|
2009-05-11 23:49:29 +02:00
|
|
|
gmov(10, ea); /* leal EA, %eax */
|
2009-07-31 04:06:34 +02:00
|
|
|
setR0Type(pPointerType, et);
|
2009-05-11 23:49:29 +02:00
|
|
|
}
|
|
|
|
|
2009-07-30 01:22:26 +02:00
|
|
|
virtual int leaForward(int ea, Type* pPointerType) {
|
|
|
|
oad(0xb8, ea); /* mov $xx, %eax */
|
|
|
|
setR0Type(pPointerType);
|
|
|
|
return getPC() - 4;
|
|
|
|
}
|
|
|
|
|
2009-08-04 23:56:09 +02:00
|
|
|
virtual void convertR0Imp(Type* pType, bool isCast){
|
2009-07-08 22:04:41 +02:00
|
|
|
Type* pR0Type = getR0Type();
|
|
|
|
if (pR0Type == NULL) {
|
2009-07-08 23:51:31 +02:00
|
|
|
assert(false);
|
2009-07-08 22:04:41 +02:00
|
|
|
setR0Type(pType);
|
2009-07-07 23:48:51 +02:00
|
|
|
return;
|
|
|
|
}
|
2009-08-04 23:56:09 +02:00
|
|
|
if (isPointerType(pType) && isPointerType(pR0Type)) {
|
|
|
|
Type* pA = pR0Type;
|
|
|
|
Type* pB = pType;
|
|
|
|
// Array decays to pointer
|
|
|
|
if (pA->tag == TY_ARRAY && pB->tag == TY_POINTER) {
|
|
|
|
pA = pA->pTail;
|
|
|
|
}
|
2009-08-25 21:23:43 +02:00
|
|
|
if (! (typeEqual(pA, pB)
|
|
|
|
|| pB->pHead->tag == TY_VOID
|
|
|
|
|| (pA->tag == TY_POINTER && pB->tag == TY_POINTER && isCast)
|
|
|
|
)) {
|
|
|
|
error("Incompatible pointer or array types");
|
2009-08-04 23:56:09 +02:00
|
|
|
}
|
|
|
|
} else if (bitsSame(pType, pR0Type)) {
|
2009-07-08 22:04:41 +02:00
|
|
|
// do nothing special
|
|
|
|
} else if (isFloatType(pType) && isFloatType(pR0Type)) {
|
|
|
|
// do nothing special, both held in same register on x87.
|
|
|
|
} else {
|
2009-07-08 23:51:31 +02:00
|
|
|
TypeTag r0Tag = collapseType(pR0Type->tag);
|
|
|
|
TypeTag destTag = collapseType(pType->tag);
|
|
|
|
if (r0Tag == TY_INT && isFloatTag(destTag)) {
|
|
|
|
// Convert R0 from int to float
|
|
|
|
o(0x50); // push %eax
|
|
|
|
o(0x2404DB); // fildl 0(%esp)
|
|
|
|
o(0x58); // pop %eax
|
|
|
|
} else if (isFloatTag(r0Tag) && destTag == TY_INT) {
|
|
|
|
// Convert R0 from float to int. Complicated because
|
|
|
|
// need to save and restore the rounding mode.
|
|
|
|
o(0x50); // push %eax
|
|
|
|
o(0x50); // push %eax
|
|
|
|
o(0x02247cD9); // fnstcw 2(%esp)
|
|
|
|
o(0x2444b70f); // movzwl 2(%esp), %eax
|
|
|
|
o(0x02);
|
|
|
|
o(0x0cb4); // movb $12, %ah
|
|
|
|
o(0x24048966); // movw %ax, 0(%esp)
|
|
|
|
o(0x242cd9); // fldcw 0(%esp)
|
|
|
|
o(0x04245cdb); // fistpl 4(%esp)
|
|
|
|
o(0x02246cd9); // fldcw 2(%esp)
|
|
|
|
o(0x58); // pop %eax
|
|
|
|
o(0x58); // pop %eax
|
|
|
|
} else {
|
|
|
|
error("Incompatible types old: %d new: %d",
|
|
|
|
pR0Type->tag, pType->tag);
|
|
|
|
}
|
2009-07-08 22:04:41 +02:00
|
|
|
}
|
|
|
|
setR0Type(pType);
|
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-07-17 03:24:47 +02:00
|
|
|
virtual size_t storeR0ToArg(int l, Type* pArgType) {
|
|
|
|
convertR0(pArgType);
|
2009-07-08 22:04:41 +02:00
|
|
|
Type* pR0Type = getR0Type();
|
|
|
|
TypeTag r0ct = collapseType(pR0Type->tag);
|
|
|
|
switch(r0ct) {
|
|
|
|
case TY_INT:
|
|
|
|
oad(0x248489, l); /* movl %eax, xxx(%esp) */
|
|
|
|
return 4;
|
|
|
|
case TY_FLOAT:
|
|
|
|
oad(0x249CD9, l); /* fstps xxx(%esp) */
|
|
|
|
return 4;
|
|
|
|
case TY_DOUBLE:
|
|
|
|
oad(0x249CDD, l); /* fstpl xxx(%esp) */
|
|
|
|
return 8;
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
return 0;
|
|
|
|
}
|
2009-05-11 23:49:29 +02:00
|
|
|
}
|
|
|
|
|
2009-07-10 07:00:24 +02:00
|
|
|
virtual void endFunctionCallArguments(Type* pDecl, int a, int l) {
|
2009-05-15 23:31:47 +02:00
|
|
|
* (int*) a = l;
|
|
|
|
}
|
|
|
|
|
2009-07-07 23:48:51 +02:00
|
|
|
virtual int callForward(int symbol, Type* pFunc) {
|
2009-07-30 01:22:26 +02:00
|
|
|
assert(pFunc->tag == TY_FUNC);
|
2009-07-07 23:48:51 +02:00
|
|
|
setR0Type(pFunc->pHead);
|
2009-05-11 23:49:29 +02:00
|
|
|
return psym(0xe8, symbol); /* call xxx */
|
|
|
|
}
|
|
|
|
|
2009-07-07 23:48:51 +02:00
|
|
|
virtual void callIndirect(int l, Type* pFunc) {
|
2009-07-30 01:22:26 +02:00
|
|
|
assert(pFunc->tag == TY_FUNC);
|
2009-07-31 04:06:34 +02:00
|
|
|
popType(); // Get rid of indirect fn pointer type
|
2009-07-07 23:48:51 +02:00
|
|
|
setR0Type(pFunc->pHead);
|
2009-05-11 23:49:29 +02:00
|
|
|
oad(0x2494ff, l); /* call *xxx(%esp) */
|
|
|
|
}
|
|
|
|
|
2009-07-10 07:00:24 +02:00
|
|
|
virtual void adjustStackAfterCall(Type* pDecl, int l, bool isIndirect) {
|
2009-07-30 01:22:26 +02:00
|
|
|
assert(pDecl->tag == TY_FUNC);
|
2009-05-15 23:31:47 +02:00
|
|
|
if (isIndirect) {
|
|
|
|
l += 4;
|
|
|
|
}
|
2009-06-18 04:13:52 +02:00
|
|
|
if (l > 0) {
|
|
|
|
oad(0xc481, l); /* add $xxx, %esp */
|
|
|
|
}
|
2009-05-11 23:49:29 +02:00
|
|
|
}
|
|
|
|
|
2009-05-14 01:24:17 +02:00
|
|
|
virtual int jumpOffset() {
|
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
|
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-07-30 01:22:26 +02:00
|
|
|
/* output a symbol and patch all calls to it, using absolute address */
|
|
|
|
virtual void resolveForward(int t) {
|
|
|
|
int n;
|
|
|
|
int pc = getPC();
|
|
|
|
while (t) {
|
|
|
|
n = *(int *) t; /* next value */
|
|
|
|
*(int *) t = pc;
|
|
|
|
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-07-07 02:24:34 +02:00
|
|
|
/**
|
2009-07-09 01:48:41 +02:00
|
|
|
* Alignment (in bytes) for this type of data
|
2009-07-07 02:24:34 +02:00
|
|
|
*/
|
2009-07-10 07:00:24 +02:00
|
|
|
virtual size_t alignmentOf(Type* pType){
|
2009-07-24 03:56:20 +02:00
|
|
|
switch (pType->tag) {
|
|
|
|
case TY_CHAR:
|
|
|
|
return 1;
|
2009-08-03 23:42:57 +02:00
|
|
|
case TY_SHORT:
|
|
|
|
return 2;
|
2009-08-04 23:56:09 +02:00
|
|
|
case TY_ARRAY:
|
|
|
|
return alignmentOf(pType->pHead);
|
2009-08-27 01:15:07 +02:00
|
|
|
case TY_STRUCT:
|
|
|
|
return pType->pHead->alignment & 0x7fffffff;
|
2009-08-04 23:56:09 +02:00
|
|
|
case TY_FUNC:
|
|
|
|
error("alignment of func not supported");
|
|
|
|
return 1;
|
2009-07-24 03:56:20 +02:00
|
|
|
default:
|
|
|
|
return 4;
|
|
|
|
}
|
2009-07-07 02:24:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Array element alignment (in bytes) for this type of data.
|
|
|
|
*/
|
|
|
|
virtual size_t sizeOf(Type* pType){
|
|
|
|
switch(pType->tag) {
|
|
|
|
case TY_INT:
|
|
|
|
return 4;
|
2009-08-03 23:42:57 +02:00
|
|
|
case TY_SHORT:
|
|
|
|
return 2;
|
2009-07-07 02:24:34 +02:00
|
|
|
case TY_CHAR:
|
|
|
|
return 1;
|
|
|
|
case TY_FLOAT:
|
|
|
|
return 4;
|
|
|
|
case TY_DOUBLE:
|
|
|
|
return 8;
|
|
|
|
case TY_POINTER:
|
|
|
|
return 4;
|
2009-08-04 23:56:09 +02:00
|
|
|
case TY_ARRAY:
|
|
|
|
return pType->length * sizeOf(pType->pHead);
|
2009-08-27 01:15:07 +02:00
|
|
|
case TY_STRUCT:
|
|
|
|
return pType->pHead->length;
|
2009-08-04 23:56:09 +02:00
|
|
|
default:
|
|
|
|
error("Unsupported type %d", pType->tag);
|
|
|
|
return 0;
|
2009-07-07 02:24:34 +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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-09 22:34:25 +02:00
|
|
|
/* Output exactly 2 bytes
|
|
|
|
*/
|
|
|
|
void o2(int n) {
|
|
|
|
ob(n & 0xff);
|
|
|
|
ob(0xff & (n >> 8));
|
|
|
|
}
|
|
|
|
|
2009-05-20 02:12:17 +02:00
|
|
|
/* psym is used to put an instruction with a data field which is a
|
|
|
|
reference to a symbol. It is in fact the same as oad ! */
|
|
|
|
int psym(int n, int t) {
|
|
|
|
return oad(n, t);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* instruction + address */
|
|
|
|
int oad(int n, int t) {
|
|
|
|
o(n);
|
|
|
|
int 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-07-09 05:40:31 +02:00
|
|
|
|
|
|
|
void setupFloatOperands() {
|
|
|
|
Type* pR0Type = getR0Type();
|
|
|
|
Type* pTOSType = getTOSType();
|
|
|
|
TypeTag tagR0 = pR0Type->tag;
|
|
|
|
TypeTag tagTOS = pTOSType->tag;
|
|
|
|
bool isFloatR0 = isFloatTag(tagR0);
|
|
|
|
bool isFloatTOS = isFloatTag(tagTOS);
|
|
|
|
if (! isFloatR0) {
|
|
|
|
// Convert R0 from int to float
|
|
|
|
o(0x50); // push %eax
|
|
|
|
o(0x2404DB); // fildl 0(%esp)
|
|
|
|
o(0x58); // pop %eax
|
|
|
|
}
|
|
|
|
if (! isFloatTOS){
|
|
|
|
o(0x2404DB); // fildl 0(%esp);
|
|
|
|
o(0x58); // pop %eax
|
|
|
|
} else {
|
|
|
|
if (tagTOS == TY_FLOAT) {
|
|
|
|
o(0x2404d9); // flds (%esp)
|
|
|
|
o(0x58); // pop %eax
|
|
|
|
} else {
|
|
|
|
o(0x2404dd); // fldl (%esp)
|
|
|
|
o(0x58); // pop %eax
|
|
|
|
o(0x58); // pop %eax
|
|
|
|
}
|
|
|
|
}
|
2009-07-10 07:00:24 +02:00
|
|
|
popType();
|
2009-07-09 05:40:31 +02:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2009-09-10 04:08:54 +02:00
|
|
|
virtual void init(ICodeBuf* pCodeBuf) {
|
2009-06-12 06:12:23 +02:00
|
|
|
mpBase->init(pCodeBuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setErrorSink(ErrorSink* pErrorSink) {
|
|
|
|
mpBase->setErrorSink(pErrorSink);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* returns address to patch with local variable size
|
|
|
|
*/
|
2009-07-10 07:00:24 +02:00
|
|
|
virtual int functionEntry(Type* pDecl) {
|
|
|
|
int result = mpBase->functionEntry(pDecl);
|
|
|
|
fprintf(stderr, "functionEntry(pDecl) -> %d\n", result);
|
2009-06-12 06:12:23 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-07-10 07:00:24 +02:00
|
|
|
virtual void functionExit(Type* pDecl, int localVariableAddress, int localVariableSize) {
|
|
|
|
fprintf(stderr, "functionExit(pDecl, %d, %d)\n",
|
|
|
|
localVariableAddress, localVariableSize);
|
|
|
|
mpBase->functionExit(pDecl, localVariableAddress, localVariableSize);
|
2009-06-12 06:12:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* load immediate value */
|
2009-07-18 01:35:23 +02:00
|
|
|
virtual void li(int t) {
|
2009-06-12 06:12:23 +02:00
|
|
|
fprintf(stderr, "li(%d)\n", t);
|
2009-07-18 01:35:23 +02:00
|
|
|
mpBase->li(t);
|
2009-06-12 06:12:23 +02:00
|
|
|
}
|
|
|
|
|
2009-07-08 22:04:41 +02:00
|
|
|
virtual void loadFloat(int address, Type* pType) {
|
2009-07-17 03:24:47 +02:00
|
|
|
fprintf(stderr, "loadFloat(%d, type=%d)\n", address, pType->tag);
|
2009-07-08 22:04:41 +02:00
|
|
|
mpBase->loadFloat(address, pType);
|
2009-07-07 03:33:20 +02:00
|
|
|
}
|
|
|
|
|
2009-08-27 01:15:07 +02:00
|
|
|
virtual void addStructOffsetR0(int offset, Type* pType) {
|
|
|
|
fprintf(stderr, "addStructOffsetR0(%d, type=%d)\n", offset, pType->tag);
|
|
|
|
mpBase->addStructOffsetR0(offset, pType);
|
|
|
|
}
|
|
|
|
|
2009-06-12 06:12:23 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-07-18 01:35:23 +02:00
|
|
|
virtual void gcmp(int op) {
|
|
|
|
fprintf(stderr, "gcmp(%d)\n", op);
|
|
|
|
mpBase->gcmp(op);
|
2009-06-12 06:12:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void genOp(int op) {
|
|
|
|
fprintf(stderr, "genOp(%d)\n", op);
|
|
|
|
mpBase->genOp(op);
|
|
|
|
}
|
|
|
|
|
2009-07-07 02:24:34 +02:00
|
|
|
|
2009-07-18 01:35:23 +02:00
|
|
|
virtual void gUnaryCmp(int op) {
|
|
|
|
fprintf(stderr, "gUnaryCmp(%d)\n", op);
|
|
|
|
mpBase->gUnaryCmp(op);
|
2009-07-07 02:24:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void genUnaryOp(int op) {
|
|
|
|
fprintf(stderr, "genUnaryOp(%d)\n", op);
|
|
|
|
mpBase->genUnaryOp(op);
|
2009-06-12 06:12:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void pushR0() {
|
|
|
|
fprintf(stderr, "pushR0()\n");
|
|
|
|
mpBase->pushR0();
|
|
|
|
}
|
|
|
|
|
2009-07-29 19:28:18 +02:00
|
|
|
virtual void over() {
|
|
|
|
fprintf(stderr, "over()\n");
|
|
|
|
mpBase->over();
|
|
|
|
}
|
|
|
|
|
2009-07-18 01:35:23 +02:00
|
|
|
virtual void popR0() {
|
|
|
|
fprintf(stderr, "popR0()\n");
|
|
|
|
mpBase->popR0();
|
2009-06-12 06:12:23 +02:00
|
|
|
}
|
|
|
|
|
2009-07-18 01:35:23 +02:00
|
|
|
virtual void storeR0ToTOS() {
|
|
|
|
fprintf(stderr, "storeR0ToTOS()\n");
|
|
|
|
mpBase->storeR0ToTOS();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void loadR0FromR0() {
|
|
|
|
fprintf(stderr, "loadR0FromR0()\n");
|
|
|
|
mpBase->loadR0FromR0();
|
2009-06-12 06:12:23 +02:00
|
|
|
}
|
|
|
|
|
2009-07-31 04:06:34 +02:00
|
|
|
virtual void leaR0(int ea, Type* pPointerType, ExpressionType et) {
|
|
|
|
fprintf(stderr, "leaR0(%d, %d, %d)\n", ea,
|
|
|
|
pPointerType->pHead->tag, et);
|
|
|
|
mpBase->leaR0(ea, pPointerType, et);
|
2009-06-12 06:12:23 +02:00
|
|
|
}
|
|
|
|
|
2009-07-30 01:22:26 +02:00
|
|
|
virtual int leaForward(int ea, Type* pPointerType) {
|
|
|
|
fprintf(stderr, "leaForward(%d)\n", ea);
|
|
|
|
return mpBase->leaForward(ea, pPointerType);
|
|
|
|
}
|
|
|
|
|
2009-08-21 00:34:23 +02:00
|
|
|
virtual void convertR0Imp(Type* pType, bool isCast){
|
|
|
|
fprintf(stderr, "convertR0(pType tag=%d, %d)\n", pType->tag, isCast);
|
|
|
|
mpBase->convertR0Imp(pType, isCast);
|
2009-06-12 06:12:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual int beginFunctionCallArguments() {
|
|
|
|
int result = mpBase->beginFunctionCallArguments();
|
|
|
|
fprintf(stderr, "beginFunctionCallArguments() = %d\n", result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-07-17 03:24:47 +02:00
|
|
|
virtual size_t storeR0ToArg(int l, Type* pArgType) {
|
|
|
|
fprintf(stderr, "storeR0ToArg(%d, pArgType=%d)\n", l,
|
|
|
|
pArgType->tag);
|
|
|
|
return mpBase->storeR0ToArg(l, pArgType);
|
2009-06-12 06:12:23 +02:00
|
|
|
}
|
|
|
|
|
2009-07-10 07:00:24 +02:00
|
|
|
virtual void endFunctionCallArguments(Type* pDecl, int a, int l) {
|
2009-06-12 06:12:23 +02:00
|
|
|
fprintf(stderr, "endFunctionCallArguments(%d, %d)\n", a, l);
|
2009-07-10 07:00:24 +02:00
|
|
|
mpBase->endFunctionCallArguments(pDecl, a, l);
|
2009-06-12 06:12:23 +02:00
|
|
|
}
|
|
|
|
|
2009-07-07 23:48:51 +02:00
|
|
|
virtual int callForward(int symbol, Type* pFunc) {
|
|
|
|
int result = mpBase->callForward(symbol, pFunc);
|
2009-06-12 06:12:23 +02:00
|
|
|
fprintf(stderr, "callForward(%d) = %d\n", symbol, result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-07-07 23:48:51 +02:00
|
|
|
virtual void callIndirect(int l, Type* pFunc) {
|
2009-07-30 01:22:26 +02:00
|
|
|
fprintf(stderr, "callIndirect(%d returntype = %d)\n", l,
|
|
|
|
pFunc->pHead->tag);
|
2009-07-07 23:48:51 +02:00
|
|
|
mpBase->callIndirect(l, pFunc);
|
2009-06-12 06:12:23 +02:00
|
|
|
}
|
|
|
|
|
2009-07-10 07:00:24 +02:00
|
|
|
virtual void adjustStackAfterCall(Type* pDecl, int l, bool isIndirect) {
|
|
|
|
fprintf(stderr, "adjustStackAfterCall(pType, %d, %d)\n", l, isIndirect);
|
|
|
|
mpBase->adjustStackAfterCall(pDecl, l, isIndirect);
|
2009-06-12 06:12:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual int jumpOffset() {
|
|
|
|
return mpBase->jumpOffset();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* output a symbol and patch all calls to it */
|
|
|
|
virtual void gsym(int t) {
|
|
|
|
fprintf(stderr, "gsym(%d)\n", t);
|
|
|
|
mpBase->gsym(t);
|
|
|
|
}
|
|
|
|
|
2009-07-30 01:22:26 +02:00
|
|
|
virtual void resolveForward(int t) {
|
|
|
|
mpBase->resolveForward(t);
|
|
|
|
}
|
|
|
|
|
2009-06-12 06:12:23 +02:00
|
|
|
virtual int finishCompile() {
|
|
|
|
int result = mpBase->finishCompile();
|
|
|
|
fprintf(stderr, "finishCompile() = %d\n", result);
|
|
|
|
return result;
|
|
|
|
}
|
2009-07-07 02:24:34 +02:00
|
|
|
|
|
|
|
/**
|
2009-07-09 01:48:41 +02:00
|
|
|
* Alignment (in bytes) for this type of data
|
2009-07-07 02:24:34 +02:00
|
|
|
*/
|
2009-07-10 07:00:24 +02:00
|
|
|
virtual size_t alignmentOf(Type* pType){
|
|
|
|
return mpBase->alignmentOf(pType);
|
2009-07-07 02:24:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Array element alignment (in bytes) for this type of data.
|
|
|
|
*/
|
|
|
|
virtual size_t sizeOf(Type* pType){
|
|
|
|
return mpBase->sizeOf(pType);
|
|
|
|
}
|
2009-07-08 22:04:41 +02:00
|
|
|
|
|
|
|
virtual Type* getR0Type() {
|
|
|
|
return mpBase->getR0Type();
|
|
|
|
}
|
2009-07-29 19:28:18 +02:00
|
|
|
|
|
|
|
virtual ExpressionType getR0ExpressionType() {
|
|
|
|
return mpBase->getR0ExpressionType();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void setR0ExpressionType(ExpressionType et) {
|
|
|
|
mpBase->setR0ExpressionType(et);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual size_t getExpressionStackDepth() {
|
|
|
|
return mpBase->getExpressionStackDepth();
|
|
|
|
}
|
2009-07-31 04:06:34 +02:00
|
|
|
|
|
|
|
virtual void forceR0RVal() {
|
|
|
|
return mpBase->forceR0RVal();
|
|
|
|
}
|
2009-06-12 06:12:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // PROVIDE_TRACE_CODEGEN
|
|
|
|
|
2009-06-29 23:29:08 +02:00
|
|
|
class Arena {
|
|
|
|
public:
|
|
|
|
// Used to record a given allocation amount.
|
|
|
|
// Used:
|
|
|
|
// Mark mark = arena.mark();
|
|
|
|
// ... lots of arena.allocate()
|
|
|
|
// arena.free(mark);
|
|
|
|
|
|
|
|
struct Mark {
|
|
|
|
size_t chunk;
|
|
|
|
size_t offset;
|
|
|
|
};
|
|
|
|
|
|
|
|
Arena() {
|
|
|
|
mCurrentChunk = 0;
|
|
|
|
Chunk start(CHUNK_SIZE);
|
|
|
|
mData.push_back(start);
|
|
|
|
}
|
|
|
|
|
|
|
|
~Arena() {
|
|
|
|
for(size_t i = 0; i < mData.size(); i++) {
|
|
|
|
mData[i].free();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Alloc using the standard alignment size safe for any variable
|
|
|
|
void* alloc(size_t size) {
|
|
|
|
return alloc(size, 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
Mark mark(){
|
|
|
|
Mark result;
|
|
|
|
result.chunk = mCurrentChunk;
|
|
|
|
result.offset = mData[mCurrentChunk].mOffset;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void freeToMark(const Mark& mark) {
|
|
|
|
mCurrentChunk = mark.chunk;
|
|
|
|
mData[mCurrentChunk].mOffset = mark.offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Allocate memory aligned to a given size
|
|
|
|
// and a given power-of-two-sized alignment (e.g. 1,2,4,8,...)
|
|
|
|
// Memory is not zero filled.
|
|
|
|
|
|
|
|
void* alloc(size_t size, size_t alignment) {
|
|
|
|
while (size > mData[mCurrentChunk].remainingCapacity(alignment)) {
|
|
|
|
if (mCurrentChunk + 1 < mData.size()) {
|
|
|
|
mCurrentChunk++;
|
|
|
|
} else {
|
|
|
|
size_t allocSize = CHUNK_SIZE;
|
|
|
|
if (allocSize < size + alignment - 1) {
|
|
|
|
allocSize = size + alignment - 1;
|
|
|
|
}
|
|
|
|
Chunk chunk(allocSize);
|
|
|
|
mData.push_back(chunk);
|
|
|
|
mCurrentChunk++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mData[mCurrentChunk].allocate(size, alignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const size_t CHUNK_SIZE = 128*1024;
|
|
|
|
// Note: this class does not deallocate its
|
|
|
|
// memory when it's destroyed. It depends upon
|
|
|
|
// its parent to deallocate the memory.
|
|
|
|
struct Chunk {
|
|
|
|
Chunk() {
|
|
|
|
mpData = 0;
|
|
|
|
mSize = 0;
|
|
|
|
mOffset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Chunk(size_t size) {
|
|
|
|
mSize = size;
|
|
|
|
mpData = (char*) malloc(size);
|
|
|
|
mOffset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
~Chunk() {
|
|
|
|
// Doesn't deallocate memory.
|
|
|
|
}
|
|
|
|
|
|
|
|
void* allocate(size_t size, size_t alignment) {
|
|
|
|
size_t alignedOffset = aligned(mOffset, alignment);
|
|
|
|
void* result = mpData + alignedOffset;
|
|
|
|
mOffset = alignedOffset + size;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void free() {
|
|
|
|
if (mpData) {
|
|
|
|
::free(mpData);
|
|
|
|
mpData = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t remainingCapacity(size_t alignment) {
|
|
|
|
return aligned(mSize, alignment) - aligned(mOffset, alignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assume alignment is a power of two
|
|
|
|
inline size_t aligned(size_t v, size_t alignment) {
|
|
|
|
size_t mask = alignment-1;
|
|
|
|
return (v + mask) & ~mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* mpData;
|
|
|
|
size_t mSize;
|
|
|
|
size_t mOffset;
|
|
|
|
};
|
|
|
|
|
|
|
|
size_t mCurrentChunk;
|
|
|
|
|
|
|
|
Vector<Chunk> mData;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct VariableInfo;
|
|
|
|
|
|
|
|
struct Token {
|
|
|
|
int hash;
|
|
|
|
size_t length;
|
|
|
|
char* pText;
|
|
|
|
tokenid_t id;
|
|
|
|
|
|
|
|
// Current values for the token
|
|
|
|
char* mpMacroDefinition;
|
|
|
|
VariableInfo* mpVariableInfo;
|
2009-08-27 01:15:07 +02:00
|
|
|
VariableInfo* mpStructInfo;
|
2009-06-29 23:29:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class TokenTable {
|
|
|
|
public:
|
|
|
|
// Don't use 0..0xff, allows characters and operators to be tokens too.
|
|
|
|
|
|
|
|
static const int TOKEN_BASE = 0x100;
|
|
|
|
TokenTable() {
|
|
|
|
mpMap = hashmapCreate(128, hashFn, equalsFn);
|
|
|
|
}
|
|
|
|
|
|
|
|
~TokenTable() {
|
|
|
|
hashmapFree(mpMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setArena(Arena* pArena) {
|
|
|
|
mpArena = pArena;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a token for a given string of characters.
|
|
|
|
tokenid_t intern(const char* pText, size_t length) {
|
|
|
|
Token probe;
|
|
|
|
int hash = hashmapHash((void*) pText, length);
|
|
|
|
{
|
|
|
|
Token probe;
|
|
|
|
probe.hash = hash;
|
|
|
|
probe.length = length;
|
|
|
|
probe.pText = (char*) pText;
|
|
|
|
Token* pValue = (Token*) hashmapGet(mpMap, &probe);
|
|
|
|
if (pValue) {
|
|
|
|
return pValue->id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Token* pToken = (Token*) mpArena->alloc(sizeof(Token));
|
|
|
|
memset(pToken, 0, sizeof(*pToken));
|
|
|
|
pToken->hash = hash;
|
|
|
|
pToken->length = length;
|
|
|
|
pToken->pText = (char*) mpArena->alloc(length + 1);
|
|
|
|
memcpy(pToken->pText, pText, length);
|
|
|
|
pToken->pText[length] = 0;
|
|
|
|
pToken->id = mTokens.size() + TOKEN_BASE;
|
|
|
|
mTokens.push_back(pToken);
|
|
|
|
hashmapPut(mpMap, pToken, pToken);
|
|
|
|
return pToken->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the Token for a given tokenid.
|
|
|
|
Token& operator[](tokenid_t id) {
|
|
|
|
return *mTokens[id - TOKEN_BASE];
|
|
|
|
}
|
|
|
|
|
|
|
|
inline size_t size() {
|
|
|
|
return mTokens.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
static int hashFn(void* pKey) {
|
|
|
|
Token* pToken = (Token*) pKey;
|
|
|
|
return pToken->hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool equalsFn(void* keyA, void* keyB) {
|
|
|
|
Token* pTokenA = (Token*) keyA;
|
|
|
|
Token* pTokenB = (Token*) keyB;
|
|
|
|
// Don't need to compare hash values, they should always be equal
|
|
|
|
return pTokenA->length == pTokenB->length
|
|
|
|
&& strcmp(pTokenA->pText, pTokenB->pText) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Hashmap* mpMap;
|
|
|
|
Vector<Token*> mTokens;
|
|
|
|
Arena* mpArena;
|
|
|
|
};
|
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
class InputStream {
|
|
|
|
public:
|
2009-07-09 01:59:18 +02:00
|
|
|
virtual ~InputStream() {}
|
2009-07-17 01:50:56 +02:00
|
|
|
virtual int getChar() = 0;
|
2009-05-22 21:06:27 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
|
2009-07-17 01:50:56 +02:00
|
|
|
virtual int getChar() {
|
2009-05-22 21:06:27 +02:00
|
|
|
return mPosition < mTextLength ? pText[mPosition++] : EOF;
|
|
|
|
}
|
|
|
|
|
2009-07-17 01:50:56 +02:00
|
|
|
private:
|
2009-05-22 21:06:27 +02:00
|
|
|
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-07-01 03:09:56 +02:00
|
|
|
void append(String& other) {
|
|
|
|
appendBytes(other.getUnwrapped(), other.len());
|
|
|
|
}
|
|
|
|
|
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-29 23:29:08 +02:00
|
|
|
void internKeywords() {
|
|
|
|
// Note: order has to match TOK_ constants
|
|
|
|
static const char* keywords[] = {
|
|
|
|
"int",
|
|
|
|
"char",
|
|
|
|
"void",
|
|
|
|
"if",
|
|
|
|
"else",
|
|
|
|
"while",
|
|
|
|
"break",
|
|
|
|
"return",
|
|
|
|
"for",
|
|
|
|
"auto",
|
|
|
|
"case",
|
|
|
|
"const",
|
|
|
|
"continue",
|
|
|
|
"default",
|
|
|
|
"do",
|
|
|
|
"double",
|
|
|
|
"enum",
|
|
|
|
"extern",
|
|
|
|
"float",
|
|
|
|
"goto",
|
|
|
|
"long",
|
|
|
|
"register",
|
|
|
|
"short",
|
|
|
|
"signed",
|
|
|
|
"sizeof",
|
|
|
|
"static",
|
|
|
|
"struct",
|
|
|
|
"switch",
|
|
|
|
"typedef",
|
|
|
|
"union",
|
|
|
|
"unsigned",
|
|
|
|
"volatile",
|
|
|
|
"_Bool",
|
|
|
|
"_Complex",
|
|
|
|
"_Imaginary",
|
|
|
|
"inline",
|
|
|
|
"restrict",
|
2009-07-17 01:50:56 +02:00
|
|
|
|
|
|
|
// predefined tokens that can also be symbols start here:
|
|
|
|
"pragma",
|
|
|
|
"define",
|
|
|
|
"line",
|
2009-06-29 23:29:08 +02:00
|
|
|
0};
|
|
|
|
|
|
|
|
for(int i = 0; keywords[i]; i++) {
|
|
|
|
mTokenTable.intern(keywords[i], strlen(keywords[i]));
|
2009-06-08 23:34:26 +02:00
|
|
|
}
|
2009-06-29 23:29:08 +02:00
|
|
|
}
|
2009-06-08 23:34:26 +02:00
|
|
|
|
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
|
|
|
void* pAddress;
|
|
|
|
void* pForward; // For a forward direction, linked list of data to fix up
|
2009-06-29 23:29:08 +02:00
|
|
|
tokenid_t tok;
|
|
|
|
size_t level;
|
|
|
|
VariableInfo* pOldDefinition;
|
2009-07-01 03:09:56 +02:00
|
|
|
Type* pType;
|
2009-08-27 01:15:07 +02:00
|
|
|
bool isStructTag;
|
2009-06-11 23:29:47 +02:00
|
|
|
};
|
|
|
|
|
2009-06-12 04:06:24 +02:00
|
|
|
class SymbolStack {
|
|
|
|
public:
|
|
|
|
SymbolStack() {
|
2009-06-29 23:29:08 +02:00
|
|
|
mpArena = 0;
|
|
|
|
mpTokenTable = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setArena(Arena* pArena) {
|
|
|
|
mpArena = pArena;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setTokenTable(TokenTable* pTokenTable) {
|
|
|
|
mpTokenTable = pTokenTable;
|
2009-06-12 04:06:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void pushLevel() {
|
2009-06-29 23:29:08 +02:00
|
|
|
Mark mark;
|
|
|
|
mark.mArenaMark = mpArena->mark();
|
|
|
|
mark.mSymbolHead = mStack.size();
|
|
|
|
mLevelStack.push_back(mark);
|
2009-06-12 04:06:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void popLevel() {
|
2009-06-29 23:29:08 +02:00
|
|
|
// Undo any shadowing that was done:
|
|
|
|
Mark mark = mLevelStack.back();
|
|
|
|
mLevelStack.pop_back();
|
|
|
|
while (mStack.size() > mark.mSymbolHead) {
|
|
|
|
VariableInfo* pV = mStack.back();
|
|
|
|
mStack.pop_back();
|
2009-08-27 01:15:07 +02:00
|
|
|
if (pV->isStructTag) {
|
|
|
|
(*mpTokenTable)[pV->tok].mpStructInfo = pV->pOldDefinition;
|
|
|
|
} else {
|
|
|
|
(*mpTokenTable)[pV->tok].mpVariableInfo = pV->pOldDefinition;
|
|
|
|
}
|
2009-06-29 23:29:08 +02:00
|
|
|
}
|
|
|
|
mpArena->freeToMark(mark.mArenaMark);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isDefinedAtCurrentLevel(tokenid_t tok) {
|
|
|
|
VariableInfo* pV = (*mpTokenTable)[tok].mpVariableInfo;
|
|
|
|
return pV && pV->level == level();
|
|
|
|
}
|
|
|
|
|
2009-08-27 01:15:07 +02:00
|
|
|
bool isStructTagDefinedAtCurrentLevel(tokenid_t tok) {
|
|
|
|
VariableInfo* pV = (*mpTokenTable)[tok].mpStructInfo;
|
|
|
|
return pV && pV->level == level();
|
|
|
|
}
|
|
|
|
|
2009-06-29 23:29:08 +02:00
|
|
|
VariableInfo* add(tokenid_t tok) {
|
|
|
|
Token& token = (*mpTokenTable)[tok];
|
|
|
|
VariableInfo* pOldV = token.mpVariableInfo;
|
|
|
|
VariableInfo* pNewV =
|
|
|
|
(VariableInfo*) mpArena->alloc(sizeof(VariableInfo));
|
|
|
|
memset(pNewV, 0, sizeof(VariableInfo));
|
|
|
|
pNewV->tok = tok;
|
|
|
|
pNewV->level = level();
|
|
|
|
pNewV->pOldDefinition = pOldV;
|
|
|
|
token.mpVariableInfo = pNewV;
|
|
|
|
mStack.push_back(pNewV);
|
|
|
|
return pNewV;
|
|
|
|
}
|
|
|
|
|
2009-08-27 01:15:07 +02:00
|
|
|
VariableInfo* addStructTag(tokenid_t tok) {
|
|
|
|
Token& token = (*mpTokenTable)[tok];
|
|
|
|
VariableInfo* pOldS = token.mpStructInfo;
|
|
|
|
VariableInfo* pNewS =
|
|
|
|
(VariableInfo*) mpArena->alloc(sizeof(VariableInfo));
|
|
|
|
memset(pNewS, 0, sizeof(VariableInfo));
|
|
|
|
pNewS->tok = tok;
|
|
|
|
pNewS->level = level();
|
|
|
|
pNewS->isStructTag = true;
|
|
|
|
pNewS->pOldDefinition = pOldS;
|
|
|
|
token.mpStructInfo = pNewS;
|
|
|
|
mStack.push_back(pNewS);
|
|
|
|
return pNewS;
|
|
|
|
}
|
|
|
|
|
2009-07-01 03:09:56 +02:00
|
|
|
VariableInfo* add(Type* pType) {
|
|
|
|
VariableInfo* pVI = add(pType->id);
|
|
|
|
pVI->pType = pType;
|
|
|
|
return pVI;
|
|
|
|
}
|
|
|
|
|
2009-06-29 23:29:08 +02:00
|
|
|
void forEach(bool (*fn)(VariableInfo*, void*), void* context) {
|
|
|
|
for (size_t i = 0; i < mStack.size(); i++) {
|
|
|
|
if (! fn(mStack[i], context)) {
|
2009-06-12 04:06:24 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-06-12 20:25:59 +02:00
|
|
|
}
|
|
|
|
|
2009-06-12 04:06:24 +02:00
|
|
|
private:
|
2009-06-29 23:29:08 +02:00
|
|
|
inline size_t level() {
|
|
|
|
return mLevelStack.size();
|
2009-06-12 04:06:24 +02:00
|
|
|
}
|
|
|
|
|
2009-06-29 23:29:08 +02:00
|
|
|
struct Mark {
|
|
|
|
Arena::Mark mArenaMark;
|
|
|
|
size_t mSymbolHead;
|
2009-06-12 04:06:24 +02:00
|
|
|
};
|
|
|
|
|
2009-06-29 23:29:08 +02:00
|
|
|
Arena* mpArena;
|
|
|
|
TokenTable* mpTokenTable;
|
|
|
|
Vector<VariableInfo*> mStack;
|
|
|
|
Vector<Mark> mLevelStack;
|
2009-06-12 04:06:24 +02:00
|
|
|
};
|
2009-06-09 00:55:32 +02:00
|
|
|
|
2009-10-28 01:23:20 +01:00
|
|
|
struct MacroState {
|
|
|
|
tokenid_t name; // Name of the current macro we are expanding
|
|
|
|
char* dptr; // point to macro text during macro playback
|
|
|
|
int dch; // Saves old value of ch during a macro playback
|
|
|
|
};
|
|
|
|
|
|
|
|
#define MACRO_NESTING_MAX 32
|
|
|
|
MacroState macroState[MACRO_NESTING_MAX];
|
|
|
|
int macroLevel; // -1 means not playing any macro.
|
|
|
|
|
2009-06-09 00:55:32 +02:00
|
|
|
int ch; // Current input character, or EOF
|
2009-06-29 23:29:08 +02:00
|
|
|
tokenid_t tok; // token
|
2009-06-09 00:55:32 +02:00
|
|
|
intptr_t tokc; // token extra info
|
2009-07-07 03:33:20 +02:00
|
|
|
double tokd; // floating point constant value
|
2009-06-09 00:55:32 +02:00
|
|
|
int tokl; // token operator level
|
|
|
|
intptr_t rsym; // return symbol
|
2009-07-07 23:48:51 +02:00
|
|
|
Type* pReturnType; // type of the current function's return.
|
2009-06-09 00:55:32 +02:00
|
|
|
intptr_t loc; // local variable index
|
|
|
|
char* glo; // global variable index
|
2009-06-12 04:06:24 +02:00
|
|
|
String mTokenString;
|
2009-08-19 03:25:56 +02:00
|
|
|
bool mbSuppressMacroExpansion;
|
2009-06-09 00:55:32 +02:00
|
|
|
char* pGlobalBase;
|
2009-07-15 06:14:10 +02:00
|
|
|
ACCSymbolLookupFn mpSymbolLookupFn;
|
|
|
|
void* mpSymbolLookupContext;
|
2009-06-29 23:29:08 +02:00
|
|
|
|
|
|
|
// Arena for the duration of the compile
|
|
|
|
Arena mGlobalArena;
|
|
|
|
// Arena for data that's only needed when compiling a single function
|
|
|
|
Arena mLocalArena;
|
|
|
|
|
2009-07-24 00:11:22 +02:00
|
|
|
Arena* mpCurrentArena;
|
|
|
|
|
2009-06-29 23:29:08 +02:00
|
|
|
TokenTable mTokenTable;
|
|
|
|
SymbolStack mGlobals;
|
|
|
|
SymbolStack mLocals;
|
|
|
|
|
2009-08-27 01:15:07 +02:00
|
|
|
SymbolStack* mpCurrentSymbolStack;
|
|
|
|
|
2009-07-02 00:32:35 +02:00
|
|
|
// Prebuilt types, makes things slightly faster.
|
2009-07-07 02:24:34 +02:00
|
|
|
Type* mkpInt; // int
|
2009-08-03 23:42:57 +02:00
|
|
|
Type* mkpShort; // short
|
2009-07-07 02:24:34 +02:00
|
|
|
Type* mkpChar; // char
|
|
|
|
Type* mkpVoid; // void
|
2009-07-06 21:07:15 +02:00
|
|
|
Type* mkpFloat;
|
|
|
|
Type* mkpDouble;
|
2009-07-07 23:48:51 +02:00
|
|
|
Type* mkpIntFn;
|
2009-07-02 23:46:19 +02:00
|
|
|
Type* mkpIntPtr;
|
|
|
|
Type* mkpCharPtr;
|
2009-07-08 22:04:41 +02:00
|
|
|
Type* mkpFloatPtr;
|
|
|
|
Type* mkpDoublePtr;
|
2009-07-02 23:46:19 +02:00
|
|
|
Type* mkpPtrIntFn;
|
2009-07-01 03:09:56 +02:00
|
|
|
|
2009-06-09 00:55:32 +02:00
|
|
|
InputStream* file;
|
2009-07-17 01:50:56 +02:00
|
|
|
int mLineNumber;
|
|
|
|
bool mbBumpLine;
|
2009-06-09 00:55:32 +02:00
|
|
|
|
2009-09-10 04:08:54 +02:00
|
|
|
ICodeBuf* pCodeBuf;
|
2009-06-09 00:55:32 +02:00
|
|
|
CodeGenerator* pGen;
|
|
|
|
|
2009-06-05 01:23:40 +02:00
|
|
|
String mErrorBuf;
|
|
|
|
|
|
|
|
String mPragmas;
|
|
|
|
int mPragmaStringCount;
|
2009-07-16 23:30:33 +02:00
|
|
|
int mCompileResult;
|
2009-06-05 01:23:40 +02:00
|
|
|
|
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;
|
2009-07-07 03:33:20 +02:00
|
|
|
static const int TOK_NUM_FLOAT = 3;
|
|
|
|
static const int TOK_NUM_DOUBLE = 4;
|
2009-07-31 21:00:39 +02:00
|
|
|
static const int TOK_OP_ASSIGNMENT = 5;
|
2009-08-27 01:15:07 +02:00
|
|
|
static const int TOK_OP_ARROW = 6;
|
2009-06-12 04:06:24 +02:00
|
|
|
|
|
|
|
// 3..255 are character and/or operators
|
|
|
|
|
2009-06-11 23:29:47 +02:00
|
|
|
// Keywords start at 0x100 and increase by 1
|
2009-06-29 23:29:08 +02:00
|
|
|
// Order has to match string list in "internKeywords".
|
|
|
|
enum {
|
|
|
|
TOK_KEYWORD = TokenTable::TOKEN_BASE,
|
|
|
|
TOK_INT = TOK_KEYWORD,
|
|
|
|
TOK_CHAR,
|
|
|
|
TOK_VOID,
|
|
|
|
TOK_IF,
|
|
|
|
TOK_ELSE,
|
|
|
|
TOK_WHILE,
|
|
|
|
TOK_BREAK,
|
|
|
|
TOK_RETURN,
|
|
|
|
TOK_FOR,
|
|
|
|
TOK_AUTO,
|
|
|
|
TOK_CASE,
|
|
|
|
TOK_CONST,
|
|
|
|
TOK_CONTINUE,
|
|
|
|
TOK_DEFAULT,
|
|
|
|
TOK_DO,
|
|
|
|
TOK_DOUBLE,
|
|
|
|
TOK_ENUM,
|
|
|
|
TOK_EXTERN,
|
|
|
|
TOK_FLOAT,
|
|
|
|
TOK_GOTO,
|
|
|
|
TOK_LONG,
|
|
|
|
TOK_REGISTER,
|
|
|
|
TOK_SHORT,
|
|
|
|
TOK_SIGNED,
|
|
|
|
TOK_SIZEOF,
|
|
|
|
TOK_STATIC,
|
|
|
|
TOK_STRUCT,
|
|
|
|
TOK_SWITCH,
|
|
|
|
TOK_TYPEDEF,
|
|
|
|
TOK_UNION,
|
|
|
|
TOK_UNSIGNED,
|
|
|
|
TOK_VOLATILE,
|
|
|
|
TOK__BOOL,
|
|
|
|
TOK__COMPLEX,
|
|
|
|
TOK__IMAGINARY,
|
|
|
|
TOK_INLINE,
|
|
|
|
TOK_RESTRICT,
|
2009-07-17 01:50:56 +02:00
|
|
|
|
|
|
|
// Symbols start after keywords
|
|
|
|
|
|
|
|
TOK_SYMBOL,
|
|
|
|
TOK_PRAGMA = TOK_SYMBOL,
|
|
|
|
TOK_DEFINE,
|
|
|
|
TOK_LINE
|
2009-06-29 23:29:08 +02:00
|
|
|
};
|
2009-05-11 23:49:29 +02:00
|
|
|
|
|
|
|
static const int LOCAL = 0x200;
|
|
|
|
|
|
|
|
/* 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-06-29 23:29:08 +02:00
|
|
|
/* Called when we detect an internal problem. Does nothing in production.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void internalError() {
|
|
|
|
* (char*) 0 = 0;
|
|
|
|
}
|
|
|
|
|
2009-08-18 01:54:56 +02:00
|
|
|
void assertImpl(bool isTrue, int line) {
|
2009-07-01 03:09:56 +02:00
|
|
|
if (!isTrue) {
|
2009-08-28 18:26:31 +02:00
|
|
|
LOGD("%d: assertion failed at line %s:%d.", mLineNumber, __FILE__, line);
|
2009-07-01 03:09:56 +02:00
|
|
|
internalError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-02 00:32:35 +02:00
|
|
|
bool isSymbol(tokenid_t t) {
|
|
|
|
return t >= TOK_SYMBOL &&
|
|
|
|
((size_t) (t-TOK_SYMBOL)) < mTokenTable.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isSymbolOrKeyword(tokenid_t t) {
|
|
|
|
return t >= TOK_KEYWORD &&
|
2009-07-06 21:07:15 +02:00
|
|
|
((size_t) (t-TOK_KEYWORD)) < mTokenTable.size();
|
2009-07-02 00:32:35 +02:00
|
|
|
}
|
|
|
|
|
2009-06-29 23:29:08 +02:00
|
|
|
VariableInfo* VI(tokenid_t t) {
|
2009-07-02 00:32:35 +02:00
|
|
|
assert(isSymbol(t));
|
2009-06-29 23:29:08 +02:00
|
|
|
VariableInfo* pV = mTokenTable[t].mpVariableInfo;
|
|
|
|
if (pV && pV->tok != t) {
|
|
|
|
internalError();
|
|
|
|
}
|
|
|
|
return pV;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool isDefined(tokenid_t t) {
|
|
|
|
return t >= TOK_SYMBOL && VI(t) != 0;
|
|
|
|
}
|
|
|
|
|
2009-07-02 00:32:35 +02:00
|
|
|
const char* nameof(tokenid_t t) {
|
|
|
|
assert(isSymbolOrKeyword(t));
|
2009-06-29 23:29:08 +02:00
|
|
|
return mTokenTable[t].pText;
|
|
|
|
}
|
|
|
|
|
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() {
|
2009-10-28 01:23:20 +01:00
|
|
|
// Close any totally empty macros. We leave them on the stack until now
|
|
|
|
// so that we know which macros are being expanded when checking if the
|
|
|
|
// last token in the macro is a macro that's already being expanded.
|
|
|
|
while (macroLevel >= 0 && macroState[macroLevel].dptr == NULL) {
|
|
|
|
macroLevel--;
|
|
|
|
}
|
|
|
|
if (macroLevel >= 0) {
|
|
|
|
ch = *macroState[macroLevel].dptr++;
|
2009-06-08 23:34:26 +02:00
|
|
|
if (ch == 0) {
|
2009-10-28 01:23:20 +01:00
|
|
|
ch = macroState[macroLevel].dch;
|
|
|
|
macroState[macroLevel].dptr = NULL; // This macro's done
|
2009-05-11 23:49:29 +02:00
|
|
|
}
|
2009-07-17 01:50:56 +02:00
|
|
|
} else {
|
|
|
|
if (mbBumpLine) {
|
|
|
|
mLineNumber++;
|
|
|
|
mbBumpLine = false;
|
|
|
|
}
|
2009-06-05 01:23:40 +02:00
|
|
|
ch = file->getChar();
|
2009-07-17 01:50:56 +02:00
|
|
|
if (ch == '\n') {
|
|
|
|
mbBumpLine = true;
|
|
|
|
}
|
|
|
|
}
|
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-07-16 01:16:37 +02:00
|
|
|
int decodeHex(int c) {
|
|
|
|
if (isdigit(c)) {
|
|
|
|
c -= '0';
|
|
|
|
} else if (c <= 'F') {
|
|
|
|
c = c - 'A' + 10;
|
|
|
|
} else {
|
|
|
|
c =c - 'a' + 10;
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
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)) {
|
2009-07-16 01:16:37 +02:00
|
|
|
val = (val << 4) + decodeHex(ch);
|
2009-06-12 21:49:14 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2009-07-07 03:33:20 +02:00
|
|
|
bool acceptCh(int c) {
|
|
|
|
bool result = c == ch;
|
|
|
|
if (result) {
|
|
|
|
pdef(ch);
|
|
|
|
inp();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool acceptDigitsCh() {
|
|
|
|
bool result = false;
|
|
|
|
while (isdigit(ch)) {
|
|
|
|
result = true;
|
|
|
|
pdef(ch);
|
|
|
|
inp();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void parseFloat() {
|
|
|
|
tok = TOK_NUM_DOUBLE;
|
|
|
|
// mTokenString already has the integral part of the number.
|
2009-07-16 01:16:37 +02:00
|
|
|
if(mTokenString.len() == 0) {
|
|
|
|
mTokenString.append('0');
|
|
|
|
}
|
2009-07-07 03:33:20 +02:00
|
|
|
acceptCh('.');
|
|
|
|
acceptDigitsCh();
|
|
|
|
if (acceptCh('e') || acceptCh('E')) {
|
2009-07-16 01:16:37 +02:00
|
|
|
acceptCh('-') || acceptCh('+');
|
|
|
|
acceptDigitsCh();
|
2009-07-07 03:33:20 +02:00
|
|
|
}
|
2009-07-16 01:16:37 +02:00
|
|
|
if (ch == 'f' || ch == 'F') {
|
|
|
|
tok = TOK_NUM_FLOAT;
|
|
|
|
inp();
|
|
|
|
} else if (ch == 'l' || ch == 'L') {
|
|
|
|
inp();
|
|
|
|
error("Long floating point constants not supported.");
|
2009-07-07 03:33:20 +02:00
|
|
|
}
|
|
|
|
char* pText = mTokenString.getUnwrapped();
|
2009-07-16 01:16:37 +02:00
|
|
|
char* pEnd = pText + strlen(pText);
|
|
|
|
char* pEndPtr = 0;
|
|
|
|
errno = 0;
|
2009-07-07 03:33:20 +02:00
|
|
|
if (tok == TOK_NUM_FLOAT) {
|
2009-07-16 01:16:37 +02:00
|
|
|
tokd = strtof(pText, &pEndPtr);
|
2009-07-07 03:33:20 +02:00
|
|
|
} else {
|
2009-07-16 01:16:37 +02:00
|
|
|
tokd = strtod(pText, &pEndPtr);
|
|
|
|
}
|
|
|
|
if (errno || pEndPtr != pEnd) {
|
|
|
|
error("Can't parse constant: %s", pText);
|
2009-07-07 03:33:20 +02:00
|
|
|
}
|
2009-07-16 01:16:37 +02:00
|
|
|
// fprintf(stderr, "float constant: %s (%d) %g\n", pText, tok, tokd);
|
2009-07-07 03:33:20 +02:00
|
|
|
}
|
|
|
|
|
2009-10-28 01:23:20 +01:00
|
|
|
bool currentlyBeingExpanded(tokenid_t id) {
|
|
|
|
for (int i = 0; i <= macroLevel; i++) {
|
|
|
|
if (macroState[macroLevel].name == id) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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();
|
2009-07-17 01:50:56 +02:00
|
|
|
} else if (tok == TOK_LINE) {
|
|
|
|
doLine();
|
2009-06-05 01:23:40 +02:00
|
|
|
} 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 */
|
2009-07-16 01:16:37 +02:00
|
|
|
if (isdigit(ch) || ch == '.') {
|
|
|
|
// Start of a numeric constant. Could be integer, float, or
|
|
|
|
// double, won't know until we look further.
|
2009-06-12 04:06:24 +02:00
|
|
|
mTokenString.clear();
|
2009-07-16 01:16:37 +02:00
|
|
|
pdef(ch);
|
|
|
|
inp();
|
2009-08-27 01:15:07 +02:00
|
|
|
if (tok == '.' && !isdigit(ch)) {
|
|
|
|
goto done;
|
|
|
|
}
|
2009-07-16 01:16:37 +02:00
|
|
|
int base = 10;
|
|
|
|
if (tok == '0') {
|
|
|
|
if (ch == 'x' || ch == 'X') {
|
|
|
|
base = 16;
|
|
|
|
tok = TOK_NUM;
|
|
|
|
tokc = 0;
|
|
|
|
inp();
|
|
|
|
while ( isxdigit(ch) ) {
|
|
|
|
tokc = (tokc << 4) + decodeHex(ch);
|
|
|
|
inp();
|
|
|
|
}
|
|
|
|
} else if (isoctal(ch)){
|
|
|
|
base = 8;
|
|
|
|
tok = TOK_NUM;
|
|
|
|
tokc = 0;
|
|
|
|
while ( isoctal(ch) ) {
|
|
|
|
tokc = (tokc << 3) + (ch - '0');
|
|
|
|
inp();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (isdigit(tok)){
|
|
|
|
acceptDigitsCh();
|
2009-05-08 23:54:15 +02:00
|
|
|
}
|
2009-07-16 01:16:37 +02:00
|
|
|
if (base == 10) {
|
|
|
|
if (tok == '.' || ch == '.' || ch == 'e' || ch == 'E') {
|
2009-07-07 03:33:20 +02:00
|
|
|
parseFloat();
|
|
|
|
} else {
|
|
|
|
// It's an integer constant
|
2009-07-16 01:16:37 +02:00
|
|
|
char* pText = mTokenString.getUnwrapped();
|
|
|
|
char* pEnd = pText + strlen(pText);
|
|
|
|
char* pEndPtr = 0;
|
|
|
|
errno = 0;
|
|
|
|
tokc = strtol(pText, &pEndPtr, base);
|
|
|
|
if (errno || pEndPtr != pEnd) {
|
|
|
|
error("Can't parse constant: %s %d %d", pText, base, errno);
|
|
|
|
}
|
2009-07-07 03:33:20 +02:00
|
|
|
tok = TOK_NUM;
|
|
|
|
}
|
2009-07-16 01:16:37 +02:00
|
|
|
}
|
|
|
|
} else if (isid()) {
|
|
|
|
mTokenString.clear();
|
|
|
|
while (isid()) {
|
|
|
|
pdef(ch);
|
|
|
|
inp();
|
|
|
|
}
|
|
|
|
tok = mTokenTable.intern(mTokenString.getUnwrapped(), mTokenString.len());
|
2009-08-19 03:25:56 +02:00
|
|
|
if (! mbSuppressMacroExpansion) {
|
|
|
|
// Is this a macro?
|
|
|
|
char* pMacroDefinition = mTokenTable[tok].mpMacroDefinition;
|
2009-10-28 01:23:20 +01:00
|
|
|
if (pMacroDefinition && !currentlyBeingExpanded(tok)) {
|
2009-08-19 03:25:56 +02:00
|
|
|
// Yes, it is a macro
|
2009-10-28 01:23:20 +01:00
|
|
|
#if 0
|
|
|
|
printf("Expanding macro %s -> %s",
|
|
|
|
mTokenString.getUnwrapped(), pMacroDefinition);
|
|
|
|
#endif
|
|
|
|
if (macroLevel >= MACRO_NESTING_MAX-1) {
|
|
|
|
error("Too many levels of macro recursion.");
|
|
|
|
} else {
|
|
|
|
macroLevel++;
|
|
|
|
macroState[macroLevel].name = tok;
|
|
|
|
macroState[macroLevel].dptr = pMacroDefinition;
|
|
|
|
macroState[macroLevel].dch = ch;
|
|
|
|
inp();
|
|
|
|
next();
|
|
|
|
}
|
2009-08-19 03:25:56 +02:00
|
|
|
}
|
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-08-27 01:15:07 +02:00
|
|
|
} else if ((tok == '-') & (ch == '>')) {
|
|
|
|
inp();
|
|
|
|
tok = TOK_OP_ARROW;
|
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 */
|
|
|
|
}
|
2009-07-31 21:00:39 +02:00
|
|
|
/* check for op=, valid for * / % + - << >> & ^ | */
|
|
|
|
if (ch == '=' &&
|
|
|
|
((tokl >= 1 && tokl <= 3)
|
2009-08-01 00:25:53 +02:00
|
|
|
|| (tokl >=6 && tokl <= 8)) ) {
|
2009-07-31 21:00:39 +02:00
|
|
|
inp();
|
|
|
|
tok = TOK_OP_ASSIGNMENT;
|
|
|
|
}
|
2009-05-11 23:49:29 +02:00
|
|
|
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-08-27 01:15:07 +02:00
|
|
|
|
|
|
|
done: ;
|
2009-05-10 23:09:03 +02:00
|
|
|
#if 0
|
2009-05-11 23:49:29 +02:00
|
|
|
{
|
2009-06-29 23:29:08 +02:00
|
|
|
String buf;
|
2009-07-15 03:35:36 +02:00
|
|
|
decodeToken(buf, tok, true);
|
2009-07-01 03:09:56 +02:00
|
|
|
fprintf(stderr, "%s\n", buf.getUnwrapped());
|
|
|
|
}
|
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() {
|
2009-08-19 03:25:56 +02:00
|
|
|
mbSuppressMacroExpansion = true;
|
2009-06-29 23:29:08 +02:00
|
|
|
next();
|
2009-08-19 03:25:56 +02:00
|
|
|
mbSuppressMacroExpansion = false;
|
2009-06-29 23:29:08 +02:00
|
|
|
tokenid_t name = tok;
|
2009-06-08 23:34:26 +02:00
|
|
|
if (ch == '(') {
|
|
|
|
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();
|
|
|
|
}
|
2009-06-29 23:29:08 +02:00
|
|
|
String value;
|
2009-08-19 02:44:12 +02:00
|
|
|
bool appendToValue = true;
|
2009-06-08 23:34:26 +02:00
|
|
|
while (ch != '\n' && ch != EOF) {
|
2009-08-19 02:44:12 +02:00
|
|
|
// Check for '//' comments.
|
|
|
|
if (appendToValue && ch == '/') {
|
|
|
|
inp();
|
|
|
|
if (ch == '/') {
|
|
|
|
appendToValue = false;
|
|
|
|
} else {
|
|
|
|
value.append('/');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (appendToValue && ch != EOF) {
|
|
|
|
value.append(ch);
|
|
|
|
}
|
2009-06-08 23:34:26 +02:00
|
|
|
inp();
|
|
|
|
}
|
2009-06-29 23:29:08 +02:00
|
|
|
char* pDefn = (char*)mGlobalArena.alloc(value.len() + 1);
|
|
|
|
memcpy(pDefn, value.getUnwrapped(), value.len());
|
|
|
|
pDefn[value.len()] = 0;
|
|
|
|
mTokenTable[name].mpMacroDefinition = pDefn;
|
2009-10-28 01:23:20 +01:00
|
|
|
#if 0
|
|
|
|
{
|
|
|
|
String buf;
|
|
|
|
decodeToken(buf, name, true);
|
|
|
|
fprintf(stderr, "define %s = \"%s\"\n", buf.getUnwrapped(), pDefn);
|
|
|
|
}
|
|
|
|
#endif
|
2009-06-08 23:34:26 +02:00
|
|
|
}
|
|
|
|
|
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-07-17 01:50:56 +02:00
|
|
|
void doLine() {
|
|
|
|
// # line number { "filename "}
|
|
|
|
next();
|
|
|
|
if (tok != TOK_NUM) {
|
|
|
|
error("Expected a line-number");
|
|
|
|
} else {
|
|
|
|
mLineNumber = tokc-1; // The end-of-line will increment it.
|
|
|
|
}
|
|
|
|
while(ch != EOF && ch != '\n') {
|
|
|
|
inp();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-29 22:53:44 +02:00
|
|
|
virtual void verror(const char* fmt, va_list ap) {
|
2009-07-17 01:50:56 +02:00
|
|
|
mErrorBuf.printf("%ld: ", mLineNumber);
|
2009-06-05 01:23:40 +02:00
|
|
|
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-09-19 01:26:05 +02:00
|
|
|
if (!accept(c)) {
|
2009-05-11 23:49:29 +02:00
|
|
|
error("'%c' expected", c);
|
|
|
|
}
|
2009-05-08 23:54:15 +02:00
|
|
|
}
|
|
|
|
|
2009-07-01 03:09:56 +02:00
|
|
|
bool accept(intptr_t c) {
|
|
|
|
if (tok == c) {
|
|
|
|
next();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-07-02 00:32:35 +02:00
|
|
|
bool acceptStringLiteral() {
|
|
|
|
if (tok == '"') {
|
2009-07-31 04:06:34 +02:00
|
|
|
pGen->leaR0((int) glo, mkpCharPtr, ET_RVALUE);
|
2009-07-02 00:32:35 +02:00
|
|
|
// This while loop merges multiple adjacent string constants.
|
|
|
|
while (tok == '"') {
|
|
|
|
while (ch != '"' && ch != EOF) {
|
2009-07-09 01:48:41 +02:00
|
|
|
*allocGlobalSpace(1,1) = getq();
|
2009-07-02 00:32:35 +02:00
|
|
|
}
|
|
|
|
if (ch != '"') {
|
|
|
|
error("Unterminated string constant.");
|
|
|
|
}
|
|
|
|
inp();
|
|
|
|
next();
|
2009-05-11 23:49:29 +02:00
|
|
|
}
|
2009-07-02 00:32:35 +02:00
|
|
|
/* Null terminate */
|
2009-05-29 02:15:32 +02:00
|
|
|
*glo = 0;
|
2009-05-30 03:03:15 +02:00
|
|
|
/* align heap */
|
2009-07-09 01:48:41 +02:00
|
|
|
allocGlobalSpace(1,(char*) (((intptr_t) glo + 4) & -4) - glo);
|
2009-07-02 00:32:35 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-15 06:14:10 +02:00
|
|
|
|
2009-07-17 00:09:20 +02:00
|
|
|
void linkGlobal(tokenid_t t, bool isFunction) {
|
|
|
|
VariableInfo* pVI = VI(t);
|
|
|
|
void* n = NULL;
|
|
|
|
if (mpSymbolLookupFn) {
|
|
|
|
n = mpSymbolLookupFn(mpSymbolLookupContext, nameof(t));
|
|
|
|
}
|
|
|
|
if (pVI->pType == NULL) {
|
|
|
|
if (isFunction) {
|
|
|
|
pVI->pType = mkpIntFn;
|
|
|
|
} else {
|
|
|
|
pVI->pType = mkpInt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pVI->pAddress = n;
|
|
|
|
}
|
|
|
|
|
2009-07-31 04:38:55 +02:00
|
|
|
void unaryOrAssignment() {
|
|
|
|
unary();
|
|
|
|
if (accept('=')) {
|
|
|
|
checkLVal();
|
|
|
|
pGen->pushR0();
|
|
|
|
expr();
|
|
|
|
pGen->forceR0RVal();
|
|
|
|
pGen->storeR0ToTOS();
|
2009-07-31 21:00:39 +02:00
|
|
|
} else if (tok == TOK_OP_ASSIGNMENT) {
|
|
|
|
int t = tokc;
|
|
|
|
next();
|
|
|
|
checkLVal();
|
|
|
|
pGen->pushR0();
|
|
|
|
pGen->forceR0RVal();
|
|
|
|
pGen->pushR0();
|
|
|
|
expr();
|
|
|
|
pGen->forceR0RVal();
|
|
|
|
pGen->genOp(t);
|
|
|
|
pGen->storeR0ToTOS();
|
2009-07-31 04:38:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-02 00:32:35 +02:00
|
|
|
/* Parse and evaluate a unary expression.
|
|
|
|
*/
|
2009-07-31 04:38:55 +02:00
|
|
|
void unary() {
|
2009-07-17 00:09:20 +02:00
|
|
|
tokenid_t t;
|
2009-07-31 23:55:07 +02:00
|
|
|
intptr_t a;
|
2009-07-02 00:32:35 +02:00
|
|
|
t = 0;
|
|
|
|
if (acceptStringLiteral()) {
|
|
|
|
// Nothing else to do.
|
2009-05-11 23:49:29 +02:00
|
|
|
} else {
|
2009-07-02 00:32:35 +02:00
|
|
|
int c = tokl;
|
2009-05-11 23:49:29 +02:00
|
|
|
a = tokc;
|
2009-07-07 03:33:20 +02:00
|
|
|
double ad = tokd;
|
2009-05-11 23:49:29 +02:00
|
|
|
t = tok;
|
|
|
|
next();
|
|
|
|
if (t == TOK_NUM) {
|
2009-07-18 01:35:23 +02:00
|
|
|
pGen->li(a);
|
2009-07-07 03:33:20 +02:00
|
|
|
} else if (t == TOK_NUM_FLOAT) {
|
2009-07-08 22:04:41 +02:00
|
|
|
// Align to 4-byte boundary
|
|
|
|
glo = (char*) (((intptr_t) glo + 3) & -4);
|
|
|
|
* (float*) glo = (float) ad;
|
|
|
|
pGen->loadFloat((int) glo, mkpFloat);
|
|
|
|
glo += 4;
|
2009-07-07 03:33:20 +02:00
|
|
|
} else if (t == TOK_NUM_DOUBLE) {
|
2009-07-08 22:04:41 +02:00
|
|
|
// Align to 8-byte boundary
|
|
|
|
glo = (char*) (((intptr_t) glo + 7) & -8);
|
|
|
|
* (double*) glo = ad;
|
|
|
|
pGen->loadFloat((int) glo, mkpDouble);
|
|
|
|
glo += 8;
|
2009-05-11 23:49:29 +02:00
|
|
|
} else if (c == 2) {
|
|
|
|
/* -, +, !, ~ */
|
2009-07-31 04:38:55 +02:00
|
|
|
unary();
|
2009-07-31 04:06:34 +02:00
|
|
|
pGen->forceR0RVal();
|
2009-05-11 23:49:29 +02:00
|
|
|
if (t == '!')
|
2009-07-18 01:35:23 +02:00
|
|
|
pGen->gUnaryCmp(a);
|
2009-07-09 05:40:31 +02:00
|
|
|
else if (t == '+') {
|
|
|
|
// ignore unary plus.
|
|
|
|
} else {
|
2009-07-07 02:24:34 +02:00
|
|
|
pGen->genUnaryOp(a);
|
2009-07-09 05:40:31 +02:00
|
|
|
}
|
2009-07-31 23:34:34 +02:00
|
|
|
} else if (c == 11) {
|
|
|
|
// pre increment / pre decrement
|
|
|
|
unary();
|
|
|
|
doIncDec(a == OP_INCREMENT, 0);
|
|
|
|
}
|
|
|
|
else if (t == '(') {
|
2009-07-14 00:57:26 +02:00
|
|
|
// It's either a cast or an expression
|
2009-07-24 00:11:22 +02:00
|
|
|
Type* pCast = acceptCastTypeDeclaration();
|
2009-07-14 00:57:26 +02:00
|
|
|
if (pCast) {
|
|
|
|
skip(')');
|
2009-07-31 04:38:55 +02:00
|
|
|
unary();
|
2009-07-31 04:06:34 +02:00
|
|
|
pGen->forceR0RVal();
|
2009-08-04 23:56:09 +02:00
|
|
|
pGen->castR0(pCast);
|
2009-07-02 23:46:19 +02:00
|
|
|
} else {
|
2009-07-31 23:01:37 +02:00
|
|
|
commaExpr();
|
2009-07-14 00:57:26 +02:00
|
|
|
skip(')');
|
2009-05-11 23:49:29 +02:00
|
|
|
}
|
2009-07-14 00:57:26 +02:00
|
|
|
} else if (t == '*') {
|
|
|
|
/* This is a pointer dereference.
|
|
|
|
*/
|
2009-07-31 04:38:55 +02:00
|
|
|
unary();
|
2009-08-01 00:25:53 +02:00
|
|
|
doPointer();
|
2009-05-11 23:49:29 +02:00
|
|
|
} else if (t == '&') {
|
2009-09-05 00:24:23 +02:00
|
|
|
unary();
|
|
|
|
doAddressOf();
|
2009-06-12 04:06:24 +02:00
|
|
|
} else if (t == EOF ) {
|
|
|
|
error("Unexpected EOF.");
|
2009-07-16 03:23:22 +02:00
|
|
|
} else if (t == ';') {
|
|
|
|
error("Unexpected ';'");
|
2009-07-02 00:32:35 +02:00
|
|
|
} else if (!checkSymbol(t)) {
|
2009-06-12 23:40:04 +02:00
|
|
|
// Don't have to do anything special here, the error
|
|
|
|
// message was printed by checkSymbol() above.
|
2009-05-11 23:49:29 +02:00
|
|
|
} else {
|
2009-06-29 23:29:08 +02:00
|
|
|
if (!isDefined(t)) {
|
|
|
|
mGlobals.add(t);
|
|
|
|
// printf("Adding new global function %s\n", nameof(t));
|
2009-06-12 04:06:24 +02:00
|
|
|
}
|
2009-07-07 23:48:51 +02:00
|
|
|
VariableInfo* pVI = VI(t);
|
2009-07-31 23:55:07 +02:00
|
|
|
int n = (intptr_t) pVI->pAddress;
|
2009-07-15 06:14:10 +02:00
|
|
|
/* forward reference: try our lookup function */
|
2009-05-14 20:38:49 +02:00
|
|
|
if (!n) {
|
2009-07-17 00:09:20 +02:00
|
|
|
linkGlobal(t, tok == '(');
|
|
|
|
n = (intptr_t) pVI->pAddress;
|
|
|
|
if (!n && tok != '(') {
|
2009-09-19 00:03:03 +02:00
|
|
|
error("Undeclared variable %s", nameof(t));
|
2009-07-15 06:14:10 +02:00
|
|
|
}
|
2009-05-14 20:38:49 +02:00
|
|
|
}
|
2009-07-31 04:38:55 +02:00
|
|
|
if (tok != '(') {
|
2009-07-31 23:55:07 +02:00
|
|
|
/* variable or function name */
|
2009-06-12 20:25:59 +02:00
|
|
|
if (!n) {
|
2009-07-17 00:09:20 +02:00
|
|
|
linkGlobal(t, false);
|
|
|
|
n = (intptr_t) pVI->pAddress;
|
|
|
|
if (!n) {
|
2009-09-19 00:03:03 +02:00
|
|
|
error("Undeclared variable %s", nameof(t));
|
2009-07-17 00:09:20 +02:00
|
|
|
}
|
2009-06-12 20:25:59 +02:00
|
|
|
}
|
2009-07-31 23:55:07 +02:00
|
|
|
}
|
|
|
|
// load a variable
|
2009-08-04 23:56:09 +02:00
|
|
|
Type* pVal;
|
|
|
|
ExpressionType et;
|
|
|
|
if (pVI->pType->tag == TY_ARRAY) {
|
|
|
|
pVal = pVI->pType;
|
|
|
|
et = ET_RVALUE;
|
|
|
|
} else {
|
|
|
|
pVal = createPtrType(pVI->pType);
|
|
|
|
et = ET_LVALUE;
|
|
|
|
}
|
2009-07-31 23:55:07 +02:00
|
|
|
if (n) {
|
2009-08-04 23:56:09 +02:00
|
|
|
int tag = pVal->pHead->tag;
|
|
|
|
if (tag == TY_FUNC) {
|
2009-07-31 23:55:07 +02:00
|
|
|
et = ET_RVALUE;
|
2009-05-11 23:49:29 +02:00
|
|
|
}
|
2009-07-31 23:55:07 +02:00
|
|
|
pGen->leaR0(n, pVal, et);
|
|
|
|
} else {
|
|
|
|
pVI->pForward = (void*) pGen->leaForward(
|
|
|
|
(int) pVI->pForward, pVal);
|
2009-05-08 23:54:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-05-10 23:09:03 +02:00
|
|
|
|
2009-07-31 23:55:07 +02:00
|
|
|
/* Now handle postfix operators */
|
|
|
|
for(;;) {
|
|
|
|
if (tokl == 11) {
|
|
|
|
// post inc / post dec
|
|
|
|
doIncDec(tokc == OP_INCREMENT, true);
|
|
|
|
next();
|
2009-08-01 00:25:53 +02:00
|
|
|
} else if (accept('[')) {
|
|
|
|
// Array reference
|
|
|
|
pGen->forceR0RVal();
|
|
|
|
pGen->pushR0();
|
|
|
|
commaExpr();
|
|
|
|
pGen->forceR0RVal();
|
|
|
|
pGen->genOp(OP_PLUS);
|
|
|
|
doPointer();
|
|
|
|
skip(']');
|
2009-08-27 01:15:07 +02:00
|
|
|
} else if (accept('.')) {
|
|
|
|
// struct element
|
|
|
|
pGen->forceR0RVal();
|
|
|
|
Type* pStruct = pGen->getR0Type();
|
|
|
|
if (pStruct->tag == TY_STRUCT) {
|
2009-09-05 00:34:21 +02:00
|
|
|
doStructMember(pStruct, true);
|
2009-08-27 01:15:07 +02:00
|
|
|
} else {
|
|
|
|
error("expected a struct value to the left of '.'");
|
|
|
|
}
|
|
|
|
} else if (accept(TOK_OP_ARROW)) {
|
|
|
|
pGen->forceR0RVal();
|
|
|
|
Type* pPtr = pGen->getR0Type();
|
|
|
|
if (pPtr->tag == TY_POINTER && pPtr->pHead->tag == TY_STRUCT) {
|
|
|
|
pGen->loadR0FromR0();
|
2009-09-05 00:34:21 +02:00
|
|
|
doStructMember(pPtr->pHead, false);
|
2009-08-27 01:15:07 +02:00
|
|
|
} else {
|
|
|
|
error("Expected a pointer to a struct to the left of '->'");
|
|
|
|
}
|
2009-07-31 23:55:07 +02:00
|
|
|
} else if (accept('(')) {
|
|
|
|
/* function call */
|
|
|
|
Type* pDecl = NULL;
|
|
|
|
VariableInfo* pVI = NULL;
|
2009-07-30 01:22:26 +02:00
|
|
|
Type* pFn = pGen->getR0Type();
|
2009-10-28 01:23:20 +01:00
|
|
|
if (pFn->tag == TY_POINTER && pFn->pHead->tag == TY_FUNC) {
|
|
|
|
pDecl = pFn->pHead;
|
|
|
|
pGen->pushR0();
|
|
|
|
Type* pArgList = pDecl->pTail;
|
|
|
|
bool varArgs = pArgList == NULL;
|
|
|
|
/* push args and invert order */
|
|
|
|
a = pGen->beginFunctionCallArguments();
|
|
|
|
int l = 0;
|
|
|
|
int argCount = 0;
|
|
|
|
while (tok != ')' && tok != EOF) {
|
|
|
|
if (! varArgs && !pArgList) {
|
|
|
|
error("Unexpected argument.");
|
2009-07-31 23:55:07 +02:00
|
|
|
}
|
2009-10-28 01:23:20 +01:00
|
|
|
expr();
|
|
|
|
pGen->forceR0RVal();
|
|
|
|
Type* pTargetType;
|
|
|
|
if (pArgList) {
|
|
|
|
pTargetType = pArgList->pHead;
|
|
|
|
pArgList = pArgList->pTail;
|
|
|
|
} else {
|
|
|
|
// This is a ... function, just pass arguments in their
|
|
|
|
// natural type.
|
|
|
|
pTargetType = pGen->getR0Type();
|
|
|
|
if (pTargetType->tag == TY_FLOAT) {
|
|
|
|
pTargetType = mkpDouble;
|
|
|
|
} else if (pTargetType->tag == TY_ARRAY) {
|
|
|
|
// Pass arrays by pointer.
|
|
|
|
pTargetType = pTargetType->pTail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (pTargetType->tag == TY_VOID) {
|
|
|
|
error("Can't pass void value for argument %d",
|
|
|
|
argCount + 1);
|
|
|
|
} else {
|
|
|
|
l += pGen->storeR0ToArg(l, pTargetType);
|
|
|
|
}
|
|
|
|
if (accept(',')) {
|
|
|
|
// fine
|
|
|
|
} else if ( tok != ')') {
|
|
|
|
error("Expected ',' or ')'");
|
|
|
|
}
|
|
|
|
argCount += 1;
|
2009-07-31 23:55:07 +02:00
|
|
|
}
|
2009-10-28 01:23:20 +01:00
|
|
|
if (! varArgs && pArgList) {
|
|
|
|
error("Expected more argument(s). Saw %d", argCount);
|
2009-07-31 23:55:07 +02:00
|
|
|
}
|
2009-10-28 01:23:20 +01:00
|
|
|
pGen->endFunctionCallArguments(pDecl, a, l);
|
|
|
|
skip(')');
|
|
|
|
pGen->callIndirect(l, pDecl);
|
|
|
|
pGen->adjustStackAfterCall(pDecl, l, true);
|
|
|
|
} else {
|
|
|
|
error("Expected a function value to left of '('.");
|
2009-07-06 21:07:15 +02:00
|
|
|
}
|
2009-07-31 23:55:07 +02:00
|
|
|
} else {
|
|
|
|
break;
|
2009-07-08 22:04:41 +02:00
|
|
|
}
|
2009-05-08 23:54:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-05 00:34:21 +02:00
|
|
|
void doStructMember(Type* pStruct, bool isDot) {
|
2009-08-27 01:15:07 +02:00
|
|
|
Type* pStructElement = lookupStructMember(pStruct, tok);
|
|
|
|
if (pStructElement) {
|
|
|
|
next();
|
|
|
|
pGen->addStructOffsetR0(pStructElement->length, createPtrType(pStructElement->pHead));
|
|
|
|
} else {
|
|
|
|
String buf;
|
|
|
|
decodeToken(buf, tok, true);
|
2009-09-05 00:34:21 +02:00
|
|
|
error("Expected a struct member to the right of '%s', got %s",
|
|
|
|
isDot ? "." : "->", buf.getUnwrapped());
|
2009-08-27 01:15:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-31 23:34:34 +02:00
|
|
|
void doIncDec(int isInc, int isPost) {
|
|
|
|
// R0 already has the lval
|
|
|
|
checkLVal();
|
|
|
|
int lit = isInc ? 1 : -1;
|
|
|
|
pGen->pushR0();
|
|
|
|
pGen->loadR0FromR0();
|
|
|
|
int tag = pGen->getR0Type()->tag;
|
2009-08-03 23:42:57 +02:00
|
|
|
if (!(tag == TY_INT || tag == TY_SHORT || tag == TY_CHAR ||
|
|
|
|
tag == TY_POINTER)) {
|
2009-07-31 23:34:34 +02:00
|
|
|
error("++/-- illegal for this type. %d", tag);
|
|
|
|
}
|
|
|
|
if (isPost) {
|
|
|
|
pGen->over();
|
|
|
|
pGen->pushR0();
|
|
|
|
pGen->li(lit);
|
|
|
|
pGen->genOp(OP_PLUS);
|
|
|
|
pGen->storeR0ToTOS();
|
|
|
|
pGen->popR0();
|
|
|
|
} else {
|
|
|
|
pGen->pushR0();
|
|
|
|
pGen->li(lit);
|
|
|
|
pGen->genOp(OP_PLUS);
|
|
|
|
pGen->over();
|
|
|
|
pGen->storeR0ToTOS();
|
|
|
|
pGen->popR0();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-01 00:25:53 +02:00
|
|
|
void doPointer() {
|
|
|
|
pGen->forceR0RVal();
|
|
|
|
Type* pR0Type = pGen->getR0Type();
|
|
|
|
if (pR0Type->tag != TY_POINTER) {
|
|
|
|
error("Expected a pointer type.");
|
|
|
|
} else {
|
|
|
|
if (pR0Type->pHead->tag != TY_FUNC) {
|
|
|
|
pGen->setR0ExpressionType(ET_LVALUE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-05 00:24:23 +02:00
|
|
|
void doAddressOf() {
|
|
|
|
Type* pR0 = pGen->getR0Type();
|
|
|
|
bool isFuncPtr = pR0->tag == TY_POINTER && pR0->pHead->tag == TY_FUNC;
|
|
|
|
if ((! isFuncPtr) && pGen->getR0ExpressionType() != ET_LVALUE) {
|
|
|
|
error("Expected an lvalue");
|
|
|
|
}
|
|
|
|
Type* pR0Type = pGen->getR0Type();
|
|
|
|
pGen->setR0ExpressionType(ET_RVALUE);
|
|
|
|
}
|
|
|
|
|
2009-07-02 00:32:35 +02:00
|
|
|
/* Recursive descent parser for binary operations.
|
|
|
|
*/
|
|
|
|
void binaryOp(int level) {
|
2009-07-15 01:24:55 +02:00
|
|
|
intptr_t t, a;
|
2009-05-14 00:10:04 +02:00
|
|
|
t = 0;
|
2009-07-02 00:32:35 +02:00
|
|
|
if (level-- == 1)
|
2009-07-31 04:38:55 +02:00
|
|
|
unaryOrAssignment();
|
2009-05-11 23:49:29 +02:00
|
|
|
else {
|
2009-07-02 00:32:35 +02:00
|
|
|
binaryOp(level);
|
2009-05-11 23:49:29 +02:00
|
|
|
a = 0;
|
2009-07-02 00:32:35 +02:00
|
|
|
while (level == tokl) {
|
2009-05-11 23:49:29 +02:00
|
|
|
t = tokc;
|
|
|
|
next();
|
2009-07-31 04:06:34 +02:00
|
|
|
pGen->forceR0RVal();
|
2009-07-02 00:32:35 +02:00
|
|
|
if (level > 8) {
|
2009-05-12 21:48:35 +02:00
|
|
|
a = pGen->gtst(t == OP_LOGICAL_OR, a); /* && and || output code generation */
|
2009-07-02 00:32:35 +02:00
|
|
|
binaryOp(level);
|
2009-05-10 23:09:03 +02:00
|
|
|
} else {
|
2009-05-22 21:06:27 +02:00
|
|
|
pGen->pushR0();
|
2009-07-02 00:32:35 +02:00
|
|
|
binaryOp(level);
|
2009-07-16 03:23:22 +02:00
|
|
|
// Check for syntax error.
|
|
|
|
if (pGen->getR0Type() == NULL) {
|
|
|
|
// We failed to parse a right-hand argument.
|
|
|
|
// Push a dummy value so we don't fail
|
2009-07-18 01:35:23 +02:00
|
|
|
pGen->li(0);
|
2009-07-16 03:23:22 +02:00
|
|
|
}
|
2009-07-31 04:06:34 +02:00
|
|
|
pGen->forceR0RVal();
|
2009-07-02 00:32:35 +02:00
|
|
|
if ((level == 4) | (level == 5)) {
|
2009-07-18 01:35:23 +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 */
|
2009-07-02 00:32:35 +02:00
|
|
|
if (a && level > 8) {
|
2009-07-31 04:06:34 +02:00
|
|
|
pGen->forceR0RVal();
|
2009-05-12 21:48:35 +02:00
|
|
|
a = pGen->gtst(t == OP_LOGICAL_OR, a);
|
2009-07-18 01:35:23 +02:00
|
|
|
pGen->li(t != OP_LOGICAL_OR);
|
2009-07-31 01:19:43 +02:00
|
|
|
int b = pGen->gjmp(0);
|
2009-05-12 21:48:35 +02:00
|
|
|
pGen->gsym(a);
|
2009-07-18 01:35:23 +02:00
|
|
|
pGen->li(t == OP_LOGICAL_OR);
|
2009-07-31 01:19:43 +02:00
|
|
|
pGen->gsym(b);
|
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-07-31 23:01:37 +02:00
|
|
|
void commaExpr() {
|
|
|
|
for(;;) {
|
|
|
|
expr();
|
|
|
|
if (!accept(',')) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-11 23:49:29 +02:00
|
|
|
void expr() {
|
2009-07-02 00:32:35 +02:00
|
|
|
binaryOp(11);
|
2009-05-11 23:49:29 +02:00
|
|
|
}
|
2009-05-08 23:54:15 +02:00
|
|
|
|
2009-05-11 23:49:29 +02:00
|
|
|
int test_expr() {
|
2009-07-31 23:01:37 +02:00
|
|
|
commaExpr();
|
2009-07-31 04:06:34 +02:00
|
|
|
pGen->forceR0RVal();
|
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-07-06 21:07:15 +02:00
|
|
|
Type* pBaseType;
|
2009-10-29 00:10:17 +01:00
|
|
|
if ((pBaseType = acceptPrimitiveType(true))) {
|
2009-06-12 23:40:04 +02:00
|
|
|
/* declarations */
|
2009-07-06 21:07:15 +02:00
|
|
|
localDeclarations(pBaseType);
|
2009-06-12 23:40:04 +02:00
|
|
|
} else 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-09-10 04:08:54 +02:00
|
|
|
n = pCodeBuf->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 != ';')
|
2009-07-31 23:01:37 +02:00
|
|
|
commaExpr();
|
2009-05-11 23:49:29 +02:00
|
|
|
skip(';');
|
2009-09-10 04:08:54 +02:00
|
|
|
n = pCodeBuf->getPC();
|
2009-05-11 23:49:29 +02:00
|
|
|
a = 0;
|
|
|
|
if (tok != ';')
|
|
|
|
a = test_expr();
|
|
|
|
skip(';');
|
|
|
|
if (tok != ')') {
|
2009-05-12 21:48:35 +02:00
|
|
|
t = pGen->gjmp(0);
|
2009-07-31 23:01:37 +02:00
|
|
|
commaExpr();
|
2009-09-10 04:08:54 +02:00
|
|
|
pGen->gjmp(n - pCodeBuf->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-09-10 04:08:54 +02:00
|
|
|
pGen->gjmp(n - pCodeBuf->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) {
|
2009-06-29 23:29:08 +02:00
|
|
|
mLocals.pushLevel();
|
2009-06-12 20:25:59 +02:00
|
|
|
}
|
2009-05-10 23:09:03 +02:00
|
|
|
next();
|
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) {
|
2009-06-29 23:29:08 +02:00
|
|
|
mLocals.popLevel();
|
2009-06-12 20:25:59 +02:00
|
|
|
}
|
2009-05-11 23:49:29 +02:00
|
|
|
} else {
|
2009-07-06 21:07:15 +02:00
|
|
|
if (accept(TOK_RETURN)) {
|
2009-07-07 23:48:51 +02:00
|
|
|
if (tok != ';') {
|
2009-07-31 23:01:37 +02:00
|
|
|
commaExpr();
|
2009-07-31 04:06:34 +02:00
|
|
|
pGen->forceR0RVal();
|
2009-07-09 22:34:25 +02:00
|
|
|
if (pReturnType->tag == TY_VOID) {
|
|
|
|
error("Must not return a value from a void function");
|
|
|
|
} else {
|
|
|
|
pGen->convertR0(pReturnType);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (pReturnType->tag != TY_VOID) {
|
|
|
|
error("Must specify a value here");
|
|
|
|
}
|
2009-07-07 23:48:51 +02:00
|
|
|
}
|
2009-05-12 21:48:35 +02:00
|
|
|
rsym = pGen->gjmp(rsym); /* jmp */
|
2009-07-06 21:07:15 +02:00
|
|
|
} else if (accept(TOK_BREAK)) {
|
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 != ';')
|
2009-07-31 23:01:37 +02:00
|
|
|
commaExpr();
|
2009-05-11 23:49:29 +02:00
|
|
|
skip(';');
|
|
|
|
}
|
2009-05-08 23:54:15 +02:00
|
|
|
}
|
|
|
|
|
2009-07-14 03:40:08 +02:00
|
|
|
static bool typeEqual(Type* a, Type* b) {
|
2009-07-02 23:46:19 +02:00
|
|
|
if (a == b) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (a == NULL || b == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
TypeTag at = a->tag;
|
|
|
|
if (at != b->tag) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (at == TY_POINTER) {
|
|
|
|
return typeEqual(a->pHead, b->pHead);
|
2009-08-04 23:56:09 +02:00
|
|
|
} else if (at == TY_ARRAY) {
|
|
|
|
return a->length == b->length && typeEqual(a->pHead, b->pHead);
|
2009-07-02 23:46:19 +02:00
|
|
|
} else if (at == TY_FUNC || at == TY_PARAM) {
|
|
|
|
return typeEqual(a->pHead, b->pHead)
|
|
|
|
&& typeEqual(a->pTail, b->pTail);
|
2009-08-27 01:15:07 +02:00
|
|
|
} else if (at == TY_STRUCT) {
|
|
|
|
return a->pHead == b->pHead;
|
2009-07-02 23:46:19 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-07-24 00:11:22 +02:00
|
|
|
Type* createType(TypeTag tag, Type* pHead, Type* pTail) {
|
2009-10-29 00:10:17 +01:00
|
|
|
assert(tag >= TY_UNKNOWN && tag <= TY_PARAM);
|
2009-07-24 00:11:22 +02:00
|
|
|
Type* pType = (Type*) mpCurrentArena->alloc(sizeof(Type));
|
2009-07-01 03:09:56 +02:00
|
|
|
memset(pType, 0, sizeof(*pType));
|
2009-10-29 00:10:17 +01:00
|
|
|
pType->storageClass = SC_DEFAULT;
|
2009-07-01 03:09:56 +02:00
|
|
|
pType->tag = tag;
|
|
|
|
pType->pHead = pHead;
|
|
|
|
pType->pTail = pTail;
|
|
|
|
return pType;
|
2009-06-05 04:56:13 +02:00
|
|
|
}
|
|
|
|
|
2009-07-24 00:11:22 +02:00
|
|
|
Type* createPtrType(Type* pType) {
|
|
|
|
return createType(TY_POINTER, pType, NULL);
|
2009-07-02 23:46:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Try to print a type in declaration order
|
|
|
|
*/
|
2009-07-01 03:09:56 +02:00
|
|
|
void decodeType(String& buffer, Type* pType) {
|
2009-07-02 23:46:19 +02:00
|
|
|
buffer.clear();
|
2009-07-01 03:09:56 +02:00
|
|
|
if (pType == NULL) {
|
|
|
|
buffer.appendCStr("null");
|
|
|
|
return;
|
|
|
|
}
|
2009-07-02 23:46:19 +02:00
|
|
|
decodeTypeImp(buffer, pType);
|
|
|
|
}
|
|
|
|
|
|
|
|
void decodeTypeImp(String& buffer, Type* pType) {
|
|
|
|
decodeTypeImpPrefix(buffer, pType);
|
2009-08-27 01:15:07 +02:00
|
|
|
decodeId(buffer, pType->id);
|
|
|
|
decodeTypeImpPostfix(buffer, pType);
|
|
|
|
}
|
2009-07-02 23:46:19 +02:00
|
|
|
|
2009-08-27 01:15:07 +02:00
|
|
|
void decodeId(String& buffer, tokenid_t id) {
|
|
|
|
if (id) {
|
|
|
|
String temp;
|
|
|
|
decodeToken(temp, id, false);
|
2009-07-01 03:09:56 +02:00
|
|
|
buffer.append(temp);
|
2009-07-02 23:46:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void decodeTypeImpPrefix(String& buffer, Type* pType) {
|
|
|
|
TypeTag tag = pType->tag;
|
|
|
|
|
2009-08-27 01:15:07 +02:00
|
|
|
if ((tag >= TY_INT && tag <= TY_DOUBLE) || tag == TY_STRUCT) {
|
2009-07-02 23:46:19 +02:00
|
|
|
switch (tag) {
|
|
|
|
case TY_INT:
|
|
|
|
buffer.appendCStr("int");
|
|
|
|
break;
|
2009-08-03 23:42:57 +02:00
|
|
|
case TY_SHORT:
|
|
|
|
buffer.appendCStr("short");
|
|
|
|
break;
|
2009-07-02 23:46:19 +02:00
|
|
|
case TY_CHAR:
|
|
|
|
buffer.appendCStr("char");
|
|
|
|
break;
|
|
|
|
case TY_VOID:
|
|
|
|
buffer.appendCStr("void");
|
|
|
|
break;
|
2009-07-06 21:07:15 +02:00
|
|
|
case TY_FLOAT:
|
|
|
|
buffer.appendCStr("float");
|
|
|
|
break;
|
|
|
|
case TY_DOUBLE:
|
|
|
|
buffer.appendCStr("double");
|
|
|
|
break;
|
2009-08-27 01:15:07 +02:00
|
|
|
case TY_STRUCT:
|
|
|
|
{
|
|
|
|
bool isStruct = (pType->pHead->alignment & 0x80000000) != 0;
|
|
|
|
buffer.appendCStr(isStruct ? "struct" : "union");
|
|
|
|
if (pType->pHead && pType->pHead->structTag) {
|
|
|
|
buffer.append(' ');
|
|
|
|
decodeId(buffer, pType->pHead->structTag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2009-07-02 23:46:19 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2009-07-01 03:09:56 +02:00
|
|
|
buffer.append(' ');
|
|
|
|
}
|
2009-07-02 23:46:19 +02:00
|
|
|
|
|
|
|
switch (tag) {
|
2009-07-01 03:09:56 +02:00
|
|
|
case TY_INT:
|
|
|
|
break;
|
2009-08-03 23:42:57 +02:00
|
|
|
case TY_SHORT:
|
|
|
|
break;
|
2009-07-01 03:09:56 +02:00
|
|
|
case TY_CHAR:
|
|
|
|
break;
|
|
|
|
case TY_VOID:
|
2009-07-02 23:46:19 +02:00
|
|
|
break;
|
2009-07-06 21:07:15 +02:00
|
|
|
case TY_FLOAT:
|
|
|
|
break;
|
|
|
|
case TY_DOUBLE:
|
|
|
|
break;
|
2009-07-01 03:09:56 +02:00
|
|
|
case TY_POINTER:
|
2009-07-02 23:46:19 +02:00
|
|
|
decodeTypeImpPrefix(buffer, pType->pHead);
|
|
|
|
if(pType->pHead && pType->pHead->tag == TY_FUNC) {
|
|
|
|
buffer.append('(');
|
|
|
|
}
|
|
|
|
buffer.append('*');
|
2009-07-01 03:09:56 +02:00
|
|
|
break;
|
2009-08-04 23:56:09 +02:00
|
|
|
case TY_ARRAY:
|
|
|
|
decodeTypeImpPrefix(buffer, pType->pHead);
|
|
|
|
break;
|
2009-08-27 01:15:07 +02:00
|
|
|
case TY_STRUCT:
|
|
|
|
break;
|
2009-07-01 03:09:56 +02:00
|
|
|
case TY_FUNC:
|
2009-07-02 23:46:19 +02:00
|
|
|
decodeTypeImp(buffer, pType->pHead);
|
2009-07-01 03:09:56 +02:00
|
|
|
break;
|
|
|
|
case TY_PARAM:
|
2009-07-02 23:46:19 +02:00
|
|
|
decodeTypeImp(buffer, pType->pHead);
|
2009-07-01 03:09:56 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
String temp;
|
|
|
|
temp.printf("Unknown tag %d", pType->tag);
|
|
|
|
buffer.append(temp);
|
|
|
|
break;
|
|
|
|
}
|
2009-07-02 23:46:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void decodeTypeImpPostfix(String& buffer, Type* pType) {
|
|
|
|
TypeTag tag = pType->tag;
|
|
|
|
|
|
|
|
switch(tag) {
|
|
|
|
case TY_POINTER:
|
|
|
|
if(pType->pHead && pType->pHead->tag == TY_FUNC) {
|
|
|
|
buffer.append(')');
|
|
|
|
}
|
|
|
|
decodeTypeImpPostfix(buffer, pType->pHead);
|
|
|
|
break;
|
2009-08-04 23:56:09 +02:00
|
|
|
case TY_ARRAY:
|
|
|
|
{
|
|
|
|
String temp;
|
|
|
|
temp.printf("[%d]", pType->length);
|
|
|
|
buffer.append(temp);
|
|
|
|
}
|
|
|
|
break;
|
2009-08-27 01:15:07 +02:00
|
|
|
case TY_STRUCT:
|
|
|
|
if (pType->pHead->length >= 0) {
|
|
|
|
buffer.appendCStr(" {");
|
|
|
|
for(Type* pArg = pType->pTail; pArg; pArg = pArg->pTail) {
|
|
|
|
decodeTypeImp(buffer, pArg->pHead);
|
|
|
|
buffer.appendCStr(";");
|
|
|
|
}
|
|
|
|
buffer.append('}');
|
|
|
|
}
|
|
|
|
break;
|
2009-07-02 23:46:19 +02:00
|
|
|
case TY_FUNC:
|
|
|
|
buffer.append('(');
|
|
|
|
for(Type* pArg = pType->pTail; pArg; pArg = pArg->pTail) {
|
|
|
|
decodeTypeImp(buffer, pArg);
|
|
|
|
if (pArg->pTail) {
|
|
|
|
buffer.appendCStr(", ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buffer.append(')');
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2009-07-01 03:09:56 +02:00
|
|
|
}
|
2009-06-05 04:56:13 +02:00
|
|
|
}
|
|
|
|
|
2009-07-01 03:09:56 +02:00
|
|
|
void printType(Type* pType) {
|
|
|
|
String buffer;
|
|
|
|
decodeType(buffer, pType);
|
|
|
|
fprintf(stderr, "%s\n", buffer.getUnwrapped());
|
2009-06-05 04:56:13 +02:00
|
|
|
}
|
|
|
|
|
2009-10-29 00:10:17 +01:00
|
|
|
void insertTypeSpecifier(Type** ppType, TypeTag tag) {
|
|
|
|
if (! *ppType) {
|
|
|
|
*ppType = createType(tag, NULL, NULL);
|
2009-06-05 04:56:13 +02:00
|
|
|
} else {
|
2009-10-29 00:10:17 +01:00
|
|
|
if ((*ppType)->tag != TY_UNKNOWN) {
|
|
|
|
error("Only one type specifier allowed.");
|
|
|
|
} else {
|
|
|
|
(*ppType)->tag = tag;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void insertStorageClass(Type** ppType, StorageClass storageClass) {
|
|
|
|
if (! *ppType) {
|
|
|
|
*ppType = createType(TY_UNKNOWN, NULL, NULL);
|
|
|
|
}
|
|
|
|
if ((*ppType)->storageClass != SC_DEFAULT) {
|
|
|
|
error("Only one storage class allowed.");
|
|
|
|
} else {
|
|
|
|
(*ppType)->storageClass = storageClass;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Type* acceptPrimitiveType(bool allowStorageClass) {
|
|
|
|
Type* pType = NULL;
|
|
|
|
for (bool keepGoing = true; keepGoing;) {
|
|
|
|
switch(tok) {
|
|
|
|
case TOK_AUTO:
|
|
|
|
insertStorageClass(&pType, SC_AUTO);
|
|
|
|
break;
|
|
|
|
case TOK_REGISTER:
|
|
|
|
insertStorageClass(&pType, SC_REGISTER);
|
|
|
|
break;
|
|
|
|
case TOK_STATIC:
|
|
|
|
insertStorageClass(&pType, SC_STATIC);
|
|
|
|
break;
|
|
|
|
case TOK_EXTERN:
|
|
|
|
insertStorageClass(&pType, SC_EXTERN);
|
|
|
|
break;
|
|
|
|
case TOK_TYPEDEF:
|
|
|
|
insertStorageClass(&pType, SC_TYPEDEF);
|
|
|
|
break;
|
|
|
|
case TOK_INT:
|
|
|
|
insertTypeSpecifier(&pType, TY_INT);
|
|
|
|
break;
|
|
|
|
case TOK_SHORT:
|
|
|
|
insertTypeSpecifier(&pType, TY_SHORT);
|
|
|
|
break;
|
|
|
|
case TOK_CHAR:
|
|
|
|
insertTypeSpecifier(&pType, TY_CHAR);
|
|
|
|
break;
|
|
|
|
case TOK_VOID:
|
|
|
|
insertTypeSpecifier(&pType, TY_VOID);
|
|
|
|
break;
|
|
|
|
case TOK_FLOAT:
|
|
|
|
insertTypeSpecifier(&pType, TY_FLOAT);
|
|
|
|
break;
|
|
|
|
case TOK_DOUBLE:
|
|
|
|
insertTypeSpecifier(&pType, TY_DOUBLE);
|
|
|
|
break;
|
|
|
|
case TOK_STRUCT:
|
|
|
|
case TOK_UNION:
|
|
|
|
{
|
|
|
|
insertTypeSpecifier(&pType, TY_STRUCT);
|
|
|
|
bool isStruct = (tok == TOK_STRUCT);
|
|
|
|
next();
|
|
|
|
pType = acceptStruct(pType, isStruct);
|
|
|
|
keepGoing = false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Is it a typedef?
|
|
|
|
if (isSymbol(tok)) {
|
|
|
|
VariableInfo* pV = VI(tok);
|
|
|
|
if (pV && pV->pType->storageClass == SC_TYPEDEF) {
|
|
|
|
if (! pType) {
|
|
|
|
pType = createType(TY_UNKNOWN, NULL, NULL);
|
|
|
|
}
|
|
|
|
StorageClass storageClass = pType->storageClass;
|
|
|
|
*pType = *pV->pType;
|
|
|
|
pType->storageClass = storageClass;
|
|
|
|
} else {
|
|
|
|
keepGoing = false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
keepGoing = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (keepGoing) {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (pType) {
|
|
|
|
if (pType->tag == TY_UNKNOWN) {
|
|
|
|
pType->tag = TY_INT;
|
|
|
|
}
|
|
|
|
if (allowStorageClass) {
|
|
|
|
switch(pType->storageClass) {
|
|
|
|
case SC_AUTO: error("auto not supported."); break;
|
|
|
|
case SC_REGISTER: error("register not supported."); break;
|
|
|
|
case SC_STATIC: error("static not supported."); break;
|
|
|
|
case SC_EXTERN: error("extern not supported."); break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (pType->storageClass != SC_DEFAULT) {
|
|
|
|
error("An explicit storage class is not allowed in this type declaration");
|
|
|
|
}
|
|
|
|
}
|
2009-06-05 04:56:13 +02:00
|
|
|
}
|
2009-07-01 03:09:56 +02:00
|
|
|
return pType;
|
|
|
|
}
|
|
|
|
|
2009-10-29 00:10:17 +01:00
|
|
|
Type* acceptStruct(Type* pStructType, bool isStruct) {
|
2009-08-27 01:15:07 +02:00
|
|
|
tokenid_t structTag = acceptSymbol();
|
|
|
|
bool isDeclaration = accept('{');
|
|
|
|
bool fail = false;
|
|
|
|
|
|
|
|
if (structTag) {
|
|
|
|
Token* pToken = &mTokenTable[structTag];
|
|
|
|
VariableInfo* pStructInfo = pToken->mpStructInfo;
|
|
|
|
bool needToDeclare = !pStructInfo;
|
|
|
|
if (pStructInfo) {
|
|
|
|
if (isDeclaration) {
|
|
|
|
if (mpCurrentSymbolStack->isStructTagDefinedAtCurrentLevel(structTag)) {
|
|
|
|
if (pStructInfo->pType->pHead->length == -1) {
|
|
|
|
// we're filling in a forward declaration.
|
|
|
|
needToDeclare = false;
|
|
|
|
} else {
|
|
|
|
error("A struct with the same name is already defined at this level.");
|
|
|
|
fail = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
needToDeclare = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!fail) {
|
|
|
|
assert(pStructInfo->isStructTag);
|
|
|
|
pStructType->pHead = pStructInfo->pType;
|
|
|
|
pStructType->pTail = pStructType->pHead->pTail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (needToDeclare) {
|
|
|
|
// This is a new struct name
|
|
|
|
pToken->mpStructInfo = mpCurrentSymbolStack->addStructTag(structTag);
|
2009-10-29 00:10:17 +01:00
|
|
|
StorageClass storageClass = pStructType->storageClass;
|
2009-08-27 01:15:07 +02:00
|
|
|
pStructType = createType(TY_STRUCT, NULL, NULL);
|
|
|
|
pStructType->structTag = structTag;
|
|
|
|
pStructType->pHead = pStructType;
|
2009-10-29 00:10:17 +01:00
|
|
|
pStructType->storageClass = storageClass;
|
2009-08-27 01:15:07 +02:00
|
|
|
if (! isDeclaration) {
|
|
|
|
// A forward declaration
|
|
|
|
pStructType->length = -1;
|
|
|
|
}
|
|
|
|
pToken->mpStructInfo->pType = pStructType;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// An anonymous struct
|
|
|
|
pStructType->pHead = pStructType;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isDeclaration) {
|
|
|
|
size_t offset = 0;
|
|
|
|
size_t structSize = 0;
|
|
|
|
size_t structAlignment = 0;
|
|
|
|
Type** pParamHolder = & pStructType->pHead->pTail;
|
|
|
|
while (tok != '}' && tok != EOF) {
|
2009-10-29 00:10:17 +01:00
|
|
|
Type* pPrimitiveType = expectPrimitiveType(false);
|
2009-08-27 01:15:07 +02:00
|
|
|
if (pPrimitiveType) {
|
|
|
|
while (tok != ';' && tok != EOF) {
|
|
|
|
Type* pItem = acceptDeclaration(pPrimitiveType, true, false);
|
|
|
|
if (!pItem) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (lookupStructMember(pStructType, pItem->id)) {
|
|
|
|
String buf;
|
|
|
|
decodeToken(buf, pItem->id, false);
|
|
|
|
error("Duplicate struct member %s", buf.getUnwrapped());
|
|
|
|
}
|
|
|
|
Type* pStructElement = createType(TY_PARAM, pItem, NULL);
|
|
|
|
size_t alignment = pGen->alignmentOf(pItem);
|
|
|
|
if (alignment > structAlignment) {
|
|
|
|
structAlignment = alignment;
|
|
|
|
}
|
|
|
|
size_t alignmentMask = alignment - 1;
|
|
|
|
offset = (offset + alignmentMask) & ~alignmentMask;
|
|
|
|
pStructElement->length = offset;
|
|
|
|
size_t size = pGen->sizeOf(pItem);
|
|
|
|
if (isStruct) {
|
|
|
|
offset += size;
|
|
|
|
structSize = offset;
|
|
|
|
} else {
|
|
|
|
if (size >= structSize) {
|
|
|
|
structSize = size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*pParamHolder = pStructElement;
|
|
|
|
pParamHolder = &pStructElement->pTail;
|
|
|
|
accept(',');
|
|
|
|
}
|
|
|
|
skip(';');
|
|
|
|
} else {
|
|
|
|
// Some sort of syntax error, skip token and keep trying
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!fail) {
|
|
|
|
pStructType->pHead->length = structSize;
|
|
|
|
pStructType->pHead->alignment = structAlignment | (isStruct << 31);
|
|
|
|
}
|
|
|
|
skip('}');
|
|
|
|
}
|
|
|
|
if (fail) {
|
|
|
|
pStructType = NULL;
|
|
|
|
}
|
|
|
|
return pStructType;
|
|
|
|
}
|
|
|
|
|
|
|
|
Type* lookupStructMember(Type* pStruct, tokenid_t memberId) {
|
|
|
|
for(Type* pStructElement = pStruct->pHead->pTail; pStructElement; pStructElement = pStructElement->pTail) {
|
|
|
|
if (pStructElement->pHead->id == memberId) {
|
|
|
|
return pStructElement;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-07-24 00:11:22 +02:00
|
|
|
Type* acceptDeclaration(Type* pType, bool nameAllowed, bool nameRequired) {
|
2009-07-02 23:46:19 +02:00
|
|
|
tokenid_t declName = 0;
|
2009-07-17 04:05:07 +02:00
|
|
|
bool reportFailure = false;
|
2009-10-29 00:10:17 +01:00
|
|
|
StorageClass storageClass = pType->storageClass;
|
2009-07-02 23:46:19 +02:00
|
|
|
pType = acceptDecl2(pType, declName, nameAllowed,
|
2009-07-24 00:11:22 +02:00
|
|
|
nameRequired, reportFailure);
|
2009-07-02 23:46:19 +02:00
|
|
|
if (declName) {
|
|
|
|
// Clone the parent type so we can set a unique ID
|
2009-08-04 23:56:09 +02:00
|
|
|
Type* pOldType = pType;
|
2009-07-24 00:11:22 +02:00
|
|
|
pType = createType(pType->tag, pType->pHead, pType->pTail);
|
2009-08-27 01:15:07 +02:00
|
|
|
*pType = *pOldType;
|
2009-07-01 03:09:56 +02:00
|
|
|
pType->id = declName;
|
2009-10-29 00:10:17 +01:00
|
|
|
pType->storageClass = storageClass;
|
2009-08-04 23:56:09 +02:00
|
|
|
} else if (nameRequired) {
|
|
|
|
error("Expected a variable name");
|
2009-07-02 23:46:19 +02:00
|
|
|
}
|
2009-08-27 01:15:07 +02:00
|
|
|
#if 0
|
|
|
|
fprintf(stderr, "Parsed a declaration: ");
|
|
|
|
printType(pType);
|
|
|
|
#endif
|
2009-07-17 04:05:07 +02:00
|
|
|
if (reportFailure) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-07-02 23:46:19 +02:00
|
|
|
return pType;
|
|
|
|
}
|
|
|
|
|
2009-07-24 00:11:22 +02:00
|
|
|
Type* expectDeclaration(Type* pBaseType) {
|
2009-08-27 01:15:07 +02:00
|
|
|
bool nameRequired = pBaseType->tag != TY_STRUCT;
|
|
|
|
Type* pType = acceptDeclaration(pBaseType, true, nameRequired);
|
2009-07-02 23:46:19 +02:00
|
|
|
if (! pType) {
|
|
|
|
error("Expected a declaration");
|
2009-07-01 03:09:56 +02:00
|
|
|
}
|
|
|
|
return pType;
|
|
|
|
}
|
|
|
|
|
2009-07-02 23:46:19 +02:00
|
|
|
/* Used for accepting types that appear in casts */
|
2009-07-24 00:11:22 +02:00
|
|
|
Type* acceptCastTypeDeclaration() {
|
2009-10-29 00:10:17 +01:00
|
|
|
Type* pType = acceptPrimitiveType(false);
|
2009-07-02 23:46:19 +02:00
|
|
|
if (pType) {
|
2009-07-24 00:11:22 +02:00
|
|
|
pType = acceptDeclaration(pType, false, false);
|
2009-07-02 23:46:19 +02:00
|
|
|
}
|
|
|
|
return pType;
|
|
|
|
}
|
|
|
|
|
2009-07-24 00:11:22 +02:00
|
|
|
Type* expectCastTypeDeclaration() {
|
|
|
|
Type* pType = acceptCastTypeDeclaration();
|
2009-07-01 03:09:56 +02:00
|
|
|
if (! pType) {
|
|
|
|
error("Expected a declaration");
|
|
|
|
}
|
|
|
|
return pType;
|
2009-06-05 04:56:13 +02:00
|
|
|
}
|
|
|
|
|
2009-07-02 23:46:19 +02:00
|
|
|
Type* acceptDecl2(Type* pType, tokenid_t& declName,
|
2009-07-24 00:11:22 +02:00
|
|
|
bool nameAllowed, bool nameRequired,
|
2009-07-17 04:05:07 +02:00
|
|
|
bool& reportFailure) {
|
2009-07-02 23:46:19 +02:00
|
|
|
while (accept('*')) {
|
2009-08-01 00:58:19 +02:00
|
|
|
pType = createType(TY_POINTER, pType, NULL);
|
2009-07-02 23:46:19 +02:00
|
|
|
}
|
2009-07-24 00:11:22 +02:00
|
|
|
pType = acceptDecl3(pType, declName, nameAllowed, nameRequired,
|
2009-07-17 04:05:07 +02:00
|
|
|
reportFailure);
|
2009-07-01 03:09:56 +02:00
|
|
|
return pType;
|
|
|
|
}
|
|
|
|
|
2009-07-02 23:46:19 +02:00
|
|
|
Type* acceptDecl3(Type* pType, tokenid_t& declName,
|
2009-07-24 00:11:22 +02:00
|
|
|
bool nameAllowed, bool nameRequired,
|
2009-07-17 04:05:07 +02:00
|
|
|
bool& reportFailure) {
|
2009-07-02 23:46:19 +02:00
|
|
|
// direct-dcl :
|
|
|
|
// name
|
|
|
|
// (dcl)
|
|
|
|
// direct-dcl()
|
|
|
|
// direct-dcl[]
|
|
|
|
Type* pNewHead = NULL;
|
2009-07-01 03:09:56 +02:00
|
|
|
if (accept('(')) {
|
2009-07-02 23:46:19 +02:00
|
|
|
pNewHead = acceptDecl2(pNewHead, declName, nameAllowed,
|
2009-07-24 00:11:22 +02:00
|
|
|
nameRequired, reportFailure);
|
2009-07-01 03:09:56 +02:00
|
|
|
skip(')');
|
2009-07-02 23:46:19 +02:00
|
|
|
} else if ((declName = acceptSymbol()) != 0) {
|
|
|
|
if (nameAllowed == false && declName) {
|
|
|
|
error("Symbol %s not allowed here", nameof(declName));
|
2009-07-17 04:05:07 +02:00
|
|
|
reportFailure = true;
|
2009-07-02 23:46:19 +02:00
|
|
|
}
|
2009-07-17 04:05:07 +02:00
|
|
|
} else if (nameRequired && ! declName) {
|
|
|
|
String temp;
|
|
|
|
decodeToken(temp, tok, true);
|
|
|
|
error("Expected name. Got %s", temp.getUnwrapped());
|
|
|
|
reportFailure = true;
|
2009-06-05 04:56:13 +02:00
|
|
|
}
|
2009-08-04 23:56:09 +02:00
|
|
|
for(;;) {
|
|
|
|
if (accept('(')) {
|
|
|
|
// Function declaration
|
|
|
|
Type* pTail = acceptArgs(nameAllowed);
|
|
|
|
pType = createType(TY_FUNC, pType, pTail);
|
|
|
|
skip(')');
|
|
|
|
} if (accept('[')) {
|
|
|
|
if (tok != ']') {
|
|
|
|
if (tok != TOK_NUM || tokc <= 0) {
|
|
|
|
error("Expected positive integer constant");
|
|
|
|
} else {
|
|
|
|
Type* pDecayType = createPtrType(pType);
|
|
|
|
pType = createType(TY_ARRAY, pType, pDecayType);
|
|
|
|
pType->length = tokc;
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
skip(']');
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2009-07-01 03:09:56 +02:00
|
|
|
}
|
2009-07-02 23:46:19 +02:00
|
|
|
|
|
|
|
if (pNewHead) {
|
|
|
|
Type* pA = pNewHead;
|
|
|
|
while (pA->pHead) {
|
|
|
|
pA = pA->pHead;
|
|
|
|
}
|
|
|
|
pA->pHead = pType;
|
|
|
|
pType = pNewHead;
|
|
|
|
}
|
2009-07-01 03:09:56 +02:00
|
|
|
return pType;
|
|
|
|
}
|
|
|
|
|
2009-07-24 00:11:22 +02:00
|
|
|
Type* acceptArgs(bool nameAllowed) {
|
2009-07-01 03:09:56 +02:00
|
|
|
Type* pHead = NULL;
|
|
|
|
Type* pTail = NULL;
|
|
|
|
for(;;) {
|
2009-10-29 00:10:17 +01:00
|
|
|
Type* pBaseArg = acceptPrimitiveType(false);
|
2009-07-01 03:09:56 +02:00
|
|
|
if (pBaseArg) {
|
2009-07-24 00:11:22 +02:00
|
|
|
Type* pArg = acceptDeclaration(pBaseArg, nameAllowed, false);
|
2009-07-01 03:09:56 +02:00
|
|
|
if (pArg) {
|
2009-07-24 00:11:22 +02:00
|
|
|
Type* pParam = createType(TY_PARAM, pArg, NULL);
|
2009-07-01 03:09:56 +02:00
|
|
|
if (!pHead) {
|
|
|
|
pHead = pParam;
|
|
|
|
pTail = pParam;
|
|
|
|
} else {
|
|
|
|
pTail->pTail = pParam;
|
|
|
|
pTail = pParam;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (! accept(',')) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pHead;
|
2009-06-05 04:56:13 +02:00
|
|
|
}
|
|
|
|
|
2009-10-29 00:10:17 +01:00
|
|
|
Type* expectPrimitiveType(bool allowStorageClass) {
|
|
|
|
Type* pType = acceptPrimitiveType(allowStorageClass);
|
2009-07-01 03:09:56 +02:00
|
|
|
if (!pType) {
|
2009-06-29 23:29:08 +02:00
|
|
|
String buf;
|
2009-07-15 03:35:36 +02:00
|
|
|
decodeToken(buf, tok, true);
|
2009-06-29 23:29:08 +02:00
|
|
|
error("Expected a type, got %s", buf.getUnwrapped());
|
2009-06-05 04:56:13 +02:00
|
|
|
}
|
2009-07-01 03:09:56 +02:00
|
|
|
return pType;
|
2009-06-05 04:56:13 +02:00
|
|
|
}
|
|
|
|
|
2009-07-31 04:06:34 +02:00
|
|
|
void checkLVal() {
|
|
|
|
if (pGen->getR0ExpressionType() != ET_LVALUE) {
|
2009-09-05 00:24:23 +02:00
|
|
|
error("Expected an lvalue");
|
2009-07-31 04:06:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-01 03:09:56 +02:00
|
|
|
void addGlobalSymbol(Type* pDecl) {
|
|
|
|
tokenid_t t = pDecl->id;
|
|
|
|
VariableInfo* pVI = VI(t);
|
2009-06-29 23:29:08 +02:00
|
|
|
if(pVI && pVI->pAddress) {
|
2009-07-01 03:09:56 +02:00
|
|
|
reportDuplicate(t);
|
2009-06-29 23:29:08 +02:00
|
|
|
}
|
2009-07-01 03:09:56 +02:00
|
|
|
mGlobals.add(pDecl);
|
2009-06-12 20:25:59 +02:00
|
|
|
}
|
|
|
|
|
2009-07-01 03:09:56 +02:00
|
|
|
void reportDuplicate(tokenid_t t) {
|
|
|
|
error("Duplicate definition of %s", nameof(t));
|
2009-06-12 04:06:24 +02:00
|
|
|
}
|
|
|
|
|
2009-07-01 03:09:56 +02:00
|
|
|
void addLocalSymbol(Type* pDecl) {
|
|
|
|
tokenid_t t = pDecl->id;
|
|
|
|
if (mLocals.isDefinedAtCurrentLevel(t)) {
|
|
|
|
reportDuplicate(t);
|
2009-06-29 23:29:08 +02:00
|
|
|
}
|
2009-07-01 03:09:56 +02:00
|
|
|
mLocals.add(pDecl);
|
2009-06-12 04:06:24 +02:00
|
|
|
}
|
|
|
|
|
2009-09-08 20:06:40 +02:00
|
|
|
bool checkUndeclaredStruct(Type* pBaseType) {
|
|
|
|
if (pBaseType->tag == TY_STRUCT && pBaseType->length < 0) {
|
|
|
|
String temp;
|
|
|
|
decodeToken(temp, pBaseType->structTag, false);
|
|
|
|
error("Undeclared struct %s", temp.getUnwrapped());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-07-06 21:07:15 +02:00
|
|
|
void localDeclarations(Type* pBaseType) {
|
2009-05-20 21:12:06 +02:00
|
|
|
intptr_t a;
|
2009-06-05 04:56:13 +02:00
|
|
|
|
2009-07-06 21:07:15 +02:00
|
|
|
while (pBaseType) {
|
2009-06-12 22:12:55 +02:00
|
|
|
while (tok != ';' && tok != EOF) {
|
2009-07-24 00:11:22 +02:00
|
|
|
Type* pDecl = expectDeclaration(pBaseType);
|
2009-07-01 03:09:56 +02:00
|
|
|
if (!pDecl) {
|
|
|
|
break;
|
2009-06-12 20:25:59 +02:00
|
|
|
}
|
2009-08-27 01:15:07 +02:00
|
|
|
if (!pDecl->id) {
|
|
|
|
break;
|
|
|
|
}
|
2009-09-08 20:06:40 +02:00
|
|
|
if (checkUndeclaredStruct(pDecl)) {
|
|
|
|
break;
|
|
|
|
}
|
2009-07-01 03:09:56 +02:00
|
|
|
addLocalSymbol(pDecl);
|
2009-09-19 00:03:03 +02:00
|
|
|
if (pDecl->tag == TY_FUNC) {
|
|
|
|
if (tok == '{') {
|
|
|
|
error("Nested functions are not allowed. Did you forget a '}' ?");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Else it's a forward declaration of a function.
|
2009-10-29 00:10:17 +01:00
|
|
|
} else if (pDecl->storageClass != SC_TYPEDEF) {
|
2009-09-19 00:03:03 +02:00
|
|
|
int variableAddress = 0;
|
|
|
|
size_t alignment = pGen->alignmentOf(pDecl);
|
|
|
|
assert(alignment > 0);
|
|
|
|
size_t alignmentMask = ~ (alignment - 1);
|
|
|
|
size_t sizeOf = pGen->sizeOf(pDecl);
|
|
|
|
assert(sizeOf > 0);
|
|
|
|
loc = (loc + alignment - 1) & alignmentMask;
|
|
|
|
size_t alignedSize = (sizeOf + alignment - 1) & alignmentMask;
|
|
|
|
loc = loc + alignedSize;
|
|
|
|
variableAddress = -loc;
|
|
|
|
VI(pDecl->id)->pAddress = (void*) variableAddress;
|
|
|
|
if (accept('=')) {
|
|
|
|
/* assignment */
|
|
|
|
pGen->leaR0(variableAddress, createPtrType(pDecl), ET_LVALUE);
|
|
|
|
pGen->pushR0();
|
|
|
|
expr();
|
|
|
|
pGen->forceR0RVal();
|
|
|
|
pGen->storeR0ToTOS();
|
|
|
|
}
|
2009-06-12 23:26:58 +02:00
|
|
|
}
|
2009-06-05 04:56:13 +02:00
|
|
|
if (tok == ',')
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
skip(';');
|
2009-10-29 00:10:17 +01:00
|
|
|
pBaseType = acceptPrimitiveType(true);
|
2009-06-05 04:56:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-12 22:53:51 +02:00
|
|
|
bool checkSymbol() {
|
2009-07-02 00:32:35 +02:00
|
|
|
return checkSymbol(tok);
|
2009-06-12 23:40:04 +02:00
|
|
|
}
|
|
|
|
|
2009-07-15 03:35:36 +02:00
|
|
|
void decodeToken(String& buffer, tokenid_t token, bool quote) {
|
2009-06-29 23:29:08 +02:00
|
|
|
if (token == EOF ) {
|
|
|
|
buffer.printf("EOF");
|
|
|
|
} else if (token == TOK_NUM) {
|
2009-10-28 01:23:20 +01:00
|
|
|
buffer.printf("numeric constant %d(0x%x)", tokc, tokc);
|
|
|
|
} else if (token == TOK_NUM_FLOAT) {
|
|
|
|
buffer.printf("numeric constant float %g", tokd);
|
|
|
|
} else if (token == TOK_NUM_DOUBLE) {
|
|
|
|
buffer.printf("numeric constant double %g", tokd);
|
|
|
|
} else if (token >= 0 && token < 256) {
|
2009-07-01 03:09:56 +02:00
|
|
|
if (token < 32) {
|
|
|
|
buffer.printf("'\\x%02x'", token);
|
|
|
|
} else {
|
|
|
|
buffer.printf("'%c'", token);
|
|
|
|
}
|
2009-06-29 23:29:08 +02:00
|
|
|
} else {
|
2009-07-15 03:35:36 +02:00
|
|
|
if (quote) {
|
|
|
|
if (token >= TOK_KEYWORD && token < TOK_SYMBOL) {
|
|
|
|
buffer.printf("keyword \"%s\"", nameof(token));
|
|
|
|
} else {
|
|
|
|
buffer.printf("symbol \"%s\"", nameof(token));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
buffer.printf("%s", nameof(token));
|
|
|
|
}
|
2009-06-29 23:29:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-27 01:15:07 +02:00
|
|
|
void printToken(tokenid_t token) {
|
|
|
|
String buffer;
|
|
|
|
decodeToken(buffer, token, true);
|
|
|
|
fprintf(stderr, "%s\n", buffer.getUnwrapped());
|
|
|
|
}
|
|
|
|
|
2009-07-02 00:32:35 +02:00
|
|
|
bool checkSymbol(tokenid_t token) {
|
2009-06-29 23:29:08 +02:00
|
|
|
bool result = token >= TOK_SYMBOL;
|
2009-06-12 22:53:51 +02:00
|
|
|
if (!result) {
|
|
|
|
String temp;
|
2009-07-15 03:35:36 +02:00
|
|
|
decodeToken(temp, token, true);
|
2009-06-12 22:53:51 +02:00
|
|
|
error("Expected symbol. Got %s", temp.getUnwrapped());
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-07-01 03:09:56 +02:00
|
|
|
tokenid_t acceptSymbol() {
|
|
|
|
tokenid_t result = 0;
|
|
|
|
if (tok >= TOK_SYMBOL) {
|
|
|
|
result = tok;
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-06-05 04:56:13 +02:00
|
|
|
void globalDeclarations() {
|
2009-08-27 01:15:07 +02:00
|
|
|
mpCurrentSymbolStack = &mGlobals;
|
2009-06-05 04:56:13 +02:00
|
|
|
while (tok != EOF) {
|
2009-10-29 00:10:17 +01:00
|
|
|
Type* pBaseType = expectPrimitiveType(true);
|
2009-07-01 03:09:56 +02:00
|
|
|
if (!pBaseType) {
|
2009-06-12 22:53:51 +02:00
|
|
|
break;
|
|
|
|
}
|
2009-07-24 00:11:22 +02:00
|
|
|
Type* pDecl = expectDeclaration(pBaseType);
|
2009-07-01 03:09:56 +02:00
|
|
|
if (!pDecl) {
|
|
|
|
break;
|
|
|
|
}
|
2009-08-27 01:15:07 +02:00
|
|
|
if (!pDecl->id) {
|
|
|
|
skip(';');
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-09-08 20:06:40 +02:00
|
|
|
if (checkUndeclaredStruct(pDecl)) {
|
|
|
|
skip(';');
|
|
|
|
continue;
|
|
|
|
}
|
2009-07-01 03:09:56 +02:00
|
|
|
if (! isDefined(pDecl->id)) {
|
|
|
|
addGlobalSymbol(pDecl);
|
2009-06-12 20:25:59 +02:00
|
|
|
}
|
2009-07-01 03:09:56 +02:00
|
|
|
VariableInfo* name = VI(pDecl->id);
|
2009-06-12 20:25:59 +02:00
|
|
|
if (name && name->pAddress) {
|
2009-07-01 03:09:56 +02:00
|
|
|
error("Already defined global %s", nameof(pDecl->id));
|
2009-06-12 20:25:59 +02:00
|
|
|
}
|
2009-07-01 03:09:56 +02:00
|
|
|
if (pDecl->tag < TY_FUNC) {
|
2009-06-05 04:56:13 +02:00
|
|
|
// it's a variable declaration
|
|
|
|
for(;;) {
|
2009-10-29 00:10:17 +01:00
|
|
|
if (pDecl->storageClass == SC_TYPEDEF) {
|
|
|
|
// Do not allocate storage.
|
|
|
|
} else {
|
|
|
|
if (name && !name->pAddress) {
|
|
|
|
name->pAddress = (int*) allocGlobalSpace(
|
|
|
|
pGen->alignmentOf(name->pType),
|
|
|
|
pGen->sizeOf(name->pType));
|
|
|
|
}
|
|
|
|
if (accept('=')) {
|
|
|
|
if (tok == TOK_NUM) {
|
|
|
|
if (name) {
|
|
|
|
* (int*) name->pAddress = tokc;
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
} else {
|
|
|
|
error("Expected an integer constant");
|
2009-06-12 23:26:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-01 03:09:56 +02:00
|
|
|
if (!accept(',')) {
|
2009-06-05 04:56:13 +02:00
|
|
|
break;
|
2009-05-11 23:49:29 +02:00
|
|
|
}
|
2009-07-24 00:11:22 +02:00
|
|
|
pDecl = expectDeclaration(pBaseType);
|
2009-07-01 03:09:56 +02:00
|
|
|
if (!pDecl) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (! isDefined(pDecl->id)) {
|
|
|
|
addGlobalSymbol(pDecl);
|
|
|
|
}
|
|
|
|
name = VI(pDecl->id);
|
2009-05-11 23:49:29 +02:00
|
|
|
}
|
|
|
|
skip(';');
|
|
|
|
} else {
|
2009-07-01 03:09:56 +02:00
|
|
|
// Function declaration
|
2009-07-06 21:07:15 +02:00
|
|
|
if (accept(';')) {
|
|
|
|
// forward declaration.
|
2009-07-16 03:23:22 +02:00
|
|
|
} else if (tok != '{') {
|
|
|
|
error("expected '{'");
|
2009-07-06 21:07:15 +02:00
|
|
|
} else {
|
2009-07-24 00:11:22 +02:00
|
|
|
mpCurrentArena = &mLocalArena;
|
2009-08-27 01:15:07 +02:00
|
|
|
mpCurrentSymbolStack = &mLocals;
|
2009-07-06 21:07:15 +02:00
|
|
|
if (name) {
|
2009-07-30 01:22:26 +02:00
|
|
|
/* patch forward references */
|
|
|
|
pGen->resolveForward((int) name->pForward);
|
2009-07-06 21:07:15 +02:00
|
|
|
/* put function address */
|
2009-09-10 04:08:54 +02:00
|
|
|
name->pAddress = (void*) pCodeBuf->getPC();
|
2009-07-06 21:07:15 +02:00
|
|
|
}
|
|
|
|
// Calculate stack offsets for parameters
|
|
|
|
mLocals.pushLevel();
|
|
|
|
intptr_t a = 8;
|
|
|
|
int argCount = 0;
|
|
|
|
for (Type* pP = pDecl->pTail; pP; pP = pP->pTail) {
|
|
|
|
Type* pArg = pP->pHead;
|
2009-08-19 19:53:43 +02:00
|
|
|
if (pArg->id) {
|
|
|
|
addLocalSymbol(pArg);
|
|
|
|
}
|
2009-07-06 21:07:15 +02:00
|
|
|
/* read param name and compute offset */
|
2009-08-27 01:15:07 +02:00
|
|
|
Type* pPassingType = passingType(pArg);
|
|
|
|
size_t alignment = pGen->alignmentOf(pPassingType);
|
2009-07-10 07:00:24 +02:00
|
|
|
a = (a + alignment - 1) & ~ (alignment-1);
|
2009-08-19 19:53:43 +02:00
|
|
|
if (pArg->id) {
|
|
|
|
VI(pArg->id)->pAddress = (void*) a;
|
|
|
|
}
|
2009-08-27 01:15:07 +02:00
|
|
|
a = a + pGen->sizeOf(pPassingType);
|
2009-07-06 21:07:15 +02:00
|
|
|
argCount++;
|
|
|
|
}
|
|
|
|
rsym = loc = 0;
|
2009-07-07 23:48:51 +02:00
|
|
|
pReturnType = pDecl->pHead;
|
2009-07-10 07:00:24 +02:00
|
|
|
a = pGen->functionEntry(pDecl);
|
2009-07-06 21:07:15 +02:00
|
|
|
block(0, true);
|
|
|
|
pGen->gsym(rsym);
|
2009-07-10 07:00:24 +02:00
|
|
|
pGen->functionExit(pDecl, a, loc);
|
2009-07-06 21:07:15 +02:00
|
|
|
mLocals.popLevel();
|
2009-07-24 00:11:22 +02:00
|
|
|
mpCurrentArena = &mGlobalArena;
|
2009-08-27 01:15:07 +02:00
|
|
|
mpCurrentSymbolStack = &mGlobals;
|
2009-05-11 23:49:29 +02:00
|
|
|
}
|
2009-05-08 23:54:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-05-11 23:49:29 +02:00
|
|
|
|
2009-08-27 01:15:07 +02:00
|
|
|
Type* passingType(Type* pType) {
|
|
|
|
switch (pType->tag) {
|
|
|
|
case TY_CHAR:
|
|
|
|
case TY_SHORT:
|
|
|
|
return mkpInt;
|
|
|
|
default:
|
|
|
|
return pType;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-09 01:48:41 +02:00
|
|
|
char* allocGlobalSpace(size_t alignment, size_t bytes) {
|
|
|
|
size_t base = (((size_t) glo) + alignment - 1) & ~(alignment-1);
|
|
|
|
size_t end = base + bytes;
|
2009-07-09 05:40:31 +02:00
|
|
|
if ((end - (size_t) pGlobalBase) > (size_t) ALLOC_SIZE) {
|
2009-05-30 03:03:15 +02:00
|
|
|
error("Global space exhausted");
|
2009-08-27 01:15:07 +02:00
|
|
|
assert(false);
|
2009-06-11 19:53:51 +02:00
|
|
|
return NULL;
|
2009-05-30 03:03:15 +02:00
|
|
|
}
|
2009-07-09 01:48:41 +02:00
|
|
|
char* result = (char*) base;
|
|
|
|
glo = (char*) end;
|
2009-05-30 03:03:15 +02:00
|
|
|
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-09-10 04:08:54 +02:00
|
|
|
if (pCodeBuf) {
|
|
|
|
delete pCodeBuf;
|
|
|
|
pCodeBuf = 0;
|
|
|
|
}
|
2009-05-22 21:06:27 +02:00
|
|
|
if (file) {
|
|
|
|
delete file;
|
|
|
|
file = 0;
|
|
|
|
}
|
2009-05-11 23:49:29 +02:00
|
|
|
}
|
|
|
|
|
2009-07-15 06:14:10 +02:00
|
|
|
// One-time initialization, when class is constructed.
|
|
|
|
void init() {
|
|
|
|
mpSymbolLookupFn = 0;
|
|
|
|
mpSymbolLookupContext = 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;
|
2009-10-28 01:23:20 +01:00
|
|
|
macroLevel = -1;
|
2009-05-11 23:49:29 +02:00
|
|
|
file = 0;
|
|
|
|
pGlobalBase = 0;
|
2009-09-10 04:08:54 +02:00
|
|
|
pCodeBuf = 0;
|
2009-05-11 23:49:29 +02:00
|
|
|
pGen = 0;
|
2009-06-05 01:23:40 +02:00
|
|
|
mPragmaStringCount = 0;
|
2009-07-16 23:30:33 +02:00
|
|
|
mCompileResult = 0;
|
2009-07-17 01:50:56 +02:00
|
|
|
mLineNumber = 1;
|
|
|
|
mbBumpLine = false;
|
2009-08-19 03:25:56 +02:00
|
|
|
mbSuppressMacroExpansion = false;
|
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;
|
|
|
|
|
2009-09-10 04:08:54 +02:00
|
|
|
delete pCodeBuf;
|
|
|
|
pCodeBuf = new CodeBuf();
|
|
|
|
|
2009-05-13 19:58:45 +02:00
|
|
|
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-09-10 04:08:54 +02:00
|
|
|
pCodeBuf = new ARMCodeBuf(pCodeBuf);
|
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-09-10 04:08:54 +02:00
|
|
|
pCodeBuf = new ARMCodeBuf(pCodeBuf);
|
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-07-14 03:40:08 +02:00
|
|
|
pGen->setTypes(mkpInt);
|
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-07-15 06:14:10 +02:00
|
|
|
init();
|
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-07-15 06:14:10 +02:00
|
|
|
void registerSymbolCallback(ACCSymbolLookupFn pFn, ACCvoid* pContext) {
|
|
|
|
mpSymbolLookupFn = pFn;
|
|
|
|
mpSymbolLookupContext = pContext;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2009-07-24 00:11:22 +02:00
|
|
|
mpCurrentArena = &mGlobalArena;
|
2009-07-14 03:40:08 +02:00
|
|
|
createPrimitiveTypes();
|
2009-06-11 19:53:51 +02:00
|
|
|
cleanup();
|
|
|
|
clear();
|
2009-06-29 23:29:08 +02:00
|
|
|
mTokenTable.setArena(&mGlobalArena);
|
|
|
|
mGlobals.setArena(&mGlobalArena);
|
|
|
|
mGlobals.setTokenTable(&mTokenTable);
|
|
|
|
mLocals.setArena(&mLocalArena);
|
|
|
|
mLocals.setTokenTable(&mTokenTable);
|
|
|
|
|
|
|
|
internKeywords();
|
2009-06-11 19:53:51 +02:00
|
|
|
setArchitecture(NULL);
|
|
|
|
if (!pGen) {
|
|
|
|
return -1;
|
|
|
|
}
|
2009-06-12 06:12:23 +02:00
|
|
|
#ifdef PROVIDE_TRACE_CODEGEN
|
|
|
|
pGen = new TraceCodeGenerator(pGen);
|
|
|
|
#endif
|
2009-09-10 04:08:54 +02:00
|
|
|
pGen->setErrorSink(this);
|
|
|
|
|
|
|
|
if (pCodeBuf) {
|
|
|
|
pCodeBuf->init(ALLOC_SIZE);
|
|
|
|
}
|
|
|
|
pGen->init(pCodeBuf);
|
2009-06-11 19:53:51 +02:00
|
|
|
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-07-16 23:30:33 +02:00
|
|
|
mCompileResult = result;
|
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-07-01 03:09:56 +02:00
|
|
|
void createPrimitiveTypes() {
|
2009-07-24 00:11:22 +02:00
|
|
|
mkpInt = createType(TY_INT, NULL, NULL);
|
2009-08-03 23:42:57 +02:00
|
|
|
mkpShort = createType(TY_SHORT, NULL, NULL);
|
2009-07-24 00:11:22 +02:00
|
|
|
mkpChar = createType(TY_CHAR, NULL, NULL);
|
|
|
|
mkpVoid = createType(TY_VOID, NULL, NULL);
|
|
|
|
mkpFloat = createType(TY_FLOAT, NULL, NULL);
|
|
|
|
mkpDouble = createType(TY_DOUBLE, NULL, NULL);
|
|
|
|
mkpIntFn = createType(TY_FUNC, mkpInt, NULL);
|
|
|
|
mkpIntPtr = createPtrType(mkpInt);
|
|
|
|
mkpCharPtr = createPtrType(mkpChar);
|
|
|
|
mkpFloatPtr = createPtrType(mkpFloat);
|
|
|
|
mkpDoublePtr = createPtrType(mkpDouble);
|
|
|
|
mkpPtrIntFn = createPtrType(mkpIntFn);
|
2009-07-01 03:09:56 +02:00
|
|
|
}
|
|
|
|
|
2009-06-12 20:25:59 +02:00
|
|
|
void checkForUndefinedForwardReferences() {
|
2009-06-29 23:29:08 +02:00
|
|
|
mGlobals.forEach(static_ufrcFn, this);
|
2009-06-12 20:25:59 +02:00
|
|
|
}
|
|
|
|
|
2009-06-29 23:29:08 +02:00
|
|
|
static bool static_ufrcFn(VariableInfo* value, void* context) {
|
2009-06-12 20:25:59 +02:00
|
|
|
Compiler* pCompiler = (Compiler*) context;
|
2009-06-29 23:29:08 +02:00
|
|
|
return pCompiler->undefinedForwardReferenceCheck(value);
|
2009-06-12 20:25:59 +02:00
|
|
|
}
|
|
|
|
|
2009-06-29 23:29:08 +02:00
|
|
|
bool undefinedForwardReferenceCheck(VariableInfo* value) {
|
2009-06-12 20:25:59 +02:00
|
|
|
if (!value->pAddress && value->pForward) {
|
2009-06-29 23:29:08 +02:00
|
|
|
error("Undefined forward reference: %s",
|
|
|
|
mTokenTable[value->tok].pText);
|
2009-06-12 20:25:59 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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-07-16 23:30:33 +02:00
|
|
|
if (mCompileResult == 0) {
|
|
|
|
tokenid_t tok = mTokenTable.intern(name, strlen(name));
|
|
|
|
VariableInfo* pVariableInfo = VI(tok);
|
|
|
|
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-09-09 22:19:34 +02:00
|
|
|
void getProgramBinary(ACCvoid** base, ACCsizei* length) {
|
2009-09-10 04:08:54 +02:00
|
|
|
*base = pCodeBuf->getBase();
|
|
|
|
*length = (ACCsizei) pCodeBuf->getSize();
|
2009-09-09 22:19:34 +02:00
|
|
|
}
|
|
|
|
|
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_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-07-15 06:14:10 +02:00
|
|
|
void registerSymbolCallback(ACCSymbolLookupFn pFn, ACCvoid* pContext) {
|
|
|
|
compiler.registerSymbolCallback(pFn, pContext);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-07-15 06:14:10 +02:00
|
|
|
extern "C"
|
|
|
|
void accRegisterSymbolCallback(ACCscript* script, ACCSymbolLookupFn pFn,
|
|
|
|
ACCvoid* pContext) {
|
|
|
|
script->registerSymbolCallback(pFn, pContext);
|
|
|
|
}
|
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
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';
|
2009-09-08 20:06:40 +02:00
|
|
|
|
|
|
|
#ifdef DEBUG_SAVE_INPUT_TO_FILE
|
2009-09-08 20:46:42 +02:00
|
|
|
LOGD("Saving input to file...");
|
2009-09-08 20:06:40 +02:00
|
|
|
int counter;
|
|
|
|
char path[PATH_MAX];
|
|
|
|
for (counter = 0; counter < 4096; counter++) {
|
|
|
|
sprintf(path, DEBUG_DUMP_PATTERN, counter);
|
|
|
|
if(access(path, F_OK) != 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (counter < 4096) {
|
2009-09-08 20:46:42 +02:00
|
|
|
LOGD("Saving input to file %s", path);
|
2009-09-08 20:06:40 +02:00
|
|
|
FILE* fd = fopen(path, "w");
|
|
|
|
if (fd) {
|
|
|
|
fwrite(text, totalLength, 1, fd);
|
|
|
|
fclose(fd);
|
2009-09-08 20:46:42 +02:00
|
|
|
LOGD("Saved input to file %s", path);
|
|
|
|
} else {
|
|
|
|
LOGD("Could not save. errno: %d", errno);
|
2009-09-08 20:06:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2009-05-22 21:06:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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-06-18 04:13:52 +02:00
|
|
|
extern "C"
|
2009-09-09 22:19:34 +02:00
|
|
|
void accGetProgramBinary(ACCscript* script,
|
|
|
|
ACCvoid** base, ACCsizei* length) {
|
|
|
|
script->compiler.getProgramBinary(base, length);
|
2009-06-18 04:13:52 +02:00
|
|
|
}
|
|
|
|
|
2009-06-05 01:23:40 +02:00
|
|
|
|
2009-05-22 21:06:27 +02:00
|
|
|
} // namespace acc
|
|
|
|
|