-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebugging.zig
More file actions
145 lines (126 loc) · 6.32 KB
/
debugging.zig
File metadata and controls
145 lines (126 loc) · 6.32 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
const std = @import("std");
const Chunk = @import("chunk.zig").Chunk;
const Opc = @import("chunk.zig").OpCode;
const Value = @import("value.zig").Value;
const printValue = @import("value.zig").printValue;
pub fn disassembleChunk(chunk: *Chunk, name: []const u8) !void {
std.debug.print("== {s} ==\n", .{name});
var offset: usize = 0;
while (offset < chunk.code.count) : (offset = disassembleInstruction(chunk, offset)) {}
}
pub fn disassembleInstruction(chunk: *Chunk, offset: usize) usize {
std.debug.print("{d:0>4}", .{offset});
const lines = chunk.lines;
const instruction = Opc.fromU8(chunk.code.items[offset]);
if (offset > 0 and lines.items[offset] == lines.items[offset - 1]) {
std.debug.print(" | ", .{});
} else {
std.debug.print("{d: >4} ", .{lines.items[offset]});
}
return switch (instruction) {
.op_return => simpleInstruction(instruction.toString(), offset),
.op_class => constInstruction(instruction.toString(), chunk, offset),
.op_print => simpleInstruction(instruction.toString(), offset),
.op_constant => constInstruction(instruction.toString(), chunk, offset),
.op_false => simpleInstruction(instruction.toString(), offset),
.op_true => simpleInstruction(instruction.toString(), offset),
.op_pop => simpleInstruction(instruction.toString(), offset),
.op_duplicate => simpleInstruction(instruction.toString(), offset),
.op_define_global => constInstruction(instruction.toString(), chunk, offset),
.op_get_global => constInstruction(instruction.toString(), chunk, offset),
.op_set_global => constInstruction(instruction.toString(), chunk, offset),
.op_get_local => byteInstruction(instruction.toString(), chunk, offset),
.op_set_local => byteInstruction(instruction.toString(), chunk, offset),
.op_get_upvalue => byteInstruction(instruction.toString(), chunk, offset),
.op_set_upvalue => byteInstruction(instruction.toString(), chunk, offset),
.op_get_property => constInstruction(instruction.toString(), chunk, offset),
.op_set_property => constInstruction(instruction.toString(), chunk, offset),
.op_jump => jumpInstruction(instruction.toString(), 1, chunk, offset),
.op_jump_if_false => jumpInstruction(instruction.toString(), 1, chunk, offset),
.op_loop => jumpInstruction(instruction.toString(), -1, chunk, offset),
.op_call => byteInstruction(instruction.toString(), chunk, offset),
.op_closure => closureInstruction(instruction.toString(), chunk, offset),
.op_close_upvalue => simpleInstruction(instruction.toString(), offset),
.op_nil => simpleInstruction(instruction.toString(), offset),
.op_not => simpleInstruction(instruction.toString(), offset),
.op_greater => simpleInstruction(instruction.toString(), offset),
.op_less => simpleInstruction(instruction.toString(), offset),
.op_equal => simpleInstruction(instruction.toString(), offset),
.op_negate => simpleInstruction(instruction.toString(), offset),
.op_add => simpleInstruction(instruction.toString(), offset),
.op_subtract => simpleInstruction(instruction.toString(), offset),
.op_mult => simpleInstruction(instruction.toString(), offset),
.op_divide => simpleInstruction(instruction.toString(), offset),
.op_constant_long => longConstInstruction(instruction.toString(), chunk, offset),
};
}
pub fn simpleInstruction(name: []const u8, offset: usize) usize {
std.debug.print(" {s} \n", .{name});
return offset + 1;
}
pub fn longConstInstruction(name: []const u8, chunk: *Chunk, offset: usize) usize {
const constant: u32 = @as(u32, chunk.code.items[offset + 1]) |
@as(u32, (chunk.code.items[offset + 2])) << 8 |
@as(u32, (chunk.code.items[offset + 3])) << 16;
const stdout = std.io.getStdOut().writer();
// Use try to handle potential errors when printing the name
_ = stdout.print("{s}\n", .{name}) catch |err| {
std.debug.print("Failed to print name: {}\n", .{err});
};
const tag = @TypeOf(chunk.constants.items[constant]);
std.debug.print("Type: {}\n", .{tag});
printValue(chunk.constants.items[constant]);
// Call the Zig equivalent of printValue
//printValue(chunk.constants.items[constant]);
// Print the ending quote and newline
_ = stdout.print("'\n", .{}) catch |err| {
std.debug.print("Failed to print ending quote: {}\n", .{err});
};
return offset + 4;
}
fn closureInstruction(name: []const u8, chunk: *Chunk, initialOffset: usize) usize {
var offset = initialOffset + 1;
const constant = chunk.code.items[offset];
offset += 1;
std.debug.print("{s} {} ", .{ name, constant });
printValue(chunk.constants.items[constant]);
std.debug.print("\n", .{});
const function = chunk.constants.items[constant].obj.asFunction();
var i: usize = 0;
while (i < function.upvalueCount) : (i += 1) {
const isLocal = chunk.code.items[offset] != 1;
const valueType = if (isLocal) "local" else "upvalue";
offset += 1;
const index = chunk.code.items[offset];
offset += 1;
std.debug.print("{} | {s} {}\n", .{ offset - 2, valueType, index });
}
return offset;
}
fn jumpInstruction(name: []const u8, sign: isize, chunk: *Chunk, offset: usize) usize {
var jump = @as(u16, chunk.code.items[offset + 1]) << 8;
jump |= chunk.code.items[offset + 2];
const target = @as(isize, @intCast(offset)) + 3 + sign * @as(isize, @intCast(jump));
std.debug.print("{s} {d} -> {d}\n", .{ name, offset, target });
return offset + 3;
}
pub fn constInstruction(name: []const u8, chunk: *Chunk, offset: usize) usize {
const constant_idx = chunk.code.items[offset + 1];
const constant = chunk.constants.items[constant_idx];
printValue(constant);
std.debug.print("{s: <16} '{}'\n", .{ name, constant });
return offset + 2;
}
fn byteInstruction(name: []const u8, chunk: *Chunk, offset: usize) usize {
const slot = chunk.code.items[offset + 1];
std.debug.print("{s} {d} \n", .{ name, slot });
std.debug.print("\n", .{});
return offset + 2;
}
pub fn printStack(stack: []Value) void {
std.debug.print(" ", .{});
for (stack) |value| {
std.debug.print("[{}]", .{value});
}
std.debug.print("\n", .{});
}