Commit 4c1a62326b

Andrew Kelley <andrew@ziglang.org>
2021-12-04 01:33:20
stage2: use Target.Abi instead of introducing Target.TargetAbi
This branch introduced std.Target.TargetAbi when we already had std.Target.Abi which was, unsurprisingly, already suited for this task. Also pull out the -mabi= cc flag addition to the common area instead of duplicating it for assembly and c files.
1 parent 9ede943
lib/std/build.zig
@@ -1474,7 +1474,6 @@ pub const LibExeObjStep = struct {
     name_prefix: []const u8,
     filter: ?[]const u8,
     test_evented_io: bool = false,
-    target_abi: ?std.Target.TargetAbi = null,
     code_model: std.builtin.CodeModel = .default,
     wasi_exec_model: ?std.builtin.WasiExecModel = null,
 
@@ -2459,9 +2458,6 @@ pub const LibExeObjStep = struct {
             try zig_args.append(builder.fmt("--global-base={d}", .{global_base}));
         }
 
-        if (self.target_abi) |target_abi| {
-            try zig_args.append(builder.fmt("-mabi={s}", .{@tagName(target_abi)}));
-        }
         if (self.code_model != .default) {
             try zig_args.append("-mcmodel");
             try zig_args.append(@tagName(self.code_model));
lib/std/target.zig
@@ -560,84 +560,6 @@ pub const Target = struct {
         }
     };
 
-    /// processor specific ABI
-    pub const TargetAbi = enum {
-        //TODO add ARM, Mips, and PowerPC
-        ilp32,
-        ilp32d,
-        ilp32e,
-        ilp32f,
-        lp64,
-        lp64d,
-        lp64f,
-
-        const Riscv32ABI = struct {
-            fn default(features: Cpu.Feature.Set) TargetAbi {
-                if (riscv.featureSetHas(features, .d)) {
-                    return .ilp32d;
-                } else if (riscv.featureSetHas(features, .e)) {
-                    return .ilp32e;
-                } else {
-                    return .ilp32;
-                }
-            }
-            fn validate(target_abi: TargetAbi, features: Cpu.Feature.Set) ParseError!void {
-                const has_e = riscv.featureSetHas(features, .e);
-                const has_f = riscv.featureSetHas(features, .f);
-                const has_d = riscv.featureSetHas(features, .d);
-                return switch (target_abi) {
-                    .ilp32e => if (has_e) {} else error.FeatureAbiMismatch,
-                    .ilp32 => if (!has_e) {} else error.FeatureAbiMismatch,
-                    .ilp32f => if (!has_e and has_f) {} else error.FeatureAbiMismatch,
-                    .ilp32d => if (!has_e and has_d) {} else error.FeatureAbiMismatch,
-                    else => error.ArchAbiMismatch,
-                };
-            }
-        };
-        const Riscv64ABI = struct {
-            fn default(features: Cpu.Feature.Set) TargetAbi {
-                if (riscv.featureSetHas(features, .d)) {
-                    return .lp64d;
-                } else {
-                    return .lp64;
-                }
-            }
-            fn validate(target_abi: TargetAbi, features: Cpu.Feature.Set) ParseError!void {
-                const has_f = riscv.featureSetHas(features, .f);
-                const has_d = riscv.featureSetHas(features, .d);
-                return switch (target_abi) {
-                    .lp64 => {},
-                    .lp64f => if (has_f) {} else error.FeatureAbiMismatch,
-                    .lp64d => if (has_d) {} else error.FeatureAbiMismatch,
-                    else => error.ArchAbiMismatch,
-                };
-            }
-        };
-        pub fn default(arch: Cpu.Arch, features: Cpu.Feature.Set) ?TargetAbi {
-            return switch (arch) {
-                .riscv32 => Riscv32ABI.default(features),
-                .riscv64 => Riscv64ABI.default(features),
-                else => null,
-            };
-        }
-
-        pub const ParseError = error{
-            InvalidAbi,
-            ArchAbiMismatch,
-            FeatureAbiMismatch,
-        };
-        pub fn parse(cpu: Cpu, abi_string: []const u8) ParseError!TargetAbi {
-            const target_abi = std.meta.stringToEnum(TargetAbi, abi_string) orelse return error.InvalidAbi;
-
-            switch (cpu.arch) {
-                .riscv32 => try Riscv32ABI.validate(target_abi, cpu.features),
-                .riscv64 => try Riscv64ABI.validate(target_abi, cpu.features),
-                else => return error.ArchAbiMismatch,
-            }
-            return target_abi;
-        }
-    };
-
     pub const ObjectFormat = enum {
         /// Common Object File Format (Windows)
         coff,
src/codegen/llvm.zig
@@ -15,6 +15,7 @@ const TypedValue = @import("../TypedValue.zig");
 const Zir = @import("../Zir.zig");
 const Air = @import("../Air.zig");
 const Liveness = @import("../Liveness.zig");
+const target_util = @import("../target.zig");
 
 const Value = @import("../value.zig").Value;
 const Type = @import("../type.zig").Type;
@@ -244,8 +245,6 @@ pub const Object = struct {
         // TODO handle float ABI better- it should depend on the ABI portion of std.Target
         const float_abi: llvm.ABIType = .Default;
 
-        const abi_name: ?[*:0]const u8 = if (options.target_abi) |t| @tagName(t) else null;
-
         const target_machine = llvm.TargetMachine.create(
             target,
             llvm_target_triple.ptr,
@@ -256,7 +255,7 @@ pub const Object = struct {
             code_model,
             options.function_sections,
             float_abi,
-            abi_name,
+            if (target_util.llvmMachineAbi(options.target)) |s| s.ptr else null,
         );
         errdefer target_machine.dispose();
 
src/Compilation.zig
@@ -767,7 +767,6 @@ pub const InitOptions = struct {
     compatibility_version: ?std.builtin.Version = null,
     libc_installation: ?*const LibCInstallation = null,
     machine_code_model: std.builtin.CodeModel = .default,
-    target_abi: ?std.Target.TargetAbi,
     clang_preprocessor_mode: ClangPreprocessorMode = .no,
     /// This is for stage1 and should be deleted upon completion of self-hosting.
     color: @import("main.zig").Color = .auto,
@@ -1183,7 +1182,6 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
         cache.hash.add(options.target.os.getVersionRange());
         cache.hash.add(options.is_native_os);
         cache.hash.add(options.target.abi);
-        cache.hash.addOptionalBytes(if (options.target_abi) |t| @tagName(t) else null);
         cache.hash.add(ofmt);
         cache.hash.add(pic);
         cache.hash.add(pie);
@@ -1496,7 +1494,6 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
             .single_threaded = single_threaded,
             .verbose_link = options.verbose_link,
             .machine_code_model = options.machine_code_model,
-            .target_abi = options.target_abi,
             .dll_export_fns = dll_export_fns,
             .error_return_tracing = error_return_tracing,
             .llvm_cpu_features = llvm_cpu_features,
@@ -3460,9 +3457,6 @@ pub fn addCCArgs(
                 try argv.append("-mthumb");
             }
 
-            if (comp.bin_file.options.target_abi) |target_abi| {
-                try argv.append(try std.fmt.allocPrint(arena, "-mabi={s}", .{@tagName(target_abi)}));
-            }
             if (comp.sanitize_c and !comp.bin_file.options.tsan) {
                 try argv.append("-fsanitize=undefined");
                 try argv.append("-fsanitize-trap=undefined");
@@ -3595,11 +3589,6 @@ pub fn addCCArgs(
                     // TODO
                 },
             }
-
-            if (comp.bin_file.options.target_abi) |target_abi| {
-                try argv.append(try std.fmt.allocPrint(arena, "-mabi={s}", .{@tagName(target_abi)}));
-            }
-
             if (target_util.clangAssemblerSupportsMcpuArg(target)) {
                 if (target.cpu.model.llvm_name) |llvm_name| {
                     try argv.append(try std.fmt.allocPrint(arena, "-mcpu={s}", .{llvm_name}));
@@ -3607,6 +3596,11 @@ pub fn addCCArgs(
             }
         },
     }
+
+    if (target_util.llvmMachineAbi(target)) |mabi| {
+        try argv.append(try std.fmt.allocPrint(arena, "-mabi={s}", .{mabi}));
+    }
+
     if (out_dep_path) |p| {
         try argv.appendSlice(&[_][]const u8{ "-MD", "-MV", "-MF", p });
     }
@@ -4395,7 +4389,6 @@ fn buildOutputFromZig(
         .strip = comp.compilerRtStrip(),
         .is_native_os = comp.bin_file.options.is_native_os,
         .is_native_abi = comp.bin_file.options.is_native_abi,
-        .target_abi = comp.bin_file.options.target_abi,
         .self_exe_path = comp.self_exe_path,
         .verbose_cc = comp.verbose_cc,
         .verbose_link = comp.bin_file.options.verbose_link,
@@ -4584,7 +4577,7 @@ fn updateStage1Module(comp: *Compilation, main_progress_node: *std.Progress.Node
         .is_native_cpu = false, // Only true when bootstrapping the compiler.
         .llvm_cpu_name = if (target.cpu.model.llvm_name) |s| s.ptr else null,
         .llvm_cpu_features = comp.bin_file.options.llvm_cpu_features.?,
-        .llvm_target_abi = if (comp.bin_file.options.target_abi) |t| @tagName(t) else null,
+        .llvm_target_abi = if (target_util.llvmMachineAbi(target)) |s| s.ptr else null,
     };
 
     comp.stage1_cache_manifest = &man;
@@ -4836,7 +4829,6 @@ pub fn build_crt_file(
         .strip = comp.compilerRtStrip(),
         .is_native_os = comp.bin_file.options.is_native_os,
         .is_native_abi = comp.bin_file.options.is_native_abi,
-        .target_abi = comp.bin_file.options.target_abi,
         .self_exe_path = comp.self_exe_path,
         .c_source_files = c_source_files,
         .verbose_cc = comp.verbose_cc,
src/glibc.zig
@@ -961,7 +961,6 @@ fn buildSharedLib(
         .strip = comp.compilerRtStrip(),
         .is_native_os = false,
         .is_native_abi = false,
-        .target_abi = comp.bin_file.options.target_abi,
         .self_exe_path = comp.self_exe_path,
         .verbose_cc = comp.verbose_cc,
         .verbose_link = comp.bin_file.options.verbose_link,
src/libcxx.zig
@@ -200,7 +200,6 @@ pub fn buildLibCXX(comp: *Compilation) !void {
         .strip = comp.compilerRtStrip(),
         .is_native_os = comp.bin_file.options.is_native_os,
         .is_native_abi = comp.bin_file.options.is_native_abi,
-        .target_abi = comp.bin_file.options.target_abi,
         .self_exe_path = comp.self_exe_path,
         .c_source_files = c_source_files.items,
         .verbose_cc = comp.verbose_cc,
@@ -333,7 +332,6 @@ pub fn buildLibCXXABI(comp: *Compilation) !void {
         .strip = comp.compilerRtStrip(),
         .is_native_os = comp.bin_file.options.is_native_os,
         .is_native_abi = comp.bin_file.options.is_native_abi,
-        .target_abi = comp.bin_file.options.target_abi,
         .self_exe_path = comp.self_exe_path,
         .c_source_files = c_source_files.items,
         .verbose_cc = comp.verbose_cc,
src/libtsan.zig
@@ -218,7 +218,6 @@ pub fn buildTsan(comp: *Compilation) !void {
         .strip = comp.compilerRtStrip(),
         .is_native_os = comp.bin_file.options.is_native_os,
         .is_native_abi = comp.bin_file.options.is_native_abi,
-        .target_abi = comp.bin_file.options.target_abi,
         .self_exe_path = comp.self_exe_path,
         .c_source_files = c_source_files.items,
         .verbose_cc = comp.verbose_cc,
src/libunwind.zig
@@ -124,7 +124,6 @@ pub fn buildStaticLib(comp: *Compilation) !void {
         .strip = comp.compilerRtStrip(),
         .is_native_os = comp.bin_file.options.is_native_os,
         .is_native_abi = comp.bin_file.options.is_native_abi,
-        .target_abi = comp.bin_file.options.target_abi,
         .self_exe_path = comp.self_exe_path,
         .c_source_files = &c_source_files,
         .verbose_cc = comp.verbose_cc,
src/link.zig
@@ -134,7 +134,6 @@ pub const Options = struct {
     version_script: ?[]const u8,
     soname: ?[]const u8,
     llvm_cpu_features: ?[*:0]const u8,
-    target_abi: ?std.Target.TargetAbi,
 
     objects: []const []const u8,
     framework_dirs: []const []const u8,
src/main.zig
@@ -328,7 +328,6 @@ const usage_build_generic =
     \\Compile Options:
     \\  -target [name]            <arch><sub>-<os>-<abi> see the targets command
     \\  -mcpu [cpu]               Specify target CPU and feature set
-    \\  -mabi [target-abi]        Specify processor specific target-abi
     \\  -mcmodel=[default|tiny|   Limit range of code and data virtual addresses
     \\            small|kernel|
     \\            medium|large]
@@ -654,7 +653,6 @@ fn buildOutputType(
     var sysroot: ?[]const u8 = null;
     var libc_paths_file: ?[]const u8 = try optionalStringEnvVar(arena, "ZIG_LIBC");
     var machine_code_model: std.builtin.CodeModel = .default;
-    var target_abi_str: ?[]const u8 = null;
     var runtime_args_start: ?usize = null;
     var test_filter: ?[]const u8 = null;
     var test_name_prefix: ?[]const u8 = null;
@@ -928,12 +926,6 @@ fn buildOutputType(
                         target_mcpu = arg["-mcpu=".len..];
                     } else if (mem.startsWith(u8, arg, "-mcmodel=")) {
                         machine_code_model = parseCodeModel(arg["-mcmodel=".len..]);
-                    } else if (mem.eql(u8, arg, "-mabi")) {
-                        if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg});
-                        i += 1;
-                        target_abi_str = args[i];
-                    } else if (mem.startsWith(u8, arg, "-mabi=")) {
-                        target_abi_str = arg["-mabi=".len..];
                     } else if (mem.startsWith(u8, arg, "-O")) {
                         optimize_mode_string = arg["-O".len..];
                     } else if (mem.eql(u8, arg, "--dynamic-linker")) {
@@ -1880,12 +1872,6 @@ fn buildOutputType(
     const cross_target = try parseCrossTargetOrReportFatalError(arena, target_parse_options);
     const target_info = try detectNativeTargetInfo(gpa, cross_target);
 
-    const target_abi = if (target_abi_str) |s| std.Target.TargetAbi.parse(target_info.target.cpu, s) catch |err| switch (err) {
-        error.InvalidAbi => fatal("invalid target-abi value '{s}'", .{target_abi_str}),
-        error.ArchAbiMismatch => fatal("target-abi {s} is not valid for arch {s}", .{ target_abi_str, target_info.target.cpu.arch }),
-        error.FeatureAbiMismatch => fatal("target-abi {s} is not compatible with CPU features", .{target_abi_str}),
-    } else std.Target.TargetAbi.default(target_info.target.cpu.arch, target_info.target.cpu.features);
-
     if (target_info.target.os.tag != .freestanding) {
         if (ensure_libc_on_non_freestanding)
             link_libc = true;
@@ -2473,7 +2459,6 @@ fn buildOutputType(
         .verbose_cimport = verbose_cimport,
         .verbose_llvm_cpu_features = verbose_llvm_cpu_features,
         .machine_code_model = machine_code_model,
-        .target_abi = target_abi,
         .color = color,
         .time_report = time_report,
         .stack_report = stack_report,
@@ -3397,7 +3382,6 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
             .target = target_info.target,
             .is_native_os = cross_target.isNativeOs(),
             .is_native_abi = cross_target.isNativeAbi(),
-            .target_abi = std.Target.TargetAbi.default(target_info.target.cpu.arch, target_info.target.cpu.features),
             .dynamic_linker = target_info.dynamic_linker.get(),
             .output_mode = .Exe,
             .main_pkg = &main_pkg,
src/musl.zig
@@ -214,7 +214,6 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile) !void {
                 .strip = comp.compilerRtStrip(),
                 .is_native_os = false,
                 .is_native_abi = false,
-                .target_abi = comp.bin_file.options.target_abi,
                 .self_exe_path = comp.self_exe_path,
                 .verbose_cc = comp.verbose_cc,
                 .verbose_link = comp.bin_file.options.verbose_link,
src/target.zig
@@ -599,3 +599,38 @@ pub fn defaultAddressSpace(
     _ = context;
     return .generic;
 }
+
+pub fn llvmMachineAbi(target: std.Target) ?[:0]const u8 {
+    const have_float = switch (target.abi) {
+        .gnuilp32 => return "ilp32",
+        .gnueabihf, .musleabihf, .eabihf => true,
+        else => false,
+    };
+
+    switch (target.cpu.arch) {
+        .riscv64 => {
+            const featureSetHas = std.Target.riscv.featureSetHas;
+            if (featureSetHas(target.cpu.features, .d)) {
+                return "lp64d";
+            } else if (have_float) {
+                return "lp64f";
+            } else {
+                return "lp64";
+            }
+        },
+        .riscv32 => {
+            const featureSetHas = std.Target.riscv.featureSetHas;
+            if (featureSetHas(target.cpu.features, .d)) {
+                return "ilp32d";
+            } else if (have_float) {
+                return "ilp32f";
+            } else if (featureSetHas(target.cpu.features, .e)) {
+                return "ilp32e";
+            } else {
+                return "ilp32";
+            }
+        },
+        //TODO add ARM, Mips, and PowerPC
+        else => return null,
+    }
+}
src/test.zig
@@ -907,7 +907,6 @@ pub const TestContext = struct {
             .object_format = case.object_format,
             .is_native_os = case.target.isNativeOs(),
             .is_native_abi = case.target.isNativeAbi(),
-            .target_abi = std.Target.TargetAbi.default(target.cpu.arch, target.cpu.features),
             .dynamic_linker = target_info.dynamic_linker.get(),
             .link_libc = link_libc,
             .use_llvm = use_llvm,