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