Skip to content

Instantly share code, notes, and snippets.

@NilPtrDeref
Created May 31, 2024 02:31
Show Gist options
  • Save NilPtrDeref/2177bb523299254ae9216bfade26e23e to your computer and use it in GitHub Desktop.
Save NilPtrDeref/2177bb523299254ae9216bfade26e23e to your computer and use it in GitHub Desktop.
Experiment on zig compile-time checks (Conditional code in debug mode)
// Results from `https://godbolt.org/` with Zig 0.12.0 compiler.
const std = @import("std");
const builtin = @import("builtin");
// Check happens at comptime
export fn t1() void {
comptime var dbg: bool = false;
dbg = builtin.mode != std.builtin.Mode.Debug;
if (dbg) {
std.debug.print("Test", .{});
}
}
// t1:
// push rbp
// mov rbp, rsp
// pop rbp
// ret
// Check happens at runtime
export fn t2() void {
const dbg = builtin.mode != std.builtin.Mode.Debug;
if (dbg) {
std.debug.print("Test", .{});
}
}
// t2:
// push rbp
// mov rbp, rsp
// sub rsp, 1
// mov byte ptr [rbp - 1], 0
// add rsp, 1
// pop rbp
// ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment