blob: 4283dd4eaf3c654cce2822899243e52ef986537c [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
206 // No need to fill in the Name, Line, Size, Alignment, Offset in case of // CVR derived types.
207 DTy->setContext(Unit);
208 DTy->setFromType(FromTy);
209
210 return DTy;
211}
212
213
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000214/// getOrCreateBuiltinType - Get the Basic type from the cache or create a new
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000215/// one if necessary.
216llvm::TypeDesc *
217CGDebugInfo::getOrCreateBuiltinType(QualType type, llvm::CompileUnitDesc *Unit)
218{
219 assert (type->getTypeClass() == Type::Builtin);
220
221 const BuiltinType *BT = type->getAsBuiltinType();
222
223 unsigned Encoding = 0;
224 switch (BT->getKind())
225 {
226 case BuiltinType::Void:
227 return NULL;
228 case BuiltinType::UChar:
229 case BuiltinType::Char_U:
230 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
231 break;
232 case BuiltinType::Char_S:
233 case BuiltinType::SChar:
234 Encoding = llvm::dwarf::DW_ATE_signed_char;
235 break;
236 case BuiltinType::UShort:
237 case BuiltinType::UInt:
238 case BuiltinType::ULong:
239 case BuiltinType::ULongLong:
240 Encoding = llvm::dwarf::DW_ATE_unsigned;
241 break;
242 case BuiltinType::Short:
243 case BuiltinType::Int:
244 case BuiltinType::Long:
245 case BuiltinType::LongLong:
246 Encoding = llvm::dwarf::DW_ATE_signed;
247 break;
248 case BuiltinType::Bool:
249 Encoding = llvm::dwarf::DW_ATE_boolean;
250 break;
251 case BuiltinType::Float:
252 case BuiltinType::Double:
253 Encoding = llvm::dwarf::DW_ATE_float;
254 break;
255 default:
256 Encoding = llvm::dwarf::DW_ATE_signed;
257 break;
258 }
259
260 // Ty will have contain the resulting type.
261 llvm::BasicTypeDesc *BTy = new llvm::BasicTypeDesc();
262
263 // Get the name and location early to assist debugging.
264 const char *TyName = BT->getName();
265
266 // Bit size, align and offset of the type.
267 uint64_t Size = M->getContext().getTypeSize(type);
268 uint64_t Align = M->getContext().getTypeAlign(type);
269 uint64_t Offset = 0;
270
271 // If the type is defined, fill in the details.
272 if (BTy) {
273 BTy->setContext(Unit);
274 BTy->setName(TyName);
275 BTy->setSize(Size);
276 BTy->setAlign(Align);
277 BTy->setOffset(Offset);
278 BTy->setEncoding(Encoding);
279 }
280
281 return BTy;
282}
283
284llvm::TypeDesc *
285CGDebugInfo::getOrCreatePointerType(QualType type, llvm::CompileUnitDesc *Unit)
286{
287 // type*
288 llvm::DerivedTypeDesc *DTy =
289 new llvm::DerivedTypeDesc(llvm::dwarf::DW_TAG_pointer_type);
290
291 // Handle the derived type.
292 const PointerType *PTRT = type->getAsPointerType();
293 llvm::TypeDesc *FromTy = getOrCreateType(PTRT->getPointeeType(), Unit);
294
295 // Get the name and location early to assist debugging.
296 SourceManager &SM = M->getContext().getSourceManager();
297 uint64_t Line = SM.getLogicalLineNumber(CurLoc);
298
299 // Bit size, align and offset of the type.
300 uint64_t Size = M->getContext().getTypeSize(type);
301 uint64_t Align = M->getContext().getTypeAlign(type);
302 uint64_t Offset = 0;
303
304 // If the type is defined, fill in the details.
305 if (DTy) {
306 DTy->setContext(Unit);
307 DTy->setLine(Line);
308 DTy->setSize(Size);
309 DTy->setAlign(Align);
310 DTy->setOffset(Offset);
311 DTy->setFromType(FromTy);
312 }
313
314 return DTy;
315}
316
317llvm::TypeDesc *
318CGDebugInfo::getOrCreateTypedefType(QualType type, llvm::CompileUnitDesc *Unit)
319{
320 // typedefs are derived from some other type.
321 llvm::DerivedTypeDesc *DTy =
322 new llvm::DerivedTypeDesc(llvm::dwarf::DW_TAG_typedef);
323
324 // Handle derived type.
325 const TypedefType *TDT = type->getAsTypedefType();
326 llvm::TypeDesc *FromTy = getOrCreateType(TDT->LookThroughTypedefs(),
327 Unit);
328
329 // Get the name and location early to assist debugging.
330 const char *TyName = TDT->getDecl()->getName();
331 SourceManager &SM = M->getContext().getSourceManager();
332 uint64_t Line = SM.getLogicalLineNumber(TDT->getDecl()->getLocation());
333
334 // If the type is defined, fill in the details.
335 if (DTy) {
336 DTy->setContext(Unit);
337 DTy->setFile(getOrCreateCompileUnit(TDT->getDecl()->getLocation()));
338 DTy->setLine(Line);
339 DTy->setName(TyName);
340 DTy->setFromType(FromTy);
341 }
342
343 return DTy;
344}
345
346llvm::TypeDesc *
347CGDebugInfo::getOrCreateFunctionType(QualType type, llvm::CompileUnitDesc *Unit)
348{
349 llvm::CompositeTypeDesc *SubrTy =
350 new llvm::CompositeTypeDesc(llvm::dwarf::DW_TAG_subroutine_type);
351
352 // Prepare to add the arguments for the subroutine.
353 std::vector<llvm::DebugInfoDesc *> &Elements = SubrTy->getElements();
354
355 // Get result type.
356 const FunctionType *FT = type->getAsFunctionType();
357 llvm::TypeDesc *ArgTy = getOrCreateType(FT->getResultType(), Unit);
358 if (ArgTy) Elements.push_back(ArgTy);
359
360 // Set up remainder of arguments.
361 if (type->getTypeClass() == Type::FunctionProto) {
362 const FunctionTypeProto *FTPro = dyn_cast<FunctionTypeProto>(type);
363 for (unsigned int i =0; i < FTPro->getNumArgs(); i++) {
364 QualType ParamType = FTPro->getArgType(i);
365 ArgTy = getOrCreateType(ParamType, Unit);
366 if (ArgTy) Elements.push_back(ArgTy);
367 }
368 }
369
Mike Stump9ea58842008-06-19 20:57:50 +0000370 // FIXME: set other fields file, line here.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000371 SubrTy->setContext(Unit);
372
373 return SubrTy;
374}
375
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000376/// getOrCreateRecordType - get structure or union type.
377llvm::TypeDesc *
378CGDebugInfo::getOrCreateRecordType(QualType type, llvm::CompileUnitDesc *Unit)
379{
380 llvm::CompositeTypeDesc *RecType;
381 if(type->isStructureType())
382 RecType = new llvm::CompositeTypeDesc(llvm::dwarf::DW_TAG_structure_type);
383 else if(type->isUnionType())
384 RecType = new llvm::CompositeTypeDesc(llvm::dwarf::DW_TAG_union_type);
385 else
386 return NULL;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000387
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000388 RecordDecl *RecDecl = type->getAsRecordType()->getDecl();
Daniel Dunbar5273f512008-10-17 01:07:56 +0000389 // We can not get the type for forward declarations.
390 // FIXME: What *should* we be doing here?
391 if (!RecDecl->getDefinition(M->getContext()))
392 return NULL;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000393 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);
405 MemberTy->setOffset(RL.getFieldOffset(i));
406 Elements.push_back(MemberTy);
407 }
408
409 // Fill in the blanks.
410 if(RecType) {
411 RecType->setContext(Unit);
412 RecType->setName(RecDecl->getName());
413 RecType->setFile(getOrCreateCompileUnit(RecDecl->getLocation()));
414 RecType->setLine(Line);
415 RecType->setSize(RL.getSize());
416 RecType->setAlign(RL.getAlignment());
417 RecType->setOffset(0);
418 }
419 return(RecType);
420}
421
422/// getOrCreateEnumType - get Enum type.
423llvm::TypeDesc *
424CGDebugInfo::getOrCreateEnumType(QualType type, llvm::CompileUnitDesc *Unit)
425{
426 llvm::CompositeTypeDesc *EnumTy
427 = new llvm::CompositeTypeDesc(llvm::dwarf::DW_TAG_enumeration_type);
428
429 EnumType *EType = dyn_cast<EnumType>(type);
430 if (!EType) return(NULL);
431
432 EnumDecl *EDecl = EType->getDecl();
433 SourceManager &SM = M->getContext().getSourceManager();
434 uint64_t Line = SM.getLogicalLineNumber(EDecl->getLocation());
435
436 // Size, align and offset of the type.
437 uint64_t Size = M->getContext().getTypeSize(type);
438 uint64_t Align = M->getContext().getTypeAlign(type);
439
440 // Create descriptors for enum members.
441 std::vector<llvm::DebugInfoDesc *> &Elements = EnumTy->getElements();
442 EnumConstantDecl *ElementList = EDecl->getEnumConstantList();
443 while (ElementList) {
444 llvm::EnumeratorDesc *EnumDesc = new llvm::EnumeratorDesc();
445 // push it to the enum desc list so that we can free it later.
446 EnumDescList.push_back(EnumDesc);
447
448 const char *ElementName = ElementList->getName();
449 uint64_t Value = ElementList->getInitVal().getZExtValue();
450
451 EnumDesc->setName(ElementName);
452 EnumDesc->setValue(Value);
453 Elements.push_back(EnumDesc);
454 if (ElementList->getNextDeclarator())
455 ElementList
456 = dyn_cast<EnumConstantDecl>(ElementList->getNextDeclarator());
457 else
458 break;
459 }
460
461 // Fill in the blanks.
462 if (EnumTy) {
463 EnumTy->setContext(Unit);
464 EnumTy->setName(EDecl->getName());
465 EnumTy->setSize(Size);
466 EnumTy->setAlign(Align);
467 EnumTy->setOffset(0);
468 EnumTy->setFile(getOrCreateCompileUnit(EDecl->getLocation()));
469 EnumTy->setLine(Line);
470 }
471 return EnumTy;
472}
473
Sanjiv Gupta507de852008-06-09 10:47:41 +0000474/// getOrCreateArrayType - get or create array types.
475llvm::TypeDesc *
476CGDebugInfo::getOrCreateArrayType(QualType type, llvm::CompileUnitDesc *Unit)
477{
478 llvm::CompositeTypeDesc *ArrayTy
479 = new llvm::CompositeTypeDesc(llvm::dwarf::DW_TAG_array_type);
480
481 // Size, align and offset of the type.
482 uint64_t Size = M->getContext().getTypeSize(type);
483 uint64_t Align = M->getContext().getTypeAlign(type);
484
485 SourceManager &SM = M->getContext().getSourceManager();
486 uint64_t Line = SM.getLogicalLineNumber(CurLoc);
487
488 // Add the dimensions of the array.
489 std::vector<llvm::DebugInfoDesc *> &Elements = ArrayTy->getElements();
490 do {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000491 const ArrayType *AT = M->getContext().getAsArrayType(type);
Sanjiv Gupta507de852008-06-09 10:47:41 +0000492 llvm::SubrangeDesc *Subrange = new llvm::SubrangeDesc();
493
494 // push it back on the subrange desc list so that we can free it later.
495 SubrangeDescList.push_back(Subrange);
496
497 uint64_t Upper = 0;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000498 if (const ConstantArrayType *ConstArrTy = dyn_cast<ConstantArrayType>(AT)) {
Sanjiv Gupta507de852008-06-09 10:47:41 +0000499 Upper = ConstArrTy->getSize().getZExtValue() - 1;
500 }
501 Subrange->setLo(0);
502 Subrange->setHi(Upper);
503 Elements.push_back(Subrange);
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000504 type = AT->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000505 } while (type->isArrayType());
506
507 ArrayTy->setFromType(getOrCreateType(type, Unit));
508
509 if (ArrayTy) {
510 ArrayTy->setContext(Unit);
511 ArrayTy->setSize(Size);
512 ArrayTy->setAlign(Align);
513 ArrayTy->setOffset(0);
514 ArrayTy->setFile(getOrCreateCompileUnit(CurLoc));
515 ArrayTy->setLine(Line);
516 }
517 return ArrayTy;
518}
519
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000520
521/// getOrCreateTaggedType - get or create structure/union/Enum type.
522llvm::TypeDesc *
523CGDebugInfo::getOrCreateTaggedType(QualType type, llvm::CompileUnitDesc *Unit)
524{
525 if (type->isStructureType() || type->isUnionType())
526 return getOrCreateRecordType(type, Unit);
527 else if (type->isEnumeralType())
528 return getOrCreateEnumType(type, Unit);
529 else
530 return NULL;
531}
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000532
533/// getOrCreateType - Get the type from the cache or create a new
534/// one if necessary.
535llvm::TypeDesc *
536CGDebugInfo::getOrCreateType(QualType type, llvm::CompileUnitDesc *Unit)
537{
538 if (type.isNull())
539 return NULL;
540
541 // Check to see if the compile unit already has created this type.
542 llvm::TypeDesc *&Slot = TypeCache[type.getAsOpaquePtr()];
543 if (Slot) return Slot;
544
545 // We need to check for the CVR qualifiers as the first thing.
546 if (type.getCVRQualifiers()) {
547 Slot = getOrCreateCVRType (type, Unit);
548 return Slot;
549 }
550
551 // Work out details of type.
552 switch(type->getTypeClass()) {
553 case Type::Complex:
554 case Type::Reference:
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000555 case Type::Vector:
556 case Type::ExtVector:
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000557 case Type::ASQual:
558 case Type::ObjCInterface:
559 case Type::ObjCQualifiedInterface:
560 case Type::ObjCQualifiedId:
561 case Type::TypeOfExp:
562 case Type::TypeOfTyp:
563 default:
564 {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000565 return NULL;
566 }
567
568 case Type::TypeName:
569 Slot = getOrCreateTypedefType(type, Unit);
570 break;
571
572 case Type::FunctionProto:
573 case Type::FunctionNoProto:
574 Slot = getOrCreateFunctionType(type, Unit);
575 break;
576
577 case Type::Builtin:
578 Slot = getOrCreateBuiltinType(type, Unit);
579 break;
580
581 case Type::Pointer:
582 Slot = getOrCreatePointerType(type, Unit);
583 break;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000584
585 case Type::Tagged:
586 Slot = getOrCreateTaggedType(type, Unit);
587 break;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000588
589 case Type::ConstantArray:
590 case Type::VariableArray:
591 case Type::IncompleteArray:
592 Slot = getOrCreateArrayType(type, Unit);
593 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000594 }
595
596 return Slot;
597}
598
599/// EmitFunctionStart - Constructs the debug code for entering a function -
600/// "llvm.dbg.func.start.".
Daniel Dunbar2284ac92008-10-18 18:22:23 +0000601void CGDebugInfo::EmitFunctionStart(const char *Name,
602 QualType ReturnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000603 llvm::Function *Fn,
Chris Lattner85e35682008-08-08 19:57:58 +0000604 llvm::IRBuilder<> &Builder)
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000605{
606 // Create subprogram descriptor.
607 Subprogram = new llvm::SubprogramDesc();
608
609 // Make sure we have an anchor.
610 if (!SubprogramAnchor) {
611 SubprogramAnchor = new llvm::AnchorDesc(Subprogram);
612 }
613
614 // Get name information.
Daniel Dunbar2284ac92008-10-18 18:22:23 +0000615 Subprogram->setName(Name);
616 Subprogram->setFullName(Name);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000617
618 // Gather location information.
619 llvm::CompileUnitDesc *Unit = getOrCreateCompileUnit(CurLoc);
620 SourceManager &SM = M->getContext().getSourceManager();
621 uint64_t Loc = SM.getLogicalLineNumber(CurLoc);
622
623 // Get Function Type.
Daniel Dunbar2284ac92008-10-18 18:22:23 +0000624 llvm::TypeDesc *SPTy = getOrCreateType(ReturnType, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000625
626 Subprogram->setAnchor(SubprogramAnchor);
627 Subprogram->setContext(Unit);
628 Subprogram->setFile(Unit);
629 Subprogram->setLine(Loc);
630 Subprogram->setType(SPTy);
631 Subprogram->setIsStatic(Fn->hasInternalLinkage());
632 Subprogram->setIsDefinition(true);
633
634 // Lazily construct llvm.dbg.func.start.
635 if (!FuncStartFn)
636 FuncStartFn = llvm::Intrinsic::getDeclaration(&M->getModule(),
637 llvm::Intrinsic::dbg_func_start);
638
639 // Call llvm.dbg.func.start which also implicitly calls llvm.dbg.stoppoint.
640 Builder.CreateCall(FuncStartFn, getCastValueFor(Subprogram), "");
641
642 // Push function on region stack.
643 RegionStack.push_back(Subprogram);
644}
645
646
647void
Chris Lattner85e35682008-08-08 19:57:58 +0000648CGDebugInfo::EmitStopPoint(llvm::Function *Fn, llvm::IRBuilder<> &Builder)
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000649{
650 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
651
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000652 // Don't bother if things are the same as last time.
653 SourceManager &SM = M->getContext().getSourceManager();
654 if (CurLoc == PrevLoc
655 || (SM.getLineNumber(CurLoc) == SM.getLineNumber(PrevLoc)
656 && SM.isFromSameFile(CurLoc, PrevLoc)))
657 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000658
659 // Update last state.
660 PrevLoc = CurLoc;
661
662 // Get the appropriate compile unit.
663 llvm::CompileUnitDesc *Unit = getOrCreateCompileUnit(CurLoc);
664
665 // Lazily construct llvm.dbg.stoppoint function.
666 if (!StopPointFn)
667 StopPointFn = llvm::Intrinsic::getDeclaration(&M->getModule(),
Eli Friedman86eb3112008-05-13 14:40:48 +0000668 llvm::Intrinsic::dbg_stoppoint);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000669
670 uint64_t CurLineNo = SM.getLogicalLineNumber(CurLoc);
671 uint64_t ColumnNo = SM.getLogicalColumnNumber(CurLoc);
672
673 // Invoke llvm.dbg.stoppoint
674 Builder.CreateCall3(StopPointFn,
Eli Friedman86eb3112008-05-13 14:40:48 +0000675 llvm::ConstantInt::get(llvm::Type::Int32Ty, CurLineNo),
676 llvm::ConstantInt::get(llvm::Type::Int32Ty, ColumnNo),
677 getCastValueFor(Unit), "");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000678}
679
680/// EmitRegionStart- Constructs the debug code for entering a declarative
681/// region - "llvm.dbg.region.start.".
Chris Lattner85e35682008-08-08 19:57:58 +0000682void CGDebugInfo::EmitRegionStart(llvm::Function *Fn,
683 llvm::IRBuilder<> &Builder)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000684{
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000685 llvm::BlockDesc *Block = new llvm::BlockDesc();
Daniel Dunbar5273f512008-10-17 01:07:56 +0000686 if (!RegionStack.empty())
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000687 Block->setContext(RegionStack.back());
688 RegionStack.push_back(Block);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000689
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000690 // Lazily construct llvm.dbg.region.start function.
691 if (!RegionStartFn)
692 RegionStartFn = llvm::Intrinsic::getDeclaration(&M->getModule(),
693 llvm::Intrinsic::dbg_region_start);
694
695 // Call llvm.dbg.func.start.
696 Builder.CreateCall(RegionStartFn, getCastValueFor(Block), "");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000697}
698
699/// EmitRegionEnd - Constructs the debug code for exiting a declarative
700/// region - "llvm.dbg.region.end."
Chris Lattner85e35682008-08-08 19:57:58 +0000701void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, llvm::IRBuilder<> &Builder)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000702{
Daniel Dunbar5273f512008-10-17 01:07:56 +0000703 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
704
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000705 // Lazily construct llvm.dbg.region.end function.
706 if (!RegionEndFn)
707 RegionEndFn =llvm::Intrinsic::getDeclaration(&M->getModule(),
Eli Friedman86eb3112008-05-13 14:40:48 +0000708 llvm::Intrinsic::dbg_region_end);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000709
710 // Provide an region stop point.
711 EmitStopPoint(Fn, Builder);
712
713 // Call llvm.dbg.func.end.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000714 llvm::DebugInfoDesc *DID = RegionStack.back();
715 Builder.CreateCall(RegionEndFn, getCastValueFor(DID), "");
716 RegionStack.pop_back();
Daniel Dunbar66031a52008-10-17 16:15:48 +0000717 // FIXME: Should be freeing here?
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000718}
719
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000720/// EmitDeclare - Emit local variable declaration debug info.
721void CGDebugInfo::EmitDeclare(const VarDecl *decl, unsigned Tag,
722 llvm::Value *AI,
Chris Lattner85e35682008-08-08 19:57:58 +0000723 llvm::IRBuilder<> &Builder)
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000724{
Daniel Dunbar5273f512008-10-17 01:07:56 +0000725 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
726
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000727 // FIXME: If it is a compiler generated temporary then return.
728
729 // Construct llvm.dbg.declare function.
730 if (!DeclareFn)
731 DeclareFn = llvm::Intrinsic::getDeclaration(&M->getModule(),
Mike Stump9ea58842008-06-19 20:57:50 +0000732 llvm::Intrinsic::dbg_declare);
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000733
734 // Get type information.
735 llvm::CompileUnitDesc *Unit = getOrCreateCompileUnit(CurLoc);
736 llvm::TypeDesc *TyDesc = getOrCreateType(decl->getType(), Unit);
737
738 SourceManager &SM = M->getContext().getSourceManager();
739 uint64_t Loc = SM.getLogicalLineNumber(CurLoc);
740
741 // Construct variable.
742 llvm::VariableDesc *Variable = new llvm::VariableDesc(Tag);
743 Variable->setContext(RegionStack.back());
744 Variable->setName(decl->getName());
745 Variable->setFile(Unit);
746 Variable->setLine(Loc);
747 Variable->setType(TyDesc);
748
749 // Push it onto the list so that we can free it.
750 VariableDescList.push_back(Variable);
751
752 // Cast the AllocA result to a {}* for the call to llvm.dbg.declare.
753 // These bit cast instructions will get freed when the basic block is
754 // deleted. So do not need to free them explicity.
755 const llvm::PointerType *EmpPtr = SR->getEmptyStructPtrType();
756 llvm::Value *AllocACast = new llvm::BitCastInst(AI, EmpPtr, decl->getName(),
757 Builder.GetInsertBlock());
758
759 // Call llvm.dbg.declare.
760 Builder.CreateCall2(DeclareFn, AllocACast, getCastValueFor(Variable), "");
761}
762
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000763/// EmitGlobalVariable - Emit information about a global variable.
764void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *GV,
765 const VarDecl *decl)
766{
767 // Create global variable debug descriptor.
768 llvm::GlobalVariableDesc *Global = new llvm::GlobalVariableDesc();
769
770 // Push it onto the list so that we can free it.
771 GlobalVarDescList.push_back(Global);
772
773 // Make sure we have an anchor.
774 if (!GlobalVariableAnchor)
775 GlobalVariableAnchor = new llvm::AnchorDesc(Global);
776
777 // Get name information.
778 Global->setName(decl->getName());
779 Global->setFullName(decl->getName());
780
781 llvm::CompileUnitDesc *Unit = getOrCreateCompileUnit(CurLoc);
782 SourceManager &SM = M->getContext().getSourceManager();
783 uint64_t Loc = SM.getLogicalLineNumber(CurLoc);
784
785 llvm::TypeDesc *TyD = getOrCreateType(decl->getType(), Unit);
786
787 // Fill in the Global information.
788 Global->setAnchor(GlobalVariableAnchor);
789 Global->setContext(Unit);
790 Global->setFile(Unit);
791 Global->setLine(Loc);
792 Global->setType(TyD);
793 Global->setIsDefinition(true);
794 Global->setIsStatic(GV->hasInternalLinkage());
795 Global->setGlobalVariable(GV);
796
797 // Make sure global is created if needed.
798 getValueFor(Global);
799}
800