From ec96095efd8671ae280df15eaf73f63bf029fbfa Mon Sep 17 00:00:00 2001 From: kcbanner Date: Thu, 13 Jul 2023 01:14:31 -0400 Subject: [PATCH] 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. --- src/Compilation.zig | 2 + src/target.zig | 2 +- test/standalone.zig | 4 +- .../build.zig | 22 ++++++-- .../shared_lib.c | 0 .../shared_lib_unwind.zig | 0 .../zig_unwind.zig | 52 +++++++++++++------ 7 files changed, 60 insertions(+), 22 deletions(-) rename test/standalone/{dwarf_unwinding => stack_iterator}/build.zig (70%) rename test/standalone/{dwarf_unwinding => stack_iterator}/shared_lib.c (100%) rename test/standalone/{dwarf_unwinding => stack_iterator}/shared_lib_unwind.zig (100%) rename test/standalone/{dwarf_unwinding => stack_iterator}/zig_unwind.zig (55%) diff --git a/src/Compilation.zig b/src/Compilation.zig index 383b60a66d..f6abfae00f 100644 --- a/src/Compilation.zig +++ b/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) { diff --git a/src/target.zig b/src/target.zig index a7af9aef22..030cad6bdc 100644 --- a/src/target.zig +++ b/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( diff --git a/test/standalone.zig b/test/standalone.zig index c9277e26a8..f52705f05f 100644 --- a/test/standalone.zig +++ b/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"), }, }; diff --git a/test/standalone/dwarf_unwinding/build.zig b/test/standalone/stack_iterator/build.zig similarity index 70% rename from test/standalone/dwarf_unwinding/build.zig rename to test/standalone/stack_iterator/build.zig index 885207f068..6a999aecff 100644 --- a/test/standalone/dwarf_unwinding/build.zig +++ b/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", diff --git a/test/standalone/dwarf_unwinding/shared_lib.c b/test/standalone/stack_iterator/shared_lib.c similarity index 100% rename from test/standalone/dwarf_unwinding/shared_lib.c rename to test/standalone/stack_iterator/shared_lib.c diff --git a/test/standalone/dwarf_unwinding/shared_lib_unwind.zig b/test/standalone/stack_iterator/shared_lib_unwind.zig similarity index 100% rename from test/standalone/dwarf_unwinding/shared_lib_unwind.zig rename to test/standalone/stack_iterator/shared_lib_unwind.zig diff --git a/test/standalone/dwarf_unwinding/zig_unwind.zig b/test/standalone/stack_iterator/zig_unwind.zig similarity index 55% rename from test/standalone/dwarf_unwinding/zig_unwind.zig rename to test/standalone/stack_iterator/zig_unwind.zig index 2d7e098eff..5421ac052f 100644 --- a/test/standalone/dwarf_unwinding/zig_unwind.zig +++ b/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 => {}, }