aboutsummaryrefslogtreecommitdiffstats
path: root/src/vm.c
blob: 0791706e70984e5249290909b0f933ec1a7300c3 (plain)
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#ifndef VM_C
#define VM_C

#include "badlib.h"
#include "compiler.c"

#define N_CONST 256
#define STACK_SIZE MB(4)
typedef struct VM {
    Chunk *main;
    Chunk *chunk;
    u8 stack[STACK_SIZE];
    Instruction *ip;
    u8 *sp;
    u64 *fp;
    Constant *regs;
} VM;

void
vm_init(VM *vm, Chunk *chunk) {
    assert(vm);
    assert(chunk);
    assert(chunk->code);
    vm->main = chunk;
    vm->chunk = chunk;
    vm->ip = vm->chunk->code;
    vm->fp = (u64 *)vm->stack;
    vm->sp = vm->stack + chunk->var_off;
    vm->regs = (Constant *)vm->sp;
    vm->sp += sizeof(Constant) * chunk->reg_idx;
}

#define OP_UNARY(OP, TYPE)                            \
    do {                                              \
        u8 dst = instruction.dst;                     \
        u8 src_a = instruction.a;                     \
        vm->regs[dst].TYPE = OP vm->regs[src_a].TYPE; \
    } while (0);

#define OP_UNARY_CONST(OP, TYPE)                                  \
    do {                                                          \
        u8 dst = instruction.dst;                                 \
        u8 src_a = instruction.a;                                 \
        vm->regs[dst].TYPE = OP vm->chunk->constants[src_a].TYPE; \
    } while (0);

#define OP_BINARY(OP, TYPE)                                                \
    do {                                                                   \
        u8 dst = instruction.dst;                                          \
        u8 src_a = instruction.a;                                          \
        u8 src_b = instruction.b;                                          \
        vm->regs[dst].TYPE = vm->regs[src_a].TYPE OP vm->regs[src_b].TYPE; \
    } while (0);

#define OP_BINARY_CONST(OP, TYPE)                                     \
    do {                                                              \
        u8 dst = instruction.dst;                                     \
        u8 src_a = instruction.a;                                     \
        u8 src_b = instruction.b;                                     \
        vm->regs[dst].TYPE =                                          \
            vm->regs[src_a].TYPE OP vm->chunk->constants[src_b].TYPE; \
    } while (0);

#include <math.h>

void
vm_run(VM *vm) {
    assert(vm);
    assert(vm->chunk);
    assert(vm->ip);
    println("VM running...");
    while (true) {
        Instruction instruction = *vm->ip++;
#if DEBUG == 1
        print("IP: %d  -> ", vm->ip - vm->chunk->code - 1);
        disassemble_instruction(instruction);
#endif

        switch (instruction.op) {
            case OP_LD64K: {
                u8 dst = instruction.dst;
                u8 src_a = instruction.a;
                vm->regs[dst].i = vm->chunk->constants[src_a].i;
            } break;
            case OP_NOT: OP_UNARY(!, i) break;
            case OP_BITNOT: OP_UNARY(~, i) break;
            case OP_BITOR: OP_BINARY(|, i) break;
            case OP_BITXOR: OP_BINARY(^, i) break;
            case OP_BITAND: OP_BINARY(&, i) break;
            case OP_BITLSHIFT: OP_BINARY(<<, i) break;
            case OP_BITRSHIFT: OP_BINARY(>>, i) break;
            case OP_EQ: OP_BINARY(==, i) break;
            case OP_NEQ: OP_BINARY(!=, i) break;
            case OP_LT: OP_BINARY(<, i) break;
            case OP_GT: OP_BINARY(>, i) break;
            case OP_LE: OP_BINARY(<=, i) break;
            case OP_GE: OP_BINARY(>=, i) break;
            case OP_AND: OP_BINARY(&&, i) break;
            case OP_OR: OP_BINARY(||, i) break;
            case OP_ADD: OP_BINARY(+, i) break;
            case OP_SUB: OP_BINARY(-, i) break;
            case OP_MUL: OP_BINARY(*, i) break;
            case OP_DIV: OP_BINARY(/, i) break;
            case OP_MOD: OP_BINARY(%, i) break;
            case OP_ADDF: OP_BINARY(+, f) break;
            case OP_SUBF: OP_BINARY(-, f) break;
            case OP_MULF: OP_BINARY(*, f) break;
            case OP_DIVF: OP_BINARY(/, f) break;
            case OP_MODF: {
                u8 dst = instruction.dst;
                u8 src_a = instruction.a;
                u8 src_b = instruction.b;
                vm->regs[dst].f =
                    fmod(vm->regs[src_a].f, vm->chunk->constants[src_b].f);
            } break;
            case OP_NOTI: OP_UNARY_CONST(!, i) break;
            case OP_BITNOTI: OP_UNARY_CONST(~, i) break;
            case OP_BITORI: OP_BINARY_CONST(|, i) break;
            case OP_BITXORI: OP_BINARY_CONST(^, i) break;
            case OP_BITANDI: OP_BINARY_CONST(&, i) break;
            case OP_BITLSHIFTI: OP_BINARY_CONST(<<, i) break;
            case OP_BITRSHIFTI: OP_BINARY_CONST(>>, i) break;
            case OP_EQI: OP_BINARY_CONST(==, i) break;
            case OP_NEQI: OP_BINARY_CONST(!=, i) break;
            case OP_LTI: OP_BINARY_CONST(<, i) break;
            case OP_GTI: OP_BINARY_CONST(>, i) break;
            case OP_LEI: OP_BINARY_CONST(<=, i) break;
            case OP_GEI: OP_BINARY_CONST(>=, i) break;
            case OP_ANDI: OP_BINARY_CONST(&&, i) break;
            case OP_ORI: OP_BINARY_CONST(||, i) break;
            case OP_ADDI: OP_BINARY_CONST(+, i) break;
            case OP_SUBI: OP_BINARY_CONST(-, i) break;
            case OP_MULI: OP_BINARY_CONST(*, i) break;
            case OP_DIVI: OP_BINARY_CONST(/, i) break;
            case OP_MODI: OP_BINARY_CONST(%, i) break;
            case OP_ADDFI: OP_BINARY_CONST(+, f) break;
            case OP_SUBFI: OP_BINARY_CONST(-, f) break;
            case OP_MULFI: OP_BINARY_CONST(*, f) break;
            case OP_DIVFI: OP_BINARY_CONST(/, f) break;
            case OP_MODFI: {
                u8 dst = instruction.dst;
                u8 src_a = instruction.a;
                u8 src_b = instruction.b;
                vm->regs[dst].f =
                    fmod(vm->regs[src_a].f, vm->chunk->constants[src_b].f);
            } break;
            case OP_STGVAR: {
                u8 dst = instruction.dst;
                u8 src = instruction.a;
                Variable var = vm->main->vars[dst];
                s64 *stack = (s64 *)&vm->stack[var.offset];
                *stack = vm->regs[src].i;
            } break;
            case OP_STGVARI: {
                u8 dst = instruction.dst;
                u8 src = instruction.a;
                Variable var = vm->main->vars[dst];
                s64 *stack = (s64 *)&vm->stack[var.offset];
                *stack = vm->chunk->constants[src].i;
            } break;
            case OP_LDGVAR: {
                u8 dst = instruction.dst;
                u8 src = instruction.a;
                Variable var = vm->main->vars[src];
                s64 *stack = (s64 *)&vm->stack[var.offset];
                vm->regs[dst].i = *stack;
            } break;
            case OP_LDGADDR: {
                u8 dst = instruction.dst;
                u8 src = instruction.a;
                Variable var = vm->main->vars[src];
                s64 *stack = (s64 *)&vm->stack[var.offset];
                vm->regs[dst].ptr = (ptrsize)stack;
            } break;
            case OP_STLVAR: {
                u8 dst = instruction.dst;
                u8 src = instruction.a;
                Variable var = vm->chunk->vars[dst];
                vm->fp[var.offset / 8] = vm->regs[src].i;
            } break;
            case OP_STLVARI: {
                u8 dst = instruction.dst;
                u8 src = instruction.a;
                Variable var = vm->chunk->vars[dst];
                vm->fp[var.offset / 8] = vm->chunk->constants[src].i;
            } break;
            case OP_LDLVAR: {
                u8 dst = instruction.dst;
                u8 src = instruction.a;
                Variable var = vm->chunk->vars[src];
                vm->regs[dst].i = vm->fp[var.offset / 8];
            } break;
            case OP_LDLADDR: {
                u8 dst = instruction.dst;
                u8 src = instruction.a;
                Variable var = vm->chunk->vars[src];
                vm->regs[dst].i = (ptrsize)&vm->fp[var.offset / 8];
            } break;
            case OP_LDSTR: {
                u8 dst = instruction.dst;
                u8 src = instruction.a;
                Str *str = &vm->chunk->strings[src];
                vm->regs[dst].ptr = (ptrsize)str;
            } break;
            case OP_ST64I: {
                sz value = vm->regs[instruction.dst].ptr;
                s64 *addr = (s64 *)vm->regs[instruction.a].ptr;
                sz offset = vm->chunk->constants[instruction.b].i;
                addr[offset] = value;
            } break;
            case OP_ST64: {
                sz value = vm->regs[instruction.dst].i;
                s64 *addr = (s64 *)vm->regs[instruction.a].ptr;
                sz offset = vm->regs[instruction.b].i;
                addr[offset] = value;
            } break;
            case OP_LD64I: {
                s64 *addr = (s64 *)vm->regs[instruction.a].ptr;
                sz offset = vm->chunk->constants[instruction.b].i;
                vm->regs[instruction.dst].i = addr[offset];
            } break;
            case OP_LD64: {
                s64 *addr = (s64 *)vm->regs[instruction.a].ptr;
                sz offset = vm->regs[instruction.b].i;
                vm->regs[instruction.dst].i = addr[offset];
            } break;
            case OP_JMP: {
                u8 dst = instruction.dst;
                sz pos = intintmap_lookup(&vm->chunk->labels, dst)->val;
                vm->ip = vm->chunk->code + pos;
            } break;
            case OP_JMPFI: {
                u8 dst = instruction.dst;
                sz pos = intintmap_lookup(&vm->chunk->labels, dst)->val;
                bool cond = vm->chunk->constants[instruction.a].i;
                if (!cond) {
                    vm->ip = vm->chunk->code + pos;
                }
            } break;
            case OP_JMPTI: {
                u8 dst = instruction.dst;
                sz pos = intintmap_lookup(&vm->chunk->labels, dst)->val;
                bool cond = vm->chunk->constants[instruction.a].i;
                if (cond) {
                    vm->ip = vm->chunk->code + pos;
                }
            } break;
            case OP_JMPF: {
                u8 dst = instruction.dst;
                sz pos = intintmap_lookup(&vm->chunk->labels, dst)->val;
                bool cond = vm->regs[instruction.a].i;
                if (!cond) {
                    vm->ip = vm->chunk->code + pos;
                }
            } break;
            case OP_JMPT: {
                u8 dst = instruction.dst;
                sz pos = intintmap_lookup(&vm->chunk->labels, dst)->val;
                bool cond = vm->regs[instruction.a].i;
                if (cond) {
                    vm->ip = vm->chunk->code + pos;
                }
            } break;
            case OP_MOV64: {
                u8 dst = instruction.dst;
                u8 src = instruction.a;
                vm->regs[dst] = vm->regs[src];
            } break;
            case OP_MOV32: {
                u8 dst = instruction.dst;
                u8 src = instruction.a;
                vm->regs[dst].i = vm->regs[src].i & 0xFFFFFFFF;
            } break;
            case OP_MOV16: {
                u8 dst = instruction.dst;
                u8 src = instruction.a;
                vm->regs[dst].i = vm->regs[src].i & 0xFFFF;
            } break;
            case OP_MOV8: {
                u8 dst = instruction.dst;
                u8 src = instruction.a;
                vm->regs[dst].i = vm->regs[src].i & 0xFF;
            } break;
            case OP_PRINTS64: {
                u8 idx = instruction.dst;
                print("%d", vm->regs[idx].i);
            } break;
            case OP_PRINTS64I: {
                u8 idx = instruction.dst;
                print("%d", vm->chunk->constants[idx].i);
            } break;
            case OP_PRINTBOOL: {
                u8 idx = instruction.dst;
                bool val = vm->regs[idx].i;
                if (val) {
                    print("true");
                } else {
                    print("false");
                }
            } break;
            case OP_PRINTBOOLI: {
                u8 idx = instruction.dst;
                bool val = vm->chunk->constants[idx].i;
                if (val) {
                    print("true");
                } else {
                    print("false");
                }
            } break;
            case OP_PRINTF64: {
                u8 idx = instruction.dst;
                printf("%f", vm->regs[idx].f);
            } break;
            case OP_PRINTF64I: {
                u8 idx = instruction.dst;
                printf("%f", vm->chunk->constants[idx].f);
            } break;
            case OP_PRINTSTR: {
                u8 idx = instruction.dst;
                Str *string = (Str *)vm->regs[idx].ptr;
                print("%s", *string);
            } break;
            case OP_PRINTSTRI: {
                u8 idx = instruction.dst;
                Str string = vm->chunk->strings[idx];
                print("%s", string);
            } break;
            case OP_RESERVE: {
                sz offset = vm->chunk->constants[instruction.dst].i;
                vm->sp += offset;
            } break;
            case OP_PUSH: {
                sz val = vm->regs[instruction.dst].i;
                u64 *p = (u64 *)vm->sp;
                *p = val;
                vm->sp += sizeof(ptrsize);
            } break;
            case OP_PUSHI: {
                sz val = vm->chunk->constants[instruction.dst].i;
                u64 *p = (u64 *)vm->sp;
                *p = val;
                vm->sp += sizeof(ptrsize);
            } break;
            case OP_POP: {
                vm->sp -= sizeof(ptrsize);
                u64 *p = (u64 *)vm->sp;
                vm->regs[instruction.dst].i = *p;
            } break;
            case OP_PUTRET: {
                sz val = vm->regs[instruction.dst].i;
                vm->fp[-1] = val;
            } break;
            case OP_PUTRETI: {
                sz val = vm->chunk->constants[instruction.dst].i;
                vm->fp[-1] = val;
            } break;
            case OP_CALL: {
                u8 dst = instruction.dst;
                Chunk *func = vm->main->functions[dst];

                ptrsize chunk_addr = (ptrsize)vm->chunk;
                ptrsize ip_addr = (ptrsize)vm->ip;
                ptrsize reg_addr = (ptrsize)vm->regs;
                ptrsize old_fp = (ptrsize)vm->fp;

                // Allocate space for the locals.
                memset(vm->sp, 0, func->var_off - func->param_off);
                vm->fp = (u64 *)(vm->sp - func->param_off);
                vm->sp += func->var_off - func->param_off;
                vm->chunk = func;
                vm->ip = func->code;
                vm->regs = (Constant *)vm->sp;

                // Allocate registers.
                vm->sp += sizeof(Constant) * func->reg_idx;

                // Store memory addresses we need to return to this function.
                u64 *p = (u64 *)vm->sp;
                p[0] = chunk_addr;
                p[1] = ip_addr;
                p[2] = reg_addr;
                p[3] = old_fp;
                vm->sp += sizeof(ptrsize) * 4;
            } break;
            case OP_RECUR: {
                vm->ip = vm->chunk->code;
            } break;
            case OP_RET: {
                u64 *p = (u64 *)vm->sp;
                ptrsize chunk_addr = p[-4];
                ptrsize ip_addr = p[-3];
                ptrsize reg_addr = p[-2];
                ptrsize old_fp = p[-1];
                vm->sp -= sizeof(ptrsize) * 4;

                // Deallocate registers.
                vm->sp -= sizeof(Constant) * vm->chunk->reg_idx;

                // Deallocate locals.
                vm->sp -= vm->chunk->var_off;

                // Restore previous activation record.
                vm->regs = (Constant *)reg_addr;
                vm->ip = (Instruction *)ip_addr;
                vm->chunk = (Chunk *)chunk_addr;
                vm->fp = (u64 *)old_fp;
            } break;
            case OP_HALT: {
                println("VM halt...");
                if (instruction.a != 0) {
                    println("Result:");
                    Constant result = vm->regs[instruction.dst];
                    printf("\tint    -> %lld\n", result.i);
                    printf("\tfloat  -> %.10e\n", result.f);
                    printf("\thex    -> %llx\n", (u64)result.i);
                    println("\tbinary -> %b", result.i);
                }
                return;
            }
            default: {
                // eprintln("unimplemented OP code: %d", instruction.op);
                eprintln("unimplemented OP code: %s", op_str[instruction.op]);
                return;
            }
        }
    }
}

#endif  // VM_C