CGDebugInfo.cpp revision cc9b16394fe6c9245dc4f8661a63d0c3937b62f0
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/Basic/SourceManager.h" 18#include "clang/Basic/FileManager.h" 19#include "llvm/Constants.h" 20#include "llvm/DerivedTypes.h" 21#include "llvm/Instructions.h" 22#include "llvm/Intrinsics.h" 23#include "llvm/Module.h" 24#include "llvm/ADT/StringExtras.h" 25#include "llvm/ADT/SmallVector.h" 26#include "llvm/CodeGen/MachineModuleInfo.h" 27#include "llvm/Support/Dwarf.h" 28#include "llvm/Support/IRBuilder.h" 29#include "llvm/Target/TargetMachine.h" 30using namespace clang; 31using namespace clang::CodeGen; 32 33CGDebugInfo::CGDebugInfo(CodeGenModule *m) 34: M(m) 35, CurLoc() 36, PrevLoc() 37, CompileUnitCache() 38, TypeCache() 39, StopPointFn(NULL) 40, FuncStartFn(NULL) 41, DeclareFn(NULL) 42, RegionStartFn(NULL) 43, RegionEndFn(NULL) 44, CompileUnitAnchor(NULL) 45, SubprogramAnchor(NULL) 46, RegionStack() 47, VariableDescList() 48, Subprogram(NULL) 49{ 50 SR = new llvm::DISerializer(); 51 SR->setModule (&M->getModule()); 52} 53 54CGDebugInfo::~CGDebugInfo() 55{ 56 delete SR; 57 58 // Free CompileUnitCache. 59 for (std::map<unsigned, llvm::CompileUnitDesc *>::iterator I 60 = CompileUnitCache.begin(); I != CompileUnitCache.end(); ++I) { 61 delete I->second; 62 } 63 CompileUnitCache.clear(); 64 65 // Free TypeCache. 66 for (std::map<void *, llvm::TypeDesc *>::iterator I 67 = TypeCache.begin(); I != TypeCache.end(); ++I) { 68 delete I->second; 69 } 70 TypeCache.clear(); 71 72 for (std::vector<llvm::DebugInfoDesc *>::iterator I 73 = RegionStack.begin(); I != RegionStack.end(); ++I) { 74 delete *I; 75 } 76 77 for (std::vector<llvm::VariableDesc *>::iterator I 78 = VariableDescList.begin(); I != VariableDescList.end(); ++I) { 79 delete *I; 80 } 81 82 delete CompileUnitAnchor; 83 delete SubprogramAnchor; 84} 85 86void CGDebugInfo::setLocation(SourceLocation loc) 87{ 88 SourceManager &SM = M->getContext().getSourceManager(); 89 CurLoc = SM.getLogicalLoc(loc); 90} 91 92/// getCastValueFor - Return a llvm representation for a given debug information 93/// descriptor cast to an empty struct pointer. 94llvm::Value *CGDebugInfo::getCastValueFor(llvm::DebugInfoDesc *DD) { 95 return llvm::ConstantExpr::getBitCast(SR->Serialize(DD), 96 SR->getEmptyStructPtrType()); 97} 98 99/// getValueFor - Return a llvm representation for a given debug information 100/// descriptor. 101llvm::Value *CGDebugInfo::getValueFor(llvm::DebugInfoDesc *DD) { 102 return SR->Serialize(DD); 103} 104 105/// getOrCreateCompileUnit - Get the compile unit from the cache or create a new 106/// one if necessary. 107llvm::CompileUnitDesc 108*CGDebugInfo::getOrCreateCompileUnit(const SourceLocation Loc) { 109 110 // See if this compile unit has been used before. 111 llvm::CompileUnitDesc *&Slot = CompileUnitCache[Loc.getFileID()]; 112 if (Slot) return Slot; 113 114 // Create new compile unit. 115 // FIXME: Where to free these? 116 // One way is to iterate over the CompileUnitCache in ~CGDebugInfo. 117 llvm::CompileUnitDesc *Unit = new llvm::CompileUnitDesc(); 118 119 // Make sure we have an anchor. 120 if (!CompileUnitAnchor) { 121 CompileUnitAnchor = new llvm::AnchorDesc(Unit); 122 } 123 124 // Get source file information. 125 SourceManager &SM = M->getContext().getSourceManager(); 126 const FileEntry *FE = SM.getFileEntryForLoc(Loc); 127 const char *FileName, *DirName; 128 if (FE) { 129 FileName = FE->getName(); 130 DirName = FE->getDir()->getName(); 131 } else { 132 FileName = SM.getSourceName(Loc); 133 DirName = ""; 134 } 135 136 Unit->setAnchor(CompileUnitAnchor); 137 Unit->setFileName(FileName); 138 Unit->setDirectory(DirName); 139 140 // Set up producer name. 141 // FIXME: Do not know how to get clang version yet. 142 Unit->setProducer("clang"); 143 144 // Set up Language number. 145 // FIXME: Handle other languages as well. 146 Unit->setLanguage(llvm::dwarf::DW_LANG_C89); 147 148 // Update cache. 149 Slot = Unit; 150 151 return Unit; 152} 153 154 155llvm::TypeDesc * 156CGDebugInfo::getOrCreateCVRType(QualType type, llvm::CompileUnitDesc *Unit) 157{ 158 // We will create a Derived type. 159 llvm::DerivedTypeDesc *DTy = NULL; 160 llvm::TypeDesc *FromTy = NULL; 161 162 if (type.isConstQualified()) { 163 DTy = new llvm::DerivedTypeDesc(llvm::dwarf::DW_TAG_const_type); 164 type.removeConst(); 165 FromTy = getOrCreateType(type, Unit); 166 } else if (type.isVolatileQualified()) { 167 DTy = new llvm::DerivedTypeDesc(llvm::dwarf::DW_TAG_volatile_type); 168 type.removeVolatile(); 169 FromTy = getOrCreateType(type, Unit); 170 } else if (type.isRestrictQualified()) { 171 DTy = new llvm::DerivedTypeDesc(llvm::dwarf::DW_TAG_restrict_type); 172 type.removeRestrict(); 173 FromTy = getOrCreateType(type, Unit); 174 } 175 176 // No need to fill in the Name, Line, Size, Alignment, Offset in case of // CVR derived types. 177 DTy->setContext(Unit); 178 DTy->setFromType(FromTy); 179 180 return DTy; 181} 182 183 184/// getOrCreateType - Get the Basic type from the cache or create a new 185/// one if necessary. 186llvm::TypeDesc * 187CGDebugInfo::getOrCreateBuiltinType(QualType type, llvm::CompileUnitDesc *Unit) 188{ 189 assert (type->getTypeClass() == Type::Builtin); 190 191 const BuiltinType *BT = type->getAsBuiltinType(); 192 193 unsigned Encoding = 0; 194 switch (BT->getKind()) 195 { 196 case BuiltinType::Void: 197 return NULL; 198 case BuiltinType::UChar: 199 case BuiltinType::Char_U: 200 Encoding = llvm::dwarf::DW_ATE_unsigned_char; 201 break; 202 case BuiltinType::Char_S: 203 case BuiltinType::SChar: 204 Encoding = llvm::dwarf::DW_ATE_signed_char; 205 break; 206 case BuiltinType::UShort: 207 case BuiltinType::UInt: 208 case BuiltinType::ULong: 209 case BuiltinType::ULongLong: 210 Encoding = llvm::dwarf::DW_ATE_unsigned; 211 break; 212 case BuiltinType::Short: 213 case BuiltinType::Int: 214 case BuiltinType::Long: 215 case BuiltinType::LongLong: 216 Encoding = llvm::dwarf::DW_ATE_signed; 217 break; 218 case BuiltinType::Bool: 219 Encoding = llvm::dwarf::DW_ATE_boolean; 220 break; 221 case BuiltinType::Float: 222 case BuiltinType::Double: 223 Encoding = llvm::dwarf::DW_ATE_float; 224 break; 225 default: 226 Encoding = llvm::dwarf::DW_ATE_signed; 227 break; 228 } 229 230 // Ty will have contain the resulting type. 231 llvm::BasicTypeDesc *BTy = new llvm::BasicTypeDesc(); 232 233 // Get the name and location early to assist debugging. 234 const char *TyName = BT->getName(); 235 236 // Bit size, align and offset of the type. 237 uint64_t Size = M->getContext().getTypeSize(type); 238 uint64_t Align = M->getContext().getTypeAlign(type); 239 uint64_t Offset = 0; 240 241 // If the type is defined, fill in the details. 242 if (BTy) { 243 BTy->setContext(Unit); 244 BTy->setName(TyName); 245 BTy->setSize(Size); 246 BTy->setAlign(Align); 247 BTy->setOffset(Offset); 248 BTy->setEncoding(Encoding); 249 } 250 251 return BTy; 252} 253 254llvm::TypeDesc * 255CGDebugInfo::getOrCreatePointerType(QualType type, llvm::CompileUnitDesc *Unit) 256{ 257 // type* 258 llvm::DerivedTypeDesc *DTy = 259 new llvm::DerivedTypeDesc(llvm::dwarf::DW_TAG_pointer_type); 260 261 // Handle the derived type. 262 const PointerType *PTRT = type->getAsPointerType(); 263 llvm::TypeDesc *FromTy = getOrCreateType(PTRT->getPointeeType(), Unit); 264 265 // Get the name and location early to assist debugging. 266 SourceManager &SM = M->getContext().getSourceManager(); 267 uint64_t Line = SM.getLogicalLineNumber(CurLoc); 268 269 // Bit size, align and offset of the type. 270 uint64_t Size = M->getContext().getTypeSize(type); 271 uint64_t Align = M->getContext().getTypeAlign(type); 272 uint64_t Offset = 0; 273 274 // If the type is defined, fill in the details. 275 if (DTy) { 276 DTy->setContext(Unit); 277 DTy->setLine(Line); 278 DTy->setSize(Size); 279 DTy->setAlign(Align); 280 DTy->setOffset(Offset); 281 DTy->setFromType(FromTy); 282 } 283 284 return DTy; 285} 286 287llvm::TypeDesc * 288CGDebugInfo::getOrCreateTypedefType(QualType type, llvm::CompileUnitDesc *Unit) 289{ 290 // typedefs are derived from some other type. 291 llvm::DerivedTypeDesc *DTy = 292 new llvm::DerivedTypeDesc(llvm::dwarf::DW_TAG_typedef); 293 294 // Handle derived type. 295 const TypedefType *TDT = type->getAsTypedefType(); 296 llvm::TypeDesc *FromTy = getOrCreateType(TDT->LookThroughTypedefs(), 297 Unit); 298 299 // Get the name and location early to assist debugging. 300 const char *TyName = TDT->getDecl()->getName(); 301 SourceManager &SM = M->getContext().getSourceManager(); 302 uint64_t Line = SM.getLogicalLineNumber(TDT->getDecl()->getLocation()); 303 304 // If the type is defined, fill in the details. 305 if (DTy) { 306 DTy->setContext(Unit); 307 DTy->setFile(getOrCreateCompileUnit(TDT->getDecl()->getLocation())); 308 DTy->setLine(Line); 309 DTy->setName(TyName); 310 DTy->setFromType(FromTy); 311 } 312 313 return DTy; 314} 315 316llvm::TypeDesc * 317CGDebugInfo::getOrCreateFunctionType(QualType type, llvm::CompileUnitDesc *Unit) 318{ 319 llvm::CompositeTypeDesc *SubrTy = 320 new llvm::CompositeTypeDesc(llvm::dwarf::DW_TAG_subroutine_type); 321 322 // Prepare to add the arguments for the subroutine. 323 std::vector<llvm::DebugInfoDesc *> &Elements = SubrTy->getElements(); 324 325 // Get result type. 326 const FunctionType *FT = type->getAsFunctionType(); 327 llvm::TypeDesc *ArgTy = getOrCreateType(FT->getResultType(), Unit); 328 if (ArgTy) Elements.push_back(ArgTy); 329 330 // Set up remainder of arguments. 331 if (type->getTypeClass() == Type::FunctionProto) { 332 const FunctionTypeProto *FTPro = dyn_cast<FunctionTypeProto>(type); 333 for (unsigned int i =0; i < FTPro->getNumArgs(); i++) { 334 QualType ParamType = FTPro->getArgType(i); 335 ArgTy = getOrCreateType(ParamType, Unit); 336 if (ArgTy) Elements.push_back(ArgTy); 337 } 338 } 339 340 // FIXME: set other fields file, line here. 341 SubrTy->setContext(Unit); 342 343 return SubrTy; 344} 345 346 347 348/// getOrCreateType - Get the type from the cache or create a new 349/// one if necessary. 350llvm::TypeDesc * 351CGDebugInfo::getOrCreateType(QualType type, llvm::CompileUnitDesc *Unit) 352{ 353 // TODO: Re-enable once we can generate all types 354 return 0; 355 if (type.isNull()) 356 return NULL; 357 358 // Check to see if the compile unit already has created this type. 359 llvm::TypeDesc *&Slot = TypeCache[type.getAsOpaquePtr()]; 360 if (Slot) return Slot; 361 362 // We need to check for the CVR qualifiers as the first thing. 363 if (type.getCVRQualifiers()) { 364 Slot = getOrCreateCVRType (type, Unit); 365 return Slot; 366 } 367 368 // Work out details of type. 369 switch(type->getTypeClass()) { 370 case Type::Complex: 371 case Type::Reference: 372 case Type::ConstantArray: 373 case Type::VariableArray: 374 case Type::IncompleteArray: 375 case Type::Vector: 376 case Type::ExtVector: 377 case Type::Tagged: 378 case Type::ASQual: 379 case Type::ObjCInterface: 380 case Type::ObjCQualifiedInterface: 381 case Type::ObjCQualifiedId: 382 case Type::TypeOfExp: 383 case Type::TypeOfTyp: 384 default: 385 { 386 assert (0 && "Unsupported type"); 387 return NULL; 388 } 389 390 case Type::TypeName: 391 Slot = getOrCreateTypedefType(type, Unit); 392 break; 393 394 case Type::FunctionProto: 395 case Type::FunctionNoProto: 396 Slot = getOrCreateFunctionType(type, Unit); 397 break; 398 399 case Type::Builtin: 400 Slot = getOrCreateBuiltinType(type, Unit); 401 break; 402 403 case Type::Pointer: 404 Slot = getOrCreatePointerType(type, Unit); 405 break; 406 } 407 408 return Slot; 409} 410 411/// EmitFunctionStart - Constructs the debug code for entering a function - 412/// "llvm.dbg.func.start.". 413void CGDebugInfo::EmitFunctionStart(const FunctionDecl *FnDecl, 414 llvm::Function *Fn, 415 llvm::IRBuilder &Builder) 416{ 417 // Create subprogram descriptor. 418 Subprogram = new llvm::SubprogramDesc(); 419 420 // Make sure we have an anchor. 421 if (!SubprogramAnchor) { 422 SubprogramAnchor = new llvm::AnchorDesc(Subprogram); 423 } 424 425 // Get name information. 426 Subprogram->setName(FnDecl->getName()); 427 Subprogram->setFullName(FnDecl->getName()); 428 429 // Gather location information. 430 llvm::CompileUnitDesc *Unit = getOrCreateCompileUnit(CurLoc); 431 SourceManager &SM = M->getContext().getSourceManager(); 432 uint64_t Loc = SM.getLogicalLineNumber(CurLoc); 433 434 // Get Function Type. 435 QualType type = FnDecl->getResultType(); 436 llvm::TypeDesc *SPTy = getOrCreateType(type, Unit); 437 438 Subprogram->setAnchor(SubprogramAnchor); 439 Subprogram->setContext(Unit); 440 Subprogram->setFile(Unit); 441 Subprogram->setLine(Loc); 442 Subprogram->setType(SPTy); 443 Subprogram->setIsStatic(Fn->hasInternalLinkage()); 444 Subprogram->setIsDefinition(true); 445 446 // Lazily construct llvm.dbg.func.start. 447 if (!FuncStartFn) 448 FuncStartFn = llvm::Intrinsic::getDeclaration(&M->getModule(), 449 llvm::Intrinsic::dbg_func_start); 450 451 // Call llvm.dbg.func.start which also implicitly calls llvm.dbg.stoppoint. 452 Builder.CreateCall(FuncStartFn, getCastValueFor(Subprogram), ""); 453 454 // Push function on region stack. 455 RegionStack.push_back(Subprogram); 456} 457 458 459void 460CGDebugInfo::EmitStopPoint(llvm::Function *Fn, llvm::IRBuilder &Builder) 461{ 462 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return; 463 464 // Don't bother if things are the same as last time. 465 SourceManager &SM = M->getContext().getSourceManager(); 466 if (CurLoc == PrevLoc 467 || (SM.getLineNumber(CurLoc) == SM.getLineNumber(PrevLoc) 468 && SM.isFromSameFile(CurLoc, PrevLoc))) 469 return; 470 471 // Update last state. 472 PrevLoc = CurLoc; 473 474 // Get the appropriate compile unit. 475 llvm::CompileUnitDesc *Unit = getOrCreateCompileUnit(CurLoc); 476 477 // Lazily construct llvm.dbg.stoppoint function. 478 if (!StopPointFn) 479 StopPointFn = llvm::Intrinsic::getDeclaration(&M->getModule(), 480 llvm::Intrinsic::dbg_stoppoint); 481 482 uint64_t CurLineNo = SM.getLogicalLineNumber(CurLoc); 483 uint64_t ColumnNo = SM.getLogicalColumnNumber(CurLoc); 484 485 // Invoke llvm.dbg.stoppoint 486 Builder.CreateCall3(StopPointFn, 487 llvm::ConstantInt::get(llvm::Type::Int32Ty, CurLineNo), 488 llvm::ConstantInt::get(llvm::Type::Int32Ty, ColumnNo), 489 getCastValueFor(Unit), ""); 490} 491 492/// EmitRegionStart- Constructs the debug code for entering a declarative 493/// region - "llvm.dbg.region.start.". 494void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, llvm::IRBuilder &Builder) 495{ 496 llvm::BlockDesc *Block = new llvm::BlockDesc(); 497 if (RegionStack.size() > 0) 498 Block->setContext(RegionStack.back()); 499 RegionStack.push_back(Block); 500 501 // Lazily construct llvm.dbg.region.start function. 502 if (!RegionStartFn) 503 RegionStartFn = llvm::Intrinsic::getDeclaration(&M->getModule(), 504 llvm::Intrinsic::dbg_region_start); 505 506 // Call llvm.dbg.func.start. 507 Builder.CreateCall(RegionStartFn, getCastValueFor(Block), ""); 508} 509 510/// EmitRegionEnd - Constructs the debug code for exiting a declarative 511/// region - "llvm.dbg.region.end." 512void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, llvm::IRBuilder &Builder) 513{ 514 // Lazily construct llvm.dbg.region.end function. 515 if (!RegionEndFn) 516 RegionEndFn =llvm::Intrinsic::getDeclaration(&M->getModule(), 517 llvm::Intrinsic::dbg_region_end); 518 519 // Provide an region stop point. 520 EmitStopPoint(Fn, Builder); 521 522 // Call llvm.dbg.func.end. 523 llvm::DebugInfoDesc *DID = RegionStack.back(); 524 Builder.CreateCall(RegionEndFn, getCastValueFor(DID), ""); 525 RegionStack.pop_back(); 526} 527 528/// EmitDeclare - Emit local variable declaration debug info. 529void CGDebugInfo::EmitDeclare(const VarDecl *decl, unsigned Tag, 530 llvm::Value *AI, 531 llvm::IRBuilder &Builder) 532{ 533 // FIXME: If it is a compiler generated temporary then return. 534 535 // Construct llvm.dbg.declare function. 536 if (!DeclareFn) 537 DeclareFn = llvm::Intrinsic::getDeclaration(&M->getModule(), 538 llvm::Intrinsic::dbg_declare); 539 540 // Get type information. 541 llvm::CompileUnitDesc *Unit = getOrCreateCompileUnit(CurLoc); 542 llvm::TypeDesc *TyDesc = getOrCreateType(decl->getType(), Unit); 543 544 SourceManager &SM = M->getContext().getSourceManager(); 545 uint64_t Loc = SM.getLogicalLineNumber(CurLoc); 546 547 // Construct variable. 548 llvm::VariableDesc *Variable = new llvm::VariableDesc(Tag); 549 Variable->setContext(RegionStack.back()); 550 Variable->setName(decl->getName()); 551 Variable->setFile(Unit); 552 Variable->setLine(Loc); 553 Variable->setType(TyDesc); 554 555 // Push it onto the list so that we can free it. 556 VariableDescList.push_back(Variable); 557 558 // Cast the AllocA result to a {}* for the call to llvm.dbg.declare. 559 // These bit cast instructions will get freed when the basic block is 560 // deleted. So do not need to free them explicity. 561 const llvm::PointerType *EmpPtr = SR->getEmptyStructPtrType(); 562 llvm::Value *AllocACast = new llvm::BitCastInst(AI, EmpPtr, decl->getName(), 563 Builder.GetInsertBlock()); 564 565 // Call llvm.dbg.declare. 566 Builder.CreateCall2(DeclareFn, AllocACast, getCastValueFor(Variable), ""); 567} 568 569