DwarfDebug.cpp revision f2b04232006142eb7933972fb21d9ffb9b8c2646
1c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)//===-- llvm/CodeGen/DwarfDebug.cpp - Dwarf Debug Framework ---------------===//
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//                     The LLVM Compiler Infrastructure
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This file is distributed under the University of Illinois Open Source
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// License. See LICENSE.TXT for details.
77d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)//
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//===----------------------------------------------------------------------===//
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This file contains support for writing dwarf debug info into asm files.
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//===----------------------------------------------------------------------===//
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
144311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch#define DEBUG_TYPE "dwarfdebug"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "DwarfDebug.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "DIE.h"
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "DwarfCompileUnit.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/Constants.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/Module.h"
207d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "llvm/Instructions.h"
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/CodeGen/MachineFunction.h"
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/CodeGen/MachineModuleInfo.h"
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/MC/MCAsmInfo.h"
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/MC/MCSection.h"
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/MC/MCStreamer.h"
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/MC/MCSymbol.h"
27b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#include "llvm/Target/Mangler.h"
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/Target/TargetData.h"
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/Target/TargetFrameLowering.h"
30eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "llvm/Target/TargetLoweringObjectFile.h"
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/Target/TargetMachine.h"
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/Target/TargetRegisterInfo.h"
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/Target/TargetOptions.h"
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/Analysis/DebugInfo.h"
35a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#include "llvm/Analysis/DIBuilder.h"
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/ADT/Statistic.h"
37c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "llvm/ADT/STLExtras.h"
38c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "llvm/ADT/StringExtras.h"
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/Support/CommandLine.h"
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/Support/Debug.h"
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/Support/ErrorHandling.h"
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/Support/ValueHandle.h"
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/Support/FormattedStream.h"
44b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#include "llvm/Support/Timer.h"
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/Support/Path.h"
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)using namespace llvm;
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static cl::opt<bool> PrintDbgScope("print-dbgscope", cl::Hidden,
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)     cl::desc("Print DbgScope information for each machine instruction"));
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static cl::opt<bool> DisableDebugInfoPrinting("disable-debug-info-print",
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                              cl::Hidden,
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)     cl::desc("Disable debug info printing"));
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static cl::opt<bool> UnknownLocations("use-unknown-locations", cl::Hidden,
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)     cl::desc("Make an absence of debug location information explicit."),
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     cl::init(false));
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
59ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdochnamespace {
603240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  const char *DWARFGroupName = "DWARF Emission";
613240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  const char *DbgTimerName = "DWARF Debug Writer";
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // end anonymous namespace
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===----------------------------------------------------------------------===//
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// Configuration values for initial hash set sizes (log2).
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)///
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace llvm {
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)DIType DbgVariable::getType()               const {
739ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  DIType Ty = Var.getType();
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // FIXME: isBlockByrefVariable should be reformulated in terms of complex
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // addresses instead.
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (Var.isBlockByrefVariable()) {
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /* Byref variables, in Blocks, are declared by the programmer as
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       "SomeType VarName;", but the compiler creates a
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       __Block_byref_x_VarName struct, and gives the variable VarName
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       either the struct, or a pointer to the struct, as its type.  This
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       is necessary for various behind-the-scenes things the compiler
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       needs to do with by-reference variables in blocks.
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       However, as far as the original *programmer* is concerned, the
852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       variable should still have type 'SomeType', as originally declared.
862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       The following function dives into the __Block_byref_x_VarName
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       struct to find the original type of the variable.  This will be
892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       passed back to the code generating the type for the Debug
902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       Information Entry for the variable 'VarName'.  'VarName' will then
912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       have the original type 'SomeType' in its debug information.
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       The original type 'SomeType' will be the type of the field named
942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       'VarName' inside the __Block_byref_x_VarName struct.
952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       NOTE: In order for this to not completely fail on the debugger
972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       side, the Debug Information Entry for the variable VarName needs to
982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       have a DW_AT_location that tells the debugger how to unwind through
992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       the pointers and __Block_byref_x_VarName struct to find the actual
100eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch       value of the variable.  The function addBlockByrefType does this.  */
101eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    DIType subType = Ty;
102eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    unsigned tag = Ty.getTag();
103eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
104eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    if (tag == dwarf::DW_TAG_pointer_type) {
105eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      DIDerivedType DTy = DIDerivedType(Ty);
106eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      subType = DTy.getTypeDerivedFrom();
107eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    }
1087dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
1097dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    DICompositeType blockStruct = DICompositeType(subType);
1107dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    DIArray Elements = blockStruct.getTypeArray();
1117dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
1127dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1137dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      DIDescriptor Element = Elements.getElement(i);
1147dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      DIDerivedType DT = DIDerivedType(Element);
1157dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      if (getName() == DT.getName())
1167dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        return (DT.getTypeDerivedFrom());
1177dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    }
1187dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    return Ty;
1197dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  }
1207dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  return Ty;
1217dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}
1222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//===----------------------------------------------------------------------===//
1242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// DbgRange - This is used to track range of instructions with identical
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// debug info scope.
126c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)///
1274311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdochtypedef std::pair<const MachineInstr *, const MachineInstr *> DbgRange;
1284311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch
1294311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch//===----------------------------------------------------------------------===//
1304311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch/// DbgScope - This class is used to track scope information.
1314311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch///
132c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class DbgScope {
133c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DbgScope *Parent;                   // Parent to this scope.
134c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DIDescriptor Desc;                  // Debug info descriptor for scope.
135c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Location at which this scope is inlined.
136c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  AssertingVH<const MDNode> InlinedAtLocation;
137c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  bool AbstractScope;                 // Abstract Scope
138c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const MachineInstr *LastInsn;       // Last instruction of this scope.
139c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const MachineInstr *FirstInsn;      // First instruction of this scope.
140c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  unsigned DFSIn, DFSOut;
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Scopes defined in scope.  Contents not owned.
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SmallVector<DbgScope *, 4> Scopes;
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Variables declared in scope.  Contents owned.
1447d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  SmallVector<DbgVariable *, 8> Variables;
1457d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  SmallVector<DbgRange, 4> Ranges;
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Private state for dump()
1477d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  mutable unsigned IndentLevel;
1487d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)public:
1497d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  DbgScope(DbgScope *P, DIDescriptor D, const MDNode *I = 0)
1507d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    : Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(false),
1517d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      LastInsn(0), FirstInsn(0),
1522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      DFSIn(0), DFSOut(0), IndentLevel(0) {}
1537d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  virtual ~DbgScope();
154bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch
155bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  // Accessors.
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DbgScope *getParent()          const { return Parent; }
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void setParent(DbgScope *P)          { Parent = P; }
1587d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  DIDescriptor getDesc()         const { return Desc; }
159f3ce491c3623b9924dccab3703a43e03157e988aTorne (Richard Coles)  const MDNode *getInlinedAt()         const { return InlinedAtLocation; }
160f3ce491c3623b9924dccab3703a43e03157e988aTorne (Richard Coles)  const MDNode *getScopeNode()         const { return Desc; }
161f3ce491c3623b9924dccab3703a43e03157e988aTorne (Richard Coles)  const SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
1627dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  const SmallVector<DbgVariable *, 8> &getDbgVariables() { return Variables; }
1632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const SmallVector<DbgRange, 4> &getRanges() { return Ranges; }
1642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// openInsnRange - This scope covers instruction range starting from MI.
1662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void openInsnRange(const MachineInstr *MI) {
1672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (!FirstInsn)
168a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch      FirstInsn = MI;
169a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch
170a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch    if (Parent)
1712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      Parent->openInsnRange(MI);
1727d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
174c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// extendInsnRange - Extend the current instruction range covered by
175c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// this scope.
176a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch  void extendInsnRange(const MachineInstr *MI) {
177ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    assert (FirstInsn && "MI Range is not open!");
178ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    LastInsn = MI;
179b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    if (Parent)
18090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      Parent->extendInsnRange(MI);
18190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
182eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
183eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  /// closeInsnRange - Create a range based on FirstInsn and LastInsn collected
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// until now. This is used when a new scope is encountered while walking
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// machine instructions.
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void closeInsnRange(DbgScope *NewScope = NULL) {
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    assert (LastInsn && "Last insn missing!");
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Ranges.push_back(DbgRange(FirstInsn, LastInsn));
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    FirstInsn = NULL;
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LastInsn = NULL;
1913240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch    // If Parent dominates NewScope then do not close Parent's instruction
1923240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch    // range.
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (Parent && (!NewScope || !Parent->dominates(NewScope)))
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      Parent->closeInsnRange(NewScope);
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1973240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  void setAbstractScope() { AbstractScope = true; }
1983240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  bool isAbstractScope() const { return AbstractScope; }
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Depth First Search support to walk and mainpluate DbgScope hierarchy.
2013240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  unsigned getDFSOut() const { return DFSOut; }
2023240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  void setDFSOut(unsigned O) { DFSOut = O; }
2033240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  unsigned getDFSIn() const  { return DFSIn; }
2043240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  void setDFSIn(unsigned I)  { DFSIn = I; }
2053240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  bool dominates(const DbgScope *S) {
206c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (S == this)
2072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return true;
2082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (DFSIn < S->getDFSIn() && DFSOut > S->getDFSOut())
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return true;
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
2112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// addScope - Add a scope to the scope.
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ///
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void addScope(DbgScope *S) { Scopes.push_back(S); }
2162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// addVariable - Add a variable to the scope.
218c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ///
219c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void addVariable(DbgVariable *V) { Variables.push_back(V); }
220c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
221c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#ifndef NDEBUG
222c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void dump() const;
223c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#endif
2242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} // end llvm namespace
2272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifndef NDEBUG
2292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void DbgScope::dump() const {
2302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  raw_ostream &err = dbgs();
2312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  err.indent(IndentLevel);
2322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const MDNode *N = Desc;
2332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  N->dump();
2342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (AbstractScope)
2352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    err << "Abstract Scope\n";
2362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  IndentLevel += 2;
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!Scopes.empty())
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    err << "Children ...\n";
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (unsigned i = 0, e = Scopes.size(); i != e; ++i)
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (Scopes[i] != this)
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      Scopes[i]->dump();
2432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  IndentLevel -= 2;
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)DbgScope::~DbgScope() {
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (unsigned j = 0, M = Variables.size(); j < M; ++j)
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    delete Variables[j];
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
2542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  : Asm(A), MMI(Asm->MMI), FirstCU(0),
2552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    AbbreviationsSet(InitAbbreviationsSetSize),
2562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    CurrentFnDbgScope(0), PrevLabel(NULL) {
2572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  NextStringPoolNumber = 0;
2582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0;
2602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DwarfStrSectionSym = TextSectionSym = 0;
2612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DwarfDebugRangeSectionSym = DwarfDebugLocSectionSym = 0;
2622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FunctionBeginSym = FunctionEndSym = 0;
2632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  {
2642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
2652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    beginModule(M);
2662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)DwarfDebug::~DwarfDebug() {
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)MCSymbol *DwarfDebug::getStringPoolEntry(StringRef Str) {
272c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  std::pair<MCSymbol*, unsigned> &Entry = StringPool[Str];
2732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (Entry.first) return Entry.first;
2742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Entry.second = NextStringPoolNumber++;
2762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return Entry.first = Asm->GetTempSymbol("string", Entry.second);
2772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// assignAbbrevNumber - Define a unique number for the abbreviation.
2812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)///
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void DwarfDebug::assignAbbrevNumber(DIEAbbrev &Abbrev) {
283fe6178dcfbd04cfd6b65eab8deb6f64cf0177454Martin Kosiba  // Profile the node so that we can make it unique.
284fe6178dcfbd04cfd6b65eab8deb6f64cf0177454Martin Kosiba  FoldingSetNodeID ID;
285fe6178dcfbd04cfd6b65eab8deb6f64cf0177454Martin Kosiba  Abbrev.Profile(ID);
286fe6178dcfbd04cfd6b65eab8deb6f64cf0177454Martin Kosiba
287fe6178dcfbd04cfd6b65eab8deb6f64cf0177454Martin Kosiba  // Check the set for priors.
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
290c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // If it's newly added.
2917dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (InSet == &Abbrev) {
2927dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    // Add to abbreviation list.
2937dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    Abbreviations.push_back(&Abbrev);
2947dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
2957dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    // Assign the vector position + 1 as its number.
2967dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    Abbrev.setNumber(Abbreviations.size());
2977c720b7466665b17575e0fd6976f9321c6bff489Torne (Richard Coles)  } else {
2987dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    // Assign existing abbreviation number.
2997dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    Abbrev.setNumber(InSet->getNumber());
3007dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  }
3017dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}
3027dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
3037dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
3047dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch/// printer to not emit usual symbol prefix before the symbol name is used then
3057dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch/// return linkage name after skipping this special LLVM prefix.
3067dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochstatic StringRef getRealLinkageName(StringRef LinkageName) {
307eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  char One = '\1';
308eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (LinkageName.startswith(StringRef(&One, 1)))
3097dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    return LinkageName.substr(1);
310558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch  return LinkageName;
3117dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}
312558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch
3137dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch/// createSubprogramDIE - Create new DIE using SP.
3147dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochDIE *DwarfDebug::createSubprogramDIE(DISubprogram SP) {
3157dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  CompileUnit *SPCU = getCompileUnit(SP);
3167dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  DIE *SPDie = SPCU->getDIE(SP);
3177dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (SPDie)
318eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    return SPDie;
319eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
320eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  SPDie = new DIE(dwarf::DW_TAG_subprogram);
321eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
3227c720b7466665b17575e0fd6976f9321c6bff489Torne (Richard Coles)  // DW_TAG_inlined_subroutine may refer to this DIE.
323eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  SPCU->insertDIE(SP, SPDie);
324eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
325eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // Add to context owner.
326eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  SPCU->addToContextOwner(SPDie, SP.getContext());
327eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
328eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // Add function template parameters.
329eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  SPCU->addTemplateParams(*SPDie, SP.getTemplateParams());
330eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
331eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  StringRef LinkageName = SP.getLinkageName();
332eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (!LinkageName.empty())
333eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    SPCU->addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_FORM_string,
334eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                    getRealLinkageName(LinkageName));
335a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
336a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // If this DIE is going to refer declaration info using AT_specification
337a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // then there is no need to add other attributes.
338a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  if (SP.getFunctionDeclaration().isSubprogram())
339a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    return SPDie;
340eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
341eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // Constructors and operators for anonymous aggregates do not have names.
342eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (!SP.getName().empty())
343a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    SPCU->addString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
344c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                    SP.getName());
345c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
346c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SPCU->addSourceLine(SPDie, SP);
347c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
348c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (SP.isPrototyped())
349c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    SPCU->addUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
350c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
351c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Add Return Type.
352c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DICompositeType SPTy = SP.getType();
353c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DIArray Args = SPTy.getTypeArray();
354c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  unsigned SPTag = SPTy.getTag();
355a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
356c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (Args.getNumElements() == 0 || SPTag != dwarf::DW_TAG_subroutine_type)
357c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    SPCU->addType(SPDie, SPTy);
358c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  else
359c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    SPCU->addType(SPDie, DIType(Args.getElement(0)));
360b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
361a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  unsigned VK = SP.getVirtuality();
362a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  if (VK) {
363a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    SPCU->addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag, VK);
364a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    DIEBlock *Block = SPCU->getDIEBlock();
365a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    SPCU->addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
366a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    SPCU->addUInt(Block, 0, dwarf::DW_FORM_udata, SP.getVirtualIndex());
367a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    SPCU->addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
368a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    ContainingTypeMap.insert(std::make_pair(SPDie,
369a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                                            SP.getContainingType()));
370b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  }
371a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
372a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  if (!SP.isDefinition()) {
373a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    SPCU->addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
374a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
375a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    // Add arguments. Do not add arguments for subprogram definition. They will
376b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    // be handled while processing variables.
3775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DICompositeType SPTy = SP.getType();
3782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DIArray Args = SPTy.getTypeArray();
3795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unsigned SPTag = SPTy.getTag();
3805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (SPTag == dwarf::DW_TAG_subroutine_type)
382c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      for (unsigned i = 1, N =  Args.getNumElements(); i < N; ++i) {
383c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
384c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        DIType ATy = DIType(DIType(Args.getElement(i)));
3855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        SPCU->addType(Arg, ATy);
3862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        if (ATy.isArtificial())
3872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          SPCU->addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
3885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        SPDie->addChild(Arg);
389c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      }
390c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
391c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
392c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (SP.isArtificial())
3937879b4e35bc0616056661d21ed48d28e6cecd0a0Torne (Richard Coles)    SPCU->addUInt(SPDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
3947879b4e35bc0616056661d21ed48d28e6cecd0a0Torne (Richard Coles)
3957879b4e35bc0616056661d21ed48d28e6cecd0a0Torne (Richard Coles)  if (!SP.isLocalToUnit())
3967879b4e35bc0616056661d21ed48d28e6cecd0a0Torne (Richard Coles)    SPCU->addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
3977879b4e35bc0616056661d21ed48d28e6cecd0a0Torne (Richard Coles)
3987879b4e35bc0616056661d21ed48d28e6cecd0a0Torne (Richard Coles)  if (SP.isOptimized())
3997879b4e35bc0616056661d21ed48d28e6cecd0a0Torne (Richard Coles)    SPCU->addUInt(SPDie, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
400b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
401b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  if (unsigned isa = Asm->getISAEncoding()) {
402a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    SPCU->addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
403b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  }
404b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
405b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  return SPDie;
406b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)}
407b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
408b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)DbgScope *DwarfDebug::getOrCreateAbstractScope(const MDNode *N) {
409b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  assert(N && "Invalid Scope encoding!");
410a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
411b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  DbgScope *AScope = AbstractScopes.lookup(N);
412b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  if (AScope)
413b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    return AScope;
414b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
415b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  DbgScope *Parent = NULL;
416c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
417c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DIDescriptor Scope(N);
418c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (Scope.isLexicalBlock()) {
419c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DILexicalBlock DB(N);
420c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DIDescriptor ParentDesc = DB.getContext();
421c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    Parent = getOrCreateAbstractScope(ParentDesc);
422c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
423c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
4247879b4e35bc0616056661d21ed48d28e6cecd0a0Torne (Richard Coles)  AScope = new DbgScope(Parent, DIDescriptor(N), NULL);
4257879b4e35bc0616056661d21ed48d28e6cecd0a0Torne (Richard Coles)
4262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (Parent)
4272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    Parent->addScope(AScope);
4282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  AScope->setAbstractScope();
4297d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  AbstractScopes[N] = AScope;
430c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (DIDescriptor(N).isSubprogram())
4317d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    AbstractScopesList.push_back(AScope);
432c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return AScope;
433c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
4344d9f1c5c0093589a3f3f112641bf9c80442391a0Martin Kosiba
4357d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)/// isSubprogramContext - Return true if Context is either a subprogram
4367d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)/// or another context nested inside a subprogram.
4377d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)static bool isSubprogramContext(const MDNode *Context) {
4387d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (!Context)
4397d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    return false;
4407d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  DIDescriptor D(Context);
4417d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (D.isSubprogram())
4427d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    return true;
4437d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (D.isType())
4447d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    return isSubprogramContext(DIType(Context).getContext());
4457d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  return false;
44668dded969729681d695306eb2b5a6e13c4f06498Torne (Richard Coles)}
44768dded969729681d695306eb2b5a6e13c4f06498Torne (Richard Coles)
44868dded969729681d695306eb2b5a6e13c4f06498Torne (Richard Coles)/// updateSubprogramScopeDIE - Find DIE for the given subprogram and
4497d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)/// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
45068dded969729681d695306eb2b5a6e13c4f06498Torne (Richard Coles)/// If there are global variables in this scope then create and insert
4517d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)/// DIEs for these variables.
452558790d6acca3451cf3a6b497803a5f07d0bec58Ben MurdochDIE *DwarfDebug::updateSubprogramScopeDIE(const MDNode *SPNode) {
4537879b4e35bc0616056661d21ed48d28e6cecd0a0Torne (Richard Coles)  CompileUnit *SPCU = getCompileUnit(SPNode);
4547d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  DIE *SPDie = SPCU->getDIE(SPNode);
4557d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
4567d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  assert(SPDie && "Unable to find subprogram DIE!");
4577d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  DISubprogram SP(SPNode);
458a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
459a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  DISubprogram SPDecl = SP.getFunctionDeclaration();
4607d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (SPDecl.isSubprogram())
461bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch    // Refer function declaration directly.
4624311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch    SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
463bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch                      createSubprogramDIE(SPDecl));
4647d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  else {
4652982f4c76a849b6361b775dc1bc992f6e92b81d6boliu@chromium.org    // There is not any need to generate specification DIE for a function
4662982f4c76a849b6361b775dc1bc992f6e92b81d6boliu@chromium.org    // defined at compile unit level. If a function is defined inside another
4672982f4c76a849b6361b775dc1bc992f6e92b81d6boliu@chromium.org    // function then gdb prefers the definition at top level and but does not
4687d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // expect specification DIE in parent function. So avoid creating
4697d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // specification DIE for a function defined inside a function.
4707d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
4717d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        !SP.getContext().isFile() &&
4727d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        !isSubprogramContext(SP.getContext())) {
4737d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      SPCU-> addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
4747d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
4757d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      // Add arguments.
4767d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      DICompositeType SPTy = SP.getType();
4777d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      DIArray Args = SPTy.getTypeArray();
4787d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      unsigned SPTag = SPTy.getTag();
4797d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      if (SPTag == dwarf::DW_TAG_subroutine_type)
4807d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
4817d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)          DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
4827d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)          DIType ATy = DIType(DIType(Args.getElement(i)));
4837d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)          SPCU->addType(Arg, ATy);
4847d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)          if (ATy.isArtificial())
4857d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)            SPCU->addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
4867d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)          SPDie->addChild(Arg);
4877d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        }
4887d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      DIE *SPDeclDie = SPDie;
4897d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      SPDie = new DIE(dwarf::DW_TAG_subprogram);
4907d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
4917d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                        SPDeclDie);
4925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      SPCU->addDie(SPDie);
4935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
4942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
495b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // Pick up abstract subprogram DIE.
4967d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (DIE *AbsSPDIE = AbstractSPDies.lookup(SPNode)) {
497a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    SPDie = new DIE(dwarf::DW_TAG_subprogram);
498b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    SPCU->addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin,
4997d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                      dwarf::DW_FORM_ref4, AbsSPDIE);
5000a8f47fbce7f94803320e0340e8fe7855159e31aSelim Gurun    SPCU->addDie(SPDie);
5015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5027dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
5037d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  SPCU->addLabel(SPDie, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
504eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
505a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  SPCU->addLabel(SPDie, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr,
506a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
507a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
508a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  MachineLocation Location(RI->getFrameRegister(*Asm->MF));
5097d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  SPCU->addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
5105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5117d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  return SPDie;
5127d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
5137d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
5147d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)/// constructLexicalScope - Construct new DW_TAG_lexical_block
5157d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)/// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
5167d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)DIE *DwarfDebug::constructLexicalScopeDIE(DbgScope *Scope) {
5177d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
5187d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
5197d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (Scope->isAbstractScope())
5207d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    return ScopeDIE;
5217d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
5227d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  const SmallVector<DbgRange, 4> &Ranges = Scope->getRanges();
5237d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (Ranges.empty())
5247d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    return 0;
525b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
5265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CompileUnit *TheCU = getCompileUnit(Scope->getScopeNode());
5277d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin();
5285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (Ranges.size() > 1) {
5295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // .debug_range section has not been laid out yet. Emit offset in
5307d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // .debug_range as a uint, size 4, for now. emitDIE will handle
5317d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // DW_AT_ranges appropriately.
5327d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
5337dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                   DebugRangeSymbols.size() * Asm->getTargetData().getPointerSize());
5347dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    for (SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(),
535f3ce491c3623b9924dccab3703a43e03157e988aTorne (Richard Coles)         RE = Ranges.end(); RI != RE; ++RI) {
536f3ce491c3623b9924dccab3703a43e03157e988aTorne (Richard Coles)      DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
537f3ce491c3623b9924dccab3703a43e03157e988aTorne (Richard Coles)      DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
538cbc64c0be3b310fda39ba08409e70f94954e19c8Kristian Monsen    }
5397dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    DebugRangeSymbols.push_back(NULL);
5407dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    DebugRangeSymbols.push_back(NULL);
541cbc64c0be3b310fda39ba08409e70f94954e19c8Kristian Monsen    return ScopeDIE;
542f3ce491c3623b9924dccab3703a43e03157e988aTorne (Richard Coles)  }
543f3ce491c3623b9924dccab3703a43e03157e988aTorne (Richard Coles)
5447dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  const MCSymbol *Start = getLabelBeforeInsn(RI->first);
545f3ce491c3623b9924dccab3703a43e03157e988aTorne (Richard Coles)  const MCSymbol *End = getLabelAfterInsn(RI->second);
546f3ce491c3623b9924dccab3703a43e03157e988aTorne (Richard Coles)
547f3ce491c3623b9924dccab3703a43e03157e988aTorne (Richard Coles)  if (End == 0) return 0;
5487dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
5497d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
5505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  assert(End->isDefined() && "Invalid end label for an inlined scope!");
5517dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
5527dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, Start);
5537dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, End);
5547d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
555f3ce491c3623b9924dccab3703a43e03157e988aTorne (Richard Coles)  return ScopeDIE;
556f3ce491c3623b9924dccab3703a43e03157e988aTorne (Richard Coles)}
557cbc64c0be3b310fda39ba08409e70f94954e19c8Kristian Monsen
5585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// constructInlinedScopeDIE - This scope represents inlined body of
5595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// a function. Construct DIE to represent this concrete inlined copy
5603240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch/// of the function.
5613240926e260ce088908e02ac07a6cf7b0c0cbf44Ben MurdochDIE *DwarfDebug::constructInlinedScopeDIE(DbgScope *Scope) {
5623240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch
5633240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  const SmallVector<DbgRange, 4> &Ranges = Scope->getRanges();
5643240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  assert (Ranges.empty() == false
5653240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch          && "DbgScope does not have instruction markers!");
5665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5673240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  // FIXME : .debug_inlined section specification does not clearly state how
5683240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  // to emit inlined scope that is split into multiple instruction ranges.
5693240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  // For now, use first instruction range and emit low_pc/high_pc pair and
5703240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  // corresponding .debug_inlined section entry for this pair.
5713240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin();
5723240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  const MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
5733240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  const MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
5743240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch
5753240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  if (StartLabel == 0 || EndLabel == 0) {
5763240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch    assert (0 && "Unexpected Start and End  labels for a inlined scope!");
5773240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch    return 0;
5783240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  }
5793240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  assert(StartLabel->isDefined() &&
5803240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch         "Invalid starting label for an inlined scope!");
5813240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  assert(EndLabel->isDefined() &&
5823240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch         "Invalid end label for an inlined scope!");
5833240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch
5843240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  if (!Scope->getScopeNode())
5853240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch    return NULL;
5863240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  DIScope DS(Scope->getScopeNode());
5873240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  DISubprogram InlinedSP = getDISubprogram(DS);
5887d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  CompileUnit *TheCU = getCompileUnit(InlinedSP);
5893240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  DIE *OriginDIE = TheCU->getDIE(InlinedSP);
5903240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  if (!OriginDIE) {
5917d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    DEBUG(dbgs() << "Unable to find original DIE for inlined subprogram.");
5927d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    return NULL;
5937d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
5947d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
5957d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
5967d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                     dwarf::DW_FORM_ref4, OriginDIE);
5977d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
5987d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  TheCU->addLabel(ScopeDIE, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, StartLabel);
5997d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  TheCU->addLabel(ScopeDIE, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr, EndLabel);
6007d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
6012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  InlinedSubprogramDIEs.insert(OriginDIE);
6022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6035d5be6dc8a24d729a037d5b76e0d1fa711ece471Selim Gurun  // Track the start label for this inlined function.
6045d5be6dc8a24d729a037d5b76e0d1fa711ece471Selim Gurun  DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator
6055d5be6dc8a24d729a037d5b76e0d1fa711ece471Selim Gurun    I = InlineInfo.find(InlinedSP);
6065d5be6dc8a24d729a037d5b76e0d1fa711ece471Selim Gurun
6075d5be6dc8a24d729a037d5b76e0d1fa711ece471Selim Gurun  if (I == InlineInfo.end()) {
608f466d2a0332c0853e69efff71f2979b31e357eddSelim Gurun    InlineInfo[InlinedSP].push_back(std::make_pair(StartLabel,
6095d5be6dc8a24d729a037d5b76e0d1fa711ece471Selim Gurun                                                             ScopeDIE));
6105d5be6dc8a24d729a037d5b76e0d1fa711ece471Selim Gurun    InlinedSPNodes.push_back(InlinedSP);
6115d5be6dc8a24d729a037d5b76e0d1fa711ece471Selim Gurun  } else
6125d5be6dc8a24d729a037d5b76e0d1fa711ece471Selim Gurun    I->second.push_back(std::make_pair(StartLabel, ScopeDIE));
6135d5be6dc8a24d729a037d5b76e0d1fa711ece471Selim Gurun
6145d5be6dc8a24d729a037d5b76e0d1fa711ece471Selim Gurun  DILocation DL(Scope->getInlinedAt());
6155d5be6dc8a24d729a037d5b76e0d1fa711ece471Selim Gurun  TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0, TheCU->getID());
6165d5be6dc8a24d729a037d5b76e0d1fa711ece471Selim Gurun  TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
6175d5be6dc8a24d729a037d5b76e0d1fa711ece471Selim Gurun
6182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return ScopeDIE;
6192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
6205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
622c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)/// constructVariableDIE - Construct a DIE for the given DbgVariable.
623c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)DIE *DwarfDebug::constructVariableDIE(DbgVariable *DV, DbgScope *Scope) {
624c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  StringRef Name = DV->getName();
625c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (Name.empty())
6265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return NULL;
6275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Translate tag to proper Dwarf tag.  The result variable is dropped for
6295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // now.
630eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  unsigned Tag;
631eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  switch (DV->getTag()) {
632eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  case dwarf::DW_TAG_return_variable:
633eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    return NULL;
634eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  case dwarf::DW_TAG_arg_variable:
635eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    Tag = dwarf::DW_TAG_formal_parameter;
636eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    break;
637eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  case dwarf::DW_TAG_auto_variable:    // fall thru
638eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  default:
6395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Tag = dwarf::DW_TAG_variable;
6407c720b7466665b17575e0fd6976f9321c6bff489Torne (Richard Coles)    break;
6417c720b7466665b17575e0fd6976f9321c6bff489Torne (Richard Coles)  }
6427c720b7466665b17575e0fd6976f9321c6bff489Torne (Richard Coles)
6437c720b7466665b17575e0fd6976f9321c6bff489Torne (Richard Coles)  // Define variable debug information entry.
6445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DIE *VariableDie = new DIE(Tag);
6452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  CompileUnit *VariableCU = getCompileUnit(DV->getVariable());
6462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DIE *AbsDIE = NULL;
6475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DenseMap<const DbgVariable *, const DbgVariable *>::iterator
6485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    V2AVI = VarToAbstractVarMap.find(DV);
649a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  if (V2AVI != VarToAbstractVarMap.end())
650a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    AbsDIE = V2AVI->second->getDIE();
651a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
652a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  if (AbsDIE)
653a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    VariableCU->addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
654a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                       dwarf::DW_FORM_ref4, AbsDIE);
6557c720b7466665b17575e0fd6976f9321c6bff489Torne (Richard Coles)  else {
6564311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch    VariableCU->addString(VariableDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
6574311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch                          Name);
6584311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch    VariableCU->addSourceLine(VariableDie, DV->getVariable());
6594311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch
660a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    // Add variable type.
661a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    VariableCU->addType(VariableDie, DV->getType());
662a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  }
6637d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
6647d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (Tag == dwarf::DW_TAG_formal_parameter && DV->getType().isArtificial())
6657d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    VariableCU->addUInt(VariableDie, dwarf::DW_AT_artificial,
6667d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                        dwarf::DW_FORM_flag, 1);
6677d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  else if (DIVariable(DV->getVariable()).isArtificial())
6687d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    VariableCU->addUInt(VariableDie, dwarf::DW_AT_artificial,
6697d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                        dwarf::DW_FORM_flag, 1);
6707d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
6717d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (Scope->isAbstractScope()) {
6725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DV->setDIE(VariableDie);
673a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch    return VariableDie;
674a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch  }
675a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch
676a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch  // Add variable address.
6777d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
6787dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  unsigned Offset = DV->getDotDebugLocOffset();
6797dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (Offset != ~0U) {
6807d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    VariableCU->addLabel(VariableDie, dwarf::DW_AT_location, dwarf::DW_FORM_data4,
6817d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)             Asm->GetTempSymbol("debug_loc", Offset));
6827d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    DV->setDIE(VariableDie);
6837d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    UseDotDebugLocEntry.insert(VariableDie);
684a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    return VariableDie;
6857d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
686a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch
6872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Check if variable is described by a  DBG_VALUE instruction.
688bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  DenseMap<const DbgVariable *, const MachineInstr *>::iterator DVI =
689bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch    DbgVariableToDbgInstMap.find(DV);
690bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  if (DVI != DbgVariableToDbgInstMap.end()) {
691bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch    const MachineInstr *DVInsn = DVI->second;
692bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch    bool updated = false;
693bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch    // FIXME : Handle getNumOperands != 3
6942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (DVInsn->getNumOperands() == 3) {
6952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      if (DVInsn->getOperand(0).isReg()) {
6962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        const MachineOperand RegOp = DVInsn->getOperand(0);
6972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
6982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        if (DVInsn->getOperand(1).isImm() &&
6992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            TRI->getFrameRegister(*Asm->MF) == RegOp.getReg()) {
7002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          unsigned FrameReg = 0;
7012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
7022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          int Offset =
7032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            TFI->getFrameIndexReference(*Asm->MF,
7042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                        DVInsn->getOperand(1).getImm(),
7052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                        FrameReg);
7062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          MachineLocation Location(FrameReg, Offset);
7072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          VariableCU->addVariableAddress(DV, VariableDie, Location);
7082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7097c720b7466665b17575e0fd6976f9321c6bff489Torne (Richard Coles)        } else if (RegOp.getReg())
7104311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch          VariableCU->addVariableAddress(DV, VariableDie,
7114311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch                                         MachineLocation(RegOp.getReg()));
7124311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch        updated = true;
7135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
7145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      else if (DVInsn->getOperand(0).isImm())
7152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        updated = VariableCU->addConstantValue(VariableDie,
716ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch                                               DVInsn->getOperand(0));
7172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      else if (DVInsn->getOperand(0).isFPImm())
7182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        updated =
7192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          VariableCU->addConstantFPValue(VariableDie, DVInsn->getOperand(0));
7202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    } else {
7217c720b7466665b17575e0fd6976f9321c6bff489Torne (Richard Coles)      VariableCU->addVariableAddress(DV, VariableDie,
722ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch                                     Asm->getDebugValueLocation(DVInsn));
723ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      updated = true;
724ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    }
725ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    if (!updated) {
726ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      // If variableDie is not updated then DBG_VALUE instruction does not
727ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      // have valid variable info.
728ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      delete VariableDie;
729ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      return NULL;
730ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    }
731ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    DV->setDIE(VariableDie);
732b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    return VariableDie;
7332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
7342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // .. else use frame index, if available.
7362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  int FI = 0;
7375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (findVariableFrameIndex(DV, &FI)) {
7385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unsigned FrameReg = 0;
7395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
7405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int Offset =
7412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
7425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    MachineLocation Location(FrameReg, Offset);
7435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    VariableCU->addVariableAddress(DV, VariableDie, Location);
7445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
7455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DV->setDIE(VariableDie);
7475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return VariableDie;
7485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void CompileUnit::addPubTypes(DISubprogram SP) {
7525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DICompositeType SPTy = SP.getType();
7535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  unsigned SPTag = SPTy.getTag();
7545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (SPTag != dwarf::DW_TAG_subroutine_type)
755c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return;
7565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DIArray Args = SPTy.getTypeArray();
7585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
7592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DIType ATy(Args.getElement(i));
7602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (!ATy.Verify())
7612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      continue;
7622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DICompositeType CATy = getDICompositeType(ATy);
7632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (DIDescriptor(CATy).Verify() && !CATy.getName().empty()
7642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        && !CATy.isForwardDecl()) {
7652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      if (DIEEntry *Entry = getDIEEntry(CATy))
7662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        addGlobalType(CATy.getName(), Entry->getEntry());
7672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
7682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
7692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
7702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// constructScopeDIE - Construct a DIE for this scope.
7722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)DIE *DwarfDebug::constructScopeDIE(DbgScope *Scope) {
7732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!Scope || !Scope->getScopeNode())
7742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return NULL;
7752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SmallVector <DIE *, 8> Children;
7775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Collect arguments for current function.
7795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (Scope == CurrentFnDbgScope)
7805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    for (unsigned i = 0, N = CurrentFnArguments.size(); i < N; ++i)
7815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (DbgVariable *ArgDV = CurrentFnArguments[i])
7825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        if (DIE *Arg = constructVariableDIE(ArgDV, Scope))
7835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          Children.push_back(Arg);
7845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Collect lexical scope childrens first.
7865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const SmallVector<DbgVariable *, 8> &Variables = Scope->getDbgVariables();
7875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (unsigned i = 0, N = Variables.size(); i < N; ++i)
7885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (DIE *Variable = constructVariableDIE(Variables[i], Scope))
7895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      Children.push_back(Variable);
7905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const SmallVector<DbgScope *, 4> &Scopes = Scope->getScopes();
7915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (unsigned j = 0, M = Scopes.size(); j < M; ++j)
7925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (DIE *Nested = constructScopeDIE(Scopes[j]))
7932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      Children.push_back(Nested);
7942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DIScope DS(Scope->getScopeNode());
7952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DIE *ScopeDIE = NULL;
7962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (Scope->getInlinedAt())
7972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ScopeDIE = constructInlinedScopeDIE(Scope);
7982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  else if (DS.isSubprogram()) {
7992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ProcessedSPNodes.insert(DS);
8007dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    if (Scope->isAbstractScope()) {
8017dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      ScopeDIE = getCompileUnit(DS)->getDIE(DS);
8022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // Note down abstract DIE.
803b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      if (ScopeDIE)
804b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)        AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
805b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    }
806b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    else
8075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ScopeDIE = updateSubprogramScopeDIE(DS);
8085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
8092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  else {
8102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // There is no need to emit empty lexical block DIE.
8112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (Children.empty())
8122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return NULL;
8132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ScopeDIE = constructLexicalScopeDIE(Scope);
8142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
8152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
8162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!ScopeDIE) return NULL;
8172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
8182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Add children
8192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (SmallVector<DIE *, 8>::iterator I = Children.begin(),
8202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)         E = Children.end(); I != E; ++I)
8212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ScopeDIE->addChild(*I);
8222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
8232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (DS.isSubprogram())
8242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    getCompileUnit(DS)->addPubTypes(DISubprogram(DS));
8252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
8262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return ScopeDIE;
8272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
8282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
8292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// GetOrCreateSourceID - Look up the source id with the given directory and
8307c720b7466665b17575e0fd6976f9321c6bff489Torne (Richard Coles)/// source file names. If none currently exists, create a new id and insert it
8312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// in the SourceIds map. This can update DirectoryNames and SourceFileNames
8322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// maps as well.
8332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
8342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)unsigned DwarfDebug::GetOrCreateSourceID(StringRef FileName,
8352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                         StringRef DirName) {
836a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch  // If FE did not provide a file name, then assume stdin.
837a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch  if (FileName.empty())
838a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch    return GetOrCreateSourceID("<stdin>", StringRef());
839a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch
840a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch  // MCStream expects full path name as filename.
841a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch  if (!DirName.empty() && !FileName.startswith("/")) {
842a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch    std::string FullPathName(DirName.data());
843a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch    if (!DirName.endswith("/"))
844a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch      FullPathName += "/";
845a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch    FullPathName += FileName.data();
846a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch    // Here FullPathName will be copied into StringMap by GetOrCreateSourceID.
847a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch    return GetOrCreateSourceID(StringRef(FullPathName), StringRef());
848a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch  }
849a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch
850a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch  StringMapEntry<unsigned> &Entry = SourceIdMap.GetOrCreateValue(FileName);
851b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  if (Entry.getValue())
85268dded969729681d695306eb2b5a6e13c4f06498Torne (Richard Coles)    return Entry.getValue();
853b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
854b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  unsigned SrcId = SourceIdMap.size();
855b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  Entry.setValue(SrcId);
856b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
857b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // Print out a .file directive to specify files for .loc directives.
858b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  Asm->OutStreamer.EmitDwarfFileDirective(SrcId, Entry.getKey());
8595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return SrcId;
8612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
8622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
8632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// getOrCreateNameSpace - Create a DIE for DINameSpace.
8642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)DIE *CompileUnit::getOrCreateNameSpace(DINameSpace NS) {
8652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DIE *NDie = getDIE(NS);
8662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (NDie)
867bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch    return NDie;
868bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  NDie = new DIE(dwarf::DW_TAG_namespace);
869bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  insertDIE(NS, NDie);
870bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  if (!NS.getName().empty())
871bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch    addString(NDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, NS.getName());
872bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  addSourceLine(NDie, NS);
873bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  addToContextOwner(NDie, NS.getContext());
874bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  return NDie;
875bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch}
876bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch
8774311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch/// constructCompileUnit - Create new CompileUnit for the given
8784311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch/// metadata node with tag DW_TAG_compile_unit.
8794311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdochvoid DwarfDebug::constructCompileUnit(const MDNode *N) {
8804311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch  DICompileUnit DIUnit(N);
8814311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch  StringRef FN = DIUnit.getFilename();
8824311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch  StringRef Dir = DIUnit.getDirectory();
8834311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch  unsigned ID = GetOrCreateSourceID(FN, Dir);
8844311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch
8854311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch  DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
8864311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch  CompileUnit *NewCU = new CompileUnit(ID, Die, Asm, this);
8874311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch  NewCU->addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string,
8884311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch                   DIUnit.getProducer());
8894311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch  NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
8904311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch                 DIUnit.getLanguage());
8914311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch  NewCU->addString(Die, dwarf::DW_AT_name, dwarf::DW_FORM_string, FN);
8924311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch  // Use DW_AT_entry_pc instead of DW_AT_low_pc/DW_AT_high_pc pair. This
8934311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch  // simplifies debug range entries.
8944311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch  NewCU->addUInt(Die, dwarf::DW_AT_entry_pc, dwarf::DW_FORM_addr, 0);
8954311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch  // DW_AT_stmt_list is a offset of line number information for this
8964311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch  // compile unit in debug_line section.
8974311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch  if(Asm->MAI->doesDwarfRequireRelocationForStmtList())
8984311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch    NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
8994311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch                    Asm->GetTempSymbol("section_line"));
9004311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch  else
9014311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch    NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
9024311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch
9034311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch  if (!Dir.empty())
9044311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch    NewCU->addString(Die, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string, Dir);
9054311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch  if (DIUnit.isOptimized())
9064311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch    NewCU->addUInt(Die, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
9074311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch
9084311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch  StringRef Flags = DIUnit.getFlags();
9094311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch  if (!Flags.empty())
9104311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch    NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string, Flags);
9114311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch
9124311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch  unsigned RVer = DIUnit.getRunTimeVersion();
9134311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch  if (RVer)
9144311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch    NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
9157c720b7466665b17575e0fd6976f9321c6bff489Torne (Richard Coles)            dwarf::DW_FORM_data1, RVer);
9164311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch
9174311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch  if (!FirstCU)
9184311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch    FirstCU = NewCU;
9194311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch  CUMap.insert(std::make_pair(N, NewCU));
9204311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch}
9214311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch
9224311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch/// getCompielUnit - Get CompileUnit DIE.
9234311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen MurdochCompileUnit *DwarfDebug::getCompileUnit(const MDNode *N) const {
9244311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch  assert (N && "Invalid DwarfDebug::getCompileUnit argument!");
925bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  DIDescriptor D(N);
926bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  const MDNode *CUNode = NULL;
927bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  if (D.isCompileUnit())
928bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch    CUNode = N;
929bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  else if (D.isSubprogram())
930bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch    CUNode = DISubprogram(N).getCompileUnit();
931bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  else if (D.isType())
932bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch    CUNode = DIType(N).getCompileUnit();
933bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  else if (D.isGlobalVariable())
934bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch    CUNode = DIGlobalVariable(N).getCompileUnit();
935bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  else if (D.isVariable())
936bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch    CUNode = DIVariable(N).getCompileUnit();
937bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  else if (D.isNameSpace())
938bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch    CUNode = DINameSpace(N).getCompileUnit();
939bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  else if (D.isFile())
940bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch    CUNode = DIFile(N).getCompileUnit();
941bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  else
942bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch    return FirstCU;
943bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch
944bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  DenseMap<const MDNode *, CompileUnit *>::const_iterator I
945bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch    = CUMap.find(CUNode);
946bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  if (I == CUMap.end())
947bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch    return FirstCU;
948bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  return I->second;
949bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch}
950bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch
951bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch/// isUnsignedDIType - Return true if type encoding is unsigned.
952bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdochstatic bool isUnsignedDIType(DIType Ty) {
95368dded969729681d695306eb2b5a6e13c4f06498Torne (Richard Coles)  DIDerivedType DTy(Ty);
95468dded969729681d695306eb2b5a6e13c4f06498Torne (Richard Coles)  if (DTy.Verify())
95568dded969729681d695306eb2b5a6e13c4f06498Torne (Richard Coles)    return isUnsignedDIType(DTy.getTypeDerivedFrom());
95668dded969729681d695306eb2b5a6e13c4f06498Torne (Richard Coles)
95768dded969729681d695306eb2b5a6e13c4f06498Torne (Richard Coles)  DIBasicType BTy(Ty);
95868dded969729681d695306eb2b5a6e13c4f06498Torne (Richard Coles)  if (BTy.Verify()) {
95968dded969729681d695306eb2b5a6e13c4f06498Torne (Richard Coles)    unsigned Encoding = BTy.getEncoding();
96068dded969729681d695306eb2b5a6e13c4f06498Torne (Richard Coles)    if (Encoding == dwarf::DW_ATE_unsigned ||
96168dded969729681d695306eb2b5a6e13c4f06498Torne (Richard Coles)        Encoding == dwarf::DW_ATE_unsigned_char)
962a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      return true;
963a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  }
964a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  return false;
965a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
966a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
967a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)// Return const exprssion if value is a GEP to access merged global
968a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)// constant. e.g.
9697dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch// i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
9702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
9712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
9727dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (!CE || CE->getNumOperands() != 3 ||
9732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      CE->getOpcode() != Instruction::GetElementPtr)
9742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return NULL;
9752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
9767dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // First operand points to a global value.
9772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!isa<GlobalValue>(CE->getOperand(0)))
9782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return NULL;
9797dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
9802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Second operand is zero.
9812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const ConstantInt *CI =
9822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
9837dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (!CI || !CI->isZero())
9842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return NULL;
9852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
9867dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // Third operand is offset.
9872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!isa<ConstantInt>(CE->getOperand(2)))
9882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return NULL;
9892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
9907dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  return CE;
9912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
9922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
9937dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch/// constructGlobalVariableDIE - Construct global variable DIE.
9942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void DwarfDebug::constructGlobalVariableDIE(const MDNode *N) {
9952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DIGlobalVariable GV(N);
9962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
9977dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // If debug information is malformed then ignore it.
9982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (GV.Verify() == false)
9992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
10007dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
10012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Check for pre-existence.
10022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  CompileUnit *TheCU = getCompileUnit(N);
10032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (TheCU->getDIE(GV))
10042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
10052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
10062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DIType GTy = GV.getType();
10072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DIE *VariableDIE = new DIE(GV.getTag());
10082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
10092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool isGlobalVariable = GV.getGlobal() != NULL;
10102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
10112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Add name.
10122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  TheCU->addString(VariableDIE, dwarf::DW_AT_name, dwarf::DW_FORM_string,
10132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                   GV.getDisplayName());
10142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  StringRef LinkageName = GV.getLinkageName();
10152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!LinkageName.empty() && isGlobalVariable)
10162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    TheCU->addString(VariableDIE, dwarf::DW_AT_MIPS_linkage_name,
10172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                     dwarf::DW_FORM_string,
10182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                     getRealLinkageName(LinkageName));
10192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Add type.
10202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  TheCU->addType(VariableDIE, GTy);
10212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (GTy.isCompositeType() && !GTy.getName().empty()
10222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      && !GTy.isForwardDecl()) {
10232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DIEEntry *Entry = TheCU->getDIEEntry(GTy);
10242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    assert(Entry && "Missing global type!");
10252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    TheCU->addGlobalType(GTy.getName(), Entry->getEntry());
10262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
10272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Add scoping info.
10282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!GV.isLocalToUnit()) {
10292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    TheCU->addUInt(VariableDIE, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
10302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Expose as global.
10312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    TheCU->addGlobal(GV.getName(), VariableDIE);
10322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
10332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Add line number info.
10342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  TheCU->addSourceLine(VariableDIE, GV);
10352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Add to map.
10362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  TheCU->insertDIE(N, VariableDIE);
10372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Add to context owner.
10382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DIDescriptor GVContext = GV.getContext();
10392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  TheCU->addToContextOwner(VariableDIE, GVContext);
10402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Add location.
10412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (isGlobalVariable) {
10422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
10432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    TheCU->addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
10442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    TheCU->addLabel(Block, 0, dwarf::DW_FORM_udata,
10452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)             Asm->Mang->getSymbol(GV.getGlobal()));
10462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Do not create specification DIE if context is either compile unit
10472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // or a subprogram.
10482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (GV.isDefinition() && !GVContext.isCompileUnit() &&
10492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        !GVContext.isFile() && !isSubprogramContext(GVContext)) {
10502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // Create specification DIE.
10512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      DIE *VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
10522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      TheCU->addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
10532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  dwarf::DW_FORM_ref4, VariableDIE);
10542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      TheCU->addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
10552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      TheCU->addUInt(VariableDIE, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
10562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      TheCU->addDie(VariableSpecDIE);
10572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    } else {
10582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      TheCU->addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
10592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
10602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  } else if (ConstantInt *CI =
10612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)             dyn_cast_or_null<ConstantInt>(GV.getConstant()))
10622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    TheCU->addConstantValue(VariableDIE, CI, isUnsignedDIType(GTy));
10632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  else if (const ConstantExpr *CE = getMergedGlobalExpr(N->getOperand(11))) {
10642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // GV is a merged global.
10652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
10662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    TheCU->addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
10672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    TheCU->addLabel(Block, 0, dwarf::DW_FORM_udata,
10682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                    Asm->Mang->getSymbol(cast<GlobalValue>(CE->getOperand(0))));
10692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ConstantInt *CII = cast<ConstantInt>(CE->getOperand(2));
10702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    TheCU->addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
10712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    TheCU->addUInt(Block, 0, dwarf::DW_FORM_udata, CII->getZExtValue());
10722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    TheCU->addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
10732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    TheCU->addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
10742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
10752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
10762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return;
1077f3ce491c3623b9924dccab3703a43e03157e988aTorne (Richard Coles)}
1078f3ce491c3623b9924dccab3703a43e03157e988aTorne (Richard Coles)
1079f3ce491c3623b9924dccab3703a43e03157e988aTorne (Richard Coles)/// construct SubprogramDIE - Construct subprogram DIE.
10802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void DwarfDebug::constructSubprogramDIE(const MDNode *N) {
10812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DISubprogram SP(N);
10822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
10832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Check for pre-existence.
10842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  CompileUnit *TheCU = getCompileUnit(N);
10852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (TheCU->getDIE(N))
1086f3ce491c3623b9924dccab3703a43e03157e988aTorne (Richard Coles)    return;
1087f3ce491c3623b9924dccab3703a43e03157e988aTorne (Richard Coles)
1088f3ce491c3623b9924dccab3703a43e03157e988aTorne (Richard Coles)  if (!SP.isDefinition())
10892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // This is a method declaration which will be handled while constructing
10902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // class type.
10912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
10922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
10932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DIE *SubprogramDie = createSubprogramDIE(SP);
10942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1095f3ce491c3623b9924dccab3703a43e03157e988aTorne (Richard Coles)  // Add to map.
10962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  TheCU->insertDIE(N, SubprogramDie);
10972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
10982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Add to context owner.
10992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  TheCU->addToContextOwner(SubprogramDie, SP.getContext());
11002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
11012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Expose as global.
11022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  TheCU->addGlobal(SP.getName(), SubprogramDie);
11032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
11042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return;
11052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
11062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
11072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// beginModule - Emit all Dwarf sections that should come prior to the
11082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// content. Create global DIEs and emit initial debug info sections.
11092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// This is inovked by the target AsmPrinter.
11102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void DwarfDebug::beginModule(Module *M) {
11112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (DisableDebugInfoPrinting)
11122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
11132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
11142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If module has named metadata anchors then use them, otherwise scan the module
11152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // using debug info finder to collect debug info.
11162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
11172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (CU_Nodes) {
11182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
11192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    NamedMDNode *GV_Nodes = M->getNamedMetadata("llvm.dbg.gv");
11205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NamedMDNode *SP_Nodes = M->getNamedMetadata("llvm.dbg.sp");
11215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (!GV_Nodes && !SP_Nodes)
11225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // If there are not any global variables or any functions then
11235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // there is not any debug info in this module.
11245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return;
11255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i)
11275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      constructCompileUnit(CU_Nodes->getOperand(i));
11285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (GV_Nodes)
11305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      for (unsigned i = 0, e = GV_Nodes->getNumOperands(); i != e; ++i)
11312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        constructGlobalVariableDIE(GV_Nodes->getOperand(i));
11325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (SP_Nodes)
11345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      for (unsigned i = 0, e = SP_Nodes->getNumOperands(); i != e; ++i)
11355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        constructSubprogramDIE(SP_Nodes->getOperand(i));
11365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
11385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DebugInfoFinder DbgFinder;
11405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DbgFinder.processModule(*M);
11415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool HasDebugInfo = false;
11435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Scan all the compile-units to see if there are any marked as the main unit.
11445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // if not, we do not generate debug info.
11455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
11465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)           E = DbgFinder.compile_unit_end(); I != E; ++I) {
11475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (DICompileUnit(*I).isMain()) {
11485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        HasDebugInfo = true;
11495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        break;
11505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
11515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
11525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (!HasDebugInfo) return;
11535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Create all the compile unit DIEs.
11555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
11565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)           E = DbgFinder.compile_unit_end(); I != E; ++I)
11575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      constructCompileUnit(*I);
11585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Create DIEs for each global variable.
11605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
11615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)           E = DbgFinder.global_variable_end(); I != E; ++I)
11625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      constructGlobalVariableDIE(*I);
11635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Create DIEs for each subprogram.
11652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
11662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)           E = DbgFinder.subprogram_end(); I != E; ++I)
11672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      constructSubprogramDIE(*I);
11682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
11692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
11702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Tell MMI that we have debug info.
11712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  MMI->setDebugInfoAvailability(true);
11722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
11732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Emit initial sections.
11742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EmitSectionLabels();
11752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
11762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //getOrCreateTypeDIE
11772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.enum"))
11782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
11792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      DIType Ty(NMD->getOperand(i));
11802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      getCompileUnit(Ty)->getOrCreateTypeDIE(Ty);
11812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
11822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
11832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.ty"))
11842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
11852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      DIType Ty(NMD->getOperand(i));
11865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      getCompileUnit(Ty)->getOrCreateTypeDIE(Ty);
1187eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    }
11885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Prime section data.
11905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SectionMap.insert(Asm->getObjFileLowering().getTextSection());
11915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
11925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1193eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch/// endModule - Emit all Dwarf sections that should come after the content.
11945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)///
11955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void DwarfDebug::endModule() {
11965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!FirstCU) return;
11975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const Module *M = MMI->getModule();
11985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DenseMap<const MDNode *, DbgScope *> DeadFnScopeMap;
11995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (NamedMDNode *AllSPs = M->getNamedMetadata("llvm.dbg.sp")) {
12005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    for (unsigned SI = 0, SE = AllSPs->getNumOperands(); SI != SE; ++SI) {
12012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      if (ProcessedSPNodes.count(AllSPs->getOperand(SI)) != 0) continue;
12022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      DISubprogram SP(AllSPs->getOperand(SI));
12035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (!SP.Verify()) continue;
12045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // Collect info for variables that were optimized out.
12062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      if (!SP.isDefinition()) continue;
12075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      StringRef FName = SP.getLinkageName();
12082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      if (FName.empty())
12092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        FName = SP.getName();
12105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      NamedMDNode *NMD = getFnSpecificMDNode(*(MMI->getModule()), FName);
12115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (!NMD) continue;
12125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      unsigned E = NMD->getNumOperands();
12135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (!E) continue;
12145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      DbgScope *Scope = new DbgScope(NULL, DIDescriptor(SP), NULL);
12152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      DeadFnScopeMap[SP] = Scope;
12162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      for (unsigned I = 0; I != E; ++I) {
12175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        DIVariable DV(NMD->getOperand(I));
12185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        if (!DV.Verify()) continue;
12192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        Scope->addVariable(new DbgVariable(DV));
12202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      }
12212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
12225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // Construct subprogram DIE and add variables DIEs.
12235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      constructSubprogramDIE(SP);
12245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      DIE *ScopeDIE = getCompileUnit(SP)->getDIE(SP);
12255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const SmallVector<DbgVariable *, 8> &Variables = Scope->getDbgVariables();
12265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
12275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        DIE *VariableDIE = constructVariableDIE(Variables[i], Scope);
12282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        if (VariableDIE)
12295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          ScopeDIE->addChild(VariableDIE);
12302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      }
12315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
12329fb6271fc125ce7a5aca36a00a3b1354d305c227Primiano Tucci  }
12339fb6271fc125ce7a5aca36a00a3b1354d305c227Primiano Tucci
12349fb6271fc125ce7a5aca36a00a3b1354d305c227Primiano Tucci  // Attach DW_AT_inline attribute with inlined subprogram DIEs.
12359fb6271fc125ce7a5aca36a00a3b1354d305c227Primiano Tucci  for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
12369fb6271fc125ce7a5aca36a00a3b1354d305c227Primiano Tucci         AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
12372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DIE *ISP = *AI;
12382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
12395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
12405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
12425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         CE = ContainingTypeMap.end(); CI != CE; ++CI) {
12435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DIE *SPDie = CI->first;
12445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const MDNode *N = dyn_cast_or_null<MDNode>(CI->second);
12455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (!N) continue;
12465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DIE *NDie = getCompileUnit(N)->getDIE(N);
12472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (!NDie) continue;
12485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    getCompileUnit(N)->addDIEEntry(SPDie, dwarf::DW_AT_containing_type,
12492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                   dwarf::DW_FORM_ref4, NDie);
12505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
12512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
12525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Standard sections final addresses.
12535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getTextSection());
12545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("text_end"));
12555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection());
12562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end"));
12572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
12582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // End text sections.
12592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
12602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    Asm->OutStreamer.SwitchSection(SectionMap[i]);
12612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", i));
12622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
12632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
12642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Compute DIE offsets and sizes.
12652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  computeSizeAndOffsets();
12662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
12672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Emit all the DIEs into a debug info section
12682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  emitDebugInfo();
1269a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
1270a031efa9dbd9b321781674193e8ac7636d47eb1dBen Murdoch  // Corresponding abbreviations into a abbrev section.
12712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  emitAbbreviations();
12722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
12732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Emit info into a debug pubnames section.
12742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  emitDebugPubNames();
12752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
12762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Emit info into a debug pubtypes section.
1277a031efa9dbd9b321781674193e8ac7636d47eb1dBen Murdoch  emitDebugPubTypes();
12782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
12792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Emit info into a debug loc section.
12802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  emitDebugLoc();
12812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
12822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Emit info into a debug aranges section.
12832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EmitDebugARanges();
1284a031efa9dbd9b321781674193e8ac7636d47eb1dBen Murdoch
12852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Emit info into a debug ranges section.
12862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  emitDebugRanges();
12872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
12882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Emit info into a debug macinfo section.
12892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  emitDebugMacInfo();
12902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
12912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Emit inline info.
12922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  emitDebugInlineInfo();
12932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
12942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Emit info into a debug str section.
12952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  emitDebugStr();
12962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
12972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // clean up.
12982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DeleteContainerSeconds(DeadFnScopeMap);
12992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
13002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)         E = CUMap.end(); I != E; ++I)
13012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    delete I->second;
13022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FirstCU = NULL;  // Reset for the next Module, if any.
13032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
13042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
13052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// findAbstractVariable - Find abstract variable, if any, associated with Var.
13062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &Var,
13072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                              DebugLoc ScopeLoc) {
13082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
13092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
13102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (AbsDbgVariable)
13112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return AbsDbgVariable;
13122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
13132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  LLVMContext &Ctx = Var->getContext();
13142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DbgScope *Scope = AbstractScopes.lookup(ScopeLoc.getScope(Ctx));
13152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!Scope)
13162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return NULL;
13172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
13182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  AbsDbgVariable = new DbgVariable(Var);
13192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Scope->addVariable(AbsDbgVariable);
13202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  AbstractVariables[Var] = AbsDbgVariable;
13212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return AbsDbgVariable;
13227dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}
1323bfb105516e12ad16ce5cefd7511e89e46ccd443eJonathan Dixon
13247dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch/// addCurrentFnArgument - If Var is an current function argument that add
13257dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch/// it in CurrentFnArguments list.
13267dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochbool DwarfDebug::addCurrentFnArgument(const MachineFunction *MF,
13277dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                                      DbgVariable *Var, DbgScope *Scope) {
13287dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (Scope != CurrentFnDbgScope)
13297dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    return false;
13307dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  DIVariable DV = Var->getVariable();
13317dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (DV.getTag() != dwarf::DW_TAG_arg_variable)
13327dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    return false;
13337dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  unsigned ArgNo = DV.getArgNumber();
13347dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (ArgNo == 0)
13357dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    return false;
13367dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
13377dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  size_t Size = CurrentFnArguments.size();
13387dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (Size == 0)
1339bfb105516e12ad16ce5cefd7511e89e46ccd443eJonathan Dixon    CurrentFnArguments.resize(MF->getFunction()->arg_size());
1340bfb105516e12ad16ce5cefd7511e89e46ccd443eJonathan Dixon  // llvm::Function argument size is not good indicator of how many
1341bfb105516e12ad16ce5cefd7511e89e46ccd443eJonathan Dixon  // arguments does the function have at source level.
1342bfb105516e12ad16ce5cefd7511e89e46ccd443eJonathan Dixon  if (ArgNo > Size)
1343bfb105516e12ad16ce5cefd7511e89e46ccd443eJonathan Dixon    CurrentFnArguments.resize(ArgNo * 2);
1344bfb105516e12ad16ce5cefd7511e89e46ccd443eJonathan Dixon  CurrentFnArguments[ArgNo - 1] = Var;
1345bfb105516e12ad16ce5cefd7511e89e46ccd443eJonathan Dixon  return true;
13462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
13472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
13482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// collectVariableInfoFromMMITable - Collect variable information from
13492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// side table maintained by MMI.
13502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void
13512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction * MF,
13522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                   SmallPtrSet<const MDNode *, 16> &Processed) {
13532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
13542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
1355558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch  for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
1356558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch         VE = VMap.end(); VI != VE; ++VI) {
13572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const MDNode *Var = VI->first;
1358558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch    if (!Var) continue;
13592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    Processed.insert(Var);
13602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DIVariable DV(Var);
13612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::pair<unsigned, DebugLoc> &VP = VI->second;
13622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
13632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DbgScope *Scope = 0;
13642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (const MDNode *IA = VP.second.getInlinedAt(Ctx))
13652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      Scope = ConcreteScopes.lookup(IA);
13662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (Scope == 0)
13672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      Scope = DbgScopeMap.lookup(VP.second.getScope(Ctx));
13682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1369bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch    // If variable scope is not found then skip this variable.
1370bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch    if (Scope == 0)
1371bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch      continue;
1372bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch
13732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
13742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DbgVariable *RegVar = new DbgVariable(DV);
13752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    recordVariableFrameIndex(RegVar, VP.first);
13762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (!addCurrentFnArgument(MF, RegVar, Scope))
13772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      Scope->addVariable(RegVar);
13782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (AbsDbgVariable) {
13792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      recordVariableFrameIndex(AbsDbgVariable, VP.first);
13802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      VarToAbstractVarMap[RegVar] = AbsDbgVariable;
13812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
13822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
13832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
13842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
13852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// isDbgValueInDefinedReg - Return true if debug value, encoded by
13862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// DBG_VALUE instruction, is in a defined reg.
13872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
13882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  assert (MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
13892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return MI->getNumOperands() == 3 &&
13902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)         MI->getOperand(0).isReg() && MI->getOperand(0).getReg() &&
13912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)         MI->getOperand(1).isImm() && MI->getOperand(1).getImm() == 0;
13922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
13932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
13942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// collectVariableInfo - Populate DbgScope entries with variables' info.
13952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void
13962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)DwarfDebug::collectVariableInfo(const MachineFunction *MF,
13972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                SmallPtrSet<const MDNode *, 16> &Processed) {
13982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
13997dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  /// collection info from MMI table.
14007dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  collectVariableInfoFromMMITable(MF, Processed);
14012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
14022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (SmallVectorImpl<const MDNode*>::const_iterator
14037c720b7466665b17575e0fd6976f9321c6bff489Torne (Richard Coles)         UVI = UserVariables.begin(), UVE = UserVariables.end(); UVI != UVE;
14047dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch         ++UVI) {
1405b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    const MDNode *Var = *UVI;
14062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (Processed.count(Var))
14072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      continue;
14082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
14092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // History contains relevant DBG_VALUE instructions for Var and instructions
14102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // clobbering it.
14112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
14122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (History.empty())
14132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      continue;
14142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const MachineInstr *MInsn = History.front();
14157dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
14163240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch    DIVariable DV(Var);
1417b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    DbgScope *Scope = NULL;
1418b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
1419b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)        DISubprogram(DV.getContext()).describes(MF->getFunction()))
1420b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      Scope = CurrentFnDbgScope;
14212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    else
14223240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch      Scope = findDbgScope(MInsn);
14233240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch    // If variable scope is not found then skip this variable.
14243240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch    if (!Scope)
14253240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch      continue;
14263240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch
14273240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch    Processed.insert(DV);
14283240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch    assert(MInsn->isDebugValue() && "History must begin with debug value");
14292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DbgVariable *RegVar = new DbgVariable(DV);
14302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (!addCurrentFnArgument(MF, RegVar, Scope))
14312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      Scope->addVariable(RegVar);
14322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc())) {
14332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      DbgVariableToDbgInstMap[AbsVar] = MInsn;
14342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      VarToAbstractVarMap[RegVar] = AbsVar;
1435b32343c9b70645bd3e272fe2a92d87e22bc2cca8Torne (Richard Coles)    }
14362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
14372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Simple ranges that are fully coalesced.
14382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (History.size() <= 1 || (History.size() == 2 &&
14392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                MInsn->isIdenticalTo(History.back()))) {
14402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      DbgVariableToDbgInstMap[RegVar] = MInsn;
14412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      continue;
144290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
1443cbc64c0be3b310fda39ba08409e70f94954e19c8Kristian Monsen
14442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // handle multiple DBG_VALUE instructions describing one variable.
14452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
14462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
14472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    for (SmallVectorImpl<const MachineInstr*>::const_iterator
14482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)           HI = History.begin(), HE = History.end(); HI != HE; ++HI) {
14492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const MachineInstr *Begin = *HI;
14502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      assert(Begin->isDebugValue() && "Invalid History entry");
14517dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      MachineLocation MLoc;
145258e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch      if (Begin->getNumOperands() == 3) {
1453f7d68f49f5a75246de7737294c928c1411bb64ffMartin Kosiba        if (Begin->getOperand(0).isReg() && Begin->getOperand(1).isImm())
1454fe6178dcfbd04cfd6b65eab8deb6f64cf0177454Martin Kosiba          MLoc.set(Begin->getOperand(0).getReg(), Begin->getOperand(1).getImm());
14552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      } else
14562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        MLoc = Asm->getDebugValueLocation(Begin);
14572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
14582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // FIXME: emitDebugLoc only understands registers.
14592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      if (!MLoc.getReg())
14602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        continue;
14612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1462f3ce491c3623b9924dccab3703a43e03157e988aTorne (Richard Coles)      // Compute the range for a register location.
1463f3ce491c3623b9924dccab3703a43e03157e988aTorne (Richard Coles)      const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
1464f3ce491c3623b9924dccab3703a43e03157e988aTorne (Richard Coles)      const MCSymbol *SLabel = 0;
14652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
14662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      if (HI + 1 == HE)
14672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        // If Begin is the last instruction in History then its value is valid
14682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        // until the end of the function.
14692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        SLabel = FunctionEndSym;
14702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      else {
1471f3ce491c3623b9924dccab3703a43e03157e988aTorne (Richard Coles)        const MachineInstr *End = HI[1];
1472f3ce491c3623b9924dccab3703a43e03157e988aTorne (Richard Coles)        if (End->isDebugValue())
1473f3ce491c3623b9924dccab3703a43e03157e988aTorne (Richard Coles)          SLabel = getLabelBeforeInsn(End);
1474eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        else {
1475eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          // End is a normal instruction clobbering the range.
1476f3ce491c3623b9924dccab3703a43e03157e988aTorne (Richard Coles)          SLabel = getLabelAfterInsn(End);
1477f3ce491c3623b9924dccab3703a43e03157e988aTorne (Richard Coles)          assert(SLabel && "Forgot label after clobber instruction");
1478f3ce491c3623b9924dccab3703a43e03157e988aTorne (Richard Coles)          ++HI;
1479f3ce491c3623b9924dccab3703a43e03157e988aTorne (Richard Coles)        }
14807dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      }
1481eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1482f3ce491c3623b9924dccab3703a43e03157e988aTorne (Richard Coles)      // The value is valid until the next DBG_VALUE or clobber.
1483f3ce491c3623b9924dccab3703a43e03157e988aTorne (Richard Coles)      DotDebugLocEntries.push_back(DotDebugLocEntry(FLabel, SLabel, MLoc, Var));
14842385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch    }
1485f3ce491c3623b9924dccab3703a43e03157e988aTorne (Richard Coles)    DotDebugLocEntries.push_back(DotDebugLocEntry());
14862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
14872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
14882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Collect info for variables that were optimized out.
14892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const Function *F = MF->getFunction();
14902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (NamedMDNode *NMD = getFnSpecificMDNode(*(F->getParent()), F->getName())) {
14912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
14922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      DIVariable DV(cast<MDNode>(NMD->getOperand(i)));
14932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      if (!DV || !Processed.insert(DV))
14942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        continue;
14952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      DbgScope *Scope = DbgScopeMap.lookup(DV.getContext());
14962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      if (Scope)
14972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        Scope->addVariable(new DbgVariable(DV));
14987c720b7466665b17575e0fd6976f9321c6bff489Torne (Richard Coles)    }
14992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
15002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
15012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
15022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// getLabelBeforeInsn - Return Label preceding the instruction.
15032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
15042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
15052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  assert(Label && "Didn't insert label before instruction");
15062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return Label;
15072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
15082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
15092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// getLabelAfterInsn - Return Label immediately following the instruction.
15102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
15112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return LabelsAfterInsn.lookup(MI);
15122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
15137c720b7466665b17575e0fd6976f9321c6bff489Torne (Richard Coles)
15142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// beginInstruction - Process beginning of an instruction.
15152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void DwarfDebug::beginInstruction(const MachineInstr *MI) {
15162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Check if source location changes, but ignore DBG_VALUE locations.
15172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!MI->isDebugValue()) {
15182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DebugLoc DL = MI->getDebugLoc();
15192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
15202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      PrevInstLoc = DL;
15212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      if (!DL.isUnknown()) {
15222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
15232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        recordSourceLine(DL.getLine(), DL.getCol(), Scope);
1524c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      } else
15252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        recordSourceLine(0, 0, 0);
15262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
15272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
15282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
15292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Insert labels where requested.
15302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
15312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    LabelsBeforeInsn.find(MI);
15322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
15332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // No label needed.
15342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (I == LabelsBeforeInsn.end())
15352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
15362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
15372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Label already assigned.
15382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (I->second)
15392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
15402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
15412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!PrevLabel) {
15422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    PrevLabel = MMI->getContext().CreateTempSymbol();
15432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    Asm->OutStreamer.EmitLabel(PrevLabel);
15442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1545eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  I->second = PrevLabel;
1546eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
1547eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1548eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch/// endInstruction - Process end of an instruction.
1549eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid DwarfDebug::endInstruction(const MachineInstr *MI) {
1550eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // Don't create a new label after DBG_VALUE instructions.
1551eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // They don't generate code.
1552eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (!MI->isDebugValue())
1553eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    PrevLabel = 0;
1554eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
15552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
15562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    LabelsAfterInsn.find(MI);
15572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
15582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // No label needed.
15592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (I == LabelsAfterInsn.end())
15602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
15612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
15622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Label already assigned.
15632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (I->second)
15642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
15652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
15662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // We need a label after this instruction.
15672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!PrevLabel) {
156890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    PrevLabel = MMI->getContext().CreateTempSymbol();
156990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    Asm->OutStreamer.EmitLabel(PrevLabel);
157090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
157190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  I->second = PrevLabel;
15722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
15732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
15742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// getOrCreateDbgScope - Create DbgScope for the scope.
15752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)DbgScope *DwarfDebug::getOrCreateDbgScope(const MDNode *Scope,
15762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                          const MDNode *InlinedAt) {
15772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!InlinedAt) {
15782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DbgScope *WScope = DbgScopeMap.lookup(Scope);
1579eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    if (WScope)
1580eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      return WScope;
1581eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    WScope = new DbgScope(NULL, DIDescriptor(Scope), NULL);
1582eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    DbgScopeMap.insert(std::make_pair(Scope, WScope));
1583eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    if (DIDescriptor(Scope).isLexicalBlock()) {
1584eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      DbgScope *Parent =
1585eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        getOrCreateDbgScope(DILexicalBlock(Scope).getContext(), NULL);
1586eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      WScope->setParent(Parent);
1587bfebf9c5f0206a13e1fdcf6343cc432a1d5fe776Torne (Richard Coles)      Parent->addScope(WScope);
1588bfebf9c5f0206a13e1fdcf6343cc432a1d5fe776Torne (Richard Coles)    }
1589bfebf9c5f0206a13e1fdcf6343cc432a1d5fe776Torne (Richard Coles)
1590bfebf9c5f0206a13e1fdcf6343cc432a1d5fe776Torne (Richard Coles)    if (!WScope->getParent()) {
1591bfebf9c5f0206a13e1fdcf6343cc432a1d5fe776Torne (Richard Coles)      StringRef SPName = DISubprogram(Scope).getLinkageName();
15925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // We used to check only for a linkage name, but that fails
15935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // since we began omitting the linkage name for private
15945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // functions.  The new way is to check for the name in metadata,
15955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // but that's not supported in old .ll test cases.  Ergo, we
15965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // check both.
15975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (SPName == Asm->MF->getFunction()->getName() ||
15985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          DISubprogram(Scope).getFunction() == Asm->MF->getFunction())
15995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        CurrentFnDbgScope = WScope;
16005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
16015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
16022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return WScope;
16032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
16042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
16052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  getOrCreateAbstractScope(Scope);
16062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DbgScope *WScope = DbgScopeMap.lookup(InlinedAt);
16072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (WScope)
16082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return WScope;
16092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
16102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  WScope = new DbgScope(NULL, DIDescriptor(Scope), InlinedAt);
16112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DbgScopeMap.insert(std::make_pair(InlinedAt, WScope));
16122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DILocation DL(InlinedAt);
16135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DbgScope *Parent =
16145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    getOrCreateDbgScope(DL.getScope(), DL.getOrigLocation());
16155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WScope->setParent(Parent);
16165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Parent->addScope(WScope);
16175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
16185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ConcreteScopes[InlinedAt] = WScope;
16195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
16205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return WScope;
16215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
16225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
16235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// hasValidLocation - Return true if debug location entry attached with
16245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// machine instruction encodes valid location info.
16255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static bool hasValidLocation(LLVMContext &Ctx,
16262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                             const MachineInstr *MInsn,
16272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                             const MDNode *&Scope, const MDNode *&InlinedAt) {
16282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DebugLoc DL = MInsn->getDebugLoc();
16292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (DL.isUnknown()) return false;
16302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
16312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const MDNode *S = DL.getScope(Ctx);
16322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
16332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // There is no need to create another DIE for compile unit. For all
16342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // other scopes, create one DbgScope now. This will be translated
16352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // into a scope DIE at the end.
16362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (DIScope(S).isCompileUnit()) return false;
16372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
16382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Scope = S;
16392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  InlinedAt = DL.getInlinedAt(Ctx);
16407c720b7466665b17575e0fd6976f9321c6bff489Torne (Richard Coles)  return true;
16412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
16422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
16432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// calculateDominanceGraph - Calculate dominance graph for DbgScope
16442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// hierarchy.
16452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static void calculateDominanceGraph(DbgScope *Scope) {
16462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  assert (Scope && "Unable to calculate scop edominance graph!");
16472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  SmallVector<DbgScope *, 4> WorkStack;
16482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  WorkStack.push_back(Scope);
16497c720b7466665b17575e0fd6976f9321c6bff489Torne (Richard Coles)  unsigned Counter = 0;
16502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  while (!WorkStack.empty()) {
16512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DbgScope *WS = WorkStack.back();
16522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const SmallVector<DbgScope *, 4> &Children = WS->getScopes();
16532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    bool visitedChildren = false;
16542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
16552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)           SE = Children.end(); SI != SE; ++SI) {
16562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      DbgScope *ChildScope = *SI;
16572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      if (!ChildScope->getDFSOut()) {
16582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        WorkStack.push_back(ChildScope);
16592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        visitedChildren = true;
16602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        ChildScope->setDFSIn(++Counter);
16612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        break;
16622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      }
16632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
16642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (!visitedChildren) {
16652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      WorkStack.pop_back();
16662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      WS->setDFSOut(++Counter);
16672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
16682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
16692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
16702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
16715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// printDbgScopeInfo - Print DbgScope info for each machine instruction.
16725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static
16735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void printDbgScopeInfo(LLVMContext &Ctx, const MachineFunction *MF,
16745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       DenseMap<const MachineInstr *, DbgScope *> &MI2ScopeMap)
16755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
16765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef NDEBUG
16775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  unsigned PrevDFSIn = 0;
1678b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
1679ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch       I != E; ++I) {
1680ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1681ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch         II != IE; ++II) {
1682ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      const MachineInstr *MInsn = II;
16832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const MDNode *Scope = NULL;
16842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const MDNode *InlinedAt = NULL;
16852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
16862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // Check if instruction has valid location information.
16872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      if (hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) {
16885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        dbgs() << " [ ";
16892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        if (InlinedAt)
16902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          dbgs() << "*";
16912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        DenseMap<const MachineInstr *, DbgScope *>::iterator DI =
16922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          MI2ScopeMap.find(MInsn);
16932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        if (DI != MI2ScopeMap.end()) {
16942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          DbgScope *S = DI->second;
16952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          dbgs() << S->getDFSIn();
16962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          PrevDFSIn = S->getDFSIn();
16977d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        } else
16987d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)          dbgs() << PrevDFSIn;
1699b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      } else
1700b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)        dbgs() << " [ x" << PrevDFSIn;
17017d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      dbgs() << " ]";
17027d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      MInsn->dump();
17037d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    }
1704b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    dbgs() << "\n";
17057d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
17067d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#endif
17077d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
17087d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)/// extractScopeInformation - Scan machine instructions in this function
17097d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)/// and collect DbgScopes. Return true, if at least one scope was found.
17107d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)bool DwarfDebug::extractScopeInformation() {
17112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If scope information was extracted using .dbg intrinsics then there is not
17122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // any need to extract these information by scanning each instruction.
17132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!DbgScopeMap.empty())
17142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return false;
17152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
17162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Scan each instruction and create scopes. First build working set of scopes.
17172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  LLVMContext &Ctx = Asm->MF->getFunction()->getContext();
17185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SmallVector<DbgRange, 4> MIRanges;
17195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DenseMap<const MachineInstr *, DbgScope *> MI2ScopeMap;
1720c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const MDNode *PrevScope = NULL;
17217dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  const MDNode *PrevInlinedAt = NULL;
1722c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const MachineInstr *RangeBeginMI = NULL;
17237dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  const MachineInstr *PrevMI = NULL;
1724c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end();
1725c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)       I != E; ++I) {
1726eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
17274d9f1c5c0093589a3f3f112641bf9c80442391a0Martin Kosiba         II != IE; ++II) {
17284d9f1c5c0093589a3f3f112641bf9c80442391a0Martin Kosiba      const MachineInstr *MInsn = II;
17294d9f1c5c0093589a3f3f112641bf9c80442391a0Martin Kosiba      const MDNode *Scope = NULL;
17304d9f1c5c0093589a3f3f112641bf9c80442391a0Martin Kosiba      const MDNode *InlinedAt = NULL;
17314d9f1c5c0093589a3f3f112641bf9c80442391a0Martin Kosiba
17324d9f1c5c0093589a3f3f112641bf9c80442391a0Martin Kosiba      // Check if instruction has valid location information.
173377ceb7188d76b2f05feb0f0b0ee08a2d7a181d16Selim Gurun      if (!hasValidLocation(Ctx, MInsn, Scope, InlinedAt)) {
173477ceb7188d76b2f05feb0f0b0ee08a2d7a181d16Selim Gurun        PrevMI = MInsn;
173577ceb7188d76b2f05feb0f0b0ee08a2d7a181d16Selim Gurun        continue;
173677ceb7188d76b2f05feb0f0b0ee08a2d7a181d16Selim Gurun      }
173777ceb7188d76b2f05feb0f0b0ee08a2d7a181d16Selim Gurun
1738eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      // If scope has not changed then skip this instruction.
1739eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      if (Scope == PrevScope && PrevInlinedAt == InlinedAt) {
1740eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        PrevMI = MInsn;
1741eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        continue;
1742eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      }
1743eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1744eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      // Ignore DBG_VALUE. It does not contribute any instruction in output.
1745a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      if (MInsn->isDebugValue())
1746eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        continue;
1747eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
17487dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      if (RangeBeginMI) {
17497dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        // If we have alread seen a beginning of a instruction range and
1750bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch        // current instruction scope does not match scope of first instruction
1751bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch        // in this range then create a new instruction range.
1752bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch        DbgRange R(RangeBeginMI, PrevMI);
1753bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch        MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope,
1754a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                                                        PrevInlinedAt);
1755bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch        MIRanges.push_back(R);
1756bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch      }
1757bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch
1758bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch      // This is a beginning of a new instruction range.
17597dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      RangeBeginMI = MInsn;
17607dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
17615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // Reset previous markers.
17625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      PrevMI = MInsn;
17635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      PrevScope = Scope;
17645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      PrevInlinedAt = InlinedAt;
17655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
17662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
17675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
17685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Create last instruction range.
17695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (RangeBeginMI && PrevMI && PrevScope) {
17705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DbgRange R(RangeBeginMI, PrevMI);
17715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    MIRanges.push_back(R);
17725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    MI2ScopeMap[RangeBeginMI] = getOrCreateDbgScope(PrevScope, PrevInlinedAt);
17735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
17745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
17755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!CurrentFnDbgScope)
17765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
17775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
17785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  calculateDominanceGraph(CurrentFnDbgScope);
17795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (PrintDbgScope)
17805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    printDbgScopeInfo(Ctx, Asm->MF, MI2ScopeMap);
17815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
17825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Find ranges of instructions covered by each DbgScope;
17835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DbgScope *PrevDbgScope = NULL;
17845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (SmallVector<DbgRange, 4>::const_iterator RI = MIRanges.begin(),
17855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         RE = MIRanges.end(); RI != RE; ++RI) {
17865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const DbgRange &R = *RI;
17875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DbgScope *S = MI2ScopeMap.lookup(R.first);
17885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    assert (S && "Lost DbgScope for a machine instruction!");
17895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (PrevDbgScope && !PrevDbgScope->dominates(S))
17905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      PrevDbgScope->closeInsnRange(S);
17915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    S->openInsnRange(R.first);
17925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    S->extendInsnRange(R.second);
17935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    PrevDbgScope = S;
17945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
17955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
17965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (PrevDbgScope)
17975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    PrevDbgScope->closeInsnRange();
17985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
17995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  identifyScopeMarkers();
18005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
18015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return !DbgScopeMap.empty();
18025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
18035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
18045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// identifyScopeMarkers() -
18055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// Each DbgScope has first instruction and last instruction to mark beginning
18065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// and end of a scope respectively. Create an inverse map that list scopes
18075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// starts (and ends) with an instruction. One instruction may start (or end)
18085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// multiple scopes. Ignore scopes that are not reachable.
18095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void DwarfDebug::identifyScopeMarkers() {
18105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SmallVector<DbgScope *, 4> WorkList;
18115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WorkList.push_back(CurrentFnDbgScope);
18125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  while (!WorkList.empty()) {
18135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DbgScope *S = WorkList.pop_back_val();
18145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
18155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const SmallVector<DbgScope *, 4> &Children = S->getScopes();
18167d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    if (!Children.empty())
18175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      for (SmallVector<DbgScope *, 4>::const_iterator SI = Children.begin(),
18182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)             SE = Children.end(); SI != SE; ++SI)
1819c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        WorkList.push_back(*SI);
18205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1821eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    if (S->isAbstractScope())
18227d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      continue;
18237d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
18247d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    const SmallVector<DbgRange, 4> &Ranges = S->getRanges();
18250a8f47fbce7f94803320e0340e8fe7855159e31aSelim Gurun    if (Ranges.empty())
18265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      continue;
18275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    for (SmallVector<DbgRange, 4>::const_iterator RI = Ranges.begin(),
18285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)           RE = Ranges.end(); RI != RE; ++RI) {
18295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      assert(RI->first && "DbgRange does not have first instruction!");
18305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      assert(RI->second && "DbgRange does not have second instruction!");
18315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      requestLabelBeforeInsn(RI->first);
18322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      requestLabelAfterInsn(RI->second);
18337d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    }
18347d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
1835a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
1836a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
1837a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)/// FindFirstDebugLoc - Find the first debug location in the function. This
18385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// is intended to be an approximation for the source position of the
18395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// beginning of the function.
18405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static DebugLoc FindFirstDebugLoc(const MachineFunction *MF) {
18415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
18425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       I != E; ++I)
18432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    for (MachineBasicBlock::const_iterator MBBI = I->begin(), MBBE = I->end();
18442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)         MBBI != MBBE; ++MBBI) {
18455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      DebugLoc DL = MBBI->getDebugLoc();
18462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      if (!DL.isUnknown())
18472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        return DL;
18482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
1849eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return DebugLoc();
1850f3ce491c3623b9924dccab3703a43e03157e988aTorne (Richard Coles)}
1851f3ce491c3623b9924dccab3703a43e03157e988aTorne (Richard Coles)
1852f3ce491c3623b9924dccab3703a43e03157e988aTorne (Richard Coles)/// beginFunction - Gather pre-function debug information.  Assumes being
18532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// emitted immediately after the function entry point.
18543240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdochvoid DwarfDebug::beginFunction(const MachineFunction *MF) {
1855eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (!MMI->hasDebugInfo()) return;
18567dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (!extractScopeInformation()) return;
18577dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
1858fe6178dcfbd04cfd6b65eab8deb6f64cf0177454Martin Kosiba  FunctionBeginSym = Asm->GetTempSymbol("func_begin",
18592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                        Asm->getFunctionNumber());
18602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Assumes in correct section after the entry point.
18612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Asm->OutStreamer.EmitLabel(FunctionBeginSym);
18622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
18632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Emit label for the implicitly defined dbg.stoppoint at the start of the
18642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // function.
18652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DebugLoc FDL = FindFirstDebugLoc(MF);
18667d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (FDL.isUnknown()) return;
18672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1868a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch  const MDNode *Scope = FDL.getScope(MF->getFunction()->getContext());
18692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const MDNode *TheScope = 0;
18702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
18714311e82a78ceafbe0585f51d4c8a86df9f21aa0dBen Murdoch  DISubprogram SP = getDISubprogram(Scope);
1872b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  unsigned Line, Col;
18732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (SP.Verify()) {
18742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    Line = SP.getLineNumber();
18752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    Col = 0;
1876bfebf9c5f0206a13e1fdcf6343cc432a1d5fe776Torne (Richard Coles)    TheScope = SP;
1877bfebf9c5f0206a13e1fdcf6343cc432a1d5fe776Torne (Richard Coles)  } else {
18785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Line = FDL.getLine();
1879    Col = FDL.getCol();
1880    TheScope = Scope;
1881  }
1882
1883  recordSourceLine(Line, Col, TheScope);
1884
1885  assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
1886
1887  /// ProcessedArgs - Collection of arguments already processed.
1888  SmallPtrSet<const MDNode *, 8> ProcessedArgs;
1889
1890  const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
1891
1892  /// LiveUserVar - Map physreg numbers to the MDNode they contain.
1893  std::vector<const MDNode*> LiveUserVar(TRI->getNumRegs());
1894
1895  for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
1896       I != E; ++I) {
1897    bool AtBlockEntry = true;
1898    for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1899         II != IE; ++II) {
1900      const MachineInstr *MI = II;
1901
1902      if (MI->isDebugValue()) {
1903        assert (MI->getNumOperands() > 1 && "Invalid machine instruction!");
1904
1905        // Keep track of user variables.
1906        const MDNode *Var =
1907          MI->getOperand(MI->getNumOperands() - 1).getMetadata();
1908
1909        // Variable is in a register, we need to check for clobbers.
1910        if (isDbgValueInDefinedReg(MI))
1911          LiveUserVar[MI->getOperand(0).getReg()] = Var;
1912
1913        // Check the history of this variable.
1914        SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1915        if (History.empty()) {
1916          UserVariables.push_back(Var);
1917          // The first mention of a function argument gets the FunctionBeginSym
1918          // label, so arguments are visible when breaking at function entry.
1919          DIVariable DV(Var);
1920          if (DV.Verify() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
1921              DISubprogram(getDISubprogram(DV.getContext()))
1922                .describes(MF->getFunction()))
1923            LabelsBeforeInsn[MI] = FunctionBeginSym;
1924        } else {
1925          // We have seen this variable before. Try to coalesce DBG_VALUEs.
1926          const MachineInstr *Prev = History.back();
1927          if (Prev->isDebugValue()) {
1928            // Coalesce identical entries at the end of History.
1929            if (History.size() >= 2 &&
1930                Prev->isIdenticalTo(History[History.size() - 2]))
1931              History.pop_back();
1932
1933            // Terminate old register assignments that don't reach MI;
1934            MachineFunction::const_iterator PrevMBB = Prev->getParent();
1935            if (PrevMBB != I && (!AtBlockEntry || llvm::next(PrevMBB) != I) &&
1936                isDbgValueInDefinedReg(Prev)) {
1937              // Previous register assignment needs to terminate at the end of
1938              // its basic block.
1939              MachineBasicBlock::const_iterator LastMI =
1940                PrevMBB->getLastNonDebugInstr();
1941              if (LastMI == PrevMBB->end())
1942                // Drop DBG_VALUE for empty range.
1943                History.pop_back();
1944              else {
1945                // Terminate after LastMI.
1946                History.push_back(LastMI);
1947              }
1948            }
1949          }
1950        }
1951        History.push_back(MI);
1952      } else {
1953        // Not a DBG_VALUE instruction.
1954        if (!MI->isLabel())
1955          AtBlockEntry = false;
1956
1957        // Check if the instruction clobbers any registers with debug vars.
1958        for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1959               MOE = MI->operands_end(); MOI != MOE; ++MOI) {
1960          if (!MOI->isReg() || !MOI->isDef() || !MOI->getReg())
1961            continue;
1962          for (const unsigned *AI = TRI->getOverlaps(MOI->getReg());
1963               unsigned Reg = *AI; ++AI) {
1964            const MDNode *Var = LiveUserVar[Reg];
1965            if (!Var)
1966              continue;
1967            // Reg is now clobbered.
1968            LiveUserVar[Reg] = 0;
1969
1970            // Was MD last defined by a DBG_VALUE referring to Reg?
1971            DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
1972            if (HistI == DbgValues.end())
1973              continue;
1974            SmallVectorImpl<const MachineInstr*> &History = HistI->second;
1975            if (History.empty())
1976              continue;
1977            const MachineInstr *Prev = History.back();
1978            // Sanity-check: Register assignments are terminated at the end of
1979            // their block.
1980            if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent())
1981              continue;
1982            // Is the variable still in Reg?
1983            if (!isDbgValueInDefinedReg(Prev) ||
1984                Prev->getOperand(0).getReg() != Reg)
1985              continue;
1986            // Var is clobbered. Make sure the next instruction gets a label.
1987            History.push_back(MI);
1988          }
1989        }
1990      }
1991    }
1992  }
1993
1994  for (DbgValueHistoryMap::iterator I = DbgValues.begin(), E = DbgValues.end();
1995       I != E; ++I) {
1996    SmallVectorImpl<const MachineInstr*> &History = I->second;
1997    if (History.empty())
1998      continue;
1999
2000    // Make sure the final register assignments are terminated.
2001    const MachineInstr *Prev = History.back();
2002    if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
2003      const MachineBasicBlock *PrevMBB = Prev->getParent();
2004      MachineBasicBlock::const_iterator LastMI = PrevMBB->getLastNonDebugInstr();
2005      if (LastMI == PrevMBB->end())
2006        // Drop DBG_VALUE for empty range.
2007        History.pop_back();
2008      else {
2009        // Terminate after LastMI.
2010        History.push_back(LastMI);
2011      }
2012    }
2013    // Request labels for the full history.
2014    for (unsigned i = 0, e = History.size(); i != e; ++i) {
2015      const MachineInstr *MI = History[i];
2016      if (MI->isDebugValue())
2017        requestLabelBeforeInsn(MI);
2018      else
2019        requestLabelAfterInsn(MI);
2020    }
2021  }
2022
2023  PrevInstLoc = DebugLoc();
2024  PrevLabel = FunctionBeginSym;
2025}
2026
2027/// endFunction - Gather and emit post-function debug information.
2028///
2029void DwarfDebug::endFunction(const MachineFunction *MF) {
2030  if (!MMI->hasDebugInfo() || DbgScopeMap.empty()) return;
2031
2032  if (CurrentFnDbgScope) {
2033
2034    // Define end label for subprogram.
2035    FunctionEndSym = Asm->GetTempSymbol("func_end",
2036                                        Asm->getFunctionNumber());
2037    // Assumes in correct section after the entry point.
2038    Asm->OutStreamer.EmitLabel(FunctionEndSym);
2039
2040    SmallPtrSet<const MDNode *, 16> ProcessedVars;
2041    collectVariableInfo(MF, ProcessedVars);
2042
2043    // Construct abstract scopes.
2044    for (SmallVector<DbgScope *, 4>::iterator AI = AbstractScopesList.begin(),
2045           AE = AbstractScopesList.end(); AI != AE; ++AI) {
2046      DISubprogram SP((*AI)->getScopeNode());
2047      if (SP.Verify()) {
2048        // Collect info for variables that were optimized out.
2049        StringRef FName = SP.getLinkageName();
2050        if (FName.empty())
2051          FName = SP.getName();
2052        if (NamedMDNode *NMD =
2053            getFnSpecificMDNode(*(MF->getFunction()->getParent()), FName)) {
2054          for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
2055          DIVariable DV(cast<MDNode>(NMD->getOperand(i)));
2056          if (!DV || !ProcessedVars.insert(DV))
2057            continue;
2058          DbgScope *Scope = AbstractScopes.lookup(DV.getContext());
2059          if (Scope)
2060            Scope->addVariable(new DbgVariable(DV));
2061          }
2062        }
2063      }
2064      if (ProcessedSPNodes.count((*AI)->getScopeNode()) == 0)
2065        constructScopeDIE(*AI);
2066    }
2067
2068    DIE *CurFnDIE = constructScopeDIE(CurrentFnDbgScope);
2069
2070    if (!DisableFramePointerElim(*MF))
2071      getCompileUnit(CurrentFnDbgScope->getScopeNode())->addUInt(CurFnDIE,
2072                                                                 dwarf::DW_AT_APPLE_omit_frame_ptr,
2073                                                                 dwarf::DW_FORM_flag, 1);
2074
2075
2076    DebugFrames.push_back(FunctionDebugFrameInfo(Asm->getFunctionNumber(),
2077                                                 MMI->getFrameMoves()));
2078  }
2079
2080  // Clear debug info
2081  CurrentFnDbgScope = NULL;
2082  DeleteContainerPointers(CurrentFnArguments);
2083  DbgVariableToFrameIndexMap.clear();
2084  VarToAbstractVarMap.clear();
2085  DbgVariableToDbgInstMap.clear();
2086  DeleteContainerSeconds(DbgScopeMap);
2087  UserVariables.clear();
2088  DbgValues.clear();
2089  ConcreteScopes.clear();
2090  DeleteContainerSeconds(AbstractScopes);
2091  AbstractScopesList.clear();
2092  AbstractVariables.clear();
2093  LabelsBeforeInsn.clear();
2094  LabelsAfterInsn.clear();
2095  PrevLabel = NULL;
2096}
2097
2098/// recordVariableFrameIndex - Record a variable's index.
2099void DwarfDebug::recordVariableFrameIndex(const DbgVariable *V, int Index) {
2100  assert (V && "Invalid DbgVariable!");
2101  DbgVariableToFrameIndexMap[V] = Index;
2102}
2103
2104/// findVariableFrameIndex - Return true if frame index for the variable
2105/// is found. Update FI to hold value of the index.
2106bool DwarfDebug::findVariableFrameIndex(const DbgVariable *V, int *FI) {
2107  assert (V && "Invalid DbgVariable!");
2108  DenseMap<const DbgVariable *, int>::iterator I =
2109    DbgVariableToFrameIndexMap.find(V);
2110  if (I == DbgVariableToFrameIndexMap.end())
2111    return false;
2112  *FI = I->second;
2113  return true;
2114}
2115
2116/// findDbgScope - Find DbgScope for the debug loc attached with an
2117/// instruction.
2118DbgScope *DwarfDebug::findDbgScope(const MachineInstr *MInsn) {
2119  DbgScope *Scope = NULL;
2120  LLVMContext &Ctx =
2121    MInsn->getParent()->getParent()->getFunction()->getContext();
2122  DebugLoc DL = MInsn->getDebugLoc();
2123
2124  if (DL.isUnknown())
2125    return Scope;
2126
2127  if (const MDNode *IA = DL.getInlinedAt(Ctx))
2128    Scope = ConcreteScopes.lookup(IA);
2129  if (Scope == 0)
2130    Scope = DbgScopeMap.lookup(DL.getScope(Ctx));
2131
2132  return Scope;
2133}
2134
2135
2136/// recordSourceLine - Register a source line with debug info. Returns the
2137/// unique label that was emitted and which provides correspondence to
2138/// the source line list.
2139void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S){
2140  StringRef Fn;
2141  StringRef Dir;
2142  unsigned Src = 1;
2143  if (S) {
2144    DIDescriptor Scope(S);
2145
2146    if (Scope.isCompileUnit()) {
2147      DICompileUnit CU(S);
2148      Fn = CU.getFilename();
2149      Dir = CU.getDirectory();
2150    } else if (Scope.isFile()) {
2151      DIFile F(S);
2152      Fn = F.getFilename();
2153      Dir = F.getDirectory();
2154    } else if (Scope.isSubprogram()) {
2155      DISubprogram SP(S);
2156      Fn = SP.getFilename();
2157      Dir = SP.getDirectory();
2158    } else if (Scope.isLexicalBlock()) {
2159      DILexicalBlock DB(S);
2160      Fn = DB.getFilename();
2161      Dir = DB.getDirectory();
2162    } else
2163      assert(0 && "Unexpected scope info");
2164
2165    Src = GetOrCreateSourceID(Fn, Dir);
2166  }
2167  Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, DWARF2_FLAG_IS_STMT,
2168                                         0, 0, Fn);
2169}
2170
2171//===----------------------------------------------------------------------===//
2172// Emit Methods
2173//===----------------------------------------------------------------------===//
2174
2175/// computeSizeAndOffset - Compute the size and offset of a DIE.
2176///
2177unsigned
2178DwarfDebug::computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last) {
2179  // Get the children.
2180  const std::vector<DIE *> &Children = Die->getChildren();
2181
2182  // If not last sibling and has children then add sibling offset attribute.
2183  if (!Last && !Children.empty())
2184    Die->addSiblingOffset(DIEValueAllocator);
2185
2186  // Record the abbreviation.
2187  assignAbbrevNumber(Die->getAbbrev());
2188
2189  // Get the abbreviation for this DIE.
2190  unsigned AbbrevNumber = Die->getAbbrevNumber();
2191  const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2192
2193  // Set DIE offset
2194  Die->setOffset(Offset);
2195
2196  // Start the size with the size of abbreviation code.
2197  Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
2198
2199  const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2200  const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2201
2202  // Size the DIE attribute values.
2203  for (unsigned i = 0, N = Values.size(); i < N; ++i)
2204    // Size attribute value.
2205    Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
2206
2207  // Size the DIE children if any.
2208  if (!Children.empty()) {
2209    assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
2210           "Children flag not set");
2211
2212    for (unsigned j = 0, M = Children.size(); j < M; ++j)
2213      Offset = computeSizeAndOffset(Children[j], Offset, (j + 1) == M);
2214
2215    // End of children marker.
2216    Offset += sizeof(int8_t);
2217  }
2218
2219  Die->setSize(Offset - Die->getOffset());
2220  return Offset;
2221}
2222
2223/// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
2224///
2225void DwarfDebug::computeSizeAndOffsets() {
2226  for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2227         E = CUMap.end(); I != E; ++I) {
2228    // Compute size of compile unit header.
2229    unsigned Offset =
2230      sizeof(int32_t) + // Length of Compilation Unit Info
2231      sizeof(int16_t) + // DWARF version number
2232      sizeof(int32_t) + // Offset Into Abbrev. Section
2233      sizeof(int8_t);   // Pointer Size (in bytes)
2234    computeSizeAndOffset(I->second->getCUDie(), Offset, true);
2235  }
2236}
2237
2238/// EmitSectionSym - Switch to the specified MCSection and emit an assembler
2239/// temporary label to it if SymbolStem is specified.
2240static MCSymbol *EmitSectionSym(AsmPrinter *Asm, const MCSection *Section,
2241                                const char *SymbolStem = 0) {
2242  Asm->OutStreamer.SwitchSection(Section);
2243  if (!SymbolStem) return 0;
2244
2245  MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
2246  Asm->OutStreamer.EmitLabel(TmpSym);
2247  return TmpSym;
2248}
2249
2250/// EmitSectionLabels - Emit initial Dwarf sections with a label at
2251/// the start of each one.
2252void DwarfDebug::EmitSectionLabels() {
2253  const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
2254
2255  // Dwarf sections base addresses.
2256  DwarfInfoSectionSym =
2257    EmitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
2258  DwarfAbbrevSectionSym =
2259    EmitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
2260  EmitSectionSym(Asm, TLOF.getDwarfARangesSection());
2261
2262  if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
2263    EmitSectionSym(Asm, MacroInfo);
2264
2265  EmitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
2266  EmitSectionSym(Asm, TLOF.getDwarfLocSection());
2267  EmitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
2268  EmitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
2269  DwarfStrSectionSym =
2270    EmitSectionSym(Asm, TLOF.getDwarfStrSection(), "section_str");
2271  DwarfDebugRangeSectionSym = EmitSectionSym(Asm, TLOF.getDwarfRangesSection(),
2272                                             "debug_range");
2273
2274  DwarfDebugLocSectionSym = EmitSectionSym(Asm, TLOF.getDwarfLocSection(),
2275                                           "section_debug_loc");
2276
2277  TextSectionSym = EmitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
2278  EmitSectionSym(Asm, TLOF.getDataSection());
2279}
2280
2281/// emitDIE - Recusively Emits a debug information entry.
2282///
2283void DwarfDebug::emitDIE(DIE *Die) {
2284  // Get the abbreviation for this DIE.
2285  unsigned AbbrevNumber = Die->getAbbrevNumber();
2286  const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2287
2288  // Emit the code (index) for the abbreviation.
2289  if (Asm->isVerbose())
2290    Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
2291                                Twine::utohexstr(Die->getOffset()) + ":0x" +
2292                                Twine::utohexstr(Die->getSize()) + " " +
2293                                dwarf::TagString(Abbrev->getTag()));
2294  Asm->EmitULEB128(AbbrevNumber);
2295
2296  const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2297  const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2298
2299  // Emit the DIE attribute values.
2300  for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2301    unsigned Attr = AbbrevData[i].getAttribute();
2302    unsigned Form = AbbrevData[i].getForm();
2303    assert(Form && "Too many attributes for DIE (check abbreviation)");
2304
2305    if (Asm->isVerbose())
2306      Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
2307
2308    switch (Attr) {
2309    case dwarf::DW_AT_sibling:
2310      Asm->EmitInt32(Die->getSiblingOffset());
2311      break;
2312    case dwarf::DW_AT_abstract_origin: {
2313      DIEEntry *E = cast<DIEEntry>(Values[i]);
2314      DIE *Origin = E->getEntry();
2315      unsigned Addr = Origin->getOffset();
2316      Asm->EmitInt32(Addr);
2317      break;
2318    }
2319    case dwarf::DW_AT_ranges: {
2320      // DW_AT_range Value encodes offset in debug_range section.
2321      DIEInteger *V = cast<DIEInteger>(Values[i]);
2322
2323      if (Asm->MAI->doesDwarfUsesLabelOffsetForRanges()) {
2324        Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym,
2325                                 V->getValue(),
2326                                 4);
2327      } else {
2328        Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
2329                                       V->getValue(),
2330                                       DwarfDebugRangeSectionSym,
2331                                       4);
2332      }
2333      break;
2334    }
2335    case dwarf::DW_AT_location: {
2336      if (UseDotDebugLocEntry.count(Die) != 0) {
2337        DIELabel *L = cast<DIELabel>(Values[i]);
2338        Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
2339      } else
2340        Values[i]->EmitValue(Asm, Form);
2341      break;
2342    }
2343    case dwarf::DW_AT_accessibility: {
2344      if (Asm->isVerbose()) {
2345        DIEInteger *V = cast<DIEInteger>(Values[i]);
2346        Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
2347      }
2348      Values[i]->EmitValue(Asm, Form);
2349      break;
2350    }
2351    default:
2352      // Emit an attribute using the defined form.
2353      Values[i]->EmitValue(Asm, Form);
2354      break;
2355    }
2356  }
2357
2358  // Emit the DIE children if any.
2359  if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
2360    const std::vector<DIE *> &Children = Die->getChildren();
2361
2362    for (unsigned j = 0, M = Children.size(); j < M; ++j)
2363      emitDIE(Children[j]);
2364
2365    if (Asm->isVerbose())
2366      Asm->OutStreamer.AddComment("End Of Children Mark");
2367    Asm->EmitInt8(0);
2368  }
2369}
2370
2371/// emitDebugInfo - Emit the debug info section.
2372///
2373void DwarfDebug::emitDebugInfo() {
2374  // Start debug info section.
2375  Asm->OutStreamer.SwitchSection(
2376                            Asm->getObjFileLowering().getDwarfInfoSection());
2377  for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2378         E = CUMap.end(); I != E; ++I) {
2379    CompileUnit *TheCU = I->second;
2380    DIE *Die = TheCU->getCUDie();
2381
2382    // Emit the compile units header.
2383    Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_begin",
2384                                                  TheCU->getID()));
2385
2386    // Emit size of content not including length itself
2387    unsigned ContentSize = Die->getSize() +
2388      sizeof(int16_t) + // DWARF version number
2389      sizeof(int32_t) + // Offset Into Abbrev. Section
2390      sizeof(int8_t);   // Pointer Size (in bytes)
2391
2392    Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
2393    Asm->EmitInt32(ContentSize);
2394    Asm->OutStreamer.AddComment("DWARF version number");
2395    Asm->EmitInt16(dwarf::DWARF_VERSION);
2396    Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
2397    Asm->EmitSectionOffset(Asm->GetTempSymbol("abbrev_begin"),
2398                           DwarfAbbrevSectionSym);
2399    Asm->OutStreamer.AddComment("Address Size (in bytes)");
2400    Asm->EmitInt8(Asm->getTargetData().getPointerSize());
2401
2402    emitDIE(Die);
2403    Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("info_end", TheCU->getID()));
2404  }
2405}
2406
2407/// emitAbbreviations - Emit the abbreviation section.
2408///
2409void DwarfDebug::emitAbbreviations() const {
2410  // Check to see if it is worth the effort.
2411  if (!Abbreviations.empty()) {
2412    // Start the debug abbrev section.
2413    Asm->OutStreamer.SwitchSection(
2414                            Asm->getObjFileLowering().getDwarfAbbrevSection());
2415
2416    Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_begin"));
2417
2418    // For each abbrevation.
2419    for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2420      // Get abbreviation data
2421      const DIEAbbrev *Abbrev = Abbreviations[i];
2422
2423      // Emit the abbrevations code (base 1 index.)
2424      Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
2425
2426      // Emit the abbreviations data.
2427      Abbrev->Emit(Asm);
2428    }
2429
2430    // Mark end of abbreviations.
2431    Asm->EmitULEB128(0, "EOM(3)");
2432
2433    Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("abbrev_end"));
2434  }
2435}
2436
2437/// emitEndOfLineMatrix - Emit the last address of the section and the end of
2438/// the line matrix.
2439///
2440void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
2441  // Define last address of section.
2442  Asm->OutStreamer.AddComment("Extended Op");
2443  Asm->EmitInt8(0);
2444
2445  Asm->OutStreamer.AddComment("Op size");
2446  Asm->EmitInt8(Asm->getTargetData().getPointerSize() + 1);
2447  Asm->OutStreamer.AddComment("DW_LNE_set_address");
2448  Asm->EmitInt8(dwarf::DW_LNE_set_address);
2449
2450  Asm->OutStreamer.AddComment("Section end label");
2451
2452  Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
2453                                   Asm->getTargetData().getPointerSize(),
2454                                   0/*AddrSpace*/);
2455
2456  // Mark end of matrix.
2457  Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
2458  Asm->EmitInt8(0);
2459  Asm->EmitInt8(1);
2460  Asm->EmitInt8(1);
2461}
2462
2463/// emitDebugPubNames - Emit visible names into a debug pubnames section.
2464///
2465void DwarfDebug::emitDebugPubNames() {
2466  for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2467         E = CUMap.end(); I != E; ++I) {
2468    CompileUnit *TheCU = I->second;
2469    // Start the dwarf pubnames section.
2470    Asm->OutStreamer.SwitchSection(
2471      Asm->getObjFileLowering().getDwarfPubNamesSection());
2472
2473    Asm->OutStreamer.AddComment("Length of Public Names Info");
2474    Asm->EmitLabelDifference(
2475      Asm->GetTempSymbol("pubnames_end", TheCU->getID()),
2476      Asm->GetTempSymbol("pubnames_begin", TheCU->getID()), 4);
2477
2478    Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin",
2479                                                  TheCU->getID()));
2480
2481    Asm->OutStreamer.AddComment("DWARF Version");
2482    Asm->EmitInt16(dwarf::DWARF_VERSION);
2483
2484    Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
2485    Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
2486                           DwarfInfoSectionSym);
2487
2488    Asm->OutStreamer.AddComment("Compilation Unit Length");
2489    Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
2490                             Asm->GetTempSymbol("info_begin", TheCU->getID()),
2491                             4);
2492
2493    const StringMap<DIE*> &Globals = TheCU->getGlobals();
2494    for (StringMap<DIE*>::const_iterator
2495           GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2496      const char *Name = GI->getKeyData();
2497      DIE *Entity = GI->second;
2498
2499      Asm->OutStreamer.AddComment("DIE offset");
2500      Asm->EmitInt32(Entity->getOffset());
2501
2502      if (Asm->isVerbose())
2503        Asm->OutStreamer.AddComment("External Name");
2504      Asm->OutStreamer.EmitBytes(StringRef(Name, strlen(Name)+1), 0);
2505    }
2506
2507    Asm->OutStreamer.AddComment("End Mark");
2508    Asm->EmitInt32(0);
2509    Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end",
2510                                                TheCU->getID()));
2511  }
2512}
2513
2514void DwarfDebug::emitDebugPubTypes() {
2515  for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2516         E = CUMap.end(); I != E; ++I) {
2517    CompileUnit *TheCU = I->second;
2518    // Start the dwarf pubnames section.
2519    Asm->OutStreamer.SwitchSection(
2520      Asm->getObjFileLowering().getDwarfPubTypesSection());
2521    Asm->OutStreamer.AddComment("Length of Public Types Info");
2522    Asm->EmitLabelDifference(
2523      Asm->GetTempSymbol("pubtypes_end", TheCU->getID()),
2524      Asm->GetTempSymbol("pubtypes_begin", TheCU->getID()), 4);
2525
2526    Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_begin",
2527                                                  TheCU->getID()));
2528
2529    if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DWARF Version");
2530    Asm->EmitInt16(dwarf::DWARF_VERSION);
2531
2532    Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
2533    Asm->EmitSectionOffset(Asm->GetTempSymbol("info_begin", TheCU->getID()),
2534                           DwarfInfoSectionSym);
2535
2536    Asm->OutStreamer.AddComment("Compilation Unit Length");
2537    Asm->EmitLabelDifference(Asm->GetTempSymbol("info_end", TheCU->getID()),
2538                             Asm->GetTempSymbol("info_begin", TheCU->getID()),
2539                             4);
2540
2541    const StringMap<DIE*> &Globals = TheCU->getGlobalTypes();
2542    for (StringMap<DIE*>::const_iterator
2543           GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2544      const char *Name = GI->getKeyData();
2545      DIE * Entity = GI->second;
2546
2547      if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
2548      Asm->EmitInt32(Entity->getOffset());
2549
2550      if (Asm->isVerbose()) Asm->OutStreamer.AddComment("External Name");
2551      Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1), 0);
2552    }
2553
2554    Asm->OutStreamer.AddComment("End Mark");
2555    Asm->EmitInt32(0);
2556    Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubtypes_end",
2557                                                  TheCU->getID()));
2558  }
2559}
2560
2561/// emitDebugStr - Emit visible names into a debug str section.
2562///
2563void DwarfDebug::emitDebugStr() {
2564  // Check to see if it is worth the effort.
2565  if (StringPool.empty()) return;
2566
2567  // Start the dwarf str section.
2568  Asm->OutStreamer.SwitchSection(
2569                                Asm->getObjFileLowering().getDwarfStrSection());
2570
2571  // Get all of the string pool entries and put them in an array by their ID so
2572  // we can sort them.
2573  SmallVector<std::pair<unsigned,
2574      StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
2575
2576  for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
2577       I = StringPool.begin(), E = StringPool.end(); I != E; ++I)
2578    Entries.push_back(std::make_pair(I->second.second, &*I));
2579
2580  array_pod_sort(Entries.begin(), Entries.end());
2581
2582  for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
2583    // Emit a label for reference from debug information entries.
2584    Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
2585
2586    // Emit the string itself.
2587    Asm->OutStreamer.EmitBytes(Entries[i].second->getKey(), 0/*addrspace*/);
2588  }
2589}
2590
2591/// emitDebugLoc - Emit visible names into a debug loc section.
2592///
2593void DwarfDebug::emitDebugLoc() {
2594  if (DotDebugLocEntries.empty())
2595    return;
2596
2597  for (SmallVector<DotDebugLocEntry, 4>::iterator
2598         I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
2599       I != E; ++I) {
2600    DotDebugLocEntry &Entry = *I;
2601    if (I + 1 != DotDebugLocEntries.end())
2602      Entry.Merge(I+1);
2603  }
2604
2605  // Start the dwarf loc section.
2606  Asm->OutStreamer.SwitchSection(
2607    Asm->getObjFileLowering().getDwarfLocSection());
2608  unsigned char Size = Asm->getTargetData().getPointerSize();
2609  Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
2610  unsigned index = 1;
2611  for (SmallVector<DotDebugLocEntry, 4>::iterator
2612         I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
2613       I != E; ++I, ++index) {
2614    DotDebugLocEntry &Entry = *I;
2615    if (Entry.isMerged()) continue;
2616    if (Entry.isEmpty()) {
2617      Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
2618      Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
2619      Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
2620    } else {
2621      Asm->OutStreamer.EmitSymbolValue(Entry.Begin, Size, 0);
2622      Asm->OutStreamer.EmitSymbolValue(Entry.End, Size, 0);
2623      DIVariable DV(Entry.Variable);
2624      if (DV.hasComplexAddress()) {
2625        unsigned N = DV.getNumAddrElements();
2626        unsigned i = 0;
2627        Asm->OutStreamer.AddComment("Loc expr size");
2628        if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
2629          // If first address element is OpPlus then emit
2630          // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
2631          MachineLocation Loc(Entry.Loc.getReg(), DV.getAddrElement(1));
2632          Asm->EmitInt16(Asm->getDwarfRegOpSize(Loc) + N - 2);
2633          Asm->EmitDwarfRegOp(Loc);
2634//          Asm->EmitULEB128(DV.getAddrElement(1));
2635          i = 2;
2636        } else {
2637          Asm->EmitInt16(Asm->getDwarfRegOpSize(Entry.Loc) + N);
2638          Asm->EmitDwarfRegOp(Entry.Loc);
2639        }
2640
2641        // Emit remaining complex address elements.
2642        for (; i < N; ++i) {
2643          uint64_t Element = DV.getAddrElement(i);
2644          if (Element == DIBuilder::OpPlus) {
2645            Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2646            Asm->EmitULEB128(DV.getAddrElement(++i));
2647          } else if (Element == DIBuilder::OpDeref)
2648            Asm->EmitInt8(dwarf::DW_OP_deref);
2649          else llvm_unreachable("unknown Opcode found in complex address");
2650        }
2651      } else {
2652        Asm->OutStreamer.AddComment("Loc expr size");
2653        Asm->EmitInt16(Asm->getDwarfRegOpSize(Entry.Loc));
2654        Asm->EmitDwarfRegOp(Entry.Loc);
2655      }
2656    }
2657  }
2658}
2659
2660/// EmitDebugARanges - Emit visible names into a debug aranges section.
2661///
2662void DwarfDebug::EmitDebugARanges() {
2663  // Start the dwarf aranges section.
2664  Asm->OutStreamer.SwitchSection(
2665                          Asm->getObjFileLowering().getDwarfARangesSection());
2666}
2667
2668/// emitDebugRanges - Emit visible names into a debug ranges section.
2669///
2670void DwarfDebug::emitDebugRanges() {
2671  // Start the dwarf ranges section.
2672  Asm->OutStreamer.SwitchSection(
2673    Asm->getObjFileLowering().getDwarfRangesSection());
2674  unsigned char Size = Asm->getTargetData().getPointerSize();
2675  for (SmallVector<const MCSymbol *, 8>::iterator
2676         I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
2677       I != E; ++I) {
2678    if (*I)
2679      Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size, 0);
2680    else
2681      Asm->OutStreamer.EmitIntValue(0, Size, /*addrspace*/0);
2682  }
2683}
2684
2685/// emitDebugMacInfo - Emit visible names into a debug macinfo section.
2686///
2687void DwarfDebug::emitDebugMacInfo() {
2688  if (const MCSection *LineInfo =
2689      Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
2690    // Start the dwarf macinfo section.
2691    Asm->OutStreamer.SwitchSection(LineInfo);
2692  }
2693}
2694
2695/// emitDebugInlineInfo - Emit inline info using following format.
2696/// Section Header:
2697/// 1. length of section
2698/// 2. Dwarf version number
2699/// 3. address size.
2700///
2701/// Entries (one "entry" for each function that was inlined):
2702///
2703/// 1. offset into __debug_str section for MIPS linkage name, if exists;
2704///   otherwise offset into __debug_str for regular function name.
2705/// 2. offset into __debug_str section for regular function name.
2706/// 3. an unsigned LEB128 number indicating the number of distinct inlining
2707/// instances for the function.
2708///
2709/// The rest of the entry consists of a {die_offset, low_pc} pair for each
2710/// inlined instance; the die_offset points to the inlined_subroutine die in the
2711/// __debug_info section, and the low_pc is the starting address for the
2712/// inlining instance.
2713void DwarfDebug::emitDebugInlineInfo() {
2714  if (!Asm->MAI->doesDwarfUsesInlineInfoSection())
2715    return;
2716
2717  if (!FirstCU)
2718    return;
2719
2720  Asm->OutStreamer.SwitchSection(
2721                        Asm->getObjFileLowering().getDwarfDebugInlineSection());
2722
2723  Asm->OutStreamer.AddComment("Length of Debug Inlined Information Entry");
2724  Asm->EmitLabelDifference(Asm->GetTempSymbol("debug_inlined_end", 1),
2725                           Asm->GetTempSymbol("debug_inlined_begin", 1), 4);
2726
2727  Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_begin", 1));
2728
2729  Asm->OutStreamer.AddComment("Dwarf Version");
2730  Asm->EmitInt16(dwarf::DWARF_VERSION);
2731  Asm->OutStreamer.AddComment("Address Size (in bytes)");
2732  Asm->EmitInt8(Asm->getTargetData().getPointerSize());
2733
2734  for (SmallVector<const MDNode *, 4>::iterator I = InlinedSPNodes.begin(),
2735         E = InlinedSPNodes.end(); I != E; ++I) {
2736
2737    const MDNode *Node = *I;
2738    DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> >::iterator II
2739      = InlineInfo.find(Node);
2740    SmallVector<InlineInfoLabels, 4> &Labels = II->second;
2741    DISubprogram SP(Node);
2742    StringRef LName = SP.getLinkageName();
2743    StringRef Name = SP.getName();
2744
2745    Asm->OutStreamer.AddComment("MIPS linkage name");
2746    if (LName.empty()) {
2747      Asm->OutStreamer.EmitBytes(Name, 0);
2748      Asm->OutStreamer.EmitIntValue(0, 1, 0); // nul terminator.
2749    } else
2750      Asm->EmitSectionOffset(getStringPoolEntry(getRealLinkageName(LName)),
2751                             DwarfStrSectionSym);
2752
2753    Asm->OutStreamer.AddComment("Function name");
2754    Asm->EmitSectionOffset(getStringPoolEntry(Name), DwarfStrSectionSym);
2755    Asm->EmitULEB128(Labels.size(), "Inline count");
2756
2757    for (SmallVector<InlineInfoLabels, 4>::iterator LI = Labels.begin(),
2758           LE = Labels.end(); LI != LE; ++LI) {
2759      if (Asm->isVerbose()) Asm->OutStreamer.AddComment("DIE offset");
2760      Asm->EmitInt32(LI->second->getOffset());
2761
2762      if (Asm->isVerbose()) Asm->OutStreamer.AddComment("low_pc");
2763      Asm->OutStreamer.EmitSymbolValue(LI->first,
2764                                       Asm->getTargetData().getPointerSize(),0);
2765    }
2766  }
2767
2768  Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_inlined_end", 1));
2769}
2770