| author | |
| committer | |
| log | 9bf715de74a7d5badeae932afb594b7c6b33afa3 |
| tree | 302e2c5a42d30e379c5bb2e86c24360ea49ab3fb |
| parent | 77accf597d845245847b143e42ec4109c9468480 |
15 files changed, 323 insertions(+), 512 deletions(-)
src/Zcu.zig+39-1| ... | ... | @@ -4072,15 +4072,53 @@ pub fn navValIsConst(zcu: *const Zcu, val: InternPool.Index) bool { |
| 4072 | 4072 | }; |
| 4073 | 4073 | } |
| 4074 | 4074 | |
| 4075 | pub const CodegenFailError = error{ | |
| 4076 | /// Indicates the error message has been already stored at `Zcu.failed_codegen`. | |
| 4077 | CodegenFail, | |
| 4078 | OutOfMemory, | |
| 4079 | }; | |
| 4080 | ||
| 4075 | 4081 | pub fn codegenFail( |
| 4076 | 4082 | zcu: *Zcu, |
| 4077 | 4083 | nav_index: InternPool.Nav.Index, |
| 4078 | 4084 | comptime format: []const u8, |
| 4079 | 4085 | args: anytype, |
| 4080 | ) error{ CodegenFail, OutOfMemory } { | |
| 4086 | ) CodegenFailError { | |
| 4081 | 4087 | const gpa = zcu.gpa; |
| 4082 | 4088 | try zcu.failed_codegen.ensureUnusedCapacity(gpa, 1); |
| 4083 | 4089 | const msg = try Zcu.ErrorMsg.create(gpa, zcu.navSrcLoc(nav_index), format, args); |
| 4084 | 4090 | zcu.failed_codegen.putAssumeCapacityNoClobber(nav_index, msg); |
| 4085 | 4091 | return error.CodegenFail; |
| 4086 | 4092 | } |
| 4093 | ||
| 4094 | pub fn codegenFailMsg(zcu: *Zcu, nav_index: InternPool.Nav.Index, msg: *ErrorMsg) CodegenFailError { | |
| 4095 | const gpa = zcu.gpa; | |
| 4096 | { | |
| 4097 | errdefer msg.deinit(gpa); | |
| 4098 | try zcu.failed_codegen.putNoClobber(gpa, nav_index, msg); | |
| 4099 | } | |
| 4100 | return error.CodegenFail; | |
| 4101 | } | |
| 4102 | ||
| 4103 | pub fn codegenFailType( | |
| 4104 | zcu: *Zcu, | |
| 4105 | ty_index: InternPool.Index, | |
| 4106 | comptime format: []const u8, | |
| 4107 | args: anytype, | |
| 4108 | ) CodegenFailError { | |
| 4109 | const gpa = zcu.gpa; | |
| 4110 | try zcu.failed_types.ensureUnusedCapacity(gpa, 1); | |
| 4111 | const msg = try Zcu.ErrorMsg.create(gpa, zcu.typeSrcLoc(ty_index), format, args); | |
| 4112 | zcu.failed_types.putAssumeCapacityNoClobber(ty_index, msg); | |
| 4113 | return error.CodegenFail; | |
| 4114 | } | |
| 4115 | ||
| 4116 | pub fn codegenFailTypeMsg(zcu: *Zcu, ty_index: InternPool.Index, msg: *ErrorMsg) CodegenFailError { | |
| 4117 | const gpa = zcu.gpa; | |
| 4118 | { | |
| 4119 | errdefer msg.deinit(gpa); | |
| 4120 | try zcu.failed_types.ensureUnusedCapacity(gpa, 1); | |
| 4121 | } | |
| 4122 | zcu.failed_types.putAssumeCapacityNoClobber(ty_index, msg); | |
| 4123 | return error.CodegenFail; | |
| 4124 | } |
src/Zcu/PerThread.zig+2-9| ... | ... | @@ -1726,7 +1726,6 @@ pub fn linkerUpdateFunc(pt: Zcu.PerThread, func_index: InternPool.Index, air: Ai |
| 1726 | 1726 | lf.updateFunc(pt, func_index, air, liveness) catch |err| switch (err) { |
| 1727 | 1727 | error.OutOfMemory => return error.OutOfMemory, |
| 1728 | 1728 | error.CodegenFail => assert(zcu.failed_codegen.contains(nav_index)), |
| 1729 | error.LinkFailure => assert(comp.link_diags.hasErrors()), | |
| 1730 | 1729 | error.Overflow => { |
| 1731 | 1730 | try zcu.failed_codegen.putNoClobber(gpa, nav_index, try Zcu.ErrorMsg.create( |
| 1732 | 1731 | gpa, |
| ... | ... | @@ -3112,7 +3111,6 @@ pub fn linkerUpdateNav(pt: Zcu.PerThread, nav_index: InternPool.Nav.Index) error |
| 3112 | 3111 | lf.updateNav(pt, nav_index) catch |err| switch (err) { |
| 3113 | 3112 | error.OutOfMemory => return error.OutOfMemory, |
| 3114 | 3113 | error.CodegenFail => assert(zcu.failed_codegen.contains(nav_index)), |
| 3115 | error.LinkFailure => assert(comp.link_diags.hasErrors()), | |
| 3116 | 3114 | error.Overflow => { |
| 3117 | 3115 | try zcu.failed_codegen.putNoClobber(gpa, nav_index, try Zcu.ErrorMsg.create( |
| 3118 | 3116 | gpa, |
| ... | ... | @@ -3139,7 +3137,7 @@ pub fn linkerUpdateContainerType(pt: Zcu.PerThread, ty: InternPool.Index) error{ |
| 3139 | 3137 | const codegen_prog_node = zcu.codegen_prog_node.start(Type.fromInterned(ty).containerTypeName(ip).toSlice(ip), 0); |
| 3140 | 3138 | defer codegen_prog_node.end(); |
| 3141 | 3139 | |
| 3142 | if (zcu.failed_types.fetchSwapRemove(ty)) |entry| entry.deinit(); | |
| 3140 | if (zcu.failed_types.fetchSwapRemove(ty)) |*entry| entry.value.deinit(gpa); | |
| 3143 | 3141 | |
| 3144 | 3142 | if (!Air.typeFullyResolved(Type.fromInterned(ty), zcu)) { |
| 3145 | 3143 | // This type failed to resolve. This is a transitive failure. |
| ... | ... | @@ -3148,12 +3146,7 @@ pub fn linkerUpdateContainerType(pt: Zcu.PerThread, ty: InternPool.Index) error{ |
| 3148 | 3146 | |
| 3149 | 3147 | if (comp.bin_file) |lf| lf.updateContainerType(pt, ty) catch |err| switch (err) { |
| 3150 | 3148 | error.OutOfMemory => return error.OutOfMemory, |
| 3151 | else => |e| try zcu.failed_types.putNoClobber(gpa, ty, try Zcu.ErrorMsg.create( | |
| 3152 | gpa, | |
| 3153 | zcu.typeSrcLoc(ty), | |
| 3154 | "failed to update container type: {s}", | |
| 3155 | .{@errorName(e)}, | |
| 3156 | )), | |
| 3149 | error.TypeFailureReported => assert(zcu.failed_types.contains(ty)), | |
| 3157 | 3150 | }; |
| 3158 | 3151 | } |
| 3159 | 3152 |
src/arch/aarch64/CodeGen.zig+16-36| ... | ... | @@ -24,7 +24,6 @@ const build_options = @import("build_options"); |
| 24 | 24 | const Alignment = InternPool.Alignment; |
| 25 | 25 | |
| 26 | 26 | const CodeGenError = codegen.CodeGenError; |
| 27 | const Result = codegen.Result; | |
| 28 | 27 | |
| 29 | 28 | const bits = @import("bits.zig"); |
| 30 | 29 | const abi = @import("abi.zig"); |
| ... | ... | @@ -51,7 +50,6 @@ debug_output: link.File.DebugInfoOutput, |
| 51 | 50 | target: *const std.Target, |
| 52 | 51 | func_index: InternPool.Index, |
| 53 | 52 | owner_nav: InternPool.Nav.Index, |
| 54 | err_msg: ?*ErrorMsg, | |
| 55 | 53 | args: []MCValue, |
| 56 | 54 | ret_mcv: MCValue, |
| 57 | 55 | fn_type: Type, |
| ... | ... | @@ -167,7 +165,7 @@ const DbgInfoReloc = struct { |
| 167 | 165 | name: [:0]const u8, |
| 168 | 166 | mcv: MCValue, |
| 169 | 167 | |
| 170 | fn genDbgInfo(reloc: DbgInfoReloc, function: Self) CodeGenError!void { | |
| 168 | fn genDbgInfo(reloc: DbgInfoReloc, function: Self) !void { | |
| 171 | 169 | switch (reloc.tag) { |
| 172 | 170 | .arg, |
| 173 | 171 | .dbg_arg_inline, |
| ... | ... | @@ -181,7 +179,7 @@ const DbgInfoReloc = struct { |
| 181 | 179 | } |
| 182 | 180 | } |
| 183 | 181 | |
| 184 | fn genArgDbgInfo(reloc: DbgInfoReloc, function: Self) CodeGenError!void { | |
| 182 | fn genArgDbgInfo(reloc: DbgInfoReloc, function: Self) !void { | |
| 185 | 183 | switch (function.debug_output) { |
| 186 | 184 | .dwarf => |dw| { |
| 187 | 185 | const loc: link.File.Dwarf.Loc = switch (reloc.mcv) { |
| ... | ... | @@ -209,7 +207,7 @@ const DbgInfoReloc = struct { |
| 209 | 207 | } |
| 210 | 208 | } |
| 211 | 209 | |
| 212 | fn genVarDbgInfo(reloc: DbgInfoReloc, function: Self) CodeGenError!void { | |
| 210 | fn genVarDbgInfo(reloc: DbgInfoReloc, function: Self) !void { | |
| 213 | 211 | switch (function.debug_output) { |
| 214 | 212 | .dwarf => |dwarf| { |
| 215 | 213 | const loc: link.File.Dwarf.Loc = switch (reloc.mcv) { |
| ... | ... | @@ -327,7 +325,7 @@ pub fn generate( |
| 327 | 325 | liveness: Liveness, |
| 328 | 326 | code: *std.ArrayList(u8), |
| 329 | 327 | debug_output: link.File.DebugInfoOutput, |
| 330 | ) CodeGenError!Result { | |
| 328 | ) CodeGenError!void { | |
| 331 | 329 | const zcu = pt.zcu; |
| 332 | 330 | const gpa = zcu.gpa; |
| 333 | 331 | const func = zcu.funcInfo(func_index); |
| ... | ... | @@ -353,7 +351,6 @@ pub fn generate( |
| 353 | 351 | .bin_file = lf, |
| 354 | 352 | .func_index = func_index, |
| 355 | 353 | .owner_nav = func.owner_nav, |
| 356 | .err_msg = null, | |
| 357 | 354 | .args = undefined, // populated after `resolveCallingConventionValues` |
| 358 | 355 | .ret_mcv = undefined, // populated after `resolveCallingConventionValues` |
| 359 | 356 | .fn_type = fn_type, |
| ... | ... | @@ -370,10 +367,7 @@ pub fn generate( |
| 370 | 367 | defer function.dbg_info_relocs.deinit(gpa); |
| 371 | 368 | |
| 372 | 369 | var call_info = function.resolveCallingConventionValues(fn_type) catch |err| switch (err) { |
| 373 | error.CodegenFail => return Result{ .fail = function.err_msg.? }, | |
| 374 | error.OutOfRegisters => return Result{ | |
| 375 | .fail = try ErrorMsg.create(gpa, src_loc, "CodeGen ran out of registers. This is a bug in the Zig compiler.", .{}), | |
| 376 | }, | |
| 370 | error.CodegenFail => return error.CodegenFail, | |
| 377 | 371 | else => |e| return e, |
| 378 | 372 | }; |
| 379 | 373 | defer call_info.deinit(&function); |
| ... | ... | @@ -384,15 +378,14 @@ pub fn generate( |
| 384 | 378 | function.max_end_stack = call_info.stack_byte_count; |
| 385 | 379 | |
| 386 | 380 | function.gen() catch |err| switch (err) { |
| 387 | error.CodegenFail => return Result{ .fail = function.err_msg.? }, | |
| 388 | error.OutOfRegisters => return Result{ | |
| 389 | .fail = try ErrorMsg.create(gpa, src_loc, "CodeGen ran out of registers. This is a bug in the Zig compiler.", .{}), | |
| 390 | }, | |
| 381 | error.CodegenFail => return error.CodegenFail, | |
| 382 | error.OutOfRegisters => return function.fail("ran out of registers (Zig compiler bug)", .{}), | |
| 391 | 383 | else => |e| return e, |
| 392 | 384 | }; |
| 393 | 385 | |
| 394 | 386 | for (function.dbg_info_relocs.items) |reloc| { |
| 395 | try reloc.genDbgInfo(function); | |
| 387 | reloc.genDbgInfo(function) catch |err| | |
| 388 | return function.fail("failed to generate debug info: {s}", .{@errorName(err)}); | |
| 396 | 389 | } |
| 397 | 390 | |
| 398 | 391 | var mir: Mir = .{ |
| ... | ... | @@ -417,15 +410,9 @@ pub fn generate( |
| 417 | 410 | defer emit.deinit(); |
| 418 | 411 | |
| 419 | 412 | emit.emitMir() catch |err| switch (err) { |
| 420 | error.EmitFail => return Result{ .fail = emit.err_msg.? }, | |
| 413 | error.EmitFail => return function.failMsg(emit.err_msg.?), | |
| 421 | 414 | else => |e| return e, |
| 422 | 415 | }; |
| 423 | ||
| 424 | if (function.err_msg) |em| { | |
| 425 | return Result{ .fail = em }; | |
| 426 | } else { | |
| 427 | return Result.ok; | |
| 428 | } | |
| 429 | 416 | } |
| 430 | 417 | |
| 431 | 418 | fn addInst(self: *Self, inst: Mir.Inst) error{OutOfMemory}!Mir.Inst.Index { |
| ... | ... | @@ -567,7 +554,7 @@ fn gen(self: *Self) !void { |
| 567 | 554 | .data = .{ .rr_imm12_sh = .{ .rd = .sp, .rn = .sp, .imm12 = size } }, |
| 568 | 555 | }); |
| 569 | 556 | } else { |
| 570 | return self.failSymbol("TODO AArch64: allow larger stacks", .{}); | |
| 557 | @panic("TODO AArch64: allow larger stacks"); | |
| 571 | 558 | } |
| 572 | 559 | |
| 573 | 560 | _ = try self.addInst(.{ |
| ... | ... | @@ -6191,10 +6178,7 @@ fn genTypedValue(self: *Self, val: Value) InnerError!MCValue { |
| 6191 | 6178 | .load_direct => |sym_index| .{ .linker_load = .{ .type = .direct, .sym_index = sym_index } }, |
| 6192 | 6179 | .load_symbol, .load_tlv, .lea_symbol, .lea_direct => unreachable, // TODO |
| 6193 | 6180 | }, |
| 6194 | .fail => |msg| { | |
| 6195 | self.err_msg = msg; | |
| 6196 | return error.CodegenFail; | |
| 6197 | }, | |
| 6181 | .fail => |msg| return self.failMsg(msg), | |
| 6198 | 6182 | }; |
| 6199 | 6183 | return mcv; |
| 6200 | 6184 | } |
| ... | ... | @@ -6355,18 +6339,14 @@ fn wantSafety(self: *Self) bool { |
| 6355 | 6339 | }; |
| 6356 | 6340 | } |
| 6357 | 6341 | |
| 6358 | fn fail(self: *Self, comptime format: []const u8, args: anytype) InnerError { | |
| 6342 | fn fail(self: *Self, comptime format: []const u8, args: anytype) error{ OutOfMemory, CodegenFail } { | |
| 6359 | 6343 | @branchHint(.cold); |
| 6360 | assert(self.err_msg == null); | |
| 6361 | self.err_msg = try ErrorMsg.create(self.gpa, self.src_loc, format, args); | |
| 6362 | return error.CodegenFail; | |
| 6344 | return self.pt.zcu.codegenFail(self.owner_nav, format, args); | |
| 6363 | 6345 | } |
| 6364 | 6346 | |
| 6365 | fn failSymbol(self: *Self, comptime format: []const u8, args: anytype) InnerError { | |
| 6347 | fn failMsg(self: *Self, msg: *ErrorMsg) error{ OutOfMemory, CodegenFail } { | |
| 6366 | 6348 | @branchHint(.cold); |
| 6367 | assert(self.err_msg == null); | |
| 6368 | self.err_msg = try ErrorMsg.create(self.gpa, self.src_loc, format, args); | |
| 6369 | return error.CodegenFail; | |
| 6349 | return self.pt.zcu.codegenFailMsg(self.owner_nav, msg); | |
| 6370 | 6350 | } |
| 6371 | 6351 | |
| 6372 | 6352 | fn parseRegName(name: []const u8) ?Register { |
src/arch/arm/CodeGen.zig+19-31| ... | ... | @@ -23,7 +23,6 @@ const log = std.log.scoped(.codegen); |
| 23 | 23 | const build_options = @import("build_options"); |
| 24 | 24 | const Alignment = InternPool.Alignment; |
| 25 | 25 | |
| 26 | const Result = codegen.Result; | |
| 27 | 26 | const CodeGenError = codegen.CodeGenError; |
| 28 | 27 | |
| 29 | 28 | const bits = @import("bits.zig"); |
| ... | ... | @@ -245,7 +244,7 @@ const DbgInfoReloc = struct { |
| 245 | 244 | name: [:0]const u8, |
| 246 | 245 | mcv: MCValue, |
| 247 | 246 | |
| 248 | fn genDbgInfo(reloc: DbgInfoReloc, function: Self) CodeGenError!void { | |
| 247 | fn genDbgInfo(reloc: DbgInfoReloc, function: Self) !void { | |
| 249 | 248 | switch (reloc.tag) { |
| 250 | 249 | .arg, |
| 251 | 250 | .dbg_arg_inline, |
| ... | ... | @@ -259,7 +258,7 @@ const DbgInfoReloc = struct { |
| 259 | 258 | } |
| 260 | 259 | } |
| 261 | 260 | |
| 262 | fn genArgDbgInfo(reloc: DbgInfoReloc, function: Self) CodeGenError!void { | |
| 261 | fn genArgDbgInfo(reloc: DbgInfoReloc, function: Self) !void { | |
| 263 | 262 | switch (function.debug_output) { |
| 264 | 263 | .dwarf => |dw| { |
| 265 | 264 | const loc: link.File.Dwarf.Loc = switch (reloc.mcv) { |
| ... | ... | @@ -287,7 +286,7 @@ const DbgInfoReloc = struct { |
| 287 | 286 | } |
| 288 | 287 | } |
| 289 | 288 | |
| 290 | fn genVarDbgInfo(reloc: DbgInfoReloc, function: Self) CodeGenError!void { | |
| 289 | fn genVarDbgInfo(reloc: DbgInfoReloc, function: Self) !void { | |
| 291 | 290 | switch (function.debug_output) { |
| 292 | 291 | .dwarf => |dw| { |
| 293 | 292 | const loc: link.File.Dwarf.Loc = switch (reloc.mcv) { |
| ... | ... | @@ -335,7 +334,7 @@ pub fn generate( |
| 335 | 334 | liveness: Liveness, |
| 336 | 335 | code: *std.ArrayList(u8), |
| 337 | 336 | debug_output: link.File.DebugInfoOutput, |
| 338 | ) CodeGenError!Result { | |
| 337 | ) CodeGenError!void { | |
| 339 | 338 | const zcu = pt.zcu; |
| 340 | 339 | const gpa = zcu.gpa; |
| 341 | 340 | const func = zcu.funcInfo(func_index); |
| ... | ... | @@ -377,10 +376,7 @@ pub fn generate( |
| 377 | 376 | defer function.dbg_info_relocs.deinit(gpa); |
| 378 | 377 | |
| 379 | 378 | var call_info = function.resolveCallingConventionValues(func_ty) catch |err| switch (err) { |
| 380 | error.CodegenFail => return Result{ .fail = function.err_msg.? }, | |
| 381 | error.OutOfRegisters => return Result{ | |
| 382 | .fail = try ErrorMsg.create(gpa, src_loc, "CodeGen ran out of registers. This is a bug in the Zig compiler.", .{}), | |
| 383 | }, | |
| 379 | error.CodegenFail => return error.CodegenFail, | |
| 384 | 380 | else => |e| return e, |
| 385 | 381 | }; |
| 386 | 382 | defer call_info.deinit(&function); |
| ... | ... | @@ -391,15 +387,14 @@ pub fn generate( |
| 391 | 387 | function.max_end_stack = call_info.stack_byte_count; |
| 392 | 388 | |
| 393 | 389 | function.gen() catch |err| switch (err) { |
| 394 | error.CodegenFail => return Result{ .fail = function.err_msg.? }, | |
| 395 | error.OutOfRegisters => return Result{ | |
| 396 | .fail = try ErrorMsg.create(gpa, src_loc, "CodeGen ran out of registers. This is a bug in the Zig compiler.", .{}), | |
| 397 | }, | |
| 390 | error.CodegenFail => return error.CodegenFail, | |
| 391 | error.OutOfRegisters => return function.fail("ran out of registers (Zig compiler bug)", .{}), | |
| 398 | 392 | else => |e| return e, |
| 399 | 393 | }; |
| 400 | 394 | |
| 401 | 395 | for (function.dbg_info_relocs.items) |reloc| { |
| 402 | try reloc.genDbgInfo(function); | |
| 396 | reloc.genDbgInfo(function) catch |err| | |
| 397 | return function.fail("failed to generate debug info: {s}", .{@errorName(err)}); | |
| 403 | 398 | } |
| 404 | 399 | |
| 405 | 400 | var mir = Mir{ |
| ... | ... | @@ -424,15 +419,9 @@ pub fn generate( |
| 424 | 419 | defer emit.deinit(); |
| 425 | 420 | |
| 426 | 421 | emit.emitMir() catch |err| switch (err) { |
| 427 | error.EmitFail => return Result{ .fail = emit.err_msg.? }, | |
| 422 | error.EmitFail => return function.failMsg(emit.err_msg.?), | |
| 428 | 423 | else => |e| return e, |
| 429 | 424 | }; |
| 430 | ||
| 431 | if (function.err_msg) |em| { | |
| 432 | return Result{ .fail = em }; | |
| 433 | } else { | |
| 434 | return Result.ok; | |
| 435 | } | |
| 436 | 425 | } |
| 437 | 426 | |
| 438 | 427 | fn addInst(self: *Self, inst: Mir.Inst) error{OutOfMemory}!Mir.Inst.Index { |
| ... | ... | @@ -6310,20 +6299,19 @@ fn wantSafety(self: *Self) bool { |
| 6310 | 6299 | }; |
| 6311 | 6300 | } |
| 6312 | 6301 | |
| 6313 | fn fail(self: *Self, comptime format: []const u8, args: anytype) InnerError { | |
| 6302 | fn fail(self: *Self, comptime format: []const u8, args: anytype) error{ OutOfMemory, CodegenFail } { | |
| 6314 | 6303 | @branchHint(.cold); |
| 6315 | assert(self.err_msg == null); | |
| 6316 | const gpa = self.gpa; | |
| 6317 | self.err_msg = try ErrorMsg.create(gpa, self.src_loc, format, args); | |
| 6318 | return error.CodegenFail; | |
| 6304 | const zcu = self.pt.zcu; | |
| 6305 | const func = zcu.funcInfo(self.func_index); | |
| 6306 | const msg = try ErrorMsg.create(zcu.gpa, self.src_loc, format, args); | |
| 6307 | return zcu.codegenFailMsg(func.owner_nav, msg); | |
| 6319 | 6308 | } |
| 6320 | 6309 | |
| 6321 | fn failSymbol(self: *Self, comptime format: []const u8, args: anytype) InnerError { | |
| 6310 | fn failMsg(self: *Self, msg: *ErrorMsg) error{ OutOfMemory, CodegenFail } { | |
| 6322 | 6311 | @branchHint(.cold); |
| 6323 | assert(self.err_msg == null); | |
| 6324 | const gpa = self.gpa; | |
| 6325 | self.err_msg = try ErrorMsg.create(gpa, self.src_loc, format, args); | |
| 6326 | return error.CodegenFail; | |
| 6312 | const zcu = self.pt.zcu; | |
| 6313 | const func = zcu.funcInfo(self.func_index); | |
| 6314 | return zcu.codegenFailMsg(func.owner_nav, msg); | |
| 6327 | 6315 | } |
| 6328 | 6316 | |
| 6329 | 6317 | fn parseRegName(name: []const u8) ?Register { |
src/arch/riscv64/CodeGen.zig+31-74| ... | ... | @@ -32,7 +32,6 @@ const wip_mir_log = std.log.scoped(.wip_mir); |
| 32 | 32 | const Alignment = InternPool.Alignment; |
| 33 | 33 | |
| 34 | 34 | const CodeGenError = codegen.CodeGenError; |
| 35 | const Result = codegen.Result; | |
| 36 | 35 | |
| 37 | 36 | const bits = @import("bits.zig"); |
| 38 | 37 | const abi = @import("abi.zig"); |
| ... | ... | @@ -62,7 +61,6 @@ gpa: Allocator, |
| 62 | 61 | mod: *Package.Module, |
| 63 | 62 | target: *const std.Target, |
| 64 | 63 | debug_output: link.File.DebugInfoOutput, |
| 65 | err_msg: ?*ErrorMsg, | |
| 66 | 64 | args: []MCValue, |
| 67 | 65 | ret_mcv: InstTracking, |
| 68 | 66 | fn_type: Type, |
| ... | ... | @@ -761,7 +759,7 @@ pub fn generate( |
| 761 | 759 | liveness: Liveness, |
| 762 | 760 | code: *std.ArrayList(u8), |
| 763 | 761 | debug_output: link.File.DebugInfoOutput, |
| 764 | ) CodeGenError!Result { | |
| 762 | ) CodeGenError!void { | |
| 765 | 763 | const zcu = pt.zcu; |
| 766 | 764 | const comp = zcu.comp; |
| 767 | 765 | const gpa = zcu.gpa; |
| ... | ... | @@ -788,7 +786,6 @@ pub fn generate( |
| 788 | 786 | .target = &mod.resolved_target.result, |
| 789 | 787 | .debug_output = debug_output, |
| 790 | 788 | .owner = .{ .nav_index = func.owner_nav }, |
| 791 | .err_msg = null, | |
| 792 | 789 | .args = undefined, // populated after `resolveCallingConventionValues` |
| 793 | 790 | .ret_mcv = undefined, // populated after `resolveCallingConventionValues` |
| 794 | 791 | .fn_type = fn_type, |
| ... | ... | @@ -829,10 +826,7 @@ pub fn generate( |
| 829 | 826 | |
| 830 | 827 | const fn_info = zcu.typeToFunc(fn_type).?; |
| 831 | 828 | var call_info = function.resolveCallingConventionValues(fn_info, &.{}) catch |err| switch (err) { |
| 832 | error.CodegenFail => return Result{ .fail = function.err_msg.? }, | |
| 833 | error.OutOfRegisters => return Result{ | |
| 834 | .fail = try ErrorMsg.create(gpa, src_loc, "CodeGen ran out of registers. This is a bug in the Zig compiler.", .{}), | |
| 835 | }, | |
| 829 | error.CodegenFail => return error.CodegenFail, | |
| 836 | 830 | else => |e| return e, |
| 837 | 831 | }; |
| 838 | 832 | |
| ... | ... | @@ -861,10 +855,8 @@ pub fn generate( |
| 861 | 855 | })); |
| 862 | 856 | |
| 863 | 857 | function.gen() catch |err| switch (err) { |
| 864 | error.CodegenFail => return Result{ .fail = function.err_msg.? }, | |
| 865 | error.OutOfRegisters => return Result{ | |
| 866 | .fail = try ErrorMsg.create(gpa, src_loc, "CodeGen ran out of registers. This is a bug in the Zig compiler.", .{}), | |
| 867 | }, | |
| 858 | error.CodegenFail => return error.CodegenFail, | |
| 859 | error.OutOfRegisters => return function.fail("ran out of registers (Zig compiler bug)", .{}), | |
| 868 | 860 | else => |e| return e, |
| 869 | 861 | }; |
| 870 | 862 | |
| ... | ... | @@ -895,28 +887,10 @@ pub fn generate( |
| 895 | 887 | defer emit.deinit(); |
| 896 | 888 | |
| 897 | 889 | emit.emitMir() catch |err| switch (err) { |
| 898 | error.LowerFail, error.EmitFail => return Result{ .fail = emit.lower.err_msg.? }, | |
| 899 | error.InvalidInstruction => |e| { | |
| 900 | const msg = switch (e) { | |
| 901 | error.InvalidInstruction => "CodeGen failed to find a viable instruction.", | |
| 902 | }; | |
| 903 | return Result{ | |
| 904 | .fail = try ErrorMsg.create( | |
| 905 | gpa, | |
| 906 | src_loc, | |
| 907 | "{s} This is a bug in the Zig compiler.", | |
| 908 | .{msg}, | |
| 909 | ), | |
| 910 | }; | |
| 911 | }, | |
| 890 | error.LowerFail, error.EmitFail => return function.failMsg(emit.lower.err_msg.?), | |
| 891 | error.InvalidInstruction => |e| return function.fail("emit MIR failed: {s} (Zig compiler bug)", .{@errorName(e)}), | |
| 912 | 892 | else => |e| return e, |
| 913 | 893 | }; |
| 914 | ||
| 915 | if (function.err_msg) |em| { | |
| 916 | return Result{ .fail = em }; | |
| 917 | } else { | |
| 918 | return Result.ok; | |
| 919 | } | |
| 920 | 894 | } |
| 921 | 895 | |
| 922 | 896 | pub fn generateLazy( |
| ... | ... | @@ -926,7 +900,7 @@ pub fn generateLazy( |
| 926 | 900 | lazy_sym: link.File.LazySymbol, |
| 927 | 901 | code: *std.ArrayList(u8), |
| 928 | 902 | debug_output: link.File.DebugInfoOutput, |
| 929 | ) CodeGenError!Result { | |
| 903 | ) CodeGenError!void { | |
| 930 | 904 | const comp = bin_file.comp; |
| 931 | 905 | const gpa = comp.gpa; |
| 932 | 906 | const mod = comp.root_mod; |
| ... | ... | @@ -941,7 +915,6 @@ pub fn generateLazy( |
| 941 | 915 | .target = &mod.resolved_target.result, |
| 942 | 916 | .debug_output = debug_output, |
| 943 | 917 | .owner = .{ .lazy_sym = lazy_sym }, |
| 944 | .err_msg = null, | |
| 945 | 918 | .args = undefined, // populated after `resolveCallingConventionValues` |
| 946 | 919 | .ret_mcv = undefined, // populated after `resolveCallingConventionValues` |
| 947 | 920 | .fn_type = undefined, |
| ... | ... | @@ -957,10 +930,8 @@ pub fn generateLazy( |
| 957 | 930 | defer function.mir_instructions.deinit(gpa); |
| 958 | 931 | |
| 959 | 932 | function.genLazy(lazy_sym) catch |err| switch (err) { |
| 960 | error.CodegenFail => return Result{ .fail = function.err_msg.? }, | |
| 961 | error.OutOfRegisters => return Result{ | |
| 962 | .fail = try ErrorMsg.create(gpa, src_loc, "CodeGen ran out of registers. This is a bug in the Zig compiler.", .{}), | |
| 963 | }, | |
| 933 | error.CodegenFail => return error.CodegenFail, | |
| 934 | error.OutOfRegisters => return function.fail("ran out of registers (Zig compiler bug)", .{}), | |
| 964 | 935 | else => |e| return e, |
| 965 | 936 | }; |
| 966 | 937 | |
| ... | ... | @@ -991,28 +962,10 @@ pub fn generateLazy( |
| 991 | 962 | defer emit.deinit(); |
| 992 | 963 | |
| 993 | 964 | emit.emitMir() catch |err| switch (err) { |
| 994 | error.LowerFail, error.EmitFail => return Result{ .fail = emit.lower.err_msg.? }, | |
| 995 | error.InvalidInstruction => |e| { | |
| 996 | const msg = switch (e) { | |
| 997 | error.InvalidInstruction => "CodeGen failed to find a viable instruction.", | |
| 998 | }; | |
| 999 | return Result{ | |
| 1000 | .fail = try ErrorMsg.create( | |
| 1001 | gpa, | |
| 1002 | src_loc, | |
| 1003 | "{s} This is a bug in the Zig compiler.", | |
| 1004 | .{msg}, | |
| 1005 | ), | |
| 1006 | }; | |
| 1007 | }, | |
| 965 | error.LowerFail, error.EmitFail => return function.failMsg(emit.lower.err_msg.?), | |
| 966 | error.InvalidInstruction => |e| return function.fail("emit MIR failed: {s} (Zig compiler bug)", .{@errorName(e)}), | |
| 1008 | 967 | else => |e| return e, |
| 1009 | 968 | }; |
| 1010 | ||
| 1011 | if (function.err_msg) |em| { | |
| 1012 | return Result{ .fail = em }; | |
| 1013 | } else { | |
| 1014 | return Result.ok; | |
| 1015 | } | |
| 1016 | 969 | } |
| 1017 | 970 | |
| 1018 | 971 | const FormatWipMirData = struct { |
| ... | ... | @@ -4758,19 +4711,19 @@ fn airFieldParentPtr(func: *Func, inst: Air.Inst.Index) !void { |
| 4758 | 4711 | return func.fail("TODO implement codegen airFieldParentPtr", .{}); |
| 4759 | 4712 | } |
| 4760 | 4713 | |
| 4761 | fn genArgDbgInfo(func: Func, inst: Air.Inst.Index, mcv: MCValue) !void { | |
| 4714 | fn genArgDbgInfo(func: *const Func, inst: Air.Inst.Index, mcv: MCValue) InnerError!void { | |
| 4762 | 4715 | const arg = func.air.instructions.items(.data)[@intFromEnum(inst)].arg; |
| 4763 | 4716 | const ty = arg.ty.toType(); |
| 4764 | 4717 | if (arg.name == .none) return; |
| 4765 | 4718 | |
| 4766 | 4719 | switch (func.debug_output) { |
| 4767 | 4720 | .dwarf => |dw| switch (mcv) { |
| 4768 | .register => |reg| try dw.genLocalDebugInfo( | |
| 4721 | .register => |reg| dw.genLocalDebugInfo( | |
| 4769 | 4722 | .local_arg, |
| 4770 | 4723 | arg.name.toSlice(func.air), |
| 4771 | 4724 | ty, |
| 4772 | 4725 | .{ .reg = reg.dwarfNum() }, |
| 4773 | ), | |
| 4726 | ) catch |err| return func.fail("failed to generate debug info: {s}", .{@errorName(err)}), | |
| 4774 | 4727 | .load_frame => {}, |
| 4775 | 4728 | else => {}, |
| 4776 | 4729 | }, |
| ... | ... | @@ -4779,7 +4732,7 @@ fn genArgDbgInfo(func: Func, inst: Air.Inst.Index, mcv: MCValue) !void { |
| 4779 | 4732 | } |
| 4780 | 4733 | } |
| 4781 | 4734 | |
| 4782 | fn airArg(func: *Func, inst: Air.Inst.Index) !void { | |
| 4735 | fn airArg(func: *Func, inst: Air.Inst.Index) InnerError!void { | |
| 4783 | 4736 | var arg_index = func.arg_index; |
| 4784 | 4737 | |
| 4785 | 4738 | // we skip over args that have no bits |
| ... | ... | @@ -5255,7 +5208,7 @@ fn airDbgInlineBlock(func: *Func, inst: Air.Inst.Index) !void { |
| 5255 | 5208 | try func.lowerBlock(inst, @ptrCast(func.air.extra[extra.end..][0..extra.data.body_len])); |
| 5256 | 5209 | } |
| 5257 | 5210 | |
| 5258 | fn airDbgVar(func: *Func, inst: Air.Inst.Index) !void { | |
| 5211 | fn airDbgVar(func: *Func, inst: Air.Inst.Index) InnerError!void { | |
| 5259 | 5212 | const pl_op = func.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 5260 | 5213 | const operand = pl_op.operand; |
| 5261 | 5214 | const ty = func.typeOf(operand); |
| ... | ... | @@ -5263,7 +5216,8 @@ fn airDbgVar(func: *Func, inst: Air.Inst.Index) !void { |
| 5263 | 5216 | const name: Air.NullTerminatedString = @enumFromInt(pl_op.payload); |
| 5264 | 5217 | |
| 5265 | 5218 | const tag = func.air.instructions.items(.tag)[@intFromEnum(inst)]; |
| 5266 | try func.genVarDbgInfo(tag, ty, mcv, name.toSlice(func.air)); | |
| 5219 | func.genVarDbgInfo(tag, ty, mcv, name.toSlice(func.air)) catch |err| | |
| 5220 | return func.fail("failed to generate variable debug info: {s}", .{@errorName(err)}); | |
| 5267 | 5221 | |
| 5268 | 5222 | return func.finishAir(inst, .unreach, .{ operand, .none, .none }); |
| 5269 | 5223 | } |
| ... | ... | @@ -8236,10 +8190,7 @@ fn genTypedValue(func: *Func, val: Value) InnerError!MCValue { |
| 8236 | 8190 | return func.fail("TODO: genTypedValue {s}", .{@tagName(mcv)}); |
| 8237 | 8191 | }, |
| 8238 | 8192 | }, |
| 8239 | .fail => |msg| { | |
| 8240 | func.err_msg = msg; | |
| 8241 | return error.CodegenFail; | |
| 8242 | }, | |
| 8193 | .fail => |msg| return func.failMsg(msg), | |
| 8243 | 8194 | }; |
| 8244 | 8195 | return mcv; |
| 8245 | 8196 | } |
| ... | ... | @@ -8427,17 +8378,23 @@ fn wantSafety(func: *Func) bool { |
| 8427 | 8378 | }; |
| 8428 | 8379 | } |
| 8429 | 8380 | |
| 8430 | fn fail(func: *Func, comptime format: []const u8, args: anytype) InnerError { | |
| 8381 | fn fail(func: *const Func, comptime format: []const u8, args: anytype) error{ OutOfMemory, CodegenFail } { | |
| 8431 | 8382 | @branchHint(.cold); |
| 8432 | assert(func.err_msg == null); | |
| 8433 | func.err_msg = try ErrorMsg.create(func.gpa, func.src_loc, format, args); | |
| 8383 | const zcu = func.pt.zcu; | |
| 8384 | switch (func.owner) { | |
| 8385 | .nav_index => |i| return zcu.codegenFail(i, format, args), | |
| 8386 | .lazy_sym => |s| return zcu.codegenFailType(s.ty, format, args), | |
| 8387 | } | |
| 8434 | 8388 | return error.CodegenFail; |
| 8435 | 8389 | } |
| 8436 | 8390 | |
| 8437 | fn failSymbol(func: *Func, comptime format: []const u8, args: anytype) InnerError { | |
| 8391 | fn failMsg(func: *const Func, msg: *ErrorMsg) error{ OutOfMemory, CodegenFail } { | |
| 8438 | 8392 | @branchHint(.cold); |
| 8439 | assert(func.err_msg == null); | |
| 8440 | func.err_msg = try ErrorMsg.create(func.gpa, func.src_loc, format, args); | |
| 8393 | const zcu = func.pt.zcu; | |
| 8394 | switch (func.owner) { | |
| 8395 | .nav_index => |i| return zcu.codegenFailMsg(i, msg), | |
| 8396 | .lazy_sym => |s| return zcu.codegenFailTypeMsg(s.ty, msg), | |
| 8397 | } | |
| 8441 | 8398 | return error.CodegenFail; |
| 8442 | 8399 | } |
| 8443 | 8400 |
src/arch/sparc64/CodeGen.zig+20-24| ... | ... | @@ -21,7 +21,6 @@ const Emit = @import("Emit.zig"); |
| 21 | 21 | const Liveness = @import("../../Liveness.zig"); |
| 22 | 22 | const Type = @import("../../Type.zig"); |
| 23 | 23 | const CodeGenError = codegen.CodeGenError; |
| 24 | const Result = @import("../../codegen.zig").Result; | |
| 25 | 24 | const Endian = std.builtin.Endian; |
| 26 | 25 | const Alignment = InternPool.Alignment; |
| 27 | 26 | |
| ... | ... | @@ -268,7 +267,7 @@ pub fn generate( |
| 268 | 267 | liveness: Liveness, |
| 269 | 268 | code: *std.ArrayList(u8), |
| 270 | 269 | debug_output: link.File.DebugInfoOutput, |
| 271 | ) CodeGenError!Result { | |
| 270 | ) CodeGenError!void { | |
| 272 | 271 | const zcu = pt.zcu; |
| 273 | 272 | const gpa = zcu.gpa; |
| 274 | 273 | const func = zcu.funcInfo(func_index); |
| ... | ... | @@ -310,10 +309,7 @@ pub fn generate( |
| 310 | 309 | defer function.exitlude_jump_relocs.deinit(gpa); |
| 311 | 310 | |
| 312 | 311 | var call_info = function.resolveCallingConventionValues(func_ty, .callee) catch |err| switch (err) { |
| 313 | error.CodegenFail => return Result{ .fail = function.err_msg.? }, | |
| 314 | error.OutOfRegisters => return Result{ | |
| 315 | .fail = try ErrorMsg.create(gpa, src_loc, "CodeGen ran out of registers. This is a bug in the Zig compiler.", .{}), | |
| 316 | }, | |
| 312 | error.CodegenFail => return error.CodegenFail, | |
| 317 | 313 | else => |e| return e, |
| 318 | 314 | }; |
| 319 | 315 | defer call_info.deinit(&function); |
| ... | ... | @@ -324,10 +320,8 @@ pub fn generate( |
| 324 | 320 | function.max_end_stack = call_info.stack_byte_count; |
| 325 | 321 | |
| 326 | 322 | function.gen() catch |err| switch (err) { |
| 327 | error.CodegenFail => return Result{ .fail = function.err_msg.? }, | |
| 328 | error.OutOfRegisters => return Result{ | |
| 329 | .fail = try ErrorMsg.create(gpa, src_loc, "CodeGen ran out of registers. This is a bug in the Zig compiler.", .{}), | |
| 330 | }, | |
| 323 | error.CodegenFail => return error.CodegenFail, | |
| 324 | error.OutOfRegisters => return function.fail("ran out of registers (Zig compiler bug)", .{}), | |
| 331 | 325 | else => |e| return e, |
| 332 | 326 | }; |
| 333 | 327 | |
| ... | ... | @@ -351,15 +345,9 @@ pub fn generate( |
| 351 | 345 | defer emit.deinit(); |
| 352 | 346 | |
| 353 | 347 | emit.emitMir() catch |err| switch (err) { |
| 354 | error.EmitFail => return Result{ .fail = emit.err_msg.? }, | |
| 348 | error.EmitFail => return function.failMsg(emit.err_msg.?), | |
| 355 | 349 | else => |e| return e, |
| 356 | 350 | }; |
| 357 | ||
| 358 | if (function.err_msg) |em| { | |
| 359 | return Result{ .fail = em }; | |
| 360 | } else { | |
| 361 | return Result.ok; | |
| 362 | } | |
| 363 | 351 | } |
| 364 | 352 | |
| 365 | 353 | fn gen(self: *Self) !void { |
| ... | ... | @@ -1014,7 +1002,7 @@ fn airAsm(self: *Self, inst: Air.Inst.Index) !void { |
| 1014 | 1002 | return bt.finishAir(result); |
| 1015 | 1003 | } |
| 1016 | 1004 | |
| 1017 | fn airArg(self: *Self, inst: Air.Inst.Index) !void { | |
| 1005 | fn airArg(self: *Self, inst: Air.Inst.Index) InnerError!void { | |
| 1018 | 1006 | const pt = self.pt; |
| 1019 | 1007 | const zcu = pt.zcu; |
| 1020 | 1008 | const arg_index = self.arg_index; |
| ... | ... | @@ -1036,7 +1024,8 @@ fn airArg(self: *Self, inst: Air.Inst.Index) !void { |
| 1036 | 1024 | } |
| 1037 | 1025 | }; |
| 1038 | 1026 | |
| 1039 | try self.genArgDbgInfo(inst, mcv); | |
| 1027 | self.genArgDbgInfo(inst, mcv) catch |err| | |
| 1028 | return self.fail("failed to generate debug info for parameter: {s}", .{@errorName(err)}); | |
| 1040 | 1029 | |
| 1041 | 1030 | if (self.liveness.isUnused(inst)) |
| 1042 | 1031 | return self.finishAirBookkeeping(); |
| ... | ... | @@ -3511,12 +3500,19 @@ fn errUnionPayload(self: *Self, error_union_mcv: MCValue, error_union_ty: Type) |
| 3511 | 3500 | } |
| 3512 | 3501 | } |
| 3513 | 3502 | |
| 3514 | fn fail(self: *Self, comptime format: []const u8, args: anytype) InnerError { | |
| 3503 | fn fail(self: *Self, comptime format: []const u8, args: anytype) error{ OutOfMemory, CodegenFail } { | |
| 3515 | 3504 | @branchHint(.cold); |
| 3516 | assert(self.err_msg == null); | |
| 3517 | const gpa = self.gpa; | |
| 3518 | self.err_msg = try ErrorMsg.create(gpa, self.src_loc, format, args); | |
| 3519 | return error.CodegenFail; | |
| 3505 | const zcu = self.pt.zcu; | |
| 3506 | const func = zcu.funcInfo(self.func_index); | |
| 3507 | const msg = try ErrorMsg.create(zcu.gpa, self.src_loc, format, args); | |
| 3508 | return zcu.codegenFailMsg(func.owner_nav, msg); | |
| 3509 | } | |
| 3510 | ||
| 3511 | fn failMsg(self: *Self, msg: *ErrorMsg) error{ OutOfMemory, CodegenFail } { | |
| 3512 | @branchHint(.cold); | |
| 3513 | const zcu = self.pt.zcu; | |
| 3514 | const func = zcu.funcInfo(self.func_index); | |
| 3515 | return zcu.codegenFailMsg(func.owner_nav, msg); | |
| 3520 | 3516 | } |
| 3521 | 3517 | |
| 3522 | 3518 | /// Called when there are no operands, and the instruction is always unreferenced. |
src/arch/x86_64/CodeGen.zig+34-83| ... | ... | @@ -19,7 +19,6 @@ const Allocator = mem.Allocator; |
| 19 | 19 | const CodeGenError = codegen.CodeGenError; |
| 20 | 20 | const Compilation = @import("../../Compilation.zig"); |
| 21 | 21 | const ErrorMsg = Zcu.ErrorMsg; |
| 22 | const Result = codegen.Result; | |
| 23 | 22 | const Emit = @import("Emit.zig"); |
| 24 | 23 | const Liveness = @import("../../Liveness.zig"); |
| 25 | 24 | const Lower = @import("Lower.zig"); |
| ... | ... | @@ -59,7 +58,6 @@ target: *const std.Target, |
| 59 | 58 | owner: Owner, |
| 60 | 59 | inline_func: InternPool.Index, |
| 61 | 60 | mod: *Package.Module, |
| 62 | err_msg: ?*ErrorMsg, | |
| 63 | 61 | arg_index: u32, |
| 64 | 62 | args: []MCValue, |
| 65 | 63 | va_info: union { |
| ... | ... | @@ -821,7 +819,7 @@ pub fn generate( |
| 821 | 819 | liveness: Liveness, |
| 822 | 820 | code: *std.ArrayList(u8), |
| 823 | 821 | debug_output: link.File.DebugInfoOutput, |
| 824 | ) CodeGenError!Result { | |
| 822 | ) CodeGenError!void { | |
| 825 | 823 | const zcu = pt.zcu; |
| 826 | 824 | const comp = zcu.comp; |
| 827 | 825 | const gpa = zcu.gpa; |
| ... | ... | @@ -841,7 +839,6 @@ pub fn generate( |
| 841 | 839 | .debug_output = debug_output, |
| 842 | 840 | .owner = .{ .nav_index = func.owner_nav }, |
| 843 | 841 | .inline_func = func_index, |
| 844 | .err_msg = null, | |
| 845 | 842 | .arg_index = undefined, |
| 846 | 843 | .args = undefined, // populated after `resolveCallingConventionValues` |
| 847 | 844 | .va_info = undefined, // populated after `resolveCallingConventionValues` |
| ... | ... | @@ -881,15 +878,7 @@ pub fn generate( |
| 881 | 878 | const fn_info = zcu.typeToFunc(fn_type).?; |
| 882 | 879 | const cc = abi.resolveCallingConvention(fn_info.cc, function.target.*); |
| 883 | 880 | var call_info = function.resolveCallingConventionValues(fn_info, &.{}, .args_frame) catch |err| switch (err) { |
| 884 | error.CodegenFail => return Result{ .fail = function.err_msg.? }, | |
| 885 | error.OutOfRegisters => return Result{ | |
| 886 | .fail = try ErrorMsg.create( | |
| 887 | gpa, | |
| 888 | src_loc, | |
| 889 | "CodeGen ran out of registers. This is a bug in the Zig compiler.", | |
| 890 | .{}, | |
| 891 | ), | |
| 892 | }, | |
| 881 | error.CodegenFail => return error.CodegenFail, | |
| 893 | 882 | else => |e| return e, |
| 894 | 883 | }; |
| 895 | 884 | defer call_info.deinit(&function); |
| ... | ... | @@ -926,10 +915,8 @@ pub fn generate( |
| 926 | 915 | }; |
| 927 | 916 | |
| 928 | 917 | function.gen() catch |err| switch (err) { |
| 929 | error.CodegenFail => return Result{ .fail = function.err_msg.? }, | |
| 930 | error.OutOfRegisters => return Result{ | |
| 931 | .fail = try ErrorMsg.create(gpa, src_loc, "CodeGen ran out of registers. This is a bug in the Zig compiler.", .{}), | |
| 932 | }, | |
| 918 | error.CodegenFail => return error.CodegenFail, | |
| 919 | error.OutOfRegisters => return function.fail("ran out of registers (Zig compiler bug)", .{}), | |
| 933 | 920 | else => |e| return e, |
| 934 | 921 | }; |
| 935 | 922 | |
| ... | ... | @@ -953,10 +940,7 @@ pub fn generate( |
| 953 | 940 | .pic = mod.pic, |
| 954 | 941 | }, |
| 955 | 942 | .atom_index = function.owner.getSymbolIndex(&function) catch |err| switch (err) { |
| 956 | error.CodegenFail => return Result{ .fail = function.err_msg.? }, | |
| 957 | error.OutOfRegisters => return Result{ | |
| 958 | .fail = try ErrorMsg.create(gpa, src_loc, "CodeGen ran out of registers. This is a bug in the Zig compiler.", .{}), | |
| 959 | }, | |
| 943 | error.CodegenFail => return error.CodegenFail, | |
| 960 | 944 | else => |e| return e, |
| 961 | 945 | }, |
| 962 | 946 | .debug_output = debug_output, |
| ... | ... | @@ -974,29 +958,11 @@ pub fn generate( |
| 974 | 958 | }; |
| 975 | 959 | defer emit.deinit(); |
| 976 | 960 | emit.emitMir() catch |err| switch (err) { |
| 977 | error.LowerFail, error.EmitFail => return Result{ .fail = emit.lower.err_msg.? }, | |
| 978 | error.InvalidInstruction, error.CannotEncode => |e| { | |
| 979 | const msg = switch (e) { | |
| 980 | error.InvalidInstruction => "CodeGen failed to find a viable instruction.", | |
| 981 | error.CannotEncode => "CodeGen failed to encode the instruction.", | |
| 982 | }; | |
| 983 | return Result{ | |
| 984 | .fail = try ErrorMsg.create( | |
| 985 | gpa, | |
| 986 | src_loc, | |
| 987 | "{s} This is a bug in the Zig compiler.", | |
| 988 | .{msg}, | |
| 989 | ), | |
| 990 | }; | |
| 991 | }, | |
| 992 | else => |e| return e, | |
| 993 | }; | |
| 961 | error.LowerFail, error.EmitFail => return function.failMsg(emit.lower.err_msg.?), | |
| 994 | 962 | |
| 995 | if (function.err_msg) |em| { | |
| 996 | return Result{ .fail = em }; | |
| 997 | } else { | |
| 998 | return Result.ok; | |
| 999 | } | |
| 963 | error.InvalidInstruction, error.CannotEncode => |e| return function.fail("emit MIR failed: {s} (Zig compiler bug)", .{@errorName(e)}), | |
| 964 | else => |e| return function.fail("emit MIR failed: {s}", .{@errorName(e)}), | |
| 965 | }; | |
| 1000 | 966 | } |
| 1001 | 967 | |
| 1002 | 968 | pub fn generateLazy( |
| ... | ... | @@ -1006,7 +972,7 @@ pub fn generateLazy( |
| 1006 | 972 | lazy_sym: link.File.LazySymbol, |
| 1007 | 973 | code: *std.ArrayList(u8), |
| 1008 | 974 | debug_output: link.File.DebugInfoOutput, |
| 1009 | ) CodeGenError!Result { | |
| 975 | ) CodeGenError!void { | |
| 1010 | 976 | const comp = bin_file.comp; |
| 1011 | 977 | const gpa = comp.gpa; |
| 1012 | 978 | // This function is for generating global code, so we use the root module. |
| ... | ... | @@ -1022,7 +988,6 @@ pub fn generateLazy( |
| 1022 | 988 | .debug_output = debug_output, |
| 1023 | 989 | .owner = .{ .lazy_sym = lazy_sym }, |
| 1024 | 990 | .inline_func = undefined, |
| 1025 | .err_msg = null, | |
| 1026 | 991 | .arg_index = undefined, |
| 1027 | 992 | .args = undefined, |
| 1028 | 993 | .va_info = undefined, |
| ... | ... | @@ -1038,10 +1003,8 @@ pub fn generateLazy( |
| 1038 | 1003 | } |
| 1039 | 1004 | |
| 1040 | 1005 | function.genLazy(lazy_sym) catch |err| switch (err) { |
| 1041 | error.CodegenFail => return Result{ .fail = function.err_msg.? }, | |
| 1042 | error.OutOfRegisters => return Result{ | |
| 1043 | .fail = try ErrorMsg.create(gpa, src_loc, "CodeGen ran out of registers. This is a bug in the Zig compiler.", .{}), | |
| 1044 | }, | |
| 1006 | error.CodegenFail => return error.CodegenFail, | |
| 1007 | error.OutOfRegisters => return function.fail("ran out of registers (Zig compiler bug)", .{}), | |
| 1045 | 1008 | else => |e| return e, |
| 1046 | 1009 | }; |
| 1047 | 1010 | |
| ... | ... | @@ -1065,10 +1028,7 @@ pub fn generateLazy( |
| 1065 | 1028 | .pic = mod.pic, |
| 1066 | 1029 | }, |
| 1067 | 1030 | .atom_index = function.owner.getSymbolIndex(&function) catch |err| switch (err) { |
| 1068 | error.CodegenFail => return Result{ .fail = function.err_msg.? }, | |
| 1069 | error.OutOfRegisters => return Result{ | |
| 1070 | .fail = try ErrorMsg.create(gpa, src_loc, "CodeGen ran out of registers. This is a bug in the Zig compiler.", .{}), | |
| 1071 | }, | |
| 1031 | error.CodegenFail => return error.CodegenFail, | |
| 1072 | 1032 | else => |e| return e, |
| 1073 | 1033 | }, |
| 1074 | 1034 | .debug_output = debug_output, |
| ... | ... | @@ -1078,29 +1038,11 @@ pub fn generateLazy( |
| 1078 | 1038 | }; |
| 1079 | 1039 | defer emit.deinit(); |
| 1080 | 1040 | emit.emitMir() catch |err| switch (err) { |
| 1081 | error.LowerFail, error.EmitFail => return Result{ .fail = emit.lower.err_msg.? }, | |
| 1082 | error.InvalidInstruction, error.CannotEncode => |e| { | |
| 1083 | const msg = switch (e) { | |
| 1084 | error.InvalidInstruction => "CodeGen failed to find a viable instruction.", | |
| 1085 | error.CannotEncode => "CodeGen failed to encode the instruction.", | |
| 1086 | }; | |
| 1087 | return Result{ | |
| 1088 | .fail = try ErrorMsg.create( | |
| 1089 | gpa, | |
| 1090 | src_loc, | |
| 1091 | "{s} This is a bug in the Zig compiler.", | |
| 1092 | .{msg}, | |
| 1093 | ), | |
| 1094 | }; | |
| 1095 | }, | |
| 1096 | else => |e| return e, | |
| 1041 | error.LowerFail, error.EmitFail => return function.failMsg(emit.lower.err_msg.?), | |
| 1042 | error.InvalidInstruction => return function.fail("failed to find a viable x86 instruction (Zig compiler bug)", .{}), | |
| 1043 | error.CannotEncode => return function.fail("failed to find encode x86 instruction (Zig compiler bug)", .{}), | |
| 1044 | else => |e| return function.fail("failed to emit MIR: {s}", .{@errorName(e)}), | |
| 1097 | 1045 | }; |
| 1098 | ||
| 1099 | if (function.err_msg) |em| { | |
| 1100 | return Result{ .fail = em }; | |
| 1101 | } else { | |
| 1102 | return Result.ok; | |
| 1103 | } | |
| 1104 | 1046 | } |
| 1105 | 1047 | |
| 1106 | 1048 | const FormatNavData = struct { |
| ... | ... | @@ -19276,10 +19218,7 @@ fn genTypedValue(self: *Self, val: Value) InnerError!MCValue { |
| 19276 | 19218 | .load_got => |sym_index| .{ .lea_got = sym_index }, |
| 19277 | 19219 | .load_tlv => |sym_index| .{ .lea_tlv = sym_index }, |
| 19278 | 19220 | }, |
| 19279 | .fail => |msg| { | |
| 19280 | self.err_msg = msg; | |
| 19281 | return error.CodegenFail; | |
| 19282 | }, | |
| 19221 | .fail => |msg| return self.failMsg(msg), | |
| 19283 | 19222 | }; |
| 19284 | 19223 | } |
| 19285 | 19224 | |
| ... | ... | @@ -19592,11 +19531,23 @@ fn resolveCallingConventionValues( |
| 19592 | 19531 | return result; |
| 19593 | 19532 | } |
| 19594 | 19533 | |
| 19595 | fn fail(self: *Self, comptime format: []const u8, args: anytype) InnerError { | |
| 19534 | fn fail(self: *Self, comptime format: []const u8, args: anytype) error{ OutOfMemory, CodegenFail } { | |
| 19596 | 19535 | @branchHint(.cold); |
| 19597 | assert(self.err_msg == null); | |
| 19598 | const gpa = self.gpa; | |
| 19599 | self.err_msg = try ErrorMsg.create(gpa, self.src_loc, format, args); | |
| 19536 | const zcu = self.pt.zcu; | |
| 19537 | switch (self.owner) { | |
| 19538 | .nav_index => |i| return zcu.codegenFail(i, format, args), | |
| 19539 | .lazy_sym => |s| return zcu.codegenFailType(s.ty, format, args), | |
| 19540 | } | |
| 19541 | return error.CodegenFail; | |
| 19542 | } | |
| 19543 | ||
| 19544 | fn failMsg(self: *Self, msg: *ErrorMsg) error{ OutOfMemory, CodegenFail } { | |
| 19545 | @branchHint(.cold); | |
| 19546 | const zcu = self.pt.zcu; | |
| 19547 | switch (self.owner) { | |
| 19548 | .nav_index => |i| return zcu.codegenFailMsg(i, msg), | |
| 19549 | .lazy_sym => |s| return zcu.codegenFailTypeMsg(s.ty, msg), | |
| 19550 | } | |
| 19600 | 19551 | return error.CodegenFail; |
| 19601 | 19552 | } |
| 19602 | 19553 |
src/codegen.zig+56-113| ... | ... | @@ -24,13 +24,6 @@ const Zir = std.zig.Zir; |
| 24 | 24 | const Alignment = InternPool.Alignment; |
| 25 | 25 | const dev = @import("dev.zig"); |
| 26 | 26 | |
| 27 | pub const Result = union(enum) { | |
| 28 | /// The `code` parameter passed to `generateSymbol` has the value. | |
| 29 | ok, | |
| 30 | /// There was a codegen error. | |
| 31 | fail: *ErrorMsg, | |
| 32 | }; | |
| 33 | ||
| 34 | 27 | pub const CodeGenError = error{ |
| 35 | 28 | OutOfMemory, |
| 36 | 29 | /// Compiler was asked to operate on a number larger than supported. |
| ... | ... | @@ -64,7 +57,7 @@ pub fn generateFunction( |
| 64 | 57 | liveness: Liveness, |
| 65 | 58 | code: *std.ArrayList(u8), |
| 66 | 59 | debug_output: link.File.DebugInfoOutput, |
| 67 | ) CodeGenError!Result { | |
| 60 | ) CodeGenError!void { | |
| 68 | 61 | const zcu = pt.zcu; |
| 69 | 62 | const func = zcu.funcInfo(func_index); |
| 70 | 63 | const target = zcu.navFileScope(func.owner_nav).mod.resolved_target.result; |
| ... | ... | @@ -89,7 +82,7 @@ pub fn generateLazyFunction( |
| 89 | 82 | lazy_sym: link.File.LazySymbol, |
| 90 | 83 | code: *std.ArrayList(u8), |
| 91 | 84 | debug_output: link.File.DebugInfoOutput, |
| 92 | ) CodeGenError!Result { | |
| 85 | ) CodeGenError!void { | |
| 93 | 86 | const zcu = pt.zcu; |
| 94 | 87 | const file = Type.fromInterned(lazy_sym.ty).typeDeclInstAllowGeneratedTag(zcu).?.resolveFile(&zcu.intern_pool); |
| 95 | 88 | const target = zcu.fileByIndex(file).mod.resolved_target.result; |
| ... | ... | @@ -120,17 +113,17 @@ pub fn generateLazySymbol( |
| 120 | 113 | code: *std.ArrayList(u8), |
| 121 | 114 | debug_output: link.File.DebugInfoOutput, |
| 122 | 115 | reloc_parent: link.File.RelocInfo.Parent, |
| 123 | ) CodeGenError!Result { | |
| 116 | ) CodeGenError!void { | |
| 124 | 117 | _ = reloc_parent; |
| 125 | 118 | |
| 126 | 119 | const tracy = trace(@src()); |
| 127 | 120 | defer tracy.end(); |
| 128 | 121 | |
| 129 | 122 | const comp = bin_file.comp; |
| 130 | const ip = &pt.zcu.intern_pool; | |
| 123 | const zcu = pt.zcu; | |
| 124 | const ip = &zcu.intern_pool; | |
| 131 | 125 | const target = comp.root_mod.resolved_target.result; |
| 132 | 126 | const endian = target.cpu.arch.endian(); |
| 133 | const gpa = comp.gpa; | |
| 134 | 127 | |
| 135 | 128 | log.debug("generateLazySymbol: kind = {s}, ty = {}", .{ |
| 136 | 129 | @tagName(lazy_sym.kind), |
| ... | ... | @@ -161,26 +154,29 @@ pub fn generateLazySymbol( |
| 161 | 154 | string_index += @intCast(err_name.len + 1); |
| 162 | 155 | } |
| 163 | 156 | mem.writeInt(u32, code.items[offset_index..][0..4], string_index, endian); |
| 164 | return .ok; | |
| 165 | } else if (Type.fromInterned(lazy_sym.ty).zigTypeTag(pt.zcu) == .@"enum") { | |
| 157 | } else if (Type.fromInterned(lazy_sym.ty).zigTypeTag(zcu) == .@"enum") { | |
| 166 | 158 | alignment.* = .@"1"; |
| 167 | 159 | const enum_ty = Type.fromInterned(lazy_sym.ty); |
| 168 | const tag_names = enum_ty.enumFields(pt.zcu); | |
| 160 | const tag_names = enum_ty.enumFields(zcu); | |
| 169 | 161 | for (0..tag_names.len) |tag_index| { |
| 170 | 162 | const tag_name = tag_names.get(ip)[tag_index].toSlice(ip); |
| 171 | 163 | try code.ensureUnusedCapacity(tag_name.len + 1); |
| 172 | 164 | code.appendSliceAssumeCapacity(tag_name); |
| 173 | 165 | code.appendAssumeCapacity(0); |
| 174 | 166 | } |
| 175 | return .ok; | |
| 176 | } else return .{ .fail = try .create( | |
| 177 | gpa, | |
| 178 | src_loc, | |
| 179 | "TODO implement generateLazySymbol for {s} {}", | |
| 180 | .{ @tagName(lazy_sym.kind), Type.fromInterned(lazy_sym.ty).fmt(pt) }, | |
| 181 | ) }; | |
| 167 | } else { | |
| 168 | return zcu.codegenFailType(lazy_sym.ty, "TODO implement generateLazySymbol for {s} {}", .{ | |
| 169 | @tagName(lazy_sym.kind), Type.fromInterned(lazy_sym.ty).fmt(pt), | |
| 170 | }); | |
| 171 | } | |
| 182 | 172 | } |
| 183 | 173 | |
| 174 | pub const GenerateSymbolError = error{ | |
| 175 | OutOfMemory, | |
| 176 | /// Compiler was asked to operate on a number larger than supported. | |
| 177 | Overflow, | |
| 178 | }; | |
| 179 | ||
| 184 | 180 | pub fn generateSymbol( |
| 185 | 181 | bin_file: *link.File, |
| 186 | 182 | pt: Zcu.PerThread, |
| ... | ... | @@ -188,7 +184,7 @@ pub fn generateSymbol( |
| 188 | 184 | val: Value, |
| 189 | 185 | code: *std.ArrayList(u8), |
| 190 | 186 | reloc_parent: link.File.RelocInfo.Parent, |
| 191 | ) CodeGenError!Result { | |
| 187 | ) GenerateSymbolError!void { | |
| 192 | 188 | const tracy = trace(@src()); |
| 193 | 189 | defer tracy.end(); |
| 194 | 190 | |
| ... | ... | @@ -204,7 +200,7 @@ pub fn generateSymbol( |
| 204 | 200 | if (val.isUndefDeep(zcu)) { |
| 205 | 201 | const abi_size = math.cast(usize, ty.abiSize(zcu)) orelse return error.Overflow; |
| 206 | 202 | try code.appendNTimes(0xaa, abi_size); |
| 207 | return .ok; | |
| 203 | return; | |
| 208 | 204 | } |
| 209 | 205 | |
| 210 | 206 | switch (ip.indexToKey(val.toIntern())) { |
| ... | ... | @@ -266,7 +262,7 @@ pub fn generateSymbol( |
| 266 | 262 | |
| 267 | 263 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) { |
| 268 | 264 | try code.writer().writeInt(u16, err_val, endian); |
| 269 | return .ok; | |
| 265 | return; | |
| 270 | 266 | } |
| 271 | 267 | |
| 272 | 268 | const payload_align = payload_ty.abiAlignment(zcu); |
| ... | ... | @@ -281,13 +277,10 @@ pub fn generateSymbol( |
| 281 | 277 | // emit payload part of the error union |
| 282 | 278 | { |
| 283 | 279 | const begin = code.items.len; |
| 284 | switch (try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(switch (error_union.val) { | |
| 280 | try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(switch (error_union.val) { | |
| 285 | 281 | .err_name => try pt.intern(.{ .undef = payload_ty.toIntern() }), |
| 286 | 282 | .payload => |payload| payload, |
| 287 | }), code, reloc_parent)) { | |
| 288 | .ok => {}, | |
| 289 | .fail => |em| return .{ .fail = em }, | |
| 290 | } | |
| 283 | }), code, reloc_parent); | |
| 291 | 284 | const unpadded_end = code.items.len - begin; |
| 292 | 285 | const padded_end = abi_align.forward(unpadded_end); |
| 293 | 286 | const padding = math.cast(usize, padded_end - unpadded_end) orelse return error.Overflow; |
| ... | ... | @@ -312,10 +305,7 @@ pub fn generateSymbol( |
| 312 | 305 | }, |
| 313 | 306 | .enum_tag => |enum_tag| { |
| 314 | 307 | const int_tag_ty = ty.intTagType(zcu); |
| 315 | switch (try generateSymbol(bin_file, pt, src_loc, try pt.getCoerced(Value.fromInterned(enum_tag.int), int_tag_ty), code, reloc_parent)) { | |
| 316 | .ok => {}, | |
| 317 | .fail => |em| return .{ .fail = em }, | |
| 318 | } | |
| 308 | try generateSymbol(bin_file, pt, src_loc, try pt.getCoerced(Value.fromInterned(enum_tag.int), int_tag_ty), code, reloc_parent); | |
| 319 | 309 | }, |
| 320 | 310 | .float => |float| switch (float.storage) { |
| 321 | 311 | .f16 => |f16_val| writeFloat(f16, f16_val, target, endian, try code.addManyAsArray(2)), |
| ... | ... | @@ -328,19 +318,10 @@ pub fn generateSymbol( |
| 328 | 318 | }, |
| 329 | 319 | .f128 => |f128_val| writeFloat(f128, f128_val, target, endian, try code.addManyAsArray(16)), |
| 330 | 320 | }, |
| 331 | .ptr => switch (try lowerPtr(bin_file, pt, src_loc, val.toIntern(), code, reloc_parent, 0)) { | |
| 332 | .ok => {}, | |
| 333 | .fail => |em| return .{ .fail = em }, | |
| 334 | }, | |
| 321 | .ptr => try lowerPtr(bin_file, pt, src_loc, val.toIntern(), code, reloc_parent, 0), | |
| 335 | 322 | .slice => |slice| { |
| 336 | switch (try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(slice.ptr), code, reloc_parent)) { | |
| 337 | .ok => {}, | |
| 338 | .fail => |em| return .{ .fail = em }, | |
| 339 | } | |
| 340 | switch (try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(slice.len), code, reloc_parent)) { | |
| 341 | .ok => {}, | |
| 342 | .fail => |em| return .{ .fail = em }, | |
| 343 | } | |
| 323 | try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(slice.ptr), code, reloc_parent); | |
| 324 | try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(slice.len), code, reloc_parent); | |
| 344 | 325 | }, |
| 345 | 326 | .opt => { |
| 346 | 327 | const payload_type = ty.optionalChild(zcu); |
| ... | ... | @@ -349,10 +330,7 @@ pub fn generateSymbol( |
| 349 | 330 | |
| 350 | 331 | if (ty.optionalReprIsPayload(zcu)) { |
| 351 | 332 | if (payload_val) |value| { |
| 352 | switch (try generateSymbol(bin_file, pt, src_loc, value, code, reloc_parent)) { | |
| 353 | .ok => {}, | |
| 354 | .fail => |em| return Result{ .fail = em }, | |
| 355 | } | |
| 333 | try generateSymbol(bin_file, pt, src_loc, value, code, reloc_parent); | |
| 356 | 334 | } else { |
| 357 | 335 | try code.appendNTimes(0, abi_size); |
| 358 | 336 | } |
| ... | ... | @@ -362,10 +340,7 @@ pub fn generateSymbol( |
| 362 | 340 | const value = payload_val orelse Value.fromInterned(try pt.intern(.{ |
| 363 | 341 | .undef = payload_type.toIntern(), |
| 364 | 342 | })); |
| 365 | switch (try generateSymbol(bin_file, pt, src_loc, value, code, reloc_parent)) { | |
| 366 | .ok => {}, | |
| 367 | .fail => |em| return Result{ .fail = em }, | |
| 368 | } | |
| 343 | try generateSymbol(bin_file, pt, src_loc, value, code, reloc_parent); | |
| 369 | 344 | } |
| 370 | 345 | try code.writer().writeByte(@intFromBool(payload_val != null)); |
| 371 | 346 | try code.appendNTimes(0, padding); |
| ... | ... | @@ -377,17 +352,14 @@ pub fn generateSymbol( |
| 377 | 352 | .elems, .repeated_elem => { |
| 378 | 353 | var index: u64 = 0; |
| 379 | 354 | while (index < array_type.lenIncludingSentinel()) : (index += 1) { |
| 380 | switch (try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(switch (aggregate.storage) { | |
| 355 | try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(switch (aggregate.storage) { | |
| 381 | 356 | .bytes => unreachable, |
| 382 | 357 | .elems => |elems| elems[@intCast(index)], |
| 383 | 358 | .repeated_elem => |elem| if (index < array_type.len) |
| 384 | 359 | elem |
| 385 | 360 | else |
| 386 | 361 | array_type.sentinel, |
| 387 | }), code, reloc_parent)) { | |
| 388 | .ok => {}, | |
| 389 | .fail => |em| return .{ .fail = em }, | |
| 390 | } | |
| 362 | }), code, reloc_parent); | |
| 391 | 363 | } |
| 392 | 364 | }, |
| 393 | 365 | }, |
| ... | ... | @@ -437,16 +409,13 @@ pub fn generateSymbol( |
| 437 | 409 | .elems, .repeated_elem => { |
| 438 | 410 | var index: u64 = 0; |
| 439 | 411 | while (index < vector_type.len) : (index += 1) { |
| 440 | switch (try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(switch (aggregate.storage) { | |
| 412 | try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(switch (aggregate.storage) { | |
| 441 | 413 | .bytes => unreachable, |
| 442 | 414 | .elems => |elems| elems[ |
| 443 | 415 | math.cast(usize, index) orelse return error.Overflow |
| 444 | 416 | ], |
| 445 | 417 | .repeated_elem => |elem| elem, |
| 446 | }), code, reloc_parent)) { | |
| 447 | .ok => {}, | |
| 448 | .fail => |em| return .{ .fail = em }, | |
| 449 | } | |
| 418 | }), code, reloc_parent); | |
| 450 | 419 | } |
| 451 | 420 | }, |
| 452 | 421 | } |
| ... | ... | @@ -476,10 +445,7 @@ pub fn generateSymbol( |
| 476 | 445 | .repeated_elem => |elem| elem, |
| 477 | 446 | }; |
| 478 | 447 | |
| 479 | switch (try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(field_val), code, reloc_parent)) { | |
| 480 | .ok => {}, | |
| 481 | .fail => |em| return Result{ .fail = em }, | |
| 482 | } | |
| 448 | try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(field_val), code, reloc_parent); | |
| 483 | 449 | const unpadded_field_end = code.items.len - struct_begin; |
| 484 | 450 | |
| 485 | 451 | // Pad struct members if required |
| ... | ... | @@ -518,10 +484,8 @@ pub fn generateSymbol( |
| 518 | 484 | return error.Overflow; |
| 519 | 485 | var tmp_list = try std.ArrayList(u8).initCapacity(code.allocator, field_size); |
| 520 | 486 | defer tmp_list.deinit(); |
| 521 | switch (try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(field_val), &tmp_list, reloc_parent)) { | |
| 522 | .ok => @memcpy(code.items[current_pos..][0..tmp_list.items.len], tmp_list.items), | |
| 523 | .fail => |em| return Result{ .fail = em }, | |
| 524 | } | |
| 487 | try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(field_val), &tmp_list, reloc_parent); | |
| 488 | @memcpy(code.items[current_pos..][0..tmp_list.items.len], tmp_list.items); | |
| 525 | 489 | } else { |
| 526 | 490 | Value.fromInterned(field_val).writeToPackedMemory(Type.fromInterned(field_ty), pt, code.items[current_pos..], bits) catch unreachable; |
| 527 | 491 | } |
| ... | ... | @@ -553,10 +517,7 @@ pub fn generateSymbol( |
| 553 | 517 | ) orelse return error.Overflow; |
| 554 | 518 | if (padding > 0) try code.appendNTimes(0, padding); |
| 555 | 519 | |
| 556 | switch (try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(field_val), code, reloc_parent)) { | |
| 557 | .ok => {}, | |
| 558 | .fail => |em| return Result{ .fail = em }, | |
| 559 | } | |
| 520 | try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(field_val), code, reloc_parent); | |
| 560 | 521 | } |
| 561 | 522 | |
| 562 | 523 | const size = struct_type.sizeUnordered(ip); |
| ... | ... | @@ -582,10 +543,7 @@ pub fn generateSymbol( |
| 582 | 543 | |
| 583 | 544 | // Check if we should store the tag first. |
| 584 | 545 | if (layout.tag_size > 0 and layout.tag_align.compare(.gte, layout.payload_align)) { |
| 585 | switch (try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(un.tag), code, reloc_parent)) { | |
| 586 | .ok => {}, | |
| 587 | .fail => |em| return Result{ .fail = em }, | |
| 588 | } | |
| 546 | try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(un.tag), code, reloc_parent); | |
| 589 | 547 | } |
| 590 | 548 | |
| 591 | 549 | const union_obj = zcu.typeToUnion(ty).?; |
| ... | ... | @@ -595,10 +553,7 @@ pub fn generateSymbol( |
| 595 | 553 | if (!field_ty.hasRuntimeBits(zcu)) { |
| 596 | 554 | try code.appendNTimes(0xaa, math.cast(usize, layout.payload_size) orelse return error.Overflow); |
| 597 | 555 | } else { |
| 598 | switch (try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(un.val), code, reloc_parent)) { | |
| 599 | .ok => {}, | |
| 600 | .fail => |em| return Result{ .fail = em }, | |
| 601 | } | |
| 556 | try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(un.val), code, reloc_parent); | |
| 602 | 557 | |
| 603 | 558 | const padding = math.cast(usize, layout.payload_size - field_ty.abiSize(zcu)) orelse return error.Overflow; |
| 604 | 559 | if (padding > 0) { |
| ... | ... | @@ -606,17 +561,11 @@ pub fn generateSymbol( |
| 606 | 561 | } |
| 607 | 562 | } |
| 608 | 563 | } else { |
| 609 | switch (try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(un.val), code, reloc_parent)) { | |
| 610 | .ok => {}, | |
| 611 | .fail => |em| return Result{ .fail = em }, | |
| 612 | } | |
| 564 | try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(un.val), code, reloc_parent); | |
| 613 | 565 | } |
| 614 | 566 | |
| 615 | 567 | if (layout.tag_size > 0 and layout.tag_align.compare(.lt, layout.payload_align)) { |
| 616 | switch (try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(un.tag), code, reloc_parent)) { | |
| 617 | .ok => {}, | |
| 618 | .fail => |em| return Result{ .fail = em }, | |
| 619 | } | |
| 568 | try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(un.tag), code, reloc_parent); | |
| 620 | 569 | |
| 621 | 570 | if (layout.padding > 0) { |
| 622 | 571 | try code.appendNTimes(0, layout.padding); |
| ... | ... | @@ -625,7 +574,6 @@ pub fn generateSymbol( |
| 625 | 574 | }, |
| 626 | 575 | .memoized_call => unreachable, |
| 627 | 576 | } |
| 628 | return .ok; | |
| 629 | 577 | } |
| 630 | 578 | |
| 631 | 579 | fn lowerPtr( |
| ... | ... | @@ -636,7 +584,7 @@ fn lowerPtr( |
| 636 | 584 | code: *std.ArrayList(u8), |
| 637 | 585 | reloc_parent: link.File.RelocInfo.Parent, |
| 638 | 586 | prev_offset: u64, |
| 639 | ) CodeGenError!Result { | |
| 587 | ) GenerateSymbolError!void { | |
| 640 | 588 | const zcu = pt.zcu; |
| 641 | 589 | const ptr = zcu.intern_pool.indexToKey(ptr_val).ptr; |
| 642 | 590 | const offset: u64 = prev_offset + ptr.byte_offset; |
| ... | ... | @@ -689,7 +637,7 @@ fn lowerUavRef( |
| 689 | 637 | code: *std.ArrayList(u8), |
| 690 | 638 | reloc_parent: link.File.RelocInfo.Parent, |
| 691 | 639 | offset: u64, |
| 692 | ) CodeGenError!Result { | |
| 640 | ) GenerateSymbolError!void { | |
| 693 | 641 | const zcu = pt.zcu; |
| 694 | 642 | const gpa = zcu.gpa; |
| 695 | 643 | const ip = &zcu.intern_pool; |
| ... | ... | @@ -702,14 +650,7 @@ fn lowerUavRef( |
| 702 | 650 | const is_fn_body = uav_ty.zigTypeTag(zcu) == .@"fn"; |
| 703 | 651 | if (!is_fn_body and !uav_ty.hasRuntimeBits(zcu)) { |
| 704 | 652 | try code.appendNTimes(0xaa, ptr_width_bytes); |
| 705 | return .ok; | |
| 706 | } | |
| 707 | ||
| 708 | const uav_align = ip.indexToKey(uav.orig_ty).ptr_type.flags.alignment; | |
| 709 | const res = try lf.lowerUav(pt, uav_val, uav_align, src_loc); | |
| 710 | switch (res) { | |
| 711 | .mcv => {}, | |
| 712 | .fail => |em| return .{ .fail = em }, | |
| 653 | return; | |
| 713 | 654 | } |
| 714 | 655 | |
| 715 | 656 | switch (lf.tag) { |
| ... | ... | @@ -727,11 +668,17 @@ fn lowerUavRef( |
| 727 | 668 | .pointee = .{ .uav_index = uav.val }, |
| 728 | 669 | }); |
| 729 | 670 | try code.appendNTimes(0, ptr_width_bytes); |
| 730 | return .ok; | |
| 671 | return; | |
| 731 | 672 | }, |
| 732 | 673 | else => {}, |
| 733 | 674 | } |
| 734 | 675 | |
| 676 | const uav_align = ip.indexToKey(uav.orig_ty).ptr_type.flags.alignment; | |
| 677 | switch (try lf.lowerUav(pt, uav_val, uav_align, src_loc)) { | |
| 678 | .mcv => {}, | |
| 679 | .fail => |em| std.debug.panic("TODO rework lowerUav. internal error: {s}", .{em.msg}), | |
| 680 | } | |
| 681 | ||
| 735 | 682 | const vaddr = try lf.getUavVAddr(uav_val, .{ |
| 736 | 683 | .parent = reloc_parent, |
| 737 | 684 | .offset = code.items.len, |
| ... | ... | @@ -744,8 +691,6 @@ fn lowerUavRef( |
| 744 | 691 | 8 => mem.writeInt(u64, try code.addManyAsArray(8), vaddr, endian), |
| 745 | 692 | else => unreachable, |
| 746 | 693 | } |
| 747 | ||
| 748 | return Result.ok; | |
| 749 | 694 | } |
| 750 | 695 | |
| 751 | 696 | fn lowerNavRef( |
| ... | ... | @@ -756,7 +701,7 @@ fn lowerNavRef( |
| 756 | 701 | code: *std.ArrayList(u8), |
| 757 | 702 | reloc_parent: link.File.RelocInfo.Parent, |
| 758 | 703 | offset: u64, |
| 759 | ) CodeGenError!Result { | |
| 704 | ) GenerateSymbolError!void { | |
| 760 | 705 | _ = src_loc; |
| 761 | 706 | const zcu = pt.zcu; |
| 762 | 707 | const gpa = zcu.gpa; |
| ... | ... | @@ -768,7 +713,7 @@ fn lowerNavRef( |
| 768 | 713 | const is_fn_body = nav_ty.zigTypeTag(zcu) == .@"fn"; |
| 769 | 714 | if (!is_fn_body and !nav_ty.hasRuntimeBits(zcu)) { |
| 770 | 715 | try code.appendNTimes(0xaa, ptr_width_bytes); |
| 771 | return Result.ok; | |
| 716 | return; | |
| 772 | 717 | } |
| 773 | 718 | |
| 774 | 719 | switch (lf.tag) { |
| ... | ... | @@ -786,16 +731,16 @@ fn lowerNavRef( |
| 786 | 731 | .pointee = .{ .nav_index = nav_index }, |
| 787 | 732 | }); |
| 788 | 733 | try code.appendNTimes(0, ptr_width_bytes); |
| 789 | return .ok; | |
| 734 | return; | |
| 790 | 735 | }, |
| 791 | 736 | else => {}, |
| 792 | 737 | } |
| 793 | 738 | |
| 794 | const vaddr = try lf.getNavVAddr(pt, nav_index, .{ | |
| 739 | const vaddr = lf.getNavVAddr(pt, nav_index, .{ | |
| 795 | 740 | .parent = reloc_parent, |
| 796 | 741 | .offset = code.items.len, |
| 797 | 742 | .addend = @intCast(offset), |
| 798 | }); | |
| 743 | }) catch @panic("TODO rework getNavVAddr"); | |
| 799 | 744 | const endian = target.cpu.arch.endian(); |
| 800 | 745 | switch (ptr_width_bytes) { |
| 801 | 746 | 2 => mem.writeInt(u16, try code.addManyAsArray(2), @intCast(vaddr), endian), |
| ... | ... | @@ -803,8 +748,6 @@ fn lowerNavRef( |
| 803 | 748 | 8 => mem.writeInt(u64, try code.addManyAsArray(8), vaddr, endian), |
| 804 | 749 | else => unreachable, |
| 805 | 750 | } |
| 806 | ||
| 807 | return .ok; | |
| 808 | 751 | } |
| 809 | 752 | |
| 810 | 753 | /// Helper struct to denote that the value is in memory but requires a linker relocation fixup: |
src/link.zig+7-1| ... | ... | @@ -673,7 +673,13 @@ pub const File = struct { |
| 673 | 673 | } |
| 674 | 674 | } |
| 675 | 675 | |
| 676 | pub fn updateContainerType(base: *File, pt: Zcu.PerThread, ty: InternPool.Index) UpdateNavError!void { | |
| 676 | pub const UpdateContainerTypeError = error{ | |
| 677 | OutOfMemory, | |
| 678 | /// `Zcu.failed_types` is already populated with the error message. | |
| 679 | TypeFailureReported, | |
| 680 | }; | |
| 681 | ||
| 682 | pub fn updateContainerType(base: *File, pt: Zcu.PerThread, ty: InternPool.Index) UpdateContainerTypeError!void { | |
| 677 | 683 | switch (base.tag) { |
| 678 | 684 | else => {}, |
| 679 | 685 | inline .elf => |tag| { |
src/link/Coff.zig+57-48| ... | ... | @@ -754,7 +754,7 @@ fn allocateGlobal(coff: *Coff) !u32 { |
| 754 | 754 | return index; |
| 755 | 755 | } |
| 756 | 756 | |
| 757 | fn addGotEntry(coff: *Coff, target: SymbolWithLoc) error{ OutOfMemory, LinkFailure }!void { | |
| 757 | fn addGotEntry(coff: *Coff, target: SymbolWithLoc) !void { | |
| 758 | 758 | const gpa = coff.base.comp.gpa; |
| 759 | 759 | if (coff.got_table.lookup.contains(target)) return; |
| 760 | 760 | const got_index = try coff.got_table.allocateEntry(gpa, target); |
| ... | ... | @@ -780,7 +780,7 @@ pub fn createAtom(coff: *Coff) !Atom.Index { |
| 780 | 780 | return atom_index; |
| 781 | 781 | } |
| 782 | 782 | |
| 783 | fn growAtom(coff: *Coff, atom_index: Atom.Index, new_atom_size: u32, alignment: u32) link.File.UpdateNavError!u32 { | |
| 783 | fn growAtom(coff: *Coff, atom_index: Atom.Index, new_atom_size: u32, alignment: u32) !u32 { | |
| 784 | 784 | const atom = coff.getAtom(atom_index); |
| 785 | 785 | const sym = atom.getSymbol(coff); |
| 786 | 786 | const align_ok = mem.alignBackward(u32, sym.value, alignment) == sym.value; |
| ... | ... | @@ -909,12 +909,12 @@ fn writeOffsetTableEntry(coff: *Coff, index: usize) !void { |
| 909 | 909 | .p32 => { |
| 910 | 910 | var buf: [4]u8 = undefined; |
| 911 | 911 | mem.writeInt(u32, &buf, @intCast(entry_value + coff.image_base), .little); |
| 912 | try coff.pwriteAll(&buf, file_offset); | |
| 912 | try coff.base.file.?.pwriteAll(&buf, file_offset); | |
| 913 | 913 | }, |
| 914 | 914 | .p64 => { |
| 915 | 915 | var buf: [8]u8 = undefined; |
| 916 | 916 | mem.writeInt(u64, &buf, entry_value + coff.image_base, .little); |
| 917 | try coff.pwriteAll(&buf, file_offset); | |
| 917 | try coff.base.file.?.pwriteAll(&buf, file_offset); | |
| 918 | 918 | }, |
| 919 | 919 | } |
| 920 | 920 | |
| ... | ... | @@ -1122,7 +1122,7 @@ pub fn updateFunc( |
| 1122 | 1122 | var code_buffer = std.ArrayList(u8).init(gpa); |
| 1123 | 1123 | defer code_buffer.deinit(); |
| 1124 | 1124 | |
| 1125 | const res = codegen.generateFunction( | |
| 1125 | codegen.generateFunction( | |
| 1126 | 1126 | &coff.base, |
| 1127 | 1127 | pt, |
| 1128 | 1128 | zcu.navSrcLoc(nav_index), |
| ... | ... | @@ -1134,7 +1134,7 @@ pub fn updateFunc( |
| 1134 | 1134 | ) catch |err| switch (err) { |
| 1135 | 1135 | error.CodegenFail => return error.CodegenFail, |
| 1136 | 1136 | error.OutOfMemory => return error.OutOfMemory, |
| 1137 | else => |e| { | |
| 1137 | error.Overflow => |e| { | |
| 1138 | 1138 | try zcu.failed_codegen.putNoClobber(gpa, nav_index, try Zcu.ErrorMsg.create( |
| 1139 | 1139 | gpa, |
| 1140 | 1140 | zcu.navSrcLoc(nav_index), |
| ... | ... | @@ -1145,15 +1145,8 @@ pub fn updateFunc( |
| 1145 | 1145 | return error.CodegenFail; |
| 1146 | 1146 | }, |
| 1147 | 1147 | }; |
| 1148 | const code = switch (res) { | |
| 1149 | .ok => code_buffer.items, | |
| 1150 | .fail => |em| { | |
| 1151 | try zcu.failed_codegen.put(zcu.gpa, nav_index, em); | |
| 1152 | return; | |
| 1153 | }, | |
| 1154 | }; | |
| 1155 | 1148 | |
| 1156 | try coff.updateNavCode(pt, nav_index, code, .FUNCTION); | |
| 1149 | try coff.updateNavCode(pt, nav_index, code_buffer.items, .FUNCTION); | |
| 1157 | 1150 | |
| 1158 | 1151 | // Exports will be updated by `Zcu.processExports` after the update. |
| 1159 | 1152 | } |
| ... | ... | @@ -1182,16 +1175,13 @@ fn lowerConst( |
| 1182 | 1175 | try coff.setSymbolName(sym, name); |
| 1183 | 1176 | sym.section_number = @as(coff_util.SectionNumber, @enumFromInt(sect_id + 1)); |
| 1184 | 1177 | |
| 1185 | const res = try codegen.generateSymbol(&coff.base, pt, src_loc, val, &code_buffer, .{ | |
| 1178 | try codegen.generateSymbol(&coff.base, pt, src_loc, val, &code_buffer, .{ | |
| 1186 | 1179 | .atom_index = coff.getAtom(atom_index).getSymbolIndex().?, |
| 1187 | 1180 | }); |
| 1188 | const code = switch (res) { | |
| 1189 | .ok => code_buffer.items, | |
| 1190 | .fail => |em| return .{ .fail = em }, | |
| 1191 | }; | |
| 1181 | const code = code_buffer.items; | |
| 1192 | 1182 | |
| 1193 | 1183 | const atom = coff.getAtomPtr(atom_index); |
| 1194 | atom.size = @as(u32, @intCast(code.len)); | |
| 1184 | atom.size = @intCast(code.len); | |
| 1195 | 1185 | atom.getSymbolPtr(coff).value = try coff.allocateAtom( |
| 1196 | 1186 | atom_index, |
| 1197 | 1187 | atom.size, |
| ... | ... | @@ -1250,7 +1240,7 @@ pub fn updateNav( |
| 1250 | 1240 | var code_buffer = std.ArrayList(u8).init(gpa); |
| 1251 | 1241 | defer code_buffer.deinit(); |
| 1252 | 1242 | |
| 1253 | const res = try codegen.generateSymbol( | |
| 1243 | try codegen.generateSymbol( | |
| 1254 | 1244 | &coff.base, |
| 1255 | 1245 | pt, |
| 1256 | 1246 | zcu.navSrcLoc(nav_index), |
| ... | ... | @@ -1258,15 +1248,8 @@ pub fn updateNav( |
| 1258 | 1248 | &code_buffer, |
| 1259 | 1249 | .{ .atom_index = atom.getSymbolIndex().? }, |
| 1260 | 1250 | ); |
| 1261 | const code = switch (res) { | |
| 1262 | .ok => code_buffer.items, | |
| 1263 | .fail => |em| { | |
| 1264 | try zcu.failed_codegen.put(gpa, nav_index, em); | |
| 1265 | return; | |
| 1266 | }, | |
| 1267 | }; | |
| 1268 | 1251 | |
| 1269 | try coff.updateNavCode(pt, nav_index, code, .NULL); | |
| 1252 | try coff.updateNavCode(pt, nav_index, code_buffer.items, .NULL); | |
| 1270 | 1253 | } |
| 1271 | 1254 | |
| 1272 | 1255 | // Exports will be updated by `Zcu.processExports` after the update. |
| ... | ... | @@ -1278,11 +1261,10 @@ fn updateLazySymbolAtom( |
| 1278 | 1261 | sym: link.File.LazySymbol, |
| 1279 | 1262 | atom_index: Atom.Index, |
| 1280 | 1263 | section_index: u16, |
| 1281 | ) link.File.FlushError!void { | |
| 1264 | ) !void { | |
| 1282 | 1265 | const zcu = pt.zcu; |
| 1283 | 1266 | const comp = coff.base.comp; |
| 1284 | 1267 | const gpa = comp.gpa; |
| 1285 | const diags = &comp.link_diags; | |
| 1286 | 1268 | |
| 1287 | 1269 | var required_alignment: InternPool.Alignment = .none; |
| 1288 | 1270 | var code_buffer = std.ArrayList(u8).init(gpa); |
| ... | ... | @@ -1298,7 +1280,7 @@ fn updateLazySymbolAtom( |
| 1298 | 1280 | const local_sym_index = atom.getSymbolIndex().?; |
| 1299 | 1281 | |
| 1300 | 1282 | const src = Type.fromInterned(sym.ty).srcLocOrNull(zcu) orelse Zcu.LazySrcLoc.unneeded; |
| 1301 | const res = codegen.generateLazySymbol( | |
| 1283 | try codegen.generateLazySymbol( | |
| 1302 | 1284 | &coff.base, |
| 1303 | 1285 | pt, |
| 1304 | 1286 | src, |
| ... | ... | @@ -1307,14 +1289,8 @@ fn updateLazySymbolAtom( |
| 1307 | 1289 | &code_buffer, |
| 1308 | 1290 | .none, |
| 1309 | 1291 | .{ .atom_index = local_sym_index }, |
| 1310 | ) catch |err| switch (err) { | |
| 1311 | error.CodegenFail => return error.LinkFailure, | |
| 1312 | else => |e| return diags.fail("failed to generate lazy symbol: {s}", .{@errorName(e)}), | |
| 1313 | }; | |
| 1314 | const code = switch (res) { | |
| 1315 | .ok => code_buffer.items, | |
| 1316 | .fail => |em| return diags.fail("failed to generate code: {s}", .{em.msg}), | |
| 1317 | }; | |
| 1292 | ); | |
| 1293 | const code = code_buffer.items; | |
| 1318 | 1294 | |
| 1319 | 1295 | const code_len: u32 = @intCast(code.len); |
| 1320 | 1296 | const symbol = atom.getSymbolPtr(coff); |
| ... | ... | @@ -1438,7 +1414,10 @@ fn updateNavCode( |
| 1438 | 1414 | const capacity = atom.capacity(coff); |
| 1439 | 1415 | const need_realloc = code.len > capacity or !required_alignment.check(sym.value); |
| 1440 | 1416 | if (need_realloc) { |
| 1441 | const vaddr = try coff.growAtom(atom_index, code_len, @intCast(required_alignment.toByteUnits() orelse 0)); | |
| 1417 | const vaddr = coff.growAtom(atom_index, code_len, @intCast(required_alignment.toByteUnits() orelse 0)) catch |err| switch (err) { | |
| 1418 | error.OutOfMemory => return error.OutOfMemory, | |
| 1419 | else => |e| return coff.base.cgFail(nav_index, "failed to grow atom: {s}", .{@errorName(e)}), | |
| 1420 | }; | |
| 1442 | 1421 | log.debug("growing {} from 0x{x} to 0x{x}", .{ nav.fqn.fmt(ip), sym.value, vaddr }); |
| 1443 | 1422 | log.debug(" (required alignment 0x{x}", .{required_alignment}); |
| 1444 | 1423 | |
| ... | ... | @@ -1446,7 +1425,10 @@ fn updateNavCode( |
| 1446 | 1425 | sym.value = vaddr; |
| 1447 | 1426 | log.debug(" (updating GOT entry)", .{}); |
| 1448 | 1427 | const got_entry_index = coff.got_table.lookup.get(.{ .sym_index = sym_index }).?; |
| 1449 | try coff.writeOffsetTableEntry(got_entry_index); | |
| 1428 | coff.writeOffsetTableEntry(got_entry_index) catch |err| switch (err) { | |
| 1429 | error.OutOfMemory => return error.OutOfMemory, | |
| 1430 | else => |e| return coff.base.cgFail(nav_index, "failed to write offset table entry: {s}", .{@errorName(e)}), | |
| 1431 | }; | |
| 1450 | 1432 | coff.markRelocsDirtyByTarget(.{ .sym_index = sym_index }); |
| 1451 | 1433 | } |
| 1452 | 1434 | } else if (code_len < atom.size) { |
| ... | ... | @@ -1459,16 +1441,25 @@ fn updateNavCode( |
| 1459 | 1441 | sym.section_number = @enumFromInt(sect_index + 1); |
| 1460 | 1442 | sym.type = .{ .complex_type = complex_type, .base_type = .NULL }; |
| 1461 | 1443 | |
| 1462 | const vaddr = try coff.allocateAtom(atom_index, code_len, @intCast(required_alignment.toByteUnits() orelse 0)); | |
| 1444 | const vaddr = coff.allocateAtom(atom_index, code_len, @intCast(required_alignment.toByteUnits() orelse 0)) catch |err| switch (err) { | |
| 1445 | error.OutOfMemory => return error.OutOfMemory, | |
| 1446 | else => |e| return coff.base.cgFail(nav_index, "failed to allocate atom: {s}", .{@errorName(e)}), | |
| 1447 | }; | |
| 1463 | 1448 | errdefer coff.freeAtom(atom_index); |
| 1464 | 1449 | log.debug("allocated atom for {} at 0x{x}", .{ nav.fqn.fmt(ip), vaddr }); |
| 1465 | 1450 | coff.getAtomPtr(atom_index).size = code_len; |
| 1466 | 1451 | sym.value = vaddr; |
| 1467 | 1452 | |
| 1468 | try coff.addGotEntry(.{ .sym_index = sym_index }); | |
| 1453 | coff.addGotEntry(.{ .sym_index = sym_index }) catch |err| switch (err) { | |
| 1454 | error.OutOfMemory => return error.OutOfMemory, | |
| 1455 | else => |e| return coff.base.cgFail(nav_index, "failed to add GOT entry: {s}", .{@errorName(e)}), | |
| 1456 | }; | |
| 1469 | 1457 | } |
| 1470 | 1458 | |
| 1471 | try coff.writeAtom(atom_index, code); | |
| 1459 | coff.writeAtom(atom_index, code) catch |err| switch (err) { | |
| 1460 | error.OutOfMemory => return error.OutOfMemory, | |
| 1461 | else => |e| return coff.base.cgFail(nav_index, "failed to write atom: {s}", .{@errorName(e)}), | |
| 1462 | }; | |
| 1472 | 1463 | } |
| 1473 | 1464 | |
| 1474 | 1465 | pub fn freeNav(coff: *Coff, nav_index: InternPool.NavIndex) void { |
| ... | ... | @@ -2229,12 +2220,16 @@ fn findLib(arena: Allocator, name: []const u8, lib_directories: []const Director |
| 2229 | 2220 | return null; |
| 2230 | 2221 | } |
| 2231 | 2222 | |
| 2232 | pub fn flushModule(coff: *Coff, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) link.File.FlushError!void { | |
| 2223 | pub fn flushModule( | |
| 2224 | coff: *Coff, | |
| 2225 | arena: Allocator, | |
| 2226 | tid: Zcu.PerThread.Id, | |
| 2227 | prog_node: std.Progress.Node, | |
| 2228 | ) link.File.FlushError!void { | |
| 2233 | 2229 | const tracy = trace(@src()); |
| 2234 | 2230 | defer tracy.end(); |
| 2235 | 2231 | |
| 2236 | 2232 | const comp = coff.base.comp; |
| 2237 | const gpa = comp.gpa; | |
| 2238 | 2233 | const diags = &comp.link_diags; |
| 2239 | 2234 | |
| 2240 | 2235 | if (coff.llvm_object) |llvm_object| { |
| ... | ... | @@ -2245,6 +2240,20 @@ pub fn flushModule(coff: *Coff, arena: Allocator, tid: Zcu.PerThread.Id, prog_no |
| 2245 | 2240 | const sub_prog_node = prog_node.start("COFF Flush", 0); |
| 2246 | 2241 | defer sub_prog_node.end(); |
| 2247 | 2242 | |
| 2243 | return flushModuleInner(coff, arena, tid) catch |err| switch (err) { | |
| 2244 | error.OutOfMemory => return error.OutOfMemory, | |
| 2245 | error.LinkFailure => return error.LinkFailure, | |
| 2246 | else => |e| return diags.fail("COFF flush failed: {s}", .{@errorName(e)}), | |
| 2247 | }; | |
| 2248 | } | |
| 2249 | ||
| 2250 | fn flushModuleInner(coff: *Coff, arena: Allocator, tid: Zcu.PerThread.Id) !void { | |
| 2251 | _ = arena; | |
| 2252 | ||
| 2253 | const comp = coff.base.comp; | |
| 2254 | const gpa = comp.gpa; | |
| 2255 | const diags = &comp.link_diags; | |
| 2256 | ||
| 2248 | 2257 | const pt: Zcu.PerThread = .activate( |
| 2249 | 2258 | comp.zcu orelse return diags.fail("linking without zig source is not yet implemented", .{}), |
| 2250 | 2259 | tid, |
| ... | ... | @@ -2757,7 +2766,7 @@ fn writeImportTables(coff: *Coff) !void { |
| 2757 | 2766 | coff.imports_count_dirty = false; |
| 2758 | 2767 | } |
| 2759 | 2768 | |
| 2760 | fn writeStrtab(coff: *Coff) link.File.FlushError!void { | |
| 2769 | fn writeStrtab(coff: *Coff) !void { | |
| 2761 | 2770 | if (coff.strtab_offset == null) return; |
| 2762 | 2771 | |
| 2763 | 2772 | const comp = coff.base.comp; |
src/link/Dwarf.zig+3-9| ... | ... | @@ -21,7 +21,6 @@ debug_rnglists: DebugRngLists, |
| 21 | 21 | debug_str: StringSection, |
| 22 | 22 | |
| 23 | 23 | pub const UpdateError = error{ |
| 24 | CodegenFail, | |
| 25 | 24 | ReinterpretDeclRef, |
| 26 | 25 | Unimplemented, |
| 27 | 26 | OutOfMemory, |
| ... | ... | @@ -1893,17 +1892,15 @@ pub const WipNav = struct { |
| 1893 | 1892 | if (bytes == 0) return; |
| 1894 | 1893 | var dim = wip_nav.debug_info.toManaged(wip_nav.dwarf.gpa); |
| 1895 | 1894 | defer wip_nav.debug_info = dim.moveToUnmanaged(); |
| 1896 | switch (try codegen.generateSymbol( | |
| 1895 | try codegen.generateSymbol( | |
| 1897 | 1896 | wip_nav.dwarf.bin_file, |
| 1898 | 1897 | wip_nav.pt, |
| 1899 | 1898 | src_loc, |
| 1900 | 1899 | val, |
| 1901 | 1900 | &dim, |
| 1902 | 1901 | .{ .debug_output = .{ .dwarf = wip_nav } }, |
| 1903 | )) { | |
| 1904 | .ok => assert(dim.items.len == wip_nav.debug_info.items.len + bytes), | |
| 1905 | .fail => unreachable, | |
| 1906 | } | |
| 1902 | ); | |
| 1903 | assert(dim.items.len == wip_nav.debug_info.items.len + bytes); | |
| 1907 | 1904 | } |
| 1908 | 1905 | |
| 1909 | 1906 | const AbbrevCodeForForm = struct { |
| ... | ... | @@ -2346,7 +2343,6 @@ pub fn initWipNav( |
| 2346 | 2343 | ) error{ OutOfMemory, CodegenFail }!?WipNav { |
| 2347 | 2344 | return initWipNavInner(dwarf, pt, nav_index, sym_index) catch |err| switch (err) { |
| 2348 | 2345 | error.OutOfMemory => return error.OutOfMemory, |
| 2349 | error.CodegenFail => return error.CodegenFail, | |
| 2350 | 2346 | else => |e| return pt.zcu.codegenFail(nav_index, "failed to init dwarf: {s}", .{@errorName(e)}), |
| 2351 | 2347 | }; |
| 2352 | 2348 | } |
| ... | ... | @@ -2669,7 +2665,6 @@ pub fn finishWipNav( |
| 2669 | 2665 | ) error{ OutOfMemory, CodegenFail }!void { |
| 2670 | 2666 | return finishWipNavInner(dwarf, pt, nav_index, wip_nav) catch |err| switch (err) { |
| 2671 | 2667 | error.OutOfMemory => return error.OutOfMemory, |
| 2672 | error.CodegenFail => return error.CodegenFail, | |
| 2673 | 2668 | else => |e| return pt.zcu.codegenFail(nav_index, "failed to finish dwarf: {s}", .{@errorName(e)}), |
| 2674 | 2669 | }; |
| 2675 | 2670 | } |
| ... | ... | @@ -2701,7 +2696,6 @@ fn finishWipNavInner( |
| 2701 | 2696 | pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool.Nav.Index) error{ OutOfMemory, CodegenFail }!void { |
| 2702 | 2697 | return updateComptimeNavInner(dwarf, pt, nav_index) catch |err| switch (err) { |
| 2703 | 2698 | error.OutOfMemory => return error.OutOfMemory, |
| 2704 | error.CodegenFail => return error.CodegenFail, | |
| 2705 | 2699 | else => |e| return pt.zcu.codegenFail(nav_index, "failed to update dwarf: {s}", .{@errorName(e)}), |
| 2706 | 2700 | }; |
| 2707 | 2701 | } |
src/link/Elf.zig+15-2| ... | ... | @@ -2368,12 +2368,25 @@ pub fn updateContainerType( |
| 2368 | 2368 | self: *Elf, |
| 2369 | 2369 | pt: Zcu.PerThread, |
| 2370 | 2370 | ty: InternPool.Index, |
| 2371 | ) link.File.UpdateNavError!void { | |
| 2371 | ) link.File.UpdateContainerTypeError!void { | |
| 2372 | 2372 | if (build_options.skip_non_native and builtin.object_format != .elf) { |
| 2373 | 2373 | @panic("Attempted to compile for object format that was disabled by build configuration"); |
| 2374 | 2374 | } |
| 2375 | 2375 | if (self.llvm_object) |_| return; |
| 2376 | return self.zigObjectPtr().?.updateContainerType(pt, ty); | |
| 2376 | const zcu = pt.zcu; | |
| 2377 | const gpa = zcu.gpa; | |
| 2378 | return self.zigObjectPtr().?.updateContainerType(pt, ty) catch |err| switch (err) { | |
| 2379 | error.OutOfMemory => return error.OutOfMemory, | |
| 2380 | else => |e| { | |
| 2381 | try zcu.failed_types.putNoClobber(gpa, ty, try Zcu.ErrorMsg.create( | |
| 2382 | gpa, | |
| 2383 | zcu.typeSrcLoc(ty), | |
| 2384 | "failed to update container type: {s}", | |
| 2385 | .{@errorName(e)}, | |
| 2386 | )); | |
| 2387 | return error.TypeFailureReported; | |
| 2388 | }, | |
| 2389 | }; | |
| 2377 | 2390 | } |
| 2378 | 2391 | |
| 2379 | 2392 | pub fn updateExports( |
src/link/Elf/ZigObject.zig+9-32| ... | ... | @@ -1437,7 +1437,7 @@ pub fn updateFunc( |
| 1437 | 1437 | var debug_wip_nav = if (self.dwarf) |*dwarf| try dwarf.initWipNav(pt, func.owner_nav, sym_index) else null; |
| 1438 | 1438 | defer if (debug_wip_nav) |*wip_nav| wip_nav.deinit(); |
| 1439 | 1439 | |
| 1440 | const res = try codegen.generateFunction( | |
| 1440 | try codegen.generateFunction( | |
| 1441 | 1441 | &elf_file.base, |
| 1442 | 1442 | pt, |
| 1443 | 1443 | zcu.navSrcLoc(func.owner_nav), |
| ... | ... | @@ -1447,14 +1447,7 @@ pub fn updateFunc( |
| 1447 | 1447 | &code_buffer, |
| 1448 | 1448 | if (debug_wip_nav) |*dn| .{ .dwarf = dn } else .none, |
| 1449 | 1449 | ); |
| 1450 | ||
| 1451 | const code = switch (res) { | |
| 1452 | .ok => code_buffer.items, | |
| 1453 | .fail => |em| { | |
| 1454 | try zcu.failed_codegen.put(gpa, func.owner_nav, em); | |
| 1455 | return; | |
| 1456 | }, | |
| 1457 | }; | |
| 1450 | const code = code_buffer.items; | |
| 1458 | 1451 | |
| 1459 | 1452 | const shndx = try self.getNavShdrIndex(elf_file, zcu, func.owner_nav, sym_index, code); |
| 1460 | 1453 | log.debug("setting shdr({x},{s}) for {}", .{ |
| ... | ... | @@ -1574,7 +1567,7 @@ pub fn updateNav( |
| 1574 | 1567 | var debug_wip_nav = if (self.dwarf) |*dwarf| try dwarf.initWipNav(pt, nav_index, sym_index) else null; |
| 1575 | 1568 | defer if (debug_wip_nav) |*wip_nav| wip_nav.deinit(); |
| 1576 | 1569 | |
| 1577 | const res = try codegen.generateSymbol( | |
| 1570 | try codegen.generateSymbol( | |
| 1578 | 1571 | &elf_file.base, |
| 1579 | 1572 | pt, |
| 1580 | 1573 | zcu.navSrcLoc(nav_index), |
| ... | ... | @@ -1582,14 +1575,7 @@ pub fn updateNav( |
| 1582 | 1575 | &code_buffer, |
| 1583 | 1576 | .{ .atom_index = sym_index }, |
| 1584 | 1577 | ); |
| 1585 | ||
| 1586 | const code = switch (res) { | |
| 1587 | .ok => code_buffer.items, | |
| 1588 | .fail => |em| { | |
| 1589 | try zcu.failed_codegen.put(zcu.gpa, nav_index, em); | |
| 1590 | return; | |
| 1591 | }, | |
| 1592 | }; | |
| 1578 | const code = code_buffer.items; | |
| 1593 | 1579 | |
| 1594 | 1580 | const shndx = try self.getNavShdrIndex(elf_file, zcu, nav_index, sym_index, code); |
| 1595 | 1581 | log.debug("setting shdr({x},{s}) for {}", .{ |
| ... | ... | @@ -1612,7 +1598,7 @@ pub fn updateContainerType( |
| 1612 | 1598 | self: *ZigObject, |
| 1613 | 1599 | pt: Zcu.PerThread, |
| 1614 | 1600 | ty: InternPool.Index, |
| 1615 | ) link.File.UpdateNavError!void { | |
| 1601 | ) !void { | |
| 1616 | 1602 | const tracy = trace(@src()); |
| 1617 | 1603 | defer tracy.end(); |
| 1618 | 1604 | |
| ... | ... | @@ -1643,7 +1629,7 @@ fn updateLazySymbol( |
| 1643 | 1629 | }; |
| 1644 | 1630 | |
| 1645 | 1631 | const src = Type.fromInterned(sym.ty).srcLocOrNull(zcu) orelse Zcu.LazySrcLoc.unneeded; |
| 1646 | const res = try codegen.generateLazySymbol( | |
| 1632 | try codegen.generateLazySymbol( | |
| 1647 | 1633 | &elf_file.base, |
| 1648 | 1634 | pt, |
| 1649 | 1635 | src, |
| ... | ... | @@ -1653,13 +1639,7 @@ fn updateLazySymbol( |
| 1653 | 1639 | .none, |
| 1654 | 1640 | .{ .atom_index = symbol_index }, |
| 1655 | 1641 | ); |
| 1656 | const code = switch (res) { | |
| 1657 | .ok => code_buffer.items, | |
| 1658 | .fail => |em| { | |
| 1659 | log.err("{s}", .{em.msg}); | |
| 1660 | return error.CodegenFail; | |
| 1661 | }, | |
| 1662 | }; | |
| 1642 | const code = code_buffer.items; | |
| 1663 | 1643 | |
| 1664 | 1644 | const output_section_index = switch (sym.kind) { |
| 1665 | 1645 | .code => if (self.text_index) |sym_index| |
| ... | ... | @@ -1732,7 +1712,7 @@ fn lowerConst( |
| 1732 | 1712 | const name_off = try self.addString(gpa, name); |
| 1733 | 1713 | const sym_index = try self.newSymbolWithAtom(gpa, name_off); |
| 1734 | 1714 | |
| 1735 | const res = try codegen.generateSymbol( | |
| 1715 | try codegen.generateSymbol( | |
| 1736 | 1716 | &elf_file.base, |
| 1737 | 1717 | pt, |
| 1738 | 1718 | src_loc, |
| ... | ... | @@ -1740,10 +1720,7 @@ fn lowerConst( |
| 1740 | 1720 | &code_buffer, |
| 1741 | 1721 | .{ .atom_index = sym_index }, |
| 1742 | 1722 | ); |
| 1743 | const code = switch (res) { | |
| 1744 | .ok => code_buffer.items, | |
| 1745 | .fail => |em| return .{ .fail = em }, | |
| 1746 | }; | |
| 1723 | const code = code_buffer.items; | |
| 1747 | 1724 | |
| 1748 | 1725 | const local_sym = self.symbol(sym_index); |
| 1749 | 1726 | const local_esym = &self.symtab.items(.elf_sym)[local_sym.esym_index]; |
src/link/MachO/ZigObject.zig+9-34| ... | ... | @@ -590,7 +590,6 @@ pub fn flushModule(self: *ZigObject, macho_file: *MachO, tid: Zcu.PerThread.Id) |
| 590 | 590 | defer pt.deactivate(); |
| 591 | 591 | dwarf.flushModule(pt) catch |err| switch (err) { |
| 592 | 592 | error.OutOfMemory => return error.OutOfMemory, |
| 593 | error.CodegenFail => return error.LinkFailure, | |
| 594 | 593 | else => |e| return diags.fail("failed to flush dwarf module: {s}", .{@errorName(e)}), |
| 595 | 594 | }; |
| 596 | 595 | |
| ... | ... | @@ -796,7 +795,7 @@ pub fn updateFunc( |
| 796 | 795 | var debug_wip_nav = if (self.dwarf) |*dwarf| try dwarf.initWipNav(pt, func.owner_nav, sym_index) else null; |
| 797 | 796 | defer if (debug_wip_nav) |*wip_nav| wip_nav.deinit(); |
| 798 | 797 | |
| 799 | const res = try codegen.generateFunction( | |
| 798 | try codegen.generateFunction( | |
| 800 | 799 | &macho_file.base, |
| 801 | 800 | pt, |
| 802 | 801 | zcu.navSrcLoc(func.owner_nav), |
| ... | ... | @@ -806,14 +805,7 @@ pub fn updateFunc( |
| 806 | 805 | &code_buffer, |
| 807 | 806 | if (debug_wip_nav) |*wip_nav| .{ .dwarf = wip_nav } else .none, |
| 808 | 807 | ); |
| 809 | ||
| 810 | const code = switch (res) { | |
| 811 | .ok => code_buffer.items, | |
| 812 | .fail => |em| { | |
| 813 | try zcu.failed_codegen.put(gpa, func.owner_nav, em); | |
| 814 | return error.CodegenFail; | |
| 815 | }, | |
| 816 | }; | |
| 808 | const code = code_buffer.items; | |
| 817 | 809 | |
| 818 | 810 | const sect_index = try self.getNavOutputSection(macho_file, zcu, func.owner_nav, code); |
| 819 | 811 | const old_rva, const old_alignment = blk: { |
| ... | ... | @@ -914,7 +906,7 @@ pub fn updateNav( |
| 914 | 906 | var debug_wip_nav = if (self.dwarf) |*dwarf| try dwarf.initWipNav(pt, nav_index, sym_index) else null; |
| 915 | 907 | defer if (debug_wip_nav) |*wip_nav| wip_nav.deinit(); |
| 916 | 908 | |
| 917 | const res = try codegen.generateSymbol( | |
| 909 | try codegen.generateSymbol( | |
| 918 | 910 | &macho_file.base, |
| 919 | 911 | pt, |
| 920 | 912 | zcu.navSrcLoc(nav_index), |
| ... | ... | @@ -922,14 +914,8 @@ pub fn updateNav( |
| 922 | 914 | &code_buffer, |
| 923 | 915 | .{ .atom_index = sym_index }, |
| 924 | 916 | ); |
| 917 | const code = code_buffer.items; | |
| 925 | 918 | |
| 926 | const code = switch (res) { | |
| 927 | .ok => code_buffer.items, | |
| 928 | .fail => |em| { | |
| 929 | try zcu.failed_codegen.put(zcu.gpa, nav_index, em); | |
| 930 | return; | |
| 931 | }, | |
| 932 | }; | |
| 933 | 919 | const sect_index = try self.getNavOutputSection(macho_file, zcu, nav_index, code); |
| 934 | 920 | if (isThreadlocal(macho_file, nav_index)) |
| 935 | 921 | try self.updateTlv(macho_file, pt, nav_index, sym_index, sect_index, code) |
| ... | ... | @@ -1221,7 +1207,7 @@ fn lowerConst( |
| 1221 | 1207 | const name_str = try self.addString(gpa, name); |
| 1222 | 1208 | const sym_index = try self.newSymbolWithAtom(gpa, name_str, macho_file); |
| 1223 | 1209 | |
| 1224 | const res = try codegen.generateSymbol( | |
| 1210 | try codegen.generateSymbol( | |
| 1225 | 1211 | &macho_file.base, |
| 1226 | 1212 | pt, |
| 1227 | 1213 | src_loc, |
| ... | ... | @@ -1229,10 +1215,7 @@ fn lowerConst( |
| 1229 | 1215 | &code_buffer, |
| 1230 | 1216 | .{ .atom_index = sym_index }, |
| 1231 | 1217 | ); |
| 1232 | const code = switch (res) { | |
| 1233 | .ok => code_buffer.items, | |
| 1234 | .fail => |em| return .{ .fail = em }, | |
| 1235 | }; | |
| 1218 | const code = code_buffer.items; | |
| 1236 | 1219 | |
| 1237 | 1220 | const sym = &self.symbols.items[sym_index]; |
| 1238 | 1221 | sym.out_n_sect = output_section_index; |
| ... | ... | @@ -1367,7 +1350,6 @@ fn updateLazySymbol( |
| 1367 | 1350 | ) !void { |
| 1368 | 1351 | const zcu = pt.zcu; |
| 1369 | 1352 | const gpa = zcu.gpa; |
| 1370 | const diags = &macho_file.base.comp.link_diags; | |
| 1371 | 1353 | |
| 1372 | 1354 | var required_alignment: Atom.Alignment = .none; |
| 1373 | 1355 | var code_buffer = std.ArrayList(u8).init(gpa); |
| ... | ... | @@ -1383,7 +1365,7 @@ fn updateLazySymbol( |
| 1383 | 1365 | }; |
| 1384 | 1366 | |
| 1385 | 1367 | const src = Type.fromInterned(lazy_sym.ty).srcLocOrNull(zcu) orelse Zcu.LazySrcLoc.unneeded; |
| 1386 | const res = codegen.generateLazySymbol( | |
| 1368 | try codegen.generateLazySymbol( | |
| 1387 | 1369 | &macho_file.base, |
| 1388 | 1370 | pt, |
| 1389 | 1371 | src, |
| ... | ... | @@ -1392,15 +1374,8 @@ fn updateLazySymbol( |
| 1392 | 1374 | &code_buffer, |
| 1393 | 1375 | .none, |
| 1394 | 1376 | .{ .atom_index = symbol_index }, |
| 1395 | ) catch |err| switch (err) { | |
| 1396 | error.CodegenFail => return error.LinkFailure, | |
| 1397 | error.OutOfMemory => return error.OutOfMemory, | |
| 1398 | else => |e| return diags.fail("failed to codegen symbol: {s}", .{@errorName(e)}), | |
| 1399 | }; | |
| 1400 | const code = switch (res) { | |
| 1401 | .ok => code_buffer.items, | |
| 1402 | .fail => |em| return diags.fail("codegen failure: {s}", .{em.msg}), | |
| 1403 | }; | |
| 1377 | ); | |
| 1378 | const code = code_buffer.items; | |
| 1404 | 1379 | |
| 1405 | 1380 | const output_section_index = switch (lazy_sym.kind) { |
| 1406 | 1381 | .code => macho_file.zig_text_sect_index.?, |
src/link/Plan9.zig+6-15| ... | ... | @@ -465,7 +465,7 @@ pub fn updateNav(self: *Plan9, pt: Zcu.PerThread, nav_index: InternPool.Nav.Inde |
| 465 | 465 | var code_buffer = std.ArrayList(u8).init(gpa); |
| 466 | 466 | defer code_buffer.deinit(); |
| 467 | 467 | // TODO we need the symbol index for symbol in the table of locals for the containing atom |
| 468 | const res = try codegen.generateSymbol( | |
| 468 | try codegen.generateSymbol( | |
| 469 | 469 | &self.base, |
| 470 | 470 | pt, |
| 471 | 471 | zcu.navSrcLoc(nav_index), |
| ... | ... | @@ -473,10 +473,7 @@ pub fn updateNav(self: *Plan9, pt: Zcu.PerThread, nav_index: InternPool.Nav.Inde |
| 473 | 473 | &code_buffer, |
| 474 | 474 | .{ .atom_index = @intCast(atom_idx) }, |
| 475 | 475 | ); |
| 476 | const code = switch (res) { | |
| 477 | .ok => code_buffer.items, | |
| 478 | .fail => |em| return zcu.failed_codegen.put(gpa, nav_index, em), | |
| 479 | }; | |
| 476 | const code = code_buffer.items; | |
| 480 | 477 | try self.data_nav_table.ensureUnusedCapacity(gpa, 1); |
| 481 | 478 | const duped_code = try gpa.dupe(u8, code); |
| 482 | 479 | self.getAtomPtr(self.navs.get(nav_index).?.index).code = .{ .code_ptr = null, .other = .{ .nav_index = nav_index } }; |
| ... | ... | @@ -1081,7 +1078,7 @@ fn updateLazySymbolAtom( |
| 1081 | 1078 | |
| 1082 | 1079 | // generate the code |
| 1083 | 1080 | const src = Type.fromInterned(sym.ty).srcLocOrNull(pt.zcu) orelse Zcu.LazySrcLoc.unneeded; |
| 1084 | const res = codegen.generateLazySymbol( | |
| 1081 | codegen.generateLazySymbol( | |
| 1085 | 1082 | &self.base, |
| 1086 | 1083 | pt, |
| 1087 | 1084 | src, |
| ... | ... | @@ -1095,10 +1092,7 @@ fn updateLazySymbolAtom( |
| 1095 | 1092 | error.CodegenFail => return error.LinkFailure, |
| 1096 | 1093 | error.Overflow => return diags.fail("codegen failure: encountered number too big for compiler", .{}), |
| 1097 | 1094 | }; |
| 1098 | const code = switch (res) { | |
| 1099 | .ok => code_buffer.items, | |
| 1100 | .fail => |em| return diags.fail("codegen failure: {s}", .{em.msg}), | |
| 1101 | }; | |
| 1095 | const code = code_buffer.items; | |
| 1102 | 1096 | // duped_code is freed when the atom is freed |
| 1103 | 1097 | const duped_code = try gpa.dupe(u8, code); |
| 1104 | 1098 | errdefer gpa.free(duped_code); |
| ... | ... | @@ -1408,11 +1402,8 @@ pub fn lowerUav( |
| 1408 | 1402 | gop.value_ptr.* = index; |
| 1409 | 1403 | // we need to free name latex |
| 1410 | 1404 | var code_buffer = std.ArrayList(u8).init(gpa); |
| 1411 | const res = try codegen.generateSymbol(&self.base, pt, src_loc, val, &code_buffer, .{ .atom_index = index }); | |
| 1412 | const code = switch (res) { | |
| 1413 | .ok => code_buffer.items, | |
| 1414 | .fail => |em| return .{ .fail = em }, | |
| 1415 | }; | |
| 1405 | try codegen.generateSymbol(&self.base, pt, src_loc, val, &code_buffer, .{ .atom_index = index }); | |
| 1406 | const code = code_buffer.items; | |
| 1416 | 1407 | const atom_ptr = self.getAtomPtr(index); |
| 1417 | 1408 | atom_ptr.* = .{ |
| 1418 | 1409 | .type = .d, |