#include "llvm/IR/DataLayout.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/Instructions.h"
+#include "llvm/MC/MCSection.h"
+#include "llvm/MC/MCStreamer.h"
#include "llvm/Target/Mangler.h"
#include "llvm/Target/TargetFrameLowering.h"
#include "llvm/Target/TargetMachine.h"
/// CompileUnit - Compile unit constructor.
CompileUnit::CompileUnit(unsigned UID, DIE *D, const MDNode *N, AsmPrinter *A,
DwarfDebug *DW, DwarfUnits *DWU)
- : UniqueID(UID), Node(N), CUDie(D), Asm(A), DD(DW), DU(DWU), IndexTyDie(0) {
+ : UniqueID(UID), Node(N), CUDie(D), Asm(A), DD(DW), DU(DWU), IndexTyDie(0),
+ DebugInfoOffset(0) {
DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1);
insertDIE(N, D);
}
Die->addValue(Attribute, dwarf::DW_FORM_ref4, createDIEEntry(Entry));
}
+/// Create a DIE with the given Tag, add the DIE to its parent, and
+/// call insertDIE if MD is not null.
+DIE *CompileUnit::createAndAddDIE(unsigned Tag, DIE &Parent, const MDNode *MD) {
+ DIE *Die = new DIE(Tag);
+ Parent.addChild(Die);
+ if (MD)
+ insertDIE(MD, Die);
+ return Die;
+}
+
/// addBlock - Add block data.
///
void CompileUnit::addBlock(DIE *Die, dwarf::Attribute Attribute,
for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) {
DIDescriptor Element = TParams.getElement(i);
if (Element.isTemplateTypeParameter())
- getOrCreateTemplateTypeParameterDIE(DITemplateTypeParameter(Element),
- Buffer);
+ constructTemplateTypeParameterDIE(Buffer,
+ DITemplateTypeParameter(Element));
else if (Element.isTemplateValueParameter())
- getOrCreateTemplateValueParameterDIE(DITemplateValueParameter(Element),
- Buffer);
+ constructTemplateValueParameterDIE(Buffer,
+ DITemplateValueParameter(Element));
}
}
return getDIE(Context);
}
-/// addToContextOwner - Add Die into the list of its context owner's children.
-void CompileUnit::addToContextOwner(DIE *Die, DIScope Context) {
- assert(!Die->getParent());
- if (DIE *ContextDIE = getOrCreateContextDIE(Context)) {
- if (Die->getParent()) {
- // While creating the context, if this is a type member, we will have
- // added the child to the context already.
- assert(Die->getParent() == ContextDIE);
- return;
- }
- ContextDIE->addChild(Die);
- } else
- addDie(Die);
-}
-
/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
/// given DIType.
DIE *CompileUnit::getOrCreateTypeDIE(const MDNode *TyNode) {
DIType Ty(TyNode);
if (!Ty.isType())
return NULL;
+
+ // Construct the context before querying for the existence of the DIE in case
+ // such construction creates the DIE.
+ DIE *ContextDIE = getOrCreateContextDIE(resolve(Ty.getContext()));
+ if (!ContextDIE)
+ ContextDIE = CUDie.get();
+
DIE *TyDIE = getDIE(Ty);
if (TyDIE)
return TyDIE;
// Create new type.
- TyDIE = new DIE(Ty.getTag());
- insertDIE(Ty, TyDIE);
+ TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty);
+
if (Ty.isBasicType())
constructTypeDIE(*TyDIE, DIBasicType(Ty));
else if (Ty.isCompositeType())
addAccelType(Ty.getName(), std::make_pair(TyDIE, Flags));
}
- addToContextOwner(TyDIE, resolve(Ty.getContext()));
return TyDIE;
}
for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
DIDescriptor Enum(Elements.getElement(i));
if (Enum.isEnumerator())
- constructEnumTypeDIE(DIEnumerator(Enum), Buffer);
+ constructEnumTypeDIE(Buffer, DIEnumerator(Enum));
}
DIType DTy = resolve(CTy.getTypeDerivedFrom());
if (DTy) {
for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
DIDescriptor Ty = Elements.getElement(i);
if (Ty.isUnspecifiedParameter()) {
- DIE *Arg = new DIE(dwarf::DW_TAG_unspecified_parameters);
- Buffer.addChild(Arg);
+ createAndAddDIE(dwarf::DW_TAG_unspecified_parameters, Buffer);
isPrototyped = false;
} else {
- DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
+ DIE *Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, Buffer);
addType(Arg, DIType(Ty));
if (DIType(Ty).isArtificial())
addFlag(Arg, dwarf::DW_AT_artificial);
- Buffer.addChild(Arg);
}
}
// Add prototype flag if we're dealing with a C language and the
} else if (Element.isDerivedType()) {
DIDerivedType DDTy(Element);
if (DDTy.getTag() == dwarf::DW_TAG_friend) {
- ElemDie = new DIE(dwarf::DW_TAG_friend);
+ ElemDie = createAndAddDIE(dwarf::DW_TAG_friend, Buffer);
addType(ElemDie, resolve(DDTy.getTypeDerivedFrom()),
dwarf::DW_AT_friend);
- Buffer.addChild(ElemDie);
} else if (DDTy.isStaticMember()) {
- ElemDie = getOrCreateStaticMemberDIE(DDTy);
+ getOrCreateStaticMemberDIE(DDTy);
} else {
- ElemDie = createMemberDIE(DDTy, Buffer);
+ constructMemberDIE(Buffer, DDTy);
}
} else if (Element.isObjCProperty()) {
DIObjCProperty Property(Element);
- ElemDie = new DIE(Property.getTag());
+ ElemDie = createAndAddDIE(Property.getTag(), Buffer);
StringRef PropertyName = Property.getObjCPropertyName();
addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
addType(ElemDie, Property.getType());
Entry = createDIEEntry(ElemDie);
insertDIEEntry(Element, Entry);
}
- Buffer.addChild(ElemDie);
} else
continue;
}
DD->addTypeUnitType(&Buffer);
}
-/// getOrCreateTemplateTypeParameterDIE - Find existing DIE or create new DIE
-/// for the given DITemplateTypeParameter.
-DIE *
-CompileUnit::getOrCreateTemplateTypeParameterDIE(DITemplateTypeParameter TP,
- DIE &Buffer) {
- DIE *ParamDIE = getDIE(TP);
- if (ParamDIE)
- return ParamDIE;
-
- ParamDIE = new DIE(dwarf::DW_TAG_template_type_parameter);
- Buffer.addChild(ParamDIE);
+/// constructTemplateTypeParameterDIE - Construct new DIE for the given
+/// DITemplateTypeParameter.
+void
+CompileUnit::constructTemplateTypeParameterDIE(DIE &Buffer,
+ DITemplateTypeParameter TP) {
+ DIE *ParamDIE =
+ createAndAddDIE(dwarf::DW_TAG_template_type_parameter, Buffer);
// Add the type if it exists, it could be void and therefore no type.
if (TP.getType())
addType(ParamDIE, resolve(TP.getType()));
if (!TP.getName().empty())
addString(ParamDIE, dwarf::DW_AT_name, TP.getName());
- return ParamDIE;
}
-/// getOrCreateTemplateValueParameterDIE - Find existing DIE or create new DIE
-/// for the given DITemplateValueParameter.
-DIE *
-CompileUnit::getOrCreateTemplateValueParameterDIE(DITemplateValueParameter VP,
- DIE &Buffer) {
- DIE *ParamDIE = getDIE(VP);
- if (ParamDIE)
- return ParamDIE;
-
- ParamDIE = new DIE(VP.getTag());
- Buffer.addChild(ParamDIE);
+/// constructTemplateValueParameterDIE - Construct new DIE for the given
+/// DITemplateValueParameter.
+void
+CompileUnit::constructTemplateValueParameterDIE(DIE &Buffer,
+ DITemplateValueParameter VP) {
+ DIE *ParamDIE = createAndAddDIE(VP.getTag(), Buffer);
// Add the type if there is one, template template and template parameter
// packs will not have a type.
// For declaration non-type template parameters (such as global values and
// functions)
DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
- addOpAddress(Block, Asm->Mang->getSymbol(GV));
+ addOpAddress(Block, Asm->getSymbol(GV));
// Emit DW_OP_stack_value to use the address as the immediate value of the
// parameter, rather than a pointer to it.
addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value);
addTemplateParams(*ParamDIE, A);
}
}
-
- return ParamDIE;
}
/// getOrCreateNameSpace - Create a DIE for DINameSpace.
DIE *CompileUnit::getOrCreateNameSpace(DINameSpace NS) {
+ // Construct the context before querying for the existence of the DIE in case
+ // such construction creates the DIE.
+ DIE *ContextDIE = getOrCreateContextDIE(NS.getContext());
+ if (!ContextDIE)
+ // If the context is null, DIE should belong to the CU we call construct
+ // function on.
+ ContextDIE = CUDie.get();
+
DIE *NDie = getDIE(NS);
if (NDie)
return NDie;
- NDie = new DIE(dwarf::DW_TAG_namespace);
- insertDIE(NS, NDie);
+ NDie = createAndAddDIE(dwarf::DW_TAG_namespace, *ContextDIE, NS);
+
if (!NS.getName().empty()) {
addString(NDie, dwarf::DW_AT_name, NS.getName());
addAccelNamespace(NS.getName(), NDie);
} else
addAccelNamespace("(anonymous namespace)", NDie);
addSourceLine(NDie, NS);
- addToContextOwner(NDie, NS.getContext());
return NDie;
}
if (SPDie)
return SPDie;
- SPDie = new DIE(dwarf::DW_TAG_subprogram);
+ DISubprogram SPDecl = SP.getFunctionDeclaration();
+ if (SPDecl.isSubprogram())
+ // Add subprogram definitions to the CU die directly.
+ ContextDIE = CUDie.get();
// DW_TAG_inlined_subroutine may refer to this DIE.
- insertDIE(SP, SPDie);
+ SPDie = createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, SP);
- DISubprogram SPDecl = SP.getFunctionDeclaration();
DIE *DeclDie = NULL;
if (SPDecl.isSubprogram())
DeclDie = getOrCreateSubprogramDIE(SPDecl);
// Refer function declaration directly.
addDIEEntry(SPDie, dwarf::DW_AT_specification, DeclDie);
- // Add subprogram definitions to the CU die directly.
- addDie(SPDie);
-
return SPDie;
}
- // Add to context owner.
- ContextDIE->addChild(SPDie);
-
// Add the linkage name if we have one.
StringRef LinkageName = SP.getLinkageName();
if (!LinkageName.empty())
// Add arguments. Do not add arguments for subprogram definition. They will
// be handled while processing variables.
for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
- DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
+ DIE *Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, *SPDie);
DIType ATy = DIType(Args.getElement(i));
addType(Arg, ATy);
if (ATy.isArtificial())
addFlag(Arg, dwarf::DW_AT_artificial);
- SPDie->addChild(Arg);
}
}
// If this is not a static data member definition, create the variable
// DIE and add the initial set of attributes to it.
if (!VariableDIE) {
- VariableDIE = new DIE(GV.getTag());
+ // Construct the context before querying for the existence of the DIE in
+ // case such construction creates the DIE.
+ DIE *ContextDIE = getOrCreateContextDIE(GVContext);
+ if (!ContextDIE)
+ ContextDIE = CUDie.get();
+
// Add to map.
- insertDIE(N, VariableDIE);
+ VariableDIE = createAndAddDIE(GV.getTag(), *ContextDIE, N);
// Add name and type.
addString(VariableDIE, dwarf::DW_AT_name, GV.getDisplayName());
// Add line number info.
addSourceLine(VariableDIE, GV);
- // Add to context owner.
- addToContextOwner(VariableDIE, GVContext);
}
// Add location.
if (isGlobalVariable) {
addToAccelTable = true;
DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
- const MCSymbol *Sym = Asm->Mang->getSymbol(GV.getGlobal());
+ const MCSymbol *Sym = Asm->getSymbol(GV.getGlobal());
if (GV.getGlobal()->isThreadLocal()) {
// FIXME: Make this work with -gsplit-dwarf.
unsigned PointerSize = Asm->getDataLayout().getPointerSize();
if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() &&
!GVContext.isFile() && !DD->isSubprogramContext(GVContext)) {
// Create specification DIE.
- VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
+ VariableSpecDIE = createAndAddDIE(dwarf::DW_TAG_variable, *CUDie.get());
addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification, VariableDIE);
addBlock(VariableSpecDIE, dwarf::DW_AT_location, Block);
// A static member's declaration is already flagged as such.
if (!SDMDecl.Verify())
addFlag(VariableDIE, dwarf::DW_AT_declaration);
- addDie(VariableSpecDIE);
} else {
addBlock(VariableDIE, dwarf::DW_AT_location, Block);
}
// GV is a merged global.
DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Value *Ptr = CE->getOperand(0);
- addOpAddress(Block, Asm->Mang->getSymbol(cast<GlobalValue>(Ptr)));
+ addOpAddress(Block, Asm->getSymbol(cast<GlobalValue>(Ptr)));
addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
SmallVector<Value *, 3> Idx(CE->op_begin() + 1, CE->op_end());
addUInt(Block, dwarf::DW_FORM_udata,
/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
void CompileUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR,
DIE *IndexTy) {
- DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
+ DIE *DW_Subrange = createAndAddDIE(dwarf::DW_TAG_subrange_type, Buffer);
addDIEEntry(DW_Subrange, dwarf::DW_AT_type, IndexTy);
// The LowerBound value defines the lower bounds which is typically zero for
// FIXME: An unbounded array should reference the expression that defines
// the array.
addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, None, LowerBound + Count - 1);
-
- Buffer.addChild(DW_Subrange);
}
/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
DIE *IdxTy = getIndexTyDie();
if (!IdxTy) {
// Construct an anonymous type for index type.
- IdxTy = new DIE(dwarf::DW_TAG_base_type);
+ IdxTy = createAndAddDIE(dwarf::DW_TAG_base_type, *CUDie.get());
addString(IdxTy, dwarf::DW_AT_name, "int");
addUInt(IdxTy, dwarf::DW_AT_byte_size, None, sizeof(int32_t));
addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
dwarf::DW_ATE_signed);
- addDie(IdxTy);
setIndexTyDie(IdxTy);
}
}
/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
-DIE *CompileUnit::constructEnumTypeDIE(DIEnumerator ETy, DIE &Buffer) {
- DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
- Buffer.addChild(Enumerator);
+void CompileUnit::constructEnumTypeDIE(DIE &Buffer, DIEnumerator ETy) {
+ DIE *Enumerator = createAndAddDIE(dwarf::DW_TAG_enumerator, Buffer);
StringRef Name = ETy.getName();
addString(Enumerator, dwarf::DW_AT_name, Name);
int64_t Value = ETy.getEnumValue();
addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
- return Enumerator;
}
/// constructContainingTypeDIEs - Construct DIEs for types that contain
return VariableDie;
}
-/// createMemberDIE - Create new member DIE.
-DIE *CompileUnit::createMemberDIE(DIDerivedType DT, DIE &Buffer) {
- DIE *MemberDie = new DIE(DT.getTag());
- Buffer.addChild(MemberDie);
+/// constructMemberDIE - Construct member DIE from DIDerivedType.
+void CompileUnit::constructMemberDIE(DIE &Buffer, DIDerivedType DT) {
+ DIE *MemberDie = createAndAddDIE(DT.getTag(), Buffer);
StringRef Name = DT.getName();
if (!Name.empty())
addString(MemberDie, dwarf::DW_AT_name, Name);
if (DT.isArtificial())
addFlag(MemberDie, dwarf::DW_AT_artificial);
-
- return MemberDie;
}
/// getOrCreateStaticMemberDIE - Create new DIE for C++ static member.
if (StaticMemberDIE)
return StaticMemberDIE;
- StaticMemberDIE = new DIE(DT.getTag());
- // Add to context owner.
- ContextDIE->addChild(StaticMemberDIE);
+ StaticMemberDIE = createAndAddDIE(DT.getTag(), *ContextDIE, DT);
DIType Ty = resolve(DT.getTypeDerivedFrom());
if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT.getConstant()))
addConstantFPValue(StaticMemberDIE, CFP);
- insertDIE(DT, StaticMemberDIE);
return StaticMemberDIE;
}
+
+void CompileUnit::emitHeader(const MCSection *ASection,
+ const MCSymbol *ASectionSym) {
+ Asm->OutStreamer.AddComment("DWARF version number");
+ Asm->EmitInt16(DD->getDwarfVersion());
+ Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
+ Asm->EmitSectionOffset(Asm->GetTempSymbol(ASection->getLabelBeginName()),
+ ASectionSym);
+ Asm->OutStreamer.AddComment("Address Size (in bytes)");
+ Asm->EmitInt8(Asm->getDataLayout().getPointerSize());
+}