-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.c
194 lines (161 loc) · 6.21 KB
/
debug.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "debug.h"
#include <stdio.h>
#include "chunk.h"
#include "object.h"
static size_t simple_instruction(const char* name, int offset);
static size_t byte_instruction(const char* name, const Chunk* chunk, int offset);
static size_t long_instruction(const char* name, const Chunk* chunk, int offset);
static size_t jump_instruction(const char* name, const Chunk* chunk, int sign, int offset);
static size_t constant_instruction(const char* name, const Chunk* chunk, const size_t offset);
static size_t constant_long_instruction(const char* name, const Chunk* chunk, const size_t offset);
void disassemble_chunk(Chunk *chunk, const char *name) {
printf("== %s ==\n", name);
for (size_t offset = 0; offset < chunk->count;) {
offset = disassemble_instruction(chunk, offset);
}
printf("\n");
}
size_t disassemble_instruction(Chunk* chunk, size_t offset) {
printf("%04zu ", offset);
size_t line = Chunk_get_line(chunk, offset);
if (offset > 0 && line == Chunk_get_line(chunk, offset - 1)) {
printf(" | ");
} else {
printf("%4zu ", line);
}
uint8_t instruction = chunk->code[offset];
switch (instruction) {
case OP_LOAD_CONST:
return constant_instruction("OP_LOAD_CONST", chunk, offset);
case OP_LOAD_CONST_LONG:
return constant_long_instruction("OP_LOAD_CONST_LONG", chunk, offset);
case OP_DEF_GLOBAL:
return constant_instruction("OP_DEF_GLOBAL", chunk, offset);
case OP_DEF_GLOBAL_LONG:
return constant_long_instruction("OP_DEF_GLOBAL_LONG", chunk, offset);
case OP_GET_GLOBAL:
return constant_instruction("OP_GET_GLOBAL", chunk, offset);
case OP_GET_GLOBAL_LONG:
return constant_long_instruction("OP_GET_GLOBAL_LONG", chunk, offset);
case OP_SET_GLOBAL:
return constant_instruction("OP_SET_GLOBAL", chunk, offset);
case OP_SET_GLOBAL_LONG:
return constant_long_instruction("OP_SET_GLOBAL_LONG", chunk, offset);
case OP_GET_LOCAL:
return byte_instruction("OP_GET_LOCAL", chunk, offset);
case OP_GET_LOCAL_LONG:
return long_instruction("OP_GET_LOCAL_LONG", chunk, offset);
case OP_SET_LOCAL:
return byte_instruction("OP_SET_LOCAL", chunk, offset);
case OP_SET_LOCAL_LONG:
return long_instruction("OP_SET_LOCAL_LONG", chunk, offset);
case OP_GET_UPVALUE:
return byte_instruction("OP_GET_UPVALUE", chunk, offset);
case OP_SET_UPVALUE:
return byte_instruction("OP_SET_UPVALUE", chunk, offset);
case OP_JUMP_IF_FALSE:
return jump_instruction("OP_JUMP_IF_FALSE", chunk, 1, offset);
case OP_JUMP:
return jump_instruction("OP_JUMP", chunk, 1, offset);
case OP_LOOP:
return jump_instruction("OP_LOOP", chunk, -1, offset);
case OP_NIL:
return simple_instruction("OP_NIL", offset);
case OP_FALSE:
return simple_instruction("OP_FALSE", offset);
case OP_TRUE:
return simple_instruction("OP_TRUE", offset);
case OP_NEGATE:
return simple_instruction("OP_NEGATE", offset);
case OP_ADD:
return simple_instruction("OP_ADD", offset);
case OP_SUB:
return simple_instruction("OP_SUB", offset);
case OP_DIV:
return simple_instruction("OP_DIV", offset);
case OP_MUL:
return simple_instruction("OP_MUL", offset);
case OP_RETURN:
return simple_instruction("OP_RETURN", offset);
case OP_NOT:
return simple_instruction("OP_NOT", offset);
case OP_EQUAL:
return simple_instruction("OP_EQUAL", offset);
case OP_GREATER:
return simple_instruction("OP_GREATER", offset);
case OP_LESS:
return simple_instruction("OP_LESS", offset);
case OP_PRINT:
return simple_instruction("OP_PRINT", offset);
case OP_POP:
return simple_instruction("OP_POP", offset);
case OP_CALL:
return byte_instruction("OP_CALL", chunk, offset);
case OP_CLOSURE: {
offset++;
uint8_t constant = chunk->code[offset++];
printf("%-16s %4d ", "OP_CLOSURE", constant);
Value_print(chunk->constants.values[constant]);
printf("\n");
ObjectFunction* function = AS_FUNCTION(chunk->constants.values[constant]);
for (int j = 0; j < function->upvalue_count; j++) {
bool is_local = chunk->code[offset++];
bool index = chunk->code[offset++];
printf(
"%04ld | %s %d\n",
offset - 2, is_local ? "local" : "upvalue", index
);
}
return offset;
}
case OP_CLOSE_UPVALUE: {
return simple_instruction("OP_CLOSE_UPVALUE", offset);
}
default:
printf("Unknown opcode: %d\n", instruction);
return offset + 1;
}
}
static size_t simple_instruction(const char* name, int offset) {
printf("%s\n", name);
return offset + 1;
}
static size_t byte_instruction(const char* name, const Chunk* chunk, int offset) {
uint8_t slot = chunk->code[offset + 1];
printf("%-16s %4d\n", name, slot);
return offset + 2;
}
static size_t long_instruction(const char* name, const Chunk* chunk, int offset) {
uint8_t a = chunk->code[offset + 1];
uint8_t b = chunk->code[offset + 2];
uint8_t c = chunk->code[offset + 3];
uint32_t slot = a | (b << 8) | (c << 16);
printf("%-16s %4u\n", name, slot);
return offset + 4;
}
static size_t jump_instruction(const char* name, const Chunk* chunk,
int sign, int offset) {
uint8_t a = chunk->code[offset + 1];
uint8_t b = chunk->code[offset + 2];
uint16_t jump = a | (b << 8);
printf("%-16s %4u -> %u\n", name, offset, offset + 3 + sign * jump);
return offset + 3;
}
static size_t constant_instruction(const char* name, const Chunk* chunk,
const size_t offset) {
uint8_t constant = chunk->code[offset + 1];
printf("%-16s %4d '", name, constant);
Value_print(chunk->constants.values[constant]);
printf("'\n");
return offset + 2;
}
static size_t constant_long_instruction(const char* name, const Chunk* chunk, const size_t offset) {
const uint8_t a = chunk->code[offset + 1];
const uint8_t b = chunk->code[offset + 2];
const uint8_t c = chunk->code[offset + 3];
const uint32_t constant = a | (b << 8) | (c << 16);
printf("%-16s %4u '", name, constant);
Value_print(chunk->constants.values[constant]);
printf("'\n");
return offset + 4;
}