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