OSDN Git Service

Merge branch 'master' of git.sourceforge.jp:/gitroot/heavyosecpu/HeavyOSECPU
[heavyosecpu/HeavyOSECPU.git] / jitc.c
1 #include "osecpu.h"
2
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
19
20 void errorHandler(HOSECPU_RuntimeEnvironment *r)
21 {
22         puts("security error! abort...");
23         printf("debugInfo0=%d, debugInfo1=%d\n", r->debugInfo0, r->debugInfo1);
24 #if (USE_DEBUGGER != 0)
25         dbgrMain(r);
26 #endif
27         exit(1);
28 }
29
30 int jitCompCmdLen(const unsigned char *src)
31 {
32     //BCode命令長を取得する
33         int i = 1;
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];
45         return i;
46 }
47 #if (JITC_ARCNUM == 0x0001)
48 //
49 // for x86-32bit
50 //
51
52 /* 他のCPUへ移植する人へ:
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
63
64 struct JitCompWork {
65         unsigned char *dst, *dst0;
66         int err, maxLabels;
67 #if (jitCompA0001_USE_R3F_IMM32 != 0)
68         int r3f;
69 #endif
70         char prefix;    //CND命令の値を記録(初期値=0)
71 };
72
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
77
78 static void jitCompPutImm32(struct JitCompWork *w, int i)
79 {
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);
84         return;
85 }
86
87 int jitCompGetImm32(const unsigned char *src)
88 {
89         return (src[0] << 24) | (src[1] << 16) | (src[2] << 8) | src[3];
90 }
91
92 int jitCompGetLabelNum(struct JitCompWork *w, const unsigned char *src)
93 {
94         int i = jitCompGetImm32(src);
95         if (i < 0 || i >= w->maxLabels) {
96                 w->err = JITC_ERR_LABELNUM;
97                 i = 0;
98         }
99         return i;
100 }
101
102 void jitCompA0001_85DispN(struct JitCompWork *w, int disp, int n)
103 {
104         disp -= jitCompA0001_EBP128;
105         if (-128 <= disp && disp <= 127) {
106                 jitCompPutByte2(w->dst, 0x45 | (n << 3), disp & 0xff);
107         }
108         else {
109                 jitCompPutByte1(w->dst, 0x85 | (n << 3));
110                 jitCompPutImm32(w, disp);
111         }
112         return;
113 }
114
115 void jitCompA0001_movEbpDispReg32(struct JitCompWork *w, int disp, int reg32)
116 {
117         jitCompPutByte1(w->dst, 0x89); /* MOV(mem, reg32); */
118         jitCompA0001_85DispN(w, disp, reg32);
119         return;
120 }
121
122 void jitCompA0001_movReg32EbpDisp(struct JitCompWork *w, int reg32, int disp)
123 {
124         jitCompPutByte1(w->dst, 0x8b); /* MOV(reg32, mem); */
125         jitCompA0001_85DispN(w, disp, reg32);
126         return;
127 }
128
129 void jitCompA0001_movEaxRxx(struct JitCompWork *w, int rxx)
130 {
131 #if (jitCompA0001_USE_R3F_IMM32 != 0)
132         if (rxx == 0x3f) {
133                 jitCompPutByte1(w->dst, 0xb8); /* MOV(EAX, ?); */
134                 jitCompPutImm32(w, w->r3f);
135                 return;
136         }
137 #endif
138         if (rxx >= 0x40 || rxx < 0) w->err = JITC_ERR_REGNUM;
139         jitCompA0001_movReg32EbpDisp(w, 0 /* EAX */, rxx * 4); /* MOV(EAX, [EBP+?]); */
140         return;
141 }
142
143 void jitCompA0001_movRxxEax(struct JitCompWork *w, int rxx)
144 {
145         if (rxx >= 0x40 || rxx < 0) w->err = JITC_ERR_REGNUM;
146         jitCompA0001_movEbpDispReg32(w, rxx * 4, 0 /* EAX */); /* MOV([EBP+?], EAX); */
147         return;
148 }
149
150 void jitCompA0001_fixPrefix(struct JitCompWork *w)
151 {
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);
155         }
156         return;
157 }
158
159 void jitCompA0001_checkCompPtr(struct JitCompWork *w, int p0, int p1)
160 {
161         if (p0 >= 0x3f || p0 < 0) w->err = JITC_ERR_PREGNUM;
162         if (p1 >= 0x3f || p1 < 0) w->err = JITC_ERR_PREGNUM;
163         /* 比較可能可能なのかのチェックのコードを出力 */   /* 未完成 */
164         return;
165 }
166
167 void jitCompA000_loadRegCacheAll(struct JitCompWork *w)
168 {
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; */
172         return;
173 }
174
175 void jitCompA000_storeRegCacheAll(struct JitCompWork *w)
176 {
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; */
180         return;
181 }
182
183 void jitCompA000_loadRegCacheEcx(struct JitCompWork *w)
184 {
185         jitCompA0001_movReg32EbpDisp(w, 1 /* ECX */, 1 * 4); /* ECX = R01; */
186         return;
187 }
188
189 void jitCompA000_storeRegCacheEcx(struct JitCompWork *w)
190 {
191         jitCompA0001_movEbpDispReg32(w, 1 * 4, 1 /* ECX */); /* R01 = ECX; */
192         return;
193 }
194
195 void jitCompA000_loadRegCacheEdx(struct JitCompWork *w)
196 {
197         jitCompA0001_movReg32EbpDisp(w, 2 /* EDX */, 2 * 4); /* EDX = R02; */
198         return;
199 }
200
201 void jitCompA000_storeRegCacheEdx(struct JitCompWork *w)
202 {
203         jitCompA0001_movEbpDispReg32(w, 2 * 4, 2 /* EDX */); /* R02 = EDX; */
204         return;
205 }
206
207 int jitCompA000_selectRegCache(int rxx, int reg)
208 {
209         if (rxx == 0) reg = 3; /* EBX */
210         if (rxx == 1) reg = 1; /* ECX */
211         if (rxx == 2) reg = 2; /* EDX */
212         return reg;
213 }
214
215 void jitCompA000_loadPRegCacheAll(struct JitCompWork *w)
216 {
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; */
220         return;
221 }
222
223 void jitCompA000_storePRegCacheAll(struct JitCompWork *w)
224 {
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; */
228         return;
229 }
230
231 int jitCompA000_selectPRegCache(int pxx, int reg)
232 {
233         //      if (pxx == 0) reg = 5; /* EBP */
234         if (pxx == 1) reg = 6; /* ESI */
235         if (pxx == 2) reg = 7; /* EDI */
236         return reg;
237 }
238
239 int jitCompA000_convTyp(int t)
240 {
241         int r = -1;
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);
246         return r;
247 }
248
249 int jitCompA000_dataWidth(int t)
250 {
251         int r = -1;
252         if (t == 0x0001) r = 256;
253         t >>= 1;
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;
264         return r;
265 }
266
267 static unsigned char *errfnc;
268
269 void jitCompA0001_checkType0(struct JitCompWork *w, int pxx, int typ, int ac)
270 {
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));
277         return;
278 }
279
280 void jitCompA0001_checkType(struct JitCompWork *w, int pxx, int typ, int ac)
281 // data用.
282 // 将来的にはaliveやアクセス権チェックも入れる
283 {
284         jitCompA0001_checkType0(w, pxx, typ, ac);
285         return;
286 }
287
288 void jitCompA0001_checkLimit(struct JitCompWork *w, int reg, int pxx)
289 {
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));
298         return;
299 }
300
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を指定必須にしてもフロントエンドコードに影響はない.
308
309 void errHndl(HOSECPU_RuntimeEnvironment *r);
310
311 /*
312  * dst : 現在の書き込みアドレス。
313  * dst1 : 書き込みアドレスの最大値
314  * src : 現在の読み込みアドレス(ヘッダ部は飛ばしてある
315  * src1 : 読み込みアドレスの最大値
316  * src0 : 読み込みバイナリのアドレス
317  */
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)
319 /* IA-32用 */
320 /* 本来ならこのレイヤでは文法チェックしない */
321 {
322         struct JitCompWork w;
323         unsigned char *dst00 = dst, *enter0 = NULL, *tmp_ucp;
324     char *errmsg = "";
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;
329         w.err = 0;
330         w.maxLabels = maxLabels;
331
332         if ((flags & JITC_NOSTARTUP) == 0) {
333                 jitCompPutByte1(w.dst, 0x60); /* PUSHAD(); */
334                 jitCompA000_loadRegCacheAll(&w); /* start-up */
335                 jitCompA000_loadPRegCacheAll(&w);
336         }
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); */
343         }
344         while (src < src1) {
345                 w.prefix = 0;   //0x04 CND 命令で変更される
346                 if (w.dst + 256 > dst1) { w.err = JITC_ERR_DST1; goto err_w; }  // 書き込み領域が残り256バイト未満ならエラー
347                 timecount++;
348                 if (timecount >= 64) {
349                         timecount -= 64;
350                         /* 未完成(timeoutチェックコードを入れる) */
351                 }
352         prefix_continue:        // CND命令実行後ここに戻る
353                 switch (*src) {
354
355                 case 0x00:      /* NOP */
356                         if (w.prefix != 0) { w.err = JITC_ERR_PREFIX; goto err_w; }     // 「条件付きでNOPを実行」するなんて、矛盾している!
357                         break;
358
359                 case 0x01:      /* LB */
360                         
361                         /*
362                          * LB : ラベル設置命令。(6byte)
363                          * ・prefex = 1にする
364                          * ・timecount++し、timecountのチェックをする。
365                          * ・ラベル位置を登録する。
366                          * ・割り込みがある場合、このタイミングで割り込みを発生させる。
367                          *
368                          *  1   2       3       456
369                          *      LB      01      opt     imm32
370                          *
371                          */
372                         
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 : 次の命令との相対オフセットだけ相対ニアジャンプする
376                                 enter0 = w.dst;
377                                 jitCompPutImm32(&w, 0); // 飛び相対座標が0 ・・・パイプラインのフラッシュ??
378                         }
379                         if (src[6] == 0x34) {   // LBの次の命令がDATA ・・・DAT_SA0(label, typ32, length) ・・・メモリ確保命令
380                                 tmp_ucp = w.dst;
381                                 jitCompPutByte1(w.dst, 0xe9);   // (x86) JMP rel32 : 次の命令との相対オフセットだけ相対ニアジャンプする
382                                 i = jitCompGetImm32(&src[7]);   // type32 を取得
383                                 j = 32;
384                                 if (i != 1) {
385                                         i = jitCompA000_convTyp(i);
386                                         j = 0;
387                                         if (i == 2 || i == 3) { j = 1; }
388                                         if (i == 4 || i == 5) { j = 2; }
389                                         if (i == 6 || i == 7) { j = 4; }
390                                 }
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) {
396                                         w.dst -= 5;
397                                         jitCompPutByte2(w.dst, 0xeb, j);
398                                 }
399 #endif
400                         }
401 #if (jitCompA0001_OPTIMIZE_ALIGN != 0)
402                         for (;;) {
403                                 i = ((int)w.dst) & (jitCompA0001_OPTIMIZE_ALIGN - 1); /* ALIGNで割ったあまりを計算 */
404                                 if (i == 0) break;
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]); */
413                         }
414 #endif
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;
421                                 }
422                         }
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 */
430                                 label[i].p = w.dst;
431                                 label[i].p1 = w.dst + 1;
432                                 lastlabel = i;
433                         }
434                         cmp0reg = -1;
435                         timecount = 0;
436                         /* 未完成(timeoutチェックコードを入れる) */
437                         break;
438
439                 case 0x02:      /* LIMM */      
440                         
441                         /*
442                          * LIMM : 定数即値代入命令(6byte)
443                          * 
444                          *      1       2               3456
445                          *      02      reg0R   imm32
446                          *
447                          * ・reg3F は条件比較慣用句指定用&演算命令即値慣用句指定用。よってCND命令の直後では使用できない。
448                          */
449                         
450                         if (src[1] == 0x3f && w.prefix != 0) w.err = JITC_ERR_PREFIX;   // CND命令の直後でR3Fを書き換えるなんて変だよね
451
452 #if (jitCompA0001_USE_R3F_IMM32 != 0)
453                         if (src[1] == 0x3f) {           // R3Fへの代入は例外敵に、 w.r3f を使用
454                                 w.r3f = jitCompGetImm32(src + 2);
455                                 break;
456                         }
457 #endif
458                         i = jitCompGetImm32(src + 2);   // 与えられた即値(第二引数)を取得
459
460                         /* R00-R02 なら EBX, ECX, EDX 、それ以外なら EAX のレジスタIDを reg0 に代入 */
461                         reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
462
463 #if (jitCompA0001_OPTIMIZE_MOV != 0)
464                         if (i == 0) {
465                                 jitCompPutByte2(w.dst, 0x31, 0xc0 | reg0 << 3 | reg0);  /* XOR(reg0, reg0); */
466                                 jitCompA0001_movRxxEax(&w, src[1]);
467                                 break;
468                         }
469 #endif
470                         
471                         /* reg0 のレジスタに対応したMOV命令を発行 */
472                         jitCompPutByte1(w.dst, 0xb8 | reg0);    /* MOV(reg0, ?);  == 10111000b+wr imm32 */
473                         jitCompPutImm32(&w, i);
474
475                         if (reg0 == 0)  // R03以降の、レジスタの内容をメモリ上に格納してエミュレートする場合
476                                 
477                                 jitCompA0001_movRxxEax(&w, src[1]);
478                         break;
479
480                 case 0x03:      /* PLIMM */     /* 未完成(plsまで対応) */
481
482                         /*
483                         * PLIMM : ラベル番号代入命令(6byte)
484                         *
485                         *       1       2       3456
486                         *       03      PXX     imm32
487                         *
488                         * ・P28 はAPI用
489                         * ・P30 はリターンアドレス
490                         * ・P3F はプログラムカウンタ
491                         */
492
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でない値は代入できない
498                         }
499                         if (src[1] == 0x3f) {   // プログラムカウンタへの代入なら
500                                 if (w.prefix == 0) {    // CND命令による条件付きでなければ、即座に移動
501                                         jitCompPutByte1(w.dst, 0xe9); /* JMP(?); */
502                                 }
503                                 else {  // 直前はCND命令。
504                                         
505                                         /*
506                                          * CND命令
507                                          *      1       2       
508                                          *      04      reg0R
509                                          *
510                                          * いま、dstの末端はJZ命令になっている。 0x0F 0x84 cd
511                                          */
512
513                                          // JZのとび先アドレスの書き換え?
514                                         w.dst[-1] = w.dst[-2] ^ 0xf1; /* 74->85, 75->84 */
515                                         w.dst[-2] = 0x0f;
516
517                                         w.prefix = 0;
518                                 }
519                                 j = 0;
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) {
526                                                 j += 3;
527                                                 w.dst -= 5;
528                                                 jitCompPutByte1(w.dst, 0xeb); /* JMP(?); */
529                                         }
530                                         else {
531                                                 j += 4;
532                                                 w.dst -= 6;
533                                                 jitCompPutByte1(w.dst, w.dst[1] ^ 0xf0);
534                                         }
535                                         jitCompPutByte1(w.dst, j & 0xff);
536                                 }
537 #endif
538                         }
539                         else {  // プログラムカウンタ以外への代入
540                                 
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);   // ラベルのパスを各レジスタに代入
545
546                                 // レジスタへの代入をメモリでエミュレーションする場合は、スタックに積む。
547                                 if (reg0 == 0)
548                                         jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32, 0); /* MOV([EBP+?], EAX); */
549
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 */
562                                 }
563                         }
564                         break;
565
566                 case 0x04:      /* CND (prefix) */
567
568                         /*
569                          * CND命令
570                          * 与えられたRxxの最下位ビットが1であれば後続の命令を実行、そうでなければ飛ばす。
571                          */
572                         
573                         if (src[1] >= 0x40) w.err = JITC_ERR_REGNUM;    // R00-R3F 以外のレジスタは比較対象にできない
574
575                         // 比較対象のレジスタがメモリ上にあれば-1, それ以外なら適切なレジスタ番号を返す
576                         reg0 = jitCompA000_selectRegCache(src[1], -1 /* mem */);
577
578                         /* TEST命令を発行 */
579                         if (reg0 < 0) { //比較対象のレジスタはメモリ上にある
580                                 jitCompPutByte1(w.dst, 0xf7); /* TEST([EBP+?],1); */
581                                 jitCompA0001_85DispN(&w, src[1] * 4, 0);
582                         }
583                         else {
584                                 jitCompPutByte2(w.dst, 0xf7, 0xc0 | reg0); /* TEST(reg0,1); */
585                         }
586                         jitCompPutImm32(&w, 1);
587
588                         /* JZ命令を発行 */
589                         jitCompPutByte2(w.dst, 0x74, 0x00);     /* JZ($+2) */
590                         cmp0reg = -1;
591                         if (w.err != 0) goto err_w;
592                         src += 2;
593                         w.prefix = 1;   // プリフィックスをセット
594                         w.dst0 = w.dst; 
595                         goto prefix_continue;
596
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
602                                 cmp0reg = -1;
603                         }
604                         reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
605                         reg1 = jitCompA000_selectPRegCache(src[6], 2 /* EDX */);
606                         if (reg0 != 0 /* EAX */ && reg1 == 2 /* EDX */)
607                                 reg1 = 0; /* EAX */
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));
615                         switch (i) {
616                         case 0x0002:
617                                 jitCompPutByte3(w.dst, 0x0f, 0xbe, reg0 << 3 | reg1);   /* MOVSX(reg0,BYTE [reg1]); */
618                                 break;
619                         case 0x0003:
620                                 jitCompPutByte3(w.dst, 0x0f, 0xb6, reg0 << 3 | reg1);   /* MOVZX(reg0,BYTE [reg1]); */
621                                 break;
622                         case 0x0004:
623                                 jitCompPutByte3(w.dst, 0x0f, 0xbf, reg0 << 3 | reg1);   /* MOVSX(reg0,WORD [reg1]); */
624                                 break;
625                         case 0x0005:
626                                 jitCompPutByte3(w.dst, 0x0f, 0xb7, reg0 << 3 | reg1);   /* MOVZX(reg0,WORD [reg1]); */
627                                 break;
628                         case 0x0006:
629                         case 0x0007:
630                                 jitCompPutByte2(w.dst, 0x8b, reg0 << 3 | reg1); /* MOV(reg0, [reg1]); */
631                                 break;
632                         default:
633                                 w.err = JITC_ERR_BADTYPE;
634                         }
635                         if (reg0 == 0 /* EAX */)
636                                 jitCompA0001_movRxxEax(&w, src[1]);
637                         if (reg1 == 2 /* EDX */)
638                                 jitCompA000_loadRegCacheEdx(&w);
639                         break;
640
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
646                                 cmp0reg = -1;
647                         }
648                         reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
649                         reg1 = jitCompA000_selectPRegCache(src[6], 2 /* EDX */);
650                         if (reg0 != 0 /* EAX */ && reg1 == 2 /* EDX */)
651                                 reg1 = 0; /* EAX */
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]);
660                         /* 値の範囲チェック */
661                         i = jitCompA000_convTyp(jitCompGetImm32(src + 2));
662                         switch (i) {
663                         case 0x0002:
664                         case 0x0003:
665                                 jitCompPutByte2(w.dst, 0x88, reg0 << 3 | reg1); /* MOV([reg1], BYTE(reg0)); */
666                                 break;
667                         case 0x0004:
668                         case 0x0005:
669                                 jitCompPutByte3(w.dst, 0x66, 0x89, reg0 << 3 | reg1);   /* MOV([reg1], WORD(reg0)); */
670                                 break;
671                         case 0x0006:
672                         case 0x0007:
673                                 jitCompPutByte2(w.dst, 0x89, reg0 << 3 | reg1); /* MOV([reg1], reg0); */
674                                 break;
675                         default:
676                                 w.err = JITC_ERR_BADTYPE;
677                         }
678                         if (reg1 == 2 /* EDX */)
679                                 jitCompA000_loadRegCacheEdx(&w);
680                         break;
681
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
687                                 cmp0reg = -1;
688                         }
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);
695                                 reg1 = 2; /* EDX */
696                         }
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); */
709                         }
710                         if (reg1 == 2 /* EDX */)
711                                 jitCompA000_loadRegCacheEdx(&w);
712                         break;
713
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
719                                 cmp0reg = -1;
720                         }
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); */
737                         }
738                         if (reg1 == 2 /* EDX */)
739                                 jitCompA000_loadRegCacheEdx(&w);
740                         break;
741
742                 case 0x0e: /* PADD */           /* 完成 */
743                         if (level < JITC_LV_FASTER) {
744                                 jitCompA0001_checkType0(&w, src[6], jitCompGetImm32(src + 2), 2); // other, aliveテストはとりあえずしない.
745                                 cmp0reg = -1;
746                         }
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); */
753                         }
754                         i = jitCompGetImm32(src + 2);
755                         j = -1;
756                         if (i == 1)
757                                 j = 5; /* 32 */
758                         else {
759                                 i = jitCompA000_convTyp(i);
760                                 if (0x0002 <= i && i <= 0x0007)
761                                         j = (i - 0x0002) >> 1;
762                         }
763                         if (j < 0) { w.err = JITC_ERR_BADTYPE; goto err_w; }
764 #if (jitCompA0001_USE_R3F_IMM32 != 0)
765                         if (src[7] == 0x3f) {
766                                 j = w.r3f << j;
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 */
772 #endif
773                                         jitCompPutByte3(w.dst, 0x83, 0xc0 | reg0, j & 0xff);    /* ADD(reg0, im8); */
774                                         goto padd1;
775                                 }
776 #endif
777                                 if (reg0 == 0) {
778                                         jitCompPutByte1(w.dst, 0x05);   /* ADD(reg0, ?); */
779                                 }
780                                 else {
781                                         jitCompPutByte2(w.dst, 0x81, 0xc0 | reg0);      /* ADD(reg0, ?); */
782                                 }
783                                 jitCompPutImm32(&w, j);
784                                 goto padd1;
785                         }
786 #endif
787                         if (src[7] >= 0x40) w.err = JITC_ERR_REGNUM;
788                         if (j == 0) {
789                                 reg1 = jitCompA000_selectRegCache(src[7], -1 /* mem */);
790                                 if (reg1 >= 0) {
791                                         jitCompPutByte2(w.dst, 0x01, 0xc0 | reg1 << 3 | reg0);  /* ADD(reg0, reg1); */
792                                 }
793                                 else {
794                                         jitCompPutByte1(w.dst, 0x03);   /* ADD(reg0, [EBP+?]); */
795                                         jitCompA0001_85DispN(&w, src[7] * 4, reg0);
796                                 }
797                         }
798                         else {
799                                 reg1 = jitCompA000_selectRegCache(src[7], -1 /* mem */);
800                                 reg2 = 2; /* EDX */
801                                 jitCompA000_storeRegCacheEdx(&w);
802                                 if (reg1 < 0)
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); */
806                                 }
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);
810                         }
811 #if (jitCompA0001_USE_R3F_IMM32 != 0)
812                 padd1:
813 #endif
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); */
820                                 }
821                         }
822                         cmp0reg = -1;
823                         break;
824
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));
833                         j = -1;
834                         if (0x0002 <= i && i <= 0x0007)
835                                 j = (i - 0x0002) >> 1;
836                         if (j < 0) { w.err = JITC_ERR_BADTYPE; goto err_w; }
837                         if (j > 0) {
838                                 jitCompPutByte3(w.dst, 0xc1, 0xf8 | reg0, j);   /* SAR(reg0,?); */
839                         }
840                         if (reg0 == 0 /* EAX */)
841                                 jitCompA0001_movRxxEax(&w, src[1]);
842                         cmp0reg = src[1]; cmp0lev = 1;
843                         break;
844
845                 case 0x10:      /* OR */
846                 case 0x11:      /* XOR */
847                 case 0x12:      /* AND */
848                 case 0x14:      /* ADD */
849                 case 0x15:      /* SUB */
850                 case 0x16:      /* MUL */
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 */);
858                                 if (reg2 >= 0)
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);
864                                 if (reg0 == 0)
865                                         jitCompA0001_movRxxEax(&w, src[1]);
866                                 break;
867                         }
868 #endif
869                         if (reg1 < 0) {
870                                 jitCompA0001_movReg32EbpDisp(&w, reg0, src[2] * 4); /* MOV(reg0, [EBP+?]); */
871                         }
872                         if (reg1 >= 0 && reg0 != reg1) {
873                                 jitCompPutByte2(w.dst, 0x89, 0xc0 | reg1 << 3 | reg0); /* MOV(reg0, reg1); */
874                         }
875                         if (!(src[0] == 0x10 && src[3] == 0xff)) {  // bugfix: hinted by Iris, 2013.06.26. thanks!
876                                 cmp0reg = src[1];
877                                 cmp0lev = 1;
878                                 if (src[0] < 0x14)
879                                         cmp0lev = 2;
880                                 if (src[0] == 0x16)
881                                         cmp0reg = -1;
882                         }
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); */
888                                                 if (reg0 == 0)
889                                                         jitCompA0001_movRxxEax(&w, src[1]);
890                                                 break;
891                                         }
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); */
895                                                 if (reg0 == 0)
896                                                         jitCompA0001_movRxxEax(&w, src[1]);
897                                                 break;
898                                         }
899                                         if ((*src == 0x15 && w.r3f == 1) || (*src == 0x14 && w.r3f == -1)) {
900                                                 jitCompPutByte1(w.dst, 0x48 | reg0);    /* DEC(reg0); */
901                                                 if (reg0 == 0)
902                                                         jitCompA0001_movRxxEax(&w, src[1]);
903                                                 break;
904                                         }
905 #endif
906 #if (jitCompA0001_USE_R3F_IMM8 != 0)
907                                         if (-0x80 <= w.r3f && w.r3f <= 0x7f) {
908                                                 if (*src != 0x16) {
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);
911                                                 }
912                                                 else {
913                                                         jitCompPutByte3(w.dst, 0x6b, 0xc0 | reg0 << 3 | reg0, w.r3f & 0xff);
914                                                 }
915                                                 if (reg0 == 0)
916                                                         jitCompA0001_movRxxEax(&w, src[1]);
917                                                 break;
918                                         }
919 #endif
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]);
924                                         }
925                                         else {
926                                                 if (*src != 0x16) {
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);
929                                                 }
930                                                 else {
931                                                         jitCompPutByte2(w.dst, 0x69, 0xc0 | reg0 << 3 | reg0);
932                                                 }
933                                         }
934                                         jitCompPutImm32(&w, w.r3f);
935                                         if (reg0 == 0)
936                                                 jitCompA0001_movRxxEax(&w, src[1]);
937                                         break;
938                                 }
939 #endif
940                                 reg1 = jitCompA000_selectRegCache(src[3], -1 /* mem */);
941                                 if (src[3] >= 0x40) w.err = JITC_ERR_REGNUM;
942                                 if (*src != 0x16) {
943                                         if (reg1 >= 0) {
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);
946                                         }
947                                         else {
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);
951                                         }
952                                 }
953                                 else {
954                                         if (reg1 >= 0) {
955                                                 jitCompPutByte3(w.dst, 0x0f, 0xaf, 0xc0 | reg0 << 3 | reg1);
956                                         }
957                                         else {
958                                                 jitCompPutByte2(w.dst, 0x0f, 0xaf);
959                                                 jitCompA0001_85DispN(&w, src[3] * 4, reg0);
960                                         }
961                                 }
962                         }
963                         if (reg0 == 0)
964                                 jitCompA0001_movRxxEax(&w, src[1]);
965                         break;
966
967                 case 0x18:      /* SHL */
968                 case 0x19:      /* SAR */
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;
976                                 if (reg1 == -1)
977                                         jitCompA0001_movReg32EbpDisp(&w, reg0, src[2] * 4); /* MOV(reg1, [EBP+?]); */
978                                 else {
979                                         if (reg0 != reg1) {
980                                                 jitCompPutByte2(w.dst, 0x89, 0xc0 | reg1 << 3 | reg0); /* MOV(reg0, reg1); */
981                                         }
982                                 }
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]);
987                                 cmp0reg = src[1];
988                                 cmp0lev = 1;
989                                 break;
990                         }
991 #endif
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);
998                         }
999                         else {
1000                                 jitCompA0001_movEaxRxx(&w, src[2]);
1001                         }
1002 #else
1003                         jitCompA0001_movEaxRxx(&w, src[2]);
1004 #endif
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); // 手抜き.
1009                         cmp0reg = src[1];
1010                         cmp0lev = 1;
1011                         break;
1012
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);
1023                         }
1024                         else {
1025                                 jitCompA0001_movReg32EbpDisp(&w, 1 /* ECX */, src[3] * 4); /* MOV(ECX, [EBP+?]); */
1026                         }
1027                         if (src[2] == 0x3f) {
1028                                 jitCompPutByte1(w.dst, 0xb8 | 0);       /* MOV(EAX, ?); */
1029                                 jitCompPutImm32(&w, w.r3f);
1030                         }
1031                         else {
1032                                 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, src[2] * 4); /* MOV(EAX, [EBP+?]); */
1033                         }
1034 #else
1035                         jitCompA0001_movReg32EbpDisp(&w, 1 /* ECX */, src[3] * 4); /* MOV(ECX, [EBP+?]); */
1036                         jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, src[2] * 4); /* MOV(EAX, [EBP+?]); */
1037 #endif
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); // 手抜き.
1044                         cmp0reg = -1;
1045                         break;
1046
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) {
1051                                 cmp0reg = -1;
1052                                 if (level < JITC_LV_FASTER) {
1053                                         // typ が一致していることを確認.
1054                                         // plsとliveSignが一致していることを確認.
1055
1056                                         // preg1はp0 <= p <= p1 を満たしているか?.
1057                                         // 新しいp0/p1は古いp0〜p1に適合しているか?.
1058
1059                                 }
1060                         }
1061
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); */
1071                                 }
1072                                 jitCompA000_loadPRegCacheAll(&w); // 手抜き.
1073                         }
1074                         else {
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とか) */
1081                                 }
1082                                 reg0 = 0; /* EAX */
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));
1090                                 }
1091                                 jitCompPutByte2(w.dst, 0xff, 0xe0);     /* JMP(EAX); */
1092                         }
1093                         break;
1094
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+?]); */
1102                                         if (i == 4) {
1103                                                 jitCompPutByte1(w.dst, 0x0d); /* OR(EAX, ?); */
1104                                                 jitCompPutImm32(&w, 0x80000000);
1105                                         }
1106                                         jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + i, 0 /* EAX */); /* MOV([EBP+?], EAX); */
1107                                 }
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); // 手抜き.
1112                                 cmp0reg = -1;
1113                                 break;
1114                         }
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+?]); */
1119                                         if (i == 4) {
1120                                                 jitCompPutByte1(w.dst, 0x25); /* AND(EAX, ?); */
1121                                                 jitCompPutImm32(&w, 0x7fffffff);
1122                                         }
1123                                         jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + i, 0 /* EAX */); /* MOV([EBP+?], EAX); */
1124                                 }
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) */
1132                                 }
1133                                 jitCompA000_loadPRegCacheAll(&w); // 手抜き.
1134                                 cmp0reg = -1;
1135                                 break;
1136                         }
1137                         w.err = JITC_ERR_OPECODE;
1138                         goto err_w;
1139
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) {
1151                                 /* 特殊構文チェック */
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;
1155                                 }
1156                         }
1157                         if (reg0 == 0)
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)) {
1163                                         i = 0;
1164                                         if (cmp0reg == src[2]) {
1165                                                 if (cmp0lev >= 1 && (src[0] == 0x20 || src[0] == 0x21 || src[0] == 0x26 || src[0] == 0x27))
1166                                                         i = 1;
1167                                                 if (cmp0lev >= 2 && (src[0] == 0x22 || src[0] == 0x23 || src[0] == 0x24 || src[0] == 0x25))
1168                                                         i = 1;
1169                                         }
1170                                         if (i == 0) {
1171                                                 jitCompPutByte2(w.dst, 0x85, 0xc0 | reg0 << 3 | reg0);  /* TEST(reg0, reg0); */
1172                                         }
1173                                         cmp0reg = src[2];
1174                                         cmp0lev = 2;
1175                                         goto cmpcc1;
1176                                 }
1177 #endif
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);
1181                                         goto cmpcc1;
1182                                 }
1183 #endif
1184                                 if (reg0 == 0) {
1185                                         if (*src <= 0x25) { jitCompPutByte1(w.dst, 0x3d); }
1186                                         if (*src >= 0x26) { jitCompPutByte1(w.dst, 0xa9); }
1187                                 }
1188                                 else {
1189                                         if (*src <= 0x25) { jitCompPutByte2(w.dst, 0x81, 0xf8 | reg0); }
1190                                         if (*src >= 0x26) { jitCompPutByte2(w.dst, 0xf7, 0xc0 | reg0); }
1191                                 }
1192                                 jitCompPutImm32(&w, w.r3f);
1193                                 goto cmpcc1;
1194                         }
1195 #endif
1196                         if (src[3] >= 0x40) w.err = JITC_ERR_PREGNUM;
1197                         if (reg1 >= 0) {
1198                                 if (*src <= 0x25) { jitCompPutByte2(w.dst, 0x39, 0xc0 | reg1 << 3 | reg0); }
1199                                 if (*src >= 0x26) { jitCompPutByte2(w.dst, 0x85, 0xc0 | reg1 << 3 | reg0); }
1200                         }
1201                         else {
1202                                 if (*src <= 0x25) { jitCompPutByte1(w.dst, 0x3b); }
1203                                 if (*src >= 0x26) { jitCompPutByte1(w.dst, 0x85); }
1204                                 jitCompA0001_85DispN(&w, src[3] * 4, reg0);
1205                         }
1206                 cmpcc1:
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 */
1211                         };
1212 #if (jitCompA0001_USE_R3F_CMPJMP != 0)
1213                         if (src[1] == 0x3f) {
1214                                 /* 特殊構文を利用した最適化 */
1215                                 jitCompPutByte2(w.dst, 0x0f, 0x80 | cmpcc_table0[*src - 0x20]);
1216                                 src += 6;
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; }
1221                                 }
1222                                 j = 0;
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) {
1228                                         j += 4;
1229                                         w.dst -= 6;
1230                                         jitCompPutByte2(w.dst, w.dst[1] ^ 0xf0, j & 0xff);
1231                                 }
1232 #endif
1233                                 src += 6;
1234                                 if (w.err != 0) goto err_w;
1235                                 continue;
1236                         }
1237 #endif
1238                         /* 一般的なJITC */
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); */
1243                         if (reg0 == 0)
1244                                 jitCompA0001_movRxxEax(&w, src[1]);
1245                         cmp0reg = src[2];
1246                         cmp0lev = 1;
1247                         break;
1248
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) {
1256                                 /* 特殊構文チェック */
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;
1260                                 }
1261                         }
1262                         if (src[2] >= 0x40) w.err = JITC_ERR_PREGNUM;
1263                         jitCompA000_storePRegCacheAll(&w); // 手抜き.
1264                         if (src[3] != 0xff)
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);
1270                         }
1271                         else {
1272                                 /* ヌルポインタとの比較はこれでいいのか?たぶんよくない */
1273                                 jitCompPutByte3(w.dst, 0x83, 0xf8, 0x00);       /* CMP(EAX, 0); */
1274                         }
1275                         cmp0reg = -1;
1276                         goto cmpcc1;
1277
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); // 手抜き.
1297                         cmp0reg = -1;
1298                         break;
1299
1300                 case 0x34:      /* data (暫定) */
1301                         cmp0reg = -1;
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;
1306                         if (k != 1) {
1307                                 i = jitCompA000_convTyp(k);
1308                                 if (i < 2 || i > 7) { w.err = JITC_ERR_BADTYPE; goto err_w; }
1309                         }
1310                         j = jitCompGetImm32(&src[5]);
1311                         oldsrc = src;
1312                         src += 9;
1313                         if (k != 1) {
1314                                 bitCount = 7;
1315                                 while (j > 0) {
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; }
1318                                         tmpData = 0;
1319                                         for (k = 0; k < dataWidth; k++) {
1320                                                 tmpData = tmpData << 1 | ((*src >> bitCount) & 1);
1321                                                 bitCount--;
1322                                                 if (bitCount < 0) {
1323                                                         bitCount = 7;
1324                                                         src++;
1325                                                 }
1326                                         }
1327                                         if ((i & 1) == 0 && dataWidth <= 31 && (tmpData >> (dataWidth - 1)) != 0) {
1328                                                 tmpData -= 1 << dataWidth;
1329                                         }
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); }
1333                                         j--;
1334                                 }
1335                         }
1336                         else {
1337                                 while (j > 0) {
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);
1341                                         src += 4;
1342                                         if ((flags & JITC_PHASE1) != 0 && w.err == 0) {
1343                                                 if (label[i].opt == 0) { w.err = JITC_ERR_LABELNODEF; goto err_w; }
1344                                         }
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);
1353                                         j--;
1354                                 }
1355                         }
1356                         if (lastlabel >= 0 && label[lastlabel].p1 < w.dst)
1357                                 label[lastlabel].p1 = w.dst;
1358                         continue;
1359
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); // 手抜き.
1377                         cmp0reg = -1;
1378                         break;
1379
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); // 手抜き.
1397                         cmp0reg = -1;
1398                         break;
1399
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); */
1406                                 }
1407                         }
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); */
1413                                 }
1414                         }
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); */
1423                                 }
1424                         }
1425                         break;
1426
1427                 default:
1428                         w.err = JITC_ERR_OPECODE;
1429                         goto err_w;
1430                 }
1431                 if (w.err != 0) goto err_w;
1432                 jitCompA0001_fixPrefix(&w);
1433                 if (w.err != 0) goto err_w;
1434                 src += jitCompCmdLen(src);
1435         }
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;
1442         }
1443         if ((flags & JITC_NOSTARTUP) == 0) {
1444                 jitCompA000_storeRegCacheAll(&w);
1445                 jitCompA000_storePRegCacheAll(&w);
1446                 jitCompPutByte1(w.dst, 0x61); /* POPAD(); */
1447         }
1448         if ((flags & JITC_PHASE1) != 0)
1449                 return w.dst - dst00;
1450         return 0;
1451
1452 err_w:
1453         if ((w.err & JITC_ERR_PHASE0ONLY) != 0) {
1454                 if ((flags & JITC_PHASE1) == 0)
1455                         w.err &= ~JITC_ERR_PHASE0ONLY;
1456         }
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" };
1482                 errmsg = "??";
1483                 if (*src < 0x30) errmsg = table[*src];
1484                 fprintf(stderr, "(%s)\n", errmsg);
1485         }
1486         return -1;
1487 }
1488
1489 unsigned char *jitCompCallFunc(unsigned char *dst, void *func)
1490 {
1491         struct JitCompWork w;
1492         w.dst = dst;
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);
1500
1501         //この関数の中では結局w->dstしか参照していない
1502         jitCompPutImm32(&w, j);
1503
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); */
1511         return w.dst;
1512 }
1513
1514 unsigned char *jitCompInit(unsigned char *dst)
1515 {
1516         errfnc = dst;
1517         return jitCompCallFunc(dst, &errHndl);
1518 }
1519
1520 void func3c(char *ebp, int opt, int r1, int p1, int lenR, int lenP, int r0, int p0)
1521 {
1522         HOSECPU_RuntimeEnvironment *r = (HOSECPU_RuntimeEnvironment *) (ebp - jitCompA0001_EBP128);
1523         int i, *pi;
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++)
1528                 pi[i] = r->ireg[i];
1529         pp = (void *)r->junkStack; r->junkStack += p1 * 32;
1530         for (i = 0; i < p1; i++)
1531                 pp[i] = r->preg[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];
1540         return;
1541 }
1542
1543 void func3d(char *ebp, int opt, int r1, int p1, int lenR, int lenP, int r0, int p0)
1544 {
1545         HOSECPU_RuntimeEnvironment *r = (HOSECPU_RuntimeEnvironment *) (ebp - jitCompA0001_EBP128);
1546         int i;
1547         r->junkStack -= 4;
1548         r->junkStack -= 32;
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++)
1553                 r->preg[i] = pp[i];
1554         r->junkStack -= r1 * 4; int *pi = (void *)r->junkStack;
1555         for (i = 0; i < r1; i++)
1556                 r->ireg[i] = pi[i];
1557         return;
1558 }
1559
1560 void funcf4(char *ebp, int pxx, int typ, int len)
1561 {
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)
1565                 (*(r->errHndl))(r);
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];
1569         r->preg[pxx].p = p;
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;
1578                 pi = p;
1579                 for (i = 0; i < i1; i++)
1580                         pi[i] = 0;
1581         }
1582         return;
1583 }
1584
1585 void funcf5(char *ebp, int pxx, int typ, int len)
1586 {
1587         HOSECPU_RuntimeEnvironment *r = (HOSECPU_RuntimeEnvironment *) (ebp - jitCompA0001_EBP128);
1588         r->junkStack -= sizeof (int);
1589         int *pi = (int *)r->junkStack;
1590         r->junkStack -= *pi;
1591 #if 0
1592         int width = jitCompA000_dataWidth(r->ireg[typ]);
1593         void *p = r->junkStack;
1594         r->junkStack -= width * r->ireg[len];
1595 #endif
1596         return;
1597 }
1598
1599 void funcf6(char *ebp, int pxx, int typ, int len)
1600 {
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)
1604                 (*(r->errHndl))(r);
1605         void *p = malloc(width * r->ireg[len]);
1606         r->preg[pxx].p = p;
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;
1612                 pi = p;
1613                 for (i = 0; i < i1; i++)
1614                         pi[i] = 0;
1615                 for (i = 1; i < i1; i += 8)
1616                         pi[i] |= -1;
1617         }
1618         return;
1619 }
1620
1621 void funcf7(char *ebp, int pxx, int typ, int len)
1622 {
1623         HOSECPU_RuntimeEnvironment *r = (HOSECPU_RuntimeEnvironment *) (ebp - jitCompA0001_EBP128);
1624         free(r->preg[pxx].p);
1625         return;
1626 }
1627
1628 void errHndl(HOSECPU_RuntimeEnvironment *r)
1629 {
1630         r = (HOSECPU_RuntimeEnvironment *) (((char *)r) - jitCompA0001_EBP128);
1631         (*(r->errHndl))(r);
1632         // ここに帰ってきてはいけない.
1633 }
1634
1635 /*
1636  * jitcの出力コードをひとまとめにする関数を作成しその中身をjitCompile()で生成
1637  *
1638  * qq : 出力バイナリの書き込み位置のアドレスへの参照(書き込み位置を呼び出しに反映させるため参照渡しにする)
1639  * q1 : 出力バイナリの書き込み位置のアドレスの最大値
1640  * p0 : (*.ose)バイナリの読み込み位置のアドレス(ヘッダ部除去済)
1641  * p1 : (*.ose)バイナリの読み込み位置の取りうる最大値
1642  *      (ただし、「確保したメモリ」の最大値なのでこれより手前にゴミデータが入っているかもしれない)
1643  * ret=1 : ヘッダのエラー
1644  * ret=2 : jitコンパイルエラー
1645  */
1646 int jitc0(unsigned char **qq, unsigned char *q1, const unsigned char *p0, const unsigned char *p1, int level, HOSECPU_LabelListTag *label)
1647 {
1648         unsigned char *q = *qq;
1649         if (p0[0] != 0x05 || p0[1] != SIGN1)    // OSECPUのヘッダ (05E1) を確認
1650                 return 1;
1651
1652         *q++ = 0x55; /* PUSH(EBP); */
1653         *q++ = 0x8b; *q++ = 0x6c; *q++ = 0x24; *q++ = 0x08; /* MOV(EBP,[ESP+8]); */
1654
1655         int i;
1656         for (i = 0; i < JITC_MAXLABELS; i++)
1657                 label[i].opt = 0;
1658
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;
1664         q += i;
1665
1666         *q++ = 0x5d; /* POP(EBP); */
1667         *q++ = 0xc3; /* RET(); */
1668
1669         *qq = q;
1670         return 0;
1671 }
1672
1673 #if (USE_DEBUGGER != 0)
1674
1675 int dbgrGetRegNum(const char *p)
1676 {
1677         int i, j, r = -1;
1678         if (p[2] <= ' ') {
1679                 i = p[0] - '0';
1680                 j = p[1] - '0';
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)
1684                         r = i << 4 | j;
1685         }
1686         return r;
1687 }
1688
1689 void dbgrMain(HOSECPU_RuntimeEnvironment *r)
1690 {
1691         if (r->dbgr == 0) return;
1692         for (;;) {
1693                 char cmd[64], *p;
1694                 int i, j, k;
1695                 printf("\ndbgr>");
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') {
1701                         p = &cmd[2];
1702                         while (*p <= ' ' && *p != '\0') p++;
1703                         if (*p == 'R') {
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]);
1707                                 }
1708                                 else
1709                                         puts("register name error");
1710                                 continue;
1711                         }
1712                         if (*p == 'P') {
1713                                 i = dbgrGetRegNum(p + 1);
1714                                 if (0 <= i && i <= 0x3f) {
1715                                         p = "invalid";
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"
1724                                                 };
1725                                                 p = typName[r->preg[i].typ];
1726                                         }
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;
1730                                                 if (j <= 0) j = 1;
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);
1735                                         }
1736                                         else {
1737                                                 puts("  null pointer");
1738                                         }
1739                                 }
1740                                 else
1741                                         puts("register name error");
1742                                 continue;
1743                         }
1744                 }
1745                 puts("command error");
1746         }
1747         return;
1748 }
1749
1750 #endif
1751
1752
1753 #endif
1754
1755
1756