commit 164d01c1dc258068c23016ec2110e2b6080b8a2d (tree)
parent c824ce954ee81d438613ea7cf1e9a8111f730732
Author: Nathan Bourgeois <iridescentrosesfall@gmail.com>
Date: Sat, 21 Mar 2026 21:32:22 -0400
codegen: notraps on mips causes break and infinite loop on `@trap()`
Diffstat:
1 file changed, 19 insertions(+), 1 deletion(-)
diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig
@@ -9537,7 +9537,25 @@ pub const FuncGen = struct {
fn airTrap(self: *FuncGen, inst: Air.Inst.Index) !void {
_ = inst;
- _ = try self.wip.callIntrinsic(.normal, .none, .trap, &.{}, &.{}, "");
+ const target = self.ng.object.target;
+ if ((target.cpu.arch == .mips or target.cpu.arch == .mipsel) and
+ target.cpu.has(.mips, .notraps))
+ {
+ // Emit a MIPS `break` instruction followed by an infinite loop (to fulfill the noreturn)
+ // since this CPU does not support trap instructions.
+ const o = self.ng.object;
+ _ = try self.wip.callAsm(
+ .none,
+ try o.builder.fnType(.void, &.{}, .normal),
+ .{ .sideeffect = true },
+ try o.builder.string("break\n0:\nj 0b\nnop"),
+ try o.builder.string("~{memory}"),
+ &.{},
+ "",
+ );
+ } else {
+ _ = try self.wip.callIntrinsic(.normal, .none, .trap, &.{}, &.{}, "");
+ }
_ = try self.wip.@"unreachable"();
}