#ifndef LLVM_MC_MCDWARF_H
#define LLVM_MC_MCDWARF_H
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Dwarf.h"
#include "llvm/Support/raw_ostream.h"
#include <map>
#include <string>
#include <utility>
#include <vector>
namespace llvm {
class MCAsmBackend;
class MCContext;
class MCObjectStreamer;
class MCSection;
class MCStreamer;
class MCSymbol;
class SourceMgr;
class SMLoc;
struct MCDwarfFile {
std::string Name;
unsigned DirIndex;
};
class MCDwarfLoc {
unsigned FileNum;
unsigned Line;
unsigned Column;
unsigned Flags;
unsigned Isa;
unsigned Discriminator;
#define DWARF2_LINE_DEFAULT_IS_STMT 1
#define DWARF2_FLAG_IS_STMT (1 << 0)
#define DWARF2_FLAG_BASIC_BLOCK (1 << 1)
#define DWARF2_FLAG_PROLOGUE_END (1 << 2)
#define DWARF2_FLAG_EPILOGUE_BEGIN (1 << 3)
private: friend class MCContext;
friend class MCLineEntry;
MCDwarfLoc(unsigned fileNum, unsigned line, unsigned column, unsigned flags,
unsigned isa, unsigned discriminator)
: FileNum(fileNum), Line(line), Column(column), Flags(flags), Isa(isa),
Discriminator(discriminator) {}
public:
unsigned getFileNum() const { return FileNum; }
unsigned getLine() const { return Line; }
unsigned getColumn() const { return Column; }
unsigned getFlags() const { return Flags; }
unsigned getIsa() const { return Isa; }
unsigned getDiscriminator() const { return Discriminator; }
void setFileNum(unsigned fileNum) { FileNum = fileNum; }
void setLine(unsigned line) { Line = line; }
void setColumn(unsigned column) { Column = column; }
void setFlags(unsigned flags) { Flags = flags; }
void setIsa(unsigned isa) { Isa = isa; }
void setDiscriminator(unsigned discriminator) {
Discriminator = discriminator;
}
};
class MCLineEntry : public MCDwarfLoc {
MCSymbol *Label;
private:
public:
MCLineEntry(MCSymbol *label, const MCDwarfLoc loc)
: MCDwarfLoc(loc), Label(label) {}
MCSymbol *getLabel() const { return Label; }
static void Make(MCObjectStreamer *MCOS, const MCSection *Section);
};
class MCLineSection {
public:
void addLineEntry(const MCLineEntry &LineEntry, const MCSection *Sec) {
MCLineDivisions[Sec].push_back(LineEntry);
}
typedef std::vector<MCLineEntry> MCLineEntryCollection;
typedef MCLineEntryCollection::iterator iterator;
typedef MCLineEntryCollection::const_iterator const_iterator;
typedef MapVector<const MCSection *, MCLineEntryCollection> MCLineDivisionMap;
private:
MCLineDivisionMap MCLineDivisions;
public:
const MCLineDivisionMap &getMCLineEntries() const {
return MCLineDivisions;
}
};
struct MCDwarfLineTableParameters {
unsigned char DWARF2LineOpcodeBase = 13;
char DWARF2LineBase = -5;
unsigned char DWARF2LineRange = 14;
};
struct MCDwarfLineTableHeader {
MCSymbol *Label;
SmallVector<std::string, 3> MCDwarfDirs;
SmallVector<MCDwarfFile, 3> MCDwarfFiles;
StringMap<unsigned> SourceIdMap;
StringRef CompilationDir;
MCDwarfLineTableHeader()
: Label(nullptr) {}
unsigned getFile(StringRef &Directory, StringRef &FileName,
unsigned FileNumber = 0);
std::pair<MCSymbol *, MCSymbol *>
Emit(MCStreamer *MCOS, MCDwarfLineTableParameters Params) const;
std::pair<MCSymbol *, MCSymbol *>
Emit(MCStreamer *MCOS, MCDwarfLineTableParameters Params,
ArrayRef<char> SpecialOpcodeLengths) const;
};
class MCDwarfDwoLineTable {
MCDwarfLineTableHeader Header;
public:
void setCompilationDir(StringRef CompilationDir) {
Header.CompilationDir = CompilationDir;
}
unsigned getFile(StringRef Directory, StringRef FileName) {
return Header.getFile(Directory, FileName);
}
void Emit(MCStreamer &MCOS, MCDwarfLineTableParameters Params) const;
};
class MCDwarfLineTable {
MCDwarfLineTableHeader Header;
MCLineSection MCLineSections;
public:
static void Emit(MCObjectStreamer *MCOS, MCDwarfLineTableParameters Params);
void EmitCU(MCObjectStreamer *MCOS, MCDwarfLineTableParameters Params) const;
unsigned getFile(StringRef &Directory, StringRef &FileName,
unsigned FileNumber = 0);
MCSymbol *getLabel() const {
return Header.Label;
}
void setLabel(MCSymbol *Label) {
Header.Label = Label;
}
void setCompilationDir(StringRef CompilationDir) {
Header.CompilationDir = CompilationDir;
}
const SmallVectorImpl<std::string> &getMCDwarfDirs() const {
return Header.MCDwarfDirs;
}
SmallVectorImpl<std::string> &getMCDwarfDirs() {
return Header.MCDwarfDirs;
}
const SmallVectorImpl<MCDwarfFile> &getMCDwarfFiles() const {
return Header.MCDwarfFiles;
}
SmallVectorImpl<MCDwarfFile> &getMCDwarfFiles() {
return Header.MCDwarfFiles;
}
const MCLineSection &getMCLineSections() const {
return MCLineSections;
}
MCLineSection &getMCLineSections() {
return MCLineSections;
}
};
class MCDwarfLineAddr {
public:
static void Encode(MCContext &Context, MCDwarfLineTableParameters Params,
int64_t LineDelta, uint64_t AddrDelta,
raw_ostream &OS);
static void Emit(MCStreamer *MCOS, MCDwarfLineTableParameters Params,
int64_t LineDelta, uint64_t AddrDelta);
};
class MCGenDwarfInfo {
public:
static void Emit(MCStreamer *MCOS);
};
class MCGenDwarfLabelEntry {
private:
StringRef Name;
unsigned FileNumber;
unsigned LineNumber;
MCSymbol *Label;
public:
MCGenDwarfLabelEntry(StringRef name, unsigned fileNumber, unsigned lineNumber,
MCSymbol *label)
: Name(name), FileNumber(fileNumber), LineNumber(lineNumber),
Label(label) {}
StringRef getName() const { return Name; }
unsigned getFileNumber() const { return FileNumber; }
unsigned getLineNumber() const { return LineNumber; }
MCSymbol *getLabel() const { return Label; }
static void Make(MCSymbol *Symbol, MCStreamer *MCOS, SourceMgr &SrcMgr,
SMLoc &Loc);
};
class MCCFIInstruction {
public:
enum OpType {
OpSameValue,
OpRememberState,
OpRestoreState,
OpOffset,
OpRealignedOffset,
OpDefCfaRegister,
OpDefCfaOffset,
OpDefCfa,
OpRelOffset,
OpAdjustCfaOffset,
OpEscape,
OpRestore,
OpUndefined,
OpRegister,
OpWindowSave
};
private:
OpType Operation;
MCSymbol *Label;
unsigned Register;
union {
int Offset;
unsigned Register2;
};
int Align;
int Offset2;
std::vector<char> Values;
MCCFIInstruction(OpType Op, MCSymbol *L, unsigned R, int O, StringRef V)
: Operation(Op), Label(L), Register(R), Offset(O),
Values(V.begin(), V.end()) {
assert(Op != OpRegister && Op != OpRealignedOffset);
}
MCCFIInstruction(OpType Op, MCSymbol *L, unsigned R1, unsigned R2)
: Operation(Op), Label(L), Register(R1), Register2(R2) {
assert(Op == OpRegister);
}
MCCFIInstruction(OpType Op, MCSymbol *L, unsigned R, int O1, int A, int O2)
: Operation(Op), Label(L), Register(R), Offset(O1), Align(A), Offset2(O2) {
assert(Op == OpRealignedOffset);
}
public:
static MCCFIInstruction createDefCfa(MCSymbol *L, unsigned Register,
int Offset) {
return MCCFIInstruction(OpDefCfa, L, Register, -Offset, "");
}
static MCCFIInstruction createDefCfaRegister(MCSymbol *L, unsigned Register) {
return MCCFIInstruction(OpDefCfaRegister, L, Register, 0, "");
}
static MCCFIInstruction createDefCfaOffset(MCSymbol *L, int Offset) {
return MCCFIInstruction(OpDefCfaOffset, L, 0, -Offset, "");
}
static MCCFIInstruction createAdjustCfaOffset(MCSymbol *L, int Adjustment) {
return MCCFIInstruction(OpAdjustCfaOffset, L, 0, Adjustment, "");
}
static MCCFIInstruction createOffset(MCSymbol *L, unsigned Register,
int Offset) {
return MCCFIInstruction(OpOffset, L, Register, Offset, "");
}
static MCCFIInstruction createRelOffset(MCSymbol *L, unsigned Register,
int Offset) {
return MCCFIInstruction(OpRelOffset, L, Register, Offset, "");
}
static MCCFIInstruction createRealignedOffset(MCSymbol *L, unsigned Register,
int PreOffset, int Align,
int PostOffset) {
return MCCFIInstruction(OpRealignedOffset, L, Register, PreOffset, Align,
PostOffset);
}
static MCCFIInstruction createRegister(MCSymbol *L, unsigned Register1,
unsigned Register2) {
return MCCFIInstruction(OpRegister, L, Register1, Register2);
}
static MCCFIInstruction createWindowSave(MCSymbol *L) {
return MCCFIInstruction(OpWindowSave, L, 0, 0, "");
}
static MCCFIInstruction createRestore(MCSymbol *L, unsigned Register) {
return MCCFIInstruction(OpRestore, L, Register, 0, "");
}
static MCCFIInstruction createUndefined(MCSymbol *L, unsigned Register) {
return MCCFIInstruction(OpUndefined, L, Register, 0, "");
}
static MCCFIInstruction createSameValue(MCSymbol *L, unsigned Register) {
return MCCFIInstruction(OpSameValue, L, Register, 0, "");
}
static MCCFIInstruction createRememberState(MCSymbol *L) {
return MCCFIInstruction(OpRememberState, L, 0, 0, "");
}
static MCCFIInstruction createRestoreState(MCSymbol *L) {
return MCCFIInstruction(OpRestoreState, L, 0, 0, "");
}
static MCCFIInstruction createEscape(MCSymbol *L, StringRef Vals) {
return MCCFIInstruction(OpEscape, L, 0, 0, Vals);
}
OpType getOperation() const { return Operation; }
MCSymbol *getLabel() const { return Label; }
unsigned getRegister() const {
assert(Operation == OpDefCfa || Operation == OpOffset ||
Operation == OpRealignedOffset || Operation == OpRestore ||
Operation == OpUndefined || Operation == OpSameValue ||
Operation == OpDefCfaRegister || Operation == OpRelOffset ||
Operation == OpRegister);
return Register;
}
unsigned getRegister2() const {
assert(Operation == OpRegister);
return Register2;
}
int getOffset() const {
assert(Operation == OpDefCfa || Operation == OpOffset ||
Operation == OpRelOffset || Operation == OpRealignedOffset ||
Operation == OpDefCfaOffset || Operation == OpAdjustCfaOffset);
return Offset;
}
int getAlign() const {
assert(Operation == OpRealignedOffset);
return Align;
}
int getOffset2() const {
assert(Operation == OpRealignedOffset);
return Offset2;
}
StringRef getValues() const {
assert(Operation == OpEscape);
return StringRef(&Values[0], Values.size());
}
};
struct MCDwarfFrameInfo {
MCDwarfFrameInfo()
: Begin(nullptr), End(nullptr), Personality(nullptr), Lsda(nullptr),
Instructions(), CurrentCfaRegister(0), PersonalityEncoding(),
LsdaEncoding(0), CompactUnwindEncoding(0), IsSignalFrame(false),
IsSimple(false) {}
MCSymbol *Begin;
MCSymbol *End;
const MCSymbol *Personality;
const MCSymbol *Lsda;
std::vector<MCCFIInstruction> Instructions;
unsigned CurrentCfaRegister;
unsigned PersonalityEncoding;
unsigned LsdaEncoding;
uint32_t CompactUnwindEncoding;
bool IsSignalFrame;
bool IsSimple;
};
class MCDwarfFrameEmitter {
public:
static void Emit(MCObjectStreamer &streamer, MCAsmBackend *MAB, bool isEH);
static void EmitAdvanceLoc(MCObjectStreamer &Streamer, uint64_t AddrDelta);
static void EncodeAdvanceLoc(MCContext &Context, uint64_t AddrDelta,
raw_ostream &OS);
};
}
#endif