-
Notifications
You must be signed in to change notification settings - Fork 0
/
IS.c
254 lines (237 loc) · 9.08 KB
/
IS.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
* @author Elio Yang
* @email jluelioyang2001@gamil.com
* @date 2021/8/23
*/
#include "IS.h"
//registers:
// program counter :address of next instruction
// base pointer
// stack pointer :top of stack,decrease when push stack
// ax is general register
extern int64_t *pc,
*bp,
*sp,
ax,
cycle;
//extern definitions
extern int64_t *text, *old_text, *stack;
extern int32_t pool_size;
void reg_init()
{
bp = sp = (int64_t *) ((char *) stack + pool_size);
ax = 0;
}
//interpreter part (using VM)
int64_t eval()
{
int64_t op;
int64_t *tmp;
while (true) {
op = *pc++;
if (op == IMM) {
ax = *pc++;
} else if (op == LOADC) {
ax = *(char *) ax;
} else if (op == LOADI) {
ax = *(int64_t *) ax;
} else if (op == STOREC) {
// sp store addr
// *sp -> an address
// assuming store char -> (char*)
// store the value in ax
ax = *(char *) *sp = ax;
sp++;
} else if (op == STOREI) {
*(int64_t *) *sp = ax;
sp++;
} else if (op == PUSH) {
*--sp = ax;
} else if (op == JMP) {
// pc points to next instruction
// JMP <addr>
// pc store 'addr'
// ins1: JMP <-run here
// ins2: addr <-pc
pc = (int64_t *) *pc;
} else if (op == JZ) {
pc = (ax ? pc + 1 : (int64_t *) *pc);
} else if (op == JNZ) {
pc = (ax ? (int64_t *) *pc : pc + 1);
} else if (op == CALL) {
// call <addr>
// jmp to addr
// save current pc & jmp addr = call
//push current pc to stack (save environment)
*--sp = (int64_t) (pc + 1);
// set pc the 'addr'
pc = (int64_t *) *pc;
} else if (op == RET) {
//get ret addr from stack
pc = (int64_t *) *sp++;
} else if (op == ENT) {
// new new frame
// ENT <size>
// alloc some space in stack
// to store local variables
// in intel:
// push bp
// mov bp, sp
// sub imm, sp
// save old bp into stack
*--sp = (int64_t) bp;
// set bp sp
bp = sp;
// get size to save
int64_t size = *pc;
// pc -> next instr
pc++;
// alloc stack
sp -= size;
} else if (op == ADJ) {
// clear stack data when leave
// ADJ <size>
// in intel :
// add sp, size
int64_t size = *pc;
pc++;
sp += size;
} else if (op == LEV) {
// restore old frame
/*
+--------+ <high addr>
| ... |
+--------+
| save pc|
+--------+
bp->| save bp|<- old sp = bp
+--------+ ^
| ... | | +size
| | |
| (size) |<- sp
+--------+
| |
+--------+ <low addr>
*/
sp = bp;
bp = (int64_t *) *sp;
sp++;
pc = (int64_t *) *sp;
sp++;
} else if (op == LEA) {
// get args from callee in caller
// __ecall: ECC calling convention
// args push in order (left -> right)
// callee do the clean job
/*
T func1(...){
T local1;
T local2;
func2(arg1,arg2);
...
}
+-----------+
| ... | <high address>
+-----------+
| arg1 | <- bp+3
+-----------+
| arg2 | <- bp+2
+-----------+
| ret addr | <- bp+1
+-----------+
| old bp | <- bp
+-----------+
| local 1 | <- bp-1
+-----------+
| local 2 | <- bp-2
+-----------+
| ... | <low address>
+-----------+
*/
// LEA <offset>
// in intel :
// add bp,offset
// ax store args' addr
ax = (int64_t) (bp + *pc);
pc++;
}
// for arithmetic all instructions are
// binary operand
// the first arg in stack
// the second arg in ax
// result will be save into ax
else if (op == OR) {
ax = *sp | ax;
sp++;
} else if (op == XOR) {
ax = *sp ^ ax;
sp++;
} else if (op == AND) {
ax = *sp & ax;
sp++;
} else if (op == EQ) {
ax = *sp == ax;
sp++;
} else if (op == NE) {
ax = *sp != ax;
sp++;
} else if (op == LT) {
ax = *sp < ax;
sp++;
} else if (op == LE) {
ax = *sp <= ax;
sp++;
} else if (op == GT) {
ax = *sp > ax;
sp++;
} else if (op == GE) {
ax = *sp >= ax;
sp++;
} else if (op == SHL) {
ax = *sp << ax;
sp++;
} else if (op == SHR) {
ax = *sp >> ax;
sp++;
} else if (op == ADD) {
ax = *sp + ax;
sp++;
} else if (op == SUB) {
ax = *sp - ax;
sp++;
} else if (op == MUL) {
ax = *sp * ax;
sp++;
} else if (op == DIV) {
ax = *sp / ax;
sp++;
} else if (op == MOD) {
ax = *sp % ax;
sp++;
}
// C api part
// use some useful c function
else if (op == EXIT) {
printf("exit(%ld)\n", *sp);
return *sp;
} else if (op == OPEN) {
ax = open((char *) sp[1], sp[0]);
} else if (op == CLOS) {
ax = close(*sp);
} else if (op == READ) {
ax = read(sp[2], (char *) sp[1], *sp);
} else if (op == PRTF) {
tmp = sp + pc[1];
ax = printf((char *) tmp[-1], tmp[-2], tmp[-3], tmp[-4], tmp[-5], tmp[-6]);
} else if (op == MALC) {
ax = (int64_t) malloc(*sp);
} else if (op == MSET) {
ax = (int64_t) memset((char *) sp[2], sp[1], *sp);
} else if (op == MCMP) {
ax = memcmp((char *) sp[2], (char *) sp[1], *sp);
} else {
printf("unknown instruction :%ld\n", op);
return err_unk;
}
}
}