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(
23372337 };
23382338 defer gen_scope.instructions.deinit(mod.gpa);
23392339
2340 const init_result_loc: astgen.ResultLoc = if (var_decl.ast.type_node != 0) .{
2341 .ty = try astgen.expr(mod, &gen_scope.base, .{
2342 .ty = @enumToInt(zir.Const.type_type),
2343 }, var_decl.ast.type_node),
2344 } else .none;
2340 const init_result_loc: astgen.ResultLoc = if (var_decl.ast.type_node != 0)
2341 .{
2342 .ty = try astgen.expr(mod, &gen_scope.base, .{
2343 .ty = @enumToInt(zir.Const.type_type),
2344 }, var_decl.ast.type_node),
2345 }
2346 else
2347 .none;
23452348
23462349 const init_inst = try astgen.comptimeExpr(
23472350 mod,
src/codegen/llvm.zig+16-11
......@@ -15,6 +15,9 @@ const Inst = ir.Inst;
1515const Value = @import("../value.zig").Value;
1616const Type = @import("../type.zig").Type;
1717
18const LazySrcLoc = Module.LazySrcLoc;
19const SrcLoc = Module.SrcLoc;
20
1821pub fn targetTriple(allocator: *Allocator, target: std.Target) ![:0]u8 {
1922 const llvm_arch = switch (target.cpu.arch) {
2023 .arm => "arm",
......@@ -158,6 +161,10 @@ pub const LLVMIRModule = struct {
158161 // TODO: The fields below should really move into a different struct,
159162 // 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
161168 /// This stores the LLVM values used in a function, such that they can be
162169 /// referred to in other instructions. This table is cleared before every function is generated.
163170 /// 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 {
342349
343350 fn gen(self: *LLVMIRModule, module: *Module, decl: *Module.Decl) !void {
344351 const typed_value = decl.typed_value.most_recent.typed_value;
345 const src = decl.src();
346
347352 self.src_loc = decl.srcLoc();
353 self.decl = decl;
354 const src = self.src_loc.lazy;
348355
349356 log.debug("gen: {s} type: {}, value: {}", .{ decl.name, typed_value.ty, typed_value.val });
350357
......@@ -765,7 +772,7 @@ pub const LLVMIRModule = struct {
765772 return self.fail(inst.src, "TODO implement global llvm values (or the value is not in the func_inst_table table)", .{});
766773 }
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 {
769776 const llvm_type = try self.getLLVMType(tv.ty, src);
770777
771778 if (tv.val.isUndef())
......@@ -852,7 +859,7 @@ pub const LLVMIRModule = struct {
852859 }
853860 }
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 {
856863 switch (t.zigTypeTag()) {
857864 .Void => return self.context.voidType(),
858865 .NoReturn => return self.context.voidType(),
......@@ -891,7 +898,7 @@ pub const LLVMIRModule = struct {
891898 }
892899 }
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 {
895902 // TODO: do we want to store this in our own datastructure?
896903 if (self.llvm_module.getNamedGlobal(decl.name)) |val| return val;
897904
......@@ -910,7 +917,7 @@ pub const LLVMIRModule = struct {
910917 }
911918
912919 /// 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 {
914921 // TODO: do we want to store this in our own datastructure?
915922 if (self.llvm_module.getNamedFunction(func.name)) |llvm_fn| return llvm_fn;
916923
......@@ -958,13 +965,11 @@ pub const LLVMIRModule = struct {
958965 self.addAttr(val, std.math.maxInt(llvm.AttributeIndex), attr_name);
959966 }
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 } {
962969 @setCold(true);
963970 assert(self.err_msg == null);
964 self.err_msg = try Module.ErrorMsg.create(self.gpa, .{
965 .file_scope = self.src_loc.file_scope,
966 .byte_offset = src,
967 }, format, args);
971 const src_loc = src.toSrcLocWithDecl(self.decl);
972 self.err_msg = try Module.ErrorMsg.create(self.gpa, src_loc, format, args);
968973 return error.CodegenFail;
969974 }
970975};