diff options
Diffstat (limited to 'src/bytecode/debug.h')
-rw-r--r-- | src/bytecode/debug.h | 21 |
1 files changed, 15 insertions, 6 deletions
diff --git a/src/bytecode/debug.h b/src/bytecode/debug.h index 3d08d8f..e07b9a2 100644 --- a/src/bytecode/debug.h +++ b/src/bytecode/debug.h | |||
@@ -1,27 +1,36 @@ | |||
1 | #ifndef BDL_DEBUG_H | 1 | #ifndef BDL_DEBUG_H |
2 | #define BDL_DEBUG_H | 2 | #define BDL_DEBUG_H |
3 | 3 | ||
4 | void disassemble_chunk(u8 *chunk, const char *name); | 4 | #include "chunk.h" |
5 | size_t disassemble_instruction(u8 *chunk, size_t offset); | 5 | |
6 | void disassemble_chunk(Chunk chunk, const char *name); | ||
7 | size_t disassemble_instruction(Chunk chunk, size_t offset); | ||
6 | 8 | ||
7 | void | 9 | void |
8 | disassemble_chunk(u8 *chunk, const char *name) { | 10 | disassemble_chunk(Chunk chunk, const char *name) { |
9 | printf("== %s ==\n", name); | 11 | printf("== %s ==\n", name); |
10 | size_t offset = 0; | 12 | size_t offset = 0; |
11 | while (offset < array_size(chunk)) { | 13 | while (offset < array_size(chunk.code)) { |
12 | offset = disassemble_instruction(chunk, offset); | 14 | offset = disassemble_instruction(chunk, offset); |
13 | } | 15 | } |
14 | } | 16 | } |
15 | 17 | ||
16 | size_t | 18 | size_t |
17 | disassemble_instruction(u8 *chunk, size_t offset) { | 19 | disassemble_instruction(Chunk chunk, size_t offset) { |
18 | printf("%04ld ", offset); | 20 | printf("%04ld ", offset); |
19 | u8 instruction = chunk[offset]; | 21 | u8 instruction = chunk.code[offset]; |
20 | switch (instruction) { | 22 | switch (instruction) { |
21 | case OP_RETURN: { | 23 | case OP_RETURN: { |
22 | printf("OP_RETURN\n"); | 24 | printf("OP_RETURN\n"); |
23 | return offset + 1; | 25 | return offset + 1; |
24 | } break; | 26 | } break; |
27 | case OP_CONSTANT: { | ||
28 | u8 constant = chunk.code[offset + 1]; | ||
29 | printf("%-16s %4d (", "OP_CONSTANT", constant); | ||
30 | display(chunk.constants[constant]); | ||
31 | printf(")\n"); | ||
32 | return offset + 2; | ||
33 | } break; | ||
25 | default: { | 34 | default: { |
26 | printf("Unknown OP: %d\n", instruction); | 35 | printf("Unknown OP: %d\n", instruction); |
27 | return offset + 1; | 36 | return offset + 1; |