blob: 42139353ebe60c5e7cb2610fbfc97eef880031de [file] [log] [blame]
Sanjiv Gupta40e56a12008-05-08 08:54:20 +00001//===--- 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 Gupta93eb8252008-05-25 05:15:42 +000016#include "clang/AST/ASTContext.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000017#include "clang/AST/Decl.h"
18#include "clang/AST/RecordLayout.h"
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000019#include "clang/Basic/SourceManager.h"
20#include "clang/Basic/FileManager.h"
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000021#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 Gupta40e56a12008-05-08 08:54:20 +000026#include "llvm/ADT/StringExtras.h"
27#include "llvm/ADT/SmallVector.h"
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000028#include "llvm/Support/Dwarf.h"
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000029#include "llvm/Target/TargetMachine.h"
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000030using namespace clang;
31using namespace clang::CodeGen;
32
33CGDebugInfo::CGDebugInfo(CodeGenModule *m)
Chris Lattner562ce0a2008-11-10 06:08:34 +000034 : M(m), DebugFactory(M->getModule()) {
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000035}
36
Chris Lattner562ce0a2008-11-10 06:08:34 +000037CGDebugInfo::~CGDebugInfo() {
Daniel Dunbar6fc1f972008-10-17 16:15:48 +000038 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000039}
40
Chris Lattner562ce0a2008-11-10 06:08:34 +000041void CGDebugInfo::setLocation(SourceLocation Loc) {
42 if (Loc.isValid())
43 CurLoc = M->getContext().getSourceManager().getLogicalLoc(Loc);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000044}
45
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000046/// getOrCreateCompileUnit - Get the compile unit from the cache or create a new
Daniel Dunbar4a66ef12008-10-24 08:38:36 +000047/// one if necessary. This returns null for invalid source locations.
Chris Lattner562ce0a2008-11-10 06:08:34 +000048llvm::DICompileUnit CGDebugInfo::getOrCreateCompileUnit(SourceLocation Loc) {
Daniel Dunbar4a66ef12008-10-24 08:38:36 +000049 if (Loc.isInvalid())
Chris Lattner562ce0a2008-11-10 06:08:34 +000050 return llvm::DICompileUnit();
Daniel Dunbar4a66ef12008-10-24 08:38:36 +000051
Daniel Dunbar9865e6f2008-10-24 00:46:51 +000052 SourceManager &SM = M->getContext().getSourceManager();
53 const FileEntry *FE = SM.getFileEntryForLoc(Loc);
Chris Lattner562ce0a2008-11-10 06:08:34 +000054 if (FE == 0) return llvm::DICompileUnit();
55
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000056 // See if this compile unit has been used before.
Chris Lattner562ce0a2008-11-10 06:08:34 +000057 llvm::DICompileUnit &Unit = CompileUnitCache[FE];
58 if (!Unit.isNull()) return Unit;
59
60 // Get source file information.
61 const char *FileName = FE->getName();
62 const char *DirName = FE->getDir()->getName();
Daniel Dunbar9865e6f2008-10-24 00:46:51 +000063
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000064 // Create new compile unit.
Chris Lattner562ce0a2008-11-10 06:08:34 +000065 // FIXME: Handle other language IDs as well.
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000066 // FIXME: Do not know how to get clang version yet.
Chris Lattner562ce0a2008-11-10 06:08:34 +000067 return Unit = DebugFactory.CreateCompileUnit(llvm::dwarf::DW_LANG_C89,
68 FileName, DirName, "clang");
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000069}
70
Chris Lattner562ce0a2008-11-10 06:08:34 +000071/// getOrCreateBuiltinType - Get the Basic type from the cache or create a new
72/// one if necessary.
73llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
74 llvm::DICompileUnit Unit){
75 unsigned Encoding = 0;
76 switch (BT->getKind()) {
77 default:
78 case BuiltinType::Void:
79 return llvm::DIType();
80 case BuiltinType::UChar:
81 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
82 case BuiltinType::Char_S:
83 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
84 case BuiltinType::UShort:
85 case BuiltinType::UInt:
86 case BuiltinType::ULong:
87 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
88 case BuiltinType::Short:
89 case BuiltinType::Int:
90 case BuiltinType::Long:
91 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
92 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
93 case BuiltinType::Float:
94 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
95 }
96 // Bit size, align and offset of the type.
97 uint64_t Size = M->getContext().getTypeSize(BT);
98 uint64_t Align = M->getContext().getTypeAlign(BT);
99 uint64_t Offset = 0;
100
101 return DebugFactory.CreateBasicType(Unit, BT->getName(), Unit, 0, Size, Align,
102 Offset, /*flags*/ 0, Encoding);
103}
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000104
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000105/// getOrCreateCVRType - Get the CVR qualified type from the cache or create
106/// a new one if necessary.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000107llvm::DIType CGDebugInfo::CreateCVRType(QualType Ty, llvm::DICompileUnit Unit) {
108 // We will create one Derived type for one qualifier and recurse to handle any
109 // additional ones.
110 llvm::DIType FromTy;
111 unsigned Tag;
112 if (Ty.isConstQualified()) {
113 Tag = llvm::dwarf::DW_TAG_const_type;
114 Ty.removeConst();
115 FromTy = getOrCreateType(Ty, Unit);
116 } else if (Ty.isVolatileQualified()) {
117 Tag = llvm::dwarf::DW_TAG_volatile_type;
118 Ty.removeVolatile();
119 FromTy = getOrCreateType(Ty, Unit);
120 } else {
121 assert(Ty.isRestrictQualified() && "Unknown type qualifier for debug info");
122 Tag = llvm::dwarf::DW_TAG_restrict_type;
123 Ty.removeRestrict();
124 FromTy = getOrCreateType(Ty, Unit);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000125 }
Chris Lattner562ce0a2008-11-10 06:08:34 +0000126
Daniel Dunbar44252b42008-10-31 03:54:29 +0000127 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
128 // CVR derived types.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000129 return DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
130 0, 0, 0, 0, 0, FromTy);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000131}
132
Chris Lattner562ce0a2008-11-10 06:08:34 +0000133llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
134 llvm::DICompileUnit Unit) {
135 llvm::DIType EltTy = getOrCreateType(Ty->getPointeeType(), Unit);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000136
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000137 // Bit size, align and offset of the type.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000138 uint64_t Size = M->getContext().getTypeSize(Ty);
139 uint64_t Align = M->getContext().getTypeAlign(Ty);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000140
Chris Lattner562ce0a2008-11-10 06:08:34 +0000141 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
142 "", llvm::DICompileUnit(),
143 0, Size, Align, 0, 0, EltTy);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000144}
145
Chris Lattner562ce0a2008-11-10 06:08:34 +0000146llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
147 llvm::DICompileUnit Unit) {
148 // Typedefs are derived from some other type. If we have a typedef of a
149 // typedef, make sure to emit the whole chain.
150 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
151
152 // We don't set size information, but do specify where the typedef was
153 // declared.
154 const char *TyName = Ty->getDecl()->getName();
155 SourceLocation DefLoc = Ty->getDecl()->getLocation();
156 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000157
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000158 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattner562ce0a2008-11-10 06:08:34 +0000159 uint64_t Line = SM.getLogicalLineNumber(DefLoc);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000160
Chris Lattner562ce0a2008-11-10 06:08:34 +0000161 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef, Unit,
162 TyName, DefUnit, Line, 0, 0, 0, 0, Src);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000163}
164
Chris Lattner562ce0a2008-11-10 06:08:34 +0000165llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
166 llvm::DICompileUnit Unit) {
167 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000168
Chris Lattner562ce0a2008-11-10 06:08:34 +0000169 // Add the result type at least.
170 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
171
172 // Set up remainder of arguments if there is a prototype.
173 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
174 if (const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(Ty)) {
175 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
176 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
177 } else {
178 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000179 }
180
Chris Lattner562ce0a2008-11-10 06:08:34 +0000181 llvm::DIArray EltTypeArray =
182 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
183
184 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
185 Unit, "", llvm::DICompileUnit(),
186 0, 0, 0, 0, 0,
187 llvm::DIType(), EltTypeArray);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000188}
189
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000190/// getOrCreateRecordType - get structure or union type.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000191llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
192 llvm::DICompileUnit Unit) {
193 const RecordDecl *Decl = Ty->getDecl();
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000194
Chris Lattner562ce0a2008-11-10 06:08:34 +0000195 unsigned Tag;
196 if (Decl->isStruct())
197 Tag = llvm::dwarf::DW_TAG_structure_type;
198 else if (Decl->isUnion())
199 Tag = llvm::dwarf::DW_TAG_union_type;
200 else {
201 assert(Decl->isClass() && "Unknown RecordType!");
202 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000203 }
204
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000205 SourceManager &SM = M->getContext().getSourceManager();
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000206
Chris Lattner562ce0a2008-11-10 06:08:34 +0000207 // Get overall information about the record type for the debug info.
208 const char *Name = Decl->getName();
209 if (Name == 0) Name = "";
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000210
Chris Lattner562ce0a2008-11-10 06:08:34 +0000211 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 Guptab1e662e2008-06-09 10:47:41 +0000228
Chris Lattner562ce0a2008-11-10 06:08:34 +0000229 // 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);
243
Chris Lattner562ce0a2008-11-10 06:08:34 +0000244 const char *FieldName = Field->getName();
245 if (FieldName == 0) FieldName = "";
246
247 // Get the location for the field.
248 SourceLocation FieldDefLoc = Field->getLocation();
249 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
250 uint64_t FieldLine = SM.getLogicalLineNumber(FieldDefLoc);
251
252 // Bit size, align and offset of the type.
253 uint64_t FieldSize = M->getContext().getTypeSize(Ty);
254 uint64_t FieldAlign = M->getContext().getTypeAlign(Ty);
255 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
256
257 // Create a DW_TAG_member node to remember the offset of this field in the
258 // struct. FIXME: This is an absolutely insane way to capture this
259 // information. When we gut debug info, this should be fixed.
260 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
261 FieldName, FieldDefUnit,
262 FieldLine, FieldSize, FieldAlign,
263 FieldOffset, 0, FieldTy);
Chris Lattner562ce0a2008-11-10 06:08:34 +0000264 EltTys.push_back(FieldTy);
265 }
266
267 llvm::DIArray Elements =
268 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size());
269
270 // Bit size, align and offset of the type.
271 uint64_t Size = M->getContext().getTypeSize(Ty);
272 uint64_t Align = M->getContext().getTypeAlign(Ty);
273
274 llvm::DIType RealDecl =
275 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size,
276 Align, 0, 0, llvm::DIType(), Elements);
277
278 // Now that we have a real decl for the struct, replace anything using the
279 // old decl with the new one. This will recursively update the debug info.
280 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV());
281 FwdDecl.getGV()->eraseFromParent();
282
283 return RealDecl;
284}
285
286llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
287 llvm::DICompileUnit Unit) {
288 EnumDecl *Decl = Ty->getDecl();
289
290 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
291
292 // Create DIEnumerator elements for each enumerator.
293 for (EnumConstantDecl *Elt = Decl->getEnumConstantList(); Elt;
294 Elt = dyn_cast_or_null<EnumConstantDecl>(Elt->getNextDeclarator())) {
295 Enumerators.push_back(DebugFactory.CreateEnumerator(Elt->getName(),
296 Elt->getInitVal().getZExtValue()));
297 }
298
299 // Return a CompositeType for the enum itself.
300 llvm::DIArray EltArray =
301 DebugFactory.GetOrCreateArray(&Enumerators[0], Enumerators.size());
302
303 const char *EnumName = Decl->getName() ? Decl->getName() : "";
304 SourceLocation DefLoc = Decl->getLocation();
305 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
306 SourceManager &SM = M->getContext().getSourceManager();
307 uint64_t Line = SM.getLogicalLineNumber(DefLoc);
308
309 // Size and align of the type.
310 uint64_t Size = M->getContext().getTypeSize(Ty);
311 uint64_t Align = M->getContext().getTypeAlign(Ty);
312
313 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
314 Unit, EnumName, DefUnit, Line,
315 Size, Align, 0, 0,
316 llvm::DIType(), EltArray);
317}
318
319llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
320 llvm::DICompileUnit Unit) {
321 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
322 return CreateType(RT, Unit);
323 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
324 return CreateType(ET, Unit);
325
326 return llvm::DIType();
327}
328
329llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
330 llvm::DICompileUnit Unit) {
331 // Size and align of the whole array, not the element type.
332 uint64_t Size = M->getContext().getTypeSize(Ty);
333 uint64_t Align = M->getContext().getTypeAlign(Ty);
334
335 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
336 // interior arrays, do we care? Why aren't nested arrays represented the
337 // obvious/recursive way?
338 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
339 QualType EltTy(Ty, 0);
340 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000341 uint64_t Upper = 0;
Chris Lattner562ce0a2008-11-10 06:08:34 +0000342 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
343 Upper = CAT->getSize().getZExtValue() - 1;
344 // FIXME: Verify this is right for VLAs.
345 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
346 EltTy = Ty->getElementType();
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000347 }
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000348
Chris Lattner562ce0a2008-11-10 06:08:34 +0000349 llvm::DIArray SubscriptArray =
350 DebugFactory.GetOrCreateArray(&Subscripts[0], Subscripts.size());
351
352 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
353 Unit, "", llvm::DICompileUnit(),
354 0, Size, Align, 0, 0,
355 getOrCreateType(EltTy, Unit),
356 SubscriptArray);
357}
358
359
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000360/// getOrCreateType - Get the type from the cache or create a new
361/// one if necessary.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000362llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
363 llvm::DICompileUnit Unit) {
364 if (Ty.isNull())
365 return llvm::DIType();
366
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000367 // Check to see if the compile unit already has created this type.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000368 llvm::DIType &Slot = TypeCache[Ty.getAsOpaquePtr()];
369 if (!Slot.isNull()) return Slot;
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000370
Chris Lattner562ce0a2008-11-10 06:08:34 +0000371 // Handle CVR qualifiers, which recursively handles what they refer to.
372 if (Ty.getCVRQualifiers())
373 return Slot = CreateCVRType(Ty, Unit);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000374
375 // Work out details of type.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000376 switch (Ty->getTypeClass()) {
377 case Type::Complex:
378 case Type::Reference:
379 case Type::Vector:
380 case Type::ExtVector:
381 case Type::ASQual:
382 case Type::ObjCInterface:
383 case Type::ObjCQualifiedInterface:
384 case Type::ObjCQualifiedId:
385 case Type::TypeOfExp:
386 case Type::TypeOfTyp:
387 default:
388 return llvm::DIType();
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000389
Chris Lattner562ce0a2008-11-10 06:08:34 +0000390 case Type::Builtin: Slot = CreateType(cast<BuiltinType>(Ty), Unit); break;
391 case Type::Pointer: Slot = CreateType(cast<PointerType>(Ty), Unit); break;
392 case Type::TypeName: Slot = CreateType(cast<TypedefType>(Ty), Unit); break;
393 case Type::Tagged: Slot = CreateType(cast<TagType>(Ty), Unit); break;
394 case Type::FunctionProto:
395 case Type::FunctionNoProto:
396 Slot = CreateType(cast<FunctionType>(Ty), Unit);
397 break;
398
399 case Type::ConstantArray:
400 case Type::VariableArray:
401 case Type::IncompleteArray:
402 Slot = CreateType(cast<ArrayType>(Ty), Unit);
403 break;
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000404 }
Chris Lattner562ce0a2008-11-10 06:08:34 +0000405
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000406 return Slot;
407}
408
409/// EmitFunctionStart - Constructs the debug code for entering a function -
410/// "llvm.dbg.func.start.".
Chris Lattner562ce0a2008-11-10 06:08:34 +0000411void CGDebugInfo::EmitFunctionStart(const char *Name, QualType ReturnType,
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000412 llvm::Function *Fn,
Chris Lattner562ce0a2008-11-10 06:08:34 +0000413 CGBuilderTy &Builder) {
414 // FIXME: Why is this using CurLoc???
415 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000416 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattner562ce0a2008-11-10 06:08:34 +0000417 uint64_t LineNo = SM.getLogicalLineNumber(CurLoc);
418
419 llvm::DISubprogram SP =
420 DebugFactory.CreateSubprogram(Unit, Name, Name, "", Unit, LineNo,
421 getOrCreateType(ReturnType, Unit),
422 Fn->hasInternalLinkage(), true/*definition*/);
423
424 DebugFactory.InsertSubprogramStart(SP, Builder.GetInsertBlock());
425
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000426 // Push function on region stack.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000427 RegionStack.push_back(SP);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000428}
429
430
Chris Lattner562ce0a2008-11-10 06:08:34 +0000431void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000432 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
433
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000434 // Don't bother if things are the same as last time.
435 SourceManager &SM = M->getContext().getSourceManager();
436 if (CurLoc == PrevLoc
437 || (SM.getLineNumber(CurLoc) == SM.getLineNumber(PrevLoc)
438 && SM.isFromSameFile(CurLoc, PrevLoc)))
439 return;
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000440
441 // Update last state.
442 PrevLoc = CurLoc;
443
444 // Get the appropriate compile unit.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000445 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
446 DebugFactory.InsertStopPoint(Unit, SM.getLogicalLineNumber(CurLoc),
447 SM.getLogicalColumnNumber(CurLoc),
448 Builder.GetInsertBlock());
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000449}
450
451/// EmitRegionStart- Constructs the debug code for entering a declarative
452/// region - "llvm.dbg.region.start.".
Chris Lattner562ce0a2008-11-10 06:08:34 +0000453void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
454 llvm::DIDescriptor D;
Daniel Dunbar1257f8c2008-10-17 01:07:56 +0000455 if (!RegionStack.empty())
Chris Lattner562ce0a2008-11-10 06:08:34 +0000456 D = RegionStack.back();
457 D = DebugFactory.CreateBlock(D);
458 RegionStack.push_back(D);
459 DebugFactory.InsertRegionStart(D, Builder.GetInsertBlock());
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000460}
461
462/// EmitRegionEnd - Constructs the debug code for exiting a declarative
463/// region - "llvm.dbg.region.end."
Chris Lattner562ce0a2008-11-10 06:08:34 +0000464void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar1257f8c2008-10-17 01:07:56 +0000465 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
466
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000467 // Provide an region stop point.
468 EmitStopPoint(Fn, Builder);
469
Chris Lattner562ce0a2008-11-10 06:08:34 +0000470 DebugFactory.InsertRegionEnd(RegionStack.back(), Builder.GetInsertBlock());
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000471 RegionStack.pop_back();
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000472}
473
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000474/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000475void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
476 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar1257f8c2008-10-17 01:07:56 +0000477 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
478
Chris Lattner562ce0a2008-11-10 06:08:34 +0000479 // Get location information.
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000480 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattner562ce0a2008-11-10 06:08:34 +0000481 uint64_t Line = SM.getLogicalLineNumber(Decl->getLocation());
482 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
483
484 // Create the descriptor for the variable.
485 llvm::DIVariable D =
486 DebugFactory.CreateVariable(Tag, RegionStack.back(), Decl->getName(),
487 Unit, Line,
488 getOrCreateType(Decl->getType(), Unit));
489 // Insert an llvm.dbg.declare into the current block.
490 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000491}
492
Chris Lattner562ce0a2008-11-10 06:08:34 +0000493void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
494 llvm::Value *Storage,
495 CGBuilderTy &Builder) {
496 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
497}
498
499/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
500/// variable declaration.
501void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
502 CGBuilderTy &Builder) {
503 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
504}
505
506
507
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000508/// EmitGlobalVariable - Emit information about a global variable.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000509void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
510 const VarDecl *Decl) {
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000511 // Create global variable debug descriptor.
Chris Lattner562ce0a2008-11-10 06:08:34 +0000512 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000513 SourceManager &SM = M->getContext().getSourceManager();
Chris Lattner562ce0a2008-11-10 06:08:34 +0000514 uint64_t LineNo = SM.getLogicalLineNumber(Decl->getLocation());
515 const char *Name = Decl->getName();
516
517 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo,
518 getOrCreateType(Decl->getType(), Unit),
519 Var->hasInternalLinkage(),
520 true/*definition*/, Var);
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000521}
522