blob: f6dfbf6cfa560678964710228b84f20854dea617 [file] [log] [blame]
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +00001//===- DebugInfoMetadata.cpp - Implement debug info metadata --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the debug info Metadata classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/IR/DebugInfoMetadata.h"
15#include "LLVMContextImpl.h"
16#include "MetadataImpl.h"
Duncan P. N. Exon Smithdf523492015-02-18 20:32:57 +000017#include "llvm/IR/Function.h"
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000018
19using namespace llvm;
20
21MDLocation::MDLocation(LLVMContext &C, StorageType Storage, unsigned Line,
22 unsigned Column, ArrayRef<Metadata *> MDs)
23 : MDNode(C, MDLocationKind, Storage, MDs) {
24 assert((MDs.size() == 1 || MDs.size() == 2) &&
25 "Expected a scope and optional inlined-at");
26
27 // Set line and column.
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000028 assert(Column < (1u << 16) && "Expected 16-bit column");
29
30 SubclassData32 = Line;
31 SubclassData16 = Column;
32}
33
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000034static void adjustColumn(unsigned &Column) {
35 // Set to unknown on overflow. We only have 16 bits to play with here.
36 if (Column >= (1u << 16))
37 Column = 0;
38}
39
40MDLocation *MDLocation::getImpl(LLVMContext &Context, unsigned Line,
41 unsigned Column, Metadata *Scope,
42 Metadata *InlinedAt, StorageType Storage,
43 bool ShouldCreate) {
Duncan P. N. Exon Smithaf677eb2015-02-06 22:50:13 +000044 // Fixup column.
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000045 adjustColumn(Column);
46
47 if (Storage == Uniqued) {
48 if (auto *N =
49 getUniqued(Context.pImpl->MDLocations,
50 MDLocationInfo::KeyTy(Line, Column, Scope, InlinedAt)))
51 return N;
52 if (!ShouldCreate)
53 return nullptr;
54 } else {
55 assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
56 }
57
58 SmallVector<Metadata *, 2> Ops;
59 Ops.push_back(Scope);
60 if (InlinedAt)
61 Ops.push_back(InlinedAt);
62 return storeImpl(new (Ops.size())
63 MDLocation(Context, Storage, Line, Column, Ops),
64 Storage, Context.pImpl->MDLocations);
65}
66
Duncan P. N. Exon Smith9146fc82015-02-02 20:01:03 +000067static StringRef getString(const MDString *S) {
68 if (S)
69 return S->getString();
70 return StringRef();
71}
72
Duncan P. N. Exon Smithc7e08132015-02-02 20:20:56 +000073#ifndef NDEBUG
Duncan P. N. Exon Smith9146fc82015-02-02 20:01:03 +000074static bool isCanonical(const MDString *S) {
75 return !S || !S->getString().empty();
Duncan P. N. Exon Smith442ec022015-02-02 19:54:05 +000076}
Duncan P. N. Exon Smithc7e08132015-02-02 20:20:56 +000077#endif
Duncan P. N. Exon Smith442ec022015-02-02 19:54:05 +000078
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000079GenericDebugNode *GenericDebugNode::getImpl(LLVMContext &Context, unsigned Tag,
Duncan P. N. Exon Smith9146fc82015-02-02 20:01:03 +000080 MDString *Header,
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000081 ArrayRef<Metadata *> DwarfOps,
82 StorageType Storage,
83 bool ShouldCreate) {
84 unsigned Hash = 0;
85 if (Storage == Uniqued) {
Duncan P. N. Exon Smith9146fc82015-02-02 20:01:03 +000086 GenericDebugNodeInfo::KeyTy Key(Tag, getString(Header), DwarfOps);
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000087 if (auto *N = getUniqued(Context.pImpl->GenericDebugNodes, Key))
88 return N;
89 if (!ShouldCreate)
90 return nullptr;
91 Hash = Key.getHash();
92 } else {
93 assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
94 }
95
96 // Use a nullptr for empty headers.
Duncan P. N. Exon Smith9146fc82015-02-02 20:01:03 +000097 assert(isCanonical(Header) && "Expected canonical MDString");
98 Metadata *PreOps[] = {Header};
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000099 return storeImpl(new (DwarfOps.size() + 1) GenericDebugNode(
100 Context, Storage, Hash, Tag, PreOps, DwarfOps),
101 Storage, Context.pImpl->GenericDebugNodes);
102}
103
104void GenericDebugNode::recalculateHash() {
105 setHash(GenericDebugNodeInfo::KeyTy::calculateHash(this));
106}
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000107
108#define UNWRAP_ARGS_IMPL(...) __VA_ARGS__
109#define UNWRAP_ARGS(ARGS) UNWRAP_ARGS_IMPL ARGS
110#define DEFINE_GETIMPL_LOOKUP(CLASS, ARGS) \
111 do { \
112 if (Storage == Uniqued) { \
113 if (auto *N = getUniqued(Context.pImpl->CLASS##s, \
114 CLASS##Info::KeyTy(UNWRAP_ARGS(ARGS)))) \
115 return N; \
116 if (!ShouldCreate) \
117 return nullptr; \
118 } else { \
119 assert(ShouldCreate && \
120 "Expected non-uniqued nodes to always be created"); \
121 } \
122 } while (false)
123#define DEFINE_GETIMPL_STORE(CLASS, ARGS, OPS) \
124 return storeImpl(new (ArrayRef<Metadata *>(OPS).size()) \
125 CLASS(Context, Storage, UNWRAP_ARGS(ARGS), OPS), \
126 Storage, Context.pImpl->CLASS##s)
127#define DEFINE_GETIMPL_STORE_NO_OPS(CLASS, ARGS) \
128 return storeImpl(new (0u) CLASS(Context, Storage, UNWRAP_ARGS(ARGS)), \
129 Storage, Context.pImpl->CLASS##s)
Duncan P. N. Exon Smithbd33d372015-02-10 01:59:57 +0000130#define DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(CLASS, OPS) \
131 return storeImpl(new (ArrayRef<Metadata *>(OPS).size()) \
132 CLASS(Context, Storage, OPS), \
133 Storage, Context.pImpl->CLASS##s)
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000134
135MDSubrange *MDSubrange::getImpl(LLVMContext &Context, int64_t Count, int64_t Lo,
136 StorageType Storage, bool ShouldCreate) {
137 DEFINE_GETIMPL_LOOKUP(MDSubrange, (Count, Lo));
138 DEFINE_GETIMPL_STORE_NO_OPS(MDSubrange, (Count, Lo));
139}
140
141MDEnumerator *MDEnumerator::getImpl(LLVMContext &Context, int64_t Value,
142 MDString *Name, StorageType Storage,
143 bool ShouldCreate) {
144 assert(isCanonical(Name) && "Expected canonical MDString");
145 DEFINE_GETIMPL_LOOKUP(MDEnumerator, (Value, getString(Name)));
146 Metadata *Ops[] = {Name};
147 DEFINE_GETIMPL_STORE(MDEnumerator, (Value), Ops);
148}
149
150MDBasicType *MDBasicType::getImpl(LLVMContext &Context, unsigned Tag,
151 MDString *Name, unsigned SizeInBits,
152 unsigned AlignInBits, unsigned Encoding,
153 StorageType Storage, bool ShouldCreate) {
154 assert(isCanonical(Name) && "Expected canonical MDString");
155 DEFINE_GETIMPL_LOOKUP(
156 MDBasicType, (Tag, getString(Name), SizeInBits, AlignInBits, Encoding));
157 Metadata *Ops[] = {nullptr, nullptr, Name};
158 DEFINE_GETIMPL_STORE(MDBasicType, (Tag, SizeInBits, AlignInBits, Encoding),
159 Ops);
160}
161
162MDDerivedType *MDDerivedType::getImpl(
163 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
164 unsigned Line, Metadata *Scope, Metadata *BaseType, unsigned SizeInBits,
165 unsigned AlignInBits, unsigned OffsetInBits, unsigned Flags,
166 Metadata *ExtraData, StorageType Storage, bool ShouldCreate) {
167 assert(isCanonical(Name) && "Expected canonical MDString");
168 DEFINE_GETIMPL_LOOKUP(MDDerivedType, (Tag, getString(Name), File, Line, Scope,
169 BaseType, SizeInBits, AlignInBits,
170 OffsetInBits, Flags, ExtraData));
171 Metadata *Ops[] = {File, Scope, Name, BaseType, ExtraData};
172 DEFINE_GETIMPL_STORE(
173 MDDerivedType, (Tag, Line, SizeInBits, AlignInBits, OffsetInBits, Flags),
174 Ops);
175}
176
177MDCompositeType *MDCompositeType::getImpl(
178 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
179 unsigned Line, Metadata *Scope, Metadata *BaseType, unsigned SizeInBits,
180 unsigned AlignInBits, unsigned OffsetInBits, unsigned Flags,
181 Metadata *Elements, unsigned RuntimeLang, Metadata *VTableHolder,
182 Metadata *TemplateParams, MDString *Identifier, StorageType Storage,
183 bool ShouldCreate) {
184 assert(isCanonical(Name) && "Expected canonical MDString");
185 DEFINE_GETIMPL_LOOKUP(MDCompositeType,
186 (Tag, getString(Name), File, Line, Scope, BaseType,
187 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
188 RuntimeLang, VTableHolder, TemplateParams,
189 getString(Identifier)));
190 Metadata *Ops[] = {File, Scope, Name, BaseType,
191 Elements, VTableHolder, TemplateParams, Identifier};
192 DEFINE_GETIMPL_STORE(MDCompositeType, (Tag, Line, RuntimeLang, SizeInBits,
193 AlignInBits, OffsetInBits, Flags),
194 Ops);
195}
196
197MDSubroutineType *MDSubroutineType::getImpl(LLVMContext &Context,
198 unsigned Flags, Metadata *TypeArray,
199 StorageType Storage,
200 bool ShouldCreate) {
201 DEFINE_GETIMPL_LOOKUP(MDSubroutineType, (Flags, TypeArray));
202 Metadata *Ops[] = {nullptr, nullptr, nullptr, nullptr,
203 TypeArray, nullptr, nullptr};
204 DEFINE_GETIMPL_STORE(MDSubroutineType, (Flags), Ops);
205}
206
207MDFile *MDFile::getImpl(LLVMContext &Context, MDString *Filename,
208 MDString *Directory, StorageType Storage,
209 bool ShouldCreate) {
210 assert(isCanonical(Filename) && "Expected canonical MDString");
211 assert(isCanonical(Directory) && "Expected canonical MDString");
212 DEFINE_GETIMPL_LOOKUP(MDFile, (getString(Filename), getString(Directory)));
213 Metadata *NodeOps[] = {Filename, Directory};
214 Metadata *Ops[] = {MDTuple::get(Context, NodeOps)};
215 return storeImpl(new (ArrayRef<Metadata *>(Ops).size())
216 MDFile(Context, Storage, Ops),
217 Storage, Context.pImpl->MDFiles);
218}
219
220MDCompileUnit *MDCompileUnit::getImpl(
221 LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
222 MDString *Producer, bool IsOptimized, MDString *Flags,
223 unsigned RuntimeVersion, MDString *SplitDebugFilename,
224 unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
225 Metadata *Subprograms, Metadata *GlobalVariables,
226 Metadata *ImportedEntities, StorageType Storage, bool ShouldCreate) {
227 assert(isCanonical(Producer) && "Expected canonical MDString");
228 assert(isCanonical(Flags) && "Expected canonical MDString");
229 assert(isCanonical(SplitDebugFilename) && "Expected canonical MDString");
230 DEFINE_GETIMPL_LOOKUP(
231 MDCompileUnit,
232 (SourceLanguage, File, getString(Producer), IsOptimized, getString(Flags),
233 RuntimeVersion, getString(SplitDebugFilename), EmissionKind, EnumTypes,
234 RetainedTypes, Subprograms, GlobalVariables, ImportedEntities));
235 Metadata *Ops[] = {File, Producer, Flags, SplitDebugFilename, EnumTypes,
236 RetainedTypes, Subprograms, GlobalVariables,
237 ImportedEntities};
238 DEFINE_GETIMPL_STORE(
239 MDCompileUnit,
240 (SourceLanguage, IsOptimized, RuntimeVersion, EmissionKind), Ops);
241}
242
243MDSubprogram *MDSubprogram::getImpl(
244 LLVMContext &Context, Metadata *Scope, MDString *Name,
245 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
246 bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
247 Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
248 unsigned Flags, bool IsOptimized, Metadata *Function,
249 Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables,
250 StorageType Storage, bool ShouldCreate) {
251 assert(isCanonical(Name) && "Expected canonical MDString");
252 assert(isCanonical(LinkageName) && "Expected canonical MDString");
253 DEFINE_GETIMPL_LOOKUP(MDSubprogram,
254 (Scope, getString(Name), getString(LinkageName), File,
255 Line, Type, IsLocalToUnit, IsDefinition, ScopeLine,
256 ContainingType, Virtuality, VirtualIndex, Flags,
257 IsOptimized, Function, TemplateParams, Declaration,
258 Variables));
259 Metadata *Ops[] = {File, Scope, Name, Name,
260 LinkageName, Type, ContainingType, Function,
261 TemplateParams, Declaration, Variables};
262 DEFINE_GETIMPL_STORE(MDSubprogram,
263 (Line, ScopeLine, Virtuality, VirtualIndex, Flags,
264 IsLocalToUnit, IsDefinition, IsOptimized),
265 Ops);
266}
267
Duncan P. N. Exon Smithdf523492015-02-18 20:32:57 +0000268void MDSubprogram::replaceFunction(Function *F) {
269 replaceFunction(F ? ConstantAsMetadata::get(F)
270 : static_cast<ConstantAsMetadata *>(nullptr));
271}
272
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000273MDLexicalBlock *MDLexicalBlock::getImpl(LLVMContext &Context, Metadata *Scope,
274 Metadata *File, unsigned Line,
275 unsigned Column, StorageType Storage,
276 bool ShouldCreate) {
277 DEFINE_GETIMPL_LOOKUP(MDLexicalBlock, (Scope, File, Line, Column));
278 Metadata *Ops[] = {File, Scope};
279 DEFINE_GETIMPL_STORE(MDLexicalBlock, (Line, Column), Ops);
280}
281
282MDLexicalBlockFile *MDLexicalBlockFile::getImpl(LLVMContext &Context,
283 Metadata *Scope, Metadata *File,
284 unsigned Discriminator,
285 StorageType Storage,
286 bool ShouldCreate) {
287 DEFINE_GETIMPL_LOOKUP(MDLexicalBlockFile, (Scope, File, Discriminator));
288 Metadata *Ops[] = {File, Scope};
289 DEFINE_GETIMPL_STORE(MDLexicalBlockFile, (Discriminator), Ops);
290}
291
292MDNamespace *MDNamespace::getImpl(LLVMContext &Context, Metadata *Scope,
293 Metadata *File, MDString *Name, unsigned Line,
294 StorageType Storage, bool ShouldCreate) {
295 assert(isCanonical(Name) && "Expected canonical MDString");
296 DEFINE_GETIMPL_LOOKUP(MDNamespace, (Scope, File, getString(Name), Line));
297 Metadata *Ops[] = {File, Scope, Name};
298 DEFINE_GETIMPL_STORE(MDNamespace, (Line), Ops);
299}
300
301MDTemplateTypeParameter *
302MDTemplateTypeParameter::getImpl(LLVMContext &Context, Metadata *Scope,
Duncan P. N. Exon Smithbd33d372015-02-10 01:59:57 +0000303 MDString *Name, Metadata *Type,
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000304 StorageType Storage, bool ShouldCreate) {
305 assert(isCanonical(Name) && "Expected canonical MDString");
306 DEFINE_GETIMPL_LOOKUP(MDTemplateTypeParameter,
Duncan P. N. Exon Smithbd33d372015-02-10 01:59:57 +0000307 (Scope, getString(Name), Type));
308 Metadata *Ops[] = {Scope, Name, Type};
309 DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(MDTemplateTypeParameter, Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000310}
311
312MDTemplateValueParameter *MDTemplateValueParameter::getImpl(
313 LLVMContext &Context, unsigned Tag, Metadata *Scope, MDString *Name,
Duncan P. N. Exon Smithbd33d372015-02-10 01:59:57 +0000314 Metadata *Type, Metadata *Value, StorageType Storage, bool ShouldCreate) {
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000315 assert(isCanonical(Name) && "Expected canonical MDString");
Duncan P. N. Exon Smithbd33d372015-02-10 01:59:57 +0000316 DEFINE_GETIMPL_LOOKUP(MDTemplateValueParameter,
317 (Tag, Scope, getString(Name), Type, Value));
318 Metadata *Ops[] = {Scope, Name, Type, Value};
319 DEFINE_GETIMPL_STORE(MDTemplateValueParameter, (Tag), Ops);
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000320}
321
322MDGlobalVariable *
323MDGlobalVariable::getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
324 MDString *LinkageName, Metadata *File, unsigned Line,
325 Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
326 Metadata *Variable,
327 Metadata *StaticDataMemberDeclaration,
328 StorageType Storage, bool ShouldCreate) {
329 assert(isCanonical(Name) && "Expected canonical MDString");
330 assert(isCanonical(LinkageName) && "Expected canonical MDString");
331 DEFINE_GETIMPL_LOOKUP(MDGlobalVariable,
332 (Scope, getString(Name), getString(LinkageName), File,
333 Line, Type, IsLocalToUnit, IsDefinition, Variable,
334 StaticDataMemberDeclaration));
335 Metadata *Ops[] = {Scope, Name, File, Type,
336 Name, LinkageName, Variable, StaticDataMemberDeclaration};
337 DEFINE_GETIMPL_STORE(MDGlobalVariable, (Line, IsLocalToUnit, IsDefinition),
338 Ops);
339}
340
341MDLocalVariable *MDLocalVariable::getImpl(
342 LLVMContext &Context, unsigned Tag, Metadata *Scope, MDString *Name,
343 Metadata *File, unsigned Line, Metadata *Type, unsigned Arg, unsigned Flags,
344 Metadata *InlinedAt, StorageType Storage, bool ShouldCreate) {
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +0000345 // Truncate Arg to 8 bits.
346 //
347 // FIXME: This is gross (and should be changed to an assert or removed), but
348 // it matches historical behaviour for now.
349 Arg &= (1u << 8) - 1;
350
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000351 assert(isCanonical(Name) && "Expected canonical MDString");
352 DEFINE_GETIMPL_LOOKUP(MDLocalVariable, (Tag, Scope, getString(Name), File,
353 Line, Type, Arg, Flags, InlinedAt));
354 Metadata *Ops[] = {Scope, Name, File, Type, InlinedAt};
355 DEFINE_GETIMPL_STORE(MDLocalVariable, (Tag, Line, Arg, Flags), Ops);
356}
357
358MDExpression *MDExpression::getImpl(LLVMContext &Context,
359 ArrayRef<uint64_t> Elements,
360 StorageType Storage, bool ShouldCreate) {
361 DEFINE_GETIMPL_LOOKUP(MDExpression, (Elements));
362 DEFINE_GETIMPL_STORE_NO_OPS(MDExpression, (Elements));
363}
364
Duncan P. N. Exon Smith193a4fd2015-02-13 01:07:46 +0000365unsigned MDExpression::ExprOperand::getSize() const {
366 switch (getOp()) {
367 case dwarf::DW_OP_bit_piece:
368 return 3;
369 case dwarf::DW_OP_plus:
370 return 2;
371 default:
372 return 1;
373 }
374}
375
376bool MDExpression::isValid() const {
377 for (auto I = expr_op_begin(), E = expr_op_end(); I != E; ++I) {
378 // Check that there's space for the operand.
379 if (I->get() + I->getSize() > E->get())
380 return false;
381
382 // Check that the operand is valid.
383 switch (I->getOp()) {
384 default:
385 return false;
386 case dwarf::DW_OP_bit_piece:
387 // Piece expressions must be at the end.
388 return I->get() + I->getSize() == E->get();
389 case dwarf::DW_OP_plus:
390 case dwarf::DW_OP_deref:
391 break;
392 }
393 }
394 return true;
395}
396
Duncan P. N. Exon Smith01fc1762015-02-10 00:52:32 +0000397MDObjCProperty *MDObjCProperty::getImpl(
398 LLVMContext &Context, MDString *Name, Metadata *File, unsigned Line,
399 MDString *GetterName, MDString *SetterName, unsigned Attributes,
400 Metadata *Type, StorageType Storage, bool ShouldCreate) {
401 assert(isCanonical(Name) && "Expected canonical MDString");
402 assert(isCanonical(GetterName) && "Expected canonical MDString");
403 assert(isCanonical(SetterName) && "Expected canonical MDString");
404 DEFINE_GETIMPL_LOOKUP(MDObjCProperty,
405 (getString(Name), File, Line, getString(GetterName),
406 getString(SetterName), Attributes, Type));
407 Metadata *Ops[] = {Name, File, GetterName, SetterName, Type};
408 DEFINE_GETIMPL_STORE(MDObjCProperty, (Line, Attributes), Ops);
409}
410
411MDImportedEntity *MDImportedEntity::getImpl(LLVMContext &Context, unsigned Tag,
412 Metadata *Scope, Metadata *Entity,
413 unsigned Line, MDString *Name,
414 StorageType Storage,
415 bool ShouldCreate) {
416 assert(isCanonical(Name) && "Expected canonical MDString");
417 DEFINE_GETIMPL_LOOKUP(MDImportedEntity,
418 (Tag, Scope, Entity, Line, getString(Name)));
419 Metadata *Ops[] = {Scope, Entity, Name};
420 DEFINE_GETIMPL_STORE(MDImportedEntity, (Tag, Line), Ops);
421}