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

This commit is contained in:
Vexu 2019-11-07 16:40:59 +02:00
parent 56ea07f4fc
commit bca672372a
No known key found for this signature in database
GPG Key ID: 5AEABFCAFF5CD8D6
6 changed files with 53 additions and 52 deletions

View File

@ -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);

View File

@ -1901,7 +1901,6 @@ pub const Builder = struct {
);
}
return error.Unimplemented;
}
const Ident = union(enum) {

View File

@ -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)),
);
}

View File

@ -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 => {},
}

View File

@ -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;
}

View File

@ -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;
}