Commit 084e92879a

mlugg <mlugg@mlugg.co.uk>
2025-09-18 16:52:02
std: don't get CPU context when using CBE targeting MSVC
Calling `current` here causes compilation failures as the C backend currently does not emit valid MSVC inline assembly. This change means that when building for MSVC with the self-hosted C backend, only FP unwinding can be used.
1 parent dae703d
Changed files (2)
lib/std/debug/cpu_context.zig
@@ -316,7 +316,7 @@ pub const X86_64 = struct {
             \\movq %%r15, 0x78(%%rdi)
             \\leaq (%%rip), %%rax
             \\movq %%rax, 0x80(%%rdi)
-            \\movq 0x00(%%rdi), %%rax  // restore saved rax
+            \\movq 0x00(%%rdi), %%rax
             :
             : [gprs] "{rdi}" (&ctx.gprs.values),
             : .{ .memory = true });
@@ -431,7 +431,7 @@ pub const Aarch64 = extern struct {
             \\str x1, [x0, #0x0f8]
             \\adr x1, .
             \\str x1, [x0, #0x100]
-            \\ldr x1, [x0, #0x008] // restore saved x1
+            \\ldr x1, [x0, #0x008]
             :
             : [gprs] "{x0}" (&ctx),
             : .{ .memory = true });
lib/std/debug.zig
@@ -756,7 +756,13 @@ const StackIterator = union(enum) {
             // Use `di_first` here so we report the PC in the context before unwinding any further.
             return .{ .di_first = .init(context_ptr) };
         }
-        if (SelfInfo.supports_unwinding and cpu_context.Native != noreturn) {
+        // Workaround the C backend being unable to use inline assembly on MSVC by disabling the
+        // call to `current`. This effectively constrains stack trace collection and dumping to FP
+        // unwinding when building with CBE for MSVC.
+        if (!(builtin.zig_backend == .stage2_c and builtin.target.abi == .msvc) and
+            SelfInfo.supports_unwinding and
+            cpu_context.Native != noreturn)
+        {
             // We don't need `di_first` here, because our PC is in `std.debug`; we're only interested
             // in our caller's frame and above.
             return .{ .di = .init(&.current()) };