aboutsummaryrefslogtreecommitdiffstats
path: root/src/vm.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/vm.c')
-rw-r--r--src/vm.c60
1 files changed, 58 insertions, 2 deletions
diff --git a/src/vm.c b/src/vm.c
index 4e15bee..205c15a 100644
--- a/src/vm.c
+++ b/src/vm.c
@@ -137,7 +137,6 @@ vm_run(VM *vm) {
137 case OP_LDGVAR: { 137 case OP_LDGVAR: {
138 u8 dst = instruction.dst; 138 u8 dst = instruction.dst;
139 u8 src = instruction.a; 139 u8 src = instruction.a;
140 println("dst: %d src: %d", dst, src);
141 Variable var = vm->chunk->vars[src]; 140 Variable var = vm->chunk->vars[src];
142 s64 *stack = (s64 *)&vm->stack[var.offset]; 141 s64 *stack = (s64 *)&vm->stack[var.offset];
143 vm->regs[dst].i = *stack; 142 vm->regs[dst].i = *stack;
@@ -156,6 +155,62 @@ vm_run(VM *vm) {
156 s64 *stack = (s64 *)&vm->stack[var.offset]; 155 s64 *stack = (s64 *)&vm->stack[var.offset];
157 *stack = vm->chunk->constants[src].i; 156 *stack = vm->chunk->constants[src].i;
158 } break; 157 } break;
158 case OP_JMPI: {
159 sz offset = vm->chunk->constants[instruction.dst].i;
160 vm->ip += offset - 1;
161 } break;
162 case OP_JMPFI: {
163 bool cond = vm->chunk->constants[instruction.dst].i;
164 sz offset = vm->chunk->constants[instruction.a].i;
165 if (!cond) {
166 vm->ip += offset - 1;
167 }
168 } break;
169 case OP_JMPTI: {
170 bool cond = vm->chunk->constants[instruction.dst].i;
171 sz offset = vm->chunk->constants[instruction.a].i;
172 if (cond) {
173 vm->ip += offset - 1;
174 }
175 } break;
176 case OP_JMP: {
177 sz offset = vm->chunk->constants[instruction.dst].i;
178 vm->ip += offset - 1;
179 } break;
180 case OP_JMPF: {
181 bool cond = vm->regs[instruction.dst].i;
182 sz offset = vm->chunk->constants[instruction.a].i;
183 if (!cond) {
184 vm->ip += offset - 1;
185 }
186 } break;
187 case OP_JMPT: {
188 bool cond = vm->regs[instruction.dst].i;
189 sz offset = vm->chunk->constants[instruction.a].i;
190 if (cond) {
191 vm->ip += offset - 1;
192 }
193 } break;
194 case OP_MOV64: {
195 u8 dst = instruction.dst;
196 u8 src = instruction.a;
197 vm->regs[dst] = vm->regs[src];
198 } break;
199 case OP_MOV32: {
200 u8 dst = instruction.dst;
201 u8 src = instruction.a;
202 vm->regs[dst].i = vm->regs[src].i & 0xFFFFFFFF;
203 } break;
204 case OP_MOV16: {
205 u8 dst = instruction.dst;
206 u8 src = instruction.a;
207 vm->regs[dst].i = vm->regs[src].i & 0xFFFF;
208 } break;
209 case OP_MOV8: {
210 u8 dst = instruction.dst;
211 u8 src = instruction.a;
212 vm->regs[dst].i = vm->regs[src].i & 0xFF;
213 } break;
159 case OP_HALT: { 214 case OP_HALT: {
160 println("VM HALT (int) -> %d", vm->regs[instruction.dst]); 215 println("VM HALT (int) -> %d", vm->regs[instruction.dst]);
161 println("VM HALT (float) -> %f", vm->regs[instruction.dst]); 216 println("VM HALT (float) -> %f", vm->regs[instruction.dst]);
@@ -163,7 +218,8 @@ vm_run(VM *vm) {
163 return; 218 return;
164 } 219 }
165 default: { 220 default: {
166 eprintln("unimplemented OP code: %d", instruction.op); 221 // eprintln("unimplemented OP code: %d", instruction.op);
222 eprintln("unimplemented OP code: %s", op_str[instruction.op]);
167 return; 223 return;
168 } 224 }
169 } 225 }