authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-12 19:55:48-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-13 03:41:31-04:00
log0bc96354909c0828b7fd36ce0be5705b7b21d63c
tree2b544abbf34776414478cb5fe4b8baad661337b3
parent7ec2261dbf7a85f5c215f2dac22b80fca7de1e0f

stage2: add debug info for locals in the LLVM backend

Adds 2 new AIR instructions: * dbg_var_ptr * dbg_var_val Sema no longer emits dbg_stmt AIR instructions when strip=true. LLVM backend: fixed lowerPtrToVoid when calling ptrAlignment on the element type is problematic. LLVM backend: fixed alloca instructions improperly getting debug location annotated, causing chaotic debug info behavior. zig_llvm.cpp: fixed incorrect bindings for a function that should use unsigned integers for line and column. A bunch of C test cases regressed because the new dbg_var AIR instructions caused their operands to be alive, exposing latent bugs. Mostly it's just a problem that the C backend lowers mutable and const slices to the same C type, so we need to represent that in the C backend instead of printing two duplicate typedefs.

29 files changed, 338 insertions(+), 26 deletions(-)

src/Air.zig+22-2
......@@ -327,6 +327,15 @@ pub const Inst = struct {
327327 /// Result type is always void.
328328 /// Uses the `dbg_stmt` field.
329329 dbg_stmt,
330 /// Marks the beginning of a local variable. The operand is a pointer pointing
331 /// to the storage for the variable. The local may be a const or a var.
332 /// Result type is always void.
333 /// Uses `pl_op`. The payload index is the variable name. It points to the extra
334 /// array, reinterpreting the bytes there as a null-terminated string.
335 dbg_var_ptr,
336 /// Same as `dbg_var_ptr` except the local is a const, not a var, and the
337 /// operand is the local's value.
338 dbg_var_val,
330339 /// ?T => bool
331340 /// Result type is always bool.
332341 /// Uses the `un_op` field.
......@@ -962,6 +971,8 @@ pub fn typeOfIndex(air: Air, inst: Air.Inst.Index) Type {
962971
963972 .breakpoint,
964973 .dbg_stmt,
974 .dbg_var_ptr,
975 .dbg_var_val,
965976 .store,
966977 .fence,
967978 .atomic_store_unordered,
......@@ -972,13 +983,13 @@ pub fn typeOfIndex(air: Air, inst: Air.Inst.Index) Type {
972983 .memcpy,
973984 .set_union_tag,
974985 .prefetch,
975 => return Type.initTag(.void),
986 => return Type.void,
976987
977988 .ptrtoint,
978989 .slice_len,
979990 .ret_addr,
980991 .frame_addr,
981 => return Type.initTag(.usize),
992 => return Type.usize,
982993
983994 .wasm_memory_grow => return Type.i32,
984995 .wasm_memory_size => return Type.u32,
......@@ -1089,3 +1100,12 @@ pub fn value(air: Air, inst: Air.Inst.Ref) ?Value {
10891100 else => return air.typeOfIndex(inst_index).onePossibleValue(),
10901101 }
10911102}
1103
1104pub fn nullTerminatedString(air: Air, index: usize) [:0]const u8 {
1105 const bytes = std.mem.sliceAsBytes(air.extra[index..]);
1106 var end: usize = 0;
1107 while (bytes[end] != 0) {
1108 end += 1;
1109 }
1110 return bytes[0..end :0];
1111}
src/AstGen.zig+40
......@@ -2389,6 +2389,8 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: Ast.Node.Index) Inner
23892389 .breakpoint,
23902390 .fence,
23912391 .dbg_stmt,
2392 .dbg_var_ptr,
2393 .dbg_var_val,
23922394 .ensure_result_used,
23932395 .ensure_result_non_error,
23942396 .@"export",
......@@ -2666,6 +2668,15 @@ fn varDecl(
26662668 } else .none;
26672669 const init_inst = try reachableExpr(gz, scope, result_loc, var_decl.ast.init_node, node);
26682670
2671 if (!gz.force_comptime) {
2672 _ = try gz.add(.{ .tag = .dbg_var_val, .data = .{
2673 .str_op = .{
2674 .str = ident_name,
2675 .operand = init_inst,
2676 },
2677 } });
2678 }
2679
26692680 const sub_scope = try block_arena.create(Scope.LocalVal);
26702681 sub_scope.* = .{
26712682 .parent = scope,
......@@ -2751,6 +2762,15 @@ fn varDecl(
27512762 }
27522763 gz.instructions.items.len = dst;
27532764
2765 if (!gz.force_comptime) {
2766 _ = try gz.add(.{ .tag = .dbg_var_val, .data = .{
2767 .str_op = .{
2768 .str = ident_name,
2769 .operand = init_inst,
2770 },
2771 } });
2772 }
2773
27542774 const sub_scope = try block_arena.create(Scope.LocalVal);
27552775 sub_scope.* = .{
27562776 .parent = scope,
......@@ -2785,6 +2805,16 @@ fn varDecl(
27852805 _ = try gz.addUnNode(.resolve_inferred_alloc, resolve_inferred_alloc, node);
27862806 }
27872807 const const_ptr = try gz.addUnNode(.make_ptr_const, init_scope.rl_ptr, node);
2808
2809 if (!gz.force_comptime) {
2810 _ = try gz.add(.{ .tag = .dbg_var_ptr, .data = .{
2811 .str_op = .{
2812 .str = ident_name,
2813 .operand = const_ptr,
2814 },
2815 } });
2816 }
2817
27882818 const sub_scope = try block_arena.create(Scope.LocalPtr);
27892819 sub_scope.* = .{
27902820 .parent = scope,
......@@ -2848,6 +2878,16 @@ fn varDecl(
28482878 if (resolve_inferred_alloc != .none) {
28492879 _ = try gz.addUnNode(.resolve_inferred_alloc, resolve_inferred_alloc, node);
28502880 }
2881
2882 if (!gz.force_comptime) {
2883 _ = try gz.add(.{ .tag = .dbg_var_ptr, .data = .{
2884 .str_op = .{
2885 .str = ident_name,
2886 .operand = var_data.alloc,
2887 },
2888 } });
2889 }
2890
28512891 const sub_scope = try block_arena.create(Scope.LocalPtr);
28522892 sub_scope.* = .{
28532893 .parent = scope,
src/Liveness.zig+7
......@@ -394,6 +394,13 @@ fn analyzeInst(
394394 return trackOperands(a, new_set, inst, main_tomb, .{ operand, .none, .none });
395395 },
396396
397 .dbg_var_ptr,
398 .dbg_var_val,
399 => {
400 const operand = inst_datas[inst].pl_op.operand;
401 return trackOperands(a, new_set, inst, main_tomb, .{ operand, .none, .none });
402 },
403
397404 .prefetch => {
398405 const prefetch = inst_datas[inst].prefetch;
399406 return trackOperands(a, new_set, inst, main_tomb, .{ prefetch.ptr, .none, .none });
src/Sema.zig+43-4
......@@ -872,6 +872,16 @@ fn analyzeBodyInner(
872872 i += 1;
873873 continue;
874874 },
875 .dbg_var_ptr => {
876 try sema.zirDbgVar(block, inst, .dbg_var_ptr);
877 i += 1;
878 continue;
879 },
880 .dbg_var_val => {
881 try sema.zirDbgVar(block, inst, .dbg_var_val);
882 i += 1;
883 continue;
884 },
875885 .ensure_err_payload_void => {
876886 try sema.zirEnsureErrPayloadVoid(block, inst);
877887 i += 1;
......@@ -4158,14 +4168,11 @@ fn zirBreak(sema: *Sema, start_block: *Block, inst: Zir.Inst.Index) CompileError
41584168}
41594169
41604170fn zirDbgStmt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
4161 const tracy = trace(@src());
4162 defer tracy.end();
4163
41644171 // We do not set sema.src here because dbg_stmt instructions are only emitted for
41654172 // ZIR code that possibly will need to generate runtime code. So error messages
41664173 // and other source locations must not rely on sema.src being set from dbg_stmt
41674174 // instructions.
4168 if (block.is_comptime) return;
4175 if (block.is_comptime or sema.mod.comp.bin_file.options.strip) return;
41694176
41704177 const inst_data = sema.code.instructions.items(.data)[inst].dbg_stmt;
41714178 _ = try block.addInst(.{
......@@ -4177,6 +4184,38 @@ fn zirDbgStmt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!voi
41774184 });
41784185}
41794186
4187fn zirDbgVar(
4188 sema: *Sema,
4189 block: *Block,
4190 inst: Zir.Inst.Index,
4191 air_tag: Air.Inst.Tag,
4192) CompileError!void {
4193 if (block.is_comptime or sema.mod.comp.bin_file.options.strip) return;
4194
4195 const str_op = sema.code.instructions.items(.data)[inst].str_op;
4196 const operand = sema.resolveInst(str_op.operand);
4197 const operand_ty = sema.typeOf(operand);
4198 if (!(try sema.typeHasRuntimeBits(block, sema.src, operand_ty))) return;
4199 const name = str_op.getStr(sema.code);
4200
4201 // Add the name to the AIR.
4202 const name_extra_index = @intCast(u32, sema.air_extra.items.len);
4203 const elements_used = name.len / 4 + 1;
4204 try sema.air_extra.ensureUnusedCapacity(sema.gpa, elements_used);
4205 const buffer = mem.sliceAsBytes(sema.air_extra.unusedCapacitySlice());
4206 mem.copy(u8, buffer, name);
4207 buffer[name.len] = 0;
4208 sema.air_extra.items.len += elements_used;
4209
4210 _ = try block.addInst(.{
4211 .tag = air_tag,
4212 .data = .{ .pl_op = .{
4213 .payload = name_extra_index,
4214 .operand = operand,
4215 } },
4216 });
4217}
4218
41804219fn zirDeclRef(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
41814220 const inst_data = sema.code.instructions.items(.data)[inst].str_tok;
41824221 const src = inst_data.src();
src/Zir.zig+22
......@@ -322,6 +322,14 @@ pub const Inst = struct {
322322 /// Uses the `dbg_stmt` union field. The line and column are offset
323323 /// from the parent declaration.
324324 dbg_stmt,
325 /// Marks a variable declaration. Used for debug info.
326 /// Uses the `str_op` union field. The string is the local variable name,
327 /// and the operand is the pointer to the variable's location. The local
328 /// may be a const or a var.
329 dbg_var_ptr,
330 /// Same as `dbg_var_ptr` but the local is always a const and the operand
331 /// is the local's value.
332 dbg_var_val,
325333 /// Uses a name to identify a Decl and takes a pointer to it.
326334 /// Uses the `str_tok` union field.
327335 decl_ref,
......@@ -1032,6 +1040,8 @@ pub const Inst = struct {
10321040 .error_set_decl_anon,
10331041 .error_set_decl_func,
10341042 .dbg_stmt,
1043 .dbg_var_ptr,
1044 .dbg_var_val,
10351045 .decl_ref,
10361046 .decl_val,
10371047 .load,
......@@ -1297,6 +1307,8 @@ pub const Inst = struct {
12971307 .error_set_decl_anon = .pl_node,
12981308 .error_set_decl_func = .pl_node,
12991309 .dbg_stmt = .dbg_stmt,
1310 .dbg_var_ptr = .str_op,
1311 .dbg_var_val = .str_op,
13001312 .decl_ref = .str_tok,
13011313 .decl_val = .str_tok,
13021314 .load = .un_node,
......@@ -2232,6 +2244,15 @@ pub const Inst = struct {
22322244 return .{ .node_offset = self.src_node };
22332245 }
22342246 },
2247 str_op: struct {
2248 /// Offset into `string_bytes`. Null-terminated.
2249 str: u32,
2250 operand: Ref,
2251
2252 pub fn getStr(self: @This(), zir: Zir) [:0]const u8 {
2253 return zir.nullTerminatedString(self.str);
2254 }
2255 },
22352256
22362257 // Make sure we don't accidentally add a field to make this union
22372258 // bigger than expected. Note that in Debug builds, Zig is allowed
......@@ -2268,6 +2289,7 @@ pub const Inst = struct {
22682289 switch_capture,
22692290 dbg_stmt,
22702291 inst_node,
2292 str_op,
22712293 };
22722294 };
22732295
src/arch/aarch64/CodeGen.zig+13
......@@ -643,6 +643,10 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
643643 .prefetch => try self.airPrefetch(inst),
644644 .mul_add => try self.airMulAdd(inst),
645645
646 .dbg_var_ptr,
647 .dbg_var_val,
648 => try self.airDbgVar(inst),
649
646650 .call => try self.airCall(inst, .auto),
647651 .call_always_tail => try self.airCall(inst, .always_tail),
648652 .call_never_tail => try self.airCall(inst, .never_tail),
......@@ -2650,6 +2654,15 @@ fn airDbgStmt(self: *Self, inst: Air.Inst.Index) !void {
26502654 return self.finishAirBookkeeping();
26512655}
26522656
2657fn airDbgVar(self: *Self, inst: Air.Inst.Index) !void {
2658 const pl_op = self.air.instructions.items(.data)[inst].pl_op;
2659 const name = self.air.nullTerminatedString(pl_op.payload);
2660 const operand = pl_op.operand;
2661 // TODO emit debug info for this variable
2662 _ = name;
2663 return self.finishAir(inst, .dead, .{ operand, .none, .none });
2664}
2665
26532666fn airCondBr(self: *Self, inst: Air.Inst.Index) !void {
26542667 const pl_op = self.air.instructions.items(.data)[inst].pl_op;
26552668 const cond = try self.resolveInst(pl_op.operand);
src/arch/arm/CodeGen.zig+13
......@@ -642,6 +642,10 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
642642 .prefetch => try self.airPrefetch(inst),
643643 .mul_add => try self.airMulAdd(inst),
644644
645 .dbg_var_ptr,
646 .dbg_var_val,
647 => try self.airDbgVar(inst),
648
645649 .call => try self.airCall(inst, .auto),
646650 .call_always_tail => try self.airCall(inst, .always_tail),
647651 .call_never_tail => try self.airCall(inst, .never_tail),
......@@ -2831,6 +2835,15 @@ fn airDbgStmt(self: *Self, inst: Air.Inst.Index) !void {
28312835 return self.finishAirBookkeeping();
28322836}
28332837
2838fn airDbgVar(self: *Self, inst: Air.Inst.Index) !void {
2839 const pl_op = self.air.instructions.items(.data)[inst].pl_op;
2840 const name = self.air.nullTerminatedString(pl_op.payload);
2841 const operand = pl_op.operand;
2842 // TODO emit debug info for this variable
2843 _ = name;
2844 return self.finishAir(inst, .dead, .{ operand, .none, .none });
2845}
2846
28342847fn airCondBr(self: *Self, inst: Air.Inst.Index) !void {
28352848 const pl_op = self.air.instructions.items(.data)[inst].pl_op;
28362849 const cond = try self.resolveInst(pl_op.operand);
src/arch/riscv64/CodeGen.zig+13
......@@ -609,6 +609,10 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
609609 .prefetch => try self.airPrefetch(inst),
610610 .mul_add => try self.airMulAdd(inst),
611611
612 .dbg_var_ptr,
613 .dbg_var_val,
614 => try self.airDbgVar(inst),
615
612616 .call => try self.airCall(inst, .auto),
613617 .call_always_tail => try self.airCall(inst, .always_tail),
614618 .call_never_tail => try self.airCall(inst, .never_tail),
......@@ -1636,6 +1640,15 @@ fn airDbgStmt(self: *Self, inst: Air.Inst.Index) !void {
16361640 return self.finishAirBookkeeping();
16371641}
16381642
1643fn airDbgVar(self: *Self, inst: Air.Inst.Index) !void {
1644 const pl_op = self.air.instructions.items(.data)[inst].pl_op;
1645 const name = self.air.nullTerminatedString(pl_op.payload);
1646 const operand = pl_op.operand;
1647 // TODO emit debug info for this variable
1648 _ = name;
1649 return self.finishAir(inst, .dead, .{ operand, .none, .none });
1650}
1651
16391652fn airCondBr(self: *Self, inst: Air.Inst.Index) !void {
16401653 _ = inst;
16411654
src/arch/wasm/CodeGen.zig+6-1
......@@ -1219,13 +1219,18 @@ fn genInst(self: *Self, inst: Air.Inst.Index) !WValue {
12191219 .br => self.airBr(inst),
12201220 .bool_to_int => self.airBoolToInt(inst),
12211221 .cond_br => self.airCondBr(inst),
1222 .dbg_stmt => WValue.none,
12231222 .intcast => self.airIntcast(inst),
12241223 .fptrunc => self.airFptrunc(inst),
12251224 .fpext => self.airFpext(inst),
12261225 .float_to_int => self.airFloatToInt(inst),
12271226 .get_union_tag => self.airGetUnionTag(inst),
12281227
1228 // TODO
1229 .dbg_stmt,
1230 .dbg_var_ptr,
1231 .dbg_var_val,
1232 => WValue.none,
1233
12291234 .call => self.airCall(inst, .auto),
12301235 .call_always_tail => self.airCall(inst, .always_tail),
12311236 .call_never_tail => self.airCall(inst, .never_tail),
src/arch/x86_64/CodeGen.zig+13
......@@ -726,6 +726,10 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
726726 .prefetch => try self.airPrefetch(inst),
727727 .mul_add => try self.airMulAdd(inst),
728728
729 .dbg_var_ptr,
730 .dbg_var_val,
731 => try self.airDbgVar(inst),
732
729733 .call => try self.airCall(inst, .auto),
730734 .call_always_tail => try self.airCall(inst, .always_tail),
731735 .call_never_tail => try self.airCall(inst, .never_tail),
......@@ -3666,6 +3670,15 @@ fn airDbgStmt(self: *Self, inst: Air.Inst.Index) !void {
36663670 return self.finishAirBookkeeping();
36673671}
36683672
3673fn airDbgVar(self: *Self, inst: Air.Inst.Index) !void {
3674 const pl_op = self.air.instructions.items(.data)[inst].pl_op;
3675 const name = self.air.nullTerminatedString(pl_op.payload);
3676 const operand = pl_op.operand;
3677 // TODO emit debug info for this variable
3678 _ = name;
3679 return self.finishAir(inst, .dead, .{ operand, .none, .none });
3680}
3681
36693682fn genCondBrMir(self: *Self, ty: Type, mcv: MCValue) !u32 {
36703683 const abi_size = ty.abiSize(self.target.*);
36713684 switch (mcv) {
src/codegen/c.zig+14
......@@ -1721,6 +1721,10 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutO
17211721 .union_init => try airUnionInit(f, inst),
17221722 .prefetch => try airPrefetch(f, inst),
17231723
1724 .dbg_var_ptr,
1725 .dbg_var_val,
1726 => try airDbgVar(f, inst),
1727
17241728 .call => try airCall(f, inst, .auto),
17251729 .call_always_tail => try airCall(f, inst, .always_tail),
17261730 .call_never_tail => try airCall(f, inst, .never_tail),
......@@ -2651,6 +2655,16 @@ fn airDbgStmt(f: *Function, inst: Air.Inst.Index) !CValue {
26512655 return CValue.none;
26522656}
26532657
2658fn airDbgVar(f: *Function, inst: Air.Inst.Index) !CValue {
2659 const pl_op = f.air.instructions.items(.data)[inst].pl_op;
2660 const name = f.air.nullTerminatedString(pl_op.payload);
2661 const operand = try f.resolveInst(pl_op.operand);
2662 _ = operand;
2663 const writer = f.object.writer();
2664 try writer.print("/* var:{s} */\n", .{name});
2665 return CValue.none;
2666}
2667
26542668fn airBlock(f: *Function, inst: Air.Inst.Index) !CValue {
26552669 const ty_pl = f.air.instructions.items(.data)[inst].ty_pl;
26562670 const extra = f.air.extraData(Air.Block, ty_pl.payload);
src/codegen/llvm.zig+66-9
......@@ -613,6 +613,8 @@ pub const Object = struct {
613613 .single_threaded = module.comp.bin_file.options.single_threaded,
614614 .di_scope = di_scope,
615615 .di_file = di_file,
616 .prev_dbg_line = 0,
617 .prev_dbg_column = 0,
616618 };
617619 defer fg.deinit();
618620
......@@ -2885,8 +2887,7 @@ pub const DeclGen = struct {
28852887 }
28862888
28872889 fn lowerPtrToVoid(dg: *DeclGen, ptr_ty: Type) !*const llvm.Value {
2888 const target = dg.module.getTarget();
2889 const alignment = ptr_ty.ptrAlignment(target);
2890 const alignment = ptr_ty.ptrInfo().data.@"align";
28902891 // Even though we are pointing at something which has zero bits (e.g. `void`),
28912892 // Pointers are defined to have bits. So we must return something here.
28922893 // The value cannot be undefined, because we use the `nonnull` annotation
......@@ -2902,6 +2903,7 @@ pub const DeclGen = struct {
29022903 // have an "undef_but_not_null" attribute. As an example, if this `alloc` AIR
29032904 // instruction is followed by a `wrap_optional`, it will return this value
29042905 // verbatim, and the result should test as non-null.
2906 const target = dg.module.getTarget();
29052907 const int = switch (target.cpu.arch.ptrBitWidth()) {
29062908 32 => llvm_usize.constInt(0xaaaaaaaa, .False),
29072909 64 => llvm_usize.constInt(0xaaaaaaaa_aaaaaaaa, .False),
......@@ -3004,6 +3006,8 @@ pub const FuncGen = struct {
30043006 builder: *const llvm.Builder,
30053007 di_scope: ?*llvm.DIScope,
30063008 di_file: ?*llvm.DIFile,
3009 prev_dbg_line: c_uint,
3010 prev_dbg_column: c_uint,
30073011
30083012 /// This stores the LLVM values used in a function, such that they can be referred to
30093013 /// in other instructions. This table is cleared before every function is generated.
......@@ -3255,6 +3259,8 @@ pub const FuncGen = struct {
32553259 .const_ty => unreachable,
32563260 .unreach => self.airUnreach(inst),
32573261 .dbg_stmt => self.airDbgStmt(inst),
3262 .dbg_var_ptr => try self.airDbgVarPtr(inst),
3263 .dbg_var_val => try self.airDbgVarVal(inst),
32583264 // zig fmt: on
32593265 };
32603266 if (opt_value) |val| {
......@@ -3967,11 +3973,56 @@ pub const FuncGen = struct {
39673973 fn airDbgStmt(self: *FuncGen, inst: Air.Inst.Index) ?*const llvm.Value {
39683974 const di_scope = self.di_scope orelse return null;
39693975 const dbg_stmt = self.air.instructions.items(.data)[inst].dbg_stmt;
3970 self.builder.setCurrentDebugLocation(
3971 @intCast(c_int, self.dg.decl.src_line + dbg_stmt.line + 1),
3972 @intCast(c_int, dbg_stmt.column + 1),
3973 di_scope,
3976 self.prev_dbg_line = @intCast(c_uint, self.dg.decl.src_line + dbg_stmt.line + 1);
3977 self.prev_dbg_column = @intCast(c_uint, dbg_stmt.column + 1);
3978 self.builder.setCurrentDebugLocation(self.prev_dbg_line, self.prev_dbg_column, di_scope);
3979 return null;
3980 }
3981
3982 fn airDbgVarPtr(self: *FuncGen, inst: Air.Inst.Index) !?*const llvm.Value {
3983 const dib = self.dg.object.di_builder orelse return null;
3984 const pl_op = self.air.instructions.items(.data)[inst].pl_op;
3985 const operand = try self.resolveInst(pl_op.operand);
3986 const name = self.air.nullTerminatedString(pl_op.payload);
3987
3988 const di_local_var = dib.createAutoVariable(
3989 self.di_scope.?,
3990 name.ptr,
3991 self.di_file.?,
3992 self.prev_dbg_line,
3993 try self.dg.lowerDebugType(self.air.typeOf(pl_op.operand)),
3994 true, // always preserve
3995 0, // flags
3996 );
3997 const debug_loc = llvm.getDebugLoc(self.prev_dbg_line, self.prev_dbg_column, self.di_scope.?);
3998 const insert_block = self.builder.getInsertBlock();
3999 _ = dib.insertDeclareAtEnd(operand, di_local_var, debug_loc, insert_block);
4000 return null;
4001 }
4002
4003 fn airDbgVarVal(self: *FuncGen, inst: Air.Inst.Index) !?*const llvm.Value {
4004 const dib = self.dg.object.di_builder orelse return null;
4005 const pl_op = self.air.instructions.items(.data)[inst].pl_op;
4006 const operand = try self.resolveInst(pl_op.operand);
4007 const operand_ty = self.air.typeOf(pl_op.operand);
4008 const name = self.air.nullTerminatedString(pl_op.payload);
4009
4010 const di_local_var = dib.createAutoVariable(
4011 self.di_scope.?,
4012 name.ptr,
4013 self.di_file.?,
4014 self.prev_dbg_line,
4015 try self.dg.lowerDebugType(operand_ty),
4016 true, // always preserve
4017 0, // flags
39744018 );
4019 const debug_loc = llvm.getDebugLoc(self.prev_dbg_line, self.prev_dbg_column, self.di_scope.?);
4020 const insert_block = self.builder.getInsertBlock();
4021 if (isByRef(operand_ty)) {
4022 _ = dib.insertDeclareAtEnd(operand, di_local_var, debug_loc, insert_block);
4023 } else {
4024 _ = dib.insertDbgValueIntrinsicAtEnd(operand, di_local_var, debug_loc, insert_block);
4025 }
39754026 return null;
39764027 }
39774028
......@@ -5272,6 +5323,13 @@ pub const FuncGen = struct {
52725323 /// put the alloca instruction at the top of the function!
52735324 fn buildAlloca(self: *FuncGen, llvm_ty: *const llvm.Type) *const llvm.Value {
52745325 const prev_block = self.builder.getInsertBlock();
5326 const prev_debug_location = self.builder.getCurrentDebugLocation2();
5327 defer {
5328 self.builder.positionBuilderAtEnd(prev_block);
5329 if (self.di_scope != null) {
5330 self.builder.setCurrentDebugLocation2(prev_debug_location);
5331 }
5332 }
52755333
52765334 const entry_block = self.llvm_func.getFirstBasicBlock().?;
52775335 if (entry_block.getFirstInstruction()) |first_inst| {
......@@ -5279,10 +5337,9 @@ pub const FuncGen = struct {
52795337 } else {
52805338 self.builder.positionBuilderAtEnd(entry_block);
52815339 }
5340 self.builder.clearCurrentDebugLocation();
52825341
5283 const alloca = self.builder.buildAlloca(llvm_ty, "");
5284 self.builder.positionBuilderAtEnd(prev_block);
5285 return alloca;
5342 return self.builder.buildAlloca(llvm_ty, "");
52865343 }
52875344
52885345 fn airStore(self: *FuncGen, inst: Air.Inst.Index) !?*const llvm.Value {
src/codegen/llvm/bindings.zig+1-1
......@@ -840,7 +840,7 @@ pub const Builder = opaque {
840840 extern fn LLVMBuildExactSDiv(*const Builder, LHS: *const Value, RHS: *const Value, Name: [*:0]const u8) *const Value;
841841
842842 pub const setCurrentDebugLocation = ZigLLVMSetCurrentDebugLocation;
843 extern fn ZigLLVMSetCurrentDebugLocation(builder: *const Builder, line: c_int, column: c_int, scope: *DIScope) void;
843 extern fn ZigLLVMSetCurrentDebugLocation(builder: *const Builder, line: c_uint, column: c_uint, scope: *DIScope) void;
844844
845845 pub const clearCurrentDebugLocation = ZigLLVMClearCurrentDebugLocation;
846846 extern fn ZigLLVMClearCurrentDebugLocation(builder: *const Builder) void;
src/print_air.zig+14-3
......@@ -233,6 +233,10 @@ const Writer = struct {
233233 .call_never_inline,
234234 => try w.writeCall(s, inst),
235235
236 .dbg_var_ptr,
237 .dbg_var_val,
238 => try w.writeDbgVar(s, inst),
239
236240 .struct_field_ptr => try w.writeStructField(s, inst),
237241 .struct_field_val => try w.writeStructField(s, inst),
238242 .constant => try w.writeConstant(s, inst),
......@@ -499,7 +503,7 @@ const Writer = struct {
499503 extra_i += inputs.len;
500504
501505 for (outputs) |output| {
502 const constraint = std.mem.sliceTo(std.mem.sliceAsBytes(w.air.extra[extra_i..]), 0);
506 const constraint = w.air.nullTerminatedString(extra_i);
503507 // This equation accounts for the fact that even if we have exactly 4 bytes
504508 // for the string, we still use the next u32 for the null terminator.
505509 extra_i += constraint.len / 4 + 1;
......@@ -515,7 +519,7 @@ const Writer = struct {
515519 }
516520
517521 for (inputs) |input| {
518 const constraint = std.mem.sliceTo(std.mem.sliceAsBytes(w.air.extra[extra_i..]), 0);
522 const constraint = w.air.nullTerminatedString(extra_i);
519523 // This equation accounts for the fact that even if we have exactly 4 bytes
520524 // for the string, we still use the next u32 for the null terminator.
521525 extra_i += constraint.len / 4 + 1;
......@@ -529,7 +533,7 @@ const Writer = struct {
529533 {
530534 var clobber_i: u32 = 0;
531535 while (clobber_i < clobbers_len) : (clobber_i += 1) {
532 const clobber = std.mem.sliceTo(std.mem.sliceAsBytes(w.air.extra[extra_i..]), 0);
536 const clobber = w.air.nullTerminatedString(extra_i);
533537 // This equation accounts for the fact that even if we have exactly 4 bytes
534538 // for the string, we still use the next u32 for the null terminator.
535539 extra_i += clobber.len / 4 + 1;
......@@ -548,6 +552,13 @@ const Writer = struct {
548552 try s.print("{d}:{d}", .{ dbg_stmt.line + 1, dbg_stmt.column + 1 });
549553 }
550554
555 fn writeDbgVar(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
556 const pl_op = w.air.instructions.items(.data)[inst].pl_op;
557 try w.writeOperand(s, inst, 0, pl_op.operand);
558 const name = w.air.nullTerminatedString(pl_op.payload);
559 try s.print(", {s}", .{name});
560 }
561
551562 fn writeCall(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
552563 const pl_op = w.air.instructions.items(.data)[inst].pl_op;
553564 const extra = w.air.extraData(Air.Call, pl_op.payload);
src/print_zir.zig+11
......@@ -424,6 +424,10 @@ const Writer = struct {
424424 .param_anytype_comptime,
425425 => try self.writeStrTok(stream, inst),
426426
427 .dbg_var_ptr,
428 .dbg_var_val,
429 => try self.writeStrOp(stream, inst),
430
427431 .param, .param_comptime => try self.writeParam(stream, inst),
428432
429433 .func => try self.writeFunc(stream, inst, false),
......@@ -1837,6 +1841,13 @@ const Writer = struct {
18371841 try self.writeSrc(stream, inst_data.src());
18381842 }
18391843
1844 fn writeStrOp(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
1845 const inst_data = self.code.instructions.items(.data)[inst].str_op;
1846 const str = inst_data.getStr(self.code);
1847 try self.writeInstRef(stream, inst_data.operand);
1848 try stream.print(", \"{}\")", .{std.zig.fmtEscapes(str)});
1849 }
1850
18401851 fn writeFunc(
18411852 self: *Writer,
18421853 stream: anytype,
src/zig_llvm.cpp+3-1
......@@ -791,7 +791,9 @@ void ZigLLVMDisposeDIBuilder(ZigLLVMDIBuilder *dbuilder) {
791791 delete di_builder;
792792}
793793
794void ZigLLVMSetCurrentDebugLocation(LLVMBuilderRef builder, int line, int column, ZigLLVMDIScope *scope) {
794void ZigLLVMSetCurrentDebugLocation(LLVMBuilderRef builder,
795 unsigned int line, unsigned int column, ZigLLVMDIScope *scope)
796{
795797 DIScope* di_scope = reinterpret_cast<DIScope*>(scope);
796798 DebugLoc debug_loc = DILocation::get(di_scope->getContext(), line, column, di_scope, nullptr, false);
797799 unwrap(builder)->SetCurrentDebugLocation(debug_loc);
src/zig_llvm.h+2-2
......@@ -228,8 +228,8 @@ ZIG_EXTERN_C void ZigLLVMSetModulePICLevel(LLVMModuleRef module);
228228ZIG_EXTERN_C void ZigLLVMSetModulePIELevel(LLVMModuleRef module);
229229ZIG_EXTERN_C void ZigLLVMSetModuleCodeModel(LLVMModuleRef module, LLVMCodeModel code_model);
230230
231ZIG_EXTERN_C void ZigLLVMSetCurrentDebugLocation(LLVMBuilderRef builder, int line, int column,
232 struct ZigLLVMDIScope *scope);
231ZIG_EXTERN_C void ZigLLVMSetCurrentDebugLocation(LLVMBuilderRef builder,
232 unsigned int line, unsigned int column, struct ZigLLVMDIScope *scope);
233233ZIG_EXTERN_C void ZigLLVMClearCurrentDebugLocation(LLVMBuilderRef builder);
234234
235235ZIG_EXTERN_C struct ZigLLVMDIScope *ZigLLVMLexicalBlockToScope(struct ZigLLVMDILexicalBlock *lexical_block);
test/behavior/align.zig+2
......@@ -6,6 +6,8 @@ const native_arch = builtin.target.cpu.arch;
66var foo: u8 align(4) = 100;
77
88test "global variable alignment" {
9 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
10
911 comptime try expect(@typeInfo(@TypeOf(&foo)).Pointer.alignment == 4);
1012 comptime try expect(@TypeOf(&foo) == *align(4) u8);
1113 {
test/behavior/bugs/2692.zig+1
......@@ -5,6 +5,7 @@ fn foo(a: []u8) void {
55}
66
77test "address of 0 length array" {
8 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
89 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
910 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
1011
test/behavior/bugs/4954.zig+1
......@@ -5,6 +5,7 @@ fn f(buf: []u8) void {
55}
66
77test "crash" {
8 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
89 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
910 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
1011
test/behavior/bugs/5398.zig+2
......@@ -19,10 +19,12 @@ pub const Renderable = struct {
1919var renderable: Renderable = undefined;
2020
2121test "assignment of field with padding" {
22 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
2223 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
2324 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
2425 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
2526 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
27
2628 renderable = Renderable{
2729 .mesh = Mesh{ .id = 0 },
2830 .material = Material{
test/behavior/eval.zig+4
......@@ -416,6 +416,8 @@ fn copyWithPartialInline(s: []u32, b: []u8) void {
416416}
417417
418418test "binary math operator in partially inlined function" {
419 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
420
419421 var s: [4]u32 = undefined;
420422 var b: [16]u8 = undefined;
421423
......@@ -545,6 +547,8 @@ var simple_struct = SimpleStruct{ .field = 1234 };
545547const bound_fn = simple_struct.method;
546548
547549test "ptr to local array argument at comptime" {
550 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
551
548552 comptime {
549553 var bytes: [10]u8 = undefined;
550554 modifySomeBytes(bytes[0..]);
test/behavior/floatop.zig+16-3
......@@ -302,7 +302,11 @@ fn testExp2() !void {
302302}
303303
304304test "@log" {
305 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
305 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
306 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
307 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
308 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
309 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
306310
307311 comptime try testLog();
308312 try testLog();
......@@ -326,13 +330,22 @@ fn testLog() !void {
326330 try expect(math.approxEqAbs(ty, @log(@as(ty, 2)), 0.6931471805599, eps));
327331 try expect(math.approxEqAbs(ty, @log(@as(ty, 5)), 1.6094379124341, eps));
328332 }
333}
334
335test "@log with vectors" {
336 if (builtin.zig_backend == .stage2_llvm) return error.SkipZigTest; // TODO
337 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
338 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
339 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
340 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
341 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
329342
330343 {
331 var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 0.3, 0.4 };
344 var v: @Vector(4, f32) = [_]f32{ 1.1, 2.2, 0.3, 0.4 };
332345 var result = @log(v);
333346 try expect(math.approxEqAbs(f32, @log(@as(f32, 1.1)), result[0], epsilon));
334347 try expect(math.approxEqAbs(f32, @log(@as(f32, 2.2)), result[1], epsilon));
335 try expect(math.approxEqAbs(f32, @log(@as(f32, 0.3)), result[2], epsilon));
348 try expect(@log(@as(f32, 0.3)) == result[2]);
336349 try expect(math.approxEqAbs(f32, @log(@as(f32, 0.4)), result[3], epsilon));
337350 }
338351}
test/behavior/fn.zig+1
......@@ -296,6 +296,7 @@ fn voidFun(a: i32, b: void, c: i32, d: void) !void {
296296}
297297
298298test "call function with empty string" {
299 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
299300 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
300301
301302 acceptsString("");
test/behavior/for.zig+1
......@@ -151,6 +151,7 @@ test "2 break statements and an else" {
151151}
152152
153153test "for loop with pointer elem var" {
154 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
154155 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
155156 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
156157 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
test/behavior/generics.zig+1
......@@ -170,6 +170,7 @@ fn getFirstByte(comptime T: type, mem: []const T) u8 {
170170}
171171
172172test "generic fn keeps non-generic parameter types" {
173 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
173174 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
174175 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
175176
test/behavior/slice.zig+2
......@@ -242,6 +242,7 @@ test "result location zero sized array inside struct field implicit cast to slic
242242}
243243
244244test "runtime safety lets us slice from len..len" {
245 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
245246 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
246247 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
247248
......@@ -350,6 +351,7 @@ test "empty array to slice" {
350351}
351352
352353test "@ptrCast slice to pointer" {
354 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
353355 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
354356
355357 const S = struct {
test/behavior/union.zig+1
......@@ -453,6 +453,7 @@ pub const FooUnion = union(enum) {
453453var glbl_array: [2]FooUnion = undefined;
454454
455455test "initialize global array of union" {
456 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
456457 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
457458 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
458459 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
test/behavior/union_with_members.zig+3
......@@ -1,3 +1,4 @@
1const builtin = @import("builtin");
12const std = @import("std");
23const expect = std.testing.expect;
34const mem = std.mem;
......@@ -16,6 +17,8 @@ const ET = union(enum) {
1617};
1718
1819test "enum with members" {
20 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
21
1922 const a = ET{ .SINT = -42 };
2023 const b = ET{ .UINT = 42 };
2124 var buf: [20]u8 = undefined;