authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-01-20 13:40:25-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-01-20 13:40:25-05:00
logf3dd9bbdaca5ba3735feb405a890a4646905533a
treee9fb7bec1a4323375dde4a205d1b71b322c719bc
parentbf82929557f0b116979261c522c62cf6393a08f2
signature Commit is signed but in an unrecognized format.

improve `zig targets`


7 files changed, 135 insertions(+), 67 deletions(-)

BRANCH_TODO+2-8
......@@ -1,12 +1,8 @@
11Finish these thigns before merging teh branch
22
3 * it gets the wrong answers with `-target-feature -sse,-avx`
4
3 * zig targets
4 - use non-reflection based cpu detection?
55 * finish refactoring target/arch/*
6 * `zig builtin` integration
7 * move target details to better location
8 * +foo,-bar vs foo,bar
9 * baseline features
106
117
128const riscv32_default_features: []*const std.target.Feature = &[_]*const std.target.Feature{
......@@ -38,5 +34,3 @@ const i386_default_features_freestanding: []*const std.target.Feature = &[_]*con
3834 &std.target.x86.feature_slowUnalignedMem16,
3935 &std.target.x86.feature_x87,
4036};
41
42
lib/std/builtin.zig+2-2
......@@ -18,8 +18,8 @@ pub const ObjectFormat = std.Target.ObjectFormat;
1818/// Deprecated: use `std.Target.SubSystem`.
1919pub const SubSystem = std.Target.SubSystem;
2020
21/// Deprecated: use `std.Target.Cross.CpuFeatures`.
22pub const CpuFeatures = std.Target.Cross.CpuFeatures;
21/// Deprecated: use `std.Target.CpuFeatures`.
22pub const CpuFeatures = std.Target.CpuFeatures;
2323
2424/// Deprecated: use `std.Target.Cpu`.
2525pub const Cpu = std.Target.Cpu;
lib/std/target.zig+18-11
......@@ -569,18 +569,18 @@ pub const Target = union(enum) {
569569 os: Os,
570570 abi: Abi,
571571 cpu_features: CpuFeatures = .baseline,
572 };
572573
573 pub const CpuFeatures = union(enum) {
574 /// The "default" set of CPU features for cross-compiling. A conservative set
575 /// of features that is expected to be supported on most available hardware.
576 baseline,
574 pub const CpuFeatures = union(enum) {
575 /// The "default" set of CPU features for cross-compiling. A conservative set
576 /// of features that is expected to be supported on most available hardware.
577 baseline,
577578
578 /// Target one specific CPU.
579 cpu: *const Cpu,
579 /// Target one specific CPU.
580 cpu: *const Cpu,
580581
581 /// Explicitly provide the entire CPU feature set.
582 features: Cpu.Feature.Set,
583 };
582 /// Explicitly provide the entire CPU feature set.
583 features: Cpu.Feature.Set,
584584 };
585585
586586 pub const current = Target{
......@@ -594,8 +594,15 @@ pub const Target = union(enum) {
594594
595595 pub const stack_align = 16;
596596
597 pub fn cpuFeatures(self: Target) []const *const Cpu.Feature {
598 return switch (self.cpu_features) {
597 pub fn getCpuFeatures(self: Target) CpuFeatures {
598 return switch (self) {
599 .Native => builtin.cpu_features,
600 .Cross => |cross| cross.cpu_features,
601 };
602 }
603
604 pub fn cpuFeaturesList(self: Target) []const *const Cpu.Feature {
605 return switch (self.getCpuFeatures()) {
599606 .baseline => self.arch.baselineFeatures(),
600607 .cpu => |cpu| cpu.features,
601608 .features => |features| features,
src-self-hosted/main.zig+1-43
......@@ -79,7 +79,7 @@ pub fn main() !void {
7979 } else if (mem.eql(u8, cmd, "libc")) {
8080 return cmdLibC(allocator, cmd_args);
8181 } else if (mem.eql(u8, cmd, "targets")) {
82 return cmdTargets(allocator, cmd_args);
82 return @import("print_targets.zig").cmdTargets(allocator, cmd_args, stdout);
8383 } else if (mem.eql(u8, cmd, "version")) {
8484 return cmdVersion(allocator, cmd_args);
8585 } else if (mem.eql(u8, cmd, "zen")) {
......@@ -789,48 +789,6 @@ async fn fmtPath(fmt: *Fmt, file_path_ref: []const u8, check_mode: bool) FmtErro
789789 }
790790}
791791
792// cmd:targets /////////////////////////////////////////////////////////////////////////////////////
793
794pub fn cmdTargets(allocator: *Allocator, args: []const []const u8) !void {
795 try stdout.write("Architectures:\n");
796 {
797 comptime var i: usize = 0;
798 inline while (i < @memberCount(builtin.Arch)) : (i += 1) {
799 comptime const arch_tag = @memberName(builtin.Arch, i);
800 // NOTE: Cannot use empty string, see #918.
801 comptime const native_str = if (comptime mem.eql(u8, arch_tag, @tagName(builtin.arch))) " (native)\n" else "\n";
802
803 try stdout.print(" {}{}", .{ arch_tag, native_str });
804 }
805 }
806 try stdout.write("\n");
807
808 try stdout.write("Operating Systems:\n");
809 {
810 comptime var i: usize = 0;
811 inline while (i < @memberCount(Target.Os)) : (i += 1) {
812 comptime const os_tag = @memberName(Target.Os, i);
813 // NOTE: Cannot use empty string, see #918.
814 comptime const native_str = if (comptime mem.eql(u8, os_tag, @tagName(builtin.os))) " (native)\n" else "\n";
815
816 try stdout.print(" {}{}", .{ os_tag, native_str });
817 }
818 }
819 try stdout.write("\n");
820
821 try stdout.write("C ABIs:\n");
822 {
823 comptime var i: usize = 0;
824 inline while (i < @memberCount(Target.Abi)) : (i += 1) {
825 comptime const abi_tag = @memberName(Target.Abi, i);
826 // NOTE: Cannot use empty string, see #918.
827 comptime const native_str = if (comptime mem.eql(u8, abi_tag, @tagName(builtin.abi))) " (native)\n" else "\n";
828
829 try stdout.print(" {}{}", .{ abi_tag, native_str });
830 }
831 }
832}
833
834792fn cmdVersion(allocator: *Allocator, args: []const []const u8) !void {
835793 try stdout.print("{}\n", .{std.mem.toSliceConst(u8, c.ZIG_VERSION_STRING)});
836794}
src-self-hosted/print_targets.zig created+101
......@@ -0,0 +1,101 @@
1const std = @import("std");
2const fs = std.fs;
3const io = std.io;
4const mem = std.mem;
5const Allocator = mem.Allocator;
6const Target = std.Target;
7
8pub fn cmdTargets(
9 allocator: *Allocator,
10 args: []const []const u8,
11 stdout: *io.OutStream(fs.File.WriteError),
12) !void {
13 const BOS = io.BufferedOutStream(fs.File.WriteError);
14 var bos = BOS.init(stdout);
15 var jws = std.json.WriteStream(BOS.Stream, 6).init(&bos.stream);
16
17 try jws.beginObject();
18
19 try jws.objectField("arch");
20 try jws.beginObject();
21 {
22 inline for (@typeInfo(Target.Arch).Union.fields) |field| {
23 try jws.objectField(field.name);
24 if (field.field_type == void) {
25 try jws.emitNull();
26 } else {
27 try jws.emitString(@typeName(field.field_type));
28 }
29 }
30 }
31 try jws.endObject();
32
33 try jws.objectField("subArch");
34 try jws.beginObject();
35 const sub_arch_list = [_]type{
36 Target.Arch.Arm32,
37 Target.Arch.Arm64,
38 Target.Arch.Kalimba,
39 Target.Arch.Mips,
40 };
41 inline for (sub_arch_list) |SubArch| {
42 try jws.objectField(@typeName(SubArch));
43 try jws.beginArray();
44 inline for (@typeInfo(SubArch).Enum.fields) |field| {
45 try jws.arrayElem();
46 try jws.emitString(field.name);
47 }
48 try jws.endArray();
49 }
50 try jws.endObject();
51
52 try jws.objectField("os");
53 try jws.beginArray();
54 {
55 comptime var i: usize = 0;
56 inline while (i < @memberCount(Target.Os)) : (i += 1) {
57 const os_tag = @memberName(Target.Os, i);
58 try jws.arrayElem();
59 try jws.emitString(os_tag);
60 }
61 }
62 try jws.endArray();
63
64 try jws.objectField("abi");
65 try jws.beginArray();
66 {
67 comptime var i: usize = 0;
68 inline while (i < @memberCount(Target.Abi)) : (i += 1) {
69 const abi_tag = @memberName(Target.Abi, i);
70 try jws.arrayElem();
71 try jws.emitString(abi_tag);
72 }
73 }
74 try jws.endArray();
75
76 try jws.objectField("native");
77 try jws.beginObject();
78 {
79 const triple = try Target.current.zigTriple(allocator);
80 defer allocator.free(triple);
81 try jws.objectField("triple");
82 try jws.emitString(triple);
83 }
84 try jws.objectField("arch");
85 try jws.emitString(@tagName(Target.current.getArch()));
86 try jws.objectField("os");
87 try jws.emitString(@tagName(Target.current.getOs()));
88 try jws.objectField("abi");
89 try jws.emitString(@tagName(Target.current.getAbi()));
90 try jws.objectField("cpuName");
91 switch (Target.current.getCpuFeatures()) {
92 .baseline, .features => try jws.emitNull(),
93 .cpu => |cpu| try jws.emitString(cpu.name),
94 }
95 try jws.endObject();
96
97 try jws.endObject();
98
99 try bos.stream.writeByte('\n');
100 return bos.flush();
101}
src-self-hosted/stage1.zig+6-2
......@@ -539,7 +539,11 @@ export fn stage2_progress_update_node(node: *std.Progress.Node, done_count: usiz
539539
540540// ABI warning
541541export fn stage2_cmd_targets() c_int {
542 self_hosted_main.cmdTargets(std.heap.c_allocator, &[0][]u8{}) catch |err| {
542 @import("print_targets.zig").cmdTargets(
543 std.heap.c_allocator,
544 &[0][]u8{},
545 &std.io.getStdOut().outStream().stream,
546 ) catch |err| {
543547 std.debug.warn("unable to list targets: {}\n", .{@errorName(err)});
544548 return -1;
545549 };
......@@ -548,7 +552,7 @@ export fn stage2_cmd_targets() c_int {
548552
549553const Stage2CpuFeatures = struct {
550554 allocator: *mem.Allocator,
551 cpu_features: Target.Cross.CpuFeatures,
555 cpu_features: Target.CpuFeatures,
552556
553557 llvm_cpu_name: ?[*:0]const u8,
554558 llvm_features_str: ?[*:0]const u8,
src/ir.cpp+5-1
......@@ -22349,7 +22349,11 @@ static IrInstruction *ir_analyze_instruction_enum_tag_name(IrAnalyze *ira, IrIns
2234922349 return ira->codegen->invalid_instruction;
2235022350 }
2235122351
22352 assert(target->value->type->id == ZigTypeIdEnum);
22352 if (target->value->type->id != ZigTypeIdEnum) {
22353 ir_add_error(ira, target,
22354 buf_sprintf("expected enum tag, found '%s'", buf_ptr(&target->value->type->name)));
22355 return ira->codegen->invalid_instruction;
22356 }
2235322357
2235422358 if (target->value->type->data.enumeration.src_field_count == 1 &&
2235522359 !target->value->type->data.enumeration.non_exhaustive) {