commit bba2e8132dd4f2290e356b68a2e41333007cc2df
parent e9bfcfebc59533b20754cf50764babf548e8e88d
Author: Walther Chen <walther.chen@gmail.com>
Date: Sat, 12 Nov 2022 21:38:21 -0500
add debug-ops build option, improve debug output formatting
Diffstat:
4 files changed, 29 insertions(+), 15 deletions(-)
diff --git a/build.zig b/build.zig
@@ -11,10 +11,14 @@ pub fn build(b: *std.build.Builder) void {
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
const mode = b.standardReleaseOptions();
- // Build option for tracing instructions
+ // Build options
+ const build_options_step = b.addOptions();
+
const trace_value = b.option(bool, "trace", "enable tracing instructions for interpreter") orelse false;
- const trace_step = b.addOptions();
- trace_step.addOption(bool, "TRACE", trace_value);
+ build_options_step.addOption(bool, "TRACE", trace_value);
+
+ const debug_ops_value = b.option(bool, "debug-ops", "enable debug output for ops") orelse false;
+ build_options_step.addOption(bool, "DEBUG_OPS", debug_ops_value);
const stages = [_][]const u8{
"og",
@@ -28,7 +32,7 @@ pub fn build(b: *std.build.Builder) void {
const exe = b.addExecutable(stage, "src/" ++ stage ++ ".zig");
exe.setTarget(target);
exe.setBuildMode(mode);
- exe.addOptions("build_with_trace", trace_step);
+ exe.addOptions("build_options", build_options_step);
exe.install();
const run_cmd = exe.run();
@@ -43,7 +47,7 @@ pub fn build(b: *std.build.Builder) void {
const exe_tests = b.addTest("src/" ++ stage ++ ".zig");
exe_tests.setTarget(target);
exe_tests.setBuildMode(mode);
- exe_tests.addOptions("build_with_trace", trace_step);
+ exe_tests.addOptions("build_options", build_options_step);
test_step.dependOn(&exe_tests.step);
}
}
diff --git a/justfile b/justfile
@@ -0,0 +1,3 @@
+benchmark:
+ zig build -Drelease-fast && \
+ hyperfine zig-out/bin/*
diff --git a/src/opt1.zig b/src/opt1.zig
@@ -7,8 +7,9 @@ const ArrayList = std.ArrayList;
const Program = common.Program;
const JumpTable = ArrayList(usize);
-// comptime known, from build option -Dtrace
-const TRACE = @import("build_with_trace").TRACE;
+// comptime known build option, -Dtrace and -Ddebug-opts
+const build_options = @import("build_options");
+const TRACE = build_options.TRACE;
pub fn main() anyerror!void {
if (TRACE) {
@@ -23,7 +24,7 @@ test "og: interpret hello world" {
}
fn interpret(program: Program, memory: []u8, rdr: anytype, wtr: anytype, alloc: Allocator) !void {
- comptime var instruction_count = if (TRACE) std.AutoHashMap(u8, usize).init(alloc) else undefined;
+ var instruction_count = if (TRACE) std.AutoHashMap(u8, usize).init(alloc) else undefined;
if (TRACE) {
defer instruction_count.deinit();
}
@@ -72,6 +73,7 @@ fn interpret(program: Program, memory: []u8, rdr: anytype, wtr: anytype, alloc:
if (TRACE) {
var kv = instruction_count.iterator();
+ std.debug.print("\n# Instruction Count\n", .{});
while (kv.next()) |entry| {
std.debug.print("{c}: {d}\n", .{ entry.key_ptr.*, entry.value_ptr.* });
}
diff --git a/src/opt2.zig b/src/opt2.zig
@@ -6,8 +6,10 @@ const Allocator = std.mem.Allocator;
const ArrayList = std.ArrayList;
const JumpTable = ArrayList(usize);
-// comptime known, from build option -Dtrace
-const TRACE = @import("build_with_trace").TRACE;
+// comptime known build option, -Dtrace and -Ddebug-opts
+const build_options = @import("build_options");
+const TRACE = build_options.TRACE;
+const DEBUG_OPS = build_options.DEBUG_OPS;
pub fn main() anyerror!void {
if (TRACE) {
@@ -71,8 +73,8 @@ const Op = struct {
};
};
-fn ops_debug(ops: []const Op) void {
- std.debug.print("\n", .{});
+fn opsDebug(ops: []const Op) void {
+ std.debug.print("\n# Ops\n", .{});
for (ops) |item, idx| {
std.debug.print("{d:0>2}: {any} {d}\n", .{ idx, item.tag, item.value });
}
@@ -165,7 +167,9 @@ fn parse(src: []const u8, alloc: Allocator) !Program {
});
}
- ops_debug(ops.items);
+ if (DEBUG_OPS) {
+ opsDebug(ops.items);
+ }
return .{ .ops = ops.toOwnedSlice(), .alloc = alloc };
}
@@ -238,7 +242,7 @@ test "opt2: parse basic 1" {
}
fn interpret(program: Program, memory: []u8, rdr: anytype, wtr: anytype, alloc: Allocator) !void {
- comptime var instruction_count = if (TRACE) std.AutoHashMap(Op.OpKind, usize).init(alloc) else undefined;
+ var instruction_count = if (TRACE) std.AutoHashMap(Op.Tag, usize).init(alloc) else undefined;
if (TRACE) {
defer instruction_count.deinit();
}
@@ -251,7 +255,7 @@ fn interpret(program: Program, memory: []u8, rdr: anytype, wtr: anytype, alloc:
const op = ops[pc];
if (TRACE) {
- var entry = try instruction_count.getOrPut(op);
+ var entry = try instruction_count.getOrPut(op.tag);
if (entry.found_existing) {
entry.value_ptr.* += 1;
} else {
@@ -294,6 +298,7 @@ fn interpret(program: Program, memory: []u8, rdr: anytype, wtr: anytype, alloc:
if (TRACE) {
var kv = instruction_count.iterator();
+ std.debug.print("\n# Ops Count\n", .{});
while (kv.next()) |entry| {
std.debug.print("{c}: {d}\n", .{ entry.key_ptr.*, entry.value_ptr.* });
}