diff options
author | Bad Diode <bd@badd10de.dev> | 2021-10-22 09:59:31 +0200 |
---|---|---|
committer | Bad Diode <bd@badd10de.dev> | 2021-10-22 09:59:31 +0200 |
commit | eeff5e273f22aa28e81ab080e9ffdce85ac394b8 (patch) | |
tree | 71d11c76be7c0bb649099bb55e6181f9b7c6c8a8 /src/bytecode/debug.h | |
parent | 5bd694fc7071bfb76b9f65c89d253b2b4e18cf63 (diff) | |
download | bdl-eeff5e273f22aa28e81ab080e9ffdce85ac394b8.tar.gz bdl-eeff5e273f22aa28e81ab080e9ffdce85ac394b8.zip |
Prepare skeleton for bytecode interpreter
Diffstat (limited to 'src/bytecode/debug.h')
-rw-r--r-- | src/bytecode/debug.h | 32 |
1 files changed, 32 insertions, 0 deletions
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 @@ | |||
1 | #ifndef BDL_DEBUG_H | ||
2 | #define BDL_DEBUG_H | ||
3 | |||
4 | void disassemble_chunk(u8 *chunk, const char *name); | ||
5 | size_t disassemble_instruction(u8 *chunk, size_t offset); | ||
6 | |||
7 | void | ||
8 | disassemble_chunk(u8 *chunk, const char *name) { | ||
9 | printf("== %s ==\n", name); | ||
10 | size_t offset = 0; | ||
11 | while (offset < array_size(chunk)) { | ||
12 | offset = disassemble_instruction(chunk, offset); | ||
13 | } | ||
14 | } | ||
15 | |||
16 | size_t | ||
17 | disassemble_instruction(u8 *chunk, size_t offset) { | ||
18 | printf("%04ld ", offset); | ||
19 | u8 instruction = chunk[offset]; | ||
20 | switch (instruction) { | ||
21 | case OP_RETURN: { | ||
22 | printf("OP_RETURN\n"); | ||
23 | return offset + 1; | ||
24 | } break; | ||
25 | default: { | ||
26 | printf("Unknown OP: %d\n", instruction); | ||
27 | return offset + 1; | ||
28 | } break; | ||
29 | } | ||
30 | } | ||
31 | |||
32 | #endif // BDL_DEBUG_H | ||