OSDN Git Service

Automatic OS configurations
[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         int i = 1;
33         if (0x01 <= *src && *src < 0x04) i = 6;
34         if (*src == 0x04) i = 2;
35         if (0x08 <= *src && *src < 0x0d) i = 8 + src[7] * 4;
36         if (0x0e <= *src && *src < 0x10) i = 8;
37         if (0x10 <= *src && *src < 0x2e) i = 4;
38         if (0x1c <= *src && *src < 0x1f) i = 3;
39         if (*src == 0x1f) i = 11;
40         if (*src == 0x2f) i = 4 + src[1];
41         if (0x30 <= *src && *src <= 0x33) i = 4;
42         if (0x3c <= *src && *src <= 0x3d) i = 7;
43         if (*src == 0xfe) i = 2 + src[1];
44         return i;
45 }
46 #if (JITC_ARCNUM == 0x0001)     /* x86-32bit */
47
48 /* 他のCPUへ移植する人へ:
49 以下は最適化のためのものなので、すべて0として簡単に移植しても問題ありません */
50 #define jitCompA0001_USE_R3F_CMPJMP             1*1
51 #define jitCompA0001_USE_R3F_IMM32              1*1
52 #define jitCompA0001_USE_R3F_IMM8               1*1
53 #define jitCompA0001_USE_R3F_INCDEC             1*1
54 #define jitCompA0001_OPTIMIZE_JMP               1*1
55 #define jitCompA0001_OPTIMIZE_MOV               1*1     /* 1にすると速度低下する? */
56 #define jitCompA0001_OPTIMIZE_CMP               1*1
57 #define jitCompA0001_OPTIMIZE_ALIGN             4*1     /* 0-8を想定 */
58 #define jitCompA0001_EBP128                             128*1
59
60 struct JitCompWork {
61         unsigned char *dst, *dst0;
62         int err, maxLabels;
63 #if (jitCompA0001_USE_R3F_IMM32 != 0)
64         int r3f;
65 #endif
66         char prefix;    //CND命令の値を記録(初期値=0)
67 };
68
69 #define jitCompPutByte1(p, c0)                          *p++ = c0
70 #define jitCompPutByte2(p, c0, c1)                      *p++ = c0; *p++ = c1
71 #define jitCompPutByte3(p, c0, c1, c2)          *p++ = c0; *p++ = c1; *p++ = c2
72 #define jitCompPutByte4(p, c0, c1, c2, c3)      *p++ = c0; *p++ = c1; *p++ = c2; *p++ = c3
73
74 static void jitCompPutImm32(struct JitCompWork *w, int i)
75 {
76         jitCompPutByte1(w->dst, i & 0xff);
77         jitCompPutByte1(w->dst, (i >> 8) & 0xff);
78         jitCompPutByte1(w->dst, (i >> 16) & 0xff);
79         jitCompPutByte1(w->dst, (i >> 24) & 0xff);
80         return;
81 }
82
83 int jitCompGetImm32(const unsigned char *src)
84 {
85         return (src[0] << 24) | (src[1] << 16) | (src[2] << 8) | src[3];
86 }
87
88 int jitCompGetLabelNum(struct JitCompWork *w, const unsigned char *src)
89 {
90         int i = jitCompGetImm32(src);
91         if (i < 0 || i >= w->maxLabels) {
92                 w->err = JITC_ERR_LABELNUM;
93                 i = 0;
94         }
95         return i;
96 }
97
98 void jitCompA0001_85DispN(struct JitCompWork *w, int disp, int n)
99 {
100         disp -= jitCompA0001_EBP128;
101         if (-128 <= disp && disp <= 127) {
102                 jitCompPutByte2(w->dst, 0x45 | (n << 3), disp & 0xff);
103         }
104         else {
105                 jitCompPutByte1(w->dst, 0x85 | (n << 3));
106                 jitCompPutImm32(w, disp);
107         }
108         return;
109 }
110
111 void jitCompA0001_movEbpDispReg32(struct JitCompWork *w, int disp, int reg32)
112 {
113         jitCompPutByte1(w->dst, 0x89); /* MOV(mem, reg32); */
114         jitCompA0001_85DispN(w, disp, reg32);
115         return;
116 }
117
118 void jitCompA0001_movReg32EbpDisp(struct JitCompWork *w, int reg32, int disp)
119 {
120         jitCompPutByte1(w->dst, 0x8b); /* MOV(reg32, mem); */
121         jitCompA0001_85DispN(w, disp, reg32);
122         return;
123 }
124
125 void jitCompA0001_movEaxRxx(struct JitCompWork *w, int rxx)
126 {
127 #if (jitCompA0001_USE_R3F_IMM32 != 0)
128         if (rxx == 0x3f) {
129                 jitCompPutByte1(w->dst, 0xb8); /* MOV(EAX, ?); */
130                 jitCompPutImm32(w, w->r3f);
131                 return;
132         }
133 #endif
134         if (rxx >= 0x40 || rxx < 0) w->err = JITC_ERR_REGNUM;
135         jitCompA0001_movReg32EbpDisp(w, 0 /* EAX */, rxx * 4); /* MOV(EAX, [EBP+?]); */
136         return;
137 }
138
139 void jitCompA0001_movRxxEax(struct JitCompWork *w, int rxx)
140 {
141         if (rxx >= 0x40 || rxx < 0) w->err = JITC_ERR_REGNUM;
142         jitCompA0001_movEbpDispReg32(w, rxx * 4, 0 /* EAX */); /* MOV([EBP+?], EAX); */
143         return;
144 }
145
146 void jitCompA0001_fixPrefix(struct JitCompWork *w)
147 {
148         if (w->prefix != 0) {
149                 if (w->dst - w->dst0 > 127) w->err = JITC_ERR_REGNUM;
150                 w->dst0[-1] = (unsigned char)((w->dst - w->dst0) & 0xff);
151         }
152         return;
153 }
154
155 void jitCompA0001_checkCompPtr(struct JitCompWork *w, int p0, int p1)
156 {
157         if (p0 >= 0x3f || p0 < 0) w->err = JITC_ERR_PREGNUM;
158         if (p1 >= 0x3f || p1 < 0) w->err = JITC_ERR_PREGNUM;
159         /* 比較可能可能なのかのチェックのコードを出力 */   /* 未完成 */
160         return;
161 }
162
163 void jitCompA000_loadRegCacheAll(struct JitCompWork *w)
164 {
165         jitCompA0001_movReg32EbpDisp(w, 3 /* EBX */, 0 * 4); /* EBX = R00; */
166         jitCompA0001_movReg32EbpDisp(w, 1 /* ECX */, 1 * 4); /* ECX = R01; */
167         jitCompA0001_movReg32EbpDisp(w, 2 /* EDX */, 2 * 4); /* EDX = R02; */
168         return;
169 }
170
171 void jitCompA000_storeRegCacheAll(struct JitCompWork *w)
172 {
173         jitCompA0001_movEbpDispReg32(w, 0 * 4, 3 /* EBX */); /* R00 = EBX; */
174         jitCompA0001_movEbpDispReg32(w, 1 * 4, 1 /* ECX */); /* R01 = ECX; */
175         jitCompA0001_movEbpDispReg32(w, 2 * 4, 2 /* EDX */); /* R02 = EDX; */
176         return;
177 }
178
179 void jitCompA000_loadRegCacheEcx(struct JitCompWork *w)
180 {
181         jitCompA0001_movReg32EbpDisp(w, 1 /* ECX */, 1 * 4); /* ECX = R01; */
182         return;
183 }
184
185 void jitCompA000_storeRegCacheEcx(struct JitCompWork *w)
186 {
187         jitCompA0001_movEbpDispReg32(w, 1 * 4, 1 /* ECX */); /* R01 = ECX; */
188         return;
189 }
190
191 void jitCompA000_loadRegCacheEdx(struct JitCompWork *w)
192 {
193         jitCompA0001_movReg32EbpDisp(w, 2 /* EDX */, 2 * 4); /* EDX = R02; */
194         return;
195 }
196
197 void jitCompA000_storeRegCacheEdx(struct JitCompWork *w)
198 {
199         jitCompA0001_movEbpDispReg32(w, 2 * 4, 2 /* EDX */); /* R02 = EDX; */
200         return;
201 }
202
203 int jitCompA000_selectRegCache(int rxx, int reg)
204 {
205         if (rxx == 0) reg = 3; /* EBX */
206         if (rxx == 1) reg = 1; /* ECX */
207         if (rxx == 2) reg = 2; /* EDX */
208         return reg;
209 }
210
211 void jitCompA000_loadPRegCacheAll(struct JitCompWork *w)
212 {
213         //      jitCompA0001_movReg32EbpDisp(w, 5 /* EBP */, 256 + 0 * 32 + 0); /* EBP = P00; */
214         jitCompA0001_movReg32EbpDisp(w, 6 /* ESI */, 256 + 1 * 32 + 0); /* ESI = P01; */
215         jitCompA0001_movReg32EbpDisp(w, 7 /* EDI */, 256 + 2 * 32 + 0); /* EDI = P02; */
216         return;
217 }
218
219 void jitCompA000_storePRegCacheAll(struct JitCompWork *w)
220 {
221         //      jitCompA0001_movEbpDispReg32(w, 256 + 0 * 32 + 0, 5 /* EBP */); /* P00 = EBP; */
222         jitCompA0001_movEbpDispReg32(w, 256 + 1 * 32 + 0, 6 /* ESI */); /* P01 = ESI; */
223         jitCompA0001_movEbpDispReg32(w, 256 + 2 * 32 + 0, 7 /* EDI */); /* P02 = EDI; */
224         return;
225 }
226
227 int jitCompA000_selectPRegCache(int pxx, int reg)
228 {
229         //      if (pxx == 0) reg = 5; /* EBP */
230         if (pxx == 1) reg = 6; /* ESI */
231         if (pxx == 2) reg = 7; /* EDI */
232         return reg;
233 }
234
235 int jitCompA000_convTyp(int t)
236 {
237         int r = -1;
238         if (1 <= t && t <= 7) r = t;
239         if (8 <= t && t <= 13) r = 2 | (t & 1);
240         if (14 <= t && t <= 15) r = 4 | (t & 1);
241         if (16 <= t && t <= 21) r = 6 | (t & 1);
242         return r;
243 }
244
245 int jitCompA000_dataWidth(int t)
246 {
247         int r = -1;
248         if (t == 0x0001) r = 256;
249         t >>= 1;
250         if (t == 0x0002 / 2) r = 8;
251         if (t == 0x0004 / 2) r = 16;
252         if (t == 0x0006 / 2) r = 32;
253         if (t == 0x0008 / 2) r = 4;
254         if (t == 0x000a / 2) r = 2;
255         if (t == 0x000c / 2) r = 1;
256         if (t == 0x000e / 2) r = 12;
257         if (t == 0x0010 / 2) r = 20;
258         if (t == 0x0012 / 2) r = 24;
259         if (t == 0x0014 / 2) r = 28;
260         return r;
261 }
262
263 static unsigned char *errfnc;
264
265 void jitCompA0001_checkType0(struct JitCompWork *w, int pxx, int typ, int ac)
266 {
267         if (typ <= 0) { w->err = JITC_ERR_BADTYPE; }
268         if (typ > 0x7f) { w->err = JITC_ERR_INTERNAL; }
269         jitCompA0001_movReg32EbpDisp(w, 0 /* EAX */, 256 + pxx * 32 + 4); /* MOV(EAX, [EBP+?]); */      /* typ */
270         jitCompPutByte3(w->dst, 0x83, 0xf8, typ & 0x7f);        /* CMP(EAX, ?); */
271         jitCompPutByte2(w->dst, 0x0f, 0x85); /* JNE */
272         jitCompPutImm32(w, errfnc - (w->dst + 4));
273         return;
274 }
275
276 void jitCompA0001_checkType(struct JitCompWork *w, int pxx, int typ, int ac)
277 // data用.
278 // 将来的にはaliveやアクセス権チェックも入れる
279 {
280         jitCompA0001_checkType0(w, pxx, typ, ac);
281         return;
282 }
283
284 void jitCompA0001_checkLimit(struct JitCompWork *w, int reg, int pxx)
285 {
286         jitCompPutByte1(w->dst, 0x3b);  /* CMP(reg, [EBP+?]); */
287         jitCompA0001_85DispN(w, 256 + pxx * 32 + 8, reg);       /* p0 */
288         jitCompPutByte2(w->dst, 0x0f, 0x82); /* JB */
289         jitCompPutImm32(w, errfnc - (w->dst + 4));
290         jitCompPutByte1(w->dst, 0x3b);  /* CMP(reg, [EBP+?]); */
291         jitCompA0001_85DispN(w, 256 + pxx * 32 + 12, reg);      /* p1 */
292         jitCompPutByte2(w->dst, 0x0f, 0x83); /* JAE */
293         jitCompPutImm32(w, errfnc - (w->dst + 4));
294         return;
295 }
296
297 void func3c(char *ebp, int opt, int r1, int p1, int lenR, int lenP, int r0, int p0);
298 void func3d(char *ebp, int opt, int r1, int p1, int lenR, int lenP, int r0, int p0);
299 void funcf4(char *ebp, int pxx, int typ, int len);
300 void funcf5(char *ebp, int pxx, int typ, int len); // pxxはダミーで参照されない.
301 void funcf6(char *ebp, int pxx, int typ, int len);
302 void funcf7(char *ebp, int pxx, int typ, int len); // typとlenはダミーで参照されない.
303 // F5の場合、decoderが対応するalloc-freeを結びつけるのが簡単で、typやlenを指定必須にしてもフロントエンドコードに影響はない.
304
305 void errHndl(HOSECPU_RuntimeEnvironment *r);
306
307 /*
308  * dst : 現在の書き込みアドレス。
309  * dst1 : 書き込みアドレスの最大値
310  * src : 現在の読み込みアドレス(ヘッダ部は飛ばしてある
311  * src1 : 読み込みアドレスの最大値
312  * src0 : 読み込みバイナリのアドレス
313  */
314 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)
315 /* IA-32用 */
316 /* 本来ならこのレイヤでは文法チェックしない */
317 {
318         struct JitCompWork w;
319         unsigned char *dst00 = dst, *enter0 = NULL, *tmp_ucp;
320     char *errmsg = "";
321         const unsigned char *oldsrc;
322         int timecount = 0, i, j = 0, lastlabel = -1, debugInfo0 = -1;
323         int reg0, reg1, reg2, cmp0reg = -1, cmp0lev = 0;
324         w.dst = w.dst0 = dst;
325         w.err = 0;
326         w.maxLabels = maxLabels;
327
328         if ((flags & JITC_NOSTARTUP) == 0) {
329                 jitCompPutByte1(w.dst, 0x60); /* PUSHAD(); */
330                 jitCompA000_loadRegCacheAll(&w); /* start-up */
331                 jitCompA000_loadPRegCacheAll(&w);
332         }
333         if (level <= JITC_LV_SLOWER) {
334                 jitCompPutByte2(w.dst, 0x31, 0xc0);     /* XOR(EAX, EAX); */
335                 jitCompA0001_movEbpDispReg32(&w, 2304 + 0, 0 /* EAX */); /* MOV(debugInfo0, EAX); */
336                 jitCompPutByte1(w.dst, 0xb8);   /* MOV(EAX, ?); */
337                 jitCompPutImm32(&w, debugInfo1);
338                 jitCompA0001_movEbpDispReg32(&w, 2304 + 4, 0 /* EAX */); /* MOV(debugInfo1, EAX); */
339         }
340         while (src < src1) {
341                 w.prefix = 0;   //0x04 CND 命令で変更される
342                 if (w.dst + 256 > dst1) { w.err = JITC_ERR_DST1; goto err_w; }  // 書き込み領域が残り256バイト未満ならエラー
343                 timecount++;
344                 if (timecount >= 64) {
345                         timecount -= 64;
346                         /* 未完成(timeoutチェックコードを入れる) */
347                 }
348         prefix_continue:        // CND命令実行後ここに戻る
349                 switch (*src) {
350
351                 case 0x00:      /* NOP */
352                         if (w.prefix != 0) { w.err = JITC_ERR_PREFIX; goto err_w; }     // 「条件付きでNOPを実行」するなんて、矛盾している!
353                         break;
354
355                 case 0x01:      /* LB */
356                         
357                         /*
358                          * LB : ラベル設置命令。(6byte)
359                          * ・prefex = 1にする
360                          * ・timecount++し、timecountのチェックをする。
361                          * ・ラベル位置を登録する。
362                          * ・割り込みがある場合、このタイミングで割り込みを発生させる。
363                          *
364                          *  1   2       3       456
365                          *      LB      01      opt     imm32
366                          *
367                          */
368                         
369                         if (enter0 == NULL && (src[6] == 0x3c /* 多数のレジスタをスタックに退避 */ || (src[6] == 0xfe/* REMARK */ && src[7] == 0x01 && src[9] == 0x3c))) {       //beginFunc()中のLB
370                                 // LB命令の後に0x3C命令・・・beginFunc()
371                                 jitCompPutByte1(w.dst, 0xe9);   // (x86) JMP rel32 : 次の命令との相対オフセットだけ相対ニアジャンプする
372                                 enter0 = w.dst;
373                                 jitCompPutImm32(&w, 0); // 飛び相対座標が0 ・・・パイプラインのフラッシュ??
374                         }
375                         if (src[6] == 0x34) {   // LBの次の命令がDATA ・・・DAT_SA0(label, typ32, length) ・・・メモリ確保命令
376                                 tmp_ucp = w.dst;
377                                 jitCompPutByte1(w.dst, 0xe9);   // (x86) JMP rel32 : 次の命令との相対オフセットだけ相対ニアジャンプする
378                                 i = jitCompGetImm32(&src[7]);   // type32 を取得
379                                 j = 32;
380                                 if (i != 1) {
381                                         i = jitCompA000_convTyp(i);
382                                         j = 0;
383                                         if (i == 2 || i == 3) { j = 1; }
384                                         if (i == 4 || i == 5) { j = 2; }
385                                         if (i == 6 || i == 7) { j = 4; }
386                                 }
387                                 j *= jitCompGetImm32(&src[11]);
388                                 if (j <= 0) w.err = JITC_ERR_BADTYPE;
389                                 jitCompPutImm32(&w, j);
390 #if (jitCompA0001_OPTIMIZE_JMP != 0)
391                                 if (j <= 127 - jitCompA0001_OPTIMIZE_ALIGN) {
392                                         w.dst -= 5;
393                                         jitCompPutByte2(w.dst, 0xeb, j);
394                                 }
395 #endif
396                         }
397 #if (jitCompA0001_OPTIMIZE_ALIGN != 0)
398                         for (;;) {
399                                 i = ((int)w.dst) & (jitCompA0001_OPTIMIZE_ALIGN - 1); /* ALIGNで割ったあまりを計算 */
400                                 if (i == 0) break;
401                                 i = jitCompA0001_OPTIMIZE_ALIGN - i;
402                                 if (i == 1) { jitCompPutByte1(w.dst, 0x90); j += i; } /* NOP(); */
403                                 if (i == 2) { jitCompPutByte2(w.dst, 0x89, 0xc0); j += i; } /* MOV(EAX, EAX); */
404                                 if (i == 3) { jitCompPutByte3(w.dst, 0x8d, 0x76, 0x00); j += i; } /* LEA(ESI, [ESI+0]); */
405                                 if (i == 4) { jitCompPutByte4(w.dst, 0x8d, 0x74, 0x26, 0x00); j += i; } /* LEA(ESI, [ESI*1+0]); */
406                                 if (i == 5) { jitCompPutByte1(w.dst, 0x0d); jitCompPutImm32(&w, 0); j += i; } /* OR(EAX, 0); */
407                                 if (i == 6) { jitCompPutByte2(w.dst, 0x8d, 0xb6); jitCompPutImm32(&w, 0); j += i; } /* LEA(ESI, [ESI+0]); */
408                                 if (i >= 7) { jitCompPutByte3(w.dst, 0x8d, 0xb4, 0x26); jitCompPutImm32(&w, 0); j += 7; } /* LEA(ESI, [ESI*1+0]); */
409                         }
410 #endif
411                         if (src[6] == 0x34) {
412                                 tmp_ucp[1] = j & 0xff;
413                                 if (*tmp_ucp == 0xe9) {
414                                         tmp_ucp[2] = (j >> 8) & 0xff;
415                                         tmp_ucp[3] = (j >> 16) & 0xff;
416                                         tmp_ucp[4] = (j >> 24) & 0xff;
417                                 }
418                         }
419                         if ((flags & JITC_PHASE1) == 0) {
420                                 i = jitCompGetLabelNum(&w, src + 2);
421                                 //printf("i=%06X %06X\n", i, src-src0);
422                                 if (label[i].opt != 0 && w.err == 0) { w.err = JITC_ERR_LABELREDEF; goto err_w; }
423                                 if (w.prefix != 0) { w.err = JITC_ERR_PREFIX; goto err_w; }
424                                 label[i].opt = src[1] + 1;
425                                 label[i].typ = 0; /* TYP_CODE */
426                                 label[i].p = w.dst;
427                                 label[i].p1 = w.dst + 1;
428                                 lastlabel = i;
429                         }
430                         cmp0reg = -1;
431                         timecount = 0;
432                         /* 未完成(timeoutチェックコードを入れる) */
433                         break;
434
435                 case 0x02:      /* LIMM */      
436                         
437                         /*
438                          * LIMM : 定数即値代入命令(6byte)
439                          * 
440                          *      1       2               3456
441                          *      02      reg0R   imm32
442                          *
443                          * ・reg3F は条件比較慣用句指定用&演算命令即値慣用句指定用。よってCND命令の直後では使用できない。
444                          */
445                         
446                         if (src[1] == 0x3f && w.prefix != 0) w.err = JITC_ERR_PREFIX;   // CND命令の直後でR3Fを書き換えるなんて変だよね
447
448 #if (jitCompA0001_USE_R3F_IMM32 != 0)
449                         if (src[1] == 0x3f) {           // R3Fへの代入は例外敵に、 w.r3f を使用
450                                 w.r3f = jitCompGetImm32(src + 2);
451                                 break;
452                         }
453 #endif
454                         i = jitCompGetImm32(src + 2);   // 与えられた即値(第二引数)を取得
455
456                         /* R00-R02 なら EBX, ECX, EDX 、それ以外なら EAX のレジスタIDを reg0 に代入 */
457                         reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
458
459 #if (jitCompA0001_OPTIMIZE_MOV != 0)
460                         if (i == 0) {
461                                 jitCompPutByte2(w.dst, 0x31, 0xc0 | reg0 << 3 | reg0);  /* XOR(reg0, reg0); */
462                                 jitCompA0001_movRxxEax(&w, src[1]);
463                                 break;
464                         }
465 #endif
466                         
467                         /* reg0 のレジスタに対応したMOV命令を発行 */
468                         jitCompPutByte1(w.dst, 0xb8 | reg0);    /* MOV(reg0, ?);  == 10111000b+wr imm32 */
469                         jitCompPutImm32(&w, i);
470
471                         if (reg0 == 0)  // R03以降の、レジスタの内容をメモリ上に格納してエミュレートする場合
472                                 
473                                 jitCompA0001_movRxxEax(&w, src[1]);
474                         break;
475
476                 case 0x03:      /* PLIMM */     /* 未完成(plsまで対応) */
477
478                         /*
479                         * PLIMM : ラベル番号代入命令(6byte)
480                         *
481                         *       1       2       3456
482                         *       03      PXX     imm32
483                         *
484                         * ・P28 はAPI用
485                         * ・P30 はリターンアドレス
486                         * ・P3F はプログラムカウンタ
487                         */
488
489                         i = jitCompGetLabelNum(&w, src + 2);    // Pxxに代入するラベルの番号(第二引数)
490                         if ((flags & JITC_PHASE1) != 0 && w.err == 0) { // Phase 1であるならば
491                                 if (label[i].opt == 0) { w.err = JITC_ERR_LABELNODEF; goto err_w; }             // 指定されたラベル番号は存在しない
492                                 if (src[1] != 0x3f && label[i].opt != 2) { w.err = JITC_ERR_LABELTYP; goto err_w; }     // 
493                                 if (src[1] == 0x3f && label[i].typ != 0) { w.err = JITC_ERR_LABELTYP; goto err_w; } // プログラムカウンタに TYP_CODEでない値は代入できない
494                         }
495                         if (src[1] == 0x3f) {   // プログラムカウンタへの代入なら
496                                 if (w.prefix == 0) {    // CND命令による条件付きでなければ、即座に移動
497                                         jitCompPutByte1(w.dst, 0xe9); /* JMP(?); */
498                                 }
499                                 else {  // 直前はCND命令。
500                                         
501                                         /*
502                                          * CND命令
503                                          *      1       2       
504                                          *      04      reg0R
505                                          *
506                                          * いま、dstの末端はJZ命令になっている。 0x0F 0x84 cd
507                                          */
508
509                                          // JZのとび先アドレスの書き換え?
510                                         w.dst[-1] = w.dst[-2] ^ 0xf1; /* 74->85, 75->84 */
511                                         w.dst[-2] = 0x0f;
512
513                                         w.prefix = 0;
514                                 }
515                                 j = 0;
516                                 if ((flags & JITC_PHASE1) != 0 || (((flags & JITC_PHASE1) == 0) && label[i].opt != 0))  // label番号iが確保されていれば (このif文は意味をなさない)
517                                         j = label[i].p - (w.dst + 4);   // j はとび先の相対番地 
518                                 jitCompPutImm32(&w, j); // JMP もしくは JZ 命令のアドレス部を記述
519 #if (jitCompA0001_OPTIMIZE_JMP != 0)
520                                 if (-128 - 3 <= j && j < 0) {
521                                         if (w.dst[-5] == 0xe9) {
522                                                 j += 3;
523                                                 w.dst -= 5;
524                                                 jitCompPutByte1(w.dst, 0xeb); /* JMP(?); */
525                                         }
526                                         else {
527                                                 j += 4;
528                                                 w.dst -= 6;
529                                                 jitCompPutByte1(w.dst, w.dst[1] ^ 0xf0);
530                                         }
531                                         jitCompPutByte1(w.dst, j & 0xff);
532                                 }
533 #endif
534                         }
535                         else {  // プログラムカウンタ以外への代入
536                                 
537                                 // 代入先が P01, P02なら ESI, EDI,それ以外ならEAXを指定
538                                 reg0 = jitCompA000_selectPRegCache(src[1], 0 /* EAX */);
539                                 jitCompPutByte1(w.dst, 0xb8 | reg0);    /* MOV(reg0, ?); */
540                                 jitCompPutImm32(&w, (int)label[i].p);   // ラベルのパスを各レジスタに代入
541
542                                 // レジスタへの代入をメモリでエミュレーションする場合は、スタックに積む。
543                                 if (reg0 == 0)
544                                         jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32, 0); /* MOV([EBP+?], EAX); */
545
546                                 if (level < JITC_LV_FASTEST) {
547                                         jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 8, reg0); /* MOV([EBP+?], reg0); */ /* p0 */
548                                         jitCompPutByte1(w.dst, 0xb8); /* MOV(EAX, ?); */
549                                         jitCompPutImm32(&w, label[i].typ);
550                                         jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 4, 0); /* MOV([EBP+?], EAX); */ /* typ */
551                                         jitCompPutByte1(w.dst, 0xb8); /* MOV(EAX, ?); */
552                                         jitCompPutImm32(&w, (int)label[i].p1);
553                                         jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 12, 0); /* MOV([EBP+?], EAX); */ /* p1 */
554                                         jitCompPutByte2(w.dst, 0x31, 0xc0);     /* XOR(EAX, EAX); */
555                                         jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 16, 0); /* MOV([EBP+?], EAX); */ /* liveSign */
556                                         jitCompA0001_movReg32EbpDisp(&w, 0, 2320); /* MOV(EAX, ptrCtrl); */
557                                         jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 20, 0); /* MOV([EBP+?], EAX); */ /* pls */
558                                 }
559                         }
560                         break;
561
562                 case 0x04:      /* CND (prefix) */
563
564                         /*
565                          * CND命令
566                          * 与えられたRxxの最下位ビットが1であれば後続の命令を実行、そうでなければ飛ばす。
567                          */
568                         
569                         if (src[1] >= 0x40) w.err = JITC_ERR_REGNUM;    // R00-R3F 以外のレジスタは比較対象にできない
570
571                         // 比較対象のレジスタがメモリ上にあれば-1, それ以外なら適切なレジスタ番号を返す
572                         reg0 = jitCompA000_selectRegCache(src[1], -1 /* mem */);
573
574                         /* TEST命令を発行 */
575                         if (reg0 < 0) { //比較対象のレジスタはメモリ上にある
576                                 jitCompPutByte1(w.dst, 0xf7); /* TEST([EBP+?],1); */
577                                 jitCompA0001_85DispN(&w, src[1] * 4, 0);
578                         }
579                         else {
580                                 jitCompPutByte2(w.dst, 0xf7, 0xc0 | reg0); /* TEST(reg0,1); */
581                         }
582                         jitCompPutImm32(&w, 1);
583
584                         /* JZ命令を発行 */
585                         jitCompPutByte2(w.dst, 0x74, 0x00);     /* JZ($+2) */
586                         cmp0reg = -1;
587                         if (w.err != 0) goto err_w;
588                         src += 2;
589                         w.prefix = 1;   // プリフィックスをセット
590                         w.dst0 = w.dst; 
591                         goto prefix_continue;
592
593                 case 0x08: /* LMEM */   /* 完成 */
594                         i = jitCompGetImm32(src + 2);
595                         if (i == 0x0001) w.err = JITC_ERR_BADTYPE;
596                         if (level < JITC_LV_FASTER) {
597                                 jitCompA0001_checkType(&w, src[6], i, 0); // read
598                                 cmp0reg = -1;
599                         }
600                         reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
601                         reg1 = jitCompA000_selectPRegCache(src[6], 2 /* EDX */);
602                         if (reg0 != 0 /* EAX */ && reg1 == 2 /* EDX */)
603                                 reg1 = 0; /* EAX */
604                         if (reg1 == 2 /* EDX */)
605                                 jitCompA000_storeRegCacheEdx(&w);
606                         if (reg1 <= 3 /* EAX, EDX */)
607                                 jitCompA0001_movReg32EbpDisp(&w, reg1, 256 + src[6] * 32 + 0); /* MOV(reg1, [EBP+?]); */
608                         if (level < JITC_LV_FASTER)
609                                 jitCompA0001_checkLimit(&w, reg1, src[6]);
610                         i = jitCompA000_convTyp(jitCompGetImm32(src + 2));
611                         switch (i) {
612                         case 0x0002:
613                                 jitCompPutByte3(w.dst, 0x0f, 0xbe, reg0 << 3 | reg1);   /* MOVSX(reg0,BYTE [reg1]); */
614                                 break;
615                         case 0x0003:
616                                 jitCompPutByte3(w.dst, 0x0f, 0xb6, reg0 << 3 | reg1);   /* MOVZX(reg0,BYTE [reg1]); */
617                                 break;
618                         case 0x0004:
619                                 jitCompPutByte3(w.dst, 0x0f, 0xbf, reg0 << 3 | reg1);   /* MOVSX(reg0,WORD [reg1]); */
620                                 break;
621                         case 0x0005:
622                                 jitCompPutByte3(w.dst, 0x0f, 0xb7, reg0 << 3 | reg1);   /* MOVZX(reg0,WORD [reg1]); */
623                                 break;
624                         case 0x0006:
625                         case 0x0007:
626                                 jitCompPutByte2(w.dst, 0x8b, reg0 << 3 | reg1); /* MOV(reg0, [reg1]); */
627                                 break;
628                         default:
629                                 w.err = JITC_ERR_BADTYPE;
630                         }
631                         if (reg0 == 0 /* EAX */)
632                                 jitCompA0001_movRxxEax(&w, src[1]);
633                         if (reg1 == 2 /* EDX */)
634                                 jitCompA000_loadRegCacheEdx(&w);
635                         break;
636
637                 case 0x09: /* SMEM */   /* 完成 */
638                         i = jitCompGetImm32(src + 2);
639                         if (i == 0x0001) w.err = JITC_ERR_BADTYPE;
640                         if (level < JITC_LV_FASTER) {
641                                 jitCompA0001_checkType(&w, src[6], i, 1); // write
642                                 cmp0reg = -1;
643                         }
644                         reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
645                         reg1 = jitCompA000_selectPRegCache(src[6], 2 /* EDX */);
646                         if (reg0 != 0 /* EAX */ && reg1 == 2 /* EDX */)
647                                 reg1 = 0; /* EAX */
648                         if (reg1 == 2 /* EDX */)
649                                 jitCompA000_storeRegCacheEdx(&w);
650                         if (reg1 <= 3 /* EAX, EDX */)
651                                 jitCompA0001_movReg32EbpDisp(&w, reg1, 256 + src[6] * 32 + 0); /* MOV(reg1, [EBP+?]); */
652                         if (level < JITC_LV_FASTER)
653                                 jitCompA0001_checkLimit(&w, reg1, src[6]);
654                         if (reg0 == 0 /* EAX */)
655                                 jitCompA0001_movEaxRxx(&w, src[1]);
656                         /* 値の範囲チェック */
657                         i = jitCompA000_convTyp(jitCompGetImm32(src + 2));
658                         switch (i) {
659                         case 0x0002:
660                         case 0x0003:
661                                 jitCompPutByte2(w.dst, 0x88, reg0 << 3 | reg1); /* MOV([reg1], BYTE(reg0)); */
662                                 break;
663                         case 0x0004:
664                         case 0x0005:
665                                 jitCompPutByte3(w.dst, 0x66, 0x89, reg0 << 3 | reg1);   /* MOV([reg1], WORD(reg0)); */
666                                 break;
667                         case 0x0006:
668                         case 0x0007:
669                                 jitCompPutByte2(w.dst, 0x89, reg0 << 3 | reg1); /* MOV([reg1], reg0); */
670                                 break;
671                         default:
672                                 w.err = JITC_ERR_BADTYPE;
673                         }
674                         if (reg1 == 2 /* EDX */)
675                                 jitCompA000_loadRegCacheEdx(&w);
676                         break;
677
678                 case 0x0a: /* PLMEM */  /* 完成 */
679                         i = jitCompGetImm32(src + 2);
680                         if (i != 0x0001) w.err = JITC_ERR_BADTYPE;
681                         if (level < JITC_LV_FASTER) {
682                                 jitCompA0001_checkType(&w, src[6], i, 0); // read
683                                 cmp0reg = -1;
684                         }
685                         reg0 = jitCompA000_selectPRegCache(src[1], 0 /* EAX */);
686                         reg1 = jitCompA000_selectPRegCache(src[6], 2 /* EDX */);
687                         //      if (reg0 != 0 /* EAX */ && reg1 == 2 /* EDX */) /* これをやってはいけない!(by K, 2013.08.02) */
688                         //              reg1 = 0; /* EAX */
689                         if (reg0 == reg1 && reg0 != 0) {        // bugfix: hinted by yao, 2013.09.14. thanks!
690                                 jitCompA000_storePRegCacheAll(&w);
691                                 reg1 = 2; /* EDX */
692                         }
693                         if (reg1 == 2 /* EDX */)
694                                 jitCompA000_storeRegCacheEdx(&w);
695                         if (reg1 <= 3 /* EAX, EDX */)
696                                 jitCompA0001_movReg32EbpDisp(&w, reg1, 256 + src[6] * 32 + 0); /* MOV(reg1, [EBP+?]); */
697                         if (level < JITC_LV_FASTER)
698                                 jitCompA0001_checkLimit(&w, reg1, src[6]);
699                         jitCompPutByte2(w.dst, 0x8b, reg0 << 3 | reg1); /* MOV(reg0, [reg1]); */
700                         if (reg0 == 0 /* EAX */)
701                                 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 0, 0); /* MOV([EBP+?], EAX); */
702                         for (i = 4; i < 32; i += 4) {
703                                 jitCompPutByte3(w.dst, 0x8b, 0x40 | reg1, i);   /* MOV(EAX, [reg1+?]); */
704                                 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + i, 0 /* EAX */); /* MOV([EBP+?], EAX); */
705                         }
706                         if (reg1 == 2 /* EDX */)
707                                 jitCompA000_loadRegCacheEdx(&w);
708                         break;
709
710                 case 0x0b: /* PSMEM */  /* 完成 */
711                         i = jitCompGetImm32(src + 2);
712                         if (i != 0x0001) w.err = JITC_ERR_BADTYPE;
713                         if (level < JITC_LV_FASTER) {
714                                 jitCompA0001_checkType(&w, src[6], i, 1); // write
715                                 cmp0reg = -1;
716                         }
717                         reg0 = jitCompA000_selectPRegCache(src[1], 0 /* EAX */);
718                         reg1 = jitCompA000_selectPRegCache(src[6], 2 /* EDX */);
719                         //      if (reg0 != 0 /* EAX */ && reg1 == 2 /* EDX */) /* これをやってはいけない!(by K, 2013.08.02) */
720                         //              reg1 = 0; /* EAX */
721                         if (reg1 == 2 /* EDX */)
722                                 jitCompA000_storeRegCacheEdx(&w);
723                         if (reg1 <= 3 /* EAX, EDX */)
724                                 jitCompA0001_movReg32EbpDisp(&w, reg1, 256 + src[6] * 32 + 0); /* MOV(reg1, [EBP+?]); */
725                         if (level < JITC_LV_FASTER)
726                                 jitCompA0001_checkLimit(&w, reg1, src[6]);
727                         if (reg0 == 0 /* EAX */)
728                                 jitCompA0001_movReg32EbpDisp(&w, reg0, 256 + src[1] * 32 + 0); /* MOV(reg0, [EBP+?]); */
729                         jitCompPutByte2(w.dst, 0x89, reg0 << 3 | reg1); /* MOV([reg1], reg0); */
730                         for (i = 4; i < 32; i += 4) {
731                                 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[1] * 32 + i); /* MOV(EAX, [EBP+?]); */
732                                 jitCompPutByte3(w.dst, 0x89, 0x40 | reg1, i);   /* MOV([reg1+?], EAX); */
733                         }
734                         if (reg1 == 2 /* EDX */)
735                                 jitCompA000_loadRegCacheEdx(&w);
736                         break;
737
738                 case 0x0e: /* PADD */           /* 完成 */
739                         if (level < JITC_LV_FASTER) {
740                                 jitCompA0001_checkType0(&w, src[6], jitCompGetImm32(src + 2), 2); // other, aliveテストはとりあえずしない.
741                                 cmp0reg = -1;
742                         }
743                         reg0 = jitCompA000_selectPRegCache(src[1], 0 /* EAX */);
744                         reg1 = jitCompA000_selectPRegCache(src[6], -1 /* mem */);
745                         if (reg1 < 0 /* mem */)
746                                 jitCompA0001_movReg32EbpDisp(&w, reg0, 256 + src[6] * 32 + 0); /* MOV(reg0, [EBP+?]); */
747                         if (reg1 >= 0 && reg0 != reg1) {
748                                 jitCompPutByte2(w.dst, 0x89, 0xc0 | reg1 << 3 | reg0); /* MOV(reg0, reg1); */
749                         }
750                         i = jitCompGetImm32(src + 2);
751                         j = -1;
752                         if (i == 1)
753                                 j = 5; /* 32 */
754                         else {
755                                 i = jitCompA000_convTyp(i);
756                                 if (0x0002 <= i && i <= 0x0007)
757                                         j = (i - 0x0002) >> 1;
758                         }
759                         if (j < 0) { w.err = JITC_ERR_BADTYPE; goto err_w; }
760 #if (jitCompA0001_USE_R3F_IMM32 != 0)
761                         if (src[7] == 0x3f) {
762                                 j = w.r3f << j;
763 #if (jitCompA0001_USE_R3F_IMM8 != 0)
764                                 if (-0x80 <= j && j <= 0x7f) {
765 #if (jitCompA0001_USE_R3F_INCDEC != 0)
766                                         if (j == 1) { jitCompPutByte1(w.dst, 0x40 | reg0); goto padd1; } /* INC */
767                                         if (j == -1) { jitCompPutByte1(w.dst, 0x48 | reg0); goto padd1; } /* DEC */
768 #endif
769                                         jitCompPutByte3(w.dst, 0x83, 0xc0 | reg0, j & 0xff);    /* ADD(reg0, im8); */
770                                         goto padd1;
771                                 }
772 #endif
773                                 if (reg0 == 0) {
774                                         jitCompPutByte1(w.dst, 0x05);   /* ADD(reg0, ?); */
775                                 }
776                                 else {
777                                         jitCompPutByte2(w.dst, 0x81, 0xc0 | reg0);      /* ADD(reg0, ?); */
778                                 }
779                                 jitCompPutImm32(&w, j);
780                                 goto padd1;
781                         }
782 #endif
783                         if (src[7] >= 0x40) w.err = JITC_ERR_REGNUM;
784                         if (j == 0) {
785                                 reg1 = jitCompA000_selectRegCache(src[7], -1 /* mem */);
786                                 if (reg1 >= 0) {
787                                         jitCompPutByte2(w.dst, 0x01, 0xc0 | reg1 << 3 | reg0);  /* ADD(reg0, reg1); */
788                                 }
789                                 else {
790                                         jitCompPutByte1(w.dst, 0x03);   /* ADD(reg0, [EBP+?]); */
791                                         jitCompA0001_85DispN(&w, src[7] * 4, reg0);
792                                 }
793                         }
794                         else {
795                                 reg1 = jitCompA000_selectRegCache(src[7], -1 /* mem */);
796                                 reg2 = 2; /* EDX */
797                                 jitCompA000_storeRegCacheEdx(&w);
798                                 if (reg1 < 0)
799                                         jitCompA0001_movReg32EbpDisp(&w, reg2, src[7] * 4); /* MOV(reg2, [EBP+?]); */
800                                 if (reg1 >= 0 && reg1 != reg2) {
801                                         jitCompPutByte2(w.dst, 0x89, 0xc0 | reg1 << 3 | reg2); /* MOV(reg2, reg1); */
802                                 }
803                                 jitCompPutByte3(w.dst, 0xc1, 0xe0 | reg2, j);   /* SHL(reg2, ?); */
804                                 jitCompPutByte2(w.dst, 0x01, 0xc0 | reg2 << 3 | reg0);  /* ADD(reg0, reg2); */
805                                 jitCompA000_loadRegCacheEdx(&w);
806                         }
807 #if (jitCompA0001_USE_R3F_IMM32 != 0)
808                 padd1:
809 #endif
810                         if (reg0 == 0 /* EAX */)
811                                 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 0, reg0); /* MOV([EBP+?], reg0); */
812                         if (src[1] != src[6]) {
813                                 for (i = 4; i < 32; i += 4) {
814                                         jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[6] * 32 + i); /* MOV(EAX, [EBP+?]); */
815                                         jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + i, 0 /* EAX */); /* MOV([EBP+?], EAX); */
816                                 }
817                         }
818                         cmp0reg = -1;
819                         break;
820
821                 case 0x0f: /* PDIF */   /* 未完成 */
822                         reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
823                         jitCompA000_storePRegCacheAll(&w); // 手抜き.
824                         jitCompA0001_checkCompPtr(&w, src[6], src[7]);
825                         jitCompA0001_movReg32EbpDisp(&w, reg0, 256 + src[6] * 32 + 0); /* MOV(reg0, [EBP+?]); */
826                         jitCompPutByte1(w.dst, 0x2b);   /* SUB(EAX, [EBP+?]); */
827                         jitCompA0001_85DispN(&w, 256 + src[7] * 32 + 0, reg0);
828                         i = jitCompA000_convTyp(jitCompGetImm32(src + 2));
829                         j = -1;
830                         if (0x0002 <= i && i <= 0x0007)
831                                 j = (i - 0x0002) >> 1;
832                         if (j < 0) { w.err = JITC_ERR_BADTYPE; goto err_w; }
833                         if (j > 0) {
834                                 jitCompPutByte3(w.dst, 0xc1, 0xf8 | reg0, j);   /* SAR(reg0,?); */
835                         }
836                         if (reg0 == 0 /* EAX */)
837                                 jitCompA0001_movRxxEax(&w, src[1]);
838                         cmp0reg = src[1]; cmp0lev = 1;
839                         break;
840
841                 case 0x10:      /* OR */
842                 case 0x11:      /* XOR */
843                 case 0x12:      /* AND */
844                 case 0x14:      /* ADD */
845                 case 0x15:      /* SUB */
846                 case 0x16:      /* MUL */
847                         if (src[1] >= 0x3f) w.err = JITC_ERR_REGNUM;
848                         reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
849                         reg1 = jitCompA000_selectRegCache(src[2], -1 /* mem */);
850 #if (jitCompA0001_USE_R3F_IMM32 != 0)
851                         if (src[2] == 0x3f) {   // SUBのみ該当.
852                                 if (*src != 0x15) w.err = JITC_ERR_REGNUM;
853                                 reg2 = jitCompA000_selectRegCache(src[3], -1 /* mem */);
854                                 if (reg2 >= 0)
855                                         jitCompA000_storeRegCacheAll(&w);
856                                 jitCompPutByte1(w.dst, 0xb8 | reg0);    /* MOV(reg0, ?); */
857                                 jitCompPutImm32(&w, w.r3f);
858                                 jitCompPutByte1(w.dst, 0x2b);
859                                 jitCompA0001_85DispN(&w, src[3] * 4, reg0);
860                                 if (reg0 == 0)
861                                         jitCompA0001_movRxxEax(&w, src[1]);
862                                 break;
863                         }
864 #endif
865                         if (reg1 < 0) {
866                                 jitCompA0001_movReg32EbpDisp(&w, reg0, src[2] * 4); /* MOV(reg0, [EBP+?]); */
867                         }
868                         if (reg1 >= 0 && reg0 != reg1) {
869                                 jitCompPutByte2(w.dst, 0x89, 0xc0 | reg1 << 3 | reg0); /* MOV(reg0, reg1); */
870                         }
871                         if (!(src[0] == 0x10 && src[3] == 0xff)) {  // bugfix: hinted by Iris, 2013.06.26. thanks!
872                                 cmp0reg = src[1];
873                                 cmp0lev = 1;
874                                 if (src[0] < 0x14)
875                                         cmp0lev = 2;
876                                 if (src[0] == 0x16)
877                                         cmp0reg = -1;
878                         }
879                         if (!(src[0] == 0x10 && src[3] == 0xff)) {
880 #if (jitCompA0001_USE_R3F_IMM32 != 0)
881                                 if (src[3] == 0x3f) {
882                                         if (*src == 0x16 && w.r3f == -1) {
883                                                 jitCompPutByte2(w.dst, 0xf7, 0xd8 | reg0); /* NEG(reg0); */
884                                                 if (reg0 == 0)
885                                                         jitCompA0001_movRxxEax(&w, src[1]);
886                                                 break;
887                                         }
888 #if (jitCompA0001_USE_R3F_INCDEC != 0)
889                                         if ((*src == 0x14 && w.r3f == 1) || (*src == 0x15 && w.r3f == -1)) {
890                                                 jitCompPutByte1(w.dst, 0x40 | reg0);    /* INC(reg0); */
891                                                 if (reg0 == 0)
892                                                         jitCompA0001_movRxxEax(&w, src[1]);
893                                                 break;
894                                         }
895                                         if ((*src == 0x15 && w.r3f == 1) || (*src == 0x14 && w.r3f == -1)) {
896                                                 jitCompPutByte1(w.dst, 0x48 | reg0);    /* DEC(reg0); */
897                                                 if (reg0 == 0)
898                                                         jitCompA0001_movRxxEax(&w, src[1]);
899                                                 break;
900                                         }
901 #endif
902 #if (jitCompA0001_USE_R3F_IMM8 != 0)
903                                         if (-0x80 <= w.r3f && w.r3f <= 0x7f) {
904                                                 if (*src != 0x16) {
905                                                         static unsigned char basic_op_table_im8[] = { 0xc8, 0xf0, 0xe0, 0, 0xc0, 0xe8 };
906                                                         jitCompPutByte3(w.dst, 0x83, basic_op_table_im8[*src - 0x10] | reg0, w.r3f & 0xff);
907                                                 }
908                                                 else {
909                                                         jitCompPutByte3(w.dst, 0x6b, 0xc0 | reg0 << 3 | reg0, w.r3f & 0xff);
910                                                 }
911                                                 if (reg0 == 0)
912                                                         jitCompA0001_movRxxEax(&w, src[1]);
913                                                 break;
914                                         }
915 #endif
916                                         if (reg0 == 0 /* EAX */) {
917                                                 static unsigned char basic_op_table_im32_eax[] = { 0x0d, 0x35, 0x25, 0, 0x05, 0x2d, 0xc0 };
918                                                 if (*src == 0x16) { jitCompPutByte1(w.dst, 0x69); }
919                                                 jitCompPutByte1(w.dst, basic_op_table_im32_eax[*src - 0x10]);
920                                         }
921                                         else {
922                                                 if (*src != 0x16) {
923                                                         static unsigned char basic_op_table_im32_reg[] = { 0xc8, 0xf0, 0xe0, 0, 0xc0, 0xe8 };
924                                                         jitCompPutByte2(w.dst, 0x81, basic_op_table_im32_reg[*src - 0x10] | reg0);
925                                                 }
926                                                 else {
927                                                         jitCompPutByte2(w.dst, 0x69, 0xc0 | reg0 << 3 | reg0);
928                                                 }
929                                         }
930                                         jitCompPutImm32(&w, w.r3f);
931                                         if (reg0 == 0)
932                                                 jitCompA0001_movRxxEax(&w, src[1]);
933                                         break;
934                                 }
935 #endif
936                                 reg1 = jitCompA000_selectRegCache(src[3], -1 /* mem */);
937                                 if (src[3] >= 0x40) w.err = JITC_ERR_REGNUM;
938                                 if (*src != 0x16) {
939                                         if (reg1 >= 0) {
940                                                 static unsigned char basic_op_table_rr[] = { 0x09, 0x31, 0x21, 0, 0x01, 0x29 }; /* op(reg,reg); */
941                                                 jitCompPutByte2(w.dst, basic_op_table_rr[*src - 0x10], 0xc0 | reg1 << 3 | reg0);
942                                         }
943                                         else {
944                                                 static unsigned char basic_op_table_rm[] = { 0x0b, 0x33, 0x23, 0, 0x03, 0x2b, 0xaf }; /* op(reg,mem); */
945                                                 jitCompPutByte1(w.dst, basic_op_table_rm[*src - 0x10]);
946                                                 jitCompA0001_85DispN(&w, src[3] * 4, reg0);
947                                         }
948                                 }
949                                 else {
950                                         if (reg1 >= 0) {
951                                                 jitCompPutByte3(w.dst, 0x0f, 0xaf, 0xc0 | reg0 << 3 | reg1);
952                                         }
953                                         else {
954                                                 jitCompPutByte2(w.dst, 0x0f, 0xaf);
955                                                 jitCompA0001_85DispN(&w, src[3] * 4, reg0);
956                                         }
957                                 }
958                         }
959                         if (reg0 == 0)
960                                 jitCompA0001_movRxxEax(&w, src[1]);
961                         break;
962
963                 case 0x18:      /* SHL */
964                 case 0x19:      /* SAR */
965                         if (src[1] >= 0x3f) w.err = JITC_ERR_REGNUM;
966                         if (src[3] >= 0x40) w.err = JITC_ERR_REGNUM;
967 #if (jitCompA0001_USE_R3F_IMM32 != 0)
968                         if (src[3] == 0x3f) {
969                                 reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
970                                 reg1 = jitCompA000_selectRegCache(src[2], -1 /* mem */);
971                                 if (src[1] >= 0x3f) w.err = JITC_ERR_REGNUM;
972                                 if (reg1 == -1)
973                                         jitCompA0001_movReg32EbpDisp(&w, reg0, src[2] * 4); /* MOV(reg1, [EBP+?]); */
974                                 else {
975                                         if (reg0 != reg1) {
976                                                 jitCompPutByte2(w.dst, 0x89, 0xc0 | reg1 << 3 | reg0); /* MOV(reg0, reg1); */
977                                         }
978                                 }
979                                 if (*src == 0x18) { jitCompPutByte3(w.dst, 0xc1, 0xe0 | reg0, w.r3f); } /* SHL(reg0, im8); */
980                                 if (*src == 0x19) { jitCompPutByte3(w.dst, 0xc1, 0xf8 | reg0, w.r3f); } /* SAR(reg0, im8); */
981                                 if (reg0 == 0 /* EAX */)
982                                         jitCompA0001_movRxxEax(&w, src[1]);
983                                 cmp0reg = src[1];
984                                 cmp0lev = 1;
985                                 break;
986                         }
987 #endif
988                         jitCompA000_storeRegCacheAll(&w); // 手抜き.
989                         jitCompA0001_movReg32EbpDisp(&w, 1 /* ECX */, src[3] * 4); /* MOV(ECX, [EBP+?]); */
990 #if (jitCompA0001_USE_R3F_IMM32 != 0)
991                         if (src[2] == 0x3f) {
992                                 jitCompPutByte1(w.dst, 0xb8);   /* MOV(EAX, ?); */
993                                 jitCompPutImm32(&w, w.r3f);
994                         }
995                         else {
996                                 jitCompA0001_movEaxRxx(&w, src[2]);
997                         }
998 #else
999                         jitCompA0001_movEaxRxx(&w, src[2]);
1000 #endif
1001                         if (*src == 0x18) { jitCompPutByte2(w.dst, 0xd3, 0xe0); } /* SHL(EAX, CL); */
1002                         if (*src == 0x19) { jitCompPutByte2(w.dst, 0xd3, 0xf8); } /* SAR(EAX, CL); */
1003                         jitCompA0001_movRxxEax(&w, src[1]);
1004                         jitCompA000_loadRegCacheAll(&w); // 手抜き.
1005                         cmp0reg = src[1];
1006                         cmp0lev = 1;
1007                         break;
1008
1009                 case 0x1a:      /* DIV */
1010                 case 0x1b:      /* MOD */
1011                         if (src[1] >= 0x3f) w.err = JITC_ERR_REGNUM;
1012                         if (src[2] >= 0x40) w.err = JITC_ERR_REGNUM;
1013                         if (src[3] >= 0x40) w.err = JITC_ERR_REGNUM;
1014                         jitCompA000_storeRegCacheAll(&w); // 手抜き.
1015 #if (jitCompA0001_USE_R3F_IMM32 != 0)
1016                         if (src[3] == 0x3f) {
1017                                 jitCompPutByte1(w.dst, 0xb8 | 1);       /* MOV(ECX, ?); */
1018                                 jitCompPutImm32(&w, w.r3f);
1019                         }
1020                         else {
1021                                 jitCompA0001_movReg32EbpDisp(&w, 1 /* ECX */, src[3] * 4); /* MOV(ECX, [EBP+?]); */
1022                         }
1023                         if (src[2] == 0x3f) {
1024                                 jitCompPutByte1(w.dst, 0xb8 | 0);       /* MOV(EAX, ?); */
1025                                 jitCompPutImm32(&w, w.r3f);
1026                         }
1027                         else {
1028                                 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, src[2] * 4); /* MOV(EAX, [EBP+?]); */
1029                         }
1030 #else
1031                         jitCompA0001_movReg32EbpDisp(&w, 1 /* ECX */, src[3] * 4); /* MOV(ECX, [EBP+?]); */
1032                         jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, src[2] * 4); /* MOV(EAX, [EBP+?]); */
1033 #endif
1034                         jitCompPutByte1(w.dst, 0x99);   /* CDQ(); */
1035                         /* ECXがゼロではないことを確認すべき */
1036                         jitCompPutByte2(w.dst, 0xf7, 0xf9);     /* IDIV(ECX); */
1037                         if (*src == 0x1a) { jitCompA0001_movEbpDispReg32(&w, src[1] * 4, 0 /* EAX */); }
1038                         if (*src == 0x1b) { jitCompA0001_movEbpDispReg32(&w, src[1] * 4, 2 /* EDX */); }
1039                         jitCompA000_loadRegCacheAll(&w); // 手抜き.
1040                         cmp0reg = -1;
1041                         break;
1042
1043                 case 0x1c:      /* PLMT0 */
1044                 case 0x1d:      /* PLMT1 */
1045                         if (src[1] >= 0x40 || src[2] >= 0x40) w.err = JITC_ERR_PREGNUM;
1046                         if (level < JITC_LV_FASTEST) {
1047                                 cmp0reg = -1;
1048                                 if (level < JITC_LV_FASTER) {
1049                                         // typ が一致していることを確認.
1050                                         // plsとliveSignが一致していることを確認.
1051
1052                                         // preg1はp0 <= p <= p1 を満たしているか?.
1053                                         // 新しいp0/p1は古いp0〜p1に適合しているか?.
1054
1055                                 }
1056                         }
1057
1058                 case 0x1e: /* PCP */            /* 未完成(p1まで完成) */
1059                         if (src[1] >= 0x40 || src[2] >= 0x40) w.err = JITC_ERR_PREGNUM;
1060                         if (src[2] == 0x3f) w.err = JITC_ERR_PREGNUM;
1061                         if (src[1] != 0x3f) {
1062                                 /* src[2] == 0xff の場合に対応できてない */
1063                                 jitCompA000_storePRegCacheAll(&w); // 手抜き.
1064                                 for (i = 0; i < 32; i += 4) {
1065                                         jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[2] * 32 + i); /* MOV(EAX, [EBP+?]); */
1066                                         jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + i, 0 /* EAX */); /* MOV([EBP+?], EAX); */
1067                                 }
1068                                 jitCompA000_loadPRegCacheAll(&w); // 手抜き.
1069                         }
1070                         else {
1071                                 if (level < JITC_LV_FASTER) {
1072                                         jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[2] * 32 + 4); /* MOV(EAX, [EBP+?]); */  /* typ */
1073                                         jitCompPutByte3(w.dst, 0x83, 0xf8, 0);  /* CMP(EAX, 0); */
1074                                         jitCompPutByte2(w.dst, 0x0f, 0x85); /* JNE */
1075                                         jitCompPutImm32(&w, errfnc - (w.dst + 4));
1076                                         /* セキュリティチェックが足りてない!(aliveとか) */
1077                                 }
1078                                 reg0 = 0; /* EAX */
1079                                 jitCompA000_storePRegCacheAll(&w); // 手抜き.
1080                                 jitCompA0001_movReg32EbpDisp(&w, reg0, 256 + src[2] * 32 + 0); /* MOV(EAX, [EBP+?]); */
1081                                 if (level < JITC_LV_FASTER) {
1082                                         jitCompPutByte1(w.dst, 0x3b);   /* CMP(reg0, [EBP+?]); */
1083                                         jitCompA0001_85DispN(&w, 256 + src[2] * 32 + 8, reg0);  /* p0 */
1084                                         jitCompPutByte2(w.dst, 0x0f, 0x85); /* JNE */
1085                                         jitCompPutImm32(&w, errfnc - (w.dst + 4));
1086                                 }
1087                                 jitCompPutByte2(w.dst, 0xff, 0xe0);     /* JMP(EAX); */
1088                         }
1089                         break;
1090
1091                 case 0x1f: /* PCST */
1092                         if (jitCompGetImm32(src + 2) == 0) {
1093                                 if (level < JITC_LV_FASTER)
1094                                         jitCompA0001_checkType0(&w, src[6], jitCompGetImm32(src + 7), 2);
1095                                 jitCompA000_storePRegCacheAll(&w); // 手抜き.
1096                                 for (i = 0; i < 32 - 4; i += 4) {
1097                                         jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[6] * 32 + i); /* MOV(EAX, [EBP+?]); */
1098                                         if (i == 4) {
1099                                                 jitCompPutByte1(w.dst, 0x0d); /* OR(EAX, ?); */
1100                                                 jitCompPutImm32(&w, 0x80000000);
1101                                         }
1102                                         jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + i, 0 /* EAX */); /* MOV([EBP+?], EAX); */
1103                                 }
1104                                 jitCompPutByte1(w.dst, 0xb8);   /* MOV(EAX, ?); */
1105                                 jitCompPutImm32(&w, debugInfo1);
1106                                 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 28, 0 /* EAX */); /* MOV([EBP+?], EAX); */
1107                                 jitCompA000_loadPRegCacheAll(&w); // 手抜き.
1108                                 cmp0reg = -1;
1109                                 break;
1110                         }
1111                         if (jitCompGetImm32(src + 7) == 0) {
1112                                 jitCompA000_storePRegCacheAll(&w); // 手抜き.
1113                                 for (i = 0; i < 32 - 4; i += 4) {
1114                                         jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[6] * 32 + i); /* MOV(EAX, [EBP+?]); */
1115                                         if (i == 4) {
1116                                                 jitCompPutByte1(w.dst, 0x25); /* AND(EAX, ?); */
1117                                                 jitCompPutImm32(&w, 0x7fffffff);
1118                                         }
1119                                         jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + i, 0 /* EAX */); /* MOV([EBP+?], EAX); */
1120                                 }
1121                                 if (level < JITC_LV_FASTER) {
1122                                         jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[6] * 32 + 28); /* MOV(EAX, [EBP+?]); */
1123                                         jitCompPutByte1(w.dst, 0x3d);   /* CMP(EAX, ?); */
1124                                         jitCompPutImm32(&w, debugInfo1);
1125                                         jitCompPutByte2(w.dst, 0x74, 8); /* JE */
1126                                         jitCompPutByte2(w.dst, 0x31, 0xc0);     /* XOR(EAX, EAX); (2) */
1127                                         jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 0, 0 /* EAX */); /* MOV([EBP+?], EAX); (1+1+4) */
1128                                 }
1129                                 jitCompA000_loadPRegCacheAll(&w); // 手抜き.
1130                                 cmp0reg = -1;
1131                                 break;
1132                         }
1133                         w.err = JITC_ERR_OPECODE;
1134                         goto err_w;
1135
1136                 case 0x20:      /* CMPE */
1137                 case 0x21:      /* CMPNE */
1138                 case 0x22:      /* CMPL */
1139                 case 0x23:      /* CMPGE */
1140                 case 0x24:      /* CMPLE */
1141                 case 0x25:      /* CMPG */
1142                 case 0x26:      /* TSTZ */
1143                 case 0x27:      /* TSTNZ */
1144                         reg0 = jitCompA000_selectRegCache(src[2], 0 /* EAX */);
1145                         reg1 = jitCompA000_selectRegCache(src[3], -1 /* mem */);
1146                         if (src[1] == 0x3f) {
1147                                 /* 特殊構文チェック */
1148                                 if (w.prefix != 0) { w.err = JITC_ERR_PREFIX; goto err_w; }
1149                                 if (src[4] != 0x04 || src[5] != 0x3f || src[6] != 0x03 || src[7] != 0x3f) {
1150                                         w.err = JITC_ERR_IDIOM; goto err_w;
1151                                 }
1152                         }
1153                         if (reg0 == 0)
1154                                 jitCompA0001_movEaxRxx(&w, src[2]);
1155 #if (jitCompA0001_USE_R3F_IMM32 != 0)
1156                         if (src[3] == 0x3f) {
1157 #if (jitCompA0001_OPTIMIZE_CMP != 0)
1158                                 if ((*src <= 0x25 && w.r3f == 0) || (*src >= 0x26 && w.r3f == -1)) {
1159                                         i = 0;
1160                                         if (cmp0reg == src[2]) {
1161                                                 if (cmp0lev >= 1 && (src[0] == 0x20 || src[0] == 0x21 || src[0] == 0x26 || src[0] == 0x27))
1162                                                         i = 1;
1163                                                 if (cmp0lev >= 2 && (src[0] == 0x22 || src[0] == 0x23 || src[0] == 0x24 || src[0] == 0x25))
1164                                                         i = 1;
1165                                         }
1166                                         if (i == 0) {
1167                                                 jitCompPutByte2(w.dst, 0x85, 0xc0 | reg0 << 3 | reg0);  /* TEST(reg0, reg0); */
1168                                         }
1169                                         cmp0reg = src[2];
1170                                         cmp0lev = 2;
1171                                         goto cmpcc1;
1172                                 }
1173 #endif
1174 #if (jitCompA0001_USE_R3F_IMM8 != 0)
1175                                 if (-0x80 <= w.r3f && w.r3f <= 0x7f && *src <= 0x25) {
1176                                         jitCompPutByte3(w.dst, 0x83, 0xf8 | reg0, w.r3f);
1177                                         goto cmpcc1;
1178                                 }
1179 #endif
1180                                 if (reg0 == 0) {
1181                                         if (*src <= 0x25) { jitCompPutByte1(w.dst, 0x3d); }
1182                                         if (*src >= 0x26) { jitCompPutByte1(w.dst, 0xa9); }
1183                                 }
1184                                 else {
1185                                         if (*src <= 0x25) { jitCompPutByte2(w.dst, 0x81, 0xf8 | reg0); }
1186                                         if (*src >= 0x26) { jitCompPutByte2(w.dst, 0xf7, 0xc0 | reg0); }
1187                                 }
1188                                 jitCompPutImm32(&w, w.r3f);
1189                                 goto cmpcc1;
1190                         }
1191 #endif
1192                         if (src[3] >= 0x40) w.err = JITC_ERR_PREGNUM;
1193                         if (reg1 >= 0) {
1194                                 if (*src <= 0x25) { jitCompPutByte2(w.dst, 0x39, 0xc0 | reg1 << 3 | reg0); }
1195                                 if (*src >= 0x26) { jitCompPutByte2(w.dst, 0x85, 0xc0 | reg1 << 3 | reg0); }
1196                         }
1197                         else {
1198                                 if (*src <= 0x25) { jitCompPutByte1(w.dst, 0x3b); }
1199                                 if (*src >= 0x26) { jitCompPutByte1(w.dst, 0x85); }
1200                                 jitCompA0001_85DispN(&w, src[3] * 4, reg0);
1201                         }
1202                 cmpcc1:
1203                         if (w.err != 0) goto err_w;
1204                         static unsigned char cmpcc_table0[] = {
1205                                 0x04, 0x05, 0x0c, 0x0d, 0x0e, 0x0f, 0x04, 0x05, /* CMPcc, TSTcc */
1206                                 0x04, 0x05, 0x02, 0x03, 0x06, 0x07                              /* PCMPcc */
1207                         };
1208 #if (jitCompA0001_USE_R3F_CMPJMP != 0)
1209                         if (src[1] == 0x3f) {
1210                                 /* 特殊構文を利用した最適化 */
1211                                 jitCompPutByte2(w.dst, 0x0f, 0x80 | cmpcc_table0[*src - 0x20]);
1212                                 src += 6;
1213                                 i = jitCompGetLabelNum(&w, src + 2);
1214                                 if ((flags & JITC_PHASE1) != 0 && w.err != 0) {
1215                                         if (label[i].opt == 0) { w.err = JITC_ERR_LABELNODEF; goto err_w; }
1216                                         //      if (label[i].typ != 1) { w.err = JITC_ERR_LABELTYP; goto err_w; }
1217                                 }
1218                                 j = 0;
1219                                 if ((flags & JITC_PHASE1) != 0 || (((flags & JITC_PHASE1) == 0) && label[i].opt != 0))
1220                                         j = label[i].p - (w.dst + 4);
1221                                 jitCompPutImm32(&w, j);
1222 #if (jitCompA0001_OPTIMIZE_JMP != 0)
1223                                 if (-128 - 4 <= j && j < 0) {
1224                                         j += 4;
1225                                         w.dst -= 6;
1226                                         jitCompPutByte2(w.dst, w.dst[1] ^ 0xf0, j & 0xff);
1227                                 }
1228 #endif
1229                                 src += 6;
1230                                 if (w.err != 0) goto err_w;
1231                                 continue;
1232                         }
1233 #endif
1234                         /* 一般的なJITC */
1235                         reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
1236                         jitCompPutByte3(w.dst, 0x0f, 0x90 | cmpcc_table0[*src - 0x20], 0xc0 | reg0);    /* SETcc(BYTE(reg0)); */
1237                         jitCompPutByte3(w.dst, 0x0f, 0xb6, 0xc0 | reg0 << 3 | reg0);    /* MOVZX(reg0, BYTE(reg0)); */
1238                         jitCompPutByte2(w.dst, 0xf7, 0xd8 | reg0);      /* NEG(reg0); */
1239                         if (reg0 == 0)
1240                                 jitCompA0001_movRxxEax(&w, src[1]);
1241                         cmp0reg = src[2];
1242                         cmp0lev = 1;
1243                         break;
1244
1245                 case 0x28:      /* PCMPE */
1246                 case 0x29:      /* PCMPNE */
1247                 case 0x2a:      /* PCMPL */
1248                 case 0x2b:      /* PCMPGE */
1249                 case 0x2c:      /* PCMPLE */
1250                 case 0x2d:      /* PCMPG */
1251                         if (src[1] == 0x3f) {
1252                                 /* 特殊構文チェック */
1253                                 if (w.prefix != 0) { w.err = JITC_ERR_PREFIX; goto err_w; }
1254                                 if (src[4] != 0x04 || src[5] != 0x3f || src[6] != 0x03 || src[7] != 0x3f) {
1255                                         w.err = JITC_ERR_IDIOM; goto err_w;
1256                                 }
1257                         }
1258                         if (src[2] >= 0x40) w.err = JITC_ERR_PREGNUM;
1259                         jitCompA000_storePRegCacheAll(&w); // 手抜き.
1260                         if (src[3] != 0xff)
1261                                 jitCompA0001_checkCompPtr(&w, src[2], src[3]);
1262                         jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[2] * 32 + 0); /* MOV(EAX, [EBP+?]); */
1263                         if (src[3] != 0xff) {
1264                                 jitCompPutByte1(w.dst, 0x3b);   /* CMP(EAX, [EBP+?]); */
1265                                 jitCompA0001_85DispN(&w, 256 + src[3] * 32 + 0, 0);
1266                         }
1267                         else {
1268                                 /* ヌルポインタとの比較はこれでいいのか?たぶんよくない */
1269                                 jitCompPutByte3(w.dst, 0x83, 0xf8, 0x00);       /* CMP(EAX, 0); */
1270                         }
1271                         cmp0reg = -1;
1272                         goto cmpcc1;
1273
1274                 case 0x30:      /* talloc(old:F4) */
1275                 case 0x31:      /* tfree(old:F5) */
1276                 case 0x32:      /* malloc(old:F6) */
1277                 case 0x33:      /* mfree(old:F7) */
1278                         jitCompA000_storeRegCacheAll(&w); // 手抜き.
1279                         jitCompA000_storePRegCacheAll(&w); // 手抜き.
1280                         jitCompPutByte2(w.dst, 0x6a, src[3]);   /* PUSH(?); */
1281                         jitCompPutByte2(w.dst, 0x6a, src[2]);   /* PUSH(?); */
1282                         jitCompPutByte2(w.dst, 0x6a, src[1]);   /* PUSH(?); */
1283                         jitCompPutByte1(w.dst, 0x55);   /* PUSH(EBP); */
1284                         jitCompPutByte1(w.dst, 0xe8);
1285                         if (*src == 0x30) j = ((unsigned char *)&funcf4) - (w.dst + 4);
1286                         if (*src == 0x31) j = ((unsigned char *)&funcf5) - (w.dst + 4);
1287                         if (*src == 0x32) j = ((unsigned char *)&funcf6) - (w.dst + 4);
1288                         if (*src == 0x33) j = ((unsigned char *)&funcf7) - (w.dst + 4);
1289                         jitCompPutImm32(&w, j);
1290                         jitCompPutByte3(w.dst, 0x83, 0xc4, 0x10);       /* ADD(ESP,16); */
1291                         jitCompA000_loadRegCacheAll(&w); // 手抜き.
1292                         jitCompA000_loadPRegCacheAll(&w); // 手抜き.
1293                         cmp0reg = -1;
1294                         break;
1295
1296                 case 0x34:      /* data (暫定) */
1297                         cmp0reg = -1;
1298                         if (w.prefix != 0) { w.err = JITC_ERR_PREFIX; goto err_w; }
1299                         int k = jitCompGetImm32(&src[1]), tmpData, bitCount, dataWidth = jitCompA000_dataWidth(k);
1300                         if (lastlabel >= 0 && label[lastlabel].typ == 0)
1301                                 label[lastlabel].typ = k;
1302                         if (k != 1) {
1303                                 i = jitCompA000_convTyp(k);
1304                                 if (i < 2 || i > 7) { w.err = JITC_ERR_BADTYPE; goto err_w; }
1305                         }
1306                         j = jitCompGetImm32(&src[5]);
1307                         oldsrc = src;
1308                         src += 9;
1309                         if (k != 1) {
1310                                 bitCount = 7;
1311                                 while (j > 0) {
1312                                         if (src >= src1) { w.err = JITC_ERR_SRC1; src = oldsrc; goto err_w; }
1313                                         if (w.dst + 256 > dst1) { w.err = JITC_ERR_DST1; src = oldsrc; goto err_w; }
1314                                         tmpData = 0;
1315                                         for (k = 0; k < dataWidth; k++) {
1316                                                 tmpData = tmpData << 1 | ((*src >> bitCount) & 1);
1317                                                 bitCount--;
1318                                                 if (bitCount < 0) {
1319                                                         bitCount = 7;
1320                                                         src++;
1321                                                 }
1322                                         }
1323                                         if ((i & 1) == 0 && dataWidth <= 31 && (tmpData >> (dataWidth - 1)) != 0) {
1324                                                 tmpData -= 1 << dataWidth;
1325                                         }
1326                                         if (i == 2 || i == 3) { jitCompPutByte1(w.dst, tmpData & 0xff); }
1327                                         if (i == 4 || i == 5) { jitCompPutByte2(w.dst, tmpData & 0xff, (tmpData >> 8) & 0xff); }
1328                                         if (i == 6 || i == 7) { jitCompPutByte4(w.dst, tmpData & 0xff, (tmpData >> 8) & 0xff, (tmpData >> 16) & 0xff, (tmpData >> 24) & 0xff); }
1329                                         j--;
1330                                 }
1331                         }
1332                         else {
1333                                 while (j > 0) {
1334                                         if (src >= src1) { w.err = JITC_ERR_SRC1; src = oldsrc; goto err_w; }
1335                                         if (w.dst + 256 > dst1) { w.err = JITC_ERR_DST1; src = oldsrc; goto err_w; }
1336                                         i = jitCompGetImm32(src);
1337                                         src += 4;
1338                                         if ((flags & JITC_PHASE1) != 0 && w.err == 0) {
1339                                                 if (label[i].opt == 0) { w.err = JITC_ERR_LABELNODEF; goto err_w; }
1340                                         }
1341                                         jitCompPutImm32(&w, (int)label[i].p);
1342                                         jitCompPutImm32(&w, label[i].typ);
1343                                         jitCompPutImm32(&w, (int)label[i].p);
1344                                         jitCompPutImm32(&w, (int)label[i].p1);
1345                                         jitCompPutImm32(&w, 0); /* liveSign */
1346                                         jitCompPutImm32(&w, 2320); /* pls */
1347                                         jitCompPutImm32(&w, 0);
1348                                         jitCompPutImm32(&w, 0);
1349                                         j--;
1350                                 }
1351                         }
1352                         if (lastlabel >= 0 && label[lastlabel].p1 < w.dst)
1353                                 label[lastlabel].p1 = w.dst;
1354                         continue;
1355
1356                 case 0x3c:      /* ENTER */
1357                         jitCompA000_storeRegCacheAll(&w); // 手抜き.
1358                         jitCompA000_storePRegCacheAll(&w); // 手抜き.
1359                         jitCompPutByte2(w.dst, 0x6a, src[6]);   /* PUSH(?); */
1360                         jitCompPutByte2(w.dst, 0x6a, src[5]);   /* PUSH(?); */
1361                         jitCompPutByte2(w.dst, 0x6a, src[4] & 0x0f);    /* PUSH(?); */
1362                         jitCompPutByte2(w.dst, 0x6a, (src[4] >> 4) & 0x0f);     /* PUSH(?); */
1363                         jitCompPutByte2(w.dst, 0x6a, src[3]);   /* PUSH(?); */
1364                         jitCompPutByte2(w.dst, 0x6a, src[2]);   /* PUSH(?); */
1365                         jitCompPutByte2(w.dst, 0x6a, src[1]);   /* PUSH(?); */
1366                         jitCompPutByte1(w.dst, 0x55);   /* PUSH(EBP); */
1367                         jitCompPutByte1(w.dst, 0xe8);
1368                         j = ((unsigned char *)&func3c) - (w.dst + 4);
1369                         jitCompPutImm32(&w, j);
1370                         jitCompPutByte3(w.dst, 0x83, 0xc4, 0x20);       /* ADD(ESP,32); */
1371                         jitCompA000_loadRegCacheAll(&w); // 手抜き.
1372                         jitCompA000_loadPRegCacheAll(&w); // 手抜き.
1373                         cmp0reg = -1;
1374                         break;
1375
1376                 case 0x3d:      /* LEAVE */
1377                         jitCompA000_storeRegCacheAll(&w); // 手抜き.
1378                         jitCompA000_storePRegCacheAll(&w); // 手抜き.
1379                         jitCompPutByte2(w.dst, 0x6a, src[6]);   /* PUSH(?); */
1380                         jitCompPutByte2(w.dst, 0x6a, src[5]);   /* PUSH(?); */
1381                         jitCompPutByte2(w.dst, 0x6a, src[4] & 0x0f);    /* PUSH(?); */
1382                         jitCompPutByte2(w.dst, 0x6a, (src[4] >> 4) & 0x0f);     /* PUSH(?); */
1383                         jitCompPutByte2(w.dst, 0x6a, src[3]);   /* PUSH(?); */
1384                         jitCompPutByte2(w.dst, 0x6a, src[2]);   /* PUSH(?); */
1385                         jitCompPutByte2(w.dst, 0x6a, src[1]);   /* PUSH(?); */
1386                         jitCompPutByte1(w.dst, 0x55);   /* PUSH(EBP); */
1387                         jitCompPutByte1(w.dst, 0xe8);
1388                         j = ((unsigned char *)&func3d) - (w.dst + 4);
1389                         jitCompPutImm32(&w, j);
1390                         jitCompPutByte3(w.dst, 0x83, 0xc4, 0x20);       /* ADD(ESP,32); */
1391                         jitCompA000_loadRegCacheAll(&w); // 手抜き.
1392                         jitCompA000_loadPRegCacheAll(&w); // 手抜き.
1393                         cmp0reg = -1;
1394                         break;
1395
1396                 case 0xfe:      /* remark */
1397                         if (src[1] == 0x01 && src[2] == 0x00) { // DBGINFO1
1398                                 if (level <= JITC_LV_SLOWER) {
1399                                         jitCompPutByte1(w.dst, 0xb8);   /* MOV(EAX, ?); */
1400                                         jitCompPutImm32(&w, debugInfo1);
1401                                         jitCompA0001_movEbpDispReg32(&w, 2304 + 4, 0 /* EAX */); /* MOV(debugInfo1, EAX); */
1402                                 }
1403                         }
1404                         if (src[1] == 0x01 && src[2] == 0x03) { // DBGINFO1CLR
1405                                 if (level <= JITC_LV_SLOWER) {
1406                                         jitCompPutByte1(w.dst, 0xb8);   /* MOV(EAX, ?); */
1407                                         jitCompPutImm32(&w, -1);
1408                                         jitCompA0001_movEbpDispReg32(&w, 2304 + 4, 0 /* EAX */); /* MOV(debugInfo1, EAX); */
1409                                 }
1410                         }
1411                         if (src[1] == 0x05 && src[2] == 0x00) { // DBGINFO0
1412                                 if (level <= JITC_LV_SLOWEST) {
1413                                         debugInfo0 = jitCompGetImm32(src + 3);
1414                                         //      jitCompPutByte1(w.dst, 0xbf);   /* MOV(EDI, ?); */
1415                                         //      jitCompPutImm32(&w, debugInfo0);
1416                                         jitCompPutByte1(w.dst, 0xb8);   /* MOV(EAX, ?); */
1417                                         jitCompPutImm32(&w, debugInfo0);
1418                                         jitCompA0001_movEbpDispReg32(&w, 2304 + 0, 0 /* EAX */); /* MOV(debugInfo0, EAX); */
1419                                 }
1420                         }
1421                         break;
1422
1423                 default:
1424                         w.err = JITC_ERR_OPECODE;
1425                         goto err_w;
1426                 }
1427                 if (w.err != 0) goto err_w;
1428                 jitCompA0001_fixPrefix(&w);
1429                 if (w.err != 0) goto err_w;
1430                 src += jitCompCmdLen(src);
1431         }
1432         if (enter0 != NULL) {
1433                 j = w.dst - (enter0 + 4);
1434                 enter0[0] = j & 0xff;
1435                 enter0[1] = (j >> 8) & 0xff;
1436                 enter0[2] = (j >> 16) & 0xff;
1437                 enter0[3] = (j >> 24) & 0xff;
1438         }
1439         if ((flags & JITC_NOSTARTUP) == 0) {
1440                 jitCompA000_storeRegCacheAll(&w);
1441                 jitCompA000_storePRegCacheAll(&w);
1442                 jitCompPutByte1(w.dst, 0x61); /* POPAD(); */
1443         }
1444         if ((flags & JITC_PHASE1) != 0)
1445                 return w.dst - dst00;
1446         return 0;
1447
1448 err_w:
1449         if ((w.err & JITC_ERR_PHASE0ONLY) != 0) {
1450                 if ((flags & JITC_PHASE1) == 0)
1451                         w.err &= ~JITC_ERR_PHASE0ONLY;
1452         }
1453         if (w.err == (JITC_ERR_MASK & JITC_ERR_REGNUM))                 errmsg = "reg-number error";
1454         if (w.err == (JITC_ERR_MASK & JITC_ERR_DST1))                   errmsg = "dst1 error";
1455         if (w.err == (JITC_ERR_MASK & JITC_ERR_OPECODE))                errmsg = "opecode error";
1456         if (w.err == (JITC_ERR_MASK & JITC_ERR_LABELNUM))               errmsg = "label number too large";
1457         if (w.err == (JITC_ERR_MASK & JITC_ERR_LABELREDEF))             errmsg = "label redefine";
1458         if (w.err == (JITC_ERR_MASK & JITC_ERR_PREFIX))                 { errmsg = "prefix redefine"; w.dst -= 2; }
1459         if (w.err == (JITC_ERR_MASK & JITC_ERR_LABELNODEF))             errmsg = "label not defined";
1460         if (w.err == (JITC_ERR_MASK & JITC_ERR_LABELTYP))               errmsg = "label type error";
1461         if (w.err == (JITC_ERR_MASK & JITC_ERR_IDIOM))                  errmsg = "idiom error";
1462         if (w.err == (JITC_ERR_MASK & JITC_ERR_PREGNUM))                errmsg = "preg-number error";
1463         if (w.err == (JITC_ERR_MASK & JITC_ERR_SRC1))                   errmsg = "src1 error";
1464         if (w.err == (JITC_ERR_MASK & JITC_ERR_BADTYPE))                errmsg = "bad type code";
1465         if (w.err == (JITC_ERR_MASK & JITC_ERR_PREFIXFAR))              errmsg = "prefix internal error";
1466         if (w.err == (JITC_ERR_MASK & JITC_ERR_INTERNAL))               errmsg = "general internal error";
1467         if (*errmsg != '\0') {
1468                 fprintf(stderr, "JITC: %s at %06X (debugInfo0=%d)\n    ", errmsg, src - src0, debugInfo0);
1469                 for (i = 0; i < 16; i++)
1470                         fprintf(stderr, "%02X ", src[i]);
1471                 static char *table[0x30] = {
1472                         "NOP", "LB", "LIMM", "PLIMM", "CND", "??", "??", "??",
1473                         "LMEM", "SMEM", "PLMEM", "PSMEM", "LEA", "??", "PADD", "PDIF",
1474                         "CP/OR", "XOR", "AND", "??", "ADD", "SUB", "MUL", "??",
1475                         "SHL", "SAR", "DIV", "MOD", "PLMT0", "PLMT1", "PCP", "PCST",
1476                         "CMPE", "CMPNE", "CMPL", "CMPGE", "CMPLE", "CMPG", "TSTZ", "TSTNZ",
1477                         "PCMPE", "PCMPNE", "PCMPL", "PCMPGE", "PCMPLE", "PCMPG", "??", "EXT" };
1478                 errmsg = "??";
1479                 if (*src < 0x30) errmsg = table[*src];
1480                 fprintf(stderr, "(%s)\n", errmsg);
1481         }
1482         return -1;
1483 }
1484
1485 unsigned char *jitCompCallFunc(unsigned char *dst, void *func)
1486 {
1487         struct JitCompWork w;
1488         w.dst = dst;
1489         jitCompA000_storeRegCacheAll(&w);
1490         jitCompA000_storePRegCacheAll(&w);
1491         jitCompPutByte1(w.dst, 0x60);   /* PUSHAD(); */
1492         jitCompPutByte1(w.dst, 0x50);   /* PUSH(EAX); */        /* for 16byte-align(win32では不要なのだけど、MacOSには必要らしい) */
1493         jitCompPutByte1(w.dst, 0x55);   /* PUSH(EBP); */
1494         jitCompPutByte1(w.dst, 0xe8);   /* CALL(func); */
1495         int j = ((unsigned char *)func) - (w.dst + 4);
1496
1497         //この関数の中では結局w->dstしか参照していない
1498         jitCompPutImm32(&w, j);
1499
1500         jitCompPutByte1(w.dst, 0x58);   /* POP(EAX); */         /* (win32では不要なのだけど、MacOSには必要らしい) */
1501         jitCompPutByte1(w.dst, 0x58);   /* POP(EAX); */
1502         jitCompPutByte1(w.dst, 0x61);   /* POPAD(); */
1503         jitCompA000_loadRegCacheAll(&w);
1504         jitCompA000_loadPRegCacheAll(&w);
1505         jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + 0x30 * 32 + 0); /* MOV(EAX, [EBP+?]); */
1506         jitCompPutByte2(w.dst, 0xff, 0xe0);     /* JMP(EAX); */
1507         return w.dst;
1508 }
1509
1510 unsigned char *jitCompInit(unsigned char *dst)
1511 {
1512         errfnc = dst;
1513         return jitCompCallFunc(dst, &errHndl);
1514 }
1515
1516 void func3c(char *ebp, int opt, int r1, int p1, int lenR, int lenP, int r0, int p0)
1517 {
1518         HOSECPU_RuntimeEnvironment *r = (HOSECPU_RuntimeEnvironment *) (ebp - jitCompA0001_EBP128);
1519         int i, *pi;
1520         HOSECPU_PointerRegisterEntry *pp;
1521         if (r->junkStack + 2048 > r->junkStack1) (*(r->errHndl))(r);
1522         pi = (void *)r->junkStack; r->junkStack += r1 * 4;
1523         for (i = 0; i < r1; i++)
1524                 pi[i] = r->ireg[i];
1525         pp = (void *)r->junkStack; r->junkStack += p1 * 32;
1526         for (i = 0; i < p1; i++)
1527                 pp[i] = r->preg[i];
1528         pp = (void *)r->junkStack; r->junkStack += 32;
1529         *pp = r->preg[0x30];
1530         pi = (void *)r->junkStack; r->junkStack += 4;
1531         *pi = opt << 16 | r1 << 8 | p1;
1532         for (i = 0; i < lenR; i++)
1533                 r->ireg[r0 + i] = r->ireg[0x30 + i];
1534         for (i = 0; i < lenP; i++)
1535                 r->preg[p0 + i] = r->preg[0x31 + i];
1536         return;
1537 }
1538
1539 void func3d(char *ebp, int opt, int r1, int p1, int lenR, int lenP, int r0, int p0)
1540 {
1541         HOSECPU_RuntimeEnvironment *r = (HOSECPU_RuntimeEnvironment *) (ebp - jitCompA0001_EBP128);
1542         int i;
1543         r->junkStack -= 4;
1544         r->junkStack -= 32;
1545     HOSECPU_PointerRegisterEntry *pp = (void *)r->junkStack;
1546         r->preg[0x30] = *pp;
1547         r->junkStack -= p1 * 32; pp = (void *)r->junkStack;
1548         for (i = 0; i < p1; i++)
1549                 r->preg[i] = pp[i];
1550         r->junkStack -= r1 * 4; int *pi = (void *)r->junkStack;
1551         for (i = 0; i < r1; i++)
1552                 r->ireg[i] = pi[i];
1553         return;
1554 }
1555
1556 void funcf4(char *ebp, int pxx, int typ, int len)
1557 {
1558         HOSECPU_RuntimeEnvironment *r = (HOSECPU_RuntimeEnvironment *) (ebp - jitCompA0001_EBP128);
1559         int width = jitCompA000_dataWidth(jitCompA000_convTyp(r->ireg[typ])) >> 3;
1560         if (width < 0 || r->ireg[len] < 0)
1561                 (*(r->errHndl))(r);
1562         void *p = r->junkStack;
1563         if (r->junkStack + width * r->ireg[len] + 256 > r->junkStack1) (*(r->errHndl))(r);
1564         r->junkStack += width * r->ireg[len];
1565         r->preg[pxx].p = p;
1566         r->preg[pxx].typ = r->ireg[typ];
1567         r->preg[pxx].p0 = p;
1568         r->preg[pxx].p1 = (void *)r->junkStack;
1569         int *pi = (int *)r->junkStack;
1570         *pi = width * r->ireg[len];
1571         r->junkStack += sizeof (int);
1572         if (r->ireg[typ] == 1) {
1573                 int i, i1 = (width * r->ireg[len]) >> 2;
1574                 pi = p;
1575                 for (i = 0; i < i1; i++)
1576                         pi[i] = 0;
1577         }
1578         return;
1579 }
1580
1581 void funcf5(char *ebp, int pxx, int typ, int len)
1582 {
1583         HOSECPU_RuntimeEnvironment *r = (HOSECPU_RuntimeEnvironment *) (ebp - jitCompA0001_EBP128);
1584         r->junkStack -= sizeof (int);
1585         int *pi = (int *)r->junkStack;
1586         r->junkStack -= *pi;
1587 #if 0
1588         int width = jitCompA000_dataWidth(r->ireg[typ]);
1589         void *p = r->junkStack;
1590         r->junkStack -= width * r->ireg[len];
1591 #endif
1592         return;
1593 }
1594
1595 void funcf6(char *ebp, int pxx, int typ, int len)
1596 {
1597         HOSECPU_RuntimeEnvironment *r = (HOSECPU_RuntimeEnvironment *) (ebp - jitCompA0001_EBP128);
1598         int width = jitCompA000_dataWidth(jitCompA000_convTyp(r->ireg[typ])) >> 3;
1599         if (width < 0 || r->ireg[len] < 0)
1600                 (*(r->errHndl))(r);
1601         void *p = malloc(width * r->ireg[len]);
1602         r->preg[pxx].p = p;
1603         r->preg[pxx].typ = r->ireg[typ];
1604         r->preg[pxx].p0 = p;
1605         r->preg[pxx].p1 = (unsigned char *)p + width * r->ireg[len];
1606         if (r->ireg[typ] == 1) {
1607                 int i, i1 = (width * r->ireg[len]) >> 2, *pi;
1608                 pi = p;
1609                 for (i = 0; i < i1; i++)
1610                         pi[i] = 0;
1611                 for (i = 1; i < i1; i += 8)
1612                         pi[i] |= -1;
1613         }
1614         return;
1615 }
1616
1617 void funcf7(char *ebp, int pxx, int typ, int len)
1618 {
1619         HOSECPU_RuntimeEnvironment *r = (HOSECPU_RuntimeEnvironment *) (ebp - jitCompA0001_EBP128);
1620         free(r->preg[pxx].p);
1621         return;
1622 }
1623
1624 void errHndl(HOSECPU_RuntimeEnvironment *r)
1625 {
1626         r = (HOSECPU_RuntimeEnvironment *) (((char *)r) - jitCompA0001_EBP128);
1627         (*(r->errHndl))(r);
1628         // ここに帰ってきてはいけない.
1629 }
1630
1631 /*
1632  * jitcの出力コードをひとまとめにする関数を作成しその中身をjitCompile()で生成
1633  *
1634  * qq : 出力バイナリの書き込み位置のアドレスへの参照(書き込み位置を呼び出しに反映させるため参照渡しにする)
1635  * q1 : 出力バイナリの書き込み位置のアドレスの最大値
1636  * p0 : (*.ose)バイナリの読み込み位置のアドレス(ヘッダ部除去済)
1637  * p1 : (*.ose)バイナリの読み込み位置の取りうる最大値
1638  *      (ただし、「確保したメモリ」の最大値なのでこれより手前にゴミデータが入っているかもしれない)
1639  * ret=1 : ヘッダのエラー
1640  * ret=2 : jitコンパイルエラー
1641  */
1642 int jitc0(unsigned char **qq, unsigned char *q1, const unsigned char *p0, const unsigned char *p1, int level, HOSECPU_LabelListTag *label)
1643 {
1644         unsigned char *q = *qq;
1645         if (p0[0] != 0x05 || p0[1] != SIGN1)    // OSECPUのヘッダ (05E1) を確認
1646                 return 1;
1647
1648         *q++ = 0x55; /* PUSH(EBP); */
1649         *q++ = 0x8b; *q++ = 0x6c; *q++ = 0x24; *q++ = 0x08; /* MOV(EBP,[ESP+8]); */
1650
1651         int i;
1652         for (i = 0; i < JITC_MAXLABELS; i++)
1653                 label[i].opt = 0;
1654
1655         // 以下のjitCompile()呼び出しでは第二引数をq1-2にした方がよいのではないか?
1656         i = jitCompiler(q, q1, p0 + 2, p1, p0, label, JITC_MAXLABELS, level, di1_serial, 0);
1657         if (i != 0) return 2;
1658         i = jitCompiler(q, q1, p0 + 2, p1, p0, label, JITC_MAXLABELS, level, di1_serial, JITC_PHASE1 + 0);
1659         if (i < 0) return 2;
1660         q += i;
1661
1662         *q++ = 0x5d; /* POP(EBP); */
1663         *q++ = 0xc3; /* RET(); */
1664
1665         *qq = q;
1666         return 0;
1667 }
1668
1669 #if (USE_DEBUGGER != 0)
1670
1671 int dbgrGetRegNum(const char *p)
1672 {
1673         int i, j, r = -1;
1674         if (p[2] <= ' ') {
1675                 i = p[0] - '0';
1676                 j = p[1] - '0';
1677                 if (i > 9) i -= 'A' - '0' - 10;
1678                 if (j > 9) j -= 'A' - '0' - 10;
1679                 if (0 <= i && i <= 15 && 0 <= j && j <= 15)
1680                         r = i << 4 | j;
1681         }
1682         return r;
1683 }
1684
1685 void dbgrMain(HOSECPU_RuntimeEnvironment *r)
1686 {
1687         if (r->dbgr == 0) return;
1688         for (;;) {
1689                 char cmd[64], *p;
1690                 int i, j, k;
1691                 printf("\ndbgr>");
1692                 p = fgets(cmd, 64, stdin);
1693                 if (p == NULL) break;
1694                 if (cmd[0] == '\0') continue;
1695                 if (cmd[0] == 'q' && cmd[1] <= ' ') break;
1696                 if (cmd[0] == 'p' && cmd[1] <= ' ' && cmd[1] != '\0') {
1697                         p = &cmd[2];
1698                         while (*p <= ' ' && *p != '\0') p++;
1699                         if (*p == 'R') {
1700                                 i = dbgrGetRegNum(p + 1);
1701                                 if (0 <= i && i <= 0x3f) {
1702                                         printf("R%02X = 0x%08X = %d\n", i, r->ireg[i], r->ireg[i]);
1703                                 }
1704                                 else
1705                                         puts("register name error");
1706                                 continue;
1707                         }
1708                         if (*p == 'P') {
1709                                 i = dbgrGetRegNum(p + 1);
1710                                 if (0 <= i && i <= 0x3f) {
1711                                         p = "invalid";
1712                                         if (0 <= r->preg[i].typ && r->preg[i].typ <= 0x15) {
1713                                                 static char *typName[] = {
1714                                                         "T_CODE", "T_VPTR", "T_SINT8", "T_UINT8",
1715                                                         "T_SINT16", "T_UINT16", "T_SINT32", "T_UINT32",
1716                                                         "T_SINT4", "T_UINT4", "T_SINT2", "T_UINT2",
1717                                                         "T_SINT1", "T_UINT1", "T_SINT12", "T_UINT12",
1718                                                         "T_SINT20", "T_UINT20", "T_SINT24", "T_UINT24",
1719                                                         "T_SINT28", "T_UINT28"
1720                                                 };
1721                                                 p = typName[r->preg[i].typ];
1722                                         }
1723                                         printf("P%02X:\n  type = %s(%04X),  (origin-ptr) = 0x%08X\n", i, p, r->preg[i].typ, (unsigned int)(r->preg[i].p0));
1724                                         if (r->preg[i].p != NULL && r->preg[i].p0 != NULL) {
1725                                                 j = jitCompA000_dataWidth(jitCompA000_convTyp(r->preg[i].typ)) >> 3;
1726                                                 if (j <= 0) j = 1;
1727                                                 k = (r->preg[i].p1 - r->preg[i].p0) / j;
1728                                                 printf("  size = 0x%08X = %d\n", k, k);
1729                                                 k = (r->preg[i].p - r->preg[i].p0) / j;
1730                                                 printf("  pos  = 0x%08X = %d\n", k, k);
1731                                         }
1732                                         else {
1733                                                 puts("  null pointer");
1734                                         }
1735                                 }
1736                                 else
1737                                         puts("register name error");
1738                                 continue;
1739                         }
1740                 }
1741                 puts("command error");
1742         }
1743         return;
1744 }
1745
1746 #endif
1747
1748
1749 #endif
1750
1751
1752