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)
35 if (0x01 <= *src && *src < 0x04){
38 } else if (*src == 0x04){
41 } else if (0x08 <= *src && *src < 0x0d){
42 // LMEM, SMEM, ??, ??, ??
44 } else if (0x0e <= *src && *src < 0x10){
47 } else if (0x10 <= *src && *src < 0x1c){
48 // CP/OR, XOR, AND, ADD, SUB, MUL, SHL, SAR, DIV, MOD,
50 } else if (0x1c <= *src && *src < 0x1f){
53 } else if (*src == 0x1f){
56 } else if(0x20 <= *src && *src < 0x2e){
57 // CMPE, CMPNE, CMPL, CMPGE, CMPLE, CMPG, TSTZ, TSTNZ,
58 // PCMPE, PCMPNE, PCMPL, PCMPGE, PCMPLE, PCMPG,
60 } else if (*src == 0x2f){
63 } else if (0x30 <= *src && *src < 0x34){
66 } else if (0x3c <= *src && *src < 0x3e){
69 } else if (*src == 0xfe){
76 #if (JITC_ARCNUM == 0x0001)
82 以下は最適化のためのものなので、すべて0として簡単に移植しても問題ありません */
83 #define jitCompA0001_USE_R3F_CMPJMP 1*1
84 #define jitCompA0001_USE_R3F_IMM32 1*1
85 #define jitCompA0001_USE_R3F_IMM8 1*1
86 #define jitCompA0001_USE_R3F_INCDEC 1*1
87 #define jitCompA0001_OPTIMIZE_JMP 1*1
88 #define jitCompA0001_OPTIMIZE_MOV 1*1 /* 1にすると速度低下する? */
89 #define jitCompA0001_OPTIMIZE_CMP 1*1
90 #define jitCompA0001_OPTIMIZE_ALIGN 4*1 /* 0-8を想定 */
91 #define jitCompA0001_EBP128 128*1
94 unsigned char *dst, *dst0;
96 #if (jitCompA0001_USE_R3F_IMM32 != 0)
99 char prefix; //CND命令の値を記録(初期値=0)
102 #define jitCompPutByte1(p, c0) *p++ = c0
103 #define jitCompPutByte2(p, c0, c1) *p++ = c0; *p++ = c1
104 #define jitCompPutByte3(p, c0, c1, c2) *p++ = c0; *p++ = c1; *p++ = c2
105 #define jitCompPutByte4(p, c0, c1, c2, c3) *p++ = c0; *p++ = c1; *p++ = c2; *p++ = c3
107 static void jitCompPutImm32(struct JitCompWork *w, int i)
109 jitCompPutByte1(w->dst, i & 0xff);
110 jitCompPutByte1(w->dst, (i >> 8) & 0xff);
111 jitCompPutByte1(w->dst, (i >> 16) & 0xff);
112 jitCompPutByte1(w->dst, (i >> 24) & 0xff);
116 int jitCompGetImm32(const unsigned char *src)
118 return (src[0] << 24) | (src[1] << 16) | (src[2] << 8) | src[3];
121 int jitCompGetLabelNum(struct JitCompWork *w, const unsigned char *src)
123 int i = jitCompGetImm32(src);
124 if (i < 0 || i >= w->maxLabels) {
125 w->err = JITC_ERR_LABELNUM;
131 void jitCompA0001_85DispN(struct JitCompWork *w, int disp, int n)
133 disp -= jitCompA0001_EBP128;
134 if (-128 <= disp && disp <= 127) {
135 jitCompPutByte2(w->dst, 0x45 | (n << 3), disp & 0xff);
137 jitCompPutByte1(w->dst, 0x85 | (n << 3));
138 jitCompPutImm32(w, disp);
143 void jitCompA0001_movEbpDispReg32(struct JitCompWork *w, int disp, int reg32)
145 jitCompPutByte1(w->dst, 0x89); /* MOV(mem, reg32); */
146 jitCompA0001_85DispN(w, disp, reg32);
150 void jitCompA0001_movReg32EbpDisp(struct JitCompWork *w, int reg32, int disp)
152 jitCompPutByte1(w->dst, 0x8b); /* MOV(reg32, mem); */
153 jitCompA0001_85DispN(w, disp, reg32);
157 void jitCompA0001_movEaxRxx(struct JitCompWork *w, int rxx)
159 #if (jitCompA0001_USE_R3F_IMM32 != 0)
161 jitCompPutByte1(w->dst, 0xb8); /* MOV(EAX, ?); */
162 jitCompPutImm32(w, w->r3f);
166 if (rxx >= 0x40 || rxx < 0){
167 w->err = JITC_ERR_REGNUM;
169 jitCompA0001_movReg32EbpDisp(w, 0 /* EAX */, rxx * 4); /* MOV(EAX, [EBP+?]); */
173 void jitCompA0001_movRxxEax(struct JitCompWork *w, int rxx)
175 if (rxx >= 0x40 || rxx < 0){
176 w->err = JITC_ERR_REGNUM;
178 jitCompA0001_movEbpDispReg32(w, rxx * 4, 0 /* EAX */); /* MOV([EBP+?], EAX); */
182 void jitCompA0001_fixPrefix(struct JitCompWork *w)
184 if (w->prefix != 0) {
185 if (w->dst - w->dst0 > 127){
186 w->err = JITC_ERR_REGNUM;
188 w->dst0[-1] = (unsigned char)((w->dst - w->dst0) & 0xff);
193 void jitCompA0001_checkCompPtr(struct JitCompWork *w, int p0, int p1)
195 if (p0 >= 0x3f || p0 < 0){
196 w->err = JITC_ERR_PREGNUM;
198 if (p1 >= 0x3f || p1 < 0){
199 w->err = JITC_ERR_PREGNUM;
201 /* 比較可能可能なのかのチェックのコードを出力 */ /* 未完成 */
205 void jitCompA000_loadRegCacheAll(struct JitCompWork *w)
207 jitCompA0001_movReg32EbpDisp(w, 3 /* EBX */, 0 * 4); /* EBX = R00; */
208 jitCompA0001_movReg32EbpDisp(w, 1 /* ECX */, 1 * 4); /* ECX = R01; */
209 jitCompA0001_movReg32EbpDisp(w, 2 /* EDX */, 2 * 4); /* EDX = R02; */
213 void jitCompA000_storeRegCacheAll(struct JitCompWork *w)
215 jitCompA0001_movEbpDispReg32(w, 0 * 4, 3 /* EBX */); /* R00 = EBX; */
216 jitCompA0001_movEbpDispReg32(w, 1 * 4, 1 /* ECX */); /* R01 = ECX; */
217 jitCompA0001_movEbpDispReg32(w, 2 * 4, 2 /* EDX */); /* R02 = EDX; */
221 void jitCompA000_loadRegCacheEcx(struct JitCompWork *w)
223 jitCompA0001_movReg32EbpDisp(w, 1 /* ECX */, 1 * 4); /* ECX = R01; */
227 void jitCompA000_storeRegCacheEcx(struct JitCompWork *w)
229 jitCompA0001_movEbpDispReg32(w, 1 * 4, 1 /* ECX */); /* R01 = ECX; */
233 void jitCompA000_loadRegCacheEdx(struct JitCompWork *w)
235 jitCompA0001_movReg32EbpDisp(w, 2 /* EDX */, 2 * 4); /* EDX = R02; */
239 void jitCompA000_storeRegCacheEdx(struct JitCompWork *w)
241 jitCompA0001_movEbpDispReg32(w, 2 * 4, 2 /* EDX */); /* R02 = EDX; */
245 int jitCompA000_selectRegCache(int rxx, int reg)
264 void jitCompA000_loadPRegCacheAll(struct JitCompWork *w)
266 // jitCompA0001_movReg32EbpDisp(w, 5 /* EBP */, 256 + 0 * 32 + 0); /* EBP = P00; */
267 jitCompA0001_movReg32EbpDisp(w, 6 /* ESI */, 256 + 1 * 32 + 0); /* ESI = P01; */
268 jitCompA0001_movReg32EbpDisp(w, 7 /* EDI */, 256 + 2 * 32 + 0); /* EDI = P02; */
272 void jitCompA000_storePRegCacheAll(struct JitCompWork *w)
274 // jitCompA0001_movEbpDispReg32(w, 256 + 0 * 32 + 0, 5 /* EBP */); /* P00 = EBP; */
275 jitCompA0001_movEbpDispReg32(w, 256 + 1 * 32 + 0, 6 /* ESI */); /* P01 = ESI; */
276 jitCompA0001_movEbpDispReg32(w, 256 + 2 * 32 + 0, 7 /* EDI */); /* P02 = EDI; */
280 int jitCompA000_selectPRegCache(int pxx, int reg)
282 // if (pxx == 0) reg = 5; /* EBP */
297 int jitCompA000_convTyp(int t)
301 if (1 <= t && t <= 7){
303 } else if (8 <= t && t <= 13){
305 } else if (14 <= t && t <= 15){
307 } else if (16 <= t && t <= 21){
313 int jitCompA000_dataWidth(int t)
316 if (t == 0x0001) r = 256;
318 if (t == 0x0002 / 2) r = 8;
319 if (t == 0x0004 / 2) r = 16;
320 if (t == 0x0006 / 2) r = 32;
321 if (t == 0x0008 / 2) r = 4;
322 if (t == 0x000a / 2) r = 2;
323 if (t == 0x000c / 2) r = 1;
324 if (t == 0x000e / 2) r = 12;
325 if (t == 0x0010 / 2) r = 20;
326 if (t == 0x0012 / 2) r = 24;
327 if (t == 0x0014 / 2) r = 28;
331 static unsigned char *errfnc;
333 void jitCompA0001_checkType0(struct JitCompWork *w, int pxx, int typ, int ac)
335 if (typ <= 0) { w->err = JITC_ERR_BADTYPE; }
336 if (typ > 0x7f) { w->err = JITC_ERR_INTERNAL; }
337 jitCompA0001_movReg32EbpDisp(w, 0 /* EAX */, 256 + pxx * 32 + 4); /* MOV(EAX, [EBP+?]); */ /* typ */
338 jitCompPutByte3(w->dst, 0x83, 0xf8, typ & 0x7f); /* CMP(EAX, ?); */
339 jitCompPutByte2(w->dst, 0x0f, 0x85); /* JNE */
340 jitCompPutImm32(w, errfnc - (w->dst + 4));
344 void jitCompA0001_checkType(struct JitCompWork *w, int pxx, int typ, int ac)
346 // 将来的にはaliveやアクセス権チェックも入れる
348 jitCompA0001_checkType0(w, pxx, typ, ac);
352 void jitCompA0001_checkLimit(struct JitCompWork *w, int reg, int pxx)
354 jitCompPutByte1(w->dst, 0x3b); /* CMP(reg, [EBP+?]); */
355 jitCompA0001_85DispN(w, 256 + pxx * 32 + 8, reg); /* p0 */
356 jitCompPutByte2(w->dst, 0x0f, 0x82); /* JB */
357 jitCompPutImm32(w, errfnc - (w->dst + 4));
358 jitCompPutByte1(w->dst, 0x3b); /* CMP(reg, [EBP+?]); */
359 jitCompA0001_85DispN(w, 256 + pxx * 32 + 12, reg); /* p1 */
360 jitCompPutByte2(w->dst, 0x0f, 0x83); /* JAE */
361 jitCompPutImm32(w, errfnc - (w->dst + 4));
365 void func3c(char *ebp, int opt, int r1, int p1, int lenR, int lenP, int r0, int p0);
366 void func3d(char *ebp, int opt, int r1, int p1, int lenR, int lenP, int r0, int p0);
367 void funcf4(char *ebp, int pxx, int typ, int len);
368 void funcf5(char *ebp, int pxx, int typ, int len); // pxxはダミーで参照されない.
369 void funcf6(char *ebp, int pxx, int typ, int len);
370 void funcf7(char *ebp, int pxx, int typ, int len); // typとlenはダミーで参照されない.
371 // F5の場合、decoderが対応するalloc-freeを結びつけるのが簡単で、typやlenを指定必須にしてもフロントエンドコードに影響はない.
373 void errHndl(HOSECPU_RuntimeEnvironment *r);
377 * dst1 : 書き込みアドレスの最大値
378 * src : 現在の読み込みアドレス(ヘッダ部は飛ばしてある
379 * src1 : 読み込みアドレスの最大値
380 * src0 : 読み込みバイナリのアドレス
382 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)
384 /* 本来ならこのレイヤでは文法チェックしない */
386 struct JitCompWork w;
387 unsigned char *dst00 = dst, *enter0 = NULL, *tmp_ucp;
389 const unsigned char *oldsrc;
390 int timecount = 0, i, j = 0, lastlabel = -1, debugInfo0 = -1;
391 int reg0, reg1, reg2, cmp0reg = -1, cmp0lev = 0;
392 w.dst = w.dst0 = dst;
394 w.maxLabels = maxLabels;
396 if ((flags & JITC_NOSTARTUP) == 0) {
397 jitCompPutByte1(w.dst, 0x60); /* PUSHAD(); */
398 jitCompA000_loadRegCacheAll(&w); /* start-up */
399 jitCompA000_loadPRegCacheAll(&w);
401 if (level <= JITC_LV_SLOWER) {
402 jitCompPutByte2(w.dst, 0x31, 0xc0); /* XOR(EAX, EAX); */
403 jitCompA0001_movEbpDispReg32(&w, 2304 + 0, 0 /* EAX */); /* MOV(debugInfo0, EAX); */
404 jitCompPutByte1(w.dst, 0xb8); /* MOV(EAX, ?); */
405 jitCompPutImm32(&w, debugInfo1);
406 jitCompA0001_movEbpDispReg32(&w, 2304 + 4, 0 /* EAX */); /* MOV(debugInfo1, EAX); */
409 w.prefix = 0; //0x04 CND 命令で変更される
410 if (w.dst + 256 > dst1) { w.err = JITC_ERR_DST1; goto err_w; } // 書き込み領域が残り256バイト未満ならエラー
412 if (timecount >= 64) {
414 /* 未完成(timeoutチェックコードを入れる) */
416 prefix_continue: // CND命令実行後ここに戻る
420 if (w.prefix != 0) { w.err = JITC_ERR_PREFIX; goto err_w; } // 「条件付きでNOPを実行」するなんて、矛盾している!
426 * LB : ラベル設置命令。(6byte)
428 * ・timecount++し、timecountのチェックをする。
430 * ・割り込みがある場合、このタイミングで割り込みを発生させる。
437 if (enter0 == NULL && (src[6] == 0x3c /* 多数のレジスタをスタックに退避 */ || (src[6] == 0xfe/* REMARK */ && src[7] == 0x01 && src[9] == 0x3c))) { //beginFunc()中のLB
438 // LB命令の後に0x3C命令・・・beginFunc()
439 jitCompPutByte1(w.dst, 0xe9); // (x86) JMP rel32 : 次の命令との相対オフセットだけ相対ニアジャンプする
441 jitCompPutImm32(&w, 0); // 飛び相対座標が0 ・・・パイプラインのフラッシュ??
443 if (src[6] == 0x34) { // LBの次の命令がDATA ・・・DAT_SA0(label, typ32, length) ・・・メモリ確保命令
445 jitCompPutByte1(w.dst, 0xe9); // (x86) JMP rel32 : 次の命令との相対オフセットだけ相対ニアジャンプする
446 i = jitCompGetImm32(&src[7]); // type32 を取得
449 i = jitCompA000_convTyp(i);
451 if (i == 2 || i == 3) { j = 1; }
452 if (i == 4 || i == 5) { j = 2; }
453 if (i == 6 || i == 7) { j = 4; }
455 j *= jitCompGetImm32(&src[11]);
456 if (j <= 0) w.err = JITC_ERR_BADTYPE;
457 jitCompPutImm32(&w, j);
458 #if (jitCompA0001_OPTIMIZE_JMP != 0)
459 if (j <= 127 - jitCompA0001_OPTIMIZE_ALIGN) {
461 jitCompPutByte2(w.dst, 0xeb, j);
465 #if (jitCompA0001_OPTIMIZE_ALIGN != 0)
467 i = ((int)w.dst) & (jitCompA0001_OPTIMIZE_ALIGN - 1); /* ALIGNで割ったあまりを計算 */
469 i = jitCompA0001_OPTIMIZE_ALIGN - i;
470 if (i == 1) { jitCompPutByte1(w.dst, 0x90); j += i; } /* NOP(); */
471 if (i == 2) { jitCompPutByte2(w.dst, 0x89, 0xc0); j += i; } /* MOV(EAX, EAX); */
472 if (i == 3) { jitCompPutByte3(w.dst, 0x8d, 0x76, 0x00); j += i; } /* LEA(ESI, [ESI+0]); */
473 if (i == 4) { jitCompPutByte4(w.dst, 0x8d, 0x74, 0x26, 0x00); j += i; } /* LEA(ESI, [ESI*1+0]); */
474 if (i == 5) { jitCompPutByte1(w.dst, 0x0d); jitCompPutImm32(&w, 0); j += i; } /* OR(EAX, 0); */
475 if (i == 6) { jitCompPutByte2(w.dst, 0x8d, 0xb6); jitCompPutImm32(&w, 0); j += i; } /* LEA(ESI, [ESI+0]); */
476 if (i >= 7) { jitCompPutByte3(w.dst, 0x8d, 0xb4, 0x26); jitCompPutImm32(&w, 0); j += 7; } /* LEA(ESI, [ESI*1+0]); */
479 if (src[6] == 0x34) {
480 tmp_ucp[1] = j & 0xff;
481 if (*tmp_ucp == 0xe9) {
482 tmp_ucp[2] = (j >> 8) & 0xff;
483 tmp_ucp[3] = (j >> 16) & 0xff;
484 tmp_ucp[4] = (j >> 24) & 0xff;
487 if ((flags & JITC_PHASE1) == 0) {
488 i = jitCompGetLabelNum(&w, src + 2);
489 //printf("i=%06X %06X\n", i, src-src0);
490 if (label[i].opt != 0 && w.err == 0) { w.err = JITC_ERR_LABELREDEF; goto err_w; }
491 if (w.prefix != 0) { w.err = JITC_ERR_PREFIX; goto err_w; }
492 label[i].opt = src[1] + 1;
493 label[i].typ = 0; /* TYP_CODE */
495 label[i].p1 = w.dst + 1;
500 /* 未完成(timeoutチェックコードを入れる) */
503 case 0x02: /* LIMM */
506 * LIMM : 定数即値代入命令(6byte)
511 * ・reg3F は条件比較慣用句指定用&演算命令即値慣用句指定用。よってCND命令の直後では使用できない。
514 if (src[1] == 0x3f && w.prefix != 0) w.err = JITC_ERR_PREFIX; // CND命令の直後でR3Fを書き換えるなんて変だよね
516 #if (jitCompA0001_USE_R3F_IMM32 != 0)
517 if (src[1] == 0x3f) { // R3Fへの代入は例外敵に、 w.r3f を使用
518 w.r3f = jitCompGetImm32(src + 2);
522 i = jitCompGetImm32(src + 2); // 与えられた即値(第二引数)を取得
524 /* R00-R02 なら EBX, ECX, EDX 、それ以外なら EAX のレジスタIDを reg0 に代入 */
525 reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
527 #if (jitCompA0001_OPTIMIZE_MOV != 0)
529 jitCompPutByte2(w.dst, 0x31, 0xc0 | reg0 << 3 | reg0); /* XOR(reg0, reg0); */
530 jitCompA0001_movRxxEax(&w, src[1]);
535 /* reg0 のレジスタに対応したMOV命令を発行 */
536 jitCompPutByte1(w.dst, 0xb8 | reg0); /* MOV(reg0, ?); == 10111000b+wr imm32 */
537 jitCompPutImm32(&w, i);
539 if (reg0 == 0) // R03以降の、レジスタの内容をメモリ上に格納してエミュレートする場合
541 jitCompA0001_movRxxEax(&w, src[1]);
544 case 0x03: /* PLIMM */ /* 未完成(plsまで対応) */
547 * PLIMM : ラベル番号代入命令(6byte)
557 i = jitCompGetLabelNum(&w, src + 2); // Pxxに代入するラベルの番号(第二引数)
558 if ((flags & JITC_PHASE1) != 0 && w.err == 0) { // Phase 1であるならば
559 if (label[i].opt == 0) { w.err = JITC_ERR_LABELNODEF; goto err_w; } // 指定されたラベル番号は存在しない
560 if (src[1] != 0x3f && label[i].opt != 2) { w.err = JITC_ERR_LABELTYP; goto err_w; } //
561 if (src[1] == 0x3f && label[i].typ != 0) { w.err = JITC_ERR_LABELTYP; goto err_w; } // プログラムカウンタに TYP_CODEでない値は代入できない
563 if (src[1] == 0x3f) { // プログラムカウンタへの代入なら
564 if (w.prefix == 0) { // CND命令による条件付きでなければ、即座に移動
565 jitCompPutByte1(w.dst, 0xe9); /* JMP(?); */
574 * いま、dstの末端はJZ命令になっている。 0x0F 0x84 cd
578 w.dst[-1] = w.dst[-2] ^ 0xf1; /* 74->85, 75->84 */
584 if ((flags & JITC_PHASE1) != 0 || (((flags & JITC_PHASE1) == 0) && label[i].opt != 0)) // label番号iが確保されていれば (このif文は意味をなさない)
585 j = label[i].p - (w.dst + 4); // j はとび先の相対番地
586 jitCompPutImm32(&w, j); // JMP もしくは JZ 命令のアドレス部を記述
587 #if (jitCompA0001_OPTIMIZE_JMP != 0)
588 if (-128 - 3 <= j && j < 0) {
589 if (w.dst[-5] == 0xe9) {
592 jitCompPutByte1(w.dst, 0xeb); /* JMP(?); */
597 jitCompPutByte1(w.dst, w.dst[1] ^ 0xf0);
599 jitCompPutByte1(w.dst, j & 0xff);
603 else { // プログラムカウンタ以外への代入
605 // 代入先が P01, P02なら ESI, EDI,それ以外ならEAXを指定
606 reg0 = jitCompA000_selectPRegCache(src[1], 0 /* EAX */);
607 jitCompPutByte1(w.dst, 0xb8 | reg0); /* MOV(reg0, ?); */
608 jitCompPutImm32(&w, (int)label[i].p); // ラベルのパスを各レジスタに代入
610 // レジスタへの代入をメモリでエミュレーションする場合は、スタックに積む。
612 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32, 0); /* MOV([EBP+?], EAX); */
614 if (level < JITC_LV_FASTEST) {
615 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 8, reg0); /* MOV([EBP+?], reg0); */ /* p0 */
616 jitCompPutByte1(w.dst, 0xb8); /* MOV(EAX, ?); */
617 jitCompPutImm32(&w, label[i].typ);
618 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 4, 0); /* MOV([EBP+?], EAX); */ /* typ */
619 jitCompPutByte1(w.dst, 0xb8); /* MOV(EAX, ?); */
620 jitCompPutImm32(&w, (int)label[i].p1);
621 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 12, 0); /* MOV([EBP+?], EAX); */ /* p1 */
622 jitCompPutByte2(w.dst, 0x31, 0xc0); /* XOR(EAX, EAX); */
623 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 16, 0); /* MOV([EBP+?], EAX); */ /* liveSign */
624 jitCompA0001_movReg32EbpDisp(&w, 0, 2320); /* MOV(EAX, ptrCtrl); */
625 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 20, 0); /* MOV([EBP+?], EAX); */ /* pls */
630 case 0x04: /* CND (prefix) */
634 * 与えられたRxxの最下位ビットが1であれば後続の命令を実行、そうでなければ飛ばす。
637 if (src[1] >= 0x40) w.err = JITC_ERR_REGNUM; // R00-R3F 以外のレジスタは比較対象にできない
639 // 比較対象のレジスタがメモリ上にあれば-1, それ以外なら適切なレジスタ番号を返す
640 reg0 = jitCompA000_selectRegCache(src[1], -1 /* mem */);
643 if (reg0 < 0) { //比較対象のレジスタはメモリ上にある
644 jitCompPutByte1(w.dst, 0xf7); /* TEST([EBP+?],1); */
645 jitCompA0001_85DispN(&w, src[1] * 4, 0);
648 jitCompPutByte2(w.dst, 0xf7, 0xc0 | reg0); /* TEST(reg0,1); */
650 jitCompPutImm32(&w, 1);
653 jitCompPutByte2(w.dst, 0x74, 0x00); /* JZ($+2) */
655 if (w.err != 0) goto err_w;
657 w.prefix = 1; // プリフィックスをセット
659 goto prefix_continue;
661 case 0x08: /* LMEM */ /* 完成 */
662 i = jitCompGetImm32(src + 2);
663 if (i == 0x0001) w.err = JITC_ERR_BADTYPE;
664 if (level < JITC_LV_FASTER) {
665 jitCompA0001_checkType(&w, src[6], i, 0); // read
668 reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
669 reg1 = jitCompA000_selectPRegCache(src[6], 2 /* EDX */);
670 if (reg0 != 0 /* EAX */ && reg1 == 2 /* EDX */)
672 if (reg1 == 2 /* EDX */)
673 jitCompA000_storeRegCacheEdx(&w);
674 if (reg1 <= 3 /* EAX, EDX */)
675 jitCompA0001_movReg32EbpDisp(&w, reg1, 256 + src[6] * 32 + 0); /* MOV(reg1, [EBP+?]); */
676 if (level < JITC_LV_FASTER)
677 jitCompA0001_checkLimit(&w, reg1, src[6]);
678 i = jitCompA000_convTyp(jitCompGetImm32(src + 2));
681 jitCompPutByte3(w.dst, 0x0f, 0xbe, reg0 << 3 | reg1); /* MOVSX(reg0,BYTE [reg1]); */
684 jitCompPutByte3(w.dst, 0x0f, 0xb6, reg0 << 3 | reg1); /* MOVZX(reg0,BYTE [reg1]); */
687 jitCompPutByte3(w.dst, 0x0f, 0xbf, reg0 << 3 | reg1); /* MOVSX(reg0,WORD [reg1]); */
690 jitCompPutByte3(w.dst, 0x0f, 0xb7, reg0 << 3 | reg1); /* MOVZX(reg0,WORD [reg1]); */
694 jitCompPutByte2(w.dst, 0x8b, reg0 << 3 | reg1); /* MOV(reg0, [reg1]); */
697 w.err = JITC_ERR_BADTYPE;
699 if (reg0 == 0 /* EAX */)
700 jitCompA0001_movRxxEax(&w, src[1]);
701 if (reg1 == 2 /* EDX */)
702 jitCompA000_loadRegCacheEdx(&w);
705 case 0x09: /* SMEM */ /* 完成 */
706 i = jitCompGetImm32(src + 2);
707 if (i == 0x0001) w.err = JITC_ERR_BADTYPE;
708 if (level < JITC_LV_FASTER) {
709 jitCompA0001_checkType(&w, src[6], i, 1); // write
712 reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
713 reg1 = jitCompA000_selectPRegCache(src[6], 2 /* EDX */);
714 if (reg0 != 0 /* EAX */ && reg1 == 2 /* EDX */)
716 if (reg1 == 2 /* EDX */)
717 jitCompA000_storeRegCacheEdx(&w);
718 if (reg1 <= 3 /* EAX, EDX */)
719 jitCompA0001_movReg32EbpDisp(&w, reg1, 256 + src[6] * 32 + 0); /* MOV(reg1, [EBP+?]); */
720 if (level < JITC_LV_FASTER)
721 jitCompA0001_checkLimit(&w, reg1, src[6]);
722 if (reg0 == 0 /* EAX */)
723 jitCompA0001_movEaxRxx(&w, src[1]);
725 i = jitCompA000_convTyp(jitCompGetImm32(src + 2));
729 jitCompPutByte2(w.dst, 0x88, reg0 << 3 | reg1); /* MOV([reg1], BYTE(reg0)); */
733 jitCompPutByte3(w.dst, 0x66, 0x89, reg0 << 3 | reg1); /* MOV([reg1], WORD(reg0)); */
737 jitCompPutByte2(w.dst, 0x89, reg0 << 3 | reg1); /* MOV([reg1], reg0); */
740 w.err = JITC_ERR_BADTYPE;
742 if (reg1 == 2 /* EDX */)
743 jitCompA000_loadRegCacheEdx(&w);
746 case 0x0a: /* PLMEM */ /* 完成 */
747 i = jitCompGetImm32(src + 2);
748 if (i != 0x0001) w.err = JITC_ERR_BADTYPE;
749 if (level < JITC_LV_FASTER) {
750 jitCompA0001_checkType(&w, src[6], i, 0); // read
753 reg0 = jitCompA000_selectPRegCache(src[1], 0 /* EAX */);
754 reg1 = jitCompA000_selectPRegCache(src[6], 2 /* EDX */);
755 // if (reg0 != 0 /* EAX */ && reg1 == 2 /* EDX */) /* これをやってはいけない!(by K, 2013.08.02) */
756 // reg1 = 0; /* EAX */
757 if (reg0 == reg1 && reg0 != 0) { // bugfix: hinted by yao, 2013.09.14. thanks!
758 jitCompA000_storePRegCacheAll(&w);
761 if (reg1 == 2 /* EDX */)
762 jitCompA000_storeRegCacheEdx(&w);
763 if (reg1 <= 3 /* EAX, EDX */)
764 jitCompA0001_movReg32EbpDisp(&w, reg1, 256 + src[6] * 32 + 0); /* MOV(reg1, [EBP+?]); */
765 if (level < JITC_LV_FASTER)
766 jitCompA0001_checkLimit(&w, reg1, src[6]);
767 jitCompPutByte2(w.dst, 0x8b, reg0 << 3 | reg1); /* MOV(reg0, [reg1]); */
768 if (reg0 == 0 /* EAX */)
769 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 0, 0); /* MOV([EBP+?], EAX); */
770 for (i = 4; i < 32; i += 4) {
771 jitCompPutByte3(w.dst, 0x8b, 0x40 | reg1, i); /* MOV(EAX, [reg1+?]); */
772 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + i, 0 /* EAX */); /* MOV([EBP+?], EAX); */
774 if (reg1 == 2 /* EDX */)
775 jitCompA000_loadRegCacheEdx(&w);
778 case 0x0b: /* PSMEM */ /* 完成 */
779 i = jitCompGetImm32(src + 2);
780 if (i != 0x0001) w.err = JITC_ERR_BADTYPE;
781 if (level < JITC_LV_FASTER) {
782 jitCompA0001_checkType(&w, src[6], i, 1); // write
785 reg0 = jitCompA000_selectPRegCache(src[1], 0 /* EAX */);
786 reg1 = jitCompA000_selectPRegCache(src[6], 2 /* EDX */);
787 // if (reg0 != 0 /* EAX */ && reg1 == 2 /* EDX */) /* これをやってはいけない!(by K, 2013.08.02) */
788 // reg1 = 0; /* EAX */
789 if (reg1 == 2 /* EDX */)
790 jitCompA000_storeRegCacheEdx(&w);
791 if (reg1 <= 3 /* EAX, EDX */)
792 jitCompA0001_movReg32EbpDisp(&w, reg1, 256 + src[6] * 32 + 0); /* MOV(reg1, [EBP+?]); */
793 if (level < JITC_LV_FASTER)
794 jitCompA0001_checkLimit(&w, reg1, src[6]);
795 if (reg0 == 0 /* EAX */)
796 jitCompA0001_movReg32EbpDisp(&w, reg0, 256 + src[1] * 32 + 0); /* MOV(reg0, [EBP+?]); */
797 jitCompPutByte2(w.dst, 0x89, reg0 << 3 | reg1); /* MOV([reg1], reg0); */
798 for (i = 4; i < 32; i += 4) {
799 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[1] * 32 + i); /* MOV(EAX, [EBP+?]); */
800 jitCompPutByte3(w.dst, 0x89, 0x40 | reg1, i); /* MOV([reg1+?], EAX); */
802 if (reg1 == 2 /* EDX */)
803 jitCompA000_loadRegCacheEdx(&w);
806 case 0x0e: /* PADD */ /* 完成 */
807 if (level < JITC_LV_FASTER) {
808 jitCompA0001_checkType0(&w, src[6], jitCompGetImm32(src + 2), 2); // other, aliveテストはとりあえずしない.
811 reg0 = jitCompA000_selectPRegCache(src[1], 0 /* EAX */);
812 reg1 = jitCompA000_selectPRegCache(src[6], -1 /* mem */);
813 if (reg1 < 0 /* mem */)
814 jitCompA0001_movReg32EbpDisp(&w, reg0, 256 + src[6] * 32 + 0); /* MOV(reg0, [EBP+?]); */
815 if (reg1 >= 0 && reg0 != reg1) {
816 jitCompPutByte2(w.dst, 0x89, 0xc0 | reg1 << 3 | reg0); /* MOV(reg0, reg1); */
818 i = jitCompGetImm32(src + 2);
823 i = jitCompA000_convTyp(i);
824 if (0x0002 <= i && i <= 0x0007)
825 j = (i - 0x0002) >> 1;
827 if (j < 0) { w.err = JITC_ERR_BADTYPE; goto err_w; }
828 #if (jitCompA0001_USE_R3F_IMM32 != 0)
829 if (src[7] == 0x3f) {
831 #if (jitCompA0001_USE_R3F_IMM8 != 0)
832 if (-0x80 <= j && j <= 0x7f) {
833 #if (jitCompA0001_USE_R3F_INCDEC != 0)
834 if (j == 1) { jitCompPutByte1(w.dst, 0x40 | reg0); goto padd1; } /* INC */
835 if (j == -1) { jitCompPutByte1(w.dst, 0x48 | reg0); goto padd1; } /* DEC */
837 jitCompPutByte3(w.dst, 0x83, 0xc0 | reg0, j & 0xff); /* ADD(reg0, im8); */
842 jitCompPutByte1(w.dst, 0x05); /* ADD(reg0, ?); */
845 jitCompPutByte2(w.dst, 0x81, 0xc0 | reg0); /* ADD(reg0, ?); */
847 jitCompPutImm32(&w, j);
851 if (src[7] >= 0x40) w.err = JITC_ERR_REGNUM;
853 reg1 = jitCompA000_selectRegCache(src[7], -1 /* mem */);
855 jitCompPutByte2(w.dst, 0x01, 0xc0 | reg1 << 3 | reg0); /* ADD(reg0, reg1); */
858 jitCompPutByte1(w.dst, 0x03); /* ADD(reg0, [EBP+?]); */
859 jitCompA0001_85DispN(&w, src[7] * 4, reg0);
863 reg1 = jitCompA000_selectRegCache(src[7], -1 /* mem */);
865 jitCompA000_storeRegCacheEdx(&w);
867 jitCompA0001_movReg32EbpDisp(&w, reg2, src[7] * 4); /* MOV(reg2, [EBP+?]); */
868 if (reg1 >= 0 && reg1 != reg2) {
869 jitCompPutByte2(w.dst, 0x89, 0xc0 | reg1 << 3 | reg2); /* MOV(reg2, reg1); */
871 jitCompPutByte3(w.dst, 0xc1, 0xe0 | reg2, j); /* SHL(reg2, ?); */
872 jitCompPutByte2(w.dst, 0x01, 0xc0 | reg2 << 3 | reg0); /* ADD(reg0, reg2); */
873 jitCompA000_loadRegCacheEdx(&w);
875 #if (jitCompA0001_USE_R3F_IMM32 != 0)
878 if (reg0 == 0 /* EAX */)
879 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 0, reg0); /* MOV([EBP+?], reg0); */
880 if (src[1] != src[6]) {
881 for (i = 4; i < 32; i += 4) {
882 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[6] * 32 + i); /* MOV(EAX, [EBP+?]); */
883 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + i, 0 /* EAX */); /* MOV([EBP+?], EAX); */
889 case 0x0f: /* PDIF */ /* 未完成 */
890 reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
891 jitCompA000_storePRegCacheAll(&w); // 手抜き.
892 jitCompA0001_checkCompPtr(&w, src[6], src[7]);
893 jitCompA0001_movReg32EbpDisp(&w, reg0, 256 + src[6] * 32 + 0); /* MOV(reg0, [EBP+?]); */
894 jitCompPutByte1(w.dst, 0x2b); /* SUB(EAX, [EBP+?]); */
895 jitCompA0001_85DispN(&w, 256 + src[7] * 32 + 0, reg0);
896 i = jitCompA000_convTyp(jitCompGetImm32(src + 2));
898 if (0x0002 <= i && i <= 0x0007)
899 j = (i - 0x0002) >> 1;
900 if (j < 0) { w.err = JITC_ERR_BADTYPE; goto err_w; }
902 jitCompPutByte3(w.dst, 0xc1, 0xf8 | reg0, j); /* SAR(reg0,?); */
904 if (reg0 == 0 /* EAX */)
905 jitCompA0001_movRxxEax(&w, src[1]);
906 cmp0reg = src[1]; cmp0lev = 1;
915 if (src[1] >= 0x3f) w.err = JITC_ERR_REGNUM;
916 reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
917 reg1 = jitCompA000_selectRegCache(src[2], -1 /* mem */);
918 #if (jitCompA0001_USE_R3F_IMM32 != 0)
919 if (src[2] == 0x3f) { // SUBのみ該当.
920 if (*src != 0x15) w.err = JITC_ERR_REGNUM;
921 reg2 = jitCompA000_selectRegCache(src[3], -1 /* mem */);
923 jitCompA000_storeRegCacheAll(&w);
924 jitCompPutByte1(w.dst, 0xb8 | reg0); /* MOV(reg0, ?); */
925 jitCompPutImm32(&w, w.r3f);
926 jitCompPutByte1(w.dst, 0x2b);
927 jitCompA0001_85DispN(&w, src[3] * 4, reg0);
929 jitCompA0001_movRxxEax(&w, src[1]);
934 jitCompA0001_movReg32EbpDisp(&w, reg0, src[2] * 4); /* MOV(reg0, [EBP+?]); */
936 if (reg1 >= 0 && reg0 != reg1) {
937 jitCompPutByte2(w.dst, 0x89, 0xc0 | reg1 << 3 | reg0); /* MOV(reg0, reg1); */
939 if (!(src[0] == 0x10 && src[3] == 0xff)) { // bugfix: hinted by Iris, 2013.06.26. thanks!
947 if (!(src[0] == 0x10 && src[3] == 0xff)) {
948 #if (jitCompA0001_USE_R3F_IMM32 != 0)
949 if (src[3] == 0x3f) {
950 if (*src == 0x16 && w.r3f == -1) {
951 jitCompPutByte2(w.dst, 0xf7, 0xd8 | reg0); /* NEG(reg0); */
953 jitCompA0001_movRxxEax(&w, src[1]);
956 #if (jitCompA0001_USE_R3F_INCDEC != 0)
957 if ((*src == 0x14 && w.r3f == 1) || (*src == 0x15 && w.r3f == -1)) {
958 jitCompPutByte1(w.dst, 0x40 | reg0); /* INC(reg0); */
960 jitCompA0001_movRxxEax(&w, src[1]);
963 if ((*src == 0x15 && w.r3f == 1) || (*src == 0x14 && w.r3f == -1)) {
964 jitCompPutByte1(w.dst, 0x48 | reg0); /* DEC(reg0); */
966 jitCompA0001_movRxxEax(&w, src[1]);
970 #if (jitCompA0001_USE_R3F_IMM8 != 0)
971 if (-0x80 <= w.r3f && w.r3f <= 0x7f) {
973 static unsigned char basic_op_table_im8[] = { 0xc8, 0xf0, 0xe0, 0, 0xc0, 0xe8 };
974 jitCompPutByte3(w.dst, 0x83, basic_op_table_im8[*src - 0x10] | reg0, w.r3f & 0xff);
977 jitCompPutByte3(w.dst, 0x6b, 0xc0 | reg0 << 3 | reg0, w.r3f & 0xff);
980 jitCompA0001_movRxxEax(&w, src[1]);
984 if (reg0 == 0 /* EAX */) {
985 static unsigned char basic_op_table_im32_eax[] = { 0x0d, 0x35, 0x25, 0, 0x05, 0x2d, 0xc0 };
986 if (*src == 0x16) { jitCompPutByte1(w.dst, 0x69); }
987 jitCompPutByte1(w.dst, basic_op_table_im32_eax[*src - 0x10]);
991 static unsigned char basic_op_table_im32_reg[] = { 0xc8, 0xf0, 0xe0, 0, 0xc0, 0xe8 };
992 jitCompPutByte2(w.dst, 0x81, basic_op_table_im32_reg[*src - 0x10] | reg0);
995 jitCompPutByte2(w.dst, 0x69, 0xc0 | reg0 << 3 | reg0);
998 jitCompPutImm32(&w, w.r3f);
1000 jitCompA0001_movRxxEax(&w, src[1]);
1004 reg1 = jitCompA000_selectRegCache(src[3], -1 /* mem */);
1005 if (src[3] >= 0x40) w.err = JITC_ERR_REGNUM;
1008 static unsigned char basic_op_table_rr[] = { 0x09, 0x31, 0x21, 0, 0x01, 0x29 }; /* op(reg,reg); */
1009 jitCompPutByte2(w.dst, basic_op_table_rr[*src - 0x10], 0xc0 | reg1 << 3 | reg0);
1012 static unsigned char basic_op_table_rm[] = { 0x0b, 0x33, 0x23, 0, 0x03, 0x2b, 0xaf }; /* op(reg,mem); */
1013 jitCompPutByte1(w.dst, basic_op_table_rm[*src - 0x10]);
1014 jitCompA0001_85DispN(&w, src[3] * 4, reg0);
1019 jitCompPutByte3(w.dst, 0x0f, 0xaf, 0xc0 | reg0 << 3 | reg1);
1022 jitCompPutByte2(w.dst, 0x0f, 0xaf);
1023 jitCompA0001_85DispN(&w, src[3] * 4, reg0);
1028 jitCompA0001_movRxxEax(&w, src[1]);
1031 case 0x18: /* SHL */
1032 case 0x19: /* SAR */
1033 if (src[1] >= 0x3f) w.err = JITC_ERR_REGNUM;
1034 if (src[3] >= 0x40) w.err = JITC_ERR_REGNUM;
1035 #if (jitCompA0001_USE_R3F_IMM32 != 0)
1036 if (src[3] == 0x3f) {
1037 reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
1038 reg1 = jitCompA000_selectRegCache(src[2], -1 /* mem */);
1039 if (src[1] >= 0x3f) w.err = JITC_ERR_REGNUM;
1041 jitCompA0001_movReg32EbpDisp(&w, reg0, src[2] * 4); /* MOV(reg1, [EBP+?]); */
1044 jitCompPutByte2(w.dst, 0x89, 0xc0 | reg1 << 3 | reg0); /* MOV(reg0, reg1); */
1047 if (*src == 0x18) { jitCompPutByte3(w.dst, 0xc1, 0xe0 | reg0, w.r3f); } /* SHL(reg0, im8); */
1048 if (*src == 0x19) { jitCompPutByte3(w.dst, 0xc1, 0xf8 | reg0, w.r3f); } /* SAR(reg0, im8); */
1049 if (reg0 == 0 /* EAX */)
1050 jitCompA0001_movRxxEax(&w, src[1]);
1056 jitCompA000_storeRegCacheAll(&w); // 手抜き.
1057 jitCompA0001_movReg32EbpDisp(&w, 1 /* ECX */, src[3] * 4); /* MOV(ECX, [EBP+?]); */
1058 #if (jitCompA0001_USE_R3F_IMM32 != 0)
1059 if (src[2] == 0x3f) {
1060 jitCompPutByte1(w.dst, 0xb8); /* MOV(EAX, ?); */
1061 jitCompPutImm32(&w, w.r3f);
1064 jitCompA0001_movEaxRxx(&w, src[2]);
1067 jitCompA0001_movEaxRxx(&w, src[2]);
1069 if (*src == 0x18) { jitCompPutByte2(w.dst, 0xd3, 0xe0); } /* SHL(EAX, CL); */
1070 if (*src == 0x19) { jitCompPutByte2(w.dst, 0xd3, 0xf8); } /* SAR(EAX, CL); */
1071 jitCompA0001_movRxxEax(&w, src[1]);
1072 jitCompA000_loadRegCacheAll(&w); // 手抜き.
1077 case 0x1a: /* DIV */
1078 case 0x1b: /* MOD */
1079 if (src[1] >= 0x3f) w.err = JITC_ERR_REGNUM;
1080 if (src[2] >= 0x40) w.err = JITC_ERR_REGNUM;
1081 if (src[3] >= 0x40) w.err = JITC_ERR_REGNUM;
1082 jitCompA000_storeRegCacheAll(&w); // 手抜き.
1083 #if (jitCompA0001_USE_R3F_IMM32 != 0)
1084 if (src[3] == 0x3f) {
1085 jitCompPutByte1(w.dst, 0xb8 | 1); /* MOV(ECX, ?); */
1086 jitCompPutImm32(&w, w.r3f);
1089 jitCompA0001_movReg32EbpDisp(&w, 1 /* ECX */, src[3] * 4); /* MOV(ECX, [EBP+?]); */
1091 if (src[2] == 0x3f) {
1092 jitCompPutByte1(w.dst, 0xb8 | 0); /* MOV(EAX, ?); */
1093 jitCompPutImm32(&w, w.r3f);
1096 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, src[2] * 4); /* MOV(EAX, [EBP+?]); */
1099 jitCompA0001_movReg32EbpDisp(&w, 1 /* ECX */, src[3] * 4); /* MOV(ECX, [EBP+?]); */
1100 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, src[2] * 4); /* MOV(EAX, [EBP+?]); */
1102 jitCompPutByte1(w.dst, 0x99); /* CDQ(); */
1103 /* ECXがゼロではないことを確認すべき */
1104 jitCompPutByte2(w.dst, 0xf7, 0xf9); /* IDIV(ECX); */
1105 if (*src == 0x1a) { jitCompA0001_movEbpDispReg32(&w, src[1] * 4, 0 /* EAX */); }
1106 if (*src == 0x1b) { jitCompA0001_movEbpDispReg32(&w, src[1] * 4, 2 /* EDX */); }
1107 jitCompA000_loadRegCacheAll(&w); // 手抜き.
1111 case 0x1c: /* PLMT0 */
1112 case 0x1d: /* PLMT1 */
1113 if (src[1] >= 0x40 || src[2] >= 0x40) w.err = JITC_ERR_PREGNUM;
1114 if (level < JITC_LV_FASTEST) {
1116 if (level < JITC_LV_FASTER) {
1117 // typ が一致していることを確認.
1118 // plsとliveSignが一致していることを確認.
1120 // preg1はp0 <= p <= p1 を満たしているか?.
1121 // 新しいp0/p1は古いp0〜p1に適合しているか?.
1126 case 0x1e: /* PCP */ /* 未完成(p1まで完成) */
1127 if (src[1] >= 0x40 || src[2] >= 0x40) w.err = JITC_ERR_PREGNUM;
1128 if (src[2] == 0x3f) w.err = JITC_ERR_PREGNUM;
1129 if (src[1] != 0x3f) {
1130 /* src[2] == 0xff の場合に対応できてない */
1131 jitCompA000_storePRegCacheAll(&w); // 手抜き.
1132 for (i = 0; i < 32; i += 4) {
1133 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[2] * 32 + i); /* MOV(EAX, [EBP+?]); */
1134 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + i, 0 /* EAX */); /* MOV([EBP+?], EAX); */
1136 jitCompA000_loadPRegCacheAll(&w); // 手抜き.
1139 if (level < JITC_LV_FASTER) {
1140 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[2] * 32 + 4); /* MOV(EAX, [EBP+?]); */ /* typ */
1141 jitCompPutByte3(w.dst, 0x83, 0xf8, 0); /* CMP(EAX, 0); */
1142 jitCompPutByte2(w.dst, 0x0f, 0x85); /* JNE */
1143 jitCompPutImm32(&w, errfnc - (w.dst + 4));
1144 /* セキュリティチェックが足りてない!(aliveとか) */
1147 jitCompA000_storePRegCacheAll(&w); // 手抜き.
1148 jitCompA0001_movReg32EbpDisp(&w, reg0, 256 + src[2] * 32 + 0); /* MOV(EAX, [EBP+?]); */
1149 if (level < JITC_LV_FASTER) {
1150 jitCompPutByte1(w.dst, 0x3b); /* CMP(reg0, [EBP+?]); */
1151 jitCompA0001_85DispN(&w, 256 + src[2] * 32 + 8, reg0); /* p0 */
1152 jitCompPutByte2(w.dst, 0x0f, 0x85); /* JNE */
1153 jitCompPutImm32(&w, errfnc - (w.dst + 4));
1155 jitCompPutByte2(w.dst, 0xff, 0xe0); /* JMP(EAX); */
1159 case 0x1f: /* PCST */
1160 if (jitCompGetImm32(src + 2) == 0) {
1161 if (level < JITC_LV_FASTER)
1162 jitCompA0001_checkType0(&w, src[6], jitCompGetImm32(src + 7), 2);
1163 jitCompA000_storePRegCacheAll(&w); // 手抜き.
1164 for (i = 0; i < 32 - 4; i += 4) {
1165 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[6] * 32 + i); /* MOV(EAX, [EBP+?]); */
1167 jitCompPutByte1(w.dst, 0x0d); /* OR(EAX, ?); */
1168 jitCompPutImm32(&w, 0x80000000);
1170 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + i, 0 /* EAX */); /* MOV([EBP+?], EAX); */
1172 jitCompPutByte1(w.dst, 0xb8); /* MOV(EAX, ?); */
1173 jitCompPutImm32(&w, debugInfo1);
1174 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 28, 0 /* EAX */); /* MOV([EBP+?], EAX); */
1175 jitCompA000_loadPRegCacheAll(&w); // 手抜き.
1179 if (jitCompGetImm32(src + 7) == 0) {
1180 jitCompA000_storePRegCacheAll(&w); // 手抜き.
1181 for (i = 0; i < 32 - 4; i += 4) {
1182 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[6] * 32 + i); /* MOV(EAX, [EBP+?]); */
1184 jitCompPutByte1(w.dst, 0x25); /* AND(EAX, ?); */
1185 jitCompPutImm32(&w, 0x7fffffff);
1187 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + i, 0 /* EAX */); /* MOV([EBP+?], EAX); */
1189 if (level < JITC_LV_FASTER) {
1190 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[6] * 32 + 28); /* MOV(EAX, [EBP+?]); */
1191 jitCompPutByte1(w.dst, 0x3d); /* CMP(EAX, ?); */
1192 jitCompPutImm32(&w, debugInfo1);
1193 jitCompPutByte2(w.dst, 0x74, 8); /* JE */
1194 jitCompPutByte2(w.dst, 0x31, 0xc0); /* XOR(EAX, EAX); (2) */
1195 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 0, 0 /* EAX */); /* MOV([EBP+?], EAX); (1+1+4) */
1197 jitCompA000_loadPRegCacheAll(&w); // 手抜き.
1201 w.err = JITC_ERR_OPECODE;
1204 case 0x20: /* CMPE */
1205 case 0x21: /* CMPNE */
1206 case 0x22: /* CMPL */
1207 case 0x23: /* CMPGE */
1208 case 0x24: /* CMPLE */
1209 case 0x25: /* CMPG */
1210 case 0x26: /* TSTZ */
1211 case 0x27: /* TSTNZ */
1212 reg0 = jitCompA000_selectRegCache(src[2], 0 /* EAX */);
1213 reg1 = jitCompA000_selectRegCache(src[3], -1 /* mem */);
1214 if (src[1] == 0x3f) {
1216 if (w.prefix != 0) { w.err = JITC_ERR_PREFIX; goto err_w; }
1217 if (src[4] != 0x04 || src[5] != 0x3f || src[6] != 0x03 || src[7] != 0x3f) {
1218 w.err = JITC_ERR_IDIOM; goto err_w;
1222 jitCompA0001_movEaxRxx(&w, src[2]);
1223 #if (jitCompA0001_USE_R3F_IMM32 != 0)
1224 if (src[3] == 0x3f) {
1225 #if (jitCompA0001_OPTIMIZE_CMP != 0)
1226 if ((*src <= 0x25 && w.r3f == 0) || (*src >= 0x26 && w.r3f == -1)) {
1228 if (cmp0reg == src[2]) {
1229 if (cmp0lev >= 1 && (src[0] == 0x20 || src[0] == 0x21 || src[0] == 0x26 || src[0] == 0x27))
1231 if (cmp0lev >= 2 && (src[0] == 0x22 || src[0] == 0x23 || src[0] == 0x24 || src[0] == 0x25))
1235 jitCompPutByte2(w.dst, 0x85, 0xc0 | reg0 << 3 | reg0); /* TEST(reg0, reg0); */
1242 #if (jitCompA0001_USE_R3F_IMM8 != 0)
1243 if (-0x80 <= w.r3f && w.r3f <= 0x7f && *src <= 0x25) {
1244 jitCompPutByte3(w.dst, 0x83, 0xf8 | reg0, w.r3f);
1249 if (*src <= 0x25) { jitCompPutByte1(w.dst, 0x3d); }
1250 if (*src >= 0x26) { jitCompPutByte1(w.dst, 0xa9); }
1253 if (*src <= 0x25) { jitCompPutByte2(w.dst, 0x81, 0xf8 | reg0); }
1254 if (*src >= 0x26) { jitCompPutByte2(w.dst, 0xf7, 0xc0 | reg0); }
1256 jitCompPutImm32(&w, w.r3f);
1260 if (src[3] >= 0x40) w.err = JITC_ERR_PREGNUM;
1262 if (*src <= 0x25) { jitCompPutByte2(w.dst, 0x39, 0xc0 | reg1 << 3 | reg0); }
1263 if (*src >= 0x26) { jitCompPutByte2(w.dst, 0x85, 0xc0 | reg1 << 3 | reg0); }
1266 if (*src <= 0x25) { jitCompPutByte1(w.dst, 0x3b); }
1267 if (*src >= 0x26) { jitCompPutByte1(w.dst, 0x85); }
1268 jitCompA0001_85DispN(&w, src[3] * 4, reg0);
1271 if (w.err != 0) goto err_w;
1272 static unsigned char cmpcc_table0[] = {
1273 0x04, 0x05, 0x0c, 0x0d, 0x0e, 0x0f, 0x04, 0x05, /* CMPcc, TSTcc */
1274 0x04, 0x05, 0x02, 0x03, 0x06, 0x07 /* PCMPcc */
1276 #if (jitCompA0001_USE_R3F_CMPJMP != 0)
1277 if (src[1] == 0x3f) {
1279 jitCompPutByte2(w.dst, 0x0f, 0x80 | cmpcc_table0[*src - 0x20]);
1281 i = jitCompGetLabelNum(&w, src + 2);
1282 if ((flags & JITC_PHASE1) != 0 && w.err != 0) {
1283 if (label[i].opt == 0) { w.err = JITC_ERR_LABELNODEF; goto err_w; }
1284 // if (label[i].typ != 1) { w.err = JITC_ERR_LABELTYP; goto err_w; }
1287 if ((flags & JITC_PHASE1) != 0 || (((flags & JITC_PHASE1) == 0) && label[i].opt != 0))
1288 j = label[i].p - (w.dst + 4);
1289 jitCompPutImm32(&w, j);
1290 #if (jitCompA0001_OPTIMIZE_JMP != 0)
1291 if (-128 - 4 <= j && j < 0) {
1294 jitCompPutByte2(w.dst, w.dst[1] ^ 0xf0, j & 0xff);
1298 if (w.err != 0) goto err_w;
1303 reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
1304 jitCompPutByte3(w.dst, 0x0f, 0x90 | cmpcc_table0[*src - 0x20], 0xc0 | reg0); /* SETcc(BYTE(reg0)); */
1305 jitCompPutByte3(w.dst, 0x0f, 0xb6, 0xc0 | reg0 << 3 | reg0); /* MOVZX(reg0, BYTE(reg0)); */
1306 jitCompPutByte2(w.dst, 0xf7, 0xd8 | reg0); /* NEG(reg0); */
1308 jitCompA0001_movRxxEax(&w, src[1]);
1313 case 0x28: /* PCMPE */
1314 case 0x29: /* PCMPNE */
1315 case 0x2a: /* PCMPL */
1316 case 0x2b: /* PCMPGE */
1317 case 0x2c: /* PCMPLE */
1318 case 0x2d: /* PCMPG */
1319 if (src[1] == 0x3f) {
1321 if (w.prefix != 0) { w.err = JITC_ERR_PREFIX; goto err_w; }
1322 if (src[4] != 0x04 || src[5] != 0x3f || src[6] != 0x03 || src[7] != 0x3f) {
1323 w.err = JITC_ERR_IDIOM; goto err_w;
1326 if (src[2] >= 0x40) w.err = JITC_ERR_PREGNUM;
1327 jitCompA000_storePRegCacheAll(&w); // 手抜き.
1329 jitCompA0001_checkCompPtr(&w, src[2], src[3]);
1330 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[2] * 32 + 0); /* MOV(EAX, [EBP+?]); */
1331 if (src[3] != 0xff) {
1332 jitCompPutByte1(w.dst, 0x3b); /* CMP(EAX, [EBP+?]); */
1333 jitCompA0001_85DispN(&w, 256 + src[3] * 32 + 0, 0);
1336 /* ヌルポインタとの比較はこれでいいのか?たぶんよくない */
1337 jitCompPutByte3(w.dst, 0x83, 0xf8, 0x00); /* CMP(EAX, 0); */
1342 case 0x30: /* talloc(old:F4) */
1343 case 0x31: /* tfree(old:F5) */
1344 case 0x32: /* malloc(old:F6) */
1345 case 0x33: /* mfree(old:F7) */
1346 jitCompA000_storeRegCacheAll(&w); // 手抜き.
1347 jitCompA000_storePRegCacheAll(&w); // 手抜き.
1348 jitCompPutByte2(w.dst, 0x6a, src[3]); /* PUSH(?); */
1349 jitCompPutByte2(w.dst, 0x6a, src[2]); /* PUSH(?); */
1350 jitCompPutByte2(w.dst, 0x6a, src[1]); /* PUSH(?); */
1351 jitCompPutByte1(w.dst, 0x55); /* PUSH(EBP); */
1352 jitCompPutByte1(w.dst, 0xe8);
1353 if (*src == 0x30) j = ((unsigned char *)&funcf4) - (w.dst + 4);
1354 if (*src == 0x31) j = ((unsigned char *)&funcf5) - (w.dst + 4);
1355 if (*src == 0x32) j = ((unsigned char *)&funcf6) - (w.dst + 4);
1356 if (*src == 0x33) j = ((unsigned char *)&funcf7) - (w.dst + 4);
1357 jitCompPutImm32(&w, j);
1358 jitCompPutByte3(w.dst, 0x83, 0xc4, 0x10); /* ADD(ESP,16); */
1359 jitCompA000_loadRegCacheAll(&w); // 手抜き.
1360 jitCompA000_loadPRegCacheAll(&w); // 手抜き.
1364 case 0x34: /* data (暫定) */
1366 if (w.prefix != 0) { w.err = JITC_ERR_PREFIX; goto err_w; }
1367 int k = jitCompGetImm32(&src[1]), tmpData, bitCount, dataWidth = jitCompA000_dataWidth(k);
1368 if (lastlabel >= 0 && label[lastlabel].typ == 0)
1369 label[lastlabel].typ = k;
1371 i = jitCompA000_convTyp(k);
1372 if (i < 2 || i > 7) { w.err = JITC_ERR_BADTYPE; goto err_w; }
1374 j = jitCompGetImm32(&src[5]);
1380 if (src >= src1) { w.err = JITC_ERR_SRC1; src = oldsrc; goto err_w; }
1381 if (w.dst + 256 > dst1) { w.err = JITC_ERR_DST1; src = oldsrc; goto err_w; }
1383 for (k = 0; k < dataWidth; k++) {
1384 tmpData = tmpData << 1 | ((*src >> bitCount) & 1);
1391 if ((i & 1) == 0 && dataWidth <= 31 && (tmpData >> (dataWidth - 1)) != 0) {
1392 tmpData -= 1 << dataWidth;
1394 if (i == 2 || i == 3) { jitCompPutByte1(w.dst, tmpData & 0xff); }
1395 if (i == 4 || i == 5) { jitCompPutByte2(w.dst, tmpData & 0xff, (tmpData >> 8) & 0xff); }
1396 if (i == 6 || i == 7) { jitCompPutByte4(w.dst, tmpData & 0xff, (tmpData >> 8) & 0xff, (tmpData >> 16) & 0xff, (tmpData >> 24) & 0xff); }
1402 if (src >= src1) { w.err = JITC_ERR_SRC1; src = oldsrc; goto err_w; }
1403 if (w.dst + 256 > dst1) { w.err = JITC_ERR_DST1; src = oldsrc; goto err_w; }
1404 i = jitCompGetImm32(src);
1406 if ((flags & JITC_PHASE1) != 0 && w.err == 0) {
1407 if (label[i].opt == 0) { w.err = JITC_ERR_LABELNODEF; goto err_w; }
1409 jitCompPutImm32(&w, (int)label[i].p);
1410 jitCompPutImm32(&w, label[i].typ);
1411 jitCompPutImm32(&w, (int)label[i].p);
1412 jitCompPutImm32(&w, (int)label[i].p1);
1413 jitCompPutImm32(&w, 0); /* liveSign */
1414 jitCompPutImm32(&w, 2320); /* pls */
1415 jitCompPutImm32(&w, 0);
1416 jitCompPutImm32(&w, 0);
1420 if (lastlabel >= 0 && label[lastlabel].p1 < w.dst)
1421 label[lastlabel].p1 = w.dst;
1424 case 0x3c: /* ENTER */
1425 jitCompA000_storeRegCacheAll(&w); // 手抜き.
1426 jitCompA000_storePRegCacheAll(&w); // 手抜き.
1427 jitCompPutByte2(w.dst, 0x6a, src[6]); /* PUSH(?); */
1428 jitCompPutByte2(w.dst, 0x6a, src[5]); /* PUSH(?); */
1429 jitCompPutByte2(w.dst, 0x6a, src[4] & 0x0f); /* PUSH(?); */
1430 jitCompPutByte2(w.dst, 0x6a, (src[4] >> 4) & 0x0f); /* PUSH(?); */
1431 jitCompPutByte2(w.dst, 0x6a, src[3]); /* PUSH(?); */
1432 jitCompPutByte2(w.dst, 0x6a, src[2]); /* PUSH(?); */
1433 jitCompPutByte2(w.dst, 0x6a, src[1]); /* PUSH(?); */
1434 jitCompPutByte1(w.dst, 0x55); /* PUSH(EBP); */
1435 jitCompPutByte1(w.dst, 0xe8);
1436 j = ((unsigned char *)&func3c) - (w.dst + 4);
1437 jitCompPutImm32(&w, j);
1438 jitCompPutByte3(w.dst, 0x83, 0xc4, 0x20); /* ADD(ESP,32); */
1439 jitCompA000_loadRegCacheAll(&w); // 手抜き.
1440 jitCompA000_loadPRegCacheAll(&w); // 手抜き.
1444 case 0x3d: /* LEAVE */
1445 jitCompA000_storeRegCacheAll(&w); // 手抜き.
1446 jitCompA000_storePRegCacheAll(&w); // 手抜き.
1447 jitCompPutByte2(w.dst, 0x6a, src[6]); /* PUSH(?); */
1448 jitCompPutByte2(w.dst, 0x6a, src[5]); /* PUSH(?); */
1449 jitCompPutByte2(w.dst, 0x6a, src[4] & 0x0f); /* PUSH(?); */
1450 jitCompPutByte2(w.dst, 0x6a, (src[4] >> 4) & 0x0f); /* PUSH(?); */
1451 jitCompPutByte2(w.dst, 0x6a, src[3]); /* PUSH(?); */
1452 jitCompPutByte2(w.dst, 0x6a, src[2]); /* PUSH(?); */
1453 jitCompPutByte2(w.dst, 0x6a, src[1]); /* PUSH(?); */
1454 jitCompPutByte1(w.dst, 0x55); /* PUSH(EBP); */
1455 jitCompPutByte1(w.dst, 0xe8);
1456 j = ((unsigned char *)&func3d) - (w.dst + 4);
1457 jitCompPutImm32(&w, j);
1458 jitCompPutByte3(w.dst, 0x83, 0xc4, 0x20); /* ADD(ESP,32); */
1459 jitCompA000_loadRegCacheAll(&w); // 手抜き.
1460 jitCompA000_loadPRegCacheAll(&w); // 手抜き.
1464 case 0xfe: /* remark */
1465 if (src[1] == 0x01 && src[2] == 0x00) { // DBGINFO1
1466 if (level <= JITC_LV_SLOWER) {
1467 jitCompPutByte1(w.dst, 0xb8); /* MOV(EAX, ?); */
1468 jitCompPutImm32(&w, debugInfo1);
1469 jitCompA0001_movEbpDispReg32(&w, 2304 + 4, 0 /* EAX */); /* MOV(debugInfo1, EAX); */
1472 if (src[1] == 0x01 && src[2] == 0x03) { // DBGINFO1CLR
1473 if (level <= JITC_LV_SLOWER) {
1474 jitCompPutByte1(w.dst, 0xb8); /* MOV(EAX, ?); */
1475 jitCompPutImm32(&w, -1);
1476 jitCompA0001_movEbpDispReg32(&w, 2304 + 4, 0 /* EAX */); /* MOV(debugInfo1, EAX); */
1479 if (src[1] == 0x05 && src[2] == 0x00) { // DBGINFO0
1480 if (level <= JITC_LV_SLOWEST) {
1481 debugInfo0 = jitCompGetImm32(src + 3);
1482 // jitCompPutByte1(w.dst, 0xbf); /* MOV(EDI, ?); */
1483 // jitCompPutImm32(&w, debugInfo0);
1484 jitCompPutByte1(w.dst, 0xb8); /* MOV(EAX, ?); */
1485 jitCompPutImm32(&w, debugInfo0);
1486 jitCompA0001_movEbpDispReg32(&w, 2304 + 0, 0 /* EAX */); /* MOV(debugInfo0, EAX); */
1492 w.err = JITC_ERR_OPECODE;
1495 if (w.err != 0) goto err_w;
1496 jitCompA0001_fixPrefix(&w);
1497 if (w.err != 0) goto err_w;
1498 src += jitCompCmdLen(src);
1500 if (enter0 != NULL) {
1501 j = w.dst - (enter0 + 4);
1502 enter0[0] = j & 0xff;
1503 enter0[1] = (j >> 8) & 0xff;
1504 enter0[2] = (j >> 16) & 0xff;
1505 enter0[3] = (j >> 24) & 0xff;
1507 if ((flags & JITC_NOSTARTUP) == 0) {
1508 jitCompA000_storeRegCacheAll(&w);
1509 jitCompA000_storePRegCacheAll(&w);
1510 jitCompPutByte1(w.dst, 0x61); /* POPAD(); */
1512 if ((flags & JITC_PHASE1) != 0)
1513 return w.dst - dst00;
1517 if ((w.err & JITC_ERR_PHASE0ONLY) != 0) {
1518 if ((flags & JITC_PHASE1) == 0)
1519 w.err &= ~JITC_ERR_PHASE0ONLY;
1521 if (w.err == (JITC_ERR_MASK & JITC_ERR_REGNUM)) errmsg = "reg-number error";
1522 if (w.err == (JITC_ERR_MASK & JITC_ERR_DST1)) errmsg = "dst1 error";
1523 if (w.err == (JITC_ERR_MASK & JITC_ERR_OPECODE)) errmsg = "opecode error";
1524 if (w.err == (JITC_ERR_MASK & JITC_ERR_LABELNUM)) errmsg = "label number too large";
1525 if (w.err == (JITC_ERR_MASK & JITC_ERR_LABELREDEF)) errmsg = "label redefine";
1526 if (w.err == (JITC_ERR_MASK & JITC_ERR_PREFIX)) { errmsg = "prefix redefine"; w.dst -= 2; }
1527 if (w.err == (JITC_ERR_MASK & JITC_ERR_LABELNODEF)) errmsg = "label not defined";
1528 if (w.err == (JITC_ERR_MASK & JITC_ERR_LABELTYP)) errmsg = "label type error";
1529 if (w.err == (JITC_ERR_MASK & JITC_ERR_IDIOM)) errmsg = "idiom error";
1530 if (w.err == (JITC_ERR_MASK & JITC_ERR_PREGNUM)) errmsg = "preg-number error";
1531 if (w.err == (JITC_ERR_MASK & JITC_ERR_SRC1)) errmsg = "src1 error";
1532 if (w.err == (JITC_ERR_MASK & JITC_ERR_BADTYPE)) errmsg = "bad type code";
1533 if (w.err == (JITC_ERR_MASK & JITC_ERR_PREFIXFAR)) errmsg = "prefix internal error";
1534 if (w.err == (JITC_ERR_MASK & JITC_ERR_INTERNAL)) errmsg = "general internal error";
1535 if (*errmsg != '\0') {
1536 fprintf(stderr, "JITC: %s at %06X (debugInfo0=%d)\n ", errmsg, src - src0, debugInfo0);
1537 for (i = 0; i < 16; i++)
1538 fprintf(stderr, "%02X ", src[i]);
1539 static char *table[0x30] = {
1540 "NOP", "LB", "LIMM", "PLIMM", "CND", "??", "??", "??",
1541 "LMEM", "SMEM", "PLMEM", "PSMEM", "LEA", "??", "PADD", "PDIF",
1542 "CP/OR", "XOR", "AND", "??", "ADD", "SUB", "MUL", "??",
1543 "SHL", "SAR", "DIV", "MOD", "PLMT0", "PLMT1", "PCP", "PCST",
1544 "CMPE", "CMPNE", "CMPL", "CMPGE", "CMPLE", "CMPG", "TSTZ", "TSTNZ",
1545 "PCMPE", "PCMPNE", "PCMPL", "PCMPGE", "PCMPLE", "PCMPG", "??", "EXT" };
1547 if (*src < 0x30) errmsg = table[*src];
1548 fprintf(stderr, "(%s)\n", errmsg);
1553 unsigned char *jitCompCallFunc(unsigned char *dst, void *func)
1555 struct JitCompWork w;
1557 jitCompA000_storeRegCacheAll(&w);
1558 jitCompA000_storePRegCacheAll(&w);
1559 jitCompPutByte1(w.dst, 0x60); /* PUSHAD(); */
1560 jitCompPutByte1(w.dst, 0x50); /* PUSH(EAX); */ /* for 16byte-align(win32では不要なのだけど、MacOSには必要らしい) */
1561 jitCompPutByte1(w.dst, 0x55); /* PUSH(EBP); */
1562 jitCompPutByte1(w.dst, 0xe8); /* CALL(func); */
1563 int j = ((unsigned char *)func) - (w.dst + 4);
1565 //この関数の中では結局w->dstしか参照していない
1566 jitCompPutImm32(&w, j);
1568 jitCompPutByte1(w.dst, 0x58); /* POP(EAX); */ /* (win32では不要なのだけど、MacOSには必要らしい) */
1569 jitCompPutByte1(w.dst, 0x58); /* POP(EAX); */
1570 jitCompPutByte1(w.dst, 0x61); /* POPAD(); */
1571 jitCompA000_loadRegCacheAll(&w);
1572 jitCompA000_loadPRegCacheAll(&w);
1573 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + 0x30 * 32 + 0); /* MOV(EAX, [EBP+?]); */
1574 jitCompPutByte2(w.dst, 0xff, 0xe0); /* JMP(EAX); */
1578 unsigned char *jitCompInit(unsigned char *dst)
1581 return jitCompCallFunc(dst, &errHndl);
1584 void func3c(char *ebp, int opt, int r1, int p1, int lenR, int lenP, int r0, int p0)
1586 HOSECPU_RuntimeEnvironment *r = (HOSECPU_RuntimeEnvironment *) (ebp - jitCompA0001_EBP128);
1588 HOSECPU_PointerRegisterEntry *pp;
1589 if (r->junkStack + 2048 > r->junkStack1) (*(r->errHndl))(r);
1590 pi = (void *)r->junkStack; r->junkStack += r1 * 4;
1591 for (i = 0; i < r1; i++)
1593 pp = (void *)r->junkStack; r->junkStack += p1 * 32;
1594 for (i = 0; i < p1; i++)
1596 pp = (void *)r->junkStack; r->junkStack += 32;
1597 *pp = r->preg[0x30];
1598 pi = (void *)r->junkStack; r->junkStack += 4;
1599 *pi = opt << 16 | r1 << 8 | p1;
1600 for (i = 0; i < lenR; i++)
1601 r->ireg[r0 + i] = r->ireg[0x30 + i];
1602 for (i = 0; i < lenP; i++)
1603 r->preg[p0 + i] = r->preg[0x31 + i];
1607 void func3d(char *ebp, int opt, int r1, int p1, int lenR, int lenP, int r0, int p0)
1609 HOSECPU_RuntimeEnvironment *r = (HOSECPU_RuntimeEnvironment *) (ebp - jitCompA0001_EBP128);
1613 HOSECPU_PointerRegisterEntry *pp = (void *)r->junkStack;
1614 r->preg[0x30] = *pp;
1615 r->junkStack -= p1 * 32; pp = (void *)r->junkStack;
1616 for (i = 0; i < p1; i++)
1618 r->junkStack -= r1 * 4; int *pi = (void *)r->junkStack;
1619 for (i = 0; i < r1; i++)
1624 void funcf4(char *ebp, int pxx, int typ, int len)
1626 HOSECPU_RuntimeEnvironment *r = (HOSECPU_RuntimeEnvironment *) (ebp - jitCompA0001_EBP128);
1627 int width = jitCompA000_dataWidth(jitCompA000_convTyp(r->ireg[typ])) >> 3;
1628 if (width < 0 || r->ireg[len] < 0)
1630 void *p = r->junkStack;
1631 if (r->junkStack + width * r->ireg[len] + 256 > r->junkStack1) (*(r->errHndl))(r);
1632 r->junkStack += width * r->ireg[len];
1634 r->preg[pxx].typ = r->ireg[typ];
1635 r->preg[pxx].p0 = p;
1636 r->preg[pxx].p1 = (void *)r->junkStack;
1637 int *pi = (int *)r->junkStack;
1638 *pi = width * r->ireg[len];
1639 r->junkStack += sizeof (int);
1640 if (r->ireg[typ] == 1) {
1641 int i, i1 = (width * r->ireg[len]) >> 2;
1643 for (i = 0; i < i1; i++)
1649 void funcf5(char *ebp, int pxx, int typ, int len)
1651 HOSECPU_RuntimeEnvironment *r = (HOSECPU_RuntimeEnvironment *) (ebp - jitCompA0001_EBP128);
1652 r->junkStack -= sizeof (int);
1653 int *pi = (int *)r->junkStack;
1654 r->junkStack -= *pi;
1656 int width = jitCompA000_dataWidth(r->ireg[typ]);
1657 void *p = r->junkStack;
1658 r->junkStack -= width * r->ireg[len];
1663 void funcf6(char *ebp, int pxx, int typ, int len)
1665 HOSECPU_RuntimeEnvironment *r = (HOSECPU_RuntimeEnvironment *) (ebp - jitCompA0001_EBP128);
1666 int width = jitCompA000_dataWidth(jitCompA000_convTyp(r->ireg[typ])) >> 3;
1667 if (width < 0 || r->ireg[len] < 0)
1669 void *p = malloc(width * r->ireg[len]);
1671 r->preg[pxx].typ = r->ireg[typ];
1672 r->preg[pxx].p0 = p;
1673 r->preg[pxx].p1 = (unsigned char *)p + width * r->ireg[len];
1674 if (r->ireg[typ] == 1) {
1675 int i, i1 = (width * r->ireg[len]) >> 2, *pi;
1677 for (i = 0; i < i1; i++)
1679 for (i = 1; i < i1; i += 8)
1685 void funcf7(char *ebp, int pxx, int typ, int len)
1687 HOSECPU_RuntimeEnvironment *r = (HOSECPU_RuntimeEnvironment *) (ebp - jitCompA0001_EBP128);
1688 free(r->preg[pxx].p);
1692 void errHndl(HOSECPU_RuntimeEnvironment *r)
1694 r = (HOSECPU_RuntimeEnvironment *) (((char *)r) - jitCompA0001_EBP128);
1700 * jitcの出力コードをひとまとめにする関数を作成しその中身をjitCompile()で生成
1702 * qq : 出力バイナリの書き込み位置のアドレスへの参照(書き込み位置を呼び出しに反映させるため参照渡しにする)
1703 * q1 : 出力バイナリの書き込み位置のアドレスの最大値
1704 * p0 : (*.ose)バイナリの読み込み位置のアドレス(ヘッダ部除去済)
1705 * p1 : (*.ose)バイナリの読み込み位置の取りうる最大値
1706 * (ただし、「確保したメモリ」の最大値なのでこれより手前にゴミデータが入っているかもしれない)
1708 * ret=2 : jitコンパイルエラー
1710 int jitc0(unsigned char **qq, unsigned char *q1, const unsigned char *p0, const unsigned char *p1, int level, HOSECPU_LabelListTag *label)
1712 unsigned char *q = *qq;
1713 if (p0[0] != 0x05 || p0[1] != SIGN1) // OSECPUのヘッダ (05E1) を確認
1716 *q++ = 0x55; /* PUSH(EBP); */
1717 *q++ = 0x8b; *q++ = 0x6c; *q++ = 0x24; *q++ = 0x08; /* MOV(EBP,[ESP+8]); */
1720 for (i = 0; i < JITC_MAXLABELS; i++)
1723 // 以下のjitCompile()呼び出しでは第二引数をq1-2にした方がよいのではないか?
1724 i = jitCompiler(q, q1, p0 + 2, p1, p0, label, JITC_MAXLABELS, level, di1_serial, 0);
1725 if (i != 0) return 2;
1726 i = jitCompiler(q, q1, p0 + 2, p1, p0, label, JITC_MAXLABELS, level, di1_serial, JITC_PHASE1 + 0);
1727 if (i < 0) return 2;
1730 *q++ = 0x5d; /* POP(EBP); */
1731 *q++ = 0xc3; /* RET(); */
1737 #if (USE_DEBUGGER != 0)
1739 int dbgrGetRegNum(const char *p)
1745 if (i > 9) i -= 'A' - '0' - 10;
1746 if (j > 9) j -= 'A' - '0' - 10;
1747 if (0 <= i && i <= 15 && 0 <= j && j <= 15)
1753 void dbgrMain(HOSECPU_RuntimeEnvironment *r)
1755 if (r->dbgr == 0) return;
1760 p = fgets(cmd, 64, stdin);
1761 if (p == NULL) break;
1762 if (cmd[0] == '\0') continue;
1763 if (cmd[0] == 'q' && cmd[1] <= ' ') break;
1764 if (cmd[0] == 'p' && cmd[1] <= ' ' && cmd[1] != '\0') {
1766 while (*p <= ' ' && *p != '\0') p++;
1768 i = dbgrGetRegNum(p + 1);
1769 if (0 <= i && i <= 0x3f) {
1770 printf("R%02X = 0x%08X = %d\n", i, r->ireg[i], r->ireg[i]);
1773 puts("register name error");
1777 i = dbgrGetRegNum(p + 1);
1778 if (0 <= i && i <= 0x3f) {
1780 if (0 <= r->preg[i].typ && r->preg[i].typ <= 0x15) {
1781 static char *typName[] = {
1782 "T_CODE", "T_VPTR", "T_SINT8", "T_UINT8",
1783 "T_SINT16", "T_UINT16", "T_SINT32", "T_UINT32",
1784 "T_SINT4", "T_UINT4", "T_SINT2", "T_UINT2",
1785 "T_SINT1", "T_UINT1", "T_SINT12", "T_UINT12",
1786 "T_SINT20", "T_UINT20", "T_SINT24", "T_UINT24",
1787 "T_SINT28", "T_UINT28"
1789 p = typName[r->preg[i].typ];
1791 printf("P%02X:\n type = %s(%04X), (origin-ptr) = 0x%08X\n", i, p, r->preg[i].typ, (unsigned int)(r->preg[i].p0));
1792 if (r->preg[i].p != NULL && r->preg[i].p0 != NULL) {
1793 j = jitCompA000_dataWidth(jitCompA000_convTyp(r->preg[i].typ)) >> 3;
1795 k = (r->preg[i].p1 - r->preg[i].p0) / j;
1796 printf(" size = 0x%08X = %d\n", k, k);
1797 k = (r->preg[i].p - r->preg[i].p0) / j;
1798 printf(" pos = 0x%08X = %d\n", k, k);
1801 puts(" null pointer");
1805 puts("register name error");
1809 puts("command error");