blob: d246c56a9cd55fc42533d394993db9457c79dbb4 [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 *
Daniel Dunbar64440782008-11-08 06:12:46 +0000217CGDebugInfo::getOrCreateBuiltinType(const BuiltinType *type,
218 llvm::CompileUnitDesc *Unit) {
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000219 unsigned Encoding = 0;
Daniel Dunbar64440782008-11-08 06:12:46 +0000220 switch (type->getKind())
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000221 {
222 case BuiltinType::Void:
223 return NULL;
224 case BuiltinType::UChar:
225 case BuiltinType::Char_U:
226 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
227 break;
228 case BuiltinType::Char_S:
229 case BuiltinType::SChar:
230 Encoding = llvm::dwarf::DW_ATE_signed_char;
231 break;
232 case BuiltinType::UShort:
233 case BuiltinType::UInt:
234 case BuiltinType::ULong:
235 case BuiltinType::ULongLong:
236 Encoding = llvm::dwarf::DW_ATE_unsigned;
237 break;
238 case BuiltinType::Short:
239 case BuiltinType::Int:
240 case BuiltinType::Long:
241 case BuiltinType::LongLong:
242 Encoding = llvm::dwarf::DW_ATE_signed;
243 break;
244 case BuiltinType::Bool:
245 Encoding = llvm::dwarf::DW_ATE_boolean;
246 break;
247 case BuiltinType::Float:
248 case BuiltinType::Double:
249 Encoding = llvm::dwarf::DW_ATE_float;
250 break;
251 default:
252 Encoding = llvm::dwarf::DW_ATE_signed;
253 break;
254 }
255
256 // Ty will have contain the resulting type.
257 llvm::BasicTypeDesc *BTy = new llvm::BasicTypeDesc();
258
259 // Get the name and location early to assist debugging.
Daniel Dunbar64440782008-11-08 06:12:46 +0000260 const char *TyName = type->getName();
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000261
262 // Bit size, align and offset of the type.
263 uint64_t Size = M->getContext().getTypeSize(type);
264 uint64_t Align = M->getContext().getTypeAlign(type);
265 uint64_t Offset = 0;
266
267 // If the type is defined, fill in the details.
268 if (BTy) {
269 BTy->setContext(Unit);
270 BTy->setName(TyName);
271 BTy->setSize(Size);
272 BTy->setAlign(Align);
273 BTy->setOffset(Offset);
274 BTy->setEncoding(Encoding);
275 }
276
277 return BTy;
278}
279
280llvm::TypeDesc *
Daniel Dunbar64440782008-11-08 06:12:46 +0000281CGDebugInfo::getOrCreatePointerType(const PointerType *type,
282 llvm::CompileUnitDesc *Unit) {
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000283 // type*
284 llvm::DerivedTypeDesc *DTy =
285 new llvm::DerivedTypeDesc(llvm::dwarf::DW_TAG_pointer_type);
286
287 // Handle the derived type.
Daniel Dunbar64440782008-11-08 06:12:46 +0000288 llvm::TypeDesc *FromTy = getOrCreateType(type->getPointeeType(), Unit);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000289
290 // Get the name and location early to assist debugging.
291 SourceManager &SM = M->getContext().getSourceManager();
292 uint64_t Line = SM.getLogicalLineNumber(CurLoc);
293
294 // Bit size, align and offset of the type.
295 uint64_t Size = M->getContext().getTypeSize(type);
296 uint64_t Align = M->getContext().getTypeAlign(type);
297 uint64_t Offset = 0;
298
299 // If the type is defined, fill in the details.
300 if (DTy) {
301 DTy->setContext(Unit);
302 DTy->setLine(Line);
303 DTy->setSize(Size);
304 DTy->setAlign(Align);
305 DTy->setOffset(Offset);
306 DTy->setFromType(FromTy);
307 }
308
309 return DTy;
310}
311
312llvm::TypeDesc *
Daniel Dunbar64440782008-11-08 06:12:46 +0000313CGDebugInfo::getOrCreateTypedefType(const TypedefType *TDT,
314 llvm::CompileUnitDesc *Unit) {
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000315 // typedefs are derived from some other type.
316 llvm::DerivedTypeDesc *DTy =
317 new llvm::DerivedTypeDesc(llvm::dwarf::DW_TAG_typedef);
318
319 // Handle derived type.
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000320 llvm::TypeDesc *FromTy = getOrCreateType(TDT->LookThroughTypedefs(),
Daniel Dunbar64440782008-11-08 06:12:46 +0000321 Unit);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000322
323 // Get the name and location early to assist debugging.
324 const char *TyName = TDT->getDecl()->getName();
325 SourceManager &SM = M->getContext().getSourceManager();
326 uint64_t Line = SM.getLogicalLineNumber(TDT->getDecl()->getLocation());
327
328 // If the type is defined, fill in the details.
329 if (DTy) {
330 DTy->setContext(Unit);
331 DTy->setFile(getOrCreateCompileUnit(TDT->getDecl()->getLocation()));
332 DTy->setLine(Line);
333 DTy->setName(TyName);
334 DTy->setFromType(FromTy);
335 }
336
337 return DTy;
338}
339
340llvm::TypeDesc *
341CGDebugInfo::getOrCreateFunctionType(QualType type, llvm::CompileUnitDesc *Unit)
342{
343 llvm::CompositeTypeDesc *SubrTy =
344 new llvm::CompositeTypeDesc(llvm::dwarf::DW_TAG_subroutine_type);
345
346 // Prepare to add the arguments for the subroutine.
347 std::vector<llvm::DebugInfoDesc *> &Elements = SubrTy->getElements();
348
349 // Get result type.
350 const FunctionType *FT = type->getAsFunctionType();
351 llvm::TypeDesc *ArgTy = getOrCreateType(FT->getResultType(), Unit);
Daniel Dunbar65b69a82008-10-31 08:12:03 +0000352 Elements.push_back(ArgTy);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000353
354 // Set up remainder of arguments.
355 if (type->getTypeClass() == Type::FunctionProto) {
356 const FunctionTypeProto *FTPro = dyn_cast<FunctionTypeProto>(type);
357 for (unsigned int i =0; i < FTPro->getNumArgs(); i++) {
358 QualType ParamType = FTPro->getArgType(i);
359 ArgTy = getOrCreateType(ParamType, Unit);
Daniel Dunbar536e4ae2008-11-08 04:42:29 +0000360 // FIXME: Remove once we support all types.
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000361 if (ArgTy) Elements.push_back(ArgTy);
362 }
363 }
364
Mike Stumpa3e509e2008-06-19 20:57:50 +0000365 // FIXME: set other fields file, line here.
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000366 SubrTy->setContext(Unit);
367
368 return SubrTy;
369}
370
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000371/// getOrCreateRecordType - get structure or union type.
Daniel Dunbar64440782008-11-08 06:12:46 +0000372void CGDebugInfo::getOrCreateRecordType(const RecordType *type,
Daniel Dunbar8b4aa902008-10-31 04:04:54 +0000373 llvm::CompileUnitDesc *Unit,
374 llvm::TypeDesc *&Slot)
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000375{
Daniel Dunbar8b4aa902008-10-31 04:04:54 +0000376 // Prevent recursing in type generation by initializing the slot
377 // here.
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000378 llvm::CompositeTypeDesc *RecType;
Daniel Dunbar44252b42008-10-31 03:54:29 +0000379 if (type->isStructureType())
Daniel Dunbar8b4aa902008-10-31 04:04:54 +0000380 Slot = RecType =
381 new llvm::CompositeTypeDesc(llvm::dwarf::DW_TAG_structure_type);
Daniel Dunbar44252b42008-10-31 03:54:29 +0000382 else if (type->isUnionType())
Daniel Dunbar8b4aa902008-10-31 04:04:54 +0000383 Slot = RecType =
384 new llvm::CompositeTypeDesc(llvm::dwarf::DW_TAG_union_type);
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000385 else
Daniel Dunbar8b4aa902008-10-31 04:04:54 +0000386 return;
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000387
Daniel Dunbar64440782008-11-08 06:12:46 +0000388 RecordDecl *RecDecl = type->getDecl();
Daniel Dunbar1257f8c2008-10-17 01:07:56 +0000389 // We can not get the type for forward declarations.
390 // FIXME: What *should* we be doing here?
391 if (!RecDecl->getDefinition(M->getContext()))
Daniel Dunbar8b4aa902008-10-31 04:04:54 +0000392 return;
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000393 const ASTRecordLayout &RL = M->getContext().getASTRecordLayout(RecDecl);
394
395 SourceManager &SM = M->getContext().getSourceManager();
396 uint64_t Line = SM.getLogicalLineNumber(RecDecl->getLocation());
397
398 std::vector<llvm::DebugInfoDesc *> &Elements = RecType->getElements();
399
400 // Add the members.
401 int NumMembers = RecDecl->getNumMembers();
402 for (int i = 0; i < NumMembers; i++) {
403 FieldDecl *Member = RecDecl->getMember(i);
404 llvm::TypeDesc *MemberTy = getOrCreateType(Member->getType(), Unit);
Daniel Dunbar536e4ae2008-11-08 04:42:29 +0000405 // FIXME: Remove once we support all types.
406 if (MemberTy) {
407 MemberTy->setOffset(RL.getFieldOffset(i));
408 Elements.push_back(MemberTy);
409 }
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000410 }
411
412 // Fill in the blanks.
Daniel Dunbar44252b42008-10-31 03:54:29 +0000413 if (RecType) {
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000414 RecType->setContext(Unit);
415 RecType->setName(RecDecl->getName());
416 RecType->setFile(getOrCreateCompileUnit(RecDecl->getLocation()));
417 RecType->setLine(Line);
418 RecType->setSize(RL.getSize());
419 RecType->setAlign(RL.getAlignment());
420 RecType->setOffset(0);
421 }
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000422}
423
424/// getOrCreateEnumType - get Enum type.
425llvm::TypeDesc *
Daniel Dunbar64440782008-11-08 06:12:46 +0000426CGDebugInfo::getOrCreateEnumType(const EnumType *type,
427 llvm::CompileUnitDesc *Unit) {
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000428 llvm::CompositeTypeDesc *EnumTy
429 = new llvm::CompositeTypeDesc(llvm::dwarf::DW_TAG_enumeration_type);
430
Daniel Dunbar64440782008-11-08 06:12:46 +0000431 EnumDecl *EDecl = type->getDecl();
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000432 SourceManager &SM = M->getContext().getSourceManager();
433 uint64_t Line = SM.getLogicalLineNumber(EDecl->getLocation());
434
435 // Size, align and offset of the type.
436 uint64_t Size = M->getContext().getTypeSize(type);
437 uint64_t Align = M->getContext().getTypeAlign(type);
438
439 // Create descriptors for enum members.
440 std::vector<llvm::DebugInfoDesc *> &Elements = EnumTy->getElements();
441 EnumConstantDecl *ElementList = EDecl->getEnumConstantList();
442 while (ElementList) {
443 llvm::EnumeratorDesc *EnumDesc = new llvm::EnumeratorDesc();
444 // push it to the enum desc list so that we can free it later.
445 EnumDescList.push_back(EnumDesc);
446
447 const char *ElementName = ElementList->getName();
448 uint64_t Value = ElementList->getInitVal().getZExtValue();
449
450 EnumDesc->setName(ElementName);
451 EnumDesc->setValue(Value);
452 Elements.push_back(EnumDesc);
453 if (ElementList->getNextDeclarator())
454 ElementList
455 = dyn_cast<EnumConstantDecl>(ElementList->getNextDeclarator());
456 else
457 break;
458 }
459
460 // Fill in the blanks.
461 if (EnumTy) {
462 EnumTy->setContext(Unit);
463 EnumTy->setName(EDecl->getName());
464 EnumTy->setSize(Size);
465 EnumTy->setAlign(Align);
466 EnumTy->setOffset(0);
467 EnumTy->setFile(getOrCreateCompileUnit(EDecl->getLocation()));
468 EnumTy->setLine(Line);
469 }
470 return EnumTy;
471}
472
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000473/// getOrCreateArrayType - get or create array types.
474llvm::TypeDesc *
Daniel Dunbar64440782008-11-08 06:12:46 +0000475CGDebugInfo::getOrCreateArrayType(QualType type,
476 llvm::CompileUnitDesc *Unit) {
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000477 llvm::CompositeTypeDesc *ArrayTy
478 = new llvm::CompositeTypeDesc(llvm::dwarf::DW_TAG_array_type);
479
480 // Size, align and offset of the type.
481 uint64_t Size = M->getContext().getTypeSize(type);
482 uint64_t Align = M->getContext().getTypeAlign(type);
483
484 SourceManager &SM = M->getContext().getSourceManager();
485 uint64_t Line = SM.getLogicalLineNumber(CurLoc);
486
487 // Add the dimensions of the array.
488 std::vector<llvm::DebugInfoDesc *> &Elements = ArrayTy->getElements();
489 do {
Chris Lattnera1923f62008-08-04 07:31:14 +0000490 const ArrayType *AT = M->getContext().getAsArrayType(type);
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000491 llvm::SubrangeDesc *Subrange = new llvm::SubrangeDesc();
492
493 // push it back on the subrange desc list so that we can free it later.
494 SubrangeDescList.push_back(Subrange);
495
496 uint64_t Upper = 0;
Chris Lattnera1923f62008-08-04 07:31:14 +0000497 if (const ConstantArrayType *ConstArrTy = dyn_cast<ConstantArrayType>(AT)) {
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000498 Upper = ConstArrTy->getSize().getZExtValue() - 1;
499 }
500 Subrange->setLo(0);
501 Subrange->setHi(Upper);
502 Elements.push_back(Subrange);
Chris Lattnera1923f62008-08-04 07:31:14 +0000503 type = AT->getElementType();
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000504 } while (type->isArrayType());
505
506 ArrayTy->setFromType(getOrCreateType(type, Unit));
507
508 if (ArrayTy) {
509 ArrayTy->setContext(Unit);
510 ArrayTy->setSize(Size);
511 ArrayTy->setAlign(Align);
512 ArrayTy->setOffset(0);
513 ArrayTy->setFile(getOrCreateCompileUnit(CurLoc));
514 ArrayTy->setLine(Line);
515 }
516 return ArrayTy;
517}
518
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000519
520/// getOrCreateTaggedType - get or create structure/union/Enum type.
Daniel Dunbar64440782008-11-08 06:12:46 +0000521void CGDebugInfo::getOrCreateTagType(const TagType *type,
Daniel Dunbar8b4aa902008-10-31 04:04:54 +0000522 llvm::CompileUnitDesc *Unit,
Daniel Dunbar64440782008-11-08 06:12:46 +0000523 llvm::TypeDesc *&Slot) {
524 if (const RecordType *RT = dyn_cast<RecordType>(type))
525 getOrCreateRecordType(RT, Unit, Slot);
526 else if (const EnumType *ET = dyn_cast<EnumType>(type))
527 Slot = getOrCreateEnumType(ET, Unit);
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000528}
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000529
530/// getOrCreateType - Get the type from the cache or create a new
531/// one if necessary.
532llvm::TypeDesc *
Daniel Dunbar64440782008-11-08 06:12:46 +0000533CGDebugInfo::getOrCreateType(QualType type, llvm::CompileUnitDesc *Unit) {
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000534 if (type.isNull())
535 return NULL;
536
537 // Check to see if the compile unit already has created this type.
538 llvm::TypeDesc *&Slot = TypeCache[type.getAsOpaquePtr()];
539 if (Slot) return Slot;
540
541 // We need to check for the CVR qualifiers as the first thing.
542 if (type.getCVRQualifiers()) {
Daniel Dunbar8b4aa902008-10-31 04:04:54 +0000543 Slot = getOrCreateCVRType(type, Unit);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000544 return Slot;
545 }
546
547 // Work out details of type.
Daniel Dunbar44252b42008-10-31 03:54:29 +0000548 switch (type->getTypeClass()) {
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000549 case Type::Complex:
550 case Type::Reference:
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000551 case Type::Vector:
552 case Type::ExtVector:
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000553 case Type::ASQual:
554 case Type::ObjCInterface:
555 case Type::ObjCQualifiedInterface:
556 case Type::ObjCQualifiedId:
557 case Type::TypeOfExp:
558 case Type::TypeOfTyp:
559 default:
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000560 return NULL;
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000561
562 case Type::TypeName:
Daniel Dunbar64440782008-11-08 06:12:46 +0000563 Slot = getOrCreateTypedefType(cast<TypedefType>(type), Unit);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000564 break;
565
566 case Type::FunctionProto:
567 case Type::FunctionNoProto:
568 Slot = getOrCreateFunctionType(type, Unit);
569 break;
570
571 case Type::Builtin:
Daniel Dunbar64440782008-11-08 06:12:46 +0000572 Slot = getOrCreateBuiltinType(cast<BuiltinType>(type), Unit);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000573 break;
574
575 case Type::Pointer:
Daniel Dunbar64440782008-11-08 06:12:46 +0000576 Slot = getOrCreatePointerType(cast<PointerType>(type), Unit);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000577 break;
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000578
579 case Type::Tagged:
Daniel Dunbar64440782008-11-08 06:12:46 +0000580 getOrCreateTagType(cast<TagType>(type), Unit, Slot);
Sanjiv Gupta0f805bd2008-06-07 04:46:53 +0000581 break;
Sanjiv Guptab1e662e2008-06-09 10:47:41 +0000582
583 case Type::ConstantArray:
584 case Type::VariableArray:
585 case Type::IncompleteArray:
586 Slot = getOrCreateArrayType(type, Unit);
587 break;
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000588 }
589
590 return Slot;
591}
592
593/// EmitFunctionStart - Constructs the debug code for entering a function -
594/// "llvm.dbg.func.start.".
Daniel Dunbar54968bf2008-10-18 18:22:23 +0000595void CGDebugInfo::EmitFunctionStart(const char *Name,
596 QualType ReturnType,
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000597 llvm::Function *Fn,
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000598 CGBuilderTy &Builder)
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000599{
600 // Create subprogram descriptor.
601 Subprogram = new llvm::SubprogramDesc();
602
603 // Make sure we have an anchor.
604 if (!SubprogramAnchor) {
605 SubprogramAnchor = new llvm::AnchorDesc(Subprogram);
606 }
607
608 // Get name information.
Daniel Dunbar54968bf2008-10-18 18:22:23 +0000609 Subprogram->setName(Name);
610 Subprogram->setFullName(Name);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000611
612 // Gather location information.
613 llvm::CompileUnitDesc *Unit = getOrCreateCompileUnit(CurLoc);
614 SourceManager &SM = M->getContext().getSourceManager();
615 uint64_t Loc = SM.getLogicalLineNumber(CurLoc);
616
617 // Get Function Type.
Daniel Dunbar54968bf2008-10-18 18:22:23 +0000618 llvm::TypeDesc *SPTy = getOrCreateType(ReturnType, Unit);
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000619
620 Subprogram->setAnchor(SubprogramAnchor);
621 Subprogram->setContext(Unit);
622 Subprogram->setFile(Unit);
623 Subprogram->setLine(Loc);
624 Subprogram->setType(SPTy);
625 Subprogram->setIsStatic(Fn->hasInternalLinkage());
626 Subprogram->setIsDefinition(true);
627
628 // Lazily construct llvm.dbg.func.start.
629 if (!FuncStartFn)
630 FuncStartFn = llvm::Intrinsic::getDeclaration(&M->getModule(),
631 llvm::Intrinsic::dbg_func_start);
632
633 // Call llvm.dbg.func.start which also implicitly calls llvm.dbg.stoppoint.
634 Builder.CreateCall(FuncStartFn, getCastValueFor(Subprogram), "");
635
636 // Push function on region stack.
637 RegionStack.push_back(Subprogram);
638}
639
640
641void
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000642CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder)
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000643{
644 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
645
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000646 // Don't bother if things are the same as last time.
647 SourceManager &SM = M->getContext().getSourceManager();
648 if (CurLoc == PrevLoc
649 || (SM.getLineNumber(CurLoc) == SM.getLineNumber(PrevLoc)
650 && SM.isFromSameFile(CurLoc, PrevLoc)))
651 return;
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000652
653 // Update last state.
654 PrevLoc = CurLoc;
655
656 // Get the appropriate compile unit.
657 llvm::CompileUnitDesc *Unit = getOrCreateCompileUnit(CurLoc);
658
659 // Lazily construct llvm.dbg.stoppoint function.
660 if (!StopPointFn)
661 StopPointFn = llvm::Intrinsic::getDeclaration(&M->getModule(),
Eli Friedman6fb115c2008-05-13 14:40:48 +0000662 llvm::Intrinsic::dbg_stoppoint);
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000663
664 uint64_t CurLineNo = SM.getLogicalLineNumber(CurLoc);
665 uint64_t ColumnNo = SM.getLogicalColumnNumber(CurLoc);
666
667 // Invoke llvm.dbg.stoppoint
668 Builder.CreateCall3(StopPointFn,
Eli Friedman6fb115c2008-05-13 14:40:48 +0000669 llvm::ConstantInt::get(llvm::Type::Int32Ty, CurLineNo),
670 llvm::ConstantInt::get(llvm::Type::Int32Ty, ColumnNo),
671 getCastValueFor(Unit), "");
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000672}
673
674/// EmitRegionStart- Constructs the debug code for entering a declarative
675/// region - "llvm.dbg.region.start.".
Chris Lattnerfaf23db2008-08-08 19:57:58 +0000676void CGDebugInfo::EmitRegionStart(llvm::Function *Fn,
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000677 CGBuilderTy &Builder)
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000678{
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000679 llvm::BlockDesc *Block = new llvm::BlockDesc();
Daniel Dunbar1257f8c2008-10-17 01:07:56 +0000680 if (!RegionStack.empty())
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000681 Block->setContext(RegionStack.back());
682 RegionStack.push_back(Block);
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000683
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000684 // Lazily construct llvm.dbg.region.start function.
685 if (!RegionStartFn)
686 RegionStartFn = llvm::Intrinsic::getDeclaration(&M->getModule(),
687 llvm::Intrinsic::dbg_region_start);
688
689 // Call llvm.dbg.func.start.
690 Builder.CreateCall(RegionStartFn, getCastValueFor(Block), "");
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000691}
692
693/// EmitRegionEnd - Constructs the debug code for exiting a declarative
694/// region - "llvm.dbg.region.end."
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000695void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder)
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000696{
Daniel Dunbar1257f8c2008-10-17 01:07:56 +0000697 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
698
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000699 // Lazily construct llvm.dbg.region.end function.
700 if (!RegionEndFn)
701 RegionEndFn =llvm::Intrinsic::getDeclaration(&M->getModule(),
Eli Friedman6fb115c2008-05-13 14:40:48 +0000702 llvm::Intrinsic::dbg_region_end);
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000703
704 // Provide an region stop point.
705 EmitStopPoint(Fn, Builder);
706
707 // Call llvm.dbg.func.end.
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000708 llvm::DebugInfoDesc *DID = RegionStack.back();
709 Builder.CreateCall(RegionEndFn, getCastValueFor(DID), "");
710 RegionStack.pop_back();
Daniel Dunbar6fc1f972008-10-17 16:15:48 +0000711 // FIXME: Should be freeing here?
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000712}
713
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000714/// EmitDeclare - Emit local variable declaration debug info.
715void CGDebugInfo::EmitDeclare(const VarDecl *decl, unsigned Tag,
716 llvm::Value *AI,
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000717 CGBuilderTy &Builder)
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000718{
Daniel Dunbar1257f8c2008-10-17 01:07:56 +0000719 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
720
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000721 // FIXME: If it is a compiler generated temporary then return.
722
723 // Construct llvm.dbg.declare function.
724 if (!DeclareFn)
725 DeclareFn = llvm::Intrinsic::getDeclaration(&M->getModule(),
Mike Stumpa3e509e2008-06-19 20:57:50 +0000726 llvm::Intrinsic::dbg_declare);
Sanjiv Guptab2a10452008-05-30 10:30:31 +0000727
728 // Get type information.
729 llvm::CompileUnitDesc *Unit = getOrCreateCompileUnit(CurLoc);
730 llvm::TypeDesc *TyDesc = getOrCreateType(decl->getType(), Unit);
731
732 SourceManager &SM = M->getContext().getSourceManager();
733 uint64_t Loc = SM.getLogicalLineNumber(CurLoc);
734
735 // Construct variable.
736 llvm::VariableDesc *Variable = new llvm::VariableDesc(Tag);
737 Variable->setContext(RegionStack.back());
738 Variable->setName(decl->getName());
739 Variable->setFile(Unit);
740 Variable->setLine(Loc);
741 Variable->setType(TyDesc);
742
743 // Push it onto the list so that we can free it.
744 VariableDescList.push_back(Variable);
745
746 // Cast the AllocA result to a {}* for the call to llvm.dbg.declare.
747 // These bit cast instructions will get freed when the basic block is
748 // deleted. So do not need to free them explicity.
749 const llvm::PointerType *EmpPtr = SR->getEmptyStructPtrType();
750 llvm::Value *AllocACast = new llvm::BitCastInst(AI, EmpPtr, decl->getName(),
751 Builder.GetInsertBlock());
752
753 // Call llvm.dbg.declare.
754 Builder.CreateCall2(DeclareFn, AllocACast, getCastValueFor(Variable), "");
755}
756
Sanjiv Gupta54d97542008-06-05 08:59:10 +0000757/// EmitGlobalVariable - Emit information about a global variable.
758void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *GV,
759 const VarDecl *decl)
760{
761 // Create global variable debug descriptor.
762 llvm::GlobalVariableDesc *Global = new llvm::GlobalVariableDesc();
763
764 // Push it onto the list so that we can free it.
765 GlobalVarDescList.push_back(Global);
766
767 // Make sure we have an anchor.
768 if (!GlobalVariableAnchor)
769 GlobalVariableAnchor = new llvm::AnchorDesc(Global);
770
771 // Get name information.
772 Global->setName(decl->getName());
773 Global->setFullName(decl->getName());
774
775 llvm::CompileUnitDesc *Unit = getOrCreateCompileUnit(CurLoc);
776 SourceManager &SM = M->getContext().getSourceManager();
777 uint64_t Loc = SM.getLogicalLineNumber(CurLoc);
778
779 llvm::TypeDesc *TyD = getOrCreateType(decl->getType(), Unit);
780
781 // Fill in the Global information.
782 Global->setAnchor(GlobalVariableAnchor);
783 Global->setContext(Unit);
784 Global->setFile(Unit);
785 Global->setLine(Loc);
786 Global->setType(TyD);
787 Global->setIsDefinition(true);
788 Global->setIsStatic(GV->hasInternalLinkage());
789 Global->setGlobalVariable(GV);
790
791 // Make sure global is created if needed.
792 getValueFor(Global);
793}
794