| ... | @@ -15,6 +15,9 @@ const Inst = ir.Inst; | ... | @@ -15,6 +15,9 @@ const Inst = ir.Inst; |
| 15 | const Value = @import("../value.zig").Value; | 15 | const Value = @import("../value.zig").Value; |
| 16 | const Type = @import("../type.zig").Type; | 16 | const Type = @import("../type.zig").Type; |
| 17 | | 17 | |
| | 18 | const LazySrcLoc = Module.LazySrcLoc; |
| | 19 | const SrcLoc = Module.SrcLoc; |
| | 20 | |
| 18 | pub fn targetTriple(allocator: *Allocator, target: std.Target) ![:0]u8 { | 21 | pub 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 function | 162 | // because they are only valid when generating a function |
| 160 | | 163 | |
| | 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 be | 168 | /// 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 blocks | 170 | /// 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 { |
| 342 | | 349 | |
| 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; |
| 348 | | 355 | |
| 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 }); |
| 350 | | 357 | |
| ... | @@ -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 | } |
| 767 | | 774 | |
| 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); |
| 770 | | 777 | |
| 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 | } |
| 854 | | 861 | |
| 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 | } |
| 893 | | 900 | |
| 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; |
| 897 | | 904 | |
| ... | @@ -910,7 +917,7 @@ pub const LLVMIRModule = struct { | ... | @@ -910,7 +917,7 @@ pub const LLVMIRModule = struct { |
| 910 | } | 917 | } |
| 911 | | 918 | |
| 912 | /// If the llvm function does not exist, create it | 919 | /// 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; |
| 916 | | 923 | |
| ... | @@ -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 | } |
| 960 | | 967 | |
| 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 | }; |