Commit ec96095efd

kcbanner <kcbanner@gmail.com>
2023-07-13 07:14:31
compilation: pass omit_frame_pointer through to builtin.zig
Renamed dwarf_unwinding -> stack_iterator to better reflect that it's not just DWARF unwinding. Added a test for unwinding with a frame pointer.
1 parent 7d8b423
src/Compilation.zig
@@ -5288,6 +5288,7 @@ pub fn generateBuiltinZigSource(comp: *Compilation, allocator: Allocator) Alloca
         \\pub const position_independent_executable = {};
         \\pub const strip_debug_info = {};
         \\pub const code_model = std.builtin.CodeModel.{};
+        \\pub const omit_frame_pointer = {};
         \\
     , .{
         std.zig.fmtId(@tagName(target.ofmt)),
@@ -5301,6 +5302,7 @@ pub fn generateBuiltinZigSource(comp: *Compilation, allocator: Allocator) Alloca
         comp.bin_file.options.pie,
         comp.bin_file.options.strip,
         std.zig.fmtId(@tagName(comp.bin_file.options.machine_code_model)),
+        comp.bin_file.options.omit_frame_pointer,
     });
 
     if (target.os.tag == .wasi) {
src/target.zig
@@ -510,7 +510,7 @@ pub fn clangAssemblerSupportsMcpuArg(target: std.Target) bool {
 }
 
 pub fn needUnwindTables(target: std.Target) bool {
-    return target.os.tag == .windows or target.ofmt == .macho;
+    return target.os.tag == .windows or target.isDarwin();
 }
 
 pub fn defaultAddressSpace(
test/standalone/dwarf_unwinding/build.zig → test/standalone/stack_iterator/build.zig
@@ -7,10 +7,26 @@ pub fn build(b: *std.Build) void {
     const target = b.standardTargetOptions(.{});
     const optimize = b.standardOptimizeOption(.{});
 
-    // Test unwinding pure zig code (no libc)
+    // Unwinding pure zig code, with a frame pointer
     {
         const exe = b.addExecutable(.{
-            .name = "zig_unwind",
+            .name = "zig_unwind_fp",
+            .root_source_file = .{ .path = "zig_unwind.zig" },
+            .target = target,
+            .optimize = optimize,
+        });
+
+        if (target.isDarwin()) exe.unwind_tables = true;
+        exe.omit_frame_pointer = false;
+
+        const run_cmd = b.addRunArtifact(exe);
+        test_step.dependOn(&run_cmd.step);
+    }
+
+    // Unwinding pure zig code, without a frame pointer
+    {
+        const exe = b.addExecutable(.{
+            .name = "zig_unwind_nofp",
             .root_source_file = .{ .path = "zig_unwind.zig" },
             .target = target,
             .optimize = optimize,
@@ -23,7 +39,7 @@ pub fn build(b: *std.Build) void {
         test_step.dependOn(&run_cmd.step);
     }
 
-    // Test unwinding through a C shared library
+    // Unwinding through a C shared library without a frame pointer (libc)
     {
         const c_shared_lib = b.addSharedLibrary(.{
             .name = "c_shared_lib",
test/standalone/dwarf_unwinding/shared_lib.c → test/standalone/stack_iterator/shared_lib.c
File renamed without changes
test/standalone/dwarf_unwinding/shared_lib_unwind.zig → test/standalone/stack_iterator/shared_lib_unwind.zig
File renamed without changes
test/standalone/dwarf_unwinding/zig_unwind.zig → test/standalone/stack_iterator/zig_unwind.zig
@@ -23,24 +23,44 @@ noinline fn frame2(expected: *[4]usize, unwound: *[4]usize) void {
     if (builtin.target.ofmt != .c) {
         switch (builtin.cpu.arch) {
             .x86 => {
-                asm volatile (
-                    \\movl $3, %%ebx
-                    \\movl $1, %%ecx
-                    \\movl $2, %%edx
-                    \\movl $7, %%edi
-                    \\movl $6, %%esi
-                    \\movl $5, %%ebp
-                    ::: "ebx", "ecx", "edx", "edi", "esi", "ebp");
+                if (builtin.omit_frame_pointer) {
+                    asm volatile (
+                        \\movl $3, %%ebx
+                        \\movl $1, %%ecx
+                        \\movl $2, %%edx
+                        \\movl $7, %%edi
+                        \\movl $6, %%esi
+                        \\movl $5, %%ebp
+                        ::: "ebx", "ecx", "edx", "edi", "esi", "ebp");
+                } else {
+                    asm volatile (
+                        \\movl $3, %%ebx
+                        \\movl $1, %%ecx
+                        \\movl $2, %%edx
+                        \\movl $7, %%edi
+                        \\movl $6, %%esi
+                        ::: "ebx", "ecx", "edx", "edi", "esi");
+                }
             },
             .x86_64 => {
-                asm volatile (
-                    \\movq $3, %%rbx
-                    \\movq $12, %%r12
-                    \\movq $13, %%r13
-                    \\movq $14, %%r14
-                    \\movq $15, %%r15
-                    \\movq $6, %%rbp
-                    ::: "rbx", "r12", "r13", "r14", "r15", "rbp");
+                if (builtin.omit_frame_pointer) {
+                    asm volatile (
+                        \\movq $3, %%rbx
+                        \\movq $12, %%r12
+                        \\movq $13, %%r13
+                        \\movq $14, %%r14
+                        \\movq $15, %%r15
+                        \\movq $6, %%rbp
+                        ::: "rbx", "r12", "r13", "r14", "r15", "rbp");
+                } else {
+                    asm volatile (
+                        \\movq $3, %%rbx
+                        \\movq $12, %%r12
+                        \\movq $13, %%r13
+                        \\movq $14, %%r14
+                        \\movq $15, %%r15
+                        ::: "rbx", "r12", "r13", "r14", "r15");
+                }
             },
             else => {},
         }
test/standalone.zig
@@ -231,8 +231,8 @@ pub const build_cases = [_]BuildCase{
         .import = @import("standalone/zerolength_check/build.zig"),
     },
     .{
-        .build_root = "test/standalone/dwarf_unwinding",
-        .import = @import("standalone/dwarf_unwinding/build.zig"),
+        .build_root = "test/standalone/stack_iterator",
+        .import = @import("standalone/stack_iterator/build.zig"),
     },
 };