aboutsummaryrefslogtreecommitdiffstats
path: root/src/vm.c
blob: 08a9ec3873cf8e0d4f0eaa314fe3cfbf37888246 (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
#ifndef VM_C
#define VM_C

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

void
disassemble_instruction(Instruction instruction) {
    switch (instruction.op) {
        case OP_MOV8:
        case OP_MOV16:
        case OP_MOV32:
        case OP_MOV64:
            println("%s r%d, r%d", op_str[instruction.op], instruction.dst,
                    instruction.a, instruction.b);
            break;
        case OP_LD8K:
        case OP_LD16K:
        case OP_LD32K:
        case OP_LD64K:
            println("%s r%d, c%d", op_str[instruction.op], instruction.dst,
                    instruction.a, instruction.b);
            break;
        case OP_LD8I:
        case OP_LD16I:
        case OP_LD32I:
        case OP_LD64I:
        case OP_ST8I:
        case OP_ST16I:
        case OP_ST32I:
        case OP_ST64I:
        case OP_ADDI:
        case OP_SUBI:
        case OP_MULI:
        case OP_DIVI:
        case OP_MODI:
        case OP_ADDFI:
        case OP_SUBFI:
        case OP_MULFI:
        case OP_DIVFI:
        case OP_MODFI:
        case OP_EQI:
        case OP_NEQI:
        case OP_LTI:
        case OP_GTI:
        case OP_LEI:
        case OP_GEI:
        case OP_ANDI:
        case OP_ORI:
            println("%s r%d, r%d, c%d", op_str[instruction.op], instruction.dst,
                    instruction.a, instruction.b);
            break;
        case OP_LD8:
        case OP_LD16:
        case OP_LD32:
        case OP_LD64:
        case OP_ST8:
        case OP_ST16:
        case OP_ST32:
        case OP_ST64:
        case OP_ADD:
        case OP_SUB:
        case OP_MUL:
        case OP_DIV:
        case OP_MOD:
        case OP_ADDF:
        case OP_SUBF:
        case OP_MULF:
        case OP_DIVF:
        case OP_MODF:
        case OP_EQ:
        case OP_NEQ:
        case OP_LT:
        case OP_GT:
        case OP_LE:
        case OP_GE:
        case OP_AND:
        case OP_OR:
            println("%s r%d, r%d, r%d", op_str[instruction.op], instruction.dst,
                    instruction.a, instruction.b);
            break;
        case OP_HALT: println("%s", op_str[instruction.op]); break;
        default: println("Unknown opcode %d", instruction.op); break;
    }
}

void
disassemble_chunk(Chunk chunk) {
    println("%s: =========== code ===========", chunk.file_name);
    for (sz i = 0; i < array_size(chunk.code); i++) {
        print("%s: %x{4}:  ", chunk.file_name, i);
        disassemble_instruction(chunk.code[i]);
    }
    if (array_size(chunk.constants) > 0) {
        println("%s: ========= constants ========", chunk.file_name);
        for (sz i = 0; i < array_size(chunk.constants); i++) {
            println("%s: %x{2}:  %x{8}", chunk.file_name, i,
                    chunk.constants[i]);
        }
    }
    if (array_size(chunk.strings) > 0) {
        println("%s: ========== strings =========", chunk.file_name);
        for (sz i = 0; i < array_size(chunk.strings); i++) {
            println("%s: %x{2}:  %s", chunk.file_name, i, chunk.strings[i]);
        }
    }
}

#define N_CONST 256
typedef struct VM {
    Chunk *chunk;
    Constant regs[N_CONST];
    Instruction *ip;
} VM;

void
vm_init(VM *vm, Chunk *chunk) {
    assert(vm);
    assert(chunk);
    assert(chunk->code);
    vm->chunk = chunk;
    vm->ip = vm->chunk->code;
}

#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_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_HALT: {
                println("VM HALT (int) -> %d", vm->regs[instruction.dst]);
                println("VM HALT (float) -> %f", vm->regs[instruction.dst]);
                return;
            }
            default: {
                eprintln("unimplemented OP code: %d", instruction.op);
                return;
            }
        }
    }
}

#endif  // VM_C