authorgravatar for jacoblevgw@gmail.comJacob G-W <jacoblevgw@gmail.com> 2021-03-19 08:51:09-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-03-19 14:46:37-07:00
logc50397c2682846635d1ed116fcd5fe98e8f08c81
treedede42b62f886b26eb17a854ea0dd3bb8f6562b8
parente9810d9e79a1aa327d006c27c5d3098b2d29dfe7

llvm backend: use new srcloc

this allows to compile with ninja

2 files changed, 24 insertions(+), 16 deletions(-)

src/Module.zig+8-5
...@@ -2337,11 +2337,14 @@ fn astgenAndSemaVarDecl(...@@ -2337,11 +2337,14 @@ fn astgenAndSemaVarDecl(
2337 };2337 };
2338 defer gen_scope.instructions.deinit(mod.gpa);2338 defer gen_scope.instructions.deinit(mod.gpa);
23392339
2340 const init_result_loc: astgen.ResultLoc = if (var_decl.ast.type_node != 0) .{2340 const init_result_loc: astgen.ResultLoc = if (var_decl.ast.type_node != 0)
2341 .ty = try astgen.expr(mod, &gen_scope.base, .{2341 .{
2342 .ty = @enumToInt(zir.Const.type_type),2342 .ty = try astgen.expr(mod, &gen_scope.base, .{
2343 }, var_decl.ast.type_node),2343 .ty = @enumToInt(zir.Const.type_type),
2344 } else .none;2344 }, var_decl.ast.type_node),
2345 }
2346 else
2347 .none;
23452348
2346 const init_inst = try astgen.comptimeExpr(2349 const init_inst = try astgen.comptimeExpr(
2347 mod,2350 mod,
src/codegen/llvm.zig+16-11
...@@ -15,6 +15,9 @@ const Inst = ir.Inst;...@@ -15,6 +15,9 @@ const Inst = ir.Inst;
15const Value = @import("../value.zig").Value;15const Value = @import("../value.zig").Value;
16const Type = @import("../type.zig").Type;16const Type = @import("../type.zig").Type;
1717
18const LazySrcLoc = Module.LazySrcLoc;
19const SrcLoc = Module.SrcLoc;
20
18pub fn targetTriple(allocator: *Allocator, target: std.Target) ![:0]u8 {21pub fn targetTriple(allocator: *Allocator, target: std.Target) ![:0]u8 {
19 const llvm_arch = switch (target.cpu.arch) {22 const llvm_arch = switch (target.cpu.arch) {
20 .arm => "arm",23 .arm => "arm",
...@@ -158,6 +161,10 @@ pub const LLVMIRModule = struct {...@@ -158,6 +161,10 @@ pub const LLVMIRModule = struct {
158 // TODO: The fields below should really move into a different struct,161 // TODO: The fields below should really move into a different struct,
159 // because they are only valid when generating a function162 // because they are only valid when generating a function
160163
164 /// TODO: this should not be undefined since it should be in another per-decl struct
165 /// Curent decl we are analysing. Stored to get source locations from relative info
166 decl: *Module.Decl = undefined,
167
161 /// This stores the LLVM values used in a function, such that they can be168 /// This stores the LLVM values used in a function, such that they can be
162 /// referred to in other instructions. This table is cleared before every function is generated.169 /// referred to in other instructions. This table is cleared before every function is generated.
163 /// TODO: Change this to a stack of Branch. Currently we store all the values from all the blocks170 /// TODO: Change this to a stack of Branch. Currently we store all the values from all the blocks
...@@ -342,9 +349,9 @@ pub const LLVMIRModule = struct {...@@ -342,9 +349,9 @@ pub const LLVMIRModule = struct {
342349
343 fn gen(self: *LLVMIRModule, module: *Module, decl: *Module.Decl) !void {350 fn gen(self: *LLVMIRModule, module: *Module, decl: *Module.Decl) !void {
344 const typed_value = decl.typed_value.most_recent.typed_value;351 const typed_value = decl.typed_value.most_recent.typed_value;
345 const src = decl.src();
346
347 self.src_loc = decl.srcLoc();352 self.src_loc = decl.srcLoc();
353 self.decl = decl;
354 const src = self.src_loc.lazy;
348355
349 log.debug("gen: {s} type: {}, value: {}", .{ decl.name, typed_value.ty, typed_value.val });356 log.debug("gen: {s} type: {}, value: {}", .{ decl.name, typed_value.ty, typed_value.val });
350357
...@@ -765,7 +772,7 @@ pub const LLVMIRModule = struct {...@@ -765,7 +772,7 @@ pub const LLVMIRModule = struct {
765 return self.fail(inst.src, "TODO implement global llvm values (or the value is not in the func_inst_table table)", .{});772 return self.fail(inst.src, "TODO implement global llvm values (or the value is not in the func_inst_table table)", .{});
766 }773 }
767774
768 fn genTypedValue(self: *LLVMIRModule, src: usize, tv: TypedValue) error{ OutOfMemory, CodegenFail }!*const llvm.Value {775 fn genTypedValue(self: *LLVMIRModule, src: LazySrcLoc, tv: TypedValue) error{ OutOfMemory, CodegenFail }!*const llvm.Value {
769 const llvm_type = try self.getLLVMType(tv.ty, src);776 const llvm_type = try self.getLLVMType(tv.ty, src);
770777
771 if (tv.val.isUndef())778 if (tv.val.isUndef())
...@@ -852,7 +859,7 @@ pub const LLVMIRModule = struct {...@@ -852,7 +859,7 @@ pub const LLVMIRModule = struct {
852 }859 }
853 }860 }
854861
855 fn getLLVMType(self: *LLVMIRModule, t: Type, src: usize) error{ OutOfMemory, CodegenFail }!*const llvm.Type {862 fn getLLVMType(self: *LLVMIRModule, t: Type, src: LazySrcLoc) error{ OutOfMemory, CodegenFail }!*const llvm.Type {
856 switch (t.zigTypeTag()) {863 switch (t.zigTypeTag()) {
857 .Void => return self.context.voidType(),864 .Void => return self.context.voidType(),
858 .NoReturn => return self.context.voidType(),865 .NoReturn => return self.context.voidType(),
...@@ -891,7 +898,7 @@ pub const LLVMIRModule = struct {...@@ -891,7 +898,7 @@ pub const LLVMIRModule = struct {
891 }898 }
892 }899 }
893900
894 fn resolveGlobalDecl(self: *LLVMIRModule, decl: *Module.Decl, src: usize) error{ OutOfMemory, CodegenFail }!*const llvm.Value {901 fn resolveGlobalDecl(self: *LLVMIRModule, decl: *Module.Decl, src: LazySrcLoc) error{ OutOfMemory, CodegenFail }!*const llvm.Value {
895 // TODO: do we want to store this in our own datastructure?902 // TODO: do we want to store this in our own datastructure?
896 if (self.llvm_module.getNamedGlobal(decl.name)) |val| return val;903 if (self.llvm_module.getNamedGlobal(decl.name)) |val| return val;
897904
...@@ -910,7 +917,7 @@ pub const LLVMIRModule = struct {...@@ -910,7 +917,7 @@ pub const LLVMIRModule = struct {
910 }917 }
911918
912 /// If the llvm function does not exist, create it919 /// If the llvm function does not exist, create it
913 fn resolveLLVMFunction(self: *LLVMIRModule, func: *Module.Decl, src: usize) !*const llvm.Value {920 fn resolveLLVMFunction(self: *LLVMIRModule, func: *Module.Decl, src: LazySrcLoc) !*const llvm.Value {
914 // TODO: do we want to store this in our own datastructure?921 // TODO: do we want to store this in our own datastructure?
915 if (self.llvm_module.getNamedFunction(func.name)) |llvm_fn| return llvm_fn;922 if (self.llvm_module.getNamedFunction(func.name)) |llvm_fn| return llvm_fn;
916923
...@@ -958,13 +965,11 @@ pub const LLVMIRModule = struct {...@@ -958,13 +965,11 @@ pub const LLVMIRModule = struct {
958 self.addAttr(val, std.math.maxInt(llvm.AttributeIndex), attr_name);965 self.addAttr(val, std.math.maxInt(llvm.AttributeIndex), attr_name);
959 }966 }
960967
961 pub fn fail(self: *LLVMIRModule, src: usize, comptime format: []const u8, args: anytype) error{ OutOfMemory, CodegenFail } {968 pub fn fail(self: *LLVMIRModule, src: LazySrcLoc, comptime format: []const u8, args: anytype) error{ OutOfMemory, CodegenFail } {
962 @setCold(true);969 @setCold(true);
963 assert(self.err_msg == null);970 assert(self.err_msg == null);
964 self.err_msg = try Module.ErrorMsg.create(self.gpa, .{971 const src_loc = src.toSrcLocWithDecl(self.decl);
965 .file_scope = self.src_loc.file_scope,972 self.err_msg = try Module.ErrorMsg.create(self.gpa, src_loc, format, args);
966 .byte_offset = src,
967 }, format, args);
968 return error.CodegenFail;973 return error.CodegenFail;
969 }974 }
970};975};