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