authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-12-03 17:33:20-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-12-03 17:33:20-07:00
log4c1a62326b5090fad67c004a6e58a6e4265a77ad
tree922d865e78b10012f773c292795b9d7479894bdd
parent9ede943e0753b250cb9a60a5b9b66ba9bff4a694

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.

13 files changed, 43 insertions(+), 123 deletions(-)

lib/std/build.zig-4
......@@ -1474,7 +1474,6 @@ pub const LibExeObjStep = struct {
14741474 name_prefix: []const u8,
14751475 filter: ?[]const u8,
14761476 test_evented_io: bool = false,
1477 target_abi: ?std.Target.TargetAbi = null,
14781477 code_model: std.builtin.CodeModel = .default,
14791478 wasi_exec_model: ?std.builtin.WasiExecModel = null,
14801479
......@@ -2459,9 +2458,6 @@ pub const LibExeObjStep = struct {
24592458 try zig_args.append(builder.fmt("--global-base={d}", .{global_base}));
24602459 }
24612460
2462 if (self.target_abi) |target_abi| {
2463 try zig_args.append(builder.fmt("-mabi={s}", .{@tagName(target_abi)}));
2464 }
24652461 if (self.code_model != .default) {
24662462 try zig_args.append("-mcmodel");
24672463 try zig_args.append(@tagName(self.code_model));
lib/std/target.zig-78
......@@ -560,84 +560,6 @@ pub const Target = struct {
560560 }
561561 };
562562
563 /// processor specific ABI
564 pub const TargetAbi = enum {
565 //TODO add ARM, Mips, and PowerPC
566 ilp32,
567 ilp32d,
568 ilp32e,
569 ilp32f,
570 lp64,
571 lp64d,
572 lp64f,
573
574 const Riscv32ABI = struct {
575 fn default(features: Cpu.Feature.Set) TargetAbi {
576 if (riscv.featureSetHas(features, .d)) {
577 return .ilp32d;
578 } else if (riscv.featureSetHas(features, .e)) {
579 return .ilp32e;
580 } else {
581 return .ilp32;
582 }
583 }
584 fn validate(target_abi: TargetAbi, features: Cpu.Feature.Set) ParseError!void {
585 const has_e = riscv.featureSetHas(features, .e);
586 const has_f = riscv.featureSetHas(features, .f);
587 const has_d = riscv.featureSetHas(features, .d);
588 return switch (target_abi) {
589 .ilp32e => if (has_e) {} else error.FeatureAbiMismatch,
590 .ilp32 => if (!has_e) {} else error.FeatureAbiMismatch,
591 .ilp32f => if (!has_e and has_f) {} else error.FeatureAbiMismatch,
592 .ilp32d => if (!has_e and has_d) {} else error.FeatureAbiMismatch,
593 else => error.ArchAbiMismatch,
594 };
595 }
596 };
597 const Riscv64ABI = struct {
598 fn default(features: Cpu.Feature.Set) TargetAbi {
599 if (riscv.featureSetHas(features, .d)) {
600 return .lp64d;
601 } else {
602 return .lp64;
603 }
604 }
605 fn validate(target_abi: TargetAbi, features: Cpu.Feature.Set) ParseError!void {
606 const has_f = riscv.featureSetHas(features, .f);
607 const has_d = riscv.featureSetHas(features, .d);
608 return switch (target_abi) {
609 .lp64 => {},
610 .lp64f => if (has_f) {} else error.FeatureAbiMismatch,
611 .lp64d => if (has_d) {} else error.FeatureAbiMismatch,
612 else => error.ArchAbiMismatch,
613 };
614 }
615 };
616 pub fn default(arch: Cpu.Arch, features: Cpu.Feature.Set) ?TargetAbi {
617 return switch (arch) {
618 .riscv32 => Riscv32ABI.default(features),
619 .riscv64 => Riscv64ABI.default(features),
620 else => null,
621 };
622 }
623
624 pub const ParseError = error{
625 InvalidAbi,
626 ArchAbiMismatch,
627 FeatureAbiMismatch,
628 };
629 pub fn parse(cpu: Cpu, abi_string: []const u8) ParseError!TargetAbi {
630 const target_abi = std.meta.stringToEnum(TargetAbi, abi_string) orelse return error.InvalidAbi;
631
632 switch (cpu.arch) {
633 .riscv32 => try Riscv32ABI.validate(target_abi, cpu.features),
634 .riscv64 => try Riscv64ABI.validate(target_abi, cpu.features),
635 else => return error.ArchAbiMismatch,
636 }
637 return target_abi;
638 }
639 };
640
641563 pub const ObjectFormat = enum {
642564 /// Common Object File Format (Windows)
643565 coff,
src/Compilation.zig+6-14
......@@ -767,7 +767,6 @@ pub const InitOptions = struct {
767767 compatibility_version: ?std.builtin.Version = null,
768768 libc_installation: ?*const LibCInstallation = null,
769769 machine_code_model: std.builtin.CodeModel = .default,
770 target_abi: ?std.Target.TargetAbi,
771770 clang_preprocessor_mode: ClangPreprocessorMode = .no,
772771 /// This is for stage1 and should be deleted upon completion of self-hosting.
773772 color: @import("main.zig").Color = .auto,
......@@ -1183,7 +1182,6 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
11831182 cache.hash.add(options.target.os.getVersionRange());
11841183 cache.hash.add(options.is_native_os);
11851184 cache.hash.add(options.target.abi);
1186 cache.hash.addOptionalBytes(if (options.target_abi) |t| @tagName(t) else null);
11871185 cache.hash.add(ofmt);
11881186 cache.hash.add(pic);
11891187 cache.hash.add(pie);
......@@ -1496,7 +1494,6 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
14961494 .single_threaded = single_threaded,
14971495 .verbose_link = options.verbose_link,
14981496 .machine_code_model = options.machine_code_model,
1499 .target_abi = options.target_abi,
15001497 .dll_export_fns = dll_export_fns,
15011498 .error_return_tracing = error_return_tracing,
15021499 .llvm_cpu_features = llvm_cpu_features,
......@@ -3460,9 +3457,6 @@ pub fn addCCArgs(
34603457 try argv.append("-mthumb");
34613458 }
34623459
3463 if (comp.bin_file.options.target_abi) |target_abi| {
3464 try argv.append(try std.fmt.allocPrint(arena, "-mabi={s}", .{@tagName(target_abi)}));
3465 }
34663460 if (comp.sanitize_c and !comp.bin_file.options.tsan) {
34673461 try argv.append("-fsanitize=undefined");
34683462 try argv.append("-fsanitize-trap=undefined");
......@@ -3595,11 +3589,6 @@ pub fn addCCArgs(
35953589 // TODO
35963590 },
35973591 }
3598
3599 if (comp.bin_file.options.target_abi) |target_abi| {
3600 try argv.append(try std.fmt.allocPrint(arena, "-mabi={s}", .{@tagName(target_abi)}));
3601 }
3602
36033592 if (target_util.clangAssemblerSupportsMcpuArg(target)) {
36043593 if (target.cpu.model.llvm_name) |llvm_name| {
36053594 try argv.append(try std.fmt.allocPrint(arena, "-mcpu={s}", .{llvm_name}));
......@@ -3607,6 +3596,11 @@ pub fn addCCArgs(
36073596 }
36083597 },
36093598 }
3599
3600 if (target_util.llvmMachineAbi(target)) |mabi| {
3601 try argv.append(try std.fmt.allocPrint(arena, "-mabi={s}", .{mabi}));
3602 }
3603
36103604 if (out_dep_path) |p| {
36113605 try argv.appendSlice(&[_][]const u8{ "-MD", "-MV", "-MF", p });
36123606 }
......@@ -4395,7 +4389,6 @@ fn buildOutputFromZig(
43954389 .strip = comp.compilerRtStrip(),
43964390 .is_native_os = comp.bin_file.options.is_native_os,
43974391 .is_native_abi = comp.bin_file.options.is_native_abi,
4398 .target_abi = comp.bin_file.options.target_abi,
43994392 .self_exe_path = comp.self_exe_path,
44004393 .verbose_cc = comp.verbose_cc,
44014394 .verbose_link = comp.bin_file.options.verbose_link,
......@@ -4584,7 +4577,7 @@ fn updateStage1Module(comp: *Compilation, main_progress_node: *std.Progress.Node
45844577 .is_native_cpu = false, // Only true when bootstrapping the compiler.
45854578 .llvm_cpu_name = if (target.cpu.model.llvm_name) |s| s.ptr else null,
45864579 .llvm_cpu_features = comp.bin_file.options.llvm_cpu_features.?,
4587 .llvm_target_abi = if (comp.bin_file.options.target_abi) |t| @tagName(t) else null,
4580 .llvm_target_abi = if (target_util.llvmMachineAbi(target)) |s| s.ptr else null,
45884581 };
45894582
45904583 comp.stage1_cache_manifest = &man;
......@@ -4836,7 +4829,6 @@ pub fn build_crt_file(
48364829 .strip = comp.compilerRtStrip(),
48374830 .is_native_os = comp.bin_file.options.is_native_os,
48384831 .is_native_abi = comp.bin_file.options.is_native_abi,
4839 .target_abi = comp.bin_file.options.target_abi,
48404832 .self_exe_path = comp.self_exe_path,
48414833 .c_source_files = c_source_files,
48424834 .verbose_cc = comp.verbose_cc,
src/codegen/llvm.zig+2-3
......@@ -15,6 +15,7 @@ const TypedValue = @import("../TypedValue.zig");
1515const Zir = @import("../Zir.zig");
1616const Air = @import("../Air.zig");
1717const Liveness = @import("../Liveness.zig");
18const target_util = @import("../target.zig");
1819
1920const Value = @import("../value.zig").Value;
2021const Type = @import("../type.zig").Type;
......@@ -244,8 +245,6 @@ pub const Object = struct {
244245 // TODO handle float ABI better- it should depend on the ABI portion of std.Target
245246 const float_abi: llvm.ABIType = .Default;
246247
247 const abi_name: ?[*:0]const u8 = if (options.target_abi) |t| @tagName(t) else null;
248
249248 const target_machine = llvm.TargetMachine.create(
250249 target,
251250 llvm_target_triple.ptr,
......@@ -256,7 +255,7 @@ pub const Object = struct {
256255 code_model,
257256 options.function_sections,
258257 float_abi,
259 abi_name,
258 if (target_util.llvmMachineAbi(options.target)) |s| s.ptr else null,
260259 );
261260 errdefer target_machine.dispose();
262261
src/glibc.zig-1
......@@ -961,7 +961,6 @@ fn buildSharedLib(
961961 .strip = comp.compilerRtStrip(),
962962 .is_native_os = false,
963963 .is_native_abi = false,
964 .target_abi = comp.bin_file.options.target_abi,
965964 .self_exe_path = comp.self_exe_path,
966965 .verbose_cc = comp.verbose_cc,
967966 .verbose_link = comp.bin_file.options.verbose_link,
src/libcxx.zig-2
......@@ -200,7 +200,6 @@ pub fn buildLibCXX(comp: *Compilation) !void {
200200 .strip = comp.compilerRtStrip(),
201201 .is_native_os = comp.bin_file.options.is_native_os,
202202 .is_native_abi = comp.bin_file.options.is_native_abi,
203 .target_abi = comp.bin_file.options.target_abi,
204203 .self_exe_path = comp.self_exe_path,
205204 .c_source_files = c_source_files.items,
206205 .verbose_cc = comp.verbose_cc,
......@@ -333,7 +332,6 @@ pub fn buildLibCXXABI(comp: *Compilation) !void {
333332 .strip = comp.compilerRtStrip(),
334333 .is_native_os = comp.bin_file.options.is_native_os,
335334 .is_native_abi = comp.bin_file.options.is_native_abi,
336 .target_abi = comp.bin_file.options.target_abi,
337335 .self_exe_path = comp.self_exe_path,
338336 .c_source_files = c_source_files.items,
339337 .verbose_cc = comp.verbose_cc,
src/libtsan.zig-1
......@@ -218,7 +218,6 @@ pub fn buildTsan(comp: *Compilation) !void {
218218 .strip = comp.compilerRtStrip(),
219219 .is_native_os = comp.bin_file.options.is_native_os,
220220 .is_native_abi = comp.bin_file.options.is_native_abi,
221 .target_abi = comp.bin_file.options.target_abi,
222221 .self_exe_path = comp.self_exe_path,
223222 .c_source_files = c_source_files.items,
224223 .verbose_cc = comp.verbose_cc,
src/libunwind.zig-1
......@@ -124,7 +124,6 @@ pub fn buildStaticLib(comp: *Compilation) !void {
124124 .strip = comp.compilerRtStrip(),
125125 .is_native_os = comp.bin_file.options.is_native_os,
126126 .is_native_abi = comp.bin_file.options.is_native_abi,
127 .target_abi = comp.bin_file.options.target_abi,
128127 .self_exe_path = comp.self_exe_path,
129128 .c_source_files = &c_source_files,
130129 .verbose_cc = comp.verbose_cc,
src/link.zig-1
......@@ -134,7 +134,6 @@ pub const Options = struct {
134134 version_script: ?[]const u8,
135135 soname: ?[]const u8,
136136 llvm_cpu_features: ?[*:0]const u8,
137 target_abi: ?std.Target.TargetAbi,
138137
139138 objects: []const []const u8,
140139 framework_dirs: []const []const u8,
src/main.zig-16
......@@ -328,7 +328,6 @@ const usage_build_generic =
328328 \\Compile Options:
329329 \\ -target [name] <arch><sub>-<os>-<abi> see the targets command
330330 \\ -mcpu [cpu] Specify target CPU and feature set
331 \\ -mabi [target-abi] Specify processor specific target-abi
332331 \\ -mcmodel=[default|tiny| Limit range of code and data virtual addresses
333332 \\ small|kernel|
334333 \\ medium|large]
......@@ -654,7 +653,6 @@ fn buildOutputType(
654653 var sysroot: ?[]const u8 = null;
655654 var libc_paths_file: ?[]const u8 = try optionalStringEnvVar(arena, "ZIG_LIBC");
656655 var machine_code_model: std.builtin.CodeModel = .default;
657 var target_abi_str: ?[]const u8 = null;
658656 var runtime_args_start: ?usize = null;
659657 var test_filter: ?[]const u8 = null;
660658 var test_name_prefix: ?[]const u8 = null;
......@@ -928,12 +926,6 @@ fn buildOutputType(
928926 target_mcpu = arg["-mcpu=".len..];
929927 } else if (mem.startsWith(u8, arg, "-mcmodel=")) {
930928 machine_code_model = parseCodeModel(arg["-mcmodel=".len..]);
931 } else if (mem.eql(u8, arg, "-mabi")) {
932 if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg});
933 i += 1;
934 target_abi_str = args[i];
935 } else if (mem.startsWith(u8, arg, "-mabi=")) {
936 target_abi_str = arg["-mabi=".len..];
937929 } else if (mem.startsWith(u8, arg, "-O")) {
938930 optimize_mode_string = arg["-O".len..];
939931 } else if (mem.eql(u8, arg, "--dynamic-linker")) {
......@@ -1880,12 +1872,6 @@ fn buildOutputType(
18801872 const cross_target = try parseCrossTargetOrReportFatalError(arena, target_parse_options);
18811873 const target_info = try detectNativeTargetInfo(gpa, cross_target);
18821874
1883 const target_abi = if (target_abi_str) |s| std.Target.TargetAbi.parse(target_info.target.cpu, s) catch |err| switch (err) {
1884 error.InvalidAbi => fatal("invalid target-abi value '{s}'", .{target_abi_str}),
1885 error.ArchAbiMismatch => fatal("target-abi {s} is not valid for arch {s}", .{ target_abi_str, target_info.target.cpu.arch }),
1886 error.FeatureAbiMismatch => fatal("target-abi {s} is not compatible with CPU features", .{target_abi_str}),
1887 } else std.Target.TargetAbi.default(target_info.target.cpu.arch, target_info.target.cpu.features);
1888
18891875 if (target_info.target.os.tag != .freestanding) {
18901876 if (ensure_libc_on_non_freestanding)
18911877 link_libc = true;
......@@ -2473,7 +2459,6 @@ fn buildOutputType(
24732459 .verbose_cimport = verbose_cimport,
24742460 .verbose_llvm_cpu_features = verbose_llvm_cpu_features,
24752461 .machine_code_model = machine_code_model,
2476 .target_abi = target_abi,
24772462 .color = color,
24782463 .time_report = time_report,
24792464 .stack_report = stack_report,
......@@ -3397,7 +3382,6 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
33973382 .target = target_info.target,
33983383 .is_native_os = cross_target.isNativeOs(),
33993384 .is_native_abi = cross_target.isNativeAbi(),
3400 .target_abi = std.Target.TargetAbi.default(target_info.target.cpu.arch, target_info.target.cpu.features),
34013385 .dynamic_linker = target_info.dynamic_linker.get(),
34023386 .output_mode = .Exe,
34033387 .main_pkg = &main_pkg,
src/musl.zig-1
......@@ -214,7 +214,6 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile) !void {
214214 .strip = comp.compilerRtStrip(),
215215 .is_native_os = false,
216216 .is_native_abi = false,
217 .target_abi = comp.bin_file.options.target_abi,
218217 .self_exe_path = comp.self_exe_path,
219218 .verbose_cc = comp.verbose_cc,
220219 .verbose_link = comp.bin_file.options.verbose_link,
src/target.zig+35
......@@ -599,3 +599,38 @@ pub fn defaultAddressSpace(
599599 _ = context;
600600 return .generic;
601601}
602
603pub fn llvmMachineAbi(target: std.Target) ?[:0]const u8 {
604 const have_float = switch (target.abi) {
605 .gnuilp32 => return "ilp32",
606 .gnueabihf, .musleabihf, .eabihf => true,
607 else => false,
608 };
609
610 switch (target.cpu.arch) {
611 .riscv64 => {
612 const featureSetHas = std.Target.riscv.featureSetHas;
613 if (featureSetHas(target.cpu.features, .d)) {
614 return "lp64d";
615 } else if (have_float) {
616 return "lp64f";
617 } else {
618 return "lp64";
619 }
620 },
621 .riscv32 => {
622 const featureSetHas = std.Target.riscv.featureSetHas;
623 if (featureSetHas(target.cpu.features, .d)) {
624 return "ilp32d";
625 } else if (have_float) {
626 return "ilp32f";
627 } else if (featureSetHas(target.cpu.features, .e)) {
628 return "ilp32e";
629 } else {
630 return "ilp32";
631 }
632 },
633 //TODO add ARM, Mips, and PowerPC
634 else => return null,
635 }
636}
src/test.zig-1
......@@ -907,7 +907,6 @@ pub const TestContext = struct {
907907 .object_format = case.object_format,
908908 .is_native_os = case.target.isNativeOs(),
909909 .is_native_abi = case.target.isNativeAbi(),
910 .target_abi = std.Target.TargetAbi.default(target.cpu.arch, target.cpu.features),
911910 .dynamic_linker = target_info.dynamic_linker.get(),
912911 .link_libc = link_libc,
913912 .use_llvm = use_llvm,