From b643366c0ef18c27aa1ac52e1dd035f1d6efa45e Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Sat, 29 Jun 2024 11:04:24 +0200 Subject: Add linecol info for debugging if needed --- src/compiler.c | 61 ++++++++++++++++++++++++++++----------------------- src/main.c | 8 +++---- src/vm.c | 6 ++--- tests/compilation.bad | 34 +++++----------------------- 4 files changed, 47 insertions(+), 62 deletions(-) diff --git a/src/compiler.c b/src/compiler.c index bd50955..84cc99a 100644 --- a/src/compiler.c +++ b/src/compiler.c @@ -29,6 +29,11 @@ typedef union Constant { ptrsize ptr; } Constant; +typedef struct LineCol { + sz line; + sz col; +} LineCol; + typedef struct Chunk { sz id; Instruction *code; @@ -54,17 +59,17 @@ typedef struct Chunk { // Debugging. Str file_name; Arena *storage; - // TODO: line/col info for debugging. + LineCol *linecol; } Chunk; typedef enum OpCode { // OP DST A B // --------------------------------------------------------------- // VM/high level instructions. - OP_HALT, // halt - OP_STVARI, // stvari vx, ca - OP_STVAR, // stvar vx, ra - OP_LDVAR, // ldvar rx, vx + OP_HALT, // halt + OP_STGVARI, // stgvari vx, ca + OP_STGVAR, // stgvar vx, ra + OP_LDGVAR, // ldgvar rx, vx // Load/Store instructions. OP_LD8K, // ld8k rx, ca -> u8 rx = ca OP_LD16K, // ld16k rx, ca -> u16 rx = ca @@ -147,9 +152,9 @@ typedef enum OpCode { Str op_str[] = { [OP_HALT] = cstr("HALT "), - [OP_STVAR] = cstr("STVAR "), - [OP_STVARI] = cstr("STVARI "), - [OP_LDVAR] = cstr("LDVAR "), + [OP_STGVAR] = cstr("STGVAR "), + [OP_STGVARI] = cstr("STGVARI "), + [OP_LDGVAR] = cstr("LDGVAR "), // Load ops. [OP_LD8K] = cstr("LD8K "), [OP_LD16K] = cstr("LD16K "), @@ -245,15 +250,17 @@ typedef struct CompResult { CompResult compile_expr(Chunk *chunk, Node *node); -#define EMIT_OP(OP, DST, A, B, NODE, CHUNK) \ - do { \ - Instruction inst = (Instruction){ \ - .op = (OP), \ - .dst = (DST), \ - .a = (A), \ - .b = (B), \ - }; \ - array_push((CHUNK)->code, inst, (CHUNK)->storage); \ +#define EMIT_OP(OP, DST, A, B, NODE, CHUNK) \ + do { \ + Instruction inst = (Instruction){ \ + .op = (OP), \ + .dst = (DST), \ + .a = (A), \ + .b = (B), \ + }; \ + array_push((CHUNK)->code, inst, (CHUNK)->storage); \ + LineCol linecol = (LineCol){.line = (NODE)->line, .col = (NODE)->col}; \ + array_push((CHUNK)->linecol, linecol, (CHUNK)->storage); \ } while (0) CompResult @@ -512,11 +519,11 @@ compile_expr(Chunk *chunk, Node *node) { CompResult res = compile_expr(chunk, node->var_val); switch (res.type) { case COMP_CONST: { - EMIT_OP(OP_STVARI, idx, res.idx, 0, node->var_val, + EMIT_OP(OP_STGVARI, idx, res.idx, 0, node->var_val, chunk); } break; case COMP_REG: { - EMIT_OP(OP_STVAR, idx, res.idx, 0, node->var_val, + EMIT_OP(OP_STGVAR, idx, res.idx, 0, node->var_val, chunk); } break; default: { @@ -536,7 +543,7 @@ compile_expr(Chunk *chunk, Node *node) { } Variable var = map->val; u8 reg_dst = chunk->reg_idx++; - EMIT_OP(OP_LDVAR, reg_dst, var.idx, 0, node->var_val, chunk); + EMIT_OP(OP_LDGVAR, reg_dst, var.idx, 0, node, chunk); return (CompResult){.type = COMP_REG, .idx = reg_dst}; } break; case NODE_BLOCK: { @@ -639,15 +646,15 @@ disassemble_instruction(Instruction instruction) { println("%s r%d, r%d, r%d", op_str[instruction.op], instruction.dst, instruction.a, instruction.b); break; - case OP_LDVAR: + case OP_LDGVAR: println("%s r%d, v%d", op_str[instruction.op], instruction.dst, instruction.a, instruction.b); break; - case OP_STVAR: + case OP_STGVAR: println("%s v%d, r%d", op_str[instruction.op], instruction.dst, instruction.a, instruction.b); break; - case OP_STVARI: + case OP_STGVARI: println("%s v%d, c%d", op_str[instruction.op], instruction.dst, instruction.a, instruction.b); break; @@ -668,26 +675,26 @@ disassemble_instruction(Instruction instruction) { void disassemble_chunk(Chunk chunk) { - println("%s: =========== code ===========", chunk.file_name); + 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); + 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); + 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]); } } if (array_size(chunk.vars) > 0) { - println("%s: ========= variables ========", chunk.file_name); + println("%s: ============ variables ===========", chunk.file_name); for (sz i = 0; i < array_size(chunk.vars); i++) { println("%s: %x{2}: [%x{4}:%x{4}] %s: %s", chunk.file_name, i, chunk.vars[i].offset, diff --git a/src/main.c b/src/main.c index e14ef43..0e453c6 100644 --- a/src/main.c +++ b/src/main.c @@ -219,10 +219,10 @@ process_file(Str path) { // println("VM REGISTERS BEFORE:\n%{Mem}", // &(Array){.mem = (u8 *)&vm.regs, sizeof(vm.regs)}); vm_run(&vm); - println("VM REGISTERS AFTER:\n%{Mem}", - &(Array){.mem = (u8 *)&vm.regs, sizeof(vm.regs)}); - println("VM MEMORY AFTER:\n%{Mem}", - &(Array){.mem = (u8 *)&vm.stack, sizeof(vm.stack)}); + // println("VM REGISTERS AFTER:\n%{Mem}", + // &(Array){.mem = (u8 *)&vm.regs, sizeof(vm.regs)}); + // println("VM MEMORY AFTER:\n%{Mem}", + // &(Array){.mem = (u8 *)&vm.stack, sizeof(vm.stack)}); #if DEBUG == 1 println("Space used: %{Arena}", &lexer_arena); diff --git a/src/vm.c b/src/vm.c index fac477a..4e15bee 100644 --- a/src/vm.c +++ b/src/vm.c @@ -134,7 +134,7 @@ vm_run(VM *vm) { vm->regs[dst].f = fmod(vm->regs[src_a].f, vm->chunk->constants[src_b].f); } break; - case OP_LDVAR: { + case OP_LDGVAR: { u8 dst = instruction.dst; u8 src = instruction.a; println("dst: %d src: %d", dst, src); @@ -142,14 +142,14 @@ vm_run(VM *vm) { s64 *stack = (s64 *)&vm->stack[var.offset]; vm->regs[dst].i = *stack; } break; - case OP_STVAR: { + case OP_STGVAR: { u8 dst = instruction.dst; u8 src = instruction.a; Variable var = vm->chunk->vars[dst]; s64 *stack = (s64 *)&vm->stack[var.offset]; *stack = vm->regs[src].i; } break; - case OP_STVARI: { + case OP_STGVARI: { u8 dst = instruction.dst; u8 src = instruction.a; Variable var = vm->chunk->vars[dst]; diff --git a/tests/compilation.bad b/tests/compilation.bad index 0ffbb06..f1f027a 100644 --- a/tests/compilation.bad +++ b/tests/compilation.bad @@ -1,30 +1,8 @@ -let b = 8 let a = 8 -; { -; let a = 1 + 2 * 4 -; let b = 3.0 -; } +let b = 16 +let c = { + let a = 32 + a + 8 +} -1 + a -; 0xf | 0xf0 -; 0xf & 0xff -; 0x1 << 2 -; 0x1 << 2 -; (~0xf & 0xfff << 8) == 0xfff00 -; 0xf << 4 | 0xf -; 1 + 2 -; 1 < 2 -; !(1 == 0 || 1 <= 1) -; 1 == 1 && 1 <= 1 -; !((1 * 2 * 3) * (1 * 2 * 3) >= 3 && 4 != 3 + 2) -; 1 + 2 * 3 -; 1.0 + 2.0 * 3.0 -; true -; false -; 0 1 -; "hello" -; "world" -; fun foo(): int { -; 32 -; } -; 1 + 2 + foo() +a + b + c -- cgit v1.2.1