Skip to content

Instantly share code, notes, and snippets.

@mikdusan
Last active April 13, 2020 02:06
Show Gist options
  • Save mikdusan/f6b52669b5eb7971b94e37fb772f5848 to your computer and use it in GitHub Desktop.
Save mikdusan/f6b52669b5eb7971b94e37fb772f5848 to your computer and use it in GitHub Desktop.
IR fun
export fn entry() void {
comptime {
var aaa: ?u64 = 333;
var answer: u64 = undefined;
answer = aaa.?;
}
}
pub fn panic(msg: []const u8, error_return_trace: ?*@import("builtin").StackTrace) noreturn {
while (true) {}
}
@mikdusan
Copy link
Author

zig build-obj -fno-emit-h -fno-emit-bin unwrap.zig --verbose-ir |& less

...
~ #38 | SrcVarPtr             | (unknown)   | 1 | &aaa
-> #18 | GenAlloca             | *?u64       | 0 | Alloca(align=0,name=aaa)
~ /Users/mike/project/zig/work/main/unwrap.zig:5:21
~ #39 | SrcOptionalUnwrapPtr  | (unknown)   | 1 | &#38.*.?
-> #47 | GenConst              | *u64        | 0 | *333

@mikdusan
Copy link
Author

Advanced IR Debugging 

Andrew added native-debug helper functions for the Zig compiler.
Print triplet of (source:line:col) by calling member function src() for types IrExecutable{Src,Gen}, AstNode, IrInst, IrInst{Src,Gen} .

Dump IR segment by calling member function dump() for types IrExecutable{Src,Gen}, AstNode, IrInst, IrInst{Src,Gen} .

Dump ZigValue type-as-string by calling member function Dump .

When --verbose-ir is enabled, call dbg_ir_break(src_file_zig, line) to breakpoint inside ir_analyze() .

Call dbg_ir_clear() to clear all breakpoints.

@mikdusan
Copy link
Author

image

image

image

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment