From eeff5e273f22aa28e81ab080e9ffdce85ac394b8 Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Fri, 22 Oct 2021 09:59:31 +0200 Subject: Prepare skeleton for bytecode interpreter --- src/bytecode/debug.h | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/bytecode/debug.h (limited to 'src/bytecode/debug.h') diff --git a/src/bytecode/debug.h b/src/bytecode/debug.h new file mode 100644 index 0000000..3d08d8f --- /dev/null +++ b/src/bytecode/debug.h @@ -0,0 +1,32 @@ +#ifndef BDL_DEBUG_H +#define BDL_DEBUG_H + +void disassemble_chunk(u8 *chunk, const char *name); +size_t disassemble_instruction(u8 *chunk, size_t offset); + +void +disassemble_chunk(u8 *chunk, const char *name) { + printf("== %s ==\n", name); + size_t offset = 0; + while (offset < array_size(chunk)) { + offset = disassemble_instruction(chunk, offset); + } +} + +size_t +disassemble_instruction(u8 *chunk, size_t offset) { + printf("%04ld ", offset); + u8 instruction = chunk[offset]; + switch (instruction) { + case OP_RETURN: { + printf("OP_RETURN\n"); + return offset + 1; + } break; + default: { + printf("Unknown OP: %d\n", instruction); + return offset + 1; + } break; + } +} + +#endif // BDL_DEBUG_H -- cgit v1.2.1