forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIRGenDebugInfo.h
358 lines (307 loc) · 14.3 KB
/
IRGenDebugInfo.h
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
//===--- IRGenDebugInfo.h - Debug Info Support-------------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file defines IR codegen support for debug information.
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_IRGEN_DEBUGINFO_H
#define SWIFT_IRGEN_DEBUGINFO_H
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/PointerUnion.h"
#include "llvm/IR/ValueHandle.h"
#include "llvm/IR/DebugInfo.h"
#include "llvm/IR/DIBuilder.h"
#include "llvm/Support/Allocator.h"
#include "swift/AST/Module.h"
#include "swift/SIL/SILLocation.h"
#include "swift/SIL/SILBasicBlock.h"
#include "DebugTypeInfo.h"
#include "IRBuilder.h"
#include "IRGenFunction.h"
#include "IRGenModule.h"
#include "GenType.h"
#include <set>
namespace llvm {
class DIBuilder;
}
namespace swift {
class ASTContext;
class AllocStackInst;
class ClangImporter;
class IRGenOptions;
class SILArgument;
class SILDebugScope;
class SILModule;
enum class SILFunctionTypeRepresentation : uint8_t;
namespace irgen {
class IRGenFunction;
typedef struct {
unsigned Line, Col;
const char *Filename;
} Location;
typedef struct { Location LocForLinetable, Loc; } FullLocation;
typedef llvm::DenseMap<const llvm::MDString *, llvm::TrackingMDNodeRef>
TrackingDIRefMap;
enum IndirectionKind : bool { DirectValue = false, IndirectValue = true };
enum ArtificialKind : bool { RealValue = false, ArtificialValue = true };
/// IRGenDebugInfo - Helper object that keeps track of the current
/// CompileUnit, File, LexicalScope, and translates SILLocations into
/// <llvm::DebugLoc>s.
class IRGenDebugInfo {
friend class ArtificialLocation;
const IRGenOptions &Opts;
ClangImporter &CI;
SourceManager &SM;
llvm::Module &M;
llvm::DIBuilder DBuilder;
IRGenModule &IGM;
// Various caches.
llvm::DenseMap<const SILDebugScope *, llvm::TrackingMDNodeRef> ScopeCache;
llvm::DenseMap<const char *, llvm::TrackingMDNodeRef> DIFileCache;
llvm::DenseMap<TypeBase *, llvm::TrackingMDNodeRef> DITypeCache;
llvm::StringMap<llvm::TrackingMDNodeRef> DIModuleCache;
TrackingDIRefMap DIRefMap;
llvm::SmallPtrSet<const llvm::DIType *, 16> IndirectEnumCases;
llvm::BumpPtrAllocator DebugInfoNames;
StringRef CWDName; /// The current working directory.
llvm::DICompileUnit *TheCU = nullptr; /// The current compilation unit.
llvm::DIFile *MainFile = nullptr; /// The main file.
llvm::DIModule *MainModule = nullptr; /// The current module.
llvm::MDNode *EntryPointFn; /// Scope of SWIFT_ENTRY_POINT_FUNCTION.
TypeAliasDecl *MetadataTypeDecl; /// The type decl for swift.type.
llvm::DIType *InternalType; /// Catch-all type for opaque internal types.
Location LastDebugLoc; /// The last location that was emitted.
const SILDebugScope *LastScope; /// The scope of that last location.
bool IsLibrary; /// Whether this is a libary or a top level module.
#ifndef NDEBUG
/// The basic block where the location was last changed.
llvm::BasicBlock *LastBasicBlock;
bool lineNumberIsSane(IRBuilder &Builder, unsigned Line);
#endif
/// Used by pushLoc.
SmallVector<std::pair<Location, const SILDebugScope *>, 8> LocationStack;
// FIXME: move this to something more local in type generation.
CanGenericSignature CurGenerics;
class GenericsRAII {
IRGenDebugInfo &Self;
GenericContextScope Scope;
CanGenericSignature OldGenerics;
public:
GenericsRAII(IRGenDebugInfo &self, CanGenericSignature generics)
: Self(self), Scope(self.IGM, generics), OldGenerics(self.CurGenerics) {
if (generics) self.CurGenerics = generics;
}
~GenericsRAII() {
Self.CurGenerics = OldGenerics;
}
};
public:
IRGenDebugInfo(const IRGenOptions &Opts, ClangImporter &CI, IRGenModule &IGM,
llvm::Module &M, SourceFile *SF);
/// Finalize the llvm::DIBuilder owned by this object.
void finalize();
/// Update the IRBuilder's current debug location to the location
/// Loc and the lexical scope DS.
void setCurrentLoc(IRBuilder &Builder, const SILDebugScope *DS,
Optional<SILLocation> Loc = None);
void clearLoc(IRBuilder &Builder) {
LastDebugLoc = {};
LastScope = nullptr;
Builder.SetCurrentDebugLocation(llvm::DebugLoc());
}
/// Push the current debug location onto a stack and initialize the
/// IRBuilder to an empty location.
void pushLoc() {
LocationStack.push_back(std::make_pair(LastDebugLoc, LastScope));
LastDebugLoc = {};
LastScope = nullptr;
}
/// Restore the current debug location from the stack.
void popLoc() {
std::tie(LastDebugLoc, LastScope) = LocationStack.pop_back_val();
}
/// Emit the final line 0 location for the unified trap block at the
/// end of the function.
void setArtificialTrapLocation(IRBuilder &Builder, const SILDebugScope *Scope) {
auto DL = llvm::DebugLoc::get(0, 0, getOrCreateScope(Scope));
Builder.SetCurrentDebugLocation(DL);
}
/// Emit debug info for an import declaration.
void emitImport(ImportDecl *D);
/// Emit debug info for the given function.
/// \param DS The parent scope of the function.
/// \param Fn The IR representation of the function.
/// \param Rep The calling convention of the function.
/// \param Ty The signature of the function.
llvm::DISubprogram *emitFunction(SILModule &SILMod, const SILDebugScope *DS,
llvm::Function *Fn,
SILFunctionTypeRepresentation Rep,
SILType Ty, DeclContext *DeclCtx = nullptr);
/// Emit debug info for a given SIL function.
llvm::DISubprogram *emitFunction(SILFunction &SILFn, llvm::Function *Fn);
/// Convenience function useful for functions without any source
/// location. Internally calls emitFunction, emits a debug
/// scope, and finally sets it using setCurrentLoc.
inline void emitArtificialFunction(IRGenFunction &IGF, llvm::Function *Fn,
SILType SILTy = SILType()) {
emitArtificialFunction(*IGF.IGM.SILMod, IGF.Builder, Fn, SILTy);
}
void emitArtificialFunction(SILModule &SILMod, IRBuilder &Builder,
llvm::Function *Fn, SILType SILTy = SILType());
/// Emit a dbg.declare instrinsic at the current insertion point and
/// the Builder's current debug location.
void emitVariableDeclaration(IRBuilder &Builder,
ArrayRef<llvm::Value *> Storage,
DebugTypeInfo Ty, const SILDebugScope *DS,
StringRef Name, unsigned ArgNo = 0,
IndirectionKind = DirectValue,
ArtificialKind = RealValue);
/// Convenience function for stack-allocated variables. Calls
/// emitVariableDeclaration internally.
void emitStackVariableDeclaration(IRBuilder &Builder,
ArrayRef<llvm::Value *> Storage,
DebugTypeInfo Ty, const SILDebugScope *DS,
StringRef Name,
IndirectionKind Indirection = DirectValue);
/// Convenience function for variables that are function arguments.
void emitArgVariableDeclaration(IRBuilder &Builder,
ArrayRef<llvm::Value *> Storage,
DebugTypeInfo Ty, const SILDebugScope *DS,
StringRef Name, unsigned ArgNo,
IndirectionKind = DirectValue,
ArtificialKind = RealValue);
/// Emit a dbg.declare or dbg.value intrinsic, depending on Storage.
void emitDbgIntrinsic(llvm::BasicBlock *BB, llvm::Value *Storage,
llvm::DILocalVariable *Var, llvm::DIExpression *Expr,
unsigned Line, unsigned Col, llvm::DILocalScope *Scope,
const SILDebugScope *DS);
/// Create debug metadata for a global variable.
void emitGlobalVariableDeclaration(llvm::GlobalValue *Storage, StringRef Name,
StringRef LinkageName,
DebugTypeInfo DebugType,
Optional<SILLocation> Loc);
/// Emit debug metadata for type metadata (for generic types). So meta.
void emitTypeMetadata(IRGenFunction &IGF, llvm::Value *Metadata,
StringRef Name);
/// Return the DIBuilder.
llvm::DIBuilder &getBuilder() { return DBuilder; }
/// Removes the function from the Functions map again.
void eraseFunction(llvm::Function *Fn);
private:
StringRef BumpAllocatedString(const char *Data, size_t Length);
StringRef BumpAllocatedString(std::string S);
StringRef BumpAllocatedString(StringRef S);
llvm::DIType *createType(DebugTypeInfo DbgTy, StringRef MangledName,
llvm::DIScope *Scope, llvm::DIFile *File);
llvm::DIType *getOrCreateType(DebugTypeInfo DbgTy);
llvm::DIScope *getOrCreateScope(const SILDebugScope *DS);
llvm::DIScope *getOrCreateContext(DeclContext *DC);
llvm::MDNode *createInlinedAt(const SILDebugScope *Scope);
llvm::DIFile *getOrCreateFile(const char *Filename);
llvm::DIType *getOrCreateDesugaredType(Type Ty, DebugTypeInfo DTI);
StringRef getName(const FuncDecl &FD);
StringRef getName(SILLocation L);
StringRef getMangledName(TypeAliasDecl *Decl);
StringRef getMangledName(DebugTypeInfo DTI);
llvm::DITypeRefArray createParameterTypes(CanSILFunctionType FnTy,
DeclContext *DeclCtx);
llvm::DITypeRefArray createParameterTypes(SILType SILTy,
DeclContext *DeclCtx);
void createParameterType(llvm::SmallVectorImpl<llvm::Metadata *> &Parameters,
SILType CanTy, DeclContext *DeclCtx);
llvm::DINodeArray getTupleElements(TupleType *TupleTy, llvm::DIScope *Scope,
llvm::DIFile *File, unsigned Flags,
DeclContext *DeclContext,
unsigned &SizeInBits);
llvm::DIFile *getFile(llvm::DIScope *Scope);
llvm::DIModule *getOrCreateModule(ModuleDecl::ImportedModule M);
llvm::DIModule *getOrCreateModule(StringRef Key, llvm::DIScope *Parent,
StringRef Name, StringRef IncludePath);
llvm::DIScope *getModule(StringRef MangledName);
llvm::DINodeArray getStructMembers(NominalTypeDecl *D, Type BaseTy,
llvm::DIScope *Scope, llvm::DIFile *File,
unsigned Flags, unsigned &SizeInBits);
llvm::DICompositeType *
createStructType(DebugTypeInfo DbgTy, NominalTypeDecl *Decl, Type BaseTy,
llvm::DIScope *Scope, llvm::DIFile *File, unsigned Line,
unsigned SizeInBits, unsigned AlignInBits, unsigned Flags,
llvm::DIType *DerivedFrom, unsigned RuntimeLang,
StringRef UniqueID);
llvm::DIDerivedType *createMemberType(DebugTypeInfo DTI, StringRef Name,
unsigned &OffsetInBits,
llvm::DIScope *Scope,
llvm::DIFile *File, unsigned Flags);
llvm::DINodeArray getEnumElements(DebugTypeInfo DbgTy, EnumDecl *D,
llvm::DIScope *Scope, llvm::DIFile *File,
unsigned Flags);
llvm::DICompositeType *createEnumType(DebugTypeInfo DbgTy, EnumDecl *Decl,
StringRef MangledName,
llvm::DIScope *Scope,
llvm::DIFile *File, unsigned Line,
unsigned Flags);
llvm::DIType *createPointerSizedStruct(llvm::DIScope *Scope, StringRef Name,
llvm::DIFile *File, unsigned Line,
unsigned Flags, StringRef MangledName);
llvm::DIType *createPointerSizedStruct(llvm::DIScope *Scope, StringRef Name,
llvm::DIType *PointeeTy,
llvm::DIFile *File, unsigned Line,
unsigned Flags, StringRef MangledName);
uint64_t getSizeOfBasicType(DebugTypeInfo DbgTy);
TypeAliasDecl *getMetadataType();
};
/// \brief An RAII object that autorestores the debug location.
class AutoRestoreLocation {
IRGenDebugInfo *DI;
public:
AutoRestoreLocation(IRGenDebugInfo *DI) : DI(DI) {
if (DI)
DI->pushLoc();
}
/// \brief Autorestore everything back to normal.
~AutoRestoreLocation() {
if (DI)
DI->popLoc();
}
};
/// \brief An RAII object that temporarily switches to
/// an artificial debug location that has a valid scope, but no line
/// information. This is useful when emitting compiler-generated
/// instructions (e.g., ARC-inserted calls to release()) that have no
/// source location associated with them. The DWARF specification
/// allows the compiler to use the special line number 0 to indicate
/// code that can not be attributed to any source location.
class ArtificialLocation : public AutoRestoreLocation {
public:
/// \brief Set the current location to line 0, but within scope DS.
ArtificialLocation(const SILDebugScope *DS, IRGenDebugInfo *DI,
IRBuilder &Builder)
: AutoRestoreLocation(DI) {
if (DI) {
auto DL = llvm::DebugLoc::get(0, 0, DI->getOrCreateScope(DS));
Builder.SetCurrentDebugLocation(DL);
}
}
};
/// \brief An RAII object that temporarily switches to an
/// empty location. This is how the function prologue is represented.
class PrologueLocation : public AutoRestoreLocation {
public:
/// \brief Set the current location to an empty location.
PrologueLocation(IRGenDebugInfo *DI, IRBuilder &Builder)
: AutoRestoreLocation(DI) {
if (DI)
DI->clearLoc(Builder);
}
};
} // irgen
} // swift
#endif