-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.c
106 lines (96 loc) · 3.57 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
//
// Created by Fabian Simon on 29.09.23.
//
#include <stdio.h>
#include "debug.h"
#include "value.h"
int constant_instruction(const char* name, Chunk* 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;
}
int simple_instruction(const char* name, int offset) {
printf("%s\n", name);
return offset+1;
}
int byte_instruction(const char* name, Chunk* 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* 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;
}
void disassemble_chunk(Chunk* chunk, const char* name) {
printf("== %s ==\n", name);
for (int offset = 0; offset < chunk->count;) {
offset = disassemble_instruction(chunk, offset);
}
}
int disassemble_instruction(Chunk* chunk, int 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);
case OP_ADD:
return simple_instruction("OP_ADD", offset);
case OP_SUBTRACT:
return simple_instruction("OP_SUBTRACT", offset);
case OP_MULTIPLY:
return simple_instruction("OP_MULTIPLY", offset);
case OP_DIVIDE:
return simple_instruction("OP_DIVIDE", offset);
case OP_NOT:
return simple_instruction("OP_NOT", offset);
case OP_NEGATE:
return simple_instruction("OP_NEGATE", offset);
case OP_PRINT:
return simple_instruction("OP_PRINT", offset);
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_RETURN:
return simple_instruction("OP_RETURN", offset);
case OP_NIL:
return simple_instruction("OP_NIL", offset);
case OP_TRUE:
return simple_instruction("OP_TRUE", offset);
case OP_POP:
return simple_instruction("OP_POP", offset);
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_FALSE:
return simple_instruction("OP_FALSE", 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_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);
default:
printf("Unkown opcode %d\n", instruction);
return offset+1;
}
}