1 //===- 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" 17 #include "llvm/IR/Function.h" 18 19 using namespace llvm; 20 21 MDLocation::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. 28 assert(Column < (1u << 16) && "Expected 16-bit column"); 29 30 SubclassData32 = Line; 31 SubclassData16 = Column; 32 } 33 34 static 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 40 MDLocation *MDLocation::getImpl(LLVMContext &Context, unsigned Line, 41 unsigned Column, Metadata *Scope, 42 Metadata *InlinedAt, StorageType Storage, 43 bool ShouldCreate) { 44 // Fixup column. 45 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 67 static StringRef getString(const MDString *S) { 68 if (S) 69 return S->getString(); 70 return StringRef(); 71 } 72 73 #ifndef NDEBUG 74 static bool isCanonical(const MDString *S) { 75 return !S || !S->getString().empty(); 76 } 77 #endif 78 79 GenericDebugNode *GenericDebugNode::getImpl(LLVMContext &Context, unsigned Tag, 80 MDString *Header, 81 ArrayRef<Metadata *> DwarfOps, 82 StorageType Storage, 83 bool ShouldCreate) { 84 unsigned Hash = 0; 85 if (Storage == Uniqued) { 86 GenericDebugNodeInfo::KeyTy Key(Tag, getString(Header), DwarfOps); 87 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. 97 assert(isCanonical(Header) && "Expected canonical MDString"); 98 Metadata *PreOps[] = {Header}; 99 return storeImpl(new (DwarfOps.size() + 1) GenericDebugNode( 100 Context, Storage, Hash, Tag, PreOps, DwarfOps), 101 Storage, Context.pImpl->GenericDebugNodes); 102 } 103 104 void GenericDebugNode::recalculateHash() { 105 setHash(GenericDebugNodeInfo::KeyTy::calculateHash(this)); 106 } 107 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) 130 #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) 134 135 MDSubrange *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 141 MDEnumerator *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 150 MDBasicType *MDBasicType::getImpl(LLVMContext &Context, unsigned Tag, 151 MDString *Name, uint64_t SizeInBits, 152 uint64_t 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 162 MDDerivedType *MDDerivedType::getImpl( 163 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File, 164 unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits, 165 uint64_t AlignInBits, uint64_t 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 177 MDCompositeType *MDCompositeType::getImpl( 178 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File, 179 unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits, 180 uint64_t AlignInBits, uint64_t 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 197 MDSubroutineType *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, nullptr}; 204 DEFINE_GETIMPL_STORE(MDSubroutineType, (Flags), Ops); 205 } 206 207 MDFile *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 *Ops[] = {Filename, Directory}; 214 DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(MDFile, Ops); 215 } 216 217 MDCompileUnit *MDCompileUnit::getImpl( 218 LLVMContext &Context, unsigned SourceLanguage, Metadata *File, 219 MDString *Producer, bool IsOptimized, MDString *Flags, 220 unsigned RuntimeVersion, MDString *SplitDebugFilename, 221 unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes, 222 Metadata *Subprograms, Metadata *GlobalVariables, 223 Metadata *ImportedEntities, StorageType Storage, bool ShouldCreate) { 224 assert(isCanonical(Producer) && "Expected canonical MDString"); 225 assert(isCanonical(Flags) && "Expected canonical MDString"); 226 assert(isCanonical(SplitDebugFilename) && "Expected canonical MDString"); 227 DEFINE_GETIMPL_LOOKUP( 228 MDCompileUnit, 229 (SourceLanguage, File, getString(Producer), IsOptimized, getString(Flags), 230 RuntimeVersion, getString(SplitDebugFilename), EmissionKind, EnumTypes, 231 RetainedTypes, Subprograms, GlobalVariables, ImportedEntities)); 232 Metadata *Ops[] = {File, Producer, Flags, SplitDebugFilename, EnumTypes, 233 RetainedTypes, Subprograms, GlobalVariables, 234 ImportedEntities}; 235 DEFINE_GETIMPL_STORE( 236 MDCompileUnit, 237 (SourceLanguage, IsOptimized, RuntimeVersion, EmissionKind), Ops); 238 } 239 240 MDSubprogram *MDSubprogram::getImpl( 241 LLVMContext &Context, Metadata *Scope, MDString *Name, 242 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type, 243 bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine, 244 Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex, 245 unsigned Flags, bool IsOptimized, Metadata *Function, 246 Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables, 247 StorageType Storage, bool ShouldCreate) { 248 assert(isCanonical(Name) && "Expected canonical MDString"); 249 assert(isCanonical(LinkageName) && "Expected canonical MDString"); 250 DEFINE_GETIMPL_LOOKUP(MDSubprogram, 251 (Scope, getString(Name), getString(LinkageName), File, 252 Line, Type, IsLocalToUnit, IsDefinition, ScopeLine, 253 ContainingType, Virtuality, VirtualIndex, Flags, 254 IsOptimized, Function, TemplateParams, Declaration, 255 Variables)); 256 Metadata *Ops[] = {File, Scope, Name, Name, 257 LinkageName, Type, ContainingType, Function, 258 TemplateParams, Declaration, Variables}; 259 DEFINE_GETIMPL_STORE(MDSubprogram, 260 (Line, ScopeLine, Virtuality, VirtualIndex, Flags, 261 IsLocalToUnit, IsDefinition, IsOptimized), 262 Ops); 263 } 264 265 void MDSubprogram::replaceFunction(Function *F) { 266 replaceFunction(F ? ConstantAsMetadata::get(F) 267 : static_cast<ConstantAsMetadata *>(nullptr)); 268 } 269 270 MDLexicalBlock *MDLexicalBlock::getImpl(LLVMContext &Context, Metadata *Scope, 271 Metadata *File, unsigned Line, 272 unsigned Column, StorageType Storage, 273 bool ShouldCreate) { 274 DEFINE_GETIMPL_LOOKUP(MDLexicalBlock, (Scope, File, Line, Column)); 275 Metadata *Ops[] = {File, Scope}; 276 DEFINE_GETIMPL_STORE(MDLexicalBlock, (Line, Column), Ops); 277 } 278 279 MDLexicalBlockFile *MDLexicalBlockFile::getImpl(LLVMContext &Context, 280 Metadata *Scope, Metadata *File, 281 unsigned Discriminator, 282 StorageType Storage, 283 bool ShouldCreate) { 284 DEFINE_GETIMPL_LOOKUP(MDLexicalBlockFile, (Scope, File, Discriminator)); 285 Metadata *Ops[] = {File, Scope}; 286 DEFINE_GETIMPL_STORE(MDLexicalBlockFile, (Discriminator), Ops); 287 } 288 289 MDNamespace *MDNamespace::getImpl(LLVMContext &Context, Metadata *Scope, 290 Metadata *File, MDString *Name, unsigned Line, 291 StorageType Storage, bool ShouldCreate) { 292 assert(isCanonical(Name) && "Expected canonical MDString"); 293 DEFINE_GETIMPL_LOOKUP(MDNamespace, (Scope, File, getString(Name), Line)); 294 Metadata *Ops[] = {File, Scope, Name}; 295 DEFINE_GETIMPL_STORE(MDNamespace, (Line), Ops); 296 } 297 298 MDTemplateTypeParameter *MDTemplateTypeParameter::getImpl(LLVMContext &Context, 299 MDString *Name, 300 Metadata *Type, 301 StorageType Storage, 302 bool ShouldCreate) { 303 assert(isCanonical(Name) && "Expected canonical MDString"); 304 DEFINE_GETIMPL_LOOKUP(MDTemplateTypeParameter, (getString(Name), Type)); 305 Metadata *Ops[] = {Name, Type}; 306 DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(MDTemplateTypeParameter, Ops); 307 } 308 309 MDTemplateValueParameter *MDTemplateValueParameter::getImpl( 310 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *Type, 311 Metadata *Value, StorageType Storage, bool ShouldCreate) { 312 assert(isCanonical(Name) && "Expected canonical MDString"); 313 DEFINE_GETIMPL_LOOKUP(MDTemplateValueParameter, 314 (Tag, getString(Name), Type, Value)); 315 Metadata *Ops[] = {Name, Type, Value}; 316 DEFINE_GETIMPL_STORE(MDTemplateValueParameter, (Tag), Ops); 317 } 318 319 MDGlobalVariable * 320 MDGlobalVariable::getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name, 321 MDString *LinkageName, Metadata *File, unsigned Line, 322 Metadata *Type, bool IsLocalToUnit, bool IsDefinition, 323 Metadata *Variable, 324 Metadata *StaticDataMemberDeclaration, 325 StorageType Storage, bool ShouldCreate) { 326 assert(isCanonical(Name) && "Expected canonical MDString"); 327 assert(isCanonical(LinkageName) && "Expected canonical MDString"); 328 DEFINE_GETIMPL_LOOKUP(MDGlobalVariable, 329 (Scope, getString(Name), getString(LinkageName), File, 330 Line, Type, IsLocalToUnit, IsDefinition, Variable, 331 StaticDataMemberDeclaration)); 332 Metadata *Ops[] = {Scope, Name, File, Type, 333 Name, LinkageName, Variable, StaticDataMemberDeclaration}; 334 DEFINE_GETIMPL_STORE(MDGlobalVariable, (Line, IsLocalToUnit, IsDefinition), 335 Ops); 336 } 337 338 MDLocalVariable *MDLocalVariable::getImpl( 339 LLVMContext &Context, unsigned Tag, Metadata *Scope, MDString *Name, 340 Metadata *File, unsigned Line, Metadata *Type, unsigned Arg, unsigned Flags, 341 Metadata *InlinedAt, StorageType Storage, bool ShouldCreate) { 342 // Truncate Arg to 8 bits. 343 // 344 // FIXME: This is gross (and should be changed to an assert or removed), but 345 // it matches historical behaviour for now. 346 Arg &= (1u << 8) - 1; 347 348 assert(isCanonical(Name) && "Expected canonical MDString"); 349 DEFINE_GETIMPL_LOOKUP(MDLocalVariable, (Tag, Scope, getString(Name), File, 350 Line, Type, Arg, Flags, InlinedAt)); 351 Metadata *Ops[] = {Scope, Name, File, Type, InlinedAt}; 352 DEFINE_GETIMPL_STORE(MDLocalVariable, (Tag, Line, Arg, Flags), Ops); 353 } 354 355 MDExpression *MDExpression::getImpl(LLVMContext &Context, 356 ArrayRef<uint64_t> Elements, 357 StorageType Storage, bool ShouldCreate) { 358 DEFINE_GETIMPL_LOOKUP(MDExpression, (Elements)); 359 DEFINE_GETIMPL_STORE_NO_OPS(MDExpression, (Elements)); 360 } 361 362 unsigned MDExpression::ExprOperand::getSize() const { 363 switch (getOp()) { 364 case dwarf::DW_OP_bit_piece: 365 return 3; 366 case dwarf::DW_OP_plus: 367 return 2; 368 default: 369 return 1; 370 } 371 } 372 373 bool MDExpression::isValid() const { 374 for (auto I = expr_op_begin(), E = expr_op_end(); I != E; ++I) { 375 // Check that there's space for the operand. 376 if (I->get() + I->getSize() > E->get()) 377 return false; 378 379 // Check that the operand is valid. 380 switch (I->getOp()) { 381 default: 382 return false; 383 case dwarf::DW_OP_bit_piece: 384 // Piece expressions must be at the end. 385 return I->get() + I->getSize() == E->get(); 386 case dwarf::DW_OP_plus: 387 case dwarf::DW_OP_deref: 388 break; 389 } 390 } 391 return true; 392 } 393 394 MDObjCProperty *MDObjCProperty::getImpl( 395 LLVMContext &Context, MDString *Name, Metadata *File, unsigned Line, 396 MDString *GetterName, MDString *SetterName, unsigned Attributes, 397 Metadata *Type, StorageType Storage, bool ShouldCreate) { 398 assert(isCanonical(Name) && "Expected canonical MDString"); 399 assert(isCanonical(GetterName) && "Expected canonical MDString"); 400 assert(isCanonical(SetterName) && "Expected canonical MDString"); 401 DEFINE_GETIMPL_LOOKUP(MDObjCProperty, 402 (getString(Name), File, Line, getString(GetterName), 403 getString(SetterName), Attributes, Type)); 404 Metadata *Ops[] = {Name, File, GetterName, SetterName, Type}; 405 DEFINE_GETIMPL_STORE(MDObjCProperty, (Line, Attributes), Ops); 406 } 407 408 MDImportedEntity *MDImportedEntity::getImpl(LLVMContext &Context, unsigned Tag, 409 Metadata *Scope, Metadata *Entity, 410 unsigned Line, MDString *Name, 411 StorageType Storage, 412 bool ShouldCreate) { 413 assert(isCanonical(Name) && "Expected canonical MDString"); 414 DEFINE_GETIMPL_LOOKUP(MDImportedEntity, 415 (Tag, Scope, Entity, Line, getString(Name))); 416 Metadata *Ops[] = {Scope, Entity, Name}; 417 DEFINE_GETIMPL_STORE(MDImportedEntity, (Tag, Line), Ops); 418 } 419