コミットメタ情報

リビジョン152bd3574280c5c7754983964bfd2eabc8fe0205 (tree)
日時2014-03-29 15:18:59
作者hikarupsp <hikarupsp@user...>
コミッターhikarupsp

ログメッセージ

HeavyOSECPUMainの各変数名を適切なものへ変更。

変更サマリ

差分

--- a/jitc.c
+++ b/jitc.c
@@ -12,6 +12,11 @@ void errorHandler(HOSECPU_RuntimeEnvironment *r)
1212 #if (ENABLE_DEBUG_CODE != 0)
1313 printf("Last opcode may be 0x%02X (Backend).\n", r->dbg_currentCode);
1414 #endif
15+
16+#if (ENABLE_DEBUG_CODE_ADDR != 0)
17+ printf("At backend code bin +0x%04X.\n", r->dbg_currentCodeAddress);
18+#endif
19+
1520 #if (USE_DEBUGGER != 0)
1621 dbgrMain(r);
1722 #endif
--- a/jitc.h
+++ b/jitc.h
@@ -1,157 +1,159 @@
1-
2-#ifndef HeavyOSECPU_jitc_h
3-#define HeavyOSECPU_jitc_h
4-
5-
6-
7-// Error flags
8-#define JITC_ERR_MASK 255
9-#define JITC_ERR_PHASE0ONLY 256
10-#define JITC_ERR_REGNUM (1 | JITC_ERR_PHASE0ONLY)
11-#define JITC_ERR_DST1 (2 | JITC_ERR_PHASE0ONLY)
12-#define JITC_ERR_OPECODE (3 | JITC_ERR_PHASE0ONLY)
13-#define JITC_ERR_LABELNUM (4 | JITC_ERR_PHASE0ONLY)
14-#define JITC_ERR_LABELREDEF (5 | JITC_ERR_PHASE0ONLY)
15-#define JITC_ERR_PREFIX (6 | JITC_ERR_PHASE0ONLY)
16-#define JITC_ERR_LABELNODEF 7
17-#define JITC_ERR_LABELTYP 8
18-#define JITC_ERR_IDIOM 9
19-#define JITC_ERR_PREGNUM (10 | JITC_ERR_PHASE0ONLY)
20-#define JITC_ERR_SRC1 (11 | JITC_ERR_PHASE0ONLY)
21-#define JITC_ERR_BADTYPE (12 | JITC_ERR_PHASE0ONLY)
22-#define JITC_ERR_PREFIXFAR (13 | JITC_ERR_PHASE0ONLY)
23-#define JITC_ERR_INTERNAL 99
24-
25-// Byte operations
26-#define jitCompPutByte1(p, c0) *p++ = c0
27-#define jitCompPutByte2(p, c0, c1) *p++ = c0; *p++ = c1
28-#define jitCompPutByte3(p, c0, c1, c2) *p++ = c0; *p++ = c1; *p++ = c2
29-#define jitCompPutByte4(p, c0, c1, c2, c3) *p++ = c0; *p++ = c1; *p++ = c2; *p++ = c3
30-
31-//
32-// functions (jitc internal)
33-//
34-
35-// @jitc.c
36-void errorHandler(HOSECPU_RuntimeEnvironment *r);
37-int jitCompCmdLen(const unsigned char *src);
38-
39-// @jitcx86.c
40-#if (JITC_ARCNUM == 0x0001)
41-//
42-// for x86-32bit
43-//
44-#define IA32_REG0_EAX 0
45-#define IA32_REG1_ECX 1
46-#define IA32_REG2_EDX 2
47-#define IA32_REG3_EBX 3
48-#define IA32_REG4_ESP 4
49-#define IA32_REG5_EBP 5
50-#define IA32_REG6_ESI 6
51-#define IA32_REG7_EDI 7
52-//
53-#define IA32_MOD_R_M(mod, reg, rm) ((mod << 6) | reg << 3 | rm)
54-//
55-#define IA32_OP_MOD_INDEXONLY 0
56-#define IA32_OP_MOD_INDEX_AND_DISP_BYTE 1
57-#define IA32_OP_MOD_INDEX_AND_DISP_FULL 2
58-#define IA32_OP_MOD_REGISTER 3
59-//
60-#define IA32_OP_RM32_MOD00_ADDR_DISP32 5
61-//
62-#define envOffset_DBGINFO0 (2304 + 0)
63-#define envOffset_DBGINFO1 (2304 + 4)
64-#define envOffset_DBGCURRENTCODE (2304 + 8)
65-#define envOffset_PTRCTRL (2320)
66-#define PRegOffset(regid) (256 + 32 * regid)
67-//
68-#define jitCompPutImm32(p, i) jitCompPutByte4(p, ((i) & 0xff), (((i) >> 8) & 0xff), (((i) >> 16) & 0xff), (((i) >> 24) & 0xff))
69-//
70-#define jitCompPutOp_ADD_GReg_Imm8(p, dReg, i) jitCompPutByte3(p, 0x83, 0xc0 | dReg, i); /* ADD(reg0, imm8); == [1000 0011] [11000 reg] imm8 */
71-#define jitCompPutOp_XOR_GReg_GReg(p, d, s) jitCompPutByte2(w.dst, 0x31, 0xc0 | (s) << 3 | (d));
72-#define jitCompPutOp_MOV_GReg_Imm32(p, dReg, i) jitCompPutByte1(p, 0xb8 | dReg); jitCompPutImm32(p, i); /* MOV(reg0, imm32); == [1011 1 reg] imm32 */
73-#define jitCompPutOp_PUSHAD(p) jitCompPutByte1(p, 0x60);
74-#define jitCompPutOp_POPAD(p) jitCompPutByte1(p, 0x61);
75-#define jitCompPutOp_PUSH_GReg(p, reg) jitCompPutByte1(p, 0x50 | (reg));
76-#define jitCompPutOp_PUSH_Imm8(p, i) jitCompPutByte2(p, 0x6a, i);
77-#define jitCompPutOp_POP_GReg(p, reg) jitCompPutByte1(p, 0x58 | (reg));
78-#define jitCompPutOp_CALL_Relative(p, diff) jitCompPutByte1(p, 0xe8); jitCompPutImm32(p, diff); /*次の命令との相対オフセットだけ相対コールする*/
79-#define jitCompPutOp_JMPnear(p, diff) jitCompPutByte1(p, 0xe9); jitCompPutImm32(p, diff); /*次の命令との相対オフセットだけ相対ジャンプする*/
80-#define jitCompPutOp_JMPshort(p, diff) jitCompPutByte2(p, 0xeb, diff & 0xff);/*次の命令との相対オフセットだけ相対ジャンプする*/
81-#define jitCompPutOp_INT3(p) jitCompPutByte1(p, 0xCC);
82-//
83-#define jitCompPutOp_MOV_EAX_ZERO(p) jitCompPutOp_XOR_GReg_GReg(p, IA32_REG0_EAX, IA32_REG0_EAX);
84-//
85-#define DEBUGCode(work, code) jitCompPutOp_MOV_GReg_Imm32((work)->dst, IA32_REG0_EAX, code); jitCompPutOp_MOV_EBPDisp_GReg(work, envOffset_DBGCURRENTCODE, IA32_REG0_EAX);
86-// Optimization settings
87-// 他のCPUへ移植する人へ:
88-// 以下の定数は最適化のためのものなので、すべて0として簡単に移植しても問題ありません
89-#define jitCompA0001_USE_R3F_CMPJMP 1*1
90-#define jitCompA0001_USE_R3F_IMM32 1*1
91-#define jitCompA0001_USE_R3F_IMM8 1*1
92-#define jitCompA0001_USE_R3F_INCDEC 1*1
93-#define jitCompA0001_OPTIMIZE_JMP 1*1
94-#define jitCompA0001_OPTIMIZE_MOV 1*1 /* 1にすると速度低下する? */
95-#define jitCompA0001_OPTIMIZE_CMP 1*1
96-#define jitCompA0001_OPTIMIZE_ALIGN 8 /* 0-8を想定 */
97-#define jitCompA0001_EBP128 128 // 0にもできる
98-
99-struct JitCompWork {
100- unsigned char *dst, *dst0;
101- int err, maxLabels;
102-#if (jitCompA0001_USE_R3F_IMM32 != 0)
103- int r3f;
104-#endif
105- char prefix; //CND命令の値を記録(初期値=0)
106-};
107-
108-// @jitcx86a.c
109-int jitCompGetImm32(const unsigned char *src);
110-int jitCompGetLabelNum(struct JitCompWork *w, const unsigned char *src);
111-void jitCompPutModRM_Disp_BaseEBP(struct JitCompWork *w, int disp, int opReg);
112-void jitCompPutOp_MOV_EBPDisp_GReg(struct JitCompWork *w, int disp, int reg32);
113-void jitCompPutOp_MOV_GReg_EBPDisp(struct JitCompWork *w, int reg32, int disp);
114-void jitCompA0001_movEaxRxx(struct JitCompWork *w, int rxx);
115-void jitCompA0001_movRxxEax(struct JitCompWork *w, int rxx);
116-void jitCompA0001_fixPrefix(struct JitCompWork *w);
117-void jitCompA0001_checkCompPtr(struct JitCompWork *w, int p0, int p1);
118-void jitCompA000_loadRegCacheAll(struct JitCompWork *w);
119-void jitCompA000_storeRegCacheAll(struct JitCompWork *w);
120-void jitCompA000_loadRegCacheEcx(struct JitCompWork *w);
121-void jitCompA000_storeRegCacheEcx(struct JitCompWork *w);
122-void jitCompA000_loadRegCacheEdx(struct JitCompWork *w);
123-void jitCompA000_storeRegCacheEdx(struct JitCompWork *w);
124-int jitCompA000_selectRegCache(int rxx, int reg);
125-void jitCompA000_loadPRegCacheAll(struct JitCompWork *w);
126-void jitCompA000_storePRegCacheAll(struct JitCompWork *w);
127-int jitCompA000_selectPRegCache(int pxx, int reg);
128-int jitCompA000_convTyp(int t);
129-int jitCompA000_dataWidth(int t);
130-void jitCompA0001_checkType0(struct JitCompWork *w, int pxx, int typ, int ac);
131-void jitCompA0001_checkType(struct JitCompWork *w, int pxx, int typ, int ac);
132-void jitCompA0001_checkLimit(struct JitCompWork *w, int reg, int pxx);
133-
134-// @jitcx86.c
135-extern unsigned char *errfnc;
136-int jitCompiler(unsigned char *dst, unsigned char *dst1, const unsigned char *src, const unsigned char *src1, const unsigned char *src0, HOSECPU_LabelListTag *label, int maxLabels, int level, int debugInfo1, int flags);
137-unsigned char *jitCompCallFunc(unsigned char *dst, void *func);
138-unsigned char *jitCompInit(unsigned char *dst);
139-void func3c(char *ebp, int opt, int r1, int p1, int lenR, int lenP, int r0, int p0);
140-void func3d(char *ebp, int opt, int r1, int p1, int lenR, int lenP, int r0, int p0);
141-void funcf4(char *ebp, int pxx, int typ, int len);
142-void funcf5(char *ebp, int pxx, int typ, int len);
143-void funcf6(char *ebp, int pxx, int typ, int len);
144-void funcf7(char *ebp, int pxx, int typ, int len);
145-void errHndl(HOSECPU_RuntimeEnvironment *r);
146-int jitc0(unsigned char **qq, unsigned char *q1, const unsigned char *p0, const unsigned char *p1, int level, HOSECPU_LabelListTag *label);
147-#if (USE_DEBUGGER != 0)
148-int dbgrGetRegNum(const char *p);
149-void dbgrMain(HOSECPU_RuntimeEnvironment *r);
150-#endif
151-
152-
153-
154-
155-#endif
156-
157-#endif
1+
2+#ifndef HeavyOSECPU_jitc_h
3+#define HeavyOSECPU_jitc_h
4+
5+
6+
7+// Error flags
8+#define JITC_ERR_MASK 255
9+#define JITC_ERR_PHASE0ONLY 256
10+#define JITC_ERR_REGNUM (1 | JITC_ERR_PHASE0ONLY)
11+#define JITC_ERR_DST1 (2 | JITC_ERR_PHASE0ONLY)
12+#define JITC_ERR_OPECODE (3 | JITC_ERR_PHASE0ONLY)
13+#define JITC_ERR_LABELNUM (4 | JITC_ERR_PHASE0ONLY)
14+#define JITC_ERR_LABELREDEF (5 | JITC_ERR_PHASE0ONLY)
15+#define JITC_ERR_PREFIX (6 | JITC_ERR_PHASE0ONLY)
16+#define JITC_ERR_LABELNODEF 7
17+#define JITC_ERR_LABELTYP 8
18+#define JITC_ERR_IDIOM 9
19+#define JITC_ERR_PREGNUM (10 | JITC_ERR_PHASE0ONLY)
20+#define JITC_ERR_SRC1 (11 | JITC_ERR_PHASE0ONLY)
21+#define JITC_ERR_BADTYPE (12 | JITC_ERR_PHASE0ONLY)
22+#define JITC_ERR_PREFIXFAR (13 | JITC_ERR_PHASE0ONLY)
23+#define JITC_ERR_INTERNAL 99
24+
25+// Byte operations
26+#define jitCompPutByte1(p, c0) *p++ = c0
27+#define jitCompPutByte2(p, c0, c1) *p++ = c0; *p++ = c1
28+#define jitCompPutByte3(p, c0, c1, c2) *p++ = c0; *p++ = c1; *p++ = c2
29+#define jitCompPutByte4(p, c0, c1, c2, c3) *p++ = c0; *p++ = c1; *p++ = c2; *p++ = c3
30+
31+//
32+// functions (jitc internal)
33+//
34+
35+// @jitc.c
36+void errorHandler(HOSECPU_RuntimeEnvironment *r);
37+int jitCompCmdLen(const unsigned char *src);
38+
39+// @jitcx86.c
40+#if (JITC_ARCNUM == 0x0001)
41+//
42+// for x86-32bit
43+//
44+#define IA32_REG0_EAX 0
45+#define IA32_REG1_ECX 1
46+#define IA32_REG2_EDX 2
47+#define IA32_REG3_EBX 3
48+#define IA32_REG4_ESP 4
49+#define IA32_REG5_EBP 5
50+#define IA32_REG6_ESI 6
51+#define IA32_REG7_EDI 7
52+//
53+#define IA32_MOD_R_M(mod, reg, rm) ((mod << 6) | reg << 3 | rm)
54+//
55+#define IA32_OP_MOD_INDEXONLY 0
56+#define IA32_OP_MOD_INDEX_AND_DISP_BYTE 1
57+#define IA32_OP_MOD_INDEX_AND_DISP_FULL 2
58+#define IA32_OP_MOD_REGISTER 3
59+//
60+#define IA32_OP_RM32_MOD00_ADDR_DISP32 5
61+//
62+#define envOffset_DBGINFO0 (2304 + 0)
63+#define envOffset_DBGINFO1 (2304 + 4)
64+#define envOffset_DBGCURRENTCODE (2304 + 8)
65+#define envOffset_DBGCURRENTCODEADDR (2304 + 12)
66+#define envOffset_PTRCTRL (2320)
67+#define PRegOffset(regid) (256 + 32 * regid)
68+//
69+#define jitCompPutImm32(p, i) jitCompPutByte4(p, ((i) & 0xff), (((i) >> 8) & 0xff), (((i) >> 16) & 0xff), (((i) >> 24) & 0xff))
70+//
71+#define jitCompPutOp_ADD_GReg_Imm8(p, dReg, i) jitCompPutByte3(p, 0x83, 0xc0 | dReg, i); /* ADD(reg0, imm8); == [1000 0011] [11000 reg] imm8 */
72+#define jitCompPutOp_XOR_GReg_GReg(p, d, s) jitCompPutByte2(w.dst, 0x31, 0xc0 | (s) << 3 | (d));
73+#define jitCompPutOp_MOV_GReg_Imm32(p, dReg, i) jitCompPutByte1(p, 0xb8 | dReg); jitCompPutImm32(p, i); /* MOV(reg0, imm32); == [1011 1 reg] imm32 */
74+#define jitCompPutOp_PUSHAD(p) jitCompPutByte1(p, 0x60);
75+#define jitCompPutOp_POPAD(p) jitCompPutByte1(p, 0x61);
76+#define jitCompPutOp_PUSH_GReg(p, reg) jitCompPutByte1(p, 0x50 | (reg));
77+#define jitCompPutOp_PUSH_Imm8(p, i) jitCompPutByte2(p, 0x6a, i);
78+#define jitCompPutOp_POP_GReg(p, reg) jitCompPutByte1(p, 0x58 | (reg));
79+#define jitCompPutOp_CALL_Relative(p, diff) jitCompPutByte1(p, 0xe8); jitCompPutImm32(p, diff); /*次の命令との相対オフセットだけ相対コールする*/
80+#define jitCompPutOp_JMPnear(p, diff) jitCompPutByte1(p, 0xe9); jitCompPutImm32(p, diff); /*次の命令との相対オフセットだけ相対ジャンプする*/
81+#define jitCompPutOp_JMPshort(p, diff) jitCompPutByte2(p, 0xeb, diff & 0xff);/*次の命令との相対オフセットだけ相対ジャンプする*/
82+#define jitCompPutOp_INT3(p) jitCompPutByte1(p, 0xCC);
83+//
84+#define jitCompPutOp_MOV_EAX_ZERO(p) jitCompPutOp_XOR_GReg_GReg(p, IA32_REG0_EAX, IA32_REG0_EAX);
85+//
86+#define DEBUGCode(work, code) jitCompPutOp_MOV_GReg_Imm32((work)->dst, IA32_REG0_EAX, code); jitCompPutOp_MOV_EBPDisp_GReg(work, envOffset_DBGCURRENTCODE, IA32_REG0_EAX);
87+#define DEBUGCodeAddress(work, addr) jitCompPutOp_MOV_GReg_Imm32((work)->dst, IA32_REG0_EAX, addr); jitCompPutOp_MOV_EBPDisp_GReg(work, envOffset_DBGCURRENTCODEADDR, IA32_REG0_EAX);
88+// Optimization settings
89+// 他のCPUへ移植する人へ:
90+// 以下の定数は最適化のためのものなので、すべて0として簡単に移植しても問題ありません
91+#define jitCompA0001_USE_R3F_CMPJMP 1*1
92+#define jitCompA0001_USE_R3F_IMM32 1*1
93+#define jitCompA0001_USE_R3F_IMM8 1*1
94+#define jitCompA0001_USE_R3F_INCDEC 1*1
95+#define jitCompA0001_OPTIMIZE_JMP 1*1
96+#define jitCompA0001_OPTIMIZE_MOV 1*1 /* 1にすると速度低下する? */
97+#define jitCompA0001_OPTIMIZE_CMP 1*1
98+#define jitCompA0001_OPTIMIZE_ALIGN 8 /* 0-8を想定 */
99+#define jitCompA0001_EBP128 128 // 0にもできる
100+
101+struct JitCompWork {
102+ unsigned char *dst, *dst0;
103+ int err, maxLabels;
104+#if (jitCompA0001_USE_R3F_IMM32 != 0)
105+ int r3f;
106+#endif
107+ char prefix; //CND命令の値を記録(初期値=0)
108+};
109+
110+// @jitcx86a.c
111+int jitCompGetImm32(const unsigned char *src);
112+int jitCompGetLabelNum(struct JitCompWork *w, const unsigned char *src);
113+void jitCompPutModRM_Disp_BaseEBP(struct JitCompWork *w, int disp, int opReg);
114+void jitCompPutOp_MOV_EBPDisp_GReg(struct JitCompWork *w, int disp, int reg32);
115+void jitCompPutOp_MOV_GReg_EBPDisp(struct JitCompWork *w, int reg32, int disp);
116+void jitCompA0001_movEaxRxx(struct JitCompWork *w, int rxx);
117+void jitCompA0001_movRxxEax(struct JitCompWork *w, int rxx);
118+void jitCompA0001_fixPrefix(struct JitCompWork *w);
119+void jitCompA0001_checkCompPtr(struct JitCompWork *w, int p0, int p1);
120+void jitCompA000_loadRegCacheAll(struct JitCompWork *w);
121+void jitCompA000_storeRegCacheAll(struct JitCompWork *w);
122+void jitCompA000_loadRegCacheEcx(struct JitCompWork *w);
123+void jitCompA000_storeRegCacheEcx(struct JitCompWork *w);
124+void jitCompA000_loadRegCacheEdx(struct JitCompWork *w);
125+void jitCompA000_storeRegCacheEdx(struct JitCompWork *w);
126+int jitCompA000_selectRegCache(int rxx, int reg);
127+void jitCompA000_loadPRegCacheAll(struct JitCompWork *w);
128+void jitCompA000_storePRegCacheAll(struct JitCompWork *w);
129+int jitCompA000_selectPRegCache(int pxx, int reg);
130+int jitCompA000_convTyp(int t);
131+int jitCompA000_dataWidth(int t);
132+void jitCompA0001_checkType0(struct JitCompWork *w, int pxx, int typ, int ac);
133+void jitCompA0001_checkType(struct JitCompWork *w, int pxx, int typ, int ac);
134+void jitCompA0001_checkLimit(struct JitCompWork *w, int reg, int pxx);
135+
136+// @jitcx86.c
137+extern unsigned char *errfnc;
138+int jitCompiler(unsigned char *dst, unsigned char *dst1, const unsigned char *src, const unsigned char *src1, const unsigned char *src0, HOSECPU_LabelListTag *label, int maxLabels, int level, int debugInfo1, int flags);
139+unsigned char *jitCompCallFunc(unsigned char *dst, void *func);
140+unsigned char *jitCompInit(unsigned char *dst);
141+void func3c(char *ebp, int opt, int r1, int p1, int lenR, int lenP, int r0, int p0);
142+void func3d(char *ebp, int opt, int r1, int p1, int lenR, int lenP, int r0, int p0);
143+void funcf4(char *ebp, int pxx, int typ, int len);
144+void funcf5(char *ebp, int pxx, int typ, int len);
145+void funcf6(char *ebp, int pxx, int typ, int len);
146+void funcf7(char *ebp, int pxx, int typ, int len);
147+void errHndl(HOSECPU_RuntimeEnvironment *r);
148+int jitc0(unsigned char **qq, unsigned char *q1, const unsigned char *p0, const unsigned char *p1, int level, HOSECPU_LabelListTag *label);
149+#if (USE_DEBUGGER != 0)
150+int dbgrGetRegNum(const char *p);
151+void dbgrMain(HOSECPU_RuntimeEnvironment *r);
152+#endif
153+
154+
155+
156+
157+#endif
158+
159+#endif
--- a/jitcx86.c
+++ b/jitcx86.c
@@ -56,10 +56,18 @@ int jitCompiler(unsigned char *dst, unsigned char *dst1, const unsigned char *sr
5656 }
5757 #if ENABLE_DEBUG_CODE != 0
5858 if(*src != 0x00 && *src != 0x01 && *src != 0x34){
59-
6059 DEBUGCode(&w, *src);
6160 }
6261 #endif
62+#if ENABLE_DEBUG_CODE != 0
63+ if(*src != 0x00 && *src != 0x01 && *src != 0x34){
64+ DEBUGCodeAddress(&w, (unsigned int)(src - src0));
65+ }
66+#endif
67+ if((unsigned int)(src - src0) == 0xEB){
68+ // 特定のバックエンドコードアドレスにブレークポイントを仕込みたい場合はこうする
69+ jitCompPutOp_INT3(w.dst);
70+ }
6371 prefix_continue:
6472 // CND命令コンパイル後ここに戻る
6573 switch (*src) {
@@ -1471,6 +1479,7 @@ unsigned char *jitCompCallFunc(unsigned char *dst, void *func)
14711479
14721480 unsigned char *jitCompInit(unsigned char *dst)
14731481 {
1482+ // エラーハンドル関数呼び出しコードを先頭に追加し、それをerrfncにする。
14741483 errfnc = dst;
14751484 return jitCompCallFunc(dst, &errHndl);
14761485 }
@@ -1657,8 +1666,8 @@ int jitc0(unsigned char **qq, unsigned char *q1, const unsigned char *p0, const
16571666 label[i].opt = 0;
16581667 }
16591668
1660- // 以下のjitCompile()呼び出しでは第二引数をq1-2にした方がよいのではないか?
1661- i = jitCompiler(q, q1, p0 + 2, p1, p0, label, JITC_MAXLABELS, level, di1_serial, 0); // ラベルのチェック
1669+ // ラベルのチェック
1670+ i = jitCompiler(q, q1, p0 + 2, p1, p0, label, JITC_MAXLABELS, level, di1_serial, 0);
16621671 if (i != 0){
16631672 return 2;
16641673 }
--- a/main.c
+++ b/main.c
@@ -8,7 +8,7 @@ HOSECPU_RuntimeEnvironment *dbg_env;
88
99
1010
11-unsigned char *Init_LoadSysLib(char argv0[], unsigned char *tmpWorkMemory);
11+unsigned char *Init_LoadSysLib(char argv0[]);
1212 void LoadAppBin(HOSECPU_RuntimeEnvironment *env);
1313
1414 void putKeybuf(int i)
@@ -25,84 +25,106 @@ void putKeybuf(int i)
2525 int HeavyOSECPUMain(int argc, char **argv)
2626 {
2727 HOSECPU_RuntimeEnvironment env;
28- FILE *fp;
29- unsigned char *jitbuf, *sysjit00, *sysjit;
30- unsigned char *systmp0, *systmp1, *systmp2;
31- unsigned char *opTbl;
28+ unsigned char *pAppJITBin;
29+ unsigned char *appBinDevFuncBase;
30+ unsigned char *pSyslibJITBin;
31+ unsigned char *syslibJITBinBase;
3232 HOSECPU_LabelListTag *label;
33- int tmpsiz, i;
34- double tm0, tm1, tm2;
3533 HOSECPU_PointerControlTag *ptrCtrl;
36- unsigned char *syslib;
37- int argDebug = 0, stacksiz = 1;
38- const char *cp;
39- void(*jitfunc)(char *);
40- unsigned char *jp;
34+ // For decoder work
35+ unsigned char *bufP06; // P06 UINT8 >=2M
36+ unsigned int *bufP0A; // P0A UINT32 16K
37+ int *pxxFlag; // P0B SINT32 64
38+ int *typLabel; // P0C SINT32 4K
39+ unsigned char *opTbl; // P0D UINT8 256
40+ // For tek decomp
41+ unsigned char *tekDecompTemp;
42+ // time counter
43+ double tm0, tm1, tm2;
44+ // options from arguments
45+ int argDebugMode = 0;
46+ int argStackSize = 1;
47+ //
48+ int i;
49+ const char *s;
50+ unsigned char *p;
51+ FILE *fp;
52+
53+ //
54+ // Initialize
55+ //
4156
4257 // For debug
4358 dbg_env = &env;
44-
4559 // Initialize mainWindow
4660 mainWindow.vram = NULL;
4761 mainWindow.xsize = 0;
4862 mainWindow.ysize = 0;
49- di1_serial = 0;
50-
51- // 実行環境初期化
63+ // Initialize keyInputting
64+ keybuf = malloc(KEYBUFSIZ * sizeof(int));
65+ keybuf_r = keybuf_w = keybuf_c = 0;
66+ // Initialize HOSECPU_RuntimeEnvironment
5267 env.mainArgc = argc;
5368 env.mainArgv = (const char **)argv;
54- env.appBin = malloc(APPSIZ1);
69+ //
70+ env.appOSECPUBin = malloc(APPBINSIZE);
71+ env.appOSECPUBackendBin = malloc(APPBINSIZE);
72+ //
73+ env.appJITBin = mallocRWE(APPJITSIZE);
74+ fprintf(stderr, "appJITBin: %p\n", env.appJITBin);
75+ pAppJITBin = env.appJITBin;
76+ fprintf(stderr, "pAppJITBin: %p\n", pAppJITBin);
77+ env.syslibJITBin = mallocRWE(SYSLIBJITSIZE);
78+ pSyslibJITBin = env.syslibJITBin;
79+ // syslibJITBin image:
80+ // env.syslibJITBin + - - - -
81+ // | errHndl
82+ // syslibJITBinBase + - - - -
83+ // | syslib
84+ //
5585 env.executionLevel = JITC_LV_SLOWEST;
56- jitbuf = mallocRWE(APPJITSIZE); /* とりあえず1MBで */
57- // syslib.oseのjitc結果を格納する領域を確保。
58- sysjit00 = mallocRWE(SYSJITSIZ1);
59- sysjit = sysjit00;
60- // 現在の、jitc結果を格納するメモリへの書き込み位置のアドレス
61- // sysjit: 現在のjitc書き込み位置
62- // sysjit00: jitc結果の先頭
63- //ワークメモリを三つくらいもらう
64- systmp0 = malloc(SYSTMP0SIZ); /* syslibのjitc用 */
65- systmp1 = malloc(SYSTMP1SIZ);
66- systmp2 = malloc(1024 * 1024);
67-
86+ // For decoder work
87+ bufP06 = malloc(DECODER_BUF06_SIZE);
88+ bufP0A = malloc(DECODER_BUF0A_SIZE);
89+ pxxFlag = malloc(64 * 4);
90+ typLabel = malloc(4096 * 4);
6891 opTbl = malloc(256);
92+ //
6993 label = malloc(JITC_MAXLABELS * sizeof (HOSECPU_LabelListTag));
70- keybuf = malloc(KEYBUFSIZ * sizeof (int));
71- keybuf_r = keybuf_w = keybuf_c = 0;
7294 ptrCtrl = malloc(PTRCTRLSIZ * sizeof (HOSECPU_PointerControlTag));
73-
74- randStatInit((unsigned int)time(NULL));
7595 for (i = 0; i < PTRCTRLSIZ; i++) {
7696 ptrCtrl[i].liveSign = 0;
7797 ptrCtrl[i].size = -1;
7898 }
7999 ptrCtrl[0].size = -2;
100+ //
101+ randStatInit((unsigned int)time(NULL));
80102
81- /* syslibの読み込み */
82- syslib = Init_LoadSysLib(argv[0], systmp0);
103+ //
104+ // Load syslib
105+ //
106+
107+ env.syslibOSECPUBin = Init_LoadSysLib(argv[0]);
83108
84- sysjit = jitCompInit(sysjit);
85- sysjit00 = sysjit;
109+ pSyslibJITBin = jitCompInit(pSyslibJITBin);
110+ syslibJITBinBase = pSyslibJITBin;
86111 // labelはjitc0()内で初期化される。
87- i = jitc0(&sysjit, sysjit00 + SYSJITSIZ1, syslib + 32, syslib + SYSLIBSIZ1, JITC_LV_SLOWEST+9, label);
112+ i = jitc0(&pSyslibJITBin, env.syslibJITBin + SYSLIBJITSIZE, env.syslibOSECPUBin + 32, env.syslibOSECPUBin + SYSLIBBINSIZE, JITC_LV_SLOWEST + 9, label);
88113 if (i != 0){
89114 fputs("syslib-file JITC error.\n", stderr);
90115 return 1;
91116 }
92117
93- // エラー時にデバッグ用に表示する変数を加算
94- di1_serial++;
95-
96- /* アプリバイナリの読み込み */
97118 LoadAppBin(&env);
98119
99- /* クロック初期化 */
100120 tm0 = clock() / (double)CLOCKS_PER_SEC;
101121
102- if (env.appBin[2] == 0xf0) {
122+ if (env.appOSECPUBin[2] == 0xf0) {
103123 // tek5圧縮がかかっている
104124 #if (USE_TEK5 != 0)
105- env.appSize1 = tek5Decomp(env.appBin + 2, env.appBin + env.appSize0, systmp0);
125+ tekDecompTemp = malloc(TEKDECOMPTMPSIZE);
126+ env.appSize1 = tek5Decomp(env.appOSECPUBin + 2, env.appOSECPUBin + env.appSize0, tekDecompTemp);
127+ free(tekDecompTemp);
106128 env.appSize1 += 2;
107129 #else
108130 env.appSize1 = -9;
@@ -113,61 +135,58 @@ int HeavyOSECPUMain(int argc, char **argv)
113135 }
114136 }
115137 //デバッグモード指定
116- cp = searchArg(argc, (const char **)argv, "debug:", 0);
117- if (cp != NULL){
118- argDebug = *cp - '0';
138+ s = searchArg(argc, (const char **)argv, "debug:", 0);
139+ if (s != NULL){
140+ argDebugMode = *s - '0';
119141 }
120142 //スタックサイズ指定(MiB単位)
121- cp = searchArg(argc, (const char **)argv, "stack:", 0);
122- if (cp != NULL){
123- stacksiz = strtol(cp, NULL, 0);
143+ s = searchArg(argc, (const char **)argv, "stack:", 0);
144+ if (s != NULL){
145+ argStackSize = strtol(s, NULL, 0);
124146 }
125-
126- // jitbufは先頭。 jpは現在位置
127- jp = jitbuf; /* JIT-pointer */
128147
129148 /* フロントエンドコードをバックエンドコードに変換する */
130- if ((env.appBin[2] & 0xf0) != 0) { // 3バイト目が00なら処理しない
131- systmp0[0] = env.appBin[0];
132- systmp0[1] = env.appBin[1];
133- env.preg[2].p = systmp0 + 2;
134- env.preg[3].p = systmp0 + SYSTMP0SIZ;
135- env.preg[4].p = env.appBin + 2;
136- env.preg[5].p = env.appBin + env.appSize1;
137- env.preg[6].p = systmp1;
138- env.preg[7].p = systmp1 + SYSTMP1SIZ;
139- env.preg[10].p = systmp2;
140- int pxxFlag[64], typLabel[4096];
141- env.preg[0x0b].p = (void *)pxxFlag;
142- env.preg[0x0c].p = (void *)typLabel;
143- env.preg[0x0d].p = opTbl;
144- jitfunc = (void *)sysjit00;
145- jitcRunBinary(jitfunc, &env);
149+ if ((env.appOSECPUBin[2] & 0xf0) != 0) { // 3バイト目が00なら処理しない
150+ env.appOSECPUBackendBin[0] = env.appOSECPUBin[0];
151+ env.appOSECPUBackendBin[1] = env.appOSECPUBin[1];
152+ env.preg[2].p = &env.appOSECPUBackendBin[2];
153+ env.preg[3].p = &env.appOSECPUBackendBin[APPBINSIZE];
154+ env.preg[4].p = &env.appOSECPUBin[2];
155+ env.preg[5].p = &env.appOSECPUBin[env.appSize1];
156+ env.preg[6].p = bufP06;
157+ env.preg[7].p = bufP06 + DECODER_BUF06_SIZE;
158+ env.preg[0x0A].p = (unsigned char *)bufP0A;
159+ env.preg[0x0B].p = (void *)pxxFlag;
160+ env.preg[0x0C].p = (void *)typLabel;
161+ env.preg[0x0D].p = opTbl;
162+ jitcRunBinary((void *)syslibJITBinBase, &env);
146163 if (env.ireg[0] != 0) {
147- jp = env.preg[2].p - 1;
148- fprintf(stderr, "unpack error: %02X (at %06X) (R00=%d)\n", *jp, jp - systmp0, env.ireg[0]);
149- if ((argDebug & 2) != 0) {
164+ p = env.preg[2].p - 1;
165+ fprintf(stderr, "unpack error: %02X (at %06X) (R00=%d)\n", *p, p - env.appOSECPUBackendBin, env.ireg[0]);
166+ if ((argDebugMode & 2) != 0) {
150167 fp = fopen("debug2.bin", "wb");
151- fwrite(systmp0, 1, jp - systmp0 + 16, fp);
168+ fwrite(env.appOSECPUBackendBin, 1, p - env.appOSECPUBackendBin + 16, fp);
152169 fclose(fp);
153170 }
154171 exit(1);
155172 }
156- tmpsiz = env.preg[2].p - systmp0;
173+ env.appOSECPUBackendBinSize = env.preg[2].p - env.appOSECPUBackendBin;
157174 } else{
158- memcpy(systmp0, env.appBin, env.appSize1);
159- tmpsiz = env.appSize1;
175+ memcpy(env.appOSECPUBackendBin, env.appOSECPUBin, env.appSize1);
176+ env.appOSECPUBackendBinSize = env.appSize1;
160177 }
161178
162- if ((argDebug & 2) != 0) {
179+ if ((argDebugMode & 2) != 0) {
163180 /*変換後のバックエンドコードをファイルへ保存*/
164181 fp = fopen("debug2.bin", "wb");
165- fwrite(systmp0, 1, tmpsiz, fp);
182+ fwrite(env.appOSECPUBackendBin, 1, env.appOSECPUBackendBinSize, fp);
166183 fclose(fp);
167184 }
168185
169186 //JITコンパイル
170- i = jitc0(&jp, jitbuf + 1024 * 1024, systmp0, systmp0 + tmpsiz, env.executionLevel, label);
187+ fprintf(stderr, "pAppJITBin: %p\n", pAppJITBin);
188+ i = jitc0(&pAppJITBin, env.appJITBin + APPJITSIZE, env.appOSECPUBackendBin, env.appOSECPUBackendBin + APPBINSIZE, env.executionLevel, label);
189+ fprintf(stderr, "pAppJITBin: %p\n", pAppJITBin);
171190 if (i == 1){
172191 fputs("app-file header error.\n", stderr);
173192 return 1;
@@ -177,10 +196,11 @@ int HeavyOSECPUMain(int argc, char **argv)
177196 }
178197 di1_serial++;
179198
180- int appsiz2 = jp - jitbuf;
199+ env.appJITBinSize = pAppJITBin - env.appOSECPUBackendBin;
181200
182- unsigned char *p28 = jp;
183- jp = jitCompCallFunc(jp, &devFunc);
201+ // 末尾にAPI関数呼び出しコードを追加
202+ appBinDevFuncBase = pAppJITBin;
203+ pAppJITBin = jitCompCallFunc(pAppJITBin, &devFunc);
184204
185205 tm1 = clock() / (double)CLOCKS_PER_SEC;
186206
@@ -196,24 +216,24 @@ int HeavyOSECPUMain(int argc, char **argv)
196216 }
197217
198218 env.buf0 = env.buf1 = NULL;
199-
219+
200220 // p28にapiをコールするアドレスを設定
201- env.preg[0x28].p = p28; // p28には、devFuncをコールするコードが書かれている
221+ env.preg[0x28].p = appBinDevFuncBase; // appBinDevFuncBaseには、devFuncをコールするコードが書かれている
202222 env.preg[0x28].typ = 0; // TYP_CODE
203- env.preg[0x28].p0 = p28; // アドレス演算できる範囲を制限
204- env.preg[0x28].p1 = p28 + 1; // アドレス演算できる範囲を制限
205-
223+ env.preg[0x28].p0 = appBinDevFuncBase; // アドレス演算できる範囲を制限
224+ env.preg[0x28].p1 = appBinDevFuncBase + 1; // アドレス演算できる範囲を制限
225+
206226 //env.preg[0x00].p = malloc(1024 * 1024) + (1024 * 1024 - 32);
207- env.junkStack = malloc(stacksiz << 20);
208- env.junkStack1 = env.junkStack + (stacksiz << 20);
227+ env.junkStack = malloc(argStackSize << 20);
228+ env.junkStack1 = env.junkStack + (argStackSize << 20);
209229 env.winClosed = 0;
210230 env.autoSleep = 0;
211231 env.lastConsoleChar = '\n';
212232
213233 env.label = label;
214234 env.maxLabels = JITC_MAXLABELS;
215- env.jitbuf = jp;
216- env.jitbuf1 = jitbuf + 1024 * 1024;
235+ env.jitbuf = pAppJITBin;
236+ env.jitbuf1 = env.appJITBin + APPJITSIZE;
217237 env.errHndl = &errorHandler;
218238 env.appReturnCode = 0;
219239
@@ -222,16 +242,15 @@ int HeavyOSECPUMain(int argc, char **argv)
222242 env.dbgr = 1;
223243 }
224244
225- if ((argDebug & 1) != 0) {
245+ if ((argDebugMode & 1) != 0) {
226246 fp = fopen("debug1.bin", "wb");
227- fwrite(jitbuf, 1, jp - jitbuf, fp);
247+ fwrite(env.appJITBin, 1, pAppJITBin - env.appJITBin, fp);
228248 fclose(fp);
229249 }
230250
231251 /* JITコード実行 */
232- jitfunc = (void *)jitbuf;
233252 if (setjmp(env.setjmpEnv) == 0){
234- jitcRunBinary(jitfunc, &env);
253+ jitcRunBinary((void *)env.appJITBin, &env);
235254 }
236255 if (env.autoSleep != 0) {
237256 if (mainWindow.vram != NULL){
@@ -250,7 +269,7 @@ int HeavyOSECPUMain(int argc, char **argv)
250269 /* 実行結果確認のためのレジスタダンプ */
251270 if (searchArg(argc, (const char **)argv, "verbose:1", 0) != NULL) {
252271 printf("time: JITC=%.3f[sec], exec=%.3f[sec]\n", tm1 - tm0, tm2 - tm1);
253- printf("size: OSECPU=%d, decomp=%d, tmp=%d, native=%d\n", env.appSize0, env.appSize1, tmpsiz, appsiz2);
272+ printf("size: OSECPU=%d, decomp=%d, tmp=%d, native=%d\n", env.appSize0, env.appSize1, env.appOSECPUBackendBinSize, env.appJITBinSize);
254273 printf("result:\n");
255274 printf("R00:0x%08X R01:0x%08X R02:0x%08X R03:0x%08X\n", env.ireg[0], env.ireg[1], env.ireg[2], env.ireg[3]);
256275 }
@@ -258,17 +277,20 @@ int HeavyOSECPUMain(int argc, char **argv)
258277 dbgrMain(&env);
259278 #endif
260279 return env.appReturnCode;
280+
281+ return 0;
261282 }
262283
263-unsigned char *Init_LoadSysLib(char argv0[], unsigned char *tmpWorkMemory)
284+unsigned char *Init_LoadSysLib(char argv0[])
264285 {
265286 unsigned char *syslib;
266287 FILE *fp;
267288 unsigned char *up;
268289 int appsize;
290+ unsigned char *tmpWorkMemory;
269291
270292 /* syslibの読み込み */
271- syslib = malloc(SYSLIBSIZ1);
293+ syslib = malloc(SYSLIBBINSIZE);
272294 fp = fopen(SYSLIB_OSE, "rb");
273295 if (fp == NULL) {
274296 syslib[0] = '/';
@@ -288,22 +310,25 @@ unsigned char *Init_LoadSysLib(char argv0[], unsigned char *tmpWorkMemory)
288310 fputs("syslib-file fopen error.\n", stderr);
289311 exit(EXIT_FAILURE);
290312 }
291- appsize = fread(syslib, 1, SYSLIBSIZ1 - 4, fp);
313+ appsize = fread(syslib, 1, SYSLIBBINSIZE - 4, fp);
292314 fclose(fp);
293- if (appsize >= SYSLIBSIZ1 - 4) {
315+ if (appsize >= SYSLIBBINSIZE - 4) {
294316 fputs("syslib-file too large.\n", stderr);
295317 exit(EXIT_FAILURE);
296318 }
297319 if (syslib[0] == 0x05 && syslib[1] == 0xc1) {
298320 // maklib のライブラリ形式である。
321+ tmpWorkMemory = malloc(SYSLIBDECOMPTMPSIZE);
299322 memcpy(tmpWorkMemory, syslib, appsize);
300323 ComLib_main(tmpWorkMemory + 2, syslib + 2);
324+ free(tmpWorkMemory);
325+ //
301326 syslib[0] = 0x05;
302327 syslib[1] = 0x1b;
303328 }
304329
305330 fp = fopen("syslib_dbg.ose", "wb");
306- fwrite(syslib, 1, SYSLIBSIZ1, fp);
331+ fwrite(syslib, 1, SYSLIBBINSIZE, fp);
307332 fclose(fp);
308333 return syslib;
309334 }
@@ -332,11 +357,11 @@ void LoadAppBin(HOSECPU_RuntimeEnvironment *env)
332357 fputs("app-file load error.\n", stderr);
333358 exit(EXIT_FAILURE);
334359 }
335- env->appSize0 = fread(env->appBin, 1, APPSIZ1 - 4, fp);
360+ env->appSize0 = fread(env->appOSECPUBin, 1, APPBINSIZE - 4, fp);
336361 env->appSize1 = env->appSize0;
337362 fclose(fp);
338363
339- if (env->appSize0 >= APPSIZ1 - 4) {
364+ if (env->appSize0 >= APPBINSIZE - 4) {
340365 fputs("app-file too large.\n", stderr);
341366 exit(EXIT_FAILURE);
342367 }
--- a/osecpu.h
+++ b/osecpu.h
@@ -51,8 +51,9 @@
5151 // It will be changed in OSECPU Rev.2 to "e2" (no adaptation in their binary layers)
5252 #define SIGN1 0xe1
5353
54-#define USE_DEBUGGER 1 // デバッグ機能を実行バイナリに含むか設定。デバッガ利用には dbgr:1 引数が実行時に必要。
55-#define ENABLE_DEBUG_CODE 1 // env.dbg_currentCodeに実行するコードを保存するデバッグコードを挿入する
54+#define USE_DEBUGGER 1 // デバッグ機能を実行バイナリに含むか設定。デバッガ利用には dbgr:1 引数が実行時に必要。
55+#define ENABLE_DEBUG_CODE 1 // env.dbg_currentCodeに実行するコードを保存するデバッグコードを挿入する
56+#define ENABLE_DEBUG_CODE_ADDR 1
5657 #define USE_TEK5 1
5758
5859
@@ -62,18 +63,24 @@
6263 #define JITC_LV_SAFE 2 /* とにかく止まる、場所は不明、テストは必要最小限 */
6364 #define JITC_LV_FASTER 4 /* 情報は生成するがチェックをしない */
6465 #define JITC_LV_FASTEST 5 /* 情報すら生成しない */
66+
6567 #define JITC_PHASE1 0x0001
6668 #define JITC_SKIPCHECK 0x0002 /* セキュリティチェックを省略する(高速危険モード) */
6769 #define JITC_NOSTARTUP 0x0004
70+
6871 #define JITC_MAXLABELS 4096
6972 #define PTRCTRLSIZ 4096
7073
71-#define APPSIZ1 1 * 1024 * 1024 /* 1MB for now */
72-#define APPJITSIZE 1 * 1024 * 1024 /* 1MB for now */
73-#define SYSJITSIZ1 2 * 1024 * 1024 /* 1MB for now */
74-#define SYSLIBSIZ1 1 * 1024 * 1024 /* 1MB for now */
75-#define SYSTMP0SIZ 1 * 1024 * 1024 /* 1MB for now */
76-#define SYSTMP1SIZ 2 * 1024 * 1024 /* 1MB for now */
74+#define APPBINSIZE 1 * 1024 * 1024 /* 1MB for now */
75+#define APPJITSIZE 1 * 1024 * 1024 /* 1MB for now */
76+#define SYSLIBBINSIZE 1 * 1024 * 1024 /* 1MB for now */
77+#define SYSLIBJITSIZE 2 * 1024 * 1024 /* 2MB for now */
78+#define SYSLIBDECOMPTMPSIZE 1 * 1024 * 1024 /* 1MB for now */
79+#define TEKDECOMPTMPSIZE 1 * 1024 * 1024 /* 1MB for now */
80+#define SYSTMP1SIZ 2 * 1024 * 1024 /* 2MB for now */
81+
82+#define DECODER_BUF06_SIZE 2 * 1024 * 1024
83+#define DECODER_BUF0A_SIZE 1 * 1024 * 1024 // too large?
7784
7885 #define KEYBUFSIZ 4096
7986
@@ -161,7 +168,7 @@ struct Regs {
161168 int debugInfo0; // 2304
162169 int debugInfo1; // 2308
163170 int dbg_currentCode; // 2312
164- int dmy; // 2316
171+ int dbg_currentCodeAddress; // 2316
165172 //
166173 HOSECPU_PointerControlTag *ptrCtrl; // 2320
167174 char winClosed, autoSleep;
@@ -171,7 +178,14 @@ struct Regs {
171178 /* Main environment */
172179 int mainArgc; // HOSECPU起動引数の個数
173180 const char **mainArgv; // HOSECPU起動引数リスト
174- unsigned char *appBin; // 実行するアプリのバイナリ
181+ //unsigned char *appBin; // 実行するアプリの元バイナリ(渡されたファイルの内容そのもの)
182+ unsigned char *appOSECPUBin; // 実行するアプリの元バイナリ(渡されたファイルの内容そのもの)
183+ unsigned char *appOSECPUBackendBin; // 実行するアプリの元バイナリ(渡されたファイルの内容そのもの)
184+ int appOSECPUBackendBinSize;
185+ unsigned char *appJITBin; // 実行するアプリのJITコンパイルされたバイナリ(実行可能)
186+ int appJITBinSize;
187+ unsigned char *syslibOSECPUBin; // syslibの元バイナリ
188+ unsigned char *syslibJITBin; // syslibのJITコンパイルされたバイナリ(実行可能)
175189 int appSize0;
176190 int appSize1;
177191 int executionLevel;
旧リポジトリブラウザで表示