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" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 17 | #include "clang/AST/Decl.h" |
| 18 | #include "clang/AST/RecordLayout.h" |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 19 | #include "clang/Basic/SourceManager.h" |
| 20 | #include "clang/Basic/FileManager.h" |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 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" |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/StringExtras.h" |
| 27 | #include "llvm/ADT/SmallVector.h" |
| 28 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Dwarf.h" |
| 30 | #include "llvm/Support/IRBuilder.h" |
| 31 | #include "llvm/Target/TargetMachine.h" |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 32 | using namespace clang; |
| 33 | using namespace clang::CodeGen; |
| 34 | |
| 35 | CGDebugInfo::CGDebugInfo(CodeGenModule *m) |
| 36 | : M(m) |
| 37 | , CurLoc() |
| 38 | , PrevLoc() |
| 39 | , CompileUnitCache() |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 40 | , TypeCache() |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 41 | , StopPointFn(NULL) |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 42 | , FuncStartFn(NULL) |
| 43 | , DeclareFn(NULL) |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 44 | , RegionStartFn(NULL) |
| 45 | , RegionEndFn(NULL) |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 46 | , CompileUnitAnchor(NULL) |
| 47 | , SubprogramAnchor(NULL) |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 48 | , GlobalVariableAnchor(NULL) |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 49 | , RegionStack() |
Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 50 | , VariableDescList() |
Nuno Lopes | 3cd1a2d | 2008-06-08 10:16:34 +0000 | [diff] [blame] | 51 | , GlobalVarDescList() |
| 52 | , EnumDescList() |
Sanjiv Gupta | 507de85 | 2008-06-09 10:47:41 +0000 | [diff] [blame] | 53 | , SubrangeDescList() |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 54 | , Subprogram(NULL) |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 55 | { |
| 56 | SR = new llvm::DISerializer(); |
| 57 | SR->setModule (&M->getModule()); |
| 58 | } |
| 59 | |
| 60 | CGDebugInfo::~CGDebugInfo() |
| 61 | { |
Daniel Dunbar | 66031a5 | 2008-10-17 16:15:48 +0000 | [diff] [blame] | 62 | assert(RegionStack.empty() && "Region stack mismatch, stack not empty!"); |
| 63 | |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 64 | delete SR; |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 65 | |
| 66 | // Free CompileUnitCache. |
Daniel Dunbar | 2104bf9 | 2008-10-24 00:46:51 +0000 | [diff] [blame] | 67 | for (std::map<const FileEntry*, llvm::CompileUnitDesc *>::iterator I |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 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 | |
Sanjiv Gupta | f58c27a | 2008-06-07 04:46:53 +0000 | [diff] [blame] | 80 | // Free region descriptors. |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 81 | for (std::vector<llvm::DebugInfoDesc *>::iterator I |
| 82 | = RegionStack.begin(); I != RegionStack.end(); ++I) { |
| 83 | delete *I; |
| 84 | } |
Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 85 | |
Sanjiv Gupta | f58c27a | 2008-06-07 04:46:53 +0000 | [diff] [blame] | 86 | // Free local var descriptors. |
Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 87 | for (std::vector<llvm::VariableDesc *>::iterator I |
| 88 | = VariableDescList.begin(); I != VariableDescList.end(); ++I) { |
| 89 | delete *I; |
| 90 | } |
| 91 | |
Sanjiv Gupta | f58c27a | 2008-06-07 04:46:53 +0000 | [diff] [blame] | 92 | // Free global var descriptors. |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 93 | for (std::vector<llvm::GlobalVariableDesc *>::iterator I |
| 94 | = GlobalVarDescList.begin(); I != GlobalVarDescList.end(); ++I) { |
| 95 | delete *I; |
| 96 | } |
| 97 | |
Sanjiv Gupta | f58c27a | 2008-06-07 04:46:53 +0000 | [diff] [blame] | 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 | |
Sanjiv Gupta | 507de85 | 2008-06-09 10:47:41 +0000 | [diff] [blame] | 104 | // Free subrange descriptors. |
| 105 | for (std::vector<llvm::SubrangeDesc *>::iterator I |
| 106 | = SubrangeDescList.begin(); I != SubrangeDescList.end(); ++I) { |
| 107 | delete *I; |
| 108 | } |
| 109 | |
Eli Friedman | 3f2af10 | 2008-05-22 01:40:10 +0000 | [diff] [blame] | 110 | delete CompileUnitAnchor; |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 111 | delete SubprogramAnchor; |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 112 | delete GlobalVariableAnchor; |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 115 | void CGDebugInfo::setLocation(SourceLocation loc) { |
Daniel Dunbar | 66031a5 | 2008-10-17 16:15:48 +0000 | [diff] [blame] | 116 | if (loc.isValid()) |
| 117 | CurLoc = M->getContext().getSourceManager().getLogicalLoc(loc); |
Eli Friedman | 32ea35f | 2008-05-29 11:08:17 +0000 | [diff] [blame] | 118 | } |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 119 | |
| 120 | /// getCastValueFor - Return a llvm representation for a given debug information |
| 121 | /// descriptor cast to an empty struct pointer. |
| 122 | llvm::Value *CGDebugInfo::getCastValueFor(llvm::DebugInfoDesc *DD) { |
| 123 | return llvm::ConstantExpr::getBitCast(SR->Serialize(DD), |
Eli Friedman | 86eb311 | 2008-05-13 14:40:48 +0000 | [diff] [blame] | 124 | SR->getEmptyStructPtrType()); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 127 | /// getValueFor - Return a llvm representation for a given debug information |
| 128 | /// descriptor. |
| 129 | llvm::Value *CGDebugInfo::getValueFor(llvm::DebugInfoDesc *DD) { |
| 130 | return SR->Serialize(DD); |
| 131 | } |
| 132 | |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 133 | /// getOrCreateCompileUnit - Get the compile unit from the cache or create a new |
Daniel Dunbar | 25f51dd | 2008-10-24 08:38:36 +0000 | [diff] [blame^] | 134 | /// one if necessary. This returns null for invalid source locations. |
| 135 | llvm::CompileUnitDesc* |
| 136 | CGDebugInfo::getOrCreateCompileUnit(const SourceLocation Loc) { |
| 137 | if (Loc.isInvalid()) |
| 138 | return NULL; |
| 139 | |
Daniel Dunbar | 2104bf9 | 2008-10-24 00:46:51 +0000 | [diff] [blame] | 140 | SourceManager &SM = M->getContext().getSourceManager(); |
| 141 | const FileEntry *FE = SM.getFileEntryForLoc(Loc); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 142 | |
| 143 | // See if this compile unit has been used before. |
Daniel Dunbar | 2104bf9 | 2008-10-24 00:46:51 +0000 | [diff] [blame] | 144 | llvm::CompileUnitDesc *&Unit = CompileUnitCache[FE]; |
| 145 | if (Unit) return Unit; |
| 146 | |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 147 | // Create new compile unit. |
| 148 | // FIXME: Where to free these? |
| 149 | // One way is to iterate over the CompileUnitCache in ~CGDebugInfo. |
Daniel Dunbar | 2104bf9 | 2008-10-24 00:46:51 +0000 | [diff] [blame] | 150 | Unit = new llvm::CompileUnitDesc(); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 151 | |
| 152 | // Make sure we have an anchor. |
| 153 | if (!CompileUnitAnchor) { |
| 154 | CompileUnitAnchor = new llvm::AnchorDesc(Unit); |
| 155 | } |
| 156 | |
| 157 | // Get source file information. |
Eli Friedman | 3f2af10 | 2008-05-22 01:40:10 +0000 | [diff] [blame] | 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 | } |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 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 | |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 179 | return Unit; |
| 180 | } |
| 181 | |
| 182 | |
Sanjiv Gupta | f58c27a | 2008-06-07 04:46:53 +0000 | [diff] [blame] | 183 | /// getOrCreateCVRType - Get the CVR qualified type from the cache or create |
| 184 | /// a new one if necessary. |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 185 | llvm::TypeDesc * |
| 186 | CGDebugInfo::getOrCreateCVRType(QualType type, llvm::CompileUnitDesc *Unit) |
| 187 | { |
| 188 | // We will create a Derived type. |
| 189 | llvm::DerivedTypeDesc *DTy = NULL; |
| 190 | llvm::TypeDesc *FromTy = NULL; |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 191 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 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 | |
Sanjiv Gupta | f58c27a | 2008-06-07 04:46:53 +0000 | [diff] [blame] | 214 | /// getOrCreateBuiltinType - Get the Basic type from the cache or create a new |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 215 | /// one if necessary. |
| 216 | llvm::TypeDesc * |
| 217 | CGDebugInfo::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 | |
| 284 | llvm::TypeDesc * |
| 285 | CGDebugInfo::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 | |
| 317 | llvm::TypeDesc * |
| 318 | CGDebugInfo::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 | |
| 346 | llvm::TypeDesc * |
| 347 | CGDebugInfo::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 | |
Mike Stump | 9ea5884 | 2008-06-19 20:57:50 +0000 | [diff] [blame] | 370 | // FIXME: set other fields file, line here. |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 371 | SubrTy->setContext(Unit); |
| 372 | |
| 373 | return SubrTy; |
| 374 | } |
| 375 | |
Sanjiv Gupta | f58c27a | 2008-06-07 04:46:53 +0000 | [diff] [blame] | 376 | /// getOrCreateRecordType - get structure or union type. |
| 377 | llvm::TypeDesc * |
| 378 | CGDebugInfo::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; |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 387 | |
Sanjiv Gupta | f58c27a | 2008-06-07 04:46:53 +0000 | [diff] [blame] | 388 | RecordDecl *RecDecl = type->getAsRecordType()->getDecl(); |
Daniel Dunbar | 5273f51 | 2008-10-17 01:07:56 +0000 | [diff] [blame] | 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; |
Sanjiv Gupta | f58c27a | 2008-06-07 04:46:53 +0000 | [diff] [blame] | 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. |
| 423 | llvm::TypeDesc * |
| 424 | CGDebugInfo::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 | |
Sanjiv Gupta | 507de85 | 2008-06-09 10:47:41 +0000 | [diff] [blame] | 474 | /// getOrCreateArrayType - get or create array types. |
| 475 | llvm::TypeDesc * |
| 476 | CGDebugInfo::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 { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 491 | const ArrayType *AT = M->getContext().getAsArrayType(type); |
Sanjiv Gupta | 507de85 | 2008-06-09 10:47:41 +0000 | [diff] [blame] | 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; |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 498 | if (const ConstantArrayType *ConstArrTy = dyn_cast<ConstantArrayType>(AT)) { |
Sanjiv Gupta | 507de85 | 2008-06-09 10:47:41 +0000 | [diff] [blame] | 499 | Upper = ConstArrTy->getSize().getZExtValue() - 1; |
| 500 | } |
| 501 | Subrange->setLo(0); |
| 502 | Subrange->setHi(Upper); |
| 503 | Elements.push_back(Subrange); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 504 | type = AT->getElementType(); |
Sanjiv Gupta | 507de85 | 2008-06-09 10:47:41 +0000 | [diff] [blame] | 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 | |
Sanjiv Gupta | f58c27a | 2008-06-07 04:46:53 +0000 | [diff] [blame] | 520 | |
| 521 | /// getOrCreateTaggedType - get or create structure/union/Enum type. |
| 522 | llvm::TypeDesc * |
| 523 | CGDebugInfo::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 | } |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 532 | |
| 533 | /// getOrCreateType - Get the type from the cache or create a new |
| 534 | /// one if necessary. |
| 535 | llvm::TypeDesc * |
| 536 | CGDebugInfo::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: |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 555 | case Type::Vector: |
| 556 | case Type::ExtVector: |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 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 | { |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 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; |
Sanjiv Gupta | f58c27a | 2008-06-07 04:46:53 +0000 | [diff] [blame] | 584 | |
| 585 | case Type::Tagged: |
| 586 | Slot = getOrCreateTaggedType(type, Unit); |
| 587 | break; |
Sanjiv Gupta | 507de85 | 2008-06-09 10:47:41 +0000 | [diff] [blame] | 588 | |
| 589 | case Type::ConstantArray: |
| 590 | case Type::VariableArray: |
| 591 | case Type::IncompleteArray: |
| 592 | Slot = getOrCreateArrayType(type, Unit); |
| 593 | break; |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | return Slot; |
| 597 | } |
| 598 | |
| 599 | /// EmitFunctionStart - Constructs the debug code for entering a function - |
| 600 | /// "llvm.dbg.func.start.". |
Daniel Dunbar | 2284ac9 | 2008-10-18 18:22:23 +0000 | [diff] [blame] | 601 | void CGDebugInfo::EmitFunctionStart(const char *Name, |
| 602 | QualType ReturnType, |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 603 | llvm::Function *Fn, |
Chris Lattner | 85e3568 | 2008-08-08 19:57:58 +0000 | [diff] [blame] | 604 | llvm::IRBuilder<> &Builder) |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 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. |
Daniel Dunbar | 2284ac9 | 2008-10-18 18:22:23 +0000 | [diff] [blame] | 615 | Subprogram->setName(Name); |
| 616 | Subprogram->setFullName(Name); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 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. |
Daniel Dunbar | 2284ac9 | 2008-10-18 18:22:23 +0000 | [diff] [blame] | 624 | llvm::TypeDesc *SPTy = getOrCreateType(ReturnType, Unit); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 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 | |
| 647 | void |
Chris Lattner | 85e3568 | 2008-08-08 19:57:58 +0000 | [diff] [blame] | 648 | CGDebugInfo::EmitStopPoint(llvm::Function *Fn, llvm::IRBuilder<> &Builder) |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 649 | { |
| 650 | if (CurLoc.isInvalid() || CurLoc.isMacroID()) return; |
| 651 | |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 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; |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 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(), |
Eli Friedman | 86eb311 | 2008-05-13 14:40:48 +0000 | [diff] [blame] | 668 | llvm::Intrinsic::dbg_stoppoint); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 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, |
Eli Friedman | 86eb311 | 2008-05-13 14:40:48 +0000 | [diff] [blame] | 675 | llvm::ConstantInt::get(llvm::Type::Int32Ty, CurLineNo), |
| 676 | llvm::ConstantInt::get(llvm::Type::Int32Ty, ColumnNo), |
| 677 | getCastValueFor(Unit), ""); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | /// EmitRegionStart- Constructs the debug code for entering a declarative |
| 681 | /// region - "llvm.dbg.region.start.". |
Chris Lattner | 85e3568 | 2008-08-08 19:57:58 +0000 | [diff] [blame] | 682 | void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, |
| 683 | llvm::IRBuilder<> &Builder) |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 684 | { |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 685 | llvm::BlockDesc *Block = new llvm::BlockDesc(); |
Daniel Dunbar | 5273f51 | 2008-10-17 01:07:56 +0000 | [diff] [blame] | 686 | if (!RegionStack.empty()) |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 687 | Block->setContext(RegionStack.back()); |
| 688 | RegionStack.push_back(Block); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 689 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 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), ""); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | /// EmitRegionEnd - Constructs the debug code for exiting a declarative |
| 700 | /// region - "llvm.dbg.region.end." |
Chris Lattner | 85e3568 | 2008-08-08 19:57:58 +0000 | [diff] [blame] | 701 | void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, llvm::IRBuilder<> &Builder) |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 702 | { |
Daniel Dunbar | 5273f51 | 2008-10-17 01:07:56 +0000 | [diff] [blame] | 703 | assert(!RegionStack.empty() && "Region stack mismatch, stack empty!"); |
| 704 | |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 705 | // Lazily construct llvm.dbg.region.end function. |
| 706 | if (!RegionEndFn) |
| 707 | RegionEndFn =llvm::Intrinsic::getDeclaration(&M->getModule(), |
Eli Friedman | 86eb311 | 2008-05-13 14:40:48 +0000 | [diff] [blame] | 708 | llvm::Intrinsic::dbg_region_end); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 709 | |
| 710 | // Provide an region stop point. |
| 711 | EmitStopPoint(Fn, Builder); |
| 712 | |
| 713 | // Call llvm.dbg.func.end. |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 714 | llvm::DebugInfoDesc *DID = RegionStack.back(); |
| 715 | Builder.CreateCall(RegionEndFn, getCastValueFor(DID), ""); |
| 716 | RegionStack.pop_back(); |
Daniel Dunbar | 66031a5 | 2008-10-17 16:15:48 +0000 | [diff] [blame] | 717 | // FIXME: Should be freeing here? |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 718 | } |
| 719 | |
Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 720 | /// EmitDeclare - Emit local variable declaration debug info. |
| 721 | void CGDebugInfo::EmitDeclare(const VarDecl *decl, unsigned Tag, |
| 722 | llvm::Value *AI, |
Chris Lattner | 85e3568 | 2008-08-08 19:57:58 +0000 | [diff] [blame] | 723 | llvm::IRBuilder<> &Builder) |
Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 724 | { |
Daniel Dunbar | 5273f51 | 2008-10-17 01:07:56 +0000 | [diff] [blame] | 725 | assert(!RegionStack.empty() && "Region stack mismatch, stack empty!"); |
| 726 | |
Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 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(), |
Mike Stump | 9ea5884 | 2008-06-19 20:57:50 +0000 | [diff] [blame] | 732 | llvm::Intrinsic::dbg_declare); |
Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 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 | |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 763 | /// EmitGlobalVariable - Emit information about a global variable. |
| 764 | void 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 | |