execution tracing

This commit is contained in:
Patrick MARIE 2024-08-24 18:49:14 +02:00
parent 2a365287a8
commit 1cb8c95fe9
3 changed files with 19 additions and 3 deletions

View File

@ -61,6 +61,7 @@ pub const Chunk = struct {
while (offset < self.count) { while (offset < self.count) {
offset += self.dissassemble_instruction(offset); offset += self.dissassemble_instruction(offset);
} }
debug.print("== end of {s} ==\n\n", .{name});
} }
pub fn dissassemble_instruction(self: Chunk, offset: usize) usize { pub fn dissassemble_instruction(self: Chunk, offset: usize) usize {

View File

@ -6,6 +6,8 @@ const Chunk = @import("./chunk.zig").Chunk;
const OpCode = @import("./opcode.zig").OpCode; const OpCode = @import("./opcode.zig").OpCode;
const VM = @import("./vm.zig").VM; const VM = @import("./vm.zig").VM;
pub const DEBUG_TRACE_EXECUTION = true;
pub fn main() !void { pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{ .safety = true }){}; var gpa = std.heap.GeneralPurposeAllocator(.{ .safety = true }){};
defer _ = debug.assert(gpa.deinit() == .ok); defer _ = debug.assert(gpa.deinit() == .ok);
@ -16,10 +18,10 @@ pub fn main() !void {
var chunk = Chunk.new(); var chunk = Chunk.new();
try chunk.init(allocator); try chunk.init(allocator);
try chunk.write(allocator, @intFromEnum(OpCode.OP_RETURN), 123);
const constant = try chunk.add_constant(allocator, 1.2); const constant = try chunk.add_constant(allocator, 1.2);
try chunk.write(allocator, @intFromEnum(OpCode.OP_CONSTANT), 123); try chunk.write(allocator, @intFromEnum(OpCode.OP_CONSTANT), 123);
try chunk.write(allocator, @intCast(constant), 123); try chunk.write(allocator, @intCast(constant), 123);
try chunk.write(allocator, @intFromEnum(OpCode.OP_RETURN), 123);
chunk.dissassemble("test chunk"); chunk.dissassemble("test chunk");

View File

@ -3,6 +3,8 @@ const Chunk = @import("./chunk.zig").Chunk;
const OpCode = @import("./opcode.zig").OpCode; const OpCode = @import("./opcode.zig").OpCode;
const Value = @import("./values.zig").Value; const Value = @import("./values.zig").Value;
const DEBUG_TRACE_EXECUTION = @import("./main.zig").DEBUG_TRACE_EXECUTION;
const print_value = @import("./values.zig").print_value; const print_value = @import("./values.zig").print_value;
const InterpretResult = enum { const InterpretResult = enum {
@ -35,15 +37,25 @@ pub const VM = struct {
pub fn run(self: *VM) InterpretResult { pub fn run(self: *VM) InterpretResult {
while (true) { while (true) {
if (DEBUG_TRACE_EXECUTION) {
_ = self.chunk.?.dissassemble_instruction(self.ip.?);
}
const instruction = self.read_byte(); const instruction = self.read_byte();
switch (instruction) { switch (instruction) {
@intFromEnum(OpCode.OP_CONSTANT) => { @intFromEnum(OpCode.OP_CONSTANT) => {
const constant = self.read_constant(); const constant = self.read_constant();
// XXX Those should not be std.debug, but stdout.
print_value(constant); print_value(constant);
std.debug.print("\n", .{});
}, },
@intFromEnum(OpCode.OP_RETURN) => return InterpretResult.OK, @intFromEnum(OpCode.OP_RETURN) => return InterpretResult.OK,
else => return InterpretResult.RUNTIME_ERROR, else => {
std.debug.print("Invalid instruction: {d}\n", .{instruction});
return InterpretResult.RUNTIME_ERROR;
},
} }
} }
@ -53,9 +65,10 @@ pub const VM = struct {
// XXX In the book, we're using a ptr to data directly, to avoid dereferencing to a given offset // XXX In the book, we're using a ptr to data directly, to avoid dereferencing to a given offset
// How to do that in Zig? // How to do that in Zig?
pub fn read_byte(self: *VM) u8 { pub fn read_byte(self: *VM) u8 {
const ret = self.chunk.?.code[self.ip.?];
self.ip.? += 1; self.ip.? += 1;
return self.chunk.?.code[self.ip.?]; return ret;
} }
pub fn read_constant(self: *VM) Value { pub fn read_constant(self: *VM) Value {