authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2019-11-08 00:18:14+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2019-11-08 00:18:14+02:00
log86d9563d1539cd7581dc120023bb830fae77ba0f
treee06c9f1131da348cc8ee7e0c705dbf58572b0c77
parentbca672372aa57e97fcfb76672fe02f8ca6c2d00e
signaturelock-open Commit is signed but in an unrecognized format.

self hosted compiler: various small fixes


7 files changed, 38 insertions(+), 48 deletions(-)

src-self-hosted/codegen.zig+7-7
...@@ -11,12 +11,12 @@ const assert = std.debug.assert;...@@ -11,12 +11,12 @@ const assert = std.debug.assert;
11const DW = std.dwarf;11const DW = std.dwarf;
12const maxInt = std.math.maxInt;12const maxInt = std.math.maxInt;
1313
14pub async fn renderToLlvm(comp: *Compilation, fn_val: *Value.Fn, code: *ir.Code) !void {14pub async fn renderToLlvm(comp: *Compilation, fn_val: *Value.Fn, code: *ir.Code) Compilation.BuildError!void {
15 fn_val.base.ref();15 fn_val.base.ref();
16 defer fn_val.base.deref(comp);16 defer fn_val.base.deref(comp);
17 defer code.destroy(comp.gpa());17 defer code.destroy(comp.gpa());
1818
19 var output_path = try comp.createRandomOutputPath(comp.target.objFileExt());19 var output_path = try comp.createRandomOutputPath(comp.target.oFileExt());
20 errdefer output_path.deinit();20 errdefer output_path.deinit();
2121
22 const llvm_handle = try comp.zig_compiler.getAnyLlvmContext();22 const llvm_handle = try comp.zig_compiler.getAnyLlvmContext();
...@@ -30,11 +30,11 @@ pub async fn renderToLlvm(comp: *Compilation, fn_val: *Value.Fn, code: *ir.Code)...@@ -30,11 +30,11 @@ pub async fn renderToLlvm(comp: *Compilation, fn_val: *Value.Fn, code: *ir.Code)
30 llvm.SetTarget(module, comp.llvm_triple.ptr());30 llvm.SetTarget(module, comp.llvm_triple.ptr());
31 llvm.SetDataLayout(module, comp.target_layout_str);31 llvm.SetDataLayout(module, comp.target_layout_str);
3232
33 if (comp.target.getObjectFormat() == .coff) {33 // if (comp.target.getObjectFormat() == .coff) {
34 llvm.AddModuleCodeViewFlag(module);34 // llvm.AddModuleCodeViewFlag(module);
35 } else {35 // } else {
36 llvm.AddModuleDebugInfoFlag(module);36 llvm.AddModuleDebugInfoFlag(module);
37 }37 // }
3838
39 const builder = llvm.CreateBuilderInContext(context) orelse return error.OutOfMemory;39 const builder = llvm.CreateBuilderInContext(context) orelse return error.OutOfMemory;
40 defer llvm.DisposeBuilder(builder);40 defer llvm.DisposeBuilder(builder);
...@@ -113,7 +113,7 @@ pub async fn renderToLlvm(comp: *Compilation, fn_val: *Value.Fn, code: *ir.Code)...@@ -113,7 +113,7 @@ pub async fn renderToLlvm(comp: *Compilation, fn_val: *Value.Fn, code: *ir.Code)
113 comp.target_machine,113 comp.target_machine,
114 module,114 module,
115 output_path.ptr(),115 output_path.ptr(),
116 .EmitBinary,116 llvm.EmitBinary,
117 &err_msg,117 &err_msg,
118 is_debug,118 is_debug,
119 is_small,119 is_small,
src-self-hosted/compilation.zig+7-7
...@@ -857,7 +857,7 @@ pub const Compilation = struct {...@@ -857,7 +857,7 @@ pub const Compilation = struct {
857 defer locked_table.release();857 defer locked_table.release();
858858
859 var decl_group = event.Group(BuildError!void).init(self.gpa());859 var decl_group = event.Group(BuildError!void).init(self.gpa());
860 defer decl_group.deinit();860 defer decl_group.wait() catch {};
861861
862 try self.rebuildChangedDecls(862 try self.rebuildChangedDecls(
863 &decl_group,863 &decl_group,
...@@ -937,7 +937,7 @@ pub const Compilation = struct {...@@ -937,7 +937,7 @@ pub const Compilation = struct {
937 .parent_scope = &decl_scope.base,937 .parent_scope = &decl_scope.base,
938 .tree_scope = tree_scope,938 .tree_scope = tree_scope,
939 },939 },
940 .value = Decl.Fn.Val{ .Unresolved = {} },940 .value = .Unresolved,
941 .fn_proto = fn_proto,941 .fn_proto = fn_proto,
942 };942 };
943 tree_scope.base.ref();943 tree_scope.base.ref();
...@@ -1100,7 +1100,7 @@ pub const Compilation = struct {...@@ -1100,7 +1100,7 @@ pub const Compilation = struct {
1100 async fn addCompileErrorAsync(1100 async fn addCompileErrorAsync(
1101 self: *Compilation,1101 self: *Compilation,
1102 msg: *Msg,1102 msg: *Msg,
1103 ) !void {1103 ) BuildError!void {
1104 errdefer msg.destroy();1104 errdefer msg.destroy();
11051105
1106 const compile_errors = self.compile_errors.acquire();1106 const compile_errors = self.compile_errors.acquire();
...@@ -1109,7 +1109,7 @@ pub const Compilation = struct {...@@ -1109,7 +1109,7 @@ pub const Compilation = struct {
1109 try compile_errors.value.append(msg);1109 try compile_errors.value.append(msg);
1110 }1110 }
11111111
1112 async fn verifyUniqueSymbol(self: *Compilation, decl: *Decl) !void {1112 async fn verifyUniqueSymbol(self: *Compilation, decl: *Decl) BuildError!void {
1113 const exported_symbol_names = self.exported_symbol_names.acquire();1113 const exported_symbol_names = self.exported_symbol_names.acquire();
1114 defer exported_symbol_names.release();1114 defer exported_symbol_names.release();
11151115
...@@ -1264,7 +1264,7 @@ pub const Compilation = struct {...@@ -1264,7 +1264,7 @@ pub const Compilation = struct {
1264 }1264 }
12651265
1266 /// This declaration has been blessed as going into the final code generation.1266 /// This declaration has been blessed as going into the final code generation.
1267 pub async fn resolveDecl(comp: *Compilation, decl: *Decl) !void {1267 pub async fn resolveDecl(comp: *Compilation, decl: *Decl) BuildError!void {
1268 if (decl.resolution.start()) |ptr| return ptr.*;1268 if (decl.resolution.start()) |ptr| return ptr.*;
12691269
1270 decl.resolution.data = try generateDecl(comp, decl);1270 decl.resolution.data = try generateDecl(comp, decl);
...@@ -1363,7 +1363,7 @@ async fn generateDeclFn(comp: *Compilation, fn_decl: *Decl.Fn) !void {...@@ -1363,7 +1363,7 @@ async fn generateDeclFn(comp: *Compilation, fn_decl: *Decl.Fn) !void {
1363 try comp.prelink_group.call(addFnToLinkSet, comp, fn_val);1363 try comp.prelink_group.call(addFnToLinkSet, comp, fn_val);
1364}1364}
13651365
1366async fn addFnToLinkSet(comp: *Compilation, fn_val: *Value.Fn) void {1366async fn addFnToLinkSet(comp: *Compilation, fn_val: *Value.Fn) Compilation.BuildError!void {
1367 fn_val.base.ref();1367 fn_val.base.ref();
1368 defer fn_val.base.deref(comp);1368 defer fn_val.base.deref(comp);
13691369
...@@ -1421,7 +1421,7 @@ async fn analyzeFnType(...@@ -1421,7 +1421,7 @@ async fn analyzeFnType(
1421 .return_type = return_type,1421 .return_type = return_type,
1422 .params = params.toOwnedSlice(),1422 .params = params.toOwnedSlice(),
1423 .is_var_args = false, // TODO1423 .is_var_args = false, // TODO
1424 .cc = Type.Fn.CallingConvention.Auto, // TODO1424 .cc = .Unspecified, // TODO
1425 },1425 },
1426 },1426 },
1427 };1427 };
src-self-hosted/decl.zig+6-3
...@@ -69,12 +69,15 @@ pub const Decl = struct {...@@ -69,12 +69,15 @@ pub const Decl = struct {
6969
70 pub const Fn = struct {70 pub const Fn = struct {
71 base: Decl,71 base: Decl,
72 value: union(enum) {72 value: Val,
73 fn_proto: *ast.Node.FnProto,
74
75 // TODO https://github.com/ziglang/zig/issues/683 and then make this anonymous
76 pub const Val = union(enum) {
73 Unresolved,77 Unresolved,
74 Fn: *Value.Fn,78 Fn: *Value.Fn,
75 FnProto: *Value.FnProto,79 FnProto: *Value.FnProto,
76 },80 };
77 fn_proto: *ast.Node.FnProto,
7881
79 pub fn externLibName(self: Fn, tree: *ast.Tree) ?[]const u8 {82 pub fn externLibName(self: Fn, tree: *ast.Tree) ?[]const u8 {
80 return if (self.fn_proto.extern_export_inline_token) |tok_index| x: {83 return if (self.fn_proto.extern_export_inline_token) |tok_index| x: {
src-self-hosted/ir.zig+5-5
...@@ -521,7 +521,7 @@ pub const Inst = struct {...@@ -521,7 +521,7 @@ pub const Inst = struct {
521 .Const => @panic("TODO"),521 .Const => @panic("TODO"),
522 .Param => |param| {522 .Param => |param| {
523 const new_inst = try ira.irb.build(523 const new_inst = try ira.irb.build(
524 .VarPtr,524 Inst.VarPtr,
525 self.base.scope,525 self.base.scope,
526 self.base.span,526 self.base.span,
527 Inst.VarPtr.Params{ .var_scope = self.params.var_scope },527 Inst.VarPtr.Params{ .var_scope = self.params.var_scope },
...@@ -1134,7 +1134,7 @@ pub const Builder = struct {...@@ -1134,7 +1134,7 @@ pub const Builder = struct {
1134 .VarType => return error.Unimplemented,1134 .VarType => return error.Unimplemented,
1135 .ErrorType => return error.Unimplemented,1135 .ErrorType => return error.Unimplemented,
1136 .FnProto => return error.Unimplemented,1136 .FnProto => return error.Unimplemented,
1137 .PromiseType => return error.Unimplemented,1137 .AnyFrameType => return error.Unimplemented,
1138 .IntegerLiteral => {1138 .IntegerLiteral => {
1139 const int_lit = @fieldParentPtr(ast.Node.IntegerLiteral, "base", node);1139 const int_lit = @fieldParentPtr(ast.Node.IntegerLiteral, "base", node);
1140 return irb.lvalWrap(scope, try irb.genIntLit(int_lit, scope), lval);1140 return irb.lvalWrap(scope, try irb.genIntLit(int_lit, scope), lval);
...@@ -1812,9 +1812,9 @@ pub const Builder = struct {...@@ -1812,9 +1812,9 @@ pub const Builder = struct {
1812 for (@field(inst.params, @memberName(I.Params, i))) |other|1812 for (@field(inst.params, @memberName(I.Params, i))) |other|
1813 other.ref(self);1813 other.ref(self);
1814 },1814 },
1815 .Mut,1815 Type.Pointer.Mut,
1816 .Vol,1816 Type.Pointer.Vol,
1817 .Size,1817 Type.Pointer.Size,
1818 LVal,1818 LVal,
1819 *Decl,1819 *Decl,
1820 *Scope.Var,1820 *Scope.Var,
src-self-hosted/link.zig+12-10
...@@ -6,6 +6,7 @@ const Target = std.Target;...@@ -6,6 +6,7 @@ const Target = std.Target;
6const ObjectFormat = Target.ObjectFormat;6const ObjectFormat = Target.ObjectFormat;
7const LibCInstallation = @import("libc_installation.zig").LibCInstallation;7const LibCInstallation = @import("libc_installation.zig").LibCInstallation;
8const assert = std.debug.assert;8const assert = std.debug.assert;
9const util = @import("util.zig");
910
10const Context = struct {11const Context = struct {
11 comp: *Compilation,12 comp: *Compilation,
...@@ -44,10 +45,10 @@ pub async fn link(comp: *Compilation) !void {...@@ -44,10 +45,10 @@ pub async fn link(comp: *Compilation) !void {
44 try ctx.out_file_path.append(comp.target.exeFileExt());45 try ctx.out_file_path.append(comp.target.exeFileExt());
45 },46 },
46 .Lib => {47 .Lib => {
47 try ctx.out_file_path.append(comp.target.libFileExt(comp.is_static));48 try ctx.out_file_path.append(if (comp.is_static) comp.target.staticLibSuffix() else comp.target.dynamicLibSuffix());
48 },49 },
49 .Obj => {50 .Obj => {
50 try ctx.out_file_path.append(comp.target.objFileExt());51 try ctx.out_file_path.append(comp.target.oFileExt());
51 },52 },
52 }53 }
53 }54 }
...@@ -77,7 +78,7 @@ pub async fn link(comp: *Compilation) !void {...@@ -77,7 +78,7 @@ pub async fn link(comp: *Compilation) !void {
77 std.debug.warn("\n");78 std.debug.warn("\n");
78 }79 }
7980
80 const extern_ofmt = toExternObjectFormatType(comp.target.getObjectFormat());81 const extern_ofmt = toExternObjectFormatType(.elf); //comp.target.getObjectFormat());
81 const args_slice = ctx.args.toSlice();82 const args_slice = ctx.args.toSlice();
8283
83 {84 {
...@@ -129,13 +130,14 @@ fn toExternObjectFormatType(ofmt: ObjectFormat) c.ZigLLVM_ObjectFormatType {...@@ -129,13 +130,14 @@ fn toExternObjectFormatType(ofmt: ObjectFormat) c.ZigLLVM_ObjectFormatType {
129}130}
130131
131fn constructLinkerArgs(ctx: *Context) !void {132fn constructLinkerArgs(ctx: *Context) !void {
132 switch (ctx.comp.target.getObjectFormat()) {133 return constructLinkerArgsElf(ctx);
133 .unknown => unreachable,134 // switch (ctx.comp.target.getObjectFormat()) {
134 .coff => return constructLinkerArgsCoff(ctx),135 // .unknown => unreachable,
135 .elf => return constructLinkerArgsElf(ctx),136 // .coff => return constructLinkerArgsCoff(ctx),
136 .macho => return constructLinkerArgsMachO(ctx),137 // .elf => return constructLinkerArgsElf(ctx),
137 .wasm => return constructLinkerArgsWasm(ctx),138 // .macho => return constructLinkerArgsMachO(ctx),
138 }139 // .wasm => return constructLinkerArgsWasm(ctx),
140 // }
139}141}
140142
141fn constructLinkerArgsElf(ctx: *Context) !void {143fn constructLinkerArgsElf(ctx: *Context) !void {
src-self-hosted/type.zig+1-1
...@@ -350,7 +350,7 @@ pub const Type = struct {...@@ -350,7 +350,7 @@ pub const Type = struct {
350350
351 fn ccFnTypeStr(cc: CallingConvention) []const u8 {351 fn ccFnTypeStr(cc: CallingConvention) []const u8 {
352 return switch (cc) {352 return switch (cc) {
353 .Auto => "",353 .Unspecified => "",
354 .C => "extern ",354 .C => "extern ",
355 .Cold => "coldcc ",355 .Cold => "coldcc ",
356 .Naked => "nakedcc ",356 .Naked => "nakedcc ",
src-self-hosted/util.zig-15
...@@ -1,7 +1,6 @@...@@ -1,7 +1,6 @@
1const std = @import("std");1const std = @import("std");
2const Target = std.Target;2const Target = std.Target;
3const llvm = @import("llvm.zig");3const llvm = @import("llvm.zig");
4// const builtin = @import("builtin");
54
6pub const FloatAbi = enum {5pub const FloatAbi = enum {
7 Hard,6 Hard,
...@@ -9,20 +8,6 @@ pub const FloatAbi = enum {...@@ -9,20 +8,6 @@ pub const FloatAbi = enum {
9 SoftFp,8 SoftFp,
10};9};
1110
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 separately11/// TODO expose the arch and subarch separately
27pub fn isArmOrThumb(self: Target) bool {12pub fn isArmOrThumb(self: Target) bool {
28 return switch (self.getArch()) {13 return switch (self.getArch()) {