authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-01-21 01:22:37-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-01-21 01:22:37-05:00
log39759b90fce2fa114f59793958390778275c0477
tree281c89dbe1c2dca8498ec5ef342a7ec47b7e035b
parente640d015351c3d3bf7c41020ff2a87f38f853140
signaturelock-open Commit is signed but in an unrecognized format.

make zig targets show native cpu name and features


6 files changed, 101 insertions(+), 57 deletions(-)

lib/std/target.zig+2-2
...@@ -631,9 +631,9 @@ pub const Target = union(enum) {...@@ -631,9 +631,9 @@ pub const Target = union(enum) {
631 };631 };
632 }632 }
633633
634 pub fn cpuFeaturesList(self: Target) []const *const Cpu.Feature {634 pub fn cpuFeatureSet(self: Target) Cpu.Feature.Set {
635 return switch (self.getCpuFeatures()) {635 return switch (self.getCpuFeatures()) {
636 .baseline => self.arch.baselineFeatures(),636 .baseline => self.getArch().baselineFeatures(),
637 .cpu => |cpu| cpu.features,637 .cpu => |cpu| cpu.features,
638 .features => |features| features,638 .features => |features| features,
639 };639 };
src-self-hosted/print_targets.zig+18-5
...@@ -9,6 +9,7 @@ pub fn cmdTargets(...@@ -9,6 +9,7 @@ pub fn cmdTargets(
9 allocator: *Allocator,9 allocator: *Allocator,
10 args: []const []const u8,10 args: []const []const u8,
11 stdout: *io.OutStream(fs.File.WriteError),11 stdout: *io.OutStream(fs.File.WriteError),
12 native_target: Target,
12) !void {13) !void {
13 const BOS = io.BufferedOutStream(fs.File.WriteError);14 const BOS = io.BufferedOutStream(fs.File.WriteError);
14 var bos = BOS.init(stdout);15 var bos = BOS.init(stdout);
...@@ -76,22 +77,34 @@ pub fn cmdTargets(...@@ -76,22 +77,34 @@ pub fn cmdTargets(
76 try jws.objectField("native");77 try jws.objectField("native");
77 try jws.beginObject();78 try jws.beginObject();
78 {79 {
79 const triple = try Target.current.zigTriple(allocator);80 const triple = try native_target.zigTriple(allocator);
80 defer allocator.free(triple);81 defer allocator.free(triple);
81 try jws.objectField("triple");82 try jws.objectField("triple");
82 try jws.emitString(triple);83 try jws.emitString(triple);
83 }84 }
84 try jws.objectField("arch");85 try jws.objectField("arch");
85 try jws.emitString(@tagName(Target.current.getArch()));86 try jws.emitString(@tagName(native_target.getArch()));
86 try jws.objectField("os");87 try jws.objectField("os");
87 try jws.emitString(@tagName(Target.current.getOs()));88 try jws.emitString(@tagName(native_target.getOs()));
88 try jws.objectField("abi");89 try jws.objectField("abi");
89 try jws.emitString(@tagName(Target.current.getAbi()));90 try jws.emitString(@tagName(native_target.getAbi()));
90 try jws.objectField("cpuName");91 try jws.objectField("cpuName");
91 switch (Target.current.getCpuFeatures()) {92 switch (native_target.getCpuFeatures()) {
92 .baseline, .features => try jws.emitNull(),93 .baseline, .features => try jws.emitNull(),
93 .cpu => |cpu| try jws.emitString(cpu.name),94 .cpu => |cpu| try jws.emitString(cpu.name),
94 }95 }
96 {
97 try jws.objectField("cpuFeatures");
98 try jws.beginArray();
99 const feature_set = native_target.cpuFeatureSet();
100 for (native_target.getArch().allFeaturesList()) |feature, i| {
101 if (feature_set.isEnabled(@intCast(u8, i))) {
102 try jws.arrayElem();
103 try jws.emitString(feature.name);
104 }
105 }
106 try jws.endArray();
107 }
95 try jws.endObject();108 try jws.endObject();
96109
97 try jws.endObject();110 try jws.endObject();
src-self-hosted/stage1.zig+78-47
...@@ -539,19 +539,82 @@ export fn stage2_progress_update_node(node: *std.Progress.Node, done_count: usiz...@@ -539,19 +539,82 @@ export fn stage2_progress_update_node(node: *std.Progress.Node, done_count: usiz
539 node.context.maybeRefresh();539 node.context.maybeRefresh();
540}540}
541541
542fn cpuFeaturesFromLLVM(
543 arch: Target.Arch,
544 llvm_cpu_name_z: ?[*:0]const u8,
545 llvm_cpu_features_opt: ?[*:0]const u8,
546) !Target.CpuFeatures {
547 if (llvm_cpu_name_z) |cpu_name_z| {
548 const llvm_cpu_name = mem.toSliceConst(u8, cpu_name_z);
549
550 for (arch.allCpus()) |cpu| {
551 const this_llvm_name = cpu.llvm_name orelse continue;
552 if (mem.eql(u8, this_llvm_name, llvm_cpu_name)) {
553 return Target.CpuFeatures{ .cpu = cpu };
554 }
555 }
556 }
557
558 var set = arch.baselineFeatures();
559 const llvm_cpu_features = llvm_cpu_features_opt orelse return Target.CpuFeatures{
560 .features = set,
561 };
562
563 var it = mem.tokenize(mem.toSliceConst(u8, llvm_cpu_features), ",");
564 while (it.next()) |decorated_llvm_feat| {
565 var op: enum {
566 add,
567 sub,
568 } = undefined;
569 var llvm_feat: []const u8 = undefined;
570 if (mem.startsWith(u8, decorated_llvm_feat, "+")) {
571 op = .add;
572 llvm_feat = decorated_llvm_feat[1..];
573 } else if (mem.startsWith(u8, decorated_llvm_feat, "-")) {
574 op = .sub;
575 llvm_feat = decorated_llvm_feat[1..];
576 } else {
577 return error.InvalidLlvmCpuFeaturesFormat;
578 }
579 for (arch.allFeaturesList()) |feature, index| {
580 const this_llvm_name = feature.llvm_name orelse continue;
581 if (mem.eql(u8, llvm_feat, this_llvm_name)) {
582 switch (op) {
583 .add => set.addFeature(@intCast(u8, index)),
584 .sub => set.removeFeature(@intCast(u8, index)),
585 }
586 break;
587 }
588 }
589 }
590 return Target.CpuFeatures{ .features = set };
591}
592
542// ABI warning593// ABI warning
543export fn stage2_cmd_targets() c_int {594export fn stage2_cmd_targets(zig_triple: [*:0]const u8) c_int {
544 @import("print_targets.zig").cmdTargets(595 cmdTargets(zig_triple) catch |err| {
545 std.heap.c_allocator,
546 &[0][]u8{},
547 &std.io.getStdOut().outStream().stream,
548 ) catch |err| {
549 std.debug.warn("unable to list targets: {}\n", .{@errorName(err)});596 std.debug.warn("unable to list targets: {}\n", .{@errorName(err)});
550 return -1;597 return -1;
551 };598 };
552 return 0;599 return 0;
553}600}
554601
602fn cmdTargets(zig_triple: [*:0]const u8) !void {
603 var target = try Target.parse(mem.toSliceConst(u8, zig_triple));
604 target.Cross.cpu_features = blk: {
605 const llvm = @import("llvm.zig");
606 const llvm_cpu_name = llvm.GetHostCPUName();
607 const llvm_cpu_features = llvm.GetNativeFeatures();
608 break :blk try cpuFeaturesFromLLVM(target.Cross.arch, llvm_cpu_name, llvm_cpu_features);
609 };
610 return @import("print_targets.zig").cmdTargets(
611 std.heap.c_allocator,
612 &[0][]u8{},
613 &std.io.getStdOut().outStream().stream,
614 target,
615 );
616}
617
555const Stage2CpuFeatures = struct {618const Stage2CpuFeatures = struct {
556 allocator: *mem.Allocator,619 allocator: *mem.Allocator,
557 cpu_features: Target.CpuFeatures,620 cpu_features: Target.CpuFeatures,
...@@ -588,49 +651,17 @@ const Stage2CpuFeatures = struct {...@@ -588,49 +651,17 @@ const Stage2CpuFeatures = struct {
588 fn createFromLLVM(651 fn createFromLLVM(
589 allocator: *mem.Allocator,652 allocator: *mem.Allocator,
590 zig_triple: [*:0]const u8,653 zig_triple: [*:0]const u8,
591 llvm_cpu_name_z: [*:0]const u8,654 llvm_cpu_name_z: ?[*:0]const u8,
592 llvm_cpu_features: [*:0]const u8,655 llvm_cpu_features: ?[*:0]const u8,
593 ) !*Self {656 ) !*Self {
594 const target = try Target.parse(mem.toSliceConst(u8, zig_triple));657 const target = try Target.parse(mem.toSliceConst(u8, zig_triple));
595 const arch = target.Cross.arch;658 const arch = target.Cross.arch;
596 const llvm_cpu_name = mem.toSliceConst(u8, llvm_cpu_name_z);659 const cpu_features = try cpuFeaturesFromLLVM(arch, llvm_cpu_name_z, llvm_cpu_features);
597660 switch (cpu_features) {
598 for (arch.allCpus()) |cpu| {661 .baseline => return createBaseline(allocator),
599 const this_llvm_name = cpu.llvm_name orelse continue;662 .cpu => |cpu| return createFromCpu(allocator, arch, cpu),
600 if (mem.eql(u8, this_llvm_name, llvm_cpu_name)) {663 .features => |features| return createFromCpuFeatures(allocator, arch, features),
601 return createFromCpu(allocator, arch, cpu);
602 }
603 }
604
605 var set = arch.baselineFeatures();
606 var it = mem.tokenize(mem.toSliceConst(u8, llvm_cpu_features), ",");
607 while (it.next()) |decorated_llvm_feat| {
608 var op: enum {
609 add,
610 sub,
611 } = undefined;
612 var llvm_feat: []const u8 = undefined;
613 if (mem.startsWith(u8, decorated_llvm_feat, "+")) {
614 op = .add;
615 llvm_feat = decorated_llvm_feat[1..];
616 } else if (mem.startsWith(u8, decorated_llvm_feat, "-")) {
617 op = .sub;
618 llvm_feat = decorated_llvm_feat[1..];
619 } else {
620 return error.InvalidLlvmCpuFeaturesFormat;
621 }
622 for (arch.allFeaturesList()) |feature, index| {
623 const this_llvm_name = feature.llvm_name orelse continue;
624 if (mem.eql(u8, llvm_feat, this_llvm_name)) {
625 switch (op) {
626 .add => set.addFeature(@intCast(u8, index)),
627 .sub => set.removeFeature(@intCast(u8, index)),
628 }
629 break;
630 }
631 }
632 }664 }
633 return createFromCpuFeatures(allocator, arch, set);
634 }665 }
635666
636 fn createFromCpu(allocator: *mem.Allocator, arch: Target.Arch, cpu: *const Target.Cpu) !*Self {667 fn createFromCpu(allocator: *mem.Allocator, arch: Target.Arch, cpu: *const Target.Cpu) !*Self {
...@@ -823,8 +854,8 @@ export fn stage2_cpu_features_baseline(result: **Stage2CpuFeatures) Error {...@@ -823,8 +854,8 @@ export fn stage2_cpu_features_baseline(result: **Stage2CpuFeatures) Error {
823export fn stage2_cpu_features_llvm(854export fn stage2_cpu_features_llvm(
824 result: **Stage2CpuFeatures,855 result: **Stage2CpuFeatures,
825 zig_triple: [*:0]const u8,856 zig_triple: [*:0]const u8,
826 llvm_cpu_name: [*:0]const u8,857 llvm_cpu_name: ?[*:0]const u8,
827 llvm_cpu_features: [*:0]const u8,858 llvm_cpu_features: ?[*:0]const u8,
828) Error {859) Error {
829 result.* = Stage2CpuFeatures.createFromLLVM(860 result.* = Stage2CpuFeatures.createFromLLVM(
830 std.heap.c_allocator,861 std.heap.c_allocator,
src/main.cpp+1-1
...@@ -1372,7 +1372,7 @@ int main(int argc, char **argv) {...@@ -1372,7 +1372,7 @@ int main(int argc, char **argv) {
1372 return main_exit(root_progress_node, EXIT_SUCCESS);1372 return main_exit(root_progress_node, EXIT_SUCCESS);
1373 }1373 }
1374 case CmdTargets:1374 case CmdTargets:
1375 return stage2_cmd_targets();1375 return stage2_cmd_targets(buf_ptr(&zig_triple_buf));
1376 case CmdNone:1376 case CmdNone:
1377 return print_full_usage(arg0, stderr, EXIT_FAILURE);1377 return print_full_usage(arg0, stderr, EXIT_FAILURE);
1378 }1378 }
src/userland.cpp+1-1
...@@ -141,7 +141,7 @@ void stage2_cpu_features_get_builtin_str(const Stage2CpuFeatures *cpu_features,...@@ -141,7 +141,7 @@ void stage2_cpu_features_get_builtin_str(const Stage2CpuFeatures *cpu_features,
141 *len = strlen(cpu_features->builtin_str);141 *len = strlen(cpu_features->builtin_str);
142}142}
143143
144int stage2_cmd_targets(void) {144int stage2_cmd_targets(const char *zig_triple) {
145 const char *msg = "stage0 called stage2_cmd_targets";145 const char *msg = "stage0 called stage2_cmd_targets";
146 stage2_panic(msg, strlen(msg));146 stage2_panic(msg, strlen(msg));
147}147}
src/userland.h+1-1
...@@ -213,7 +213,7 @@ ZIG_EXTERN_C void stage2_cpu_features_get_cache_hash(const struct Stage2CpuFeatu...@@ -213,7 +213,7 @@ ZIG_EXTERN_C void stage2_cpu_features_get_cache_hash(const struct Stage2CpuFeatu
213 const char **ptr, size_t *len);213 const char **ptr, size_t *len);
214214
215// ABI warning215// ABI warning
216ZIG_EXTERN_C int stage2_cmd_targets(void);216ZIG_EXTERN_C int stage2_cmd_targets(const char *zig_triple);
217217
218218
219#endif219#endif