-
Notifications
You must be signed in to change notification settings - Fork 0
/
sinavm.c
329 lines (276 loc) · 7.12 KB
/
sinavm.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
/*
* sinavm.c
*
* implements the functions defined in sinavm.h (see header file
* for comments on the functions)
*/
#include "sina_allocator.h"
#include "sina_types.h"
#include "sinavm.h"
#include <string.h>
#include "sina_builtins.h"
#include <stdlib.h>
#include <stdio.h>
/* these hooks into sinavm_push/pop_front could be set by the allocator for monitoring
* changes to DS/CS */
list_head_chunk* (*sinavm_push_front_hook)(
list_head_chunk* list, chunk_header* data) = NULL;
list_head_chunk* (*sinavm_pop_front_to_register_hook)(
list_head_chunk* list) = NULL;
list_head_chunk* (*sinavm_push_back_hook)(
list_head_chunk* list, chunk_header* data) = NULL;
void sinavm_initialize(sinavm_data* vm)
{
vm->cs = sinavm_new_list();
vm->ds = sinavm_new_list();
vm->flags = 0;
vm->bindings = malloc(sizeof(chunk_header*) * SINAVM_MAX_SYMBOLS);
int i;
for (i = 0; i < SINAVM_MAX_SYMBOLS; ++i)
{
(vm->bindings)[i] = NULL;
}
builtins_add(vm);
}
list_head_chunk* sinavm_new_list()
{
list_head_chunk* result = (list_head_chunk*)
allocate_chunk(LIST_HEAD_CHUNK);
result->first = NULL;
result->last = NULL;
return result;
}
list_head_chunk* sinavm_push_back(list_head_chunk* list, chunk_header* data)
{
if (sinavm_push_back_hook)
{
list = sinavm_push_back_hook(list, data);
}
allocate_push_register((chunk_header*) list);
allocate_push_register(data);
list_node_chunk* node = (list_node_chunk*)
allocate_chunk(LIST_NODE_CHUNK); /* invalidates list and data */
data = allocate_pop_register();
list = (list_head_chunk*) allocate_pop_register();
node->data = data;
node->next = NULL;
if (list->first == NULL)
{
list->first = node;
list->last = node;
}
else
{
volatile list_node_chunk* last = list->last;
last->next = node;
list->last = node;
}
return list;
}
list_head_chunk* sinavm_push_front(list_head_chunk* list, chunk_header* data)
{
if (sinavm_push_front_hook)
{
list = sinavm_push_front_hook(list, data);
}
allocate_push_register((chunk_header*) list);
allocate_push_register(data);
list_node_chunk* node = (list_node_chunk*)
allocate_chunk(LIST_NODE_CHUNK); /* invalidates list and data */
data = allocate_pop_register();
list = (list_head_chunk*) allocate_pop_register();
node->data = data;
node->next = list->first;
list->first = node;
/* node might be green (for concurrent gc) */
if (sinavm_push_front_hook)
{
list = sinavm_push_front_hook(list, node);
}
if (list->last == NULL)
{
/* list was empty on call */
list->last = node;
}
return list;
}
chunk_header* sinavm_pop_front_to_register(list_head_chunk* list)
{
if (sinavm_pop_front_to_register_hook)
{
list = sinavm_pop_front_to_register_hook(list);
}
chunk_header* result = NULL;
if (NULL != list->first)
{
result = list->first->data;
/* make sure it's pushed to the register first!!! */
allocate_push_register(result);
if (list->first == list->last)
{
/* only one element left in list */
list->first = NULL;
list->last = NULL;
}
else
{
list->first = list->first->next;
}
return result;
}
else
{
return NULL;
}
}
void sinavm_pop_front(list_head_chunk* list)
{
if (NULL != list->first)
{
if (list->first == list->last)
{
/* only one element left in list */
list->first = NULL;
list->last = NULL;
}
else
{
list->first = list->first->next;
}
}
else
{
/* do nothing */
}
}
int sinavm_type_front(list_head_chunk* list)
{
if (NULL != list->first)
{
return list->first->data->type;
}
else
{
return -1;
}
}
integer_chunk* sinavm_new_int(int value)
{
integer_chunk* result = (integer_chunk*)
allocate_chunk(INTEGER_CHUNK);
result->value = value;
return result;
}
symbol_chunk* sinavm_new_symbol(int symbol)
{
symbol_chunk* result = (symbol_chunk*)
allocate_chunk(SYMBOL_CHUNK);
result->symbol = symbol;
return result;
}
escaped_symbol_chunk* sinavm_new_escaped_symbol(int symbol)
{
escaped_symbol_chunk* result = (escaped_symbol_chunk*)
allocate_chunk(ESCAPED_SYMBOL_CHUNK);
result->symbol = symbol;
return result;
}
block_chunk* sinavm_new_block(list_head_chunk* code)
{
allocate_push_register((chunk_header*) code);
block_chunk* result = (block_chunk*)
allocate_chunk(BLOCK_CHUNK); /* invalidates code */
code = (list_head_chunk*) allocate_pop_register();
result->code = code;
result->current = code->first;
return result;
}
native_chunk* sinavm_new_native(native_func f)
{
native_chunk* result = (native_chunk*)
allocate_chunk(NATIVE_CHUNK);
result->func = f;
return result;
}
list_head_chunk* sinavm_new_string(char* string)
{
list_head_chunk* list = sinavm_new_list();
for (++string; *string != '"'; ++string)
{
allocate_push_register((chunk_header*) list);
integer_chunk* c = sinavm_new_int(*string); /* invalidates list */
list = (list_head_chunk*) allocate_pop_register();
list = sinavm_push_back(list, (chunk_header*) c);
}
return list;
}
int sinavm_list_empty(list_head_chunk* list)
{
if (list->first == NULL && list->last == NULL)
{
/* list is empty */
return 1;
}
else
{
/* list has at least 1 element */
return 0;
}
}
chunk_header* sinavm_dereference_symbol(sinavm_data* vm, int symbol)
{
return (vm->bindings)[symbol];
}
void sinavm_bind(sinavm_data* vm, int symbol, chunk_header* data)
{
if (symbol >= SINAVM_MAX_SYMBOLS)
{
error_exit("symbol range exceeded.");
}
else
{
(vm->bindings)[symbol] = data;
}
}
/* reg0: block to execute, set to NULL on return */
void sinavm_execute_block(sinavm_data* vm, block_chunk* block)
{
allocate_push_register((chunk_header*) block);
block_chunk* newblock = sinavm_new_block(block->code); /* invalidates block */
sinavm_push_front(vm->cs, (chunk_header*) newblock);
block = (block_chunk*) allocate_pop_register();
}
/* return 1, if the flowcontrol flag is set. This bit means, that a flow control
* command was executed and that the vm is set to the next statement (don't
* advance the blocks current pointer)
*/
#define SINAVM_FLAGS_FLOWCONTROL 1
int sinavm_flowcontrol_get(sinavm_data* vm)
{
return vm->flags & SINAVM_FLAGS_FLOWCONTROL;
}
/* sets the flowcontrol flag. Used by builtins such as 'loop' and 'break' */
void sinavm_flowcontrol_set(sinavm_data* vm)
{
vm->flags = vm->flags | SINAVM_FLAGS_FLOWCONTROL;
}
/* unsets the flowcontrol flag. Called by the interpreter on reading a set flowcontrol
* flag.
*/
void sinavm_flowcontrol_unset(sinavm_data* vm)
{
vm->flags = vm->flags & ~SINAVM_FLAGS_FLOWCONTROL;
}
#define SINAVM_FLAGS_TRACE 2
int sinavm_trace_get(sinavm_data* vm)
{
return vm->flags & SINAVM_FLAGS_TRACE;
}
void sinavm_trace_set(sinavm_data* vm)
{
vm->flags = vm->flags | SINAVM_FLAGS_TRACE;
}
void sinavm_trace_unset(sinavm_data* vm)
{
vm->flags = vm->flags & ~SINAVM_FLAGS_TRACE;
}