3 #define JITC_ERR_MASK 255
4 #define JITC_ERR_PHASE0ONLY 256
5 #define JITC_ERR_REGNUM (1 | JITC_ERR_PHASE0ONLY)
6 #define JITC_ERR_DST1 (2 | JITC_ERR_PHASE0ONLY)
7 #define JITC_ERR_OPECODE (3 | JITC_ERR_PHASE0ONLY)
8 #define JITC_ERR_LABELNUM (4 | JITC_ERR_PHASE0ONLY)
9 #define JITC_ERR_LABELREDEF (5 | JITC_ERR_PHASE0ONLY)
10 #define JITC_ERR_PREFIX (6 | JITC_ERR_PHASE0ONLY)
11 #define JITC_ERR_LABELNODEF 7
12 #define JITC_ERR_LABELTYP 8
13 #define JITC_ERR_IDIOM 9
14 #define JITC_ERR_PREGNUM (10 | JITC_ERR_PHASE0ONLY)
15 #define JITC_ERR_SRC1 (11 | JITC_ERR_PHASE0ONLY)
16 #define JITC_ERR_BADTYPE (12 | JITC_ERR_PHASE0ONLY)
17 #define JITC_ERR_PREFIXFAR (13 | JITC_ERR_PHASE0ONLY)
18 #define JITC_ERR_INTERNAL 99
20 void errorHandler(struct Regs *r)
22 puts("security error! abort...");
23 printf("debugInfo0=%d, debugInfo1=%d¥n", r->debugInfo0, r->debugInfo1);
24 #if (USE_DEBUGGER != 0)
30 int jitCompCmdLen(const unsigned char *src)
33 if (0x01 <= *src && *src < 0x04) i = 6;
34 if (*src == 0x04) i = 2;
35 if (0x08 <= *src && *src < 0x0d) i = 8 + src[7] * 4;
36 if (0x0e <= *src && *src < 0x10) i = 8;
37 if (0x10 <= *src && *src < 0x2e) i = 4;
38 if (0x1c <= *src && *src < 0x1f) i = 3;
39 if (*src == 0x1f) i = 11;
40 if (*src == 0x2f) i = 4 + src[1];
41 if (0x30 <= *src && *src <= 0x33) i = 4;
42 if (0x3c <= *src && *src <= 0x3d) i = 7;
43 if (*src == 0xfe) i = 2 + src[1];
46 #if (JITC_ARCNUM == 0x0001) /* x86-32bit */
49 以下は最適化のためのものなので、すべて0として簡単に移植しても問題ありません */
50 #define jitCompA0001_USE_R3F_CMPJMP 1*1
51 #define jitCompA0001_USE_R3F_IMM32 1*1
52 #define jitCompA0001_USE_R3F_IMM8 1*1
53 #define jitCompA0001_USE_R3F_INCDEC 1*1
54 #define jitCompA0001_OPTIMIZE_JMP 1*1
55 #define jitCompA0001_OPTIMIZE_MOV 1*1 /* 1にすると速度低下する? */
56 #define jitCompA0001_OPTIMIZE_CMP 1*1
57 #define jitCompA0001_OPTIMIZE_ALIGN 4*1 /* 0-8を想定 */
58 #define jitCompA0001_EBP128 128*1
61 unsigned char *dst, *dst0;
63 #if (jitCompA0001_USE_R3F_IMM32 != 0)
69 #define jitCompPutByte1(p, c0) *p++ = c0
70 #define jitCompPutByte2(p, c0, c1) *p++ = c0; *p++ = c1
71 #define jitCompPutByte3(p, c0, c1, c2) *p++ = c0; *p++ = c1; *p++ = c2
72 #define jitCompPutByte4(p, c0, c1, c2, c3) *p++ = c0; *p++ = c1; *p++ = c2; *p++ = c3
74 static void jitCompPutImm32(struct JitCompWork *w, int i)
76 jitCompPutByte1(w->dst, i & 0xff);
77 jitCompPutByte1(w->dst, (i >> 8) & 0xff);
78 jitCompPutByte1(w->dst, (i >> 16) & 0xff);
79 jitCompPutByte1(w->dst, (i >> 24) & 0xff);
83 int jitCompGetImm32(const unsigned char *src)
85 return (src[0] << 24) | (src[1] << 16) | (src[2] << 8) | src[3];
88 int jitCompGetLabelNum(struct JitCompWork *w, const unsigned char *src)
90 int i = jitCompGetImm32(src);
91 if (i < 0 || i >= w->maxLabels) {
92 w->err = JITC_ERR_LABELNUM;
98 void jitCompA0001_85DispN(struct JitCompWork *w, int disp, int n)
100 disp -= jitCompA0001_EBP128;
101 if (-128 <= disp && disp <= 127) {
102 jitCompPutByte2(w->dst, 0x45 | (n << 3), disp & 0xff);
105 jitCompPutByte1(w->dst, 0x85 | (n << 3));
106 jitCompPutImm32(w, disp);
111 void jitCompA0001_movEbpDispReg32(struct JitCompWork *w, int disp, int reg32)
113 jitCompPutByte1(w->dst, 0x89); /* MOV(mem, reg32); */
114 jitCompA0001_85DispN(w, disp, reg32);
118 void jitCompA0001_movReg32EbpDisp(struct JitCompWork *w, int reg32, int disp)
120 jitCompPutByte1(w->dst, 0x8b); /* MOV(reg32, mem); */
121 jitCompA0001_85DispN(w, disp, reg32);
125 void jitCompA0001_movEaxRxx(struct JitCompWork *w, int rxx)
127 #if (jitCompA0001_USE_R3F_IMM32 != 0)
129 jitCompPutByte1(w->dst, 0xb8); /* MOV(EAX, ?); */
130 jitCompPutImm32(w, w->r3f);
134 if (rxx >= 0x40 || rxx < 0) w->err = JITC_ERR_REGNUM;
135 jitCompA0001_movReg32EbpDisp(w, 0 /* EAX */, rxx * 4); /* MOV(EAX, [EBP+?]); */
139 void jitCompA0001_movRxxEax(struct JitCompWork *w, int rxx)
141 if (rxx >= 0x40 || rxx < 0) w->err = JITC_ERR_REGNUM;
142 jitCompA0001_movEbpDispReg32(w, rxx * 4, 0 /* EAX */); /* MOV([EBP+?], EAX); */
146 void jitCompA0001_fixPrefix(struct JitCompWork *w)
148 if (w->prefix != 0) {
149 if (w->dst - w->dst0 > 127) w->err = JITC_ERR_REGNUM;
150 w->dst0[-1] = (unsigned char)((w->dst - w->dst0) & 0xff);
155 void jitCompA0001_checkCompPtr(struct JitCompWork *w, int p0, int p1)
157 if (p0 >= 0x3f || p0 < 0) w->err = JITC_ERR_PREGNUM;
158 if (p1 >= 0x3f || p1 < 0) w->err = JITC_ERR_PREGNUM;
159 /* 比較可能可能なのかのチェックのコードを出力 */ /* 未完成 */
163 void jitCompA000_loadRegCacheAll(struct JitCompWork *w)
165 jitCompA0001_movReg32EbpDisp(w, 3 /* EBX */, 0 * 4); /* EBX = R00; */
166 jitCompA0001_movReg32EbpDisp(w, 1 /* ECX */, 1 * 4); /* ECX = R01; */
167 jitCompA0001_movReg32EbpDisp(w, 2 /* EDX */, 2 * 4); /* EDX = R02; */
171 void jitCompA000_storeRegCacheAll(struct JitCompWork *w)
173 jitCompA0001_movEbpDispReg32(w, 0 * 4, 3 /* EBX */); /* R00 = EBX; */
174 jitCompA0001_movEbpDispReg32(w, 1 * 4, 1 /* ECX */); /* R01 = ECX; */
175 jitCompA0001_movEbpDispReg32(w, 2 * 4, 2 /* EDX */); /* R02 = EDX; */
179 void jitCompA000_loadRegCacheEcx(struct JitCompWork *w)
181 jitCompA0001_movReg32EbpDisp(w, 1 /* ECX */, 1 * 4); /* ECX = R01; */
185 void jitCompA000_storeRegCacheEcx(struct JitCompWork *w)
187 jitCompA0001_movEbpDispReg32(w, 1 * 4, 1 /* ECX */); /* R01 = ECX; */
191 void jitCompA000_loadRegCacheEdx(struct JitCompWork *w)
193 jitCompA0001_movReg32EbpDisp(w, 2 /* EDX */, 2 * 4); /* EDX = R02; */
197 void jitCompA000_storeRegCacheEdx(struct JitCompWork *w)
199 jitCompA0001_movEbpDispReg32(w, 2 * 4, 2 /* EDX */); /* R02 = EDX; */
203 int jitCompA000_selectRegCache(int rxx, int reg)
205 if (rxx == 0) reg = 3; /* EBX */
206 if (rxx == 1) reg = 1; /* ECX */
207 if (rxx == 2) reg = 2; /* EDX */
211 void jitCompA000_loadPRegCacheAll(struct JitCompWork *w)
213 // jitCompA0001_movReg32EbpDisp(w, 5 /* EBP */, 256 + 0 * 32 + 0); /* EBP = P00; */
214 jitCompA0001_movReg32EbpDisp(w, 6 /* ESI */, 256 + 1 * 32 + 0); /* ESI = P01; */
215 jitCompA0001_movReg32EbpDisp(w, 7 /* EDI */, 256 + 2 * 32 + 0); /* EDI = P02; */
219 void jitCompA000_storePRegCacheAll(struct JitCompWork *w)
221 // jitCompA0001_movEbpDispReg32(w, 256 + 0 * 32 + 0, 5 /* EBP */); /* P00 = EBP; */
222 jitCompA0001_movEbpDispReg32(w, 256 + 1 * 32 + 0, 6 /* ESI */); /* P01 = ESI; */
223 jitCompA0001_movEbpDispReg32(w, 256 + 2 * 32 + 0, 7 /* EDI */); /* P02 = EDI; */
227 int jitCompA000_selectPRegCache(int pxx, int reg)
229 // if (pxx == 0) reg = 5; /* EBP */
230 if (pxx == 1) reg = 6; /* ESI */
231 if (pxx == 2) reg = 7; /* EDI */
235 int jitCompA000_convTyp(int t)
238 if (1 <= t && t <= 7) r = t;
239 if (8 <= t && t <= 13) r = 2 | (t & 1);
240 if (14 <= t && t <= 15) r = 4 | (t & 1);
241 if (16 <= t && t <= 21) r = 6 | (t & 1);
245 int jitCompA000_dataWidth(int t)
248 if (t == 0x0001) r = 256;
250 if (t == 0x0002 / 2) r = 8;
251 if (t == 0x0004 / 2) r = 16;
252 if (t == 0x0006 / 2) r = 32;
253 if (t == 0x0008 / 2) r = 4;
254 if (t == 0x000a / 2) r = 2;
255 if (t == 0x000c / 2) r = 1;
256 if (t == 0x000e / 2) r = 12;
257 if (t == 0x0010 / 2) r = 20;
258 if (t == 0x0012 / 2) r = 24;
259 if (t == 0x0014 / 2) r = 28;
263 static unsigned char *errfnc;
265 void jitCompA0001_checkType0(struct JitCompWork *w, int pxx, int typ, int ac)
267 if (typ <= 0) { w->err = JITC_ERR_BADTYPE; }
268 if (typ > 0x7f) { w->err = JITC_ERR_INTERNAL; }
269 jitCompA0001_movReg32EbpDisp(w, 0 /* EAX */, 256 + pxx * 32 + 4); /* MOV(EAX, [EBP+?]); */ /* typ */
270 jitCompPutByte3(w->dst, 0x83, 0xf8, typ & 0x7f); /* CMP(EAX, ?); */
271 jitCompPutByte2(w->dst, 0x0f, 0x85); /* JNE */
272 jitCompPutImm32(w, errfnc - (w->dst + 4));
276 void jitCompA0001_checkType(struct JitCompWork *w, int pxx, int typ, int ac)
278 // 将来的にはaliveやアクセス権チェックも入れる
280 jitCompA0001_checkType0(w, pxx, typ, ac);
284 void jitCompA0001_checkLimit(struct JitCompWork *w, int reg, int pxx)
286 jitCompPutByte1(w->dst, 0x3b); /* CMP(reg, [EBP+?]); */
287 jitCompA0001_85DispN(w, 256 + pxx * 32 + 8, reg); /* p0 */
288 jitCompPutByte2(w->dst, 0x0f, 0x82); /* JB */
289 jitCompPutImm32(w, errfnc - (w->dst + 4));
290 jitCompPutByte1(w->dst, 0x3b); /* CMP(reg, [EBP+?]); */
291 jitCompA0001_85DispN(w, 256 + pxx * 32 + 12, reg); /* p1 */
292 jitCompPutByte2(w->dst, 0x0f, 0x83); /* JAE */
293 jitCompPutImm32(w, errfnc - (w->dst + 4));
297 void func3c(char *ebp, int opt, int r1, int p1, int lenR, int lenP, int r0, int p0);
298 void func3d(char *ebp, int opt, int r1, int p1, int lenR, int lenP, int r0, int p0);
299 void funcf4(char *ebp, int pxx, int typ, int len);
300 void funcf5(char *ebp, int pxx, int typ, int len); // pxxはダミーで参照されない.
301 void funcf6(char *ebp, int pxx, int typ, int len);
302 void funcf7(char *ebp, int pxx, int typ, int len); // typとlenはダミーで参照されない.
303 // F5の場合、decoderが対応するalloc-freeを結びつけるのが簡単で、typやlenを指定必須にしてもフロントエンドコードに影響はない.
305 void errHndl(struct Regs *r);
307 int jitCompiler(unsigned char *dst, unsigned char *dst1, const unsigned char *src, const unsigned char *src1, const unsigned char *src0, struct LabelTable *label, int maxLabels, int level, int debugInfo1, int flags)
309 /* 本来ならこのレイヤでは文法チェックしない */
311 struct JitCompWork w;
312 unsigned char *dst00 = dst, *errmsg = "", *enter0 = NULL, *tmp_ucp;
313 const unsigned char *oldsrc;
314 int timecount = 0, i, j = 0, lastlabel = -1, debugInfo0 = -1;
315 int reg0, reg1, reg2, cmp0reg = -1, cmp0lev = 0;
316 w.dst = w.dst0 = dst;
318 w.maxLabels = maxLabels;
319 if ((flags & JITC_NOSTARTUP) == 0) {
320 jitCompPutByte1(w.dst, 0x60); /* PUSHAD(); */
321 jitCompA000_loadRegCacheAll(&w); /* start-up */
322 jitCompA000_loadPRegCacheAll(&w);
324 if (level <= JITC_LV_SLOWER) {
325 jitCompPutByte2(w.dst, 0x31, 0xc0); /* XOR(EAX, EAX); */
326 jitCompA0001_movEbpDispReg32(&w, 2304 + 0, 0 /* EAX */); /* MOV(debugInfo0, EAX); */
327 jitCompPutByte1(w.dst, 0xb8); /* MOV(EAX, ?); */
328 jitCompPutImm32(&w, debugInfo1);
329 jitCompA0001_movEbpDispReg32(&w, 2304 + 4, 0 /* EAX */); /* MOV(debugInfo1, EAX); */
333 if (w.dst + 256 > dst1) { w.err = JITC_ERR_DST1; goto err_w; }
335 if (timecount >= 64) {
337 /* 未完成(timeoutチェックコードを入れる) */
343 if (w.prefix != 0) { w.err = JITC_ERR_PREFIX; goto err_w; }
347 if (enter0 == NULL && (src[6] == 0x3c || (src[6] == 0xfe && src[7] == 0x01 && src[9] == 0x3c))) {
348 jitCompPutByte1(w.dst, 0xe9);
350 jitCompPutImm32(&w, 0);
352 if (src[6] == 0x34) {
354 jitCompPutByte1(w.dst, 0xe9);
355 i = jitCompGetImm32(&src[7]);
358 i = jitCompA000_convTyp(i);
360 if (i == 2 || i == 3) { j = 1; }
361 if (i == 4 || i == 5) { j = 2; }
362 if (i == 6 || i == 7) { j = 4; }
364 j *= jitCompGetImm32(&src[11]);
365 if (j <= 0) w.err = JITC_ERR_BADTYPE;
366 jitCompPutImm32(&w, j);
367 #if (jitCompA0001_OPTIMIZE_JMP != 0)
368 if (j <= 127 - jitCompA0001_OPTIMIZE_ALIGN) {
370 jitCompPutByte2(w.dst, 0xeb, j);
374 #if (jitCompA0001_OPTIMIZE_ALIGN != 0)
376 i = ((int)w.dst) & (jitCompA0001_OPTIMIZE_ALIGN - 1); /* ALIGNで割ったあまりを計算 */
378 i = jitCompA0001_OPTIMIZE_ALIGN - i;
379 if (i == 1) { jitCompPutByte1(w.dst, 0x90); j += i; } /* NOP(); */
380 if (i == 2) { jitCompPutByte2(w.dst, 0x89, 0xc0); j += i; } /* MOV(EAX, EAX); */
381 if (i == 3) { jitCompPutByte3(w.dst, 0x8d, 0x76, 0x00); j += i; } /* LEA(ESI, [ESI+0]); */
382 if (i == 4) { jitCompPutByte4(w.dst, 0x8d, 0x74, 0x26, 0x00); j += i; } /* LEA(ESI, [ESI*1+0]); */
383 if (i == 5) { jitCompPutByte1(w.dst, 0x0d); jitCompPutImm32(&w, 0); j += i; } /* OR(EAX, 0); */
384 if (i == 6) { jitCompPutByte2(w.dst, 0x8d, 0xb6); jitCompPutImm32(&w, 0); j += i; } /* LEA(ESI, [ESI+0]); */
385 if (i >= 7) { jitCompPutByte3(w.dst, 0x8d, 0xb4, 0x26); jitCompPutImm32(&w, 0); j += 7; } /* LEA(ESI, [ESI*1+0]); */
388 if (src[6] == 0x34) {
389 tmp_ucp[1] = j & 0xff;
390 if (*tmp_ucp == 0xe9) {
391 tmp_ucp[2] = (j >> 8) & 0xff;
392 tmp_ucp[3] = (j >> 16) & 0xff;
393 tmp_ucp[4] = (j >> 24) & 0xff;
396 if ((flags & JITC_PHASE1) == 0) {
397 i = jitCompGetLabelNum(&w, src + 2);
398 //printf("i=%06X %06X¥n", i, src-src0);
399 if (label[i].opt != 0 && w.err == 0) { w.err = JITC_ERR_LABELREDEF; goto err_w; }
400 if (w.prefix != 0) { w.err = JITC_ERR_PREFIX; goto err_w; }
401 label[i].opt = src[1] + 1;
402 label[i].typ = 0; /* TYP_CODE */
404 label[i].p1 = w.dst + 1;
409 /* 未完成(timeoutチェックコードを入れる) */
412 case 0x02: /* LIMM */
413 if (src[1] == 0x3f && w.prefix != 0) w.err = JITC_ERR_PREFIX;
414 #if (jitCompA0001_USE_R3F_IMM32 != 0)
415 if (src[1] == 0x3f) {
416 w.r3f = jitCompGetImm32(src + 2);
420 i = jitCompGetImm32(src + 2);
421 reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
422 #if (jitCompA0001_OPTIMIZE_MOV != 0)
424 jitCompPutByte2(w.dst, 0x31, 0xc0 | reg0 << 3 | reg0); /* XOR(reg0, reg0); */
425 jitCompA0001_movRxxEax(&w, src[1]);
429 jitCompPutByte1(w.dst, 0xb8 | reg0); /* MOV(reg0, ?); */
430 jitCompPutImm32(&w, i);
432 jitCompA0001_movRxxEax(&w, src[1]);
435 case 0x03: /* PLIMM */ /* 未完成(plsまで対応) */
436 i = jitCompGetLabelNum(&w, src + 2);
437 if ((flags & JITC_PHASE1) != 0 && w.err == 0) {
438 if (label[i].opt == 0) { w.err = JITC_ERR_LABELNODEF; goto err_w; }
439 if (src[1] != 0x3f && label[i].opt != 2) { w.err = JITC_ERR_LABELTYP; goto err_w; }
440 if (src[1] == 0x3f && label[i].typ != 0) { w.err = JITC_ERR_LABELTYP; goto err_w; }
442 if (src[1] == 0x3f) {
444 jitCompPutByte1(w.dst, 0xe9); /* JMP(?); */
447 w.dst[-1] = w.dst[-2] ^ 0xf1; /* 74->85, 75->84 */
452 if ((flags & JITC_PHASE1) != 0 || ((flags & JITC_PHASE1) == 0) && label[i].opt != 0)
453 j = label[i].p - (w.dst + 4);
454 jitCompPutImm32(&w, j);
455 #if (jitCompA0001_OPTIMIZE_JMP != 0)
456 if (-128 - 3 <= j && j < 0) {
457 if (w.dst[-5] == 0xe9) {
460 jitCompPutByte1(w.dst, 0xeb); /* JMP(?); */
465 jitCompPutByte1(w.dst, w.dst[1] ^ 0xf0);
467 jitCompPutByte1(w.dst, j & 0xff);
472 reg0 = jitCompA000_selectPRegCache(src[1], 0 /* EAX */);
473 jitCompPutByte1(w.dst, 0xb8 | reg0); /* MOV(reg0, ?); */
474 jitCompPutImm32(&w, (int)label[i].p);
476 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32, 0); /* MOV([EBP+?], EAX); */
477 if (level < JITC_LV_FASTEST) {
478 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 8, reg0); /* MOV([EBP+?], reg0); */ /* p0 */
479 jitCompPutByte1(w.dst, 0xb8); /* MOV(EAX, ?); */
480 jitCompPutImm32(&w, label[i].typ);
481 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 4, 0); /* MOV([EBP+?], EAX); */ /* typ */
482 jitCompPutByte1(w.dst, 0xb8); /* MOV(EAX, ?); */
483 jitCompPutImm32(&w, (int)label[i].p1);
484 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 12, 0); /* MOV([EBP+?], EAX); */ /* p1 */
485 jitCompPutByte2(w.dst, 0x31, 0xc0); /* XOR(EAX, EAX); */
486 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 16, 0); /* MOV([EBP+?], EAX); */ /* liveSign */
487 jitCompA0001_movReg32EbpDisp(&w, 0, 2320); /* MOV(EAX, ptrCtrl); */
488 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 20, 0); /* MOV([EBP+?], EAX); */ /* pls */
493 case 0x04: /* CND (prefix) */
494 if (src[1] >= 0x40) w.err = JITC_ERR_REGNUM;
495 reg0 = jitCompA000_selectRegCache(src[1], -1 /* mem */);
497 jitCompPutByte1(w.dst, 0xf7); /* TEST([EBP+?],1); */
498 jitCompA0001_85DispN(&w, src[1] * 4, 0);
501 jitCompPutByte2(w.dst, 0xf7, 0xc0 | reg0); /* TEST(reg0,1); */
503 jitCompPutImm32(&w, 1);
504 jitCompPutByte2(w.dst, 0x74, 0x00); /* JZ($+2) */
506 if (w.err != 0) goto err_w;
510 goto prefix_continue;
512 case 0x08: /* LMEM */ /* 完成 */
513 i = jitCompGetImm32(src + 2);
514 if (i == 0x0001) w.err = JITC_ERR_BADTYPE;
515 if (level < JITC_LV_FASTER) {
516 jitCompA0001_checkType(&w, src[6], i, 0); // read
519 reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
520 reg1 = jitCompA000_selectPRegCache(src[6], 2 /* EDX */);
521 if (reg0 != 0 /* EAX */ && reg1 == 2 /* EDX */)
523 if (reg1 == 2 /* EDX */)
524 jitCompA000_storeRegCacheEdx(&w);
525 if (reg1 <= 3 /* EAX, EDX */)
526 jitCompA0001_movReg32EbpDisp(&w, reg1, 256 + src[6] * 32 + 0); /* MOV(reg1, [EBP+?]); */
527 if (level < JITC_LV_FASTER)
528 jitCompA0001_checkLimit(&w, reg1, src[6]);
529 i = jitCompA000_convTyp(jitCompGetImm32(src + 2));
532 jitCompPutByte3(w.dst, 0x0f, 0xbe, reg0 << 3 | reg1); /* MOVSX(reg0,BYTE [reg1]); */
535 jitCompPutByte3(w.dst, 0x0f, 0xb6, reg0 << 3 | reg1); /* MOVZX(reg0,BYTE [reg1]); */
538 jitCompPutByte3(w.dst, 0x0f, 0xbf, reg0 << 3 | reg1); /* MOVSX(reg0,WORD [reg1]); */
541 jitCompPutByte3(w.dst, 0x0f, 0xb7, reg0 << 3 | reg1); /* MOVZX(reg0,WORD [reg1]); */
545 jitCompPutByte2(w.dst, 0x8b, reg0 << 3 | reg1); /* MOV(reg0, [reg1]); */
548 w.err = JITC_ERR_BADTYPE;
550 if (reg0 == 0 /* EAX */)
551 jitCompA0001_movRxxEax(&w, src[1]);
552 if (reg1 == 2 /* EDX */)
553 jitCompA000_loadRegCacheEdx(&w);
556 case 0x09: /* SMEM */ /* 完成 */
557 i = jitCompGetImm32(src + 2);
558 if (i == 0x0001) w.err = JITC_ERR_BADTYPE;
559 if (level < JITC_LV_FASTER) {
560 jitCompA0001_checkType(&w, src[6], i, 1); // write
563 reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
564 reg1 = jitCompA000_selectPRegCache(src[6], 2 /* EDX */);
565 if (reg0 != 0 /* EAX */ && reg1 == 2 /* EDX */)
567 if (reg1 == 2 /* EDX */)
568 jitCompA000_storeRegCacheEdx(&w);
569 if (reg1 <= 3 /* EAX, EDX */)
570 jitCompA0001_movReg32EbpDisp(&w, reg1, 256 + src[6] * 32 + 0); /* MOV(reg1, [EBP+?]); */
571 if (level < JITC_LV_FASTER)
572 jitCompA0001_checkLimit(&w, reg1, src[6]);
573 if (reg0 == 0 /* EAX */)
574 jitCompA0001_movEaxRxx(&w, src[1]);
576 i = jitCompA000_convTyp(jitCompGetImm32(src + 2));
580 jitCompPutByte2(w.dst, 0x88, reg0 << 3 | reg1); /* MOV([reg1], BYTE(reg0)); */
584 jitCompPutByte3(w.dst, 0x66, 0x89, reg0 << 3 | reg1); /* MOV([reg1], WORD(reg0)); */
588 jitCompPutByte2(w.dst, 0x89, reg0 << 3 | reg1); /* MOV([reg1], reg0); */
591 w.err = JITC_ERR_BADTYPE;
593 if (reg1 == 2 /* EDX */)
594 jitCompA000_loadRegCacheEdx(&w);
597 case 0x0a: /* PLMEM */ /* 完成 */
598 i = jitCompGetImm32(src + 2);
599 if (i != 0x0001) w.err = JITC_ERR_BADTYPE;
600 if (level < JITC_LV_FASTER) {
601 jitCompA0001_checkType(&w, src[6], i, 0); // read
604 reg0 = jitCompA000_selectPRegCache(src[1], 0 /* EAX */);
605 reg1 = jitCompA000_selectPRegCache(src[6], 2 /* EDX */);
606 // if (reg0 != 0 /* EAX */ && reg1 == 2 /* EDX */) /* これをやってはいけない!(by K, 2013.08.02) */
607 // reg1 = 0; /* EAX */
608 if (reg0 == reg1 && reg0 != 0) { // bugfix: hinted by yao, 2013.09.14. thanks!
609 jitCompA000_storePRegCacheAll(&w);
612 if (reg1 == 2 /* EDX */)
613 jitCompA000_storeRegCacheEdx(&w);
614 if (reg1 <= 3 /* EAX, EDX */)
615 jitCompA0001_movReg32EbpDisp(&w, reg1, 256 + src[6] * 32 + 0); /* MOV(reg1, [EBP+?]); */
616 if (level < JITC_LV_FASTER)
617 jitCompA0001_checkLimit(&w, reg1, src[6]);
618 jitCompPutByte2(w.dst, 0x8b, reg0 << 3 | reg1); /* MOV(reg0, [reg1]); */
619 if (reg0 == 0 /* EAX */)
620 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 0, 0); /* MOV([EBP+?], EAX); */
621 for (i = 4; i < 32; i += 4) {
622 jitCompPutByte3(w.dst, 0x8b, 0x40 | reg1, i); /* MOV(EAX, [reg1+?]); */
623 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + i, 0 /* EAX */); /* MOV([EBP+?], EAX); */
625 if (reg1 == 2 /* EDX */)
626 jitCompA000_loadRegCacheEdx(&w);
629 case 0x0b: /* PSMEM */ /* 完成 */
630 i = jitCompGetImm32(src + 2);
631 if (i != 0x0001) w.err = JITC_ERR_BADTYPE;
632 if (level < JITC_LV_FASTER) {
633 jitCompA0001_checkType(&w, src[6], i, 1); // write
636 reg0 = jitCompA000_selectPRegCache(src[1], 0 /* EAX */);
637 reg1 = jitCompA000_selectPRegCache(src[6], 2 /* EDX */);
638 // if (reg0 != 0 /* EAX */ && reg1 == 2 /* EDX */) /* これをやってはいけない!(by K, 2013.08.02) */
639 // reg1 = 0; /* EAX */
640 if (reg1 == 2 /* EDX */)
641 jitCompA000_storeRegCacheEdx(&w);
642 if (reg1 <= 3 /* EAX, EDX */)
643 jitCompA0001_movReg32EbpDisp(&w, reg1, 256 + src[6] * 32 + 0); /* MOV(reg1, [EBP+?]); */
644 if (level < JITC_LV_FASTER)
645 jitCompA0001_checkLimit(&w, reg1, src[6]);
646 if (reg0 == 0 /* EAX */)
647 jitCompA0001_movReg32EbpDisp(&w, reg0, 256 + src[1] * 32 + 0); /* MOV(reg0, [EBP+?]); */
648 jitCompPutByte2(w.dst, 0x89, reg0 << 3 | reg1); /* MOV([reg1], reg0); */
649 for (i = 4; i < 32; i += 4) {
650 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[1] * 32 + i); /* MOV(EAX, [EBP+?]); */
651 jitCompPutByte3(w.dst, 0x89, 0x40 | reg1, i); /* MOV([reg1+?], EAX); */
653 if (reg1 == 2 /* EDX */)
654 jitCompA000_loadRegCacheEdx(&w);
657 case 0x0e: /* PADD */ /* 完成 */
658 if (level < JITC_LV_FASTER) {
659 jitCompA0001_checkType0(&w, src[6], jitCompGetImm32(src + 2), 2); // other, aliveテストはとりあえずしない.
662 reg0 = jitCompA000_selectPRegCache(src[1], 0 /* EAX */);
663 reg1 = jitCompA000_selectPRegCache(src[6], -1 /* mem */);
664 if (reg1 < 0 /* mem */)
665 jitCompA0001_movReg32EbpDisp(&w, reg0, 256 + src[6] * 32 + 0); /* MOV(reg0, [EBP+?]); */
666 if (reg1 >= 0 && reg0 != reg1) {
667 jitCompPutByte2(w.dst, 0x89, 0xc0 | reg1 << 3 | reg0); /* MOV(reg0, reg1); */
669 i = jitCompGetImm32(src + 2);
674 i = jitCompA000_convTyp(i);
675 if (0x0002 <= i && i <= 0x0007)
676 j = (i - 0x0002) >> 1;
678 if (j < 0) { w.err = JITC_ERR_BADTYPE; goto err_w; }
679 #if (jitCompA0001_USE_R3F_IMM32 != 0)
680 if (src[7] == 0x3f) {
682 #if (jitCompA0001_USE_R3F_IMM8 != 0)
683 if (-0x80 <= j && j <= 0x7f) {
684 #if (jitCompA0001_USE_R3F_INCDEC != 0)
685 if (j == 1) { jitCompPutByte1(w.dst, 0x40 | reg0); goto padd1; } /* INC */
686 if (j == -1) { jitCompPutByte1(w.dst, 0x48 | reg0); goto padd1; } /* DEC */
688 jitCompPutByte3(w.dst, 0x83, 0xc0 | reg0, j & 0xff); /* ADD(reg0, im8); */
693 jitCompPutByte1(w.dst, 0x05); /* ADD(reg0, ?); */
696 jitCompPutByte2(w.dst, 0x81, 0xc0 | reg0); /* ADD(reg0, ?); */
698 jitCompPutImm32(&w, j);
702 if (src[7] >= 0x40) w.err = JITC_ERR_REGNUM;
704 reg1 = jitCompA000_selectRegCache(src[7], -1 /* mem */);
706 jitCompPutByte2(w.dst, 0x01, 0xc0 | reg1 << 3 | reg0); /* ADD(reg0, reg1); */
709 jitCompPutByte1(w.dst, 0x03); /* ADD(reg0, [EBP+?]); */
710 jitCompA0001_85DispN(&w, src[7] * 4, reg0);
714 reg1 = jitCompA000_selectRegCache(src[7], -1 /* mem */);
716 jitCompA000_storeRegCacheEdx(&w);
718 jitCompA0001_movReg32EbpDisp(&w, reg2, src[7] * 4); /* MOV(reg2, [EBP+?]); */
719 if (reg1 >= 0 && reg1 != reg2) {
720 jitCompPutByte2(w.dst, 0x89, 0xc0 | reg1 << 3 | reg2); /* MOV(reg2, reg1); */
722 jitCompPutByte3(w.dst, 0xc1, 0xe0 | reg2, j); /* SHL(reg2, ?); */
723 jitCompPutByte2(w.dst, 0x01, 0xc0 | reg2 << 3 | reg0); /* ADD(reg0, reg2); */
724 jitCompA000_loadRegCacheEdx(&w);
726 #if (jitCompA0001_USE_R3F_IMM32 != 0)
729 if (reg0 == 0 /* EAX */)
730 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 0, reg0); /* MOV([EBP+?], reg0); */
731 if (src[1] != src[6]) {
732 for (i = 4; i < 32; i += 4) {
733 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[6] * 32 + i); /* MOV(EAX, [EBP+?]); */
734 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + i, 0 /* EAX */); /* MOV([EBP+?], EAX); */
740 case 0x0f: /* PDIF */ /* 未完成 */
741 reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
742 jitCompA000_storePRegCacheAll(&w); // 手抜き.
743 jitCompA0001_checkCompPtr(&w, src[6], src[7]);
744 jitCompA0001_movReg32EbpDisp(&w, reg0, 256 + src[6] * 32 + 0); /* MOV(reg0, [EBP+?]); */
745 jitCompPutByte1(w.dst, 0x2b); /* SUB(EAX, [EBP+?]); */
746 jitCompA0001_85DispN(&w, 256 + src[7] * 32 + 0, reg0);
747 i = jitCompA000_convTyp(jitCompGetImm32(src + 2));
749 if (0x0002 <= i && i <= 0x0007)
750 j = (i - 0x0002) >> 1;
751 if (j < 0) { w.err = JITC_ERR_BADTYPE; goto err_w; }
753 jitCompPutByte3(w.dst, 0xc1, 0xf8 | reg0, j); /* SAR(reg0,?); */
755 if (reg0 == 0 /* EAX */)
756 jitCompA0001_movRxxEax(&w, src[1]);
757 cmp0reg = src[1]; cmp0lev = 1;
766 if (src[1] >= 0x3f) w.err = JITC_ERR_REGNUM;
767 reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
768 reg1 = jitCompA000_selectRegCache(src[2], -1 /* mem */);
769 #if (jitCompA0001_USE_R3F_IMM32 != 0)
770 if (src[2] == 0x3f) { // SUBのみ該当.
771 if (*src != 0x15) w.err = JITC_ERR_REGNUM;
772 reg2 = jitCompA000_selectRegCache(src[3], -1 /* mem */);
774 jitCompA000_storeRegCacheAll(&w);
775 jitCompPutByte1(w.dst, 0xb8 | reg0); /* MOV(reg0, ?); */
776 jitCompPutImm32(&w, w.r3f);
777 jitCompPutByte1(w.dst, 0x2b);
778 jitCompA0001_85DispN(&w, src[3] * 4, reg0);
780 jitCompA0001_movRxxEax(&w, src[1]);
785 jitCompA0001_movReg32EbpDisp(&w, reg0, src[2] * 4); /* MOV(reg0, [EBP+?]); */
787 if (reg1 >= 0 && reg0 != reg1) {
788 jitCompPutByte2(w.dst, 0x89, 0xc0 | reg1 << 3 | reg0); /* MOV(reg0, reg1); */
790 if (!(src[0] == 0x10 && src[3] == 0xff)) { // bugfix: hinted by Iris, 2013.06.26. thanks!
798 if (!(src[0] == 0x10 && src[3] == 0xff)) {
799 #if (jitCompA0001_USE_R3F_IMM32 != 0)
800 if (src[3] == 0x3f) {
801 if (*src == 0x16 && w.r3f == -1) {
802 jitCompPutByte2(w.dst, 0xf7, 0xd8 | reg0); /* NEG(reg0); */
804 jitCompA0001_movRxxEax(&w, src[1]);
807 #if (jitCompA0001_USE_R3F_INCDEC != 0)
808 if ((*src == 0x14 && w.r3f == 1) || (*src == 0x15 && w.r3f == -1)) {
809 jitCompPutByte1(w.dst, 0x40 | reg0); /* INC(reg0); */
811 jitCompA0001_movRxxEax(&w, src[1]);
814 if ((*src == 0x15 && w.r3f == 1) || (*src == 0x14 && w.r3f == -1)) {
815 jitCompPutByte1(w.dst, 0x48 | reg0); /* DEC(reg0); */
817 jitCompA0001_movRxxEax(&w, src[1]);
821 #if (jitCompA0001_USE_R3F_IMM8 != 0)
822 if (-0x80 <= w.r3f && w.r3f <= 0x7f) {
824 static unsigned char basic_op_table_im8[] = { 0xc8, 0xf0, 0xe0, 0, 0xc0, 0xe8 };
825 jitCompPutByte3(w.dst, 0x83, basic_op_table_im8[*src - 0x10] | reg0, w.r3f & 0xff);
828 jitCompPutByte3(w.dst, 0x6b, 0xc0 | reg0 << 3 | reg0, w.r3f & 0xff);
831 jitCompA0001_movRxxEax(&w, src[1]);
835 if (reg0 == 0 /* EAX */) {
836 static unsigned char basic_op_table_im32_eax[] = { 0x0d, 0x35, 0x25, 0, 0x05, 0x2d, 0xc0 };
837 if (*src == 0x16) { jitCompPutByte1(w.dst, 0x69); }
838 jitCompPutByte1(w.dst, basic_op_table_im32_eax[*src - 0x10]);
842 static unsigned char basic_op_table_im32_reg[] = { 0xc8, 0xf0, 0xe0, 0, 0xc0, 0xe8 };
843 jitCompPutByte2(w.dst, 0x81, basic_op_table_im32_reg[*src - 0x10] | reg0);
846 jitCompPutByte2(w.dst, 0x69, 0xc0 | reg0 << 3 | reg0);
849 jitCompPutImm32(&w, w.r3f);
851 jitCompA0001_movRxxEax(&w, src[1]);
855 reg1 = jitCompA000_selectRegCache(src[3], -1 /* mem */);
856 if (src[3] >= 0x40) w.err = JITC_ERR_REGNUM;
859 static unsigned char basic_op_table_rr[] = { 0x09, 0x31, 0x21, 0, 0x01, 0x29 }; /* op(reg,reg); */
860 jitCompPutByte2(w.dst, basic_op_table_rr[*src - 0x10], 0xc0 | reg1 << 3 | reg0);
863 static unsigned char basic_op_table_rm[] = { 0x0b, 0x33, 0x23, 0, 0x03, 0x2b, 0xaf }; /* op(reg,mem); */
864 jitCompPutByte1(w.dst, basic_op_table_rm[*src - 0x10]);
865 jitCompA0001_85DispN(&w, src[3] * 4, reg0);
870 jitCompPutByte3(w.dst, 0x0f, 0xaf, 0xc0 | reg0 << 3 | reg1);
873 jitCompPutByte2(w.dst, 0x0f, 0xaf);
874 jitCompA0001_85DispN(&w, src[3] * 4, reg0);
879 jitCompA0001_movRxxEax(&w, src[1]);
884 if (src[1] >= 0x3f) w.err = JITC_ERR_REGNUM;
885 if (src[3] >= 0x40) w.err = JITC_ERR_REGNUM;
886 #if (jitCompA0001_USE_R3F_IMM32 != 0)
887 if (src[3] == 0x3f) {
888 reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
889 reg1 = jitCompA000_selectRegCache(src[2], -1 /* mem */);
890 if (src[1] >= 0x3f) w.err = JITC_ERR_REGNUM;
892 jitCompA0001_movReg32EbpDisp(&w, reg0, src[2] * 4); /* MOV(reg1, [EBP+?]); */
895 jitCompPutByte2(w.dst, 0x89, 0xc0 | reg1 << 3 | reg0); /* MOV(reg0, reg1); */
898 if (*src == 0x18) { jitCompPutByte3(w.dst, 0xc1, 0xe0 | reg0, w.r3f); } /* SHL(reg0, im8); */
899 if (*src == 0x19) { jitCompPutByte3(w.dst, 0xc1, 0xf8 | reg0, w.r3f); } /* SAR(reg0, im8); */
900 if (reg0 == 0 /* EAX */)
901 jitCompA0001_movRxxEax(&w, src[1]);
907 jitCompA000_storeRegCacheAll(&w); // 手抜き.
908 jitCompA0001_movReg32EbpDisp(&w, 1 /* ECX */, src[3] * 4); /* MOV(ECX, [EBP+?]); */
909 #if (jitCompA0001_USE_R3F_IMM32 != 0)
910 if (src[2] == 0x3f) {
911 jitCompPutByte1(w.dst, 0xb8); /* MOV(EAX, ?); */
912 jitCompPutImm32(&w, w.r3f);
915 jitCompA0001_movEaxRxx(&w, src[2]);
918 jitCompA0001_movEaxRxx(&w, src[2]);
920 if (*src == 0x18) { jitCompPutByte2(w.dst, 0xd3, 0xe0); } /* SHL(EAX, CL); */
921 if (*src == 0x19) { jitCompPutByte2(w.dst, 0xd3, 0xf8); } /* SAR(EAX, CL); */
922 jitCompA0001_movRxxEax(&w, src[1]);
923 jitCompA000_loadRegCacheAll(&w); // 手抜き.
930 if (src[1] >= 0x3f) w.err = JITC_ERR_REGNUM;
931 if (src[2] >= 0x40) w.err = JITC_ERR_REGNUM;
932 if (src[3] >= 0x40) w.err = JITC_ERR_REGNUM;
933 jitCompA000_storeRegCacheAll(&w); // 手抜き.
934 #if (jitCompA0001_USE_R3F_IMM32 != 0)
935 if (src[3] == 0x3f) {
936 jitCompPutByte1(w.dst, 0xb8 | 1); /* MOV(ECX, ?); */
937 jitCompPutImm32(&w, w.r3f);
940 jitCompA0001_movReg32EbpDisp(&w, 1 /* ECX */, src[3] * 4); /* MOV(ECX, [EBP+?]); */
942 if (src[2] == 0x3f) {
943 jitCompPutByte1(w.dst, 0xb8 | 0); /* MOV(EAX, ?); */
944 jitCompPutImm32(&w, w.r3f);
947 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, src[2] * 4); /* MOV(EAX, [EBP+?]); */
950 jitCompA0001_movReg32EbpDisp(&w, 1 /* ECX */, src[3] * 4); /* MOV(ECX, [EBP+?]); */
951 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, src[2] * 4); /* MOV(EAX, [EBP+?]); */
953 jitCompPutByte1(w.dst, 0x99); /* CDQ(); */
954 /* ECXがゼロではないことを確認すべき */
955 jitCompPutByte2(w.dst, 0xf7, 0xf9); /* IDIV(ECX); */
956 if (*src == 0x1a) { jitCompA0001_movEbpDispReg32(&w, src[1] * 4, 0 /* EAX */); }
957 if (*src == 0x1b) { jitCompA0001_movEbpDispReg32(&w, src[1] * 4, 2 /* EDX */); }
958 jitCompA000_loadRegCacheAll(&w); // 手抜き.
962 case 0x1c: /* PLMT0 */
963 case 0x1d: /* PLMT1 */
964 if (src[1] >= 0x40 || src[2] >= 0x40) w.err = JITC_ERR_PREGNUM;
965 if (level < JITC_LV_FASTEST) {
967 if (level < JITC_LV_FASTER) {
969 // plsとliveSignが一致していることを確認.
971 // preg1はp0 <= p <= p1 を満たしているか?.
972 // 新しいp0/p1は古いp0〜p1に適合しているか?.
977 case 0x1e: /* PCP */ /* 未完成(p1まで完成) */
978 if (src[1] >= 0x40 || src[2] >= 0x40) w.err = JITC_ERR_PREGNUM;
979 if (src[2] == 0x3f) w.err = JITC_ERR_PREGNUM;
980 if (src[1] != 0x3f) {
981 /* src[2] == 0xff の場合に対応できてない */
982 jitCompA000_storePRegCacheAll(&w); // 手抜き.
983 for (i = 0; i < 32; i += 4) {
984 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[2] * 32 + i); /* MOV(EAX, [EBP+?]); */
985 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + i, 0 /* EAX */); /* MOV([EBP+?], EAX); */
987 jitCompA000_loadPRegCacheAll(&w); // 手抜き.
990 if (level < JITC_LV_FASTER) {
991 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[2] * 32 + 4); /* MOV(EAX, [EBP+?]); */ /* typ */
992 jitCompPutByte3(w.dst, 0x83, 0xf8, 0); /* CMP(EAX, 0); */
993 jitCompPutByte2(w.dst, 0x0f, 0x85); /* JNE */
994 jitCompPutImm32(&w, errfnc - (w.dst + 4));
995 /* セキュリティチェックが足りてない!(aliveとか) */
998 jitCompA000_storePRegCacheAll(&w); // 手抜き.
999 jitCompA0001_movReg32EbpDisp(&w, reg0, 256 + src[2] * 32 + 0); /* MOV(EAX, [EBP+?]); */
1000 if (level < JITC_LV_FASTER) {
1001 jitCompPutByte1(w.dst, 0x3b); /* CMP(reg0, [EBP+?]); */
1002 jitCompA0001_85DispN(&w, 256 + src[2] * 32 + 8, reg0); /* p0 */
1003 jitCompPutByte2(w.dst, 0x0f, 0x85); /* JNE */
1004 jitCompPutImm32(&w, errfnc - (w.dst + 4));
1006 jitCompPutByte2(w.dst, 0xff, 0xe0); /* JMP(EAX); */
1010 case 0x1f: /* PCST */
1011 if (jitCompGetImm32(src + 2) == 0) {
1012 if (level < JITC_LV_FASTER)
1013 jitCompA0001_checkType0(&w, src[6], jitCompGetImm32(src + 7), 2);
1014 jitCompA000_storePRegCacheAll(&w); // 手抜き.
1015 for (i = 0; i < 32 - 4; i += 4) {
1016 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[6] * 32 + i); /* MOV(EAX, [EBP+?]); */
1018 jitCompPutByte1(w.dst, 0x0d); /* OR(EAX, ?); */
1019 jitCompPutImm32(&w, 0x80000000);
1021 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + i, 0 /* EAX */); /* MOV([EBP+?], EAX); */
1023 jitCompPutByte1(w.dst, 0xb8); /* MOV(EAX, ?); */
1024 jitCompPutImm32(&w, debugInfo1);
1025 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 28, 0 /* EAX */); /* MOV([EBP+?], EAX); */
1026 jitCompA000_loadPRegCacheAll(&w); // 手抜き.
1030 if (jitCompGetImm32(src + 7) == 0) {
1031 jitCompA000_storePRegCacheAll(&w); // 手抜き.
1032 for (i = 0; i < 32 - 4; i += 4) {
1033 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[6] * 32 + i); /* MOV(EAX, [EBP+?]); */
1035 jitCompPutByte1(w.dst, 0x25); /* AND(EAX, ?); */
1036 jitCompPutImm32(&w, 0x7fffffff);
1038 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + i, 0 /* EAX */); /* MOV([EBP+?], EAX); */
1040 if (level < JITC_LV_FASTER) {
1041 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[6] * 32 + 28); /* MOV(EAX, [EBP+?]); */
1042 jitCompPutByte1(w.dst, 0x3d); /* CMP(EAX, ?); */
1043 jitCompPutImm32(&w, debugInfo1);
1044 jitCompPutByte2(w.dst, 0x74, 8); /* JE */
1045 jitCompPutByte2(w.dst, 0x31, 0xc0); /* XOR(EAX, EAX); (2) */
1046 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 0, 0 /* EAX */); /* MOV([EBP+?], EAX); (1+1+4) */
1048 jitCompA000_loadPRegCacheAll(&w); // 手抜き.
1052 w.err = JITC_ERR_OPECODE;
1055 case 0x20: /* CMPE */
1056 case 0x21: /* CMPNE */
1057 case 0x22: /* CMPL */
1058 case 0x23: /* CMPGE */
1059 case 0x24: /* CMPLE */
1060 case 0x25: /* CMPG */
1061 case 0x26: /* TSTZ */
1062 case 0x27: /* TSTNZ */
1063 reg0 = jitCompA000_selectRegCache(src[2], 0 /* EAX */);
1064 reg1 = jitCompA000_selectRegCache(src[3], -1 /* mem */);
1065 if (src[1] == 0x3f) {
1067 if (w.prefix != 0) { w.err = JITC_ERR_PREFIX; goto err_w; }
1068 if (src[4] != 0x04 || src[5] != 0x3f || src[6] != 0x03 || src[7] != 0x3f) {
1069 w.err = JITC_ERR_IDIOM; goto err_w;
1073 jitCompA0001_movEaxRxx(&w, src[2]);
1074 #if (jitCompA0001_USE_R3F_IMM32 != 0)
1075 if (src[3] == 0x3f) {
1076 #if (jitCompA0001_OPTIMIZE_CMP != 0)
1077 if ((*src <= 0x25 && w.r3f == 0) || (*src >= 0x26 && w.r3f == -1)) {
1079 if (cmp0reg == src[2]) {
1080 if (cmp0lev >= 1 && (src[0] == 0x20 || src[0] == 0x21 || src[0] == 0x26 || src[0] == 0x27))
1082 if (cmp0lev >= 2 && (src[0] == 0x22 || src[0] == 0x23 || src[0] == 0x24 || src[0] == 0x25))
1086 jitCompPutByte2(w.dst, 0x85, 0xc0 | reg0 << 3 | reg0); /* TEST(reg0, reg0); */
1093 #if (jitCompA0001_USE_R3F_IMM8 != 0)
1094 if (-0x80 <= w.r3f && w.r3f <= 0x7f && *src <= 0x25) {
1095 jitCompPutByte3(w.dst, 0x83, 0xf8 | reg0, w.r3f);
1100 if (*src <= 0x25) { jitCompPutByte1(w.dst, 0x3d); }
1101 if (*src >= 0x26) { jitCompPutByte1(w.dst, 0xa9); }
1104 if (*src <= 0x25) { jitCompPutByte2(w.dst, 0x81, 0xf8 | reg0); }
1105 if (*src >= 0x26) { jitCompPutByte2(w.dst, 0xf7, 0xc0 | reg0); }
1107 jitCompPutImm32(&w, w.r3f);
1111 if (src[3] >= 0x40) w.err = JITC_ERR_PREGNUM;
1113 if (*src <= 0x25) { jitCompPutByte2(w.dst, 0x39, 0xc0 | reg1 << 3 | reg0); }
1114 if (*src >= 0x26) { jitCompPutByte2(w.dst, 0x85, 0xc0 | reg1 << 3 | reg0); }
1117 if (*src <= 0x25) { jitCompPutByte1(w.dst, 0x3b); }
1118 if (*src >= 0x26) { jitCompPutByte1(w.dst, 0x85); }
1119 jitCompA0001_85DispN(&w, src[3] * 4, reg0);
1122 if (w.err != 0) goto err_w;
1123 static unsigned char cmpcc_table0[] = {
1124 0x04, 0x05, 0x0c, 0x0d, 0x0e, 0x0f, 0x04, 0x05, /* CMPcc, TSTcc */
1125 0x04, 0x05, 0x02, 0x03, 0x06, 0x07 /* PCMPcc */
1127 #if (jitCompA0001_USE_R3F_CMPJMP != 0)
1128 if (src[1] == 0x3f) {
1130 jitCompPutByte2(w.dst, 0x0f, 0x80 | cmpcc_table0[*src - 0x20]);
1132 i = jitCompGetLabelNum(&w, src + 2);
1133 if ((flags & JITC_PHASE1) != 0 && w.err != 0) {
1134 if (label[i].opt == 0) { w.err = JITC_ERR_LABELNODEF; goto err_w; }
1135 // if (label[i].typ != 1) { w.err = JITC_ERR_LABELTYP; goto err_w; }
1138 if ((flags & JITC_PHASE1) != 0 || ((flags & JITC_PHASE1) == 0) && label[i].opt != 0)
1139 j = label[i].p - (w.dst + 4);
1140 jitCompPutImm32(&w, j);
1141 #if (jitCompA0001_OPTIMIZE_JMP != 0)
1142 if (-128 - 4 <= j && j < 0) {
1145 jitCompPutByte2(w.dst, w.dst[1] ^ 0xf0, j & 0xff);
1149 if (w.err != 0) goto err_w;
1154 reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
1155 jitCompPutByte3(w.dst, 0x0f, 0x90 | cmpcc_table0[*src - 0x20], 0xc0 | reg0); /* SETcc(BYTE(reg0)); */
1156 jitCompPutByte3(w.dst, 0x0f, 0xb6, 0xc0 | reg0 << 3 | reg0); /* MOVZX(reg0, BYTE(reg0)); */
1157 jitCompPutByte2(w.dst, 0xf7, 0xd8 | reg0); /* NEG(reg0); */
1159 jitCompA0001_movRxxEax(&w, src[1]);
1164 case 0x28: /* PCMPE */
1165 case 0x29: /* PCMPNE */
1166 case 0x2a: /* PCMPL */
1167 case 0x2b: /* PCMPGE */
1168 case 0x2c: /* PCMPLE */
1169 case 0x2d: /* PCMPG */
1170 if (src[1] == 0x3f) {
1172 if (w.prefix != 0) { w.err = JITC_ERR_PREFIX; goto err_w; }
1173 if (src[4] != 0x04 || src[5] != 0x3f || src[6] != 0x03 || src[7] != 0x3f) {
1174 w.err = JITC_ERR_IDIOM; goto err_w;
1177 if (src[2] >= 0x40) w.err = JITC_ERR_PREGNUM;
1178 jitCompA000_storePRegCacheAll(&w); // 手抜き.
1180 jitCompA0001_checkCompPtr(&w, src[2], src[3]);
1181 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[2] * 32 + 0); /* MOV(EAX, [EBP+?]); */
1182 if (src[3] != 0xff) {
1183 jitCompPutByte1(w.dst, 0x3b); /* CMP(EAX, [EBP+?]); */
1184 jitCompA0001_85DispN(&w, 256 + src[3] * 32 + 0, 0);
1187 /* ヌルポインタとの比較はこれでいいのか?たぶんよくない */
1188 jitCompPutByte3(w.dst, 0x83, 0xf8, 0x00); /* CMP(EAX, 0); */
1193 case 0x30: /* talloc(old:F4) */
1194 case 0x31: /* tfree(old:F5) */
1195 case 0x32: /* malloc(old:F6) */
1196 case 0x33: /* mfree(old:F7) */
1197 jitCompA000_storeRegCacheAll(&w); // 手抜き.
1198 jitCompA000_storePRegCacheAll(&w); // 手抜き.
1199 jitCompPutByte2(w.dst, 0x6a, src[3]); /* PUSH(?); */
1200 jitCompPutByte2(w.dst, 0x6a, src[2]); /* PUSH(?); */
1201 jitCompPutByte2(w.dst, 0x6a, src[1]); /* PUSH(?); */
1202 jitCompPutByte1(w.dst, 0x55); /* PUSH(EBP); */
1203 jitCompPutByte1(w.dst, 0xe8);
1204 if (*src == 0x30) j = ((unsigned char *)&funcf4) - (w.dst + 4);
1205 if (*src == 0x31) j = ((unsigned char *)&funcf5) - (w.dst + 4);
1206 if (*src == 0x32) j = ((unsigned char *)&funcf6) - (w.dst + 4);
1207 if (*src == 0x33) j = ((unsigned char *)&funcf7) - (w.dst + 4);
1208 jitCompPutImm32(&w, j);
1209 jitCompPutByte3(w.dst, 0x83, 0xc4, 0x10); /* ADD(ESP,16); */
1210 jitCompA000_loadRegCacheAll(&w); // 手抜き.
1211 jitCompA000_loadPRegCacheAll(&w); // 手抜き.
1215 case 0x34: /* data (暫定) */
1217 if (w.prefix != 0) { w.err = JITC_ERR_PREFIX; goto err_w; }
1218 int k = jitCompGetImm32(&src[1]), tmpData, bitCount, dataWidth = jitCompA000_dataWidth(k);
1219 if (lastlabel >= 0 && label[lastlabel].typ == 0)
1220 label[lastlabel].typ = k;
1222 i = jitCompA000_convTyp(k);
1223 if (i < 2 || i > 7) { w.err = JITC_ERR_BADTYPE; goto err_w; }
1225 j = jitCompGetImm32(&src[5]);
1231 if (src >= src1) { w.err = JITC_ERR_SRC1; src = oldsrc; goto err_w; }
1232 if (w.dst + 256 > dst1) { w.err = JITC_ERR_DST1; src = oldsrc; goto err_w; }
1234 for (k = 0; k < dataWidth; k++) {
1235 tmpData = tmpData << 1 | ((*src >> bitCount) & 1);
1242 if ((i & 1) == 0 && dataWidth <= 31 && (tmpData >> (dataWidth - 1)) != 0) {
1243 tmpData -= 1 << dataWidth;
1245 if (i == 2 || i == 3) { jitCompPutByte1(w.dst, tmpData & 0xff); }
1246 if (i == 4 || i == 5) { jitCompPutByte2(w.dst, tmpData & 0xff, (tmpData >> 8) & 0xff); }
1247 if (i == 6 || i == 7) { jitCompPutByte4(w.dst, tmpData & 0xff, (tmpData >> 8) & 0xff, (tmpData >> 16) & 0xff, (tmpData >> 24) & 0xff); }
1253 if (src >= src1) { w.err = JITC_ERR_SRC1; src = oldsrc; goto err_w; }
1254 if (w.dst + 256 > dst1) { w.err = JITC_ERR_DST1; src = oldsrc; goto err_w; }
1255 i = jitCompGetImm32(src);
1257 if ((flags & JITC_PHASE1) != 0 && w.err == 0) {
1258 if (label[i].opt == 0) { w.err = JITC_ERR_LABELNODEF; goto err_w; }
1260 jitCompPutImm32(&w, (int)label[i].p);
1261 jitCompPutImm32(&w, label[i].typ);
1262 jitCompPutImm32(&w, (int)label[i].p);
1263 jitCompPutImm32(&w, (int)label[i].p1);
1264 jitCompPutImm32(&w, 0); /* liveSign */
1265 jitCompPutImm32(&w, 2320); /* pls */
1266 jitCompPutImm32(&w, 0);
1267 jitCompPutImm32(&w, 0);
1271 if (lastlabel >= 0 && label[lastlabel].p1 < w.dst)
1272 label[lastlabel].p1 = w.dst;
1275 case 0x3c: /* ENTER */
1276 jitCompA000_storeRegCacheAll(&w); // 手抜き.
1277 jitCompA000_storePRegCacheAll(&w); // 手抜き.
1278 jitCompPutByte2(w.dst, 0x6a, src[6]); /* PUSH(?); */
1279 jitCompPutByte2(w.dst, 0x6a, src[5]); /* PUSH(?); */
1280 jitCompPutByte2(w.dst, 0x6a, src[4] & 0x0f); /* PUSH(?); */
1281 jitCompPutByte2(w.dst, 0x6a, (src[4] >> 4) & 0x0f); /* PUSH(?); */
1282 jitCompPutByte2(w.dst, 0x6a, src[3]); /* PUSH(?); */
1283 jitCompPutByte2(w.dst, 0x6a, src[2]); /* PUSH(?); */
1284 jitCompPutByte2(w.dst, 0x6a, src[1]); /* PUSH(?); */
1285 jitCompPutByte1(w.dst, 0x55); /* PUSH(EBP); */
1286 jitCompPutByte1(w.dst, 0xe8);
1287 j = ((unsigned char *)&func3c) - (w.dst + 4);
1288 jitCompPutImm32(&w, j);
1289 jitCompPutByte3(w.dst, 0x83, 0xc4, 0x20); /* ADD(ESP,32); */
1290 jitCompA000_loadRegCacheAll(&w); // 手抜き.
1291 jitCompA000_loadPRegCacheAll(&w); // 手抜き.
1295 case 0x3d: /* LEAVE */
1296 jitCompA000_storeRegCacheAll(&w); // 手抜き.
1297 jitCompA000_storePRegCacheAll(&w); // 手抜き.
1298 jitCompPutByte2(w.dst, 0x6a, src[6]); /* PUSH(?); */
1299 jitCompPutByte2(w.dst, 0x6a, src[5]); /* PUSH(?); */
1300 jitCompPutByte2(w.dst, 0x6a, src[4] & 0x0f); /* PUSH(?); */
1301 jitCompPutByte2(w.dst, 0x6a, (src[4] >> 4) & 0x0f); /* PUSH(?); */
1302 jitCompPutByte2(w.dst, 0x6a, src[3]); /* PUSH(?); */
1303 jitCompPutByte2(w.dst, 0x6a, src[2]); /* PUSH(?); */
1304 jitCompPutByte2(w.dst, 0x6a, src[1]); /* PUSH(?); */
1305 jitCompPutByte1(w.dst, 0x55); /* PUSH(EBP); */
1306 jitCompPutByte1(w.dst, 0xe8);
1307 j = ((unsigned char *)&func3d) - (w.dst + 4);
1308 jitCompPutImm32(&w, j);
1309 jitCompPutByte3(w.dst, 0x83, 0xc4, 0x20); /* ADD(ESP,32); */
1310 jitCompA000_loadRegCacheAll(&w); // 手抜き.
1311 jitCompA000_loadPRegCacheAll(&w); // 手抜き.
1315 case 0xfe: /* remark */
1316 if (src[1] == 0x01 && src[2] == 0x00) { // DBGINFO1
1317 if (level <= JITC_LV_SLOWER) {
1318 jitCompPutByte1(w.dst, 0xb8); /* MOV(EAX, ?); */
1319 jitCompPutImm32(&w, debugInfo1);
1320 jitCompA0001_movEbpDispReg32(&w, 2304 + 4, 0 /* EAX */); /* MOV(debugInfo1, EAX); */
1323 if (src[1] == 0x01 && src[2] == 0x03) { // DBGINFO1CLR
1324 if (level <= JITC_LV_SLOWER) {
1325 jitCompPutByte1(w.dst, 0xb8); /* MOV(EAX, ?); */
1326 jitCompPutImm32(&w, -1);
1327 jitCompA0001_movEbpDispReg32(&w, 2304 + 4, 0 /* EAX */); /* MOV(debugInfo1, EAX); */
1330 if (src[1] == 0x05 && src[2] == 0x00) { // DBGINFO0
1331 if (level <= JITC_LV_SLOWEST) {
1332 debugInfo0 = jitCompGetImm32(src + 3);
1333 // jitCompPutByte1(w.dst, 0xbf); /* MOV(EDI, ?); */
1334 // jitCompPutImm32(&w, debugInfo0);
1335 jitCompPutByte1(w.dst, 0xb8); /* MOV(EAX, ?); */
1336 jitCompPutImm32(&w, debugInfo0);
1337 jitCompA0001_movEbpDispReg32(&w, 2304 + 0, 0 /* EAX */); /* MOV(debugInfo0, EAX); */
1343 w.err = JITC_ERR_OPECODE;
1346 if (w.err != 0) goto err_w;
1347 jitCompA0001_fixPrefix(&w);
1348 if (w.err != 0) goto err_w;
1349 src += jitCompCmdLen(src);
1351 if (enter0 != NULL) {
1352 j = w.dst - (enter0 + 4);
1353 enter0[0] = j & 0xff;
1354 enter0[1] = (j >> 8) & 0xff;
1355 enter0[2] = (j >> 16) & 0xff;
1356 enter0[3] = (j >> 24) & 0xff;
1358 if ((flags & JITC_NOSTARTUP) == 0) {
1359 jitCompA000_storeRegCacheAll(&w);
1360 jitCompA000_storePRegCacheAll(&w);
1361 jitCompPutByte1(w.dst, 0x61); /* POPAD(); */
1363 if ((flags & JITC_PHASE1) != 0)
1364 return w.dst - dst00;
1368 if ((w.err & JITC_ERR_PHASE0ONLY) != 0) {
1369 if ((flags & JITC_PHASE1) == 0)
1370 w.err &= ~JITC_ERR_PHASE0ONLY;
1372 if (w.err == (JITC_ERR_MASK & JITC_ERR_REGNUM)) errmsg = "reg-number error";
1373 if (w.err == (JITC_ERR_MASK & JITC_ERR_DST1)) errmsg = "dst1 error";
1374 if (w.err == (JITC_ERR_MASK & JITC_ERR_OPECODE)) errmsg = "opecode error";
1375 if (w.err == (JITC_ERR_MASK & JITC_ERR_LABELNUM)) errmsg = "label number too large";
1376 if (w.err == (JITC_ERR_MASK & JITC_ERR_LABELREDEF)) errmsg = "label redefine";
1377 if (w.err == (JITC_ERR_MASK & JITC_ERR_PREFIX)) { errmsg = "prefix redefine"; w.dst -= 2; }
1378 if (w.err == (JITC_ERR_MASK & JITC_ERR_LABELNODEF)) errmsg = "label not defined";
1379 if (w.err == (JITC_ERR_MASK & JITC_ERR_LABELTYP)) errmsg = "label type error";
1380 if (w.err == (JITC_ERR_MASK & JITC_ERR_IDIOM)) errmsg = "idiom error";
1381 if (w.err == (JITC_ERR_MASK & JITC_ERR_PREGNUM)) errmsg = "preg-number error";
1382 if (w.err == (JITC_ERR_MASK & JITC_ERR_SRC1)) errmsg = "src1 error";
1383 if (w.err == (JITC_ERR_MASK & JITC_ERR_BADTYPE)) errmsg = "bad type code";
1384 if (w.err == (JITC_ERR_MASK & JITC_ERR_PREFIXFAR)) errmsg = "prefix internal error";
1385 if (w.err == (JITC_ERR_MASK & JITC_ERR_INTERNAL)) errmsg = "general internal error";
1386 if (*errmsg != '¥0') {
1387 fprintf(stderr, "JITC: %s at %06X (debugInfo0=%d)¥n ", errmsg, src - src0, debugInfo0);
1388 for (i = 0; i < 16; i++)
1389 fprintf(stderr, "%02X ", src[i]);
1390 static char *table[0x30] = {
1391 "NOP", "LB", "LIMM", "PLIMM", "CND", "??", "??", "??",
1392 "LMEM", "SMEM", "PLMEM", "PSMEM", "LEA", "??", "PADD", "PDIF",
1393 "CP/OR", "XOR", "AND", "??", "ADD", "SUB", "MUL", "??",
1394 "SHL", "SAR", "DIV", "MOD", "PLMT0", "PLMT1", "PCP", "PCST",
1395 "CMPE", "CMPNE", "CMPL", "CMPGE", "CMPLE", "CMPG", "TSTZ", "TSTNZ",
1396 "PCMPE", "PCMPNE", "PCMPL", "PCMPGE", "PCMPLE", "PCMPG", "??", "EXT" };
1398 if (*src < 0x30) errmsg = table[*src];
1399 fprintf(stderr, "(%s)¥n", errmsg);
1404 unsigned char *jitCompCallFunc(unsigned char *dst, void *func)
1406 struct JitCompWork w;
1408 jitCompA000_storeRegCacheAll(&w);
1409 jitCompA000_storePRegCacheAll(&w);
1410 jitCompPutByte1(w.dst, 0x60); /* PUSHAD(); */
1411 jitCompPutByte1(w.dst, 0x50); /* PUSH(EAX); */ /* for 16byte-align(win32では不要なのだけど、MacOSには必要らしい) */
1412 jitCompPutByte1(w.dst, 0x55); /* PUSH(EBP); */
1413 jitCompPutByte1(w.dst, 0xe8); /* CALL(func); */
1414 int j = ((unsigned char *)func) - (w.dst + 4);
1415 jitCompPutImm32(&w, j);
1416 jitCompPutByte1(w.dst, 0x58); /* POP(EAX); */ /* (win32では不要なのだけど、MacOSには必要らしい) */
1417 jitCompPutByte1(w.dst, 0x58); /* POP(EAX); */
1418 jitCompPutByte1(w.dst, 0x61); /* POPAD(); */
1419 jitCompA000_loadRegCacheAll(&w);
1420 jitCompA000_loadPRegCacheAll(&w);
1421 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + 0x30 * 32 + 0); /* MOV(EAX, [EBP+?]); */
1422 jitCompPutByte2(w.dst, 0xff, 0xe0); /* JMP(EAX); */
1426 unsigned char *jitCompInit(unsigned char *dst)
1429 return jitCompCallFunc(dst, &errHndl);
1432 void func3c(char *ebp, int opt, int r1, int p1, int lenR, int lenP, int r0, int p0)
1434 struct Regs *r = (struct Regs *) (ebp - jitCompA0001_EBP128);
1437 if (r->junkStack + 2048 > r->junkStack1) (*(r->errHndl))(r);
1438 pi = (void *)r->junkStack; r->junkStack += r1 * 4;
1439 for (i = 0; i < r1; i++)
1441 pp = (void *)r->junkStack; r->junkStack += p1 * 32;
1442 for (i = 0; i < p1; i++)
1444 pp = (void *)r->junkStack; r->junkStack += 32;
1445 *pp = r->preg[0x30];
1446 pi = (void *)r->junkStack; r->junkStack += 4;
1447 *pi = opt << 16 | r1 << 8 | p1;
1448 for (i = 0; i < lenR; i++)
1449 r->ireg[r0 + i] = r->ireg[0x30 + i];
1450 for (i = 0; i < lenP; i++)
1451 r->preg[p0 + i] = r->preg[0x31 + i];
1455 void func3d(char *ebp, int opt, int r1, int p1, int lenR, int lenP, int r0, int p0)
1457 struct Regs *r = (struct Regs *) (ebp - jitCompA0001_EBP128);
1460 r->junkStack -= 32; struct Ptr *pp = (void *)r->junkStack;
1461 r->preg[0x30] = *pp;
1462 r->junkStack -= p1 * 32; pp = (void *)r->junkStack;
1463 for (i = 0; i < p1; i++)
1465 r->junkStack -= r1 * 4; int *pi = (void *)r->junkStack;
1466 for (i = 0; i < r1; i++)
1471 void funcf4(char *ebp, int pxx, int typ, int len)
1473 struct Regs *r = (struct Regs *) (ebp - jitCompA0001_EBP128);
1474 int width = jitCompA000_dataWidth(jitCompA000_convTyp(r->ireg[typ])) >> 3;
1475 if (width < 0 || r->ireg[len] < 0)
1477 void *p = r->junkStack;
1478 if (r->junkStack + width * r->ireg[len] + 256 > r->junkStack1) (*(r->errHndl))(r);
1479 r->junkStack += width * r->ireg[len];
1481 r->preg[pxx].typ = r->ireg[typ];
1482 r->preg[pxx].p0 = p;
1483 r->preg[pxx].p1 = (void *)r->junkStack;
1484 int *pi = (int *)r->junkStack;
1485 *pi = width * r->ireg[len];
1486 r->junkStack += sizeof (int);
1487 if (r->ireg[typ] == 1) {
1488 int i, i1 = (width * r->ireg[len]) >> 2;
1490 for (i = 0; i < i1; i++)
1496 void funcf5(char *ebp, int pxx, int typ, int len)
1498 struct Regs *r = (struct Regs *) (ebp - jitCompA0001_EBP128);
1499 r->junkStack -= sizeof (int);
1500 int *pi = (int *)r->junkStack;
1501 r->junkStack -= *pi;
1503 int width = jitCompA000_dataWidth(r->ireg[typ]);
1504 void *p = r->junkStack;
1505 r->junkStack -= width * r->ireg[len];
1510 void funcf6(char *ebp, int pxx, int typ, int len)
1512 struct Regs *r = (struct Regs *) (ebp - jitCompA0001_EBP128);
1513 int width = jitCompA000_dataWidth(jitCompA000_convTyp(r->ireg[typ])) >> 3;
1514 if (width < 0 || r->ireg[len] < 0)
1516 void *p = malloc(width * r->ireg[len]);
1518 r->preg[pxx].typ = r->ireg[typ];
1519 r->preg[pxx].p0 = p;
1520 r->preg[pxx].p1 = (unsigned char *)p + width * r->ireg[len];
1521 if (r->ireg[typ] == 1) {
1522 int i, i1 = (width * r->ireg[len]) >> 2, *pi;
1524 for (i = 0; i < i1; i++)
1526 for (i = 1; i < i1; i += 8)
1532 void funcf7(char *ebp, int pxx, int typ, int len)
1534 struct Regs *r = (struct Regs *) (ebp - jitCompA0001_EBP128);
1535 free(r->preg[pxx].p);
1539 void errHndl(struct Regs *r)
1541 r = (struct Regs *) (((char *)r) - jitCompA0001_EBP128);
1546 int jitc0(unsigned char **qq, unsigned char *q1, const unsigned char *p0, const unsigned char *p1, int level, struct LabelTable *label)
1548 unsigned char *q = *qq;
1549 if (p0[0] != 0x05 || p0[1] != SIGN1)
1552 *q++ = 0x55; /* PUSH(EBP); */
1553 *q++ = 0x8b; *q++ = 0x6c; *q++ = 0x24; *q++ = 0x08; /* MOV(EBP,[ESP+8]); */
1556 for (i = 0; i < JITC_MAXLABELS; i++)
1559 i = jitCompiler(q, q1, p0 + 2, p1, p0, label, JITC_MAXLABELS, level, di1_serial, 0);
1560 if (i != 0) return 2;
1561 i = jitCompiler(q, q1, p0 + 2, p1, p0, label, JITC_MAXLABELS, level, di1_serial, JITC_PHASE1 + 0);
1562 if (i < 0) return 2;
1565 *q++ = 0x5d; /* POP(EBP); */
1566 *q++ = 0xc3; /* RET(); */
1572 #if (USE_DEBUGGER != 0)
1574 int dbgrGetRegNum(const unsigned char *p)
1580 if (i > 9) i -= 'A' - '0' - 10;
1581 if (j > 9) j -= 'A' - '0' - 10;
1582 if (0 <= i && i <= 15 && 0 <= j && j <= 15)
1588 void dbgrMain(struct Regs *r)
1590 if (r->dbgr == 0) return;
1592 unsigned char cmd[64], *p;
1595 p = fgets(cmd, 64, stdin);
1596 if (p == NULL) break;
1597 if (cmd[0] == '¥0') continue;
1598 if (cmd[0] == 'q' && cmd[1] <= ' ') break;
1599 if (cmd[0] == 'p' && cmd[1] <= ' ' && cmd[1] != '¥0') {
1601 while (*p <= ' ' && *p != '¥0') p++;
1603 i = dbgrGetRegNum(p + 1);
1604 if (0 <= i && i <= 0x3f) {
1605 printf("R%02X = 0x%08X = %d¥n", i, r->ireg[i], r->ireg[i]);
1608 puts("register name error");
1612 i = dbgrGetRegNum(p + 1);
1613 if (0 <= i && i <= 0x3f) {
1615 if (0 <= r->preg[i].typ && r->preg[i].typ <= 0x15) {
1616 static unsigned char *typName[] = {
1617 "T_CODE", "T_VPTR", "T_SINT8", "T_UINT8",
1618 "T_SINT16", "T_UINT16", "T_SINT32", "T_UINT32",
1619 "T_SINT4", "T_UINT4", "T_SINT2", "T_UINT2",
1620 "T_SINT1", "T_UINT1", "T_SINT12", "T_UINT12",
1621 "T_SINT20", "T_UINT20", "T_SINT24", "T_UINT24",
1622 "T_SINT28", "T_UINT28"
1624 p = typName[r->preg[i].typ];
1626 printf("P%02X:¥n type = %s(%04X), (origin-ptr) = 0x%08X¥n", i, p, r->preg[i].typ, r->preg[i].p0);
1627 if (r->preg[i].p != NULL && r->preg[i].p0 != NULL) {
1628 j = jitCompA000_dataWidth(jitCompA000_convTyp(r->preg[i].typ)) >> 3;
1630 k = (r->preg[i].p1 - r->preg[i].p0) / j;
1631 printf(" size = 0x%08X = %d¥n", k, k);
1632 k = (r->preg[i].p - r->preg[i].p0) / j;
1633 printf(" pos = 0x%08X = %d¥n", k, k);
1636 puts(" null pointer");
1640 puts("register name error");
1644 puts("command error");