-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathvm.h
More file actions
46 lines (36 loc) · 1006 Bytes
/
Copy pathvm.h
File metadata and controls
46 lines (36 loc) · 1006 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
44
45
46
#ifndef VM_H
#define VM_H
#include <stdio.h>
#include "stack.h"
#include "sym.h"
#include "asm.h"
#include "defs.h"
#define POP_BOTH arg2 = pop(); arg1 = pop()
byte *cur_opcode;
byte next_opcode()
{
return *cur_opcode++;
}
void run(byte *bytecode)
{
int arg1, arg2;
cur_opcode = bytecode;
next_op:
switch (next_opcode()) {
case PUSH: push(next_opcode()); goto next_op;
case READ: push(get_sym(next_opcode())->val); goto next_op;
case WRITE: set_sym(next_opcode(), 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: {
int i;
for (i = 0; i < get_table_size(); i++) {
printf("%s = %i\n", get_sym(i)->name, get_sym(i)->val);
}
return;
}
}
}
#endif