Skip to content
← Back

src/ir/llvm/llvm_debug_info.ghul

1
namespace IR.LLVMTarget is
2
use IR.Program
3
4
use Collections.MAP
5
6
use LLVM.Metadata
7
use LLVM.MetadataNode
8
use LLVM.MODULE
9
10
/// The source-level debug information a module built with `--debug`
11
/// carries: one compile unit, a file for each source file, a subprogram
12
/// for each function, against which every instruction's line and column
13
/// are recorded, and a variable for each argument and named local, so a
14
/// debugger and a crash's backtrace name ghūl files, lines and variables.
15
/// A scalar is described by its kind, a reference to an instance of a
16
/// class as a pointer to a structure of its fields, and anything else as
17
/// a pointer.
18
class LLVM_DEBUG_INFO is
19
_module: MODULE
20
_files: MAP[string, Metadata]
21
_unit: Metadata?
22
_subroutine_type: Metadata?
23
_scalar_types: MAP[Scalar, Metadata]
24
_pointer_type: Metadata?
25
_class_types: MAP[CLASS_INSTANCE, Metadata]
26
27
/// The program's classes, whose fields a pointer to an object is
28
/// described with once they are laid out.
29
objects: LLVM_OBJECTS? public
30
31
/// The language the compile unit is recorded as, until DWARF has an
32
/// identifier for ghūl.
33
LANGUAGE: string static => "DW_LANG_C"
34
35
/// - module: where the debug information is added
36
init(module: MODULE) is
37
_module = module
38
_files = MAP[string, Metadata]()
39
_scalar_types = MAP[Scalar, Metadata]()
40
_class_types = MAP[CLASS_INSTANCE, Metadata]()
41
si
42
43
/// The subprogram for a function, named `name`, starting at
44
/// `location`, which every location in its body is scoped to. The
45
/// function carries it as its `!dbg` attachment.
46
///
47
/// - function: the compiled function
48
/// - name: the ghūl name the debugger shows for it
49
/// - location: where the function is declared
50
subprogram(function: LLVM.FUNCTION, name: string, location: Source.LOCATION) -> Metadata is
51
let file = _file(location)
52
let line = _line(location)
53
54
let subprogram =
55
_module.add_metadata(
56
MetadataNode.SUBPROGRAM(name, function.name, file, file, line, _type(), line, true, _compile_unit(location)),
57
true)
58
59
function.attach("dbg", subprogram)
60
61
return subprogram
62
si
63
64
/// A location within `scope`, a subprogram, at `location`'s first
65
/// line and column.
66
location(location: Source.LOCATION, scope: Metadata) -> Metadata =>
67
_module.add_metadata(MetadataNode.LOCATION(_line(location), location.start_column, scope, null), false)
68
69
/// The source variable `name`, an argument where `argument` is its
70
/// position from 1 and a local where it is 0, declared at `location`
71
/// in `scope`, holding a value of `type`.
72
variable(name: string, argument: int, scope: Metadata, location: Source.LOCATION, type: Semantic.Types.Type?) -> Metadata =>
73
_module.add_metadata(MetadataNode.LOCAL_VARIABLE(name, argument, scope, _file(location), _line(location), _type_of(type)), false)
74
75
// A scalar is described by its kind, as the lowering holds it, and a
76
// reference to an instance of a class by the class's fields; any
77
// other value is a pointer to nothing a debugger can show.
78
_type_of(type: Semantic.Types.Type?) -> Metadata is
79
if let o = objects /\ o.is_built, c = o.class_of(type) /\ o.is_object_type(c) /\ !o.is_string(c) /\ !o.is_array(c) then
80
return _module.add_metadata(MetadataNode.POINTER_TYPE(_structure(o, c), 64), false)
81
fi
82
83
if let scalar = LLVM_TYPES.scalar(type) then
84
let found: Metadata mut
85
86
if _scalar_types.try_get_value(scalar, found ref) then
87
return found
88
fi
89
90
let stored = LLVM_TYPES.stored_type(scalar)
91
let size = if LLVM_TYPES.width(stored) > 0 then LLVM_TYPES.width(stored) elif stored =~ LLVM.Type.FLOAT then 32 else 64 fi
92
93
let node = _module.add_metadata(MetadataNode.BASIC_TYPE(_scalar_name(scalar), size, _encoding(scalar)), false)
94
95
_scalar_types[scalar] = node
96
97
return node
98
fi
99
100
if let pointer = _pointer_type then
101
return pointer
102
fi
103
104
let pointer = _module.add_metadata(MetadataNode.POINTER_TYPE(Metadata.NULL, 64), false)
105
106
_pointer_type = pointer
107
108
return pointer
109
si
110
111
// A class's instance as a structure of its fields, each at the offset
112
// the layout gives it. The descriptor that starts every instance is
113
// left out. The structure is made before its members, so a field
114
// holding the class itself refers back to it.
115
_structure(objects: LLVM_OBJECTS, type: CLASS_INSTANCE) -> Metadata is
116
let found: Metadata mut
117
118
if _class_types.try_get_value(type, found ref) then
119
return found
120
fi
121
122
let location = type.definition.location
123
let file = _file(location)
124
let structure = _module.add_metadata(MetadataNode.TUPLE([]), false)
125
126
_class_types[type] = structure
127
128
let layout = cast LLVM.Type.STRUCT(objects.struct_type(type))
129
let members = Collections.LIST[Metadata]()
130
131
for (`field, field_type, position) in objects.fields_of(type) do
132
let held = layout.fields[position]
133
let name = if `field.name.starts_with('$') then `field.name.substring(1) else `field.name fi
134
let declared = _declared_at(`field, name, type)
135
136
members.add(
137
_module.add_metadata(
138
MetadataNode.MEMBER(
139
name,
140
_file(declared),
141
_line(declared),
142
_type_of(field_type),
143
LLVM_TYPES.size_of(held) * 8,
144
LLVM_TYPES.offset_of(layout, position) * 8),
145
false))
146
od
147
148
let elements = _module.add_metadata(MetadataNode.TUPLE(members.to_array()), false)
149
150
_module.replace_metadata(
151
structure,
152
MetadataNode.STRUCTURE_TYPE(
153
TYPE_NAMES.name_of(type, objects.classes, null, t => LLVM_TYPES.scalar(t), t => LLVM_TYPES.is_string(t)),
154
file,
155
_line(location),
156
objects.size_of(type) * 8,
157
elements))
158
159
return structure
160
si
161
162
// Where `field` is declared: an auto-property's backing field has no
163
// location of its own, so the property named `name` stands for it, or
164
// failing that the class.
165
_declared_at(`field: Semantic.Symbols.Symbol, name: string, type: CLASS_INSTANCE) -> Source.LOCATION is
166
if `field.location != Source.LOCATION.internal then
167
return `field.location
168
fi
169
170
if let property = type.definition.find_direct(name) /\ property.location != Source.LOCATION.internal then
171
return property.location
172
fi
173
174
return type.definition.location
175
si
176
177
_scalar_name(scalar: Scalar) -> string =>
178
case scalar
179
when Scalar.BOOL then "bool"
180
when Scalar.CHAR then "char"
181
when Scalar.INT8 then "byte"
182
when Scalar.UINT8 then "ubyte"
183
when Scalar.INT16 then "short"
184
when Scalar.UINT16 then "ushort"
185
when Scalar.INT32 then "int"
186
when Scalar.UINT32 then "uint"
187
when Scalar.INT64 then "long"
188
when Scalar.UINT64 then "ulong"
189
when Scalar.INT_NATIVE then "word"
190
when Scalar.UINT_NATIVE then "uword"
191
when Scalar.FLOAT32 then "single"
192
when Scalar.FLOAT64 then "double"
193
esac
194
195
_encoding(scalar: Scalar) -> string =>
196
case scalar
197
when Scalar.BOOL then "DW_ATE_boolean"
198
when Scalar.CHAR then "DW_ATE_UTF"
199
when Scalar.FLOAT32, Scalar.FLOAT64 then "DW_ATE_float"
200
else if LLVM_TYPES.is_unsigned(scalar) then "DW_ATE_unsigned" else "DW_ATE_signed" fi
201
esac
202
203
/// Adds the module flags a debugger needs, and lists the compile unit,
204
/// where any function carries debug information.
205
finish() is
206
if let unit = _unit then
207
_module.add_module_flag(LLVM.ModuleFlagBehavior.MAX, "Dwarf Version", LLVM.Values.i32(5))
208
_module.add_module_flag(LLVM.ModuleFlagBehavior.WARNING, "Debug Info Version", LLVM.Values.i32(3))
209
210
_module.named("llvm.dbg.cu").operands.add(unit)
211
fi
212
si
213
214
// The compile unit, recorded against the first file a function is
215
// declared in.
216
_compile_unit(location: Source.LOCATION) -> Metadata is
217
if let unit = _unit then
218
return unit
219
fi
220
221
let unit =
222
_module.add_metadata(
223
MetadataNode.COMPILE_UNIT(LANGUAGE, _file(location), "ghul", false, LLVM.DebugEmission.FULL_DEBUG),
224
true)
225
226
_unit = unit
227
228
return unit
229
si
230
231
_file(location: Source.LOCATION) -> Metadata is
232
let path = IO.Path.get_full_path(location.file_name)
233
let found: Metadata mut
234
235
if _files.try_get_value(path, found ref) then
236
return found
237
fi
238
239
let file =
240
_module.add_metadata(
241
MetadataNode.FILE(IO.Path.get_file_name(path) ?? path, IO.Path.get_directory_name(path) ?? ""),
242
false)
243
244
_files[path] = file
245
246
return file
247
si
248
249
// Every subprogram has the same type: line tables do not record
250
// argument types.
251
_type() -> Metadata is
252
if let type = _subroutine_type then
253
return type
254
fi
255
256
let type =
257
_module.add_metadata(
258
MetadataNode.SUBROUTINE_TYPE(_module.add_metadata(MetadataNode.TUPLE([]), false)),
259
false)
260
261
_subroutine_type = type
262
263
return type
264
si
265
266
_line(location: Source.LOCATION) -> int => if location.start_line > 0 then location.start_line else 1 fi
267
si
268
si