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