| ... | @@ -49,7 +49,7 @@ pub const DeclGen = struct { | ... | @@ -49,7 +49,7 @@ pub const DeclGen = struct { |
| 49 | spv: *SpvModule, | 49 | spv: *SpvModule, |
| 50 | | 50 | |
| 51 | /// The decl we are currently generating code for. | 51 | /// The decl we are currently generating code for. |
| 52 | decl: *Decl, | 52 | decl_index: Decl.Index, |
| 53 | | 53 | |
| 54 | /// The intermediate code of the declaration we are currently generating. Note: If | 54 | /// The intermediate code of the declaration we are currently generating. Note: If |
| 55 | /// the declaration is not a function, this value will be undefined! | 55 | /// the declaration is not a function, this value will be undefined! |
| ... | @@ -59,6 +59,8 @@ pub const DeclGen = struct { | ... | @@ -59,6 +59,8 @@ pub const DeclGen = struct { |
| 59 | /// Note: If the declaration is not a function, this value will be undefined! | 59 | /// Note: If the declaration is not a function, this value will be undefined! |
| 60 | liveness: Liveness, | 60 | liveness: Liveness, |
| 61 | | 61 | |
| | 62 | ids: *const std.AutoHashMap(Decl.Index, IdResult), |
| | 63 | |
| 62 | /// An array of function argument result-ids. Each index corresponds with the | 64 | /// An array of function argument result-ids. Each index corresponds with the |
| 63 | /// function argument of the same index. | 65 | /// function argument of the same index. |
| 64 | args: std.ArrayListUnmanaged(IdRef) = .{}, | 66 | args: std.ArrayListUnmanaged(IdRef) = .{}, |
| ... | @@ -133,14 +135,20 @@ pub const DeclGen = struct { | ... | @@ -133,14 +135,20 @@ pub const DeclGen = struct { |
| 133 | | 135 | |
| 134 | /// Initialize the common resources of a DeclGen. Some fields are left uninitialized, | 136 | /// Initialize the common resources of a DeclGen. Some fields are left uninitialized, |
| 135 | /// only set when `gen` is called. | 137 | /// only set when `gen` is called. |
| 136 | pub fn init(allocator: Allocator, module: *Module, spv: *SpvModule) DeclGen { | 138 | pub fn init( |
| | 139 | allocator: Allocator, |
| | 140 | module: *Module, |
| | 141 | spv: *SpvModule, |
| | 142 | ids: *const std.AutoHashMap(Decl.Index, IdResult), |
| | 143 | ) DeclGen { |
| 137 | return .{ | 144 | return .{ |
| 138 | .gpa = allocator, | 145 | .gpa = allocator, |
| 139 | .module = module, | 146 | .module = module, |
| 140 | .spv = spv, | 147 | .spv = spv, |
| 141 | .decl = undefined, | 148 | .decl_index = undefined, |
| 142 | .air = undefined, | 149 | .air = undefined, |
| 143 | .liveness = undefined, | 150 | .liveness = undefined, |
| | 151 | .ids = ids, |
| 144 | .next_arg_index = undefined, | 152 | .next_arg_index = undefined, |
| 145 | .current_block_label_id = undefined, | 153 | .current_block_label_id = undefined, |
| 146 | .error_msg = undefined, | 154 | .error_msg = undefined, |
| ... | @@ -150,9 +158,9 @@ pub const DeclGen = struct { | ... | @@ -150,9 +158,9 @@ pub const DeclGen = struct { |
| 150 | /// Generate the code for `decl`. If a reportable error occurred during code generation, | 158 | /// Generate the code for `decl`. If a reportable error occurred during code generation, |
| 151 | /// a message is returned by this function. Callee owns the memory. If this function | 159 | /// a message is returned by this function. Callee owns the memory. If this function |
| 152 | /// returns such a reportable error, it is valid to be called again for a different decl. | 160 | /// returns such a reportable error, it is valid to be called again for a different decl. |
| 153 | pub fn gen(self: *DeclGen, decl: *Decl, air: Air, liveness: Liveness) !?*Module.ErrorMsg { | 161 | pub fn gen(self: *DeclGen, decl_index: Decl.Index, air: Air, liveness: Liveness) !?*Module.ErrorMsg { |
| 154 | // Reset internal resources, we don't want to re-allocate these. | 162 | // Reset internal resources, we don't want to re-allocate these. |
| 155 | self.decl = decl; | 163 | self.decl_index = decl_index; |
| 156 | self.air = air; | 164 | self.air = air; |
| 157 | self.liveness = liveness; | 165 | self.liveness = liveness; |
| 158 | self.args.items.len = 0; | 166 | self.args.items.len = 0; |
| ... | @@ -194,7 +202,7 @@ pub const DeclGen = struct { | ... | @@ -194,7 +202,7 @@ pub const DeclGen = struct { |
| 194 | pub fn fail(self: *DeclGen, comptime format: []const u8, args: anytype) Error { | 202 | pub fn fail(self: *DeclGen, comptime format: []const u8, args: anytype) Error { |
| 195 | @setCold(true); | 203 | @setCold(true); |
| 196 | const src = LazySrcLoc.nodeOffset(0); | 204 | const src = LazySrcLoc.nodeOffset(0); |
| 197 | const src_loc = src.toSrcLoc(self.decl); | 205 | const src_loc = src.toSrcLoc(self.module.declPtr(self.decl_index)); |
| 198 | assert(self.error_msg == null); | 206 | assert(self.error_msg == null); |
| 199 | self.error_msg = try Module.ErrorMsg.create(self.module.gpa, src_loc, format, args); | 207 | self.error_msg = try Module.ErrorMsg.create(self.module.gpa, src_loc, format, args); |
| 200 | return error.CodegenFail; | 208 | return error.CodegenFail; |
| ... | @@ -332,7 +340,7 @@ pub const DeclGen = struct { | ... | @@ -332,7 +340,7 @@ pub const DeclGen = struct { |
| 332 | }; | 340 | }; |
| 333 | const decl = self.module.declPtr(fn_decl_index); | 341 | const decl = self.module.declPtr(fn_decl_index); |
| 334 | self.module.markDeclAlive(decl); | 342 | self.module.markDeclAlive(decl); |
| 335 | return decl.fn_link.spirv.id.toRef(); | 343 | return self.ids.get(fn_decl_index).?.toRef(); |
| 336 | } | 344 | } |
| 337 | | 345 | |
| 338 | const target = self.getTarget(); | 346 | const target = self.getTarget(); |
| ... | @@ -553,8 +561,8 @@ pub const DeclGen = struct { | ... | @@ -553,8 +561,8 @@ pub const DeclGen = struct { |
| 553 | } | 561 | } |
| 554 | | 562 | |
| 555 | fn genDecl(self: *DeclGen) !void { | 563 | fn genDecl(self: *DeclGen) !void { |
| 556 | const decl = self.decl; | 564 | const result_id = self.ids.get(self.decl_index).?; |
| 557 | const result_id = decl.fn_link.spirv.id; | 565 | const decl = self.module.declPtr(self.decl_index); |
| 558 | | 566 | |
| 559 | if (decl.val.castTag(.function)) |_| { | 567 | if (decl.val.castTag(.function)) |_| { |
| 560 | assert(decl.ty.zigTypeTag() == .Fn); | 568 | assert(decl.ty.zigTypeTag() == .Fn); |
| ... | @@ -945,7 +953,7 @@ pub const DeclGen = struct { | ... | @@ -945,7 +953,7 @@ pub const DeclGen = struct { |
| 945 | | 953 | |
| 946 | fn airDbgStmt(self: *DeclGen, inst: Air.Inst.Index) !void { | 954 | fn airDbgStmt(self: *DeclGen, inst: Air.Inst.Index) !void { |
| 947 | const dbg_stmt = self.air.instructions.items(.data)[inst].dbg_stmt; | 955 | const dbg_stmt = self.air.instructions.items(.data)[inst].dbg_stmt; |
| 948 | const src_fname_id = try self.spv.resolveSourceFileName(self.decl); | 956 | const src_fname_id = try self.spv.resolveSourceFileName(self.module.declPtr(self.decl_index)); |
| 949 | try self.func.body.emit(self.spv.gpa, .OpLine, .{ | 957 | try self.func.body.emit(self.spv.gpa, .OpLine, .{ |
| 950 | .file = src_fname_id, | 958 | .file = src_fname_id, |
| 951 | .line = dbg_stmt.line, | 959 | .line = dbg_stmt.line, |
| ... | @@ -1106,7 +1114,7 @@ pub const DeclGen = struct { | ... | @@ -1106,7 +1114,7 @@ pub const DeclGen = struct { |
| 1106 | assert(as.errors.items.len != 0); | 1114 | assert(as.errors.items.len != 0); |
| 1107 | assert(self.error_msg == null); | 1115 | assert(self.error_msg == null); |
| 1108 | const loc = LazySrcLoc.nodeOffset(0); | 1116 | const loc = LazySrcLoc.nodeOffset(0); |
| 1109 | const src_loc = loc.toSrcLoc(self.decl); | 1117 | const src_loc = loc.toSrcLoc(self.module.declPtr(self.decl_index)); |
| 1110 | self.error_msg = try Module.ErrorMsg.create(self.module.gpa, src_loc, "failed to assemble SPIR-V inline assembly", .{}); | 1118 | self.error_msg = try Module.ErrorMsg.create(self.module.gpa, src_loc, "failed to assemble SPIR-V inline assembly", .{}); |
| 1111 | const notes = try self.module.gpa.alloc(Module.ErrorMsg, as.errors.items.len); | 1119 | const notes = try self.module.gpa.alloc(Module.ErrorMsg, as.errors.items.len); |
| 1112 | | 1120 | |