123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- #ifndef __arithmetic_h__
- #define __arithmetic_h__
- typedef unsigned char byte;
- #define INST_TYPES \
- X(MOV) \
- X(LOAD) \
- X(STORE) \
- X(ADD) \
- X(SUB) \
- X(MUL) \
- X(DIV) \
- enum InstructionType {
- #define X(x) INST_##x,
- INST_TYPES
- #undef X
- };
- char* InstructionTypeNames[] = {
- #define X(x) [INST_##x] = #x,
- INST_TYPES
- #undef X
- };
- // Argument passing:
- // rdi, rsi, rdx, rcx, r8, r9, ....stack
- typedef struct AssemblerInfo {
- int type;
- unsigned char opcodes[3];
- char numOpcodes;
- char dstIsRM;
- char* textName;
-
- // int alignment;
- } AssemblerInfo;
- #define TOKEN_TYPES \
- X(NONE) \
- \
- X(ADD) \
- X(SUB) \
- X(MUL) \
- X(DIV) \
- X(MIN) \
- X(MAX) \
- X(SQRT) \
- X(RECIP) \
- \
- X(IMMED) \
- X(CONST) \
- X(VAR) \
-
-
- enum TokType {
- #define X(x) T_##x,
- TOKEN_TYPES
- #undef X
- };
- // AST Node
- typedef struct Node {
- char* text;
- enum TokType type;
- struct Node* l, *r;
-
- int resultVar;
-
- } Node;
- // context struct for the reg alloc algorithm
- typedef struct RegAllocInfo {
- int nextVar;
- } RegAllocInfo;
- typedef struct ThreeAddressInstr {
- char* text;
- enum TokType type;
- int out;
- int inL, inR;
- } ThreeAddressInstr;
- typedef struct VarDef {
- int varNum;
- int slot;
- char slotType; // 0 = temp/var, 1 = input, 2 = const
- float constVal;
- char* varTextName;
- int regNum;
- int assignmentInst;
- int lastUsedInst;
- } VarDef;
- typedef struct SlotInfo {
- char* textName;
- int varNum;
- size_t byteOffset;
- size_t elemSize;
- } SlotInfo;
- typedef struct AsmInst {
- int instID;
- short dstReg; // register num or slotType
- short srcReg;
- char dstType; // 0 = reg, 1 = mem
- char srcType; // 0 = reg, 1 = mem
- size_t memOffset;
-
- unsigned char* machineCode;
- int mcLen;
- } AsmInst;
- typedef struct LinearizationInfo {
- VEC(ThreeAddressInstr*) ilist;
- int varCnt;
- int inputSlotCnt;
- int constSlotCnt;
- int tempSlotCnt;
-
- VarDef* vars;
- SlotInfo* inputSlots;
- SlotInfo* constSlots;
- SlotInfo* tempSlots;
-
- VEC(int) freeRegStack;
-
- VEC(AsmInst) code;
-
- } LinearizationInfo;
- void linearizeAST(Node* n, LinearizationInfo* info);
- void setupSlots(LinearizationInfo* info);
- void assembleCode(LinearizationInfo* info);
- void generateCode(LinearizationInfo* info);
- typedef void (*arithmetic_fn)(float* /*in*/, float* /*out*/, float* /*constants*/);
- typedef struct ProgramInfo {
- size_t mcLen;
- arithmetic_fn fn;
-
- int width; // SIMD width. 4 for now with SSE *ps instructions
-
- float* constants;
- } ProgramInfo;
- ProgramInfo* compile(char** source, size_t slen);
- #endif // __arithmetic_h__
|