blob: 7641c4ee15f6b7cdab3af89705936940f03bcf1d [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"
30#include "llvm/Support/IRBuilder.h"
31#include "llvm/Target/TargetMachine.h"
Sanjiv Gupta40e56a12008-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 Gupta93eb8252008-05-25 05:15:42 +000040, TypeCache()
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000041, StopPointFn(NULL)
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000042, FuncStartFn(NULL)
43, DeclareFn(NULL)
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000044, RegionStartFn(NULL)
45, RegionEndFn(NULL)
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000046, CompileUnitAnchor(NULL)
47, SubprogramAnchor(NULL)
Sanjiv Gupta54d97542008-06-05 08:59:10 +000048, GlobalVariableAnchor(NULL)
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000049, RegionStack()
Sanjiv Guptab2a10452008-05-30 10:30:31 +000050, VariableDescList()
Nuno Lopesa250b892008-06-08 10:16:34 +000051, GlobalVarDescList()
52, EnumDescList()
Sanjiv Guptab1e662e2008-06-09 10:47:41 +000053, SubrangeDescList()
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000054, Subprogram(NULL)
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000055{
56 SR = new llvm::DISerializer();
57 SR->setModule (&M->getModule());
58}
59
60CGDebugInfo::~CGDebugInfo()
61{
Daniel Dunbar6fc1f972008-10-17 16:15:48 +000062 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
63
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000064 delete SR;
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000065
66 // Free CompileUnitCache.
Daniel Dunbar9865e6f2008-10-24 00:46:51 +000067 for (std::map<const FileEntry*, llvm::CompileUnitDesc *>::iterator I
Sanjiv Gupta93eb8252008-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 Gupta0f805bd2008-06-07 04:46:53 +000080 // Free region descriptors.
Sanjiv Gupta93eb8252008-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 Guptab2a10452008-05-30 10:30:31 +000085
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +000086 // Free local var descriptors.
Sanjiv Guptab2a10452008-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 Gupta0f805bd2008-06-07 04:46:53 +000092 // Free global var descriptors.
Sanjiv Gupta54d97542008-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 Gupta0f805bd2008-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 Guptab1e662e2008-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 Friedman9bc7c8d2008-05-22 01:40:10 +0000110 delete CompileUnitAnchor;
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000111 delete SubprogramAnchor;
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000112 delete GlobalVariableAnchor;
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000113}
114
Chris Lattnera1923f62008-08-04 07:31:14 +0000115void CGDebugInfo::setLocation(SourceLocation loc) {
Daniel Dunbar6fc1f972008-10-17 16:15:48 +0000116 if (loc.isValid())
117 CurLoc = M->getContext().getSourceManager().getLogicalLoc(loc);
Eli Friedman6a03c262008-05-29 11:08:17 +0000118}
Sanjiv Gupta40e56a12008-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 Friedman6fb115c2008-05-13 14:40:48 +0000124 SR->getEmptyStructPtrType());
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000125}
126
Sanjiv Gupta93eb8252008-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 Gupta40e56a12008-05-08 08:54:20 +0000133/// getOrCreateCompileUnit - Get the compile unit from the cache or create a new
Daniel Dunbar4a66ef12008-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 Dunbar9865e6f2008-10-24 00:46:51 +0000140 SourceManager &SM = M->getContext().getSourceManager();
141 const FileEntry *FE = SM.getFileEntryForLoc(Loc);
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000142
143 // See if this compile unit has been used before.
Daniel Dunbar9865e6f2008-10-24 00:46:51 +0000144 llvm::CompileUnitDesc *&Unit = CompileUnitCache[FE];
145 if (Unit) return Unit;
146
Sanjiv Gupta40e56a12008-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 Dunbar9865e6f2008-10-24 00:46:51 +0000150 Unit = new llvm::CompileUnitDesc();
Sanjiv Gupta40e56a12008-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 Friedman9bc7c8d2008-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 Gupta40e56a12008-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 Gupta40e56a12008-05-08 08:54:20 +0000179 return Unit;
180}
181
182
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000183/// getOrCreateCVRType - Get the CVR qualified type from the cache or create
184/// a new one if necessary.
Sanjiv Gupta93eb8252008-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 Gupta40e56a12008-05-08 08:54:20 +0000191
Sanjiv Gupta93eb8252008-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 Dunbar44252b42008-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 Gupta93eb8252008-05-25 05:15:42 +0000208 DTy->setContext(Unit);
209 DTy->setFromType(FromTy);
210
211 return DTy;
212}
213
214
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000215/// getOrCreateBuiltinType - Get the Basic type from the cache or create a new
Sanjiv Gupta93eb8252008-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);
Daniel Dunbar65b69a82008-10-31 08:12:03 +0000359 Elements.push_back(ArgTy);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000360
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 Stumpa3e509e2008-06-19 20:57:50 +0000371 // FIXME: set other fields file, line here.
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000372 SubrTy->setContext(Unit);
373
374 return SubrTy;
375}
376
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000377/// getOrCreateRecordType - get structure or union type.
Daniel Dunbar8b4aa902008-10-31 04:04:54 +0000378void CGDebugInfo::getOrCreateRecordType(QualType type,
379 llvm::CompileUnitDesc *Unit,
380 llvm::TypeDesc *&Slot)
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000381{
Daniel Dunbar8b4aa902008-10-31 04:04:54 +0000382 // Prevent recursing in type generation by initializing the slot
383 // here.
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000384 llvm::CompositeTypeDesc *RecType;
Daniel Dunbar44252b42008-10-31 03:54:29 +0000385 if (type->isStructureType())
Daniel Dunbar8b4aa902008-10-31 04:04:54 +0000386 Slot = RecType =
387 new llvm::CompositeTypeDesc(llvm::dwarf::DW_TAG_structure_type);
Daniel Dunbar44252b42008-10-31 03:54:29 +0000388 else if (type->isUnionType())
Daniel Dunbar8b4aa902008-10-31 04:04:54 +0000389 Slot = RecType =
390 new llvm::CompositeTypeDesc(llvm::dwarf::DW_TAG_union_type);
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000391 else
Daniel Dunbar8b4aa902008-10-31 04:04:54 +0000392 return;
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000393
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000394 RecordDecl *RecDecl = type->getAsRecordType()->getDecl();
Daniel Dunbar1257f8c2008-10-17 01:07:56 +0000395 // We can not get the type for forward declarations.
396 // FIXME: What *should* we be doing here?
397 if (!RecDecl->getDefinition(M->getContext()))
Daniel Dunbar8b4aa902008-10-31 04:04:54 +0000398 return;
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000399 const ASTRecordLayout &RL = M->getContext().getASTRecordLayout(RecDecl);
400
401 SourceManager &SM = M->getContext().getSourceManager();
402 uint64_t Line = SM.getLogicalLineNumber(RecDecl->getLocation());
403
404 std::vector<llvm::DebugInfoDesc *> &Elements = RecType->getElements();
405
406 // Add the members.
407 int NumMembers = RecDecl->getNumMembers();
408 for (int i = 0; i < NumMembers; i++) {
409 FieldDecl *Member = RecDecl->getMember(i);
410 llvm::TypeDesc *MemberTy = getOrCreateType(Member->getType(), Unit);
411 MemberTy->setOffset(RL.getFieldOffset(i));
412 Elements.push_back(MemberTy);
413 }
414
415 // Fill in the blanks.
Daniel Dunbar44252b42008-10-31 03:54:29 +0000416 if (RecType) {
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000417 RecType->setContext(Unit);
418 RecType->setName(RecDecl->getName());
419 RecType->setFile(getOrCreateCompileUnit(RecDecl->getLocation()));
420 RecType->setLine(Line);
421 RecType->setSize(RL.getSize());
422 RecType->setAlign(RL.getAlignment());
423 RecType->setOffset(0);
424 }
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000425}
426
427/// getOrCreateEnumType - get Enum type.
428llvm::TypeDesc *
429CGDebugInfo::getOrCreateEnumType(QualType type, llvm::CompileUnitDesc *Unit)
430{
431 llvm::CompositeTypeDesc *EnumTy
432 = new llvm::CompositeTypeDesc(llvm::dwarf::DW_TAG_enumeration_type);
433
434 EnumType *EType = dyn_cast<EnumType>(type);
Daniel Dunbar8b4aa902008-10-31 04:04:54 +0000435 if (!EType) return(NULL);
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000436
437 EnumDecl *EDecl = EType->getDecl();
438 SourceManager &SM = M->getContext().getSourceManager();
439 uint64_t Line = SM.getLogicalLineNumber(EDecl->getLocation());
440
441 // Size, align and offset of the type.
442 uint64_t Size = M->getContext().getTypeSize(type);
443 uint64_t Align = M->getContext().getTypeAlign(type);
444
445 // Create descriptors for enum members.
446 std::vector<llvm::DebugInfoDesc *> &Elements = EnumTy->getElements();
447 EnumConstantDecl *ElementList = EDecl->getEnumConstantList();
448 while (ElementList) {
449 llvm::EnumeratorDesc *EnumDesc = new llvm::EnumeratorDesc();
450 // push it to the enum desc list so that we can free it later.
451 EnumDescList.push_back(EnumDesc);
452
453 const char *ElementName = ElementList->getName();
454 uint64_t Value = ElementList->getInitVal().getZExtValue();
455
456 EnumDesc->setName(ElementName);
457 EnumDesc->setValue(Value);
458 Elements.push_back(EnumDesc);
459 if (ElementList->getNextDeclarator())
460 ElementList
461 = dyn_cast<EnumConstantDecl>(ElementList->getNextDeclarator());
462 else
463 break;
464 }
465
466 // Fill in the blanks.
467 if (EnumTy) {
468 EnumTy->setContext(Unit);
469 EnumTy->setName(EDecl->getName());
470 EnumTy->setSize(Size);
471 EnumTy->setAlign(Align);
472 EnumTy->setOffset(0);
473 EnumTy->setFile(getOrCreateCompileUnit(EDecl->getLocation()));
474 EnumTy->setLine(Line);
475 }
476 return EnumTy;
477}
478
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000479/// getOrCreateArrayType - get or create array types.
480llvm::TypeDesc *
481CGDebugInfo::getOrCreateArrayType(QualType type, llvm::CompileUnitDesc *Unit)
482{
483 llvm::CompositeTypeDesc *ArrayTy
484 = new llvm::CompositeTypeDesc(llvm::dwarf::DW_TAG_array_type);
485
486 // Size, align and offset of the type.
487 uint64_t Size = M->getContext().getTypeSize(type);
488 uint64_t Align = M->getContext().getTypeAlign(type);
489
490 SourceManager &SM = M->getContext().getSourceManager();
491 uint64_t Line = SM.getLogicalLineNumber(CurLoc);
492
493 // Add the dimensions of the array.
494 std::vector<llvm::DebugInfoDesc *> &Elements = ArrayTy->getElements();
495 do {
Chris Lattnera1923f62008-08-04 07:31:14 +0000496 const ArrayType *AT = M->getContext().getAsArrayType(type);
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000497 llvm::SubrangeDesc *Subrange = new llvm::SubrangeDesc();
498
499 // push it back on the subrange desc list so that we can free it later.
500 SubrangeDescList.push_back(Subrange);
501
502 uint64_t Upper = 0;
Chris Lattnera1923f62008-08-04 07:31:14 +0000503 if (const ConstantArrayType *ConstArrTy = dyn_cast<ConstantArrayType>(AT)) {
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000504 Upper = ConstArrTy->getSize().getZExtValue() - 1;
505 }
506 Subrange->setLo(0);
507 Subrange->setHi(Upper);
508 Elements.push_back(Subrange);
Chris Lattnera1923f62008-08-04 07:31:14 +0000509 type = AT->getElementType();
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000510 } while (type->isArrayType());
511
512 ArrayTy->setFromType(getOrCreateType(type, Unit));
513
514 if (ArrayTy) {
515 ArrayTy->setContext(Unit);
516 ArrayTy->setSize(Size);
517 ArrayTy->setAlign(Align);
518 ArrayTy->setOffset(0);
519 ArrayTy->setFile(getOrCreateCompileUnit(CurLoc));
520 ArrayTy->setLine(Line);
521 }
522 return ArrayTy;
523}
524
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000525
526/// getOrCreateTaggedType - get or create structure/union/Enum type.
Daniel Dunbar8b4aa902008-10-31 04:04:54 +0000527void CGDebugInfo::getOrCreateTaggedType(QualType type,
528 llvm::CompileUnitDesc *Unit,
529 llvm::TypeDesc *&Slot)
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000530{
531 if (type->isStructureType() || type->isUnionType())
Daniel Dunbar8b4aa902008-10-31 04:04:54 +0000532 getOrCreateRecordType(type, Unit, Slot);
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000533 else if (type->isEnumeralType())
Daniel Dunbar8b4aa902008-10-31 04:04:54 +0000534 Slot = getOrCreateEnumType(type, Unit);
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000535}
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000536
537/// getOrCreateType - Get the type from the cache or create a new
538/// one if necessary.
539llvm::TypeDesc *
540CGDebugInfo::getOrCreateType(QualType type, llvm::CompileUnitDesc *Unit)
541{
542 if (type.isNull())
543 return NULL;
544
545 // Check to see if the compile unit already has created this type.
546 llvm::TypeDesc *&Slot = TypeCache[type.getAsOpaquePtr()];
547 if (Slot) return Slot;
548
549 // We need to check for the CVR qualifiers as the first thing.
550 if (type.getCVRQualifiers()) {
Daniel Dunbar8b4aa902008-10-31 04:04:54 +0000551 Slot = getOrCreateCVRType(type, Unit);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000552 return Slot;
553 }
554
555 // Work out details of type.
Daniel Dunbar44252b42008-10-31 03:54:29 +0000556 switch (type->getTypeClass()) {
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000557 case Type::Complex:
558 case Type::Reference:
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000559 case Type::Vector:
560 case Type::ExtVector:
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000561 case Type::ASQual:
562 case Type::ObjCInterface:
563 case Type::ObjCQualifiedInterface:
564 case Type::ObjCQualifiedId:
565 case Type::TypeOfExp:
566 case Type::TypeOfTyp:
567 default:
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000568 return NULL;
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000569
570 case Type::TypeName:
571 Slot = getOrCreateTypedefType(type, Unit);
572 break;
573
574 case Type::FunctionProto:
575 case Type::FunctionNoProto:
576 Slot = getOrCreateFunctionType(type, Unit);
577 break;
578
579 case Type::Builtin:
580 Slot = getOrCreateBuiltinType(type, Unit);
581 break;
582
583 case Type::Pointer:
584 Slot = getOrCreatePointerType(type, Unit);
585 break;
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000586
587 case Type::Tagged:
Daniel Dunbar8b4aa902008-10-31 04:04:54 +0000588 getOrCreateTaggedType(type, Unit, Slot);
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000589 break;
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000590
591 case Type::ConstantArray:
592 case Type::VariableArray:
593 case Type::IncompleteArray:
594 Slot = getOrCreateArrayType(type, Unit);
595 break;
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000596 }
597
598 return Slot;
599}
600
601/// EmitFunctionStart - Constructs the debug code for entering a function -
602/// "llvm.dbg.func.start.".
Daniel Dunbar54968bf2008-10-18 18:22:23 +0000603void CGDebugInfo::EmitFunctionStart(const char *Name,
604 QualType ReturnType,
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000605 llvm::Function *Fn,
Chris Lattnerfaf23db2008-08-08 19:57:58 +0000606 llvm::IRBuilder<> &Builder)
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000607{
608 // Create subprogram descriptor.
609 Subprogram = new llvm::SubprogramDesc();
610
611 // Make sure we have an anchor.
612 if (!SubprogramAnchor) {
613 SubprogramAnchor = new llvm::AnchorDesc(Subprogram);
614 }
615
616 // Get name information.
Daniel Dunbar54968bf2008-10-18 18:22:23 +0000617 Subprogram->setName(Name);
618 Subprogram->setFullName(Name);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000619
620 // Gather location information.
621 llvm::CompileUnitDesc *Unit = getOrCreateCompileUnit(CurLoc);
622 SourceManager &SM = M->getContext().getSourceManager();
623 uint64_t Loc = SM.getLogicalLineNumber(CurLoc);
624
625 // Get Function Type.
Daniel Dunbar54968bf2008-10-18 18:22:23 +0000626 llvm::TypeDesc *SPTy = getOrCreateType(ReturnType, Unit);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000627
628 Subprogram->setAnchor(SubprogramAnchor);
629 Subprogram->setContext(Unit);
630 Subprogram->setFile(Unit);
631 Subprogram->setLine(Loc);
632 Subprogram->setType(SPTy);
633 Subprogram->setIsStatic(Fn->hasInternalLinkage());
634 Subprogram->setIsDefinition(true);
635
636 // Lazily construct llvm.dbg.func.start.
637 if (!FuncStartFn)
638 FuncStartFn = llvm::Intrinsic::getDeclaration(&M->getModule(),
639 llvm::Intrinsic::dbg_func_start);
640
641 // Call llvm.dbg.func.start which also implicitly calls llvm.dbg.stoppoint.
642 Builder.CreateCall(FuncStartFn, getCastValueFor(Subprogram), "");
643
644 // Push function on region stack.
645 RegionStack.push_back(Subprogram);
646}
647
648
649void
Chris Lattnerfaf23db2008-08-08 19:57:58 +0000650CGDebugInfo::EmitStopPoint(llvm::Function *Fn, llvm::IRBuilder<> &Builder)
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000651{
652 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
653
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000654 // Don't bother if things are the same as last time.
655 SourceManager &SM = M->getContext().getSourceManager();
656 if (CurLoc == PrevLoc
657 || (SM.getLineNumber(CurLoc) == SM.getLineNumber(PrevLoc)
658 && SM.isFromSameFile(CurLoc, PrevLoc)))
659 return;
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000660
661 // Update last state.
662 PrevLoc = CurLoc;
663
664 // Get the appropriate compile unit.
665 llvm::CompileUnitDesc *Unit = getOrCreateCompileUnit(CurLoc);
666
667 // Lazily construct llvm.dbg.stoppoint function.
668 if (!StopPointFn)
669 StopPointFn = llvm::Intrinsic::getDeclaration(&M->getModule(),
Eli Friedman6fb115c2008-05-13 14:40:48 +0000670 llvm::Intrinsic::dbg_stoppoint);
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000671
672 uint64_t CurLineNo = SM.getLogicalLineNumber(CurLoc);
673 uint64_t ColumnNo = SM.getLogicalColumnNumber(CurLoc);
674
675 // Invoke llvm.dbg.stoppoint
676 Builder.CreateCall3(StopPointFn,
Eli Friedman6fb115c2008-05-13 14:40:48 +0000677 llvm::ConstantInt::get(llvm::Type::Int32Ty, CurLineNo),
678 llvm::ConstantInt::get(llvm::Type::Int32Ty, ColumnNo),
679 getCastValueFor(Unit), "");
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000680}
681
682/// EmitRegionStart- Constructs the debug code for entering a declarative
683/// region - "llvm.dbg.region.start.".
Chris Lattnerfaf23db2008-08-08 19:57:58 +0000684void CGDebugInfo::EmitRegionStart(llvm::Function *Fn,
685 llvm::IRBuilder<> &Builder)
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000686{
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000687 llvm::BlockDesc *Block = new llvm::BlockDesc();
Daniel Dunbar1257f8c2008-10-17 01:07:56 +0000688 if (!RegionStack.empty())
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000689 Block->setContext(RegionStack.back());
690 RegionStack.push_back(Block);
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000691
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000692 // Lazily construct llvm.dbg.region.start function.
693 if (!RegionStartFn)
694 RegionStartFn = llvm::Intrinsic::getDeclaration(&M->getModule(),
695 llvm::Intrinsic::dbg_region_start);
696
697 // Call llvm.dbg.func.start.
698 Builder.CreateCall(RegionStartFn, getCastValueFor(Block), "");
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000699}
700
701/// EmitRegionEnd - Constructs the debug code for exiting a declarative
702/// region - "llvm.dbg.region.end."
Chris Lattnerfaf23db2008-08-08 19:57:58 +0000703void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, llvm::IRBuilder<> &Builder)
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000704{
Daniel Dunbar1257f8c2008-10-17 01:07:56 +0000705 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
706
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000707 // Lazily construct llvm.dbg.region.end function.
708 if (!RegionEndFn)
709 RegionEndFn =llvm::Intrinsic::getDeclaration(&M->getModule(),
Eli Friedman6fb115c2008-05-13 14:40:48 +0000710 llvm::Intrinsic::dbg_region_end);
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000711
712 // Provide an region stop point.
713 EmitStopPoint(Fn, Builder);
714
715 // Call llvm.dbg.func.end.
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000716 llvm::DebugInfoDesc *DID = RegionStack.back();
717 Builder.CreateCall(RegionEndFn, getCastValueFor(DID), "");
718 RegionStack.pop_back();
Daniel Dunbar6fc1f972008-10-17 16:15:48 +0000719 // FIXME: Should be freeing here?
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000720}
721
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000722/// EmitDeclare - Emit local variable declaration debug info.
723void CGDebugInfo::EmitDeclare(const VarDecl *decl, unsigned Tag,
724 llvm::Value *AI,
Chris Lattnerfaf23db2008-08-08 19:57:58 +0000725 llvm::IRBuilder<> &Builder)
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000726{
Daniel Dunbar1257f8c2008-10-17 01:07:56 +0000727 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
728
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000729 // FIXME: If it is a compiler generated temporary then return.
730
731 // Construct llvm.dbg.declare function.
732 if (!DeclareFn)
733 DeclareFn = llvm::Intrinsic::getDeclaration(&M->getModule(),
Mike Stumpa3e509e2008-06-19 20:57:50 +0000734 llvm::Intrinsic::dbg_declare);
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000735
736 // Get type information.
737 llvm::CompileUnitDesc *Unit = getOrCreateCompileUnit(CurLoc);
738 llvm::TypeDesc *TyDesc = getOrCreateType(decl->getType(), Unit);
739
740 SourceManager &SM = M->getContext().getSourceManager();
741 uint64_t Loc = SM.getLogicalLineNumber(CurLoc);
742
743 // Construct variable.
744 llvm::VariableDesc *Variable = new llvm::VariableDesc(Tag);
745 Variable->setContext(RegionStack.back());
746 Variable->setName(decl->getName());
747 Variable->setFile(Unit);
748 Variable->setLine(Loc);
749 Variable->setType(TyDesc);
750
751 // Push it onto the list so that we can free it.
752 VariableDescList.push_back(Variable);
753
754 // Cast the AllocA result to a {}* for the call to llvm.dbg.declare.
755 // These bit cast instructions will get freed when the basic block is
756 // deleted. So do not need to free them explicity.
757 const llvm::PointerType *EmpPtr = SR->getEmptyStructPtrType();
758 llvm::Value *AllocACast = new llvm::BitCastInst(AI, EmpPtr, decl->getName(),
759 Builder.GetInsertBlock());
760
761 // Call llvm.dbg.declare.
762 Builder.CreateCall2(DeclareFn, AllocACast, getCastValueFor(Variable), "");
763}
764
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000765/// EmitGlobalVariable - Emit information about a global variable.
766void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *GV,
767 const VarDecl *decl)
768{
769 // Create global variable debug descriptor.
770 llvm::GlobalVariableDesc *Global = new llvm::GlobalVariableDesc();
771
772 // Push it onto the list so that we can free it.
773 GlobalVarDescList.push_back(Global);
774
775 // Make sure we have an anchor.
776 if (!GlobalVariableAnchor)
777 GlobalVariableAnchor = new llvm::AnchorDesc(Global);
778
779 // Get name information.
780 Global->setName(decl->getName());
781 Global->setFullName(decl->getName());
782
783 llvm::CompileUnitDesc *Unit = getOrCreateCompileUnit(CurLoc);
784 SourceManager &SM = M->getContext().getSourceManager();
785 uint64_t Loc = SM.getLogicalLineNumber(CurLoc);
786
787 llvm::TypeDesc *TyD = getOrCreateType(decl->getType(), Unit);
788
789 // Fill in the Global information.
790 Global->setAnchor(GlobalVariableAnchor);
791 Global->setContext(Unit);
792 Global->setFile(Unit);
793 Global->setLine(Loc);
794 Global->setType(TyD);
795 Global->setIsDefinition(true);
796 Global->setIsStatic(GV->hasInternalLinkage());
797 Global->setGlobalVariable(GV);
798
799 // Make sure global is created if needed.
800 getValueFor(Global);
801}
802