diff options
author | Bad Diode <bd@badd10de.dev> | 2024-06-19 16:43:18 +0200 |
---|---|---|
committer | Bad Diode <bd@badd10de.dev> | 2024-06-19 16:43:18 +0200 |
commit | c0faac681a32ffc2e323917f8b54f33558b391a5 (patch) | |
tree | 7ccafe27d4233be6ffee460add131ed89d8a5341 /src/main.c | |
parent | c2c93cc4de5f7195d2c62af8984d6f41b96b0b1c (diff) | |
download | bdl-c0faac681a32ffc2e323917f8b54f33558b391a5.tar.gz bdl-c0faac681a32ffc2e323917f8b54f33558b391a5.zip |
Add temp result printing after OP_HALT
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 26 |
1 files changed, 22 insertions, 4 deletions
@@ -221,7 +221,7 @@ disassemble_chunk(Chunk chunk) { | |||
221 | if (array_size(chunk.constants) > 0) { | 221 | if (array_size(chunk.constants) > 0) { |
222 | println("%s: ======= constants ======", chunk.file_name); | 222 | println("%s: ======= constants ======", chunk.file_name); |
223 | for (sz i = 0; i < array_size(chunk.constants); i++) { | 223 | for (sz i = 0; i < array_size(chunk.constants); i++) { |
224 | println("%s: %x{2}: %x", chunk.file_name, i, chunk.constants[i]); | 224 | println("%s: %x{2}: %x{8}", chunk.file_name, i, chunk.constants[i]); |
225 | } | 225 | } |
226 | } | 226 | } |
227 | } | 227 | } |
@@ -297,8 +297,10 @@ vm_run(VM *vm) { | |||
297 | fmod(vm->regs[src_a].f, vm->chunk->constants[src_b].f); | 297 | fmod(vm->regs[src_a].f, vm->chunk->constants[src_b].f); |
298 | } break; | 298 | } break; |
299 | case OP_HALT: { | 299 | case OP_HALT: { |
300 | println("VM HALT -> %d", vm->regs[instruction.dst]); | ||
300 | return; | 301 | return; |
301 | } | 302 | } |
303 | |||
302 | default: { | 304 | default: { |
303 | eprintln("unimplemented OP code: %d", instruction.op); | 305 | eprintln("unimplemented OP code: %d", instruction.op); |
304 | return; | 306 | return; |
@@ -324,9 +326,9 @@ CompResult compile_expr(Chunk *chunk, Node *node); | |||
324 | 326 | ||
325 | CompResult | 327 | CompResult |
326 | compile_binary(OpCode op, Chunk *chunk, Node *node) { | 328 | compile_binary(OpCode op, Chunk *chunk, Node *node) { |
329 | sz reg_dst = chunk->reg_idx++; | ||
327 | CompResult comp_a = compile_expr(chunk, node->left); | 330 | CompResult comp_a = compile_expr(chunk, node->left); |
328 | CompResult comp_b = compile_expr(chunk, node->right); | 331 | CompResult comp_b = compile_expr(chunk, node->right); |
329 | sz reg_dst = chunk->reg_idx++; | ||
330 | sz reg_a; | 332 | sz reg_a; |
331 | sz reg_b; | 333 | sz reg_b; |
332 | switch (comp_a.type) { | 334 | switch (comp_a.type) { |
@@ -454,12 +456,28 @@ process_file(Str path) { | |||
454 | array_zero(chunk.constants, 256, &bytecode_arena); | 456 | array_zero(chunk.constants, 256, &bytecode_arena); |
455 | array_zero(chunk.code, 0xffff, &bytecode_arena); | 457 | array_zero(chunk.code, 0xffff, &bytecode_arena); |
456 | sz n_roots = array_size(parser.nodes); | 458 | sz n_roots = array_size(parser.nodes); |
459 | CompResult res; | ||
457 | for (sz i = 0; i < n_roots; i++) { | 460 | for (sz i = 0; i < n_roots; i++) { |
458 | // The parser stores the root nodes as a stack. | 461 | // The parser stores the root nodes as a stack. |
459 | Node *root = parser.nodes[i]; | 462 | Node *root = parser.nodes[i]; |
460 | compile_expr(&chunk, root); | 463 | res = compile_expr(&chunk, root); |
464 | } | ||
465 | sz res_reg = 0; | ||
466 | switch (res.type) { | ||
467 | case COMP_CONST: { | ||
468 | res_reg = chunk.reg_idx++; | ||
469 | Instruction inst = | ||
470 | (Instruction){.op = OP_LD64K, .dst = res_reg, .a = res.idx}; | ||
471 | array_push(chunk.code, inst, chunk.storage); | ||
472 | } break; | ||
473 | case COMP_REG: { | ||
474 | res_reg = res.idx; | ||
475 | } break; | ||
476 | default: break; | ||
461 | } | 477 | } |
462 | array_push(chunk.code, (Instruction){.op = OP_HALT}, &bytecode_arena); | 478 | // After we are done move the last result to r0 for printing. |
479 | Instruction halt = (Instruction){.op = OP_HALT, .dst = res_reg}; | ||
480 | array_push(chunk.code, halt, &bytecode_arena); | ||
463 | 481 | ||
464 | // Run bytecode on VM. | 482 | // Run bytecode on VM. |
465 | VM vm = {0}; | 483 | VM vm = {0}; |