CGDebugInfo.cpp revision 25f51dda883d8b08d99e30fcec3b274599264712
1//===--- CGDebugInfo.cpp - Emit Debug Information for a Module ------------===// 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 coordinates the debug information generation while generating code. 11// 12//===----------------------------------------------------------------------===// 13 14#include "CGDebugInfo.h" 15#include "CodeGenModule.h" 16#include "clang/AST/ASTContext.h" 17#include "clang/AST/Decl.h" 18#include "clang/AST/RecordLayout.h" 19#include "clang/Basic/SourceManager.h" 20#include "clang/Basic/FileManager.h" 21#include "llvm/Constants.h" 22#include "llvm/DerivedTypes.h" 23#include "llvm/Instructions.h" 24#include "llvm/Intrinsics.h" 25#include "llvm/Module.h" 26#include "llvm/ADT/StringExtras.h" 27#include "llvm/ADT/SmallVector.h" 28#include "llvm/CodeGen/MachineModuleInfo.h" 29#include "llvm/Support/Dwarf.h" 30#include "llvm/Support/IRBuilder.h" 31#include "llvm/Target/TargetMachine.h" 32using namespace clang; 33using namespace clang::CodeGen; 34 35CGDebugInfo::CGDebugInfo(CodeGenModule *m) 36: M(m) 37, CurLoc() 38, PrevLoc() 39, CompileUnitCache() 40, TypeCache() 41, StopPointFn(NULL) 42, FuncStartFn(NULL) 43, DeclareFn(NULL) 44, RegionStartFn(NULL) 45, RegionEndFn(NULL) 46, CompileUnitAnchor(NULL) 47, SubprogramAnchor(NULL) 48, GlobalVariableAnchor(NULL) 49, RegionStack() 50, VariableDescList() 51, GlobalVarDescList() 52, EnumDescList() 53, SubrangeDescList() 54, Subprogram(NULL) 55{ 56 SR = new llvm::DISerializer(); 57 SR->setModule (&M->getModule()); 58} 59 60CGDebugInfo::~CGDebugInfo() 61{ 62 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!"); 63 64 delete SR; 65 66 // Free CompileUnitCache. 67 for (std::map<const FileEntry*, llvm::CompileUnitDesc *>::iterator I 68 = CompileUnitCache.begin(); I != CompileUnitCache.end(); ++I) { 69 delete I->second; 70 } 71 CompileUnitCache.clear(); 72 73 // Free TypeCache. 74 for (std::map<void *, llvm::TypeDesc *>::iterator I 75 = TypeCache.begin(); I != TypeCache.end(); ++I) { 76 delete I->second; 77 } 78 TypeCache.clear(); 79 80 // Free region descriptors. 81 for (std::vector<llvm::DebugInfoDesc *>::iterator I 82 = RegionStack.begin(); I != RegionStack.end(); ++I) { 83 delete *I; 84 } 85 86 // Free local var descriptors. 87 for (std::vector<llvm::VariableDesc *>::iterator I 88 = VariableDescList.begin(); I != VariableDescList.end(); ++I) { 89 delete *I; 90 } 91 92 // Free global var descriptors. 93 for (std::vector<llvm::GlobalVariableDesc *>::iterator I 94 = GlobalVarDescList.begin(); I != GlobalVarDescList.end(); ++I) { 95 delete *I; 96 } 97 98 // Free enum constants descriptors. 99 for (std::vector<llvm::EnumeratorDesc *>::iterator I 100 = EnumDescList.begin(); I != EnumDescList.end(); ++I) { 101 delete *I; 102 } 103 104 // Free subrange descriptors. 105 for (std::vector<llvm::SubrangeDesc *>::iterator I 106 = SubrangeDescList.begin(); I != SubrangeDescList.end(); ++I) { 107 delete *I; 108 } 109 110 delete CompileUnitAnchor; 111 delete SubprogramAnchor; 112 delete GlobalVariableAnchor; 113} 114 115void CGDebugInfo::setLocation(SourceLocation loc) { 116 if (loc.isValid()) 117 CurLoc = M->getContext().getSourceManager().getLogicalLoc(loc); 118} 119 120/// getCastValueFor - Return a llvm representation for a given debug information 121/// descriptor cast to an empty struct pointer. 122llvm::Value *CGDebugInfo::getCastValueFor(llvm::DebugInfoDesc *DD) { 123 return llvm::ConstantExpr::getBitCast(SR->Serialize(DD), 124 SR->getEmptyStructPtrType()); 125} 126 127/// getValueFor - Return a llvm representation for a given debug information 128/// descriptor. 129llvm::Value *CGDebugInfo::getValueFor(llvm::DebugInfoDesc *DD) { 130 return SR->Serialize(DD); 131} 132 133/// getOrCreateCompileUnit - Get the compile unit from the cache or create a new 134/// one if necessary. This returns null for invalid source locations. 135llvm::CompileUnitDesc* 136CGDebugInfo::getOrCreateCompileUnit(const SourceLocation Loc) { 137 if (Loc.isInvalid()) 138 return NULL; 139 140 SourceManager &SM = M->getContext().getSourceManager(); 141 const FileEntry *FE = SM.getFileEntryForLoc(Loc); 142 143 // See if this compile unit has been used before. 144 llvm::CompileUnitDesc *&Unit = CompileUnitCache[FE]; 145 if (Unit) return Unit; 146 147 // Create new compile unit. 148 // FIXME: Where to free these? 149 // One way is to iterate over the CompileUnitCache in ~CGDebugInfo. 150 Unit = new llvm::CompileUnitDesc(); 151 152 // Make sure we have an anchor. 153 if (!CompileUnitAnchor) { 154 CompileUnitAnchor = new llvm::AnchorDesc(Unit); 155 } 156 157 // Get source file information. 158 const char *FileName, *DirName; 159 if (FE) { 160 FileName = FE->getName(); 161 DirName = FE->getDir()->getName(); 162 } else { 163 FileName = SM.getSourceName(Loc); 164 DirName = ""; 165 } 166 167 Unit->setAnchor(CompileUnitAnchor); 168 Unit->setFileName(FileName); 169 Unit->setDirectory(DirName); 170 171 // Set up producer name. 172 // FIXME: Do not know how to get clang version yet. 173 Unit->setProducer("clang"); 174 175 // Set up Language number. 176 // FIXME: Handle other languages as well. 177 Unit->setLanguage(llvm::dwarf::DW_LANG_C89); 178 179 return Unit; 180} 181 182 183/// getOrCreateCVRType - Get the CVR qualified type from the cache or create 184/// a new one if necessary. 185llvm::TypeDesc * 186CGDebugInfo::getOrCreateCVRType(QualType type, llvm::CompileUnitDesc *Unit) 187{ 188 // We will create a Derived type. 189 llvm::DerivedTypeDesc *DTy = NULL; 190 llvm::TypeDesc *FromTy = NULL; 191 192 if (type.isConstQualified()) { 193 DTy = new llvm::DerivedTypeDesc(llvm::dwarf::DW_TAG_const_type); 194 type.removeConst(); 195 FromTy = getOrCreateType(type, Unit); 196 } else if (type.isVolatileQualified()) { 197 DTy = new llvm::DerivedTypeDesc(llvm::dwarf::DW_TAG_volatile_type); 198 type.removeVolatile(); 199 FromTy = getOrCreateType(type, Unit); 200 } else if (type.isRestrictQualified()) { 201 DTy = new llvm::DerivedTypeDesc(llvm::dwarf::DW_TAG_restrict_type); 202 type.removeRestrict(); 203 FromTy = getOrCreateType(type, Unit); 204 } 205 206 // No need to fill in the Name, Line, Size, Alignment, Offset in case of // CVR derived types. 207 DTy->setContext(Unit); 208 DTy->setFromType(FromTy); 209 210 return DTy; 211} 212 213 214/// getOrCreateBuiltinType - Get the Basic type from the cache or create a new 215/// one if necessary. 216llvm::TypeDesc * 217CGDebugInfo::getOrCreateBuiltinType(QualType type, llvm::CompileUnitDesc *Unit) 218{ 219 assert (type->getTypeClass() == Type::Builtin); 220 221 const BuiltinType *BT = type->getAsBuiltinType(); 222 223 unsigned Encoding = 0; 224 switch (BT->getKind()) 225 { 226 case BuiltinType::Void: 227 return NULL; 228 case BuiltinType::UChar: 229 case BuiltinType::Char_U: 230 Encoding = llvm::dwarf::DW_ATE_unsigned_char; 231 break; 232 case BuiltinType::Char_S: 233 case BuiltinType::SChar: 234 Encoding = llvm::dwarf::DW_ATE_signed_char; 235 break; 236 case BuiltinType::UShort: 237 case BuiltinType::UInt: 238 case BuiltinType::ULong: 239 case BuiltinType::ULongLong: 240 Encoding = llvm::dwarf::DW_ATE_unsigned; 241 break; 242 case BuiltinType::Short: 243 case BuiltinType::Int: 244 case BuiltinType::Long: 245 case BuiltinType::LongLong: 246 Encoding = llvm::dwarf::DW_ATE_signed; 247 break; 248 case BuiltinType::Bool: 249 Encoding = llvm::dwarf::DW_ATE_boolean; 250 break; 251 case BuiltinType::Float: 252 case BuiltinType::Double: 253 Encoding = llvm::dwarf::DW_ATE_float; 254 break; 255 default: 256 Encoding = llvm::dwarf::DW_ATE_signed; 257 break; 258 } 259 260 // Ty will have contain the resulting type. 261 llvm::BasicTypeDesc *BTy = new llvm::BasicTypeDesc(); 262 263 // Get the name and location early to assist debugging. 264 const char *TyName = BT->getName(); 265 266 // Bit size, align and offset of the type. 267 uint64_t Size = M->getContext().getTypeSize(type); 268 uint64_t Align = M->getContext().getTypeAlign(type); 269 uint64_t Offset = 0; 270 271 // If the type is defined, fill in the details. 272 if (BTy) { 273 BTy->setContext(Unit); 274 BTy->setName(TyName); 275 BTy->setSize(Size); 276 BTy->setAlign(Align); 277 BTy->setOffset(Offset); 278 BTy->setEncoding(Encoding); 279 } 280 281 return BTy; 282} 283 284llvm::TypeDesc * 285CGDebugInfo::getOrCreatePointerType(QualType type, llvm::CompileUnitDesc *Unit) 286{ 287 // type* 288 llvm::DerivedTypeDesc *DTy = 289 new llvm::DerivedTypeDesc(llvm::dwarf::DW_TAG_pointer_type); 290 291 // Handle the derived type. 292 const PointerType *PTRT = type->getAsPointerType(); 293 llvm::TypeDesc *FromTy = getOrCreateType(PTRT->getPointeeType(), Unit); 294 295 // Get the name and location early to assist debugging. 296 SourceManager &SM = M->getContext().getSourceManager(); 297 uint64_t Line = SM.getLogicalLineNumber(CurLoc); 298 299 // Bit size, align and offset of the type. 300 uint64_t Size = M->getContext().getTypeSize(type); 301 uint64_t Align = M->getContext().getTypeAlign(type); 302 uint64_t Offset = 0; 303 304 // If the type is defined, fill in the details. 305 if (DTy) { 306 DTy->setContext(Unit); 307 DTy->setLine(Line); 308 DTy->setSize(Size); 309 DTy->setAlign(Align); 310 DTy->setOffset(Offset); 311 DTy->setFromType(FromTy); 312 } 313 314 return DTy; 315} 316 317llvm::TypeDesc * 318CGDebugInfo::getOrCreateTypedefType(QualType type, llvm::CompileUnitDesc *Unit) 319{ 320 // typedefs are derived from some other type. 321 llvm::DerivedTypeDesc *DTy = 322 new llvm::DerivedTypeDesc(llvm::dwarf::DW_TAG_typedef); 323 324 // Handle derived type. 325 const TypedefType *TDT = type->getAsTypedefType(); 326 llvm::TypeDesc *FromTy = getOrCreateType(TDT->LookThroughTypedefs(), 327 Unit); 328 329 // Get the name and location early to assist debugging. 330 const char *TyName = TDT->getDecl()->getName(); 331 SourceManager &SM = M->getContext().getSourceManager(); 332 uint64_t Line = SM.getLogicalLineNumber(TDT->getDecl()->getLocation()); 333 334 // If the type is defined, fill in the details. 335 if (DTy) { 336 DTy->setContext(Unit); 337 DTy->setFile(getOrCreateCompileUnit(TDT->getDecl()->getLocation())); 338 DTy->setLine(Line); 339 DTy->setName(TyName); 340 DTy->setFromType(FromTy); 341 } 342 343 return DTy; 344} 345 346llvm::TypeDesc * 347CGDebugInfo::getOrCreateFunctionType(QualType type, llvm::CompileUnitDesc *Unit) 348{ 349 llvm::CompositeTypeDesc *SubrTy = 350 new llvm::CompositeTypeDesc(llvm::dwarf::DW_TAG_subroutine_type); 351 352 // Prepare to add the arguments for the subroutine. 353 std::vector<llvm::DebugInfoDesc *> &Elements = SubrTy->getElements(); 354 355 // Get result type. 356 const FunctionType *FT = type->getAsFunctionType(); 357 llvm::TypeDesc *ArgTy = getOrCreateType(FT->getResultType(), Unit); 358 if (ArgTy) Elements.push_back(ArgTy); 359 360 // Set up remainder of arguments. 361 if (type->getTypeClass() == Type::FunctionProto) { 362 const FunctionTypeProto *FTPro = dyn_cast<FunctionTypeProto>(type); 363 for (unsigned int i =0; i < FTPro->getNumArgs(); i++) { 364 QualType ParamType = FTPro->getArgType(i); 365 ArgTy = getOrCreateType(ParamType, Unit); 366 if (ArgTy) Elements.push_back(ArgTy); 367 } 368 } 369 370 // FIXME: set other fields file, line here. 371 SubrTy->setContext(Unit); 372 373 return SubrTy; 374} 375 376/// getOrCreateRecordType - get structure or union type. 377llvm::TypeDesc * 378CGDebugInfo::getOrCreateRecordType(QualType type, llvm::CompileUnitDesc *Unit) 379{ 380 llvm::CompositeTypeDesc *RecType; 381 if(type->isStructureType()) 382 RecType = new llvm::CompositeTypeDesc(llvm::dwarf::DW_TAG_structure_type); 383 else if(type->isUnionType()) 384 RecType = new llvm::CompositeTypeDesc(llvm::dwarf::DW_TAG_union_type); 385 else 386 return NULL; 387 388 RecordDecl *RecDecl = type->getAsRecordType()->getDecl(); 389 // We can not get the type for forward declarations. 390 // FIXME: What *should* we be doing here? 391 if (!RecDecl->getDefinition(M->getContext())) 392 return NULL; 393 const ASTRecordLayout &RL = M->getContext().getASTRecordLayout(RecDecl); 394 395 SourceManager &SM = M->getContext().getSourceManager(); 396 uint64_t Line = SM.getLogicalLineNumber(RecDecl->getLocation()); 397 398 std::vector<llvm::DebugInfoDesc *> &Elements = RecType->getElements(); 399 400 // Add the members. 401 int NumMembers = RecDecl->getNumMembers(); 402 for (int i = 0; i < NumMembers; i++) { 403 FieldDecl *Member = RecDecl->getMember(i); 404 llvm::TypeDesc *MemberTy = getOrCreateType(Member->getType(), Unit); 405 MemberTy->setOffset(RL.getFieldOffset(i)); 406 Elements.push_back(MemberTy); 407 } 408 409 // Fill in the blanks. 410 if(RecType) { 411 RecType->setContext(Unit); 412 RecType->setName(RecDecl->getName()); 413 RecType->setFile(getOrCreateCompileUnit(RecDecl->getLocation())); 414 RecType->setLine(Line); 415 RecType->setSize(RL.getSize()); 416 RecType->setAlign(RL.getAlignment()); 417 RecType->setOffset(0); 418 } 419 return(RecType); 420} 421 422/// getOrCreateEnumType - get Enum type. 423llvm::TypeDesc * 424CGDebugInfo::getOrCreateEnumType(QualType type, llvm::CompileUnitDesc *Unit) 425{ 426 llvm::CompositeTypeDesc *EnumTy 427 = new llvm::CompositeTypeDesc(llvm::dwarf::DW_TAG_enumeration_type); 428 429 EnumType *EType = dyn_cast<EnumType>(type); 430 if (!EType) return(NULL); 431 432 EnumDecl *EDecl = EType->getDecl(); 433 SourceManager &SM = M->getContext().getSourceManager(); 434 uint64_t Line = SM.getLogicalLineNumber(EDecl->getLocation()); 435 436 // Size, align and offset of the type. 437 uint64_t Size = M->getContext().getTypeSize(type); 438 uint64_t Align = M->getContext().getTypeAlign(type); 439 440 // Create descriptors for enum members. 441 std::vector<llvm::DebugInfoDesc *> &Elements = EnumTy->getElements(); 442 EnumConstantDecl *ElementList = EDecl->getEnumConstantList(); 443 while (ElementList) { 444 llvm::EnumeratorDesc *EnumDesc = new llvm::EnumeratorDesc(); 445 // push it to the enum desc list so that we can free it later. 446 EnumDescList.push_back(EnumDesc); 447 448 const char *ElementName = ElementList->getName(); 449 uint64_t Value = ElementList->getInitVal().getZExtValue(); 450 451 EnumDesc->setName(ElementName); 452 EnumDesc->setValue(Value); 453 Elements.push_back(EnumDesc); 454 if (ElementList->getNextDeclarator()) 455 ElementList 456 = dyn_cast<EnumConstantDecl>(ElementList->getNextDeclarator()); 457 else 458 break; 459 } 460 461 // Fill in the blanks. 462 if (EnumTy) { 463 EnumTy->setContext(Unit); 464 EnumTy->setName(EDecl->getName()); 465 EnumTy->setSize(Size); 466 EnumTy->setAlign(Align); 467 EnumTy->setOffset(0); 468 EnumTy->setFile(getOrCreateCompileUnit(EDecl->getLocation())); 469 EnumTy->setLine(Line); 470 } 471 return EnumTy; 472} 473 474/// getOrCreateArrayType - get or create array types. 475llvm::TypeDesc * 476CGDebugInfo::getOrCreateArrayType(QualType type, llvm::CompileUnitDesc *Unit) 477{ 478 llvm::CompositeTypeDesc *ArrayTy 479 = new llvm::CompositeTypeDesc(llvm::dwarf::DW_TAG_array_type); 480 481 // Size, align and offset of the type. 482 uint64_t Size = M->getContext().getTypeSize(type); 483 uint64_t Align = M->getContext().getTypeAlign(type); 484 485 SourceManager &SM = M->getContext().getSourceManager(); 486 uint64_t Line = SM.getLogicalLineNumber(CurLoc); 487 488 // Add the dimensions of the array. 489 std::vector<llvm::DebugInfoDesc *> &Elements = ArrayTy->getElements(); 490 do { 491 const ArrayType *AT = M->getContext().getAsArrayType(type); 492 llvm::SubrangeDesc *Subrange = new llvm::SubrangeDesc(); 493 494 // push it back on the subrange desc list so that we can free it later. 495 SubrangeDescList.push_back(Subrange); 496 497 uint64_t Upper = 0; 498 if (const ConstantArrayType *ConstArrTy = dyn_cast<ConstantArrayType>(AT)) { 499 Upper = ConstArrTy->getSize().getZExtValue() - 1; 500 } 501 Subrange->setLo(0); 502 Subrange->setHi(Upper); 503 Elements.push_back(Subrange); 504 type = AT->getElementType(); 505 } while (type->isArrayType()); 506 507 ArrayTy->setFromType(getOrCreateType(type, Unit)); 508 509 if (ArrayTy) { 510 ArrayTy->setContext(Unit); 511 ArrayTy->setSize(Size); 512 ArrayTy->setAlign(Align); 513 ArrayTy->setOffset(0); 514 ArrayTy->setFile(getOrCreateCompileUnit(CurLoc)); 515 ArrayTy->setLine(Line); 516 } 517 return ArrayTy; 518} 519 520 521/// getOrCreateTaggedType - get or create structure/union/Enum type. 522llvm::TypeDesc * 523CGDebugInfo::getOrCreateTaggedType(QualType type, llvm::CompileUnitDesc *Unit) 524{ 525 if (type->isStructureType() || type->isUnionType()) 526 return getOrCreateRecordType(type, Unit); 527 else if (type->isEnumeralType()) 528 return getOrCreateEnumType(type, Unit); 529 else 530 return NULL; 531} 532 533/// getOrCreateType - Get the type from the cache or create a new 534/// one if necessary. 535llvm::TypeDesc * 536CGDebugInfo::getOrCreateType(QualType type, llvm::CompileUnitDesc *Unit) 537{ 538 if (type.isNull()) 539 return NULL; 540 541 // Check to see if the compile unit already has created this type. 542 llvm::TypeDesc *&Slot = TypeCache[type.getAsOpaquePtr()]; 543 if (Slot) return Slot; 544 545 // We need to check for the CVR qualifiers as the first thing. 546 if (type.getCVRQualifiers()) { 547 Slot = getOrCreateCVRType (type, Unit); 548 return Slot; 549 } 550 551 // Work out details of type. 552 switch(type->getTypeClass()) { 553 case Type::Complex: 554 case Type::Reference: 555 case Type::Vector: 556 case Type::ExtVector: 557 case Type::ASQual: 558 case Type::ObjCInterface: 559 case Type::ObjCQualifiedInterface: 560 case Type::ObjCQualifiedId: 561 case Type::TypeOfExp: 562 case Type::TypeOfTyp: 563 default: 564 { 565 return NULL; 566 } 567 568 case Type::TypeName: 569 Slot = getOrCreateTypedefType(type, Unit); 570 break; 571 572 case Type::FunctionProto: 573 case Type::FunctionNoProto: 574 Slot = getOrCreateFunctionType(type, Unit); 575 break; 576 577 case Type::Builtin: 578 Slot = getOrCreateBuiltinType(type, Unit); 579 break; 580 581 case Type::Pointer: 582 Slot = getOrCreatePointerType(type, Unit); 583 break; 584 585 case Type::Tagged: 586 Slot = getOrCreateTaggedType(type, Unit); 587 break; 588 589 case Type::ConstantArray: 590 case Type::VariableArray: 591 case Type::IncompleteArray: 592 Slot = getOrCreateArrayType(type, Unit); 593 break; 594 } 595 596 return Slot; 597} 598 599/// EmitFunctionStart - Constructs the debug code for entering a function - 600/// "llvm.dbg.func.start.". 601void CGDebugInfo::EmitFunctionStart(const char *Name, 602 QualType ReturnType, 603 llvm::Function *Fn, 604 llvm::IRBuilder<> &Builder) 605{ 606 // Create subprogram descriptor. 607 Subprogram = new llvm::SubprogramDesc(); 608 609 // Make sure we have an anchor. 610 if (!SubprogramAnchor) { 611 SubprogramAnchor = new llvm::AnchorDesc(Subprogram); 612 } 613 614 // Get name information. 615 Subprogram->setName(Name); 616 Subprogram->setFullName(Name); 617 618 // Gather location information. 619 llvm::CompileUnitDesc *Unit = getOrCreateCompileUnit(CurLoc); 620 SourceManager &SM = M->getContext().getSourceManager(); 621 uint64_t Loc = SM.getLogicalLineNumber(CurLoc); 622 623 // Get Function Type. 624 llvm::TypeDesc *SPTy = getOrCreateType(ReturnType, Unit); 625 626 Subprogram->setAnchor(SubprogramAnchor); 627 Subprogram->setContext(Unit); 628 Subprogram->setFile(Unit); 629 Subprogram->setLine(Loc); 630 Subprogram->setType(SPTy); 631 Subprogram->setIsStatic(Fn->hasInternalLinkage()); 632 Subprogram->setIsDefinition(true); 633 634 // Lazily construct llvm.dbg.func.start. 635 if (!FuncStartFn) 636 FuncStartFn = llvm::Intrinsic::getDeclaration(&M->getModule(), 637 llvm::Intrinsic::dbg_func_start); 638 639 // Call llvm.dbg.func.start which also implicitly calls llvm.dbg.stoppoint. 640 Builder.CreateCall(FuncStartFn, getCastValueFor(Subprogram), ""); 641 642 // Push function on region stack. 643 RegionStack.push_back(Subprogram); 644} 645 646 647void 648CGDebugInfo::EmitStopPoint(llvm::Function *Fn, llvm::IRBuilder<> &Builder) 649{ 650 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return; 651 652 // Don't bother if things are the same as last time. 653 SourceManager &SM = M->getContext().getSourceManager(); 654 if (CurLoc == PrevLoc 655 || (SM.getLineNumber(CurLoc) == SM.getLineNumber(PrevLoc) 656 && SM.isFromSameFile(CurLoc, PrevLoc))) 657 return; 658 659 // Update last state. 660 PrevLoc = CurLoc; 661 662 // Get the appropriate compile unit. 663 llvm::CompileUnitDesc *Unit = getOrCreateCompileUnit(CurLoc); 664 665 // Lazily construct llvm.dbg.stoppoint function. 666 if (!StopPointFn) 667 StopPointFn = llvm::Intrinsic::getDeclaration(&M->getModule(), 668 llvm::Intrinsic::dbg_stoppoint); 669 670 uint64_t CurLineNo = SM.getLogicalLineNumber(CurLoc); 671 uint64_t ColumnNo = SM.getLogicalColumnNumber(CurLoc); 672 673 // Invoke llvm.dbg.stoppoint 674 Builder.CreateCall3(StopPointFn, 675 llvm::ConstantInt::get(llvm::Type::Int32Ty, CurLineNo), 676 llvm::ConstantInt::get(llvm::Type::Int32Ty, ColumnNo), 677 getCastValueFor(Unit), ""); 678} 679 680/// EmitRegionStart- Constructs the debug code for entering a declarative 681/// region - "llvm.dbg.region.start.". 682void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, 683 llvm::IRBuilder<> &Builder) 684{ 685 llvm::BlockDesc *Block = new llvm::BlockDesc(); 686 if (!RegionStack.empty()) 687 Block->setContext(RegionStack.back()); 688 RegionStack.push_back(Block); 689 690 // Lazily construct llvm.dbg.region.start function. 691 if (!RegionStartFn) 692 RegionStartFn = llvm::Intrinsic::getDeclaration(&M->getModule(), 693 llvm::Intrinsic::dbg_region_start); 694 695 // Call llvm.dbg.func.start. 696 Builder.CreateCall(RegionStartFn, getCastValueFor(Block), ""); 697} 698 699/// EmitRegionEnd - Constructs the debug code for exiting a declarative 700/// region - "llvm.dbg.region.end." 701void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, llvm::IRBuilder<> &Builder) 702{ 703 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!"); 704 705 // Lazily construct llvm.dbg.region.end function. 706 if (!RegionEndFn) 707 RegionEndFn =llvm::Intrinsic::getDeclaration(&M->getModule(), 708 llvm::Intrinsic::dbg_region_end); 709 710 // Provide an region stop point. 711 EmitStopPoint(Fn, Builder); 712 713 // Call llvm.dbg.func.end. 714 llvm::DebugInfoDesc *DID = RegionStack.back(); 715 Builder.CreateCall(RegionEndFn, getCastValueFor(DID), ""); 716 RegionStack.pop_back(); 717 // FIXME: Should be freeing here? 718} 719 720/// EmitDeclare - Emit local variable declaration debug info. 721void CGDebugInfo::EmitDeclare(const VarDecl *decl, unsigned Tag, 722 llvm::Value *AI, 723 llvm::IRBuilder<> &Builder) 724{ 725 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!"); 726 727 // FIXME: If it is a compiler generated temporary then return. 728 729 // Construct llvm.dbg.declare function. 730 if (!DeclareFn) 731 DeclareFn = llvm::Intrinsic::getDeclaration(&M->getModule(), 732 llvm::Intrinsic::dbg_declare); 733 734 // Get type information. 735 llvm::CompileUnitDesc *Unit = getOrCreateCompileUnit(CurLoc); 736 llvm::TypeDesc *TyDesc = getOrCreateType(decl->getType(), Unit); 737 738 SourceManager &SM = M->getContext().getSourceManager(); 739 uint64_t Loc = SM.getLogicalLineNumber(CurLoc); 740 741 // Construct variable. 742 llvm::VariableDesc *Variable = new llvm::VariableDesc(Tag); 743 Variable->setContext(RegionStack.back()); 744 Variable->setName(decl->getName()); 745 Variable->setFile(Unit); 746 Variable->setLine(Loc); 747 Variable->setType(TyDesc); 748 749 // Push it onto the list so that we can free it. 750 VariableDescList.push_back(Variable); 751 752 // Cast the AllocA result to a {}* for the call to llvm.dbg.declare. 753 // These bit cast instructions will get freed when the basic block is 754 // deleted. So do not need to free them explicity. 755 const llvm::PointerType *EmpPtr = SR->getEmptyStructPtrType(); 756 llvm::Value *AllocACast = new llvm::BitCastInst(AI, EmpPtr, decl->getName(), 757 Builder.GetInsertBlock()); 758 759 // Call llvm.dbg.declare. 760 Builder.CreateCall2(DeclareFn, AllocACast, getCastValueFor(Variable), ""); 761} 762 763/// EmitGlobalVariable - Emit information about a global variable. 764void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *GV, 765 const VarDecl *decl) 766{ 767 // Create global variable debug descriptor. 768 llvm::GlobalVariableDesc *Global = new llvm::GlobalVariableDesc(); 769 770 // Push it onto the list so that we can free it. 771 GlobalVarDescList.push_back(Global); 772 773 // Make sure we have an anchor. 774 if (!GlobalVariableAnchor) 775 GlobalVariableAnchor = new llvm::AnchorDesc(Global); 776 777 // Get name information. 778 Global->setName(decl->getName()); 779 Global->setFullName(decl->getName()); 780 781 llvm::CompileUnitDesc *Unit = getOrCreateCompileUnit(CurLoc); 782 SourceManager &SM = M->getContext().getSourceManager(); 783 uint64_t Loc = SM.getLogicalLineNumber(CurLoc); 784 785 llvm::TypeDesc *TyD = getOrCreateType(decl->getType(), Unit); 786 787 // Fill in the Global information. 788 Global->setAnchor(GlobalVariableAnchor); 789 Global->setContext(Unit); 790 Global->setFile(Unit); 791 Global->setLine(Loc); 792 Global->setType(TyD); 793 Global->setIsDefinition(true); 794 Global->setIsStatic(GV->hasInternalLinkage()); 795 Global->setGlobalVariable(GV); 796 797 // Make sure global is created if needed. 798 getValueFor(Global); 799} 800 801