debug info implementation

This commit is contained in:
David Gonzalez Martin 2024-02-01 21:19:42 +01:00
parent fab2ea5434
commit f96768ece8
4 changed files with 537 additions and 471 deletions

View File

@ -505,7 +505,7 @@ pub const Type = union(enum) {
};
pub const Instruction = union(enum) {
argument_declaration: ArgumentDeclaration,
argument_declaration: *Debug.Declaration.Argument,
block: Debug.Block.Index,
// TODO
call: Instruction.Call,
@ -516,6 +516,7 @@ pub const Instruction = union(enum) {
type: Type.Index,
},
debug_checkpoint: DebugCheckPoint,
debug_declare_local_variable: DebugDeclareLocalVariable,
global: *Debug.Declaration.Global,
inline_assembly: InlineAssembly.Index,
integer_binary_operation: Instruction.IntegerBinaryOperation,
@ -530,6 +531,10 @@ pub const Instruction = union(enum) {
syscall: Syscall,
@"unreachable",
const DebugDeclareLocalVariable = struct{
variable: *Debug.Declaration.Local,
stack: Instruction.Index,
};
const Syscall = struct{
arguments: []const V,
@ -654,12 +659,13 @@ pub const Function = struct{
};
const Struct = struct{
pub const Struct = struct{
fields: ArrayList(Field) = .{},
scope: Debug.Scope.Global,
backing_type: Type.Index,
type: Type.Index,
const Field = struct{
pub const Field = struct{
name: u32,
type: u32,
value: V.Comptime,
@ -780,14 +786,16 @@ pub const Debug = struct{
declaration: *Declaration,
};
const Local = struct{
pub const Local = struct{
scope: Scope,
local_declaration_map: AutoArrayHashMap(*Debug.Declaration.Local, Instruction.Index) = .{},
};
const Global = struct{
pub const Global = struct{
scope: Scope,
};
const Function = struct{
pub const Function = struct{
scope: Scope,
argument_map: AutoArrayHashMap(*Debug.Declaration.Argument, Instruction.Index) = .{},
};
@ -820,7 +828,7 @@ pub const Debug = struct{
}
const Kind = enum{
pub const Kind = enum{
compilation_unit,
file,
file_container,
@ -1138,13 +1146,16 @@ pub const Builder = struct {
fn pushScope(builder: *Builder, unit: *Unit, context: *const Context, new_scope: *Debug.Scope) !void {
const old_scope = builder.current_scope;
assert(@intFromEnum(old_scope.kind) <= @intFromEnum(new_scope.kind));
if (builder.current_basic_block != .null) {
assert(@intFromEnum(old_scope.kind) >= @intFromEnum(Debug.Scope.Kind.function));
const instruction = try unit.instructions.append(context.allocator, .{
.push_scope = .{
.old = old_scope,
.new = new_scope,
},
});
});
try builder.appendInstruction(unit, context, instruction);
}
@ -1156,6 +1167,8 @@ pub const Builder = struct {
const old_scope = builder.current_scope;
const new_scope = old_scope.parent.?;
assert(@intFromEnum(old_scope.kind) >= @intFromEnum(new_scope.kind));
if (builder.current_basic_block != .null) {
const instruction = try unit.instructions.append(context.allocator, .{
.pop_scope = .{
@ -1178,6 +1191,14 @@ pub const Builder = struct {
}
fn analyzeFile(builder: *Builder, unit: *Unit, context: *const Context, file_index: Debug.File.Index) !void {
const old_function = builder.current_function;
builder.current_function = .null;
defer builder.current_function = old_function;
const old_basic_block = builder.current_basic_block;
defer builder.current_basic_block = old_basic_block;
builder.current_basic_block = .null;
const old_scope = builder.current_scope;
builder.current_scope = &unit.scope.scope;
defer builder.current_scope = old_scope;
@ -1471,7 +1492,7 @@ pub const Builder = struct {
const function = unit.function_definitions.get(builder.current_function);
builder.last_check_point = .{};
assert(builder.current_scope.kind == .file_container or builder.current_scope.kind == .file);
try builder.pushScope(unit, context, &function.scope.scope);
defer builder.popScope(unit, context) catch unreachable;
@ -1479,6 +1500,9 @@ pub const Builder = struct {
builder.current_basic_block = entry_basic_block;
defer builder.current_basic_block = .null;
const body_node = unit.getNode(body_node_index);
try builder.insertDebugCheckPoint(unit, context, body_node.token);
// Get argument declarations into scope
const function_prototype_node = unit.getNode(function_prototype_node_index);
if (function_prototype_node.left != .null) {
@ -1516,10 +1540,7 @@ pub const Builder = struct {
try builder.current_scope.declarations.putNoClobber(context.allocator, argument_name_hash, &argument.declaration);
const argument_instruction = try unit.instructions.append(context.allocator, .{
.argument_declaration = .{
.name = argument.declaration.name,
.type = argument.declaration.type,
},
.argument_declaration = argument,
});
try builder.appendInstruction(unit, context, argument_instruction);
@ -1528,8 +1549,6 @@ pub const Builder = struct {
}
}
const body_node = unit.getNode(body_node_index);
if (body_node.id == .block) {
function.body = try builder.resolveBlock(unit, context, body_node_index);
@ -1548,7 +1567,7 @@ pub const Builder = struct {
log(.compilation, .ir, "{}, {}", .{checkpoint.line, checkpoint.column});
},
.argument_declaration => |arg|{
log(.compilation, .ir, "\"{s}\"", .{unit.getIdentifier(arg.name)});
log(.compilation, .ir, "\"{s}\"", .{unit.getIdentifier(arg.declaration.name)});
},
.cast => |cast| {
log(.compilation, .ir, "{s}", .{@tagName(cast.id)});
@ -2056,12 +2075,14 @@ pub const Builder = struct {
},
},
.backing_type = backing_type,
.type = .null,
});
const struct_type = unit.structs.get(struct_index);
const type_index = try unit.types.append(context.allocator, .{
.@"struct" = struct_index,
});
struct_type.type = type_index;
// Save file type
switch (builder.current_scope.kind) {
@ -2641,6 +2662,10 @@ pub const Builder = struct {
});
const block = unit.blocks.get(block_index);
if (builder.current_basic_block != .null) {
assert(builder.current_scope.kind == .block or builder.current_scope.kind == .function);
}
try builder.pushScope(unit, context, &block.scope.scope);
defer builder.popScope(unit, context) catch unreachable;
@ -2681,6 +2706,12 @@ pub const Builder = struct {
std.debug.panic("Identifier '{s}' already declarared on scope", .{identifier});
}
const mutability: Mutability = switch (statement_node.id) {
.constant_symbol_declaration => .@"const",
.variable_symbol_declaration => .@"var",
else => unreachable,
};
const metadata_node_index = statement_node.left;
const value_node_index = statement_node.right;
assert(value_node_index != .null);
@ -2700,16 +2731,12 @@ pub const Builder = struct {
const initialization = try builder.resolveRuntimeValue(unit, context, type_expect, value_node_index, .right);
const emit = !(mutability == .@"const" and initialization.value == .@"comptime");
const declaration_type = switch (type_expect) {
.none => initialization.type,
.type => |type_index| type_index,
};
const mutability: Mutability = switch (statement_node.id) {
.constant_symbol_declaration => .@"const",
.variable_symbol_declaration => .@"var",
else => unreachable,
};
const declaration_index = try unit.local_declarations.append(context.allocator, .{
.declaration = .{
@ -2726,7 +2753,7 @@ pub const Builder = struct {
const local_declaration = unit.local_declarations.get(declaration_index);
try builder.current_scope.declarations.putNoClobber(context.allocator, identifier_hash, &local_declaration.declaration);
if (!(mutability == .@"const" and initialization.value == .@"comptime")) {
if (emit) {
const stack = try unit.instructions.append(context.allocator, .{
.stack_slot = .{
.type = declaration_type,
@ -2736,7 +2763,16 @@ pub const Builder = struct {
try builder.appendInstruction(unit, context, stack);
try block.scope.local_declaration_map.putNoClobber(context.allocator, local_declaration, stack);
const debug_declare_local = try unit.instructions.append(context.allocator, .{
.debug_declare_local_variable = .{
.variable = local_declaration,
.stack = stack,
},
});
try builder.appendInstruction(unit, context, debug_declare_local);
const store = try unit.instructions.append(context.allocator, .{
.store = .{
.destination = .{

View File

@ -96,6 +96,22 @@ extern "C" void NativityLLVMBuilderSetCurrentDebugLocation(IRBuilder<>& builder,
builder.SetCurrentDebugLocation(debug_location);
}
extern "C" DIExpression* NativityLLVMDebugInfoBuilderCreateExpression(DIBuilder& builder, uint64_t* address, size_t length)
{
auto expr = ArrayRef<uint64_t>(address, length);
auto* expression = builder.createExpression(expr);
return expression;
}
extern "C" DIGlobalVariableExpression* NativityLLVMDebugInfoBuilderCreateGlobalVariableExpression(DIBuilder& builder, DIScope* scope, const char* name_ptr, size_t name_len, const char* linkage_name_ptr, size_t linkage_name_len, DIFile* file, unsigned line_number, DIType* type, bool is_local_to_unit, bool is_defined, DIExpression* expression, MDNode* declaration, MDTuple* template_parameters, uint32_t alignment)
{
auto name = StringRef(name_ptr, name_len);
auto linkage_name = StringRef(linkage_name_ptr, linkage_name_len);
auto annotations = nullptr;
auto* global_variable = builder.createGlobalVariableExpression(scope, name, linkage_name, file, line_number, type, is_local_to_unit, is_defined, expression, declaration, template_parameters, alignment, annotations);
return global_variable;
}
extern "C" DILocalVariable* NativityLLVMDebugInfoBuilderCreateParameterVariable(DIBuilder& builder, DIScope* scope, const char* name_ptr, size_t name_len, unsigned argument_index, DIFile* file, unsigned line_number, DIType* type, bool always_preserve, DINode::DIFlags flags)
{
assert(isa<DILocalScope>(scope));

File diff suppressed because it is too large Load Diff

View File

@ -17,9 +17,13 @@ pub extern fn NativityLLVMDebugInfoBuilderCreateCompileUnit(builder: *LLVM.Debug
pub extern fn NativityLLVMDebugInfoBuilderCreateFunction(builder: *LLVM.DebugInfo.Builder, scope: *LLVM.DebugInfo.Scope, name_ptr: [*]const u8, name_len: usize, linkage_name_ptr: [*]const u8, linkage_name_len: usize, file: *LLVM.DebugInfo.File, line_number: c_uint, type: *LLVM.DebugInfo.SubroutineType, scope_line: c_uint, flags: LLVM.DebugInfo.Node.Flags, subprogram_flags: LLVM.DebugInfo.Subprogram.Flags, declaration: ?*LLVM.DebugInfo.Subprogram) ?*LLVM.DebugInfo.Subprogram;
pub extern fn NativityLLVMDebugInfoBuilderCreateSubroutineType(builder: *LLVM.DebugInfo.Builder, parameter_types_ptr: [*]const *LLVM.DebugInfo.Type, parameter_type_count: usize, flags: LLVM.DebugInfo.Node.Flags, calling_convention: LLVM.DebugInfo.CallingConvention) ?*LLVM.DebugInfo.SubroutineType;
pub extern fn NativityLLVMDebugInfoBuilderCreateLexicalBlock(builder: *LLVM.DebugInfo.Builder, parent_scope: *LLVM.DebugInfo.Scope, parent_file: *LLVM.DebugInfo.File, line: c_uint, column: c_uint) ?*LLVM.DebugInfo.LexicalBlock;
pub extern fn NativityLLVMDebugInfoBuilderCreateExpression(builder: *LLVM.DebugInfo.Builder, address: [*]const u64, length: usize) *LLVM.DebugInfo.Expression;
pub extern fn NativityLLVMDebugInfoBuilderCreateGlobalVariableExpression(builder: *LLVM.DebugInfo.Builder, scope: *LLVM.DebugInfo.Scope, name_ptr: [*]const u8, name_len: usize, linkage_name_ptr: [*]const u8, linkage_name_len: usize, file: *LLVM.DebugInfo.File, line_number: c_uint, type: *LLVM.DebugInfo.Type, is_local_to_unit: bool, is_defined: bool, expression: ?*LLVM.DebugInfo.Expression, declaration: ?*LLVM.Metadata.Node, template_parameters: ?*LLVM.Metadata.Tuple, alignment: u32) ?*LLVM.DebugInfo.GlobalVariableExpression;
pub extern fn NativityLLVMDebugInfoBuilderCreateParameterVariable(builder: *LLVM.DebugInfo.Builder, scope: *LLVM.DebugInfo.Scope, name_ptr: [*]const u8, name_len: usize, argument_index: c_uint, file: *LLVM.DebugInfo.File, line_number: c_uint, type: *LLVM.DebugInfo.Type, always_preserve: bool, flags: LLVM.DebugInfo.Node.Flags) ?*LLVM.DebugInfo.LocalVariable;
pub extern fn NativityLLVMDebugInfoBuilderCreateAutoVariable(builder: *LLVM.DebugInfo.Builder, scope: *LLVM.DebugInfo.Scope, name_ptr: [*]const u8, name_len: usize, file: *LLVM.DebugInfo.File, line_number: c_uint, type: *LLVM.DebugInfo.Type, always_preserve: bool, flags: LLVM.DebugInfo.Node.Flags, alignment: u32) ?*LLVM.DebugInfo.LocalVariable; // 0 means 1 << 0 (alignment of 1)
pub extern fn NativityLLVMDebugInfoBuilderInsertDeclare(builder: *LLVM.DebugInfo.Builder, pointer: *LLVM.Value, local_variable: *LLVM.DebugInfo.LocalVariable, context: *LLVM.Context, line: c_uint, column: c_uint, scope: *LLVM.DebugInfo.Scope, basic_block: *LLVM.Value.BasicBlock) ?*LLVM.Value.Instruction;
pub extern fn NativityLLVMDebugInfoBuilderCreateBasicType(builder: *LLVM.DebugInfo.Builder, name_ptr: [*]const u8, name_len: usize, bit_count: u64, dwarf_encoding: LLVM.DebugInfo.AttributeType, flags: LLVM.DebugInfo.Node.Flags) ?*LLVM.DebugInfo.Type;
pub extern fn NativityLLVMDebugInfoBuilderCreatePointerType(builder: *LLVM.DebugInfo.Builder, element_type: *LLVM.DebugInfo.Type, pointer_bit_count: u64, alignment: u32, name_ptr: [*]const u8, name_len: usize) ?*LLVM.DebugInfo.Type.Derived;
pub extern fn NativityLLVMDebugInfoBuilderCreateStructType(builder: *LLVM.DebugInfo.Builder, scope: ?*LLVM.DebugInfo.Scope, name_ptr: [*]const u8, name_len: usize, file: ?*LLVM.DebugInfo.File, line_number: c_uint, bit_count: u64, alignment: u32, flags: LLVM.DebugInfo.Node.Flags, derived_from: ?*LLVM.DebugInfo.Type, element_type_ptr: [*]const *LLVM.DebugInfo.Type, element_type_count: usize) ?*LLVM.DebugInfo.Type.Composite;