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 | |
Daniel Dunbar | 3845f86 | 2008-10-31 03:54:29 +0000 | [diff] [blame^] | 206 | // No need to fill in the Name, Line, Size, Alignment, Offset in case of |
| 207 | // CVR derived types. |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 208 | DTy->setContext(Unit); |
| 209 | DTy->setFromType(FromTy); |
| 210 | |
| 211 | return DTy; |
| 212 | } |
| 213 | |
| 214 | |
Sanjiv Gupta | f58c27a | 2008-06-07 04:46:53 +0000 | [diff] [blame] | 215 | /// getOrCreateBuiltinType - Get the Basic type from the cache or create a new |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 216 | /// one if necessary. |
| 217 | llvm::TypeDesc * |
| 218 | CGDebugInfo::getOrCreateBuiltinType(QualType type, llvm::CompileUnitDesc *Unit) |
| 219 | { |
| 220 | assert (type->getTypeClass() == Type::Builtin); |
| 221 | |
| 222 | const BuiltinType *BT = type->getAsBuiltinType(); |
| 223 | |
| 224 | unsigned Encoding = 0; |
| 225 | switch (BT->getKind()) |
| 226 | { |
| 227 | case BuiltinType::Void: |
| 228 | return NULL; |
| 229 | case BuiltinType::UChar: |
| 230 | case BuiltinType::Char_U: |
| 231 | Encoding = llvm::dwarf::DW_ATE_unsigned_char; |
| 232 | break; |
| 233 | case BuiltinType::Char_S: |
| 234 | case BuiltinType::SChar: |
| 235 | Encoding = llvm::dwarf::DW_ATE_signed_char; |
| 236 | break; |
| 237 | case BuiltinType::UShort: |
| 238 | case BuiltinType::UInt: |
| 239 | case BuiltinType::ULong: |
| 240 | case BuiltinType::ULongLong: |
| 241 | Encoding = llvm::dwarf::DW_ATE_unsigned; |
| 242 | break; |
| 243 | case BuiltinType::Short: |
| 244 | case BuiltinType::Int: |
| 245 | case BuiltinType::Long: |
| 246 | case BuiltinType::LongLong: |
| 247 | Encoding = llvm::dwarf::DW_ATE_signed; |
| 248 | break; |
| 249 | case BuiltinType::Bool: |
| 250 | Encoding = llvm::dwarf::DW_ATE_boolean; |
| 251 | break; |
| 252 | case BuiltinType::Float: |
| 253 | case BuiltinType::Double: |
| 254 | Encoding = llvm::dwarf::DW_ATE_float; |
| 255 | break; |
| 256 | default: |
| 257 | Encoding = llvm::dwarf::DW_ATE_signed; |
| 258 | break; |
| 259 | } |
| 260 | |
| 261 | // Ty will have contain the resulting type. |
| 262 | llvm::BasicTypeDesc *BTy = new llvm::BasicTypeDesc(); |
| 263 | |
| 264 | // Get the name and location early to assist debugging. |
| 265 | const char *TyName = BT->getName(); |
| 266 | |
| 267 | // Bit size, align and offset of the type. |
| 268 | uint64_t Size = M->getContext().getTypeSize(type); |
| 269 | uint64_t Align = M->getContext().getTypeAlign(type); |
| 270 | uint64_t Offset = 0; |
| 271 | |
| 272 | // If the type is defined, fill in the details. |
| 273 | if (BTy) { |
| 274 | BTy->setContext(Unit); |
| 275 | BTy->setName(TyName); |
| 276 | BTy->setSize(Size); |
| 277 | BTy->setAlign(Align); |
| 278 | BTy->setOffset(Offset); |
| 279 | BTy->setEncoding(Encoding); |
| 280 | } |
| 281 | |
| 282 | return BTy; |
| 283 | } |
| 284 | |
| 285 | llvm::TypeDesc * |
| 286 | CGDebugInfo::getOrCreatePointerType(QualType type, llvm::CompileUnitDesc *Unit) |
| 287 | { |
| 288 | // type* |
| 289 | llvm::DerivedTypeDesc *DTy = |
| 290 | new llvm::DerivedTypeDesc(llvm::dwarf::DW_TAG_pointer_type); |
| 291 | |
| 292 | // Handle the derived type. |
| 293 | const PointerType *PTRT = type->getAsPointerType(); |
| 294 | llvm::TypeDesc *FromTy = getOrCreateType(PTRT->getPointeeType(), Unit); |
| 295 | |
| 296 | // Get the name and location early to assist debugging. |
| 297 | SourceManager &SM = M->getContext().getSourceManager(); |
| 298 | uint64_t Line = SM.getLogicalLineNumber(CurLoc); |
| 299 | |
| 300 | // Bit size, align and offset of the type. |
| 301 | uint64_t Size = M->getContext().getTypeSize(type); |
| 302 | uint64_t Align = M->getContext().getTypeAlign(type); |
| 303 | uint64_t Offset = 0; |
| 304 | |
| 305 | // If the type is defined, fill in the details. |
| 306 | if (DTy) { |
| 307 | DTy->setContext(Unit); |
| 308 | DTy->setLine(Line); |
| 309 | DTy->setSize(Size); |
| 310 | DTy->setAlign(Align); |
| 311 | DTy->setOffset(Offset); |
| 312 | DTy->setFromType(FromTy); |
| 313 | } |
| 314 | |
| 315 | return DTy; |
| 316 | } |
| 317 | |
| 318 | llvm::TypeDesc * |
| 319 | CGDebugInfo::getOrCreateTypedefType(QualType type, llvm::CompileUnitDesc *Unit) |
| 320 | { |
| 321 | // typedefs are derived from some other type. |
| 322 | llvm::DerivedTypeDesc *DTy = |
| 323 | new llvm::DerivedTypeDesc(llvm::dwarf::DW_TAG_typedef); |
| 324 | |
| 325 | // Handle derived type. |
| 326 | const TypedefType *TDT = type->getAsTypedefType(); |
| 327 | llvm::TypeDesc *FromTy = getOrCreateType(TDT->LookThroughTypedefs(), |
| 328 | Unit); |
| 329 | |
| 330 | // Get the name and location early to assist debugging. |
| 331 | const char *TyName = TDT->getDecl()->getName(); |
| 332 | SourceManager &SM = M->getContext().getSourceManager(); |
| 333 | uint64_t Line = SM.getLogicalLineNumber(TDT->getDecl()->getLocation()); |
| 334 | |
| 335 | // If the type is defined, fill in the details. |
| 336 | if (DTy) { |
| 337 | DTy->setContext(Unit); |
| 338 | DTy->setFile(getOrCreateCompileUnit(TDT->getDecl()->getLocation())); |
| 339 | DTy->setLine(Line); |
| 340 | DTy->setName(TyName); |
| 341 | DTy->setFromType(FromTy); |
| 342 | } |
| 343 | |
| 344 | return DTy; |
| 345 | } |
| 346 | |
| 347 | llvm::TypeDesc * |
| 348 | CGDebugInfo::getOrCreateFunctionType(QualType type, llvm::CompileUnitDesc *Unit) |
| 349 | { |
| 350 | llvm::CompositeTypeDesc *SubrTy = |
| 351 | new llvm::CompositeTypeDesc(llvm::dwarf::DW_TAG_subroutine_type); |
| 352 | |
| 353 | // Prepare to add the arguments for the subroutine. |
| 354 | std::vector<llvm::DebugInfoDesc *> &Elements = SubrTy->getElements(); |
| 355 | |
| 356 | // Get result type. |
| 357 | const FunctionType *FT = type->getAsFunctionType(); |
| 358 | llvm::TypeDesc *ArgTy = getOrCreateType(FT->getResultType(), Unit); |
| 359 | if (ArgTy) Elements.push_back(ArgTy); |
| 360 | |
| 361 | // Set up remainder of arguments. |
| 362 | if (type->getTypeClass() == Type::FunctionProto) { |
| 363 | const FunctionTypeProto *FTPro = dyn_cast<FunctionTypeProto>(type); |
| 364 | for (unsigned int i =0; i < FTPro->getNumArgs(); i++) { |
| 365 | QualType ParamType = FTPro->getArgType(i); |
| 366 | ArgTy = getOrCreateType(ParamType, Unit); |
| 367 | if (ArgTy) Elements.push_back(ArgTy); |
| 368 | } |
| 369 | } |
| 370 | |
Mike Stump | 9ea5884 | 2008-06-19 20:57:50 +0000 | [diff] [blame] | 371 | // FIXME: set other fields file, line here. |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 372 | SubrTy->setContext(Unit); |
| 373 | |
| 374 | return SubrTy; |
| 375 | } |
| 376 | |
Sanjiv Gupta | f58c27a | 2008-06-07 04:46:53 +0000 | [diff] [blame] | 377 | /// getOrCreateRecordType - get structure or union type. |
| 378 | llvm::TypeDesc * |
| 379 | CGDebugInfo::getOrCreateRecordType(QualType type, llvm::CompileUnitDesc *Unit) |
| 380 | { |
| 381 | llvm::CompositeTypeDesc *RecType; |
Daniel Dunbar | 3845f86 | 2008-10-31 03:54:29 +0000 | [diff] [blame^] | 382 | if (type->isStructureType()) |
Sanjiv Gupta | f58c27a | 2008-06-07 04:46:53 +0000 | [diff] [blame] | 383 | RecType = new llvm::CompositeTypeDesc(llvm::dwarf::DW_TAG_structure_type); |
Daniel Dunbar | 3845f86 | 2008-10-31 03:54:29 +0000 | [diff] [blame^] | 384 | else if (type->isUnionType()) |
Sanjiv Gupta | f58c27a | 2008-06-07 04:46:53 +0000 | [diff] [blame] | 385 | RecType = new llvm::CompositeTypeDesc(llvm::dwarf::DW_TAG_union_type); |
| 386 | else |
| 387 | return NULL; |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 388 | |
Sanjiv Gupta | f58c27a | 2008-06-07 04:46:53 +0000 | [diff] [blame] | 389 | RecordDecl *RecDecl = type->getAsRecordType()->getDecl(); |
Daniel Dunbar | 5273f51 | 2008-10-17 01:07:56 +0000 | [diff] [blame] | 390 | // We can not get the type for forward declarations. |
| 391 | // FIXME: What *should* we be doing here? |
| 392 | if (!RecDecl->getDefinition(M->getContext())) |
| 393 | return NULL; |
Sanjiv Gupta | f58c27a | 2008-06-07 04:46:53 +0000 | [diff] [blame] | 394 | const ASTRecordLayout &RL = M->getContext().getASTRecordLayout(RecDecl); |
| 395 | |
| 396 | SourceManager &SM = M->getContext().getSourceManager(); |
| 397 | uint64_t Line = SM.getLogicalLineNumber(RecDecl->getLocation()); |
| 398 | |
| 399 | std::vector<llvm::DebugInfoDesc *> &Elements = RecType->getElements(); |
| 400 | |
| 401 | // Add the members. |
| 402 | int NumMembers = RecDecl->getNumMembers(); |
| 403 | for (int i = 0; i < NumMembers; i++) { |
| 404 | FieldDecl *Member = RecDecl->getMember(i); |
| 405 | llvm::TypeDesc *MemberTy = getOrCreateType(Member->getType(), Unit); |
| 406 | MemberTy->setOffset(RL.getFieldOffset(i)); |
| 407 | Elements.push_back(MemberTy); |
| 408 | } |
| 409 | |
| 410 | // Fill in the blanks. |
Daniel Dunbar | 3845f86 | 2008-10-31 03:54:29 +0000 | [diff] [blame^] | 411 | if (RecType) { |
Sanjiv Gupta | f58c27a | 2008-06-07 04:46:53 +0000 | [diff] [blame] | 412 | RecType->setContext(Unit); |
| 413 | RecType->setName(RecDecl->getName()); |
| 414 | RecType->setFile(getOrCreateCompileUnit(RecDecl->getLocation())); |
| 415 | RecType->setLine(Line); |
| 416 | RecType->setSize(RL.getSize()); |
| 417 | RecType->setAlign(RL.getAlignment()); |
| 418 | RecType->setOffset(0); |
| 419 | } |
Daniel Dunbar | 3845f86 | 2008-10-31 03:54:29 +0000 | [diff] [blame^] | 420 | return RecType; |
Sanjiv Gupta | f58c27a | 2008-06-07 04:46:53 +0000 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | /// getOrCreateEnumType - get Enum type. |
| 424 | llvm::TypeDesc * |
| 425 | CGDebugInfo::getOrCreateEnumType(QualType type, llvm::CompileUnitDesc *Unit) |
| 426 | { |
| 427 | llvm::CompositeTypeDesc *EnumTy |
| 428 | = new llvm::CompositeTypeDesc(llvm::dwarf::DW_TAG_enumeration_type); |
| 429 | |
| 430 | EnumType *EType = dyn_cast<EnumType>(type); |
Daniel Dunbar | 3845f86 | 2008-10-31 03:54:29 +0000 | [diff] [blame^] | 431 | if (!EType) return NULL; |
Sanjiv Gupta | f58c27a | 2008-06-07 04:46:53 +0000 | [diff] [blame] | 432 | |
| 433 | EnumDecl *EDecl = EType->getDecl(); |
| 434 | SourceManager &SM = M->getContext().getSourceManager(); |
| 435 | uint64_t Line = SM.getLogicalLineNumber(EDecl->getLocation()); |
| 436 | |
| 437 | // Size, align and offset of the type. |
| 438 | uint64_t Size = M->getContext().getTypeSize(type); |
| 439 | uint64_t Align = M->getContext().getTypeAlign(type); |
| 440 | |
| 441 | // Create descriptors for enum members. |
| 442 | std::vector<llvm::DebugInfoDesc *> &Elements = EnumTy->getElements(); |
| 443 | EnumConstantDecl *ElementList = EDecl->getEnumConstantList(); |
| 444 | while (ElementList) { |
| 445 | llvm::EnumeratorDesc *EnumDesc = new llvm::EnumeratorDesc(); |
| 446 | // push it to the enum desc list so that we can free it later. |
| 447 | EnumDescList.push_back(EnumDesc); |
| 448 | |
| 449 | const char *ElementName = ElementList->getName(); |
| 450 | uint64_t Value = ElementList->getInitVal().getZExtValue(); |
| 451 | |
| 452 | EnumDesc->setName(ElementName); |
| 453 | EnumDesc->setValue(Value); |
| 454 | Elements.push_back(EnumDesc); |
| 455 | if (ElementList->getNextDeclarator()) |
| 456 | ElementList |
| 457 | = dyn_cast<EnumConstantDecl>(ElementList->getNextDeclarator()); |
| 458 | else |
| 459 | break; |
| 460 | } |
| 461 | |
| 462 | // Fill in the blanks. |
| 463 | if (EnumTy) { |
| 464 | EnumTy->setContext(Unit); |
| 465 | EnumTy->setName(EDecl->getName()); |
| 466 | EnumTy->setSize(Size); |
| 467 | EnumTy->setAlign(Align); |
| 468 | EnumTy->setOffset(0); |
| 469 | EnumTy->setFile(getOrCreateCompileUnit(EDecl->getLocation())); |
| 470 | EnumTy->setLine(Line); |
| 471 | } |
| 472 | return EnumTy; |
| 473 | } |
| 474 | |
Sanjiv Gupta | 507de85 | 2008-06-09 10:47:41 +0000 | [diff] [blame] | 475 | /// getOrCreateArrayType - get or create array types. |
| 476 | llvm::TypeDesc * |
| 477 | CGDebugInfo::getOrCreateArrayType(QualType type, llvm::CompileUnitDesc *Unit) |
| 478 | { |
| 479 | llvm::CompositeTypeDesc *ArrayTy |
| 480 | = new llvm::CompositeTypeDesc(llvm::dwarf::DW_TAG_array_type); |
| 481 | |
| 482 | // Size, align and offset of the type. |
| 483 | uint64_t Size = M->getContext().getTypeSize(type); |
| 484 | uint64_t Align = M->getContext().getTypeAlign(type); |
| 485 | |
| 486 | SourceManager &SM = M->getContext().getSourceManager(); |
| 487 | uint64_t Line = SM.getLogicalLineNumber(CurLoc); |
| 488 | |
| 489 | // Add the dimensions of the array. |
| 490 | std::vector<llvm::DebugInfoDesc *> &Elements = ArrayTy->getElements(); |
| 491 | do { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 492 | const ArrayType *AT = M->getContext().getAsArrayType(type); |
Sanjiv Gupta | 507de85 | 2008-06-09 10:47:41 +0000 | [diff] [blame] | 493 | llvm::SubrangeDesc *Subrange = new llvm::SubrangeDesc(); |
| 494 | |
| 495 | // push it back on the subrange desc list so that we can free it later. |
| 496 | SubrangeDescList.push_back(Subrange); |
| 497 | |
| 498 | uint64_t Upper = 0; |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 499 | if (const ConstantArrayType *ConstArrTy = dyn_cast<ConstantArrayType>(AT)) { |
Sanjiv Gupta | 507de85 | 2008-06-09 10:47:41 +0000 | [diff] [blame] | 500 | Upper = ConstArrTy->getSize().getZExtValue() - 1; |
| 501 | } |
| 502 | Subrange->setLo(0); |
| 503 | Subrange->setHi(Upper); |
| 504 | Elements.push_back(Subrange); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 505 | type = AT->getElementType(); |
Sanjiv Gupta | 507de85 | 2008-06-09 10:47:41 +0000 | [diff] [blame] | 506 | } while (type->isArrayType()); |
| 507 | |
| 508 | ArrayTy->setFromType(getOrCreateType(type, Unit)); |
| 509 | |
| 510 | if (ArrayTy) { |
| 511 | ArrayTy->setContext(Unit); |
| 512 | ArrayTy->setSize(Size); |
| 513 | ArrayTy->setAlign(Align); |
| 514 | ArrayTy->setOffset(0); |
| 515 | ArrayTy->setFile(getOrCreateCompileUnit(CurLoc)); |
| 516 | ArrayTy->setLine(Line); |
| 517 | } |
| 518 | return ArrayTy; |
| 519 | } |
| 520 | |
Sanjiv Gupta | f58c27a | 2008-06-07 04:46:53 +0000 | [diff] [blame] | 521 | |
| 522 | /// getOrCreateTaggedType - get or create structure/union/Enum type. |
| 523 | llvm::TypeDesc * |
| 524 | CGDebugInfo::getOrCreateTaggedType(QualType type, llvm::CompileUnitDesc *Unit) |
| 525 | { |
| 526 | if (type->isStructureType() || type->isUnionType()) |
| 527 | return getOrCreateRecordType(type, Unit); |
| 528 | else if (type->isEnumeralType()) |
| 529 | return getOrCreateEnumType(type, Unit); |
| 530 | else |
| 531 | return NULL; |
| 532 | } |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 533 | |
| 534 | /// getOrCreateType - Get the type from the cache or create a new |
| 535 | /// one if necessary. |
| 536 | llvm::TypeDesc * |
| 537 | CGDebugInfo::getOrCreateType(QualType type, llvm::CompileUnitDesc *Unit) |
| 538 | { |
| 539 | if (type.isNull()) |
| 540 | return NULL; |
| 541 | |
| 542 | // Check to see if the compile unit already has created this type. |
| 543 | llvm::TypeDesc *&Slot = TypeCache[type.getAsOpaquePtr()]; |
| 544 | if (Slot) return Slot; |
| 545 | |
| 546 | // We need to check for the CVR qualifiers as the first thing. |
| 547 | if (type.getCVRQualifiers()) { |
| 548 | Slot = getOrCreateCVRType (type, Unit); |
| 549 | return Slot; |
| 550 | } |
| 551 | |
| 552 | // Work out details of type. |
Daniel Dunbar | 3845f86 | 2008-10-31 03:54:29 +0000 | [diff] [blame^] | 553 | switch (type->getTypeClass()) { |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 554 | case Type::Complex: |
| 555 | case Type::Reference: |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 556 | case Type::Vector: |
| 557 | case Type::ExtVector: |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 558 | case Type::ASQual: |
| 559 | case Type::ObjCInterface: |
| 560 | case Type::ObjCQualifiedInterface: |
| 561 | case Type::ObjCQualifiedId: |
| 562 | case Type::TypeOfExp: |
| 563 | case Type::TypeOfTyp: |
| 564 | default: |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 565 | return NULL; |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 566 | |
| 567 | case Type::TypeName: |
| 568 | Slot = getOrCreateTypedefType(type, Unit); |
| 569 | break; |
| 570 | |
| 571 | case Type::FunctionProto: |
| 572 | case Type::FunctionNoProto: |
| 573 | Slot = getOrCreateFunctionType(type, Unit); |
| 574 | break; |
| 575 | |
| 576 | case Type::Builtin: |
| 577 | Slot = getOrCreateBuiltinType(type, Unit); |
| 578 | break; |
| 579 | |
| 580 | case Type::Pointer: |
| 581 | Slot = getOrCreatePointerType(type, Unit); |
| 582 | break; |
Sanjiv Gupta | f58c27a | 2008-06-07 04:46:53 +0000 | [diff] [blame] | 583 | |
| 584 | case Type::Tagged: |
| 585 | Slot = getOrCreateTaggedType(type, Unit); |
| 586 | break; |
Sanjiv Gupta | 507de85 | 2008-06-09 10:47:41 +0000 | [diff] [blame] | 587 | |
| 588 | case Type::ConstantArray: |
| 589 | case Type::VariableArray: |
| 590 | case Type::IncompleteArray: |
| 591 | Slot = getOrCreateArrayType(type, Unit); |
| 592 | break; |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | return Slot; |
| 596 | } |
| 597 | |
| 598 | /// EmitFunctionStart - Constructs the debug code for entering a function - |
| 599 | /// "llvm.dbg.func.start.". |
Daniel Dunbar | 2284ac9 | 2008-10-18 18:22:23 +0000 | [diff] [blame] | 600 | void CGDebugInfo::EmitFunctionStart(const char *Name, |
| 601 | QualType ReturnType, |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 602 | llvm::Function *Fn, |
Chris Lattner | 85e3568 | 2008-08-08 19:57:58 +0000 | [diff] [blame] | 603 | llvm::IRBuilder<> &Builder) |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 604 | { |
| 605 | // Create subprogram descriptor. |
| 606 | Subprogram = new llvm::SubprogramDesc(); |
| 607 | |
| 608 | // Make sure we have an anchor. |
| 609 | if (!SubprogramAnchor) { |
| 610 | SubprogramAnchor = new llvm::AnchorDesc(Subprogram); |
| 611 | } |
| 612 | |
| 613 | // Get name information. |
Daniel Dunbar | 2284ac9 | 2008-10-18 18:22:23 +0000 | [diff] [blame] | 614 | Subprogram->setName(Name); |
| 615 | Subprogram->setFullName(Name); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 616 | |
| 617 | // Gather location information. |
| 618 | llvm::CompileUnitDesc *Unit = getOrCreateCompileUnit(CurLoc); |
| 619 | SourceManager &SM = M->getContext().getSourceManager(); |
| 620 | uint64_t Loc = SM.getLogicalLineNumber(CurLoc); |
| 621 | |
| 622 | // Get Function Type. |
Daniel Dunbar | 2284ac9 | 2008-10-18 18:22:23 +0000 | [diff] [blame] | 623 | llvm::TypeDesc *SPTy = getOrCreateType(ReturnType, Unit); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 624 | |
| 625 | Subprogram->setAnchor(SubprogramAnchor); |
| 626 | Subprogram->setContext(Unit); |
| 627 | Subprogram->setFile(Unit); |
| 628 | Subprogram->setLine(Loc); |
| 629 | Subprogram->setType(SPTy); |
| 630 | Subprogram->setIsStatic(Fn->hasInternalLinkage()); |
| 631 | Subprogram->setIsDefinition(true); |
| 632 | |
| 633 | // Lazily construct llvm.dbg.func.start. |
| 634 | if (!FuncStartFn) |
| 635 | FuncStartFn = llvm::Intrinsic::getDeclaration(&M->getModule(), |
| 636 | llvm::Intrinsic::dbg_func_start); |
| 637 | |
| 638 | // Call llvm.dbg.func.start which also implicitly calls llvm.dbg.stoppoint. |
| 639 | Builder.CreateCall(FuncStartFn, getCastValueFor(Subprogram), ""); |
| 640 | |
| 641 | // Push function on region stack. |
| 642 | RegionStack.push_back(Subprogram); |
| 643 | } |
| 644 | |
| 645 | |
| 646 | void |
Chris Lattner | 85e3568 | 2008-08-08 19:57:58 +0000 | [diff] [blame] | 647 | CGDebugInfo::EmitStopPoint(llvm::Function *Fn, llvm::IRBuilder<> &Builder) |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 648 | { |
| 649 | if (CurLoc.isInvalid() || CurLoc.isMacroID()) return; |
| 650 | |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 651 | // Don't bother if things are the same as last time. |
| 652 | SourceManager &SM = M->getContext().getSourceManager(); |
| 653 | if (CurLoc == PrevLoc |
| 654 | || (SM.getLineNumber(CurLoc) == SM.getLineNumber(PrevLoc) |
| 655 | && SM.isFromSameFile(CurLoc, PrevLoc))) |
| 656 | return; |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 657 | |
| 658 | // Update last state. |
| 659 | PrevLoc = CurLoc; |
| 660 | |
| 661 | // Get the appropriate compile unit. |
| 662 | llvm::CompileUnitDesc *Unit = getOrCreateCompileUnit(CurLoc); |
| 663 | |
| 664 | // Lazily construct llvm.dbg.stoppoint function. |
| 665 | if (!StopPointFn) |
| 666 | StopPointFn = llvm::Intrinsic::getDeclaration(&M->getModule(), |
Eli Friedman | 86eb311 | 2008-05-13 14:40:48 +0000 | [diff] [blame] | 667 | llvm::Intrinsic::dbg_stoppoint); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 668 | |
| 669 | uint64_t CurLineNo = SM.getLogicalLineNumber(CurLoc); |
| 670 | uint64_t ColumnNo = SM.getLogicalColumnNumber(CurLoc); |
| 671 | |
| 672 | // Invoke llvm.dbg.stoppoint |
| 673 | Builder.CreateCall3(StopPointFn, |
Eli Friedman | 86eb311 | 2008-05-13 14:40:48 +0000 | [diff] [blame] | 674 | llvm::ConstantInt::get(llvm::Type::Int32Ty, CurLineNo), |
| 675 | llvm::ConstantInt::get(llvm::Type::Int32Ty, ColumnNo), |
| 676 | getCastValueFor(Unit), ""); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | /// EmitRegionStart- Constructs the debug code for entering a declarative |
| 680 | /// region - "llvm.dbg.region.start.". |
Chris Lattner | 85e3568 | 2008-08-08 19:57:58 +0000 | [diff] [blame] | 681 | void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, |
| 682 | llvm::IRBuilder<> &Builder) |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 683 | { |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 684 | llvm::BlockDesc *Block = new llvm::BlockDesc(); |
Daniel Dunbar | 5273f51 | 2008-10-17 01:07:56 +0000 | [diff] [blame] | 685 | if (!RegionStack.empty()) |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 686 | Block->setContext(RegionStack.back()); |
| 687 | RegionStack.push_back(Block); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 688 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 689 | // Lazily construct llvm.dbg.region.start function. |
| 690 | if (!RegionStartFn) |
| 691 | RegionStartFn = llvm::Intrinsic::getDeclaration(&M->getModule(), |
| 692 | llvm::Intrinsic::dbg_region_start); |
| 693 | |
| 694 | // Call llvm.dbg.func.start. |
| 695 | Builder.CreateCall(RegionStartFn, getCastValueFor(Block), ""); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | /// EmitRegionEnd - Constructs the debug code for exiting a declarative |
| 699 | /// region - "llvm.dbg.region.end." |
Chris Lattner | 85e3568 | 2008-08-08 19:57:58 +0000 | [diff] [blame] | 700 | void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, llvm::IRBuilder<> &Builder) |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 701 | { |
Daniel Dunbar | 5273f51 | 2008-10-17 01:07:56 +0000 | [diff] [blame] | 702 | assert(!RegionStack.empty() && "Region stack mismatch, stack empty!"); |
| 703 | |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 704 | // Lazily construct llvm.dbg.region.end function. |
| 705 | if (!RegionEndFn) |
| 706 | RegionEndFn =llvm::Intrinsic::getDeclaration(&M->getModule(), |
Eli Friedman | 86eb311 | 2008-05-13 14:40:48 +0000 | [diff] [blame] | 707 | llvm::Intrinsic::dbg_region_end); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 708 | |
| 709 | // Provide an region stop point. |
| 710 | EmitStopPoint(Fn, Builder); |
| 711 | |
| 712 | // Call llvm.dbg.func.end. |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 713 | llvm::DebugInfoDesc *DID = RegionStack.back(); |
| 714 | Builder.CreateCall(RegionEndFn, getCastValueFor(DID), ""); |
| 715 | RegionStack.pop_back(); |
Daniel Dunbar | 66031a5 | 2008-10-17 16:15:48 +0000 | [diff] [blame] | 716 | // FIXME: Should be freeing here? |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 717 | } |
| 718 | |
Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 719 | /// EmitDeclare - Emit local variable declaration debug info. |
| 720 | void CGDebugInfo::EmitDeclare(const VarDecl *decl, unsigned Tag, |
| 721 | llvm::Value *AI, |
Chris Lattner | 85e3568 | 2008-08-08 19:57:58 +0000 | [diff] [blame] | 722 | llvm::IRBuilder<> &Builder) |
Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 723 | { |
Daniel Dunbar | 5273f51 | 2008-10-17 01:07:56 +0000 | [diff] [blame] | 724 | assert(!RegionStack.empty() && "Region stack mismatch, stack empty!"); |
| 725 | |
Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 726 | // FIXME: If it is a compiler generated temporary then return. |
| 727 | |
| 728 | // Construct llvm.dbg.declare function. |
| 729 | if (!DeclareFn) |
| 730 | DeclareFn = llvm::Intrinsic::getDeclaration(&M->getModule(), |
Mike Stump | 9ea5884 | 2008-06-19 20:57:50 +0000 | [diff] [blame] | 731 | llvm::Intrinsic::dbg_declare); |
Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 732 | |
| 733 | // Get type information. |
| 734 | llvm::CompileUnitDesc *Unit = getOrCreateCompileUnit(CurLoc); |
| 735 | llvm::TypeDesc *TyDesc = getOrCreateType(decl->getType(), Unit); |
| 736 | |
| 737 | SourceManager &SM = M->getContext().getSourceManager(); |
| 738 | uint64_t Loc = SM.getLogicalLineNumber(CurLoc); |
| 739 | |
| 740 | // Construct variable. |
| 741 | llvm::VariableDesc *Variable = new llvm::VariableDesc(Tag); |
| 742 | Variable->setContext(RegionStack.back()); |
| 743 | Variable->setName(decl->getName()); |
| 744 | Variable->setFile(Unit); |
| 745 | Variable->setLine(Loc); |
| 746 | Variable->setType(TyDesc); |
| 747 | |
| 748 | // Push it onto the list so that we can free it. |
| 749 | VariableDescList.push_back(Variable); |
| 750 | |
| 751 | // Cast the AllocA result to a {}* for the call to llvm.dbg.declare. |
| 752 | // These bit cast instructions will get freed when the basic block is |
| 753 | // deleted. So do not need to free them explicity. |
| 754 | const llvm::PointerType *EmpPtr = SR->getEmptyStructPtrType(); |
| 755 | llvm::Value *AllocACast = new llvm::BitCastInst(AI, EmpPtr, decl->getName(), |
| 756 | Builder.GetInsertBlock()); |
| 757 | |
| 758 | // Call llvm.dbg.declare. |
| 759 | Builder.CreateCall2(DeclareFn, AllocACast, getCastValueFor(Variable), ""); |
| 760 | } |
| 761 | |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 762 | /// EmitGlobalVariable - Emit information about a global variable. |
| 763 | void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *GV, |
| 764 | const VarDecl *decl) |
| 765 | { |
| 766 | // Create global variable debug descriptor. |
| 767 | llvm::GlobalVariableDesc *Global = new llvm::GlobalVariableDesc(); |
| 768 | |
| 769 | // Push it onto the list so that we can free it. |
| 770 | GlobalVarDescList.push_back(Global); |
| 771 | |
| 772 | // Make sure we have an anchor. |
| 773 | if (!GlobalVariableAnchor) |
| 774 | GlobalVariableAnchor = new llvm::AnchorDesc(Global); |
| 775 | |
| 776 | // Get name information. |
| 777 | Global->setName(decl->getName()); |
| 778 | Global->setFullName(decl->getName()); |
| 779 | |
| 780 | llvm::CompileUnitDesc *Unit = getOrCreateCompileUnit(CurLoc); |
| 781 | SourceManager &SM = M->getContext().getSourceManager(); |
| 782 | uint64_t Loc = SM.getLogicalLineNumber(CurLoc); |
| 783 | |
| 784 | llvm::TypeDesc *TyD = getOrCreateType(decl->getType(), Unit); |
| 785 | |
| 786 | // Fill in the Global information. |
| 787 | Global->setAnchor(GlobalVariableAnchor); |
| 788 | Global->setContext(Unit); |
| 789 | Global->setFile(Unit); |
| 790 | Global->setLine(Loc); |
| 791 | Global->setType(TyD); |
| 792 | Global->setIsDefinition(true); |
| 793 | Global->setIsStatic(GV->hasInternalLinkage()); |
| 794 | Global->setGlobalVariable(GV); |
| 795 | |
| 796 | // Make sure global is created if needed. |
| 797 | getValueFor(Global); |
| 798 | } |
| 799 | |