blob: 478f862775eee28ba7aef0c15d7ea501c85e79f2 [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"
28#include "llvm/CodeGen/MachineModuleInfo.h"
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000029#include "llvm/Support/Dwarf.h"
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000030#include "llvm/Target/TargetMachine.h"
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000031using namespace clang;
32using namespace clang::CodeGen;
33
34CGDebugInfo::CGDebugInfo(CodeGenModule *m)
35: M(m)
36, CurLoc()
37, PrevLoc()
38, CompileUnitCache()
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000039, TypeCache()
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000040, StopPointFn(NULL)
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000041, FuncStartFn(NULL)
42, DeclareFn(NULL)
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000043, RegionStartFn(NULL)
44, RegionEndFn(NULL)
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000045, CompileUnitAnchor(NULL)
46, SubprogramAnchor(NULL)
Sanjiv Gupta54d97542008-06-05 08:59:10 +000047, GlobalVariableAnchor(NULL)
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000048, RegionStack()
Sanjiv Guptab2a10452008-05-30 10:30:31 +000049, VariableDescList()
Nuno Lopesa250b892008-06-08 10:16:34 +000050, GlobalVarDescList()
51, EnumDescList()
Sanjiv Guptab1e662e2008-06-09 10:47:41 +000052, SubrangeDescList()
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000053, Subprogram(NULL)
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000054{
55 SR = new llvm::DISerializer();
56 SR->setModule (&M->getModule());
57}
58
59CGDebugInfo::~CGDebugInfo()
60{
Daniel Dunbar6fc1f972008-10-17 16:15:48 +000061 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
62
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000063 delete SR;
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000064
65 // Free CompileUnitCache.
Daniel Dunbar9865e6f2008-10-24 00:46:51 +000066 for (std::map<const FileEntry*, llvm::CompileUnitDesc *>::iterator I
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000067 = 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 Gupta0f805bd2008-06-07 04:46:53 +000079 // Free region descriptors.
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000080 for (std::vector<llvm::DebugInfoDesc *>::iterator I
81 = RegionStack.begin(); I != RegionStack.end(); ++I) {
82 delete *I;
83 }
Sanjiv Guptab2a10452008-05-30 10:30:31 +000084
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +000085 // Free local var descriptors.
Sanjiv Guptab2a10452008-05-30 10:30:31 +000086 for (std::vector<llvm::VariableDesc *>::iterator I
87 = VariableDescList.begin(); I != VariableDescList.end(); ++I) {
88 delete *I;
89 }
90
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +000091 // Free global var descriptors.
Sanjiv Gupta54d97542008-06-05 08:59:10 +000092 for (std::vector<llvm::GlobalVariableDesc *>::iterator I
93 = GlobalVarDescList.begin(); I != GlobalVarDescList.end(); ++I) {
94 delete *I;
95 }
96
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +000097 // 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 Guptab1e662e2008-06-09 10:47:41 +0000103 // Free subrange descriptors.
104 for (std::vector<llvm::SubrangeDesc *>::iterator I
105 = SubrangeDescList.begin(); I != SubrangeDescList.end(); ++I) {
106 delete *I;
107 }
108
Eli Friedman9bc7c8d2008-05-22 01:40:10 +0000109 delete CompileUnitAnchor;
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000110 delete SubprogramAnchor;
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000111 delete GlobalVariableAnchor;
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000112}
113
Chris Lattnera1923f62008-08-04 07:31:14 +0000114void CGDebugInfo::setLocation(SourceLocation loc) {
Daniel Dunbar6fc1f972008-10-17 16:15:48 +0000115 if (loc.isValid())
116 CurLoc = M->getContext().getSourceManager().getLogicalLoc(loc);
Eli Friedman6a03c262008-05-29 11:08:17 +0000117}
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000118
119/// getCastValueFor - Return a llvm representation for a given debug information
120/// descriptor cast to an empty struct pointer.
121llvm::Value *CGDebugInfo::getCastValueFor(llvm::DebugInfoDesc *DD) {
122 return llvm::ConstantExpr::getBitCast(SR->Serialize(DD),
Eli Friedman6fb115c2008-05-13 14:40:48 +0000123 SR->getEmptyStructPtrType());
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000124}
125
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000126/// getValueFor - Return a llvm representation for a given debug information
127/// descriptor.
128llvm::Value *CGDebugInfo::getValueFor(llvm::DebugInfoDesc *DD) {
129 return SR->Serialize(DD);
130}
131
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000132/// getOrCreateCompileUnit - Get the compile unit from the cache or create a new
Daniel Dunbar4a66ef12008-10-24 08:38:36 +0000133/// one if necessary. This returns null for invalid source locations.
134llvm::CompileUnitDesc*
135CGDebugInfo::getOrCreateCompileUnit(const SourceLocation Loc) {
136 if (Loc.isInvalid())
137 return NULL;
138
Daniel Dunbar9865e6f2008-10-24 00:46:51 +0000139 SourceManager &SM = M->getContext().getSourceManager();
140 const FileEntry *FE = SM.getFileEntryForLoc(Loc);
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000141
142 // See if this compile unit has been used before.
Daniel Dunbar9865e6f2008-10-24 00:46:51 +0000143 llvm::CompileUnitDesc *&Unit = CompileUnitCache[FE];
144 if (Unit) return Unit;
145
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000146 // Create new compile unit.
147 // FIXME: Where to free these?
148 // One way is to iterate over the CompileUnitCache in ~CGDebugInfo.
Daniel Dunbar9865e6f2008-10-24 00:46:51 +0000149 Unit = new llvm::CompileUnitDesc();
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000150
151 // Make sure we have an anchor.
152 if (!CompileUnitAnchor) {
153 CompileUnitAnchor = new llvm::AnchorDesc(Unit);
154 }
155
156 // Get source file information.
Eli Friedman9bc7c8d2008-05-22 01:40:10 +0000157 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 Gupta40e56a12008-05-08 08:54:20 +0000165
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 Gupta40e56a12008-05-08 08:54:20 +0000178 return Unit;
179}
180
181
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000182/// getOrCreateCVRType - Get the CVR qualified type from the cache or create
183/// a new one if necessary.
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000184llvm::TypeDesc *
185CGDebugInfo::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 Gupta40e56a12008-05-08 08:54:20 +0000190
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000191 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 Dunbar44252b42008-10-31 03:54:29 +0000205 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
206 // CVR derived types.
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000207 DTy->setContext(Unit);
208 DTy->setFromType(FromTy);
209
210 return DTy;
211}
212
213
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000214/// getOrCreateBuiltinType - Get the Basic type from the cache or create a new
Sanjiv Gupta93eb8252008-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);
Daniel Dunbar65b69a82008-10-31 08:12:03 +0000358 Elements.push_back(ArgTy);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000359
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 Stumpa3e509e2008-06-19 20:57:50 +0000370 // FIXME: set other fields file, line here.
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000371 SubrTy->setContext(Unit);
372
373 return SubrTy;
374}
375
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000376/// getOrCreateRecordType - get structure or union type.
Daniel Dunbar8b4aa902008-10-31 04:04:54 +0000377void CGDebugInfo::getOrCreateRecordType(QualType type,
378 llvm::CompileUnitDesc *Unit,
379 llvm::TypeDesc *&Slot)
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000380{
Daniel Dunbar8b4aa902008-10-31 04:04:54 +0000381 // Prevent recursing in type generation by initializing the slot
382 // here.
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000383 llvm::CompositeTypeDesc *RecType;
Daniel Dunbar44252b42008-10-31 03:54:29 +0000384 if (type->isStructureType())
Daniel Dunbar8b4aa902008-10-31 04:04:54 +0000385 Slot = RecType =
386 new llvm::CompositeTypeDesc(llvm::dwarf::DW_TAG_structure_type);
Daniel Dunbar44252b42008-10-31 03:54:29 +0000387 else if (type->isUnionType())
Daniel Dunbar8b4aa902008-10-31 04:04:54 +0000388 Slot = RecType =
389 new llvm::CompositeTypeDesc(llvm::dwarf::DW_TAG_union_type);
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000390 else
Daniel Dunbar8b4aa902008-10-31 04:04:54 +0000391 return;
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000392
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000393 RecordDecl *RecDecl = type->getAsRecordType()->getDecl();
Daniel Dunbar1257f8c2008-10-17 01:07:56 +0000394 // We can not get the type for forward declarations.
395 // FIXME: What *should* we be doing here?
396 if (!RecDecl->getDefinition(M->getContext()))
Daniel Dunbar8b4aa902008-10-31 04:04:54 +0000397 return;
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000398 const ASTRecordLayout &RL = M->getContext().getASTRecordLayout(RecDecl);
399
400 SourceManager &SM = M->getContext().getSourceManager();
401 uint64_t Line = SM.getLogicalLineNumber(RecDecl->getLocation());
402
403 std::vector<llvm::DebugInfoDesc *> &Elements = RecType->getElements();
404
405 // Add the members.
406 int NumMembers = RecDecl->getNumMembers();
407 for (int i = 0; i < NumMembers; i++) {
408 FieldDecl *Member = RecDecl->getMember(i);
409 llvm::TypeDesc *MemberTy = getOrCreateType(Member->getType(), Unit);
410 MemberTy->setOffset(RL.getFieldOffset(i));
411 Elements.push_back(MemberTy);
412 }
413
414 // Fill in the blanks.
Daniel Dunbar44252b42008-10-31 03:54:29 +0000415 if (RecType) {
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000416 RecType->setContext(Unit);
417 RecType->setName(RecDecl->getName());
418 RecType->setFile(getOrCreateCompileUnit(RecDecl->getLocation()));
419 RecType->setLine(Line);
420 RecType->setSize(RL.getSize());
421 RecType->setAlign(RL.getAlignment());
422 RecType->setOffset(0);
423 }
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000424}
425
426/// getOrCreateEnumType - get Enum type.
427llvm::TypeDesc *
428CGDebugInfo::getOrCreateEnumType(QualType type, llvm::CompileUnitDesc *Unit)
429{
430 llvm::CompositeTypeDesc *EnumTy
431 = new llvm::CompositeTypeDesc(llvm::dwarf::DW_TAG_enumeration_type);
432
433 EnumType *EType = dyn_cast<EnumType>(type);
Daniel Dunbar8b4aa902008-10-31 04:04:54 +0000434 if (!EType) return(NULL);
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000435
436 EnumDecl *EDecl = EType->getDecl();
437 SourceManager &SM = M->getContext().getSourceManager();
438 uint64_t Line = SM.getLogicalLineNumber(EDecl->getLocation());
439
440 // Size, align and offset of the type.
441 uint64_t Size = M->getContext().getTypeSize(type);
442 uint64_t Align = M->getContext().getTypeAlign(type);
443
444 // Create descriptors for enum members.
445 std::vector<llvm::DebugInfoDesc *> &Elements = EnumTy->getElements();
446 EnumConstantDecl *ElementList = EDecl->getEnumConstantList();
447 while (ElementList) {
448 llvm::EnumeratorDesc *EnumDesc = new llvm::EnumeratorDesc();
449 // push it to the enum desc list so that we can free it later.
450 EnumDescList.push_back(EnumDesc);
451
452 const char *ElementName = ElementList->getName();
453 uint64_t Value = ElementList->getInitVal().getZExtValue();
454
455 EnumDesc->setName(ElementName);
456 EnumDesc->setValue(Value);
457 Elements.push_back(EnumDesc);
458 if (ElementList->getNextDeclarator())
459 ElementList
460 = dyn_cast<EnumConstantDecl>(ElementList->getNextDeclarator());
461 else
462 break;
463 }
464
465 // Fill in the blanks.
466 if (EnumTy) {
467 EnumTy->setContext(Unit);
468 EnumTy->setName(EDecl->getName());
469 EnumTy->setSize(Size);
470 EnumTy->setAlign(Align);
471 EnumTy->setOffset(0);
472 EnumTy->setFile(getOrCreateCompileUnit(EDecl->getLocation()));
473 EnumTy->setLine(Line);
474 }
475 return EnumTy;
476}
477
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000478/// getOrCreateArrayType - get or create array types.
479llvm::TypeDesc *
480CGDebugInfo::getOrCreateArrayType(QualType type, llvm::CompileUnitDesc *Unit)
481{
482 llvm::CompositeTypeDesc *ArrayTy
483 = new llvm::CompositeTypeDesc(llvm::dwarf::DW_TAG_array_type);
484
485 // Size, align and offset of the type.
486 uint64_t Size = M->getContext().getTypeSize(type);
487 uint64_t Align = M->getContext().getTypeAlign(type);
488
489 SourceManager &SM = M->getContext().getSourceManager();
490 uint64_t Line = SM.getLogicalLineNumber(CurLoc);
491
492 // Add the dimensions of the array.
493 std::vector<llvm::DebugInfoDesc *> &Elements = ArrayTy->getElements();
494 do {
Chris Lattnera1923f62008-08-04 07:31:14 +0000495 const ArrayType *AT = M->getContext().getAsArrayType(type);
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000496 llvm::SubrangeDesc *Subrange = new llvm::SubrangeDesc();
497
498 // push it back on the subrange desc list so that we can free it later.
499 SubrangeDescList.push_back(Subrange);
500
501 uint64_t Upper = 0;
Chris Lattnera1923f62008-08-04 07:31:14 +0000502 if (const ConstantArrayType *ConstArrTy = dyn_cast<ConstantArrayType>(AT)) {
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000503 Upper = ConstArrTy->getSize().getZExtValue() - 1;
504 }
505 Subrange->setLo(0);
506 Subrange->setHi(Upper);
507 Elements.push_back(Subrange);
Chris Lattnera1923f62008-08-04 07:31:14 +0000508 type = AT->getElementType();
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000509 } while (type->isArrayType());
510
511 ArrayTy->setFromType(getOrCreateType(type, Unit));
512
513 if (ArrayTy) {
514 ArrayTy->setContext(Unit);
515 ArrayTy->setSize(Size);
516 ArrayTy->setAlign(Align);
517 ArrayTy->setOffset(0);
518 ArrayTy->setFile(getOrCreateCompileUnit(CurLoc));
519 ArrayTy->setLine(Line);
520 }
521 return ArrayTy;
522}
523
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000524
525/// getOrCreateTaggedType - get or create structure/union/Enum type.
Daniel Dunbar8b4aa902008-10-31 04:04:54 +0000526void CGDebugInfo::getOrCreateTaggedType(QualType type,
527 llvm::CompileUnitDesc *Unit,
528 llvm::TypeDesc *&Slot)
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000529{
530 if (type->isStructureType() || type->isUnionType())
Daniel Dunbar8b4aa902008-10-31 04:04:54 +0000531 getOrCreateRecordType(type, Unit, Slot);
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000532 else if (type->isEnumeralType())
Daniel Dunbar8b4aa902008-10-31 04:04:54 +0000533 Slot = getOrCreateEnumType(type, Unit);
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000534}
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000535
536/// getOrCreateType - Get the type from the cache or create a new
537/// one if necessary.
538llvm::TypeDesc *
539CGDebugInfo::getOrCreateType(QualType type, llvm::CompileUnitDesc *Unit)
540{
541 if (type.isNull())
542 return NULL;
543
544 // Check to see if the compile unit already has created this type.
545 llvm::TypeDesc *&Slot = TypeCache[type.getAsOpaquePtr()];
546 if (Slot) return Slot;
547
548 // We need to check for the CVR qualifiers as the first thing.
549 if (type.getCVRQualifiers()) {
Daniel Dunbar8b4aa902008-10-31 04:04:54 +0000550 Slot = getOrCreateCVRType(type, Unit);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000551 return Slot;
552 }
553
554 // Work out details of type.
Daniel Dunbar44252b42008-10-31 03:54:29 +0000555 switch (type->getTypeClass()) {
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000556 case Type::Complex:
557 case Type::Reference:
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000558 case Type::Vector:
559 case Type::ExtVector:
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000560 case Type::ASQual:
561 case Type::ObjCInterface:
562 case Type::ObjCQualifiedInterface:
563 case Type::ObjCQualifiedId:
564 case Type::TypeOfExp:
565 case Type::TypeOfTyp:
566 default:
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000567 return NULL;
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000568
569 case Type::TypeName:
570 Slot = getOrCreateTypedefType(type, Unit);
571 break;
572
573 case Type::FunctionProto:
574 case Type::FunctionNoProto:
575 Slot = getOrCreateFunctionType(type, Unit);
576 break;
577
578 case Type::Builtin:
579 Slot = getOrCreateBuiltinType(type, Unit);
580 break;
581
582 case Type::Pointer:
583 Slot = getOrCreatePointerType(type, Unit);
584 break;
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000585
586 case Type::Tagged:
Daniel Dunbar8b4aa902008-10-31 04:04:54 +0000587 getOrCreateTaggedType(type, Unit, Slot);
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000588 break;
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000589
590 case Type::ConstantArray:
591 case Type::VariableArray:
592 case Type::IncompleteArray:
593 Slot = getOrCreateArrayType(type, Unit);
594 break;
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000595 }
596
597 return Slot;
598}
599
600/// EmitFunctionStart - Constructs the debug code for entering a function -
601/// "llvm.dbg.func.start.".
Daniel Dunbar54968bf2008-10-18 18:22:23 +0000602void CGDebugInfo::EmitFunctionStart(const char *Name,
603 QualType ReturnType,
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000604 llvm::Function *Fn,
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000605 CGBuilderTy &Builder)
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000606{
607 // Create subprogram descriptor.
608 Subprogram = new llvm::SubprogramDesc();
609
610 // Make sure we have an anchor.
611 if (!SubprogramAnchor) {
612 SubprogramAnchor = new llvm::AnchorDesc(Subprogram);
613 }
614
615 // Get name information.
Daniel Dunbar54968bf2008-10-18 18:22:23 +0000616 Subprogram->setName(Name);
617 Subprogram->setFullName(Name);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000618
619 // Gather location information.
620 llvm::CompileUnitDesc *Unit = getOrCreateCompileUnit(CurLoc);
621 SourceManager &SM = M->getContext().getSourceManager();
622 uint64_t Loc = SM.getLogicalLineNumber(CurLoc);
623
624 // Get Function Type.
Daniel Dunbar54968bf2008-10-18 18:22:23 +0000625 llvm::TypeDesc *SPTy = getOrCreateType(ReturnType, Unit);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000626
627 Subprogram->setAnchor(SubprogramAnchor);
628 Subprogram->setContext(Unit);
629 Subprogram->setFile(Unit);
630 Subprogram->setLine(Loc);
631 Subprogram->setType(SPTy);
632 Subprogram->setIsStatic(Fn->hasInternalLinkage());
633 Subprogram->setIsDefinition(true);
634
635 // Lazily construct llvm.dbg.func.start.
636 if (!FuncStartFn)
637 FuncStartFn = llvm::Intrinsic::getDeclaration(&M->getModule(),
638 llvm::Intrinsic::dbg_func_start);
639
640 // Call llvm.dbg.func.start which also implicitly calls llvm.dbg.stoppoint.
641 Builder.CreateCall(FuncStartFn, getCastValueFor(Subprogram), "");
642
643 // Push function on region stack.
644 RegionStack.push_back(Subprogram);
645}
646
647
648void
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000649CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder)
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000650{
651 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
652
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000653 // Don't bother if things are the same as last time.
654 SourceManager &SM = M->getContext().getSourceManager();
655 if (CurLoc == PrevLoc
656 || (SM.getLineNumber(CurLoc) == SM.getLineNumber(PrevLoc)
657 && SM.isFromSameFile(CurLoc, PrevLoc)))
658 return;
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000659
660 // Update last state.
661 PrevLoc = CurLoc;
662
663 // Get the appropriate compile unit.
664 llvm::CompileUnitDesc *Unit = getOrCreateCompileUnit(CurLoc);
665
666 // Lazily construct llvm.dbg.stoppoint function.
667 if (!StopPointFn)
668 StopPointFn = llvm::Intrinsic::getDeclaration(&M->getModule(),
Eli Friedman6fb115c2008-05-13 14:40:48 +0000669 llvm::Intrinsic::dbg_stoppoint);
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000670
671 uint64_t CurLineNo = SM.getLogicalLineNumber(CurLoc);
672 uint64_t ColumnNo = SM.getLogicalColumnNumber(CurLoc);
673
674 // Invoke llvm.dbg.stoppoint
675 Builder.CreateCall3(StopPointFn,
Eli Friedman6fb115c2008-05-13 14:40:48 +0000676 llvm::ConstantInt::get(llvm::Type::Int32Ty, CurLineNo),
677 llvm::ConstantInt::get(llvm::Type::Int32Ty, ColumnNo),
678 getCastValueFor(Unit), "");
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000679}
680
681/// EmitRegionStart- Constructs the debug code for entering a declarative
682/// region - "llvm.dbg.region.start.".
Chris Lattnerfaf23db2008-08-08 19:57:58 +0000683void CGDebugInfo::EmitRegionStart(llvm::Function *Fn,
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000684 CGBuilderTy &Builder)
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000685{
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000686 llvm::BlockDesc *Block = new llvm::BlockDesc();
Daniel Dunbar1257f8c2008-10-17 01:07:56 +0000687 if (!RegionStack.empty())
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000688 Block->setContext(RegionStack.back());
689 RegionStack.push_back(Block);
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000690
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000691 // Lazily construct llvm.dbg.region.start function.
692 if (!RegionStartFn)
693 RegionStartFn = llvm::Intrinsic::getDeclaration(&M->getModule(),
694 llvm::Intrinsic::dbg_region_start);
695
696 // Call llvm.dbg.func.start.
697 Builder.CreateCall(RegionStartFn, getCastValueFor(Block), "");
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000698}
699
700/// EmitRegionEnd - Constructs the debug code for exiting a declarative
701/// region - "llvm.dbg.region.end."
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000702void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder)
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000703{
Daniel Dunbar1257f8c2008-10-17 01:07:56 +0000704 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
705
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000706 // Lazily construct llvm.dbg.region.end function.
707 if (!RegionEndFn)
708 RegionEndFn =llvm::Intrinsic::getDeclaration(&M->getModule(),
Eli Friedman6fb115c2008-05-13 14:40:48 +0000709 llvm::Intrinsic::dbg_region_end);
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000710
711 // Provide an region stop point.
712 EmitStopPoint(Fn, Builder);
713
714 // Call llvm.dbg.func.end.
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000715 llvm::DebugInfoDesc *DID = RegionStack.back();
716 Builder.CreateCall(RegionEndFn, getCastValueFor(DID), "");
717 RegionStack.pop_back();
Daniel Dunbar6fc1f972008-10-17 16:15:48 +0000718 // FIXME: Should be freeing here?
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000719}
720
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000721/// EmitDeclare - Emit local variable declaration debug info.
722void CGDebugInfo::EmitDeclare(const VarDecl *decl, unsigned Tag,
723 llvm::Value *AI,
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000724 CGBuilderTy &Builder)
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000725{
Daniel Dunbar1257f8c2008-10-17 01:07:56 +0000726 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
727
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000728 // FIXME: If it is a compiler generated temporary then return.
729
730 // Construct llvm.dbg.declare function.
731 if (!DeclareFn)
732 DeclareFn = llvm::Intrinsic::getDeclaration(&M->getModule(),
Mike Stumpa3e509e2008-06-19 20:57:50 +0000733 llvm::Intrinsic::dbg_declare);
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000734
735 // Get type information.
736 llvm::CompileUnitDesc *Unit = getOrCreateCompileUnit(CurLoc);
737 llvm::TypeDesc *TyDesc = getOrCreateType(decl->getType(), Unit);
738
739 SourceManager &SM = M->getContext().getSourceManager();
740 uint64_t Loc = SM.getLogicalLineNumber(CurLoc);
741
742 // Construct variable.
743 llvm::VariableDesc *Variable = new llvm::VariableDesc(Tag);
744 Variable->setContext(RegionStack.back());
745 Variable->setName(decl->getName());
746 Variable->setFile(Unit);
747 Variable->setLine(Loc);
748 Variable->setType(TyDesc);
749
750 // Push it onto the list so that we can free it.
751 VariableDescList.push_back(Variable);
752
753 // Cast the AllocA result to a {}* for the call to llvm.dbg.declare.
754 // These bit cast instructions will get freed when the basic block is
755 // deleted. So do not need to free them explicity.
756 const llvm::PointerType *EmpPtr = SR->getEmptyStructPtrType();
757 llvm::Value *AllocACast = new llvm::BitCastInst(AI, EmpPtr, decl->getName(),
758 Builder.GetInsertBlock());
759
760 // Call llvm.dbg.declare.
761 Builder.CreateCall2(DeclareFn, AllocACast, getCastValueFor(Variable), "");
762}
763
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000764/// EmitGlobalVariable - Emit information about a global variable.
765void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *GV,
766 const VarDecl *decl)
767{
768 // Create global variable debug descriptor.
769 llvm::GlobalVariableDesc *Global = new llvm::GlobalVariableDesc();
770
771 // Push it onto the list so that we can free it.
772 GlobalVarDescList.push_back(Global);
773
774 // Make sure we have an anchor.
775 if (!GlobalVariableAnchor)
776 GlobalVariableAnchor = new llvm::AnchorDesc(Global);
777
778 // Get name information.
779 Global->setName(decl->getName());
780 Global->setFullName(decl->getName());
781
782 llvm::CompileUnitDesc *Unit = getOrCreateCompileUnit(CurLoc);
783 SourceManager &SM = M->getContext().getSourceManager();
784 uint64_t Loc = SM.getLogicalLineNumber(CurLoc);
785
786 llvm::TypeDesc *TyD = getOrCreateType(decl->getType(), Unit);
787
788 // Fill in the Global information.
789 Global->setAnchor(GlobalVariableAnchor);
790 Global->setContext(Unit);
791 Global->setFile(Unit);
792 Global->setLine(Loc);
793 Global->setType(TyD);
794 Global->setIsDefinition(true);
795 Global->setIsStatic(GV->hasInternalLinkage());
796 Global->setGlobalVariable(GV);
797
798 // Make sure global is created if needed.
799 getValueFor(Global);
800}
801