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