arithmetic.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #ifndef __arithmetic_h__
  2. #define __arithmetic_h__
  3. typedef unsigned char byte;
  4. #define INST_TYPES \
  5. X(MOV) \
  6. X(LOAD) \
  7. X(STORE) \
  8. X(ADD) \
  9. X(SUB) \
  10. X(MUL) \
  11. X(DIV) \
  12. enum InstructionType {
  13. #define X(x) INST_##x,
  14. INST_TYPES
  15. #undef X
  16. };
  17. char* InstructionTypeNames[] = {
  18. #define X(x) [INST_##x] = #x,
  19. INST_TYPES
  20. #undef X
  21. };
  22. // Argument passing:
  23. // rdi, rsi, rdx, rcx, r8, r9, ....stack
  24. typedef struct AssemblerInfo {
  25. int type;
  26. unsigned char opcodes[3];
  27. char numOpcodes;
  28. char dstIsRM;
  29. char* textName;
  30. // int alignment;
  31. } AssemblerInfo;
  32. #define TOKEN_TYPES \
  33. X(NONE) \
  34. \
  35. X(ADD) \
  36. X(SUB) \
  37. X(MUL) \
  38. X(DIV) \
  39. X(MIN) \
  40. X(MAX) \
  41. X(SQRT) \
  42. X(RECIP) \
  43. \
  44. X(IMMED) \
  45. X(CONST) \
  46. X(VAR) \
  47. enum TokType {
  48. #define X(x) T_##x,
  49. TOKEN_TYPES
  50. #undef X
  51. };
  52. // AST Node
  53. typedef struct Node {
  54. char* text;
  55. enum TokType type;
  56. struct Node* l, *r;
  57. int resultVar;
  58. } Node;
  59. // context struct for the reg alloc algorithm
  60. typedef struct RegAllocInfo {
  61. int nextVar;
  62. } RegAllocInfo;
  63. typedef struct ThreeAddressInstr {
  64. char* text;
  65. enum TokType type;
  66. int out;
  67. int inL, inR;
  68. } ThreeAddressInstr;
  69. typedef struct VarDef {
  70. int varNum;
  71. int slot;
  72. char slotType; // 0 = temp/var, 1 = input, 2 = const
  73. float constVal;
  74. char* varTextName;
  75. int regNum;
  76. int assignmentInst;
  77. int lastUsedInst;
  78. } VarDef;
  79. typedef struct SlotInfo {
  80. char* textName;
  81. int varNum;
  82. size_t byteOffset;
  83. size_t elemSize;
  84. } SlotInfo;
  85. typedef struct AsmInst {
  86. int instID;
  87. short dstReg; // register num or slotType
  88. short srcReg;
  89. char dstType; // 0 = reg, 1 = mem
  90. char srcType; // 0 = reg, 1 = mem
  91. size_t memOffset;
  92. unsigned char* machineCode;
  93. int mcLen;
  94. } AsmInst;
  95. typedef struct LinearizationInfo {
  96. VEC(ThreeAddressInstr*) ilist;
  97. int varCnt;
  98. int inputSlotCnt;
  99. int constSlotCnt;
  100. int tempSlotCnt;
  101. VarDef* vars;
  102. SlotInfo* inputSlots;
  103. SlotInfo* constSlots;
  104. SlotInfo* tempSlots;
  105. VEC(int) freeRegStack;
  106. VEC(AsmInst) code;
  107. } LinearizationInfo;
  108. void linearizeAST(Node* n, LinearizationInfo* info);
  109. void setupSlots(LinearizationInfo* info);
  110. void assembleCode(LinearizationInfo* info);
  111. void generateCode(LinearizationInfo* info);
  112. typedef void (*arithmetic_fn)(float* /*in*/, float* /*out*/, float* /*constants*/);
  113. typedef struct ProgramInfo {
  114. size_t mcLen;
  115. arithmetic_fn fn;
  116. int width; // SIMD width. 4 for now with SSE *ps instructions
  117. float* constants;
  118. } ProgramInfo;
  119. ProgramInfo* compile(char** source, size_t slen);
  120. #endif // __arithmetic_h__