4 #if (JITC_ARCNUM == 0x0001)
9 void jitCompPutImm32(struct JitCompWork *w, int i)
11 jitCompPutByte1(w->dst, i & 0xff);
12 jitCompPutByte1(w->dst, (i >> 8) & 0xff);
13 jitCompPutByte1(w->dst, (i >> 16) & 0xff);
14 jitCompPutByte1(w->dst, (i >> 24) & 0xff);
18 int jitCompGetImm32(const unsigned char *src)
20 return (src[0] << 24) | (src[1] << 16) | (src[2] << 8) | src[3];
23 int jitCompGetLabelNum(struct JitCompWork *w, const unsigned char *src)
25 int i = jitCompGetImm32(src);
26 if (i < 0 || i >= w->maxLabels) {
27 w->err = JITC_ERR_LABELNUM;
33 void jitCompA0001_85DispN(struct JitCompWork *w, int disp, int n)
35 disp -= jitCompA0001_EBP128;
36 if (-128 <= disp && disp <= 127) {
37 jitCompPutByte2(w->dst, 0x45 | (n << 3), disp & 0xff);
39 jitCompPutByte1(w->dst, 0x85 | (n << 3));
40 jitCompPutImm32(w, disp);
45 void jitCompA0001_movEbpDispReg32(struct JitCompWork *w, int disp, int reg32)
47 jitCompPutByte1(w->dst, 0x89); /* MOV(mem, reg32); */
48 jitCompA0001_85DispN(w, disp, reg32);
52 void jitCompA0001_movReg32EbpDisp(struct JitCompWork *w, int reg32, int disp)
54 jitCompPutByte1(w->dst, 0x8b); /* MOV(reg32, mem); */
55 jitCompA0001_85DispN(w, disp, reg32);
59 void jitCompA0001_movEaxRxx(struct JitCompWork *w, int rxx)
61 #if (jitCompA0001_USE_R3F_IMM32 != 0)
63 jitCompPutByte1(w->dst, 0xb8); /* MOV(EAX, ?); */
64 jitCompPutImm32(w, w->r3f);
68 if (rxx >= 0x40 || rxx < 0){
69 w->err = JITC_ERR_REGNUM;
71 jitCompA0001_movReg32EbpDisp(w, 0 /* EAX */, rxx * 4); /* MOV(EAX, [EBP+?]); */
75 void jitCompA0001_movRxxEax(struct JitCompWork *w, int rxx)
77 if (rxx >= 0x40 || rxx < 0){
78 w->err = JITC_ERR_REGNUM;
80 jitCompA0001_movEbpDispReg32(w, rxx * 4, 0 /* EAX */); /* MOV([EBP+?], EAX); */
84 void jitCompA0001_fixPrefix(struct JitCompWork *w)
87 if (w->dst - w->dst0 > 127){
88 w->err = JITC_ERR_REGNUM;
90 w->dst0[-1] = (unsigned char)((w->dst - w->dst0) & 0xff);
95 void jitCompA0001_checkCompPtr(struct JitCompWork *w, int p0, int p1)
97 if (p0 >= 0x3f || p0 < 0){
98 w->err = JITC_ERR_PREGNUM;
100 if (p1 >= 0x3f || p1 < 0){
101 w->err = JITC_ERR_PREGNUM;
103 /* 比較可能可能なのかのチェックのコードを出力 */ /* 未完成 */
107 void jitCompA000_loadRegCacheAll(struct JitCompWork *w)
109 jitCompA0001_movReg32EbpDisp(w, 3 /* EBX */, 0 * 4); /* EBX = R00; */
110 jitCompA0001_movReg32EbpDisp(w, 1 /* ECX */, 1 * 4); /* ECX = R01; */
111 jitCompA0001_movReg32EbpDisp(w, 2 /* EDX */, 2 * 4); /* EDX = R02; */
115 void jitCompA000_storeRegCacheAll(struct JitCompWork *w)
117 jitCompA0001_movEbpDispReg32(w, 0 * 4, 3 /* EBX */); /* R00 = EBX; */
118 jitCompA0001_movEbpDispReg32(w, 1 * 4, 1 /* ECX */); /* R01 = ECX; */
119 jitCompA0001_movEbpDispReg32(w, 2 * 4, 2 /* EDX */); /* R02 = EDX; */
123 void jitCompA000_loadRegCacheEcx(struct JitCompWork *w)
125 jitCompA0001_movReg32EbpDisp(w, 1 /* ECX */, 1 * 4); /* ECX = R01; */
129 void jitCompA000_storeRegCacheEcx(struct JitCompWork *w)
131 jitCompA0001_movEbpDispReg32(w, 1 * 4, 1 /* ECX */); /* R01 = ECX; */
135 void jitCompA000_loadRegCacheEdx(struct JitCompWork *w)
137 jitCompA0001_movReg32EbpDisp(w, 2 /* EDX */, 2 * 4); /* EDX = R02; */
141 void jitCompA000_storeRegCacheEdx(struct JitCompWork *w)
143 jitCompA0001_movEbpDispReg32(w, 2 * 4, 2 /* EDX */); /* R02 = EDX; */
147 int jitCompA000_selectRegCache(int rxx, int reg)
166 void jitCompA000_loadPRegCacheAll(struct JitCompWork *w)
168 // jitCompA0001_movReg32EbpDisp(w, 5 /* EBP */, 256 + 0 * 32 + 0); /* EBP = P00; */
169 jitCompA0001_movReg32EbpDisp(w, 6 /* ESI */, 256 + 1 * 32 + 0); /* ESI = P01; */
170 jitCompA0001_movReg32EbpDisp(w, 7 /* EDI */, 256 + 2 * 32 + 0); /* EDI = P02; */
174 void jitCompA000_storePRegCacheAll(struct JitCompWork *w)
176 // jitCompA0001_movEbpDispReg32(w, 256 + 0 * 32 + 0, 5 /* EBP */); /* P00 = EBP; */
177 jitCompA0001_movEbpDispReg32(w, 256 + 1 * 32 + 0, 6 /* ESI */); /* P01 = ESI; */
178 jitCompA0001_movEbpDispReg32(w, 256 + 2 * 32 + 0, 7 /* EDI */); /* P02 = EDI; */
182 int jitCompA000_selectPRegCache(int pxx, int reg)
184 // if (pxx == 0) reg = 5; /* EBP */
199 int jitCompA000_convTyp(int t)
203 if (1 <= t && t <= 7){
205 } else if (8 <= t && t <= 13){
207 } else if (14 <= t && t <= 15){
209 } else if (16 <= t && t <= 21){
215 int jitCompA000_dataWidth(int t)
218 if (t == 0x0001) r = 256;
220 if (t == 0x0002 / 2) r = 8;
221 if (t == 0x0004 / 2) r = 16;
222 if (t == 0x0006 / 2) r = 32;
223 if (t == 0x0008 / 2) r = 4;
224 if (t == 0x000a / 2) r = 2;
225 if (t == 0x000c / 2) r = 1;
226 if (t == 0x000e / 2) r = 12;
227 if (t == 0x0010 / 2) r = 20;
228 if (t == 0x0012 / 2) r = 24;
229 if (t == 0x0014 / 2) r = 28;
233 static unsigned char *errfnc;
235 void jitCompA0001_checkType0(struct JitCompWork *w, int pxx, int typ, int ac)
237 if (typ <= 0) { w->err = JITC_ERR_BADTYPE; }
238 if (typ > 0x7f) { w->err = JITC_ERR_INTERNAL; }
239 jitCompA0001_movReg32EbpDisp(w, 0 /* EAX */, 256 + pxx * 32 + 4); /* MOV(EAX, [EBP+?]); */ /* typ */
240 jitCompPutByte3(w->dst, 0x83, 0xf8, typ & 0x7f); /* CMP(EAX, ?); */
241 jitCompPutByte2(w->dst, 0x0f, 0x85); /* JNE */
242 jitCompPutImm32(w, errfnc - (w->dst + 4));
246 void jitCompA0001_checkType(struct JitCompWork *w, int pxx, int typ, int ac)
248 // 将来的にはaliveやアクセス権チェックも入れる
250 jitCompA0001_checkType0(w, pxx, typ, ac);
254 void jitCompA0001_checkLimit(struct JitCompWork *w, int reg, int pxx)
256 jitCompPutByte1(w->dst, 0x3b); /* CMP(reg, [EBP+?]); */
257 jitCompA0001_85DispN(w, 256 + pxx * 32 + 8, reg); /* p0 */
258 jitCompPutByte2(w->dst, 0x0f, 0x82); /* JB */
259 jitCompPutImm32(w, errfnc - (w->dst + 4));
260 jitCompPutByte1(w->dst, 0x3b); /* CMP(reg, [EBP+?]); */
261 jitCompA0001_85DispN(w, 256 + pxx * 32 + 12, reg); /* p1 */
262 jitCompPutByte2(w->dst, 0x0f, 0x83); /* JAE */
263 jitCompPutImm32(w, errfnc - (w->dst + 4));
267 // F5の場合、decoderが対応するalloc-freeを結びつけるのが簡単で、typやlenを指定必須にしてもフロントエンドコードに影響はない.
268 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)
270 // For IA-32 (x86, 32-bit)
271 // 本来ならこのレイヤでは文法チェックしない
273 // dst : 現在の書き込みアドレス。
274 // dst1 : 書き込みアドレスの最大値
275 // src : 現在の読み込みアドレス(ヘッダ部は飛ばしてある
276 // src1 : 読み込みアドレスの最大値
277 // src0 : 読み込みバイナリのアドレス
278 struct JitCompWork w;
279 unsigned char *dst00 = dst, *enter0 = NULL, *tmp_ucp;
281 const unsigned char *oldsrc;
282 int timecount = 0, i, j = 0, lastlabel = -1, debugInfo0 = -1;
283 int reg0, reg1, reg2, cmp0reg = -1, cmp0lev = 0;
284 w.dst = w.dst0 = dst;
286 w.maxLabels = maxLabels;
288 if ((flags & JITC_NOSTARTUP) == 0) {
289 jitCompPutByte1(w.dst, 0x60); /* PUSHAD(); */
290 jitCompA000_loadRegCacheAll(&w); /* start-up */
291 jitCompA000_loadPRegCacheAll(&w);
293 if (level <= JITC_LV_SLOWER) {
294 jitCompPutByte2(w.dst, 0x31, 0xc0); /* XOR(EAX, EAX); */
295 jitCompA0001_movEbpDispReg32(&w, 2304 + 0, 0 /* EAX */); /* MOV(debugInfo0, EAX); */
296 jitCompPutByte1(w.dst, 0xb8); /* MOV(EAX, ?); */
297 jitCompPutImm32(&w, debugInfo1);
298 jitCompA0001_movEbpDispReg32(&w, 2304 + 4, 0 /* EAX */); /* MOV(debugInfo1, EAX); */
301 w.prefix = 0; //0x04 CND 命令で変更される
302 if (w.dst + 256 > dst1) { w.err = JITC_ERR_DST1; goto err_w; } // 書き込み領域が残り256バイト未満ならエラー
304 if (timecount >= 64) {
306 /* 未完成(timeoutチェックコードを入れる) */
308 prefix_continue: // CND命令実行後ここに戻る
312 if (w.prefix != 0) { w.err = JITC_ERR_PREFIX; goto err_w; } // 「条件付きでNOPを実行」するなんて、矛盾している!
318 * LB : ラベル設置命令。(6byte)
320 * ・timecount++し、timecountのチェックをする。
322 * ・割り込みがある場合、このタイミングで割り込みを発生させる。
329 if (enter0 == NULL && (src[6] == 0x3c /* 多数のレジスタをスタックに退避 */ || (src[6] == 0xfe/* REMARK */ && src[7] == 0x01 && src[9] == 0x3c))) { //beginFunc()中のLB
330 // LB命令の後に0x3C命令・・・beginFunc()
331 jitCompPutByte1(w.dst, 0xe9); // (x86) JMP rel32 : 次の命令との相対オフセットだけ相対ニアジャンプする
333 jitCompPutImm32(&w, 0); // 飛び相対座標が0 ・・・パイプラインのフラッシュ??
335 if (src[6] == 0x34) { // LBの次の命令がDATA ・・・DAT_SA0(label, typ32, length) ・・・メモリ確保命令
337 jitCompPutByte1(w.dst, 0xe9); // (x86) JMP rel32 : 次の命令との相対オフセットだけ相対ニアジャンプする
338 i = jitCompGetImm32(&src[7]); // type32 を取得
341 i = jitCompA000_convTyp(i);
343 if (i == 2 || i == 3) { j = 1; }
344 if (i == 4 || i == 5) { j = 2; }
345 if (i == 6 || i == 7) { j = 4; }
347 j *= jitCompGetImm32(&src[11]);
348 if (j <= 0) w.err = JITC_ERR_BADTYPE;
349 jitCompPutImm32(&w, j);
350 #if (jitCompA0001_OPTIMIZE_JMP != 0)
351 if (j <= 127 - jitCompA0001_OPTIMIZE_ALIGN) {
353 jitCompPutByte2(w.dst, 0xeb, j);
357 #if (jitCompA0001_OPTIMIZE_ALIGN != 0)
359 i = ((int)w.dst) & (jitCompA0001_OPTIMIZE_ALIGN - 1); /* ALIGNで割ったあまりを計算 */
361 i = jitCompA0001_OPTIMIZE_ALIGN - i;
362 if (i == 1) { jitCompPutByte1(w.dst, 0x90); j += i; } /* NOP(); */
363 if (i == 2) { jitCompPutByte2(w.dst, 0x89, 0xc0); j += i; } /* MOV(EAX, EAX); */
364 if (i == 3) { jitCompPutByte3(w.dst, 0x8d, 0x76, 0x00); j += i; } /* LEA(ESI, [ESI+0]); */
365 if (i == 4) { jitCompPutByte4(w.dst, 0x8d, 0x74, 0x26, 0x00); j += i; } /* LEA(ESI, [ESI*1+0]); */
366 if (i == 5) { jitCompPutByte1(w.dst, 0x0d); jitCompPutImm32(&w, 0); j += i; } /* OR(EAX, 0); */
367 if (i == 6) { jitCompPutByte2(w.dst, 0x8d, 0xb6); jitCompPutImm32(&w, 0); j += i; } /* LEA(ESI, [ESI+0]); */
368 if (i >= 7) { jitCompPutByte3(w.dst, 0x8d, 0xb4, 0x26); jitCompPutImm32(&w, 0); j += 7; } /* LEA(ESI, [ESI*1+0]); */
371 if (src[6] == 0x34) {
372 tmp_ucp[1] = j & 0xff;
373 if (*tmp_ucp == 0xe9) {
374 tmp_ucp[2] = (j >> 8) & 0xff;
375 tmp_ucp[3] = (j >> 16) & 0xff;
376 tmp_ucp[4] = (j >> 24) & 0xff;
379 if ((flags & JITC_PHASE1) == 0) {
380 i = jitCompGetLabelNum(&w, src + 2);
381 //printf("i=%06X %06X\n", i, src-src0);
382 if (label[i].opt != 0 && w.err == 0) { w.err = JITC_ERR_LABELREDEF; goto err_w; }
383 if (w.prefix != 0) { w.err = JITC_ERR_PREFIX; goto err_w; }
384 label[i].opt = src[1] + 1;
385 label[i].typ = 0; /* TYP_CODE */
387 label[i].p1 = w.dst + 1;
392 /* 未完成(timeoutチェックコードを入れる) */
395 case 0x02: /* LIMM */
398 * LIMM : 定数即値代入命令(6byte)
403 * ・reg3F は条件比較慣用句指定用&演算命令即値慣用句指定用。よってCND命令の直後では使用できない。
406 if (src[1] == 0x3f && w.prefix != 0) w.err = JITC_ERR_PREFIX; // CND命令の直後でR3Fを書き換えるなんて変だよね
408 #if (jitCompA0001_USE_R3F_IMM32 != 0)
409 if (src[1] == 0x3f) { // R3Fへの代入は例外敵に、 w.r3f を使用
410 w.r3f = jitCompGetImm32(src + 2);
414 i = jitCompGetImm32(src + 2); // 与えられた即値(第二引数)を取得
416 /* R00-R02 なら EBX, ECX, EDX 、それ以外なら EAX のレジスタIDを reg0 に代入 */
417 reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
419 #if (jitCompA0001_OPTIMIZE_MOV != 0)
421 jitCompPutByte2(w.dst, 0x31, 0xc0 | reg0 << 3 | reg0); /* XOR(reg0, reg0); */
422 jitCompA0001_movRxxEax(&w, src[1]);
427 /* reg0 のレジスタに対応したMOV命令を発行 */
428 jitCompPutByte1(w.dst, 0xb8 | reg0); /* MOV(reg0, ?); == 10111000b+wr imm32 */
429 jitCompPutImm32(&w, i);
431 if (reg0 == 0) // R03以降の、レジスタの内容をメモリ上に格納してエミュレートする場合
433 jitCompA0001_movRxxEax(&w, src[1]);
436 case 0x03: /* PLIMM */ /* 未完成(plsまで対応) */
439 * PLIMM : ラベル番号代入命令(6byte)
449 i = jitCompGetLabelNum(&w, src + 2); // Pxxに代入するラベルの番号(第二引数)
450 if ((flags & JITC_PHASE1) != 0 && w.err == 0) { // Phase 1であるならば
451 if (label[i].opt == 0) { w.err = JITC_ERR_LABELNODEF; goto err_w; } // 指定されたラベル番号は存在しない
452 if (src[1] != 0x3f && label[i].opt != 2) { w.err = JITC_ERR_LABELTYP; goto err_w; } //
453 if (src[1] == 0x3f && label[i].typ != 0) { w.err = JITC_ERR_LABELTYP; goto err_w; } // プログラムカウンタに TYP_CODEでない値は代入できない
455 if (src[1] == 0x3f) { // プログラムカウンタへの代入なら
456 if (w.prefix == 0) { // CND命令による条件付きでなければ、即座に移動
457 jitCompPutByte1(w.dst, 0xe9); /* JMP(?); */
466 * いま、dstの末端はJZ命令になっている。 0x0F 0x84 cd
470 w.dst[-1] = w.dst[-2] ^ 0xf1; /* 74->85, 75->84 */
476 if ((flags & JITC_PHASE1) != 0 || (((flags & JITC_PHASE1) == 0) && label[i].opt != 0)) // label番号iが確保されていれば (このif文は意味をなさない)
477 j = label[i].p - (w.dst + 4); // j はとび先の相対番地
478 jitCompPutImm32(&w, j); // JMP もしくは JZ 命令のアドレス部を記述
479 #if (jitCompA0001_OPTIMIZE_JMP != 0)
480 if (-128 - 3 <= j && j < 0) {
481 if (w.dst[-5] == 0xe9) {
484 jitCompPutByte1(w.dst, 0xeb); /* JMP(?); */
489 jitCompPutByte1(w.dst, w.dst[1] ^ 0xf0);
491 jitCompPutByte1(w.dst, j & 0xff);
495 else { // プログラムカウンタ以外への代入
497 // 代入先が P01, P02なら ESI, EDI,それ以外ならEAXを指定
498 reg0 = jitCompA000_selectPRegCache(src[1], 0 /* EAX */);
499 jitCompPutByte1(w.dst, 0xb8 | reg0); /* MOV(reg0, ?); */
500 jitCompPutImm32(&w, (int)label[i].p); // ラベルのパスを各レジスタに代入
502 // レジスタへの代入をメモリでエミュレーションする場合は、スタックに積む。
504 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32, 0); /* MOV([EBP+?], EAX); */
506 if (level < JITC_LV_FASTEST) {
507 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 8, reg0); /* MOV([EBP+?], reg0); */ /* p0 */
508 jitCompPutByte1(w.dst, 0xb8); /* MOV(EAX, ?); */
509 jitCompPutImm32(&w, label[i].typ);
510 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 4, 0); /* MOV([EBP+?], EAX); */ /* typ */
511 jitCompPutByte1(w.dst, 0xb8); /* MOV(EAX, ?); */
512 jitCompPutImm32(&w, (int)label[i].p1);
513 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 12, 0); /* MOV([EBP+?], EAX); */ /* p1 */
514 jitCompPutByte2(w.dst, 0x31, 0xc0); /* XOR(EAX, EAX); */
515 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 16, 0); /* MOV([EBP+?], EAX); */ /* liveSign */
516 jitCompA0001_movReg32EbpDisp(&w, 0, 2320); /* MOV(EAX, ptrCtrl); */
517 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 20, 0); /* MOV([EBP+?], EAX); */ /* pls */
522 case 0x04: /* CND (prefix) */
526 * 与えられたRxxの最下位ビットが1であれば後続の命令を実行、そうでなければ飛ばす。
529 if (src[1] >= 0x40) w.err = JITC_ERR_REGNUM; // R00-R3F 以外のレジスタは比較対象にできない
531 // 比較対象のレジスタがメモリ上にあれば-1, それ以外なら適切なレジスタ番号を返す
532 reg0 = jitCompA000_selectRegCache(src[1], -1 /* mem */);
535 if (reg0 < 0) { //比較対象のレジスタはメモリ上にある
536 jitCompPutByte1(w.dst, 0xf7); /* TEST([EBP+?],1); */
537 jitCompA0001_85DispN(&w, src[1] * 4, 0);
540 jitCompPutByte2(w.dst, 0xf7, 0xc0 | reg0); /* TEST(reg0,1); */
542 jitCompPutImm32(&w, 1);
545 jitCompPutByte2(w.dst, 0x74, 0x00); /* JZ($+2) */
547 if (w.err != 0) goto err_w;
549 w.prefix = 1; // プリフィックスをセット
551 goto prefix_continue;
553 case 0x08: /* LMEM */ /* 完成 */
554 i = jitCompGetImm32(src + 2);
555 if (i == 0x0001) w.err = JITC_ERR_BADTYPE;
556 if (level < JITC_LV_FASTER) {
557 jitCompA0001_checkType(&w, src[6], i, 0); // read
560 reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
561 reg1 = jitCompA000_selectPRegCache(src[6], 2 /* EDX */);
562 if (reg0 != 0 /* EAX */ && reg1 == 2 /* EDX */)
564 if (reg1 == 2 /* EDX */)
565 jitCompA000_storeRegCacheEdx(&w);
566 if (reg1 <= 3 /* EAX, EDX */)
567 jitCompA0001_movReg32EbpDisp(&w, reg1, 256 + src[6] * 32 + 0); /* MOV(reg1, [EBP+?]); */
568 if (level < JITC_LV_FASTER)
569 jitCompA0001_checkLimit(&w, reg1, src[6]);
570 i = jitCompA000_convTyp(jitCompGetImm32(src + 2));
573 jitCompPutByte3(w.dst, 0x0f, 0xbe, reg0 << 3 | reg1); /* MOVSX(reg0,BYTE [reg1]); */
576 jitCompPutByte3(w.dst, 0x0f, 0xb6, reg0 << 3 | reg1); /* MOVZX(reg0,BYTE [reg1]); */
579 jitCompPutByte3(w.dst, 0x0f, 0xbf, reg0 << 3 | reg1); /* MOVSX(reg0,WORD [reg1]); */
582 jitCompPutByte3(w.dst, 0x0f, 0xb7, reg0 << 3 | reg1); /* MOVZX(reg0,WORD [reg1]); */
586 jitCompPutByte2(w.dst, 0x8b, reg0 << 3 | reg1); /* MOV(reg0, [reg1]); */
589 w.err = JITC_ERR_BADTYPE;
591 if (reg0 == 0 /* EAX */)
592 jitCompA0001_movRxxEax(&w, src[1]);
593 if (reg1 == 2 /* EDX */)
594 jitCompA000_loadRegCacheEdx(&w);
597 case 0x09: /* SMEM */ /* 完成 */
598 i = jitCompGetImm32(src + 2);
599 if (i == 0x0001) w.err = JITC_ERR_BADTYPE;
600 if (level < JITC_LV_FASTER) {
601 jitCompA0001_checkType(&w, src[6], i, 1); // write
604 reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
605 reg1 = jitCompA000_selectPRegCache(src[6], 2 /* EDX */);
606 if (reg0 != 0 /* EAX */ && reg1 == 2 /* EDX */)
608 if (reg1 == 2 /* EDX */)
609 jitCompA000_storeRegCacheEdx(&w);
610 if (reg1 <= 3 /* EAX, EDX */)
611 jitCompA0001_movReg32EbpDisp(&w, reg1, 256 + src[6] * 32 + 0); /* MOV(reg1, [EBP+?]); */
612 if (level < JITC_LV_FASTER)
613 jitCompA0001_checkLimit(&w, reg1, src[6]);
614 if (reg0 == 0 /* EAX */)
615 jitCompA0001_movEaxRxx(&w, src[1]);
617 i = jitCompA000_convTyp(jitCompGetImm32(src + 2));
621 jitCompPutByte2(w.dst, 0x88, reg0 << 3 | reg1); /* MOV([reg1], BYTE(reg0)); */
625 jitCompPutByte3(w.dst, 0x66, 0x89, reg0 << 3 | reg1); /* MOV([reg1], WORD(reg0)); */
629 jitCompPutByte2(w.dst, 0x89, reg0 << 3 | reg1); /* MOV([reg1], reg0); */
632 w.err = JITC_ERR_BADTYPE;
634 if (reg1 == 2 /* EDX */)
635 jitCompA000_loadRegCacheEdx(&w);
638 case 0x0a: /* PLMEM */ /* 完成 */
639 i = jitCompGetImm32(src + 2);
640 if (i != 0x0001) w.err = JITC_ERR_BADTYPE;
641 if (level < JITC_LV_FASTER) {
642 jitCompA0001_checkType(&w, src[6], i, 0); // read
645 reg0 = jitCompA000_selectPRegCache(src[1], 0 /* EAX */);
646 reg1 = jitCompA000_selectPRegCache(src[6], 2 /* EDX */);
647 // if (reg0 != 0 /* EAX */ && reg1 == 2 /* EDX */) /* これをやってはいけない!(by K, 2013.08.02) */
648 // reg1 = 0; /* EAX */
649 if (reg0 == reg1 && reg0 != 0) { // bugfix: hinted by yao, 2013.09.14. thanks!
650 jitCompA000_storePRegCacheAll(&w);
653 if (reg1 == 2 /* EDX */)
654 jitCompA000_storeRegCacheEdx(&w);
655 if (reg1 <= 3 /* EAX, EDX */)
656 jitCompA0001_movReg32EbpDisp(&w, reg1, 256 + src[6] * 32 + 0); /* MOV(reg1, [EBP+?]); */
657 if (level < JITC_LV_FASTER)
658 jitCompA0001_checkLimit(&w, reg1, src[6]);
659 jitCompPutByte2(w.dst, 0x8b, reg0 << 3 | reg1); /* MOV(reg0, [reg1]); */
660 if (reg0 == 0 /* EAX */)
661 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 0, 0); /* MOV([EBP+?], EAX); */
662 for (i = 4; i < 32; i += 4) {
663 jitCompPutByte3(w.dst, 0x8b, 0x40 | reg1, i); /* MOV(EAX, [reg1+?]); */
664 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + i, 0 /* EAX */); /* MOV([EBP+?], EAX); */
666 if (reg1 == 2 /* EDX */)
667 jitCompA000_loadRegCacheEdx(&w);
670 case 0x0b: /* PSMEM */ /* 完成 */
671 i = jitCompGetImm32(src + 2);
672 if (i != 0x0001) w.err = JITC_ERR_BADTYPE;
673 if (level < JITC_LV_FASTER) {
674 jitCompA0001_checkType(&w, src[6], i, 1); // write
677 reg0 = jitCompA000_selectPRegCache(src[1], 0 /* EAX */);
678 reg1 = jitCompA000_selectPRegCache(src[6], 2 /* EDX */);
679 // if (reg0 != 0 /* EAX */ && reg1 == 2 /* EDX */) /* これをやってはいけない!(by K, 2013.08.02) */
680 // reg1 = 0; /* EAX */
681 if (reg1 == 2 /* EDX */)
682 jitCompA000_storeRegCacheEdx(&w);
683 if (reg1 <= 3 /* EAX, EDX */)
684 jitCompA0001_movReg32EbpDisp(&w, reg1, 256 + src[6] * 32 + 0); /* MOV(reg1, [EBP+?]); */
685 if (level < JITC_LV_FASTER)
686 jitCompA0001_checkLimit(&w, reg1, src[6]);
687 if (reg0 == 0 /* EAX */)
688 jitCompA0001_movReg32EbpDisp(&w, reg0, 256 + src[1] * 32 + 0); /* MOV(reg0, [EBP+?]); */
689 jitCompPutByte2(w.dst, 0x89, reg0 << 3 | reg1); /* MOV([reg1], reg0); */
690 for (i = 4; i < 32; i += 4) {
691 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[1] * 32 + i); /* MOV(EAX, [EBP+?]); */
692 jitCompPutByte3(w.dst, 0x89, 0x40 | reg1, i); /* MOV([reg1+?], EAX); */
694 if (reg1 == 2 /* EDX */)
695 jitCompA000_loadRegCacheEdx(&w);
698 case 0x0e: /* PADD */ /* 完成 */
699 if (level < JITC_LV_FASTER) {
700 jitCompA0001_checkType0(&w, src[6], jitCompGetImm32(src + 2), 2); // other, aliveテストはとりあえずしない.
703 reg0 = jitCompA000_selectPRegCache(src[1], 0 /* EAX */);
704 reg1 = jitCompA000_selectPRegCache(src[6], -1 /* mem */);
705 if (reg1 < 0 /* mem */)
706 jitCompA0001_movReg32EbpDisp(&w, reg0, 256 + src[6] * 32 + 0); /* MOV(reg0, [EBP+?]); */
707 if (reg1 >= 0 && reg0 != reg1) {
708 jitCompPutByte2(w.dst, 0x89, 0xc0 | reg1 << 3 | reg0); /* MOV(reg0, reg1); */
710 i = jitCompGetImm32(src + 2);
715 i = jitCompA000_convTyp(i);
716 if (0x0002 <= i && i <= 0x0007)
717 j = (i - 0x0002) >> 1;
719 if (j < 0) { w.err = JITC_ERR_BADTYPE; goto err_w; }
720 #if (jitCompA0001_USE_R3F_IMM32 != 0)
721 if (src[7] == 0x3f) {
723 #if (jitCompA0001_USE_R3F_IMM8 != 0)
724 if (-0x80 <= j && j <= 0x7f) {
725 #if (jitCompA0001_USE_R3F_INCDEC != 0)
726 if (j == 1) { jitCompPutByte1(w.dst, 0x40 | reg0); goto padd1; } /* INC */
727 if (j == -1) { jitCompPutByte1(w.dst, 0x48 | reg0); goto padd1; } /* DEC */
729 jitCompPutByte3(w.dst, 0x83, 0xc0 | reg0, j & 0xff); /* ADD(reg0, im8); */
734 jitCompPutByte1(w.dst, 0x05); /* ADD(reg0, ?); */
737 jitCompPutByte2(w.dst, 0x81, 0xc0 | reg0); /* ADD(reg0, ?); */
739 jitCompPutImm32(&w, j);
743 if (src[7] >= 0x40) w.err = JITC_ERR_REGNUM;
745 reg1 = jitCompA000_selectRegCache(src[7], -1 /* mem */);
747 jitCompPutByte2(w.dst, 0x01, 0xc0 | reg1 << 3 | reg0); /* ADD(reg0, reg1); */
750 jitCompPutByte1(w.dst, 0x03); /* ADD(reg0, [EBP+?]); */
751 jitCompA0001_85DispN(&w, src[7] * 4, reg0);
755 reg1 = jitCompA000_selectRegCache(src[7], -1 /* mem */);
757 jitCompA000_storeRegCacheEdx(&w);
759 jitCompA0001_movReg32EbpDisp(&w, reg2, src[7] * 4); /* MOV(reg2, [EBP+?]); */
760 if (reg1 >= 0 && reg1 != reg2) {
761 jitCompPutByte2(w.dst, 0x89, 0xc0 | reg1 << 3 | reg2); /* MOV(reg2, reg1); */
763 jitCompPutByte3(w.dst, 0xc1, 0xe0 | reg2, j); /* SHL(reg2, ?); */
764 jitCompPutByte2(w.dst, 0x01, 0xc0 | reg2 << 3 | reg0); /* ADD(reg0, reg2); */
765 jitCompA000_loadRegCacheEdx(&w);
767 #if (jitCompA0001_USE_R3F_IMM32 != 0)
770 if (reg0 == 0 /* EAX */)
771 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 0, reg0); /* MOV([EBP+?], reg0); */
772 if (src[1] != src[6]) {
773 for (i = 4; i < 32; i += 4) {
774 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[6] * 32 + i); /* MOV(EAX, [EBP+?]); */
775 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + i, 0 /* EAX */); /* MOV([EBP+?], EAX); */
781 case 0x0f: /* PDIF */ /* 未完成 */
782 reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
783 jitCompA000_storePRegCacheAll(&w); // 手抜き.
784 jitCompA0001_checkCompPtr(&w, src[6], src[7]);
785 jitCompA0001_movReg32EbpDisp(&w, reg0, 256 + src[6] * 32 + 0); /* MOV(reg0, [EBP+?]); */
786 jitCompPutByte1(w.dst, 0x2b); /* SUB(EAX, [EBP+?]); */
787 jitCompA0001_85DispN(&w, 256 + src[7] * 32 + 0, reg0);
788 i = jitCompA000_convTyp(jitCompGetImm32(src + 2));
790 if (0x0002 <= i && i <= 0x0007)
791 j = (i - 0x0002) >> 1;
792 if (j < 0) { w.err = JITC_ERR_BADTYPE; goto err_w; }
794 jitCompPutByte3(w.dst, 0xc1, 0xf8 | reg0, j); /* SAR(reg0,?); */
796 if (reg0 == 0 /* EAX */)
797 jitCompA0001_movRxxEax(&w, src[1]);
798 cmp0reg = src[1]; cmp0lev = 1;
807 if (src[1] >= 0x3f) w.err = JITC_ERR_REGNUM;
808 reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
809 reg1 = jitCompA000_selectRegCache(src[2], -1 /* mem */);
810 #if (jitCompA0001_USE_R3F_IMM32 != 0)
811 if (src[2] == 0x3f) { // SUBのみ該当.
812 if (*src != 0x15) w.err = JITC_ERR_REGNUM;
813 reg2 = jitCompA000_selectRegCache(src[3], -1 /* mem */);
815 jitCompA000_storeRegCacheAll(&w);
816 jitCompPutByte1(w.dst, 0xb8 | reg0); /* MOV(reg0, ?); */
817 jitCompPutImm32(&w, w.r3f);
818 jitCompPutByte1(w.dst, 0x2b);
819 jitCompA0001_85DispN(&w, src[3] * 4, reg0);
821 jitCompA0001_movRxxEax(&w, src[1]);
826 jitCompA0001_movReg32EbpDisp(&w, reg0, src[2] * 4); /* MOV(reg0, [EBP+?]); */
828 if (reg1 >= 0 && reg0 != reg1) {
829 jitCompPutByte2(w.dst, 0x89, 0xc0 | reg1 << 3 | reg0); /* MOV(reg0, reg1); */
831 if (!(src[0] == 0x10 && src[3] == 0xff)) { // bugfix: hinted by Iris, 2013.06.26. thanks!
839 if (!(src[0] == 0x10 && src[3] == 0xff)) {
840 #if (jitCompA0001_USE_R3F_IMM32 != 0)
841 if (src[3] == 0x3f) {
842 if (*src == 0x16 && w.r3f == -1) {
843 jitCompPutByte2(w.dst, 0xf7, 0xd8 | reg0); /* NEG(reg0); */
845 jitCompA0001_movRxxEax(&w, src[1]);
848 #if (jitCompA0001_USE_R3F_INCDEC != 0)
849 if ((*src == 0x14 && w.r3f == 1) || (*src == 0x15 && w.r3f == -1)) {
850 jitCompPutByte1(w.dst, 0x40 | reg0); /* INC(reg0); */
852 jitCompA0001_movRxxEax(&w, src[1]);
855 if ((*src == 0x15 && w.r3f == 1) || (*src == 0x14 && w.r3f == -1)) {
856 jitCompPutByte1(w.dst, 0x48 | reg0); /* DEC(reg0); */
858 jitCompA0001_movRxxEax(&w, src[1]);
862 #if (jitCompA0001_USE_R3F_IMM8 != 0)
863 if (-0x80 <= w.r3f && w.r3f <= 0x7f) {
865 static unsigned char basic_op_table_im8[] = { 0xc8, 0xf0, 0xe0, 0, 0xc0, 0xe8 };
866 jitCompPutByte3(w.dst, 0x83, basic_op_table_im8[*src - 0x10] | reg0, w.r3f & 0xff);
869 jitCompPutByte3(w.dst, 0x6b, 0xc0 | reg0 << 3 | reg0, w.r3f & 0xff);
872 jitCompA0001_movRxxEax(&w, src[1]);
876 if (reg0 == 0 /* EAX */) {
877 static unsigned char basic_op_table_im32_eax[] = { 0x0d, 0x35, 0x25, 0, 0x05, 0x2d, 0xc0 };
878 if (*src == 0x16) { jitCompPutByte1(w.dst, 0x69); }
879 jitCompPutByte1(w.dst, basic_op_table_im32_eax[*src - 0x10]);
883 static unsigned char basic_op_table_im32_reg[] = { 0xc8, 0xf0, 0xe0, 0, 0xc0, 0xe8 };
884 jitCompPutByte2(w.dst, 0x81, basic_op_table_im32_reg[*src - 0x10] | reg0);
887 jitCompPutByte2(w.dst, 0x69, 0xc0 | reg0 << 3 | reg0);
890 jitCompPutImm32(&w, w.r3f);
892 jitCompA0001_movRxxEax(&w, src[1]);
896 reg1 = jitCompA000_selectRegCache(src[3], -1 /* mem */);
897 if (src[3] >= 0x40) w.err = JITC_ERR_REGNUM;
900 static unsigned char basic_op_table_rr[] = { 0x09, 0x31, 0x21, 0, 0x01, 0x29 }; /* op(reg,reg); */
901 jitCompPutByte2(w.dst, basic_op_table_rr[*src - 0x10], 0xc0 | reg1 << 3 | reg0);
904 static unsigned char basic_op_table_rm[] = { 0x0b, 0x33, 0x23, 0, 0x03, 0x2b, 0xaf }; /* op(reg,mem); */
905 jitCompPutByte1(w.dst, basic_op_table_rm[*src - 0x10]);
906 jitCompA0001_85DispN(&w, src[3] * 4, reg0);
911 jitCompPutByte3(w.dst, 0x0f, 0xaf, 0xc0 | reg0 << 3 | reg1);
914 jitCompPutByte2(w.dst, 0x0f, 0xaf);
915 jitCompA0001_85DispN(&w, src[3] * 4, reg0);
920 jitCompA0001_movRxxEax(&w, src[1]);
925 if (src[1] >= 0x3f) w.err = JITC_ERR_REGNUM;
926 if (src[3] >= 0x40) w.err = JITC_ERR_REGNUM;
927 #if (jitCompA0001_USE_R3F_IMM32 != 0)
928 if (src[3] == 0x3f) {
929 reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
930 reg1 = jitCompA000_selectRegCache(src[2], -1 /* mem */);
931 if (src[1] >= 0x3f) w.err = JITC_ERR_REGNUM;
933 jitCompA0001_movReg32EbpDisp(&w, reg0, src[2] * 4); /* MOV(reg1, [EBP+?]); */
936 jitCompPutByte2(w.dst, 0x89, 0xc0 | reg1 << 3 | reg0); /* MOV(reg0, reg1); */
939 if (*src == 0x18) { jitCompPutByte3(w.dst, 0xc1, 0xe0 | reg0, w.r3f); } /* SHL(reg0, im8); */
940 if (*src == 0x19) { jitCompPutByte3(w.dst, 0xc1, 0xf8 | reg0, w.r3f); } /* SAR(reg0, im8); */
941 if (reg0 == 0 /* EAX */)
942 jitCompA0001_movRxxEax(&w, src[1]);
948 jitCompA000_storeRegCacheAll(&w); // 手抜き.
949 jitCompA0001_movReg32EbpDisp(&w, 1 /* ECX */, src[3] * 4); /* MOV(ECX, [EBP+?]); */
950 #if (jitCompA0001_USE_R3F_IMM32 != 0)
951 if (src[2] == 0x3f) {
952 jitCompPutByte1(w.dst, 0xb8); /* MOV(EAX, ?); */
953 jitCompPutImm32(&w, w.r3f);
956 jitCompA0001_movEaxRxx(&w, src[2]);
959 jitCompA0001_movEaxRxx(&w, src[2]);
961 if (*src == 0x18) { jitCompPutByte2(w.dst, 0xd3, 0xe0); } /* SHL(EAX, CL); */
962 if (*src == 0x19) { jitCompPutByte2(w.dst, 0xd3, 0xf8); } /* SAR(EAX, CL); */
963 jitCompA0001_movRxxEax(&w, src[1]);
964 jitCompA000_loadRegCacheAll(&w); // 手抜き.
971 if (src[1] >= 0x3f) w.err = JITC_ERR_REGNUM;
972 if (src[2] >= 0x40) w.err = JITC_ERR_REGNUM;
973 if (src[3] >= 0x40) w.err = JITC_ERR_REGNUM;
974 jitCompA000_storeRegCacheAll(&w); // 手抜き.
975 #if (jitCompA0001_USE_R3F_IMM32 != 0)
976 if (src[3] == 0x3f) {
977 jitCompPutByte1(w.dst, 0xb8 | 1); /* MOV(ECX, ?); */
978 jitCompPutImm32(&w, w.r3f);
981 jitCompA0001_movReg32EbpDisp(&w, 1 /* ECX */, src[3] * 4); /* MOV(ECX, [EBP+?]); */
983 if (src[2] == 0x3f) {
984 jitCompPutByte1(w.dst, 0xb8 | 0); /* MOV(EAX, ?); */
985 jitCompPutImm32(&w, w.r3f);
988 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, src[2] * 4); /* MOV(EAX, [EBP+?]); */
991 jitCompA0001_movReg32EbpDisp(&w, 1 /* ECX */, src[3] * 4); /* MOV(ECX, [EBP+?]); */
992 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, src[2] * 4); /* MOV(EAX, [EBP+?]); */
994 jitCompPutByte1(w.dst, 0x99); /* CDQ(); */
995 /* ECXがゼロではないことを確認すべき */
996 jitCompPutByte2(w.dst, 0xf7, 0xf9); /* IDIV(ECX); */
997 if (*src == 0x1a) { jitCompA0001_movEbpDispReg32(&w, src[1] * 4, 0 /* EAX */); }
998 if (*src == 0x1b) { jitCompA0001_movEbpDispReg32(&w, src[1] * 4, 2 /* EDX */); }
999 jitCompA000_loadRegCacheAll(&w); // 手抜き.
1003 case 0x1c: /* PLMT0 */
1004 case 0x1d: /* PLMT1 */
1005 if (src[1] >= 0x40 || src[2] >= 0x40) w.err = JITC_ERR_PREGNUM;
1006 if (level < JITC_LV_FASTEST) {
1008 if (level < JITC_LV_FASTER) {
1009 // typ が一致していることを確認.
1010 // plsとliveSignが一致していることを確認.
1012 // preg1はp0 <= p <= p1 を満たしているか?.
1013 // 新しいp0/p1は古いp0〜p1に適合しているか?.
1018 case 0x1e: /* PCP */ /* 未完成(p1まで完成) */
1019 if (src[1] >= 0x40 || src[2] >= 0x40) w.err = JITC_ERR_PREGNUM;
1020 if (src[2] == 0x3f) w.err = JITC_ERR_PREGNUM;
1021 if (src[1] != 0x3f) {
1022 /* src[2] == 0xff の場合に対応できてない */
1023 jitCompA000_storePRegCacheAll(&w); // 手抜き.
1024 for (i = 0; i < 32; i += 4) {
1025 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[2] * 32 + i); /* MOV(EAX, [EBP+?]); */
1026 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + i, 0 /* EAX */); /* MOV([EBP+?], EAX); */
1028 jitCompA000_loadPRegCacheAll(&w); // 手抜き.
1031 if (level < JITC_LV_FASTER) {
1032 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[2] * 32 + 4); /* MOV(EAX, [EBP+?]); */ /* typ */
1033 jitCompPutByte3(w.dst, 0x83, 0xf8, 0); /* CMP(EAX, 0); */
1034 jitCompPutByte2(w.dst, 0x0f, 0x85); /* JNE */
1035 jitCompPutImm32(&w, errfnc - (w.dst + 4));
1036 /* セキュリティチェックが足りてない!(aliveとか) */
1039 jitCompA000_storePRegCacheAll(&w); // 手抜き.
1040 jitCompA0001_movReg32EbpDisp(&w, reg0, 256 + src[2] * 32 + 0); /* MOV(EAX, [EBP+?]); */
1041 if (level < JITC_LV_FASTER) {
1042 jitCompPutByte1(w.dst, 0x3b); /* CMP(reg0, [EBP+?]); */
1043 jitCompA0001_85DispN(&w, 256 + src[2] * 32 + 8, reg0); /* p0 */
1044 jitCompPutByte2(w.dst, 0x0f, 0x85); /* JNE */
1045 jitCompPutImm32(&w, errfnc - (w.dst + 4));
1047 jitCompPutByte2(w.dst, 0xff, 0xe0); /* JMP(EAX); */
1051 case 0x1f: /* PCST */
1052 if (jitCompGetImm32(src + 2) == 0) {
1053 if (level < JITC_LV_FASTER)
1054 jitCompA0001_checkType0(&w, src[6], jitCompGetImm32(src + 7), 2);
1055 jitCompA000_storePRegCacheAll(&w); // 手抜き.
1056 for (i = 0; i < 32 - 4; i += 4) {
1057 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[6] * 32 + i); /* MOV(EAX, [EBP+?]); */
1059 jitCompPutByte1(w.dst, 0x0d); /* OR(EAX, ?); */
1060 jitCompPutImm32(&w, 0x80000000);
1062 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + i, 0 /* EAX */); /* MOV([EBP+?], EAX); */
1064 jitCompPutByte1(w.dst, 0xb8); /* MOV(EAX, ?); */
1065 jitCompPutImm32(&w, debugInfo1);
1066 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 28, 0 /* EAX */); /* MOV([EBP+?], EAX); */
1067 jitCompA000_loadPRegCacheAll(&w); // 手抜き.
1071 if (jitCompGetImm32(src + 7) == 0) {
1072 jitCompA000_storePRegCacheAll(&w); // 手抜き.
1073 for (i = 0; i < 32 - 4; i += 4) {
1074 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[6] * 32 + i); /* MOV(EAX, [EBP+?]); */
1076 jitCompPutByte1(w.dst, 0x25); /* AND(EAX, ?); */
1077 jitCompPutImm32(&w, 0x7fffffff);
1079 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + i, 0 /* EAX */); /* MOV([EBP+?], EAX); */
1081 if (level < JITC_LV_FASTER) {
1082 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[6] * 32 + 28); /* MOV(EAX, [EBP+?]); */
1083 jitCompPutByte1(w.dst, 0x3d); /* CMP(EAX, ?); */
1084 jitCompPutImm32(&w, debugInfo1);
1085 jitCompPutByte2(w.dst, 0x74, 8); /* JE */
1086 jitCompPutByte2(w.dst, 0x31, 0xc0); /* XOR(EAX, EAX); (2) */
1087 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 0, 0 /* EAX */); /* MOV([EBP+?], EAX); (1+1+4) */
1089 jitCompA000_loadPRegCacheAll(&w); // 手抜き.
1093 w.err = JITC_ERR_OPECODE;
1096 case 0x20: /* CMPE */
1097 case 0x21: /* CMPNE */
1098 case 0x22: /* CMPL */
1099 case 0x23: /* CMPGE */
1100 case 0x24: /* CMPLE */
1101 case 0x25: /* CMPG */
1102 case 0x26: /* TSTZ */
1103 case 0x27: /* TSTNZ */
1104 reg0 = jitCompA000_selectRegCache(src[2], 0 /* EAX */);
1105 reg1 = jitCompA000_selectRegCache(src[3], -1 /* mem */);
1106 if (src[1] == 0x3f) {
1108 if (w.prefix != 0) { w.err = JITC_ERR_PREFIX; goto err_w; }
1109 if (src[4] != 0x04 || src[5] != 0x3f || src[6] != 0x03 || src[7] != 0x3f) {
1110 w.err = JITC_ERR_IDIOM; goto err_w;
1114 jitCompA0001_movEaxRxx(&w, src[2]);
1115 #if (jitCompA0001_USE_R3F_IMM32 != 0)
1116 if (src[3] == 0x3f) {
1117 #if (jitCompA0001_OPTIMIZE_CMP != 0)
1118 if ((*src <= 0x25 && w.r3f == 0) || (*src >= 0x26 && w.r3f == -1)) {
1120 if (cmp0reg == src[2]) {
1121 if (cmp0lev >= 1 && (src[0] == 0x20 || src[0] == 0x21 || src[0] == 0x26 || src[0] == 0x27))
1123 if (cmp0lev >= 2 && (src[0] == 0x22 || src[0] == 0x23 || src[0] == 0x24 || src[0] == 0x25))
1127 jitCompPutByte2(w.dst, 0x85, 0xc0 | reg0 << 3 | reg0); /* TEST(reg0, reg0); */
1134 #if (jitCompA0001_USE_R3F_IMM8 != 0)
1135 if (-0x80 <= w.r3f && w.r3f <= 0x7f && *src <= 0x25) {
1136 jitCompPutByte3(w.dst, 0x83, 0xf8 | reg0, w.r3f);
1141 if (*src <= 0x25) { jitCompPutByte1(w.dst, 0x3d); }
1142 if (*src >= 0x26) { jitCompPutByte1(w.dst, 0xa9); }
1145 if (*src <= 0x25) { jitCompPutByte2(w.dst, 0x81, 0xf8 | reg0); }
1146 if (*src >= 0x26) { jitCompPutByte2(w.dst, 0xf7, 0xc0 | reg0); }
1148 jitCompPutImm32(&w, w.r3f);
1152 if (src[3] >= 0x40) w.err = JITC_ERR_PREGNUM;
1154 if (*src <= 0x25) { jitCompPutByte2(w.dst, 0x39, 0xc0 | reg1 << 3 | reg0); }
1155 if (*src >= 0x26) { jitCompPutByte2(w.dst, 0x85, 0xc0 | reg1 << 3 | reg0); }
1158 if (*src <= 0x25) { jitCompPutByte1(w.dst, 0x3b); }
1159 if (*src >= 0x26) { jitCompPutByte1(w.dst, 0x85); }
1160 jitCompA0001_85DispN(&w, src[3] * 4, reg0);
1163 if (w.err != 0) goto err_w;
1164 static unsigned char cmpcc_table0[] = {
1165 0x04, 0x05, 0x0c, 0x0d, 0x0e, 0x0f, 0x04, 0x05, /* CMPcc, TSTcc */
1166 0x04, 0x05, 0x02, 0x03, 0x06, 0x07 /* PCMPcc */
1168 #if (jitCompA0001_USE_R3F_CMPJMP != 0)
1169 if (src[1] == 0x3f) {
1171 jitCompPutByte2(w.dst, 0x0f, 0x80 | cmpcc_table0[*src - 0x20]);
1173 i = jitCompGetLabelNum(&w, src + 2);
1174 if ((flags & JITC_PHASE1) != 0 && w.err != 0) {
1175 if (label[i].opt == 0) { w.err = JITC_ERR_LABELNODEF; goto err_w; }
1176 // if (label[i].typ != 1) { w.err = JITC_ERR_LABELTYP; goto err_w; }
1179 if ((flags & JITC_PHASE1) != 0 || (((flags & JITC_PHASE1) == 0) && label[i].opt != 0))
1180 j = label[i].p - (w.dst + 4);
1181 jitCompPutImm32(&w, j);
1182 #if (jitCompA0001_OPTIMIZE_JMP != 0)
1183 if (-128 - 4 <= j && j < 0) {
1186 jitCompPutByte2(w.dst, w.dst[1] ^ 0xf0, j & 0xff);
1190 if (w.err != 0) goto err_w;
1195 reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
1196 jitCompPutByte3(w.dst, 0x0f, 0x90 | cmpcc_table0[*src - 0x20], 0xc0 | reg0); /* SETcc(BYTE(reg0)); */
1197 jitCompPutByte3(w.dst, 0x0f, 0xb6, 0xc0 | reg0 << 3 | reg0); /* MOVZX(reg0, BYTE(reg0)); */
1198 jitCompPutByte2(w.dst, 0xf7, 0xd8 | reg0); /* NEG(reg0); */
1200 jitCompA0001_movRxxEax(&w, src[1]);
1205 case 0x28: /* PCMPE */
1206 case 0x29: /* PCMPNE */
1207 case 0x2a: /* PCMPL */
1208 case 0x2b: /* PCMPGE */
1209 case 0x2c: /* PCMPLE */
1210 case 0x2d: /* PCMPG */
1211 if (src[1] == 0x3f) {
1213 if (w.prefix != 0) { w.err = JITC_ERR_PREFIX; goto err_w; }
1214 if (src[4] != 0x04 || src[5] != 0x3f || src[6] != 0x03 || src[7] != 0x3f) {
1215 w.err = JITC_ERR_IDIOM; goto err_w;
1218 if (src[2] >= 0x40) w.err = JITC_ERR_PREGNUM;
1219 jitCompA000_storePRegCacheAll(&w); // 手抜き.
1221 jitCompA0001_checkCompPtr(&w, src[2], src[3]);
1222 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[2] * 32 + 0); /* MOV(EAX, [EBP+?]); */
1223 if (src[3] != 0xff) {
1224 jitCompPutByte1(w.dst, 0x3b); /* CMP(EAX, [EBP+?]); */
1225 jitCompA0001_85DispN(&w, 256 + src[3] * 32 + 0, 0);
1228 /* ヌルポインタとの比較はこれでいいのか?たぶんよくない */
1229 jitCompPutByte3(w.dst, 0x83, 0xf8, 0x00); /* CMP(EAX, 0); */
1234 case 0x30: /* talloc(old:F4) */
1235 case 0x31: /* tfree(old:F5) */
1236 case 0x32: /* malloc(old:F6) */
1237 case 0x33: /* mfree(old:F7) */
1238 jitCompA000_storeRegCacheAll(&w); // 手抜き.
1239 jitCompA000_storePRegCacheAll(&w); // 手抜き.
1240 jitCompPutByte2(w.dst, 0x6a, src[3]); /* PUSH(?); */
1241 jitCompPutByte2(w.dst, 0x6a, src[2]); /* PUSH(?); */
1242 jitCompPutByte2(w.dst, 0x6a, src[1]); /* PUSH(?); */
1243 jitCompPutByte1(w.dst, 0x55); /* PUSH(EBP); */
1244 jitCompPutByte1(w.dst, 0xe8);
1245 if (*src == 0x30) j = ((unsigned char *)&funcf4) - (w.dst + 4);
1246 if (*src == 0x31) j = ((unsigned char *)&funcf5) - (w.dst + 4);
1247 if (*src == 0x32) j = ((unsigned char *)&funcf6) - (w.dst + 4);
1248 if (*src == 0x33) j = ((unsigned char *)&funcf7) - (w.dst + 4);
1249 jitCompPutImm32(&w, j);
1250 jitCompPutByte3(w.dst, 0x83, 0xc4, 0x10); /* ADD(ESP,16); */
1251 jitCompA000_loadRegCacheAll(&w); // 手抜き.
1252 jitCompA000_loadPRegCacheAll(&w); // 手抜き.
1256 case 0x34: /* data (暫定) */
1258 if (w.prefix != 0) { w.err = JITC_ERR_PREFIX; goto err_w; }
1259 int k = jitCompGetImm32(&src[1]), tmpData, bitCount, dataWidth = jitCompA000_dataWidth(k);
1260 if (lastlabel >= 0 && label[lastlabel].typ == 0)
1261 label[lastlabel].typ = k;
1263 i = jitCompA000_convTyp(k);
1264 if (i < 2 || i > 7) { w.err = JITC_ERR_BADTYPE; goto err_w; }
1266 j = jitCompGetImm32(&src[5]);
1272 if (src >= src1) { w.err = JITC_ERR_SRC1; src = oldsrc; goto err_w; }
1273 if (w.dst + 256 > dst1) { w.err = JITC_ERR_DST1; src = oldsrc; goto err_w; }
1275 for (k = 0; k < dataWidth; k++) {
1276 tmpData = tmpData << 1 | ((*src >> bitCount) & 1);
1283 if ((i & 1) == 0 && dataWidth <= 31 && (tmpData >> (dataWidth - 1)) != 0) {
1284 tmpData -= 1 << dataWidth;
1286 if (i == 2 || i == 3) { jitCompPutByte1(w.dst, tmpData & 0xff); }
1287 if (i == 4 || i == 5) { jitCompPutByte2(w.dst, tmpData & 0xff, (tmpData >> 8) & 0xff); }
1288 if (i == 6 || i == 7) { jitCompPutByte4(w.dst, tmpData & 0xff, (tmpData >> 8) & 0xff, (tmpData >> 16) & 0xff, (tmpData >> 24) & 0xff); }
1294 if (src >= src1) { w.err = JITC_ERR_SRC1; src = oldsrc; goto err_w; }
1295 if (w.dst + 256 > dst1) { w.err = JITC_ERR_DST1; src = oldsrc; goto err_w; }
1296 i = jitCompGetImm32(src);
1298 if ((flags & JITC_PHASE1) != 0 && w.err == 0) {
1299 if (label[i].opt == 0) { w.err = JITC_ERR_LABELNODEF; goto err_w; }
1301 jitCompPutImm32(&w, (int)label[i].p);
1302 jitCompPutImm32(&w, label[i].typ);
1303 jitCompPutImm32(&w, (int)label[i].p);
1304 jitCompPutImm32(&w, (int)label[i].p1);
1305 jitCompPutImm32(&w, 0); /* liveSign */
1306 jitCompPutImm32(&w, 2320); /* pls */
1307 jitCompPutImm32(&w, 0);
1308 jitCompPutImm32(&w, 0);
1312 if (lastlabel >= 0 && label[lastlabel].p1 < w.dst)
1313 label[lastlabel].p1 = w.dst;
1316 case 0x3c: /* ENTER */
1317 jitCompA000_storeRegCacheAll(&w); // 手抜き.
1318 jitCompA000_storePRegCacheAll(&w); // 手抜き.
1319 jitCompPutByte2(w.dst, 0x6a, src[6]); /* PUSH(?); */
1320 jitCompPutByte2(w.dst, 0x6a, src[5]); /* PUSH(?); */
1321 jitCompPutByte2(w.dst, 0x6a, src[4] & 0x0f); /* PUSH(?); */
1322 jitCompPutByte2(w.dst, 0x6a, (src[4] >> 4) & 0x0f); /* PUSH(?); */
1323 jitCompPutByte2(w.dst, 0x6a, src[3]); /* PUSH(?); */
1324 jitCompPutByte2(w.dst, 0x6a, src[2]); /* PUSH(?); */
1325 jitCompPutByte2(w.dst, 0x6a, src[1]); /* PUSH(?); */
1326 jitCompPutByte1(w.dst, 0x55); /* PUSH(EBP); */
1327 jitCompPutByte1(w.dst, 0xe8);
1328 j = ((unsigned char *)&func3c) - (w.dst + 4);
1329 jitCompPutImm32(&w, j);
1330 jitCompPutByte3(w.dst, 0x83, 0xc4, 0x20); /* ADD(ESP,32); */
1331 jitCompA000_loadRegCacheAll(&w); // 手抜き.
1332 jitCompA000_loadPRegCacheAll(&w); // 手抜き.
1336 case 0x3d: /* LEAVE */
1337 jitCompA000_storeRegCacheAll(&w); // 手抜き.
1338 jitCompA000_storePRegCacheAll(&w); // 手抜き.
1339 jitCompPutByte2(w.dst, 0x6a, src[6]); /* PUSH(?); */
1340 jitCompPutByte2(w.dst, 0x6a, src[5]); /* PUSH(?); */
1341 jitCompPutByte2(w.dst, 0x6a, src[4] & 0x0f); /* PUSH(?); */
1342 jitCompPutByte2(w.dst, 0x6a, (src[4] >> 4) & 0x0f); /* PUSH(?); */
1343 jitCompPutByte2(w.dst, 0x6a, src[3]); /* PUSH(?); */
1344 jitCompPutByte2(w.dst, 0x6a, src[2]); /* PUSH(?); */
1345 jitCompPutByte2(w.dst, 0x6a, src[1]); /* PUSH(?); */
1346 jitCompPutByte1(w.dst, 0x55); /* PUSH(EBP); */
1347 jitCompPutByte1(w.dst, 0xe8);
1348 j = ((unsigned char *)&func3d) - (w.dst + 4);
1349 jitCompPutImm32(&w, j);
1350 jitCompPutByte3(w.dst, 0x83, 0xc4, 0x20); /* ADD(ESP,32); */
1351 jitCompA000_loadRegCacheAll(&w); // 手抜き.
1352 jitCompA000_loadPRegCacheAll(&w); // 手抜き.
1356 case 0xfe: /* remark */
1357 if (src[1] == 0x01 && src[2] == 0x00) { // DBGINFO1
1358 if (level <= JITC_LV_SLOWER) {
1359 jitCompPutByte1(w.dst, 0xb8); /* MOV(EAX, ?); */
1360 jitCompPutImm32(&w, debugInfo1);
1361 jitCompA0001_movEbpDispReg32(&w, 2304 + 4, 0 /* EAX */); /* MOV(debugInfo1, EAX); */
1364 if (src[1] == 0x01 && src[2] == 0x03) { // DBGINFO1CLR
1365 if (level <= JITC_LV_SLOWER) {
1366 jitCompPutByte1(w.dst, 0xb8); /* MOV(EAX, ?); */
1367 jitCompPutImm32(&w, -1);
1368 jitCompA0001_movEbpDispReg32(&w, 2304 + 4, 0 /* EAX */); /* MOV(debugInfo1, EAX); */
1371 if (src[1] == 0x05 && src[2] == 0x00) { // DBGINFO0
1372 if (level <= JITC_LV_SLOWEST) {
1373 debugInfo0 = jitCompGetImm32(src + 3);
1374 // jitCompPutByte1(w.dst, 0xbf); /* MOV(EDI, ?); */
1375 // jitCompPutImm32(&w, debugInfo0);
1376 jitCompPutByte1(w.dst, 0xb8); /* MOV(EAX, ?); */
1377 jitCompPutImm32(&w, debugInfo0);
1378 jitCompA0001_movEbpDispReg32(&w, 2304 + 0, 0 /* EAX */); /* MOV(debugInfo0, EAX); */
1384 w.err = JITC_ERR_OPECODE;
1387 if (w.err != 0) goto err_w;
1388 jitCompA0001_fixPrefix(&w);
1389 if (w.err != 0) goto err_w;
1390 src += jitCompCmdLen(src);
1392 if (enter0 != NULL) {
1393 j = w.dst - (enter0 + 4);
1394 enter0[0] = j & 0xff;
1395 enter0[1] = (j >> 8) & 0xff;
1396 enter0[2] = (j >> 16) & 0xff;
1397 enter0[3] = (j >> 24) & 0xff;
1399 if ((flags & JITC_NOSTARTUP) == 0) {
1400 jitCompA000_storeRegCacheAll(&w);
1401 jitCompA000_storePRegCacheAll(&w);
1402 jitCompPutByte1(w.dst, 0x61); /* POPAD(); */
1404 if ((flags & JITC_PHASE1) != 0)
1405 return w.dst - dst00;
1409 if ((w.err & JITC_ERR_PHASE0ONLY) != 0) {
1410 if ((flags & JITC_PHASE1) == 0)
1411 w.err &= ~JITC_ERR_PHASE0ONLY;
1413 if (w.err == (JITC_ERR_MASK & JITC_ERR_REGNUM)) errmsg = "reg-number error";
1414 if (w.err == (JITC_ERR_MASK & JITC_ERR_DST1)) errmsg = "dst1 error";
1415 if (w.err == (JITC_ERR_MASK & JITC_ERR_OPECODE)) errmsg = "opecode error";
1416 if (w.err == (JITC_ERR_MASK & JITC_ERR_LABELNUM)) errmsg = "label number too large";
1417 if (w.err == (JITC_ERR_MASK & JITC_ERR_LABELREDEF)) errmsg = "label redefine";
1418 if (w.err == (JITC_ERR_MASK & JITC_ERR_PREFIX)) { errmsg = "prefix redefine"; w.dst -= 2; }
1419 if (w.err == (JITC_ERR_MASK & JITC_ERR_LABELNODEF)) errmsg = "label not defined";
1420 if (w.err == (JITC_ERR_MASK & JITC_ERR_LABELTYP)) errmsg = "label type error";
1421 if (w.err == (JITC_ERR_MASK & JITC_ERR_IDIOM)) errmsg = "idiom error";
1422 if (w.err == (JITC_ERR_MASK & JITC_ERR_PREGNUM)) errmsg = "preg-number error";
1423 if (w.err == (JITC_ERR_MASK & JITC_ERR_SRC1)) errmsg = "src1 error";
1424 if (w.err == (JITC_ERR_MASK & JITC_ERR_BADTYPE)) errmsg = "bad type code";
1425 if (w.err == (JITC_ERR_MASK & JITC_ERR_PREFIXFAR)) errmsg = "prefix internal error";
1426 if (w.err == (JITC_ERR_MASK & JITC_ERR_INTERNAL)) errmsg = "general internal error";
1427 if (*errmsg != '\0') {
1428 fprintf(stderr, "JITC: %s at %06X (debugInfo0=%d)\n ", errmsg, src - src0, debugInfo0);
1429 for (i = 0; i < 16; i++)
1430 fprintf(stderr, "%02X ", src[i]);
1431 static char *table[0x30] = {
1432 "NOP", "LB", "LIMM", "PLIMM", "CND", "??", "??", "??",
1433 "LMEM", "SMEM", "PLMEM", "PSMEM", "LEA", "??", "PADD", "PDIF",
1434 "CP/OR", "XOR", "AND", "??", "ADD", "SUB", "MUL", "??",
1435 "SHL", "SAR", "DIV", "MOD", "PLMT0", "PLMT1", "PCP", "PCST",
1436 "CMPE", "CMPNE", "CMPL", "CMPGE", "CMPLE", "CMPG", "TSTZ", "TSTNZ",
1437 "PCMPE", "PCMPNE", "PCMPL", "PCMPGE", "PCMPLE", "PCMPG", "??", "EXT" };
1439 if (*src < 0x30) errmsg = table[*src];
1440 fprintf(stderr, "(%s)\n", errmsg);
1445 unsigned char *jitCompCallFunc(unsigned char *dst, void *func)
1447 struct JitCompWork w;
1449 jitCompA000_storeRegCacheAll(&w);
1450 jitCompA000_storePRegCacheAll(&w);
1451 jitCompPutByte1(w.dst, 0x60); /* PUSHAD(); */
1452 jitCompPutByte1(w.dst, 0x50); /* PUSH(EAX); */ /* for 16byte-align(win32では不要なのだけど、MacOSには必要らしい) */
1453 jitCompPutByte1(w.dst, 0x55); /* PUSH(EBP); */
1454 jitCompPutByte1(w.dst, 0xe8); /* CALL(func); */
1455 int j = ((unsigned char *)func) - (w.dst + 4);
1457 //この関数の中では結局w->dstしか参照していない
1458 jitCompPutImm32(&w, j);
1460 jitCompPutByte1(w.dst, 0x58); /* POP(EAX); */ /* (win32では不要なのだけど、MacOSには必要らしい) */
1461 jitCompPutByte1(w.dst, 0x58); /* POP(EAX); */
1462 jitCompPutByte1(w.dst, 0x61); /* POPAD(); */
1463 jitCompA000_loadRegCacheAll(&w);
1464 jitCompA000_loadPRegCacheAll(&w);
1465 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + 0x30 * 32 + 0); /* MOV(EAX, [EBP+?]); */
1466 jitCompPutByte2(w.dst, 0xff, 0xe0); /* JMP(EAX); */
1470 unsigned char *jitCompInit(unsigned char *dst)
1473 return jitCompCallFunc(dst, &errHndl);
1476 void func3c(char *ebp, int opt, int r1, int p1, int lenR, int lenP, int r0, int p0)
1478 HOSECPU_RuntimeEnvironment *r = (HOSECPU_RuntimeEnvironment *) (ebp - jitCompA0001_EBP128);
1480 HOSECPU_PointerRegisterEntry *pp;
1481 if (r->junkStack + 2048 > r->junkStack1) (*(r->errHndl))(r);
1482 pi = (void *)r->junkStack; r->junkStack += r1 * 4;
1483 for (i = 0; i < r1; i++)
1485 pp = (void *)r->junkStack; r->junkStack += p1 * 32;
1486 for (i = 0; i < p1; i++)
1488 pp = (void *)r->junkStack; r->junkStack += 32;
1489 *pp = r->preg[0x30];
1490 pi = (void *)r->junkStack; r->junkStack += 4;
1491 *pi = opt << 16 | r1 << 8 | p1;
1492 for (i = 0; i < lenR; i++)
1493 r->ireg[r0 + i] = r->ireg[0x30 + i];
1494 for (i = 0; i < lenP; i++)
1495 r->preg[p0 + i] = r->preg[0x31 + i];
1499 void func3d(char *ebp, int opt, int r1, int p1, int lenR, int lenP, int r0, int p0)
1501 HOSECPU_RuntimeEnvironment *r = (HOSECPU_RuntimeEnvironment *) (ebp - jitCompA0001_EBP128);
1505 HOSECPU_PointerRegisterEntry *pp = (void *)r->junkStack;
1506 r->preg[0x30] = *pp;
1507 r->junkStack -= p1 * 32; pp = (void *)r->junkStack;
1508 for (i = 0; i < p1; i++)
1510 r->junkStack -= r1 * 4; int *pi = (void *)r->junkStack;
1511 for (i = 0; i < r1; i++)
1516 void funcf4(char *ebp, int pxx, int typ, int len)
1518 HOSECPU_RuntimeEnvironment *r = (HOSECPU_RuntimeEnvironment *) (ebp - jitCompA0001_EBP128);
1519 int width = jitCompA000_dataWidth(jitCompA000_convTyp(r->ireg[typ])) >> 3;
1520 if (width < 0 || r->ireg[len] < 0)
1522 void *p = r->junkStack;
1523 if (r->junkStack + width * r->ireg[len] + 256 > r->junkStack1) (*(r->errHndl))(r);
1524 r->junkStack += width * r->ireg[len];
1526 r->preg[pxx].typ = r->ireg[typ];
1527 r->preg[pxx].p0 = p;
1528 r->preg[pxx].p1 = (void *)r->junkStack;
1529 int *pi = (int *)r->junkStack;
1530 *pi = width * r->ireg[len];
1531 r->junkStack += sizeof (int);
1532 if (r->ireg[typ] == 1) {
1533 int i, i1 = (width * r->ireg[len]) >> 2;
1535 for (i = 0; i < i1; i++)
1541 void funcf5(char *ebp, int pxx, int typ, int len)
1544 HOSECPU_RuntimeEnvironment *r = (HOSECPU_RuntimeEnvironment *) (ebp - jitCompA0001_EBP128);
1545 r->junkStack -= sizeof (int);
1546 int *pi = (int *)r->junkStack;
1547 r->junkStack -= *pi;
1549 int width = jitCompA000_dataWidth(r->ireg[typ]);
1550 void *p = r->junkStack;
1551 r->junkStack -= width * r->ireg[len];
1556 void funcf6(char *ebp, int pxx, int typ, int len)
1558 HOSECPU_RuntimeEnvironment *r = (HOSECPU_RuntimeEnvironment *) (ebp - jitCompA0001_EBP128);
1559 int width = jitCompA000_dataWidth(jitCompA000_convTyp(r->ireg[typ])) >> 3;
1560 if (width < 0 || r->ireg[len] < 0)
1562 void *p = malloc(width * r->ireg[len]);
1564 r->preg[pxx].typ = r->ireg[typ];
1565 r->preg[pxx].p0 = p;
1566 r->preg[pxx].p1 = (unsigned char *)p + width * r->ireg[len];
1567 if (r->ireg[typ] == 1) {
1568 int i, i1 = (width * r->ireg[len]) >> 2, *pi;
1570 for (i = 0; i < i1; i++)
1572 for (i = 1; i < i1; i += 8)
1578 void funcf7(char *ebp, int pxx, int typ, int len)
1580 // typとlenはダミーで参照されない
1581 HOSECPU_RuntimeEnvironment *r = (HOSECPU_RuntimeEnvironment *) (ebp - jitCompA0001_EBP128);
1582 free(r->preg[pxx].p);
1586 void errHndl(HOSECPU_RuntimeEnvironment *r)
1588 r = (HOSECPU_RuntimeEnvironment *) (((char *)r) - jitCompA0001_EBP128);
1594 * jitcの出力コードをひとまとめにする関数を作成しその中身をjitCompile()で生成
1596 * qq : 出力バイナリの書き込み位置のアドレスへの参照(書き込み位置を呼び出しに反映させるため参照渡しにする)
1597 * q1 : 出力バイナリの書き込み位置のアドレスの最大値
1598 * p0 : (*.ose)バイナリの読み込み位置のアドレス(ヘッダ部除去済)
1599 * p1 : (*.ose)バイナリの読み込み位置の取りうる最大値
1600 * (ただし、「確保したメモリ」の最大値なのでこれより手前にゴミデータが入っているかもしれない)
1602 * ret=2 : jitコンパイルエラー
1604 int jitc0(unsigned char **qq, unsigned char *q1, const unsigned char *p0, const unsigned char *p1, int level, HOSECPU_LabelListTag *label)
1606 unsigned char *q = *qq;
1607 if (p0[0] != 0x05 || p0[1] != SIGN1) // OSECPUのヘッダ (05E1) を確認
1610 *q++ = 0x55; /* PUSH(EBP); */
1611 *q++ = 0x8b; *q++ = 0x6c; *q++ = 0x24; *q++ = 0x08; /* MOV(EBP,[ESP+8]); */
1614 for (i = 0; i < JITC_MAXLABELS; i++)
1617 // 以下のjitCompile()呼び出しでは第二引数をq1-2にした方がよいのではないか?
1618 i = jitCompiler(q, q1, p0 + 2, p1, p0, label, JITC_MAXLABELS, level, di1_serial, 0);
1619 if (i != 0) return 2;
1620 i = jitCompiler(q, q1, p0 + 2, p1, p0, label, JITC_MAXLABELS, level, di1_serial, JITC_PHASE1 + 0);
1621 if (i < 0) return 2;
1624 *q++ = 0x5d; /* POP(EBP); */
1625 *q++ = 0xc3; /* RET(); */
1631 #if (USE_DEBUGGER != 0)
1633 int dbgrGetRegNum(const char *p)
1639 if (i > 9) i -= 'A' - '0' - 10;
1640 if (j > 9) j -= 'A' - '0' - 10;
1641 if (0 <= i && i <= 15 && 0 <= j && j <= 15)
1647 void dbgrMain(HOSECPU_RuntimeEnvironment *r)
1649 if (r->dbgr == 0) return;
1654 p = fgets(cmd, 64, stdin);
1655 if (p == NULL) break;
1656 if (cmd[0] == '\0') continue;
1657 if (cmd[0] == 'q' && cmd[1] <= ' ') break;
1658 if (cmd[0] == 'p' && cmd[1] <= ' ' && cmd[1] != '\0') {
1660 while (*p <= ' ' && *p != '\0') p++;
1662 i = dbgrGetRegNum(p + 1);
1663 if (0 <= i && i <= 0x3f) {
1664 printf("R%02X = 0x%08X = %d\n", i, r->ireg[i], r->ireg[i]);
1667 puts("register name error");
1671 i = dbgrGetRegNum(p + 1);
1672 if (0 <= i && i <= 0x3f) {
1674 if (0 <= r->preg[i].typ && r->preg[i].typ <= 0x15) {
1675 static char *typName[] = {
1676 "T_CODE", "T_VPTR", "T_SINT8", "T_UINT8",
1677 "T_SINT16", "T_UINT16", "T_SINT32", "T_UINT32",
1678 "T_SINT4", "T_UINT4", "T_SINT2", "T_UINT2",
1679 "T_SINT1", "T_UINT1", "T_SINT12", "T_UINT12",
1680 "T_SINT20", "T_UINT20", "T_SINT24", "T_UINT24",
1681 "T_SINT28", "T_UINT28"
1683 p = typName[r->preg[i].typ];
1685 printf("P%02X:\n type = %s(%04X), (origin-ptr) = 0x%08X\n", i, p, r->preg[i].typ, (unsigned int)(r->preg[i].p0));
1686 if (r->preg[i].p != NULL && r->preg[i].p0 != NULL) {
1687 j = jitCompA000_dataWidth(jitCompA000_convTyp(r->preg[i].typ)) >> 3;
1689 k = (r->preg[i].p1 - r->preg[i].p0) / j;
1690 printf(" size = 0x%08X = %d\n", k, k);
1691 k = (r->preg[i].p - r->preg[i].p0) / j;
1692 printf(" pos = 0x%08X = %d\n", k, k);
1695 puts(" null pointer");
1699 puts("register name error");
1703 puts("command error");