blob: d6902bb93e782e31dc135fbbd48cbd404591a867 [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{
62 delete SR;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000063
64 // Free CompileUnitCache.
65 for (std::map<unsigned, llvm::CompileUnitDesc *>::iterator I
66 = CompileUnitCache.begin(); I != CompileUnitCache.end(); ++I) {
67 delete I->second;
68 }
69 CompileUnitCache.clear();
70
71 // Free TypeCache.
72 for (std::map<void *, llvm::TypeDesc *>::iterator I
73 = TypeCache.begin(); I != TypeCache.end(); ++I) {
74 delete I->second;
75 }
76 TypeCache.clear();
77
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +000078 // Free region descriptors.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000079 for (std::vector<llvm::DebugInfoDesc *>::iterator I
80 = RegionStack.begin(); I != RegionStack.end(); ++I) {
81 delete *I;
82 }
Sanjiv Guptacc9b1632008-05-30 10:30:31 +000083
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +000084 // Free local var descriptors.
Sanjiv Guptacc9b1632008-05-30 10:30:31 +000085 for (std::vector<llvm::VariableDesc *>::iterator I
86 = VariableDescList.begin(); I != VariableDescList.end(); ++I) {
87 delete *I;
88 }
89
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +000090 // Free global var descriptors.
Sanjiv Gupta686226b2008-06-05 08:59:10 +000091 for (std::vector<llvm::GlobalVariableDesc *>::iterator I
92 = GlobalVarDescList.begin(); I != GlobalVarDescList.end(); ++I) {
93 delete *I;
94 }
95
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +000096 // Free enum constants descriptors.
97 for (std::vector<llvm::EnumeratorDesc *>::iterator I
98 = EnumDescList.begin(); I != EnumDescList.end(); ++I) {
99 delete *I;
100 }
101
Sanjiv Gupta507de852008-06-09 10:47:41 +0000102 // Free subrange descriptors.
103 for (std::vector<llvm::SubrangeDesc *>::iterator I
104 = SubrangeDescList.begin(); I != SubrangeDescList.end(); ++I) {
105 delete *I;
106 }
107
Eli Friedman3f2af102008-05-22 01:40:10 +0000108 delete CompileUnitAnchor;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000109 delete SubprogramAnchor;
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000110 delete GlobalVariableAnchor;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000111}
112
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000113void CGDebugInfo::setLocation(SourceLocation loc) {
114 CurLoc = M->getContext().getSourceManager().getLogicalLoc(loc);
Eli Friedman32ea35f2008-05-29 11:08:17 +0000115}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000116
117/// getCastValueFor - Return a llvm representation for a given debug information
118/// descriptor cast to an empty struct pointer.
119llvm::Value *CGDebugInfo::getCastValueFor(llvm::DebugInfoDesc *DD) {
120 return llvm::ConstantExpr::getBitCast(SR->Serialize(DD),
Eli Friedman86eb3112008-05-13 14:40:48 +0000121 SR->getEmptyStructPtrType());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000122}
123
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000124/// getValueFor - Return a llvm representation for a given debug information
125/// descriptor.
126llvm::Value *CGDebugInfo::getValueFor(llvm::DebugInfoDesc *DD) {
127 return SR->Serialize(DD);
128}
129
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000130/// getOrCreateCompileUnit - Get the compile unit from the cache or create a new
131/// one if necessary.
132llvm::CompileUnitDesc
133*CGDebugInfo::getOrCreateCompileUnit(const SourceLocation Loc) {
134
135 // See if this compile unit has been used before.
136 llvm::CompileUnitDesc *&Slot = CompileUnitCache[Loc.getFileID()];
137 if (Slot) return Slot;
138
139 // Create new compile unit.
140 // FIXME: Where to free these?
141 // One way is to iterate over the CompileUnitCache in ~CGDebugInfo.
142 llvm::CompileUnitDesc *Unit = new llvm::CompileUnitDesc();
143
144 // Make sure we have an anchor.
145 if (!CompileUnitAnchor) {
146 CompileUnitAnchor = new llvm::AnchorDesc(Unit);
147 }
148
149 // Get source file information.
150 SourceManager &SM = M->getContext().getSourceManager();
151 const FileEntry *FE = SM.getFileEntryForLoc(Loc);
Eli Friedman3f2af102008-05-22 01:40:10 +0000152 const char *FileName, *DirName;
153 if (FE) {
154 FileName = FE->getName();
155 DirName = FE->getDir()->getName();
156 } else {
157 FileName = SM.getSourceName(Loc);
158 DirName = "";
159 }
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000160
161 Unit->setAnchor(CompileUnitAnchor);
162 Unit->setFileName(FileName);
163 Unit->setDirectory(DirName);
164
165 // Set up producer name.
166 // FIXME: Do not know how to get clang version yet.
167 Unit->setProducer("clang");
168
169 // Set up Language number.
170 // FIXME: Handle other languages as well.
171 Unit->setLanguage(llvm::dwarf::DW_LANG_C89);
172
173 // Update cache.
174 Slot = Unit;
175
176 return Unit;
177}
178
179
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000180/// getOrCreateCVRType - Get the CVR qualified type from the cache or create
181/// a new one if necessary.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000182llvm::TypeDesc *
183CGDebugInfo::getOrCreateCVRType(QualType type, llvm::CompileUnitDesc *Unit)
184{
185 // We will create a Derived type.
186 llvm::DerivedTypeDesc *DTy = NULL;
187 llvm::TypeDesc *FromTy = NULL;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000188
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000189 if (type.isConstQualified()) {
190 DTy = new llvm::DerivedTypeDesc(llvm::dwarf::DW_TAG_const_type);
191 type.removeConst();
192 FromTy = getOrCreateType(type, Unit);
193 } else if (type.isVolatileQualified()) {
194 DTy = new llvm::DerivedTypeDesc(llvm::dwarf::DW_TAG_volatile_type);
195 type.removeVolatile();
196 FromTy = getOrCreateType(type, Unit);
197 } else if (type.isRestrictQualified()) {
198 DTy = new llvm::DerivedTypeDesc(llvm::dwarf::DW_TAG_restrict_type);
199 type.removeRestrict();
200 FromTy = getOrCreateType(type, Unit);
201 }
202
203 // No need to fill in the Name, Line, Size, Alignment, Offset in case of // CVR derived types.
204 DTy->setContext(Unit);
205 DTy->setFromType(FromTy);
206
207 return DTy;
208}
209
210
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000211/// getOrCreateBuiltinType - Get the Basic type from the cache or create a new
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000212/// one if necessary.
213llvm::TypeDesc *
214CGDebugInfo::getOrCreateBuiltinType(QualType type, llvm::CompileUnitDesc *Unit)
215{
216 assert (type->getTypeClass() == Type::Builtin);
217
218 const BuiltinType *BT = type->getAsBuiltinType();
219
220 unsigned Encoding = 0;
221 switch (BT->getKind())
222 {
223 case BuiltinType::Void:
224 return NULL;
225 case BuiltinType::UChar:
226 case BuiltinType::Char_U:
227 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
228 break;
229 case BuiltinType::Char_S:
230 case BuiltinType::SChar:
231 Encoding = llvm::dwarf::DW_ATE_signed_char;
232 break;
233 case BuiltinType::UShort:
234 case BuiltinType::UInt:
235 case BuiltinType::ULong:
236 case BuiltinType::ULongLong:
237 Encoding = llvm::dwarf::DW_ATE_unsigned;
238 break;
239 case BuiltinType::Short:
240 case BuiltinType::Int:
241 case BuiltinType::Long:
242 case BuiltinType::LongLong:
243 Encoding = llvm::dwarf::DW_ATE_signed;
244 break;
245 case BuiltinType::Bool:
246 Encoding = llvm::dwarf::DW_ATE_boolean;
247 break;
248 case BuiltinType::Float:
249 case BuiltinType::Double:
250 Encoding = llvm::dwarf::DW_ATE_float;
251 break;
252 default:
253 Encoding = llvm::dwarf::DW_ATE_signed;
254 break;
255 }
256
257 // Ty will have contain the resulting type.
258 llvm::BasicTypeDesc *BTy = new llvm::BasicTypeDesc();
259
260 // Get the name and location early to assist debugging.
261 const char *TyName = BT->getName();
262
263 // Bit size, align and offset of the type.
264 uint64_t Size = M->getContext().getTypeSize(type);
265 uint64_t Align = M->getContext().getTypeAlign(type);
266 uint64_t Offset = 0;
267
268 // If the type is defined, fill in the details.
269 if (BTy) {
270 BTy->setContext(Unit);
271 BTy->setName(TyName);
272 BTy->setSize(Size);
273 BTy->setAlign(Align);
274 BTy->setOffset(Offset);
275 BTy->setEncoding(Encoding);
276 }
277
278 return BTy;
279}
280
281llvm::TypeDesc *
282CGDebugInfo::getOrCreatePointerType(QualType type, llvm::CompileUnitDesc *Unit)
283{
284 // type*
285 llvm::DerivedTypeDesc *DTy =
286 new llvm::DerivedTypeDesc(llvm::dwarf::DW_TAG_pointer_type);
287
288 // Handle the derived type.
289 const PointerType *PTRT = type->getAsPointerType();
290 llvm::TypeDesc *FromTy = getOrCreateType(PTRT->getPointeeType(), Unit);
291
292 // Get the name and location early to assist debugging.
293 SourceManager &SM = M->getContext().getSourceManager();
294 uint64_t Line = SM.getLogicalLineNumber(CurLoc);
295
296 // Bit size, align and offset of the type.
297 uint64_t Size = M->getContext().getTypeSize(type);
298 uint64_t Align = M->getContext().getTypeAlign(type);
299 uint64_t Offset = 0;
300
301 // If the type is defined, fill in the details.
302 if (DTy) {
303 DTy->setContext(Unit);
304 DTy->setLine(Line);
305 DTy->setSize(Size);
306 DTy->setAlign(Align);
307 DTy->setOffset(Offset);
308 DTy->setFromType(FromTy);
309 }
310
311 return DTy;
312}
313
314llvm::TypeDesc *
315CGDebugInfo::getOrCreateTypedefType(QualType type, llvm::CompileUnitDesc *Unit)
316{
317 // typedefs are derived from some other type.
318 llvm::DerivedTypeDesc *DTy =
319 new llvm::DerivedTypeDesc(llvm::dwarf::DW_TAG_typedef);
320
321 // Handle derived type.
322 const TypedefType *TDT = type->getAsTypedefType();
323 llvm::TypeDesc *FromTy = getOrCreateType(TDT->LookThroughTypedefs(),
324 Unit);
325
326 // Get the name and location early to assist debugging.
327 const char *TyName = TDT->getDecl()->getName();
328 SourceManager &SM = M->getContext().getSourceManager();
329 uint64_t Line = SM.getLogicalLineNumber(TDT->getDecl()->getLocation());
330
331 // If the type is defined, fill in the details.
332 if (DTy) {
333 DTy->setContext(Unit);
334 DTy->setFile(getOrCreateCompileUnit(TDT->getDecl()->getLocation()));
335 DTy->setLine(Line);
336 DTy->setName(TyName);
337 DTy->setFromType(FromTy);
338 }
339
340 return DTy;
341}
342
343llvm::TypeDesc *
344CGDebugInfo::getOrCreateFunctionType(QualType type, llvm::CompileUnitDesc *Unit)
345{
346 llvm::CompositeTypeDesc *SubrTy =
347 new llvm::CompositeTypeDesc(llvm::dwarf::DW_TAG_subroutine_type);
348
349 // Prepare to add the arguments for the subroutine.
350 std::vector<llvm::DebugInfoDesc *> &Elements = SubrTy->getElements();
351
352 // Get result type.
353 const FunctionType *FT = type->getAsFunctionType();
354 llvm::TypeDesc *ArgTy = getOrCreateType(FT->getResultType(), Unit);
355 if (ArgTy) Elements.push_back(ArgTy);
356
357 // Set up remainder of arguments.
358 if (type->getTypeClass() == Type::FunctionProto) {
359 const FunctionTypeProto *FTPro = dyn_cast<FunctionTypeProto>(type);
360 for (unsigned int i =0; i < FTPro->getNumArgs(); i++) {
361 QualType ParamType = FTPro->getArgType(i);
362 ArgTy = getOrCreateType(ParamType, Unit);
363 if (ArgTy) Elements.push_back(ArgTy);
364 }
365 }
366
Mike Stump9ea58842008-06-19 20:57:50 +0000367 // FIXME: set other fields file, line here.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000368 SubrTy->setContext(Unit);
369
370 return SubrTy;
371}
372
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000373/// getOrCreateRecordType - get structure or union type.
374llvm::TypeDesc *
375CGDebugInfo::getOrCreateRecordType(QualType type, llvm::CompileUnitDesc *Unit)
376{
377 llvm::CompositeTypeDesc *RecType;
378 if(type->isStructureType())
379 RecType = new llvm::CompositeTypeDesc(llvm::dwarf::DW_TAG_structure_type);
380 else if(type->isUnionType())
381 RecType = new llvm::CompositeTypeDesc(llvm::dwarf::DW_TAG_union_type);
382 else
383 return NULL;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000384
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000385 RecordDecl *RecDecl = type->getAsRecordType()->getDecl();
386 const ASTRecordLayout &RL = M->getContext().getASTRecordLayout(RecDecl);
387
388 SourceManager &SM = M->getContext().getSourceManager();
389 uint64_t Line = SM.getLogicalLineNumber(RecDecl->getLocation());
390
391 std::vector<llvm::DebugInfoDesc *> &Elements = RecType->getElements();
392
393 // Add the members.
394 int NumMembers = RecDecl->getNumMembers();
395 for (int i = 0; i < NumMembers; i++) {
396 FieldDecl *Member = RecDecl->getMember(i);
397 llvm::TypeDesc *MemberTy = getOrCreateType(Member->getType(), Unit);
398 MemberTy->setOffset(RL.getFieldOffset(i));
399 Elements.push_back(MemberTy);
400 }
401
402 // Fill in the blanks.
403 if(RecType) {
404 RecType->setContext(Unit);
405 RecType->setName(RecDecl->getName());
406 RecType->setFile(getOrCreateCompileUnit(RecDecl->getLocation()));
407 RecType->setLine(Line);
408 RecType->setSize(RL.getSize());
409 RecType->setAlign(RL.getAlignment());
410 RecType->setOffset(0);
411 }
412 return(RecType);
413}
414
415/// getOrCreateEnumType - get Enum type.
416llvm::TypeDesc *
417CGDebugInfo::getOrCreateEnumType(QualType type, llvm::CompileUnitDesc *Unit)
418{
419 llvm::CompositeTypeDesc *EnumTy
420 = new llvm::CompositeTypeDesc(llvm::dwarf::DW_TAG_enumeration_type);
421
422 EnumType *EType = dyn_cast<EnumType>(type);
423 if (!EType) return(NULL);
424
425 EnumDecl *EDecl = EType->getDecl();
426 SourceManager &SM = M->getContext().getSourceManager();
427 uint64_t Line = SM.getLogicalLineNumber(EDecl->getLocation());
428
429 // Size, align and offset of the type.
430 uint64_t Size = M->getContext().getTypeSize(type);
431 uint64_t Align = M->getContext().getTypeAlign(type);
432
433 // Create descriptors for enum members.
434 std::vector<llvm::DebugInfoDesc *> &Elements = EnumTy->getElements();
435 EnumConstantDecl *ElementList = EDecl->getEnumConstantList();
436 while (ElementList) {
437 llvm::EnumeratorDesc *EnumDesc = new llvm::EnumeratorDesc();
438 // push it to the enum desc list so that we can free it later.
439 EnumDescList.push_back(EnumDesc);
440
441 const char *ElementName = ElementList->getName();
442 uint64_t Value = ElementList->getInitVal().getZExtValue();
443
444 EnumDesc->setName(ElementName);
445 EnumDesc->setValue(Value);
446 Elements.push_back(EnumDesc);
447 if (ElementList->getNextDeclarator())
448 ElementList
449 = dyn_cast<EnumConstantDecl>(ElementList->getNextDeclarator());
450 else
451 break;
452 }
453
454 // Fill in the blanks.
455 if (EnumTy) {
456 EnumTy->setContext(Unit);
457 EnumTy->setName(EDecl->getName());
458 EnumTy->setSize(Size);
459 EnumTy->setAlign(Align);
460 EnumTy->setOffset(0);
461 EnumTy->setFile(getOrCreateCompileUnit(EDecl->getLocation()));
462 EnumTy->setLine(Line);
463 }
464 return EnumTy;
465}
466
Sanjiv Gupta507de852008-06-09 10:47:41 +0000467/// getOrCreateArrayType - get or create array types.
468llvm::TypeDesc *
469CGDebugInfo::getOrCreateArrayType(QualType type, llvm::CompileUnitDesc *Unit)
470{
471 llvm::CompositeTypeDesc *ArrayTy
472 = new llvm::CompositeTypeDesc(llvm::dwarf::DW_TAG_array_type);
473
474 // Size, align and offset of the type.
475 uint64_t Size = M->getContext().getTypeSize(type);
476 uint64_t Align = M->getContext().getTypeAlign(type);
477
478 SourceManager &SM = M->getContext().getSourceManager();
479 uint64_t Line = SM.getLogicalLineNumber(CurLoc);
480
481 // Add the dimensions of the array.
482 std::vector<llvm::DebugInfoDesc *> &Elements = ArrayTy->getElements();
483 do {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000484 const ArrayType *AT = M->getContext().getAsArrayType(type);
Sanjiv Gupta507de852008-06-09 10:47:41 +0000485 llvm::SubrangeDesc *Subrange = new llvm::SubrangeDesc();
486
487 // push it back on the subrange desc list so that we can free it later.
488 SubrangeDescList.push_back(Subrange);
489
490 uint64_t Upper = 0;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000491 if (const ConstantArrayType *ConstArrTy = dyn_cast<ConstantArrayType>(AT)) {
Sanjiv Gupta507de852008-06-09 10:47:41 +0000492 Upper = ConstArrTy->getSize().getZExtValue() - 1;
493 }
494 Subrange->setLo(0);
495 Subrange->setHi(Upper);
496 Elements.push_back(Subrange);
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000497 type = AT->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000498 } while (type->isArrayType());
499
500 ArrayTy->setFromType(getOrCreateType(type, Unit));
501
502 if (ArrayTy) {
503 ArrayTy->setContext(Unit);
504 ArrayTy->setSize(Size);
505 ArrayTy->setAlign(Align);
506 ArrayTy->setOffset(0);
507 ArrayTy->setFile(getOrCreateCompileUnit(CurLoc));
508 ArrayTy->setLine(Line);
509 }
510 return ArrayTy;
511}
512
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000513
514/// getOrCreateTaggedType - get or create structure/union/Enum type.
515llvm::TypeDesc *
516CGDebugInfo::getOrCreateTaggedType(QualType type, llvm::CompileUnitDesc *Unit)
517{
518 if (type->isStructureType() || type->isUnionType())
519 return getOrCreateRecordType(type, Unit);
520 else if (type->isEnumeralType())
521 return getOrCreateEnumType(type, Unit);
522 else
523 return NULL;
524}
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000525
526/// getOrCreateType - Get the type from the cache or create a new
527/// one if necessary.
528llvm::TypeDesc *
529CGDebugInfo::getOrCreateType(QualType type, llvm::CompileUnitDesc *Unit)
530{
531 if (type.isNull())
532 return NULL;
533
534 // Check to see if the compile unit already has created this type.
535 llvm::TypeDesc *&Slot = TypeCache[type.getAsOpaquePtr()];
536 if (Slot) return Slot;
537
538 // We need to check for the CVR qualifiers as the first thing.
539 if (type.getCVRQualifiers()) {
540 Slot = getOrCreateCVRType (type, Unit);
541 return Slot;
542 }
543
544 // Work out details of type.
545 switch(type->getTypeClass()) {
546 case Type::Complex:
547 case Type::Reference:
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000548 case Type::Vector:
549 case Type::ExtVector:
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000550 case Type::ASQual:
551 case Type::ObjCInterface:
552 case Type::ObjCQualifiedInterface:
553 case Type::ObjCQualifiedId:
554 case Type::TypeOfExp:
555 case Type::TypeOfTyp:
556 default:
557 {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000558 return NULL;
559 }
560
561 case Type::TypeName:
562 Slot = getOrCreateTypedefType(type, Unit);
563 break;
564
565 case Type::FunctionProto:
566 case Type::FunctionNoProto:
567 Slot = getOrCreateFunctionType(type, Unit);
568 break;
569
570 case Type::Builtin:
571 Slot = getOrCreateBuiltinType(type, Unit);
572 break;
573
574 case Type::Pointer:
575 Slot = getOrCreatePointerType(type, Unit);
576 break;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000577
578 case Type::Tagged:
579 Slot = getOrCreateTaggedType(type, Unit);
580 break;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000581
582 case Type::ConstantArray:
583 case Type::VariableArray:
584 case Type::IncompleteArray:
585 Slot = getOrCreateArrayType(type, Unit);
586 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000587 }
588
589 return Slot;
590}
591
592/// EmitFunctionStart - Constructs the debug code for entering a function -
593/// "llvm.dbg.func.start.".
594void CGDebugInfo::EmitFunctionStart(const FunctionDecl *FnDecl,
595 llvm::Function *Fn,
Chris Lattner85e35682008-08-08 19:57:58 +0000596 llvm::IRBuilder<> &Builder)
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000597{
598 // Create subprogram descriptor.
599 Subprogram = new llvm::SubprogramDesc();
600
601 // Make sure we have an anchor.
602 if (!SubprogramAnchor) {
603 SubprogramAnchor = new llvm::AnchorDesc(Subprogram);
604 }
605
606 // Get name information.
607 Subprogram->setName(FnDecl->getName());
608 Subprogram->setFullName(FnDecl->getName());
609
610 // Gather location information.
611 llvm::CompileUnitDesc *Unit = getOrCreateCompileUnit(CurLoc);
612 SourceManager &SM = M->getContext().getSourceManager();
613 uint64_t Loc = SM.getLogicalLineNumber(CurLoc);
614
615 // Get Function Type.
616 QualType type = FnDecl->getResultType();
617 llvm::TypeDesc *SPTy = getOrCreateType(type, Unit);
618
619 Subprogram->setAnchor(SubprogramAnchor);
620 Subprogram->setContext(Unit);
621 Subprogram->setFile(Unit);
622 Subprogram->setLine(Loc);
623 Subprogram->setType(SPTy);
624 Subprogram->setIsStatic(Fn->hasInternalLinkage());
625 Subprogram->setIsDefinition(true);
626
627 // Lazily construct llvm.dbg.func.start.
628 if (!FuncStartFn)
629 FuncStartFn = llvm::Intrinsic::getDeclaration(&M->getModule(),
630 llvm::Intrinsic::dbg_func_start);
631
632 // Call llvm.dbg.func.start which also implicitly calls llvm.dbg.stoppoint.
633 Builder.CreateCall(FuncStartFn, getCastValueFor(Subprogram), "");
634
635 // Push function on region stack.
636 RegionStack.push_back(Subprogram);
637}
638
639
640void
Chris Lattner85e35682008-08-08 19:57:58 +0000641CGDebugInfo::EmitStopPoint(llvm::Function *Fn, llvm::IRBuilder<> &Builder)
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000642{
643 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
644
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000645 // Don't bother if things are the same as last time.
646 SourceManager &SM = M->getContext().getSourceManager();
647 if (CurLoc == PrevLoc
648 || (SM.getLineNumber(CurLoc) == SM.getLineNumber(PrevLoc)
649 && SM.isFromSameFile(CurLoc, PrevLoc)))
650 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000651
652 // Update last state.
653 PrevLoc = CurLoc;
654
655 // Get the appropriate compile unit.
656 llvm::CompileUnitDesc *Unit = getOrCreateCompileUnit(CurLoc);
657
658 // Lazily construct llvm.dbg.stoppoint function.
659 if (!StopPointFn)
660 StopPointFn = llvm::Intrinsic::getDeclaration(&M->getModule(),
Eli Friedman86eb3112008-05-13 14:40:48 +0000661 llvm::Intrinsic::dbg_stoppoint);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000662
663 uint64_t CurLineNo = SM.getLogicalLineNumber(CurLoc);
664 uint64_t ColumnNo = SM.getLogicalColumnNumber(CurLoc);
665
666 // Invoke llvm.dbg.stoppoint
667 Builder.CreateCall3(StopPointFn,
Eli Friedman86eb3112008-05-13 14:40:48 +0000668 llvm::ConstantInt::get(llvm::Type::Int32Ty, CurLineNo),
669 llvm::ConstantInt::get(llvm::Type::Int32Ty, ColumnNo),
670 getCastValueFor(Unit), "");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000671}
672
673/// EmitRegionStart- Constructs the debug code for entering a declarative
674/// region - "llvm.dbg.region.start.".
Chris Lattner85e35682008-08-08 19:57:58 +0000675void CGDebugInfo::EmitRegionStart(llvm::Function *Fn,
676 llvm::IRBuilder<> &Builder)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000677{
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000678 llvm::BlockDesc *Block = new llvm::BlockDesc();
679 if (RegionStack.size() > 0)
680 Block->setContext(RegionStack.back());
681 RegionStack.push_back(Block);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000682
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000683 // Lazily construct llvm.dbg.region.start function.
684 if (!RegionStartFn)
685 RegionStartFn = llvm::Intrinsic::getDeclaration(&M->getModule(),
686 llvm::Intrinsic::dbg_region_start);
687
688 // Call llvm.dbg.func.start.
689 Builder.CreateCall(RegionStartFn, getCastValueFor(Block), "");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000690}
691
692/// EmitRegionEnd - Constructs the debug code for exiting a declarative
693/// region - "llvm.dbg.region.end."
Chris Lattner85e35682008-08-08 19:57:58 +0000694void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, llvm::IRBuilder<> &Builder)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000695{
696 // Lazily construct llvm.dbg.region.end function.
697 if (!RegionEndFn)
698 RegionEndFn =llvm::Intrinsic::getDeclaration(&M->getModule(),
Eli Friedman86eb3112008-05-13 14:40:48 +0000699 llvm::Intrinsic::dbg_region_end);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000700
701 // Provide an region stop point.
702 EmitStopPoint(Fn, Builder);
703
704 // Call llvm.dbg.func.end.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000705 llvm::DebugInfoDesc *DID = RegionStack.back();
706 Builder.CreateCall(RegionEndFn, getCastValueFor(DID), "");
707 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000708}
709
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000710/// EmitDeclare - Emit local variable declaration debug info.
711void CGDebugInfo::EmitDeclare(const VarDecl *decl, unsigned Tag,
712 llvm::Value *AI,
Chris Lattner85e35682008-08-08 19:57:58 +0000713 llvm::IRBuilder<> &Builder)
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000714{
715 // FIXME: If it is a compiler generated temporary then return.
716
717 // Construct llvm.dbg.declare function.
718 if (!DeclareFn)
719 DeclareFn = llvm::Intrinsic::getDeclaration(&M->getModule(),
Mike Stump9ea58842008-06-19 20:57:50 +0000720 llvm::Intrinsic::dbg_declare);
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000721
722 // Get type information.
723 llvm::CompileUnitDesc *Unit = getOrCreateCompileUnit(CurLoc);
724 llvm::TypeDesc *TyDesc = getOrCreateType(decl->getType(), Unit);
725
726 SourceManager &SM = M->getContext().getSourceManager();
727 uint64_t Loc = SM.getLogicalLineNumber(CurLoc);
728
729 // Construct variable.
730 llvm::VariableDesc *Variable = new llvm::VariableDesc(Tag);
731 Variable->setContext(RegionStack.back());
732 Variable->setName(decl->getName());
733 Variable->setFile(Unit);
734 Variable->setLine(Loc);
735 Variable->setType(TyDesc);
736
737 // Push it onto the list so that we can free it.
738 VariableDescList.push_back(Variable);
739
740 // Cast the AllocA result to a {}* for the call to llvm.dbg.declare.
741 // These bit cast instructions will get freed when the basic block is
742 // deleted. So do not need to free them explicity.
743 const llvm::PointerType *EmpPtr = SR->getEmptyStructPtrType();
744 llvm::Value *AllocACast = new llvm::BitCastInst(AI, EmpPtr, decl->getName(),
745 Builder.GetInsertBlock());
746
747 // Call llvm.dbg.declare.
748 Builder.CreateCall2(DeclareFn, AllocACast, getCastValueFor(Variable), "");
749}
750
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000751/// EmitGlobalVariable - Emit information about a global variable.
752void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *GV,
753 const VarDecl *decl)
754{
755 // Create global variable debug descriptor.
756 llvm::GlobalVariableDesc *Global = new llvm::GlobalVariableDesc();
757
758 // Push it onto the list so that we can free it.
759 GlobalVarDescList.push_back(Global);
760
761 // Make sure we have an anchor.
762 if (!GlobalVariableAnchor)
763 GlobalVariableAnchor = new llvm::AnchorDesc(Global);
764
765 // Get name information.
766 Global->setName(decl->getName());
767 Global->setFullName(decl->getName());
768
769 llvm::CompileUnitDesc *Unit = getOrCreateCompileUnit(CurLoc);
770 SourceManager &SM = M->getContext().getSourceManager();
771 uint64_t Loc = SM.getLogicalLineNumber(CurLoc);
772
773 llvm::TypeDesc *TyD = getOrCreateType(decl->getType(), Unit);
774
775 // Fill in the Global information.
776 Global->setAnchor(GlobalVariableAnchor);
777 Global->setContext(Unit);
778 Global->setFile(Unit);
779 Global->setLine(Loc);
780 Global->setType(TyD);
781 Global->setIsDefinition(true);
782 Global->setIsStatic(GV->hasInternalLinkage());
783 Global->setGlobalVariable(GV);
784
785 // Make sure global is created if needed.
786 getValueFor(Global);
787}
788