blob: 009245c1122c9948f94a16edd0eadaa28cdb3755 [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 Gupta1c6a38b2008-05-25 05:15:42 +000050, Subprogram(NULL)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000051{
52 SR = new llvm::DISerializer();
53 SR->setModule (&M->getModule());
54}
55
56CGDebugInfo::~CGDebugInfo()
57{
58 delete SR;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000059
60 // Free CompileUnitCache.
61 for (std::map<unsigned, llvm::CompileUnitDesc *>::iterator I
62 = CompileUnitCache.begin(); I != CompileUnitCache.end(); ++I) {
63 delete I->second;
64 }
65 CompileUnitCache.clear();
66
67 // Free TypeCache.
68 for (std::map<void *, llvm::TypeDesc *>::iterator I
69 = TypeCache.begin(); I != TypeCache.end(); ++I) {
70 delete I->second;
71 }
72 TypeCache.clear();
73
74 for (std::vector<llvm::DebugInfoDesc *>::iterator I
75 = RegionStack.begin(); I != RegionStack.end(); ++I) {
76 delete *I;
77 }
Sanjiv Guptacc9b1632008-05-30 10:30:31 +000078
79 for (std::vector<llvm::VariableDesc *>::iterator I
80 = VariableDescList.begin(); I != VariableDescList.end(); ++I) {
81 delete *I;
82 }
83
Sanjiv Gupta686226b2008-06-05 08:59:10 +000084 for (std::vector<llvm::GlobalVariableDesc *>::iterator I
85 = GlobalVarDescList.begin(); I != GlobalVarDescList.end(); ++I) {
86 delete *I;
87 }
88
Eli Friedman3f2af102008-05-22 01:40:10 +000089 delete CompileUnitAnchor;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000090 delete SubprogramAnchor;
Sanjiv Gupta686226b2008-06-05 08:59:10 +000091 delete GlobalVariableAnchor;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000092}
93
Eli Friedman32ea35f2008-05-29 11:08:17 +000094void CGDebugInfo::setLocation(SourceLocation loc)
95{
96 SourceManager &SM = M->getContext().getSourceManager();
97 CurLoc = SM.getLogicalLoc(loc);
98}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000099
100/// getCastValueFor - Return a llvm representation for a given debug information
101/// descriptor cast to an empty struct pointer.
102llvm::Value *CGDebugInfo::getCastValueFor(llvm::DebugInfoDesc *DD) {
103 return llvm::ConstantExpr::getBitCast(SR->Serialize(DD),
Eli Friedman86eb3112008-05-13 14:40:48 +0000104 SR->getEmptyStructPtrType());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000105}
106
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000107/// getValueFor - Return a llvm representation for a given debug information
108/// descriptor.
109llvm::Value *CGDebugInfo::getValueFor(llvm::DebugInfoDesc *DD) {
110 return SR->Serialize(DD);
111}
112
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000113/// getOrCreateCompileUnit - Get the compile unit from the cache or create a new
114/// one if necessary.
115llvm::CompileUnitDesc
116*CGDebugInfo::getOrCreateCompileUnit(const SourceLocation Loc) {
117
118 // See if this compile unit has been used before.
119 llvm::CompileUnitDesc *&Slot = CompileUnitCache[Loc.getFileID()];
120 if (Slot) return Slot;
121
122 // Create new compile unit.
123 // FIXME: Where to free these?
124 // One way is to iterate over the CompileUnitCache in ~CGDebugInfo.
125 llvm::CompileUnitDesc *Unit = new llvm::CompileUnitDesc();
126
127 // Make sure we have an anchor.
128 if (!CompileUnitAnchor) {
129 CompileUnitAnchor = new llvm::AnchorDesc(Unit);
130 }
131
132 // Get source file information.
133 SourceManager &SM = M->getContext().getSourceManager();
134 const FileEntry *FE = SM.getFileEntryForLoc(Loc);
Eli Friedman3f2af102008-05-22 01:40:10 +0000135 const char *FileName, *DirName;
136 if (FE) {
137 FileName = FE->getName();
138 DirName = FE->getDir()->getName();
139 } else {
140 FileName = SM.getSourceName(Loc);
141 DirName = "";
142 }
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000143
144 Unit->setAnchor(CompileUnitAnchor);
145 Unit->setFileName(FileName);
146 Unit->setDirectory(DirName);
147
148 // Set up producer name.
149 // FIXME: Do not know how to get clang version yet.
150 Unit->setProducer("clang");
151
152 // Set up Language number.
153 // FIXME: Handle other languages as well.
154 Unit->setLanguage(llvm::dwarf::DW_LANG_C89);
155
156 // Update cache.
157 Slot = Unit;
158
159 return Unit;
160}
161
162
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000163llvm::TypeDesc *
164CGDebugInfo::getOrCreateCVRType(QualType type, llvm::CompileUnitDesc *Unit)
165{
166 // We will create a Derived type.
167 llvm::DerivedTypeDesc *DTy = NULL;
168 llvm::TypeDesc *FromTy = NULL;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000169
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000170 if (type.isConstQualified()) {
171 DTy = new llvm::DerivedTypeDesc(llvm::dwarf::DW_TAG_const_type);
172 type.removeConst();
173 FromTy = getOrCreateType(type, Unit);
174 } else if (type.isVolatileQualified()) {
175 DTy = new llvm::DerivedTypeDesc(llvm::dwarf::DW_TAG_volatile_type);
176 type.removeVolatile();
177 FromTy = getOrCreateType(type, Unit);
178 } else if (type.isRestrictQualified()) {
179 DTy = new llvm::DerivedTypeDesc(llvm::dwarf::DW_TAG_restrict_type);
180 type.removeRestrict();
181 FromTy = getOrCreateType(type, Unit);
182 }
183
184 // No need to fill in the Name, Line, Size, Alignment, Offset in case of // CVR derived types.
185 DTy->setContext(Unit);
186 DTy->setFromType(FromTy);
187
188 return DTy;
189}
190
191
192/// getOrCreateType - Get the Basic type from the cache or create a new
193/// one if necessary.
194llvm::TypeDesc *
195CGDebugInfo::getOrCreateBuiltinType(QualType type, llvm::CompileUnitDesc *Unit)
196{
197 assert (type->getTypeClass() == Type::Builtin);
198
199 const BuiltinType *BT = type->getAsBuiltinType();
200
201 unsigned Encoding = 0;
202 switch (BT->getKind())
203 {
204 case BuiltinType::Void:
205 return NULL;
206 case BuiltinType::UChar:
207 case BuiltinType::Char_U:
208 Encoding = llvm::dwarf::DW_ATE_unsigned_char;
209 break;
210 case BuiltinType::Char_S:
211 case BuiltinType::SChar:
212 Encoding = llvm::dwarf::DW_ATE_signed_char;
213 break;
214 case BuiltinType::UShort:
215 case BuiltinType::UInt:
216 case BuiltinType::ULong:
217 case BuiltinType::ULongLong:
218 Encoding = llvm::dwarf::DW_ATE_unsigned;
219 break;
220 case BuiltinType::Short:
221 case BuiltinType::Int:
222 case BuiltinType::Long:
223 case BuiltinType::LongLong:
224 Encoding = llvm::dwarf::DW_ATE_signed;
225 break;
226 case BuiltinType::Bool:
227 Encoding = llvm::dwarf::DW_ATE_boolean;
228 break;
229 case BuiltinType::Float:
230 case BuiltinType::Double:
231 Encoding = llvm::dwarf::DW_ATE_float;
232 break;
233 default:
234 Encoding = llvm::dwarf::DW_ATE_signed;
235 break;
236 }
237
238 // Ty will have contain the resulting type.
239 llvm::BasicTypeDesc *BTy = new llvm::BasicTypeDesc();
240
241 // Get the name and location early to assist debugging.
242 const char *TyName = BT->getName();
243
244 // Bit size, align and offset of the type.
245 uint64_t Size = M->getContext().getTypeSize(type);
246 uint64_t Align = M->getContext().getTypeAlign(type);
247 uint64_t Offset = 0;
248
249 // If the type is defined, fill in the details.
250 if (BTy) {
251 BTy->setContext(Unit);
252 BTy->setName(TyName);
253 BTy->setSize(Size);
254 BTy->setAlign(Align);
255 BTy->setOffset(Offset);
256 BTy->setEncoding(Encoding);
257 }
258
259 return BTy;
260}
261
262llvm::TypeDesc *
263CGDebugInfo::getOrCreatePointerType(QualType type, llvm::CompileUnitDesc *Unit)
264{
265 // type*
266 llvm::DerivedTypeDesc *DTy =
267 new llvm::DerivedTypeDesc(llvm::dwarf::DW_TAG_pointer_type);
268
269 // Handle the derived type.
270 const PointerType *PTRT = type->getAsPointerType();
271 llvm::TypeDesc *FromTy = getOrCreateType(PTRT->getPointeeType(), Unit);
272
273 // Get the name and location early to assist debugging.
274 SourceManager &SM = M->getContext().getSourceManager();
275 uint64_t Line = SM.getLogicalLineNumber(CurLoc);
276
277 // Bit size, align and offset of the type.
278 uint64_t Size = M->getContext().getTypeSize(type);
279 uint64_t Align = M->getContext().getTypeAlign(type);
280 uint64_t Offset = 0;
281
282 // If the type is defined, fill in the details.
283 if (DTy) {
284 DTy->setContext(Unit);
285 DTy->setLine(Line);
286 DTy->setSize(Size);
287 DTy->setAlign(Align);
288 DTy->setOffset(Offset);
289 DTy->setFromType(FromTy);
290 }
291
292 return DTy;
293}
294
295llvm::TypeDesc *
296CGDebugInfo::getOrCreateTypedefType(QualType type, llvm::CompileUnitDesc *Unit)
297{
298 // typedefs are derived from some other type.
299 llvm::DerivedTypeDesc *DTy =
300 new llvm::DerivedTypeDesc(llvm::dwarf::DW_TAG_typedef);
301
302 // Handle derived type.
303 const TypedefType *TDT = type->getAsTypedefType();
304 llvm::TypeDesc *FromTy = getOrCreateType(TDT->LookThroughTypedefs(),
305 Unit);
306
307 // Get the name and location early to assist debugging.
308 const char *TyName = TDT->getDecl()->getName();
309 SourceManager &SM = M->getContext().getSourceManager();
310 uint64_t Line = SM.getLogicalLineNumber(TDT->getDecl()->getLocation());
311
312 // If the type is defined, fill in the details.
313 if (DTy) {
314 DTy->setContext(Unit);
315 DTy->setFile(getOrCreateCompileUnit(TDT->getDecl()->getLocation()));
316 DTy->setLine(Line);
317 DTy->setName(TyName);
318 DTy->setFromType(FromTy);
319 }
320
321 return DTy;
322}
323
324llvm::TypeDesc *
325CGDebugInfo::getOrCreateFunctionType(QualType type, llvm::CompileUnitDesc *Unit)
326{
327 llvm::CompositeTypeDesc *SubrTy =
328 new llvm::CompositeTypeDesc(llvm::dwarf::DW_TAG_subroutine_type);
329
330 // Prepare to add the arguments for the subroutine.
331 std::vector<llvm::DebugInfoDesc *> &Elements = SubrTy->getElements();
332
333 // Get result type.
334 const FunctionType *FT = type->getAsFunctionType();
335 llvm::TypeDesc *ArgTy = getOrCreateType(FT->getResultType(), Unit);
336 if (ArgTy) Elements.push_back(ArgTy);
337
338 // Set up remainder of arguments.
339 if (type->getTypeClass() == Type::FunctionProto) {
340 const FunctionTypeProto *FTPro = dyn_cast<FunctionTypeProto>(type);
341 for (unsigned int i =0; i < FTPro->getNumArgs(); i++) {
342 QualType ParamType = FTPro->getArgType(i);
343 ArgTy = getOrCreateType(ParamType, Unit);
344 if (ArgTy) Elements.push_back(ArgTy);
345 }
346 }
347
348 // FIXME: set other fields file, line here.
349 SubrTy->setContext(Unit);
350
351 return SubrTy;
352}
353
354
355
356/// getOrCreateType - Get the type from the cache or create a new
357/// one if necessary.
358llvm::TypeDesc *
359CGDebugInfo::getOrCreateType(QualType type, llvm::CompileUnitDesc *Unit)
360{
Eli Friedman32ea35f2008-05-29 11:08:17 +0000361 // TODO: Re-enable once we can generate all types
362 return 0;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000363 if (type.isNull())
364 return NULL;
365
366 // Check to see if the compile unit already has created this type.
367 llvm::TypeDesc *&Slot = TypeCache[type.getAsOpaquePtr()];
368 if (Slot) return Slot;
369
370 // We need to check for the CVR qualifiers as the first thing.
371 if (type.getCVRQualifiers()) {
372 Slot = getOrCreateCVRType (type, Unit);
373 return Slot;
374 }
375
376 // Work out details of type.
377 switch(type->getTypeClass()) {
378 case Type::Complex:
379 case Type::Reference:
380 case Type::ConstantArray:
381 case Type::VariableArray:
382 case Type::IncompleteArray:
383 case Type::Vector:
384 case Type::ExtVector:
385 case Type::Tagged:
386 case Type::ASQual:
387 case Type::ObjCInterface:
388 case Type::ObjCQualifiedInterface:
389 case Type::ObjCQualifiedId:
390 case Type::TypeOfExp:
391 case Type::TypeOfTyp:
392 default:
393 {
394 assert (0 && "Unsupported type");
395 return NULL;
396 }
397
398 case Type::TypeName:
399 Slot = getOrCreateTypedefType(type, Unit);
400 break;
401
402 case Type::FunctionProto:
403 case Type::FunctionNoProto:
404 Slot = getOrCreateFunctionType(type, Unit);
405 break;
406
407 case Type::Builtin:
408 Slot = getOrCreateBuiltinType(type, Unit);
409 break;
410
411 case Type::Pointer:
412 Slot = getOrCreatePointerType(type, Unit);
413 break;
414 }
415
416 return Slot;
417}
418
419/// EmitFunctionStart - Constructs the debug code for entering a function -
420/// "llvm.dbg.func.start.".
421void CGDebugInfo::EmitFunctionStart(const FunctionDecl *FnDecl,
422 llvm::Function *Fn,
423 llvm::IRBuilder &Builder)
424{
425 // Create subprogram descriptor.
426 Subprogram = new llvm::SubprogramDesc();
427
428 // Make sure we have an anchor.
429 if (!SubprogramAnchor) {
430 SubprogramAnchor = new llvm::AnchorDesc(Subprogram);
431 }
432
433 // Get name information.
434 Subprogram->setName(FnDecl->getName());
435 Subprogram->setFullName(FnDecl->getName());
436
437 // Gather location information.
438 llvm::CompileUnitDesc *Unit = getOrCreateCompileUnit(CurLoc);
439 SourceManager &SM = M->getContext().getSourceManager();
440 uint64_t Loc = SM.getLogicalLineNumber(CurLoc);
441
442 // Get Function Type.
443 QualType type = FnDecl->getResultType();
444 llvm::TypeDesc *SPTy = getOrCreateType(type, Unit);
445
446 Subprogram->setAnchor(SubprogramAnchor);
447 Subprogram->setContext(Unit);
448 Subprogram->setFile(Unit);
449 Subprogram->setLine(Loc);
450 Subprogram->setType(SPTy);
451 Subprogram->setIsStatic(Fn->hasInternalLinkage());
452 Subprogram->setIsDefinition(true);
453
454 // Lazily construct llvm.dbg.func.start.
455 if (!FuncStartFn)
456 FuncStartFn = llvm::Intrinsic::getDeclaration(&M->getModule(),
457 llvm::Intrinsic::dbg_func_start);
458
459 // Call llvm.dbg.func.start which also implicitly calls llvm.dbg.stoppoint.
460 Builder.CreateCall(FuncStartFn, getCastValueFor(Subprogram), "");
461
462 // Push function on region stack.
463 RegionStack.push_back(Subprogram);
464}
465
466
467void
468CGDebugInfo::EmitStopPoint(llvm::Function *Fn, llvm::IRBuilder &Builder)
469{
470 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
471
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000472 // Don't bother if things are the same as last time.
473 SourceManager &SM = M->getContext().getSourceManager();
474 if (CurLoc == PrevLoc
475 || (SM.getLineNumber(CurLoc) == SM.getLineNumber(PrevLoc)
476 && SM.isFromSameFile(CurLoc, PrevLoc)))
477 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000478
479 // Update last state.
480 PrevLoc = CurLoc;
481
482 // Get the appropriate compile unit.
483 llvm::CompileUnitDesc *Unit = getOrCreateCompileUnit(CurLoc);
484
485 // Lazily construct llvm.dbg.stoppoint function.
486 if (!StopPointFn)
487 StopPointFn = llvm::Intrinsic::getDeclaration(&M->getModule(),
Eli Friedman86eb3112008-05-13 14:40:48 +0000488 llvm::Intrinsic::dbg_stoppoint);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000489
490 uint64_t CurLineNo = SM.getLogicalLineNumber(CurLoc);
491 uint64_t ColumnNo = SM.getLogicalColumnNumber(CurLoc);
492
493 // Invoke llvm.dbg.stoppoint
494 Builder.CreateCall3(StopPointFn,
Eli Friedman86eb3112008-05-13 14:40:48 +0000495 llvm::ConstantInt::get(llvm::Type::Int32Ty, CurLineNo),
496 llvm::ConstantInt::get(llvm::Type::Int32Ty, ColumnNo),
497 getCastValueFor(Unit), "");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000498}
499
500/// EmitRegionStart- Constructs the debug code for entering a declarative
501/// region - "llvm.dbg.region.start.".
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000502void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, llvm::IRBuilder &Builder)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000503{
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000504 llvm::BlockDesc *Block = new llvm::BlockDesc();
505 if (RegionStack.size() > 0)
506 Block->setContext(RegionStack.back());
507 RegionStack.push_back(Block);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000508
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000509 // Lazily construct llvm.dbg.region.start function.
510 if (!RegionStartFn)
511 RegionStartFn = llvm::Intrinsic::getDeclaration(&M->getModule(),
512 llvm::Intrinsic::dbg_region_start);
513
514 // Call llvm.dbg.func.start.
515 Builder.CreateCall(RegionStartFn, getCastValueFor(Block), "");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000516}
517
518/// EmitRegionEnd - Constructs the debug code for exiting a declarative
519/// region - "llvm.dbg.region.end."
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000520void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, llvm::IRBuilder &Builder)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000521{
522 // Lazily construct llvm.dbg.region.end function.
523 if (!RegionEndFn)
524 RegionEndFn =llvm::Intrinsic::getDeclaration(&M->getModule(),
Eli Friedman86eb3112008-05-13 14:40:48 +0000525 llvm::Intrinsic::dbg_region_end);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000526
527 // Provide an region stop point.
528 EmitStopPoint(Fn, Builder);
529
530 // Call llvm.dbg.func.end.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000531 llvm::DebugInfoDesc *DID = RegionStack.back();
532 Builder.CreateCall(RegionEndFn, getCastValueFor(DID), "");
533 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000534}
535
Sanjiv Guptacc9b1632008-05-30 10:30:31 +0000536/// EmitDeclare - Emit local variable declaration debug info.
537void CGDebugInfo::EmitDeclare(const VarDecl *decl, unsigned Tag,
538 llvm::Value *AI,
539 llvm::IRBuilder &Builder)
540{
541 // FIXME: If it is a compiler generated temporary then return.
542
543 // Construct llvm.dbg.declare function.
544 if (!DeclareFn)
545 DeclareFn = llvm::Intrinsic::getDeclaration(&M->getModule(),
546 llvm::Intrinsic::dbg_declare);
547
548 // Get type information.
549 llvm::CompileUnitDesc *Unit = getOrCreateCompileUnit(CurLoc);
550 llvm::TypeDesc *TyDesc = getOrCreateType(decl->getType(), Unit);
551
552 SourceManager &SM = M->getContext().getSourceManager();
553 uint64_t Loc = SM.getLogicalLineNumber(CurLoc);
554
555 // Construct variable.
556 llvm::VariableDesc *Variable = new llvm::VariableDesc(Tag);
557 Variable->setContext(RegionStack.back());
558 Variable->setName(decl->getName());
559 Variable->setFile(Unit);
560 Variable->setLine(Loc);
561 Variable->setType(TyDesc);
562
563 // Push it onto the list so that we can free it.
564 VariableDescList.push_back(Variable);
565
566 // Cast the AllocA result to a {}* for the call to llvm.dbg.declare.
567 // These bit cast instructions will get freed when the basic block is
568 // deleted. So do not need to free them explicity.
569 const llvm::PointerType *EmpPtr = SR->getEmptyStructPtrType();
570 llvm::Value *AllocACast = new llvm::BitCastInst(AI, EmpPtr, decl->getName(),
571 Builder.GetInsertBlock());
572
573 // Call llvm.dbg.declare.
574 Builder.CreateCall2(DeclareFn, AllocACast, getCastValueFor(Variable), "");
575}
576
Sanjiv Gupta686226b2008-06-05 08:59:10 +0000577/// EmitGlobalVariable - Emit information about a global variable.
578void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *GV,
579 const VarDecl *decl)
580{
581 // Create global variable debug descriptor.
582 llvm::GlobalVariableDesc *Global = new llvm::GlobalVariableDesc();
583
584 // Push it onto the list so that we can free it.
585 GlobalVarDescList.push_back(Global);
586
587 // Make sure we have an anchor.
588 if (!GlobalVariableAnchor)
589 GlobalVariableAnchor = new llvm::AnchorDesc(Global);
590
591 // Get name information.
592 Global->setName(decl->getName());
593 Global->setFullName(decl->getName());
594
595 llvm::CompileUnitDesc *Unit = getOrCreateCompileUnit(CurLoc);
596 SourceManager &SM = M->getContext().getSourceManager();
597 uint64_t Loc = SM.getLogicalLineNumber(CurLoc);
598
599 llvm::TypeDesc *TyD = getOrCreateType(decl->getType(), Unit);
600
601 // Fill in the Global information.
602 Global->setAnchor(GlobalVariableAnchor);
603 Global->setContext(Unit);
604 Global->setFile(Unit);
605 Global->setLine(Loc);
606 Global->setType(TyD);
607 Global->setIsDefinition(true);
608 Global->setIsStatic(GV->hasInternalLinkage());
609 Global->setGlobalVariable(GV);
610
611 // Make sure global is created if needed.
612 getValueFor(Global);
613}
614