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