• R/O
  • HTTP
  • SSH
  • HTTPS

HeavyOSECPU: Commit


Commit MetaInfo

Revisión407970952f85715aa1fa0d2e34b4552ab30b36e7 (tree)
Tiempo2014-03-15 16:51:43
Autorhikarupsp <hikarupsp@user...>
Commiterhikarupsp

Log Message

JITCのソース整理

Cambiar Resumen

Diferencia incremental

--- a/jitc.h
+++ b/jitc.h
@@ -54,13 +54,16 @@ int jitCompCmdLen(const unsigned char *src);
5454 #define envOffset_DBGINFO1 (2304 + 4)
5555 //
5656 #define jitCompPutImm32(p, i) jitCompPutByte4(p, ((i) & 0xff), (((i) >> 8) & 0xff), (((i) >> 16) & 0xff), (((i) >> 24) & 0xff))
57+//
58+#define jitCompPutOp_ADD_GReg_Imm8(p, dReg, i) jitCompPutByte3(p, 0x83, 0xc0 | dReg, i); /* ADD(reg0, imm8); == [1000 0011] [11000 reg] imm8 */
5759 #define jitCompPutOp_XOR_GReg_GReg(p, d, s) jitCompPutByte2(w.dst, 0x31, 0xc0 | (s) << 3 | (d));
58-#define jitCompPutOp_MOV_GReg_Imm32(p, dReg, i) jitCompPutByte1(p, 0xb8 | dReg); jitCompPutImm32(p, i); /* MOV(reg0, ?); == [1011 1 reg] imm32 */
60+#define jitCompPutOp_MOV_GReg_Imm32(p, dReg, i) jitCompPutByte1(p, 0xb8 | dReg); jitCompPutImm32(p, i); /* MOV(reg0, imm32); == [1011 1 reg] imm32 */
5961 #define jitCompPutOp_PUSHAD(p) jitCompPutByte1(p, 0x60);
6062 #define jitCompPutOp_POPAD(p) jitCompPutByte1(p, 0x61);
6163 #define jitCompPutOp_PUSH_GReg(p, reg) jitCompPutByte1(p, 0x50 | (reg));
64+#define jitCompPutOp_PUSH_Imm8(p, i) jitCompPutByte2(p, 0x6a, i);
6265 #define jitCompPutOp_POP_GReg(p, reg) jitCompPutByte1(p, 0x58 | (reg));
63-#define jitCompPutOp_CALL_Relative(p, diff) jitCompPutByte1(w.dst, 0xe8); jitCompPutImm32(&w, j);
66+#define jitCompPutOp_CALL_Relative(p, diff) jitCompPutByte1(p, 0xe8); jitCompPutImm32(p, j);
6467 //
6568 #define jitCompPutOp_MOV_EAX_ZERO(p) jitCompPutOp_XOR_GReg_GReg(p, IA32_REG0_EAX, IA32_REG0_EAX);
6669
--- a/jitcx86.c
+++ b/jitcx86.c
@@ -34,8 +34,8 @@ int jitCompiler(unsigned char *dst, unsigned char *dst1, const unsigned char *sr
3434 jitCompA000_loadPRegCacheAll(&w);
3535 }
3636 if (level <= JITC_LV_SLOWER) {
37- // debugInfo0 <- 0;
38- //
37+ // env.debugInfo0 <- 0;
38+ // env.debugInfo1 <- debugInfo1;
3939 jitCompPutOp_MOV_EAX_ZERO(w.dst);
4040 // MOV(debugInfo0, EAX);
4141 jitCompA0001_movEbpDispReg32(&w, envOffset_DBGINFO0, IA32_REG0_EAX);
@@ -46,7 +46,8 @@ int jitCompiler(unsigned char *dst, unsigned char *dst1, const unsigned char *sr
4646 w.prefix = 0; //0x04 CND 命令で変更される
4747 if (w.dst + 256 > dst1) {
4848 // 書き込み領域が残り256バイト未満ならエラー
49- w.err = JITC_ERR_DST1; goto err_w;
49+ w.err = JITC_ERR_DST1;
50+ goto err_w;
5051 }
5152 timecount++;
5253 if (timecount >= 64) {
@@ -60,7 +61,8 @@ int jitCompiler(unsigned char *dst, unsigned char *dst1, const unsigned char *sr
6061 // NOP
6162 if (w.prefix != 0) {
6263 // 「条件付きでNOPを実行」するなんて、矛盾している!
63- w.err = JITC_ERR_PREFIX; goto err_w;
64+ w.err = JITC_ERR_PREFIX;
65+ goto err_w;
6466 }
6567 break;
6668
@@ -1020,17 +1022,29 @@ int jitCompiler(unsigned char *dst, unsigned char *dst1, const unsigned char *sr
10201022 case 0x33: /* mfree(old:F7) */
10211023 jitCompA000_storeRegCacheAll(&w); // 手抜き.
10221024 jitCompA000_storePRegCacheAll(&w); // 手抜き.
1023- jitCompPutByte2(w.dst, 0x6a, src[3]); /* PUSH(?); */
1024- jitCompPutByte2(w.dst, 0x6a, src[2]); /* PUSH(?); */
1025- jitCompPutByte2(w.dst, 0x6a, src[1]); /* PUSH(?); */
1026- jitCompPutByte1(w.dst, 0x55); /* PUSH(EBP); */
1027- jitCompPutByte1(w.dst, 0xe8);
1028- if (*src == 0x30) j = ((unsigned char *)&funcf4) - (w.dst + 4);
1029- if (*src == 0x31) j = ((unsigned char *)&funcf5) - (w.dst + 4);
1030- if (*src == 0x32) j = ((unsigned char *)&funcf6) - (w.dst + 4);
1031- if (*src == 0x33) j = ((unsigned char *)&funcf7) - (w.dst + 4);
1032- jitCompPutImm32(w.dst, j);
1033- jitCompPutByte3(w.dst, 0x83, 0xc4, 0x10); /* ADD(ESP,16); */
1025+
1026+ jitCompPutOp_PUSH_Imm8(w.dst, src[3]);
1027+ jitCompPutOp_PUSH_Imm8(w.dst, src[2]);
1028+ jitCompPutOp_PUSH_Imm8(w.dst, src[1]);
1029+ jitCompPutOp_PUSH_GReg(w.dst, IA32_REG5_EBP);
1030+
1031+ switch (*src) {
1032+ case 0x30:
1033+ j = ((unsigned char *)&funcf4) - (w.dst + 1 + 4);
1034+ break;
1035+ case 0x31:
1036+ j = ((unsigned char *)&funcf5) - (w.dst + 1 + 4);
1037+ break;
1038+ case 0x32:
1039+ j = ((unsigned char *)&funcf6) - (w.dst + 1 + 4);
1040+ break;
1041+ case 0x33:
1042+ j = ((unsigned char *)&funcf7) - (w.dst + 1 + 4);
1043+ break;
1044+ }
1045+ jitCompPutOp_CALL_Relative(w.dst, j);
1046+ jitCompPutOp_ADD_GReg_Imm8(w.dst, IA32_REG4_ESP, 16);
1047+
10341048 jitCompA000_loadRegCacheAll(&w); // 手抜き.
10351049 jitCompA000_loadPRegCacheAll(&w); // 手抜き.
10361050 cmp0reg = -1;
@@ -1099,18 +1113,17 @@ int jitCompiler(unsigned char *dst, unsigned char *dst1, const unsigned char *sr
10991113 case 0x3c: /* ENTER */
11001114 jitCompA000_storeRegCacheAll(&w); // 手抜き.
11011115 jitCompA000_storePRegCacheAll(&w); // 手抜き.
1102- jitCompPutByte2(w.dst, 0x6a, src[6]); /* PUSH(?); */
1103- jitCompPutByte2(w.dst, 0x6a, src[5]); /* PUSH(?); */
1104- jitCompPutByte2(w.dst, 0x6a, src[4] & 0x0f); /* PUSH(?); */
1105- jitCompPutByte2(w.dst, 0x6a, (src[4] >> 4) & 0x0f); /* PUSH(?); */
1106- jitCompPutByte2(w.dst, 0x6a, src[3]); /* PUSH(?); */
1107- jitCompPutByte2(w.dst, 0x6a, src[2]); /* PUSH(?); */
1108- jitCompPutByte2(w.dst, 0x6a, src[1]); /* PUSH(?); */
1109- jitCompPutByte1(w.dst, 0x55); /* PUSH(EBP); */
1110- jitCompPutByte1(w.dst, 0xe8);
1111- j = ((unsigned char *)&func3c) - (w.dst + 4);
1112- jitCompPutImm32(w.dst, j);
1113- jitCompPutByte3(w.dst, 0x83, 0xc4, 0x20); /* ADD(ESP,32); */
1116+ jitCompPutOp_PUSH_Imm8(w.dst, src[6]);
1117+ jitCompPutOp_PUSH_Imm8(w.dst, src[5]);
1118+ jitCompPutOp_PUSH_Imm8(w.dst, src[4] & 0x0f);
1119+ jitCompPutOp_PUSH_Imm8(w.dst, (src[4] >> 4) & 0x0f);
1120+ jitCompPutOp_PUSH_Imm8(w.dst, src[3]);
1121+ jitCompPutOp_PUSH_Imm8(w.dst, src[2]);
1122+ jitCompPutOp_PUSH_Imm8(w.dst, src[1]);
1123+ jitCompPutOp_PUSH_GReg(w.dst, IA32_REG5_EBP);
1124+ j = ((unsigned char *)&func3c) - (w.dst + 1 + 4);
1125+ jitCompPutOp_CALL_Relative(w.dst, j)
1126+ jitCompPutOp_ADD_GReg_Imm8(w.dst, IA32_REG4_ESP, 32);
11141127 jitCompA000_loadRegCacheAll(&w); // 手抜き.
11151128 jitCompA000_loadPRegCacheAll(&w); // 手抜き.
11161129 cmp0reg = -1;
@@ -1119,18 +1132,17 @@ int jitCompiler(unsigned char *dst, unsigned char *dst1, const unsigned char *sr
11191132 case 0x3d: /* LEAVE */
11201133 jitCompA000_storeRegCacheAll(&w); // 手抜き.
11211134 jitCompA000_storePRegCacheAll(&w); // 手抜き.
1122- jitCompPutByte2(w.dst, 0x6a, src[6]); /* PUSH(?); */
1123- jitCompPutByte2(w.dst, 0x6a, src[5]); /* PUSH(?); */
1124- jitCompPutByte2(w.dst, 0x6a, src[4] & 0x0f); /* PUSH(?); */
1125- jitCompPutByte2(w.dst, 0x6a, (src[4] >> 4) & 0x0f); /* PUSH(?); */
1126- jitCompPutByte2(w.dst, 0x6a, src[3]); /* PUSH(?); */
1127- jitCompPutByte2(w.dst, 0x6a, src[2]); /* PUSH(?); */
1128- jitCompPutByte2(w.dst, 0x6a, src[1]); /* PUSH(?); */
1129- jitCompPutByte1(w.dst, 0x55); /* PUSH(EBP); */
1130- jitCompPutByte1(w.dst, 0xe8);
1131- j = ((unsigned char *)&func3d) - (w.dst + 4);
1132- jitCompPutImm32(w.dst, j);
1133- jitCompPutByte3(w.dst, 0x83, 0xc4, 0x20); /* ADD(ESP,32); */
1135+ jitCompPutOp_PUSH_Imm8(w.dst, src[6]);
1136+ jitCompPutOp_PUSH_Imm8(w.dst, src[5]);
1137+ jitCompPutOp_PUSH_Imm8(w.dst, src[4] & 0x0f);
1138+ jitCompPutOp_PUSH_Imm8(w.dst, (src[4] >> 4) & 0x0f);
1139+ jitCompPutOp_PUSH_Imm8(w.dst, src[3]);
1140+ jitCompPutOp_PUSH_Imm8(w.dst, src[2]);
1141+ jitCompPutOp_PUSH_Imm8(w.dst, src[1]);
1142+ jitCompPutOp_PUSH_GReg(w.dst, IA32_REG5_EBP);
1143+ j = ((unsigned char *)&func3d) - (w.dst + 1 + 4);
1144+ jitCompPutOp_CALL_Relative(w.dst, j)
1145+ jitCompPutOp_ADD_GReg_Imm8(w.dst, IA32_REG4_ESP, 32);
11341146 jitCompA000_loadRegCacheAll(&w); // 手抜き.
11351147 jitCompA000_loadPRegCacheAll(&w); // 手抜き.
11361148 cmp0reg = -1;
@@ -1227,21 +1239,20 @@ err_w:
12271239
12281240 unsigned char *jitCompCallFunc(unsigned char *dst, void *func)
12291241 {
1242+ //この関数の中では結局w->dstしか参照していない
12301243 struct JitCompWork w;
12311244 w.dst = dst;
12321245 jitCompA000_storeRegCacheAll(&w);
12331246 jitCompA000_storePRegCacheAll(&w);
12341247 jitCompPutOp_PUSHAD(w.dst);
12351248 jitCompPutOp_PUSH_GReg(w.dst, IA32_REG0_EAX); /* for 16Byte-align(Mac OSX) */
1236- jitCompPutOp_PUSH_GReg(w.dst, IA32_REG5_EBP); /* PUSH(EBP); */
1237- jitCompPutByte1(w.dst, 0xe8); /* CALL(func); */
1238- int j = ((unsigned char *)func) - (w.dst + 4);
1249+ jitCompPutOp_PUSH_GReg(w.dst, IA32_REG5_EBP);
12391250
1240- //この関数の中では結局w->dstしか参照していない
1241- jitCompPutImm32(w.dst, j);
1251+ int j = ((unsigned char *)func) - (w.dst + 1 + 4);
1252+ jitCompPutOp_CALL_Relative(w.dst, j);
12421253
1243- jitCompPutOp_POP_GReg(w.dst, IA32_REG0_EAX); /* for 16Byte-align (Mac OSX) */
12441254 jitCompPutOp_POP_GReg(w.dst, IA32_REG0_EAX);
1255+ jitCompPutOp_POP_GReg(w.dst, IA32_REG0_EAX); /* for 16Byte-align (Mac OSX) */
12451256 jitCompPutOp_POPAD(w.dst);
12461257 jitCompA000_loadRegCacheAll(&w);
12471258 jitCompA000_loadPRegCacheAll(&w);
@@ -1387,26 +1398,33 @@ void errHndl(HOSECPU_RuntimeEnvironment *r)
13871398 int jitc0(unsigned char **qq, unsigned char *q1, const unsigned char *p0, const unsigned char *p1, int level, HOSECPU_LabelListTag *label)
13881399 {
13891400 unsigned char *q = *qq;
1390- if (p0[0] != 0x05 || p0[1] != SIGN1) // OSECPUのヘッダ (05E1) を確認
1401+ int i;
1402+
1403+ if (p0[0] != 0x05 || p0[1] != SIGN1){
1404+ // OSECPUのヘッダ (05E1) を確認
13911405 return 1;
1406+ }
13921407
1393- *q++ = 0x55; /* PUSH(EBP); */
1408+ jitCompPutOp_PUSH_GReg(q, IA32_REG5_EBP);
13941409 *q++ = 0x8b; *q++ = 0x6c; *q++ = 0x24; *q++ = 0x08; /* MOV(EBP,[ESP+8]); */
13951410
1396- int i;
1397- for (i = 0; i < JITC_MAXLABELS; i++)
1411+ for (i = 0; i < JITC_MAXLABELS; i++){
13981412 label[i].opt = 0;
1413+ }
13991414
14001415 // 以下のjitCompile()呼び出しでは第二引数をq1-2にした方がよいのではないか?
14011416 i = jitCompiler(q, q1, p0 + 2, p1, p0, label, JITC_MAXLABELS, level, di1_serial, 0);
1402- if (i != 0) return 2;
1417+ if (i != 0){
1418+ return 2;
1419+ }
14031420 i = jitCompiler(q, q1, p0 + 2, p1, p0, label, JITC_MAXLABELS, level, di1_serial, JITC_PHASE1 + 0);
1404- if (i < 0) return 2;
1421+ if (i < 0){
1422+ return 2;
1423+ }
14051424 q += i;
14061425
1407- *q++ = 0x5d; /* POP(EBP); */
1426+ jitCompPutOp_POP_GReg(q, IA32_REG5_EBP);
14081427 *q++ = 0xc3; /* RET(); */
1409-
14101428 *qq = q;
14111429 return 0;
14121430 }
Show on old repository browser