4 #if (JITC_ARCNUM == 0x0001)
8 int jitCompGetImm32(const unsigned char *src)
10 return (src[0] << 24) | (src[1] << 16) | (src[2] << 8) | src[3];
13 int jitCompGetLabelNum(struct JitCompWork *w, const unsigned char *src)
15 int i = jitCompGetImm32(src);
16 if (i < 0 || i >= w->maxLabels) {
17 w->err = JITC_ERR_LABELNUM;
23 void jitCompPutModRM_Disp_BaseEBP(struct JitCompWork *w, int disp, int opReg)
25 // EBPをベースとするアドレスオペランドとレジスタオペランドを出力する
26 disp -= jitCompA0001_EBP128;
27 if (-128 <= disp && disp <= 127) {
29 jitCompPutByte1(w->dst, IA32_MOD_R_M(IA32_OP_MOD_INDEX_AND_DISP_BYTE, opReg, IA32_REG5_EBP));
30 jitCompPutByte1(w->dst, disp & 0xff);
33 jitCompPutByte1(w->dst, IA32_MOD_R_M(IA32_OP_MOD_INDEX_AND_DISP_FULL, opReg, IA32_REG5_EBP));
34 jitCompPutImm32(w->dst, disp);
39 void jitCompPutOp_MOV_EBPDisp_GReg(struct JitCompWork *w, int disp, int reg32)
41 // MOV([EBP + disp] <- reg32);
43 // [1000 100 1] [mod reg r/m]
44 jitCompPutByte1(w->dst, 0x89);
45 jitCompPutModRM_Disp_BaseEBP(w, disp, reg32);
49 void jitCompPutOp_MOV_GReg_EBPDisp(struct JitCompWork *w, int reg32, int disp)
51 // MOV (reg32 <- [EBP + disp])
53 // [1000 101 1] [mod reg r/m]
54 jitCompPutByte1(w->dst, 0x8b);
55 jitCompPutModRM_Disp_BaseEBP(w, disp, reg32);
59 void jitCompA0001_movEaxRxx(struct JitCompWork *w, int rxx)
61 #if (jitCompA0001_USE_R3F_IMM32 != 0)
63 jitCompPutByte1(w->dst, 0xb8); /* MOV(EAX, ?); */
64 jitCompPutImm32(w->dst, w->r3f);
68 if (rxx >= 0x40 || rxx < 0){
69 w->err = JITC_ERR_REGNUM;
71 jitCompPutOp_MOV_GReg_EBPDisp(w, IA32_REG0_EAX, rxx * 4);
75 void jitCompA0001_movRxxEax(struct JitCompWork *w, int rxx)
77 if (rxx >= 0x40 || rxx < 0){
78 w->err = JITC_ERR_REGNUM;
80 jitCompPutOp_MOV_EBPDisp_GReg(w, rxx * 4, IA32_REG0_EAX);
84 void jitCompA0001_fixPrefix(struct JitCompWork *w)
87 if (w->dst - w->dst0 > 127){
88 w->err = JITC_ERR_REGNUM;
90 w->dst0[-1] = (unsigned char)((w->dst - w->dst0) & 0xff);
95 void jitCompA0001_checkCompPtr(struct JitCompWork *w, int p0, int p1)
97 if (p0 >= 0x3f || p0 < 0){
98 w->err = JITC_ERR_PREGNUM;
100 if (p1 >= 0x3f || p1 < 0){
101 w->err = JITC_ERR_PREGNUM;
103 /* 比較可能可能なのかのチェックのコードを出力 */ /* 未完成 */
107 void jitCompA000_loadRegCacheAll(struct JitCompWork *w)
109 //保存されたレジスタキャッシュをメモリからロード
110 jitCompPutOp_MOV_GReg_EBPDisp(w, IA32_REG3_EBX, 4 * 0); /* EBX = R00; */
111 jitCompPutOp_MOV_GReg_EBPDisp(w, IA32_REG1_ECX, 4 * 1); /* ECX = R01; */
112 jitCompPutOp_MOV_GReg_EBPDisp(w, IA32_REG2_EDX, 4 * 2); /* EDX = R02; */
116 void jitCompA000_storeRegCacheAll(struct JitCompWork *w)
119 jitCompPutOp_MOV_EBPDisp_GReg(w, 0 * 4, IA32_REG3_EBX); /* R00 = EBX; */
120 jitCompPutOp_MOV_EBPDisp_GReg(w, 1 * 4, IA32_REG1_ECX); /* R01 = ECX; */
121 jitCompPutOp_MOV_EBPDisp_GReg(w, 2 * 4, IA32_REG2_EDX); /* R02 = EDX; */
125 void jitCompA000_loadRegCacheEcx(struct JitCompWork *w)
127 jitCompPutOp_MOV_GReg_EBPDisp(w, IA32_REG1_ECX, 1 * 4); /* ECX = R01; */
131 void jitCompA000_storeRegCacheEcx(struct JitCompWork *w)
133 jitCompPutOp_MOV_EBPDisp_GReg(w, 1 * 4, IA32_REG1_ECX); /* R01 = ECX; */
137 void jitCompA000_loadRegCacheEdx(struct JitCompWork *w)
139 jitCompPutOp_MOV_GReg_EBPDisp(w, IA32_REG2_EDX, 2 * 4); /* EDX = R02; */
143 void jitCompA000_storeRegCacheEdx(struct JitCompWork *w)
145 jitCompPutOp_MOV_EBPDisp_GReg(w, 2 * 4, IA32_REG2_EDX); /* R02 = EDX; */
149 int jitCompA000_selectRegCache(int rxx, int reg)
151 // OSECPUレジスタ番号をIA32レジスタ番号へ変換
152 // 対応するキャッシュレジスタがない場合regが返る
167 void jitCompA000_loadPRegCacheAll(struct JitCompWork *w)
169 jitCompPutOp_MOV_GReg_EBPDisp(w, IA32_REG6_ESI, PRegOffset(0x01) + 0); /* ESI = P01; */
170 jitCompPutOp_MOV_GReg_EBPDisp(w, IA32_REG7_EDI, PRegOffset(0x02) + 0); /* EDI = P02; */
174 void jitCompA000_storePRegCacheAll(struct JitCompWork *w)
176 jitCompPutOp_MOV_EBPDisp_GReg(w, PRegOffset(0x01) + 0, IA32_REG6_ESI); /* P01 = ESI; */
177 jitCompPutOp_MOV_EBPDisp_GReg(w, PRegOffset(0x02) + 0, IA32_REG7_EDI); /* P02 = EDI; */
181 int jitCompA000_selectPRegCache(int pxx, int reg)
183 // if (pxx == 0) reg = 5; /* EBP */
198 int jitCompA000_convTyp(int t)
201 // unsigned なら奇数(1bit目がOn)
203 // retv = 2 or 3: BYTE ( 8, 4, 2, 1 bit)
204 // retv = 4 or 5: WORD (16, 12 bit)
205 // retv = 6 or 7: DWORD(32, 20, 24, 28 bit)
209 if (1 <= t && t <= 7){
212 } else if (8 <= t && t <= 13){
214 } else if (14 <= t && t <= 15){
216 } else if (16 <= t && t <= 21){
222 int jitCompA000_dataWidth(int t)
226 if (t == 0x0001) r = 256;
228 if (t == 0x0002 / 2) r = 8;
229 if (t == 0x0004 / 2) r = 16;
230 if (t == 0x0006 / 2) r = 32;
231 if (t == 0x0008 / 2) r = 4;
232 if (t == 0x000a / 2) r = 2;
233 if (t == 0x000c / 2) r = 1;
234 if (t == 0x000e / 2) r = 12;
235 if (t == 0x0010 / 2) r = 20;
236 if (t == 0x0012 / 2) r = 24;
237 if (t == 0x0014 / 2) r = 28;
241 unsigned char *errfnc;
243 void jitCompA0001_checkType0(struct JitCompWork *w, int pxx, int typ, int ac)
245 if (typ <= 0) { w->err = JITC_ERR_BADTYPE; }
246 if (typ > 0x7f) { w->err = JITC_ERR_INTERNAL; }
247 jitCompPutOp_MOV_GReg_EBPDisp(w, IA32_REG0_EAX, PRegOffset(pxx) + 4); /* MOV(EAX, [EBP+?]); */ /* typ */
248 jitCompPutByte3(w->dst, 0x83, 0xf8, typ & 0x7f); /* CMP(EAX, ?); */
249 jitCompPutByte2(w->dst, 0x0f, 0x85); /* JNE */
250 jitCompPutImm32(w->dst, errfnc - (w->dst + 4));
254 void jitCompA0001_checkType(struct JitCompWork *w, int pxx, int typ, int ac)
257 // 将来的にはaliveやアクセス権チェックも入れる
258 jitCompA0001_checkType0(w, pxx, typ, ac);
262 void jitCompA0001_checkLimit(struct JitCompWork *w, int reg, int pxx)
264 jitCompPutByte1(w->dst, 0x3b); /* CMP(reg, [EBP+?]); */
265 jitCompPutModRM_Disp_BaseEBP(w, PRegOffset(pxx) + 8, reg); /* p0 */
266 jitCompPutByte2(w->dst, 0x0f, 0x82); /* JB */
267 jitCompPutImm32(w->dst, errfnc - (w->dst + 4));
268 jitCompPutByte1(w->dst, 0x3b); /* CMP(reg, [EBP+?]); */
269 jitCompPutModRM_Disp_BaseEBP(w, PRegOffset(pxx) + 12, reg); /* p1 */
270 jitCompPutByte2(w->dst, 0x0f, 0x83); /* JAE */
271 jitCompPutImm32(w->dst, errfnc - (w->dst + 4));