blob: 64ce8f70e8ed95d34ffad97a27a73c29a5c93868 [file] [log] [blame]
Sanjiv Guptae8b9f5b2008-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 Gupta1c6a38b2008-05-25 05:15:42 +000016#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000017#include "clang/AST/Decl.h"
18#include "clang/AST/RecordLayout.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000019#include "clang/Basic/SourceManager.h"
20#include "clang/Basic/FileManager.h"
Sanjiv Guptae8b9f5b2008-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 Guptae8b9f5b2008-05-08 08:54:20 +000026#include "llvm/ADT/StringExtras.h"
27#include "llvm/ADT/SmallVector.h"
28#include "llvm/CodeGen/MachineModuleInfo.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000029#include "llvm/Support/Dwarf.h"
30#include "llvm/Support/IRBuilder.h"
31#include "llvm/Target/TargetMachine.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000032using namespace clang;
33using namespace clang::CodeGen;
34
35CGDebugInfo::CGDebugInfo(CodeGenModule *m)
36: M(m)
37, CurLoc()
38, PrevLoc()
39, CompileUnitCache()
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000040, TypeCache()
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000041, StopPointFn(NULL)
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000042, FuncStartFn(NULL)
43, DeclareFn(NULL)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000044, RegionStartFn(NULL)
45, RegionEndFn(NULL)
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000046, CompileUnitAnchor(NULL)
47, SubprogramAnchor(NULL)
Sanjiv Gupta686226b2008-06-05 08:59:10 +000048, GlobalVariableAnchor(NULL)
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000049, RegionStack()
Sanjiv Guptacc9b1632008-05-30 10:30:31 +000050, VariableDescList()
Nuno Lopes3cd1a2d2008-06-08 10:16:34 +000051, GlobalVarDescList()
52, EnumDescList()
Sanjiv Gupta507de852008-06-09 10:47:41 +000053, SubrangeDescList()
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000054, Subprogram(NULL)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000055{
56 SR = new llvm::DISerializer();
57 SR->setModule (&M->getModule());
58}
59
60CGDebugInfo::~CGDebugInfo()
61{
Daniel Dunbar66031a52008-10-17 16:15:48 +000062 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
63
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000064 delete SR;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000065
66 // Free CompileUnitCache.
Daniel Dunbar2104bf92008-10-24 00:46:51 +000067 for (std::map<const FileEntry*, llvm::CompileUnitDesc *>::iterator I
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000068 = CompileUnitCache.begin(); I != CompileUnitCache.end(); ++I) {
69 delete I->second;
70 }
71 CompileUnitCache.clear();
72
73 // Free TypeCache.
74 for (std::map<void *, llvm::TypeDesc *>::iterator I
75 = TypeCache.begin(); I != TypeCache.end(); ++I) {
76 delete I->second;
77 }
78 TypeCache.clear();
79
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +000080 // Free region descriptors.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000081 for (std::vector<llvm::DebugInfoDesc *>::iterator I
82 = RegionStack.begin(); I != RegionStack.end(); ++I) {
83 delete *I;
84 }
Sanjiv Guptacc9b1632008-05-30 10:30:31 +000085
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +000086 // Free local var descriptors.
Sanjiv Guptacc9b1632008-05-30 10:30:31 +000087 for (std::vector<llvm::VariableDesc *>::iterator I
88 = VariableDescList.begin(); I != VariableDescList.end(); ++I) {
89 delete *I;
90 }
91
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +000092 // Free global var descriptors.
Sanjiv Gupta686226b2008-06-05 08:59:10 +000093 for (std::vector<llvm::GlobalVariableDesc *>::iterator I
94 = GlobalVarDescList.begin(); I != GlobalVarDescList.end(); ++I) {
95 delete *I;
96 }
97
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +000098 // Free enum constants descriptors.
99 for (std::vector<llvm::EnumeratorDesc *>::iterator I
100 = EnumDescList.begin(); I != EnumDescList.end(); ++I) {
101 delete *I;
102 }
103
Sanjiv Gupta507de852008-06-09 10:47:41 +0000104 // Free subrange descriptors.
105 for (std::vector<llvm::SubrangeDesc *>::iterator I
106 = SubrangeDescList.begin(); I != SubrangeDescList.end(); ++I) {
107 delete *I;
108 }
109
Eli Friedman3f2af102008-05-22 01:40:10 +0000110 delete CompileUnitAnchor;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000111 delete SubprogramAnchor;
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000112 delete GlobalVariableAnchor;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000113}
114
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000115void CGDebugInfo::setLocation(SourceLocation loc) {
Daniel Dunbar66031a52008-10-17 16:15:48 +0000116 if (loc.isValid())
117 CurLoc = M->getContext().getSourceManager().getLogicalLoc(loc);
Eli Friedman32ea35f2008-05-29 11:08:17 +0000118}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000119
120/// getCastValueFor - Return a llvm representation for a given debug information
121/// descriptor cast to an empty struct pointer.
122llvm::Value *CGDebugInfo::getCastValueFor(llvm::DebugInfoDesc *DD) {
123 return llvm::ConstantExpr::getBitCast(SR->Serialize(DD),
Eli Friedman86eb3112008-05-13 14:40:48 +0000124 SR->getEmptyStructPtrType());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000125}
126
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000127/// getValueFor - Return a llvm representation for a given debug information
128/// descriptor.
129llvm::Value *CGDebugInfo::getValueFor(llvm::DebugInfoDesc *DD) {
130 return SR->Serialize(DD);
131}
132
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000133/// getOrCreateCompileUnit - Get the compile unit from the cache or create a new
Daniel Dunbar25f51dd2008-10-24 08:38:36 +0000134/// one if necessary. This returns null for invalid source locations.
135llvm::CompileUnitDesc*
136CGDebugInfo::getOrCreateCompileUnit(const SourceLocation Loc) {
137 if (Loc.isInvalid())
138 return NULL;
139
Daniel Dunbar2104bf92008-10-24 00:46:51 +0000140 SourceManager &SM = M->getContext().getSourceManager();
141 const FileEntry *FE = SM.getFileEntryForLoc(Loc);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000142
143 // See if this compile unit has been used before.
Daniel Dunbar2104bf92008-10-24 00:46:51 +0000144 llvm::CompileUnitDesc *&Unit = CompileUnitCache[FE];
145 if (Unit) return Unit;
146
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000147 // Create new compile unit.
148 // FIXME: Where to free these?
149 // One way is to iterate over the CompileUnitCache in ~CGDebugInfo.
Daniel Dunbar2104bf92008-10-24 00:46:51 +0000150 Unit = new llvm::CompileUnitDesc();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000151
152 // Make sure we have an anchor.
153 if (!CompileUnitAnchor) {
154 CompileUnitAnchor = new llvm::AnchorDesc(Unit);
155 }
156
157 // Get source file information.
Eli Friedman3f2af102008-05-22 01:40:10 +0000158 const char *FileName, *DirName;
159 if (FE) {
160 FileName = FE->getName();
161 DirName = FE->getDir()->getName();
162 } else {
163 FileName = SM.getSourceName(Loc);
164 DirName = "";
165 }
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000166
167 Unit->setAnchor(CompileUnitAnchor);
168 Unit->setFileName(FileName);
169 Unit->setDirectory(DirName);
170
171 // Set up producer name.
172 // FIXME: Do not know how to get clang version yet.
173 Unit->setProducer("clang");
174
175 // Set up Language number.
176 // FIXME: Handle other languages as well.
177 Unit->setLanguage(llvm::dwarf::DW_LANG_C89);
178
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000179 return Unit;
180}
181
182
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000183/// getOrCreateCVRType - Get the CVR qualified type from the cache or create
184/// a new one if necessary.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000185llvm::TypeDesc *
186CGDebugInfo::getOrCreateCVRType(QualType type, llvm::CompileUnitDesc *Unit)
187{
188 // We will create a Derived type.
189 llvm::DerivedTypeDesc *DTy = NULL;
190 llvm::TypeDesc *FromTy = NULL;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000191
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000192 if (type.isConstQualified()) {
193 DTy = new llvm::DerivedTypeDesc(llvm::dwarf::DW_TAG_const_type);
194 type.removeConst();
195 FromTy = getOrCreateType(type, Unit);
196 } else if (type.isVolatileQualified()) {
197 DTy = new llvm::DerivedTypeDesc(llvm::dwarf::DW_TAG_volatile_type);
198 type.removeVolatile();
199 FromTy = getOrCreateType(type, Unit);
200 } else if (type.isRestrictQualified()) {
201 DTy = new llvm::DerivedTypeDesc(llvm::dwarf::DW_TAG_restrict_type);
202 type.removeRestrict();
203 FromTy = getOrCreateType(type, Unit);
204 }
205
Daniel Dunbar3845f862008-10-31 03:54:29 +0000206 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
207 // CVR derived types.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000208 DTy->setContext(Unit);
209 DTy->setFromType(FromTy);
210
211 return DTy;
212}
213
214
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000215/// getOrCreateBuiltinType - Get the Basic type from the cache or create a new
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000216/// one if necessary.
217llvm::TypeDesc *
218CGDebugInfo::getOrCreateBuiltinType(QualType type, llvm::CompileUnitDesc *Unit)
219{
220 assert (type->getTypeClass() == Type::Builtin);
221
222 const BuiltinType *BT = type->getAsBuiltinType();
223
224 unsigned Encoding = 0;
225 switch (BT->getKind())
226 {
227 case BuiltinType::Void:
228 return NULL;
229 case BuiltinType::UChar:
230 case BuiltinType::Char_U:
231 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
232 break;
233 case BuiltinType::Char_S:
234 case BuiltinType::SChar:
235 Encoding = llvm::dwarf::DW_ATE_signed_char;
236 break;
237 case BuiltinType::UShort:
238 case BuiltinType::UInt:
239 case BuiltinType::ULong:
240 case BuiltinType::ULongLong:
241 Encoding = llvm::dwarf::DW_ATE_unsigned;
242 break;
243 case BuiltinType::Short:
244 case BuiltinType::Int:
245 case BuiltinType::Long:
246 case BuiltinType::LongLong:
247 Encoding = llvm::dwarf::DW_ATE_signed;
248 break;
249 case BuiltinType::Bool:
250 Encoding = llvm::dwarf::DW_ATE_boolean;
251 break;
252 case BuiltinType::Float:
253 case BuiltinType::Double:
254 Encoding = llvm::dwarf::DW_ATE_float;
255 break;
256 default:
257 Encoding = llvm::dwarf::DW_ATE_signed;
258 break;
259 }
260
261 // Ty will have contain the resulting type.
262 llvm::BasicTypeDesc *BTy = new llvm::BasicTypeDesc();
263
264 // Get the name and location early to assist debugging.
265 const char *TyName = BT->getName();
266
267 // Bit size, align and offset of the type.
268 uint64_t Size = M->getContext().getTypeSize(type);
269 uint64_t Align = M->getContext().getTypeAlign(type);
270 uint64_t Offset = 0;
271
272 // If the type is defined, fill in the details.
273 if (BTy) {
274 BTy->setContext(Unit);
275 BTy->setName(TyName);
276 BTy->setSize(Size);
277 BTy->setAlign(Align);
278 BTy->setOffset(Offset);
279 BTy->setEncoding(Encoding);
280 }
281
282 return BTy;
283}
284
285llvm::TypeDesc *
286CGDebugInfo::getOrCreatePointerType(QualType type, llvm::CompileUnitDesc *Unit)
287{
288 // type*
289 llvm::DerivedTypeDesc *DTy =
290 new llvm::DerivedTypeDesc(llvm::dwarf::DW_TAG_pointer_type);
291
292 // Handle the derived type.
293 const PointerType *PTRT = type->getAsPointerType();
294 llvm::TypeDesc *FromTy = getOrCreateType(PTRT->getPointeeType(), Unit);
295
296 // Get the name and location early to assist debugging.
297 SourceManager &SM = M->getContext().getSourceManager();
298 uint64_t Line = SM.getLogicalLineNumber(CurLoc);
299
300 // Bit size, align and offset of the type.
301 uint64_t Size = M->getContext().getTypeSize(type);
302 uint64_t Align = M->getContext().getTypeAlign(type);
303 uint64_t Offset = 0;
304
305 // If the type is defined, fill in the details.
306 if (DTy) {
307 DTy->setContext(Unit);
308 DTy->setLine(Line);
309 DTy->setSize(Size);
310 DTy->setAlign(Align);
311 DTy->setOffset(Offset);
312 DTy->setFromType(FromTy);
313 }
314
315 return DTy;
316}
317
318llvm::TypeDesc *
319CGDebugInfo::getOrCreateTypedefType(QualType type, llvm::CompileUnitDesc *Unit)
320{
321 // typedefs are derived from some other type.
322 llvm::DerivedTypeDesc *DTy =
323 new llvm::DerivedTypeDesc(llvm::dwarf::DW_TAG_typedef);
324
325 // Handle derived type.
326 const TypedefType *TDT = type->getAsTypedefType();
327 llvm::TypeDesc *FromTy = getOrCreateType(TDT->LookThroughTypedefs(),
328 Unit);
329
330 // Get the name and location early to assist debugging.
331 const char *TyName = TDT->getDecl()->getName();
332 SourceManager &SM = M->getContext().getSourceManager();
333 uint64_t Line = SM.getLogicalLineNumber(TDT->getDecl()->getLocation());
334
335 // If the type is defined, fill in the details.
336 if (DTy) {
337 DTy->setContext(Unit);
338 DTy->setFile(getOrCreateCompileUnit(TDT->getDecl()->getLocation()));
339 DTy->setLine(Line);
340 DTy->setName(TyName);
341 DTy->setFromType(FromTy);
342 }
343
344 return DTy;
345}
346
347llvm::TypeDesc *
348CGDebugInfo::getOrCreateFunctionType(QualType type, llvm::CompileUnitDesc *Unit)
349{
350 llvm::CompositeTypeDesc *SubrTy =
351 new llvm::CompositeTypeDesc(llvm::dwarf::DW_TAG_subroutine_type);
352
353 // Prepare to add the arguments for the subroutine.
354 std::vector<llvm::DebugInfoDesc *> &Elements = SubrTy->getElements();
355
356 // Get result type.
357 const FunctionType *FT = type->getAsFunctionType();
358 llvm::TypeDesc *ArgTy = getOrCreateType(FT->getResultType(), Unit);
359 if (ArgTy) Elements.push_back(ArgTy);
360
361 // Set up remainder of arguments.
362 if (type->getTypeClass() == Type::FunctionProto) {
363 const FunctionTypeProto *FTPro = dyn_cast<FunctionTypeProto>(type);
364 for (unsigned int i =0; i < FTPro->getNumArgs(); i++) {
365 QualType ParamType = FTPro->getArgType(i);
366 ArgTy = getOrCreateType(ParamType, Unit);
367 if (ArgTy) Elements.push_back(ArgTy);
368 }
369 }
370
Mike Stump9ea58842008-06-19 20:57:50 +0000371 // FIXME: set other fields file, line here.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000372 SubrTy->setContext(Unit);
373
374 return SubrTy;
375}
376
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000377/// getOrCreateRecordType - get structure or union type.
378llvm::TypeDesc *
379CGDebugInfo::getOrCreateRecordType(QualType type, llvm::CompileUnitDesc *Unit)
380{
381 llvm::CompositeTypeDesc *RecType;
Daniel Dunbar3845f862008-10-31 03:54:29 +0000382 if (type->isStructureType())
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000383 RecType = new llvm::CompositeTypeDesc(llvm::dwarf::DW_TAG_structure_type);
Daniel Dunbar3845f862008-10-31 03:54:29 +0000384 else if (type->isUnionType())
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000385 RecType = new llvm::CompositeTypeDesc(llvm::dwarf::DW_TAG_union_type);
386 else
387 return NULL;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000388
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000389 RecordDecl *RecDecl = type->getAsRecordType()->getDecl();
Daniel Dunbar5273f512008-10-17 01:07:56 +0000390 // We can not get the type for forward declarations.
391 // FIXME: What *should* we be doing here?
392 if (!RecDecl->getDefinition(M->getContext()))
393 return NULL;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000394 const ASTRecordLayout &RL = M->getContext().getASTRecordLayout(RecDecl);
395
396 SourceManager &SM = M->getContext().getSourceManager();
397 uint64_t Line = SM.getLogicalLineNumber(RecDecl->getLocation());
398
399 std::vector<llvm::DebugInfoDesc *> &Elements = RecType->getElements();
400
401 // Add the members.
402 int NumMembers = RecDecl->getNumMembers();
403 for (int i = 0; i < NumMembers; i++) {
404 FieldDecl *Member = RecDecl->getMember(i);
405 llvm::TypeDesc *MemberTy = getOrCreateType(Member->getType(), Unit);
406 MemberTy->setOffset(RL.getFieldOffset(i));
407 Elements.push_back(MemberTy);
408 }
409
410 // Fill in the blanks.
Daniel Dunbar3845f862008-10-31 03:54:29 +0000411 if (RecType) {
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000412 RecType->setContext(Unit);
413 RecType->setName(RecDecl->getName());
414 RecType->setFile(getOrCreateCompileUnit(RecDecl->getLocation()));
415 RecType->setLine(Line);
416 RecType->setSize(RL.getSize());
417 RecType->setAlign(RL.getAlignment());
418 RecType->setOffset(0);
419 }
Daniel Dunbar3845f862008-10-31 03:54:29 +0000420 return RecType;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000421}
422
423/// getOrCreateEnumType - get Enum type.
424llvm::TypeDesc *
425CGDebugInfo::getOrCreateEnumType(QualType type, llvm::CompileUnitDesc *Unit)
426{
427 llvm::CompositeTypeDesc *EnumTy
428 = new llvm::CompositeTypeDesc(llvm::dwarf::DW_TAG_enumeration_type);
429
430 EnumType *EType = dyn_cast<EnumType>(type);
Daniel Dunbar3845f862008-10-31 03:54:29 +0000431 if (!EType) return NULL;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000432
433 EnumDecl *EDecl = EType->getDecl();
434 SourceManager &SM = M->getContext().getSourceManager();
435 uint64_t Line = SM.getLogicalLineNumber(EDecl->getLocation());
436
437 // Size, align and offset of the type.
438 uint64_t Size = M->getContext().getTypeSize(type);
439 uint64_t Align = M->getContext().getTypeAlign(type);
440
441 // Create descriptors for enum members.
442 std::vector<llvm::DebugInfoDesc *> &Elements = EnumTy->getElements();
443 EnumConstantDecl *ElementList = EDecl->getEnumConstantList();
444 while (ElementList) {
445 llvm::EnumeratorDesc *EnumDesc = new llvm::EnumeratorDesc();
446 // push it to the enum desc list so that we can free it later.
447 EnumDescList.push_back(EnumDesc);
448
449 const char *ElementName = ElementList->getName();
450 uint64_t Value = ElementList->getInitVal().getZExtValue();
451
452 EnumDesc->setName(ElementName);
453 EnumDesc->setValue(Value);
454 Elements.push_back(EnumDesc);
455 if (ElementList->getNextDeclarator())
456 ElementList
457 = dyn_cast<EnumConstantDecl>(ElementList->getNextDeclarator());
458 else
459 break;
460 }
461
462 // Fill in the blanks.
463 if (EnumTy) {
464 EnumTy->setContext(Unit);
465 EnumTy->setName(EDecl->getName());
466 EnumTy->setSize(Size);
467 EnumTy->setAlign(Align);
468 EnumTy->setOffset(0);
469 EnumTy->setFile(getOrCreateCompileUnit(EDecl->getLocation()));
470 EnumTy->setLine(Line);
471 }
472 return EnumTy;
473}
474
Sanjiv Gupta507de852008-06-09 10:47:41 +0000475/// getOrCreateArrayType - get or create array types.
476llvm::TypeDesc *
477CGDebugInfo::getOrCreateArrayType(QualType type, llvm::CompileUnitDesc *Unit)
478{
479 llvm::CompositeTypeDesc *ArrayTy
480 = new llvm::CompositeTypeDesc(llvm::dwarf::DW_TAG_array_type);
481
482 // Size, align and offset of the type.
483 uint64_t Size = M->getContext().getTypeSize(type);
484 uint64_t Align = M->getContext().getTypeAlign(type);
485
486 SourceManager &SM = M->getContext().getSourceManager();
487 uint64_t Line = SM.getLogicalLineNumber(CurLoc);
488
489 // Add the dimensions of the array.
490 std::vector<llvm::DebugInfoDesc *> &Elements = ArrayTy->getElements();
491 do {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000492 const ArrayType *AT = M->getContext().getAsArrayType(type);
Sanjiv Gupta507de852008-06-09 10:47:41 +0000493 llvm::SubrangeDesc *Subrange = new llvm::SubrangeDesc();
494
495 // push it back on the subrange desc list so that we can free it later.
496 SubrangeDescList.push_back(Subrange);
497
498 uint64_t Upper = 0;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000499 if (const ConstantArrayType *ConstArrTy = dyn_cast<ConstantArrayType>(AT)) {
Sanjiv Gupta507de852008-06-09 10:47:41 +0000500 Upper = ConstArrTy->getSize().getZExtValue() - 1;
501 }
502 Subrange->setLo(0);
503 Subrange->setHi(Upper);
504 Elements.push_back(Subrange);
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000505 type = AT->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000506 } while (type->isArrayType());
507
508 ArrayTy->setFromType(getOrCreateType(type, Unit));
509
510 if (ArrayTy) {
511 ArrayTy->setContext(Unit);
512 ArrayTy->setSize(Size);
513 ArrayTy->setAlign(Align);
514 ArrayTy->setOffset(0);
515 ArrayTy->setFile(getOrCreateCompileUnit(CurLoc));
516 ArrayTy->setLine(Line);
517 }
518 return ArrayTy;
519}
520
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000521
522/// getOrCreateTaggedType - get or create structure/union/Enum type.
523llvm::TypeDesc *
524CGDebugInfo::getOrCreateTaggedType(QualType type, llvm::CompileUnitDesc *Unit)
525{
526 if (type->isStructureType() || type->isUnionType())
527 return getOrCreateRecordType(type, Unit);
528 else if (type->isEnumeralType())
529 return getOrCreateEnumType(type, Unit);
530 else
531 return NULL;
532}
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000533
534/// getOrCreateType - Get the type from the cache or create a new
535/// one if necessary.
536llvm::TypeDesc *
537CGDebugInfo::getOrCreateType(QualType type, llvm::CompileUnitDesc *Unit)
538{
539 if (type.isNull())
540 return NULL;
541
542 // Check to see if the compile unit already has created this type.
543 llvm::TypeDesc *&Slot = TypeCache[type.getAsOpaquePtr()];
544 if (Slot) return Slot;
545
546 // We need to check for the CVR qualifiers as the first thing.
547 if (type.getCVRQualifiers()) {
548 Slot = getOrCreateCVRType (type, Unit);
549 return Slot;
550 }
551
552 // Work out details of type.
Daniel Dunbar3845f862008-10-31 03:54:29 +0000553 switch (type->getTypeClass()) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000554 case Type::Complex:
555 case Type::Reference:
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000556 case Type::Vector:
557 case Type::ExtVector:
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000558 case Type::ASQual:
559 case Type::ObjCInterface:
560 case Type::ObjCQualifiedInterface:
561 case Type::ObjCQualifiedId:
562 case Type::TypeOfExp:
563 case Type::TypeOfTyp:
564 default:
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000565 return NULL;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000566
567 case Type::TypeName:
568 Slot = getOrCreateTypedefType(type, Unit);
569 break;
570
571 case Type::FunctionProto:
572 case Type::FunctionNoProto:
573 Slot = getOrCreateFunctionType(type, Unit);
574 break;
575
576 case Type::Builtin:
577 Slot = getOrCreateBuiltinType(type, Unit);
578 break;
579
580 case Type::Pointer:
581 Slot = getOrCreatePointerType(type, Unit);
582 break;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000583
584 case Type::Tagged:
585 Slot = getOrCreateTaggedType(type, Unit);
586 break;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000587
588 case Type::ConstantArray:
589 case Type::VariableArray:
590 case Type::IncompleteArray:
591 Slot = getOrCreateArrayType(type, Unit);
592 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000593 }
594
595 return Slot;
596}
597
598/// EmitFunctionStart - Constructs the debug code for entering a function -
599/// "llvm.dbg.func.start.".
Daniel Dunbar2284ac92008-10-18 18:22:23 +0000600void CGDebugInfo::EmitFunctionStart(const char *Name,
601 QualType ReturnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000602 llvm::Function *Fn,
Chris Lattner85e35682008-08-08 19:57:58 +0000603 llvm::IRBuilder<> &Builder)
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000604{
605 // Create subprogram descriptor.
606 Subprogram = new llvm::SubprogramDesc();
607
608 // Make sure we have an anchor.
609 if (!SubprogramAnchor) {
610 SubprogramAnchor = new llvm::AnchorDesc(Subprogram);
611 }
612
613 // Get name information.
Daniel Dunbar2284ac92008-10-18 18:22:23 +0000614 Subprogram->setName(Name);
615 Subprogram->setFullName(Name);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000616
617 // Gather location information.
618 llvm::CompileUnitDesc *Unit = getOrCreateCompileUnit(CurLoc);
619 SourceManager &SM = M->getContext().getSourceManager();
620 uint64_t Loc = SM.getLogicalLineNumber(CurLoc);
621
622 // Get Function Type.
Daniel Dunbar2284ac92008-10-18 18:22:23 +0000623 llvm::TypeDesc *SPTy = getOrCreateType(ReturnType, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000624
625 Subprogram->setAnchor(SubprogramAnchor);
626 Subprogram->setContext(Unit);
627 Subprogram->setFile(Unit);
628 Subprogram->setLine(Loc);
629 Subprogram->setType(SPTy);
630 Subprogram->setIsStatic(Fn->hasInternalLinkage());
631 Subprogram->setIsDefinition(true);
632
633 // Lazily construct llvm.dbg.func.start.
634 if (!FuncStartFn)
635 FuncStartFn = llvm::Intrinsic::getDeclaration(&M->getModule(),
636 llvm::Intrinsic::dbg_func_start);
637
638 // Call llvm.dbg.func.start which also implicitly calls llvm.dbg.stoppoint.
639 Builder.CreateCall(FuncStartFn, getCastValueFor(Subprogram), "");
640
641 // Push function on region stack.
642 RegionStack.push_back(Subprogram);
643}
644
645
646void
Chris Lattner85e35682008-08-08 19:57:58 +0000647CGDebugInfo::EmitStopPoint(llvm::Function *Fn, llvm::IRBuilder<> &Builder)
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000648{
649 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
650
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000651 // Don't bother if things are the same as last time.
652 SourceManager &SM = M->getContext().getSourceManager();
653 if (CurLoc == PrevLoc
654 || (SM.getLineNumber(CurLoc) == SM.getLineNumber(PrevLoc)
655 && SM.isFromSameFile(CurLoc, PrevLoc)))
656 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000657
658 // Update last state.
659 PrevLoc = CurLoc;
660
661 // Get the appropriate compile unit.
662 llvm::CompileUnitDesc *Unit = getOrCreateCompileUnit(CurLoc);
663
664 // Lazily construct llvm.dbg.stoppoint function.
665 if (!StopPointFn)
666 StopPointFn = llvm::Intrinsic::getDeclaration(&M->getModule(),
Eli Friedman86eb3112008-05-13 14:40:48 +0000667 llvm::Intrinsic::dbg_stoppoint);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000668
669 uint64_t CurLineNo = SM.getLogicalLineNumber(CurLoc);
670 uint64_t ColumnNo = SM.getLogicalColumnNumber(CurLoc);
671
672 // Invoke llvm.dbg.stoppoint
673 Builder.CreateCall3(StopPointFn,
Eli Friedman86eb3112008-05-13 14:40:48 +0000674 llvm::ConstantInt::get(llvm::Type::Int32Ty, CurLineNo),
675 llvm::ConstantInt::get(llvm::Type::Int32Ty, ColumnNo),
676 getCastValueFor(Unit), "");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000677}
678
679/// EmitRegionStart- Constructs the debug code for entering a declarative
680/// region - "llvm.dbg.region.start.".
Chris Lattner85e35682008-08-08 19:57:58 +0000681void CGDebugInfo::EmitRegionStart(llvm::Function *Fn,
682 llvm::IRBuilder<> &Builder)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000683{
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000684 llvm::BlockDesc *Block = new llvm::BlockDesc();
Daniel Dunbar5273f512008-10-17 01:07:56 +0000685 if (!RegionStack.empty())
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000686 Block->setContext(RegionStack.back());
687 RegionStack.push_back(Block);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000688
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000689 // Lazily construct llvm.dbg.region.start function.
690 if (!RegionStartFn)
691 RegionStartFn = llvm::Intrinsic::getDeclaration(&M->getModule(),
692 llvm::Intrinsic::dbg_region_start);
693
694 // Call llvm.dbg.func.start.
695 Builder.CreateCall(RegionStartFn, getCastValueFor(Block), "");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000696}
697
698/// EmitRegionEnd - Constructs the debug code for exiting a declarative
699/// region - "llvm.dbg.region.end."
Chris Lattner85e35682008-08-08 19:57:58 +0000700void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, llvm::IRBuilder<> &Builder)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000701{
Daniel Dunbar5273f512008-10-17 01:07:56 +0000702 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
703
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000704 // Lazily construct llvm.dbg.region.end function.
705 if (!RegionEndFn)
706 RegionEndFn =llvm::Intrinsic::getDeclaration(&M->getModule(),
Eli Friedman86eb3112008-05-13 14:40:48 +0000707 llvm::Intrinsic::dbg_region_end);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000708
709 // Provide an region stop point.
710 EmitStopPoint(Fn, Builder);
711
712 // Call llvm.dbg.func.end.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000713 llvm::DebugInfoDesc *DID = RegionStack.back();
714 Builder.CreateCall(RegionEndFn, getCastValueFor(DID), "");
715 RegionStack.pop_back();
Daniel Dunbar66031a52008-10-17 16:15:48 +0000716 // FIXME: Should be freeing here?
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000717}
718
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000719/// EmitDeclare - Emit local variable declaration debug info.
720void CGDebugInfo::EmitDeclare(const VarDecl *decl, unsigned Tag,
721 llvm::Value *AI,
Chris Lattner85e35682008-08-08 19:57:58 +0000722 llvm::IRBuilder<> &Builder)
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000723{
Daniel Dunbar5273f512008-10-17 01:07:56 +0000724 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
725
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000726 // FIXME: If it is a compiler generated temporary then return.
727
728 // Construct llvm.dbg.declare function.
729 if (!DeclareFn)
730 DeclareFn = llvm::Intrinsic::getDeclaration(&M->getModule(),
Mike Stump9ea58842008-06-19 20:57:50 +0000731 llvm::Intrinsic::dbg_declare);
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000732
733 // Get type information.
734 llvm::CompileUnitDesc *Unit = getOrCreateCompileUnit(CurLoc);
735 llvm::TypeDesc *TyDesc = getOrCreateType(decl->getType(), Unit);
736
737 SourceManager &SM = M->getContext().getSourceManager();
738 uint64_t Loc = SM.getLogicalLineNumber(CurLoc);
739
740 // Construct variable.
741 llvm::VariableDesc *Variable = new llvm::VariableDesc(Tag);
742 Variable->setContext(RegionStack.back());
743 Variable->setName(decl->getName());
744 Variable->setFile(Unit);
745 Variable->setLine(Loc);
746 Variable->setType(TyDesc);
747
748 // Push it onto the list so that we can free it.
749 VariableDescList.push_back(Variable);
750
751 // Cast the AllocA result to a {}* for the call to llvm.dbg.declare.
752 // These bit cast instructions will get freed when the basic block is
753 // deleted. So do not need to free them explicity.
754 const llvm::PointerType *EmpPtr = SR->getEmptyStructPtrType();
755 llvm::Value *AllocACast = new llvm::BitCastInst(AI, EmpPtr, decl->getName(),
756 Builder.GetInsertBlock());
757
758 // Call llvm.dbg.declare.
759 Builder.CreateCall2(DeclareFn, AllocACast, getCastValueFor(Variable), "");
760}
761
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000762/// EmitGlobalVariable - Emit information about a global variable.
763void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *GV,
764 const VarDecl *decl)
765{
766 // Create global variable debug descriptor.
767 llvm::GlobalVariableDesc *Global = new llvm::GlobalVariableDesc();
768
769 // Push it onto the list so that we can free it.
770 GlobalVarDescList.push_back(Global);
771
772 // Make sure we have an anchor.
773 if (!GlobalVariableAnchor)
774 GlobalVariableAnchor = new llvm::AnchorDesc(Global);
775
776 // Get name information.
777 Global->setName(decl->getName());
778 Global->setFullName(decl->getName());
779
780 llvm::CompileUnitDesc *Unit = getOrCreateCompileUnit(CurLoc);
781 SourceManager &SM = M->getContext().getSourceManager();
782 uint64_t Loc = SM.getLogicalLineNumber(CurLoc);
783
784 llvm::TypeDesc *TyD = getOrCreateType(decl->getType(), Unit);
785
786 // Fill in the Global information.
787 Global->setAnchor(GlobalVariableAnchor);
788 Global->setContext(Unit);
789 Global->setFile(Unit);
790 Global->setLine(Loc);
791 Global->setType(TyD);
792 Global->setIsDefinition(true);
793 Global->setIsStatic(GV->hasInternalLinkage());
794 Global->setGlobalVariable(GV);
795
796 // Make sure global is created if needed.
797 getValueFor(Global);
798}
799