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