-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulator.c
485 lines (411 loc) · 11.5 KB
/
simulator.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <assert.h>
#include "simulator.h"
long nop(long dummy);
long sim_stack_push(long n);
long sim_stack_pushv(long n);
long sim_stack_pushl(long n);
long sim_stack_pushlv(long n);
long sim_stack_pop(long);
long incsp(long bytes);
long decsp(long bytes);
long add_op(long);
long sub_op(long);
long mult_op(long);
long div_op(long);
long mod_op(long);
long shl_op(long);
long shr_op(long);
long gt_op(long);
long lt_op(long);
long leq_op(long);
long geq_op(long);
long eq_op(long);
long neq_op(long);
long bw_and_op(long);
long bw_or_op(long);
long bw_xor_op(long);
long log_and_op(long);
long log_or_op(long);
long comma_op(long);
long preinc_op(long);
long predec_op(long);
long addr_op(long);
long deref_op(long);
long log_not_op(long);
long bin_not_op(long);
long postinc_op(long);
long postdec_op(long);
long assign_eq_op(long);
long assign_add_op(long);
long assign_sub_op(long);
long assign_mult_op(long);
long assign_div_op(long);
long assign_mod_op(long);
long assign_shl_op(long);
long assign_shr_op(long);
long assign_and_op(long);
long assign_or_op(long);
long assign_xor_op(long);
long jmp_op(long);
long jz_op(long);
long jnz_op(long);
char sim_stack[1024];
bool jump_taken;//, declaration_only;
//external variables (declared in simulator.h)
char SIM_MEM[0x10000];
char *sp = sim_stack, *bp = sim_stack;
long eax;
intermediate_spec *ip, *ip_start=(intermediate_spec *)(SIM_MEM + SIM_CODE_OFFSET), *ip_end;
/*char *get_var_addr(symbol *variable)
{
assert(0);
if(variable->lifetime == STATIC)
return variable->var;
else
return (long)bp + variable->var;
}*/
void *get_code_addr(void)
{
return ip_start;
}
struct op_entry
{
char *op;
long (*func)(long);
} op_table[] =
{
{"nop", nop},
{"push", sim_stack_push},
{"pushv", sim_stack_pushv},
{"pushl", sim_stack_pushl},
{"pushlv", sim_stack_pushlv},
{"pop", sim_stack_pop},
{"incsp", incsp},
{"decsp", decsp},
{"+", add_op},
{"-", sub_op},
{"*", mult_op},
{"/", div_op},
{"%", mod_op},
{"<<", shl_op},
{">>", shr_op},
{"<=", leq_op},
{">", gt_op},
{"<", lt_op},
{">=", geq_op},
{"==", eq_op},
{"!=", neq_op},
{"|", bw_or_op},
{"&", bw_and_op},
{"^", bw_xor_op},
{"||", log_or_op},
{"&&", log_and_op},
{"++pre", preinc_op},
{"--pre", predec_op},
{"&addr", addr_op},
{"*deref", deref_op},
{"!", log_not_op},
{"~", bin_not_op},
{"++post", postinc_op},
{"--post", postdec_op},
{"=", assign_eq_op},
{"+=", assign_add_op},
{"-=", assign_sub_op},
{"*=", assign_mult_op},
{"/=", assign_div_op},
{"%=", assign_mod_op},
{"<<=", assign_shl_op},
{">>=", assign_shr_op},
{"&=", assign_and_op},
{"|=", assign_or_op},
{"^=", assign_xor_op},
{"jmp", jmp_op},
{"jz", jz_op},
{"jnz", jnz_op}
//{"", _op},
};
long run_intermediate_code(bool verbose)
{
//verbose = true;
jump_taken = false;
long res = 0;
const long dump_spaces = 24;
long cursor;
if(verbose)
{
printf("\n-------------------\nexecution dump:\n");
cursor = printf("before: ");
while(cursor++ < dump_spaces) putchar(' ');
dump_symbol_table_oneline();
printf("\n");
}
if(verbose)
{
printf("instruction");
for(long i=strlen("instruction"); i<dump_spaces; i++) putchar(' ');
printf("regs\t\tstack\n");
}
for(ip=ip_start; ip<ip_end; )
{
jump_taken = false;
cursor = 0;
if(verbose) //(main+8)push a
{
cursor += print_instr(ip);
while(cursor++ < dump_spaces) putchar(' ');
}
//execute the instruction
bool valid_instr = false;
for(long i=0; i<sizeof(op_table)/sizeof(op_table[0]); i++)
{
if(strcmp(ip->op, op_table[i].op)==0)
{
res = op_table[i].func(ip->arg); //the arg is a dummy value for most instructions
valid_instr = true;
break;
}
}
if(!valid_instr)
{
printfcol(RED_FONT, "unrecognized instruction: %s\n", ip->op);
//exit(-1);
}
if(verbose) //bp=0 sp=4 (num main+56 0)
dump_stack();
if(!jump_taken)
ip++;
jump_taken = false;
//make sure we're not out of bounds
if(ip > ip_end)
{
printfcol(RED_FONT, "error: ip past end of code\n");
printf("ip:\t\t%ld\t", (long)ip); print_reg_or_val((long)ip);
printf("\nip_start:\t%ld\t", (long)ip_start); print_reg_or_val((long)ip_start);
printf("\nip_end:\t\t%ld\t", (long)ip_end); print_reg_or_val((long)ip_end);
printf("\n");
print_reg_or_val((long)((char*)ip-(char*)ip_start));
printf("\n");
//dump_intermediate();
assert(0);
}
}
//execution_done:
ip_start = ip;
if(sp != sim_stack)
{
printf("--- sp not at stack head, it's at %d ---\n", sp-sim_stack);
assert(0);
}
//return sim_stack_pop(0);
return res;
}
//if we're only doing a decl (i.e. if the code is a function declaration), we generate it but don't execute it --
//we call this instead of run_intermediate_code()
void skip_code(void)
{
ip = ip_start = ip_end;
}
long print_instr(intermediate_spec *instr)
{
//
printf("(");
long cursor = print_reg_or_val((long)instr) + 2;
printf(")");
//
cursor += printf("%s ", instr->op);
if(!(strcmp(instr->op, "push")==0 || strcmp(instr->op, "pushv")==0 ||
strcmp(instr->op, "pushl")==0 || strcmp(instr->op, "pushlv")==0 ||
strcmp(instr->op, "pop")==0 || strcmp(instr->op, "incsp")==0 || strcmp(instr->op, "decsp")==0 ||
strcmp(instr->op, "pushaddr")==0 || strcmp(instr->op, "jumplabel")==0)) //in case we dump before resolving
return cursor;
return cursor + print_reg_or_val(instr->arg);
}
long print_reg_or_val(long arg)
{
char *carg = (char*)arg;
symbol *sym = symbol_search_by_addr(carg);
symbol *func = symbol_search_function_addr(carg);
//else if(ip->arg == (long)&ip) return printf("ip");
if(arg == (long)&bp) return printf("bp");
else if(arg == (long)&sp) return printf("sp");
else if(arg == (long)&eax) return printf("eax");
else if(sym) return printfcol(YELLOW_FONT, "%s", sym->name);
else if(sim_stack<=carg && carg<=sp) return printf("bp+%d", carg-bp);
else if(carg >= (char*)ip_start) return printfcol(YELLOW_FONT, "main+%03ld", (long)(carg - (char*)ip_start));
else if(func) return printfcol(YELLOW_FONT, "%s+%d", func->name, carg - (char*)(func->var));
//else if(sim_stack<=carg && carg<=sp) return printf("stack+%ld", carg-sim_stack);
else return printf("%ld", arg);
}
void dump_stack(void)
{
printf("bp=%d sp=%d\t(", bp-sim_stack, sp-sim_stack);
for(char *p=sim_stack; p<sp; p+=SIM_INT_SIZE)
{
print_reg_or_val(*(long*)p);
printf(" ");
}
printf(")\n");
}
///////////////////////
long nop(long dummy)
{
return 0;
}
long sim_stack_push(long n)
{
memcpy(sp, &n, SIM_INT_SIZE);
sp += SIM_INT_SIZE;
return 0;
}
long sim_stack_pushv(long n)
{
long pushv = *(long*)n;
memcpy(sp, &pushv, SIM_INT_SIZE);
sp += SIM_INT_SIZE;
return 0;
}
long sim_stack_pushl(long n)
{
long *local = (long*)(bp + n);
memcpy(sp, &local, SIM_INT_SIZE);
sp += SIM_INT_SIZE;
return 0;
}
long sim_stack_pushlv(long n)
{
long *local = (long*)(bp + n);
long pushv = *local;
memcpy(sp, &pushv, SIM_INT_SIZE);
sp += SIM_INT_SIZE;
return 0;
}
long sim_stack_pop(long d)
{
assert(sp > sim_stack); //underflow
sp -= SIM_INT_SIZE;
long popval;
memcpy(&popval, sp, SIM_INT_SIZE);
if(d)
*(long*)d = popval;
return popval;
}
long incsp(long bytes)
{
assert(bytes > 0);
sp += bytes;
return 0;
}
long decsp(long bytes)
{
assert(bytes > 0);
sp -= bytes;
return 0;
}
//all binary operators follow the same semantic action format
#define def_binary_op(name,op) \
long name##_op(long d) \
{ \
long b = sim_stack_pop(0); \
long a = sim_stack_pop(0); \
sim_stack_push(a op b); \
return 0; \
}
#define def_unary_prefix_op(name,op,by_val) \
long name##_op(long d) \
{ \
long a = sim_stack_pop(0); \
if(by_val) \
sim_stack_push((long)op(*(long*)a)); \
else \
sim_stack_push((long)op(a)); \
return 0; \
}
#define def_unary_postfix_op(name,op) \
long name##_op(long d) \
{ \
long *a = (long*)sim_stack_pop(0); \
sim_stack_push((*a)op); \
return 0; \
}
#define def_assign_op(name,op) \
long assign_##name##_op(long d) \
{ \
long b = sim_stack_pop(0); \
long *lv = (long*)sim_stack_pop(0); \
*lv op b; \
sim_stack_push(*lv); \
return 0; \
}
#define BY_VALUE true
#define BY_REFERENCE false
#define def_jump_op(name, cond) \
long name##_op(long d) \
{ \
long jaddr = sim_stack_pop(0); \
long arg = sim_stack_pop(0); \
\
if(arg cond) \
{ \
ip = (intermediate_spec *)jaddr; \
jump_taken = true; \
} \
return 0; \
}
def_binary_op(add, +)
def_binary_op(sub, -)
def_binary_op(mult, *)
def_binary_op(div, /)
def_binary_op(mod, %)
def_binary_op(shl, <<)
def_binary_op(shr, >>)
def_binary_op(lt, <)
def_binary_op(gt, >)
def_binary_op(leq, <=)
def_binary_op(geq, >=)
def_binary_op(eq, ==)
def_binary_op(neq, !=)
def_binary_op(bw_or, |)
def_binary_op(bw_and, &)
def_binary_op(bw_xor, ^)
def_binary_op(log_or, ||)
def_binary_op(log_and, &&)
//def_binary_op(comma, ,)
//def_binary_op(,)
def_unary_prefix_op(preinc, ++, BY_VALUE)
def_unary_prefix_op(predec, --, BY_VALUE)
def_unary_prefix_op(addr, , BY_REFERENCE) //uhhh
def_unary_prefix_op(deref, , BY_VALUE)
def_unary_prefix_op(log_not, !, BY_VALUE)
def_unary_prefix_op(bin_not, ~, BY_VALUE)
def_unary_postfix_op(postinc, ++)
def_unary_postfix_op(postdec, --)
def_assign_op(eq, =)
def_assign_op(add, +=)
def_assign_op(sub, -=)
def_assign_op(mult, *=)
def_assign_op(div, /=)
def_assign_op(mod, %=)
def_assign_op(shl, <<=)
def_assign_op(shr, >>=)
def_assign_op(and, &=)
def_assign_op(or, |=)
def_assign_op(xor, ^=)
def_jump_op(jz, == 0)
def_jump_op(jnz, != 0)
long comma_op(long d)
{
long b = sim_stack_pop(0);
sim_stack_pop(0); //throw away value
sim_stack_push(b);
return 0;
}
long jmp_op(long d)
{
ip = (intermediate_spec *)sim_stack_pop(0);
jump_taken = true;
return 0;
}