forked from zakirullin/tiny-compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvirtual_machine.h
More file actions
43 lines (35 loc) · 983 Bytes
/
Copy pathvirtual_machine.h
File metadata and controls
43 lines (35 loc) · 983 Bytes
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
#ifndef VIRTUAL_MACHINE_H
#define VIRTUAL_MACHINE_H
#include <stdio.h>
#include "stack.h"
#include "symbol_table.h"
#include "codes.h"
#include "generator.h"
#define POP_BOTH arg2 = pop(); arg1 = pop()
byte *cur_byte = obj;
byte next_byte()
{
return *cur_byte++;
}
void run()
{
int arg1;
int arg2;
int i;
next_op:
switch (next_byte()) {
case PUSH: push(next_byte()); goto next_op;
case READ: push(get_sym(next_byte())->val); goto next_op;
case WRITE: set_sym(next_byte(), pop()); goto next_op;
case ADD: POP_BOTH; push(arg1 + arg2); goto next_op;
case SUB: POP_BOTH; push(arg1 - arg2); goto next_op;
case MUL: POP_BOTH; push(arg1 * arg2); goto next_op;
case DIV: POP_BOTH; push(arg1 / arg2); goto next_op;
case RET:
for (i = 0; i < table_size; i++) {
printf("%s = %i\n", get_sym(i)->name, get_sym(i)->val);
}
return;
}
}
#endif