| ... | ... | @@ -15,6 +15,9 @@ const Inst = ir.Inst; |
| 15 | 15 | const Value = @import("../value.zig").Value; |
| 16 | 16 | const Type = @import("../type.zig").Type; |
| 17 | 17 | |
| 18 | const LazySrcLoc = Module.LazySrcLoc; |
| 19 | const SrcLoc = Module.SrcLoc; |
| 20 | |
| 18 | 21 | pub fn targetTriple(allocator: *Allocator, target: std.Target) ![:0]u8 { |
| 19 | 22 | const llvm_arch = switch (target.cpu.arch) { |
| 20 | 23 | .arm => "arm", |
| ... | ... | @@ -158,6 +161,10 @@ pub const LLVMIRModule = struct { |
| 158 | 161 | // TODO: The fields below should really move into a different struct, |
| 159 | 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 | 168 | /// This stores the LLVM values used in a function, such that they can be |
| 162 | 169 | /// referred to in other instructions. This table is cleared before every function is generated. |
| 163 | 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 | 349 | |
| 343 | 350 | fn gen(self: *LLVMIRModule, module: *Module, decl: *Module.Decl) !void { |
| 344 | 351 | const typed_value = decl.typed_value.most_recent.typed_value; |
| 345 | | const src = decl.src(); |
| 346 | | |
| 347 | 352 | self.src_loc = decl.srcLoc(); |
| 353 | self.decl = decl; |
| 354 | const src = self.src_loc.lazy; |
| 348 | 355 | |
| 349 | 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 | 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 | 776 | const llvm_type = try self.getLLVMType(tv.ty, src); |
| 770 | 777 | |
| 771 | 778 | if (tv.val.isUndef()) |
| ... | ... | @@ -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 | 863 | switch (t.zigTypeTag()) { |
| 857 | 864 | .Void => return self.context.voidType(), |
| 858 | 865 | .NoReturn => return self.context.voidType(), |
| ... | ... | @@ -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 | 902 | // TODO: do we want to store this in our own datastructure? |
| 896 | 903 | if (self.llvm_module.getNamedGlobal(decl.name)) |val| return val; |
| 897 | 904 | |
| ... | ... | @@ -910,7 +917,7 @@ pub const LLVMIRModule = struct { |
| 910 | 917 | } |
| 911 | 918 | |
| 912 | 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 | 921 | // TODO: do we want to store this in our own datastructure? |
| 915 | 922 | if (self.llvm_module.getNamedFunction(func.name)) |llvm_fn| return llvm_fn; |
| 916 | 923 | |
| ... | ... | @@ -958,13 +965,11 @@ pub const LLVMIRModule = struct { |
| 958 | 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 | 969 | @setCold(true); |
| 963 | 970 | 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); |
| 968 | 973 | return error.CodegenFail; |
| 969 | 974 | } |
| 970 | 975 | }; |