Created
May 31, 2024 02:31
-
-
Save NilPtrDeref/2177bb523299254ae9216bfade26e23e to your computer and use it in GitHub Desktop.
Experiment on zig compile-time checks (Conditional code in debug mode)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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