3 #define JITC_ERR_MASK 255
4 #define JITC_ERR_PHASE0ONLY 256
5 #define JITC_ERR_REGNUM (1 | JITC_ERR_PHASE0ONLY)
6 #define JITC_ERR_DST1 (2 | JITC_ERR_PHASE0ONLY)
7 #define JITC_ERR_OPECODE (3 | JITC_ERR_PHASE0ONLY)
8 #define JITC_ERR_LABELNUM (4 | JITC_ERR_PHASE0ONLY)
9 #define JITC_ERR_LABELREDEF (5 | JITC_ERR_PHASE0ONLY)
10 #define JITC_ERR_PREFIX (6 | JITC_ERR_PHASE0ONLY)
11 #define JITC_ERR_LABELNODEF 7
12 #define JITC_ERR_LABELTYP 8
13 #define JITC_ERR_IDIOM 9
14 #define JITC_ERR_PREGNUM (10 | JITC_ERR_PHASE0ONLY)
15 #define JITC_ERR_SRC1 (11 | JITC_ERR_PHASE0ONLY)
16 #define JITC_ERR_BADTYPE (12 | JITC_ERR_PHASE0ONLY)
17 #define JITC_ERR_PREFIXFAR (13 | JITC_ERR_PHASE0ONLY)
18 #define JITC_ERR_INTERNAL 99
20 void errorHandler(HOSECPU_RuntimeEnvironment *r)
22 puts("security error! abort...");
23 printf("debugInfo0=%d, debugInfo1=%d\n", r->debugInfo0, r->debugInfo1);
24 #if (USE_DEBUGGER != 0)
30 int jitCompCmdLen(const unsigned char *src)
34 if (0x01 <= *src && *src < 0x04) i = 6;
35 if (*src == 0x04) i = 2;
36 if (0x08 <= *src && *src < 0x0d) i = 8 + src[7] * 4;
37 if (0x0e <= *src && *src < 0x10) i = 8;
38 if (0x10 <= *src && *src < 0x2e) i = 4;
39 if (0x1c <= *src && *src < 0x1f) i = 3;
40 if (*src == 0x1f) i = 11;
41 if (*src == 0x2f) i = 4 + src[1];
42 if (0x30 <= *src && *src <= 0x33) i = 4;
43 if (0x3c <= *src && *src <= 0x3d) i = 7;
44 if (*src == 0xfe) i = 2 + src[1];
47 #if (JITC_ARCNUM == 0x0001)
53 以下は最適化のためのものなので、すべて0として簡単に移植しても問題ありません */
54 #define jitCompA0001_USE_R3F_CMPJMP 1*1
55 #define jitCompA0001_USE_R3F_IMM32 1*1
56 #define jitCompA0001_USE_R3F_IMM8 1*1
57 #define jitCompA0001_USE_R3F_INCDEC 1*1
58 #define jitCompA0001_OPTIMIZE_JMP 1*1
59 #define jitCompA0001_OPTIMIZE_MOV 1*1 /* 1にすると速度低下する? */
60 #define jitCompA0001_OPTIMIZE_CMP 1*1
61 #define jitCompA0001_OPTIMIZE_ALIGN 4*1 /* 0-8を想定 */
62 #define jitCompA0001_EBP128 128*1
65 unsigned char *dst, *dst0;
67 #if (jitCompA0001_USE_R3F_IMM32 != 0)
70 char prefix; //CND命令の値を記録(初期値=0)
73 #define jitCompPutByte1(p, c0) *p++ = c0
74 #define jitCompPutByte2(p, c0, c1) *p++ = c0; *p++ = c1
75 #define jitCompPutByte3(p, c0, c1, c2) *p++ = c0; *p++ = c1; *p++ = c2
76 #define jitCompPutByte4(p, c0, c1, c2, c3) *p++ = c0; *p++ = c1; *p++ = c2; *p++ = c3
78 static void jitCompPutImm32(struct JitCompWork *w, int i)
80 jitCompPutByte1(w->dst, i & 0xff);
81 jitCompPutByte1(w->dst, (i >> 8) & 0xff);
82 jitCompPutByte1(w->dst, (i >> 16) & 0xff);
83 jitCompPutByte1(w->dst, (i >> 24) & 0xff);
87 int jitCompGetImm32(const unsigned char *src)
89 return (src[0] << 24) | (src[1] << 16) | (src[2] << 8) | src[3];
92 int jitCompGetLabelNum(struct JitCompWork *w, const unsigned char *src)
94 int i = jitCompGetImm32(src);
95 if (i < 0 || i >= w->maxLabels) {
96 w->err = JITC_ERR_LABELNUM;
102 void jitCompA0001_85DispN(struct JitCompWork *w, int disp, int n)
104 disp -= jitCompA0001_EBP128;
105 if (-128 <= disp && disp <= 127) {
106 jitCompPutByte2(w->dst, 0x45 | (n << 3), disp & 0xff);
109 jitCompPutByte1(w->dst, 0x85 | (n << 3));
110 jitCompPutImm32(w, disp);
115 void jitCompA0001_movEbpDispReg32(struct JitCompWork *w, int disp, int reg32)
117 jitCompPutByte1(w->dst, 0x89); /* MOV(mem, reg32); */
118 jitCompA0001_85DispN(w, disp, reg32);
122 void jitCompA0001_movReg32EbpDisp(struct JitCompWork *w, int reg32, int disp)
124 jitCompPutByte1(w->dst, 0x8b); /* MOV(reg32, mem); */
125 jitCompA0001_85DispN(w, disp, reg32);
129 void jitCompA0001_movEaxRxx(struct JitCompWork *w, int rxx)
131 #if (jitCompA0001_USE_R3F_IMM32 != 0)
133 jitCompPutByte1(w->dst, 0xb8); /* MOV(EAX, ?); */
134 jitCompPutImm32(w, w->r3f);
138 if (rxx >= 0x40 || rxx < 0) w->err = JITC_ERR_REGNUM;
139 jitCompA0001_movReg32EbpDisp(w, 0 /* EAX */, rxx * 4); /* MOV(EAX, [EBP+?]); */
143 void jitCompA0001_movRxxEax(struct JitCompWork *w, int rxx)
145 if (rxx >= 0x40 || rxx < 0) w->err = JITC_ERR_REGNUM;
146 jitCompA0001_movEbpDispReg32(w, rxx * 4, 0 /* EAX */); /* MOV([EBP+?], EAX); */
150 void jitCompA0001_fixPrefix(struct JitCompWork *w)
152 if (w->prefix != 0) {
153 if (w->dst - w->dst0 > 127) w->err = JITC_ERR_REGNUM;
154 w->dst0[-1] = (unsigned char)((w->dst - w->dst0) & 0xff);
159 void jitCompA0001_checkCompPtr(struct JitCompWork *w, int p0, int p1)
161 if (p0 >= 0x3f || p0 < 0) w->err = JITC_ERR_PREGNUM;
162 if (p1 >= 0x3f || p1 < 0) w->err = JITC_ERR_PREGNUM;
163 /* 比較可能可能なのかのチェックのコードを出力 */ /* 未完成 */
167 void jitCompA000_loadRegCacheAll(struct JitCompWork *w)
169 jitCompA0001_movReg32EbpDisp(w, 3 /* EBX */, 0 * 4); /* EBX = R00; */
170 jitCompA0001_movReg32EbpDisp(w, 1 /* ECX */, 1 * 4); /* ECX = R01; */
171 jitCompA0001_movReg32EbpDisp(w, 2 /* EDX */, 2 * 4); /* EDX = R02; */
175 void jitCompA000_storeRegCacheAll(struct JitCompWork *w)
177 jitCompA0001_movEbpDispReg32(w, 0 * 4, 3 /* EBX */); /* R00 = EBX; */
178 jitCompA0001_movEbpDispReg32(w, 1 * 4, 1 /* ECX */); /* R01 = ECX; */
179 jitCompA0001_movEbpDispReg32(w, 2 * 4, 2 /* EDX */); /* R02 = EDX; */
183 void jitCompA000_loadRegCacheEcx(struct JitCompWork *w)
185 jitCompA0001_movReg32EbpDisp(w, 1 /* ECX */, 1 * 4); /* ECX = R01; */
189 void jitCompA000_storeRegCacheEcx(struct JitCompWork *w)
191 jitCompA0001_movEbpDispReg32(w, 1 * 4, 1 /* ECX */); /* R01 = ECX; */
195 void jitCompA000_loadRegCacheEdx(struct JitCompWork *w)
197 jitCompA0001_movReg32EbpDisp(w, 2 /* EDX */, 2 * 4); /* EDX = R02; */
201 void jitCompA000_storeRegCacheEdx(struct JitCompWork *w)
203 jitCompA0001_movEbpDispReg32(w, 2 * 4, 2 /* EDX */); /* R02 = EDX; */
207 int jitCompA000_selectRegCache(int rxx, int reg)
209 if (rxx == 0) reg = 3; /* EBX */
210 if (rxx == 1) reg = 1; /* ECX */
211 if (rxx == 2) reg = 2; /* EDX */
215 void jitCompA000_loadPRegCacheAll(struct JitCompWork *w)
217 // jitCompA0001_movReg32EbpDisp(w, 5 /* EBP */, 256 + 0 * 32 + 0); /* EBP = P00; */
218 jitCompA0001_movReg32EbpDisp(w, 6 /* ESI */, 256 + 1 * 32 + 0); /* ESI = P01; */
219 jitCompA0001_movReg32EbpDisp(w, 7 /* EDI */, 256 + 2 * 32 + 0); /* EDI = P02; */
223 void jitCompA000_storePRegCacheAll(struct JitCompWork *w)
225 // jitCompA0001_movEbpDispReg32(w, 256 + 0 * 32 + 0, 5 /* EBP */); /* P00 = EBP; */
226 jitCompA0001_movEbpDispReg32(w, 256 + 1 * 32 + 0, 6 /* ESI */); /* P01 = ESI; */
227 jitCompA0001_movEbpDispReg32(w, 256 + 2 * 32 + 0, 7 /* EDI */); /* P02 = EDI; */
231 int jitCompA000_selectPRegCache(int pxx, int reg)
233 // if (pxx == 0) reg = 5; /* EBP */
234 if (pxx == 1) reg = 6; /* ESI */
235 if (pxx == 2) reg = 7; /* EDI */
239 int jitCompA000_convTyp(int t)
242 if (1 <= t && t <= 7) r = t;
243 if (8 <= t && t <= 13) r = 2 | (t & 1);
244 if (14 <= t && t <= 15) r = 4 | (t & 1);
245 if (16 <= t && t <= 21) r = 6 | (t & 1);
249 int jitCompA000_dataWidth(int t)
252 if (t == 0x0001) r = 256;
254 if (t == 0x0002 / 2) r = 8;
255 if (t == 0x0004 / 2) r = 16;
256 if (t == 0x0006 / 2) r = 32;
257 if (t == 0x0008 / 2) r = 4;
258 if (t == 0x000a / 2) r = 2;
259 if (t == 0x000c / 2) r = 1;
260 if (t == 0x000e / 2) r = 12;
261 if (t == 0x0010 / 2) r = 20;
262 if (t == 0x0012 / 2) r = 24;
263 if (t == 0x0014 / 2) r = 28;
267 static unsigned char *errfnc;
269 void jitCompA0001_checkType0(struct JitCompWork *w, int pxx, int typ, int ac)
271 if (typ <= 0) { w->err = JITC_ERR_BADTYPE; }
272 if (typ > 0x7f) { w->err = JITC_ERR_INTERNAL; }
273 jitCompA0001_movReg32EbpDisp(w, 0 /* EAX */, 256 + pxx * 32 + 4); /* MOV(EAX, [EBP+?]); */ /* typ */
274 jitCompPutByte3(w->dst, 0x83, 0xf8, typ & 0x7f); /* CMP(EAX, ?); */
275 jitCompPutByte2(w->dst, 0x0f, 0x85); /* JNE */
276 jitCompPutImm32(w, errfnc - (w->dst + 4));
280 void jitCompA0001_checkType(struct JitCompWork *w, int pxx, int typ, int ac)
282 // 将来的にはaliveやアクセス権チェックも入れる
284 jitCompA0001_checkType0(w, pxx, typ, ac);
288 void jitCompA0001_checkLimit(struct JitCompWork *w, int reg, int pxx)
290 jitCompPutByte1(w->dst, 0x3b); /* CMP(reg, [EBP+?]); */
291 jitCompA0001_85DispN(w, 256 + pxx * 32 + 8, reg); /* p0 */
292 jitCompPutByte2(w->dst, 0x0f, 0x82); /* JB */
293 jitCompPutImm32(w, errfnc - (w->dst + 4));
294 jitCompPutByte1(w->dst, 0x3b); /* CMP(reg, [EBP+?]); */
295 jitCompA0001_85DispN(w, 256 + pxx * 32 + 12, reg); /* p1 */
296 jitCompPutByte2(w->dst, 0x0f, 0x83); /* JAE */
297 jitCompPutImm32(w, errfnc - (w->dst + 4));
301 void func3c(char *ebp, int opt, int r1, int p1, int lenR, int lenP, int r0, int p0);
302 void func3d(char *ebp, int opt, int r1, int p1, int lenR, int lenP, int r0, int p0);
303 void funcf4(char *ebp, int pxx, int typ, int len);
304 void funcf5(char *ebp, int pxx, int typ, int len); // pxxはダミーで参照されない.
305 void funcf6(char *ebp, int pxx, int typ, int len);
306 void funcf7(char *ebp, int pxx, int typ, int len); // typとlenはダミーで参照されない.
307 // F5の場合、decoderが対応するalloc-freeを結びつけるのが簡単で、typやlenを指定必須にしてもフロントエンドコードに影響はない.
309 void errHndl(HOSECPU_RuntimeEnvironment *r);
313 * dst1 : 書き込みアドレスの最大値
314 * src : 現在の読み込みアドレス(ヘッダ部は飛ばしてある
315 * src1 : 読み込みアドレスの最大値
316 * src0 : 読み込みバイナリのアドレス
318 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)
320 /* 本来ならこのレイヤでは文法チェックしない */
322 struct JitCompWork w;
323 unsigned char *dst00 = dst, *enter0 = NULL, *tmp_ucp;
325 const unsigned char *oldsrc;
326 int timecount = 0, i, j = 0, lastlabel = -1, debugInfo0 = -1;
327 int reg0, reg1, reg2, cmp0reg = -1, cmp0lev = 0;
328 w.dst = w.dst0 = dst;
330 w.maxLabels = maxLabels;
332 if ((flags & JITC_NOSTARTUP) == 0) {
333 jitCompPutByte1(w.dst, 0x60); /* PUSHAD(); */
334 jitCompA000_loadRegCacheAll(&w); /* start-up */
335 jitCompA000_loadPRegCacheAll(&w);
337 if (level <= JITC_LV_SLOWER) {
338 jitCompPutByte2(w.dst, 0x31, 0xc0); /* XOR(EAX, EAX); */
339 jitCompA0001_movEbpDispReg32(&w, 2304 + 0, 0 /* EAX */); /* MOV(debugInfo0, EAX); */
340 jitCompPutByte1(w.dst, 0xb8); /* MOV(EAX, ?); */
341 jitCompPutImm32(&w, debugInfo1);
342 jitCompA0001_movEbpDispReg32(&w, 2304 + 4, 0 /* EAX */); /* MOV(debugInfo1, EAX); */
345 w.prefix = 0; //0x04 CND 命令で変更される
346 if (w.dst + 256 > dst1) { w.err = JITC_ERR_DST1; goto err_w; } // 書き込み領域が残り256バイト未満ならエラー
348 if (timecount >= 64) {
350 /* 未完成(timeoutチェックコードを入れる) */
352 prefix_continue: // CND命令実行後ここに戻る
356 if (w.prefix != 0) { w.err = JITC_ERR_PREFIX; goto err_w; } // 「条件付きでNOPを実行」するなんて、矛盾している!
362 * LB : ラベル設置命令。(6byte)
364 * ・timecount++し、timecountのチェックをする。
366 * ・割り込みがある場合、このタイミングで割り込みを発生させる。
373 if (enter0 == NULL && (src[6] == 0x3c /* 多数のレジスタをスタックに退避 */ || (src[6] == 0xfe/* REMARK */ && src[7] == 0x01 && src[9] == 0x3c))) { //beginFunc()中のLB
374 // LB命令の後に0x3C命令・・・beginFunc()
375 jitCompPutByte1(w.dst, 0xe9); // (x86) JMP rel32 : 次の命令との相対オフセットだけ相対ニアジャンプする
377 jitCompPutImm32(&w, 0); // 飛び相対座標が0 ・・・パイプラインのフラッシュ??
379 if (src[6] == 0x34) { // LBの次の命令がDATA ・・・DAT_SA0(label, typ32, length) ・・・メモリ確保命令
381 jitCompPutByte1(w.dst, 0xe9); // (x86) JMP rel32 : 次の命令との相対オフセットだけ相対ニアジャンプする
382 i = jitCompGetImm32(&src[7]); // type32 を取得
385 i = jitCompA000_convTyp(i);
387 if (i == 2 || i == 3) { j = 1; }
388 if (i == 4 || i == 5) { j = 2; }
389 if (i == 6 || i == 7) { j = 4; }
391 j *= jitCompGetImm32(&src[11]);
392 if (j <= 0) w.err = JITC_ERR_BADTYPE;
393 jitCompPutImm32(&w, j);
394 #if (jitCompA0001_OPTIMIZE_JMP != 0)
395 if (j <= 127 - jitCompA0001_OPTIMIZE_ALIGN) {
397 jitCompPutByte2(w.dst, 0xeb, j);
401 #if (jitCompA0001_OPTIMIZE_ALIGN != 0)
403 i = ((int)w.dst) & (jitCompA0001_OPTIMIZE_ALIGN - 1); /* ALIGNで割ったあまりを計算 */
405 i = jitCompA0001_OPTIMIZE_ALIGN - i;
406 if (i == 1) { jitCompPutByte1(w.dst, 0x90); j += i; } /* NOP(); */
407 if (i == 2) { jitCompPutByte2(w.dst, 0x89, 0xc0); j += i; } /* MOV(EAX, EAX); */
408 if (i == 3) { jitCompPutByte3(w.dst, 0x8d, 0x76, 0x00); j += i; } /* LEA(ESI, [ESI+0]); */
409 if (i == 4) { jitCompPutByte4(w.dst, 0x8d, 0x74, 0x26, 0x00); j += i; } /* LEA(ESI, [ESI*1+0]); */
410 if (i == 5) { jitCompPutByte1(w.dst, 0x0d); jitCompPutImm32(&w, 0); j += i; } /* OR(EAX, 0); */
411 if (i == 6) { jitCompPutByte2(w.dst, 0x8d, 0xb6); jitCompPutImm32(&w, 0); j += i; } /* LEA(ESI, [ESI+0]); */
412 if (i >= 7) { jitCompPutByte3(w.dst, 0x8d, 0xb4, 0x26); jitCompPutImm32(&w, 0); j += 7; } /* LEA(ESI, [ESI*1+0]); */
415 if (src[6] == 0x34) {
416 tmp_ucp[1] = j & 0xff;
417 if (*tmp_ucp == 0xe9) {
418 tmp_ucp[2] = (j >> 8) & 0xff;
419 tmp_ucp[3] = (j >> 16) & 0xff;
420 tmp_ucp[4] = (j >> 24) & 0xff;
423 if ((flags & JITC_PHASE1) == 0) {
424 i = jitCompGetLabelNum(&w, src + 2);
425 //printf("i=%06X %06X\n", i, src-src0);
426 if (label[i].opt != 0 && w.err == 0) { w.err = JITC_ERR_LABELREDEF; goto err_w; }
427 if (w.prefix != 0) { w.err = JITC_ERR_PREFIX; goto err_w; }
428 label[i].opt = src[1] + 1;
429 label[i].typ = 0; /* TYP_CODE */
431 label[i].p1 = w.dst + 1;
436 /* 未完成(timeoutチェックコードを入れる) */
439 case 0x02: /* LIMM */
442 * LIMM : 定数即値代入命令(6byte)
447 * ・reg3F は条件比較慣用句指定用&演算命令即値慣用句指定用。よってCND命令の直後では使用できない。
450 if (src[1] == 0x3f && w.prefix != 0) w.err = JITC_ERR_PREFIX; // CND命令の直後でR3Fを書き換えるなんて変だよね
452 #if (jitCompA0001_USE_R3F_IMM32 != 0)
453 if (src[1] == 0x3f) { // R3Fへの代入は例外敵に、 w.r3f を使用
454 w.r3f = jitCompGetImm32(src + 2);
458 i = jitCompGetImm32(src + 2); // 与えられた即値(第二引数)を取得
460 /* R00-R02 なら EBX, ECX, EDX 、それ以外なら EAX のレジスタIDを reg0 に代入 */
461 reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
463 #if (jitCompA0001_OPTIMIZE_MOV != 0)
465 jitCompPutByte2(w.dst, 0x31, 0xc0 | reg0 << 3 | reg0); /* XOR(reg0, reg0); */
466 jitCompA0001_movRxxEax(&w, src[1]);
471 /* reg0 のレジスタに対応したMOV命令を発行 */
472 jitCompPutByte1(w.dst, 0xb8 | reg0); /* MOV(reg0, ?); == 10111000b+wr imm32 */
473 jitCompPutImm32(&w, i);
475 if (reg0 == 0) // R03以降の、レジスタの内容をメモリ上に格納してエミュレートする場合
477 jitCompA0001_movRxxEax(&w, src[1]);
480 case 0x03: /* PLIMM */ /* 未完成(plsまで対応) */
483 * PLIMM : ラベル番号代入命令(6byte)
493 i = jitCompGetLabelNum(&w, src + 2); // Pxxに代入するラベルの番号(第二引数)
494 if ((flags & JITC_PHASE1) != 0 && w.err == 0) { // Phase 1であるならば
495 if (label[i].opt == 0) { w.err = JITC_ERR_LABELNODEF; goto err_w; } // 指定されたラベル番号は存在しない
496 if (src[1] != 0x3f && label[i].opt != 2) { w.err = JITC_ERR_LABELTYP; goto err_w; } //
497 if (src[1] == 0x3f && label[i].typ != 0) { w.err = JITC_ERR_LABELTYP; goto err_w; } // プログラムカウンタに TYP_CODEでない値は代入できない
499 if (src[1] == 0x3f) { // プログラムカウンタへの代入なら
500 if (w.prefix == 0) { // CND命令による条件付きでなければ、即座に移動
501 jitCompPutByte1(w.dst, 0xe9); /* JMP(?); */
510 * いま、dstの末端はJZ命令になっている。 0x0F 0x84 cd
514 w.dst[-1] = w.dst[-2] ^ 0xf1; /* 74->85, 75->84 */
520 if ((flags & JITC_PHASE1) != 0 || (((flags & JITC_PHASE1) == 0) && label[i].opt != 0)) // label番号iが確保されていれば (このif文は意味をなさない)
521 j = label[i].p - (w.dst + 4); // j はとび先の相対番地
522 jitCompPutImm32(&w, j); // JMP もしくは JZ 命令のアドレス部を記述
523 #if (jitCompA0001_OPTIMIZE_JMP != 0)
524 if (-128 - 3 <= j && j < 0) {
525 if (w.dst[-5] == 0xe9) {
528 jitCompPutByte1(w.dst, 0xeb); /* JMP(?); */
533 jitCompPutByte1(w.dst, w.dst[1] ^ 0xf0);
535 jitCompPutByte1(w.dst, j & 0xff);
539 else { // プログラムカウンタ以外への代入
541 // 代入先が P01, P02なら ESI, EDI,それ以外ならEAXを指定
542 reg0 = jitCompA000_selectPRegCache(src[1], 0 /* EAX */);
543 jitCompPutByte1(w.dst, 0xb8 | reg0); /* MOV(reg0, ?); */
544 jitCompPutImm32(&w, (int)label[i].p); // ラベルのパスを各レジスタに代入
546 // レジスタへの代入をメモリでエミュレーションする場合は、スタックに積む。
548 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32, 0); /* MOV([EBP+?], EAX); */
550 if (level < JITC_LV_FASTEST) {
551 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 8, reg0); /* MOV([EBP+?], reg0); */ /* p0 */
552 jitCompPutByte1(w.dst, 0xb8); /* MOV(EAX, ?); */
553 jitCompPutImm32(&w, label[i].typ);
554 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 4, 0); /* MOV([EBP+?], EAX); */ /* typ */
555 jitCompPutByte1(w.dst, 0xb8); /* MOV(EAX, ?); */
556 jitCompPutImm32(&w, (int)label[i].p1);
557 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 12, 0); /* MOV([EBP+?], EAX); */ /* p1 */
558 jitCompPutByte2(w.dst, 0x31, 0xc0); /* XOR(EAX, EAX); */
559 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 16, 0); /* MOV([EBP+?], EAX); */ /* liveSign */
560 jitCompA0001_movReg32EbpDisp(&w, 0, 2320); /* MOV(EAX, ptrCtrl); */
561 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 20, 0); /* MOV([EBP+?], EAX); */ /* pls */
566 case 0x04: /* CND (prefix) */
570 * 与えられたRxxの最下位ビットが1であれば後続の命令を実行、そうでなければ飛ばす。
573 if (src[1] >= 0x40) w.err = JITC_ERR_REGNUM; // R00-R3F 以外のレジスタは比較対象にできない
575 // 比較対象のレジスタがメモリ上にあれば-1, それ以外なら適切なレジスタ番号を返す
576 reg0 = jitCompA000_selectRegCache(src[1], -1 /* mem */);
579 if (reg0 < 0) { //比較対象のレジスタはメモリ上にある
580 jitCompPutByte1(w.dst, 0xf7); /* TEST([EBP+?],1); */
581 jitCompA0001_85DispN(&w, src[1] * 4, 0);
584 jitCompPutByte2(w.dst, 0xf7, 0xc0 | reg0); /* TEST(reg0,1); */
586 jitCompPutImm32(&w, 1);
589 jitCompPutByte2(w.dst, 0x74, 0x00); /* JZ($+2) */
591 if (w.err != 0) goto err_w;
593 w.prefix = 1; // プリフィックスをセット
595 goto prefix_continue;
597 case 0x08: /* LMEM */ /* 完成 */
598 i = jitCompGetImm32(src + 2);
599 if (i == 0x0001) w.err = JITC_ERR_BADTYPE;
600 if (level < JITC_LV_FASTER) {
601 jitCompA0001_checkType(&w, src[6], i, 0); // read
604 reg0 = jitCompA000_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 i = jitCompA000_convTyp(jitCompGetImm32(src + 2));
617 jitCompPutByte3(w.dst, 0x0f, 0xbe, reg0 << 3 | reg1); /* MOVSX(reg0,BYTE [reg1]); */
620 jitCompPutByte3(w.dst, 0x0f, 0xb6, reg0 << 3 | reg1); /* MOVZX(reg0,BYTE [reg1]); */
623 jitCompPutByte3(w.dst, 0x0f, 0xbf, reg0 << 3 | reg1); /* MOVSX(reg0,WORD [reg1]); */
626 jitCompPutByte3(w.dst, 0x0f, 0xb7, reg0 << 3 | reg1); /* MOVZX(reg0,WORD [reg1]); */
630 jitCompPutByte2(w.dst, 0x8b, reg0 << 3 | reg1); /* MOV(reg0, [reg1]); */
633 w.err = JITC_ERR_BADTYPE;
635 if (reg0 == 0 /* EAX */)
636 jitCompA0001_movRxxEax(&w, src[1]);
637 if (reg1 == 2 /* EDX */)
638 jitCompA000_loadRegCacheEdx(&w);
641 case 0x09: /* SMEM */ /* 完成 */
642 i = jitCompGetImm32(src + 2);
643 if (i == 0x0001) w.err = JITC_ERR_BADTYPE;
644 if (level < JITC_LV_FASTER) {
645 jitCompA0001_checkType(&w, src[6], i, 1); // write
648 reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
649 reg1 = jitCompA000_selectPRegCache(src[6], 2 /* EDX */);
650 if (reg0 != 0 /* EAX */ && reg1 == 2 /* EDX */)
652 if (reg1 == 2 /* EDX */)
653 jitCompA000_storeRegCacheEdx(&w);
654 if (reg1 <= 3 /* EAX, EDX */)
655 jitCompA0001_movReg32EbpDisp(&w, reg1, 256 + src[6] * 32 + 0); /* MOV(reg1, [EBP+?]); */
656 if (level < JITC_LV_FASTER)
657 jitCompA0001_checkLimit(&w, reg1, src[6]);
658 if (reg0 == 0 /* EAX */)
659 jitCompA0001_movEaxRxx(&w, src[1]);
661 i = jitCompA000_convTyp(jitCompGetImm32(src + 2));
665 jitCompPutByte2(w.dst, 0x88, reg0 << 3 | reg1); /* MOV([reg1], BYTE(reg0)); */
669 jitCompPutByte3(w.dst, 0x66, 0x89, reg0 << 3 | reg1); /* MOV([reg1], WORD(reg0)); */
673 jitCompPutByte2(w.dst, 0x89, reg0 << 3 | reg1); /* MOV([reg1], reg0); */
676 w.err = JITC_ERR_BADTYPE;
678 if (reg1 == 2 /* EDX */)
679 jitCompA000_loadRegCacheEdx(&w);
682 case 0x0a: /* PLMEM */ /* 完成 */
683 i = jitCompGetImm32(src + 2);
684 if (i != 0x0001) w.err = JITC_ERR_BADTYPE;
685 if (level < JITC_LV_FASTER) {
686 jitCompA0001_checkType(&w, src[6], i, 0); // read
689 reg0 = jitCompA000_selectPRegCache(src[1], 0 /* EAX */);
690 reg1 = jitCompA000_selectPRegCache(src[6], 2 /* EDX */);
691 // if (reg0 != 0 /* EAX */ && reg1 == 2 /* EDX */) /* これをやってはいけない!(by K, 2013.08.02) */
692 // reg1 = 0; /* EAX */
693 if (reg0 == reg1 && reg0 != 0) { // bugfix: hinted by yao, 2013.09.14. thanks!
694 jitCompA000_storePRegCacheAll(&w);
697 if (reg1 == 2 /* EDX */)
698 jitCompA000_storeRegCacheEdx(&w);
699 if (reg1 <= 3 /* EAX, EDX */)
700 jitCompA0001_movReg32EbpDisp(&w, reg1, 256 + src[6] * 32 + 0); /* MOV(reg1, [EBP+?]); */
701 if (level < JITC_LV_FASTER)
702 jitCompA0001_checkLimit(&w, reg1, src[6]);
703 jitCompPutByte2(w.dst, 0x8b, reg0 << 3 | reg1); /* MOV(reg0, [reg1]); */
704 if (reg0 == 0 /* EAX */)
705 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 0, 0); /* MOV([EBP+?], EAX); */
706 for (i = 4; i < 32; i += 4) {
707 jitCompPutByte3(w.dst, 0x8b, 0x40 | reg1, i); /* MOV(EAX, [reg1+?]); */
708 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + i, 0 /* EAX */); /* MOV([EBP+?], EAX); */
710 if (reg1 == 2 /* EDX */)
711 jitCompA000_loadRegCacheEdx(&w);
714 case 0x0b: /* PSMEM */ /* 完成 */
715 i = jitCompGetImm32(src + 2);
716 if (i != 0x0001) w.err = JITC_ERR_BADTYPE;
717 if (level < JITC_LV_FASTER) {
718 jitCompA0001_checkType(&w, src[6], i, 1); // write
721 reg0 = jitCompA000_selectPRegCache(src[1], 0 /* EAX */);
722 reg1 = jitCompA000_selectPRegCache(src[6], 2 /* EDX */);
723 // if (reg0 != 0 /* EAX */ && reg1 == 2 /* EDX */) /* これをやってはいけない!(by K, 2013.08.02) */
724 // reg1 = 0; /* EAX */
725 if (reg1 == 2 /* EDX */)
726 jitCompA000_storeRegCacheEdx(&w);
727 if (reg1 <= 3 /* EAX, EDX */)
728 jitCompA0001_movReg32EbpDisp(&w, reg1, 256 + src[6] * 32 + 0); /* MOV(reg1, [EBP+?]); */
729 if (level < JITC_LV_FASTER)
730 jitCompA0001_checkLimit(&w, reg1, src[6]);
731 if (reg0 == 0 /* EAX */)
732 jitCompA0001_movReg32EbpDisp(&w, reg0, 256 + src[1] * 32 + 0); /* MOV(reg0, [EBP+?]); */
733 jitCompPutByte2(w.dst, 0x89, reg0 << 3 | reg1); /* MOV([reg1], reg0); */
734 for (i = 4; i < 32; i += 4) {
735 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[1] * 32 + i); /* MOV(EAX, [EBP+?]); */
736 jitCompPutByte3(w.dst, 0x89, 0x40 | reg1, i); /* MOV([reg1+?], EAX); */
738 if (reg1 == 2 /* EDX */)
739 jitCompA000_loadRegCacheEdx(&w);
742 case 0x0e: /* PADD */ /* 完成 */
743 if (level < JITC_LV_FASTER) {
744 jitCompA0001_checkType0(&w, src[6], jitCompGetImm32(src + 2), 2); // other, aliveテストはとりあえずしない.
747 reg0 = jitCompA000_selectPRegCache(src[1], 0 /* EAX */);
748 reg1 = jitCompA000_selectPRegCache(src[6], -1 /* mem */);
749 if (reg1 < 0 /* mem */)
750 jitCompA0001_movReg32EbpDisp(&w, reg0, 256 + src[6] * 32 + 0); /* MOV(reg0, [EBP+?]); */
751 if (reg1 >= 0 && reg0 != reg1) {
752 jitCompPutByte2(w.dst, 0x89, 0xc0 | reg1 << 3 | reg0); /* MOV(reg0, reg1); */
754 i = jitCompGetImm32(src + 2);
759 i = jitCompA000_convTyp(i);
760 if (0x0002 <= i && i <= 0x0007)
761 j = (i - 0x0002) >> 1;
763 if (j < 0) { w.err = JITC_ERR_BADTYPE; goto err_w; }
764 #if (jitCompA0001_USE_R3F_IMM32 != 0)
765 if (src[7] == 0x3f) {
767 #if (jitCompA0001_USE_R3F_IMM8 != 0)
768 if (-0x80 <= j && j <= 0x7f) {
769 #if (jitCompA0001_USE_R3F_INCDEC != 0)
770 if (j == 1) { jitCompPutByte1(w.dst, 0x40 | reg0); goto padd1; } /* INC */
771 if (j == -1) { jitCompPutByte1(w.dst, 0x48 | reg0); goto padd1; } /* DEC */
773 jitCompPutByte3(w.dst, 0x83, 0xc0 | reg0, j & 0xff); /* ADD(reg0, im8); */
778 jitCompPutByte1(w.dst, 0x05); /* ADD(reg0, ?); */
781 jitCompPutByte2(w.dst, 0x81, 0xc0 | reg0); /* ADD(reg0, ?); */
783 jitCompPutImm32(&w, j);
787 if (src[7] >= 0x40) w.err = JITC_ERR_REGNUM;
789 reg1 = jitCompA000_selectRegCache(src[7], -1 /* mem */);
791 jitCompPutByte2(w.dst, 0x01, 0xc0 | reg1 << 3 | reg0); /* ADD(reg0, reg1); */
794 jitCompPutByte1(w.dst, 0x03); /* ADD(reg0, [EBP+?]); */
795 jitCompA0001_85DispN(&w, src[7] * 4, reg0);
799 reg1 = jitCompA000_selectRegCache(src[7], -1 /* mem */);
801 jitCompA000_storeRegCacheEdx(&w);
803 jitCompA0001_movReg32EbpDisp(&w, reg2, src[7] * 4); /* MOV(reg2, [EBP+?]); */
804 if (reg1 >= 0 && reg1 != reg2) {
805 jitCompPutByte2(w.dst, 0x89, 0xc0 | reg1 << 3 | reg2); /* MOV(reg2, reg1); */
807 jitCompPutByte3(w.dst, 0xc1, 0xe0 | reg2, j); /* SHL(reg2, ?); */
808 jitCompPutByte2(w.dst, 0x01, 0xc0 | reg2 << 3 | reg0); /* ADD(reg0, reg2); */
809 jitCompA000_loadRegCacheEdx(&w);
811 #if (jitCompA0001_USE_R3F_IMM32 != 0)
814 if (reg0 == 0 /* EAX */)
815 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 0, reg0); /* MOV([EBP+?], reg0); */
816 if (src[1] != src[6]) {
817 for (i = 4; i < 32; i += 4) {
818 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[6] * 32 + i); /* MOV(EAX, [EBP+?]); */
819 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + i, 0 /* EAX */); /* MOV([EBP+?], EAX); */
825 case 0x0f: /* PDIF */ /* 未完成 */
826 reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
827 jitCompA000_storePRegCacheAll(&w); // 手抜き.
828 jitCompA0001_checkCompPtr(&w, src[6], src[7]);
829 jitCompA0001_movReg32EbpDisp(&w, reg0, 256 + src[6] * 32 + 0); /* MOV(reg0, [EBP+?]); */
830 jitCompPutByte1(w.dst, 0x2b); /* SUB(EAX, [EBP+?]); */
831 jitCompA0001_85DispN(&w, 256 + src[7] * 32 + 0, reg0);
832 i = jitCompA000_convTyp(jitCompGetImm32(src + 2));
834 if (0x0002 <= i && i <= 0x0007)
835 j = (i - 0x0002) >> 1;
836 if (j < 0) { w.err = JITC_ERR_BADTYPE; goto err_w; }
838 jitCompPutByte3(w.dst, 0xc1, 0xf8 | reg0, j); /* SAR(reg0,?); */
840 if (reg0 == 0 /* EAX */)
841 jitCompA0001_movRxxEax(&w, src[1]);
842 cmp0reg = src[1]; cmp0lev = 1;
851 if (src[1] >= 0x3f) w.err = JITC_ERR_REGNUM;
852 reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
853 reg1 = jitCompA000_selectRegCache(src[2], -1 /* mem */);
854 #if (jitCompA0001_USE_R3F_IMM32 != 0)
855 if (src[2] == 0x3f) { // SUBのみ該当.
856 if (*src != 0x15) w.err = JITC_ERR_REGNUM;
857 reg2 = jitCompA000_selectRegCache(src[3], -1 /* mem */);
859 jitCompA000_storeRegCacheAll(&w);
860 jitCompPutByte1(w.dst, 0xb8 | reg0); /* MOV(reg0, ?); */
861 jitCompPutImm32(&w, w.r3f);
862 jitCompPutByte1(w.dst, 0x2b);
863 jitCompA0001_85DispN(&w, src[3] * 4, reg0);
865 jitCompA0001_movRxxEax(&w, src[1]);
870 jitCompA0001_movReg32EbpDisp(&w, reg0, src[2] * 4); /* MOV(reg0, [EBP+?]); */
872 if (reg1 >= 0 && reg0 != reg1) {
873 jitCompPutByte2(w.dst, 0x89, 0xc0 | reg1 << 3 | reg0); /* MOV(reg0, reg1); */
875 if (!(src[0] == 0x10 && src[3] == 0xff)) { // bugfix: hinted by Iris, 2013.06.26. thanks!
883 if (!(src[0] == 0x10 && src[3] == 0xff)) {
884 #if (jitCompA0001_USE_R3F_IMM32 != 0)
885 if (src[3] == 0x3f) {
886 if (*src == 0x16 && w.r3f == -1) {
887 jitCompPutByte2(w.dst, 0xf7, 0xd8 | reg0); /* NEG(reg0); */
889 jitCompA0001_movRxxEax(&w, src[1]);
892 #if (jitCompA0001_USE_R3F_INCDEC != 0)
893 if ((*src == 0x14 && w.r3f == 1) || (*src == 0x15 && w.r3f == -1)) {
894 jitCompPutByte1(w.dst, 0x40 | reg0); /* INC(reg0); */
896 jitCompA0001_movRxxEax(&w, src[1]);
899 if ((*src == 0x15 && w.r3f == 1) || (*src == 0x14 && w.r3f == -1)) {
900 jitCompPutByte1(w.dst, 0x48 | reg0); /* DEC(reg0); */
902 jitCompA0001_movRxxEax(&w, src[1]);
906 #if (jitCompA0001_USE_R3F_IMM8 != 0)
907 if (-0x80 <= w.r3f && w.r3f <= 0x7f) {
909 static unsigned char basic_op_table_im8[] = { 0xc8, 0xf0, 0xe0, 0, 0xc0, 0xe8 };
910 jitCompPutByte3(w.dst, 0x83, basic_op_table_im8[*src - 0x10] | reg0, w.r3f & 0xff);
913 jitCompPutByte3(w.dst, 0x6b, 0xc0 | reg0 << 3 | reg0, w.r3f & 0xff);
916 jitCompA0001_movRxxEax(&w, src[1]);
920 if (reg0 == 0 /* EAX */) {
921 static unsigned char basic_op_table_im32_eax[] = { 0x0d, 0x35, 0x25, 0, 0x05, 0x2d, 0xc0 };
922 if (*src == 0x16) { jitCompPutByte1(w.dst, 0x69); }
923 jitCompPutByte1(w.dst, basic_op_table_im32_eax[*src - 0x10]);
927 static unsigned char basic_op_table_im32_reg[] = { 0xc8, 0xf0, 0xe0, 0, 0xc0, 0xe8 };
928 jitCompPutByte2(w.dst, 0x81, basic_op_table_im32_reg[*src - 0x10] | reg0);
931 jitCompPutByte2(w.dst, 0x69, 0xc0 | reg0 << 3 | reg0);
934 jitCompPutImm32(&w, w.r3f);
936 jitCompA0001_movRxxEax(&w, src[1]);
940 reg1 = jitCompA000_selectRegCache(src[3], -1 /* mem */);
941 if (src[3] >= 0x40) w.err = JITC_ERR_REGNUM;
944 static unsigned char basic_op_table_rr[] = { 0x09, 0x31, 0x21, 0, 0x01, 0x29 }; /* op(reg,reg); */
945 jitCompPutByte2(w.dst, basic_op_table_rr[*src - 0x10], 0xc0 | reg1 << 3 | reg0);
948 static unsigned char basic_op_table_rm[] = { 0x0b, 0x33, 0x23, 0, 0x03, 0x2b, 0xaf }; /* op(reg,mem); */
949 jitCompPutByte1(w.dst, basic_op_table_rm[*src - 0x10]);
950 jitCompA0001_85DispN(&w, src[3] * 4, reg0);
955 jitCompPutByte3(w.dst, 0x0f, 0xaf, 0xc0 | reg0 << 3 | reg1);
958 jitCompPutByte2(w.dst, 0x0f, 0xaf);
959 jitCompA0001_85DispN(&w, src[3] * 4, reg0);
964 jitCompA0001_movRxxEax(&w, src[1]);
969 if (src[1] >= 0x3f) w.err = JITC_ERR_REGNUM;
970 if (src[3] >= 0x40) w.err = JITC_ERR_REGNUM;
971 #if (jitCompA0001_USE_R3F_IMM32 != 0)
972 if (src[3] == 0x3f) {
973 reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
974 reg1 = jitCompA000_selectRegCache(src[2], -1 /* mem */);
975 if (src[1] >= 0x3f) w.err = JITC_ERR_REGNUM;
977 jitCompA0001_movReg32EbpDisp(&w, reg0, src[2] * 4); /* MOV(reg1, [EBP+?]); */
980 jitCompPutByte2(w.dst, 0x89, 0xc0 | reg1 << 3 | reg0); /* MOV(reg0, reg1); */
983 if (*src == 0x18) { jitCompPutByte3(w.dst, 0xc1, 0xe0 | reg0, w.r3f); } /* SHL(reg0, im8); */
984 if (*src == 0x19) { jitCompPutByte3(w.dst, 0xc1, 0xf8 | reg0, w.r3f); } /* SAR(reg0, im8); */
985 if (reg0 == 0 /* EAX */)
986 jitCompA0001_movRxxEax(&w, src[1]);
992 jitCompA000_storeRegCacheAll(&w); // 手抜き.
993 jitCompA0001_movReg32EbpDisp(&w, 1 /* ECX */, src[3] * 4); /* MOV(ECX, [EBP+?]); */
994 #if (jitCompA0001_USE_R3F_IMM32 != 0)
995 if (src[2] == 0x3f) {
996 jitCompPutByte1(w.dst, 0xb8); /* MOV(EAX, ?); */
997 jitCompPutImm32(&w, w.r3f);
1000 jitCompA0001_movEaxRxx(&w, src[2]);
1003 jitCompA0001_movEaxRxx(&w, src[2]);
1005 if (*src == 0x18) { jitCompPutByte2(w.dst, 0xd3, 0xe0); } /* SHL(EAX, CL); */
1006 if (*src == 0x19) { jitCompPutByte2(w.dst, 0xd3, 0xf8); } /* SAR(EAX, CL); */
1007 jitCompA0001_movRxxEax(&w, src[1]);
1008 jitCompA000_loadRegCacheAll(&w); // 手抜き.
1013 case 0x1a: /* DIV */
1014 case 0x1b: /* MOD */
1015 if (src[1] >= 0x3f) w.err = JITC_ERR_REGNUM;
1016 if (src[2] >= 0x40) w.err = JITC_ERR_REGNUM;
1017 if (src[3] >= 0x40) w.err = JITC_ERR_REGNUM;
1018 jitCompA000_storeRegCacheAll(&w); // 手抜き.
1019 #if (jitCompA0001_USE_R3F_IMM32 != 0)
1020 if (src[3] == 0x3f) {
1021 jitCompPutByte1(w.dst, 0xb8 | 1); /* MOV(ECX, ?); */
1022 jitCompPutImm32(&w, w.r3f);
1025 jitCompA0001_movReg32EbpDisp(&w, 1 /* ECX */, src[3] * 4); /* MOV(ECX, [EBP+?]); */
1027 if (src[2] == 0x3f) {
1028 jitCompPutByte1(w.dst, 0xb8 | 0); /* MOV(EAX, ?); */
1029 jitCompPutImm32(&w, w.r3f);
1032 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, src[2] * 4); /* MOV(EAX, [EBP+?]); */
1035 jitCompA0001_movReg32EbpDisp(&w, 1 /* ECX */, src[3] * 4); /* MOV(ECX, [EBP+?]); */
1036 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, src[2] * 4); /* MOV(EAX, [EBP+?]); */
1038 jitCompPutByte1(w.dst, 0x99); /* CDQ(); */
1039 /* ECXがゼロではないことを確認すべき */
1040 jitCompPutByte2(w.dst, 0xf7, 0xf9); /* IDIV(ECX); */
1041 if (*src == 0x1a) { jitCompA0001_movEbpDispReg32(&w, src[1] * 4, 0 /* EAX */); }
1042 if (*src == 0x1b) { jitCompA0001_movEbpDispReg32(&w, src[1] * 4, 2 /* EDX */); }
1043 jitCompA000_loadRegCacheAll(&w); // 手抜き.
1047 case 0x1c: /* PLMT0 */
1048 case 0x1d: /* PLMT1 */
1049 if (src[1] >= 0x40 || src[2] >= 0x40) w.err = JITC_ERR_PREGNUM;
1050 if (level < JITC_LV_FASTEST) {
1052 if (level < JITC_LV_FASTER) {
1053 // typ が一致していることを確認.
1054 // plsとliveSignが一致していることを確認.
1056 // preg1はp0 <= p <= p1 を満たしているか?.
1057 // 新しいp0/p1は古いp0〜p1に適合しているか?.
1062 case 0x1e: /* PCP */ /* 未完成(p1まで完成) */
1063 if (src[1] >= 0x40 || src[2] >= 0x40) w.err = JITC_ERR_PREGNUM;
1064 if (src[2] == 0x3f) w.err = JITC_ERR_PREGNUM;
1065 if (src[1] != 0x3f) {
1066 /* src[2] == 0xff の場合に対応できてない */
1067 jitCompA000_storePRegCacheAll(&w); // 手抜き.
1068 for (i = 0; i < 32; i += 4) {
1069 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[2] * 32 + i); /* MOV(EAX, [EBP+?]); */
1070 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + i, 0 /* EAX */); /* MOV([EBP+?], EAX); */
1072 jitCompA000_loadPRegCacheAll(&w); // 手抜き.
1075 if (level < JITC_LV_FASTER) {
1076 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[2] * 32 + 4); /* MOV(EAX, [EBP+?]); */ /* typ */
1077 jitCompPutByte3(w.dst, 0x83, 0xf8, 0); /* CMP(EAX, 0); */
1078 jitCompPutByte2(w.dst, 0x0f, 0x85); /* JNE */
1079 jitCompPutImm32(&w, errfnc - (w.dst + 4));
1080 /* セキュリティチェックが足りてない!(aliveとか) */
1083 jitCompA000_storePRegCacheAll(&w); // 手抜き.
1084 jitCompA0001_movReg32EbpDisp(&w, reg0, 256 + src[2] * 32 + 0); /* MOV(EAX, [EBP+?]); */
1085 if (level < JITC_LV_FASTER) {
1086 jitCompPutByte1(w.dst, 0x3b); /* CMP(reg0, [EBP+?]); */
1087 jitCompA0001_85DispN(&w, 256 + src[2] * 32 + 8, reg0); /* p0 */
1088 jitCompPutByte2(w.dst, 0x0f, 0x85); /* JNE */
1089 jitCompPutImm32(&w, errfnc - (w.dst + 4));
1091 jitCompPutByte2(w.dst, 0xff, 0xe0); /* JMP(EAX); */
1095 case 0x1f: /* PCST */
1096 if (jitCompGetImm32(src + 2) == 0) {
1097 if (level < JITC_LV_FASTER)
1098 jitCompA0001_checkType0(&w, src[6], jitCompGetImm32(src + 7), 2);
1099 jitCompA000_storePRegCacheAll(&w); // 手抜き.
1100 for (i = 0; i < 32 - 4; i += 4) {
1101 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[6] * 32 + i); /* MOV(EAX, [EBP+?]); */
1103 jitCompPutByte1(w.dst, 0x0d); /* OR(EAX, ?); */
1104 jitCompPutImm32(&w, 0x80000000);
1106 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + i, 0 /* EAX */); /* MOV([EBP+?], EAX); */
1108 jitCompPutByte1(w.dst, 0xb8); /* MOV(EAX, ?); */
1109 jitCompPutImm32(&w, debugInfo1);
1110 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 28, 0 /* EAX */); /* MOV([EBP+?], EAX); */
1111 jitCompA000_loadPRegCacheAll(&w); // 手抜き.
1115 if (jitCompGetImm32(src + 7) == 0) {
1116 jitCompA000_storePRegCacheAll(&w); // 手抜き.
1117 for (i = 0; i < 32 - 4; i += 4) {
1118 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[6] * 32 + i); /* MOV(EAX, [EBP+?]); */
1120 jitCompPutByte1(w.dst, 0x25); /* AND(EAX, ?); */
1121 jitCompPutImm32(&w, 0x7fffffff);
1123 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + i, 0 /* EAX */); /* MOV([EBP+?], EAX); */
1125 if (level < JITC_LV_FASTER) {
1126 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[6] * 32 + 28); /* MOV(EAX, [EBP+?]); */
1127 jitCompPutByte1(w.dst, 0x3d); /* CMP(EAX, ?); */
1128 jitCompPutImm32(&w, debugInfo1);
1129 jitCompPutByte2(w.dst, 0x74, 8); /* JE */
1130 jitCompPutByte2(w.dst, 0x31, 0xc0); /* XOR(EAX, EAX); (2) */
1131 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 0, 0 /* EAX */); /* MOV([EBP+?], EAX); (1+1+4) */
1133 jitCompA000_loadPRegCacheAll(&w); // 手抜き.
1137 w.err = JITC_ERR_OPECODE;
1140 case 0x20: /* CMPE */
1141 case 0x21: /* CMPNE */
1142 case 0x22: /* CMPL */
1143 case 0x23: /* CMPGE */
1144 case 0x24: /* CMPLE */
1145 case 0x25: /* CMPG */
1146 case 0x26: /* TSTZ */
1147 case 0x27: /* TSTNZ */
1148 reg0 = jitCompA000_selectRegCache(src[2], 0 /* EAX */);
1149 reg1 = jitCompA000_selectRegCache(src[3], -1 /* mem */);
1150 if (src[1] == 0x3f) {
1152 if (w.prefix != 0) { w.err = JITC_ERR_PREFIX; goto err_w; }
1153 if (src[4] != 0x04 || src[5] != 0x3f || src[6] != 0x03 || src[7] != 0x3f) {
1154 w.err = JITC_ERR_IDIOM; goto err_w;
1158 jitCompA0001_movEaxRxx(&w, src[2]);
1159 #if (jitCompA0001_USE_R3F_IMM32 != 0)
1160 if (src[3] == 0x3f) {
1161 #if (jitCompA0001_OPTIMIZE_CMP != 0)
1162 if ((*src <= 0x25 && w.r3f == 0) || (*src >= 0x26 && w.r3f == -1)) {
1164 if (cmp0reg == src[2]) {
1165 if (cmp0lev >= 1 && (src[0] == 0x20 || src[0] == 0x21 || src[0] == 0x26 || src[0] == 0x27))
1167 if (cmp0lev >= 2 && (src[0] == 0x22 || src[0] == 0x23 || src[0] == 0x24 || src[0] == 0x25))
1171 jitCompPutByte2(w.dst, 0x85, 0xc0 | reg0 << 3 | reg0); /* TEST(reg0, reg0); */
1178 #if (jitCompA0001_USE_R3F_IMM8 != 0)
1179 if (-0x80 <= w.r3f && w.r3f <= 0x7f && *src <= 0x25) {
1180 jitCompPutByte3(w.dst, 0x83, 0xf8 | reg0, w.r3f);
1185 if (*src <= 0x25) { jitCompPutByte1(w.dst, 0x3d); }
1186 if (*src >= 0x26) { jitCompPutByte1(w.dst, 0xa9); }
1189 if (*src <= 0x25) { jitCompPutByte2(w.dst, 0x81, 0xf8 | reg0); }
1190 if (*src >= 0x26) { jitCompPutByte2(w.dst, 0xf7, 0xc0 | reg0); }
1192 jitCompPutImm32(&w, w.r3f);
1196 if (src[3] >= 0x40) w.err = JITC_ERR_PREGNUM;
1198 if (*src <= 0x25) { jitCompPutByte2(w.dst, 0x39, 0xc0 | reg1 << 3 | reg0); }
1199 if (*src >= 0x26) { jitCompPutByte2(w.dst, 0x85, 0xc0 | reg1 << 3 | reg0); }
1202 if (*src <= 0x25) { jitCompPutByte1(w.dst, 0x3b); }
1203 if (*src >= 0x26) { jitCompPutByte1(w.dst, 0x85); }
1204 jitCompA0001_85DispN(&w, src[3] * 4, reg0);
1207 if (w.err != 0) goto err_w;
1208 static unsigned char cmpcc_table0[] = {
1209 0x04, 0x05, 0x0c, 0x0d, 0x0e, 0x0f, 0x04, 0x05, /* CMPcc, TSTcc */
1210 0x04, 0x05, 0x02, 0x03, 0x06, 0x07 /* PCMPcc */
1212 #if (jitCompA0001_USE_R3F_CMPJMP != 0)
1213 if (src[1] == 0x3f) {
1215 jitCompPutByte2(w.dst, 0x0f, 0x80 | cmpcc_table0[*src - 0x20]);
1217 i = jitCompGetLabelNum(&w, src + 2);
1218 if ((flags & JITC_PHASE1) != 0 && w.err != 0) {
1219 if (label[i].opt == 0) { w.err = JITC_ERR_LABELNODEF; goto err_w; }
1220 // if (label[i].typ != 1) { w.err = JITC_ERR_LABELTYP; goto err_w; }
1223 if ((flags & JITC_PHASE1) != 0 || (((flags & JITC_PHASE1) == 0) && label[i].opt != 0))
1224 j = label[i].p - (w.dst + 4);
1225 jitCompPutImm32(&w, j);
1226 #if (jitCompA0001_OPTIMIZE_JMP != 0)
1227 if (-128 - 4 <= j && j < 0) {
1230 jitCompPutByte2(w.dst, w.dst[1] ^ 0xf0, j & 0xff);
1234 if (w.err != 0) goto err_w;
1239 reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
1240 jitCompPutByte3(w.dst, 0x0f, 0x90 | cmpcc_table0[*src - 0x20], 0xc0 | reg0); /* SETcc(BYTE(reg0)); */
1241 jitCompPutByte3(w.dst, 0x0f, 0xb6, 0xc0 | reg0 << 3 | reg0); /* MOVZX(reg0, BYTE(reg0)); */
1242 jitCompPutByte2(w.dst, 0xf7, 0xd8 | reg0); /* NEG(reg0); */
1244 jitCompA0001_movRxxEax(&w, src[1]);
1249 case 0x28: /* PCMPE */
1250 case 0x29: /* PCMPNE */
1251 case 0x2a: /* PCMPL */
1252 case 0x2b: /* PCMPGE */
1253 case 0x2c: /* PCMPLE */
1254 case 0x2d: /* PCMPG */
1255 if (src[1] == 0x3f) {
1257 if (w.prefix != 0) { w.err = JITC_ERR_PREFIX; goto err_w; }
1258 if (src[4] != 0x04 || src[5] != 0x3f || src[6] != 0x03 || src[7] != 0x3f) {
1259 w.err = JITC_ERR_IDIOM; goto err_w;
1262 if (src[2] >= 0x40) w.err = JITC_ERR_PREGNUM;
1263 jitCompA000_storePRegCacheAll(&w); // 手抜き.
1265 jitCompA0001_checkCompPtr(&w, src[2], src[3]);
1266 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[2] * 32 + 0); /* MOV(EAX, [EBP+?]); */
1267 if (src[3] != 0xff) {
1268 jitCompPutByte1(w.dst, 0x3b); /* CMP(EAX, [EBP+?]); */
1269 jitCompA0001_85DispN(&w, 256 + src[3] * 32 + 0, 0);
1272 /* ヌルポインタとの比較はこれでいいのか?たぶんよくない */
1273 jitCompPutByte3(w.dst, 0x83, 0xf8, 0x00); /* CMP(EAX, 0); */
1278 case 0x30: /* talloc(old:F4) */
1279 case 0x31: /* tfree(old:F5) */
1280 case 0x32: /* malloc(old:F6) */
1281 case 0x33: /* mfree(old:F7) */
1282 jitCompA000_storeRegCacheAll(&w); // 手抜き.
1283 jitCompA000_storePRegCacheAll(&w); // 手抜き.
1284 jitCompPutByte2(w.dst, 0x6a, src[3]); /* PUSH(?); */
1285 jitCompPutByte2(w.dst, 0x6a, src[2]); /* PUSH(?); */
1286 jitCompPutByte2(w.dst, 0x6a, src[1]); /* PUSH(?); */
1287 jitCompPutByte1(w.dst, 0x55); /* PUSH(EBP); */
1288 jitCompPutByte1(w.dst, 0xe8);
1289 if (*src == 0x30) j = ((unsigned char *)&funcf4) - (w.dst + 4);
1290 if (*src == 0x31) j = ((unsigned char *)&funcf5) - (w.dst + 4);
1291 if (*src == 0x32) j = ((unsigned char *)&funcf6) - (w.dst + 4);
1292 if (*src == 0x33) j = ((unsigned char *)&funcf7) - (w.dst + 4);
1293 jitCompPutImm32(&w, j);
1294 jitCompPutByte3(w.dst, 0x83, 0xc4, 0x10); /* ADD(ESP,16); */
1295 jitCompA000_loadRegCacheAll(&w); // 手抜き.
1296 jitCompA000_loadPRegCacheAll(&w); // 手抜き.
1300 case 0x34: /* data (暫定) */
1302 if (w.prefix != 0) { w.err = JITC_ERR_PREFIX; goto err_w; }
1303 int k = jitCompGetImm32(&src[1]), tmpData, bitCount, dataWidth = jitCompA000_dataWidth(k);
1304 if (lastlabel >= 0 && label[lastlabel].typ == 0)
1305 label[lastlabel].typ = k;
1307 i = jitCompA000_convTyp(k);
1308 if (i < 2 || i > 7) { w.err = JITC_ERR_BADTYPE; goto err_w; }
1310 j = jitCompGetImm32(&src[5]);
1316 if (src >= src1) { w.err = JITC_ERR_SRC1; src = oldsrc; goto err_w; }
1317 if (w.dst + 256 > dst1) { w.err = JITC_ERR_DST1; src = oldsrc; goto err_w; }
1319 for (k = 0; k < dataWidth; k++) {
1320 tmpData = tmpData << 1 | ((*src >> bitCount) & 1);
1327 if ((i & 1) == 0 && dataWidth <= 31 && (tmpData >> (dataWidth - 1)) != 0) {
1328 tmpData -= 1 << dataWidth;
1330 if (i == 2 || i == 3) { jitCompPutByte1(w.dst, tmpData & 0xff); }
1331 if (i == 4 || i == 5) { jitCompPutByte2(w.dst, tmpData & 0xff, (tmpData >> 8) & 0xff); }
1332 if (i == 6 || i == 7) { jitCompPutByte4(w.dst, tmpData & 0xff, (tmpData >> 8) & 0xff, (tmpData >> 16) & 0xff, (tmpData >> 24) & 0xff); }
1338 if (src >= src1) { w.err = JITC_ERR_SRC1; src = oldsrc; goto err_w; }
1339 if (w.dst + 256 > dst1) { w.err = JITC_ERR_DST1; src = oldsrc; goto err_w; }
1340 i = jitCompGetImm32(src);
1342 if ((flags & JITC_PHASE1) != 0 && w.err == 0) {
1343 if (label[i].opt == 0) { w.err = JITC_ERR_LABELNODEF; goto err_w; }
1345 jitCompPutImm32(&w, (int)label[i].p);
1346 jitCompPutImm32(&w, label[i].typ);
1347 jitCompPutImm32(&w, (int)label[i].p);
1348 jitCompPutImm32(&w, (int)label[i].p1);
1349 jitCompPutImm32(&w, 0); /* liveSign */
1350 jitCompPutImm32(&w, 2320); /* pls */
1351 jitCompPutImm32(&w, 0);
1352 jitCompPutImm32(&w, 0);
1356 if (lastlabel >= 0 && label[lastlabel].p1 < w.dst)
1357 label[lastlabel].p1 = w.dst;
1360 case 0x3c: /* ENTER */
1361 jitCompA000_storeRegCacheAll(&w); // 手抜き.
1362 jitCompA000_storePRegCacheAll(&w); // 手抜き.
1363 jitCompPutByte2(w.dst, 0x6a, src[6]); /* PUSH(?); */
1364 jitCompPutByte2(w.dst, 0x6a, src[5]); /* PUSH(?); */
1365 jitCompPutByte2(w.dst, 0x6a, src[4] & 0x0f); /* PUSH(?); */
1366 jitCompPutByte2(w.dst, 0x6a, (src[4] >> 4) & 0x0f); /* PUSH(?); */
1367 jitCompPutByte2(w.dst, 0x6a, src[3]); /* PUSH(?); */
1368 jitCompPutByte2(w.dst, 0x6a, src[2]); /* PUSH(?); */
1369 jitCompPutByte2(w.dst, 0x6a, src[1]); /* PUSH(?); */
1370 jitCompPutByte1(w.dst, 0x55); /* PUSH(EBP); */
1371 jitCompPutByte1(w.dst, 0xe8);
1372 j = ((unsigned char *)&func3c) - (w.dst + 4);
1373 jitCompPutImm32(&w, j);
1374 jitCompPutByte3(w.dst, 0x83, 0xc4, 0x20); /* ADD(ESP,32); */
1375 jitCompA000_loadRegCacheAll(&w); // 手抜き.
1376 jitCompA000_loadPRegCacheAll(&w); // 手抜き.
1380 case 0x3d: /* LEAVE */
1381 jitCompA000_storeRegCacheAll(&w); // 手抜き.
1382 jitCompA000_storePRegCacheAll(&w); // 手抜き.
1383 jitCompPutByte2(w.dst, 0x6a, src[6]); /* PUSH(?); */
1384 jitCompPutByte2(w.dst, 0x6a, src[5]); /* PUSH(?); */
1385 jitCompPutByte2(w.dst, 0x6a, src[4] & 0x0f); /* PUSH(?); */
1386 jitCompPutByte2(w.dst, 0x6a, (src[4] >> 4) & 0x0f); /* PUSH(?); */
1387 jitCompPutByte2(w.dst, 0x6a, src[3]); /* PUSH(?); */
1388 jitCompPutByte2(w.dst, 0x6a, src[2]); /* PUSH(?); */
1389 jitCompPutByte2(w.dst, 0x6a, src[1]); /* PUSH(?); */
1390 jitCompPutByte1(w.dst, 0x55); /* PUSH(EBP); */
1391 jitCompPutByte1(w.dst, 0xe8);
1392 j = ((unsigned char *)&func3d) - (w.dst + 4);
1393 jitCompPutImm32(&w, j);
1394 jitCompPutByte3(w.dst, 0x83, 0xc4, 0x20); /* ADD(ESP,32); */
1395 jitCompA000_loadRegCacheAll(&w); // 手抜き.
1396 jitCompA000_loadPRegCacheAll(&w); // 手抜き.
1400 case 0xfe: /* remark */
1401 if (src[1] == 0x01 && src[2] == 0x00) { // DBGINFO1
1402 if (level <= JITC_LV_SLOWER) {
1403 jitCompPutByte1(w.dst, 0xb8); /* MOV(EAX, ?); */
1404 jitCompPutImm32(&w, debugInfo1);
1405 jitCompA0001_movEbpDispReg32(&w, 2304 + 4, 0 /* EAX */); /* MOV(debugInfo1, EAX); */
1408 if (src[1] == 0x01 && src[2] == 0x03) { // DBGINFO1CLR
1409 if (level <= JITC_LV_SLOWER) {
1410 jitCompPutByte1(w.dst, 0xb8); /* MOV(EAX, ?); */
1411 jitCompPutImm32(&w, -1);
1412 jitCompA0001_movEbpDispReg32(&w, 2304 + 4, 0 /* EAX */); /* MOV(debugInfo1, EAX); */
1415 if (src[1] == 0x05 && src[2] == 0x00) { // DBGINFO0
1416 if (level <= JITC_LV_SLOWEST) {
1417 debugInfo0 = jitCompGetImm32(src + 3);
1418 // jitCompPutByte1(w.dst, 0xbf); /* MOV(EDI, ?); */
1419 // jitCompPutImm32(&w, debugInfo0);
1420 jitCompPutByte1(w.dst, 0xb8); /* MOV(EAX, ?); */
1421 jitCompPutImm32(&w, debugInfo0);
1422 jitCompA0001_movEbpDispReg32(&w, 2304 + 0, 0 /* EAX */); /* MOV(debugInfo0, EAX); */
1428 w.err = JITC_ERR_OPECODE;
1431 if (w.err != 0) goto err_w;
1432 jitCompA0001_fixPrefix(&w);
1433 if (w.err != 0) goto err_w;
1434 src += jitCompCmdLen(src);
1436 if (enter0 != NULL) {
1437 j = w.dst - (enter0 + 4);
1438 enter0[0] = j & 0xff;
1439 enter0[1] = (j >> 8) & 0xff;
1440 enter0[2] = (j >> 16) & 0xff;
1441 enter0[3] = (j >> 24) & 0xff;
1443 if ((flags & JITC_NOSTARTUP) == 0) {
1444 jitCompA000_storeRegCacheAll(&w);
1445 jitCompA000_storePRegCacheAll(&w);
1446 jitCompPutByte1(w.dst, 0x61); /* POPAD(); */
1448 if ((flags & JITC_PHASE1) != 0)
1449 return w.dst - dst00;
1453 if ((w.err & JITC_ERR_PHASE0ONLY) != 0) {
1454 if ((flags & JITC_PHASE1) == 0)
1455 w.err &= ~JITC_ERR_PHASE0ONLY;
1457 if (w.err == (JITC_ERR_MASK & JITC_ERR_REGNUM)) errmsg = "reg-number error";
1458 if (w.err == (JITC_ERR_MASK & JITC_ERR_DST1)) errmsg = "dst1 error";
1459 if (w.err == (JITC_ERR_MASK & JITC_ERR_OPECODE)) errmsg = "opecode error";
1460 if (w.err == (JITC_ERR_MASK & JITC_ERR_LABELNUM)) errmsg = "label number too large";
1461 if (w.err == (JITC_ERR_MASK & JITC_ERR_LABELREDEF)) errmsg = "label redefine";
1462 if (w.err == (JITC_ERR_MASK & JITC_ERR_PREFIX)) { errmsg = "prefix redefine"; w.dst -= 2; }
1463 if (w.err == (JITC_ERR_MASK & JITC_ERR_LABELNODEF)) errmsg = "label not defined";
1464 if (w.err == (JITC_ERR_MASK & JITC_ERR_LABELTYP)) errmsg = "label type error";
1465 if (w.err == (JITC_ERR_MASK & JITC_ERR_IDIOM)) errmsg = "idiom error";
1466 if (w.err == (JITC_ERR_MASK & JITC_ERR_PREGNUM)) errmsg = "preg-number error";
1467 if (w.err == (JITC_ERR_MASK & JITC_ERR_SRC1)) errmsg = "src1 error";
1468 if (w.err == (JITC_ERR_MASK & JITC_ERR_BADTYPE)) errmsg = "bad type code";
1469 if (w.err == (JITC_ERR_MASK & JITC_ERR_PREFIXFAR)) errmsg = "prefix internal error";
1470 if (w.err == (JITC_ERR_MASK & JITC_ERR_INTERNAL)) errmsg = "general internal error";
1471 if (*errmsg != '\0') {
1472 fprintf(stderr, "JITC: %s at %06X (debugInfo0=%d)\n ", errmsg, src - src0, debugInfo0);
1473 for (i = 0; i < 16; i++)
1474 fprintf(stderr, "%02X ", src[i]);
1475 static char *table[0x30] = {
1476 "NOP", "LB", "LIMM", "PLIMM", "CND", "??", "??", "??",
1477 "LMEM", "SMEM", "PLMEM", "PSMEM", "LEA", "??", "PADD", "PDIF",
1478 "CP/OR", "XOR", "AND", "??", "ADD", "SUB", "MUL", "??",
1479 "SHL", "SAR", "DIV", "MOD", "PLMT0", "PLMT1", "PCP", "PCST",
1480 "CMPE", "CMPNE", "CMPL", "CMPGE", "CMPLE", "CMPG", "TSTZ", "TSTNZ",
1481 "PCMPE", "PCMPNE", "PCMPL", "PCMPGE", "PCMPLE", "PCMPG", "??", "EXT" };
1483 if (*src < 0x30) errmsg = table[*src];
1484 fprintf(stderr, "(%s)\n", errmsg);
1489 unsigned char *jitCompCallFunc(unsigned char *dst, void *func)
1491 struct JitCompWork w;
1493 jitCompA000_storeRegCacheAll(&w);
1494 jitCompA000_storePRegCacheAll(&w);
1495 jitCompPutByte1(w.dst, 0x60); /* PUSHAD(); */
1496 jitCompPutByte1(w.dst, 0x50); /* PUSH(EAX); */ /* for 16byte-align(win32では不要なのだけど、MacOSには必要らしい) */
1497 jitCompPutByte1(w.dst, 0x55); /* PUSH(EBP); */
1498 jitCompPutByte1(w.dst, 0xe8); /* CALL(func); */
1499 int j = ((unsigned char *)func) - (w.dst + 4);
1501 //この関数の中では結局w->dstしか参照していない
1502 jitCompPutImm32(&w, j);
1504 jitCompPutByte1(w.dst, 0x58); /* POP(EAX); */ /* (win32では不要なのだけど、MacOSには必要らしい) */
1505 jitCompPutByte1(w.dst, 0x58); /* POP(EAX); */
1506 jitCompPutByte1(w.dst, 0x61); /* POPAD(); */
1507 jitCompA000_loadRegCacheAll(&w);
1508 jitCompA000_loadPRegCacheAll(&w);
1509 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + 0x30 * 32 + 0); /* MOV(EAX, [EBP+?]); */
1510 jitCompPutByte2(w.dst, 0xff, 0xe0); /* JMP(EAX); */
1514 unsigned char *jitCompInit(unsigned char *dst)
1517 return jitCompCallFunc(dst, &errHndl);
1520 void func3c(char *ebp, int opt, int r1, int p1, int lenR, int lenP, int r0, int p0)
1522 HOSECPU_RuntimeEnvironment *r = (HOSECPU_RuntimeEnvironment *) (ebp - jitCompA0001_EBP128);
1524 HOSECPU_PointerRegisterEntry *pp;
1525 if (r->junkStack + 2048 > r->junkStack1) (*(r->errHndl))(r);
1526 pi = (void *)r->junkStack; r->junkStack += r1 * 4;
1527 for (i = 0; i < r1; i++)
1529 pp = (void *)r->junkStack; r->junkStack += p1 * 32;
1530 for (i = 0; i < p1; i++)
1532 pp = (void *)r->junkStack; r->junkStack += 32;
1533 *pp = r->preg[0x30];
1534 pi = (void *)r->junkStack; r->junkStack += 4;
1535 *pi = opt << 16 | r1 << 8 | p1;
1536 for (i = 0; i < lenR; i++)
1537 r->ireg[r0 + i] = r->ireg[0x30 + i];
1538 for (i = 0; i < lenP; i++)
1539 r->preg[p0 + i] = r->preg[0x31 + i];
1543 void func3d(char *ebp, int opt, int r1, int p1, int lenR, int lenP, int r0, int p0)
1545 HOSECPU_RuntimeEnvironment *r = (HOSECPU_RuntimeEnvironment *) (ebp - jitCompA0001_EBP128);
1549 HOSECPU_PointerRegisterEntry *pp = (void *)r->junkStack;
1550 r->preg[0x30] = *pp;
1551 r->junkStack -= p1 * 32; pp = (void *)r->junkStack;
1552 for (i = 0; i < p1; i++)
1554 r->junkStack -= r1 * 4; int *pi = (void *)r->junkStack;
1555 for (i = 0; i < r1; i++)
1560 void funcf4(char *ebp, int pxx, int typ, int len)
1562 HOSECPU_RuntimeEnvironment *r = (HOSECPU_RuntimeEnvironment *) (ebp - jitCompA0001_EBP128);
1563 int width = jitCompA000_dataWidth(jitCompA000_convTyp(r->ireg[typ])) >> 3;
1564 if (width < 0 || r->ireg[len] < 0)
1566 void *p = r->junkStack;
1567 if (r->junkStack + width * r->ireg[len] + 256 > r->junkStack1) (*(r->errHndl))(r);
1568 r->junkStack += width * r->ireg[len];
1570 r->preg[pxx].typ = r->ireg[typ];
1571 r->preg[pxx].p0 = p;
1572 r->preg[pxx].p1 = (void *)r->junkStack;
1573 int *pi = (int *)r->junkStack;
1574 *pi = width * r->ireg[len];
1575 r->junkStack += sizeof (int);
1576 if (r->ireg[typ] == 1) {
1577 int i, i1 = (width * r->ireg[len]) >> 2;
1579 for (i = 0; i < i1; i++)
1585 void funcf5(char *ebp, int pxx, int typ, int len)
1587 HOSECPU_RuntimeEnvironment *r = (HOSECPU_RuntimeEnvironment *) (ebp - jitCompA0001_EBP128);
1588 r->junkStack -= sizeof (int);
1589 int *pi = (int *)r->junkStack;
1590 r->junkStack -= *pi;
1592 int width = jitCompA000_dataWidth(r->ireg[typ]);
1593 void *p = r->junkStack;
1594 r->junkStack -= width * r->ireg[len];
1599 void funcf6(char *ebp, int pxx, int typ, int len)
1601 HOSECPU_RuntimeEnvironment *r = (HOSECPU_RuntimeEnvironment *) (ebp - jitCompA0001_EBP128);
1602 int width = jitCompA000_dataWidth(jitCompA000_convTyp(r->ireg[typ])) >> 3;
1603 if (width < 0 || r->ireg[len] < 0)
1605 void *p = malloc(width * r->ireg[len]);
1607 r->preg[pxx].typ = r->ireg[typ];
1608 r->preg[pxx].p0 = p;
1609 r->preg[pxx].p1 = (unsigned char *)p + width * r->ireg[len];
1610 if (r->ireg[typ] == 1) {
1611 int i, i1 = (width * r->ireg[len]) >> 2, *pi;
1613 for (i = 0; i < i1; i++)
1615 for (i = 1; i < i1; i += 8)
1621 void funcf7(char *ebp, int pxx, int typ, int len)
1623 HOSECPU_RuntimeEnvironment *r = (HOSECPU_RuntimeEnvironment *) (ebp - jitCompA0001_EBP128);
1624 free(r->preg[pxx].p);
1628 void errHndl(HOSECPU_RuntimeEnvironment *r)
1630 r = (HOSECPU_RuntimeEnvironment *) (((char *)r) - jitCompA0001_EBP128);
1636 * jitcの出力コードをひとまとめにする関数を作成しその中身をjitCompile()で生成
1638 * qq : 出力バイナリの書き込み位置のアドレスへの参照(書き込み位置を呼び出しに反映させるため参照渡しにする)
1639 * q1 : 出力バイナリの書き込み位置のアドレスの最大値
1640 * p0 : (*.ose)バイナリの読み込み位置のアドレス(ヘッダ部除去済)
1641 * p1 : (*.ose)バイナリの読み込み位置の取りうる最大値
1642 * (ただし、「確保したメモリ」の最大値なのでこれより手前にゴミデータが入っているかもしれない)
1644 * ret=2 : jitコンパイルエラー
1646 int jitc0(unsigned char **qq, unsigned char *q1, const unsigned char *p0, const unsigned char *p1, int level, HOSECPU_LabelListTag *label)
1648 unsigned char *q = *qq;
1649 if (p0[0] != 0x05 || p0[1] != SIGN1) // OSECPUのヘッダ (05E1) を確認
1652 *q++ = 0x55; /* PUSH(EBP); */
1653 *q++ = 0x8b; *q++ = 0x6c; *q++ = 0x24; *q++ = 0x08; /* MOV(EBP,[ESP+8]); */
1656 for (i = 0; i < JITC_MAXLABELS; i++)
1659 // 以下のjitCompile()呼び出しでは第二引数をq1-2にした方がよいのではないか?
1660 i = jitCompiler(q, q1, p0 + 2, p1, p0, label, JITC_MAXLABELS, level, di1_serial, 0);
1661 if (i != 0) return 2;
1662 i = jitCompiler(q, q1, p0 + 2, p1, p0, label, JITC_MAXLABELS, level, di1_serial, JITC_PHASE1 + 0);
1663 if (i < 0) return 2;
1666 *q++ = 0x5d; /* POP(EBP); */
1667 *q++ = 0xc3; /* RET(); */
1673 #if (USE_DEBUGGER != 0)
1675 int dbgrGetRegNum(const char *p)
1681 if (i > 9) i -= 'A' - '0' - 10;
1682 if (j > 9) j -= 'A' - '0' - 10;
1683 if (0 <= i && i <= 15 && 0 <= j && j <= 15)
1689 void dbgrMain(HOSECPU_RuntimeEnvironment *r)
1691 if (r->dbgr == 0) return;
1696 p = fgets(cmd, 64, stdin);
1697 if (p == NULL) break;
1698 if (cmd[0] == '\0') continue;
1699 if (cmd[0] == 'q' && cmd[1] <= ' ') break;
1700 if (cmd[0] == 'p' && cmd[1] <= ' ' && cmd[1] != '\0') {
1702 while (*p <= ' ' && *p != '\0') p++;
1704 i = dbgrGetRegNum(p + 1);
1705 if (0 <= i && i <= 0x3f) {
1706 printf("R%02X = 0x%08X = %d\n", i, r->ireg[i], r->ireg[i]);
1709 puts("register name error");
1713 i = dbgrGetRegNum(p + 1);
1714 if (0 <= i && i <= 0x3f) {
1716 if (0 <= r->preg[i].typ && r->preg[i].typ <= 0x15) {
1717 static char *typName[] = {
1718 "T_CODE", "T_VPTR", "T_SINT8", "T_UINT8",
1719 "T_SINT16", "T_UINT16", "T_SINT32", "T_UINT32",
1720 "T_SINT4", "T_UINT4", "T_SINT2", "T_UINT2",
1721 "T_SINT1", "T_UINT1", "T_SINT12", "T_UINT12",
1722 "T_SINT20", "T_UINT20", "T_SINT24", "T_UINT24",
1723 "T_SINT28", "T_UINT28"
1725 p = typName[r->preg[i].typ];
1727 printf("P%02X:\n type = %s(%04X), (origin-ptr) = 0x%08X\n", i, p, r->preg[i].typ, (unsigned int)(r->preg[i].p0));
1728 if (r->preg[i].p != NULL && r->preg[i].p0 != NULL) {
1729 j = jitCompA000_dataWidth(jitCompA000_convTyp(r->preg[i].typ)) >> 3;
1731 k = (r->preg[i].p1 - r->preg[i].p0) / j;
1732 printf(" size = 0x%08X = %d\n", k, k);
1733 k = (r->preg[i].p - r->preg[i].p0) / j;
1734 printf(" pos = 0x%08X = %d\n", k, k);
1737 puts(" null pointer");
1741 puts("register name error");
1745 puts("command error");