aboutsummaryrefslogtreecommitdiffstats
path: root/src/bytecode/debug.h
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2021-10-22 10:34:25 +0200
committerBad Diode <bd@badd10de.dev>2021-10-22 10:34:25 +0200
commit33372512fc32c26913c8385637d20f6d98c8376c (patch)
tree9aa30e3e376d4769e858c37c912866dfdb4b4a62 /src/bytecode/debug.h
parenteeff5e273f22aa28e81ab080e9ffdce85ac394b8 (diff)
downloadbdl-33372512fc32c26913c8385637d20f6d98c8376c.tar.gz
bdl-33372512fc32c26913c8385637d20f6d98c8376c.zip
Add constants operation
Diffstat (limited to 'src/bytecode/debug.h')
-rw-r--r--src/bytecode/debug.h21
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
4void disassemble_chunk(u8 *chunk, const char *name); 4#include "chunk.h"
5size_t disassemble_instruction(u8 *chunk, size_t offset); 5
6void disassemble_chunk(Chunk chunk, const char *name);
7size_t disassemble_instruction(Chunk chunk, size_t offset);
6 8
7void 9void
8disassemble_chunk(u8 *chunk, const char *name) { 10disassemble_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
16size_t 18size_t
17disassemble_instruction(u8 *chunk, size_t offset) { 19disassemble_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;