Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 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" |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
| 17 | #include "clang/Basic/SourceManager.h" |
| 18 | #include "clang/Basic/FileManager.h" |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 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" |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/StringExtras.h" |
| 25 | #include "llvm/ADT/SmallVector.h" |
| 26 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Dwarf.h" |
| 28 | #include "llvm/Support/IRBuilder.h" |
| 29 | #include "llvm/Target/TargetMachine.h" |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 30 | using namespace clang; |
| 31 | using namespace clang::CodeGen; |
| 32 | |
| 33 | CGDebugInfo::CGDebugInfo(CodeGenModule *m) |
| 34 | : M(m) |
| 35 | , CurLoc() |
| 36 | , PrevLoc() |
| 37 | , CompileUnitCache() |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 38 | , TypeCache() |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 39 | , StopPointFn(NULL) |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 40 | , FuncStartFn(NULL) |
| 41 | , DeclareFn(NULL) |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 42 | , RegionStartFn(NULL) |
| 43 | , RegionEndFn(NULL) |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 44 | , CompileUnitAnchor(NULL) |
| 45 | , SubprogramAnchor(NULL) |
| 46 | , RegionStack() |
Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame^] | 47 | , VariableDescList() |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 48 | , Subprogram(NULL) |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 49 | { |
| 50 | SR = new llvm::DISerializer(); |
| 51 | SR->setModule (&M->getModule()); |
| 52 | } |
| 53 | |
| 54 | CGDebugInfo::~CGDebugInfo() |
| 55 | { |
| 56 | delete SR; |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 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 | } |
Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame^] | 76 | |
| 77 | for (std::vector<llvm::VariableDesc *>::iterator I |
| 78 | = VariableDescList.begin(); I != VariableDescList.end(); ++I) { |
| 79 | delete *I; |
| 80 | } |
| 81 | |
Eli Friedman | 3f2af10 | 2008-05-22 01:40:10 +0000 | [diff] [blame] | 82 | delete CompileUnitAnchor; |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 83 | delete SubprogramAnchor; |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Eli Friedman | 32ea35f | 2008-05-29 11:08:17 +0000 | [diff] [blame] | 86 | void CGDebugInfo::setLocation(SourceLocation loc) |
| 87 | { |
| 88 | SourceManager &SM = M->getContext().getSourceManager(); |
| 89 | CurLoc = SM.getLogicalLoc(loc); |
| 90 | } |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 91 | |
| 92 | /// getCastValueFor - Return a llvm representation for a given debug information |
| 93 | /// descriptor cast to an empty struct pointer. |
| 94 | llvm::Value *CGDebugInfo::getCastValueFor(llvm::DebugInfoDesc *DD) { |
| 95 | return llvm::ConstantExpr::getBitCast(SR->Serialize(DD), |
Eli Friedman | 86eb311 | 2008-05-13 14:40:48 +0000 | [diff] [blame] | 96 | SR->getEmptyStructPtrType()); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 99 | /// getValueFor - Return a llvm representation for a given debug information |
| 100 | /// descriptor. |
| 101 | llvm::Value *CGDebugInfo::getValueFor(llvm::DebugInfoDesc *DD) { |
| 102 | return SR->Serialize(DD); |
| 103 | } |
| 104 | |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 105 | /// getOrCreateCompileUnit - Get the compile unit from the cache or create a new |
| 106 | /// one if necessary. |
| 107 | llvm::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); |
Eli Friedman | 3f2af10 | 2008-05-22 01:40:10 +0000 | [diff] [blame] | 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 | } |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 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 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 155 | llvm::TypeDesc * |
| 156 | CGDebugInfo::getOrCreateCVRType(QualType type, llvm::CompileUnitDesc *Unit) |
| 157 | { |
| 158 | // We will create a Derived type. |
| 159 | llvm::DerivedTypeDesc *DTy = NULL; |
| 160 | llvm::TypeDesc *FromTy = NULL; |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 161 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 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. |
| 186 | llvm::TypeDesc * |
| 187 | CGDebugInfo::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 | |
| 254 | llvm::TypeDesc * |
| 255 | CGDebugInfo::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 | |
| 287 | llvm::TypeDesc * |
| 288 | CGDebugInfo::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 | |
| 316 | llvm::TypeDesc * |
| 317 | CGDebugInfo::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. |
| 350 | llvm::TypeDesc * |
| 351 | CGDebugInfo::getOrCreateType(QualType type, llvm::CompileUnitDesc *Unit) |
| 352 | { |
Eli Friedman | 32ea35f | 2008-05-29 11:08:17 +0000 | [diff] [blame] | 353 | // TODO: Re-enable once we can generate all types |
| 354 | return 0; |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 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.". |
| 413 | void 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 | |
| 459 | void |
| 460 | CGDebugInfo::EmitStopPoint(llvm::Function *Fn, llvm::IRBuilder &Builder) |
| 461 | { |
| 462 | if (CurLoc.isInvalid() || CurLoc.isMacroID()) return; |
| 463 | |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 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; |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 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(), |
Eli Friedman | 86eb311 | 2008-05-13 14:40:48 +0000 | [diff] [blame] | 480 | llvm::Intrinsic::dbg_stoppoint); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 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, |
Eli Friedman | 86eb311 | 2008-05-13 14:40:48 +0000 | [diff] [blame] | 487 | llvm::ConstantInt::get(llvm::Type::Int32Ty, CurLineNo), |
| 488 | llvm::ConstantInt::get(llvm::Type::Int32Ty, ColumnNo), |
| 489 | getCastValueFor(Unit), ""); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | /// EmitRegionStart- Constructs the debug code for entering a declarative |
| 493 | /// region - "llvm.dbg.region.start.". |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 494 | void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, llvm::IRBuilder &Builder) |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 495 | { |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 496 | llvm::BlockDesc *Block = new llvm::BlockDesc(); |
| 497 | if (RegionStack.size() > 0) |
| 498 | Block->setContext(RegionStack.back()); |
| 499 | RegionStack.push_back(Block); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 500 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 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), ""); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | /// EmitRegionEnd - Constructs the debug code for exiting a declarative |
| 511 | /// region - "llvm.dbg.region.end." |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 512 | void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, llvm::IRBuilder &Builder) |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 513 | { |
| 514 | // Lazily construct llvm.dbg.region.end function. |
| 515 | if (!RegionEndFn) |
| 516 | RegionEndFn =llvm::Intrinsic::getDeclaration(&M->getModule(), |
Eli Friedman | 86eb311 | 2008-05-13 14:40:48 +0000 | [diff] [blame] | 517 | llvm::Intrinsic::dbg_region_end); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 518 | |
| 519 | // Provide an region stop point. |
| 520 | EmitStopPoint(Fn, Builder); |
| 521 | |
| 522 | // Call llvm.dbg.func.end. |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 523 | llvm::DebugInfoDesc *DID = RegionStack.back(); |
| 524 | Builder.CreateCall(RegionEndFn, getCastValueFor(DID), ""); |
| 525 | RegionStack.pop_back(); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame^] | 528 | /// EmitDeclare - Emit local variable declaration debug info. |
| 529 | void 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 | |