From: cassowarii Date: Tue, 30 Jun 2026 14:40:24 +0000 (-0700) Subject: make debug flags turn on and off with a compiler switch X-Git-Url: https://www.git.cassowary.me/gitweb.cgi?a=commitdiff_plain;h=13d684d6e918497a302461bb54654ecea1dcef05;p=sarabande.git make debug flags turn on and off with a compiler switch --- diff --git a/src/compile/emit.c b/src/compile/emit.c index 8b07271..33225b0 100644 --- a/src/compile/emit.c +++ b/src/compile/emit.c @@ -2,7 +2,7 @@ #define STRING1(...) #__VA_ARGS__ #define STRING2(...) STRING1(__VA_ARGS__) -#define EMIT(...) do { printf(STRING2(__VA_ARGS__) "\n"); sbVmCompiler_write_code(cm, (u8[]) { __VA_ARGS__ }, sizeof((u8[]) { __VA_ARGS__ })); } while (0) +#define EMIT(...) do { debug(STRING2(__VA_ARGS__) "\n"); sbVmCompiler_write_code(cm, (u8[]) { __VA_ARGS__ }, sizeof((u8[]) { __VA_ARGS__ })); } while (0) #define EARG(x) (emit_arg(cm, x)) struct labelpos { @@ -33,7 +33,7 @@ void sbEmit_compile_program(sbVmProgram *vp, sbIrProgram *ir) { /* --- */ void emit_arg(sbVmCompiler *cm, i64 actual_number) { - printf(" arg %lld\n", actual_number); + debug(" arg %lld\n", actual_number); u8 buf[9]; u64 number = actual_number; if (0 <= actual_number && actual_number < 253) { @@ -74,10 +74,7 @@ void emit_arg(sbVmCompiler *cm, i64 actual_number) { void compile_chunk(sbVmCompiler *cm, sbIrChunk *chunk) { - //sbVmCompiler_add_constant(&cm, &HVINT(5)); - //sbVmCompiler_add_constant(&cm, &HVINT(9)); - - printf("\n--block %d--\n", chunk->id); + debug("\n--block %d--\n", chunk->id); int nstmts = chunk->stmts.size / sizeof(sbIrStmt*); if (chunk->id > 0) { @@ -105,7 +102,7 @@ void compile_chunk(sbVmCompiler *cm, sbIrChunk *chunk) { struct labelpos lp = ((struct labelpos*)cm->label_positions.data)[i]; u8 location_bytes[4]; u32 position = lp.label->block_position; - printf("now we know that the jump at %d should go to %d\n", lp.offset - 2, position); + debug("now we know that the jump at %d should go to %d\n", lp.offset - 2, position); location_bytes[0] = (position >> 24) & 0xFF; location_bytes[1] = (position >> 16) & 0xFF; location_bytes[2] = (position >> 8) & 0xFF; @@ -128,7 +125,7 @@ void record_labelpos(sbVmCompiler *cm, sbIrLabel *label, u32 offset) { } void compile_stmt(sbVmCompiler *cm, sbIrStmt *stmt) { - printf("%3zu ", sbVmCompiler_get_position(cm)); + debug("%3zu ", sbVmCompiler_get_position(cm)); switch (stmt->type) { case IR_S_EXPR: compile_expr(cm, stmt->expr); @@ -139,7 +136,7 @@ void compile_stmt(sbVmCompiler *cm, sbIrStmt *stmt) { EMIT(BC_JMP); } else { compile_expr(cm, stmt->jump.condition); - printf("%3zu ", sbVmCompiler_get_position(cm)); + debug("%3zu ", sbVmCompiler_get_position(cm)); if (stmt->jump.inverted) { EMIT(BC_JF); } else { @@ -149,7 +146,7 @@ void compile_stmt(sbVmCompiler *cm, sbIrStmt *stmt) { if (stmt->jump.label->found_yet) { EARG(stmt->jump.label->block_position); } else { - printf("\n"); + debug("\n"); EMIT(BC_VLONG_NUM); /* we have to remember to come back and fill in the address later when we * know where this label is! */ @@ -162,7 +159,7 @@ void compile_stmt(sbVmCompiler *cm, sbIrStmt *stmt) { case IR_S_LABEL: stmt->label->found_yet = TRUE; stmt->label->block_position = sbVmCompiler_get_position(cm); - printf("\n"); + debug("\n"); break; case IR_S_ASSIGN: compile_expr(cm, stmt->assign.expr); @@ -180,7 +177,7 @@ void compile_stmt(sbVmCompiler *cm, sbIrStmt *stmt) { EMIT(BC_RET); break; default: - printf("haven't implemented this yet!\n"); + debug("haven't implemented this yet!\n"); } } @@ -233,7 +230,7 @@ void compile_expr(sbVmCompiler *cm, sbIrExpr *expr) { } break; default: - printf("(compile an expr)\n"); + debug("(compile an expr)\n"); } } @@ -250,6 +247,6 @@ void compile_op(sbVmCompiler *cm, sbAstOp op) { case AST_OP_LE: EMIT(BC_OP_LE); break; case AST_OP_GE: EMIT(BC_OP_LT, BC_OP_NOT); break; default: - printf("unknown operation!\n"); + debug("unknown operation!\n"); } } diff --git a/src/compile/ir.c b/src/compile/ir.c index 8c931b9..612e6e0 100644 --- a/src/compile/ir.c +++ b/src/compile/ir.c @@ -48,7 +48,7 @@ void sbIrProgram_print(hIrProgram ir) { for (usize i = 0; i < nchunks; i++) { sbIrChunk *ck = ((sbIrChunk**)ir->chunks.data)[i]; - printf("\nCHUNK %d with %d variables\n", ck->id, ck->variable_count); + debug("\nCHUNK %d with %d variables\n", ck->id, ck->variable_count); usize nstmts = ck->stmts.size / sizeof(sbIrStmt*); for (usize j = 0; j < nstmts; j++) { @@ -56,7 +56,7 @@ void sbIrProgram_print(hIrProgram ir) { print_stmt(s); } - printf("\n"); + debug("\n"); } } @@ -725,90 +725,90 @@ static sbIrExpr *compile_ast_expr(hIrChunk ck, sbAst node) { } static void print_expr(sbIrExpr *e) { - printf("("); + debug("("); switch(e->type) { case IR_E_VAR: - printf("variable %zu", e->var->slot_id); + debug("variable %zu", e->var->slot_id); break; case IR_E_FUNC: - printf("{ chunk %d }", e->func->id); + debug("{ chunk %d }", e->func->id); break; case IR_E_VALUE: if (e->value.type == IT_NIL) { - printf("nil"); + debug("nil"); } else if (e->value.type == IT_INTEGER) { - printf("%lld", e->value.integer); + debug("%lld", e->value.integer); } else { - printf("some value"); + debug("some value"); } break; case IR_E_OP: print_expr(e->op.left); - printf(" %c ", e->op.type); + debug(" %c ", e->op.type); if (e->op.right) { print_expr(e->op.right); } break; case IR_E_CALL: - printf("CALL: "); + debug("CALL: "); print_expr(e->call.func); - printf(" with params "); + debug(" with params "); sbIrExpr *param = e->call.param; while (param) { print_expr(param->param.this); - printf(","); + debug(","); param = param->param.next; } break; default: - printf("something"); + debug("something"); } - printf(")"); + debug(")"); } static void print_stmt(sbIrStmt *s) { switch (s->type) { case IR_S_ARG: - printf(" bind function argument to variable %zu\n", s->arg.var->slot_id); + debug(" bind function argument to variable %zu\n", s->arg.var->slot_id); if (s->arg.last) { - printf(" (no function arguments left on the stack now)\n"); + debug(" (no function arguments left on the stack now)\n"); } break; case IR_S_LABEL: - printf("label %zu:\n", s->label->id); + debug("label %zu:\n", s->label->id); break; case IR_S_JUMP: - printf(" jump to label %zu", s->jump.label->id); + debug(" jump to label %zu", s->jump.label->id); if (s->jump.condition) { if (s->jump.inverted) { - printf(" unless "); + debug(" unless "); } else { - printf(" if "); + debug(" if "); } print_expr(s->jump.condition); } - printf("\n"); + debug("\n"); break; case IR_S_RETURN: - printf(" return "); + debug(" return "); if (s->expr) { print_expr(s->expr); } else { - printf("NOTHING"); + debug("NOTHING"); } - printf("\n"); + debug("\n"); break; case IR_S_EXPR: - printf(" "); + debug(" "); print_expr(s->expr); - printf("\n"); + debug("\n"); break; case IR_S_ASSIGN: - printf(" variable %zu = ", s->assign.var->slot_id); + debug(" variable %zu = ", s->assign.var->slot_id); print_expr(s->assign.expr); - printf("\n"); + debug("\n"); break; default: - printf(" (do something)\n"); + debug(" (do something)\n"); } } diff --git a/src/global.h b/src/global.h index 5e5f976..6976391 100644 --- a/src/global.h +++ b/src/global.h @@ -3,8 +3,10 @@ #ifdef DEBUG #define PANIC(...) do { fprintf(stderr, "PANIC: " __VA_ARGS__); fprintf(stderr, "\n at " __FILE__ ":%d\n", __LINE__); abort(); } while (0) +#define debug(...) printf(__VA_ARGS__) #else #define PANIC(...) do { fprintf(stderr, "PANIC: " __VA_ARGS__); fprintf(stderr, "\n"); abort(); } while (0) +#define debug(...) 0 #endif #include diff --git a/src/vm/exec.c b/src/vm/exec.c index 3b70370..df5ca69 100644 --- a/src/vm/exec.c +++ b/src/vm/exec.c @@ -36,11 +36,11 @@ sbVmStatus sbVm_execute(hVm vm, sbVmProgram *pm) { while (vm->running) { execute_instruction(vm); if (vm->debugmode) { - printf("Stack: "); + debug("Stack: "); for (u8 *p = vm->stack; p < vm->sp; p++) { - printf("%02X ", *p); + debug("%02X ", *p); } - printf("\n"); + debug("\n"); } } @@ -82,7 +82,7 @@ void return_from_block(hVm vm) { } void debug_print_hv(const hV *value) { - printf("%016llx %016llx\n", *(u64*)value, *((u64*)value+1)); + debug("%016llx %016llx\n", *(u64*)value, *((u64*)value+1)); } void store_local(hVm vm, usize local_index, const hV *value) { @@ -123,9 +123,7 @@ sbOpcode get_opcode(hVm vm) { void next_byte(hVm vm, u64 *result) { u8 byte = *(vm->ip++); -#ifdef DEBUG - if (vm->debugmode) printf("%02X ", byte); -#endif + if (vm->debugmode) debug("%02X ", byte); *result <<= 8; *result |= byte; } @@ -138,9 +136,7 @@ void next_byte(hVm vm, u64 *result) { * in big-endian format */ i64 get_param(hVm vm) { u64 result = *((u8*)vm->ip++); -#ifdef DEBUG - if (vm->debugmode) printf("%02llX ", result); -#endif + if (vm->debugmode) debug("%02llX ", result); i64 signed_result = result; if (result == BC_LONG_NUM) { @@ -150,7 +146,6 @@ i64 get_param(hVm vm) { signed_result = result; if (result > (1 << 15)) signed_result = result - (1 << 16); } else if (result == BC_VLONG_NUM) { - printf("here now\n"); result = 0; next_byte(vm, &result); // 0 next_byte(vm, &result); // 1 @@ -177,9 +172,7 @@ i64 get_param(hVm vm) { /* execute one instruction! wow! */ void execute_instruction(hVm vm) { sbOpcode op = get_opcode(vm); -#ifdef DEBUG - if (vm->debugmode) printf("op %02X ", op); -#endif + if (vm->debugmode) debug("op %02X ", op); u64 param; hV *v, *w, res; @@ -385,7 +378,5 @@ void execute_instruction(hVm vm) { default: PANIC("unrecognized opcode $%02X at position $%016zX", op, (usize)vm->ip); } -#ifdef DEBUG - if (vm->debugmode) printf("\n"); -#endif + if (vm->debugmode) debug("\n"); }