commit bca672372aa57e97fcfb76672fe02f8ca6c2d00e (tree)
parent 56ea07f4fc20f479d82a7cb1633a15308a99c966
Author: Vexu <15308111+Vexu@users.noreply.github.com>
Date: Thu, 7 Nov 2019 16:40:59 +0200
self hosted compiler: move functions to util.zigto avoid defining llvm instricts.
Diffstat:
7 files changed, 219 insertions(+), 219 deletions(-)
diff --git 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
@@ -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
@@ -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
@@ -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
@@ -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/target.zig
@@ -1,167 +0,0 @@
-const Target = @import("std").Target;
-// const builtin = @import("builtin");
-
-pub const FloatAbi = enum {
- Hard,
- Soft,
- SoftFp,
-};
-
-// pub const Cross = struct {
-// arch: Target.Arch,
-// os: Target.Os,
-// abi: Target.Abi,
-// object_format: builtin.ObjectFormat,
-// };
-
-// pub fn getObjectFormat(self: Target) builtin.ObjectFormat {
-// return switch (self) {
-// .Native => builtin.object_format,
-// .Cross => |t| t.object_format,
-// };
-// }
-
-/// TODO expose the arch and subarch separately
-pub fn isArmOrThumb(self: Target) bool {
- return switch (self.getArch()) {
- .arm,
- .armeb,
- .aarch64,
- .aarch64_be,
- .thumb,
- .thumbeb,
- => true,
- else => false,
- };
-}
-
-pub fn getFloatAbi(self: Target) FloatAbi {
- return switch (self.getAbi()) {
- .gnueabihf,
- .eabihf,
- .musleabihf,
- => .Hard,
- else => .Soft,
- };
-}
-
-pub fn getDynamicLinkerPath(self: Target) ?[]const u8 {
- const env = self.getAbi();
- const arch = self.getArch();
- const os = self.getOs();
- switch (os) {
- .freebsd => {
- return "/libexec/ld-elf.so.1";
- },
- .linux => {
- switch (env) {
- .android => {
- if (self.getArchPtrBitWidth() == 64) {
- return "/system/bin/linker64";
- } else {
- return "/system/bin/linker";
- }
- },
- .gnux32 => {
- if (arch == .x86_64) {
- return "/libx32/ld-linux-x32.so.2";
- }
- },
- .musl,
- .musleabi,
- .musleabihf,
- => {
- if (arch == .x86_64) {
- return "/lib/ld-musl-x86_64.so.1";
- }
- },
- else => {},
- }
- switch (arch) {
- .i386,
- .sparc,
- .sparcel,
- => return "/lib/ld-linux.so.2",
-
- .aarch64 => return "/lib/ld-linux-aarch64.so.1",
-
- .aarch64_be => return "/lib/ld-linux-aarch64_be.so.1",
-
- .arm,
- .thumb,
- => return switch (getFloatAbi(self)) {
- .Hard => return "/lib/ld-linux-armhf.so.3",
- else => return "/lib/ld-linux.so.3",
- },
-
- .armeb,
- .thumbeb,
- => return switch (getFloatAbi(self)) {
- .Hard => return "/lib/ld-linux-armhf.so.3",
- else => return "/lib/ld-linux.so.3",
- },
-
- .mips,
- .mipsel,
- .mips64,
- .mips64el,
- => return null,
-
- .powerpc => return "/lib/ld.so.1",
- .powerpc64 => return "/lib64/ld64.so.2",
- .powerpc64le => return "/lib64/ld64.so.2",
- .s390x => return "/lib64/ld64.so.1",
- .sparcv9 => return "/lib64/ld-linux.so.2",
- .x86_64 => return "/lib64/ld-linux-x86-64.so.2",
-
- .arc,
- .avr,
- .bpfel,
- .bpfeb,
- .hexagon,
- .msp430,
- .r600,
- .amdgcn,
- .riscv32,
- .riscv64,
- .tce,
- .tcele,
- .xcore,
- .nvptx,
- .nvptx64,
- .le32,
- .le64,
- .amdil,
- .amdil64,
- .hsail,
- .hsail64,
- .spir,
- .spir64,
- .kalimba,
- .shave,
- .lanai,
- .wasm32,
- .wasm64,
- .renderscript32,
- .renderscript64,
- .aarch64_32,
- => return null,
- }
- },
- else => return null,
- }
-}
-
-pub fn getDarwinArchString(self: Target) []const u8 {
- const arch = self.getArch();
- switch (arch) {
- .aarch64 => return "arm64",
- .thumb,
- .arm,
- => return "arm",
- .powerpc => return "ppc",
- .powerpc64 => return "ppc64",
- .powerpc64le => return "ppc64le",
- else => return @tagName(arch),
- }
-}
diff --git a/src-self-hosted/util.zig b/src-self-hosted/util.zig
@@ -0,0 +1,209 @@
+const std = @import("std");
+const Target = std.Target;
+const llvm = @import("llvm.zig");
+// const builtin = @import("builtin");
+
+pub const FloatAbi = enum {
+ Hard,
+ Soft,
+ SoftFp,
+};
+
+// pub const Cross = struct {
+// arch: Target.Arch,
+// os: Target.Os,
+// abi: Target.Abi,
+// object_format: builtin.ObjectFormat,
+// };
+
+// pub fn getObjectFormat(self: Target) builtin.ObjectFormat {
+// return switch (self) {
+// .Native => builtin.object_format,
+// .Cross => |t| t.object_format,
+// };
+// }
+
+/// TODO expose the arch and subarch separately
+pub fn isArmOrThumb(self: Target) bool {
+ return switch (self.getArch()) {
+ .arm,
+ .armeb,
+ .aarch64,
+ .aarch64_be,
+ .thumb,
+ .thumbeb,
+ => true,
+ else => false,
+ };
+}
+
+pub fn getFloatAbi(self: Target) FloatAbi {
+ return switch (self.getAbi()) {
+ .gnueabihf,
+ .eabihf,
+ .musleabihf,
+ => .Hard,
+ else => .Soft,
+ };
+}
+
+pub fn getDynamicLinkerPath(self: Target) ?[]const u8 {
+ const env = self.getAbi();
+ const arch = self.getArch();
+ const os = self.getOs();
+ switch (os) {
+ .freebsd => {
+ return "/libexec/ld-elf.so.1";
+ },
+ .linux => {
+ switch (env) {
+ .android => {
+ if (self.getArchPtrBitWidth() == 64) {
+ return "/system/bin/linker64";
+ } else {
+ return "/system/bin/linker";
+ }
+ },
+ .gnux32 => {
+ if (arch == .x86_64) {
+ return "/libx32/ld-linux-x32.so.2";
+ }
+ },
+ .musl,
+ .musleabi,
+ .musleabihf,
+ => {
+ if (arch == .x86_64) {
+ return "/lib/ld-musl-x86_64.so.1";
+ }
+ },
+ else => {},
+ }
+ switch (arch) {
+ .i386,
+ .sparc,
+ .sparcel,
+ => return "/lib/ld-linux.so.2",
+
+ .aarch64 => return "/lib/ld-linux-aarch64.so.1",
+
+ .aarch64_be => return "/lib/ld-linux-aarch64_be.so.1",
+
+ .arm,
+ .thumb,
+ => return switch (getFloatAbi(self)) {
+ .Hard => return "/lib/ld-linux-armhf.so.3",
+ else => return "/lib/ld-linux.so.3",
+ },
+
+ .armeb,
+ .thumbeb,
+ => return switch (getFloatAbi(self)) {
+ .Hard => return "/lib/ld-linux-armhf.so.3",
+ else => return "/lib/ld-linux.so.3",
+ },
+
+ .mips,
+ .mipsel,
+ .mips64,
+ .mips64el,
+ => return null,
+
+ .powerpc => return "/lib/ld.so.1",
+ .powerpc64 => return "/lib64/ld64.so.2",
+ .powerpc64le => return "/lib64/ld64.so.2",
+ .s390x => return "/lib64/ld64.so.1",
+ .sparcv9 => return "/lib64/ld-linux.so.2",
+ .x86_64 => return "/lib64/ld-linux-x86-64.so.2",
+
+ .arc,
+ .avr,
+ .bpfel,
+ .bpfeb,
+ .hexagon,
+ .msp430,
+ .r600,
+ .amdgcn,
+ .riscv32,
+ .riscv64,
+ .tce,
+ .tcele,
+ .xcore,
+ .nvptx,
+ .nvptx64,
+ .le32,
+ .le64,
+ .amdil,
+ .amdil64,
+ .hsail,
+ .hsail64,
+ .spir,
+ .spir64,
+ .kalimba,
+ .shave,
+ .lanai,
+ .wasm32,
+ .wasm64,
+ .renderscript32,
+ .renderscript64,
+ .aarch64_32,
+ => return null,
+ }
+ },
+ else => return null,
+ }
+}
+
+pub fn getDarwinArchString(self: Target) []const u8 {
+ const arch = self.getArch();
+ switch (arch) {
+ .aarch64 => return "arm64",
+ .thumb,
+ .arm,
+ => return "arm",
+ .powerpc => return "ppc",
+ .powerpc64 => return "ppc64",
+ .powerpc64le => return "ppc64le",
+ 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;
+}