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(HOSECPU_RuntimeEnvironment *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)
34 if (0x01 <= *src && *src < 0x04){
36 } else if (*src == 0x04){
38 } else if (0x08 <= *src && *src < 0x0d){
40 } else if (0x0e <= *src && *src < 0x10){
42 } else if (0x10 <= *src && *src < 0x2e){
44 } else if (0x1c <= *src && *src < 0x1f){
46 } else if (*src == 0x1f){
48 } else if (*src == 0x2f){
50 } else if (0x30 <= *src && *src <= 0x33){
52 } else if (0x3c <= *src && *src <= 0x3d){
54 } else if (*src == 0xfe){
59 #if (JITC_ARCNUM == 0x0001)
65 以下は最適化のためのものなので、すべて0として簡単に移植しても問題ありません */
66 #define jitCompA0001_USE_R3F_CMPJMP 1*1
67 #define jitCompA0001_USE_R3F_IMM32 1*1
68 #define jitCompA0001_USE_R3F_IMM8 1*1
69 #define jitCompA0001_USE_R3F_INCDEC 1*1
70 #define jitCompA0001_OPTIMIZE_JMP 1*1
71 #define jitCompA0001_OPTIMIZE_MOV 1*1 /* 1にすると速度低下する? */
72 #define jitCompA0001_OPTIMIZE_CMP 1*1
73 #define jitCompA0001_OPTIMIZE_ALIGN 4*1 /* 0-8を想定 */
74 #define jitCompA0001_EBP128 128*1
77 unsigned char *dst, *dst0;
79 #if (jitCompA0001_USE_R3F_IMM32 != 0)
82 char prefix; //CND命令の値を記録(初期値=0)
85 #define jitCompPutByte1(p, c0) *p++ = c0
86 #define jitCompPutByte2(p, c0, c1) *p++ = c0; *p++ = c1
87 #define jitCompPutByte3(p, c0, c1, c2) *p++ = c0; *p++ = c1; *p++ = c2
88 #define jitCompPutByte4(p, c0, c1, c2, c3) *p++ = c0; *p++ = c1; *p++ = c2; *p++ = c3
90 static void jitCompPutImm32(struct JitCompWork *w, int i)
92 jitCompPutByte1(w->dst, i & 0xff);
93 jitCompPutByte1(w->dst, (i >> 8) & 0xff);
94 jitCompPutByte1(w->dst, (i >> 16) & 0xff);
95 jitCompPutByte1(w->dst, (i >> 24) & 0xff);
99 int jitCompGetImm32(const unsigned char *src)
101 return (src[0] << 24) | (src[1] << 16) | (src[2] << 8) | src[3];
104 int jitCompGetLabelNum(struct JitCompWork *w, const unsigned char *src)
106 int i = jitCompGetImm32(src);
107 if (i < 0 || i >= w->maxLabels) {
108 w->err = JITC_ERR_LABELNUM;
114 void jitCompA0001_85DispN(struct JitCompWork *w, int disp, int n)
116 disp -= jitCompA0001_EBP128;
117 if (-128 <= disp && disp <= 127) {
118 jitCompPutByte2(w->dst, 0x45 | (n << 3), disp & 0xff);
120 jitCompPutByte1(w->dst, 0x85 | (n << 3));
121 jitCompPutImm32(w, disp);
126 void jitCompA0001_movEbpDispReg32(struct JitCompWork *w, int disp, int reg32)
128 jitCompPutByte1(w->dst, 0x89); /* MOV(mem, reg32); */
129 jitCompA0001_85DispN(w, disp, reg32);
133 void jitCompA0001_movReg32EbpDisp(struct JitCompWork *w, int reg32, int disp)
135 jitCompPutByte1(w->dst, 0x8b); /* MOV(reg32, mem); */
136 jitCompA0001_85DispN(w, disp, reg32);
140 void jitCompA0001_movEaxRxx(struct JitCompWork *w, int rxx)
142 #if (jitCompA0001_USE_R3F_IMM32 != 0)
144 jitCompPutByte1(w->dst, 0xb8); /* MOV(EAX, ?); */
145 jitCompPutImm32(w, w->r3f);
149 if (rxx >= 0x40 || rxx < 0){
150 w->err = JITC_ERR_REGNUM;
152 jitCompA0001_movReg32EbpDisp(w, 0 /* EAX */, rxx * 4); /* MOV(EAX, [EBP+?]); */
156 void jitCompA0001_movRxxEax(struct JitCompWork *w, int rxx)
158 if (rxx >= 0x40 || rxx < 0){
159 w->err = JITC_ERR_REGNUM;
161 jitCompA0001_movEbpDispReg32(w, rxx * 4, 0 /* EAX */); /* MOV([EBP+?], EAX); */
165 void jitCompA0001_fixPrefix(struct JitCompWork *w)
167 if (w->prefix != 0) {
168 if (w->dst - w->dst0 > 127){
169 w->err = JITC_ERR_REGNUM;
171 w->dst0[-1] = (unsigned char)((w->dst - w->dst0) & 0xff);
176 void jitCompA0001_checkCompPtr(struct JitCompWork *w, int p0, int p1)
178 if (p0 >= 0x3f || p0 < 0){
179 w->err = JITC_ERR_PREGNUM;
181 if (p1 >= 0x3f || p1 < 0){
182 w->err = JITC_ERR_PREGNUM;
184 /* 比較可能可能なのかのチェックのコードを出力 */ /* 未完成 */
188 void jitCompA000_loadRegCacheAll(struct JitCompWork *w)
190 jitCompA0001_movReg32EbpDisp(w, 3 /* EBX */, 0 * 4); /* EBX = R00; */
191 jitCompA0001_movReg32EbpDisp(w, 1 /* ECX */, 1 * 4); /* ECX = R01; */
192 jitCompA0001_movReg32EbpDisp(w, 2 /* EDX */, 2 * 4); /* EDX = R02; */
196 void jitCompA000_storeRegCacheAll(struct JitCompWork *w)
198 jitCompA0001_movEbpDispReg32(w, 0 * 4, 3 /* EBX */); /* R00 = EBX; */
199 jitCompA0001_movEbpDispReg32(w, 1 * 4, 1 /* ECX */); /* R01 = ECX; */
200 jitCompA0001_movEbpDispReg32(w, 2 * 4, 2 /* EDX */); /* R02 = EDX; */
204 void jitCompA000_loadRegCacheEcx(struct JitCompWork *w)
206 jitCompA0001_movReg32EbpDisp(w, 1 /* ECX */, 1 * 4); /* ECX = R01; */
210 void jitCompA000_storeRegCacheEcx(struct JitCompWork *w)
212 jitCompA0001_movEbpDispReg32(w, 1 * 4, 1 /* ECX */); /* R01 = ECX; */
216 void jitCompA000_loadRegCacheEdx(struct JitCompWork *w)
218 jitCompA0001_movReg32EbpDisp(w, 2 /* EDX */, 2 * 4); /* EDX = R02; */
222 void jitCompA000_storeRegCacheEdx(struct JitCompWork *w)
224 jitCompA0001_movEbpDispReg32(w, 2 * 4, 2 /* EDX */); /* R02 = EDX; */
228 int jitCompA000_selectRegCache(int rxx, int reg)
247 void jitCompA000_loadPRegCacheAll(struct JitCompWork *w)
249 // jitCompA0001_movReg32EbpDisp(w, 5 /* EBP */, 256 + 0 * 32 + 0); /* EBP = P00; */
250 jitCompA0001_movReg32EbpDisp(w, 6 /* ESI */, 256 + 1 * 32 + 0); /* ESI = P01; */
251 jitCompA0001_movReg32EbpDisp(w, 7 /* EDI */, 256 + 2 * 32 + 0); /* EDI = P02; */
255 void jitCompA000_storePRegCacheAll(struct JitCompWork *w)
257 // jitCompA0001_movEbpDispReg32(w, 256 + 0 * 32 + 0, 5 /* EBP */); /* P00 = EBP; */
258 jitCompA0001_movEbpDispReg32(w, 256 + 1 * 32 + 0, 6 /* ESI */); /* P01 = ESI; */
259 jitCompA0001_movEbpDispReg32(w, 256 + 2 * 32 + 0, 7 /* EDI */); /* P02 = EDI; */
263 int jitCompA000_selectPRegCache(int pxx, int reg)
265 // if (pxx == 0) reg = 5; /* EBP */
280 int jitCompA000_convTyp(int t)
283 if (1 <= t && t <= 7){
285 } else if (8 <= t && t <= 13){
287 } else if (14 <= t && t <= 15){
289 } else if (16 <= t && t <= 21){
295 int jitCompA000_dataWidth(int t)
298 if (t == 0x0001) r = 256;
300 if (t == 0x0002 / 2) r = 8;
301 if (t == 0x0004 / 2) r = 16;
302 if (t == 0x0006 / 2) r = 32;
303 if (t == 0x0008 / 2) r = 4;
304 if (t == 0x000a / 2) r = 2;
305 if (t == 0x000c / 2) r = 1;
306 if (t == 0x000e / 2) r = 12;
307 if (t == 0x0010 / 2) r = 20;
308 if (t == 0x0012 / 2) r = 24;
309 if (t == 0x0014 / 2) r = 28;
313 static unsigned char *errfnc;
315 void jitCompA0001_checkType0(struct JitCompWork *w, int pxx, int typ, int ac)
317 if (typ <= 0) { w->err = JITC_ERR_BADTYPE; }
318 if (typ > 0x7f) { w->err = JITC_ERR_INTERNAL; }
319 jitCompA0001_movReg32EbpDisp(w, 0 /* EAX */, 256 + pxx * 32 + 4); /* MOV(EAX, [EBP+?]); */ /* typ */
320 jitCompPutByte3(w->dst, 0x83, 0xf8, typ & 0x7f); /* CMP(EAX, ?); */
321 jitCompPutByte2(w->dst, 0x0f, 0x85); /* JNE */
322 jitCompPutImm32(w, errfnc - (w->dst + 4));
326 void jitCompA0001_checkType(struct JitCompWork *w, int pxx, int typ, int ac)
328 // 将来的にはaliveやアクセス権チェックも入れる
330 jitCompA0001_checkType0(w, pxx, typ, ac);
334 void jitCompA0001_checkLimit(struct JitCompWork *w, int reg, int pxx)
336 jitCompPutByte1(w->dst, 0x3b); /* CMP(reg, [EBP+?]); */
337 jitCompA0001_85DispN(w, 256 + pxx * 32 + 8, reg); /* p0 */
338 jitCompPutByte2(w->dst, 0x0f, 0x82); /* JB */
339 jitCompPutImm32(w, errfnc - (w->dst + 4));
340 jitCompPutByte1(w->dst, 0x3b); /* CMP(reg, [EBP+?]); */
341 jitCompA0001_85DispN(w, 256 + pxx * 32 + 12, reg); /* p1 */
342 jitCompPutByte2(w->dst, 0x0f, 0x83); /* JAE */
343 jitCompPutImm32(w, errfnc - (w->dst + 4));
347 void func3c(char *ebp, int opt, int r1, int p1, int lenR, int lenP, int r0, int p0);
348 void func3d(char *ebp, int opt, int r1, int p1, int lenR, int lenP, int r0, int p0);
349 void funcf4(char *ebp, int pxx, int typ, int len);
350 void funcf5(char *ebp, int pxx, int typ, int len); // pxxはダミーで参照されない.
351 void funcf6(char *ebp, int pxx, int typ, int len);
352 void funcf7(char *ebp, int pxx, int typ, int len); // typとlenはダミーで参照されない.
353 // F5の場合、decoderが対応するalloc-freeを結びつけるのが簡単で、typやlenを指定必須にしてもフロントエンドコードに影響はない.
355 void errHndl(HOSECPU_RuntimeEnvironment *r);
359 * dst1 : 書き込みアドレスの最大値
360 * src : 現在の読み込みアドレス(ヘッダ部は飛ばしてある
361 * src1 : 読み込みアドレスの最大値
362 * src0 : 読み込みバイナリのアドレス
364 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)
366 /* 本来ならこのレイヤでは文法チェックしない */
368 struct JitCompWork w;
369 unsigned char *dst00 = dst, *enter0 = NULL, *tmp_ucp;
371 const unsigned char *oldsrc;
372 int timecount = 0, i, j = 0, lastlabel = -1, debugInfo0 = -1;
373 int reg0, reg1, reg2, cmp0reg = -1, cmp0lev = 0;
374 w.dst = w.dst0 = dst;
376 w.maxLabels = maxLabels;
378 if ((flags & JITC_NOSTARTUP) == 0) {
379 jitCompPutByte1(w.dst, 0x60); /* PUSHAD(); */
380 jitCompA000_loadRegCacheAll(&w); /* start-up */
381 jitCompA000_loadPRegCacheAll(&w);
383 if (level <= JITC_LV_SLOWER) {
384 jitCompPutByte2(w.dst, 0x31, 0xc0); /* XOR(EAX, EAX); */
385 jitCompA0001_movEbpDispReg32(&w, 2304 + 0, 0 /* EAX */); /* MOV(debugInfo0, EAX); */
386 jitCompPutByte1(w.dst, 0xb8); /* MOV(EAX, ?); */
387 jitCompPutImm32(&w, debugInfo1);
388 jitCompA0001_movEbpDispReg32(&w, 2304 + 4, 0 /* EAX */); /* MOV(debugInfo1, EAX); */
391 w.prefix = 0; //0x04 CND 命令で変更される
392 if (w.dst + 256 > dst1) { w.err = JITC_ERR_DST1; goto err_w; } // 書き込み領域が残り256バイト未満ならエラー
394 if (timecount >= 64) {
396 /* 未完成(timeoutチェックコードを入れる) */
398 prefix_continue: // CND命令実行後ここに戻る
402 if (w.prefix != 0) { w.err = JITC_ERR_PREFIX; goto err_w; } // 「条件付きでNOPを実行」するなんて、矛盾している!
408 * LB : ラベル設置命令。(6byte)
410 * ・timecount++し、timecountのチェックをする。
412 * ・割り込みがある場合、このタイミングで割り込みを発生させる。
419 if (enter0 == NULL && (src[6] == 0x3c /* 多数のレジスタをスタックに退避 */ || (src[6] == 0xfe/* REMARK */ && src[7] == 0x01 && src[9] == 0x3c))) { //beginFunc()中のLB
420 // LB命令の後に0x3C命令・・・beginFunc()
421 jitCompPutByte1(w.dst, 0xe9); // (x86) JMP rel32 : 次の命令との相対オフセットだけ相対ニアジャンプする
423 jitCompPutImm32(&w, 0); // 飛び相対座標が0 ・・・パイプラインのフラッシュ??
425 if (src[6] == 0x34) { // LBの次の命令がDATA ・・・DAT_SA0(label, typ32, length) ・・・メモリ確保命令
427 jitCompPutByte1(w.dst, 0xe9); // (x86) JMP rel32 : 次の命令との相対オフセットだけ相対ニアジャンプする
428 i = jitCompGetImm32(&src[7]); // type32 を取得
431 i = jitCompA000_convTyp(i);
433 if (i == 2 || i == 3) { j = 1; }
434 if (i == 4 || i == 5) { j = 2; }
435 if (i == 6 || i == 7) { j = 4; }
437 j *= jitCompGetImm32(&src[11]);
438 if (j <= 0) w.err = JITC_ERR_BADTYPE;
439 jitCompPutImm32(&w, j);
440 #if (jitCompA0001_OPTIMIZE_JMP != 0)
441 if (j <= 127 - jitCompA0001_OPTIMIZE_ALIGN) {
443 jitCompPutByte2(w.dst, 0xeb, j);
447 #if (jitCompA0001_OPTIMIZE_ALIGN != 0)
449 i = ((int)w.dst) & (jitCompA0001_OPTIMIZE_ALIGN - 1); /* ALIGNで割ったあまりを計算 */
451 i = jitCompA0001_OPTIMIZE_ALIGN - i;
452 if (i == 1) { jitCompPutByte1(w.dst, 0x90); j += i; } /* NOP(); */
453 if (i == 2) { jitCompPutByte2(w.dst, 0x89, 0xc0); j += i; } /* MOV(EAX, EAX); */
454 if (i == 3) { jitCompPutByte3(w.dst, 0x8d, 0x76, 0x00); j += i; } /* LEA(ESI, [ESI+0]); */
455 if (i == 4) { jitCompPutByte4(w.dst, 0x8d, 0x74, 0x26, 0x00); j += i; } /* LEA(ESI, [ESI*1+0]); */
456 if (i == 5) { jitCompPutByte1(w.dst, 0x0d); jitCompPutImm32(&w, 0); j += i; } /* OR(EAX, 0); */
457 if (i == 6) { jitCompPutByte2(w.dst, 0x8d, 0xb6); jitCompPutImm32(&w, 0); j += i; } /* LEA(ESI, [ESI+0]); */
458 if (i >= 7) { jitCompPutByte3(w.dst, 0x8d, 0xb4, 0x26); jitCompPutImm32(&w, 0); j += 7; } /* LEA(ESI, [ESI*1+0]); */
461 if (src[6] == 0x34) {
462 tmp_ucp[1] = j & 0xff;
463 if (*tmp_ucp == 0xe9) {
464 tmp_ucp[2] = (j >> 8) & 0xff;
465 tmp_ucp[3] = (j >> 16) & 0xff;
466 tmp_ucp[4] = (j >> 24) & 0xff;
469 if ((flags & JITC_PHASE1) == 0) {
470 i = jitCompGetLabelNum(&w, src + 2);
471 //printf("i=%06X %06X\n", i, src-src0);
472 if (label[i].opt != 0 && w.err == 0) { w.err = JITC_ERR_LABELREDEF; goto err_w; }
473 if (w.prefix != 0) { w.err = JITC_ERR_PREFIX; goto err_w; }
474 label[i].opt = src[1] + 1;
475 label[i].typ = 0; /* TYP_CODE */
477 label[i].p1 = w.dst + 1;
482 /* 未完成(timeoutチェックコードを入れる) */
485 case 0x02: /* LIMM */
488 * LIMM : 定数即値代入命令(6byte)
493 * ・reg3F は条件比較慣用句指定用&演算命令即値慣用句指定用。よってCND命令の直後では使用できない。
496 if (src[1] == 0x3f && w.prefix != 0) w.err = JITC_ERR_PREFIX; // CND命令の直後でR3Fを書き換えるなんて変だよね
498 #if (jitCompA0001_USE_R3F_IMM32 != 0)
499 if (src[1] == 0x3f) { // R3Fへの代入は例外敵に、 w.r3f を使用
500 w.r3f = jitCompGetImm32(src + 2);
504 i = jitCompGetImm32(src + 2); // 与えられた即値(第二引数)を取得
506 /* R00-R02 なら EBX, ECX, EDX 、それ以外なら EAX のレジスタIDを reg0 に代入 */
507 reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
509 #if (jitCompA0001_OPTIMIZE_MOV != 0)
511 jitCompPutByte2(w.dst, 0x31, 0xc0 | reg0 << 3 | reg0); /* XOR(reg0, reg0); */
512 jitCompA0001_movRxxEax(&w, src[1]);
517 /* reg0 のレジスタに対応したMOV命令を発行 */
518 jitCompPutByte1(w.dst, 0xb8 | reg0); /* MOV(reg0, ?); == 10111000b+wr imm32 */
519 jitCompPutImm32(&w, i);
521 if (reg0 == 0) // R03以降の、レジスタの内容をメモリ上に格納してエミュレートする場合
523 jitCompA0001_movRxxEax(&w, src[1]);
526 case 0x03: /* PLIMM */ /* 未完成(plsまで対応) */
529 * PLIMM : ラベル番号代入命令(6byte)
539 i = jitCompGetLabelNum(&w, src + 2); // Pxxに代入するラベルの番号(第二引数)
540 if ((flags & JITC_PHASE1) != 0 && w.err == 0) { // Phase 1であるならば
541 if (label[i].opt == 0) { w.err = JITC_ERR_LABELNODEF; goto err_w; } // 指定されたラベル番号は存在しない
542 if (src[1] != 0x3f && label[i].opt != 2) { w.err = JITC_ERR_LABELTYP; goto err_w; } //
543 if (src[1] == 0x3f && label[i].typ != 0) { w.err = JITC_ERR_LABELTYP; goto err_w; } // プログラムカウンタに TYP_CODEでない値は代入できない
545 if (src[1] == 0x3f) { // プログラムカウンタへの代入なら
546 if (w.prefix == 0) { // CND命令による条件付きでなければ、即座に移動
547 jitCompPutByte1(w.dst, 0xe9); /* JMP(?); */
556 * いま、dstの末端はJZ命令になっている。 0x0F 0x84 cd
560 w.dst[-1] = w.dst[-2] ^ 0xf1; /* 74->85, 75->84 */
566 if ((flags & JITC_PHASE1) != 0 || (((flags & JITC_PHASE1) == 0) && label[i].opt != 0)) // label番号iが確保されていれば (このif文は意味をなさない)
567 j = label[i].p - (w.dst + 4); // j はとび先の相対番地
568 jitCompPutImm32(&w, j); // JMP もしくは JZ 命令のアドレス部を記述
569 #if (jitCompA0001_OPTIMIZE_JMP != 0)
570 if (-128 - 3 <= j && j < 0) {
571 if (w.dst[-5] == 0xe9) {
574 jitCompPutByte1(w.dst, 0xeb); /* JMP(?); */
579 jitCompPutByte1(w.dst, w.dst[1] ^ 0xf0);
581 jitCompPutByte1(w.dst, j & 0xff);
585 else { // プログラムカウンタ以外への代入
587 // 代入先が P01, P02なら ESI, EDI,それ以外ならEAXを指定
588 reg0 = jitCompA000_selectPRegCache(src[1], 0 /* EAX */);
589 jitCompPutByte1(w.dst, 0xb8 | reg0); /* MOV(reg0, ?); */
590 jitCompPutImm32(&w, (int)label[i].p); // ラベルのパスを各レジスタに代入
592 // レジスタへの代入をメモリでエミュレーションする場合は、スタックに積む。
594 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32, 0); /* MOV([EBP+?], EAX); */
596 if (level < JITC_LV_FASTEST) {
597 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 8, reg0); /* MOV([EBP+?], reg0); */ /* p0 */
598 jitCompPutByte1(w.dst, 0xb8); /* MOV(EAX, ?); */
599 jitCompPutImm32(&w, label[i].typ);
600 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 4, 0); /* MOV([EBP+?], EAX); */ /* typ */
601 jitCompPutByte1(w.dst, 0xb8); /* MOV(EAX, ?); */
602 jitCompPutImm32(&w, (int)label[i].p1);
603 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 12, 0); /* MOV([EBP+?], EAX); */ /* p1 */
604 jitCompPutByte2(w.dst, 0x31, 0xc0); /* XOR(EAX, EAX); */
605 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 16, 0); /* MOV([EBP+?], EAX); */ /* liveSign */
606 jitCompA0001_movReg32EbpDisp(&w, 0, 2320); /* MOV(EAX, ptrCtrl); */
607 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 20, 0); /* MOV([EBP+?], EAX); */ /* pls */
612 case 0x04: /* CND (prefix) */
616 * 与えられたRxxの最下位ビットが1であれば後続の命令を実行、そうでなければ飛ばす。
619 if (src[1] >= 0x40) w.err = JITC_ERR_REGNUM; // R00-R3F 以外のレジスタは比較対象にできない
621 // 比較対象のレジスタがメモリ上にあれば-1, それ以外なら適切なレジスタ番号を返す
622 reg0 = jitCompA000_selectRegCache(src[1], -1 /* mem */);
625 if (reg0 < 0) { //比較対象のレジスタはメモリ上にある
626 jitCompPutByte1(w.dst, 0xf7); /* TEST([EBP+?],1); */
627 jitCompA0001_85DispN(&w, src[1] * 4, 0);
630 jitCompPutByte2(w.dst, 0xf7, 0xc0 | reg0); /* TEST(reg0,1); */
632 jitCompPutImm32(&w, 1);
635 jitCompPutByte2(w.dst, 0x74, 0x00); /* JZ($+2) */
637 if (w.err != 0) goto err_w;
639 w.prefix = 1; // プリフィックスをセット
641 goto prefix_continue;
643 case 0x08: /* LMEM */ /* 完成 */
644 i = jitCompGetImm32(src + 2);
645 if (i == 0x0001) w.err = JITC_ERR_BADTYPE;
646 if (level < JITC_LV_FASTER) {
647 jitCompA0001_checkType(&w, src[6], i, 0); // read
650 reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
651 reg1 = jitCompA000_selectPRegCache(src[6], 2 /* EDX */);
652 if (reg0 != 0 /* EAX */ && reg1 == 2 /* EDX */)
654 if (reg1 == 2 /* EDX */)
655 jitCompA000_storeRegCacheEdx(&w);
656 if (reg1 <= 3 /* EAX, EDX */)
657 jitCompA0001_movReg32EbpDisp(&w, reg1, 256 + src[6] * 32 + 0); /* MOV(reg1, [EBP+?]); */
658 if (level < JITC_LV_FASTER)
659 jitCompA0001_checkLimit(&w, reg1, src[6]);
660 i = jitCompA000_convTyp(jitCompGetImm32(src + 2));
663 jitCompPutByte3(w.dst, 0x0f, 0xbe, reg0 << 3 | reg1); /* MOVSX(reg0,BYTE [reg1]); */
666 jitCompPutByte3(w.dst, 0x0f, 0xb6, reg0 << 3 | reg1); /* MOVZX(reg0,BYTE [reg1]); */
669 jitCompPutByte3(w.dst, 0x0f, 0xbf, reg0 << 3 | reg1); /* MOVSX(reg0,WORD [reg1]); */
672 jitCompPutByte3(w.dst, 0x0f, 0xb7, reg0 << 3 | reg1); /* MOVZX(reg0,WORD [reg1]); */
676 jitCompPutByte2(w.dst, 0x8b, reg0 << 3 | reg1); /* MOV(reg0, [reg1]); */
679 w.err = JITC_ERR_BADTYPE;
681 if (reg0 == 0 /* EAX */)
682 jitCompA0001_movRxxEax(&w, src[1]);
683 if (reg1 == 2 /* EDX */)
684 jitCompA000_loadRegCacheEdx(&w);
687 case 0x09: /* SMEM */ /* 完成 */
688 i = jitCompGetImm32(src + 2);
689 if (i == 0x0001) w.err = JITC_ERR_BADTYPE;
690 if (level < JITC_LV_FASTER) {
691 jitCompA0001_checkType(&w, src[6], i, 1); // write
694 reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
695 reg1 = jitCompA000_selectPRegCache(src[6], 2 /* EDX */);
696 if (reg0 != 0 /* EAX */ && reg1 == 2 /* EDX */)
698 if (reg1 == 2 /* EDX */)
699 jitCompA000_storeRegCacheEdx(&w);
700 if (reg1 <= 3 /* EAX, EDX */)
701 jitCompA0001_movReg32EbpDisp(&w, reg1, 256 + src[6] * 32 + 0); /* MOV(reg1, [EBP+?]); */
702 if (level < JITC_LV_FASTER)
703 jitCompA0001_checkLimit(&w, reg1, src[6]);
704 if (reg0 == 0 /* EAX */)
705 jitCompA0001_movEaxRxx(&w, src[1]);
707 i = jitCompA000_convTyp(jitCompGetImm32(src + 2));
711 jitCompPutByte2(w.dst, 0x88, reg0 << 3 | reg1); /* MOV([reg1], BYTE(reg0)); */
715 jitCompPutByte3(w.dst, 0x66, 0x89, reg0 << 3 | reg1); /* MOV([reg1], WORD(reg0)); */
719 jitCompPutByte2(w.dst, 0x89, reg0 << 3 | reg1); /* MOV([reg1], reg0); */
722 w.err = JITC_ERR_BADTYPE;
724 if (reg1 == 2 /* EDX */)
725 jitCompA000_loadRegCacheEdx(&w);
728 case 0x0a: /* PLMEM */ /* 完成 */
729 i = jitCompGetImm32(src + 2);
730 if (i != 0x0001) w.err = JITC_ERR_BADTYPE;
731 if (level < JITC_LV_FASTER) {
732 jitCompA0001_checkType(&w, src[6], i, 0); // read
735 reg0 = jitCompA000_selectPRegCache(src[1], 0 /* EAX */);
736 reg1 = jitCompA000_selectPRegCache(src[6], 2 /* EDX */);
737 // if (reg0 != 0 /* EAX */ && reg1 == 2 /* EDX */) /* これをやってはいけない!(by K, 2013.08.02) */
738 // reg1 = 0; /* EAX */
739 if (reg0 == reg1 && reg0 != 0) { // bugfix: hinted by yao, 2013.09.14. thanks!
740 jitCompA000_storePRegCacheAll(&w);
743 if (reg1 == 2 /* EDX */)
744 jitCompA000_storeRegCacheEdx(&w);
745 if (reg1 <= 3 /* EAX, EDX */)
746 jitCompA0001_movReg32EbpDisp(&w, reg1, 256 + src[6] * 32 + 0); /* MOV(reg1, [EBP+?]); */
747 if (level < JITC_LV_FASTER)
748 jitCompA0001_checkLimit(&w, reg1, src[6]);
749 jitCompPutByte2(w.dst, 0x8b, reg0 << 3 | reg1); /* MOV(reg0, [reg1]); */
750 if (reg0 == 0 /* EAX */)
751 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 0, 0); /* MOV([EBP+?], EAX); */
752 for (i = 4; i < 32; i += 4) {
753 jitCompPutByte3(w.dst, 0x8b, 0x40 | reg1, i); /* MOV(EAX, [reg1+?]); */
754 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + i, 0 /* EAX */); /* MOV([EBP+?], EAX); */
756 if (reg1 == 2 /* EDX */)
757 jitCompA000_loadRegCacheEdx(&w);
760 case 0x0b: /* PSMEM */ /* 完成 */
761 i = jitCompGetImm32(src + 2);
762 if (i != 0x0001) w.err = JITC_ERR_BADTYPE;
763 if (level < JITC_LV_FASTER) {
764 jitCompA0001_checkType(&w, src[6], i, 1); // write
767 reg0 = jitCompA000_selectPRegCache(src[1], 0 /* EAX */);
768 reg1 = jitCompA000_selectPRegCache(src[6], 2 /* EDX */);
769 // if (reg0 != 0 /* EAX */ && reg1 == 2 /* EDX */) /* これをやってはいけない!(by K, 2013.08.02) */
770 // reg1 = 0; /* EAX */
771 if (reg1 == 2 /* EDX */)
772 jitCompA000_storeRegCacheEdx(&w);
773 if (reg1 <= 3 /* EAX, EDX */)
774 jitCompA0001_movReg32EbpDisp(&w, reg1, 256 + src[6] * 32 + 0); /* MOV(reg1, [EBP+?]); */
775 if (level < JITC_LV_FASTER)
776 jitCompA0001_checkLimit(&w, reg1, src[6]);
777 if (reg0 == 0 /* EAX */)
778 jitCompA0001_movReg32EbpDisp(&w, reg0, 256 + src[1] * 32 + 0); /* MOV(reg0, [EBP+?]); */
779 jitCompPutByte2(w.dst, 0x89, reg0 << 3 | reg1); /* MOV([reg1], reg0); */
780 for (i = 4; i < 32; i += 4) {
781 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[1] * 32 + i); /* MOV(EAX, [EBP+?]); */
782 jitCompPutByte3(w.dst, 0x89, 0x40 | reg1, i); /* MOV([reg1+?], EAX); */
784 if (reg1 == 2 /* EDX */)
785 jitCompA000_loadRegCacheEdx(&w);
788 case 0x0e: /* PADD */ /* 完成 */
789 if (level < JITC_LV_FASTER) {
790 jitCompA0001_checkType0(&w, src[6], jitCompGetImm32(src + 2), 2); // other, aliveテストはとりあえずしない.
793 reg0 = jitCompA000_selectPRegCache(src[1], 0 /* EAX */);
794 reg1 = jitCompA000_selectPRegCache(src[6], -1 /* mem */);
795 if (reg1 < 0 /* mem */)
796 jitCompA0001_movReg32EbpDisp(&w, reg0, 256 + src[6] * 32 + 0); /* MOV(reg0, [EBP+?]); */
797 if (reg1 >= 0 && reg0 != reg1) {
798 jitCompPutByte2(w.dst, 0x89, 0xc0 | reg1 << 3 | reg0); /* MOV(reg0, reg1); */
800 i = jitCompGetImm32(src + 2);
805 i = jitCompA000_convTyp(i);
806 if (0x0002 <= i && i <= 0x0007)
807 j = (i - 0x0002) >> 1;
809 if (j < 0) { w.err = JITC_ERR_BADTYPE; goto err_w; }
810 #if (jitCompA0001_USE_R3F_IMM32 != 0)
811 if (src[7] == 0x3f) {
813 #if (jitCompA0001_USE_R3F_IMM8 != 0)
814 if (-0x80 <= j && j <= 0x7f) {
815 #if (jitCompA0001_USE_R3F_INCDEC != 0)
816 if (j == 1) { jitCompPutByte1(w.dst, 0x40 | reg0); goto padd1; } /* INC */
817 if (j == -1) { jitCompPutByte1(w.dst, 0x48 | reg0); goto padd1; } /* DEC */
819 jitCompPutByte3(w.dst, 0x83, 0xc0 | reg0, j & 0xff); /* ADD(reg0, im8); */
824 jitCompPutByte1(w.dst, 0x05); /* ADD(reg0, ?); */
827 jitCompPutByte2(w.dst, 0x81, 0xc0 | reg0); /* ADD(reg0, ?); */
829 jitCompPutImm32(&w, j);
833 if (src[7] >= 0x40) w.err = JITC_ERR_REGNUM;
835 reg1 = jitCompA000_selectRegCache(src[7], -1 /* mem */);
837 jitCompPutByte2(w.dst, 0x01, 0xc0 | reg1 << 3 | reg0); /* ADD(reg0, reg1); */
840 jitCompPutByte1(w.dst, 0x03); /* ADD(reg0, [EBP+?]); */
841 jitCompA0001_85DispN(&w, src[7] * 4, reg0);
845 reg1 = jitCompA000_selectRegCache(src[7], -1 /* mem */);
847 jitCompA000_storeRegCacheEdx(&w);
849 jitCompA0001_movReg32EbpDisp(&w, reg2, src[7] * 4); /* MOV(reg2, [EBP+?]); */
850 if (reg1 >= 0 && reg1 != reg2) {
851 jitCompPutByte2(w.dst, 0x89, 0xc0 | reg1 << 3 | reg2); /* MOV(reg2, reg1); */
853 jitCompPutByte3(w.dst, 0xc1, 0xe0 | reg2, j); /* SHL(reg2, ?); */
854 jitCompPutByte2(w.dst, 0x01, 0xc0 | reg2 << 3 | reg0); /* ADD(reg0, reg2); */
855 jitCompA000_loadRegCacheEdx(&w);
857 #if (jitCompA0001_USE_R3F_IMM32 != 0)
860 if (reg0 == 0 /* EAX */)
861 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 0, reg0); /* MOV([EBP+?], reg0); */
862 if (src[1] != src[6]) {
863 for (i = 4; i < 32; i += 4) {
864 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[6] * 32 + i); /* MOV(EAX, [EBP+?]); */
865 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + i, 0 /* EAX */); /* MOV([EBP+?], EAX); */
871 case 0x0f: /* PDIF */ /* 未完成 */
872 reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
873 jitCompA000_storePRegCacheAll(&w); // 手抜き.
874 jitCompA0001_checkCompPtr(&w, src[6], src[7]);
875 jitCompA0001_movReg32EbpDisp(&w, reg0, 256 + src[6] * 32 + 0); /* MOV(reg0, [EBP+?]); */
876 jitCompPutByte1(w.dst, 0x2b); /* SUB(EAX, [EBP+?]); */
877 jitCompA0001_85DispN(&w, 256 + src[7] * 32 + 0, reg0);
878 i = jitCompA000_convTyp(jitCompGetImm32(src + 2));
880 if (0x0002 <= i && i <= 0x0007)
881 j = (i - 0x0002) >> 1;
882 if (j < 0) { w.err = JITC_ERR_BADTYPE; goto err_w; }
884 jitCompPutByte3(w.dst, 0xc1, 0xf8 | reg0, j); /* SAR(reg0,?); */
886 if (reg0 == 0 /* EAX */)
887 jitCompA0001_movRxxEax(&w, src[1]);
888 cmp0reg = src[1]; cmp0lev = 1;
897 if (src[1] >= 0x3f) w.err = JITC_ERR_REGNUM;
898 reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
899 reg1 = jitCompA000_selectRegCache(src[2], -1 /* mem */);
900 #if (jitCompA0001_USE_R3F_IMM32 != 0)
901 if (src[2] == 0x3f) { // SUBのみ該当.
902 if (*src != 0x15) w.err = JITC_ERR_REGNUM;
903 reg2 = jitCompA000_selectRegCache(src[3], -1 /* mem */);
905 jitCompA000_storeRegCacheAll(&w);
906 jitCompPutByte1(w.dst, 0xb8 | reg0); /* MOV(reg0, ?); */
907 jitCompPutImm32(&w, w.r3f);
908 jitCompPutByte1(w.dst, 0x2b);
909 jitCompA0001_85DispN(&w, src[3] * 4, reg0);
911 jitCompA0001_movRxxEax(&w, src[1]);
916 jitCompA0001_movReg32EbpDisp(&w, reg0, src[2] * 4); /* MOV(reg0, [EBP+?]); */
918 if (reg1 >= 0 && reg0 != reg1) {
919 jitCompPutByte2(w.dst, 0x89, 0xc0 | reg1 << 3 | reg0); /* MOV(reg0, reg1); */
921 if (!(src[0] == 0x10 && src[3] == 0xff)) { // bugfix: hinted by Iris, 2013.06.26. thanks!
929 if (!(src[0] == 0x10 && src[3] == 0xff)) {
930 #if (jitCompA0001_USE_R3F_IMM32 != 0)
931 if (src[3] == 0x3f) {
932 if (*src == 0x16 && w.r3f == -1) {
933 jitCompPutByte2(w.dst, 0xf7, 0xd8 | reg0); /* NEG(reg0); */
935 jitCompA0001_movRxxEax(&w, src[1]);
938 #if (jitCompA0001_USE_R3F_INCDEC != 0)
939 if ((*src == 0x14 && w.r3f == 1) || (*src == 0x15 && w.r3f == -1)) {
940 jitCompPutByte1(w.dst, 0x40 | reg0); /* INC(reg0); */
942 jitCompA0001_movRxxEax(&w, src[1]);
945 if ((*src == 0x15 && w.r3f == 1) || (*src == 0x14 && w.r3f == -1)) {
946 jitCompPutByte1(w.dst, 0x48 | reg0); /* DEC(reg0); */
948 jitCompA0001_movRxxEax(&w, src[1]);
952 #if (jitCompA0001_USE_R3F_IMM8 != 0)
953 if (-0x80 <= w.r3f && w.r3f <= 0x7f) {
955 static unsigned char basic_op_table_im8[] = { 0xc8, 0xf0, 0xe0, 0, 0xc0, 0xe8 };
956 jitCompPutByte3(w.dst, 0x83, basic_op_table_im8[*src - 0x10] | reg0, w.r3f & 0xff);
959 jitCompPutByte3(w.dst, 0x6b, 0xc0 | reg0 << 3 | reg0, w.r3f & 0xff);
962 jitCompA0001_movRxxEax(&w, src[1]);
966 if (reg0 == 0 /* EAX */) {
967 static unsigned char basic_op_table_im32_eax[] = { 0x0d, 0x35, 0x25, 0, 0x05, 0x2d, 0xc0 };
968 if (*src == 0x16) { jitCompPutByte1(w.dst, 0x69); }
969 jitCompPutByte1(w.dst, basic_op_table_im32_eax[*src - 0x10]);
973 static unsigned char basic_op_table_im32_reg[] = { 0xc8, 0xf0, 0xe0, 0, 0xc0, 0xe8 };
974 jitCompPutByte2(w.dst, 0x81, basic_op_table_im32_reg[*src - 0x10] | reg0);
977 jitCompPutByte2(w.dst, 0x69, 0xc0 | reg0 << 3 | reg0);
980 jitCompPutImm32(&w, w.r3f);
982 jitCompA0001_movRxxEax(&w, src[1]);
986 reg1 = jitCompA000_selectRegCache(src[3], -1 /* mem */);
987 if (src[3] >= 0x40) w.err = JITC_ERR_REGNUM;
990 static unsigned char basic_op_table_rr[] = { 0x09, 0x31, 0x21, 0, 0x01, 0x29 }; /* op(reg,reg); */
991 jitCompPutByte2(w.dst, basic_op_table_rr[*src - 0x10], 0xc0 | reg1 << 3 | reg0);
994 static unsigned char basic_op_table_rm[] = { 0x0b, 0x33, 0x23, 0, 0x03, 0x2b, 0xaf }; /* op(reg,mem); */
995 jitCompPutByte1(w.dst, basic_op_table_rm[*src - 0x10]);
996 jitCompA0001_85DispN(&w, src[3] * 4, reg0);
1001 jitCompPutByte3(w.dst, 0x0f, 0xaf, 0xc0 | reg0 << 3 | reg1);
1004 jitCompPutByte2(w.dst, 0x0f, 0xaf);
1005 jitCompA0001_85DispN(&w, src[3] * 4, reg0);
1010 jitCompA0001_movRxxEax(&w, src[1]);
1013 case 0x18: /* SHL */
1014 case 0x19: /* SAR */
1015 if (src[1] >= 0x3f) w.err = JITC_ERR_REGNUM;
1016 if (src[3] >= 0x40) w.err = JITC_ERR_REGNUM;
1017 #if (jitCompA0001_USE_R3F_IMM32 != 0)
1018 if (src[3] == 0x3f) {
1019 reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
1020 reg1 = jitCompA000_selectRegCache(src[2], -1 /* mem */);
1021 if (src[1] >= 0x3f) w.err = JITC_ERR_REGNUM;
1023 jitCompA0001_movReg32EbpDisp(&w, reg0, src[2] * 4); /* MOV(reg1, [EBP+?]); */
1026 jitCompPutByte2(w.dst, 0x89, 0xc0 | reg1 << 3 | reg0); /* MOV(reg0, reg1); */
1029 if (*src == 0x18) { jitCompPutByte3(w.dst, 0xc1, 0xe0 | reg0, w.r3f); } /* SHL(reg0, im8); */
1030 if (*src == 0x19) { jitCompPutByte3(w.dst, 0xc1, 0xf8 | reg0, w.r3f); } /* SAR(reg0, im8); */
1031 if (reg0 == 0 /* EAX */)
1032 jitCompA0001_movRxxEax(&w, src[1]);
1038 jitCompA000_storeRegCacheAll(&w); // 手抜き.
1039 jitCompA0001_movReg32EbpDisp(&w, 1 /* ECX */, src[3] * 4); /* MOV(ECX, [EBP+?]); */
1040 #if (jitCompA0001_USE_R3F_IMM32 != 0)
1041 if (src[2] == 0x3f) {
1042 jitCompPutByte1(w.dst, 0xb8); /* MOV(EAX, ?); */
1043 jitCompPutImm32(&w, w.r3f);
1046 jitCompA0001_movEaxRxx(&w, src[2]);
1049 jitCompA0001_movEaxRxx(&w, src[2]);
1051 if (*src == 0x18) { jitCompPutByte2(w.dst, 0xd3, 0xe0); } /* SHL(EAX, CL); */
1052 if (*src == 0x19) { jitCompPutByte2(w.dst, 0xd3, 0xf8); } /* SAR(EAX, CL); */
1053 jitCompA0001_movRxxEax(&w, src[1]);
1054 jitCompA000_loadRegCacheAll(&w); // 手抜き.
1059 case 0x1a: /* DIV */
1060 case 0x1b: /* MOD */
1061 if (src[1] >= 0x3f) w.err = JITC_ERR_REGNUM;
1062 if (src[2] >= 0x40) w.err = JITC_ERR_REGNUM;
1063 if (src[3] >= 0x40) w.err = JITC_ERR_REGNUM;
1064 jitCompA000_storeRegCacheAll(&w); // 手抜き.
1065 #if (jitCompA0001_USE_R3F_IMM32 != 0)
1066 if (src[3] == 0x3f) {
1067 jitCompPutByte1(w.dst, 0xb8 | 1); /* MOV(ECX, ?); */
1068 jitCompPutImm32(&w, w.r3f);
1071 jitCompA0001_movReg32EbpDisp(&w, 1 /* ECX */, src[3] * 4); /* MOV(ECX, [EBP+?]); */
1073 if (src[2] == 0x3f) {
1074 jitCompPutByte1(w.dst, 0xb8 | 0); /* MOV(EAX, ?); */
1075 jitCompPutImm32(&w, w.r3f);
1078 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, src[2] * 4); /* MOV(EAX, [EBP+?]); */
1081 jitCompA0001_movReg32EbpDisp(&w, 1 /* ECX */, src[3] * 4); /* MOV(ECX, [EBP+?]); */
1082 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, src[2] * 4); /* MOV(EAX, [EBP+?]); */
1084 jitCompPutByte1(w.dst, 0x99); /* CDQ(); */
1085 /* ECXがゼロではないことを確認すべき */
1086 jitCompPutByte2(w.dst, 0xf7, 0xf9); /* IDIV(ECX); */
1087 if (*src == 0x1a) { jitCompA0001_movEbpDispReg32(&w, src[1] * 4, 0 /* EAX */); }
1088 if (*src == 0x1b) { jitCompA0001_movEbpDispReg32(&w, src[1] * 4, 2 /* EDX */); }
1089 jitCompA000_loadRegCacheAll(&w); // 手抜き.
1093 case 0x1c: /* PLMT0 */
1094 case 0x1d: /* PLMT1 */
1095 if (src[1] >= 0x40 || src[2] >= 0x40) w.err = JITC_ERR_PREGNUM;
1096 if (level < JITC_LV_FASTEST) {
1098 if (level < JITC_LV_FASTER) {
1099 // typ が一致していることを確認.
1100 // plsとliveSignが一致していることを確認.
1102 // preg1はp0 <= p <= p1 を満たしているか?.
1103 // 新しいp0/p1は古いp0〜p1に適合しているか?.
1108 case 0x1e: /* PCP */ /* 未完成(p1まで完成) */
1109 if (src[1] >= 0x40 || src[2] >= 0x40) w.err = JITC_ERR_PREGNUM;
1110 if (src[2] == 0x3f) w.err = JITC_ERR_PREGNUM;
1111 if (src[1] != 0x3f) {
1112 /* src[2] == 0xff の場合に対応できてない */
1113 jitCompA000_storePRegCacheAll(&w); // 手抜き.
1114 for (i = 0; i < 32; i += 4) {
1115 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[2] * 32 + i); /* MOV(EAX, [EBP+?]); */
1116 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + i, 0 /* EAX */); /* MOV([EBP+?], EAX); */
1118 jitCompA000_loadPRegCacheAll(&w); // 手抜き.
1121 if (level < JITC_LV_FASTER) {
1122 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[2] * 32 + 4); /* MOV(EAX, [EBP+?]); */ /* typ */
1123 jitCompPutByte3(w.dst, 0x83, 0xf8, 0); /* CMP(EAX, 0); */
1124 jitCompPutByte2(w.dst, 0x0f, 0x85); /* JNE */
1125 jitCompPutImm32(&w, errfnc - (w.dst + 4));
1126 /* セキュリティチェックが足りてない!(aliveとか) */
1129 jitCompA000_storePRegCacheAll(&w); // 手抜き.
1130 jitCompA0001_movReg32EbpDisp(&w, reg0, 256 + src[2] * 32 + 0); /* MOV(EAX, [EBP+?]); */
1131 if (level < JITC_LV_FASTER) {
1132 jitCompPutByte1(w.dst, 0x3b); /* CMP(reg0, [EBP+?]); */
1133 jitCompA0001_85DispN(&w, 256 + src[2] * 32 + 8, reg0); /* p0 */
1134 jitCompPutByte2(w.dst, 0x0f, 0x85); /* JNE */
1135 jitCompPutImm32(&w, errfnc - (w.dst + 4));
1137 jitCompPutByte2(w.dst, 0xff, 0xe0); /* JMP(EAX); */
1141 case 0x1f: /* PCST */
1142 if (jitCompGetImm32(src + 2) == 0) {
1143 if (level < JITC_LV_FASTER)
1144 jitCompA0001_checkType0(&w, src[6], jitCompGetImm32(src + 7), 2);
1145 jitCompA000_storePRegCacheAll(&w); // 手抜き.
1146 for (i = 0; i < 32 - 4; i += 4) {
1147 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[6] * 32 + i); /* MOV(EAX, [EBP+?]); */
1149 jitCompPutByte1(w.dst, 0x0d); /* OR(EAX, ?); */
1150 jitCompPutImm32(&w, 0x80000000);
1152 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + i, 0 /* EAX */); /* MOV([EBP+?], EAX); */
1154 jitCompPutByte1(w.dst, 0xb8); /* MOV(EAX, ?); */
1155 jitCompPutImm32(&w, debugInfo1);
1156 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 28, 0 /* EAX */); /* MOV([EBP+?], EAX); */
1157 jitCompA000_loadPRegCacheAll(&w); // 手抜き.
1161 if (jitCompGetImm32(src + 7) == 0) {
1162 jitCompA000_storePRegCacheAll(&w); // 手抜き.
1163 for (i = 0; i < 32 - 4; i += 4) {
1164 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[6] * 32 + i); /* MOV(EAX, [EBP+?]); */
1166 jitCompPutByte1(w.dst, 0x25); /* AND(EAX, ?); */
1167 jitCompPutImm32(&w, 0x7fffffff);
1169 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + i, 0 /* EAX */); /* MOV([EBP+?], EAX); */
1171 if (level < JITC_LV_FASTER) {
1172 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[6] * 32 + 28); /* MOV(EAX, [EBP+?]); */
1173 jitCompPutByte1(w.dst, 0x3d); /* CMP(EAX, ?); */
1174 jitCompPutImm32(&w, debugInfo1);
1175 jitCompPutByte2(w.dst, 0x74, 8); /* JE */
1176 jitCompPutByte2(w.dst, 0x31, 0xc0); /* XOR(EAX, EAX); (2) */
1177 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 0, 0 /* EAX */); /* MOV([EBP+?], EAX); (1+1+4) */
1179 jitCompA000_loadPRegCacheAll(&w); // 手抜き.
1183 w.err = JITC_ERR_OPECODE;
1186 case 0x20: /* CMPE */
1187 case 0x21: /* CMPNE */
1188 case 0x22: /* CMPL */
1189 case 0x23: /* CMPGE */
1190 case 0x24: /* CMPLE */
1191 case 0x25: /* CMPG */
1192 case 0x26: /* TSTZ */
1193 case 0x27: /* TSTNZ */
1194 reg0 = jitCompA000_selectRegCache(src[2], 0 /* EAX */);
1195 reg1 = jitCompA000_selectRegCache(src[3], -1 /* mem */);
1196 if (src[1] == 0x3f) {
1198 if (w.prefix != 0) { w.err = JITC_ERR_PREFIX; goto err_w; }
1199 if (src[4] != 0x04 || src[5] != 0x3f || src[6] != 0x03 || src[7] != 0x3f) {
1200 w.err = JITC_ERR_IDIOM; goto err_w;
1204 jitCompA0001_movEaxRxx(&w, src[2]);
1205 #if (jitCompA0001_USE_R3F_IMM32 != 0)
1206 if (src[3] == 0x3f) {
1207 #if (jitCompA0001_OPTIMIZE_CMP != 0)
1208 if ((*src <= 0x25 && w.r3f == 0) || (*src >= 0x26 && w.r3f == -1)) {
1210 if (cmp0reg == src[2]) {
1211 if (cmp0lev >= 1 && (src[0] == 0x20 || src[0] == 0x21 || src[0] == 0x26 || src[0] == 0x27))
1213 if (cmp0lev >= 2 && (src[0] == 0x22 || src[0] == 0x23 || src[0] == 0x24 || src[0] == 0x25))
1217 jitCompPutByte2(w.dst, 0x85, 0xc0 | reg0 << 3 | reg0); /* TEST(reg0, reg0); */
1224 #if (jitCompA0001_USE_R3F_IMM8 != 0)
1225 if (-0x80 <= w.r3f && w.r3f <= 0x7f && *src <= 0x25) {
1226 jitCompPutByte3(w.dst, 0x83, 0xf8 | reg0, w.r3f);
1231 if (*src <= 0x25) { jitCompPutByte1(w.dst, 0x3d); }
1232 if (*src >= 0x26) { jitCompPutByte1(w.dst, 0xa9); }
1235 if (*src <= 0x25) { jitCompPutByte2(w.dst, 0x81, 0xf8 | reg0); }
1236 if (*src >= 0x26) { jitCompPutByte2(w.dst, 0xf7, 0xc0 | reg0); }
1238 jitCompPutImm32(&w, w.r3f);
1242 if (src[3] >= 0x40) w.err = JITC_ERR_PREGNUM;
1244 if (*src <= 0x25) { jitCompPutByte2(w.dst, 0x39, 0xc0 | reg1 << 3 | reg0); }
1245 if (*src >= 0x26) { jitCompPutByte2(w.dst, 0x85, 0xc0 | reg1 << 3 | reg0); }
1248 if (*src <= 0x25) { jitCompPutByte1(w.dst, 0x3b); }
1249 if (*src >= 0x26) { jitCompPutByte1(w.dst, 0x85); }
1250 jitCompA0001_85DispN(&w, src[3] * 4, reg0);
1253 if (w.err != 0) goto err_w;
1254 static unsigned char cmpcc_table0[] = {
1255 0x04, 0x05, 0x0c, 0x0d, 0x0e, 0x0f, 0x04, 0x05, /* CMPcc, TSTcc */
1256 0x04, 0x05, 0x02, 0x03, 0x06, 0x07 /* PCMPcc */
1258 #if (jitCompA0001_USE_R3F_CMPJMP != 0)
1259 if (src[1] == 0x3f) {
1261 jitCompPutByte2(w.dst, 0x0f, 0x80 | cmpcc_table0[*src - 0x20]);
1263 i = jitCompGetLabelNum(&w, src + 2);
1264 if ((flags & JITC_PHASE1) != 0 && w.err != 0) {
1265 if (label[i].opt == 0) { w.err = JITC_ERR_LABELNODEF; goto err_w; }
1266 // if (label[i].typ != 1) { w.err = JITC_ERR_LABELTYP; goto err_w; }
1269 if ((flags & JITC_PHASE1) != 0 || (((flags & JITC_PHASE1) == 0) && label[i].opt != 0))
1270 j = label[i].p - (w.dst + 4);
1271 jitCompPutImm32(&w, j);
1272 #if (jitCompA0001_OPTIMIZE_JMP != 0)
1273 if (-128 - 4 <= j && j < 0) {
1276 jitCompPutByte2(w.dst, w.dst[1] ^ 0xf0, j & 0xff);
1280 if (w.err != 0) goto err_w;
1285 reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
1286 jitCompPutByte3(w.dst, 0x0f, 0x90 | cmpcc_table0[*src - 0x20], 0xc0 | reg0); /* SETcc(BYTE(reg0)); */
1287 jitCompPutByte3(w.dst, 0x0f, 0xb6, 0xc0 | reg0 << 3 | reg0); /* MOVZX(reg0, BYTE(reg0)); */
1288 jitCompPutByte2(w.dst, 0xf7, 0xd8 | reg0); /* NEG(reg0); */
1290 jitCompA0001_movRxxEax(&w, src[1]);
1295 case 0x28: /* PCMPE */
1296 case 0x29: /* PCMPNE */
1297 case 0x2a: /* PCMPL */
1298 case 0x2b: /* PCMPGE */
1299 case 0x2c: /* PCMPLE */
1300 case 0x2d: /* PCMPG */
1301 if (src[1] == 0x3f) {
1303 if (w.prefix != 0) { w.err = JITC_ERR_PREFIX; goto err_w; }
1304 if (src[4] != 0x04 || src[5] != 0x3f || src[6] != 0x03 || src[7] != 0x3f) {
1305 w.err = JITC_ERR_IDIOM; goto err_w;
1308 if (src[2] >= 0x40) w.err = JITC_ERR_PREGNUM;
1309 jitCompA000_storePRegCacheAll(&w); // 手抜き.
1311 jitCompA0001_checkCompPtr(&w, src[2], src[3]);
1312 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[2] * 32 + 0); /* MOV(EAX, [EBP+?]); */
1313 if (src[3] != 0xff) {
1314 jitCompPutByte1(w.dst, 0x3b); /* CMP(EAX, [EBP+?]); */
1315 jitCompA0001_85DispN(&w, 256 + src[3] * 32 + 0, 0);
1318 /* ヌルポインタとの比較はこれでいいのか?たぶんよくない */
1319 jitCompPutByte3(w.dst, 0x83, 0xf8, 0x00); /* CMP(EAX, 0); */
1324 case 0x30: /* talloc(old:F4) */
1325 case 0x31: /* tfree(old:F5) */
1326 case 0x32: /* malloc(old:F6) */
1327 case 0x33: /* mfree(old:F7) */
1328 jitCompA000_storeRegCacheAll(&w); // 手抜き.
1329 jitCompA000_storePRegCacheAll(&w); // 手抜き.
1330 jitCompPutByte2(w.dst, 0x6a, src[3]); /* PUSH(?); */
1331 jitCompPutByte2(w.dst, 0x6a, src[2]); /* PUSH(?); */
1332 jitCompPutByte2(w.dst, 0x6a, src[1]); /* PUSH(?); */
1333 jitCompPutByte1(w.dst, 0x55); /* PUSH(EBP); */
1334 jitCompPutByte1(w.dst, 0xe8);
1335 if (*src == 0x30) j = ((unsigned char *)&funcf4) - (w.dst + 4);
1336 if (*src == 0x31) j = ((unsigned char *)&funcf5) - (w.dst + 4);
1337 if (*src == 0x32) j = ((unsigned char *)&funcf6) - (w.dst + 4);
1338 if (*src == 0x33) j = ((unsigned char *)&funcf7) - (w.dst + 4);
1339 jitCompPutImm32(&w, j);
1340 jitCompPutByte3(w.dst, 0x83, 0xc4, 0x10); /* ADD(ESP,16); */
1341 jitCompA000_loadRegCacheAll(&w); // 手抜き.
1342 jitCompA000_loadPRegCacheAll(&w); // 手抜き.
1346 case 0x34: /* data (暫定) */
1348 if (w.prefix != 0) { w.err = JITC_ERR_PREFIX; goto err_w; }
1349 int k = jitCompGetImm32(&src[1]), tmpData, bitCount, dataWidth = jitCompA000_dataWidth(k);
1350 if (lastlabel >= 0 && label[lastlabel].typ == 0)
1351 label[lastlabel].typ = k;
1353 i = jitCompA000_convTyp(k);
1354 if (i < 2 || i > 7) { w.err = JITC_ERR_BADTYPE; goto err_w; }
1356 j = jitCompGetImm32(&src[5]);
1362 if (src >= src1) { w.err = JITC_ERR_SRC1; src = oldsrc; goto err_w; }
1363 if (w.dst + 256 > dst1) { w.err = JITC_ERR_DST1; src = oldsrc; goto err_w; }
1365 for (k = 0; k < dataWidth; k++) {
1366 tmpData = tmpData << 1 | ((*src >> bitCount) & 1);
1373 if ((i & 1) == 0 && dataWidth <= 31 && (tmpData >> (dataWidth - 1)) != 0) {
1374 tmpData -= 1 << dataWidth;
1376 if (i == 2 || i == 3) { jitCompPutByte1(w.dst, tmpData & 0xff); }
1377 if (i == 4 || i == 5) { jitCompPutByte2(w.dst, tmpData & 0xff, (tmpData >> 8) & 0xff); }
1378 if (i == 6 || i == 7) { jitCompPutByte4(w.dst, tmpData & 0xff, (tmpData >> 8) & 0xff, (tmpData >> 16) & 0xff, (tmpData >> 24) & 0xff); }
1384 if (src >= src1) { w.err = JITC_ERR_SRC1; src = oldsrc; goto err_w; }
1385 if (w.dst + 256 > dst1) { w.err = JITC_ERR_DST1; src = oldsrc; goto err_w; }
1386 i = jitCompGetImm32(src);
1388 if ((flags & JITC_PHASE1) != 0 && w.err == 0) {
1389 if (label[i].opt == 0) { w.err = JITC_ERR_LABELNODEF; goto err_w; }
1391 jitCompPutImm32(&w, (int)label[i].p);
1392 jitCompPutImm32(&w, label[i].typ);
1393 jitCompPutImm32(&w, (int)label[i].p);
1394 jitCompPutImm32(&w, (int)label[i].p1);
1395 jitCompPutImm32(&w, 0); /* liveSign */
1396 jitCompPutImm32(&w, 2320); /* pls */
1397 jitCompPutImm32(&w, 0);
1398 jitCompPutImm32(&w, 0);
1402 if (lastlabel >= 0 && label[lastlabel].p1 < w.dst)
1403 label[lastlabel].p1 = w.dst;
1406 case 0x3c: /* ENTER */
1407 jitCompA000_storeRegCacheAll(&w); // 手抜き.
1408 jitCompA000_storePRegCacheAll(&w); // 手抜き.
1409 jitCompPutByte2(w.dst, 0x6a, src[6]); /* PUSH(?); */
1410 jitCompPutByte2(w.dst, 0x6a, src[5]); /* PUSH(?); */
1411 jitCompPutByte2(w.dst, 0x6a, src[4] & 0x0f); /* PUSH(?); */
1412 jitCompPutByte2(w.dst, 0x6a, (src[4] >> 4) & 0x0f); /* PUSH(?); */
1413 jitCompPutByte2(w.dst, 0x6a, src[3]); /* PUSH(?); */
1414 jitCompPutByte2(w.dst, 0x6a, src[2]); /* PUSH(?); */
1415 jitCompPutByte2(w.dst, 0x6a, src[1]); /* PUSH(?); */
1416 jitCompPutByte1(w.dst, 0x55); /* PUSH(EBP); */
1417 jitCompPutByte1(w.dst, 0xe8);
1418 j = ((unsigned char *)&func3c) - (w.dst + 4);
1419 jitCompPutImm32(&w, j);
1420 jitCompPutByte3(w.dst, 0x83, 0xc4, 0x20); /* ADD(ESP,32); */
1421 jitCompA000_loadRegCacheAll(&w); // 手抜き.
1422 jitCompA000_loadPRegCacheAll(&w); // 手抜き.
1426 case 0x3d: /* LEAVE */
1427 jitCompA000_storeRegCacheAll(&w); // 手抜き.
1428 jitCompA000_storePRegCacheAll(&w); // 手抜き.
1429 jitCompPutByte2(w.dst, 0x6a, src[6]); /* PUSH(?); */
1430 jitCompPutByte2(w.dst, 0x6a, src[5]); /* PUSH(?); */
1431 jitCompPutByte2(w.dst, 0x6a, src[4] & 0x0f); /* PUSH(?); */
1432 jitCompPutByte2(w.dst, 0x6a, (src[4] >> 4) & 0x0f); /* PUSH(?); */
1433 jitCompPutByte2(w.dst, 0x6a, src[3]); /* PUSH(?); */
1434 jitCompPutByte2(w.dst, 0x6a, src[2]); /* PUSH(?); */
1435 jitCompPutByte2(w.dst, 0x6a, src[1]); /* PUSH(?); */
1436 jitCompPutByte1(w.dst, 0x55); /* PUSH(EBP); */
1437 jitCompPutByte1(w.dst, 0xe8);
1438 j = ((unsigned char *)&func3d) - (w.dst + 4);
1439 jitCompPutImm32(&w, j);
1440 jitCompPutByte3(w.dst, 0x83, 0xc4, 0x20); /* ADD(ESP,32); */
1441 jitCompA000_loadRegCacheAll(&w); // 手抜き.
1442 jitCompA000_loadPRegCacheAll(&w); // 手抜き.
1446 case 0xfe: /* remark */
1447 if (src[1] == 0x01 && src[2] == 0x00) { // DBGINFO1
1448 if (level <= JITC_LV_SLOWER) {
1449 jitCompPutByte1(w.dst, 0xb8); /* MOV(EAX, ?); */
1450 jitCompPutImm32(&w, debugInfo1);
1451 jitCompA0001_movEbpDispReg32(&w, 2304 + 4, 0 /* EAX */); /* MOV(debugInfo1, EAX); */
1454 if (src[1] == 0x01 && src[2] == 0x03) { // DBGINFO1CLR
1455 if (level <= JITC_LV_SLOWER) {
1456 jitCompPutByte1(w.dst, 0xb8); /* MOV(EAX, ?); */
1457 jitCompPutImm32(&w, -1);
1458 jitCompA0001_movEbpDispReg32(&w, 2304 + 4, 0 /* EAX */); /* MOV(debugInfo1, EAX); */
1461 if (src[1] == 0x05 && src[2] == 0x00) { // DBGINFO0
1462 if (level <= JITC_LV_SLOWEST) {
1463 debugInfo0 = jitCompGetImm32(src + 3);
1464 // jitCompPutByte1(w.dst, 0xbf); /* MOV(EDI, ?); */
1465 // jitCompPutImm32(&w, debugInfo0);
1466 jitCompPutByte1(w.dst, 0xb8); /* MOV(EAX, ?); */
1467 jitCompPutImm32(&w, debugInfo0);
1468 jitCompA0001_movEbpDispReg32(&w, 2304 + 0, 0 /* EAX */); /* MOV(debugInfo0, EAX); */
1474 w.err = JITC_ERR_OPECODE;
1477 if (w.err != 0) goto err_w;
1478 jitCompA0001_fixPrefix(&w);
1479 if (w.err != 0) goto err_w;
1480 src += jitCompCmdLen(src);
1482 if (enter0 != NULL) {
1483 j = w.dst - (enter0 + 4);
1484 enter0[0] = j & 0xff;
1485 enter0[1] = (j >> 8) & 0xff;
1486 enter0[2] = (j >> 16) & 0xff;
1487 enter0[3] = (j >> 24) & 0xff;
1489 if ((flags & JITC_NOSTARTUP) == 0) {
1490 jitCompA000_storeRegCacheAll(&w);
1491 jitCompA000_storePRegCacheAll(&w);
1492 jitCompPutByte1(w.dst, 0x61); /* POPAD(); */
1494 if ((flags & JITC_PHASE1) != 0)
1495 return w.dst - dst00;
1499 if ((w.err & JITC_ERR_PHASE0ONLY) != 0) {
1500 if ((flags & JITC_PHASE1) == 0)
1501 w.err &= ~JITC_ERR_PHASE0ONLY;
1503 if (w.err == (JITC_ERR_MASK & JITC_ERR_REGNUM)) errmsg = "reg-number error";
1504 if (w.err == (JITC_ERR_MASK & JITC_ERR_DST1)) errmsg = "dst1 error";
1505 if (w.err == (JITC_ERR_MASK & JITC_ERR_OPECODE)) errmsg = "opecode error";
1506 if (w.err == (JITC_ERR_MASK & JITC_ERR_LABELNUM)) errmsg = "label number too large";
1507 if (w.err == (JITC_ERR_MASK & JITC_ERR_LABELREDEF)) errmsg = "label redefine";
1508 if (w.err == (JITC_ERR_MASK & JITC_ERR_PREFIX)) { errmsg = "prefix redefine"; w.dst -= 2; }
1509 if (w.err == (JITC_ERR_MASK & JITC_ERR_LABELNODEF)) errmsg = "label not defined";
1510 if (w.err == (JITC_ERR_MASK & JITC_ERR_LABELTYP)) errmsg = "label type error";
1511 if (w.err == (JITC_ERR_MASK & JITC_ERR_IDIOM)) errmsg = "idiom error";
1512 if (w.err == (JITC_ERR_MASK & JITC_ERR_PREGNUM)) errmsg = "preg-number error";
1513 if (w.err == (JITC_ERR_MASK & JITC_ERR_SRC1)) errmsg = "src1 error";
1514 if (w.err == (JITC_ERR_MASK & JITC_ERR_BADTYPE)) errmsg = "bad type code";
1515 if (w.err == (JITC_ERR_MASK & JITC_ERR_PREFIXFAR)) errmsg = "prefix internal error";
1516 if (w.err == (JITC_ERR_MASK & JITC_ERR_INTERNAL)) errmsg = "general internal error";
1517 if (*errmsg != '\0') {
1518 fprintf(stderr, "JITC: %s at %06X (debugInfo0=%d)\n ", errmsg, src - src0, debugInfo0);
1519 for (i = 0; i < 16; i++)
1520 fprintf(stderr, "%02X ", src[i]);
1521 static char *table[0x30] = {
1522 "NOP", "LB", "LIMM", "PLIMM", "CND", "??", "??", "??",
1523 "LMEM", "SMEM", "PLMEM", "PSMEM", "LEA", "??", "PADD", "PDIF",
1524 "CP/OR", "XOR", "AND", "??", "ADD", "SUB", "MUL", "??",
1525 "SHL", "SAR", "DIV", "MOD", "PLMT0", "PLMT1", "PCP", "PCST",
1526 "CMPE", "CMPNE", "CMPL", "CMPGE", "CMPLE", "CMPG", "TSTZ", "TSTNZ",
1527 "PCMPE", "PCMPNE", "PCMPL", "PCMPGE", "PCMPLE", "PCMPG", "??", "EXT" };
1529 if (*src < 0x30) errmsg = table[*src];
1530 fprintf(stderr, "(%s)\n", errmsg);
1535 unsigned char *jitCompCallFunc(unsigned char *dst, void *func)
1537 struct JitCompWork w;
1539 jitCompA000_storeRegCacheAll(&w);
1540 jitCompA000_storePRegCacheAll(&w);
1541 jitCompPutByte1(w.dst, 0x60); /* PUSHAD(); */
1542 jitCompPutByte1(w.dst, 0x50); /* PUSH(EAX); */ /* for 16byte-align(win32では不要なのだけど、MacOSには必要らしい) */
1543 jitCompPutByte1(w.dst, 0x55); /* PUSH(EBP); */
1544 jitCompPutByte1(w.dst, 0xe8); /* CALL(func); */
1545 int j = ((unsigned char *)func) - (w.dst + 4);
1547 //この関数の中では結局w->dstしか参照していない
1548 jitCompPutImm32(&w, j);
1550 jitCompPutByte1(w.dst, 0x58); /* POP(EAX); */ /* (win32では不要なのだけど、MacOSには必要らしい) */
1551 jitCompPutByte1(w.dst, 0x58); /* POP(EAX); */
1552 jitCompPutByte1(w.dst, 0x61); /* POPAD(); */
1553 jitCompA000_loadRegCacheAll(&w);
1554 jitCompA000_loadPRegCacheAll(&w);
1555 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + 0x30 * 32 + 0); /* MOV(EAX, [EBP+?]); */
1556 jitCompPutByte2(w.dst, 0xff, 0xe0); /* JMP(EAX); */
1560 unsigned char *jitCompInit(unsigned char *dst)
1563 return jitCompCallFunc(dst, &errHndl);
1566 void func3c(char *ebp, int opt, int r1, int p1, int lenR, int lenP, int r0, int p0)
1568 HOSECPU_RuntimeEnvironment *r = (HOSECPU_RuntimeEnvironment *) (ebp - jitCompA0001_EBP128);
1570 HOSECPU_PointerRegisterEntry *pp;
1571 if (r->junkStack + 2048 > r->junkStack1) (*(r->errHndl))(r);
1572 pi = (void *)r->junkStack; r->junkStack += r1 * 4;
1573 for (i = 0; i < r1; i++)
1575 pp = (void *)r->junkStack; r->junkStack += p1 * 32;
1576 for (i = 0; i < p1; i++)
1578 pp = (void *)r->junkStack; r->junkStack += 32;
1579 *pp = r->preg[0x30];
1580 pi = (void *)r->junkStack; r->junkStack += 4;
1581 *pi = opt << 16 | r1 << 8 | p1;
1582 for (i = 0; i < lenR; i++)
1583 r->ireg[r0 + i] = r->ireg[0x30 + i];
1584 for (i = 0; i < lenP; i++)
1585 r->preg[p0 + i] = r->preg[0x31 + i];
1589 void func3d(char *ebp, int opt, int r1, int p1, int lenR, int lenP, int r0, int p0)
1591 HOSECPU_RuntimeEnvironment *r = (HOSECPU_RuntimeEnvironment *) (ebp - jitCompA0001_EBP128);
1595 HOSECPU_PointerRegisterEntry *pp = (void *)r->junkStack;
1596 r->preg[0x30] = *pp;
1597 r->junkStack -= p1 * 32; pp = (void *)r->junkStack;
1598 for (i = 0; i < p1; i++)
1600 r->junkStack -= r1 * 4; int *pi = (void *)r->junkStack;
1601 for (i = 0; i < r1; i++)
1606 void funcf4(char *ebp, int pxx, int typ, int len)
1608 HOSECPU_RuntimeEnvironment *r = (HOSECPU_RuntimeEnvironment *) (ebp - jitCompA0001_EBP128);
1609 int width = jitCompA000_dataWidth(jitCompA000_convTyp(r->ireg[typ])) >> 3;
1610 if (width < 0 || r->ireg[len] < 0)
1612 void *p = r->junkStack;
1613 if (r->junkStack + width * r->ireg[len] + 256 > r->junkStack1) (*(r->errHndl))(r);
1614 r->junkStack += width * r->ireg[len];
1616 r->preg[pxx].typ = r->ireg[typ];
1617 r->preg[pxx].p0 = p;
1618 r->preg[pxx].p1 = (void *)r->junkStack;
1619 int *pi = (int *)r->junkStack;
1620 *pi = width * r->ireg[len];
1621 r->junkStack += sizeof (int);
1622 if (r->ireg[typ] == 1) {
1623 int i, i1 = (width * r->ireg[len]) >> 2;
1625 for (i = 0; i < i1; i++)
1631 void funcf5(char *ebp, int pxx, int typ, int len)
1633 HOSECPU_RuntimeEnvironment *r = (HOSECPU_RuntimeEnvironment *) (ebp - jitCompA0001_EBP128);
1634 r->junkStack -= sizeof (int);
1635 int *pi = (int *)r->junkStack;
1636 r->junkStack -= *pi;
1638 int width = jitCompA000_dataWidth(r->ireg[typ]);
1639 void *p = r->junkStack;
1640 r->junkStack -= width * r->ireg[len];
1645 void funcf6(char *ebp, int pxx, int typ, int len)
1647 HOSECPU_RuntimeEnvironment *r = (HOSECPU_RuntimeEnvironment *) (ebp - jitCompA0001_EBP128);
1648 int width = jitCompA000_dataWidth(jitCompA000_convTyp(r->ireg[typ])) >> 3;
1649 if (width < 0 || r->ireg[len] < 0)
1651 void *p = malloc(width * r->ireg[len]);
1653 r->preg[pxx].typ = r->ireg[typ];
1654 r->preg[pxx].p0 = p;
1655 r->preg[pxx].p1 = (unsigned char *)p + width * r->ireg[len];
1656 if (r->ireg[typ] == 1) {
1657 int i, i1 = (width * r->ireg[len]) >> 2, *pi;
1659 for (i = 0; i < i1; i++)
1661 for (i = 1; i < i1; i += 8)
1667 void funcf7(char *ebp, int pxx, int typ, int len)
1669 HOSECPU_RuntimeEnvironment *r = (HOSECPU_RuntimeEnvironment *) (ebp - jitCompA0001_EBP128);
1670 free(r->preg[pxx].p);
1674 void errHndl(HOSECPU_RuntimeEnvironment *r)
1676 r = (HOSECPU_RuntimeEnvironment *) (((char *)r) - jitCompA0001_EBP128);
1682 * jitcの出力コードをひとまとめにする関数を作成しその中身をjitCompile()で生成
1684 * qq : 出力バイナリの書き込み位置のアドレスへの参照(書き込み位置を呼び出しに反映させるため参照渡しにする)
1685 * q1 : 出力バイナリの書き込み位置のアドレスの最大値
1686 * p0 : (*.ose)バイナリの読み込み位置のアドレス(ヘッダ部除去済)
1687 * p1 : (*.ose)バイナリの読み込み位置の取りうる最大値
1688 * (ただし、「確保したメモリ」の最大値なのでこれより手前にゴミデータが入っているかもしれない)
1690 * ret=2 : jitコンパイルエラー
1692 int jitc0(unsigned char **qq, unsigned char *q1, const unsigned char *p0, const unsigned char *p1, int level, HOSECPU_LabelListTag *label)
1694 unsigned char *q = *qq;
1695 if (p0[0] != 0x05 || p0[1] != SIGN1) // OSECPUのヘッダ (05E1) を確認
1698 *q++ = 0x55; /* PUSH(EBP); */
1699 *q++ = 0x8b; *q++ = 0x6c; *q++ = 0x24; *q++ = 0x08; /* MOV(EBP,[ESP+8]); */
1702 for (i = 0; i < JITC_MAXLABELS; i++)
1705 // 以下のjitCompile()呼び出しでは第二引数をq1-2にした方がよいのではないか?
1706 i = jitCompiler(q, q1, p0 + 2, p1, p0, label, JITC_MAXLABELS, level, di1_serial, 0);
1707 if (i != 0) return 2;
1708 i = jitCompiler(q, q1, p0 + 2, p1, p0, label, JITC_MAXLABELS, level, di1_serial, JITC_PHASE1 + 0);
1709 if (i < 0) return 2;
1712 *q++ = 0x5d; /* POP(EBP); */
1713 *q++ = 0xc3; /* RET(); */
1719 #if (USE_DEBUGGER != 0)
1721 int dbgrGetRegNum(const char *p)
1727 if (i > 9) i -= 'A' - '0' - 10;
1728 if (j > 9) j -= 'A' - '0' - 10;
1729 if (0 <= i && i <= 15 && 0 <= j && j <= 15)
1735 void dbgrMain(HOSECPU_RuntimeEnvironment *r)
1737 if (r->dbgr == 0) return;
1742 p = fgets(cmd, 64, stdin);
1743 if (p == NULL) break;
1744 if (cmd[0] == '\0') continue;
1745 if (cmd[0] == 'q' && cmd[1] <= ' ') break;
1746 if (cmd[0] == 'p' && cmd[1] <= ' ' && cmd[1] != '\0') {
1748 while (*p <= ' ' && *p != '\0') p++;
1750 i = dbgrGetRegNum(p + 1);
1751 if (0 <= i && i <= 0x3f) {
1752 printf("R%02X = 0x%08X = %d\n", i, r->ireg[i], r->ireg[i]);
1755 puts("register name error");
1759 i = dbgrGetRegNum(p + 1);
1760 if (0 <= i && i <= 0x3f) {
1762 if (0 <= r->preg[i].typ && r->preg[i].typ <= 0x15) {
1763 static char *typName[] = {
1764 "T_CODE", "T_VPTR", "T_SINT8", "T_UINT8",
1765 "T_SINT16", "T_UINT16", "T_SINT32", "T_UINT32",
1766 "T_SINT4", "T_UINT4", "T_SINT2", "T_UINT2",
1767 "T_SINT1", "T_UINT1", "T_SINT12", "T_UINT12",
1768 "T_SINT20", "T_UINT20", "T_SINT24", "T_UINT24",
1769 "T_SINT28", "T_UINT28"
1771 p = typName[r->preg[i].typ];
1773 printf("P%02X:\n type = %s(%04X), (origin-ptr) = 0x%08X\n", i, p, r->preg[i].typ, (unsigned int)(r->preg[i].p0));
1774 if (r->preg[i].p != NULL && r->preg[i].p0 != NULL) {
1775 j = jitCompA000_dataWidth(jitCompA000_convTyp(r->preg[i].typ)) >> 3;
1777 k = (r->preg[i].p1 - r->preg[i].p0) / j;
1778 printf(" size = 0x%08X = %d\n", k, k);
1779 k = (r->preg[i].p - r->preg[i].p0) / j;
1780 printf(" pos = 0x%08X = %d\n", k, k);
1783 puts(" null pointer");
1787 puts("register name error");
1791 puts("command error");