authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2019-11-07 16:40:59+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2019-11-07 23:03:57+02:00
logbca672372aa57e97fcfb76672fe02f8ca6c2d00e
tree40dcd5bedcc3958a9b2e5433dcdcbc1b1b482dd2
parent56ea07f4fc20f479d82a7cb1633a15308a99c966
signaturelock-open Commit is signed but in an unrecognized format.

self hosted compiler: move functions to util.zigto avoid defining llvm instricts.


7 files changed, 219 insertions(+), 218 deletions(-)

src-self-hosted/compilation.zig+5-5
...@@ -30,6 +30,7 @@ const link = @import("link.zig").link;...@@ -30,6 +30,7 @@ const link = @import("link.zig").link;
30const LibCInstallation = @import("libc_installation.zig").LibCInstallation;30const LibCInstallation = @import("libc_installation.zig").LibCInstallation;
31const CInt = @import("c_int.zig").CInt;31const CInt = @import("c_int.zig").CInt;
32const fs = event.fs;32const fs = event.fs;
33const util = @import("util.zig");
3334
34const max_src_size = 2 * 1024 * 1024 * 1024; // 2 GiB35const max_src_size = 2 * 1024 * 1024 * 1024; // 2 GiB
3536
...@@ -48,7 +49,7 @@ pub const ZigCompiler = struct {...@@ -48,7 +49,7 @@ pub const ZigCompiler = struct {
4849
49 pub fn init(allocator: *Allocator) !ZigCompiler {50 pub fn init(allocator: *Allocator) !ZigCompiler {
50 lazy_init_targets.get() orelse {51 lazy_init_targets.get() orelse {
51 llvm.initializeAllTargets();52 util.initializeAllTargets();
52 lazy_init_targets.resolve();53 lazy_init_targets.resolve();
53 };54 };
5455
...@@ -372,7 +373,6 @@ pub const Compilation = struct {...@@ -372,7 +373,6 @@ pub const Compilation = struct {
372 is_static: bool,373 is_static: bool,
373 zig_lib_dir: []const u8,374 zig_lib_dir: []const u8,
374 ) !void {375 ) !void {
375
376 const allocator = zig_compiler.allocator;376 const allocator = zig_compiler.allocator;
377 var comp = Compilation{377 var comp = Compilation{
378 .arena_allocator = std.heap.ArenaAllocator.init(allocator),378 .arena_allocator = std.heap.ArenaAllocator.init(allocator),
...@@ -476,8 +476,8 @@ pub const Compilation = struct {...@@ -476,8 +476,8 @@ pub const Compilation = struct {
476 }476 }
477477
478 comp.name = try Buffer.init(comp.arena(), name);478 comp.name = try Buffer.init(comp.arena(), name);
479 comp.llvm_triple = try llvm.getTriple(comp.arena(), target);479 comp.llvm_triple = try util.getTriple(comp.arena(), target);
480 comp.llvm_target = try llvm.targetFromTriple(comp.llvm_triple);480 comp.llvm_target = try util.llvmTargetFromTriple(comp.llvm_triple);
481 comp.zig_std_dir = try std.fs.path.join(comp.arena(), [_][]const u8{ zig_lib_dir, "std" });481 comp.zig_std_dir = try std.fs.path.join(comp.arena(), [_][]const u8{ zig_lib_dir, "std" });
482482
483 const opt_level = switch (build_mode) {483 const opt_level = switch (build_mode) {
...@@ -507,7 +507,7 @@ pub const Compilation = struct {...@@ -507,7 +507,7 @@ pub const Compilation = struct {
507 opt_level,507 opt_level,
508 reloc_mode,508 reloc_mode,
509 llvm.CodeModelDefault,509 llvm.CodeModelDefault,
510 false // TODO: add -ffunction-sections option510 false, // TODO: add -ffunction-sections option
511 ) orelse return error.OutOfMemory;511 ) orelse return error.OutOfMemory;
512 defer llvm.DisposeTargetMachine(comp.target_machine);512 defer llvm.DisposeTargetMachine(comp.target_machine);
513513
src-self-hosted/ir.zig-1
...@@ -1901,7 +1901,6 @@ pub const Builder = struct {...@@ -1901,7 +1901,6 @@ pub const Builder = struct {
1901 );1901 );
1902 }1902 }
1903 return error.Unimplemented;1903 return error.Unimplemented;
1904
1905 }1904 }
19061905
1907 const Ident = union(enum) {1906 const Ident = union(enum) {
src-self-hosted/libc_installation.zig+2-2
...@@ -1,7 +1,7 @@...@@ -1,7 +1,7 @@
1const std = @import("std");1const std = @import("std");
2const builtin = @import("builtin");2const builtin = @import("builtin");
3const event = std.event;3const event = std.event;
4const target = @import("target.zig");4const util = @import("util.zig");
5const Target = std.Target;5const Target = std.Target;
6const c = @import("c.zig");6const c = @import("c.zig");
7const fs = std.fs;7const fs = std.fs;
...@@ -138,7 +138,7 @@ pub const LibCInstallation = struct {...@@ -138,7 +138,7 @@ pub const LibCInstallation = struct {
138 self.static_lib_dir orelse "",138 self.static_lib_dir orelse "",
139 self.msvc_lib_dir orelse "",139 self.msvc_lib_dir orelse "",
140 self.kernel32_lib_dir orelse "",140 self.kernel32_lib_dir orelse "",
141 self.dynamic_linker_path orelse target.getDynamicLinkerPath(Target(Target.Native)),141 self.dynamic_linker_path orelse util.getDynamicLinkerPath(Target(Target.Native)),
142 );142 );
143 }143 }
144144
src-self-hosted/link.zig+3-3
...@@ -153,7 +153,7 @@ fn constructLinkerArgsElf(ctx: *Context) !void {...@@ -153,7 +153,7 @@ fn constructLinkerArgsElf(ctx: *Context) !void {
153 //bool shared = !g->is_static && is_lib;153 //bool shared = !g->is_static && is_lib;
154 //Buf *soname = nullptr;154 //Buf *soname = nullptr;
155 if (ctx.comp.is_static) {155 if (ctx.comp.is_static) {
156 if (ctx.comp.target.isArmOrThumb()) {156 if (util.isArmOrThumb(ctx.comp.target)) {
157 try ctx.args.append(c"-Bstatic");157 try ctx.args.append(c"-Bstatic");
158 } else {158 } else {
159 try ctx.args.append(c"-static");159 try ctx.args.append(c"-static");
...@@ -221,7 +221,7 @@ fn constructLinkerArgsElf(ctx: *Context) !void {...@@ -221,7 +221,7 @@ fn constructLinkerArgsElf(ctx: *Context) !void {
221 if (!ctx.comp.is_static) {221 if (!ctx.comp.is_static) {
222 const dl = blk: {222 const dl = blk: {
223 if (ctx.libc.dynamic_linker_path) |dl| break :blk dl;223 if (ctx.libc.dynamic_linker_path) |dl| break :blk dl;
224 if (ctx.comp.target.getDynamicLinkerPath()) |dl| break :blk dl;224 if (util.getDynamicLinkerPath(ctx.comp.target)) |dl| break :blk dl;
225 return error.LibCMissingDynamicLinker;225 return error.LibCMissingDynamicLinker;
226 };226 };
227 try ctx.args.append(c"-dynamic-linker");227 try ctx.args.append(c"-dynamic-linker");
...@@ -688,7 +688,7 @@ const DarwinPlatform = struct {...@@ -688,7 +688,7 @@ const DarwinPlatform = struct {
688 if (result.kind == .IPhoneOS) {688 if (result.kind == .IPhoneOS) {
689 switch (comp.target.getArch()) {689 switch (comp.target.getArch()) {
690 .i386,690 .i386,
691 .x86_64,691 .x86_64,
692 => result.kind = .IPhoneOSSimulator,692 => result.kind = .IPhoneOSSimulator,
693 else => {},693 else => {},
694 }694 }
src-self-hosted/llvm.zig-40
...@@ -290,43 +290,3 @@ pub const BuildCall = ZigLLVMBuildCall;...@@ -290,43 +290,3 @@ pub const BuildCall = ZigLLVMBuildCall;
290extern fn ZigLLVMBuildCall(B: *Builder, Fn: *Value, Args: [*]*Value, NumArgs: c_uint, CC: c_uint, fn_inline: FnInline, Name: [*]const u8) ?*Value;290extern fn ZigLLVMBuildCall(B: *Builder, Fn: *Value, Args: [*]*Value, NumArgs: c_uint, CC: c_uint, fn_inline: FnInline, Name: [*]const u8) ?*Value;
291291
292pub const PrivateLinkage = c.LLVMLinkage.LLVMPrivateLinkage;292pub const PrivateLinkage = c.LLVMLinkage.LLVMPrivateLinkage;
293
294pub fn targetFromTriple(triple: std.Buffer) !*Target {
295 var result: *Target = undefined;
296 var err_msg: [*]u8 = undefined;
297 if (GetTargetFromTriple(triple.ptr(), &result, &err_msg) != 0) {
298 std.debug.warn("triple: {s} error: {s}\n", triple.ptr(), err_msg);
299 return error.UnsupportedTarget;
300 }
301 return result;
302}
303
304pub fn initializeAllTargets() void {
305 InitializeAllTargets();
306 InitializeAllTargetInfos();
307 InitializeAllTargetMCs();
308 InitializeAllAsmPrinters();
309 InitializeAllAsmParsers();
310}
311
312pub fn getTriple(allocator: *std.mem.Allocator, self: std.Target) !std.Buffer {
313 var result = try std.Buffer.initSize(allocator, 0);
314 errdefer result.deinit();
315
316 // LLVM WebAssembly output support requires the target to be activated at
317 // build type with -DCMAKE_LLVM_EXPIERMENTAL_TARGETS_TO_BUILD=WebAssembly.
318 //
319 // LLVM determines the output format based on the abi suffix,
320 // defaulting to an object based on the architecture. The default format in
321 // LLVM 6 sets the wasm arch output incorrectly to ELF. We need to
322 // explicitly set this ourself in order for it to work.
323 //
324 // This is fixed in LLVM 7 and you will be able to get wasm output by
325 // using the target triple `wasm32-unknown-unknown-unknown`.
326 const env_name = if (self.isWasm()) "wasm" else @tagName(self.getAbi());
327
328 var out = &std.io.BufferOutStream.init(&result).stream;
329 try out.print("{}-unknown-{}-{}", @tagName(self.getArch()), @tagName(self.getOs()), env_name);
330
331 return result;
332}
\ No newline at end of file
src-self-hosted/target.zig deleted-167
...@@ -1,167 +0,0 @@
1const Target = @import("std").Target;
2// const builtin = @import("builtin");
3
4pub const FloatAbi = enum {
5 Hard,
6 Soft,
7 SoftFp,
8};
9
10// pub const Cross = struct {
11// arch: Target.Arch,
12// os: Target.Os,
13// abi: Target.Abi,
14// object_format: builtin.ObjectFormat,
15// };
16
17// pub fn getObjectFormat(self: Target) builtin.ObjectFormat {
18// return switch (self) {
19// .Native => builtin.object_format,
20// .Cross => |t| t.object_format,
21// };
22// }
23
24/// TODO expose the arch and subarch separately
25pub fn isArmOrThumb(self: Target) bool {
26 return switch (self.getArch()) {
27 .arm,
28 .armeb,
29 .aarch64,
30 .aarch64_be,
31 .thumb,
32 .thumbeb,
33 => true,
34 else => false,
35 };
36}
37
38pub fn getFloatAbi(self: Target) FloatAbi {
39 return switch (self.getAbi()) {
40 .gnueabihf,
41 .eabihf,
42 .musleabihf,
43 => .Hard,
44 else => .Soft,
45 };
46}
47
48pub fn getDynamicLinkerPath(self: Target) ?[]const u8 {
49 const env = self.getAbi();
50 const arch = self.getArch();
51 const os = self.getOs();
52 switch (os) {
53 .freebsd => {
54 return "/libexec/ld-elf.so.1";
55 },
56 .linux => {
57 switch (env) {
58 .android => {
59 if (self.getArchPtrBitWidth() == 64) {
60 return "/system/bin/linker64";
61 } else {
62 return "/system/bin/linker";
63 }
64 },
65 .gnux32 => {
66 if (arch == .x86_64) {
67 return "/libx32/ld-linux-x32.so.2";
68 }
69 },
70 .musl,
71 .musleabi,
72 .musleabihf,
73 => {
74 if (arch == .x86_64) {
75 return "/lib/ld-musl-x86_64.so.1";
76 }
77 },
78 else => {},
79 }
80 switch (arch) {
81 .i386,
82 .sparc,
83 .sparcel,
84 => return "/lib/ld-linux.so.2",
85
86 .aarch64 => return "/lib/ld-linux-aarch64.so.1",
87
88 .aarch64_be => return "/lib/ld-linux-aarch64_be.so.1",
89
90 .arm,
91 .thumb,
92 => return switch (getFloatAbi(self)) {
93 .Hard => return "/lib/ld-linux-armhf.so.3",
94 else => return "/lib/ld-linux.so.3",
95 },
96
97 .armeb,
98 .thumbeb,
99 => return switch (getFloatAbi(self)) {
100 .Hard => return "/lib/ld-linux-armhf.so.3",
101 else => return "/lib/ld-linux.so.3",
102 },
103
104 .mips,
105 .mipsel,
106 .mips64,
107 .mips64el,
108 => return null,
109
110 .powerpc => return "/lib/ld.so.1",
111 .powerpc64 => return "/lib64/ld64.so.2",
112 .powerpc64le => return "/lib64/ld64.so.2",
113 .s390x => return "/lib64/ld64.so.1",
114 .sparcv9 => return "/lib64/ld-linux.so.2",
115 .x86_64 => return "/lib64/ld-linux-x86-64.so.2",
116
117 .arc,
118 .avr,
119 .bpfel,
120 .bpfeb,
121 .hexagon,
122 .msp430,
123 .r600,
124 .amdgcn,
125 .riscv32,
126 .riscv64,
127 .tce,
128 .tcele,
129 .xcore,
130 .nvptx,
131 .nvptx64,
132 .le32,
133 .le64,
134 .amdil,
135 .amdil64,
136 .hsail,
137 .hsail64,
138 .spir,
139 .spir64,
140 .kalimba,
141 .shave,
142 .lanai,
143 .wasm32,
144 .wasm64,
145 .renderscript32,
146 .renderscript64,
147 .aarch64_32,
148 => return null,
149 }
150 },
151 else => return null,
152 }
153}
154
155pub fn getDarwinArchString(self: Target) []const u8 {
156 const arch = self.getArch();
157 switch (arch) {
158 .aarch64 => return "arm64",
159 .thumb,
160 .arm,
161 => return "arm",
162 .powerpc => return "ppc",
163 .powerpc64 => return "ppc64",
164 .powerpc64le => return "ppc64le",
165 else => return @tagName(arch),
166 }
167}
src-self-hosted/util.zig created+209
...@@ -0,0 +1,209 @@
1const std = @import("std");
2const Target = std.Target;
3const llvm = @import("llvm.zig");
4// const builtin = @import("builtin");
5
6pub const FloatAbi = enum {
7 Hard,
8 Soft,
9 SoftFp,
10};
11
12// pub const Cross = struct {
13// arch: Target.Arch,
14// os: Target.Os,
15// abi: Target.Abi,
16// object_format: builtin.ObjectFormat,
17// };
18
19// pub fn getObjectFormat(self: Target) builtin.ObjectFormat {
20// return switch (self) {
21// .Native => builtin.object_format,
22// .Cross => |t| t.object_format,
23// };
24// }
25
26/// TODO expose the arch and subarch separately
27pub fn isArmOrThumb(self: Target) bool {
28 return switch (self.getArch()) {
29 .arm,
30 .armeb,
31 .aarch64,
32 .aarch64_be,
33 .thumb,
34 .thumbeb,
35 => true,
36 else => false,
37 };
38}
39
40pub fn getFloatAbi(self: Target) FloatAbi {
41 return switch (self.getAbi()) {
42 .gnueabihf,
43 .eabihf,
44 .musleabihf,
45 => .Hard,
46 else => .Soft,
47 };
48}
49
50pub fn getDynamicLinkerPath(self: Target) ?[]const u8 {
51 const env = self.getAbi();
52 const arch = self.getArch();
53 const os = self.getOs();
54 switch (os) {
55 .freebsd => {
56 return "/libexec/ld-elf.so.1";
57 },
58 .linux => {
59 switch (env) {
60 .android => {
61 if (self.getArchPtrBitWidth() == 64) {
62 return "/system/bin/linker64";
63 } else {
64 return "/system/bin/linker";
65 }
66 },
67 .gnux32 => {
68 if (arch == .x86_64) {
69 return "/libx32/ld-linux-x32.so.2";
70 }
71 },
72 .musl,
73 .musleabi,
74 .musleabihf,
75 => {
76 if (arch == .x86_64) {
77 return "/lib/ld-musl-x86_64.so.1";
78 }
79 },
80 else => {},
81 }
82 switch (arch) {
83 .i386,
84 .sparc,
85 .sparcel,
86 => return "/lib/ld-linux.so.2",
87
88 .aarch64 => return "/lib/ld-linux-aarch64.so.1",
89
90 .aarch64_be => return "/lib/ld-linux-aarch64_be.so.1",
91
92 .arm,
93 .thumb,
94 => return switch (getFloatAbi(self)) {
95 .Hard => return "/lib/ld-linux-armhf.so.3",
96 else => return "/lib/ld-linux.so.3",
97 },
98
99 .armeb,
100 .thumbeb,
101 => return switch (getFloatAbi(self)) {
102 .Hard => return "/lib/ld-linux-armhf.so.3",
103 else => return "/lib/ld-linux.so.3",
104 },
105
106 .mips,
107 .mipsel,
108 .mips64,
109 .mips64el,
110 => return null,
111
112 .powerpc => return "/lib/ld.so.1",
113 .powerpc64 => return "/lib64/ld64.so.2",
114 .powerpc64le => return "/lib64/ld64.so.2",
115 .s390x => return "/lib64/ld64.so.1",
116 .sparcv9 => return "/lib64/ld-linux.so.2",
117 .x86_64 => return "/lib64/ld-linux-x86-64.so.2",
118
119 .arc,
120 .avr,
121 .bpfel,
122 .bpfeb,
123 .hexagon,
124 .msp430,
125 .r600,
126 .amdgcn,
127 .riscv32,
128 .riscv64,
129 .tce,
130 .tcele,
131 .xcore,
132 .nvptx,
133 .nvptx64,
134 .le32,
135 .le64,
136 .amdil,
137 .amdil64,
138 .hsail,
139 .hsail64,
140 .spir,
141 .spir64,
142 .kalimba,
143 .shave,
144 .lanai,
145 .wasm32,
146 .wasm64,
147 .renderscript32,
148 .renderscript64,
149 .aarch64_32,
150 => return null,
151 }
152 },
153 else => return null,
154 }
155}
156
157pub fn getDarwinArchString(self: Target) []const u8 {
158 const arch = self.getArch();
159 switch (arch) {
160 .aarch64 => return "arm64",
161 .thumb,
162 .arm,
163 => return "arm",
164 .powerpc => return "ppc",
165 .powerpc64 => return "ppc64",
166 .powerpc64le => return "ppc64le",
167 else => return @tagName(arch),
168 }
169}
170
171pub fn llvmTargetFromTriple(triple: std.Buffer) !*llvm.Target {
172 var result: *llvm.Target = undefined;
173 var err_msg: [*]u8 = undefined;
174 if (llvm.GetTargetFromTriple(triple.ptr(), &result, &err_msg) != 0) {
175 std.debug.warn("triple: {s} error: {s}\n", triple.ptr(), err_msg);
176 return error.UnsupportedTarget;
177 }
178 return result;
179}
180
181pub fn initializeAllTargets() void {
182 llvm.InitializeAllTargets();
183 llvm.InitializeAllTargetInfos();
184 llvm.InitializeAllTargetMCs();
185 llvm.InitializeAllAsmPrinters();
186 llvm.InitializeAllAsmParsers();
187}
188
189pub fn getTriple(allocator: *std.mem.Allocator, self: std.Target) !std.Buffer {
190 var result = try std.Buffer.initSize(allocator, 0);
191 errdefer result.deinit();
192
193 // LLVM WebAssembly output support requires the target to be activated at
194 // build type with -DCMAKE_LLVM_EXPIERMENTAL_TARGETS_TO_BUILD=WebAssembly.
195 //
196 // LLVM determines the output format based on the abi suffix,
197 // defaulting to an object based on the architecture. The default format in
198 // LLVM 6 sets the wasm arch output incorrectly to ELF. We need to
199 // explicitly set this ourself in order for it to work.
200 //
201 // This is fixed in LLVM 7 and you will be able to get wasm output by
202 // using the target triple `wasm32-unknown-unknown-unknown`.
203 const env_name = if (self.isWasm()) "wasm" else @tagName(self.getAbi());
204
205 var out = &std.io.BufferOutStream.init(&result).stream;
206 try out.print("{}-unknown-{}-{}", @tagName(self.getArch()), @tagName(self.getOs()), env_name);
207
208 return result;
209}