forked from Matway/mpl-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debugWriter.mpl
executable file
·427 lines (360 loc) · 16.2 KB
/
debugWriter.mpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
"debugWriter" module
"control" useModule
addDebugProlog: [
"declare void @llvm.dbg.declare(metadata, metadata, metadata)" toString @processor.@[email protected]
String @processor.@[email protected]
String @processor.@[email protected]
processor.debugInfo.strings.dataSize 1 - @processor.@debugInfo.@cuStringNumber set
indexCodeView: processor.debugInfo.lastId copy;
processor.debugInfo.lastId 1 + @processor.@debugInfo.@lastId set
("!" indexCodeView " = !{i32 2, !\"CodeView\", i32 1}" ) assembleString @processor.@[email protected]
indexDebug: processor.debugInfo.lastId copy;
processor.debugInfo.lastId 1 + @processor.@debugInfo.@lastId set
("!" indexDebug " = !{i32 2, !\"Debug Info Version\", i32 3}") assembleString @processor.@[email protected]
indexPIC: processor.debugInfo.lastId copy;
processor.debugInfo.lastId 1 + @processor.@debugInfo.@lastId set
("!" indexPIC " = !{i32 1, !\"PIC Level\", i32 2}") assembleString @processor.@[email protected]
("!llvm.module.flags = !{!" indexCodeView ", !" indexDebug ", !" indexPIC "}") assembleString processor.debugInfo.strings.dataSize 5 - @processor.@[email protected] set
index: processor.debugInfo.lastId copy;
processor.debugInfo.lastId 1 + @processor.@debugInfo.@lastId set
String @processor.@[email protected]
processor.debugInfo.strings.dataSize 1 - @processor.@debugInfo.@unitStringNumber set
index #for unit
t: addExpression;
t: "cond" makeStringView 8 "DW_ATE_boolean" makeStringView addTypeInfo;
t: "i8" makeStringView 8 "DW_ATE_signed" makeStringView addTypeInfo;
t: "i16" makeStringView 16 "DW_ATE_signed" makeStringView addTypeInfo;
t: "i32" makeStringView 32 "DW_ATE_signed" makeStringView addTypeInfo;
t: "i64" makeStringView 64 "DW_ATE_signed" makeStringView addTypeInfo;
t: "ix" makeStringView processor.options.pointerSize 0ix cast 0 cast "DW_ATE_signed" makeStringView addTypeInfo;
t: "n8" makeStringView 8 "DW_ATE_unsigned" makeStringView addTypeInfo;
t: "n16" makeStringView 16 "DW_ATE_unsigned" makeStringView addTypeInfo;
t: "n32" makeStringView 32 "DW_ATE_unsigned" makeStringView addTypeInfo;
t: "n64" makeStringView 64 "DW_ATE_unsigned" makeStringView addTypeInfo;
t: "nx" makeStringView processor.options.pointerSize 0ix cast 0 cast "DW_ATE_unsigned" makeStringView addTypeInfo;
t: "r32" makeStringView 32 "DW_ATE_float" makeStringView addTypeInfo;
t: "r64" makeStringView 64 "DW_ATE_float" makeStringView addTypeInfo;
t: "string element" makeStringView 8 "DW_ATE_signed_char" makeStringView addTypeInfo;
t: "DW_TAG_pointer_type" makeStringView t processor.options.pointerSize 0ix cast 0 cast addDerivedTypeInfo;
];
addLinkerOptionsDebugInfo: [
index: processor.debugInfo.lastId copy;
processor.debugInfo.lastId 1 + @processor.@debugInfo.@lastId set
processor.options.linkerOptions.getSize 0 > [
("!llvm.linker.options = !{!" index "}") assembleString @processor.@[email protected]
optionsList: String;
("!" index " = !{") @optionsList.catMany
processor.options.linkerOptions [
pair:;
pair.index 0 > [
", " @optionsList.cat
] when
("!\"" pair.value "\"") @optionsList.catMany
] each
"}" @optionsList.cat
@optionsList move @processor.@[email protected]
] [
String @processor.@[email protected]
String @processor.@[email protected]
] if
];
getPlainTypeDebugDeclaration: [
var: getVar;
var.data.getTag VarCond = [processor.debugInfo.unit 2 +] [
var.data.getTag VarInt8 = [processor.debugInfo.unit 3 +] [
var.data.getTag VarInt16 = [processor.debugInfo.unit 4 +] [
var.data.getTag VarInt32 = [processor.debugInfo.unit 5 +] [
var.data.getTag VarInt64 = [processor.debugInfo.unit 6 +] [
var.data.getTag VarIntX = [processor.debugInfo.unit 7 +] [
var.data.getTag VarNat8 = [processor.debugInfo.unit 8 +] [
var.data.getTag VarNat16 = [processor.debugInfo.unit 9 +] [
var.data.getTag VarNat32 = [processor.debugInfo.unit 10 +] [
var.data.getTag VarNat64 = [processor.debugInfo.unit 11 +] [
var.data.getTag VarNatX = [processor.debugInfo.unit 12 +] [
var.data.getTag VarReal32 = [processor.debugInfo.unit 13 +] [
var.data.getTag VarReal64 = [processor.debugInfo.unit 14 +] [
var.data.getTag VarString = [processor.debugInfo.unit 16 +] [
[FALSE] "Unknown plain struct while getting debug declaration index" assert
-1
] if
] if
] if
] if
] if
] if
] if
] if
] if
] if
] if
] if
] if
] if
];
getPointerTypeDebugDeclaration: [
refToVar:;
compileOnce
var: refToVar getVar;
fr: var.mplTypeId @processor.@[email protected];
[fr.success copy] "Pointee has no type debug info!" assert
"DW_TAG_pointer_type" makeStringView fr.value processor.options.pointerSize 0ix cast 0 cast addDerivedTypeInfo
];
addLinkedLib: [
libName:;
fr: libName makeStringView @[email protected];
fr.success not [
@libName TRUE @[email protected]
] when
];
addMemberInfo: [
copy fieldNumber:;
field:;
offset:;
fr: field.refToVar getVar.mplTypeId @processor.@[email protected];
[fr.success copy] "Field has not debug info about type!" assert
fsize: field.refToVar getStorageSize 0ix cast 0 cast;
falignment: field.refToVar getAlignment 0ix cast 0 cast;
offset falignment + 1 - 0n32 cast falignment 1 - 0n32 cast not and 0 cast @offset set
index: processor.debugInfo.lastId copy;
processor.debugInfo.lastId 1 + @processor.@debugInfo.@lastId set
name: field.nameInfo processor.nameInfos.at.name makeStringView;
name "" = [
("!" index " = !DIDerivedType(tag: DW_TAG_member, name: \"f" fieldNumber "\", scope: !" currentNode.funcDbgIndex
", file: !" currentNode.position.fileNumber processor.debugInfo.fileNameIds.at
", line: " currentNode.position.line ", baseType: !" fr.value ", size: " fsize 8 * ", offset: " offset 8 * ")") assembleString
] [
("!" index " = !DIDerivedType(tag: DW_TAG_member, name: \"" name "\", scope: !" currentNode.funcDbgIndex
", file: !" currentNode.position.fileNumber processor.debugInfo.fileNameIds.at
", line: " currentNode.position.line ", baseType: !" fr.value ", size: " fsize 8 * ", offset: " offset 8 * ")") assembleString
] if
@processor.@[email protected]
index currentNode.funcDbgIndex @processor.@[email protected]
offset fsize + @offset set
index
];
getTypeDebugDeclaration: [
refToVar:;
var: refToVar getVar;
refToVar isVirtualType [
[FALSE] "virtual type has not debug declaration" assert
-1
] [
refToVar isPlain [var.data.getTag VarString =] || [
refToVar getPlainTypeDebugDeclaration
] [
var.data.getTag VarRef = [
pointee: VarRef var.data.get;
pointee getPointerTypeDebugDeclaration
] [
var.data.getTag VarImport = [
addFuncSubroutineInfo
] [
var.data.getTag VarStruct = [
struct: VarStruct var.data.get.get;
members: Int32 Array;
f: 0 dynamic;
offset: 0 dynamic;
[
f struct.fields.dataSize < [
field: f struct.fields.at;
field.refToVar isVirtualField not [
memberInfo: @offset field f addMemberInfo;
memberInfo @members.pushBack
] when
f 1 + @f set TRUE
] &&
] loop
index: processor.debugInfo.lastId copy;
processor.debugInfo.lastId 1 + @processor.@debugInfo.@lastId set
newDebugInfo: String;
("!" index " = !{") @newDebugInfo.catMany
f: 0 dynamic;
[
f members.dataSize < [
f 0 > [", " @newDebugInfo.cat] when
"!" @newDebugInfo.cat
f members.at @newDebugInfo.cat
f 1 + @f set TRUE
] &&
] loop
"}" @newDebugInfo.cat
@newDebugInfo move @processor.@[email protected]
index currentNode.funcDbgIndex @processor.@[email protected]
index: processor.debugInfo.lastId copy;
processor.debugInfo.lastId 1 + @processor.@debugInfo.@lastId set
("!" index " = distinct !DICompositeType(tag: DW_TAG_structure_type, file: !" currentNode.position.fileNumber processor.debugInfo.fileNameIds.at
", name: \"" refToVar getDebugType "\", line: " currentNode.position.line ", size: " refToVar getStorageSize 0ix cast 0 cast 8 * ", elements: !" index 1 -
")") assembleString @processor.@[email protected]
index currentNode.funcDbgIndex @processor.@[email protected]
index
] [
[FALSE] "Unknown type in getTypeDebugDeclaration!" assert
-1
] if
] if
] if
] if
] if
];
addVariableDebugInfo: [
refToVar:;
copy nameInfo:;
refToVar isVirtualType not [
fr: refToVar getVar.mplTypeId @processor.@[email protected];
[fr.success copy] "There is no debug declaration for this type!" assert
debugDeclarationIndex: fr.value copy;
index: processor.debugInfo.lastId copy;
processor.debugInfo.lastId 1 + @processor.@debugInfo.@lastId set
("!" index " = !DILocalVariable(name: \"" nameInfo processor.nameInfos.at.name "\", scope: !" currentNode.funcDbgIndex
", file: !" currentNode.position.fileNumber processor.debugInfo.fileNameIds.at
", line: " currentNode.position.line ", type: !" debugDeclarationIndex ")") assembleString @processor.@[email protected]
index currentNode.funcDbgIndex @processor.@[email protected]
index
] [
-1
] if
];
addGlobalVariableDebugInfo: [
refToVar:;
copy nameInfo:;
refToVar isVirtualType not [
fr: refToVar getVar.mplTypeId @processor.@[email protected];
[fr.success copy] "There is no debug declaration for this type!" assert
debugDeclarationIndex: fr.value copy;
index: processor.debugInfo.lastId copy;
processor.debugInfo.lastId 1 + @processor.@debugInfo.@lastId set
("!" index " = !DIGlobalVariableExpression(var: !" index 1 + ", expr: !DIExpression())") assembleString @processor.@[email protected]
index: processor.debugInfo.lastId copy;
processor.debugInfo.lastId 1 + @processor.@debugInfo.@lastId set
("!" index " = !DIGlobalVariable(name: \"" nameInfo processor.nameInfos.at.name "\", linkageName: \"" refToVar getIrName
"\", scope: !" processor.debugInfo.unit ", file: !" currentNode.position.fileNumber processor.debugInfo.fileNameIds.at
", line: " currentNode.position.line ", type: !" debugDeclarationIndex ", isLocal: false, isDefinition: true)") assembleString @processor.@[email protected]
result: index 1 -;
result @processor.@[email protected]
result
] [
-1
] if
];
addVariableMetadata: [
refToVar:;
copy nameInfo:;
refToVar isVirtualType not [
localVariableDebugIndex: nameInfo refToVar addVariableDebugInfo;
(" call void @llvm.dbg.declare(metadata " refToVar getIrType "* " refToVar getIrName ", metadata !" localVariableDebugIndex ", metadata !" processor.debugInfo.unit 1 + ")"
) assembleString makeInstruction @[email protected]
] when
];
addExpression: [
index: processor.debugInfo.lastId copy;
processor.debugInfo.lastId 1 + @processor.@debugInfo.@lastId set
("!" index " = !DIExpression()") assembleString @processor.@[email protected]
index
];
addTypeInfo: [
encoding:;
copy size:;
name:;
index: processor.debugInfo.lastId copy;
processor.debugInfo.lastId 1 + @processor.@debugInfo.@lastId set
("!" index " = !DIBasicType(name: \"" @name "\", size: " size ", encoding: " @encoding ")") assembleString @processor.@[email protected]
index
];
addDerivedTypeInfo: [
copy size:;
copy base:;
tag:;
index: processor.debugInfo.lastId copy;
processor.debugInfo.lastId 1 + @processor.@debugInfo.@lastId set
("!" index " = !DIDerivedType(tag: " @tag ", baseType: !" base ", size: " @size ")") assembleString @processor.@[email protected]
index
];
addFileDebugInfo: [
compileOnce
fileName:;
index: processor.debugInfo.lastId copy;
processor.debugInfo.lastId 1 + @processor.@debugInfo.@lastId set
fullPath: (processor.options.mainPath fileName) assembleString;
onlyPath: onlyFileName: fullPath simplifyPath;;
("!" index " = !DIFile(filename: \"" onlyFileName "\", directory: \"" onlyPath makeStringView getStringImplementation "\")" ) assembleString @processor.@[email protected]
index
];
addFuncSubroutineInfo: [
compileOnce
index: processor.debugInfo.lastId copy;
processor.debugInfo.lastId 1 + @processor.@debugInfo.@lastId set
("!" index " = !{null}") assembleString @processor.@[email protected]
types: index copy;
index: processor.debugInfo.lastId copy;
processor.debugInfo.lastId 1 + @processor.@debugInfo.@lastId set
("!" index " = !DISubroutineType(types: !" types ")") assembleString @processor.@[email protected]
index
];
addFuncDebugInfo: [
compileOnce
copy funcDebugIndex:;
funcIRName:;
funcName:;
position:;
subroutineIndex: addFuncSubroutineInfo;
funcImplementation: funcName makeStringView getStringImplementation;
(
"!" funcDebugIndex " = distinct !DISubprogram(name: \"" funcImplementation "." funcDebugIndex "\", linkageName: \"" @funcIRName
"\", scope: !" position.fileNumber processor.debugInfo.fileNameIds.at
", file: !" position.fileNumber processor.debugInfo.fileNameIds.at ", line: " position.line ", type: !" subroutineIndex
", scopeLine: " position.line ", unit: !" processor.debugInfo.unit ")") assembleString @processor.@[email protected]
];
addDebugLocation: [
compileOnce
copy funcDbgIndex:;
position:;
index: processor.debugInfo.lastId copy;
processor.debugInfo.lastId 1 + @processor.@debugInfo.@lastId set
("!" index " = !DILocation(line: " position.line ", column: " position.column ", scope: !" funcDbgIndex ")") assembleString @processor.@[email protected]
index funcDbgIndex @processor.@[email protected]
index
];
addDebugReserve: [
index: processor.debugInfo.lastId copy;
processor.debugInfo.lastId 1 + @processor.@debugInfo.@lastId set
String @processor.@[email protected]
index
];
moveLastDebugString: [
copy index:;
processor.debugInfo.strings.last
index 4 + @processor.@[email protected] set
@processor.@[email protected]
];
correctUnitInfo: [
copy lastUnit:;
index: processor.debugInfo.lastId copy;
processor.debugInfo.lastId 1 + @processor.@debugInfo.@lastId set
newDebugInfo: ("!" index " = !{" ) assembleString;
processor.debugInfo.globals [
pair:;
i: pair.index copy;
i 0 > [", " @newDebugInfo.cat] when
"!" @newDebugInfo.cat
pair.value @newDebugInfo.cat
] each
"}" @newDebugInfo.cat
@newDebugInfo move @processor.@[email protected]
globals: index copy;
("!" processor.debugInfo.unit " = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !" lastUnit processor.debugInfo.fileNameIds.at
", producer: \"mpl compiler\", isOptimized: false, runtimeVersion: 2, emissionKind: FullDebug, globals: !" globals ")") assembleString
processor.debugInfo.unitStringNumber @processor.@[email protected] set
("!llvm.dbg.cu = !{!" processor.debugInfo.unit "}") assembleString
processor.debugInfo.cuStringNumber @processor.@[email protected] set
];
clearUnusedDebugInfo: [
processor.debugInfo.locationIds [
pair:;
locId: pair.key;
funcDbgId: pair.value;
debugString: funcDbgId 4 + processor.debugInfo.strings.at;
debugString.getTextSize 0 = [
String locId 4 + @processor.@[email protected] set
] when
] each
];