-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.c
More file actions
158 lines (146 loc) · 5.37 KB
/
debug.c
File metadata and controls
158 lines (146 loc) · 5.37 KB
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
#include "debug.h"
#include <stdint.h>
#include <stdio.h>
#include "chunk.h"
#include "object.h"
#include "value.h"
void
disassemble_chunk(chunk_t *chunk, const char *name)
{
printf("== %s == \n", name);
for (int offset = 0; offset < chunk->count;) {
offset = disassemble_instruction(chunk, offset);
}
}
static int
constant_instruction(const char *name, chunk_t *chunk, int offset)
{
uint8_t constant = chunk->code[offset + 1];
printf("%-16s %4d '", name, constant);
print_value(chunk->constants.values[constant]);
printf("'\n");
return offset + 2;
}
static int
invoke_instruction(const char *name, chunk_t *chunk, int offset)
{
uint8_t constant = chunk->code[offset + 1];
uint8_t arg_count = chunk->code[offset + 2];
printf("%-16s (%d args) %4d '", name, arg_count, constant);
print_value(chunk->constants.values[constant]);
printf("'\n");
return offset + 3;
}
static int
simple_instruction(const char *name, int offset)
{
printf("%s\n", name);
return offset + 1;
}
static int
byte_instruction(const char *name, chunk_t *chunk, int offset)
{
uint8_t slot = chunk->code[offset + 1];
printf("%-16s %4d\n", name, slot);
return offset + 2;
}
static int
jump_instruction(const char *name, int sign, chunk_t *chunk, int offset)
{
uint16_t jump = (uint16_t)(chunk->code[offset + 1] << 8);
jump |= chunk->code[offset + 2];
printf("%-16s %4d -> %d\n", name, offset, offset + 3 + sign * jump);
return offset + 3;
}
int
disassemble_instruction(chunk_t *chunk, int offset)
{
#define SIMPLE_INSTRUCTION(OP) \
case OP: \
return simple_instruction(#OP, offset)
printf("%04d ", offset);
if (offset > 0 && chunk->lines[offset] == chunk->lines[offset - 1])
printf(" | ");
else
printf("%4d ", chunk->lines[offset]);
uint8_t instruction = chunk->code[offset];
switch (instruction) {
case OP_CONSTANT:
return constant_instruction("OP_CONSTANT", chunk, offset);
SIMPLE_INSTRUCTION(OP_NIL);
SIMPLE_INSTRUCTION(OP_TRUE);
SIMPLE_INSTRUCTION(OP_FALSE);
case OP_GET_LOCAL:
return byte_instruction("OP_GET_LOCAL", chunk, offset);
case OP_SET_LOCAL:
return byte_instruction("OP_SET_LOCAL", chunk, offset);
case OP_GET_GLOBAL:
return constant_instruction("OP_GET_GLOBAL", chunk, offset);
case OP_DEFINE_GLOBAL:
return constant_instruction("OP_DEFINE_GLOBAL", chunk, offset);
case OP_SET_GLOBAL:
return constant_instruction("OP_SET_GLOBAL", 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_GET_PROPERTY:
return constant_instruction("OP_GET_PROPERTY", chunk, offset);
case OP_SET_PROPERTY:
return constant_instruction("OP_SET_PROPERTY", chunk, offset);
case OP_GET_SUPER:
return constant_instruction("OP_GET_SUPER", chunk, offset);
SIMPLE_INSTRUCTION(OP_EQUAL);
SIMPLE_INSTRUCTION(OP_POP);
SIMPLE_INSTRUCTION(OP_GREATER);
SIMPLE_INSTRUCTION(OP_LESS);
SIMPLE_INSTRUCTION(OP_ADD);
SIMPLE_INSTRUCTION(OP_SUBTRACT);
SIMPLE_INSTRUCTION(OP_MULTIPLY);
SIMPLE_INSTRUCTION(OP_DIVIDE);
SIMPLE_INSTRUCTION(OP_NOT);
SIMPLE_INSTRUCTION(OP_NEGATE);
SIMPLE_INSTRUCTION(OP_PRINT);
case OP_JUMP:
return jump_instruction("OP_JUMP", 1, chunk, offset);
case OP_JUMP_IF_FALSE:
return jump_instruction("OP_JUMP_IF_FALSE", 1, chunk, offset);
case OP_LOOP:
return jump_instruction("OP_LOOP", 1, chunk, offset);
case OP_CALL:
return byte_instruction("OP_CALL", chunk, offset);
case OP_INVOKE:
return invoke_instruction("OP_INVOKE", chunk, offset);
case OP_SUPER_INVOKE:
return invoke_instruction("OP_SUPER_INVOKE", chunk, offset);
case OP_CLOSURE: {
offset++;
uint8_t constant = chunk->code[offset++];
printf("%-16s %4d ", "OP_CLOSURE", constant);
print_value(chunk->constants.values[constant]);
printf("\n");
obj_function *function =
as_function(chunk->constants.values[constant]);
for (int j = 0; j < function->upvalue_count; j++) {
int is_local = chunk->code[offset++];
int index = chunk->code[offset++];
printf("%04d | %s %d\n",
offset - 2,
is_local ? "local" : "upvalue",
index);
}
return offset;
}
SIMPLE_INSTRUCTION(OP_CLOSE_UPVALUE);
SIMPLE_INSTRUCTION(OP_RETURN);
case OP_CLASS:
return constant_instruction("OP_CLASS", chunk, offset);
SIMPLE_INSTRUCTION(OP_INHERIT);
case OP_METHOD:
return constant_instruction("OP_METHOD", chunk, offset);
default:
printf("Unknown opcode %d\n", instruction);
return offset + 1;
}
#undef SIMPLE_INSTRUCTION
}