OSDN Git Service

ファイル構成の修正
[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(struct Regs *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;
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(struct Regs *r);
306
307 int jitCompiler(unsigned char *dst, unsigned char *dst1, const unsigned char *src, const unsigned char *src1, const unsigned char *src0, struct LabelTable *label, int maxLabels, int level, int debugInfo1, int flags)
308 /* IA-32用 */
309 /* 本来ならこのレイヤでは文法チェックしない */
310 {
311         struct JitCompWork w;
312         unsigned char *dst00 = dst, *enter0 = NULL, *tmp_ucp;
313     char *errmsg = "";
314         const unsigned char *oldsrc;
315         int timecount = 0, i, j = 0, lastlabel = -1, debugInfo0 = -1;
316         int reg0, reg1, reg2, cmp0reg = -1, cmp0lev = 0;
317         w.dst = w.dst0 = dst;
318         w.err = 0;
319         w.maxLabels = maxLabels;
320         if ((flags & JITC_NOSTARTUP) == 0) {
321                 jitCompPutByte1(w.dst, 0x60); /* PUSHAD(); */
322                 jitCompA000_loadRegCacheAll(&w); /* start-up */
323                 jitCompA000_loadPRegCacheAll(&w);
324         }
325         if (level <= JITC_LV_SLOWER) {
326                 jitCompPutByte2(w.dst, 0x31, 0xc0);     /* XOR(EAX, EAX); */
327                 jitCompA0001_movEbpDispReg32(&w, 2304 + 0, 0 /* EAX */); /* MOV(debugInfo0, EAX); */
328                 jitCompPutByte1(w.dst, 0xb8);   /* MOV(EAX, ?); */
329                 jitCompPutImm32(&w, debugInfo1);
330                 jitCompA0001_movEbpDispReg32(&w, 2304 + 4, 0 /* EAX */); /* MOV(debugInfo1, EAX); */
331         }
332         while (src < src1) {
333                 w.prefix = 0;
334                 if (w.dst + 256 > dst1) { w.err = JITC_ERR_DST1; goto err_w; }
335                 timecount++;
336                 if (timecount >= 64) {
337                         timecount -= 64;
338                         /* 未完成(timeoutチェックコードを入れる) */
339                 }
340         prefix_continue:
341                 switch (*src) {
342
343                 case 0x00:      /* NOP */
344                         if (w.prefix != 0) { w.err = JITC_ERR_PREFIX; goto err_w; }
345                         break;
346
347                 case 0x01:      /* LB */
348                         if (enter0 == NULL && (src[6] == 0x3c || (src[6] == 0xfe && src[7] == 0x01 && src[9] == 0x3c))) {
349                                 jitCompPutByte1(w.dst, 0xe9);
350                                 enter0 = w.dst;
351                                 jitCompPutImm32(&w, 0);
352                         }
353                         if (src[6] == 0x34) {
354                                 tmp_ucp = w.dst;
355                                 jitCompPutByte1(w.dst, 0xe9);
356                                 i = jitCompGetImm32(&src[7]);
357                                 j = 32;
358                                 if (i != 1) {
359                                         i = jitCompA000_convTyp(i);
360                                         j = 0;
361                                         if (i == 2 || i == 3) { j = 1; }
362                                         if (i == 4 || i == 5) { j = 2; }
363                                         if (i == 6 || i == 7) { j = 4; }
364                                 }
365                                 j *= jitCompGetImm32(&src[11]);
366                                 if (j <= 0) w.err = JITC_ERR_BADTYPE;
367                                 jitCompPutImm32(&w, j);
368 #if (jitCompA0001_OPTIMIZE_JMP != 0)
369                                 if (j <= 127 - jitCompA0001_OPTIMIZE_ALIGN) {
370                                         w.dst -= 5;
371                                         jitCompPutByte2(w.dst, 0xeb, j);
372                                 }
373 #endif
374                         }
375 #if (jitCompA0001_OPTIMIZE_ALIGN != 0)
376                         for (;;) {
377                                 i = ((int)w.dst) & (jitCompA0001_OPTIMIZE_ALIGN - 1); /* ALIGNで割ったあまりを計算 */
378                                 if (i == 0) break;
379                                 i = jitCompA0001_OPTIMIZE_ALIGN - i;
380                                 if (i == 1) { jitCompPutByte1(w.dst, 0x90); j += i; } /* NOP(); */
381                                 if (i == 2) { jitCompPutByte2(w.dst, 0x89, 0xc0); j += i; } /* MOV(EAX, EAX); */
382                                 if (i == 3) { jitCompPutByte3(w.dst, 0x8d, 0x76, 0x00); j += i; } /* LEA(ESI, [ESI+0]); */
383                                 if (i == 4) { jitCompPutByte4(w.dst, 0x8d, 0x74, 0x26, 0x00); j += i; } /* LEA(ESI, [ESI*1+0]); */
384                                 if (i == 5) { jitCompPutByte1(w.dst, 0x0d); jitCompPutImm32(&w, 0); j += i; } /* OR(EAX, 0); */
385                                 if (i == 6) { jitCompPutByte2(w.dst, 0x8d, 0xb6); jitCompPutImm32(&w, 0); j += i; } /* LEA(ESI, [ESI+0]); */
386                                 if (i >= 7) { jitCompPutByte3(w.dst, 0x8d, 0xb4, 0x26); jitCompPutImm32(&w, 0); j += 7; } /* LEA(ESI, [ESI*1+0]); */
387                         }
388 #endif
389                         if (src[6] == 0x34) {
390                                 tmp_ucp[1] = j & 0xff;
391                                 if (*tmp_ucp == 0xe9) {
392                                         tmp_ucp[2] = (j >> 8) & 0xff;
393                                         tmp_ucp[3] = (j >> 16) & 0xff;
394                                         tmp_ucp[4] = (j >> 24) & 0xff;
395                                 }
396                         }
397                         if ((flags & JITC_PHASE1) == 0) {
398                                 i = jitCompGetLabelNum(&w, src + 2);
399                                 //printf("i=%06X %06X\n", i, src-src0);
400                                 if (label[i].opt != 0 && w.err == 0) { w.err = JITC_ERR_LABELREDEF; goto err_w; }
401                                 if (w.prefix != 0) { w.err = JITC_ERR_PREFIX; goto err_w; }
402                                 label[i].opt = src[1] + 1;
403                                 label[i].typ = 0; /* TYP_CODE */
404                                 label[i].p = w.dst;
405                                 label[i].p1 = w.dst + 1;
406                                 lastlabel = i;
407                         }
408                         cmp0reg = -1;
409                         timecount = 0;
410                         /* 未完成(timeoutチェックコードを入れる) */
411                         break;
412
413                 case 0x02:      /* LIMM */
414                         if (src[1] == 0x3f && w.prefix != 0) w.err = JITC_ERR_PREFIX;
415 #if (jitCompA0001_USE_R3F_IMM32 != 0)
416                         if (src[1] == 0x3f) {
417                                 w.r3f = jitCompGetImm32(src + 2);
418                                 break;
419                         }
420 #endif
421                         i = jitCompGetImm32(src + 2);
422                         reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
423 #if (jitCompA0001_OPTIMIZE_MOV != 0)
424                         if (i == 0) {
425                                 jitCompPutByte2(w.dst, 0x31, 0xc0 | reg0 << 3 | reg0);  /* XOR(reg0, reg0); */
426                                 jitCompA0001_movRxxEax(&w, src[1]);
427                                 break;
428                         }
429 #endif
430                         jitCompPutByte1(w.dst, 0xb8 | reg0);    /* MOV(reg0, ?); */
431                         jitCompPutImm32(&w, i);
432                         if (reg0 == 0)
433                                 jitCompA0001_movRxxEax(&w, src[1]);
434                         break;
435
436                 case 0x03:      /* PLIMM */     /* 未完成(plsまで対応) */
437                         i = jitCompGetLabelNum(&w, src + 2);
438                         if ((flags & JITC_PHASE1) != 0 && w.err == 0) {
439                                 if (label[i].opt == 0) { w.err = JITC_ERR_LABELNODEF; goto err_w; }
440                                 if (src[1] != 0x3f && label[i].opt != 2) { w.err = JITC_ERR_LABELTYP; goto err_w; }
441                                 if (src[1] == 0x3f && label[i].typ != 0) { w.err = JITC_ERR_LABELTYP; goto err_w; }
442                         }
443                         if (src[1] == 0x3f) {
444                                 if (w.prefix == 0) {
445                                         jitCompPutByte1(w.dst, 0xe9); /* JMP(?); */
446                                 }
447                                 else {
448                                         w.dst[-1] = w.dst[-2] ^ 0xf1; /* 74->85, 75->84 */
449                                         w.dst[-2] = 0x0f;
450                                         w.prefix = 0;
451                                 }
452                                 j = 0;
453                                 if ((flags & JITC_PHASE1) != 0 || (((flags & JITC_PHASE1) == 0) && label[i].opt != 0))
454                                         j = label[i].p - (w.dst + 4);
455                                 jitCompPutImm32(&w, j);
456 #if (jitCompA0001_OPTIMIZE_JMP != 0)
457                                 if (-128 - 3 <= j && j < 0) {
458                                         if (w.dst[-5] == 0xe9) {
459                                                 j += 3;
460                                                 w.dst -= 5;
461                                                 jitCompPutByte1(w.dst, 0xeb); /* JMP(?); */
462                                         }
463                                         else {
464                                                 j += 4;
465                                                 w.dst -= 6;
466                                                 jitCompPutByte1(w.dst, w.dst[1] ^ 0xf0);
467                                         }
468                                         jitCompPutByte1(w.dst, j & 0xff);
469                                 }
470 #endif
471                         }
472                         else {
473                                 reg0 = jitCompA000_selectPRegCache(src[1], 0 /* EAX */);
474                                 jitCompPutByte1(w.dst, 0xb8 | reg0);    /* MOV(reg0, ?); */
475                                 jitCompPutImm32(&w, (int)label[i].p);
476                                 if (reg0 == 0)
477                                         jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32, 0); /* MOV([EBP+?], EAX); */
478                                 if (level < JITC_LV_FASTEST) {
479                                         jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 8, reg0); /* MOV([EBP+?], reg0); */ /* p0 */
480                                         jitCompPutByte1(w.dst, 0xb8); /* MOV(EAX, ?); */
481                                         jitCompPutImm32(&w, label[i].typ);
482                                         jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 4, 0); /* MOV([EBP+?], EAX); */ /* typ */
483                                         jitCompPutByte1(w.dst, 0xb8); /* MOV(EAX, ?); */
484                                         jitCompPutImm32(&w, (int)label[i].p1);
485                                         jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 12, 0); /* MOV([EBP+?], EAX); */ /* p1 */
486                                         jitCompPutByte2(w.dst, 0x31, 0xc0);     /* XOR(EAX, EAX); */
487                                         jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 16, 0); /* MOV([EBP+?], EAX); */ /* liveSign */
488                                         jitCompA0001_movReg32EbpDisp(&w, 0, 2320); /* MOV(EAX, ptrCtrl); */
489                                         jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 20, 0); /* MOV([EBP+?], EAX); */ /* pls */
490                                 }
491                         }
492                         break;
493
494                 case 0x04:      /* CND (prefix) */
495                         if (src[1] >= 0x40) w.err = JITC_ERR_REGNUM;
496                         reg0 = jitCompA000_selectRegCache(src[1], -1 /* mem */);
497                         if (reg0 < 0) {
498                                 jitCompPutByte1(w.dst, 0xf7); /* TEST([EBP+?],1); */
499                                 jitCompA0001_85DispN(&w, src[1] * 4, 0);
500                         }
501                         else {
502                                 jitCompPutByte2(w.dst, 0xf7, 0xc0 | reg0); /* TEST(reg0,1); */
503                         }
504                         jitCompPutImm32(&w, 1);
505                         jitCompPutByte2(w.dst, 0x74, 0x00);     /* JZ($+2) */
506                         cmp0reg = -1;
507                         if (w.err != 0) goto err_w;
508                         src += 2;
509                         w.prefix = 1;
510                         w.dst0 = w.dst;
511                         goto prefix_continue;
512
513                 case 0x08: /* LMEM */   /* 完成 */
514                         i = jitCompGetImm32(src + 2);
515                         if (i == 0x0001) w.err = JITC_ERR_BADTYPE;
516                         if (level < JITC_LV_FASTER) {
517                                 jitCompA0001_checkType(&w, src[6], i, 0); // read
518                                 cmp0reg = -1;
519                         }
520                         reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
521                         reg1 = jitCompA000_selectPRegCache(src[6], 2 /* EDX */);
522                         if (reg0 != 0 /* EAX */ && reg1 == 2 /* EDX */)
523                                 reg1 = 0; /* EAX */
524                         if (reg1 == 2 /* EDX */)
525                                 jitCompA000_storeRegCacheEdx(&w);
526                         if (reg1 <= 3 /* EAX, EDX */)
527                                 jitCompA0001_movReg32EbpDisp(&w, reg1, 256 + src[6] * 32 + 0); /* MOV(reg1, [EBP+?]); */
528                         if (level < JITC_LV_FASTER)
529                                 jitCompA0001_checkLimit(&w, reg1, src[6]);
530                         i = jitCompA000_convTyp(jitCompGetImm32(src + 2));
531                         switch (i) {
532                         case 0x0002:
533                                 jitCompPutByte3(w.dst, 0x0f, 0xbe, reg0 << 3 | reg1);   /* MOVSX(reg0,BYTE [reg1]); */
534                                 break;
535                         case 0x0003:
536                                 jitCompPutByte3(w.dst, 0x0f, 0xb6, reg0 << 3 | reg1);   /* MOVZX(reg0,BYTE [reg1]); */
537                                 break;
538                         case 0x0004:
539                                 jitCompPutByte3(w.dst, 0x0f, 0xbf, reg0 << 3 | reg1);   /* MOVSX(reg0,WORD [reg1]); */
540                                 break;
541                         case 0x0005:
542                                 jitCompPutByte3(w.dst, 0x0f, 0xb7, reg0 << 3 | reg1);   /* MOVZX(reg0,WORD [reg1]); */
543                                 break;
544                         case 0x0006:
545                         case 0x0007:
546                                 jitCompPutByte2(w.dst, 0x8b, reg0 << 3 | reg1); /* MOV(reg0, [reg1]); */
547                                 break;
548                         default:
549                                 w.err = JITC_ERR_BADTYPE;
550                         }
551                         if (reg0 == 0 /* EAX */)
552                                 jitCompA0001_movRxxEax(&w, src[1]);
553                         if (reg1 == 2 /* EDX */)
554                                 jitCompA000_loadRegCacheEdx(&w);
555                         break;
556
557                 case 0x09: /* SMEM */   /* 完成 */
558                         i = jitCompGetImm32(src + 2);
559                         if (i == 0x0001) w.err = JITC_ERR_BADTYPE;
560                         if (level < JITC_LV_FASTER) {
561                                 jitCompA0001_checkType(&w, src[6], i, 1); // write
562                                 cmp0reg = -1;
563                         }
564                         reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
565                         reg1 = jitCompA000_selectPRegCache(src[6], 2 /* EDX */);
566                         if (reg0 != 0 /* EAX */ && reg1 == 2 /* EDX */)
567                                 reg1 = 0; /* EAX */
568                         if (reg1 == 2 /* EDX */)
569                                 jitCompA000_storeRegCacheEdx(&w);
570                         if (reg1 <= 3 /* EAX, EDX */)
571                                 jitCompA0001_movReg32EbpDisp(&w, reg1, 256 + src[6] * 32 + 0); /* MOV(reg1, [EBP+?]); */
572                         if (level < JITC_LV_FASTER)
573                                 jitCompA0001_checkLimit(&w, reg1, src[6]);
574                         if (reg0 == 0 /* EAX */)
575                                 jitCompA0001_movEaxRxx(&w, src[1]);
576                         /* 値の範囲チェック */
577                         i = jitCompA000_convTyp(jitCompGetImm32(src + 2));
578                         switch (i) {
579                         case 0x0002:
580                         case 0x0003:
581                                 jitCompPutByte2(w.dst, 0x88, reg0 << 3 | reg1); /* MOV([reg1], BYTE(reg0)); */
582                                 break;
583                         case 0x0004:
584                         case 0x0005:
585                                 jitCompPutByte3(w.dst, 0x66, 0x89, reg0 << 3 | reg1);   /* MOV([reg1], WORD(reg0)); */
586                                 break;
587                         case 0x0006:
588                         case 0x0007:
589                                 jitCompPutByte2(w.dst, 0x89, reg0 << 3 | reg1); /* MOV([reg1], reg0); */
590                                 break;
591                         default:
592                                 w.err = JITC_ERR_BADTYPE;
593                         }
594                         if (reg1 == 2 /* EDX */)
595                                 jitCompA000_loadRegCacheEdx(&w);
596                         break;
597
598                 case 0x0a: /* PLMEM */  /* 完成 */
599                         i = jitCompGetImm32(src + 2);
600                         if (i != 0x0001) w.err = JITC_ERR_BADTYPE;
601                         if (level < JITC_LV_FASTER) {
602                                 jitCompA0001_checkType(&w, src[6], i, 0); // read
603                                 cmp0reg = -1;
604                         }
605                         reg0 = jitCompA000_selectPRegCache(src[1], 0 /* EAX */);
606                         reg1 = jitCompA000_selectPRegCache(src[6], 2 /* EDX */);
607                         //      if (reg0 != 0 /* EAX */ && reg1 == 2 /* EDX */) /* これをやってはいけない!(by K, 2013.08.02) */
608                         //              reg1 = 0; /* EAX */
609                         if (reg0 == reg1 && reg0 != 0) {        // bugfix: hinted by yao, 2013.09.14. thanks!
610                                 jitCompA000_storePRegCacheAll(&w);
611                                 reg1 = 2; /* EDX */
612                         }
613                         if (reg1 == 2 /* EDX */)
614                                 jitCompA000_storeRegCacheEdx(&w);
615                         if (reg1 <= 3 /* EAX, EDX */)
616                                 jitCompA0001_movReg32EbpDisp(&w, reg1, 256 + src[6] * 32 + 0); /* MOV(reg1, [EBP+?]); */
617                         if (level < JITC_LV_FASTER)
618                                 jitCompA0001_checkLimit(&w, reg1, src[6]);
619                         jitCompPutByte2(w.dst, 0x8b, reg0 << 3 | reg1); /* MOV(reg0, [reg1]); */
620                         if (reg0 == 0 /* EAX */)
621                                 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 0, 0); /* MOV([EBP+?], EAX); */
622                         for (i = 4; i < 32; i += 4) {
623                                 jitCompPutByte3(w.dst, 0x8b, 0x40 | reg1, i);   /* MOV(EAX, [reg1+?]); */
624                                 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + i, 0 /* EAX */); /* MOV([EBP+?], EAX); */
625                         }
626                         if (reg1 == 2 /* EDX */)
627                                 jitCompA000_loadRegCacheEdx(&w);
628                         break;
629
630                 case 0x0b: /* PSMEM */  /* 完成 */
631                         i = jitCompGetImm32(src + 2);
632                         if (i != 0x0001) w.err = JITC_ERR_BADTYPE;
633                         if (level < JITC_LV_FASTER) {
634                                 jitCompA0001_checkType(&w, src[6], i, 1); // write
635                                 cmp0reg = -1;
636                         }
637                         reg0 = jitCompA000_selectPRegCache(src[1], 0 /* EAX */);
638                         reg1 = jitCompA000_selectPRegCache(src[6], 2 /* EDX */);
639                         //      if (reg0 != 0 /* EAX */ && reg1 == 2 /* EDX */) /* これをやってはいけない!(by K, 2013.08.02) */
640                         //              reg1 = 0; /* EAX */
641                         if (reg1 == 2 /* EDX */)
642                                 jitCompA000_storeRegCacheEdx(&w);
643                         if (reg1 <= 3 /* EAX, EDX */)
644                                 jitCompA0001_movReg32EbpDisp(&w, reg1, 256 + src[6] * 32 + 0); /* MOV(reg1, [EBP+?]); */
645                         if (level < JITC_LV_FASTER)
646                                 jitCompA0001_checkLimit(&w, reg1, src[6]);
647                         if (reg0 == 0 /* EAX */)
648                                 jitCompA0001_movReg32EbpDisp(&w, reg0, 256 + src[1] * 32 + 0); /* MOV(reg0, [EBP+?]); */
649                         jitCompPutByte2(w.dst, 0x89, reg0 << 3 | reg1); /* MOV([reg1], reg0); */
650                         for (i = 4; i < 32; i += 4) {
651                                 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[1] * 32 + i); /* MOV(EAX, [EBP+?]); */
652                                 jitCompPutByte3(w.dst, 0x89, 0x40 | reg1, i);   /* MOV([reg1+?], EAX); */
653                         }
654                         if (reg1 == 2 /* EDX */)
655                                 jitCompA000_loadRegCacheEdx(&w);
656                         break;
657
658                 case 0x0e: /* PADD */           /* 完成 */
659                         if (level < JITC_LV_FASTER) {
660                                 jitCompA0001_checkType0(&w, src[6], jitCompGetImm32(src + 2), 2); // other, aliveテストはとりあえずしない.
661                                 cmp0reg = -1;
662                         }
663                         reg0 = jitCompA000_selectPRegCache(src[1], 0 /* EAX */);
664                         reg1 = jitCompA000_selectPRegCache(src[6], -1 /* mem */);
665                         if (reg1 < 0 /* mem */)
666                                 jitCompA0001_movReg32EbpDisp(&w, reg0, 256 + src[6] * 32 + 0); /* MOV(reg0, [EBP+?]); */
667                         if (reg1 >= 0 && reg0 != reg1) {
668                                 jitCompPutByte2(w.dst, 0x89, 0xc0 | reg1 << 3 | reg0); /* MOV(reg0, reg1); */
669                         }
670                         i = jitCompGetImm32(src + 2);
671                         j = -1;
672                         if (i == 1)
673                                 j = 5; /* 32 */
674                         else {
675                                 i = jitCompA000_convTyp(i);
676                                 if (0x0002 <= i && i <= 0x0007)
677                                         j = (i - 0x0002) >> 1;
678                         }
679                         if (j < 0) { w.err = JITC_ERR_BADTYPE; goto err_w; }
680 #if (jitCompA0001_USE_R3F_IMM32 != 0)
681                         if (src[7] == 0x3f) {
682                                 j = w.r3f << j;
683 #if (jitCompA0001_USE_R3F_IMM8 != 0)
684                                 if (-0x80 <= j && j <= 0x7f) {
685 #if (jitCompA0001_USE_R3F_INCDEC != 0)
686                                         if (j == 1) { jitCompPutByte1(w.dst, 0x40 | reg0); goto padd1; } /* INC */
687                                         if (j == -1) { jitCompPutByte1(w.dst, 0x48 | reg0); goto padd1; } /* DEC */
688 #endif
689                                         jitCompPutByte3(w.dst, 0x83, 0xc0 | reg0, j & 0xff);    /* ADD(reg0, im8); */
690                                         goto padd1;
691                                 }
692 #endif
693                                 if (reg0 == 0) {
694                                         jitCompPutByte1(w.dst, 0x05);   /* ADD(reg0, ?); */
695                                 }
696                                 else {
697                                         jitCompPutByte2(w.dst, 0x81, 0xc0 | reg0);      /* ADD(reg0, ?); */
698                                 }
699                                 jitCompPutImm32(&w, j);
700                                 goto padd1;
701                         }
702 #endif
703                         if (src[7] >= 0x40) w.err = JITC_ERR_REGNUM;
704                         if (j == 0) {
705                                 reg1 = jitCompA000_selectRegCache(src[7], -1 /* mem */);
706                                 if (reg1 >= 0) {
707                                         jitCompPutByte2(w.dst, 0x01, 0xc0 | reg1 << 3 | reg0);  /* ADD(reg0, reg1); */
708                                 }
709                                 else {
710                                         jitCompPutByte1(w.dst, 0x03);   /* ADD(reg0, [EBP+?]); */
711                                         jitCompA0001_85DispN(&w, src[7] * 4, reg0);
712                                 }
713                         }
714                         else {
715                                 reg1 = jitCompA000_selectRegCache(src[7], -1 /* mem */);
716                                 reg2 = 2; /* EDX */
717                                 jitCompA000_storeRegCacheEdx(&w);
718                                 if (reg1 < 0)
719                                         jitCompA0001_movReg32EbpDisp(&w, reg2, src[7] * 4); /* MOV(reg2, [EBP+?]); */
720                                 if (reg1 >= 0 && reg1 != reg2) {
721                                         jitCompPutByte2(w.dst, 0x89, 0xc0 | reg1 << 3 | reg2); /* MOV(reg2, reg1); */
722                                 }
723                                 jitCompPutByte3(w.dst, 0xc1, 0xe0 | reg2, j);   /* SHL(reg2, ?); */
724                                 jitCompPutByte2(w.dst, 0x01, 0xc0 | reg2 << 3 | reg0);  /* ADD(reg0, reg2); */
725                                 jitCompA000_loadRegCacheEdx(&w);
726                         }
727 #if (jitCompA0001_USE_R3F_IMM32 != 0)
728                 padd1:
729 #endif
730                         if (reg0 == 0 /* EAX */)
731                                 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 0, reg0); /* MOV([EBP+?], reg0); */
732                         if (src[1] != src[6]) {
733                                 for (i = 4; i < 32; i += 4) {
734                                         jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[6] * 32 + i); /* MOV(EAX, [EBP+?]); */
735                                         jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + i, 0 /* EAX */); /* MOV([EBP+?], EAX); */
736                                 }
737                         }
738                         cmp0reg = -1;
739                         break;
740
741                 case 0x0f: /* PDIF */   /* 未完成 */
742                         reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
743                         jitCompA000_storePRegCacheAll(&w); // 手抜き.
744                         jitCompA0001_checkCompPtr(&w, src[6], src[7]);
745                         jitCompA0001_movReg32EbpDisp(&w, reg0, 256 + src[6] * 32 + 0); /* MOV(reg0, [EBP+?]); */
746                         jitCompPutByte1(w.dst, 0x2b);   /* SUB(EAX, [EBP+?]); */
747                         jitCompA0001_85DispN(&w, 256 + src[7] * 32 + 0, reg0);
748                         i = jitCompA000_convTyp(jitCompGetImm32(src + 2));
749                         j = -1;
750                         if (0x0002 <= i && i <= 0x0007)
751                                 j = (i - 0x0002) >> 1;
752                         if (j < 0) { w.err = JITC_ERR_BADTYPE; goto err_w; }
753                         if (j > 0) {
754                                 jitCompPutByte3(w.dst, 0xc1, 0xf8 | reg0, j);   /* SAR(reg0,?); */
755                         }
756                         if (reg0 == 0 /* EAX */)
757                                 jitCompA0001_movRxxEax(&w, src[1]);
758                         cmp0reg = src[1]; cmp0lev = 1;
759                         break;
760
761                 case 0x10:      /* OR */
762                 case 0x11:      /* XOR */
763                 case 0x12:      /* AND */
764                 case 0x14:      /* ADD */
765                 case 0x15:      /* SUB */
766                 case 0x16:      /* MUL */
767                         if (src[1] >= 0x3f) w.err = JITC_ERR_REGNUM;
768                         reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
769                         reg1 = jitCompA000_selectRegCache(src[2], -1 /* mem */);
770 #if (jitCompA0001_USE_R3F_IMM32 != 0)
771                         if (src[2] == 0x3f) {   // SUBのみ該当.
772                                 if (*src != 0x15) w.err = JITC_ERR_REGNUM;
773                                 reg2 = jitCompA000_selectRegCache(src[3], -1 /* mem */);
774                                 if (reg2 >= 0)
775                                         jitCompA000_storeRegCacheAll(&w);
776                                 jitCompPutByte1(w.dst, 0xb8 | reg0);    /* MOV(reg0, ?); */
777                                 jitCompPutImm32(&w, w.r3f);
778                                 jitCompPutByte1(w.dst, 0x2b);
779                                 jitCompA0001_85DispN(&w, src[3] * 4, reg0);
780                                 if (reg0 == 0)
781                                         jitCompA0001_movRxxEax(&w, src[1]);
782                                 break;
783                         }
784 #endif
785                         if (reg1 < 0) {
786                                 jitCompA0001_movReg32EbpDisp(&w, reg0, src[2] * 4); /* MOV(reg0, [EBP+?]); */
787                         }
788                         if (reg1 >= 0 && reg0 != reg1) {
789                                 jitCompPutByte2(w.dst, 0x89, 0xc0 | reg1 << 3 | reg0); /* MOV(reg0, reg1); */
790                         }
791                         if (!(src[0] == 0x10 && src[3] == 0xff)) {  // bugfix: hinted by Iris, 2013.06.26. thanks!
792                                 cmp0reg = src[1];
793                                 cmp0lev = 1;
794                                 if (src[0] < 0x14)
795                                         cmp0lev = 2;
796                                 if (src[0] == 0x16)
797                                         cmp0reg = -1;
798                         }
799                         if (!(src[0] == 0x10 && src[3] == 0xff)) {
800 #if (jitCompA0001_USE_R3F_IMM32 != 0)
801                                 if (src[3] == 0x3f) {
802                                         if (*src == 0x16 && w.r3f == -1) {
803                                                 jitCompPutByte2(w.dst, 0xf7, 0xd8 | reg0); /* NEG(reg0); */
804                                                 if (reg0 == 0)
805                                                         jitCompA0001_movRxxEax(&w, src[1]);
806                                                 break;
807                                         }
808 #if (jitCompA0001_USE_R3F_INCDEC != 0)
809                                         if ((*src == 0x14 && w.r3f == 1) || (*src == 0x15 && w.r3f == -1)) {
810                                                 jitCompPutByte1(w.dst, 0x40 | reg0);    /* INC(reg0); */
811                                                 if (reg0 == 0)
812                                                         jitCompA0001_movRxxEax(&w, src[1]);
813                                                 break;
814                                         }
815                                         if ((*src == 0x15 && w.r3f == 1) || (*src == 0x14 && w.r3f == -1)) {
816                                                 jitCompPutByte1(w.dst, 0x48 | reg0);    /* DEC(reg0); */
817                                                 if (reg0 == 0)
818                                                         jitCompA0001_movRxxEax(&w, src[1]);
819                                                 break;
820                                         }
821 #endif
822 #if (jitCompA0001_USE_R3F_IMM8 != 0)
823                                         if (-0x80 <= w.r3f && w.r3f <= 0x7f) {
824                                                 if (*src != 0x16) {
825                                                         static unsigned char basic_op_table_im8[] = { 0xc8, 0xf0, 0xe0, 0, 0xc0, 0xe8 };
826                                                         jitCompPutByte3(w.dst, 0x83, basic_op_table_im8[*src - 0x10] | reg0, w.r3f & 0xff);
827                                                 }
828                                                 else {
829                                                         jitCompPutByte3(w.dst, 0x6b, 0xc0 | reg0 << 3 | reg0, w.r3f & 0xff);
830                                                 }
831                                                 if (reg0 == 0)
832                                                         jitCompA0001_movRxxEax(&w, src[1]);
833                                                 break;
834                                         }
835 #endif
836                                         if (reg0 == 0 /* EAX */) {
837                                                 static unsigned char basic_op_table_im32_eax[] = { 0x0d, 0x35, 0x25, 0, 0x05, 0x2d, 0xc0 };
838                                                 if (*src == 0x16) { jitCompPutByte1(w.dst, 0x69); }
839                                                 jitCompPutByte1(w.dst, basic_op_table_im32_eax[*src - 0x10]);
840                                         }
841                                         else {
842                                                 if (*src != 0x16) {
843                                                         static unsigned char basic_op_table_im32_reg[] = { 0xc8, 0xf0, 0xe0, 0, 0xc0, 0xe8 };
844                                                         jitCompPutByte2(w.dst, 0x81, basic_op_table_im32_reg[*src - 0x10] | reg0);
845                                                 }
846                                                 else {
847                                                         jitCompPutByte2(w.dst, 0x69, 0xc0 | reg0 << 3 | reg0);
848                                                 }
849                                         }
850                                         jitCompPutImm32(&w, w.r3f);
851                                         if (reg0 == 0)
852                                                 jitCompA0001_movRxxEax(&w, src[1]);
853                                         break;
854                                 }
855 #endif
856                                 reg1 = jitCompA000_selectRegCache(src[3], -1 /* mem */);
857                                 if (src[3] >= 0x40) w.err = JITC_ERR_REGNUM;
858                                 if (*src != 0x16) {
859                                         if (reg1 >= 0) {
860                                                 static unsigned char basic_op_table_rr[] = { 0x09, 0x31, 0x21, 0, 0x01, 0x29 }; /* op(reg,reg); */
861                                                 jitCompPutByte2(w.dst, basic_op_table_rr[*src - 0x10], 0xc0 | reg1 << 3 | reg0);
862                                         }
863                                         else {
864                                                 static unsigned char basic_op_table_rm[] = { 0x0b, 0x33, 0x23, 0, 0x03, 0x2b, 0xaf }; /* op(reg,mem); */
865                                                 jitCompPutByte1(w.dst, basic_op_table_rm[*src - 0x10]);
866                                                 jitCompA0001_85DispN(&w, src[3] * 4, reg0);
867                                         }
868                                 }
869                                 else {
870                                         if (reg1 >= 0) {
871                                                 jitCompPutByte3(w.dst, 0x0f, 0xaf, 0xc0 | reg0 << 3 | reg1);
872                                         }
873                                         else {
874                                                 jitCompPutByte2(w.dst, 0x0f, 0xaf);
875                                                 jitCompA0001_85DispN(&w, src[3] * 4, reg0);
876                                         }
877                                 }
878                         }
879                         if (reg0 == 0)
880                                 jitCompA0001_movRxxEax(&w, src[1]);
881                         break;
882
883                 case 0x18:      /* SHL */
884                 case 0x19:      /* SAR */
885                         if (src[1] >= 0x3f) w.err = JITC_ERR_REGNUM;
886                         if (src[3] >= 0x40) w.err = JITC_ERR_REGNUM;
887 #if (jitCompA0001_USE_R3F_IMM32 != 0)
888                         if (src[3] == 0x3f) {
889                                 reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
890                                 reg1 = jitCompA000_selectRegCache(src[2], -1 /* mem */);
891                                 if (src[1] >= 0x3f) w.err = JITC_ERR_REGNUM;
892                                 if (reg1 == -1)
893                                         jitCompA0001_movReg32EbpDisp(&w, reg0, src[2] * 4); /* MOV(reg1, [EBP+?]); */
894                                 else {
895                                         if (reg0 != reg1) {
896                                                 jitCompPutByte2(w.dst, 0x89, 0xc0 | reg1 << 3 | reg0); /* MOV(reg0, reg1); */
897                                         }
898                                 }
899                                 if (*src == 0x18) { jitCompPutByte3(w.dst, 0xc1, 0xe0 | reg0, w.r3f); } /* SHL(reg0, im8); */
900                                 if (*src == 0x19) { jitCompPutByte3(w.dst, 0xc1, 0xf8 | reg0, w.r3f); } /* SAR(reg0, im8); */
901                                 if (reg0 == 0 /* EAX */)
902                                         jitCompA0001_movRxxEax(&w, src[1]);
903                                 cmp0reg = src[1];
904                                 cmp0lev = 1;
905                                 break;
906                         }
907 #endif
908                         jitCompA000_storeRegCacheAll(&w); // 手抜き.
909                         jitCompA0001_movReg32EbpDisp(&w, 1 /* ECX */, src[3] * 4); /* MOV(ECX, [EBP+?]); */
910 #if (jitCompA0001_USE_R3F_IMM32 != 0)
911                         if (src[2] == 0x3f) {
912                                 jitCompPutByte1(w.dst, 0xb8);   /* MOV(EAX, ?); */
913                                 jitCompPutImm32(&w, w.r3f);
914                         }
915                         else {
916                                 jitCompA0001_movEaxRxx(&w, src[2]);
917                         }
918 #else
919                         jitCompA0001_movEaxRxx(&w, src[2]);
920 #endif
921                         if (*src == 0x18) { jitCompPutByte2(w.dst, 0xd3, 0xe0); } /* SHL(EAX, CL); */
922                         if (*src == 0x19) { jitCompPutByte2(w.dst, 0xd3, 0xf8); } /* SAR(EAX, CL); */
923                         jitCompA0001_movRxxEax(&w, src[1]);
924                         jitCompA000_loadRegCacheAll(&w); // 手抜き.
925                         cmp0reg = src[1];
926                         cmp0lev = 1;
927                         break;
928
929                 case 0x1a:      /* DIV */
930                 case 0x1b:      /* MOD */
931                         if (src[1] >= 0x3f) w.err = JITC_ERR_REGNUM;
932                         if (src[2] >= 0x40) w.err = JITC_ERR_REGNUM;
933                         if (src[3] >= 0x40) w.err = JITC_ERR_REGNUM;
934                         jitCompA000_storeRegCacheAll(&w); // 手抜き.
935 #if (jitCompA0001_USE_R3F_IMM32 != 0)
936                         if (src[3] == 0x3f) {
937                                 jitCompPutByte1(w.dst, 0xb8 | 1);       /* MOV(ECX, ?); */
938                                 jitCompPutImm32(&w, w.r3f);
939                         }
940                         else {
941                                 jitCompA0001_movReg32EbpDisp(&w, 1 /* ECX */, src[3] * 4); /* MOV(ECX, [EBP+?]); */
942                         }
943                         if (src[2] == 0x3f) {
944                                 jitCompPutByte1(w.dst, 0xb8 | 0);       /* MOV(EAX, ?); */
945                                 jitCompPutImm32(&w, w.r3f);
946                         }
947                         else {
948                                 jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, src[2] * 4); /* MOV(EAX, [EBP+?]); */
949                         }
950 #else
951                         jitCompA0001_movReg32EbpDisp(&w, 1 /* ECX */, src[3] * 4); /* MOV(ECX, [EBP+?]); */
952                         jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, src[2] * 4); /* MOV(EAX, [EBP+?]); */
953 #endif
954                         jitCompPutByte1(w.dst, 0x99);   /* CDQ(); */
955                         /* ECXがゼロではないことを確認すべき */
956                         jitCompPutByte2(w.dst, 0xf7, 0xf9);     /* IDIV(ECX); */
957                         if (*src == 0x1a) { jitCompA0001_movEbpDispReg32(&w, src[1] * 4, 0 /* EAX */); }
958                         if (*src == 0x1b) { jitCompA0001_movEbpDispReg32(&w, src[1] * 4, 2 /* EDX */); }
959                         jitCompA000_loadRegCacheAll(&w); // 手抜き.
960                         cmp0reg = -1;
961                         break;
962
963                 case 0x1c:      /* PLMT0 */
964                 case 0x1d:      /* PLMT1 */
965                         if (src[1] >= 0x40 || src[2] >= 0x40) w.err = JITC_ERR_PREGNUM;
966                         if (level < JITC_LV_FASTEST) {
967                                 cmp0reg = -1;
968                                 if (level < JITC_LV_FASTER) {
969                                         // typ が一致していることを確認.
970                                         // plsとliveSignが一致していることを確認.
971
972                                         // preg1はp0 <= p <= p1 を満たしているか?.
973                                         // 新しいp0/p1は古いp0〜p1に適合しているか?.
974
975                                 }
976                         }
977
978                 case 0x1e: /* PCP */            /* 未完成(p1まで完成) */
979                         if (src[1] >= 0x40 || src[2] >= 0x40) w.err = JITC_ERR_PREGNUM;
980                         if (src[2] == 0x3f) w.err = JITC_ERR_PREGNUM;
981                         if (src[1] != 0x3f) {
982                                 /* src[2] == 0xff の場合に対応できてない */
983                                 jitCompA000_storePRegCacheAll(&w); // 手抜き.
984                                 for (i = 0; i < 32; i += 4) {
985                                         jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[2] * 32 + i); /* MOV(EAX, [EBP+?]); */
986                                         jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + i, 0 /* EAX */); /* MOV([EBP+?], EAX); */
987                                 }
988                                 jitCompA000_loadPRegCacheAll(&w); // 手抜き.
989                         }
990                         else {
991                                 if (level < JITC_LV_FASTER) {
992                                         jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[2] * 32 + 4); /* MOV(EAX, [EBP+?]); */  /* typ */
993                                         jitCompPutByte3(w.dst, 0x83, 0xf8, 0);  /* CMP(EAX, 0); */
994                                         jitCompPutByte2(w.dst, 0x0f, 0x85); /* JNE */
995                                         jitCompPutImm32(&w, errfnc - (w.dst + 4));
996                                         /* セキュリティチェックが足りてない!(aliveとか) */
997                                 }
998                                 reg0 = 0; /* EAX */
999                                 jitCompA000_storePRegCacheAll(&w); // 手抜き.
1000                                 jitCompA0001_movReg32EbpDisp(&w, reg0, 256 + src[2] * 32 + 0); /* MOV(EAX, [EBP+?]); */
1001                                 if (level < JITC_LV_FASTER) {
1002                                         jitCompPutByte1(w.dst, 0x3b);   /* CMP(reg0, [EBP+?]); */
1003                                         jitCompA0001_85DispN(&w, 256 + src[2] * 32 + 8, reg0);  /* p0 */
1004                                         jitCompPutByte2(w.dst, 0x0f, 0x85); /* JNE */
1005                                         jitCompPutImm32(&w, errfnc - (w.dst + 4));
1006                                 }
1007                                 jitCompPutByte2(w.dst, 0xff, 0xe0);     /* JMP(EAX); */
1008                         }
1009                         break;
1010
1011                 case 0x1f: /* PCST */
1012                         if (jitCompGetImm32(src + 2) == 0) {
1013                                 if (level < JITC_LV_FASTER)
1014                                         jitCompA0001_checkType0(&w, src[6], jitCompGetImm32(src + 7), 2);
1015                                 jitCompA000_storePRegCacheAll(&w); // 手抜き.
1016                                 for (i = 0; i < 32 - 4; i += 4) {
1017                                         jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[6] * 32 + i); /* MOV(EAX, [EBP+?]); */
1018                                         if (i == 4) {
1019                                                 jitCompPutByte1(w.dst, 0x0d); /* OR(EAX, ?); */
1020                                                 jitCompPutImm32(&w, 0x80000000);
1021                                         }
1022                                         jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + i, 0 /* EAX */); /* MOV([EBP+?], EAX); */
1023                                 }
1024                                 jitCompPutByte1(w.dst, 0xb8);   /* MOV(EAX, ?); */
1025                                 jitCompPutImm32(&w, debugInfo1);
1026                                 jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 28, 0 /* EAX */); /* MOV([EBP+?], EAX); */
1027                                 jitCompA000_loadPRegCacheAll(&w); // 手抜き.
1028                                 cmp0reg = -1;
1029                                 break;
1030                         }
1031                         if (jitCompGetImm32(src + 7) == 0) {
1032                                 jitCompA000_storePRegCacheAll(&w); // 手抜き.
1033                                 for (i = 0; i < 32 - 4; i += 4) {
1034                                         jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[6] * 32 + i); /* MOV(EAX, [EBP+?]); */
1035                                         if (i == 4) {
1036                                                 jitCompPutByte1(w.dst, 0x25); /* AND(EAX, ?); */
1037                                                 jitCompPutImm32(&w, 0x7fffffff);
1038                                         }
1039                                         jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + i, 0 /* EAX */); /* MOV([EBP+?], EAX); */
1040                                 }
1041                                 if (level < JITC_LV_FASTER) {
1042                                         jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[6] * 32 + 28); /* MOV(EAX, [EBP+?]); */
1043                                         jitCompPutByte1(w.dst, 0x3d);   /* CMP(EAX, ?); */
1044                                         jitCompPutImm32(&w, debugInfo1);
1045                                         jitCompPutByte2(w.dst, 0x74, 8); /* JE */
1046                                         jitCompPutByte2(w.dst, 0x31, 0xc0);     /* XOR(EAX, EAX); (2) */
1047                                         jitCompA0001_movEbpDispReg32(&w, 256 + src[1] * 32 + 0, 0 /* EAX */); /* MOV([EBP+?], EAX); (1+1+4) */
1048                                 }
1049                                 jitCompA000_loadPRegCacheAll(&w); // 手抜き.
1050                                 cmp0reg = -1;
1051                                 break;
1052                         }
1053                         w.err = JITC_ERR_OPECODE;
1054                         goto err_w;
1055
1056                 case 0x20:      /* CMPE */
1057                 case 0x21:      /* CMPNE */
1058                 case 0x22:      /* CMPL */
1059                 case 0x23:      /* CMPGE */
1060                 case 0x24:      /* CMPLE */
1061                 case 0x25:      /* CMPG */
1062                 case 0x26:      /* TSTZ */
1063                 case 0x27:      /* TSTNZ */
1064                         reg0 = jitCompA000_selectRegCache(src[2], 0 /* EAX */);
1065                         reg1 = jitCompA000_selectRegCache(src[3], -1 /* mem */);
1066                         if (src[1] == 0x3f) {
1067                                 /* 特殊構文チェック */
1068                                 if (w.prefix != 0) { w.err = JITC_ERR_PREFIX; goto err_w; }
1069                                 if (src[4] != 0x04 || src[5] != 0x3f || src[6] != 0x03 || src[7] != 0x3f) {
1070                                         w.err = JITC_ERR_IDIOM; goto err_w;
1071                                 }
1072                         }
1073                         if (reg0 == 0)
1074                                 jitCompA0001_movEaxRxx(&w, src[2]);
1075 #if (jitCompA0001_USE_R3F_IMM32 != 0)
1076                         if (src[3] == 0x3f) {
1077 #if (jitCompA0001_OPTIMIZE_CMP != 0)
1078                                 if ((*src <= 0x25 && w.r3f == 0) || (*src >= 0x26 && w.r3f == -1)) {
1079                                         i = 0;
1080                                         if (cmp0reg == src[2]) {
1081                                                 if (cmp0lev >= 1 && (src[0] == 0x20 || src[0] == 0x21 || src[0] == 0x26 || src[0] == 0x27))
1082                                                         i = 1;
1083                                                 if (cmp0lev >= 2 && (src[0] == 0x22 || src[0] == 0x23 || src[0] == 0x24 || src[0] == 0x25))
1084                                                         i = 1;
1085                                         }
1086                                         if (i == 0) {
1087                                                 jitCompPutByte2(w.dst, 0x85, 0xc0 | reg0 << 3 | reg0);  /* TEST(reg0, reg0); */
1088                                         }
1089                                         cmp0reg = src[2];
1090                                         cmp0lev = 2;
1091                                         goto cmpcc1;
1092                                 }
1093 #endif
1094 #if (jitCompA0001_USE_R3F_IMM8 != 0)
1095                                 if (-0x80 <= w.r3f && w.r3f <= 0x7f && *src <= 0x25) {
1096                                         jitCompPutByte3(w.dst, 0x83, 0xf8 | reg0, w.r3f);
1097                                         goto cmpcc1;
1098                                 }
1099 #endif
1100                                 if (reg0 == 0) {
1101                                         if (*src <= 0x25) { jitCompPutByte1(w.dst, 0x3d); }
1102                                         if (*src >= 0x26) { jitCompPutByte1(w.dst, 0xa9); }
1103                                 }
1104                                 else {
1105                                         if (*src <= 0x25) { jitCompPutByte2(w.dst, 0x81, 0xf8 | reg0); }
1106                                         if (*src >= 0x26) { jitCompPutByte2(w.dst, 0xf7, 0xc0 | reg0); }
1107                                 }
1108                                 jitCompPutImm32(&w, w.r3f);
1109                                 goto cmpcc1;
1110                         }
1111 #endif
1112                         if (src[3] >= 0x40) w.err = JITC_ERR_PREGNUM;
1113                         if (reg1 >= 0) {
1114                                 if (*src <= 0x25) { jitCompPutByte2(w.dst, 0x39, 0xc0 | reg1 << 3 | reg0); }
1115                                 if (*src >= 0x26) { jitCompPutByte2(w.dst, 0x85, 0xc0 | reg1 << 3 | reg0); }
1116                         }
1117                         else {
1118                                 if (*src <= 0x25) { jitCompPutByte1(w.dst, 0x3b); }
1119                                 if (*src >= 0x26) { jitCompPutByte1(w.dst, 0x85); }
1120                                 jitCompA0001_85DispN(&w, src[3] * 4, reg0);
1121                         }
1122                 cmpcc1:
1123                         if (w.err != 0) goto err_w;
1124                         static unsigned char cmpcc_table0[] = {
1125                                 0x04, 0x05, 0x0c, 0x0d, 0x0e, 0x0f, 0x04, 0x05, /* CMPcc, TSTcc */
1126                                 0x04, 0x05, 0x02, 0x03, 0x06, 0x07                              /* PCMPcc */
1127                         };
1128 #if (jitCompA0001_USE_R3F_CMPJMP != 0)
1129                         if (src[1] == 0x3f) {
1130                                 /* 特殊構文を利用した最適化 */
1131                                 jitCompPutByte2(w.dst, 0x0f, 0x80 | cmpcc_table0[*src - 0x20]);
1132                                 src += 6;
1133                                 i = jitCompGetLabelNum(&w, src + 2);
1134                                 if ((flags & JITC_PHASE1) != 0 && w.err != 0) {
1135                                         if (label[i].opt == 0) { w.err = JITC_ERR_LABELNODEF; goto err_w; }
1136                                         //      if (label[i].typ != 1) { w.err = JITC_ERR_LABELTYP; goto err_w; }
1137                                 }
1138                                 j = 0;
1139                                 if ((flags & JITC_PHASE1) != 0 || (((flags & JITC_PHASE1) == 0) && label[i].opt != 0))
1140                                         j = label[i].p - (w.dst + 4);
1141                                 jitCompPutImm32(&w, j);
1142 #if (jitCompA0001_OPTIMIZE_JMP != 0)
1143                                 if (-128 - 4 <= j && j < 0) {
1144                                         j += 4;
1145                                         w.dst -= 6;
1146                                         jitCompPutByte2(w.dst, w.dst[1] ^ 0xf0, j & 0xff);
1147                                 }
1148 #endif
1149                                 src += 6;
1150                                 if (w.err != 0) goto err_w;
1151                                 continue;
1152                         }
1153 #endif
1154                         /* 一般的なJITC */
1155                         reg0 = jitCompA000_selectRegCache(src[1], 0 /* EAX */);
1156                         jitCompPutByte3(w.dst, 0x0f, 0x90 | cmpcc_table0[*src - 0x20], 0xc0 | reg0);    /* SETcc(BYTE(reg0)); */
1157                         jitCompPutByte3(w.dst, 0x0f, 0xb6, 0xc0 | reg0 << 3 | reg0);    /* MOVZX(reg0, BYTE(reg0)); */
1158                         jitCompPutByte2(w.dst, 0xf7, 0xd8 | reg0);      /* NEG(reg0); */
1159                         if (reg0 == 0)
1160                                 jitCompA0001_movRxxEax(&w, src[1]);
1161                         cmp0reg = src[2];
1162                         cmp0lev = 1;
1163                         break;
1164
1165                 case 0x28:      /* PCMPE */
1166                 case 0x29:      /* PCMPNE */
1167                 case 0x2a:      /* PCMPL */
1168                 case 0x2b:      /* PCMPGE */
1169                 case 0x2c:      /* PCMPLE */
1170                 case 0x2d:      /* PCMPG */
1171                         if (src[1] == 0x3f) {
1172                                 /* 特殊構文チェック */
1173                                 if (w.prefix != 0) { w.err = JITC_ERR_PREFIX; goto err_w; }
1174                                 if (src[4] != 0x04 || src[5] != 0x3f || src[6] != 0x03 || src[7] != 0x3f) {
1175                                         w.err = JITC_ERR_IDIOM; goto err_w;
1176                                 }
1177                         }
1178                         if (src[2] >= 0x40) w.err = JITC_ERR_PREGNUM;
1179                         jitCompA000_storePRegCacheAll(&w); // 手抜き.
1180                         if (src[3] != 0xff)
1181                                 jitCompA0001_checkCompPtr(&w, src[2], src[3]);
1182                         jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + src[2] * 32 + 0); /* MOV(EAX, [EBP+?]); */
1183                         if (src[3] != 0xff) {
1184                                 jitCompPutByte1(w.dst, 0x3b);   /* CMP(EAX, [EBP+?]); */
1185                                 jitCompA0001_85DispN(&w, 256 + src[3] * 32 + 0, 0);
1186                         }
1187                         else {
1188                                 /* ヌルポインタとの比較はこれでいいのか?たぶんよくない */
1189                                 jitCompPutByte3(w.dst, 0x83, 0xf8, 0x00);       /* CMP(EAX, 0); */
1190                         }
1191                         cmp0reg = -1;
1192                         goto cmpcc1;
1193
1194                 case 0x30:      /* talloc(old:F4) */
1195                 case 0x31:      /* tfree(old:F5) */
1196                 case 0x32:      /* malloc(old:F6) */
1197                 case 0x33:      /* mfree(old:F7) */
1198                         jitCompA000_storeRegCacheAll(&w); // 手抜き.
1199                         jitCompA000_storePRegCacheAll(&w); // 手抜き.
1200                         jitCompPutByte2(w.dst, 0x6a, src[3]);   /* PUSH(?); */
1201                         jitCompPutByte2(w.dst, 0x6a, src[2]);   /* PUSH(?); */
1202                         jitCompPutByte2(w.dst, 0x6a, src[1]);   /* PUSH(?); */
1203                         jitCompPutByte1(w.dst, 0x55);   /* PUSH(EBP); */
1204                         jitCompPutByte1(w.dst, 0xe8);
1205                         if (*src == 0x30) j = ((unsigned char *)&funcf4) - (w.dst + 4);
1206                         if (*src == 0x31) j = ((unsigned char *)&funcf5) - (w.dst + 4);
1207                         if (*src == 0x32) j = ((unsigned char *)&funcf6) - (w.dst + 4);
1208                         if (*src == 0x33) j = ((unsigned char *)&funcf7) - (w.dst + 4);
1209                         jitCompPutImm32(&w, j);
1210                         jitCompPutByte3(w.dst, 0x83, 0xc4, 0x10);       /* ADD(ESP,16); */
1211                         jitCompA000_loadRegCacheAll(&w); // 手抜き.
1212                         jitCompA000_loadPRegCacheAll(&w); // 手抜き.
1213                         cmp0reg = -1;
1214                         break;
1215
1216                 case 0x34:      /* data (暫定) */
1217                         cmp0reg = -1;
1218                         if (w.prefix != 0) { w.err = JITC_ERR_PREFIX; goto err_w; }
1219                         int k = jitCompGetImm32(&src[1]), tmpData, bitCount, dataWidth = jitCompA000_dataWidth(k);
1220                         if (lastlabel >= 0 && label[lastlabel].typ == 0)
1221                                 label[lastlabel].typ = k;
1222                         if (k != 1) {
1223                                 i = jitCompA000_convTyp(k);
1224                                 if (i < 2 || i > 7) { w.err = JITC_ERR_BADTYPE; goto err_w; }
1225                         }
1226                         j = jitCompGetImm32(&src[5]);
1227                         oldsrc = src;
1228                         src += 9;
1229                         if (k != 1) {
1230                                 bitCount = 7;
1231                                 while (j > 0) {
1232                                         if (src >= src1) { w.err = JITC_ERR_SRC1; src = oldsrc; goto err_w; }
1233                                         if (w.dst + 256 > dst1) { w.err = JITC_ERR_DST1; src = oldsrc; goto err_w; }
1234                                         tmpData = 0;
1235                                         for (k = 0; k < dataWidth; k++) {
1236                                                 tmpData = tmpData << 1 | ((*src >> bitCount) & 1);
1237                                                 bitCount--;
1238                                                 if (bitCount < 0) {
1239                                                         bitCount = 7;
1240                                                         src++;
1241                                                 }
1242                                         }
1243                                         if ((i & 1) == 0 && dataWidth <= 31 && (tmpData >> (dataWidth - 1)) != 0) {
1244                                                 tmpData -= 1 << dataWidth;
1245                                         }
1246                                         if (i == 2 || i == 3) { jitCompPutByte1(w.dst, tmpData & 0xff); }
1247                                         if (i == 4 || i == 5) { jitCompPutByte2(w.dst, tmpData & 0xff, (tmpData >> 8) & 0xff); }
1248                                         if (i == 6 || i == 7) { jitCompPutByte4(w.dst, tmpData & 0xff, (tmpData >> 8) & 0xff, (tmpData >> 16) & 0xff, (tmpData >> 24) & 0xff); }
1249                                         j--;
1250                                 }
1251                         }
1252                         else {
1253                                 while (j > 0) {
1254                                         if (src >= src1) { w.err = JITC_ERR_SRC1; src = oldsrc; goto err_w; }
1255                                         if (w.dst + 256 > dst1) { w.err = JITC_ERR_DST1; src = oldsrc; goto err_w; }
1256                                         i = jitCompGetImm32(src);
1257                                         src += 4;
1258                                         if ((flags & JITC_PHASE1) != 0 && w.err == 0) {
1259                                                 if (label[i].opt == 0) { w.err = JITC_ERR_LABELNODEF; goto err_w; }
1260                                         }
1261                                         jitCompPutImm32(&w, (int)label[i].p);
1262                                         jitCompPutImm32(&w, label[i].typ);
1263                                         jitCompPutImm32(&w, (int)label[i].p);
1264                                         jitCompPutImm32(&w, (int)label[i].p1);
1265                                         jitCompPutImm32(&w, 0); /* liveSign */
1266                                         jitCompPutImm32(&w, 2320); /* pls */
1267                                         jitCompPutImm32(&w, 0);
1268                                         jitCompPutImm32(&w, 0);
1269                                         j--;
1270                                 }
1271                         }
1272                         if (lastlabel >= 0 && label[lastlabel].p1 < w.dst)
1273                                 label[lastlabel].p1 = w.dst;
1274                         continue;
1275
1276                 case 0x3c:      /* ENTER */
1277                         jitCompA000_storeRegCacheAll(&w); // 手抜き.
1278                         jitCompA000_storePRegCacheAll(&w); // 手抜き.
1279                         jitCompPutByte2(w.dst, 0x6a, src[6]);   /* PUSH(?); */
1280                         jitCompPutByte2(w.dst, 0x6a, src[5]);   /* PUSH(?); */
1281                         jitCompPutByte2(w.dst, 0x6a, src[4] & 0x0f);    /* PUSH(?); */
1282                         jitCompPutByte2(w.dst, 0x6a, (src[4] >> 4) & 0x0f);     /* PUSH(?); */
1283                         jitCompPutByte2(w.dst, 0x6a, src[3]);   /* PUSH(?); */
1284                         jitCompPutByte2(w.dst, 0x6a, src[2]);   /* PUSH(?); */
1285                         jitCompPutByte2(w.dst, 0x6a, src[1]);   /* PUSH(?); */
1286                         jitCompPutByte1(w.dst, 0x55);   /* PUSH(EBP); */
1287                         jitCompPutByte1(w.dst, 0xe8);
1288                         j = ((unsigned char *)&func3c) - (w.dst + 4);
1289                         jitCompPutImm32(&w, j);
1290                         jitCompPutByte3(w.dst, 0x83, 0xc4, 0x20);       /* ADD(ESP,32); */
1291                         jitCompA000_loadRegCacheAll(&w); // 手抜き.
1292                         jitCompA000_loadPRegCacheAll(&w); // 手抜き.
1293                         cmp0reg = -1;
1294                         break;
1295
1296                 case 0x3d:      /* LEAVE */
1297                         jitCompA000_storeRegCacheAll(&w); // 手抜き.
1298                         jitCompA000_storePRegCacheAll(&w); // 手抜き.
1299                         jitCompPutByte2(w.dst, 0x6a, src[6]);   /* PUSH(?); */
1300                         jitCompPutByte2(w.dst, 0x6a, src[5]);   /* PUSH(?); */
1301                         jitCompPutByte2(w.dst, 0x6a, src[4] & 0x0f);    /* PUSH(?); */
1302                         jitCompPutByte2(w.dst, 0x6a, (src[4] >> 4) & 0x0f);     /* PUSH(?); */
1303                         jitCompPutByte2(w.dst, 0x6a, src[3]);   /* PUSH(?); */
1304                         jitCompPutByte2(w.dst, 0x6a, src[2]);   /* PUSH(?); */
1305                         jitCompPutByte2(w.dst, 0x6a, src[1]);   /* PUSH(?); */
1306                         jitCompPutByte1(w.dst, 0x55);   /* PUSH(EBP); */
1307                         jitCompPutByte1(w.dst, 0xe8);
1308                         j = ((unsigned char *)&func3d) - (w.dst + 4);
1309                         jitCompPutImm32(&w, j);
1310                         jitCompPutByte3(w.dst, 0x83, 0xc4, 0x20);       /* ADD(ESP,32); */
1311                         jitCompA000_loadRegCacheAll(&w); // 手抜き.
1312                         jitCompA000_loadPRegCacheAll(&w); // 手抜き.
1313                         cmp0reg = -1;
1314                         break;
1315
1316                 case 0xfe:      /* remark */
1317                         if (src[1] == 0x01 && src[2] == 0x00) { // DBGINFO1
1318                                 if (level <= JITC_LV_SLOWER) {
1319                                         jitCompPutByte1(w.dst, 0xb8);   /* MOV(EAX, ?); */
1320                                         jitCompPutImm32(&w, debugInfo1);
1321                                         jitCompA0001_movEbpDispReg32(&w, 2304 + 4, 0 /* EAX */); /* MOV(debugInfo1, EAX); */
1322                                 }
1323                         }
1324                         if (src[1] == 0x01 && src[2] == 0x03) { // DBGINFO1CLR
1325                                 if (level <= JITC_LV_SLOWER) {
1326                                         jitCompPutByte1(w.dst, 0xb8);   /* MOV(EAX, ?); */
1327                                         jitCompPutImm32(&w, -1);
1328                                         jitCompA0001_movEbpDispReg32(&w, 2304 + 4, 0 /* EAX */); /* MOV(debugInfo1, EAX); */
1329                                 }
1330                         }
1331                         if (src[1] == 0x05 && src[2] == 0x00) { // DBGINFO0
1332                                 if (level <= JITC_LV_SLOWEST) {
1333                                         debugInfo0 = jitCompGetImm32(src + 3);
1334                                         //      jitCompPutByte1(w.dst, 0xbf);   /* MOV(EDI, ?); */
1335                                         //      jitCompPutImm32(&w, debugInfo0);
1336                                         jitCompPutByte1(w.dst, 0xb8);   /* MOV(EAX, ?); */
1337                                         jitCompPutImm32(&w, debugInfo0);
1338                                         jitCompA0001_movEbpDispReg32(&w, 2304 + 0, 0 /* EAX */); /* MOV(debugInfo0, EAX); */
1339                                 }
1340                         }
1341                         break;
1342
1343                 default:
1344                         w.err = JITC_ERR_OPECODE;
1345                         goto err_w;
1346                 }
1347                 if (w.err != 0) goto err_w;
1348                 jitCompA0001_fixPrefix(&w);
1349                 if (w.err != 0) goto err_w;
1350                 src += jitCompCmdLen(src);
1351         }
1352         if (enter0 != NULL) {
1353                 j = w.dst - (enter0 + 4);
1354                 enter0[0] = j & 0xff;
1355                 enter0[1] = (j >> 8) & 0xff;
1356                 enter0[2] = (j >> 16) & 0xff;
1357                 enter0[3] = (j >> 24) & 0xff;
1358         }
1359         if ((flags & JITC_NOSTARTUP) == 0) {
1360                 jitCompA000_storeRegCacheAll(&w);
1361                 jitCompA000_storePRegCacheAll(&w);
1362                 jitCompPutByte1(w.dst, 0x61); /* POPAD(); */
1363         }
1364         if ((flags & JITC_PHASE1) != 0)
1365                 return w.dst - dst00;
1366         return 0;
1367
1368 err_w:
1369         if ((w.err & JITC_ERR_PHASE0ONLY) != 0) {
1370                 if ((flags & JITC_PHASE1) == 0)
1371                         w.err &= ~JITC_ERR_PHASE0ONLY;
1372         }
1373         if (w.err == (JITC_ERR_MASK & JITC_ERR_REGNUM))                 errmsg = "reg-number error";
1374         if (w.err == (JITC_ERR_MASK & JITC_ERR_DST1))                   errmsg = "dst1 error";
1375         if (w.err == (JITC_ERR_MASK & JITC_ERR_OPECODE))                errmsg = "opecode error";
1376         if (w.err == (JITC_ERR_MASK & JITC_ERR_LABELNUM))               errmsg = "label number too large";
1377         if (w.err == (JITC_ERR_MASK & JITC_ERR_LABELREDEF))             errmsg = "label redefine";
1378         if (w.err == (JITC_ERR_MASK & JITC_ERR_PREFIX))                 { errmsg = "prefix redefine"; w.dst -= 2; }
1379         if (w.err == (JITC_ERR_MASK & JITC_ERR_LABELNODEF))             errmsg = "label not defined";
1380         if (w.err == (JITC_ERR_MASK & JITC_ERR_LABELTYP))               errmsg = "label type error";
1381         if (w.err == (JITC_ERR_MASK & JITC_ERR_IDIOM))                  errmsg = "idiom error";
1382         if (w.err == (JITC_ERR_MASK & JITC_ERR_PREGNUM))                errmsg = "preg-number error";
1383         if (w.err == (JITC_ERR_MASK & JITC_ERR_SRC1))                   errmsg = "src1 error";
1384         if (w.err == (JITC_ERR_MASK & JITC_ERR_BADTYPE))                errmsg = "bad type code";
1385         if (w.err == (JITC_ERR_MASK & JITC_ERR_PREFIXFAR))              errmsg = "prefix internal error";
1386         if (w.err == (JITC_ERR_MASK & JITC_ERR_INTERNAL))               errmsg = "general internal error";
1387         if (*errmsg != '\0') {
1388                 fprintf(stderr, "JITC: %s at %06X (debugInfo0=%d)\n    ", errmsg, src - src0, debugInfo0);
1389                 for (i = 0; i < 16; i++)
1390                         fprintf(stderr, "%02X ", src[i]);
1391                 static char *table[0x30] = {
1392                         "NOP", "LB", "LIMM", "PLIMM", "CND", "??", "??", "??",
1393                         "LMEM", "SMEM", "PLMEM", "PSMEM", "LEA", "??", "PADD", "PDIF",
1394                         "CP/OR", "XOR", "AND", "??", "ADD", "SUB", "MUL", "??",
1395                         "SHL", "SAR", "DIV", "MOD", "PLMT0", "PLMT1", "PCP", "PCST",
1396                         "CMPE", "CMPNE", "CMPL", "CMPGE", "CMPLE", "CMPG", "TSTZ", "TSTNZ",
1397                         "PCMPE", "PCMPNE", "PCMPL", "PCMPGE", "PCMPLE", "PCMPG", "??", "EXT" };
1398                 errmsg = "??";
1399                 if (*src < 0x30) errmsg = table[*src];
1400                 fprintf(stderr, "(%s)\n", errmsg);
1401         }
1402         return -1;
1403 }
1404
1405 unsigned char *jitCompCallFunc(unsigned char *dst, void *func)
1406 {
1407         struct JitCompWork w;
1408         w.dst = dst;
1409         jitCompA000_storeRegCacheAll(&w);
1410         jitCompA000_storePRegCacheAll(&w);
1411         jitCompPutByte1(w.dst, 0x60);   /* PUSHAD(); */
1412         jitCompPutByte1(w.dst, 0x50);   /* PUSH(EAX); */        /* for 16byte-align(win32では不要なのだけど、MacOSには必要らしい) */
1413         jitCompPutByte1(w.dst, 0x55);   /* PUSH(EBP); */
1414         jitCompPutByte1(w.dst, 0xe8);   /* CALL(func); */
1415         int j = ((unsigned char *)func) - (w.dst + 4);
1416         jitCompPutImm32(&w, j);
1417         jitCompPutByte1(w.dst, 0x58);   /* POP(EAX); */         /* (win32では不要なのだけど、MacOSには必要らしい) */
1418         jitCompPutByte1(w.dst, 0x58);   /* POP(EAX); */
1419         jitCompPutByte1(w.dst, 0x61);   /* POPAD(); */
1420         jitCompA000_loadRegCacheAll(&w);
1421         jitCompA000_loadPRegCacheAll(&w);
1422         jitCompA0001_movReg32EbpDisp(&w, 0 /* EAX */, 256 + 0x30 * 32 + 0); /* MOV(EAX, [EBP+?]); */
1423         jitCompPutByte2(w.dst, 0xff, 0xe0);     /* JMP(EAX); */
1424         return w.dst;
1425 }
1426
1427 unsigned char *jitCompInit(unsigned char *dst)
1428 {
1429         errfnc = dst;
1430         return jitCompCallFunc(dst, &errHndl);
1431 }
1432
1433 void func3c(char *ebp, int opt, int r1, int p1, int lenR, int lenP, int r0, int p0)
1434 {
1435         struct Regs *r = (struct Regs *) (ebp - jitCompA0001_EBP128);
1436         int i, *pi;
1437         struct Ptr *pp;
1438         if (r->junkStack + 2048 > r->junkStack1) (*(r->errHndl))(r);
1439         pi = (void *)r->junkStack; r->junkStack += r1 * 4;
1440         for (i = 0; i < r1; i++)
1441                 pi[i] = r->ireg[i];
1442         pp = (void *)r->junkStack; r->junkStack += p1 * 32;
1443         for (i = 0; i < p1; i++)
1444                 pp[i] = r->preg[i];
1445         pp = (void *)r->junkStack; r->junkStack += 32;
1446         *pp = r->preg[0x30];
1447         pi = (void *)r->junkStack; r->junkStack += 4;
1448         *pi = opt << 16 | r1 << 8 | p1;
1449         for (i = 0; i < lenR; i++)
1450                 r->ireg[r0 + i] = r->ireg[0x30 + i];
1451         for (i = 0; i < lenP; i++)
1452                 r->preg[p0 + i] = r->preg[0x31 + i];
1453         return;
1454 }
1455
1456 void func3d(char *ebp, int opt, int r1, int p1, int lenR, int lenP, int r0, int p0)
1457 {
1458         struct Regs *r = (struct Regs *) (ebp - jitCompA0001_EBP128);
1459         int i;
1460         r->junkStack -= 4;
1461         r->junkStack -= 32; struct Ptr *pp = (void *)r->junkStack;
1462         r->preg[0x30] = *pp;
1463         r->junkStack -= p1 * 32; pp = (void *)r->junkStack;
1464         for (i = 0; i < p1; i++)
1465                 r->preg[i] = pp[i];
1466         r->junkStack -= r1 * 4; int *pi = (void *)r->junkStack;
1467         for (i = 0; i < r1; i++)
1468                 r->ireg[i] = pi[i];
1469         return;
1470 }
1471
1472 void funcf4(char *ebp, int pxx, int typ, int len)
1473 {
1474         struct Regs *r = (struct Regs *) (ebp - jitCompA0001_EBP128);
1475         int width = jitCompA000_dataWidth(jitCompA000_convTyp(r->ireg[typ])) >> 3;
1476         if (width < 0 || r->ireg[len] < 0)
1477                 (*(r->errHndl))(r);
1478         void *p = r->junkStack;
1479         if (r->junkStack + width * r->ireg[len] + 256 > r->junkStack1) (*(r->errHndl))(r);
1480         r->junkStack += width * r->ireg[len];
1481         r->preg[pxx].p = p;
1482         r->preg[pxx].typ = r->ireg[typ];
1483         r->preg[pxx].p0 = p;
1484         r->preg[pxx].p1 = (void *)r->junkStack;
1485         int *pi = (int *)r->junkStack;
1486         *pi = width * r->ireg[len];
1487         r->junkStack += sizeof (int);
1488         if (r->ireg[typ] == 1) {
1489                 int i, i1 = (width * r->ireg[len]) >> 2;
1490                 pi = p;
1491                 for (i = 0; i < i1; i++)
1492                         pi[i] = 0;
1493         }
1494         return;
1495 }
1496
1497 void funcf5(char *ebp, int pxx, int typ, int len)
1498 {
1499         struct Regs *r = (struct Regs *) (ebp - jitCompA0001_EBP128);
1500         r->junkStack -= sizeof (int);
1501         int *pi = (int *)r->junkStack;
1502         r->junkStack -= *pi;
1503 #if 0
1504         int width = jitCompA000_dataWidth(r->ireg[typ]);
1505         void *p = r->junkStack;
1506         r->junkStack -= width * r->ireg[len];
1507 #endif
1508         return;
1509 }
1510
1511 void funcf6(char *ebp, int pxx, int typ, int len)
1512 {
1513         struct Regs *r = (struct Regs *) (ebp - jitCompA0001_EBP128);
1514         int width = jitCompA000_dataWidth(jitCompA000_convTyp(r->ireg[typ])) >> 3;
1515         if (width < 0 || r->ireg[len] < 0)
1516                 (*(r->errHndl))(r);
1517         void *p = malloc(width * r->ireg[len]);
1518         r->preg[pxx].p = p;
1519         r->preg[pxx].typ = r->ireg[typ];
1520         r->preg[pxx].p0 = p;
1521         r->preg[pxx].p1 = (unsigned char *)p + width * r->ireg[len];
1522         if (r->ireg[typ] == 1) {
1523                 int i, i1 = (width * r->ireg[len]) >> 2, *pi;
1524                 pi = p;
1525                 for (i = 0; i < i1; i++)
1526                         pi[i] = 0;
1527                 for (i = 1; i < i1; i += 8)
1528                         pi[i] |= -1;
1529         }
1530         return;
1531 }
1532
1533 void funcf7(char *ebp, int pxx, int typ, int len)
1534 {
1535         struct Regs *r = (struct Regs *) (ebp - jitCompA0001_EBP128);
1536         free(r->preg[pxx].p);
1537         return;
1538 }
1539
1540 void errHndl(struct Regs *r)
1541 {
1542         r = (struct Regs *) (((char *)r) - jitCompA0001_EBP128);
1543         (*(r->errHndl))(r);
1544         // ここに帰ってきてはいけない.
1545 }
1546
1547 int jitc0(unsigned char **qq, unsigned char *q1, const unsigned char *p0, const unsigned char *p1, int level, struct LabelTable *label)
1548 {
1549         unsigned char *q = *qq;
1550         if (p0[0] != 0x05 || p0[1] != SIGN1)
1551                 return 1;
1552
1553         *q++ = 0x55; /* PUSH(EBP); */
1554         *q++ = 0x8b; *q++ = 0x6c; *q++ = 0x24; *q++ = 0x08; /* MOV(EBP,[ESP+8]); */
1555
1556         int i;
1557         for (i = 0; i < JITC_MAXLABELS; i++)
1558                 label[i].opt = 0;
1559
1560         i = jitCompiler(q, q1, p0 + 2, p1, p0, label, JITC_MAXLABELS, level, di1_serial, 0);
1561         if (i != 0) return 2;
1562         i = jitCompiler(q, q1, p0 + 2, p1, p0, label, JITC_MAXLABELS, level, di1_serial, JITC_PHASE1 + 0);
1563         if (i < 0) return 2;
1564         q += i;
1565
1566         *q++ = 0x5d; /* POP(EBP); */
1567         *q++ = 0xc3; /* RET(); */
1568
1569         *qq = q;
1570         return 0;
1571 }
1572
1573 #if (USE_DEBUGGER != 0)
1574
1575 int dbgrGetRegNum(const char *p)
1576 {
1577         int i, j, r = -1;
1578         if (p[2] <= ' ') {
1579                 i = p[0] - '0';
1580                 j = p[1] - '0';
1581                 if (i > 9) i -= 'A' - '0' - 10;
1582                 if (j > 9) j -= 'A' - '0' - 10;
1583                 if (0 <= i && i <= 15 && 0 <= j && j <= 15)
1584                         r = i << 4 | j;
1585         }
1586         return r;
1587 }
1588
1589 void dbgrMain(struct Regs *r)
1590 {
1591         if (r->dbgr == 0) return;
1592         for (;;) {
1593                 char cmd[64], *p;
1594                 int i, j, k;
1595                 printf("\ndbgr>");
1596                 p = fgets(cmd, 64, stdin);
1597                 if (p == NULL) break;
1598                 if (cmd[0] == '\0') continue;
1599                 if (cmd[0] == 'q' && cmd[1] <= ' ') break;
1600                 if (cmd[0] == 'p' && cmd[1] <= ' ' && cmd[1] != '\0') {
1601                         p = &cmd[2];
1602                         while (*p <= ' ' && *p != '\0') p++;
1603                         if (*p == 'R') {
1604                                 i = dbgrGetRegNum(p + 1);
1605                                 if (0 <= i && i <= 0x3f) {
1606                                         printf("R%02X = 0x%08X = %d\n", i, r->ireg[i], r->ireg[i]);
1607                                 }
1608                                 else
1609                                         puts("register name error");
1610                                 continue;
1611                         }
1612                         if (*p == 'P') {
1613                                 i = dbgrGetRegNum(p + 1);
1614                                 if (0 <= i && i <= 0x3f) {
1615                                         p = "invalid";
1616                                         if (0 <= r->preg[i].typ && r->preg[i].typ <= 0x15) {
1617                                                 static char *typName[] = {
1618                                                         "T_CODE", "T_VPTR", "T_SINT8", "T_UINT8",
1619                                                         "T_SINT16", "T_UINT16", "T_SINT32", "T_UINT32",
1620                                                         "T_SINT4", "T_UINT4", "T_SINT2", "T_UINT2",
1621                                                         "T_SINT1", "T_UINT1", "T_SINT12", "T_UINT12",
1622                                                         "T_SINT20", "T_UINT20", "T_SINT24", "T_UINT24",
1623                                                         "T_SINT28", "T_UINT28"
1624                                                 };
1625                                                 p = typName[r->preg[i].typ];
1626                                         }
1627                                         printf("P%02X:\n  type = %s(%04X),  (origin-ptr) = 0x%08X\n", i, p, r->preg[i].typ, (unsigned int)(r->preg[i].p0));
1628                                         if (r->preg[i].p != NULL && r->preg[i].p0 != NULL) {
1629                                                 j = jitCompA000_dataWidth(jitCompA000_convTyp(r->preg[i].typ)) >> 3;
1630                                                 if (j <= 0) j = 1;
1631                                                 k = (r->preg[i].p1 - r->preg[i].p0) / j;
1632                                                 printf("  size = 0x%08X = %d\n", k, k);
1633                                                 k = (r->preg[i].p - r->preg[i].p0) / j;
1634                                                 printf("  pos  = 0x%08X = %d\n", k, k);
1635                                         }
1636                                         else {
1637                                                 puts("  null pointer");
1638                                         }
1639                                 }
1640                                 else
1641                                         puts("register name error");
1642                                 continue;
1643                         }
1644                 }
1645                 puts("command error");
1646         }
1647         return;
1648 }
1649
1650 #endif
1651
1652
1653 #endif
1654
1655
1656