aboutsummaryrefslogtreecommitdiffstats
path: root/src/bytecode/debug.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/bytecode/debug.h')
-rwxr-xr-xsrc/bytecode/debug.h105
1 files changed, 0 insertions, 105 deletions
diff --git a/src/bytecode/debug.h b/src/bytecode/debug.h
deleted file mode 100755
index b21d8e6..0000000
--- a/src/bytecode/debug.h
+++ /dev/null
@@ -1,105 +0,0 @@
1#ifndef BDL_DEBUG_H
2#define BDL_DEBUG_H
3
4#include "chunk.h"
5#include "objects.h"
6
7void disassemble_chunk(Chunk *chunk);
8size_t disassemble_instruction(Chunk *chunk, size_t offset);
9
10static const char* ops_str[] = {
11 // Load/store ops.
12 [OP_CONSTANT] = "OP_CONSTANT",
13 [OP_LOCAL] = "OP_LOCAL",
14 [OP_CAPTURED] = "OP_CAPTURED",
15 [OP_CAPTURE_LOCAL] = "OP_CAPTURE_LOCAL",
16 [OP_SET_CAPTURED] = "OP_SET_CAPTURED",
17 [OP_DEF_LOCAL] = "OP_DEF_LOCAL",
18 [OP_SET_LOCAL] = "OP_SET_LOCAL",
19 [OP_DEF] = "OP_DEF",
20 [OP_SET] = "OP_SET",
21 [OP_GET] = "OP_GET",
22 // Arithmetic ops.
23 [OP_SUM] = "OP_SUM",
24 [OP_SUB] = "OP_SUB",
25 [OP_MUL] = "OP_MUL",
26 [OP_DIV] = "OP_DIV",
27 [OP_MOD] = "OP_MOD",
28 // Logic ops.
29 [OP_NOT] = "OP_NOT",
30 [OP_AND] = "OP_AND",
31 [OP_OR] = "OP_OR",
32 // Numerical comparison ops.
33 [OP_EQUAL] = "OP_EQUAL",
34 [OP_LESS] = "OP_LESS",
35 [OP_GREATER] = "OP_GREATER",
36 [OP_LESS_EQUAL] = "OP_LESS_EQUAL",
37 [OP_GREATER_EQUAL] = "OP_GREATER_EQUAL",
38 // Jump/conditional ops.
39 [OP_JUMP] = "OP_JUMP",
40 [OP_JUMP_IF_FALSE] = "OP_JUMP_IF_FALSE",
41 // Display ops.
42 [OP_DISPLAY] = "OP_DISPLAY",
43 [OP_PRINT] = "OP_PRINT",
44 [OP_NEWLINE] = "OP_NEWLINE",
45 // Procedures.
46 [OP_CALL] = "OP_CALL",
47 [OP_RETURN] = "OP_RETURN",
48 // Clear stack after each statement.
49 [OP_DROP] = "OP_DROP",
50};
51
52void
53disassemble_chunk(Chunk *chunk) {
54 printf("===== %.*s =====\n", (int)array_size(chunk->name), chunk->name);
55 printf("code:\n");
56 size_t offset = 0;
57 while (offset < array_size(chunk->code)) {
58 offset = disassemble_instruction(chunk, offset);
59 }
60 printf("\nconstants:\n");
61 offset = 0;
62 while (offset < array_size(chunk->constants)) {
63 printf("\t%03ld -> ", offset);
64 object_display(chunk->constants[offset]);
65 printf("\n");
66 offset++;
67 }
68 printf("\n");
69}
70
71size_t
72disassemble_instruction(Chunk *chunk, size_t offset) {
73 printf("\t%04ld ", offset);
74 if (offset > 0
75 && chunk->lines[offset].line == chunk->lines[offset - 1].line
76 && chunk->lines[offset].col == chunk->lines[offset - 1].col) {
77 printf("%4s|%-4s ", " ", " ");
78 } else {
79 printf("%4ld:%-4ld ", chunk->lines[offset].line, chunk->lines[offset].col);
80 }
81 u8 instruction = chunk->code[offset];
82 switch (instruction) {
83 case OP_CONSTANT: {
84 u8 constant = chunk->code[offset + 1];
85 printf("%-16s %4d -> ", ops_str[instruction], constant);
86 object_display(chunk->constants[constant]);
87 printf("\n");
88 return offset + 2;
89 } break;
90 case OP_JUMP:
91 case OP_JUMP_IF_FALSE: {
92 u16 a = chunk->code[offset + 1];
93 u16 b = chunk->code[offset + 2];
94 s16 jmp = (a << 8) | b;
95 printf("%-16s %4d\n", ops_str[instruction], jmp);
96 return offset + 3;
97 } break;
98 default: {
99 printf("%s\n", ops_str[instruction]);
100 return offset + 1;
101 } break;
102 }
103}
104
105#endif // BDL_DEBUG_H