From bca672372aa57e97fcfb76672fe02f8ca6c2d00e Mon Sep 17 00:00:00 2001 From: Vexu <15308111+Vexu@users.noreply.github.com> Date: Thu, 7 Nov 2019 16:40:59 +0200 Subject: [PATCH] self hosted compiler: move functions to util.zigto avoid defining llvm instricts. --- src-self-hosted/compilation.zig | 10 +++--- src-self-hosted/ir.zig | 1 - src-self-hosted/libc_installation.zig | 4 +-- src-self-hosted/link.zig | 6 ++-- src-self-hosted/llvm.zig | 40 --------------------- src-self-hosted/{target.zig => util.zig} | 44 +++++++++++++++++++++++- 6 files changed, 53 insertions(+), 52 deletions(-) rename src-self-hosted/{target.zig => util.zig} (72%) diff --git a/src-self-hosted/compilation.zig b/src-self-hosted/compilation.zig index 175fdc9697..ad8fbed144 100644 --- a/src-self-hosted/compilation.zig +++ b/src-self-hosted/compilation.zig @@ -30,6 +30,7 @@ const link = @import("link.zig").link; const LibCInstallation = @import("libc_installation.zig").LibCInstallation; const CInt = @import("c_int.zig").CInt; const fs = event.fs; +const util = @import("util.zig"); const max_src_size = 2 * 1024 * 1024 * 1024; // 2 GiB @@ -48,7 +49,7 @@ pub const ZigCompiler = struct { pub fn init(allocator: *Allocator) !ZigCompiler { lazy_init_targets.get() orelse { - llvm.initializeAllTargets(); + util.initializeAllTargets(); lazy_init_targets.resolve(); }; @@ -372,7 +373,6 @@ pub const Compilation = struct { is_static: bool, zig_lib_dir: []const u8, ) !void { - const allocator = zig_compiler.allocator; var comp = Compilation{ .arena_allocator = std.heap.ArenaAllocator.init(allocator), @@ -476,8 +476,8 @@ pub const Compilation = struct { } comp.name = try Buffer.init(comp.arena(), name); - comp.llvm_triple = try llvm.getTriple(comp.arena(), target); - comp.llvm_target = try llvm.targetFromTriple(comp.llvm_triple); + comp.llvm_triple = try util.getTriple(comp.arena(), target); + comp.llvm_target = try util.llvmTargetFromTriple(comp.llvm_triple); comp.zig_std_dir = try std.fs.path.join(comp.arena(), [_][]const u8{ zig_lib_dir, "std" }); const opt_level = switch (build_mode) { @@ -507,7 +507,7 @@ pub const Compilation = struct { opt_level, reloc_mode, llvm.CodeModelDefault, - false // TODO: add -ffunction-sections option + false, // TODO: add -ffunction-sections option ) orelse return error.OutOfMemory; defer llvm.DisposeTargetMachine(comp.target_machine); diff --git a/src-self-hosted/ir.zig b/src-self-hosted/ir.zig index 86f92c7db4..3e25277223 100644 --- a/src-self-hosted/ir.zig +++ b/src-self-hosted/ir.zig @@ -1901,7 +1901,6 @@ pub const Builder = struct { ); } return error.Unimplemented; - } const Ident = union(enum) { diff --git a/src-self-hosted/libc_installation.zig b/src-self-hosted/libc_installation.zig index 9f531ae7dc..0cd43c8ba9 100644 --- a/src-self-hosted/libc_installation.zig +++ b/src-self-hosted/libc_installation.zig @@ -1,7 +1,7 @@ const std = @import("std"); const builtin = @import("builtin"); const event = std.event; -const target = @import("target.zig"); +const util = @import("util.zig"); const Target = std.Target; const c = @import("c.zig"); const fs = std.fs; @@ -138,7 +138,7 @@ pub const LibCInstallation = struct { self.static_lib_dir orelse "", self.msvc_lib_dir orelse "", self.kernel32_lib_dir orelse "", - self.dynamic_linker_path orelse target.getDynamicLinkerPath(Target(Target.Native)), + self.dynamic_linker_path orelse util.getDynamicLinkerPath(Target(Target.Native)), ); } diff --git a/src-self-hosted/link.zig b/src-self-hosted/link.zig index 5b6358ae3e..4e573215bb 100644 --- a/src-self-hosted/link.zig +++ b/src-self-hosted/link.zig @@ -153,7 +153,7 @@ fn constructLinkerArgsElf(ctx: *Context) !void { //bool shared = !g->is_static && is_lib; //Buf *soname = nullptr; if (ctx.comp.is_static) { - if (ctx.comp.target.isArmOrThumb()) { + if (util.isArmOrThumb(ctx.comp.target)) { try ctx.args.append(c"-Bstatic"); } else { try ctx.args.append(c"-static"); @@ -221,7 +221,7 @@ fn constructLinkerArgsElf(ctx: *Context) !void { if (!ctx.comp.is_static) { const dl = blk: { if (ctx.libc.dynamic_linker_path) |dl| break :blk dl; - if (ctx.comp.target.getDynamicLinkerPath()) |dl| break :blk dl; + if (util.getDynamicLinkerPath(ctx.comp.target)) |dl| break :blk dl; return error.LibCMissingDynamicLinker; }; try ctx.args.append(c"-dynamic-linker"); @@ -688,7 +688,7 @@ const DarwinPlatform = struct { if (result.kind == .IPhoneOS) { switch (comp.target.getArch()) { .i386, - .x86_64, + .x86_64, => result.kind = .IPhoneOSSimulator, else => {}, } diff --git a/src-self-hosted/llvm.zig b/src-self-hosted/llvm.zig index 2043208da3..2ac7f63e44 100644 --- a/src-self-hosted/llvm.zig +++ b/src-self-hosted/llvm.zig @@ -290,43 +290,3 @@ pub const BuildCall = ZigLLVMBuildCall; extern fn ZigLLVMBuildCall(B: *Builder, Fn: *Value, Args: [*]*Value, NumArgs: c_uint, CC: c_uint, fn_inline: FnInline, Name: [*]const u8) ?*Value; pub const PrivateLinkage = c.LLVMLinkage.LLVMPrivateLinkage; - -pub fn targetFromTriple(triple: std.Buffer) !*Target { - var result: *Target = undefined; - var err_msg: [*]u8 = undefined; - if (GetTargetFromTriple(triple.ptr(), &result, &err_msg) != 0) { - std.debug.warn("triple: {s} error: {s}\n", triple.ptr(), err_msg); - return error.UnsupportedTarget; - } - return result; -} - -pub fn initializeAllTargets() void { - InitializeAllTargets(); - InitializeAllTargetInfos(); - InitializeAllTargetMCs(); - InitializeAllAsmPrinters(); - InitializeAllAsmParsers(); -} - -pub fn getTriple(allocator: *std.mem.Allocator, self: std.Target) !std.Buffer { - var result = try std.Buffer.initSize(allocator, 0); - errdefer result.deinit(); - - // LLVM WebAssembly output support requires the target to be activated at - // build type with -DCMAKE_LLVM_EXPIERMENTAL_TARGETS_TO_BUILD=WebAssembly. - // - // LLVM determines the output format based on the abi suffix, - // defaulting to an object based on the architecture. The default format in - // LLVM 6 sets the wasm arch output incorrectly to ELF. We need to - // explicitly set this ourself in order for it to work. - // - // This is fixed in LLVM 7 and you will be able to get wasm output by - // using the target triple `wasm32-unknown-unknown-unknown`. - const env_name = if (self.isWasm()) "wasm" else @tagName(self.getAbi()); - - var out = &std.io.BufferOutStream.init(&result).stream; - try out.print("{}-unknown-{}-{}", @tagName(self.getArch()), @tagName(self.getOs()), env_name); - - return result; -} \ No newline at end of file diff --git a/src-self-hosted/target.zig b/src-self-hosted/util.zig similarity index 72% rename from src-self-hosted/target.zig rename to src-self-hosted/util.zig index dce470a47a..40644c30c8 100644 --- a/src-self-hosted/target.zig +++ b/src-self-hosted/util.zig @@ -1,4 +1,6 @@ -const Target = @import("std").Target; +const std = @import("std"); +const Target = std.Target; +const llvm = @import("llvm.zig"); // const builtin = @import("builtin"); pub const FloatAbi = enum { @@ -165,3 +167,43 @@ pub fn getDarwinArchString(self: Target) []const u8 { else => return @tagName(arch), } } + +pub fn llvmTargetFromTriple(triple: std.Buffer) !*llvm.Target { + var result: *llvm.Target = undefined; + var err_msg: [*]u8 = undefined; + if (llvm.GetTargetFromTriple(triple.ptr(), &result, &err_msg) != 0) { + std.debug.warn("triple: {s} error: {s}\n", triple.ptr(), err_msg); + return error.UnsupportedTarget; + } + return result; +} + +pub fn initializeAllTargets() void { + llvm.InitializeAllTargets(); + llvm.InitializeAllTargetInfos(); + llvm.InitializeAllTargetMCs(); + llvm.InitializeAllAsmPrinters(); + llvm.InitializeAllAsmParsers(); +} + +pub fn getTriple(allocator: *std.mem.Allocator, self: std.Target) !std.Buffer { + var result = try std.Buffer.initSize(allocator, 0); + errdefer result.deinit(); + + // LLVM WebAssembly output support requires the target to be activated at + // build type with -DCMAKE_LLVM_EXPIERMENTAL_TARGETS_TO_BUILD=WebAssembly. + // + // LLVM determines the output format based on the abi suffix, + // defaulting to an object based on the architecture. The default format in + // LLVM 6 sets the wasm arch output incorrectly to ELF. We need to + // explicitly set this ourself in order for it to work. + // + // This is fixed in LLVM 7 and you will be able to get wasm output by + // using the target triple `wasm32-unknown-unknown-unknown`. + const env_name = if (self.isWasm()) "wasm" else @tagName(self.getAbi()); + + var out = &std.io.BufferOutStream.init(&result).stream; + try out.print("{}-unknown-{}-{}", @tagName(self.getArch()), @tagName(self.getOs()), env_name); + + return result; +}