| ... | @@ -107,6 +107,10 @@ pub const DeclGen = struct { | ... | @@ -107,6 +107,10 @@ pub const DeclGen = struct { |
| 107 | /// The code (prologue and body) for the function we are currently generating code for. | 107 | /// The code (prologue and body) for the function we are currently generating code for. |
| 108 | func: SpvModule.Fn = .{}, | 108 | func: SpvModule.Fn = .{}, |
| 109 | | 109 | |
| | 110 | /// Stack of the base offsets of the current decl, which is what `dbg_stmt` is relative to. |
| | 111 | /// This is a stack to keep track of inline functions. |
| | 112 | base_line_stack: std.ArrayListUnmanaged(u32) = .{}, |
| | 113 | |
| 110 | /// If `gen` returned `Error.CodegenFail`, this contains an explanatory message. | 114 | /// If `gen` returned `Error.CodegenFail`, this contains an explanatory message. |
| 111 | /// Memory is owned by `module.gpa`. | 115 | /// Memory is owned by `module.gpa`. |
| 112 | error_msg: ?*Module.ErrorMsg, | 116 | error_msg: ?*Module.ErrorMsg, |
| ... | @@ -205,6 +209,7 @@ pub const DeclGen = struct { | ... | @@ -205,6 +209,7 @@ pub const DeclGen = struct { |
| 205 | self.blocks.clearRetainingCapacity(); | 209 | self.blocks.clearRetainingCapacity(); |
| 206 | self.current_block_label_id = undefined; | 210 | self.current_block_label_id = undefined; |
| 207 | self.func.reset(); | 211 | self.func.reset(); |
| | 212 | self.base_line_stack.items.len = 0; |
| 208 | self.error_msg = null; | 213 | self.error_msg = null; |
| 209 | | 214 | |
| 210 | self.genDecl() catch |err| switch (err) { | 215 | self.genDecl() catch |err| switch (err) { |
| ... | @@ -229,6 +234,7 @@ pub const DeclGen = struct { | ... | @@ -229,6 +234,7 @@ pub const DeclGen = struct { |
| 229 | self.type_map.deinit(self.gpa); | 234 | self.type_map.deinit(self.gpa); |
| 230 | self.blocks.deinit(self.gpa); | 235 | self.blocks.deinit(self.gpa); |
| 231 | self.func.deinit(self.gpa); | 236 | self.func.deinit(self.gpa); |
| | 237 | self.base_line_stack.deinit(self.gpa); |
| 232 | } | 238 | } |
| 233 | | 239 | |
| 234 | /// Return the target which we are currently compiling for. | 240 | /// Return the target which we are currently compiling for. |
| ... | @@ -1422,6 +1428,8 @@ pub const DeclGen = struct { | ... | @@ -1422,6 +1428,8 @@ pub const DeclGen = struct { |
| 1422 | | 1428 | |
| 1423 | const decl_id = self.spv.declPtr(spv_decl_index).result_id; | 1429 | const decl_id = self.spv.declPtr(spv_decl_index).result_id; |
| 1424 | | 1430 | |
| | 1431 | try self.base_line_stack.append(self.gpa, decl.src_line); |
| | 1432 | |
| 1425 | if (decl.val.getFunction(mod)) |_| { | 1433 | if (decl.val.getFunction(mod)) |_| { |
| 1426 | assert(decl.ty.zigTypeTag(mod) == .Fn); | 1434 | assert(decl.ty.zigTypeTag(mod) == .Fn); |
| 1427 | const prototype_id = try self.resolveTypeId(decl.ty); | 1435 | const prototype_id = try self.resolveTypeId(decl.ty); |
| ... | @@ -1739,7 +1747,6 @@ pub const DeclGen = struct { | ... | @@ -1739,7 +1747,6 @@ pub const DeclGen = struct { |
| 1739 | .br => return self.airBr(inst), | 1747 | .br => return self.airBr(inst), |
| 1740 | .breakpoint => return, | 1748 | .breakpoint => return, |
| 1741 | .cond_br => return self.airCondBr(inst), | 1749 | .cond_br => return self.airCondBr(inst), |
| 1742 | .dbg_stmt => return self.airDbgStmt(inst), | | |
| 1743 | .loop => return self.airLoop(inst), | 1750 | .loop => return self.airLoop(inst), |
| 1744 | .ret => return self.airRet(inst), | 1751 | .ret => return self.airRet(inst), |
| 1745 | .ret_load => return self.airRetLoad(inst), | 1752 | .ret_load => return self.airRetLoad(inst), |
| ... | @@ -1747,6 +1754,14 @@ pub const DeclGen = struct { | ... | @@ -1747,6 +1754,14 @@ pub const DeclGen = struct { |
| 1747 | .switch_br => return self.airSwitchBr(inst), | 1754 | .switch_br => return self.airSwitchBr(inst), |
| 1748 | .unreach, .trap => return self.airUnreach(), | 1755 | .unreach, .trap => return self.airUnreach(), |
| 1749 | | 1756 | |
| | 1757 | .dbg_stmt => return self.airDbgStmt(inst), |
| | 1758 | .dbg_inline_begin => return self.airDbgInlineBegin(inst), |
| | 1759 | .dbg_inline_end => return self.airDbgInlineEnd(inst), |
| | 1760 | .dbg_var_ptr => return, |
| | 1761 | .dbg_var_val => return, |
| | 1762 | .dbg_block_begin => return, |
| | 1763 | .dbg_block_end => return, |
| | 1764 | |
| 1750 | .unwrap_errunion_err => try self.airErrUnionErr(inst), | 1765 | .unwrap_errunion_err => try self.airErrUnionErr(inst), |
| 1751 | .unwrap_errunion_payload => try self.airErrUnionPayload(inst), | 1766 | .unwrap_errunion_payload => try self.airErrUnionPayload(inst), |
| 1752 | .wrap_errunion_err => try self.airWrapErrUnionErr(inst), | 1767 | .wrap_errunion_err => try self.airWrapErrUnionErr(inst), |
| ... | @@ -1766,13 +1781,6 @@ pub const DeclGen = struct { | ... | @@ -1766,13 +1781,6 @@ pub const DeclGen = struct { |
| 1766 | .call_always_tail => try self.airCall(inst, .always_tail), | 1781 | .call_always_tail => try self.airCall(inst, .always_tail), |
| 1767 | .call_never_tail => try self.airCall(inst, .never_tail), | 1782 | .call_never_tail => try self.airCall(inst, .never_tail), |
| 1768 | .call_never_inline => try self.airCall(inst, .never_inline), | 1783 | .call_never_inline => try self.airCall(inst, .never_inline), |
| 1769 | | | |
| 1770 | .dbg_inline_begin => return, | | |
| 1771 | .dbg_inline_end => return, | | |
| 1772 | .dbg_var_ptr => return, | | |
| 1773 | .dbg_var_val => return, | | |
| 1774 | .dbg_block_begin => return, | | |
| 1775 | .dbg_block_end => return, | | |
| 1776 | // zig fmt: on | 1784 | // zig fmt: on |
| 1777 | | 1785 | |
| 1778 | else => |tag| return self.todo("implement AIR tag {s}", .{@tagName(tag)}), | 1786 | else => |tag| return self.todo("implement AIR tag {s}", .{@tagName(tag)}), |
| ... | @@ -3053,19 +3061,6 @@ pub const DeclGen = struct { | ... | @@ -3053,19 +3061,6 @@ pub const DeclGen = struct { |
| 3053 | try self.genBody(else_body); | 3061 | try self.genBody(else_body); |
| 3054 | } | 3062 | } |
| 3055 | | 3063 | |
| 3056 | fn airDbgStmt(self: *DeclGen, inst: Air.Inst.Index) !void { | | |
| 3057 | const dbg_stmt = self.air.instructions.items(.data)[inst].dbg_stmt; | | |
| 3058 | const src_fname_id = try self.spv.resolveSourceFileName( | | |
| 3059 | self.module, | | |
| 3060 | self.module.declPtr(self.decl_index), | | |
| 3061 | ); | | |
| 3062 | try self.func.body.emit(self.spv.gpa, .OpLine, .{ | | |
| 3063 | .file = src_fname_id, | | |
| 3064 | .line = dbg_stmt.line, | | |
| 3065 | .column = dbg_stmt.column, | | |
| 3066 | }); | | |
| 3067 | } | | |
| 3068 | | | |
| 3069 | fn airLoad(self: *DeclGen, inst: Air.Inst.Index) !?IdRef { | 3064 | fn airLoad(self: *DeclGen, inst: Air.Inst.Index) !?IdRef { |
| 3070 | const mod = self.module; | 3065 | const mod = self.module; |
| 3071 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 3066 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; |
| ... | @@ -3535,6 +3530,33 @@ pub const DeclGen = struct { | ... | @@ -3535,6 +3530,33 @@ pub const DeclGen = struct { |
| 3535 | try self.func.body.emit(self.spv.gpa, .OpUnreachable, {}); | 3530 | try self.func.body.emit(self.spv.gpa, .OpUnreachable, {}); |
| 3536 | } | 3531 | } |
| 3537 | | 3532 | |
| | 3533 | fn airDbgStmt(self: *DeclGen, inst: Air.Inst.Index) !void { |
| | 3534 | const dbg_stmt = self.air.instructions.items(.data)[inst].dbg_stmt; |
| | 3535 | const src_fname_id = try self.spv.resolveSourceFileName( |
| | 3536 | self.module, |
| | 3537 | self.module.declPtr(self.decl_index), |
| | 3538 | ); |
| | 3539 | const base_line = self.base_line_stack.getLast(); |
| | 3540 | try self.func.body.emit(self.spv.gpa, .OpLine, .{ |
| | 3541 | .file = src_fname_id, |
| | 3542 | .line = base_line + dbg_stmt.line + 1, |
| | 3543 | .column = dbg_stmt.column + 1, |
| | 3544 | }); |
| | 3545 | } |
| | 3546 | |
| | 3547 | fn airDbgInlineBegin(self: *DeclGen, inst: Air.Inst.Index) !void { |
| | 3548 | const mod = self.module; |
| | 3549 | const fn_ty = self.air.instructions.items(.data)[inst].ty_fn; |
| | 3550 | const decl_index = mod.funcInfo(fn_ty.func).owner_decl; |
| | 3551 | const decl = mod.declPtr(decl_index); |
| | 3552 | try self.base_line_stack.append(self.gpa, decl.src_line); |
| | 3553 | } |
| | 3554 | |
| | 3555 | fn airDbgInlineEnd(self: *DeclGen, inst: Air.Inst.Index) !void { |
| | 3556 | _ = inst; |
| | 3557 | _ = self.base_line_stack.pop(); |
| | 3558 | } |
| | 3559 | |
| 3538 | fn airAssembly(self: *DeclGen, inst: Air.Inst.Index) !?IdRef { | 3560 | fn airAssembly(self: *DeclGen, inst: Air.Inst.Index) !?IdRef { |
| 3539 | const mod = self.module; | 3561 | const mod = self.module; |
| 3540 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; | 3562 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; |