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" |
Chris Lattner | 3cc5c40 | 2008-11-11 07:01:36 +0000 | [diff] [blame] | 18 | #include "clang/AST/Expr.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 19 | #include "clang/AST/RecordLayout.h" |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 20 | #include "clang/Basic/SourceManager.h" |
| 21 | #include "clang/Basic/FileManager.h" |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 22 | #include "llvm/Constants.h" |
| 23 | #include "llvm/DerivedTypes.h" |
| 24 | #include "llvm/Instructions.h" |
| 25 | #include "llvm/Intrinsics.h" |
| 26 | #include "llvm/Module.h" |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/StringExtras.h" |
| 28 | #include "llvm/ADT/SmallVector.h" |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Dwarf.h" |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 30 | #include "llvm/Target/TargetMachine.h" |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 31 | using namespace clang; |
| 32 | using namespace clang::CodeGen; |
| 33 | |
| 34 | CGDebugInfo::CGDebugInfo(CodeGenModule *m) |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 35 | : M(m), DebugFactory(M->getModule()) { |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 38 | CGDebugInfo::~CGDebugInfo() { |
Daniel Dunbar | 66031a5 | 2008-10-17 16:15:48 +0000 | [diff] [blame] | 39 | assert(RegionStack.empty() && "Region stack mismatch, stack not empty!"); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 42 | void CGDebugInfo::setLocation(SourceLocation Loc) { |
| 43 | if (Loc.isValid()) |
| 44 | CurLoc = M->getContext().getSourceManager().getLogicalLoc(Loc); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 47 | /// getOrCreateCompileUnit - Get the compile unit from the cache or create a new |
Daniel Dunbar | 25f51dd | 2008-10-24 08:38:36 +0000 | [diff] [blame] | 48 | /// one if necessary. This returns null for invalid source locations. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 49 | llvm::DICompileUnit CGDebugInfo::getOrCreateCompileUnit(SourceLocation Loc) { |
Daniel Dunbar | 25f51dd | 2008-10-24 08:38:36 +0000 | [diff] [blame] | 50 | if (Loc.isInvalid()) |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 51 | return llvm::DICompileUnit(); |
Daniel Dunbar | 25f51dd | 2008-10-24 08:38:36 +0000 | [diff] [blame] | 52 | |
Daniel Dunbar | 2104bf9 | 2008-10-24 00:46:51 +0000 | [diff] [blame] | 53 | SourceManager &SM = M->getContext().getSourceManager(); |
| 54 | const FileEntry *FE = SM.getFileEntryForLoc(Loc); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 55 | if (FE == 0) return llvm::DICompileUnit(); |
| 56 | |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 57 | // See if this compile unit has been used before. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 58 | llvm::DICompileUnit &Unit = CompileUnitCache[FE]; |
| 59 | if (!Unit.isNull()) return Unit; |
| 60 | |
| 61 | // Get source file information. |
| 62 | const char *FileName = FE->getName(); |
| 63 | const char *DirName = FE->getDir()->getName(); |
Daniel Dunbar | 2104bf9 | 2008-10-24 00:46:51 +0000 | [diff] [blame] | 64 | |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 65 | // Create new compile unit. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 66 | // FIXME: Handle other language IDs as well. |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 67 | // FIXME: Do not know how to get clang version yet. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 68 | return Unit = DebugFactory.CreateCompileUnit(llvm::dwarf::DW_LANG_C89, |
| 69 | FileName, DirName, "clang"); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 72 | /// getOrCreateBuiltinType - Get the Basic type from the cache or create a new |
| 73 | /// one if necessary. |
| 74 | llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT, |
| 75 | llvm::DICompileUnit Unit){ |
| 76 | unsigned Encoding = 0; |
| 77 | switch (BT->getKind()) { |
| 78 | default: |
| 79 | case BuiltinType::Void: |
| 80 | return llvm::DIType(); |
| 81 | case BuiltinType::UChar: |
| 82 | case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break; |
| 83 | case BuiltinType::Char_S: |
| 84 | case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break; |
| 85 | case BuiltinType::UShort: |
| 86 | case BuiltinType::UInt: |
| 87 | case BuiltinType::ULong: |
| 88 | case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break; |
| 89 | case BuiltinType::Short: |
| 90 | case BuiltinType::Int: |
| 91 | case BuiltinType::Long: |
| 92 | case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break; |
| 93 | case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break; |
| 94 | case BuiltinType::Float: |
| 95 | case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break; |
| 96 | } |
| 97 | // Bit size, align and offset of the type. |
| 98 | uint64_t Size = M->getContext().getTypeSize(BT); |
| 99 | uint64_t Align = M->getContext().getTypeAlign(BT); |
| 100 | uint64_t Offset = 0; |
| 101 | |
| 102 | return DebugFactory.CreateBasicType(Unit, BT->getName(), Unit, 0, Size, Align, |
| 103 | Offset, /*flags*/ 0, Encoding); |
| 104 | } |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 105 | |
Sanjiv Gupta | f58c27a | 2008-06-07 04:46:53 +0000 | [diff] [blame] | 106 | /// getOrCreateCVRType - Get the CVR qualified type from the cache or create |
| 107 | /// a new one if necessary. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 108 | llvm::DIType CGDebugInfo::CreateCVRType(QualType Ty, llvm::DICompileUnit Unit) { |
| 109 | // We will create one Derived type for one qualifier and recurse to handle any |
| 110 | // additional ones. |
| 111 | llvm::DIType FromTy; |
| 112 | unsigned Tag; |
| 113 | if (Ty.isConstQualified()) { |
| 114 | Tag = llvm::dwarf::DW_TAG_const_type; |
| 115 | Ty.removeConst(); |
| 116 | FromTy = getOrCreateType(Ty, Unit); |
| 117 | } else if (Ty.isVolatileQualified()) { |
| 118 | Tag = llvm::dwarf::DW_TAG_volatile_type; |
| 119 | Ty.removeVolatile(); |
| 120 | FromTy = getOrCreateType(Ty, Unit); |
| 121 | } else { |
| 122 | assert(Ty.isRestrictQualified() && "Unknown type qualifier for debug info"); |
| 123 | Tag = llvm::dwarf::DW_TAG_restrict_type; |
| 124 | Ty.removeRestrict(); |
| 125 | FromTy = getOrCreateType(Ty, Unit); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 126 | } |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 127 | |
Daniel Dunbar | 3845f86 | 2008-10-31 03:54:29 +0000 | [diff] [blame] | 128 | // No need to fill in the Name, Line, Size, Alignment, Offset in case of |
| 129 | // CVR derived types. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 130 | return DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(), |
| 131 | 0, 0, 0, 0, 0, FromTy); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 134 | llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty, |
| 135 | llvm::DICompileUnit Unit) { |
| 136 | llvm::DIType EltTy = getOrCreateType(Ty->getPointeeType(), Unit); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 137 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 138 | // Bit size, align and offset of the type. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 139 | uint64_t Size = M->getContext().getTypeSize(Ty); |
| 140 | uint64_t Align = M->getContext().getTypeAlign(Ty); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 141 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 142 | return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit, |
| 143 | "", llvm::DICompileUnit(), |
| 144 | 0, Size, Align, 0, 0, EltTy); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 147 | llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty, |
| 148 | llvm::DICompileUnit Unit) { |
| 149 | // Typedefs are derived from some other type. If we have a typedef of a |
| 150 | // typedef, make sure to emit the whole chain. |
| 151 | llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit); |
| 152 | |
| 153 | // We don't set size information, but do specify where the typedef was |
| 154 | // declared. |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 155 | std::string TyName = Ty->getDecl()->getNameAsString(); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 156 | SourceLocation DefLoc = Ty->getDecl()->getLocation(); |
| 157 | llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 158 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 159 | SourceManager &SM = M->getContext().getSourceManager(); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 160 | uint64_t Line = SM.getLogicalLineNumber(DefLoc); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 161 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 162 | return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef, Unit, |
| 163 | TyName, DefUnit, Line, 0, 0, 0, 0, Src); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 166 | llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty, |
| 167 | llvm::DICompileUnit Unit) { |
| 168 | llvm::SmallVector<llvm::DIDescriptor, 16> EltTys; |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 169 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 170 | // Add the result type at least. |
| 171 | EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit)); |
| 172 | |
| 173 | // Set up remainder of arguments if there is a prototype. |
| 174 | // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'! |
| 175 | if (const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(Ty)) { |
| 176 | for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i) |
| 177 | EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit)); |
| 178 | } else { |
| 179 | // FIXME: Handle () case in C. llvm-gcc doesn't do it either. |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 182 | llvm::DIArray EltTypeArray = |
| 183 | DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size()); |
| 184 | |
| 185 | return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type, |
| 186 | Unit, "", llvm::DICompileUnit(), |
| 187 | 0, 0, 0, 0, 0, |
| 188 | llvm::DIType(), EltTypeArray); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Sanjiv Gupta | f58c27a | 2008-06-07 04:46:53 +0000 | [diff] [blame] | 191 | /// getOrCreateRecordType - get structure or union type. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 192 | llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty, |
| 193 | llvm::DICompileUnit Unit) { |
| 194 | const RecordDecl *Decl = Ty->getDecl(); |
Sanjiv Gupta | f58c27a | 2008-06-07 04:46:53 +0000 | [diff] [blame] | 195 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 196 | unsigned Tag; |
| 197 | if (Decl->isStruct()) |
| 198 | Tag = llvm::dwarf::DW_TAG_structure_type; |
| 199 | else if (Decl->isUnion()) |
| 200 | Tag = llvm::dwarf::DW_TAG_union_type; |
| 201 | else { |
| 202 | assert(Decl->isClass() && "Unknown RecordType!"); |
| 203 | Tag = llvm::dwarf::DW_TAG_class_type; |
Sanjiv Gupta | f58c27a | 2008-06-07 04:46:53 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Sanjiv Gupta | 507de85 | 2008-06-09 10:47:41 +0000 | [diff] [blame] | 206 | SourceManager &SM = M->getContext().getSourceManager(); |
Sanjiv Gupta | 507de85 | 2008-06-09 10:47:41 +0000 | [diff] [blame] | 207 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 208 | // Get overall information about the record type for the debug info. |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 209 | std::string Name = Decl->getNameAsString(); |
Sanjiv Gupta | 507de85 | 2008-06-09 10:47:41 +0000 | [diff] [blame] | 210 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 211 | llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation()); |
| 212 | uint64_t Line = SM.getLogicalLineNumber(Decl->getLocation()); |
| 213 | |
| 214 | |
| 215 | // Records and classes and unions can all be recursive. To handle them, we |
| 216 | // first generate a debug descriptor for the struct as a forward declaration. |
| 217 | // Then (if it is a definition) we go through and get debug info for all of |
| 218 | // its members. Finally, we create a descriptor for the complete type (which |
| 219 | // may refer to the forward decl if the struct is recursive) and replace all |
| 220 | // uses of the forward declaration with the final definition. |
| 221 | llvm::DIType FwdDecl = |
| 222 | DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0, |
| 223 | llvm::DIType(), llvm::DIArray()); |
| 224 | |
| 225 | // If this is just a forward declaration, return it. |
| 226 | if (!Decl->getDefinition(M->getContext())) |
| 227 | return FwdDecl; |
Sanjiv Gupta | 507de85 | 2008-06-09 10:47:41 +0000 | [diff] [blame] | 228 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 229 | // Otherwise, insert it into the TypeCache so that recursive uses will find |
| 230 | // it. |
| 231 | TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl; |
| 232 | |
| 233 | // Convert all the elements. |
| 234 | llvm::SmallVector<llvm::DIDescriptor, 16> EltTys; |
| 235 | |
| 236 | const ASTRecordLayout &RL = M->getContext().getASTRecordLayout(Decl); |
| 237 | |
| 238 | unsigned FieldNo = 0; |
| 239 | for (RecordDecl::field_const_iterator I = Decl->field_begin(), |
| 240 | E = Decl->field_end(); I != E; ++I, ++FieldNo) { |
| 241 | FieldDecl *Field = *I; |
| 242 | llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 243 | |
| 244 | std::string FieldName = Field->getNameAsString(); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 245 | |
| 246 | // Get the location for the field. |
| 247 | SourceLocation FieldDefLoc = Field->getLocation(); |
| 248 | llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc); |
| 249 | uint64_t FieldLine = SM.getLogicalLineNumber(FieldDefLoc); |
| 250 | |
| 251 | // Bit size, align and offset of the type. |
| 252 | uint64_t FieldSize = M->getContext().getTypeSize(Ty); |
| 253 | uint64_t FieldAlign = M->getContext().getTypeAlign(Ty); |
| 254 | uint64_t FieldOffset = RL.getFieldOffset(FieldNo); |
| 255 | |
| 256 | // Create a DW_TAG_member node to remember the offset of this field in the |
| 257 | // struct. FIXME: This is an absolutely insane way to capture this |
| 258 | // information. When we gut debug info, this should be fixed. |
| 259 | FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, |
| 260 | FieldName, FieldDefUnit, |
| 261 | FieldLine, FieldSize, FieldAlign, |
| 262 | FieldOffset, 0, FieldTy); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 263 | EltTys.push_back(FieldTy); |
| 264 | } |
| 265 | |
| 266 | llvm::DIArray Elements = |
| 267 | DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size()); |
| 268 | |
| 269 | // Bit size, align and offset of the type. |
| 270 | uint64_t Size = M->getContext().getTypeSize(Ty); |
| 271 | uint64_t Align = M->getContext().getTypeAlign(Ty); |
| 272 | |
| 273 | llvm::DIType RealDecl = |
| 274 | DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size, |
| 275 | Align, 0, 0, llvm::DIType(), Elements); |
| 276 | |
| 277 | // Now that we have a real decl for the struct, replace anything using the |
| 278 | // old decl with the new one. This will recursively update the debug info. |
| 279 | FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV()); |
| 280 | FwdDecl.getGV()->eraseFromParent(); |
| 281 | |
| 282 | return RealDecl; |
| 283 | } |
| 284 | |
| 285 | llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty, |
| 286 | llvm::DICompileUnit Unit) { |
| 287 | EnumDecl *Decl = Ty->getDecl(); |
| 288 | |
| 289 | llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators; |
| 290 | |
| 291 | // Create DIEnumerator elements for each enumerator. |
| 292 | for (EnumConstantDecl *Elt = Decl->getEnumConstantList(); Elt; |
| 293 | Elt = dyn_cast_or_null<EnumConstantDecl>(Elt->getNextDeclarator())) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 294 | Enumerators.push_back(DebugFactory.CreateEnumerator(Elt->getNameAsString(), |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 295 | Elt->getInitVal().getZExtValue())); |
| 296 | } |
| 297 | |
| 298 | // Return a CompositeType for the enum itself. |
| 299 | llvm::DIArray EltArray = |
| 300 | DebugFactory.GetOrCreateArray(&Enumerators[0], Enumerators.size()); |
| 301 | |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 302 | std::string EnumName = Decl->getNameAsString(); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 303 | SourceLocation DefLoc = Decl->getLocation(); |
| 304 | llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc); |
| 305 | SourceManager &SM = M->getContext().getSourceManager(); |
| 306 | uint64_t Line = SM.getLogicalLineNumber(DefLoc); |
| 307 | |
| 308 | // Size and align of the type. |
| 309 | uint64_t Size = M->getContext().getTypeSize(Ty); |
| 310 | uint64_t Align = M->getContext().getTypeAlign(Ty); |
| 311 | |
| 312 | return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type, |
| 313 | Unit, EnumName, DefUnit, Line, |
| 314 | Size, Align, 0, 0, |
| 315 | llvm::DIType(), EltArray); |
| 316 | } |
| 317 | |
| 318 | llvm::DIType CGDebugInfo::CreateType(const TagType *Ty, |
| 319 | llvm::DICompileUnit Unit) { |
| 320 | if (const RecordType *RT = dyn_cast<RecordType>(Ty)) |
| 321 | return CreateType(RT, Unit); |
| 322 | else if (const EnumType *ET = dyn_cast<EnumType>(Ty)) |
| 323 | return CreateType(ET, Unit); |
| 324 | |
| 325 | return llvm::DIType(); |
| 326 | } |
| 327 | |
| 328 | llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty, |
| 329 | llvm::DICompileUnit Unit) { |
| 330 | // Size and align of the whole array, not the element type. |
| 331 | uint64_t Size = M->getContext().getTypeSize(Ty); |
| 332 | uint64_t Align = M->getContext().getTypeAlign(Ty); |
| 333 | |
| 334 | // Add the dimensions of the array. FIXME: This loses CV qualifiers from |
| 335 | // interior arrays, do we care? Why aren't nested arrays represented the |
| 336 | // obvious/recursive way? |
| 337 | llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts; |
| 338 | QualType EltTy(Ty, 0); |
| 339 | while ((Ty = dyn_cast<ArrayType>(EltTy))) { |
Sanjiv Gupta | 507de85 | 2008-06-09 10:47:41 +0000 | [diff] [blame] | 340 | uint64_t Upper = 0; |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 341 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty)) |
| 342 | Upper = CAT->getSize().getZExtValue() - 1; |
| 343 | // FIXME: Verify this is right for VLAs. |
| 344 | Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper)); |
| 345 | EltTy = Ty->getElementType(); |
Sanjiv Gupta | 507de85 | 2008-06-09 10:47:41 +0000 | [diff] [blame] | 346 | } |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 347 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 348 | llvm::DIArray SubscriptArray = |
| 349 | DebugFactory.GetOrCreateArray(&Subscripts[0], Subscripts.size()); |
| 350 | |
| 351 | return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type, |
| 352 | Unit, "", llvm::DICompileUnit(), |
| 353 | 0, Size, Align, 0, 0, |
| 354 | getOrCreateType(EltTy, Unit), |
| 355 | SubscriptArray); |
| 356 | } |
| 357 | |
| 358 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 359 | /// getOrCreateType - Get the type from the cache or create a new |
| 360 | /// one if necessary. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 361 | llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty, |
| 362 | llvm::DICompileUnit Unit) { |
| 363 | if (Ty.isNull()) |
| 364 | return llvm::DIType(); |
| 365 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 366 | // Check to see if the compile unit already has created this type. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 367 | llvm::DIType &Slot = TypeCache[Ty.getAsOpaquePtr()]; |
| 368 | if (!Slot.isNull()) return Slot; |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 369 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 370 | // Handle CVR qualifiers, which recursively handles what they refer to. |
| 371 | if (Ty.getCVRQualifiers()) |
| 372 | return Slot = CreateCVRType(Ty, Unit); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 373 | |
| 374 | // Work out details of type. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 375 | switch (Ty->getTypeClass()) { |
| 376 | case Type::Complex: |
| 377 | case Type::Reference: |
| 378 | case Type::Vector: |
| 379 | case Type::ExtVector: |
| 380 | case Type::ASQual: |
| 381 | case Type::ObjCInterface: |
| 382 | case Type::ObjCQualifiedInterface: |
| 383 | case Type::ObjCQualifiedId: |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 384 | default: |
| 385 | return llvm::DIType(); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 386 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 387 | case Type::Builtin: Slot = CreateType(cast<BuiltinType>(Ty), Unit); break; |
| 388 | case Type::Pointer: Slot = CreateType(cast<PointerType>(Ty), Unit); break; |
| 389 | case Type::TypeName: Slot = CreateType(cast<TypedefType>(Ty), Unit); break; |
| 390 | case Type::Tagged: Slot = CreateType(cast<TagType>(Ty), Unit); break; |
| 391 | case Type::FunctionProto: |
| 392 | case Type::FunctionNoProto: |
Chris Lattner | 3cc5c40 | 2008-11-11 07:01:36 +0000 | [diff] [blame] | 393 | return Slot = CreateType(cast<FunctionType>(Ty), Unit); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 394 | |
| 395 | case Type::ConstantArray: |
| 396 | case Type::VariableArray: |
| 397 | case Type::IncompleteArray: |
Chris Lattner | 3cc5c40 | 2008-11-11 07:01:36 +0000 | [diff] [blame] | 398 | return Slot = CreateType(cast<ArrayType>(Ty), Unit); |
| 399 | case Type::TypeOfExp: |
| 400 | return Slot = getOrCreateType(cast<TypeOfExpr>(Ty)->getUnderlyingExpr() |
| 401 | ->getType(), Unit); |
| 402 | case Type::TypeOfTyp: |
| 403 | return Slot = getOrCreateType(cast<TypeOfType>(Ty)->getUnderlyingType(), |
| 404 | Unit); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 405 | } |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 406 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 407 | return Slot; |
| 408 | } |
| 409 | |
| 410 | /// EmitFunctionStart - Constructs the debug code for entering a function - |
| 411 | /// "llvm.dbg.func.start.". |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 412 | void CGDebugInfo::EmitFunctionStart(const char *Name, QualType ReturnType, |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 413 | llvm::Function *Fn, |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 414 | CGBuilderTy &Builder) { |
| 415 | // FIXME: Why is this using CurLoc??? |
| 416 | llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 417 | SourceManager &SM = M->getContext().getSourceManager(); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 418 | uint64_t LineNo = SM.getLogicalLineNumber(CurLoc); |
| 419 | |
| 420 | llvm::DISubprogram SP = |
| 421 | DebugFactory.CreateSubprogram(Unit, Name, Name, "", Unit, LineNo, |
| 422 | getOrCreateType(ReturnType, Unit), |
| 423 | Fn->hasInternalLinkage(), true/*definition*/); |
| 424 | |
| 425 | DebugFactory.InsertSubprogramStart(SP, Builder.GetInsertBlock()); |
| 426 | |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 427 | // Push function on region stack. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 428 | RegionStack.push_back(SP); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 432 | void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) { |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 433 | if (CurLoc.isInvalid() || CurLoc.isMacroID()) return; |
| 434 | |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 435 | // Don't bother if things are the same as last time. |
| 436 | SourceManager &SM = M->getContext().getSourceManager(); |
| 437 | if (CurLoc == PrevLoc |
| 438 | || (SM.getLineNumber(CurLoc) == SM.getLineNumber(PrevLoc) |
| 439 | && SM.isFromSameFile(CurLoc, PrevLoc))) |
| 440 | return; |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 441 | |
| 442 | // Update last state. |
| 443 | PrevLoc = CurLoc; |
| 444 | |
| 445 | // Get the appropriate compile unit. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 446 | llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc); |
| 447 | DebugFactory.InsertStopPoint(Unit, SM.getLogicalLineNumber(CurLoc), |
| 448 | SM.getLogicalColumnNumber(CurLoc), |
| 449 | Builder.GetInsertBlock()); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | /// EmitRegionStart- Constructs the debug code for entering a declarative |
| 453 | /// region - "llvm.dbg.region.start.". |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 454 | void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) { |
| 455 | llvm::DIDescriptor D; |
Daniel Dunbar | 5273f51 | 2008-10-17 01:07:56 +0000 | [diff] [blame] | 456 | if (!RegionStack.empty()) |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 457 | D = RegionStack.back(); |
| 458 | D = DebugFactory.CreateBlock(D); |
| 459 | RegionStack.push_back(D); |
| 460 | DebugFactory.InsertRegionStart(D, Builder.GetInsertBlock()); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | /// EmitRegionEnd - Constructs the debug code for exiting a declarative |
| 464 | /// region - "llvm.dbg.region.end." |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 465 | void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) { |
Daniel Dunbar | 5273f51 | 2008-10-17 01:07:56 +0000 | [diff] [blame] | 466 | assert(!RegionStack.empty() && "Region stack mismatch, stack empty!"); |
| 467 | |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 468 | // Provide an region stop point. |
| 469 | EmitStopPoint(Fn, Builder); |
| 470 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 471 | DebugFactory.InsertRegionEnd(RegionStack.back(), Builder.GetInsertBlock()); |
Sanjiv Gupta | 1c6a38b | 2008-05-25 05:15:42 +0000 | [diff] [blame] | 472 | RegionStack.pop_back(); |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 475 | /// EmitDeclare - Emit local variable declaration debug info. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 476 | void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag, |
| 477 | llvm::Value *Storage, CGBuilderTy &Builder) { |
Daniel Dunbar | 5273f51 | 2008-10-17 01:07:56 +0000 | [diff] [blame] | 478 | assert(!RegionStack.empty() && "Region stack mismatch, stack empty!"); |
| 479 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 480 | // Get location information. |
Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 481 | SourceManager &SM = M->getContext().getSourceManager(); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 482 | uint64_t Line = SM.getLogicalLineNumber(Decl->getLocation()); |
| 483 | llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation()); |
| 484 | |
| 485 | // Create the descriptor for the variable. |
| 486 | llvm::DIVariable D = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 487 | DebugFactory.CreateVariable(Tag, RegionStack.back(),Decl->getNameAsString(), |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 488 | Unit, Line, |
| 489 | getOrCreateType(Decl->getType(), Unit)); |
| 490 | // Insert an llvm.dbg.declare into the current block. |
| 491 | DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock()); |
Sanjiv Gupta | cc9b163 | 2008-05-30 10:30:31 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 494 | void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl, |
| 495 | llvm::Value *Storage, |
| 496 | CGBuilderTy &Builder) { |
| 497 | EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder); |
| 498 | } |
| 499 | |
| 500 | /// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument |
| 501 | /// variable declaration. |
| 502 | void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI, |
| 503 | CGBuilderTy &Builder) { |
| 504 | EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder); |
| 505 | } |
| 506 | |
| 507 | |
| 508 | |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 509 | /// EmitGlobalVariable - Emit information about a global variable. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 510 | void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var, |
| 511 | const VarDecl *Decl) { |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 512 | // Create global variable debug descriptor. |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 513 | llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation()); |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 514 | SourceManager &SM = M->getContext().getSourceManager(); |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 515 | uint64_t LineNo = SM.getLogicalLineNumber(Decl->getLocation()); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 516 | |
| 517 | std::string Name = Decl->getNameAsString(); |
Anders Carlsson | 4d6e8dd | 2008-11-26 17:40:42 +0000 | [diff] [blame^] | 518 | |
| 519 | QualType T = Decl->getType(); |
| 520 | if (T->isIncompleteArrayType()) { |
| 521 | |
| 522 | // CodeGen turns int[] into int[1] so we'll do the same here. |
| 523 | llvm::APSInt ConstVal(32); |
| 524 | |
| 525 | ConstVal = 1; |
| 526 | QualType ET = M->getContext().getAsArrayType(T)->getElementType(); |
| 527 | |
| 528 | T = M->getContext().getConstantArrayType(ET, ConstVal, |
| 529 | ArrayType::Normal, 0); |
| 530 | } |
| 531 | |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 532 | DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo, |
Anders Carlsson | 4d6e8dd | 2008-11-26 17:40:42 +0000 | [diff] [blame^] | 533 | getOrCreateType(T, Unit), |
Chris Lattner | 9c85ba3 | 2008-11-10 06:08:34 +0000 | [diff] [blame] | 534 | Var->hasInternalLinkage(), |
| 535 | true/*definition*/, Var); |
Sanjiv Gupta | 686226b | 2008-06-05 08:59:10 +0000 | [diff] [blame] | 536 | } |
| 537 | |