-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhvm.c
411 lines (362 loc) · 15.3 KB
/
hvm.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
#include "hvm.h"
#include "utils.h"
#include <stdio.h>
#ifndef HVM_NO_TRACE
#define TRACE_PRINTF(...) printf(__VA_ARGS__)
#else
#define TRACE_PRINTF(...)
#endif
static HVM_InstInfo _inst_infos[COUNT_HVM_INSTS] = {
[HVM_INST_NONE] = { .type = HVM_INST_NONE, .name = "(none)", .has_operand = ut_false, .min_sp = 0, },
[HVM_INST_HALT] = { .type = HVM_INST_HALT, .name = "halt", .has_operand = ut_false, .min_sp = 0, },
[HVM_INST_BEGIN_SCOPE] = { .type = HVM_INST_BEGIN_SCOPE, .name = "begin_scope", .has_operand = ut_false, .min_sp = 0, },
[HVM_INST_END_SCOPE] = { .type = HVM_INST_END_SCOPE, .name = "end_scope", .has_operand = ut_false, .min_sp = 0, },
[HVM_INST_POP] = { .type = HVM_INST_POP, .name = "pop", .has_operand = ut_false, .min_sp = 1, },
[HVM_INST_COPY] = { .type = HVM_INST_COPY, .name = "copy", .has_operand = ut_true, .min_sp = 0, },
[HVM_INST_SWAP] = { .type = HVM_INST_SWAP, .name = "swap", .has_operand = ut_true, .min_sp = 0, },
[HVM_INST_BCOPY] = { .type = HVM_INST_BCOPY, .name = "bcopy", .has_operand = ut_true, .min_sp = 0, },
[HVM_INST_BSWAP] = { .type = HVM_INST_BSWAP, .name = "bswap", .has_operand = ut_true, .min_sp = 0, },
[HVM_INST_COPYABS] = { .type = HVM_INST_COPYABS, .name = "copyabs", .has_operand = ut_true, .min_sp = 0, },
[HVM_INST_SWAPABS] = { .type = HVM_INST_SWAPABS, .name = "swapabs", .has_operand = ut_true, .min_sp = 0, },
[HVM_INST_PUSH] = { .type = HVM_INST_PUSH, .name = "push", .has_operand = ut_true, .min_sp = 0, },
[HVM_INST_ADD] = { .type = HVM_INST_ADD, .name = "add", .has_operand = ut_false, .min_sp = 2, },
[HVM_INST_SUB] = { .type = HVM_INST_SUB, .name = "sub", .has_operand = ut_false, .min_sp = 2, },
[HVM_INST_MUL] = { .type = HVM_INST_MUL, .name = "mul", .has_operand = ut_false, .min_sp = 2, },
[HVM_INST_EQ] = { .type = HVM_INST_EQ, .name = "eq", .has_operand = ut_false, .min_sp = 2, },
[HVM_INST_NE] = { .type = HVM_INST_NE, .name = "ne", .has_operand = ut_false, .min_sp = 2, },
[HVM_INST_GT] = { .type = HVM_INST_GT, .name = "gt", .has_operand = ut_false, .min_sp = 2, },
[HVM_INST_GE] = { .type = HVM_INST_GE, .name = "ge", .has_operand = ut_false, .min_sp = 2, },
[HVM_INST_LT] = { .type = HVM_INST_LT, .name = "lt", .has_operand = ut_false, .min_sp = 2, },
[HVM_INST_LE] = { .type = HVM_INST_LE, .name = "le", .has_operand = ut_false, .min_sp = 2, },
[HVM_INST_FPUSH] = { .type = HVM_INST_FPUSH, .name = "fpush", .has_operand = ut_false, .min_sp = 0, },
[HVM_INST_FADD] = { .type = HVM_INST_FADD, .name = "fadd", .has_operand = ut_false, .min_sp = 2, },
[HVM_INST_FSUB] = { .type = HVM_INST_FSUB, .name = "fsub", .has_operand = ut_false, .min_sp = 2, },
[HVM_INST_FMUL] = { .type = HVM_INST_FMUL, .name = "fmul", .has_operand = ut_false, .min_sp = 2, },
[HVM_INST_FEQ] = { .type = HVM_INST_FEQ, .name = "feq", .has_operand = ut_false, .min_sp = 2, },
[HVM_INST_FNE] = { .type = HVM_INST_FNE, .name = "fne", .has_operand = ut_false, .min_sp = 2, },
[HVM_INST_FGT] = { .type = HVM_INST_FGT, .name = "fgt", .has_operand = ut_false, .min_sp = 2, },
[HVM_INST_FGE] = { .type = HVM_INST_FGE, .name = "fge", .has_operand = ut_false, .min_sp = 2, },
[HVM_INST_FLT] = { .type = HVM_INST_FLT, .name = "flt", .has_operand = ut_false, .min_sp = 2, },
[HVM_INST_FLE] = { .type = HVM_INST_FLE, .name = "fle", .has_operand = ut_false, .min_sp = 2, },
[HVM_INST_JMP] = { .type = HVM_INST_JMP, .name = "jmp", .has_operand = ut_true, .min_sp = 0, },
[HVM_INST_JZ] = { .type = HVM_INST_JZ, .name = "jz", .has_operand = ut_true, .min_sp = 1, },
[HVM_INST_JN] = { .type = HVM_INST_JN, .name = "jn", .has_operand = ut_true, .min_sp = 1, },
[HVM_INST_DUMP] = { .type = HVM_INST_DUMP, .name = "dump", .has_operand = ut_false, .min_sp = 1, },
};
HVM_Trap hvm_exec(HVM *vm, HVM_Inst inst)
{
#define HVM_X(vm) (vm)->stack[(vm)->ss + (vm)->sp - 1]
#define HVM_Y(vm) (vm)->stack[(vm)->ss + (vm)->sp - 2]
#define HVM_PUSH(VM, WORD) \
do { \
if((VM)->sp + 1 > HVM_STACK_CAPACITY) \
return HVM_TRAP_STACK_OVERFLOW; \
(VM)->stack[(VM)->ss + (VM)->sp] = (WORD); \
(VM)->sp += 1; \
} while(0)
HVM_ASSERT(vm);
HVM_InstInfo info = _inst_infos[inst.type];
if(vm->sp < (uint32_t)info.min_sp) return HVM_TRAP_STACK_UNDERFLOW;
vm->pc += 1;
switch(inst.type) {
case HVM_INST_HALT:
{
vm->halt = 1;
} break;
case HVM_INST_POP:
{
vm->sp -= 1;
} break;
case HVM_INST_COPY:
{
if((vm->ss + vm->sp) + 1 > HVM_STACK_CAPACITY)
return HVM_TRAP_STACK_OVERFLOW;
if(vm->sp < (uint32_t)inst.op.as_u64)
return HVM_TRAP_STACK_UNDERFLOW;
vm->stack[(vm->ss + vm->sp)] = vm->stack[(vm->ss + vm->sp) - 1 - inst.op.as_u64];
vm->sp += 1;
} break;
case HVM_INST_BCOPY:
{
if((vm->ss + vm->sp) + 1 > HVM_STACK_CAPACITY)
return HVM_TRAP_STACK_OVERFLOW;
if(vm->sp < (uint32_t)inst.op.as_u64)
return HVM_TRAP_STACK_UNDERFLOW;
vm->stack[(vm->ss + vm->sp)] = vm->stack[vm->ss + inst.op.as_u64];
vm->sp += 1;
} break;
case HVM_INST_SWAP:
{
if(vm->sp < (uint32_t)inst.op.as_u64)
return HVM_TRAP_STACK_UNDERFLOW;
HVM_Word tmp = vm->stack[(vm->ss + vm->sp) - 1 - inst.op.as_u64];
vm->stack[(vm->ss + vm->sp) - 1 - inst.op.as_u64] = vm->stack[(vm->ss + vm->sp)];
vm->stack[(vm->ss + vm->sp)] = tmp;
} break;
case HVM_INST_BSWAP:
{
if(vm->sp < (uint32_t)inst.op.as_u64)
return HVM_TRAP_STACK_UNDERFLOW;
HVM_Word tmp = vm->stack[vm->ss + inst.op.as_u64];
vm->stack[vm->ss + inst.op.as_u64] = vm->stack[vm->ss + vm->sp - 1];
vm->stack[vm->ss + vm->sp - 1] = tmp;
} break;
case HVM_INST_COPYABS:
{
if((vm->ss + vm->sp) + 1 > HVM_STACK_CAPACITY)
return HVM_TRAP_STACK_OVERFLOW;
if((vm->ss + vm->sp) < (uint32_t)inst.op.as_u64)
return HVM_TRAP_STACK_UNDERFLOW;
vm->stack[(vm->ss + vm->sp)] = vm->stack[inst.op.as_u64];
vm->sp += 1;
} break;
case HVM_INST_SWAPABS:
{
if(vm->ss + vm->sp < (uint32_t)inst.op.as_u64)
return HVM_TRAP_STACK_UNDERFLOW;
HVM_Word tmp = vm->stack[inst.op.as_u64];
vm->stack[inst.op.as_u64] = vm->stack[vm->ss + vm->sp - 1];
vm->stack[vm->ss + vm->sp - 1] = tmp;
} break;
case HVM_INST_BEGIN_SCOPE:
{
HVM_PUSH(vm, HVM_WORD_U64(vm->ss));
vm->ss = vm->ss + vm->sp;
vm->sp = 0;
} break;
case HVM_INST_END_SCOPE:
{
uint32_t prev_ss = (uint32_t)vm->stack[vm->ss - 1].as_u64;
vm->sp = vm->ss - 1;
vm->ss = prev_ss;
} break;
case HVM_INST_PUSH:
{
HVM_PUSH(vm, inst.op);
} break;
case HVM_INST_JMP:
{
vm->pc = inst.op.as_u64;
} break;
case HVM_INST_JZ:
{
if(HVM_X(vm).as_i64 == 0)
vm->pc = inst.op.as_u64;
vm->sp -= 1;
} break;
case HVM_INST_JN:
{
if(HVM_X(vm).as_i64 != 0)
vm->pc = inst.op.as_u64;
vm->sp -= 1;
} break;
case HVM_INST_ADD:
{
HVM_Y(vm).as_i64 = HVM_Y(vm).as_i64 + HVM_X(vm).as_i64;
vm->sp -= 1;
} break;
case HVM_INST_SUB:
{
HVM_Y(vm).as_i64 = HVM_Y(vm).as_i64 - HVM_X(vm).as_i64;
vm->sp -= 1;
} break;
case HVM_INST_MUL:
{
HVM_Y(vm).as_i64 = HVM_Y(vm).as_i64 * HVM_X(vm).as_i64;
vm->sp -= 1;
} break;
case HVM_INST_EQ:
{
HVM_Y(vm).as_i64 = HVM_Y(vm).as_i64 == HVM_X(vm).as_i64;
vm->sp -= 1;
} break;
case HVM_INST_NE:
{
HVM_Y(vm).as_i64 = HVM_Y(vm).as_i64 != HVM_X(vm).as_i64;
vm->sp -= 1;
} break;
case HVM_INST_GT:
{
HVM_Y(vm).as_i64 = HVM_Y(vm).as_i64 > HVM_X(vm).as_i64;
vm->sp -= 1;
} break;
case HVM_INST_GE:
{
HVM_Y(vm).as_i64 = HVM_Y(vm).as_i64 >= HVM_X(vm).as_i64;
vm->sp -= 1;
} break;
case HVM_INST_LT:
{
HVM_Y(vm).as_i64 = HVM_Y(vm).as_i64 < HVM_X(vm).as_i64;
vm->sp -= 1;
} break;
case HVM_INST_LE:
{
HVM_Y(vm).as_i64 = HVM_Y(vm).as_i64 <= HVM_X(vm).as_i64;
vm->sp -= 1;
} break;
case HVM_INST_DUMP:
{
HVM_Word val = HVM_X(vm);
printf("HVM_Word{ .as_u64=%lu, .as_i64=%ld, .as_f64=%f }\n", val.as_u64, val.as_i64, val.as_f64);
vm->sp -= 1;
} break;
default:
return HVM_TRAP_INVALID_INSTRUCTION;
}
// TRACE_PRINTF("Executing %s(int(%ld)|float(%f)\n", info.name,
// info.has_operand ? inst.op.as_i64 : 0,
// info.has_operand ? inst.op.as_f64 : 0.0);
// hvm_dump(vm);
return HVM_TRAP_NONE;
#undef HVM_X
#undef HVM_Y
#undef HVM_PUSH
}
void hvm_dump(const HVM *vm)
{
HVM_ASSERT(vm);
printf("VM (sp=%u, pc=%u)\n", vm->sp, vm->pc);
for(uint32_t i = 0; i < vm->sp; ++i) {
const HVM_Word val = vm->stack[i];
printf(" [0x%X] HVM_Word{ .as_u64=%lu, .as_i64=%ld, .as_f64=%f }\n",
i, val.as_u64, val.as_i64, val.as_f64);
}
}
void hvm_init(HVM *vm)
{
HVM_ASSERT(vm);
vm->pc = 0;
vm->sp = 0;
vm->ss = 0;
}
void hvm_module_init(HVM_Module *module)
{
HVM_ASSERT(module);
module->items = UT_NULL;
module->count = 0;
module->capacity = 0;
buffer_init(&module->static_data);
}
void hvm_module_deinit(HVM_Module *module)
{
HVM_FREE(module->items);
module->count = 0;
module->capacity = 0;
module->items = UT_NULL;
}
void hvm_module_dump(const HVM_Module module)
{
HVM_Inst inst;
HVM_InstInfo info;
for(uint32_t i = 0; i < module.count; ++i) {
inst = module.items[i];
info = _inst_infos[inst.type];
printf("%u %s(HVM_Word{ .as_u64=%lu, .as_i64=%ld, .as_f64=%f})\n", i, info.name,
info.has_operand ? inst.op.as_u64 : 0,
info.has_operand ? inst.op.as_i64 : 0,
info.has_operand ? inst.op.as_f64 : 0.0
);
}
}
void hvm_module_append(HVM_Module *module, HVM_Inst inst)
{
HVM_ASSERT(module);
if(module->count + 1 > module->capacity) {
ut_size new_capacity = module->capacity * 2;
if(new_capacity == 0) new_capacity = 32;
HVM_Inst *new_items = HVM_MALLOC(sizeof(inst)*new_capacity);
HVM_ASSERT(new_items);
ut_memcpy(new_items, module->items, module->capacity * sizeof(inst));
HVM_FREE(module->items);
module->items = new_items;
module->capacity = new_capacity;
}
module->items[module->count] = inst;
module->count += 1;
}
HVM_Trap hvm_exec_module(HVM *vm, const HVM_Module module)
{
HVM_ASSERT(vm);
HVM_Inst inst;
HVM_Trap res;
while(!vm->halt) {
inst = module.items[vm->pc];
res = hvm_exec(vm, inst);
if(res != HVM_TRAP_NONE) {
HVM_InstInfo info = _inst_infos[inst.type];
fprintf(stderr, "Trap %d is thrown while executing %s(ptr(%lu)|int(%ld)|float(%f)\n", res, info.name,
info.has_operand ? inst.op.as_u64 : 0,
info.has_operand ? inst.op.as_i64 : 0,
info.has_operand ? inst.op.as_f64 : 0.0);
hvm_dump(vm);
break;
}
}
return res;
}
ut_bool hvm_module_save_to_file(const HVM_Module module, const char *file_path)
{
Arena a;
HVM_ModuleFileHeader header;
Buffer tmp;
Buffer res;
ut_memset(&a, 0, sizeof(a));
header.version = HVM_VERSION;
header.magic_number = HVM_MAGIC_NUMBER;
header.insts_amount = module.count;
header.program_size = sizeof(HVM_Inst) * module.count;
tmp.count = 0;
tmp.capacity = 0;
for(ut_size i = 0; i < (ut_size)module.count; ++i) {
HVM_Inst inst = module.items[i];
buffer_append_with_arena(&tmp, (void *)&inst, sizeof(inst), &a);
}
header.program_start = 0;
header.static_data_start = tmp.count;
header.static_data_size = module.static_data.count;
if(module.static_data.data) {
buffer_append_with_arena(&tmp, module.static_data.data, module.static_data.count, &a);
}
res.count = 0;
res.capacity = 0;
res.data = 0;
buffer_append_with_arena(&res, (void *)&header, sizeof(header), &a);
buffer_append_with_arena(&res, tmp.data, tmp.count, &a);
ut_bool retval = buffer_save_to_file(res, file_path);
arena_free(&a);
return retval;
}
ut_bool hvm_module_load_from_file(HVM_Module *module, const char *file_path, Arena *a)
{
UT_ASSERT(module->count == 0 && module->capacity == 0);
Buffer file;
Arena local;
ut_memset(&local, 0, sizeof(local));
if(!buffer_load_from_file_with_arena(&file, file_path, &local)) {
return ut_false;
}
HVM_ModuleFileHeader header;
BufferView header_buffer = buffer_slice(file, 0, sizeof(HVM_ModuleFileHeader));
ut_memcpy((void *)&header, header_buffer.data, header_buffer.count);
TRACE_PRINTF("Loaded module with following information\n");
TRACE_PRINTF("Magic Number: 0x%X\n", header.magic_number);
TRACE_PRINTF("HVM Module Version\n");
TRACE_PRINTF(" - major: %u\n", UT_VERSION_MAJOR(header.version));
TRACE_PRINTF(" - minor: %u\n", UT_VERSION_MINOR(header.version));
TRACE_PRINTF(" - revision: %u\n", UT_VERSION_REVISION(header.version));
TRACE_PRINTF("Instruction amount: %u\n", header.insts_amount);
TRACE_PRINTF("Program Size: %lu\n", header.program_size);
TRACE_PRINTF("Program Start: %lu\n", header.program_start);
TRACE_PRINTF("Static Data Size: %lu\n", header.static_data_size);
TRACE_PRINTF("Static Data Start: %lu\n", header.static_data_start);
BufferView insts_buffer = buffer_slice(file, header_buffer.count + header.program_start, header.program_start);
BufferView static_data_buffer = buffer_slice(file, header_buffer.count + header.static_data_start, header.static_data_size);
for(uint32_t i = 0; i < header.insts_amount; ++i) {
HVM_Inst inst = ((HVM_Inst*)insts_buffer.data)[i];
hvm_module_append(module, inst);
}
buffer_append_with_arena(&module->static_data, static_data_buffer.data, static_data_buffer.count, a);
arena_free(&local);
return ut_true;
}