#ifndef CODEGEN_ASMPRINTER_DIE_H__
#define CODEGEN_ASMPRINTER_DIE_H__
#include "llvm/ADT/FoldingSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Dwarf.h"
#include <vector>
namespace llvm {
class AsmPrinter;
class MCSymbol;
class raw_ostream;
class DIEAbbrevData {
uint16_t Attribute;
uint16_t Form;
public:
DIEAbbrevData(uint16_t A, uint16_t F) : Attribute(A), Form(F) {}
uint16_t getAttribute() const { return Attribute; }
uint16_t getForm() const { return Form; }
void Profile(FoldingSetNodeID &ID) const;
};
class DIEAbbrev : public FoldingSetNode {
uint16_t Tag;
uint16_t ChildrenFlag;
unsigned Number;
SmallVector<DIEAbbrevData, 8> Data;
public:
DIEAbbrev(uint16_t T, uint16_t C) : Tag(T), ChildrenFlag(C), Data() {}
uint16_t getTag() const { return Tag; }
unsigned getNumber() const { return Number; }
uint16_t getChildrenFlag() const { return ChildrenFlag; }
const SmallVector<DIEAbbrevData, 8> &getData() const { return Data; }
void setTag(uint16_t T) { Tag = T; }
void setChildrenFlag(uint16_t CF) { ChildrenFlag = CF; }
void setNumber(unsigned N) { Number = N; }
void AddAttribute(uint16_t Attribute, uint16_t Form) {
Data.push_back(DIEAbbrevData(Attribute, Form));
}
void AddFirstAttribute(uint16_t Attribute, uint16_t Form) {
Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
}
void Profile(FoldingSetNodeID &ID) const;
void Emit(AsmPrinter *AP) const;
#ifndef NDEBUG
void print(raw_ostream &O);
void dump();
#endif
};
class DIEValue;
class DIE {
protected:
unsigned Offset;
unsigned Size;
DIEAbbrev Abbrev;
std::vector<DIE *> Children;
DIE *Parent;
SmallVector<DIEValue*, 32> Values;
mutable unsigned IndentCount;
public:
explicit DIE(unsigned Tag)
: Offset(0), Size(0), Abbrev(Tag, dwarf::DW_CHILDREN_no), Parent(0),
IndentCount(0) {}
virtual ~DIE();
DIEAbbrev &getAbbrev() { return Abbrev; }
unsigned getAbbrevNumber() const { return Abbrev.getNumber(); }
unsigned getTag() const { return Abbrev.getTag(); }
unsigned getOffset() const { return Offset; }
unsigned getSize() const { return Size; }
const std::vector<DIE *> &getChildren() const { return Children; }
const SmallVector<DIEValue*, 32> &getValues() const { return Values; }
DIE *getParent() const { return Parent; }
void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
void setOffset(unsigned O) { Offset = O; }
void setSize(unsigned S) { Size = S; }
void addValue(unsigned Attribute, unsigned Form, DIEValue *Value) {
Abbrev.AddAttribute(Attribute, Form);
Values.push_back(Value);
}
void addChild(DIE *Child) {
if (Child->getParent()) {
assert (Child->getParent() == this && "Unexpected DIE Parent!");
return;
}
Abbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
Children.push_back(Child);
Child->Parent = this;
}
#ifndef NDEBUG
void print(raw_ostream &O, unsigned IncIndent = 0);
void dump();
#endif
};
class DIEValue {
virtual void anchor();
public:
enum {
isInteger,
isString,
isLabel,
isDelta,
isEntry,
isBlock
};
protected:
unsigned Type;
public:
explicit DIEValue(unsigned T) : Type(T) {}
virtual ~DIEValue() {}
unsigned getType() const { return Type; }
virtual void EmitValue(AsmPrinter *AP, unsigned Form) const = 0;
virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const = 0;
static bool classof(const DIEValue *) { return true; }
#ifndef NDEBUG
virtual void print(raw_ostream &O) = 0;
void dump();
#endif
};
class DIEInteger : public DIEValue {
uint64_t Integer;
public:
explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
static unsigned BestForm(bool IsSigned, uint64_t Int) {
if (IsSigned) {
if ((char)Int == (signed)Int) return dwarf::DW_FORM_data1;
if ((short)Int == (signed)Int) return dwarf::DW_FORM_data2;
if ((int)Int == (signed)Int) return dwarf::DW_FORM_data4;
} else {
if ((unsigned char)Int == Int) return dwarf::DW_FORM_data1;
if ((unsigned short)Int == Int) return dwarf::DW_FORM_data2;
if ((unsigned int)Int == Int) return dwarf::DW_FORM_data4;
}
return dwarf::DW_FORM_data8;
}
virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
uint64_t getValue() const { return Integer; }
virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const;
static bool classof(const DIEInteger *) { return true; }
static bool classof(const DIEValue *I) { return I->getType() == isInteger; }
#ifndef NDEBUG
virtual void print(raw_ostream &O);
#endif
};
class DIELabel : public DIEValue {
const MCSymbol *Label;
public:
explicit DIELabel(const MCSymbol *L) : DIEValue(isLabel), Label(L) {}
virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
const MCSymbol *getValue() const { return Label; }
virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const;
static bool classof(const DIELabel *) { return true; }
static bool classof(const DIEValue *L) { return L->getType() == isLabel; }
#ifndef NDEBUG
virtual void print(raw_ostream &O);
#endif
};
class DIEDelta : public DIEValue {
const MCSymbol *LabelHi;
const MCSymbol *LabelLo;
public:
DIEDelta(const MCSymbol *Hi, const MCSymbol *Lo)
: DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const;
static bool classof(const DIEDelta *) { return true; }
static bool classof(const DIEValue *D) { return D->getType() == isDelta; }
#ifndef NDEBUG
virtual void print(raw_ostream &O);
#endif
};
class DIEEntry : public DIEValue {
DIE *const Entry;
public:
explicit DIEEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
DIE *getEntry() const { return Entry; }
virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const {
return sizeof(int32_t);
}
static bool classof(const DIEEntry *) { return true; }
static bool classof(const DIEValue *E) { return E->getType() == isEntry; }
#ifndef NDEBUG
virtual void print(raw_ostream &O);
#endif
};
class DIEBlock : public DIEValue, public DIE {
unsigned Size; public:
DIEBlock()
: DIEValue(isBlock), DIE(0), Size(0) {}
virtual ~DIEBlock() {}
unsigned ComputeSize(AsmPrinter *AP);
unsigned BestForm() const {
if ((unsigned char)Size == Size) return dwarf::DW_FORM_block1;
if ((unsigned short)Size == Size) return dwarf::DW_FORM_block2;
if ((unsigned int)Size == Size) return dwarf::DW_FORM_block4;
return dwarf::DW_FORM_block;
}
virtual void EmitValue(AsmPrinter *AP, unsigned Form) const;
virtual unsigned SizeOf(AsmPrinter *AP, unsigned Form) const;
static bool classof(const DIEBlock *) { return true; }
static bool classof(const DIEValue *E) { return E->getType() == isBlock; }
#ifndef NDEBUG
virtual void print(raw_ostream &O);
#endif
};
}
#endif