aboutsummaryrefslogtreecommitdiffstats
path: root/src/bytecode/vm.h
blob: 3a9b5af318ea58489be45deb08ee3e726d48fe34 (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
#ifndef BDL_VM_H
#define BDL_VM_H

#include "types.h"
#include "errors.h"
#include "chunk.h"
#include "ops.h"
#include "debug.h"

#define VM_STACK_CAP 1024

typedef struct VM {
    // Program code.
    Chunk *chunk;
    // Program counter.
    u8 *pc;
    // Stack.
    Object *stack;
} VM;

void vm_init(VM *vm);
void vm_free(VM *vm);
void vm_reset(VM *vm);
void vm_interpret(VM vm, Chunk *chunk);

void
vm_init(VM *vm) {
    *vm = (VM){0};
    array_init(vm->stack, VM_STACK_CAP);
}

void
vm_free(VM *vm) {
    array_free(vm->stack);
}

void
vm_reset(VM *vm) {
    vm_free(vm);
    vm_init(vm);
}

void
vm_interpret(VM vm, Chunk *chunk) {
    vm.chunk = chunk;
    vm.pc = vm.chunk->code;

    if (vm.chunk->code == NULL || array_size(vm.chunk->code) == 0) {
        error_push((Error){
            .type = ERR_TYPE_RUNTIME,
            .value = ERR_EMPTY_CHUNK,
        });
        return;
    }

    u8 *last = vm.chunk->code + array_size(vm.chunk->code);
    while (vm.pc < last) {
#ifdef DEBUG_TRACE_EXECUTION
        printf("stack:  [ ");
        for (size_t i = 0; i < array_size(vm.stack); i++) {
            display(vm.stack[i]);
            if (i < array_size(vm.stack) - 1) {
                printf(" | ");
            }
        }
        printf(" ]\nop: ");
        disassemble_instruction(vm.chunk, (vm.pc - vm.chunk->code));
#endif
        u8 instruction = *vm.pc++;
        switch (instruction) {
            case OP_CONSTANT: {
                u8 constant = *vm.pc++;
                Object obj = vm.chunk->constants[constant];
                array_push(vm.stack, obj);
            } break;
            case OP_SUM: {
                Object a = array_pop(vm.stack);
                Object b = array_pop(vm.stack);
                array_push(vm.stack, a + b);
            } break;
            case OP_SUB: {
                Object a = array_pop(vm.stack);
                Object b = array_pop(vm.stack);
                array_push(vm.stack, b - a);
            } break;
            case OP_MUL: {
                Object a = array_pop(vm.stack);
                Object b = array_pop(vm.stack);
                array_push(vm.stack, a * b);
            } break;
            case OP_DIV: {
                Object a = array_pop(vm.stack);
                Object b = array_pop(vm.stack);
                array_push(vm.stack, b / a);
            } break;
            case OP_MOD: {
                Object a = array_pop(vm.stack);
                Object b = array_pop(vm.stack);
                array_push(vm.stack, a % b);
            } break;
            case OP_RETURN: {
                display(array_pop(vm.stack));
                printf("\n");
                return;
            } break;
            default: {
                error_push((Error){
                    .type = ERR_TYPE_RUNTIME,
                    .value = ERR_NOT_IMPLEMENTED,
                    .line = vm.chunk->lines[(vm.pc - vm.chunk->code) - 1].line,
                    .col = vm.chunk->lines[(vm.pc - vm.chunk->code) - 1].col,
                });
                return;
            } break;
        }
    }

    error_push((Error){
        .type = ERR_TYPE_RUNTIME,
        .value = ERR_PC_OOB,
        .line = vm.chunk->lines[0].line,
        .col = vm.chunk->lines[0].col,
    });
}

#endif // BDL_VM_H