authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-02-20 18:31:17-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-02-20 18:31:17-05:00
log0f016b368de2ac93a298ab771646931329062fb5
treea605eb2971c749b1dd983c3cec16c25fedb96ab2
parent33c69d5cb6fd555e164dd92e5b9dcfac37d82672
signaturelock-open Commit is signed but in an unrecognized format.

support -mcpu=baseline, both in stage1 and stage2

See e381a42de9c0f0c5439a926b0ac99026a0373f49 for more details. This is set up so that if we wish to make "baseline" depend on the OS in the future, it is possible to do that.

6 files changed, 44 insertions(+), 33 deletions(-)

lib/std/target.zig+18-14
...@@ -726,8 +726,7 @@ pub const Target = union(enum) {...@@ -726,8 +726,7 @@ pub const Target = union(enum) {
726 /// Looks like "name+a+b-c-d+e", where "name" is a CPU Model name, "a", "b", and "e"726 /// Looks like "name+a+b-c-d+e", where "name" is a CPU Model name, "a", "b", and "e"
727 /// are examples of CPU features to add to the set, and "c" and "d" are examples of CPU features727 /// are examples of CPU features to add to the set, and "c" and "d" are examples of CPU features
728 /// to remove from the set.728 /// to remove from the set.
729 /// The default value of `null` means to use the "baseline" feature set.729 cpu_features: []const u8 = "baseline",
730 cpu: ?[]const u8 = null,
731 };730 };
732731
733 pub fn parse(args: ParseOptions) !Target {732 pub fn parse(args: ParseOptions) !Target {
...@@ -742,23 +741,29 @@ pub const Target = union(enum) {...@@ -742,23 +741,29 @@ pub const Target = union(enum) {
742 const abi = if (abi_name) |n| try Abi.parse(n) else Abi.default(arch, os);741 const abi = if (abi_name) |n| try Abi.parse(n) else Abi.default(arch, os);
743742
744 const all_features = arch.allFeaturesList();743 const all_features = arch.allFeaturesList();
745 const cpu: Cpu = if (args.cpu) |cpu_text| blk: {744 var index: usize = 0;
746 var index: usize = 0;745 while (index < args.cpu_features.len and
747 while (index < cpu_text.len and cpu_text[index] != '+' and cpu_text[index] != '-') {746 args.cpu_features[index] != '+' and
748 index += 1;747 args.cpu_features[index] != '-')
749 }748 {
750 const cpu_name = cpu_text[0..index];749 index += 1;
750 }
751 const cpu_name = args.cpu_features[0..index];
752 const cpu: Cpu = if (mem.eql(u8, cpu_name, "baseline")) Cpu.baseline(arch) else blk: {
751 const cpu_model = try arch.parseCpuModel(cpu_name);753 const cpu_model = try arch.parseCpuModel(cpu_name);
752754
753 var set = cpu_model.features;755 var set = cpu_model.features;
754 while (index < cpu_text.len) {756 while (index < args.cpu_features.len) {
755 const op = cpu_text[index];757 const op = args.cpu_features[index];
756 index += 1;758 index += 1;
757 const start = index;759 const start = index;
758 while (index < cpu_text.len and cpu_text[index] != '+' and cpu_text[index] != '-') {760 while (index < args.cpu_features.len and
761 args.cpu_features[index] != '+' and
762 args.cpu_features[index] != '-')
763 {
759 index += 1;764 index += 1;
760 }765 }
761 const feature_name = cpu_text[start..index];766 const feature_name = args.cpu_features[start..index];
762 for (all_features) |feature, feat_index_usize| {767 for (all_features) |feature, feat_index_usize| {
763 const feat_index = @intCast(Cpu.Feature.Set.Index, feat_index_usize);768 const feat_index = @intCast(Cpu.Feature.Set.Index, feat_index_usize);
764 if (mem.eql(u8, feature_name, feature.name)) {769 if (mem.eql(u8, feature_name, feature.name)) {
...@@ -779,8 +784,7 @@ pub const Target = union(enum) {...@@ -779,8 +784,7 @@ pub const Target = union(enum) {
779 .model = cpu_model,784 .model = cpu_model,
780 .features = set,785 .features = set,
781 };786 };
782 } else Cpu.baseline(arch);787 };
783
784 var cross = Cross{788 var cross = Cross{
785 .cpu = cpu,789 .cpu = cpu,
786 .os = os,790 .os = os,
src-self-hosted/stage2.zig+2-2
...@@ -675,8 +675,8 @@ fn stage2TargetParse(...@@ -675,8 +675,8 @@ fn stage2TargetParse(
675) !void {675) !void {
676 const target: Target = if (zig_triple_oz) |zig_triple_z| blk: {676 const target: Target = if (zig_triple_oz) |zig_triple_z| blk: {
677 const zig_triple = mem.toSliceConst(u8, zig_triple_z);677 const zig_triple = mem.toSliceConst(u8, zig_triple_z);
678 const mcpu = if (mcpu_oz) |mcpu_z| mem.toSliceConst(u8, mcpu_z) else null;678 const mcpu = if (mcpu_oz) |mcpu_z| mem.toSliceConst(u8, mcpu_z) else "baseline";
679 break :blk try Target.parse(.{ .arch_os_abi = zig_triple, .cpu = mcpu });679 break :blk try Target.parse(.{ .arch_os_abi = zig_triple, .cpu_features = mcpu });
680 } else Target.Native;680 } else Target.Native;
681681
682 try stage1_target.fromTarget(target);682 try stage1_target.fromTarget(target);
src/stage2.cpp+18-9
...@@ -97,10 +97,20 @@ Error stage2_target_parse(struct ZigTarget *target, const char *zig_triple, cons...@@ -97,10 +97,20 @@ Error stage2_target_parse(struct ZigTarget *target, const char *zig_triple, cons
97 if (zig_triple == nullptr) {97 if (zig_triple == nullptr) {
98 get_native_target(target);98 get_native_target(target);
9999
100 target->llvm_cpu_name = ZigLLVMGetHostCPUName();100 if (mcpu == nullptr) {
101 target->llvm_cpu_features = ZigLLVMGetNativeFeatures();101 target->llvm_cpu_name = ZigLLVMGetHostCPUName();
102 target->builtin_str = "Target.Cpu.baseline(arch);\n";102 target->llvm_cpu_features = ZigLLVMGetNativeFeatures();
103 target->cache_hash = "native\n\n";103 target->builtin_str = "Target.Cpu.baseline(arch);\n";
104 target->cache_hash = "native\n\n";
105 } else if (strcmp(mcpu, "baseline") == 0) {
106 target->llvm_cpu_name = "";
107 target->llvm_cpu_features = "";
108 target->builtin_str = "Target.Cpu.baseline(arch);\n";
109 target->cache_hash = "baseline\n\n";
110 } else {
111 const char *msg = "stage0 can't handle CPU/features in the target";
112 stage2_panic(msg, strlen(msg));
113 }
104 } else {114 } else {
105 // first initialize all to zero115 // first initialize all to zero
106 *target = {};116 *target = {};
...@@ -135,15 +145,14 @@ Error stage2_target_parse(struct ZigTarget *target, const char *zig_triple, cons...@@ -135,15 +145,14 @@ Error stage2_target_parse(struct ZigTarget *target, const char *zig_triple, cons
135 target->abi = target_default_abi(target->arch, target->os);145 target->abi = target_default_abi(target->arch, target->os);
136 }146 }
137147
148 if (mcpu != nullptr && strcmp(mcpu, "baseline") != 0) {
149 const char *msg = "stage0 can't handle CPU/features in the target";
150 stage2_panic(msg, strlen(msg));
151 }
138 target->builtin_str = "Target.Cpu.baseline(arch);\n";152 target->builtin_str = "Target.Cpu.baseline(arch);\n";
139 target->cache_hash = "\n\n";153 target->cache_hash = "\n\n";
140 }154 }
141155
142 if (mcpu != nullptr) {
143 const char *msg = "stage0 can't handle CPU/features in the target";
144 stage2_panic(msg, strlen(msg));
145 }
146
147 return ErrorNone;156 return ErrorNone;
148}157}
149158
test/compile_errors.zig+1-2
...@@ -350,8 +350,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -350,8 +350,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
350 });350 });
351 tc.target = Target{351 tc.target = Target{
352 .Cross = .{352 .Cross = .{
353 .arch = .wasm32,353 .cpu = Target.Cpu.baseline(.wasm32),
354 .cpu_features = Target.Arch.wasm32.getBaselineCpuFeatures(),
355 .os = .wasi,354 .os = .wasi,
356 .abi = .none,355 .abi = .none,
357 },356 },
test/tests.zig+4-4
...@@ -135,13 +135,13 @@ const test_targets = blk: {...@@ -135,13 +135,13 @@ const test_targets = blk: {
135 TestTarget{135 TestTarget{
136 .target = Target.parse(.{136 .target = Target.parse(.{
137 .arch_os_abi = "arm-linux-none",137 .arch_os_abi = "arm-linux-none",
138 .cpu = "generic+v8a",138 .cpu_features = "generic+v8a",
139 }) catch unreachable,139 }) catch unreachable,
140 },140 },
141 TestTarget{141 TestTarget{
142 .target = Target.parse(.{142 .target = Target.parse(.{
143 .arch_os_abi = "arm-linux-musleabihf",143 .arch_os_abi = "arm-linux-musleabihf",
144 .cpu = "generic+v8a",144 .cpu_features = "generic+v8a",
145 }) catch unreachable,145 }) catch unreachable,
146 .link_libc = true,146 .link_libc = true,
147 },147 },
...@@ -149,7 +149,7 @@ const test_targets = blk: {...@@ -149,7 +149,7 @@ const test_targets = blk: {
149 //TestTarget{149 //TestTarget{
150 // .target = Target.parse(.{150 // .target = Target.parse(.{
151 // .arch_os_abi = "arm-linux-gnueabihf",151 // .arch_os_abi = "arm-linux-gnueabihf",
152 // .cpu = "generic+v8a",152 // .cpu_features = "generic+v8a",
153 // }) catch unreachable,153 // }) catch unreachable,
154 // .link_libc = true,154 // .link_libc = true,
155 //},155 //},
...@@ -449,7 +449,7 @@ pub fn addPkgTests(...@@ -449,7 +449,7 @@ pub fn addPkgTests(
449 const ArchTag = @TagType(builtin.Arch);449 const ArchTag = @TagType(builtin.Arch);
450 if (test_target.disable_native and450 if (test_target.disable_native and
451 test_target.target.getOs() == builtin.os and451 test_target.target.getOs() == builtin.os and
452 @as(ArchTag, test_target.target.getArch()) == @as(ArchTag, builtin.arch))452 test_target.target.getArch() == builtin.arch)
453 {453 {
454 continue;454 continue;
455 }455 }
test/translate_c.zig+1-2
...@@ -1095,10 +1095,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -1095,10 +1095,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
10951095
1096 cases.addWithTarget("Calling convention", tests.Target{1096 cases.addWithTarget("Calling convention", tests.Target{
1097 .Cross = .{1097 .Cross = .{
1098 .cpu = Target.Cpu.baseline(.i386),
1098 .os = .linux,1099 .os = .linux,
1099 .arch = .i386,
1100 .abi = .none,1100 .abi = .none,
1101 .cpu_features = Target.Arch.i386.getBaselineCpuFeatures(),
1102 },1101 },
1103 },1102 },
1104 \\void __attribute__((fastcall)) foo1(float *a);1103 \\void __attribute__((fastcall)) foo1(float *a);