-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathvm.c
More file actions
151 lines (125 loc) · 3.63 KB
/
vm.c
File metadata and controls
151 lines (125 loc) · 3.63 KB
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
#include <stdio.h>
#include <assert.h>
#include "runtime.h"
#include "opcode.h"
#include "bytecode.h"
// Helpers to play with the stack
#define LOCALS_MAX 10
#define STACK_MAX 10
#define STACK_PUSH(I) do { \
assert(sp-stack < STACK_MAX); \
*(++sp) = (I); \
retain(*sp); \
} while(0)
#define STACK_POP() (*sp--)
void run(void *literals[], byte instructions[]) {
byte *ip = instructions; // instruction pointer
Object *stack[STACK_MAX]; // THE famous stack
Object **sp = stack; // stack pointer, keeps track of current position
Object *locals[LOCALS_MAX] = {}; // where we store our local variables
// Setup the runtime
Object *self = Object_new();
retain(self);
// Start processing instructions
while (1) {
switch (*ip) {
case PUSH_NUMBER: {
ip++; // advance to the operand (literal index)
STACK_PUSH(Number_new((long)literals[*ip]));
break;
}
case PUSH_STRING: {
ip++; // advance to the operand (literal index)
STACK_PUSH(String_new((char *)literals[*ip]));
break;
}
case PUSH_SELF: {
STACK_PUSH(self);
break;
}
case PUSH_NIL: {
STACK_PUSH(NilObject);
break;
}
case PUSH_BOOL: {
ip++; // advance to operand (0 = false, 1 = true)
if (*ip == 0) {
STACK_PUSH(FalseObject);
} else {
STACK_PUSH(TrueObject);
}
break;
}
case CALL: {
ip++; // advance to operand (method name index in literals)
char *method = literals[*ip];
ip++; // advance to operand (# of args)
int argc = *ip;
Object *argv[10];
int i;
for (i = argc - 1; i >= 0; i--) argv[i] = STACK_POP();
Object *receiver = STACK_POP();
Object *result = call(receiver, method, argv, argc);
STACK_PUSH(result);
// Release all the objects
for (i = argc - 1; i >= 0; i--) release(argv[i]);
release(receiver);
break;
}
case RETURN: {
goto cleanup;
break;
}
case GET_LOCAL: {
ip++; // advance operand (local index)
STACK_PUSH(locals[*ip]);
break;
}
case SET_LOCAL: {
ip++; // local index
locals[*ip] = STACK_POP();
break;
}
case ADD: {
Object *a = STACK_POP();
Object *b = STACK_POP();
STACK_PUSH(Number_new(Number_value(a) + Number_value(b)));
release(a);
release(b);
break;
}
case JUMP_UNLESS: {
ip++; // operand (offset, # of bytes to jump forward)
byte offset = *ip;
Object *condition = STACK_POP();
if (!Object_is_true(condition)) ip += offset;
release(condition);
break;
}
case JUMP: {
ip++; // operand (offset, # of bytes to jump forward)
byte offset = *ip;
ip += offset;
break;
}
}
ip++;
}
cleanup:
release(self);
int i;
// Release all the local variables
for (i = 0; i < LOCALS_MAX; i++) if (locals[i]) release(locals[i]);
// Release everything left on the stack
while (sp > stack) release(STACK_POP());
}
int main (int argc, char const *argv[]) {
// Get compiled literals table and instructions from bytecode.h
void *literals[] = LITERALS;
byte instructions[] = INSTRUCTIONS;
init_runtime();
// Run that thing!
run(literals, instructions);
destroy_runtime();
return 0;
}