blob: 1827fc980c3dff651b7542bef4a7db4e2faeb7d5 [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"
17#include "clang/Basic/SourceManager.h"
18#include "clang/Basic/FileManager.h"
Sanjiv Gupta40e56a12008-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 Gupta40e56a12008-05-08 08:54:20 +000024#include "llvm/ADT/StringExtras.h"
25#include "llvm/ADT/SmallVector.h"
26#include "llvm/CodeGen/MachineModuleInfo.h"
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000027#include "llvm/Support/Dwarf.h"
28#include "llvm/Support/IRBuilder.h"
29#include "llvm/Target/TargetMachine.h"
Sanjiv Gupta40e56a12008-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 Gupta93eb8252008-05-25 05:15:42 +000038, TypeCache()
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000039, StopPointFn(NULL)
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000040, FuncStartFn(NULL)
41, DeclareFn(NULL)
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000042, RegionStartFn(NULL)
43, RegionEndFn(NULL)
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000044, CompileUnitAnchor(NULL)
45, SubprogramAnchor(NULL)
46, RegionStack()
47, Subprogram(NULL)
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000048{
49 SR = new llvm::DISerializer();
50 SR->setModule (&M->getModule());
51}
52
53CGDebugInfo::~CGDebugInfo()
54{
55 delete SR;
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000056
57 // Free CompileUnitCache.
58 for (std::map<unsigned, llvm::CompileUnitDesc *>::iterator I
59 = CompileUnitCache.begin(); I != CompileUnitCache.end(); ++I) {
60 delete I->second;
61 }
62 CompileUnitCache.clear();
63
64 // Free TypeCache.
65 for (std::map<void *, llvm::TypeDesc *>::iterator I
66 = TypeCache.begin(); I != TypeCache.end(); ++I) {
67 delete I->second;
68 }
69 TypeCache.clear();
70
71 for (std::vector<llvm::DebugInfoDesc *>::iterator I
72 = RegionStack.begin(); I != RegionStack.end(); ++I) {
73 delete *I;
74 }
75
Eli Friedman9bc7c8d2008-05-22 01:40:10 +000076 delete CompileUnitAnchor;
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000077 delete SubprogramAnchor;
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000078}
79
Eli Friedman6a03c262008-05-29 11:08:17 +000080void CGDebugInfo::setLocation(SourceLocation loc)
81{
82 SourceManager &SM = M->getContext().getSourceManager();
83 CurLoc = SM.getLogicalLoc(loc);
84}
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000085
86/// getCastValueFor - Return a llvm representation for a given debug information
87/// descriptor cast to an empty struct pointer.
88llvm::Value *CGDebugInfo::getCastValueFor(llvm::DebugInfoDesc *DD) {
89 return llvm::ConstantExpr::getBitCast(SR->Serialize(DD),
Eli Friedman6fb115c2008-05-13 14:40:48 +000090 SR->getEmptyStructPtrType());
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000091}
92
Sanjiv Gupta93eb8252008-05-25 05:15:42 +000093/// getValueFor - Return a llvm representation for a given debug information
94/// descriptor.
95llvm::Value *CGDebugInfo::getValueFor(llvm::DebugInfoDesc *DD) {
96 return SR->Serialize(DD);
97}
98
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000099/// getOrCreateCompileUnit - Get the compile unit from the cache or create a new
100/// one if necessary.
101llvm::CompileUnitDesc
102*CGDebugInfo::getOrCreateCompileUnit(const SourceLocation Loc) {
103
104 // See if this compile unit has been used before.
105 llvm::CompileUnitDesc *&Slot = CompileUnitCache[Loc.getFileID()];
106 if (Slot) return Slot;
107
108 // Create new compile unit.
109 // FIXME: Where to free these?
110 // One way is to iterate over the CompileUnitCache in ~CGDebugInfo.
111 llvm::CompileUnitDesc *Unit = new llvm::CompileUnitDesc();
112
113 // Make sure we have an anchor.
114 if (!CompileUnitAnchor) {
115 CompileUnitAnchor = new llvm::AnchorDesc(Unit);
116 }
117
118 // Get source file information.
119 SourceManager &SM = M->getContext().getSourceManager();
120 const FileEntry *FE = SM.getFileEntryForLoc(Loc);
Eli Friedman9bc7c8d2008-05-22 01:40:10 +0000121 const char *FileName, *DirName;
122 if (FE) {
123 FileName = FE->getName();
124 DirName = FE->getDir()->getName();
125 } else {
126 FileName = SM.getSourceName(Loc);
127 DirName = "";
128 }
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000129
130 Unit->setAnchor(CompileUnitAnchor);
131 Unit->setFileName(FileName);
132 Unit->setDirectory(DirName);
133
134 // Set up producer name.
135 // FIXME: Do not know how to get clang version yet.
136 Unit->setProducer("clang");
137
138 // Set up Language number.
139 // FIXME: Handle other languages as well.
140 Unit->setLanguage(llvm::dwarf::DW_LANG_C89);
141
142 // Update cache.
143 Slot = Unit;
144
145 return Unit;
146}
147
148
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000149llvm::TypeDesc *
150CGDebugInfo::getOrCreateCVRType(QualType type, llvm::CompileUnitDesc *Unit)
151{
152 // We will create a Derived type.
153 llvm::DerivedTypeDesc *DTy = NULL;
154 llvm::TypeDesc *FromTy = NULL;
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000155
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000156 if (type.isConstQualified()) {
157 DTy = new llvm::DerivedTypeDesc(llvm::dwarf::DW_TAG_const_type);
158 type.removeConst();
159 FromTy = getOrCreateType(type, Unit);
160 } else if (type.isVolatileQualified()) {
161 DTy = new llvm::DerivedTypeDesc(llvm::dwarf::DW_TAG_volatile_type);
162 type.removeVolatile();
163 FromTy = getOrCreateType(type, Unit);
164 } else if (type.isRestrictQualified()) {
165 DTy = new llvm::DerivedTypeDesc(llvm::dwarf::DW_TAG_restrict_type);
166 type.removeRestrict();
167 FromTy = getOrCreateType(type, Unit);
168 }
169
170 // No need to fill in the Name, Line, Size, Alignment, Offset in case of // CVR derived types.
171 DTy->setContext(Unit);
172 DTy->setFromType(FromTy);
173
174 return DTy;
175}
176
177
178/// getOrCreateType - Get the Basic type from the cache or create a new
179/// one if necessary.
180llvm::TypeDesc *
181CGDebugInfo::getOrCreateBuiltinType(QualType type, llvm::CompileUnitDesc *Unit)
182{
183 assert (type->getTypeClass() == Type::Builtin);
184
185 const BuiltinType *BT = type->getAsBuiltinType();
186
187 unsigned Encoding = 0;
188 switch (BT->getKind())
189 {
190 case BuiltinType::Void:
191 return NULL;
192 case BuiltinType::UChar:
193 case BuiltinType::Char_U:
194 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
195 break;
196 case BuiltinType::Char_S:
197 case BuiltinType::SChar:
198 Encoding = llvm::dwarf::DW_ATE_signed_char;
199 break;
200 case BuiltinType::UShort:
201 case BuiltinType::UInt:
202 case BuiltinType::ULong:
203 case BuiltinType::ULongLong:
204 Encoding = llvm::dwarf::DW_ATE_unsigned;
205 break;
206 case BuiltinType::Short:
207 case BuiltinType::Int:
208 case BuiltinType::Long:
209 case BuiltinType::LongLong:
210 Encoding = llvm::dwarf::DW_ATE_signed;
211 break;
212 case BuiltinType::Bool:
213 Encoding = llvm::dwarf::DW_ATE_boolean;
214 break;
215 case BuiltinType::Float:
216 case BuiltinType::Double:
217 Encoding = llvm::dwarf::DW_ATE_float;
218 break;
219 default:
220 Encoding = llvm::dwarf::DW_ATE_signed;
221 break;
222 }
223
224 // Ty will have contain the resulting type.
225 llvm::BasicTypeDesc *BTy = new llvm::BasicTypeDesc();
226
227 // Get the name and location early to assist debugging.
228 const char *TyName = BT->getName();
229
230 // Bit size, align and offset of the type.
231 uint64_t Size = M->getContext().getTypeSize(type);
232 uint64_t Align = M->getContext().getTypeAlign(type);
233 uint64_t Offset = 0;
234
235 // If the type is defined, fill in the details.
236 if (BTy) {
237 BTy->setContext(Unit);
238 BTy->setName(TyName);
239 BTy->setSize(Size);
240 BTy->setAlign(Align);
241 BTy->setOffset(Offset);
242 BTy->setEncoding(Encoding);
243 }
244
245 return BTy;
246}
247
248llvm::TypeDesc *
249CGDebugInfo::getOrCreatePointerType(QualType type, llvm::CompileUnitDesc *Unit)
250{
251 // type*
252 llvm::DerivedTypeDesc *DTy =
253 new llvm::DerivedTypeDesc(llvm::dwarf::DW_TAG_pointer_type);
254
255 // Handle the derived type.
256 const PointerType *PTRT = type->getAsPointerType();
257 llvm::TypeDesc *FromTy = getOrCreateType(PTRT->getPointeeType(), Unit);
258
259 // Get the name and location early to assist debugging.
260 SourceManager &SM = M->getContext().getSourceManager();
261 uint64_t Line = SM.getLogicalLineNumber(CurLoc);
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 (DTy) {
270 DTy->setContext(Unit);
271 DTy->setLine(Line);
272 DTy->setSize(Size);
273 DTy->setAlign(Align);
274 DTy->setOffset(Offset);
275 DTy->setFromType(FromTy);
276 }
277
278 return DTy;
279}
280
281llvm::TypeDesc *
282CGDebugInfo::getOrCreateTypedefType(QualType type, llvm::CompileUnitDesc *Unit)
283{
284 // typedefs are derived from some other type.
285 llvm::DerivedTypeDesc *DTy =
286 new llvm::DerivedTypeDesc(llvm::dwarf::DW_TAG_typedef);
287
288 // Handle derived type.
289 const TypedefType *TDT = type->getAsTypedefType();
290 llvm::TypeDesc *FromTy = getOrCreateType(TDT->LookThroughTypedefs(),
291 Unit);
292
293 // Get the name and location early to assist debugging.
294 const char *TyName = TDT->getDecl()->getName();
295 SourceManager &SM = M->getContext().getSourceManager();
296 uint64_t Line = SM.getLogicalLineNumber(TDT->getDecl()->getLocation());
297
298 // If the type is defined, fill in the details.
299 if (DTy) {
300 DTy->setContext(Unit);
301 DTy->setFile(getOrCreateCompileUnit(TDT->getDecl()->getLocation()));
302 DTy->setLine(Line);
303 DTy->setName(TyName);
304 DTy->setFromType(FromTy);
305 }
306
307 return DTy;
308}
309
310llvm::TypeDesc *
311CGDebugInfo::getOrCreateFunctionType(QualType type, llvm::CompileUnitDesc *Unit)
312{
313 llvm::CompositeTypeDesc *SubrTy =
314 new llvm::CompositeTypeDesc(llvm::dwarf::DW_TAG_subroutine_type);
315
316 // Prepare to add the arguments for the subroutine.
317 std::vector<llvm::DebugInfoDesc *> &Elements = SubrTy->getElements();
318
319 // Get result type.
320 const FunctionType *FT = type->getAsFunctionType();
321 llvm::TypeDesc *ArgTy = getOrCreateType(FT->getResultType(), Unit);
322 if (ArgTy) Elements.push_back(ArgTy);
323
324 // Set up remainder of arguments.
325 if (type->getTypeClass() == Type::FunctionProto) {
326 const FunctionTypeProto *FTPro = dyn_cast<FunctionTypeProto>(type);
327 for (unsigned int i =0; i < FTPro->getNumArgs(); i++) {
328 QualType ParamType = FTPro->getArgType(i);
329 ArgTy = getOrCreateType(ParamType, Unit);
330 if (ArgTy) Elements.push_back(ArgTy);
331 }
332 }
333
334 // FIXME: set other fields file, line here.
335 SubrTy->setContext(Unit);
336
337 return SubrTy;
338}
339
340
341
342/// getOrCreateType - Get the type from the cache or create a new
343/// one if necessary.
344llvm::TypeDesc *
345CGDebugInfo::getOrCreateType(QualType type, llvm::CompileUnitDesc *Unit)
346{
Eli Friedman6a03c262008-05-29 11:08:17 +0000347 // TODO: Re-enable once we can generate all types
348 return 0;
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000349 if (type.isNull())
350 return NULL;
351
352 // Check to see if the compile unit already has created this type.
353 llvm::TypeDesc *&Slot = TypeCache[type.getAsOpaquePtr()];
354 if (Slot) return Slot;
355
356 // We need to check for the CVR qualifiers as the first thing.
357 if (type.getCVRQualifiers()) {
358 Slot = getOrCreateCVRType (type, Unit);
359 return Slot;
360 }
361
362 // Work out details of type.
363 switch(type->getTypeClass()) {
364 case Type::Complex:
365 case Type::Reference:
366 case Type::ConstantArray:
367 case Type::VariableArray:
368 case Type::IncompleteArray:
369 case Type::Vector:
370 case Type::ExtVector:
371 case Type::Tagged:
372 case Type::ASQual:
373 case Type::ObjCInterface:
374 case Type::ObjCQualifiedInterface:
375 case Type::ObjCQualifiedId:
376 case Type::TypeOfExp:
377 case Type::TypeOfTyp:
378 default:
379 {
380 assert (0 && "Unsupported type");
381 return NULL;
382 }
383
384 case Type::TypeName:
385 Slot = getOrCreateTypedefType(type, Unit);
386 break;
387
388 case Type::FunctionProto:
389 case Type::FunctionNoProto:
390 Slot = getOrCreateFunctionType(type, Unit);
391 break;
392
393 case Type::Builtin:
394 Slot = getOrCreateBuiltinType(type, Unit);
395 break;
396
397 case Type::Pointer:
398 Slot = getOrCreatePointerType(type, Unit);
399 break;
400 }
401
402 return Slot;
403}
404
405/// EmitFunctionStart - Constructs the debug code for entering a function -
406/// "llvm.dbg.func.start.".
407void CGDebugInfo::EmitFunctionStart(const FunctionDecl *FnDecl,
408 llvm::Function *Fn,
409 llvm::IRBuilder &Builder)
410{
411 // Create subprogram descriptor.
412 Subprogram = new llvm::SubprogramDesc();
413
414 // Make sure we have an anchor.
415 if (!SubprogramAnchor) {
416 SubprogramAnchor = new llvm::AnchorDesc(Subprogram);
417 }
418
419 // Get name information.
420 Subprogram->setName(FnDecl->getName());
421 Subprogram->setFullName(FnDecl->getName());
422
423 // Gather location information.
424 llvm::CompileUnitDesc *Unit = getOrCreateCompileUnit(CurLoc);
425 SourceManager &SM = M->getContext().getSourceManager();
426 uint64_t Loc = SM.getLogicalLineNumber(CurLoc);
427
428 // Get Function Type.
429 QualType type = FnDecl->getResultType();
430 llvm::TypeDesc *SPTy = getOrCreateType(type, Unit);
431
432 Subprogram->setAnchor(SubprogramAnchor);
433 Subprogram->setContext(Unit);
434 Subprogram->setFile(Unit);
435 Subprogram->setLine(Loc);
436 Subprogram->setType(SPTy);
437 Subprogram->setIsStatic(Fn->hasInternalLinkage());
438 Subprogram->setIsDefinition(true);
439
440 // Lazily construct llvm.dbg.func.start.
441 if (!FuncStartFn)
442 FuncStartFn = llvm::Intrinsic::getDeclaration(&M->getModule(),
443 llvm::Intrinsic::dbg_func_start);
444
445 // Call llvm.dbg.func.start which also implicitly calls llvm.dbg.stoppoint.
446 Builder.CreateCall(FuncStartFn, getCastValueFor(Subprogram), "");
447
448 // Push function on region stack.
449 RegionStack.push_back(Subprogram);
450}
451
452
453void
454CGDebugInfo::EmitStopPoint(llvm::Function *Fn, llvm::IRBuilder &Builder)
455{
456 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
457
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000458 // Don't bother if things are the same as last time.
459 SourceManager &SM = M->getContext().getSourceManager();
460 if (CurLoc == PrevLoc
461 || (SM.getLineNumber(CurLoc) == SM.getLineNumber(PrevLoc)
462 && SM.isFromSameFile(CurLoc, PrevLoc)))
463 return;
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000464
465 // Update last state.
466 PrevLoc = CurLoc;
467
468 // Get the appropriate compile unit.
469 llvm::CompileUnitDesc *Unit = getOrCreateCompileUnit(CurLoc);
470
471 // Lazily construct llvm.dbg.stoppoint function.
472 if (!StopPointFn)
473 StopPointFn = llvm::Intrinsic::getDeclaration(&M->getModule(),
Eli Friedman6fb115c2008-05-13 14:40:48 +0000474 llvm::Intrinsic::dbg_stoppoint);
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000475
476 uint64_t CurLineNo = SM.getLogicalLineNumber(CurLoc);
477 uint64_t ColumnNo = SM.getLogicalColumnNumber(CurLoc);
478
479 // Invoke llvm.dbg.stoppoint
480 Builder.CreateCall3(StopPointFn,
Eli Friedman6fb115c2008-05-13 14:40:48 +0000481 llvm::ConstantInt::get(llvm::Type::Int32Ty, CurLineNo),
482 llvm::ConstantInt::get(llvm::Type::Int32Ty, ColumnNo),
483 getCastValueFor(Unit), "");
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000484}
485
486/// EmitRegionStart- Constructs the debug code for entering a declarative
487/// region - "llvm.dbg.region.start.".
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000488void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, llvm::IRBuilder &Builder)
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000489{
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000490 llvm::BlockDesc *Block = new llvm::BlockDesc();
491 if (RegionStack.size() > 0)
492 Block->setContext(RegionStack.back());
493 RegionStack.push_back(Block);
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000494
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000495 // Lazily construct llvm.dbg.region.start function.
496 if (!RegionStartFn)
497 RegionStartFn = llvm::Intrinsic::getDeclaration(&M->getModule(),
498 llvm::Intrinsic::dbg_region_start);
499
500 // Call llvm.dbg.func.start.
501 Builder.CreateCall(RegionStartFn, getCastValueFor(Block), "");
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000502}
503
504/// EmitRegionEnd - Constructs the debug code for exiting a declarative
505/// region - "llvm.dbg.region.end."
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000506void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, llvm::IRBuilder &Builder)
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000507{
508 // Lazily construct llvm.dbg.region.end function.
509 if (!RegionEndFn)
510 RegionEndFn =llvm::Intrinsic::getDeclaration(&M->getModule(),
Eli Friedman6fb115c2008-05-13 14:40:48 +0000511 llvm::Intrinsic::dbg_region_end);
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000512
513 // Provide an region stop point.
514 EmitStopPoint(Fn, Builder);
515
516 // Call llvm.dbg.func.end.
Sanjiv Gupta93eb8252008-05-25 05:15:42 +0000517 llvm::DebugInfoDesc *DID = RegionStack.back();
518 Builder.CreateCall(RegionEndFn, getCastValueFor(DID), "");
519 RegionStack.pop_back();
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000520}
521