2 #ifndef HeavyOSECPU_jitc_h
3 #define HeavyOSECPU_jitc_h
8 #define JITC_ERR_MASK 255
9 #define JITC_ERR_PHASE0ONLY 256
10 #define JITC_ERR_REGNUM (1 | JITC_ERR_PHASE0ONLY)
11 #define JITC_ERR_DST1 (2 | JITC_ERR_PHASE0ONLY)
12 #define JITC_ERR_OPECODE (3 | JITC_ERR_PHASE0ONLY)
13 #define JITC_ERR_LABELNUM (4 | JITC_ERR_PHASE0ONLY)
14 #define JITC_ERR_LABELREDEF (5 | JITC_ERR_PHASE0ONLY)
15 #define JITC_ERR_PREFIX (6 | JITC_ERR_PHASE0ONLY)
16 #define JITC_ERR_LABELNODEF 7
17 #define JITC_ERR_LABELTYP 8
18 #define JITC_ERR_IDIOM 9
19 #define JITC_ERR_PREGNUM (10 | JITC_ERR_PHASE0ONLY)
20 #define JITC_ERR_SRC1 (11 | JITC_ERR_PHASE0ONLY)
21 #define JITC_ERR_BADTYPE (12 | JITC_ERR_PHASE0ONLY)
22 #define JITC_ERR_PREFIXFAR (13 | JITC_ERR_PHASE0ONLY)
23 #define JITC_ERR_INTERNAL 99
26 #define jitCompPutByte1(p, c0) *p++ = c0
27 #define jitCompPutByte2(p, c0, c1) *p++ = c0; *p++ = c1
28 #define jitCompPutByte3(p, c0, c1, c2) *p++ = c0; *p++ = c1; *p++ = c2
29 #define jitCompPutByte4(p, c0, c1, c2, c3) *p++ = c0; *p++ = c1; *p++ = c2; *p++ = c3
32 // functions (jitc internal)
36 void errorHandler(HOSECPU_RuntimeEnvironment *r);
37 int jitCompCmdLen(const unsigned char *src);
40 #if (JITC_ARCNUM == 0x0001)
44 #define IA32_REG0_EAX 0
45 #define IA32_REG1_ECX 1
46 #define IA32_REG2_EDX 2
47 #define IA32_REG3_EBX 3
48 #define IA32_REG4_ESP 4
49 #define IA32_REG5_EBP 5
50 #define IA32_REG6_ESI 6
51 #define IA32_REG7_EDI 7
53 #define IA32_MOD_R_M(mod, reg, rm) ((mod << 6) | reg << 3 | rm)
55 #define IA32_OP_MOD_INDEXONLY 0
56 #define IA32_OP_MOD_INDEX_AND_DISP_BYTE 1
57 #define IA32_OP_MOD_INDEX_AND_DISP_FULL 2
58 #define IA32_OP_MOD_REGISTER 3
60 #define IA32_OP_RM32_MOD00_ADDR_DISP32 5
62 #define envOffset_DBGINFO0 (2304 + 0)
63 #define envOffset_DBGINFO1 (2304 + 4)
64 #define envOffset_DBGCURRENTCODE (2304 + 8)
65 #define envOffset_PTRCTRL (2320)
66 #define PRegOffset(regid) (256 + 32 * regid)
68 #define jitCompPutImm32(p, i) jitCompPutByte4(p, ((i) & 0xff), (((i) >> 8) & 0xff), (((i) >> 16) & 0xff), (((i) >> 24) & 0xff))
70 #define jitCompPutOp_ADD_GReg_Imm8(p, dReg, i) jitCompPutByte3(p, 0x83, 0xc0 | dReg, i); /* ADD(reg0, imm8); == [1000 0011] [11000 reg] imm8 */
71 #define jitCompPutOp_XOR_GReg_GReg(p, d, s) jitCompPutByte2(w.dst, 0x31, 0xc0 | (s) << 3 | (d));
72 #define jitCompPutOp_MOV_GReg_Imm32(p, dReg, i) jitCompPutByte1(p, 0xb8 | dReg); jitCompPutImm32(p, i); /* MOV(reg0, imm32); == [1011 1 reg] imm32 */
73 #define jitCompPutOp_PUSHAD(p) jitCompPutByte1(p, 0x60);
74 #define jitCompPutOp_POPAD(p) jitCompPutByte1(p, 0x61);
75 #define jitCompPutOp_PUSH_GReg(p, reg) jitCompPutByte1(p, 0x50 | (reg));
76 #define jitCompPutOp_PUSH_Imm8(p, i) jitCompPutByte2(p, 0x6a, i);
77 #define jitCompPutOp_POP_GReg(p, reg) jitCompPutByte1(p, 0x58 | (reg));
78 #define jitCompPutOp_CALL_Relative(p, diff) jitCompPutByte1(p, 0xe8); jitCompPutImm32(p, diff); /*次の命令との相対オフセットだけ相対コールする*/
79 #define jitCompPutOp_JMPnear(p, diff) jitCompPutByte1(p, 0xe9); jitCompPutImm32(p, diff); /*次の命令との相対オフセットだけ相対ジャンプする*/
80 #define jitCompPutOp_JMPshort(p, diff) jitCompPutByte2(p, 0xeb, diff & 0xff);/*次の命令との相対オフセットだけ相対ジャンプする*/
81 #define jitCompPutOp_INT3(p) jitCompPutByte1(p, 0xCC);
83 #define jitCompPutOp_MOV_EAX_ZERO(p) jitCompPutOp_XOR_GReg_GReg(p, IA32_REG0_EAX, IA32_REG0_EAX);
85 #define DEBUGCode(work, code) jitCompPutOp_MOV_GReg_Imm32((work)->dst, IA32_REG0_EAX, code); jitCompPutOp_MOV_EBPDisp_GReg(work, envOffset_DBGCURRENTCODE, IA32_REG0_EAX);
86 // Optimization settings
88 // 以下の定数は最適化のためのものなので、すべて0として簡単に移植しても問題ありません
89 #define jitCompA0001_USE_R3F_CMPJMP 1*1
90 #define jitCompA0001_USE_R3F_IMM32 1*1
91 #define jitCompA0001_USE_R3F_IMM8 1*1
92 #define jitCompA0001_USE_R3F_INCDEC 1*1
93 #define jitCompA0001_OPTIMIZE_JMP 1*1
94 #define jitCompA0001_OPTIMIZE_MOV 1*1 /* 1にすると速度低下する? */
95 #define jitCompA0001_OPTIMIZE_CMP 1*1
96 #define jitCompA0001_OPTIMIZE_ALIGN 8 /* 0-8を想定 */
97 #define jitCompA0001_EBP128 128 // 0にもできる
100 unsigned char *dst, *dst0;
102 #if (jitCompA0001_USE_R3F_IMM32 != 0)
105 char prefix; //CND命令の値を記録(初期値=0)
109 int jitCompGetImm32(const unsigned char *src);
110 int jitCompGetLabelNum(struct JitCompWork *w, const unsigned char *src);
111 void jitCompPutModRM_Disp_BaseEBP(struct JitCompWork *w, int disp, int opReg);
112 void jitCompPutOp_MOV_EBPDisp_GReg(struct JitCompWork *w, int disp, int reg32);
113 void jitCompPutOp_MOV_GReg_EBPDisp(struct JitCompWork *w, int reg32, int disp);
114 void jitCompA0001_movEaxRxx(struct JitCompWork *w, int rxx);
115 void jitCompA0001_movRxxEax(struct JitCompWork *w, int rxx);
116 void jitCompA0001_fixPrefix(struct JitCompWork *w);
117 void jitCompA0001_checkCompPtr(struct JitCompWork *w, int p0, int p1);
118 void jitCompA000_loadRegCacheAll(struct JitCompWork *w);
119 void jitCompA000_storeRegCacheAll(struct JitCompWork *w);
120 void jitCompA000_loadRegCacheEcx(struct JitCompWork *w);
121 void jitCompA000_storeRegCacheEcx(struct JitCompWork *w);
122 void jitCompA000_loadRegCacheEdx(struct JitCompWork *w);
123 void jitCompA000_storeRegCacheEdx(struct JitCompWork *w);
124 int jitCompA000_selectRegCache(int rxx, int reg);
125 void jitCompA000_loadPRegCacheAll(struct JitCompWork *w);
126 void jitCompA000_storePRegCacheAll(struct JitCompWork *w);
127 int jitCompA000_selectPRegCache(int pxx, int reg);
128 int jitCompA000_convTyp(int t);
129 int jitCompA000_dataWidth(int t);
130 void jitCompA0001_checkType0(struct JitCompWork *w, int pxx, int typ, int ac);
131 void jitCompA0001_checkType(struct JitCompWork *w, int pxx, int typ, int ac);
132 void jitCompA0001_checkLimit(struct JitCompWork *w, int reg, int pxx);
135 extern unsigned char *errfnc;
136 int jitCompiler(unsigned char *dst, unsigned char *dst1, const unsigned char *src, const unsigned char *src1, const unsigned char *src0, HOSECPU_LabelListTag *label, int maxLabels, int level, int debugInfo1, int flags);
137 unsigned char *jitCompCallFunc(unsigned char *dst, void *func);
138 unsigned char *jitCompInit(unsigned char *dst);
139 void func3c(char *ebp, int opt, int r1, int p1, int lenR, int lenP, int r0, int p0);
140 void func3d(char *ebp, int opt, int r1, int p1, int lenR, int lenP, int r0, int p0);
141 void funcf4(char *ebp, int pxx, int typ, int len);
142 void funcf5(char *ebp, int pxx, int typ, int len);
143 void funcf6(char *ebp, int pxx, int typ, int len);
144 void funcf7(char *ebp, int pxx, int typ, int len);
145 void errHndl(HOSECPU_RuntimeEnvironment *r);
146 int jitc0(unsigned char **qq, unsigned char *q1, const unsigned char *p0, const unsigned char *p1, int level, HOSECPU_LabelListTag *label);
147 #if (USE_DEBUGGER != 0)
148 int dbgrGetRegNum(const char *p);
149 void dbgrMain(HOSECPU_RuntimeEnvironment *r);