blob: 7f9f349746cf62c5f1b119eba72000029aecd433 [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"
17#include "clang/Basic/SourceManager.h"
18#include "clang/Basic/FileManager.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000019#include "llvm/Constants.h"
20#include "llvm/DerivedTypes.h"
21#include "llvm/Instructions.h"
22#include "llvm/Intrinsics.h"
23#include "llvm/Module.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000024#include "llvm/ADT/StringExtras.h"
25#include "llvm/ADT/SmallVector.h"
26#include "llvm/CodeGen/MachineModuleInfo.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000027#include "llvm/Support/Dwarf.h"
28#include "llvm/Support/IRBuilder.h"
29#include "llvm/Target/TargetMachine.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000030using namespace clang;
31using namespace clang::CodeGen;
32
33CGDebugInfo::CGDebugInfo(CodeGenModule *m)
34: M(m)
35, CurLoc()
36, PrevLoc()
37, CompileUnitCache()
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000038, TypeCache()
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000039, StopPointFn(NULL)
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000040, FuncStartFn(NULL)
41, DeclareFn(NULL)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000042, RegionStartFn(NULL)
43, RegionEndFn(NULL)
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000044, CompileUnitAnchor(NULL)
45, SubprogramAnchor(NULL)
Sanjiv Gupta686226b2008-06-05 08:59:10 +000046, GlobalVariableAnchor(NULL)
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000047, RegionStack()
Sanjiv Guptacc9b1632008-05-30 10:30:31 +000048, VariableDescList()
Sanjiv Gupta686226b2008-06-05 08:59:10 +000049, GlobalVarDescList(NULL)
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +000050, EnumDescList(NULL)
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000051, Subprogram(NULL)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000052{
53 SR = new llvm::DISerializer();
54 SR->setModule (&M->getModule());
55}
56
57CGDebugInfo::~CGDebugInfo()
58{
59 delete SR;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000060
61 // Free CompileUnitCache.
62 for (std::map<unsigned, llvm::CompileUnitDesc *>::iterator I
63 = CompileUnitCache.begin(); I != CompileUnitCache.end(); ++I) {
64 delete I->second;
65 }
66 CompileUnitCache.clear();
67
68 // Free TypeCache.
69 for (std::map<void *, llvm::TypeDesc *>::iterator I
70 = TypeCache.begin(); I != TypeCache.end(); ++I) {
71 delete I->second;
72 }
73 TypeCache.clear();
74
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +000075 // Free region descriptors.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000076 for (std::vector<llvm::DebugInfoDesc *>::iterator I
77 = RegionStack.begin(); I != RegionStack.end(); ++I) {
78 delete *I;
79 }
Sanjiv Guptacc9b1632008-05-30 10:30:31 +000080
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +000081 // Free local var descriptors.
Sanjiv Guptacc9b1632008-05-30 10:30:31 +000082 for (std::vector<llvm::VariableDesc *>::iterator I
83 = VariableDescList.begin(); I != VariableDescList.end(); ++I) {
84 delete *I;
85 }
86
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +000087 // Free global var descriptors.
Sanjiv Gupta686226b2008-06-05 08:59:10 +000088 for (std::vector<llvm::GlobalVariableDesc *>::iterator I
89 = GlobalVarDescList.begin(); I != GlobalVarDescList.end(); ++I) {
90 delete *I;
91 }
92
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +000093 // Free enum constants descriptors.
94 for (std::vector<llvm::EnumeratorDesc *>::iterator I
95 = EnumDescList.begin(); I != EnumDescList.end(); ++I) {
96 delete *I;
97 }
98
Eli Friedman3f2af102008-05-22 01:40:10 +000099 delete CompileUnitAnchor;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000100 delete SubprogramAnchor;
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000101 delete GlobalVariableAnchor;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000102}
103
Eli Friedman32ea35f2008-05-29 11:08:17 +0000104void CGDebugInfo::setLocation(SourceLocation loc)
105{
106 SourceManager &SM = M->getContext().getSourceManager();
107 CurLoc = SM.getLogicalLoc(loc);
108}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000109
110/// getCastValueFor - Return a llvm representation for a given debug information
111/// descriptor cast to an empty struct pointer.
112llvm::Value *CGDebugInfo::getCastValueFor(llvm::DebugInfoDesc *DD) {
113 return llvm::ConstantExpr::getBitCast(SR->Serialize(DD),
Eli Friedman86eb3112008-05-13 14:40:48 +0000114 SR->getEmptyStructPtrType());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000115}
116
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000117/// getValueFor - Return a llvm representation for a given debug information
118/// descriptor.
119llvm::Value *CGDebugInfo::getValueFor(llvm::DebugInfoDesc *DD) {
120 return SR->Serialize(DD);
121}
122
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000123/// getOrCreateCompileUnit - Get the compile unit from the cache or create a new
124/// one if necessary.
125llvm::CompileUnitDesc
126*CGDebugInfo::getOrCreateCompileUnit(const SourceLocation Loc) {
127
128 // See if this compile unit has been used before.
129 llvm::CompileUnitDesc *&Slot = CompileUnitCache[Loc.getFileID()];
130 if (Slot) return Slot;
131
132 // Create new compile unit.
133 // FIXME: Where to free these?
134 // One way is to iterate over the CompileUnitCache in ~CGDebugInfo.
135 llvm::CompileUnitDesc *Unit = new llvm::CompileUnitDesc();
136
137 // Make sure we have an anchor.
138 if (!CompileUnitAnchor) {
139 CompileUnitAnchor = new llvm::AnchorDesc(Unit);
140 }
141
142 // Get source file information.
143 SourceManager &SM = M->getContext().getSourceManager();
144 const FileEntry *FE = SM.getFileEntryForLoc(Loc);
Eli Friedman3f2af102008-05-22 01:40:10 +0000145 const char *FileName, *DirName;
146 if (FE) {
147 FileName = FE->getName();
148 DirName = FE->getDir()->getName();
149 } else {
150 FileName = SM.getSourceName(Loc);
151 DirName = "";
152 }
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000153
154 Unit->setAnchor(CompileUnitAnchor);
155 Unit->setFileName(FileName);
156 Unit->setDirectory(DirName);
157
158 // Set up producer name.
159 // FIXME: Do not know how to get clang version yet.
160 Unit->setProducer("clang");
161
162 // Set up Language number.
163 // FIXME: Handle other languages as well.
164 Unit->setLanguage(llvm::dwarf::DW_LANG_C89);
165
166 // Update cache.
167 Slot = Unit;
168
169 return Unit;
170}
171
172
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000173/// getOrCreateCVRType - Get the CVR qualified type from the cache or create
174/// a new one if necessary.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000175llvm::TypeDesc *
176CGDebugInfo::getOrCreateCVRType(QualType type, llvm::CompileUnitDesc *Unit)
177{
178 // We will create a Derived type.
179 llvm::DerivedTypeDesc *DTy = NULL;
180 llvm::TypeDesc *FromTy = NULL;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000181
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000182 if (type.isConstQualified()) {
183 DTy = new llvm::DerivedTypeDesc(llvm::dwarf::DW_TAG_const_type);
184 type.removeConst();
185 FromTy = getOrCreateType(type, Unit);
186 } else if (type.isVolatileQualified()) {
187 DTy = new llvm::DerivedTypeDesc(llvm::dwarf::DW_TAG_volatile_type);
188 type.removeVolatile();
189 FromTy = getOrCreateType(type, Unit);
190 } else if (type.isRestrictQualified()) {
191 DTy = new llvm::DerivedTypeDesc(llvm::dwarf::DW_TAG_restrict_type);
192 type.removeRestrict();
193 FromTy = getOrCreateType(type, Unit);
194 }
195
196 // No need to fill in the Name, Line, Size, Alignment, Offset in case of // CVR derived types.
197 DTy->setContext(Unit);
198 DTy->setFromType(FromTy);
199
200 return DTy;
201}
202
203
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000204/// getOrCreateBuiltinType - Get the Basic type from the cache or create a new
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000205/// one if necessary.
206llvm::TypeDesc *
207CGDebugInfo::getOrCreateBuiltinType(QualType type, llvm::CompileUnitDesc *Unit)
208{
209 assert (type->getTypeClass() == Type::Builtin);
210
211 const BuiltinType *BT = type->getAsBuiltinType();
212
213 unsigned Encoding = 0;
214 switch (BT->getKind())
215 {
216 case BuiltinType::Void:
217 return NULL;
218 case BuiltinType::UChar:
219 case BuiltinType::Char_U:
220 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
221 break;
222 case BuiltinType::Char_S:
223 case BuiltinType::SChar:
224 Encoding = llvm::dwarf::DW_ATE_signed_char;
225 break;
226 case BuiltinType::UShort:
227 case BuiltinType::UInt:
228 case BuiltinType::ULong:
229 case BuiltinType::ULongLong:
230 Encoding = llvm::dwarf::DW_ATE_unsigned;
231 break;
232 case BuiltinType::Short:
233 case BuiltinType::Int:
234 case BuiltinType::Long:
235 case BuiltinType::LongLong:
236 Encoding = llvm::dwarf::DW_ATE_signed;
237 break;
238 case BuiltinType::Bool:
239 Encoding = llvm::dwarf::DW_ATE_boolean;
240 break;
241 case BuiltinType::Float:
242 case BuiltinType::Double:
243 Encoding = llvm::dwarf::DW_ATE_float;
244 break;
245 default:
246 Encoding = llvm::dwarf::DW_ATE_signed;
247 break;
248 }
249
250 // Ty will have contain the resulting type.
251 llvm::BasicTypeDesc *BTy = new llvm::BasicTypeDesc();
252
253 // Get the name and location early to assist debugging.
254 const char *TyName = BT->getName();
255
256 // Bit size, align and offset of the type.
257 uint64_t Size = M->getContext().getTypeSize(type);
258 uint64_t Align = M->getContext().getTypeAlign(type);
259 uint64_t Offset = 0;
260
261 // If the type is defined, fill in the details.
262 if (BTy) {
263 BTy->setContext(Unit);
264 BTy->setName(TyName);
265 BTy->setSize(Size);
266 BTy->setAlign(Align);
267 BTy->setOffset(Offset);
268 BTy->setEncoding(Encoding);
269 }
270
271 return BTy;
272}
273
274llvm::TypeDesc *
275CGDebugInfo::getOrCreatePointerType(QualType type, llvm::CompileUnitDesc *Unit)
276{
277 // type*
278 llvm::DerivedTypeDesc *DTy =
279 new llvm::DerivedTypeDesc(llvm::dwarf::DW_TAG_pointer_type);
280
281 // Handle the derived type.
282 const PointerType *PTRT = type->getAsPointerType();
283 llvm::TypeDesc *FromTy = getOrCreateType(PTRT->getPointeeType(), Unit);
284
285 // Get the name and location early to assist debugging.
286 SourceManager &SM = M->getContext().getSourceManager();
287 uint64_t Line = SM.getLogicalLineNumber(CurLoc);
288
289 // Bit size, align and offset of the type.
290 uint64_t Size = M->getContext().getTypeSize(type);
291 uint64_t Align = M->getContext().getTypeAlign(type);
292 uint64_t Offset = 0;
293
294 // If the type is defined, fill in the details.
295 if (DTy) {
296 DTy->setContext(Unit);
297 DTy->setLine(Line);
298 DTy->setSize(Size);
299 DTy->setAlign(Align);
300 DTy->setOffset(Offset);
301 DTy->setFromType(FromTy);
302 }
303
304 return DTy;
305}
306
307llvm::TypeDesc *
308CGDebugInfo::getOrCreateTypedefType(QualType type, llvm::CompileUnitDesc *Unit)
309{
310 // typedefs are derived from some other type.
311 llvm::DerivedTypeDesc *DTy =
312 new llvm::DerivedTypeDesc(llvm::dwarf::DW_TAG_typedef);
313
314 // Handle derived type.
315 const TypedefType *TDT = type->getAsTypedefType();
316 llvm::TypeDesc *FromTy = getOrCreateType(TDT->LookThroughTypedefs(),
317 Unit);
318
319 // Get the name and location early to assist debugging.
320 const char *TyName = TDT->getDecl()->getName();
321 SourceManager &SM = M->getContext().getSourceManager();
322 uint64_t Line = SM.getLogicalLineNumber(TDT->getDecl()->getLocation());
323
324 // If the type is defined, fill in the details.
325 if (DTy) {
326 DTy->setContext(Unit);
327 DTy->setFile(getOrCreateCompileUnit(TDT->getDecl()->getLocation()));
328 DTy->setLine(Line);
329 DTy->setName(TyName);
330 DTy->setFromType(FromTy);
331 }
332
333 return DTy;
334}
335
336llvm::TypeDesc *
337CGDebugInfo::getOrCreateFunctionType(QualType type, llvm::CompileUnitDesc *Unit)
338{
339 llvm::CompositeTypeDesc *SubrTy =
340 new llvm::CompositeTypeDesc(llvm::dwarf::DW_TAG_subroutine_type);
341
342 // Prepare to add the arguments for the subroutine.
343 std::vector<llvm::DebugInfoDesc *> &Elements = SubrTy->getElements();
344
345 // Get result type.
346 const FunctionType *FT = type->getAsFunctionType();
347 llvm::TypeDesc *ArgTy = getOrCreateType(FT->getResultType(), Unit);
348 if (ArgTy) Elements.push_back(ArgTy);
349
350 // Set up remainder of arguments.
351 if (type->getTypeClass() == Type::FunctionProto) {
352 const FunctionTypeProto *FTPro = dyn_cast<FunctionTypeProto>(type);
353 for (unsigned int i =0; i < FTPro->getNumArgs(); i++) {
354 QualType ParamType = FTPro->getArgType(i);
355 ArgTy = getOrCreateType(ParamType, Unit);
356 if (ArgTy) Elements.push_back(ArgTy);
357 }
358 }
359
360 // FIXME: set other fields file, line here.
361 SubrTy->setContext(Unit);
362
363 return SubrTy;
364}
365
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000366/// getOrCreateRecordType - get structure or union type.
367llvm::TypeDesc *
368CGDebugInfo::getOrCreateRecordType(QualType type, llvm::CompileUnitDesc *Unit)
369{
370 llvm::CompositeTypeDesc *RecType;
371 if(type->isStructureType())
372 RecType = new llvm::CompositeTypeDesc(llvm::dwarf::DW_TAG_structure_type);
373 else if(type->isUnionType())
374 RecType = new llvm::CompositeTypeDesc(llvm::dwarf::DW_TAG_union_type);
375 else
376 return NULL;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000377
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000378 RecordDecl *RecDecl = type->getAsRecordType()->getDecl();
379 const ASTRecordLayout &RL = M->getContext().getASTRecordLayout(RecDecl);
380
381 SourceManager &SM = M->getContext().getSourceManager();
382 uint64_t Line = SM.getLogicalLineNumber(RecDecl->getLocation());
383
384 std::vector<llvm::DebugInfoDesc *> &Elements = RecType->getElements();
385
386 // Add the members.
387 int NumMembers = RecDecl->getNumMembers();
388 for (int i = 0; i < NumMembers; i++) {
389 FieldDecl *Member = RecDecl->getMember(i);
390 llvm::TypeDesc *MemberTy = getOrCreateType(Member->getType(), Unit);
391 MemberTy->setOffset(RL.getFieldOffset(i));
392 Elements.push_back(MemberTy);
393 }
394
395 // Fill in the blanks.
396 if(RecType) {
397 RecType->setContext(Unit);
398 RecType->setName(RecDecl->getName());
399 RecType->setFile(getOrCreateCompileUnit(RecDecl->getLocation()));
400 RecType->setLine(Line);
401 RecType->setSize(RL.getSize());
402 RecType->setAlign(RL.getAlignment());
403 RecType->setOffset(0);
404 }
405 return(RecType);
406}
407
408/// getOrCreateEnumType - get Enum type.
409llvm::TypeDesc *
410CGDebugInfo::getOrCreateEnumType(QualType type, llvm::CompileUnitDesc *Unit)
411{
412 llvm::CompositeTypeDesc *EnumTy
413 = new llvm::CompositeTypeDesc(llvm::dwarf::DW_TAG_enumeration_type);
414
415 EnumType *EType = dyn_cast<EnumType>(type);
416 if (!EType) return(NULL);
417
418 EnumDecl *EDecl = EType->getDecl();
419 SourceManager &SM = M->getContext().getSourceManager();
420 uint64_t Line = SM.getLogicalLineNumber(EDecl->getLocation());
421
422 // Size, align and offset of the type.
423 uint64_t Size = M->getContext().getTypeSize(type);
424 uint64_t Align = M->getContext().getTypeAlign(type);
425
426 // Create descriptors for enum members.
427 std::vector<llvm::DebugInfoDesc *> &Elements = EnumTy->getElements();
428 EnumConstantDecl *ElementList = EDecl->getEnumConstantList();
429 while (ElementList) {
430 llvm::EnumeratorDesc *EnumDesc = new llvm::EnumeratorDesc();
431 // push it to the enum desc list so that we can free it later.
432 EnumDescList.push_back(EnumDesc);
433
434 const char *ElementName = ElementList->getName();
435 uint64_t Value = ElementList->getInitVal().getZExtValue();
436
437 EnumDesc->setName(ElementName);
438 EnumDesc->setValue(Value);
439 Elements.push_back(EnumDesc);
440 if (ElementList->getNextDeclarator())
441 ElementList
442 = dyn_cast<EnumConstantDecl>(ElementList->getNextDeclarator());
443 else
444 break;
445 }
446
447 // Fill in the blanks.
448 if (EnumTy) {
449 EnumTy->setContext(Unit);
450 EnumTy->setName(EDecl->getName());
451 EnumTy->setSize(Size);
452 EnumTy->setAlign(Align);
453 EnumTy->setOffset(0);
454 EnumTy->setFile(getOrCreateCompileUnit(EDecl->getLocation()));
455 EnumTy->setLine(Line);
456 }
457 return EnumTy;
458}
459
460
461/// getOrCreateTaggedType - get or create structure/union/Enum type.
462llvm::TypeDesc *
463CGDebugInfo::getOrCreateTaggedType(QualType type, llvm::CompileUnitDesc *Unit)
464{
465 if (type->isStructureType() || type->isUnionType())
466 return getOrCreateRecordType(type, Unit);
467 else if (type->isEnumeralType())
468 return getOrCreateEnumType(type, Unit);
469 else
470 return NULL;
471}
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000472
473/// getOrCreateType - Get the type from the cache or create a new
474/// one if necessary.
475llvm::TypeDesc *
476CGDebugInfo::getOrCreateType(QualType type, llvm::CompileUnitDesc *Unit)
477{
478 if (type.isNull())
479 return NULL;
480
481 // Check to see if the compile unit already has created this type.
482 llvm::TypeDesc *&Slot = TypeCache[type.getAsOpaquePtr()];
483 if (Slot) return Slot;
484
485 // We need to check for the CVR qualifiers as the first thing.
486 if (type.getCVRQualifiers()) {
487 Slot = getOrCreateCVRType (type, Unit);
488 return Slot;
489 }
490
491 // Work out details of type.
492 switch(type->getTypeClass()) {
493 case Type::Complex:
494 case Type::Reference:
495 case Type::ConstantArray:
496 case Type::VariableArray:
497 case Type::IncompleteArray:
498 case Type::Vector:
499 case Type::ExtVector:
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000500 case Type::ASQual:
501 case Type::ObjCInterface:
502 case Type::ObjCQualifiedInterface:
503 case Type::ObjCQualifiedId:
504 case Type::TypeOfExp:
505 case Type::TypeOfTyp:
506 default:
507 {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000508 return NULL;
509 }
510
511 case Type::TypeName:
512 Slot = getOrCreateTypedefType(type, Unit);
513 break;
514
515 case Type::FunctionProto:
516 case Type::FunctionNoProto:
517 Slot = getOrCreateFunctionType(type, Unit);
518 break;
519
520 case Type::Builtin:
521 Slot = getOrCreateBuiltinType(type, Unit);
522 break;
523
524 case Type::Pointer:
525 Slot = getOrCreatePointerType(type, Unit);
526 break;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000527
528 case Type::Tagged:
529 Slot = getOrCreateTaggedType(type, Unit);
530 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000531 }
532
533 return Slot;
534}
535
536/// EmitFunctionStart - Constructs the debug code for entering a function -
537/// "llvm.dbg.func.start.".
538void CGDebugInfo::EmitFunctionStart(const FunctionDecl *FnDecl,
539 llvm::Function *Fn,
540 llvm::IRBuilder &Builder)
541{
542 // Create subprogram descriptor.
543 Subprogram = new llvm::SubprogramDesc();
544
545 // Make sure we have an anchor.
546 if (!SubprogramAnchor) {
547 SubprogramAnchor = new llvm::AnchorDesc(Subprogram);
548 }
549
550 // Get name information.
551 Subprogram->setName(FnDecl->getName());
552 Subprogram->setFullName(FnDecl->getName());
553
554 // Gather location information.
555 llvm::CompileUnitDesc *Unit = getOrCreateCompileUnit(CurLoc);
556 SourceManager &SM = M->getContext().getSourceManager();
557 uint64_t Loc = SM.getLogicalLineNumber(CurLoc);
558
559 // Get Function Type.
560 QualType type = FnDecl->getResultType();
561 llvm::TypeDesc *SPTy = getOrCreateType(type, Unit);
562
563 Subprogram->setAnchor(SubprogramAnchor);
564 Subprogram->setContext(Unit);
565 Subprogram->setFile(Unit);
566 Subprogram->setLine(Loc);
567 Subprogram->setType(SPTy);
568 Subprogram->setIsStatic(Fn->hasInternalLinkage());
569 Subprogram->setIsDefinition(true);
570
571 // Lazily construct llvm.dbg.func.start.
572 if (!FuncStartFn)
573 FuncStartFn = llvm::Intrinsic::getDeclaration(&M->getModule(),
574 llvm::Intrinsic::dbg_func_start);
575
576 // Call llvm.dbg.func.start which also implicitly calls llvm.dbg.stoppoint.
577 Builder.CreateCall(FuncStartFn, getCastValueFor(Subprogram), "");
578
579 // Push function on region stack.
580 RegionStack.push_back(Subprogram);
581}
582
583
584void
585CGDebugInfo::EmitStopPoint(llvm::Function *Fn, llvm::IRBuilder &Builder)
586{
587 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
588
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000589 // Don't bother if things are the same as last time.
590 SourceManager &SM = M->getContext().getSourceManager();
591 if (CurLoc == PrevLoc
592 || (SM.getLineNumber(CurLoc) == SM.getLineNumber(PrevLoc)
593 && SM.isFromSameFile(CurLoc, PrevLoc)))
594 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000595
596 // Update last state.
597 PrevLoc = CurLoc;
598
599 // Get the appropriate compile unit.
600 llvm::CompileUnitDesc *Unit = getOrCreateCompileUnit(CurLoc);
601
602 // Lazily construct llvm.dbg.stoppoint function.
603 if (!StopPointFn)
604 StopPointFn = llvm::Intrinsic::getDeclaration(&M->getModule(),
Eli Friedman86eb3112008-05-13 14:40:48 +0000605 llvm::Intrinsic::dbg_stoppoint);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000606
607 uint64_t CurLineNo = SM.getLogicalLineNumber(CurLoc);
608 uint64_t ColumnNo = SM.getLogicalColumnNumber(CurLoc);
609
610 // Invoke llvm.dbg.stoppoint
611 Builder.CreateCall3(StopPointFn,
Eli Friedman86eb3112008-05-13 14:40:48 +0000612 llvm::ConstantInt::get(llvm::Type::Int32Ty, CurLineNo),
613 llvm::ConstantInt::get(llvm::Type::Int32Ty, ColumnNo),
614 getCastValueFor(Unit), "");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000615}
616
617/// EmitRegionStart- Constructs the debug code for entering a declarative
618/// region - "llvm.dbg.region.start.".
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000619void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, llvm::IRBuilder &Builder)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000620{
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000621 llvm::BlockDesc *Block = new llvm::BlockDesc();
622 if (RegionStack.size() > 0)
623 Block->setContext(RegionStack.back());
624 RegionStack.push_back(Block);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000625
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000626 // Lazily construct llvm.dbg.region.start function.
627 if (!RegionStartFn)
628 RegionStartFn = llvm::Intrinsic::getDeclaration(&M->getModule(),
629 llvm::Intrinsic::dbg_region_start);
630
631 // Call llvm.dbg.func.start.
632 Builder.CreateCall(RegionStartFn, getCastValueFor(Block), "");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000633}
634
635/// EmitRegionEnd - Constructs the debug code for exiting a declarative
636/// region - "llvm.dbg.region.end."
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000637void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, llvm::IRBuilder &Builder)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000638{
639 // Lazily construct llvm.dbg.region.end function.
640 if (!RegionEndFn)
641 RegionEndFn =llvm::Intrinsic::getDeclaration(&M->getModule(),
Eli Friedman86eb3112008-05-13 14:40:48 +0000642 llvm::Intrinsic::dbg_region_end);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000643
644 // Provide an region stop point.
645 EmitStopPoint(Fn, Builder);
646
647 // Call llvm.dbg.func.end.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000648 llvm::DebugInfoDesc *DID = RegionStack.back();
649 Builder.CreateCall(RegionEndFn, getCastValueFor(DID), "");
650 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000651}
652
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000653/// EmitDeclare - Emit local variable declaration debug info.
654void CGDebugInfo::EmitDeclare(const VarDecl *decl, unsigned Tag,
655 llvm::Value *AI,
656 llvm::IRBuilder &Builder)
657{
658 // FIXME: If it is a compiler generated temporary then return.
659
660 // Construct llvm.dbg.declare function.
661 if (!DeclareFn)
662 DeclareFn = llvm::Intrinsic::getDeclaration(&M->getModule(),
663 llvm::Intrinsic::dbg_declare);
664
665 // Get type information.
666 llvm::CompileUnitDesc *Unit = getOrCreateCompileUnit(CurLoc);
667 llvm::TypeDesc *TyDesc = getOrCreateType(decl->getType(), Unit);
668
669 SourceManager &SM = M->getContext().getSourceManager();
670 uint64_t Loc = SM.getLogicalLineNumber(CurLoc);
671
672 // Construct variable.
673 llvm::VariableDesc *Variable = new llvm::VariableDesc(Tag);
674 Variable->setContext(RegionStack.back());
675 Variable->setName(decl->getName());
676 Variable->setFile(Unit);
677 Variable->setLine(Loc);
678 Variable->setType(TyDesc);
679
680 // Push it onto the list so that we can free it.
681 VariableDescList.push_back(Variable);
682
683 // Cast the AllocA result to a {}* for the call to llvm.dbg.declare.
684 // These bit cast instructions will get freed when the basic block is
685 // deleted. So do not need to free them explicity.
686 const llvm::PointerType *EmpPtr = SR->getEmptyStructPtrType();
687 llvm::Value *AllocACast = new llvm::BitCastInst(AI, EmpPtr, decl->getName(),
688 Builder.GetInsertBlock());
689
690 // Call llvm.dbg.declare.
691 Builder.CreateCall2(DeclareFn, AllocACast, getCastValueFor(Variable), "");
692}
693
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000694/// EmitGlobalVariable - Emit information about a global variable.
695void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *GV,
696 const VarDecl *decl)
697{
698 // Create global variable debug descriptor.
699 llvm::GlobalVariableDesc *Global = new llvm::GlobalVariableDesc();
700
701 // Push it onto the list so that we can free it.
702 GlobalVarDescList.push_back(Global);
703
704 // Make sure we have an anchor.
705 if (!GlobalVariableAnchor)
706 GlobalVariableAnchor = new llvm::AnchorDesc(Global);
707
708 // Get name information.
709 Global->setName(decl->getName());
710 Global->setFullName(decl->getName());
711
712 llvm::CompileUnitDesc *Unit = getOrCreateCompileUnit(CurLoc);
713 SourceManager &SM = M->getContext().getSourceManager();
714 uint64_t Loc = SM.getLogicalLineNumber(CurLoc);
715
716 llvm::TypeDesc *TyD = getOrCreateType(decl->getType(), Unit);
717
718 // Fill in the Global information.
719 Global->setAnchor(GlobalVariableAnchor);
720 Global->setContext(Unit);
721 Global->setFile(Unit);
722 Global->setLine(Loc);
723 Global->setType(TyD);
724 Global->setIsDefinition(true);
725 Global->setIsStatic(GV->hasInternalLinkage());
726 Global->setGlobalVariable(GV);
727
728 // Make sure global is created if needed.
729 getValueFor(Global);
730}
731