OSDN Git Service

JITCのソース整理
authorhikarupsp <hikarupsp@users.sourceforge.jp>
Sat, 15 Mar 2014 07:51:43 +0000 (16:51 +0900)
committerhikarupsp <hikarupsp@users.sourceforge.jp>
Sat, 15 Mar 2014 07:51:43 +0000 (16:51 +0900)
jitc.h
jitcx86.c

diff --git a/jitc.h b/jitc.h
index 7fc937a..3fa9efd 100644 (file)
--- a/jitc.h
+++ b/jitc.h
@@ -54,13 +54,16 @@ int jitCompCmdLen(const unsigned char *src);
 #define envOffset_DBGINFO1             (2304 + 4)\r
 //\r
 #define jitCompPutImm32(p, i)                                  jitCompPutByte4(p, ((i) & 0xff), (((i) >> 8) & 0xff), (((i) >> 16) & 0xff), (((i) >> 24) & 0xff))\r
+//\r
+#define jitCompPutOp_ADD_GReg_Imm8(p, dReg, i) jitCompPutByte3(p, 0x83, 0xc0 | dReg, i);               /* ADD(reg0, imm8);  == [1000 0011] [11000 reg] imm8 */\r
 #define jitCompPutOp_XOR_GReg_GReg(p, d, s)            jitCompPutByte2(w.dst, 0x31, 0xc0 | (s) << 3 | (d));\r
-#define jitCompPutOp_MOV_GReg_Imm32(p, dReg, i)        jitCompPutByte1(p, 0xb8 | dReg); jitCompPutImm32(p, i); /* MOV(reg0, ?);  == [1011 1 reg] imm32 */\r
+#define jitCompPutOp_MOV_GReg_Imm32(p, dReg, i)        jitCompPutByte1(p, 0xb8 | dReg); jitCompPutImm32(p, i); /* MOV(reg0, imm32);  == [1011 1 reg] imm32 */\r
 #define jitCompPutOp_PUSHAD(p)                                 jitCompPutByte1(p, 0x60);\r
 #define jitCompPutOp_POPAD(p)                                  jitCompPutByte1(p, 0x61);\r
 #define jitCompPutOp_PUSH_GReg(p, reg)                 jitCompPutByte1(p, 0x50 | (reg));\r
+#define jitCompPutOp_PUSH_Imm8(p, i)                           jitCompPutByte2(p, 0x6a, i);\r
 #define jitCompPutOp_POP_GReg(p, reg)                  jitCompPutByte1(p, 0x58 | (reg));\r
-#define jitCompPutOp_CALL_Relative(p, diff)            jitCompPutByte1(w.dst, 0xe8); jitCompPutImm32(&w, j);\r
+#define jitCompPutOp_CALL_Relative(p, diff)            jitCompPutByte1(p, 0xe8); jitCompPutImm32(p, j);\r
 //\r
 #define jitCompPutOp_MOV_EAX_ZERO(p)                           jitCompPutOp_XOR_GReg_GReg(p, IA32_REG0_EAX, IA32_REG0_EAX);\r
 \r
index 3012629..c87d77c 100644 (file)
--- a/jitcx86.c
+++ b/jitcx86.c
@@ -34,8 +34,8 @@ int jitCompiler(unsigned char *dst, unsigned char *dst1, const unsigned char *sr
                jitCompA000_loadPRegCacheAll(&w);\r
        }\r
        if (level <= JITC_LV_SLOWER) {\r
-               // debugInfo0 <- 0;\r
-               // \r
+               // env.debugInfo0 <- 0;\r
+               // env.debugInfo1 <- debugInfo1;\r
                jitCompPutOp_MOV_EAX_ZERO(w.dst);\r
                // MOV(debugInfo0, EAX);\r
                jitCompA0001_movEbpDispReg32(&w, envOffset_DBGINFO0, IA32_REG0_EAX);\r
@@ -46,7 +46,8 @@ int jitCompiler(unsigned char *dst, unsigned char *dst1, const unsigned char *sr
                w.prefix = 0;   //0x04 CND 命令で変更される\r
                if (w.dst + 256 > dst1) {\r
                        // 書き込み領域が残り256バイト未満ならエラー\r
-                       w.err = JITC_ERR_DST1; goto err_w;\r
+                       w.err = JITC_ERR_DST1;\r
+                       goto err_w;\r
                }\r
                timecount++;\r
                if (timecount >= 64) {\r
@@ -60,7 +61,8 @@ int jitCompiler(unsigned char *dst, unsigned char *dst1, const unsigned char *sr
                                // NOP\r
                                if (w.prefix != 0) {\r
                                        // 「条件付きでNOPを実行」するなんて、矛盾している!\r
-                                       w.err = JITC_ERR_PREFIX; goto err_w;\r
+                                       w.err = JITC_ERR_PREFIX;\r
+                                       goto err_w;\r
                                }\r
                                break;\r
                                \r
@@ -1020,17 +1022,29 @@ int jitCompiler(unsigned char *dst, unsigned char *dst1, const unsigned char *sr
                        case 0x33:      /* mfree(old:F7) */\r
                                jitCompA000_storeRegCacheAll(&w); // 手抜き.\r
                                jitCompA000_storePRegCacheAll(&w); // 手抜き.\r
-                               jitCompPutByte2(w.dst, 0x6a, src[3]);   /* PUSH(?); */\r
-                               jitCompPutByte2(w.dst, 0x6a, src[2]);   /* PUSH(?); */\r
-                               jitCompPutByte2(w.dst, 0x6a, src[1]);   /* PUSH(?); */\r
-                               jitCompPutByte1(w.dst, 0x55);   /* PUSH(EBP); */\r
-                               jitCompPutByte1(w.dst, 0xe8);\r
-                               if (*src == 0x30) j = ((unsigned char *)&funcf4) - (w.dst + 4);\r
-                               if (*src == 0x31) j = ((unsigned char *)&funcf5) - (w.dst + 4);\r
-                               if (*src == 0x32) j = ((unsigned char *)&funcf6) - (w.dst + 4);\r
-                               if (*src == 0x33) j = ((unsigned char *)&funcf7) - (w.dst + 4);\r
-                               jitCompPutImm32(w.dst, j);\r
-                               jitCompPutByte3(w.dst, 0x83, 0xc4, 0x10);       /* ADD(ESP,16); */\r
+                               \r
+                               jitCompPutOp_PUSH_Imm8(w.dst, src[3]);\r
+                               jitCompPutOp_PUSH_Imm8(w.dst, src[2]);\r
+                               jitCompPutOp_PUSH_Imm8(w.dst, src[1]);\r
+                               jitCompPutOp_PUSH_GReg(w.dst, IA32_REG5_EBP);\r
+                               \r
+                               switch (*src) {\r
+                                       case 0x30:\r
+                                               j = ((unsigned char *)&funcf4) - (w.dst + 1 + 4);\r
+                                               break;\r
+                                       case 0x31:\r
+                                               j = ((unsigned char *)&funcf5) - (w.dst + 1 + 4);\r
+                                               break;\r
+                                       case 0x32:\r
+                                               j = ((unsigned char *)&funcf6) - (w.dst + 1 + 4);\r
+                                               break;\r
+                                       case 0x33:\r
+                                               j = ((unsigned char *)&funcf7) - (w.dst + 1 + 4);\r
+                                               break;\r
+                               }\r
+                               jitCompPutOp_CALL_Relative(w.dst, j);\r
+                               jitCompPutOp_ADD_GReg_Imm8(w.dst, IA32_REG4_ESP, 16);\r
+                               \r
                                jitCompA000_loadRegCacheAll(&w); // 手抜き.\r
                                jitCompA000_loadPRegCacheAll(&w); // 手抜き.\r
                                cmp0reg = -1;\r
@@ -1099,18 +1113,17 @@ int jitCompiler(unsigned char *dst, unsigned char *dst1, const unsigned char *sr
                        case 0x3c:      /* ENTER */\r
                                jitCompA000_storeRegCacheAll(&w); // 手抜き.\r
                                jitCompA000_storePRegCacheAll(&w); // 手抜き.\r
-                               jitCompPutByte2(w.dst, 0x6a, src[6]);   /* PUSH(?); */\r
-                               jitCompPutByte2(w.dst, 0x6a, src[5]);   /* PUSH(?); */\r
-                               jitCompPutByte2(w.dst, 0x6a, src[4] & 0x0f);    /* PUSH(?); */\r
-                               jitCompPutByte2(w.dst, 0x6a, (src[4] >> 4) & 0x0f);     /* PUSH(?); */\r
-                               jitCompPutByte2(w.dst, 0x6a, src[3]);   /* PUSH(?); */\r
-                               jitCompPutByte2(w.dst, 0x6a, src[2]);   /* PUSH(?); */\r
-                               jitCompPutByte2(w.dst, 0x6a, src[1]);   /* PUSH(?); */\r
-                               jitCompPutByte1(w.dst, 0x55);   /* PUSH(EBP); */\r
-                               jitCompPutByte1(w.dst, 0xe8);\r
-                               j = ((unsigned char *)&func3c) - (w.dst + 4);\r
-                               jitCompPutImm32(w.dst, j);\r
-                               jitCompPutByte3(w.dst, 0x83, 0xc4, 0x20);       /* ADD(ESP,32); */\r
+                               jitCompPutOp_PUSH_Imm8(w.dst, src[6]);\r
+                               jitCompPutOp_PUSH_Imm8(w.dst, src[5]);\r
+                               jitCompPutOp_PUSH_Imm8(w.dst, src[4] & 0x0f);\r
+                               jitCompPutOp_PUSH_Imm8(w.dst, (src[4] >> 4) & 0x0f);\r
+                               jitCompPutOp_PUSH_Imm8(w.dst, src[3]);\r
+                               jitCompPutOp_PUSH_Imm8(w.dst, src[2]);\r
+                               jitCompPutOp_PUSH_Imm8(w.dst, src[1]);\r
+                               jitCompPutOp_PUSH_GReg(w.dst, IA32_REG5_EBP);\r
+                               j = ((unsigned char *)&func3c) - (w.dst + 1 + 4);\r
+                               jitCompPutOp_CALL_Relative(w.dst, j)\r
+                               jitCompPutOp_ADD_GReg_Imm8(w.dst, IA32_REG4_ESP, 32);\r
                                jitCompA000_loadRegCacheAll(&w); // 手抜き.\r
                                jitCompA000_loadPRegCacheAll(&w); // 手抜き.\r
                                cmp0reg = -1;\r
@@ -1119,18 +1132,17 @@ int jitCompiler(unsigned char *dst, unsigned char *dst1, const unsigned char *sr
                        case 0x3d:      /* LEAVE */\r
                                jitCompA000_storeRegCacheAll(&w); // 手抜き.\r
                                jitCompA000_storePRegCacheAll(&w); // 手抜き.\r
-                               jitCompPutByte2(w.dst, 0x6a, src[6]);   /* PUSH(?); */\r
-                               jitCompPutByte2(w.dst, 0x6a, src[5]);   /* PUSH(?); */\r
-                               jitCompPutByte2(w.dst, 0x6a, src[4] & 0x0f);    /* PUSH(?); */\r
-                               jitCompPutByte2(w.dst, 0x6a, (src[4] >> 4) & 0x0f);     /* PUSH(?); */\r
-                               jitCompPutByte2(w.dst, 0x6a, src[3]);   /* PUSH(?); */\r
-                               jitCompPutByte2(w.dst, 0x6a, src[2]);   /* PUSH(?); */\r
-                               jitCompPutByte2(w.dst, 0x6a, src[1]);   /* PUSH(?); */\r
-                               jitCompPutByte1(w.dst, 0x55);   /* PUSH(EBP); */\r
-                               jitCompPutByte1(w.dst, 0xe8);\r
-                               j = ((unsigned char *)&func3d) - (w.dst + 4);\r
-                               jitCompPutImm32(w.dst, j);\r
-                               jitCompPutByte3(w.dst, 0x83, 0xc4, 0x20);       /* ADD(ESP,32); */\r
+                               jitCompPutOp_PUSH_Imm8(w.dst, src[6]);\r
+                               jitCompPutOp_PUSH_Imm8(w.dst, src[5]);\r
+                               jitCompPutOp_PUSH_Imm8(w.dst, src[4] & 0x0f);\r
+                               jitCompPutOp_PUSH_Imm8(w.dst, (src[4] >> 4) & 0x0f);\r
+                               jitCompPutOp_PUSH_Imm8(w.dst, src[3]);\r
+                               jitCompPutOp_PUSH_Imm8(w.dst, src[2]);\r
+                               jitCompPutOp_PUSH_Imm8(w.dst, src[1]);\r
+                               jitCompPutOp_PUSH_GReg(w.dst, IA32_REG5_EBP);\r
+                               j = ((unsigned char *)&func3d) - (w.dst + 1 + 4);\r
+                               jitCompPutOp_CALL_Relative(w.dst, j)\r
+                               jitCompPutOp_ADD_GReg_Imm8(w.dst, IA32_REG4_ESP, 32);\r
                                jitCompA000_loadRegCacheAll(&w); // 手抜き.\r
                                jitCompA000_loadPRegCacheAll(&w); // 手抜き.\r
                                cmp0reg = -1;\r
@@ -1227,21 +1239,20 @@ err_w:
 \r
 unsigned char *jitCompCallFunc(unsigned char *dst, void *func)\r
 {\r
+       //この関数の中では結局w->dstしか参照していない\r
        struct JitCompWork w;\r
        w.dst = dst;\r
        jitCompA000_storeRegCacheAll(&w);\r
        jitCompA000_storePRegCacheAll(&w);\r
        jitCompPutOp_PUSHAD(w.dst);\r
        jitCompPutOp_PUSH_GReg(w.dst, IA32_REG0_EAX);   /* for 16Byte-align(Mac OSX) */\r
-       jitCompPutOp_PUSH_GReg(w.dst, IA32_REG5_EBP);   /* PUSH(EBP); */\r
-       jitCompPutByte1(w.dst, 0xe8);   /* CALL(func); */\r
-       int j = ((unsigned char *)func) - (w.dst + 4);\r
+       jitCompPutOp_PUSH_GReg(w.dst, IA32_REG5_EBP);\r
        \r
-       //この関数の中では結局w->dstしか参照していない\r
-       jitCompPutImm32(w.dst, j);\r
+       int j = ((unsigned char *)func) - (w.dst + 1 + 4);\r
+       jitCompPutOp_CALL_Relative(w.dst, j);\r
        \r
-       jitCompPutOp_POP_GReg(w.dst, IA32_REG0_EAX);            /* for 16Byte-align (Mac OSX) */\r
        jitCompPutOp_POP_GReg(w.dst, IA32_REG0_EAX);\r
+       jitCompPutOp_POP_GReg(w.dst, IA32_REG0_EAX);            /* for 16Byte-align (Mac OSX) */\r
        jitCompPutOp_POPAD(w.dst);\r
        jitCompA000_loadRegCacheAll(&w);\r
        jitCompA000_loadPRegCacheAll(&w);\r
@@ -1387,26 +1398,33 @@ void errHndl(HOSECPU_RuntimeEnvironment *r)
 int jitc0(unsigned char **qq, unsigned char *q1, const unsigned char *p0, const unsigned char *p1, int level, HOSECPU_LabelListTag *label)\r
 {\r
        unsigned char *q = *qq;\r
-       if (p0[0] != 0x05 || p0[1] != SIGN1)    // OSECPUのヘッダ (05E1) を確認\r
+       int i;\r
+       \r
+       if (p0[0] != 0x05 || p0[1] != SIGN1){\r
+               // OSECPUのヘッダ (05E1) を確認\r
                return 1;\r
+       }\r
        \r
-       *q++ = 0x55; /* PUSH(EBP); */\r
+       jitCompPutOp_PUSH_GReg(q, IA32_REG5_EBP);\r
        *q++ = 0x8b; *q++ = 0x6c; *q++ = 0x24; *q++ = 0x08; /* MOV(EBP,[ESP+8]); */\r
        \r
-       int i;\r
-       for (i = 0; i < JITC_MAXLABELS; i++)\r
+       for (i = 0; i < JITC_MAXLABELS; i++){\r
                label[i].opt = 0;\r
+       }\r
        \r
        // 以下のjitCompile()呼び出しでは第二引数をq1-2にした方がよいのではないか?\r
        i = jitCompiler(q, q1, p0 + 2, p1, p0, label, JITC_MAXLABELS, level, di1_serial, 0);\r
-       if (i != 0) return 2;\r
+       if (i != 0){\r
+               return 2;\r
+       }\r
        i = jitCompiler(q, q1, p0 + 2, p1, p0, label, JITC_MAXLABELS, level, di1_serial, JITC_PHASE1 + 0);\r
-       if (i < 0) return 2;\r
+       if (i < 0){\r
+               return 2;\r
+       }\r
        q += i;\r
        \r
-       *q++ = 0x5d; /* POP(EBP); */\r
+       jitCompPutOp_POP_GReg(q, IA32_REG5_EBP);\r
        *q++ = 0xc3; /* RET(); */\r
-       \r
        *qq = q;\r
        return 0;\r
 }\r