Conversation
If I could mark a builtin function as cold, I would mark @setCold as cold.
We have run out of `Zir.Inst.Tag`s so I had to move a tag from Zir.Inst.Tag to
Zir.Inst.Extended. This is because a new noreturn builtin will be added and
noreturn builtins cannot be part of Inst.Tag:
```
/// `noreturn` instructions may not go here; they must be part of the main `Tag` enum.
pub const Extended = enum(u16) {
```
Here's another reason I went for @setCold:
```
$ git grep setRuntimeSafety | wc -l
322
$ git grep setCold | wc -l
79
$ git grep setEvalBranchQuota | wc -l
82
```
This also simply removes @setCold from Autodoc and the docs frontend because
as far as I could understand it, builtins represented using Zir extended
instructions are not yet supported because I couldn't find
@setStackAlign or @setFloatMode there, either.
This should improve the developer debugging experience.
| #elif defined(__i386__) || defined(__x86_64__) | ||
| #define zig_trap() __asm__ volatile("ud2"); | ||
| #else | ||
| #define zig_trap() raise(SIGILL) |
There was a problem hiding this comment.
| #define zig_trap() raise(SIGILL) | |
| #define zig_trap() raise(SIGTRAP) |
There was a problem hiding this comment.
The references I quoted and linked in this comment might be of interest.
The SIGTRAP signal is sent to a process when an exception (or trap) occurs: a condition that a debugger has requested to be informed of
(they talk about debuggers in SIGTRAP so this is used for debugtrap below)
and then SIGILL (used for trap):
The SIGILL signal is sent to a process when it attempts to execute an illegal, malformed, unknown, or privileged instruction
I know the signal names themselves make this seem like an odd choice but that's why I did it this way.
| Unlike for {#syntax#}@breakpoint(){#endsyntax#}, execution does not continue after this point. | ||
| </p> | ||
| <p> | ||
| This function is only valid within function scope. |
There was a problem hiding this comment.
| This function is only valid within function scope. | |
| Outside function scope, this builtin causes a compile error. |
There was a problem hiding this comment.
Hmm, I don't think it does? I didn't check for that but I'm guessing it has or should have the same behavior as @breakpoint in that regard:
$ cat x.zig
const std = @import("std");
const x = @breakpoint();
test {
std.debug.print("{}\n", .{x});
}
$ zig test x.zig
Test [1/1] test_0... void
All 1 tests passed.
So I don't know if that should be a compile error, but maybe because it returns noreturn instead of void. So, should it not be valid within test scope either? Not sure.
There was a problem hiding this comment.
my suggested docs are correct; the compiler is wrong :-)
This introduces a new
@trapbuiltin, which is very similar to@breakpointin that both cause some kind of exception/interrupt usually. The code that the two builtins compile down to may equally stop the program entirely, but not always. Usually@breakpointwould allow the debugger to resume the program after inspections into the program's state have finished, whereas@trapdoes not really have anything to do with debuggers and is just there to exit the program in some way, but in an abnormal way.Zig's
@trap= LLVM'sllvm.trap= Rust'sstd::instrinsics::abortZig's
@breakpoint= LLVM'sllvm.debugtrap= Rust'sstd::instrinsics::breakpointFor example, here's what I imagine the two builtins to compile down to on the 6502:
@trap-> JAM (link doesn't work if you don't have "Undocumented opcodes" checked at the top of the page)@breakpoint-> BRKChecklist
zig run:Illegal instruction (core dumped))wasm2wat)wasm2wat)zig run:qemu: uncaught target signal 4 (Illegal instruction) - core dumpedfollowed byIllegal instruction (core dumped))zig run:qemu: uncaught target signal 5 (Trace/breakpoint trap) - core dumpedfollowed byTrace/breakpoint trap (core dumped)(same for@breakpoint))zig run:Illegal instruction (core dumped))We get some very elaborate output (same for
@breakpoint):Unhandled trap: 0x105 pc: 0000000008000004 npc: 0000000008000008 %g0-3: 0000000000000000 0000000000000000 0000000000000000 0000000000000000 %g4-7: 0000000000000000 0000000000000000 0000000000000000 0000000000000000 %o0-3: 0000000000000000 0000000000000000 0000000000000000 0000000000000000 %o4-7: 0000000000000000 0000000000000000 00000040020000f1 0000000000000000 %l0-3: 0000000000000000 0000000000000000 0000000000000000 0000000000000000 %l4-7: 0000000000000000 0000000000000000 0000000000000000 0000000000000000 %i0-3: 0000000000000000 0000000000000000 0000000000000000 0000000000000000 %i4-7: 0000000000000000 0000000000000000 00000040020001a1 0000000000000000 pstate: 00000092 ccr: 00 (icc: ---- xcc: ----) asi: 82 tl: 0 pil: 0 gl: 0 tbr: 0000000000000000 hpstate: 0000000000000000 htba: 0000000000000000 cansave: 5 canrestore: 1 otherwin: 0 wstate: 0 cleanwin: 6 cwp: 0 fsr: 0000000000000000 y: 0000000000000000 fprs: 0000000000000000call coldcc void @llvm.trap(), !dbg !2275)@breakpointand@trap; make sure people understand.@trapin the std (done:@trapfixups #14799)Fixes #14765