authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-11-10 16:50:57+01:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-11-10 16:50:57+01:00
log4b3637820d0f43bc5b0e2c938b51ea1545b1c84e
treeacf3e29d06f825993057d64417461d799b76609e
parent2d5fbbb44e15b07531251ee406a0df73321e8175
parent0914e0a4ecabc1c9f4b4a8675955ab634065449e
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #13495 from ziglang/macho-dsym

stage2: misc DWARF debug info fixes and additions for x86_64 and aarch64

7 files changed, 412 insertions(+), 116 deletions(-)

src/arch/aarch64/CodeGen.zig+265-23
......@@ -51,13 +51,14 @@ gpa: Allocator,
5151air: Air,
5252liveness: Liveness,
5353bin_file: *link.File,
54debug_output: DebugInfoOutput,
5455target: *const std.Target,
5556mod_fn: *const Module.Fn,
5657err_msg: ?*ErrorMsg,
5758args: []MCValue,
5859ret_mcv: MCValue,
5960fn_type: Type,
60arg_index: usize,
61arg_index: u32,
6162src_loc: Module.SrcLoc,
6263stack_align: u32,
6364
......@@ -75,6 +76,12 @@ end_di_column: u32,
7576/// which is a relative jump, based on the address following the reloc.
7677exitlude_jump_relocs: std.ArrayListUnmanaged(usize) = .{},
7778
79/// We postpone the creation of debug info for function args and locals
80/// until after all Mir instructions have been generated. Only then we
81/// will know saved_regs_stack_space which is necessary in order to
82/// calculate the right stack offsest with respect to the `.fp` register.
83dbg_info_relocs: std.ArrayListUnmanaged(DbgInfoReloc) = .{},
84
7885/// Whenever there is a runtime branch, we push a Branch onto this stack,
7986/// and pop it off when the runtime branch joins. This provides an "overlay"
8087/// of the table of mappings from instructions to `MCValue` from within the branch.
......@@ -160,6 +167,220 @@ const MCValue = union(enum) {
160167 stack_argument_offset: u32,
161168};
162169
170const DbgInfoReloc = struct {
171 tag: Air.Inst.Tag,
172 ty: Type,
173 name: [:0]const u8,
174 mcv: MCValue,
175
176 fn genDbgInfo(reloc: DbgInfoReloc, function: Self) !void {
177 switch (reloc.tag) {
178 .arg => try reloc.genArgDbgInfo(function),
179
180 .dbg_var_ptr,
181 .dbg_var_val,
182 => try reloc.genVarDbgInfo(function),
183
184 else => unreachable,
185 }
186 }
187
188 fn genArgDbgInfo(reloc: DbgInfoReloc, function: Self) error{OutOfMemory}!void {
189 const name_with_null = reloc.name.ptr[0 .. reloc.name.len + 1];
190
191 switch (function.debug_output) {
192 .dwarf => |dw| {
193 const dbg_info = &dw.dbg_info;
194 switch (reloc.mcv) {
195 .register => |reg| {
196 try dbg_info.ensureUnusedCapacity(3);
197 dbg_info.appendAssumeCapacity(@enumToInt(link.File.Dwarf.AbbrevKind.parameter));
198 dbg_info.appendSliceAssumeCapacity(&[2]u8{ // DW.AT.location, DW.FORM.exprloc
199 1, // ULEB128 dwarf expression length
200 reg.dwarfLocOp(),
201 });
202 try dbg_info.ensureUnusedCapacity(5 + name_with_null.len);
203 try function.addDbgInfoTypeReloc(reloc.ty); // DW.AT.type, DW.FORM.ref4
204 dbg_info.appendSliceAssumeCapacity(name_with_null); // DW.AT.name, DW.FORM.string
205 },
206
207 .stack_offset,
208 .stack_argument_offset,
209 => |offset| {
210 const adjusted_offset = switch (reloc.mcv) {
211 .stack_offset => -@intCast(i32, offset),
212 .stack_argument_offset => @intCast(i32, function.saved_regs_stack_space + offset),
213 else => unreachable,
214 };
215
216 try dbg_info.ensureUnusedCapacity(8);
217 dbg_info.appendAssumeCapacity(@enumToInt(link.File.Dwarf.AbbrevKind.parameter));
218 const fixup = dbg_info.items.len;
219 dbg_info.appendSliceAssumeCapacity(&[2]u8{ // DW.AT.location, DW.FORM.exprloc
220 1, // we will backpatch it after we encode the displacement in LEB128
221 Register.x29.dwarfLocOpDeref(), // frame pointer
222 });
223 leb128.writeILEB128(dbg_info.writer(), adjusted_offset) catch unreachable;
224 dbg_info.items[fixup] += @intCast(u8, dbg_info.items.len - fixup - 2);
225 try dbg_info.ensureUnusedCapacity(5 + name_with_null.len);
226 try function.addDbgInfoTypeReloc(reloc.ty); // DW.AT.type, DW.FORM.ref4
227 dbg_info.appendSliceAssumeCapacity(name_with_null); // DW.AT.name, DW.FORM.string
228
229 },
230
231 else => unreachable, // not a possible argument
232 }
233 },
234 .plan9 => {},
235 .none => {},
236 }
237 }
238
239 fn genVarDbgInfo(reloc: DbgInfoReloc, function: Self) !void {
240 const name_with_null = reloc.name.ptr[0 .. reloc.name.len + 1];
241 const ty = switch (reloc.tag) {
242 .dbg_var_ptr => reloc.ty.childType(),
243 .dbg_var_val => reloc.ty,
244 else => unreachable,
245 };
246
247 switch (function.debug_output) {
248 .dwarf => |dw| {
249 const dbg_info = &dw.dbg_info;
250 try dbg_info.append(@enumToInt(link.File.Dwarf.AbbrevKind.variable));
251 const endian = function.target.cpu.arch.endian();
252
253 switch (reloc.mcv) {
254 .register => |reg| {
255 try dbg_info.ensureUnusedCapacity(2);
256 dbg_info.appendSliceAssumeCapacity(&[2]u8{ // DW.AT.location, DW.FORM.exprloc
257 1, // ULEB128 dwarf expression length
258 reg.dwarfLocOp(),
259 });
260 },
261
262 .ptr_stack_offset,
263 .stack_offset,
264 .stack_argument_offset,
265 => |offset| {
266 const adjusted_offset = switch (reloc.mcv) {
267 .ptr_stack_offset,
268 .stack_offset,
269 => -@intCast(i32, offset),
270 .stack_argument_offset => @intCast(i32, function.saved_regs_stack_space + offset),
271 else => unreachable,
272 };
273
274 try dbg_info.ensureUnusedCapacity(7);
275 const fixup = dbg_info.items.len;
276 dbg_info.appendSliceAssumeCapacity(&[2]u8{ // DW.AT.location, DW.FORM.exprloc
277 1, // we will backpatch it after we encode the displacement in LEB128
278 Register.x29.dwarfLocOpDeref(), // frame pointer
279 });
280 leb128.writeILEB128(dbg_info.writer(), adjusted_offset) catch unreachable;
281 dbg_info.items[fixup] += @intCast(u8, dbg_info.items.len - fixup - 2);
282 },
283
284 .memory,
285 .linker_load,
286 => {
287 const ptr_width = @intCast(u8, @divExact(function.target.cpu.arch.ptrBitWidth(), 8));
288 const is_ptr = switch (reloc.tag) {
289 .dbg_var_ptr => true,
290 .dbg_var_val => false,
291 else => unreachable,
292 };
293 try dbg_info.ensureUnusedCapacity(2 + ptr_width);
294 dbg_info.appendSliceAssumeCapacity(&[2]u8{ // DW.AT.location, DW.FORM.exprloc
295 1 + ptr_width + @boolToInt(is_ptr),
296 DW.OP.addr, // literal address
297 });
298 const offset = @intCast(u32, dbg_info.items.len);
299 const addr = switch (reloc.mcv) {
300 .memory => |addr| addr,
301 else => 0,
302 };
303 switch (ptr_width) {
304 0...4 => {
305 try dbg_info.writer().writeInt(u32, @intCast(u32, addr), endian);
306 },
307 5...8 => {
308 try dbg_info.writer().writeInt(u64, addr, endian);
309 },
310 else => unreachable,
311 }
312 if (is_ptr) {
313 // We need deref the address as we point to the value via GOT entry.
314 try dbg_info.append(DW.OP.deref);
315 }
316 switch (reloc.mcv) {
317 .linker_load => |load_struct| try dw.addExprlocReloc(
318 load_struct.sym_index,
319 offset,
320 is_ptr,
321 ),
322 else => {},
323 }
324 },
325
326 .immediate => |x| {
327 try dbg_info.ensureUnusedCapacity(2);
328 const fixup = dbg_info.items.len;
329 dbg_info.appendSliceAssumeCapacity(&[2]u8{ // DW.AT.location, DW.FORM.exprloc
330 1,
331 if (ty.isSignedInt()) DW.OP.consts else DW.OP.constu,
332 });
333 if (ty.isSignedInt()) {
334 try leb128.writeILEB128(dbg_info.writer(), @bitCast(i64, x));
335 } else {
336 try leb128.writeULEB128(dbg_info.writer(), x);
337 }
338 try dbg_info.append(DW.OP.stack_value);
339 dbg_info.items[fixup] += @intCast(u8, dbg_info.items.len - fixup - 2);
340 },
341
342 .undef => {
343 // DW.AT.location, DW.FORM.exprloc
344 // uleb128(exprloc_len)
345 // DW.OP.implicit_value uleb128(len_of_bytes) bytes
346 const abi_size = @intCast(u32, ty.abiSize(function.target.*));
347 var implicit_value_len = std.ArrayList(u8).init(function.gpa);
348 defer implicit_value_len.deinit();
349 try leb128.writeULEB128(implicit_value_len.writer(), abi_size);
350 const total_exprloc_len = 1 + implicit_value_len.items.len + abi_size;
351 try leb128.writeULEB128(dbg_info.writer(), total_exprloc_len);
352 try dbg_info.ensureUnusedCapacity(total_exprloc_len);
353 dbg_info.appendAssumeCapacity(DW.OP.implicit_value);
354 dbg_info.appendSliceAssumeCapacity(implicit_value_len.items);
355 dbg_info.appendNTimesAssumeCapacity(0xaa, abi_size);
356 },
357
358 .none => {
359 try dbg_info.ensureUnusedCapacity(3);
360 dbg_info.appendSliceAssumeCapacity(&[3]u8{ // DW.AT.location, DW.FORM.exprloc
361 2, DW.OP.lit0, DW.OP.stack_value,
362 });
363 },
364
365 else => {
366 try dbg_info.ensureUnusedCapacity(2);
367 dbg_info.appendSliceAssumeCapacity(&[2]u8{ // DW.AT.location, DW.FORM.exprloc
368 1, DW.OP.nop,
369 });
370 log.debug("TODO generate debug info for {}", .{reloc.mcv});
371 },
372 }
373
374 try dbg_info.ensureUnusedCapacity(5 + name_with_null.len);
375 try function.addDbgInfoTypeReloc(ty); // DW.AT.type, DW.FORM.ref4
376 dbg_info.appendSliceAssumeCapacity(name_with_null); // DW.AT.name, DW.FORM.string
377 },
378 .plan9 => {},
379 .none => {},
380 }
381 }
382};
383
163384const Branch = struct {
164385 inst_table: std.AutoArrayHashMapUnmanaged(Air.Inst.Index, MCValue) = .{},
165386
......@@ -262,6 +483,7 @@ pub fn generate(
262483 .gpa = bin_file.allocator,
263484 .air = air,
264485 .liveness = liveness,
486 .debug_output = debug_output,
265487 .target = &bin_file.options.target,
266488 .bin_file = bin_file,
267489 .mod_fn = module_fn,
......@@ -279,6 +501,7 @@ pub fn generate(
279501 defer function.stack.deinit(bin_file.allocator);
280502 defer function.blocks.deinit(bin_file.allocator);
281503 defer function.exitlude_jump_relocs.deinit(bin_file.allocator);
504 defer function.dbg_info_relocs.deinit(bin_file.allocator);
282505
283506 var call_info = function.resolveCallingConventionValues(fn_type) catch |err| switch (err) {
284507 error.CodegenFail => return FnResult{ .fail = function.err_msg.? },
......@@ -302,6 +525,10 @@ pub fn generate(
302525 else => |e| return e,
303526 };
304527
528 for (function.dbg_info_relocs.items) |reloc| {
529 try reloc.genDbgInfo(function);
530 }
531
305532 var mir = Mir{
306533 .instructions = function.mir_instructions.toOwnedSlice(),
307534 .extra = function.mir_extra.toOwnedSlice(bin_file.allocator),
......@@ -854,23 +1081,20 @@ fn ensureProcessDeathCapacity(self: *Self, additional_count: usize) !void {
8541081
8551082/// Adds a Type to the .debug_info at the current position. The bytes will be populated later,
8561083/// after codegen for this symbol is done.
857fn addDbgInfoTypeReloc(self: *Self, ty: Type) !void {
1084fn addDbgInfoTypeReloc(self: Self, ty: Type) !void {
8581085 switch (self.debug_output) {
859 .dwarf => |dbg_out| {
860 assert(ty.hasRuntimeBits());
861 const index = dbg_out.dbg_info.items.len;
862 try dbg_out.dbg_info.resize(index + 4); // DW.AT.type, DW.FORM.ref4
863
864 const gop = try dbg_out.dbg_info_type_relocs.getOrPutContext(self.gpa, ty, .{
865 .target = self.target.*,
866 });
867 if (!gop.found_existing) {
868 gop.value_ptr.* = .{
869 .off = undefined,
870 .relocs = .{},
871 };
872 }
873 try gop.value_ptr.relocs.append(self.gpa, @intCast(u32, index));
1086 .dwarf => |dw| {
1087 const dbg_info = &dw.dbg_info;
1088 const index = dbg_info.items.len;
1089 try dbg_info.resize(index + 4); // DW.AT.type, DW.FORM.ref4
1090 const mod = self.bin_file.options.module.?;
1091 const fn_owner_decl = mod.declPtr(self.mod_fn.owner_decl);
1092 const atom = switch (self.bin_file.tag) {
1093 .elf => &fn_owner_decl.link.elf.dbg_info_atom,
1094 .macho => &fn_owner_decl.link.macho.dbg_info_atom,
1095 else => unreachable,
1096 };
1097 try dw.addTypeRelocGlobal(atom, ty, @intCast(u32, index));
8741098 },
8751099 .plan9 => {},
8761100 .none => {},
......@@ -3957,8 +4181,9 @@ fn airArg(self: *Self, inst: Air.Inst.Index) !void {
39574181 self.arg_index += 1;
39584182
39594183 const ty = self.air.typeOfIndex(inst);
3960
39614184 const result = self.args[arg_index];
4185 const name = self.mod_fn.getParamName(self.bin_file.options.module.?, arg_index);
4186
39624187 const mcv = switch (result) {
39634188 // Copy registers to the stack
39644189 .register => |reg| blk: {
......@@ -3974,8 +4199,14 @@ fn airArg(self: *Self, inst: Air.Inst.Index) !void {
39744199 },
39754200 else => result,
39764201 };
3977 // TODO generate debug info
3978 // try self.genArgDbgInfo(inst, mcv);
4202
4203 const tag = self.air.instructions.items(.tag)[inst];
4204 try self.dbg_info_relocs.append(self.gpa, .{
4205 .tag = tag,
4206 .ty = ty,
4207 .name = name,
4208 .mcv = result,
4209 });
39794210
39804211 if (self.liveness.isUnused(inst))
39814212 return self.finishAirBookkeeping();
......@@ -4463,10 +4694,21 @@ fn airDbgBlock(self: *Self, inst: Air.Inst.Index) !void {
44634694
44644695fn airDbgVar(self: *Self, inst: Air.Inst.Index) !void {
44654696 const pl_op = self.air.instructions.items(.data)[inst].pl_op;
4466 const name = self.air.nullTerminatedString(pl_op.payload);
44674697 const operand = pl_op.operand;
4468 // TODO emit debug info for this variable
4469 _ = name;
4698 const tag = self.air.instructions.items(.tag)[inst];
4699 const ty = self.air.typeOf(operand);
4700 const mcv = try self.resolveInst(operand);
4701 const name = self.air.nullTerminatedString(pl_op.payload);
4702
4703 log.debug("airDbgVar: %{d}: {}, {}", .{ inst, ty.fmtDebug(), mcv });
4704
4705 try self.dbg_info_relocs.append(self.gpa, .{
4706 .tag = tag,
4707 .ty = ty,
4708 .name = name,
4709 .mcv = mcv,
4710 });
4711
44704712 return self.finishAir(inst, .dead, .{ operand, .none, .none });
44714713}
44724714
src/arch/aarch64/bits.zig+7
......@@ -296,6 +296,13 @@ pub const Register = enum(u8) {
296296 pub fn dwarfLocOp(self: Register) u8 {
297297 return @as(u8, self.enc()) + DW.OP.reg0;
298298 }
299
300 /// DWARF encodings that push a value onto the DWARF stack that is either
301 /// the contents of a register or the result of adding the contents a given
302 /// register to a given signed offset.
303 pub fn dwarfLocOpDeref(self: Register) u8 {
304 return @as(u8, self.enc()) + DW.OP.breg0;
305 }
299306};
300307
301308test "Register.enc" {
src/arch/x86_64/CodeGen.zig+73-65
......@@ -3797,64 +3797,68 @@ fn airArg(self: *Self, inst: Air.Inst.Index) !void {
37973797 const ty = self.air.typeOfIndex(inst);
37983798 const mcv = self.args[arg_index];
37993799 const name = self.mod_fn.getParamName(self.bin_file.options.module.?, arg_index);
3800 const name_with_null = name.ptr[0 .. name.len + 1];
38013800
38023801 if (self.liveness.isUnused(inst))
38033802 return self.finishAirBookkeeping();
38043803
3805 const dst_mcv: MCValue = blk: {
3806 switch (mcv) {
3807 .register => |reg| {
3808 self.register_manager.getRegAssumeFree(reg.to64(), inst);
3809 switch (self.debug_output) {
3810 .dwarf => |dw| {
3811 const dbg_info = &dw.dbg_info;
3812 try dbg_info.ensureUnusedCapacity(3);
3813 dbg_info.appendAssumeCapacity(@enumToInt(link.File.Dwarf.AbbrevKind.parameter));
3814 dbg_info.appendSliceAssumeCapacity(&[2]u8{ // DW.AT.location, DW.FORM.exprloc
3815 1, // ULEB128 dwarf expression length
3816 reg.dwarfLocOp(),
3817 });
3818 try dbg_info.ensureUnusedCapacity(5 + name_with_null.len);
3819 try self.addDbgInfoTypeReloc(ty); // DW.AT.type, DW.FORM.ref4
3820 dbg_info.appendSliceAssumeCapacity(name_with_null); // DW.AT.name, DW.FORM.string
3821 },
3822 .plan9 => {},
3823 .none => {},
3824 }
3825 break :blk mcv;
3826 },
3827 .stack_offset => |off| {
3828 const offset = @intCast(i32, self.max_end_stack) - off + 16;
3829 switch (self.debug_output) {
3830 .dwarf => |dw| {
3831 const dbg_info = &dw.dbg_info;
3832 try dbg_info.ensureUnusedCapacity(8);
3833 dbg_info.appendAssumeCapacity(@enumToInt(link.File.Dwarf.AbbrevKind.parameter));
3834 const fixup = dbg_info.items.len;
3835 dbg_info.appendSliceAssumeCapacity(&[2]u8{ // DW.AT.location, DW.FORM.exprloc
3836 1, // we will backpatch it after we encode the displacement in LEB128
3837 DW.OP.breg6, // .rbp TODO handle -fomit-frame-pointer
3838 });
3839 leb128.writeILEB128(dbg_info.writer(), offset) catch unreachable;
3840 dbg_info.items[fixup] += @intCast(u8, dbg_info.items.len - fixup - 2);
3841 try dbg_info.ensureUnusedCapacity(5 + name_with_null.len);
3842 try self.addDbgInfoTypeReloc(ty); // DW.AT.type, DW.FORM.ref4
3843 dbg_info.appendSliceAssumeCapacity(name_with_null); // DW.AT.name, DW.FORM.string
3844
3845 },
3846 .plan9 => {},
3847 .none => {},
3848 }
3849 break :blk MCValue{ .stack_offset = -offset };
3850 },
3851 else => return self.fail("TODO implement arg for {}", .{mcv}),
3852 }
3804 const dst_mcv: MCValue = switch (mcv) {
3805 .register => |reg| blk: {
3806 self.register_manager.getRegAssumeFree(reg.to64(), inst);
3807 break :blk MCValue{ .register = reg };
3808 },
3809 .stack_offset => |off| blk: {
3810 const offset = @intCast(i32, self.max_end_stack) - off + 16;
3811 break :blk MCValue{ .stack_offset = -offset };
3812 },
3813 else => return self.fail("TODO implement arg for {}", .{mcv}),
38533814 };
3815 try self.genArgDbgInfo(ty, name, dst_mcv);
38543816
38553817 return self.finishAir(inst, dst_mcv, .{ .none, .none, .none });
38563818}
38573819
3820fn genArgDbgInfo(self: Self, ty: Type, name: [:0]const u8, mcv: MCValue) !void {
3821 const name_with_null = name.ptr[0 .. name.len + 1];
3822 switch (self.debug_output) {
3823 .dwarf => |dw| {
3824 const dbg_info = &dw.dbg_info;
3825 switch (mcv) {
3826 .register => |reg| {
3827 try dbg_info.ensureUnusedCapacity(3);
3828 dbg_info.appendAssumeCapacity(@enumToInt(link.File.Dwarf.AbbrevKind.parameter));
3829 dbg_info.appendSliceAssumeCapacity(&[2]u8{ // DW.AT.location, DW.FORM.exprloc
3830 1, // ULEB128 dwarf expression length
3831 reg.dwarfLocOp(),
3832 });
3833 try dbg_info.ensureUnusedCapacity(5 + name_with_null.len);
3834 try self.addDbgInfoTypeReloc(ty); // DW.AT.type, DW.FORM.ref4
3835 dbg_info.appendSliceAssumeCapacity(name_with_null); // DW.AT.name, DW.FORM.string
3836 },
3837
3838 .stack_offset => |off| {
3839 try dbg_info.ensureUnusedCapacity(8);
3840 dbg_info.appendAssumeCapacity(@enumToInt(link.File.Dwarf.AbbrevKind.parameter));
3841 const fixup = dbg_info.items.len;
3842 dbg_info.appendSliceAssumeCapacity(&[2]u8{ // DW.AT.location, DW.FORM.exprloc
3843 1, // we will backpatch it after we encode the displacement in LEB128
3844 Register.rbp.dwarfLocOpDeref(), // TODO handle -fomit-frame-pointer
3845 });
3846 leb128.writeILEB128(dbg_info.writer(), -off) catch unreachable;
3847 dbg_info.items[fixup] += @intCast(u8, dbg_info.items.len - fixup - 2);
3848 try dbg_info.ensureUnusedCapacity(5 + name_with_null.len);
3849 try self.addDbgInfoTypeReloc(ty); // DW.AT.type, DW.FORM.ref4
3850 dbg_info.appendSliceAssumeCapacity(name_with_null); // DW.AT.name, DW.FORM.string
3851
3852 },
3853
3854 else => unreachable, // not a valid function parameter
3855 }
3856 },
3857 .plan9 => {},
3858 .none => {},
3859 }
3860}
3861
38583862fn airBreakpoint(self: *Self) !void {
38593863 _ = try self.addInst(.{
38603864 .tag = .interrupt,
......@@ -4424,7 +4428,7 @@ fn airDbgVar(self: *Self, inst: Air.Inst.Index) !void {
44244428}
44254429
44264430fn genVarDbgInfo(
4427 self: *Self,
4431 self: Self,
44284432 tag: Air.Inst.Tag,
44294433 ty: Type,
44304434 mcv: MCValue,
......@@ -4445,17 +4449,23 @@ fn genVarDbgInfo(
44454449 reg.dwarfLocOp(),
44464450 });
44474451 },
4448 .ptr_stack_offset, .stack_offset => |off| {
4452
4453 .ptr_stack_offset,
4454 .stack_offset,
4455 => |off| {
44494456 try dbg_info.ensureUnusedCapacity(7);
44504457 const fixup = dbg_info.items.len;
44514458 dbg_info.appendSliceAssumeCapacity(&[2]u8{ // DW.AT.location, DW.FORM.exprloc
44524459 1, // we will backpatch it after we encode the displacement in LEB128
4453 DW.OP.breg6, // .rbp TODO handle -fomit-frame-pointer
4460 Register.rbp.dwarfLocOpDeref(), // TODO handle -fomit-frame-pointer
44544461 });
44554462 leb128.writeILEB128(dbg_info.writer(), -off) catch unreachable;
44564463 dbg_info.items[fixup] += @intCast(u8, dbg_info.items.len - fixup - 2);
44574464 },
4458 .memory, .linker_load => {
4465
4466 .memory,
4467 .linker_load,
4468 => {
44594469 const ptr_width = @intCast(u8, @divExact(self.target.cpu.arch.ptrBitWidth(), 8));
44604470 const is_ptr = switch (tag) {
44614471 .dbg_var_ptr => true,
......@@ -4494,27 +4504,23 @@ fn genVarDbgInfo(
44944504 else => {},
44954505 }
44964506 },
4507
44974508 .immediate => |x| {
4498 const signedness: std.builtin.Signedness = blk: {
4499 if (ty.zigTypeTag() != .Int) break :blk .unsigned;
4500 break :blk ty.intInfo(self.target.*).signedness;
4501 };
45024509 try dbg_info.ensureUnusedCapacity(2);
45034510 const fixup = dbg_info.items.len;
45044511 dbg_info.appendSliceAssumeCapacity(&[2]u8{ // DW.AT.location, DW.FORM.exprloc
45054512 1,
4506 switch (signedness) {
4507 .signed => DW.OP.consts,
4508 .unsigned => DW.OP.constu,
4509 },
4513 if (ty.isSignedInt()) DW.OP.consts else DW.OP.constu,
45104514 });
4511 switch (signedness) {
4512 .signed => try leb128.writeILEB128(dbg_info.writer(), @bitCast(i64, x)),
4513 .unsigned => try leb128.writeULEB128(dbg_info.writer(), x),
4515 if (ty.isSignedInt()) {
4516 try leb128.writeILEB128(dbg_info.writer(), @bitCast(i64, x));
4517 } else {
4518 try leb128.writeULEB128(dbg_info.writer(), x);
45144519 }
45154520 try dbg_info.append(DW.OP.stack_value);
45164521 dbg_info.items[fixup] += @intCast(u8, dbg_info.items.len - fixup - 2);
45174522 },
4523
45184524 .undef => {
45194525 // DW.AT.location, DW.FORM.exprloc
45204526 // uleb128(exprloc_len)
......@@ -4530,12 +4536,14 @@ fn genVarDbgInfo(
45304536 dbg_info.appendSliceAssumeCapacity(implicit_value_len.items);
45314537 dbg_info.appendNTimesAssumeCapacity(0xaa, abi_size);
45324538 },
4539
45334540 .none => {
45344541 try dbg_info.ensureUnusedCapacity(3);
45354542 dbg_info.appendSliceAssumeCapacity(&[3]u8{ // DW.AT.location, DW.FORM.exprloc
45364543 2, DW.OP.lit0, DW.OP.stack_value,
45374544 });
45384545 },
4546
45394547 else => {
45404548 try dbg_info.ensureUnusedCapacity(2);
45414549 dbg_info.appendSliceAssumeCapacity(&[2]u8{ // DW.AT.location, DW.FORM.exprloc
......@@ -4556,7 +4564,7 @@ fn genVarDbgInfo(
45564564
45574565/// Adds a Type to the .debug_info at the current position. The bytes will be populated later,
45584566/// after codegen for this symbol is done.
4559fn addDbgInfoTypeReloc(self: *Self, ty: Type) !void {
4567fn addDbgInfoTypeReloc(self: Self, ty: Type) !void {
45604568 switch (self.debug_output) {
45614569 .dwarf => |dw| {
45624570 const dbg_info = &dw.dbg_info;
src/arch/x86_64/bits.zig+61-22
......@@ -135,8 +135,6 @@ pub const Condition = enum(u5) {
135135 }
136136};
137137
138// zig fmt: off
139
140138/// Definitions of all of the general purpose x64 registers. The order is semantically meaningful.
141139/// The registers are defined such that IDs go in descending order of 64-bit,
142140/// 32-bit, 16-bit, and then 8-bit, and each set contains exactly sixteen
......@@ -152,6 +150,7 @@ pub const Condition = enum(u5) {
152150/// The ID can be easily determined by figuring out what range the register is
153151/// in, and then subtracting the base.
154152pub const Register = enum(u7) {
153 // zig fmt: off
155154 // 0 through 15, 64-bit registers. 8-15 are extended.
156155 // id is just the int value.
157156 rax, rcx, rdx, rbx, rsp, rbp, rsi, rdi,
......@@ -184,6 +183,7 @@ pub const Register = enum(u7) {
184183
185184 // Pseudo-value for MIR instructions.
186185 none,
186 // zig fmt: on
187187
188188 pub fn id(self: Register) u7 {
189189 return switch (@enumToInt(self)) {
......@@ -192,7 +192,7 @@ pub const Register = enum(u7) {
192192 else => unreachable,
193193 };
194194 }
195
195
196196 /// Returns the bit-width of the register.
197197 pub fn size(self: Register) u9 {
198198 return switch (@enumToInt(self)) {
......@@ -258,27 +258,66 @@ pub const Register = enum(u7) {
258258 }
259259
260260 pub fn dwarfLocOp(self: Register) u8 {
261 return switch (self.to64()) {
262 .rax => DW.OP.reg0,
263 .rdx => DW.OP.reg1,
264 .rcx => DW.OP.reg2,
265 .rbx => DW.OP.reg3,
266 .rsi => DW.OP.reg4,
267 .rdi => DW.OP.reg5,
268 .rbp => DW.OP.reg6,
269 .rsp => DW.OP.reg7,
270
271 .r8 => DW.OP.reg8,
272 .r9 => DW.OP.reg9,
273 .r10 => DW.OP.reg10,
274 .r11 => DW.OP.reg11,
275 .r12 => DW.OP.reg12,
276 .r13 => DW.OP.reg13,
277 .r14 => DW.OP.reg14,
278 .r15 => DW.OP.reg15,
261 switch (@enumToInt(self)) {
262 0...63 => return switch (self.to64()) {
263 .rax => DW.OP.reg0,
264 .rdx => DW.OP.reg1,
265 .rcx => DW.OP.reg2,
266 .rbx => DW.OP.reg3,
267 .rsi => DW.OP.reg4,
268 .rdi => DW.OP.reg5,
269 .rbp => DW.OP.reg6,
270 .rsp => DW.OP.reg7,
271
272 .r8 => DW.OP.reg8,
273 .r9 => DW.OP.reg9,
274 .r10 => DW.OP.reg10,
275 .r11 => DW.OP.reg11,
276 .r12 => DW.OP.reg12,
277 .r13 => DW.OP.reg13,
278 .r14 => DW.OP.reg14,
279 .r15 => DW.OP.reg15,
280
281 else => unreachable,
282 },
283
284 64...79 => return @as(u8, self.enc()) + DW.OP.reg17,
279285
280286 else => unreachable,
281 };
287 }
288 }
289
290 /// DWARF encodings that push a value onto the DWARF stack that is either
291 /// the contents of a register or the result of adding the contents a given
292 /// register to a given signed offset.
293 pub fn dwarfLocOpDeref(self: Register) u8 {
294 switch (@enumToInt(self)) {
295 0...63 => return switch (self.to64()) {
296 .rax => DW.OP.breg0,
297 .rdx => DW.OP.breg1,
298 .rcx => DW.OP.breg2,
299 .rbx => DW.OP.breg3,
300 .rsi => DW.OP.breg4,
301 .rdi => DW.OP.breg5,
302 .rbp => DW.OP.breg6,
303 .rsp => DW.OP.fbreg,
304
305 .r8 => DW.OP.breg8,
306 .r9 => DW.OP.breg9,
307 .r10 => DW.OP.breg10,
308 .r11 => DW.OP.breg11,
309 .r12 => DW.OP.breg12,
310 .r13 => DW.OP.breg13,
311 .r14 => DW.OP.breg14,
312 .r15 => DW.OP.breg15,
313
314 else => unreachable,
315 },
316
317 64...79 => return @as(u8, self.enc()) + DW.OP.breg17,
318
319 else => unreachable,
320 }
282321 }
283322};
284323
src/link/Dwarf.zig+4-1
......@@ -405,8 +405,11 @@ pub const DeclState = struct {
405405 const value: u64 = if (values) |vals| value: {
406406 if (vals.count() == 0) break :value @intCast(u64, field_i); // auto-numbered
407407 const value = vals.keys()[field_i];
408 // TODO do not assume a 64bit enum value - could be bigger.
409 // See https://github.com/ziglang/zig/issues/645
408410 var int_buffer: Value.Payload.U64 = undefined;
409 break :value value.enumToInt(ty, &int_buffer).toUnsignedInt(target);
411 const field_int_val = value.enumToInt(ty, &int_buffer);
412 break :value @bitCast(u64, field_int_val.toSignedInt());
410413 } else @intCast(u64, field_i);
411414 mem.writeInt(u64, dbg_info_buffer.addManyAsArrayAssumeCapacity(8), value, target_endian);
412415 }
src/link/MachO.zig+2-3
......@@ -329,8 +329,7 @@ pub fn openPath(allocator: Allocator, options: link.Options) !*MachO {
329329
330330 if (!options.strip and options.module != null) {
331331 // Create dSYM bundle.
332 const dir = options.module.?.zig_cache_artifact_directory;
333 log.debug("creating {s}.dSYM bundle in {?s}", .{ emit.sub_path, dir.path });
332 log.debug("creating {s}.dSYM bundle", .{emit.sub_path});
334333
335334 const d_sym_path = try fmt.allocPrint(
336335 allocator,
......@@ -339,7 +338,7 @@ pub fn openPath(allocator: Allocator, options: link.Options) !*MachO {
339338 );
340339 defer allocator.free(d_sym_path);
341340
342 var d_sym_bundle = try dir.handle.makeOpenPath(d_sym_path, .{});
341 var d_sym_bundle = try emit.directory.handle.makeOpenPath(d_sym_path, .{});
343342 defer d_sym_bundle.close();
344343
345344 const d_sym_file = try d_sym_bundle.createFile(emit.sub_path, .{
test/behavior/enum.zig-2
......@@ -1146,8 +1146,6 @@ test "size of enum with only one tag which has explicit integer tag type" {
11461146}
11471147
11481148test "switch on an extern enum with negative value" {
1149 // TODO x86, wasm backends fail because they assume that enum tag types are unsigned
1150 if (@import("builtin").zig_backend == .stage2_x86_64) return error.SkipZigTest;
11511149 if (@import("builtin").zig_backend == .stage2_wasm) return error.SkipZigTest;
11521150
11531151 const Foo = enum(c_int) {