Commit d40f204ec0

Vexu <15308111+Vexu@users.noreply.github.com>
2019-11-23 19:15:59
self hosted compiler: small miscellaneous fixes
1 parent 133579d
src-self-hosted/codegen.zig
@@ -6,6 +6,7 @@ const ir = @import("ir.zig");
 const Value = @import("value.zig").Value;
 const Type = @import("type.zig").Type;
 const Scope = @import("scope.zig").Scope;
+const util = @import("util.zig");
 const event = std.event;
 const assert = std.debug.assert;
 const DW = std.dwarf;
@@ -30,11 +31,11 @@ pub async fn renderToLlvm(comp: *Compilation, fn_val: *Value.Fn, code: *ir.Code)
     llvm.SetTarget(module, comp.llvm_triple.ptr());
     llvm.SetDataLayout(module, comp.target_layout_str);
 
-    // if (comp.target.getObjectFormat() == .coff) {
-    //     llvm.AddModuleCodeViewFlag(module);
-    // } else {
+    if (util.getObjectFormat(comp.target) == .coff) {
+        llvm.AddModuleCodeViewFlag(module);
+    } else {
         llvm.AddModuleDebugInfoFlag(module);
-    // }
+    }
 
     const builder = llvm.CreateBuilderInContext(context) orelse return error.OutOfMemory;
     defer llvm.DisposeBuilder(builder);
src-self-hosted/compilation.zig
@@ -858,7 +858,6 @@ pub const Compilation = struct {
         defer locked_table.release();
 
         var decl_group = event.Group(BuildError!void).init(self.gpa());
-        defer decl_group.wait() catch {};
 
         try self.rebuildChangedDecls(
             &decl_group,
src-self-hosted/libc_installation.zig
@@ -146,7 +146,7 @@ pub const LibCInstallation = struct {
     pub async fn findNative(self: *LibCInstallation, allocator: *Allocator) !void {
         self.initEmpty();
         var group = event.Group(FindError!void).init(allocator);
-        // errdefer group.deinit();
+        errdefer group.wait() catch {};
         var windows_sdk: ?*c.ZigWindowsSDK = null;
         errdefer if (windows_sdk) |sdk| c.zig_free_windows_sdk(@ptrCast(?[*]c.ZigWindowsSDK, sdk));
 
src-self-hosted/link.zig
@@ -78,7 +78,7 @@ pub async fn link(comp: *Compilation) !void {
         std.debug.warn("\n");
     }
 
-    const extern_ofmt = toExternObjectFormatType(.elf); //comp.target.getObjectFormat());
+    const extern_ofmt = toExternObjectFormatType(util.getObjectFormat(comp.target));
     const args_slice = ctx.args.toSlice();
 
     {
@@ -130,14 +130,13 @@ fn toExternObjectFormatType(ofmt: ObjectFormat) c.ZigLLVM_ObjectFormatType {
 }
 
 fn constructLinkerArgs(ctx: *Context) !void {
-    return constructLinkerArgsElf(ctx);
-    // switch (ctx.comp.target.getObjectFormat()) {
-    //     .unknown => unreachable,
-    //     .coff => return constructLinkerArgsCoff(ctx),
-    //     .elf => return constructLinkerArgsElf(ctx),
-    //     .macho => return constructLinkerArgsMachO(ctx),
-    //     .wasm => return constructLinkerArgsWasm(ctx),
-    // }
+    switch (util.getObjectFormat(ctx.comp.target)) {
+        .unknown => unreachable,
+        .coff => return constructLinkerArgsCoff(ctx),
+        .elf => return constructLinkerArgsElf(ctx),
+        .macho => return constructLinkerArgsMachO(ctx),
+        .wasm => return constructLinkerArgsWasm(ctx),
+    }
 }
 
 fn constructLinkerArgsElf(ctx: *Context) !void {
src-self-hosted/llvm.zig
@@ -1,6 +1,5 @@
 const c = @import("c.zig");
-const std = @import("std");
-const assert = std.debug.assert;
+const assert = @import("std").debug.assert;
 
 // we wrap the c module for 3 reasons:
 // 1. to avoid accidentally calling the non-thread-safe functions
src-self-hosted/main.zig
@@ -260,47 +260,47 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co
         process.exit(0);
     }
 
-    const build_mode = blk: {
+    const build_mode: std.builtin.Mode = blk: {
         if (flags.single("mode")) |mode_flag| {
             if (mem.eql(u8, mode_flag, "debug")) {
-                break :blk std.builtin.Mode.Debug;
+                break :blk .Debug;
             } else if (mem.eql(u8, mode_flag, "release-fast")) {
-                break :blk std.builtin.Mode.ReleaseFast;
+                break :blk .ReleaseFast;
             } else if (mem.eql(u8, mode_flag, "release-safe")) {
-                break :blk std.builtin.Mode.ReleaseSafe;
+                break :blk .ReleaseSafe;
             } else if (mem.eql(u8, mode_flag, "release-small")) {
-                break :blk std.builtin.Mode.ReleaseSmall;
+                break :blk .ReleaseSmall;
             } else unreachable;
         } else {
-            break :blk std.builtin.Mode.Debug;
+            break :blk .Debug;
         }
     };
 
-    const color = blk: {
+    const color: errmsg.Color = blk: {
         if (flags.single("color")) |color_flag| {
             if (mem.eql(u8, color_flag, "auto")) {
-                break :blk errmsg.Color.Auto;
+                break :blk .Auto;
             } else if (mem.eql(u8, color_flag, "on")) {
-                break :blk errmsg.Color.On;
+                break :blk .On;
             } else if (mem.eql(u8, color_flag, "off")) {
-                break :blk errmsg.Color.Off;
+                break :blk .Off;
             } else unreachable;
         } else {
-            break :blk errmsg.Color.Auto;
+            break :blk .Auto;
         }
     };
 
-    const emit_type = blk: {
+    const emit_type: Compilation.Emit = blk: {
         if (flags.single("emit")) |emit_flag| {
             if (mem.eql(u8, emit_flag, "asm")) {
-                break :blk Compilation.Emit.Assembly;
+                break :blk .Assembly;
             } else if (mem.eql(u8, emit_flag, "bin")) {
-                break :blk Compilation.Emit.Binary;
+                break :blk .Binary;
             } else if (mem.eql(u8, emit_flag, "llvm-ir")) {
-                break :blk Compilation.Emit.LlvmIr;
+                break :blk .LlvmIr;
             } else unreachable;
         } else {
-            break :blk Compilation.Emit.Binary;
+            break :blk .Binary;
         }
     };
 
@@ -587,17 +587,17 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void {
         process.exit(0);
     }
 
-    const color = blk: {
+    const color: errmsg.Color = blk: {
         if (flags.single("color")) |color_flag| {
             if (mem.eql(u8, color_flag, "auto")) {
-                break :blk errmsg.Color.Auto;
+                break :blk .Auto;
             } else if (mem.eql(u8, color_flag, "on")) {
-                break :blk errmsg.Color.On;
+                break :blk .On;
             } else if (mem.eql(u8, color_flag, "off")) {
-                break :blk errmsg.Color.Off;
+                break :blk .Off;
             } else unreachable;
         } else {
-            break :blk errmsg.Color.Auto;
+            break :blk .Auto;
         }
     };
 
src-self-hosted/test.zig
@@ -16,7 +16,7 @@ test "stage2" {
     try @import("../test/stage2/compile_errors.zig").addCases(&ctx);
     try @import("../test/stage2/compare_output.zig").addCases(&ctx);
 
-    try ctx.run();
+    async ctx.run();
 }
 
 const file1 = "1.zig";
@@ -44,7 +44,7 @@ pub const TestContext = struct {
         errdefer self.zig_compiler.deinit();
 
         self.group = std.event.Group(anyerror!void).init(allocator);
-        errdefer self.group.wait();
+        errdefer self.group.wait() catch {};
 
         self.zig_lib_dir = try introspect.resolveZigLibDir(allocator);
         errdefer allocator.free(self.zig_lib_dir);
@@ -59,14 +59,10 @@ pub const TestContext = struct {
         self.zig_compiler.deinit();
     }
 
-    fn run(self: *TestContext) !void {
-        const handle = try std.event.Loop.instance.?.call(waitForGroup, self);
-        await handle;
-        return self.any_err;
-    }
-
-    async fn waitForGroup(self: *TestContext) void {
+    fn run(self: *TestContext) void {
+        std.event.Loop.instance.?.startCpuBoundOperation();
         self.any_err = self.group.wait();
+        return self.any_err;
     }
 
     fn testCompileError(
@@ -173,7 +169,7 @@ pub const TestContext = struct {
             },
             Compilation.Event.Error => |err| return err,
             Compilation.Event.Fail => |msgs| {
-                var stderr = try std.io.getStdErr();
+                const stderr = std.io.getStdErr();
                 try stderr.write("build incorrectly failed:\n");
                 for (msgs) |msg| {
                     defer msg.destroy();
src-self-hosted/translate_c.zig
@@ -12,7 +12,6 @@ pub const Mode = enum {
     translate,
 };
 
-// TODO merge with Type.Fn.CallingConvention
 const CallingConvention = std.builtin.TypeInfo.CallingConvention;
 
 pub const ClangErrMsg = Stage2ErrorMsg;
src-self-hosted/type.zig
@@ -399,8 +399,7 @@ pub const Type = struct {
                 .Generic => |generic| {
                     self.non_key = NonKey{ .Generic = {} };
                     const cc_str = ccFnTypeStr(generic.cc);
-                    try name_stream.write(cc_str);
-                    try name_stream.write("fn(");
+                    try name_stream.print("{}fn(", cc_str);
                     var param_i: usize = 0;
                     while (param_i < generic.param_count) : (param_i += 1) {
                         const arg = if (param_i == 0) "var" else ", var";
@@ -408,7 +407,7 @@ pub const Type = struct {
                     }
                     try name_stream.write(")");
                     if (key.alignment) |alignment| {
-                        try name_stream.print(" align<{}>", alignment);
+                        try name_stream.print(" align({})", alignment);
                     }
                     try name_stream.write(" var");
                 },
@@ -429,7 +428,7 @@ pub const Type = struct {
                     }
                     try name_stream.write(")");
                     if (key.alignment) |alignment| {
-                        try name_stream.print(" align<{}>", alignment);
+                        try name_stream.print(" align({})", alignment);
                     }
                     try name_stream.print(" {}", normal.return_type.name);
                 },
src-self-hosted/util.zig
@@ -32,6 +32,23 @@ pub fn getFloatAbi(self: Target) FloatAbi {
     };
 }
 
+pub fn getObjectFormat(self: Target) Target.ObjectFormat {
+    return switch (self) {
+        .Native => @import("builtin").object_format,
+        .Cross => {
+            if (target.isWindows() or target.isUefi()) {
+                break .coff;
+            } else if (target.isDarwin()) {
+                break .macho;
+            }
+            if (target.isWasm()) {
+                break .wasm;
+            }
+            break .elf;
+        },
+    };
+}
+
 pub fn getDynamicLinkerPath(self: Target) ?[]const u8 {
     const env = self.getAbi();
     const arch = self.getArch();