#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 {
/* --- */
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) {
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) {
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;
}
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);
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 {
if (stmt->jump.label->found_yet) {
EARG(stmt->jump.label->block_position);
} else {
- printf("<forward jump>\n");
+ debug("<forward jump>\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! */
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);
EMIT(BC_RET);
break;
default:
- printf("haven't implemented this yet!\n");
+ debug("haven't implemented this yet!\n");
}
}
}
break;
default:
- printf("(compile an expr)\n");
+ debug("(compile an expr)\n");
}
}
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");
}
}
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++) {
print_stmt(s);
}
- printf("\n");
+ debug("\n");
}
}
}
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");
}
}
#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 <stdio.h>
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");
}
}
}
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) {
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;
}
* 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) {
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
/* 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;
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");
}