2009-05-15 00:11:17 +00:00
|
|
|
//===--- lib/CodeGen/DwarfPrinter.cpp - Dwarf Printer ---------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Emit general DWARF directives.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "DwarfPrinter.h"
|
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/CodeGen/AsmPrinter.h"
|
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
2009-08-19 21:59:18 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2009-05-15 00:11:17 +00:00
|
|
|
#include "llvm/CodeGen/MachineModuleInfo.h"
|
2009-08-22 20:48:53 +00:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
2010-01-22 22:38:16 +00:00
|
|
|
#include "llvm/MC/MCStreamer.h"
|
2010-01-16 18:50:28 +00:00
|
|
|
#include "llvm/MC/MCSymbol.h"
|
2009-05-15 00:11:17 +00:00
|
|
|
#include "llvm/Target/TargetData.h"
|
|
|
|
#include "llvm/Target/TargetFrameInfo.h"
|
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
2009-08-24 03:52:50 +00:00
|
|
|
#include "llvm/Support/Dwarf.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2009-05-15 00:11:17 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2010-01-22 22:23:57 +00:00
|
|
|
DwarfPrinter::DwarfPrinter(raw_ostream &OS, AsmPrinter *A, const MCAsmInfo *T,
|
|
|
|
const char *flavor)
|
2009-08-22 21:43:10 +00:00
|
|
|
: O(OS), Asm(A), MAI(T), TD(Asm->TM.getTargetData()),
|
2009-05-15 00:11:17 +00:00
|
|
|
RI(Asm->TM.getRegisterInfo()), M(NULL), MF(NULL), MMI(NULL),
|
|
|
|
SubprogramCount(0), Flavor(flavor), SetCounter(1) {}
|
|
|
|
|
2010-01-22 22:23:57 +00:00
|
|
|
void DwarfPrinter::PrintRelDirective(bool Force32Bit, bool isInSection) const {
|
2009-08-22 21:43:10 +00:00
|
|
|
if (isInSection && MAI->getDwarfSectionOffsetDirective())
|
|
|
|
O << MAI->getDwarfSectionOffsetDirective();
|
2009-05-15 00:11:17 +00:00
|
|
|
else if (Force32Bit || TD->getPointerSize() == sizeof(int32_t))
|
2009-08-22 21:43:10 +00:00
|
|
|
O << MAI->getData32bitsDirective();
|
2009-05-15 00:11:17 +00:00
|
|
|
else
|
2009-08-22 21:43:10 +00:00
|
|
|
O << MAI->getData64bitsDirective();
|
2009-05-15 00:11:17 +00:00
|
|
|
}
|
|
|
|
|
2010-01-22 22:38:16 +00:00
|
|
|
static const char *DecodeDWARFEncoding(unsigned Encoding) {
|
|
|
|
switch (Encoding) {
|
|
|
|
case dwarf::DW_EH_PE_absptr: return "absptr";
|
|
|
|
case dwarf::DW_EH_PE_omit: return "omit";
|
|
|
|
case dwarf::DW_EH_PE_pcrel: return "pcrel";
|
|
|
|
case dwarf::DW_EH_PE_udata4: return "udata4";
|
|
|
|
case dwarf::DW_EH_PE_udata8: return "udata8";
|
|
|
|
case dwarf::DW_EH_PE_sdata4: return "sdata4";
|
|
|
|
case dwarf::DW_EH_PE_sdata8: return "sdata8";
|
|
|
|
case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4: return "pcrel udata4";
|
|
|
|
case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4: return "pcrel sdata4";
|
|
|
|
case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8: return "pcrel udata8";
|
|
|
|
case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8: return "pcrel sdata8";
|
|
|
|
case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata4:
|
|
|
|
return "indirect pcrel udata4";
|
|
|
|
case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata4:
|
|
|
|
return "indirect pcrel sdata4";
|
|
|
|
case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata8:
|
|
|
|
return "indirect pcrel udata8";
|
|
|
|
case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata8:
|
|
|
|
return "indirect pcrel sdata8";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "<unknown encoding>";
|
|
|
|
}
|
|
|
|
|
|
|
|
/// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
|
|
|
|
/// encoding. If verbose assembly output is enabled, we output comments
|
|
|
|
/// describing the encoding. Desc is an optional string saying what the
|
|
|
|
/// encoding is specifying (e.g. "LSDA").
|
|
|
|
void DwarfPrinter::EmitEncodingByte(unsigned Val, const char *Desc) {
|
|
|
|
if (Asm->VerboseAsm) {
|
|
|
|
if (Desc != 0)
|
|
|
|
Asm->OutStreamer.AddComment(Twine(Desc)+" Encoding = " +
|
|
|
|
Twine(DecodeDWARFEncoding(Val)));
|
|
|
|
else
|
|
|
|
Asm->OutStreamer.AddComment(Twine("Encoding = ") +
|
|
|
|
DecodeDWARFEncoding(Val));
|
|
|
|
}
|
|
|
|
|
|
|
|
Asm->OutStreamer.EmitIntValue(Val, 1, 0/*addrspace*/);
|
|
|
|
}
|
|
|
|
|
2010-01-22 23:40:08 +00:00
|
|
|
/// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
|
|
|
|
void DwarfPrinter::EmitCFAByte(unsigned Val) {
|
|
|
|
if (Asm->VerboseAsm) {
|
|
|
|
if (Val >= dwarf::DW_CFA_offset && Val < dwarf::DW_CFA_offset+64)
|
|
|
|
Asm->OutStreamer.AddComment("DW_CFA_offset + Reg (" +
|
|
|
|
Twine(Val-dwarf::DW_CFA_offset) + ")");
|
|
|
|
else
|
|
|
|
Asm->OutStreamer.AddComment(dwarf::CallFrameString(Val));
|
|
|
|
}
|
|
|
|
Asm->OutStreamer.EmitIntValue(Val, 1, 0/*addrspace*/);
|
|
|
|
}
|
|
|
|
|
2010-01-22 23:18:42 +00:00
|
|
|
/// EmitSLEB128 - emit the specified signed leb128 value.
|
|
|
|
void DwarfPrinter::EmitSLEB128(int Value, const char *Desc) const {
|
|
|
|
if (Asm->VerboseAsm && Desc)
|
|
|
|
Asm->OutStreamer.AddComment(Desc);
|
|
|
|
|
|
|
|
if (MAI->hasLEB128()) {
|
|
|
|
O << "\t.sleb128\t" << Value;
|
|
|
|
Asm->OutStreamer.AddBlankLine();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we don't have .sleb128, emit as .bytes.
|
2010-01-22 22:56:55 +00:00
|
|
|
int Sign = Value >> (8 * sizeof(Value) - 1);
|
|
|
|
bool IsMore;
|
|
|
|
|
|
|
|
do {
|
|
|
|
unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
|
|
|
|
Value >>= 7;
|
|
|
|
IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
|
|
|
|
if (IsMore) Byte |= 0x80;
|
|
|
|
|
2010-01-22 23:18:42 +00:00
|
|
|
Asm->OutStreamer.EmitIntValue(Byte, 1, /*addrspace*/0);
|
2010-01-22 22:56:55 +00:00
|
|
|
} while (IsMore);
|
|
|
|
}
|
|
|
|
|
2010-01-22 23:18:42 +00:00
|
|
|
/// EmitULEB128 - emit the specified signed leb128 value.
|
|
|
|
void DwarfPrinter::EmitULEB128(unsigned Value, const char *Desc) const {
|
2010-01-22 22:56:55 +00:00
|
|
|
if (Asm->VerboseAsm && Desc)
|
|
|
|
Asm->OutStreamer.AddComment(Desc);
|
2010-01-22 23:18:42 +00:00
|
|
|
|
2010-01-22 22:56:55 +00:00
|
|
|
if (MAI->hasLEB128()) {
|
2010-01-22 23:18:42 +00:00
|
|
|
O << "\t.uleb128\t" << Value;
|
2010-01-22 22:56:55 +00:00
|
|
|
Asm->OutStreamer.AddBlankLine();
|
2010-01-22 23:18:42 +00:00
|
|
|
return;
|
2010-01-22 22:56:55 +00:00
|
|
|
}
|
2010-01-22 23:18:42 +00:00
|
|
|
|
|
|
|
// If we don't have .uleb128, emit as .bytes.
|
|
|
|
do {
|
|
|
|
unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
|
|
|
|
Value >>= 7;
|
|
|
|
if (Value) Byte |= 0x80;
|
|
|
|
Asm->OutStreamer.EmitIntValue(Byte, 1, /*addrspace*/0);
|
|
|
|
} while (Value);
|
2010-01-22 22:56:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-15 00:11:17 +00:00
|
|
|
/// PrintLabelName - Print label name in form used by Dwarf writer.
|
|
|
|
///
|
2010-01-22 22:23:57 +00:00
|
|
|
void DwarfPrinter::PrintLabelName(const char *Tag, unsigned Number) const {
|
2009-09-01 16:43:35 +00:00
|
|
|
O << MAI->getPrivateGlobalPrefix() << Tag;
|
2009-05-15 00:11:17 +00:00
|
|
|
if (Number) O << Number;
|
|
|
|
}
|
2010-01-22 22:23:57 +00:00
|
|
|
void DwarfPrinter::PrintLabelName(const char *Tag, unsigned Number,
|
|
|
|
const char *Suffix) const {
|
2009-09-01 16:43:35 +00:00
|
|
|
O << MAI->getPrivateGlobalPrefix() << Tag;
|
2009-05-15 00:11:17 +00:00
|
|
|
if (Number) O << Number;
|
|
|
|
O << Suffix;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// EmitLabel - Emit location label for internal use by Dwarf.
|
|
|
|
///
|
2010-01-22 22:23:57 +00:00
|
|
|
void DwarfPrinter::EmitLabel(const char *Tag, unsigned Number) const {
|
2009-09-01 16:43:35 +00:00
|
|
|
PrintLabelName(Tag, Number);
|
2009-05-15 00:11:17 +00:00
|
|
|
O << ":\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
/// EmitReference - Emit a reference to a label.
|
|
|
|
///
|
2010-01-22 22:23:57 +00:00
|
|
|
void DwarfPrinter::EmitReference(const char *Tag, unsigned Number,
|
|
|
|
bool IsPCRelative, bool Force32Bit) const {
|
2009-05-15 00:11:17 +00:00
|
|
|
PrintRelDirective(Force32Bit);
|
|
|
|
PrintLabelName(Tag, Number);
|
2009-08-22 21:43:10 +00:00
|
|
|
if (IsPCRelative) O << "-" << MAI->getPCSymbol();
|
2009-05-15 00:11:17 +00:00
|
|
|
}
|
2010-01-22 22:23:57 +00:00
|
|
|
void DwarfPrinter::EmitReference(const std::string &Name, bool IsPCRelative,
|
|
|
|
bool Force32Bit) const {
|
2009-05-15 00:11:17 +00:00
|
|
|
PrintRelDirective(Force32Bit);
|
|
|
|
O << Name;
|
2009-08-22 21:43:10 +00:00
|
|
|
if (IsPCRelative) O << "-" << MAI->getPCSymbol();
|
2009-05-15 00:11:17 +00:00
|
|
|
}
|
|
|
|
|
2010-01-22 22:23:57 +00:00
|
|
|
void DwarfPrinter::EmitReference(const MCSymbol *Sym, bool IsPCRelative,
|
|
|
|
bool Force32Bit) const {
|
2010-01-16 18:50:28 +00:00
|
|
|
PrintRelDirective(Force32Bit);
|
2010-01-17 21:43:43 +00:00
|
|
|
O << *Sym;
|
2010-01-16 18:50:28 +00:00
|
|
|
if (IsPCRelative) O << "-" << MAI->getPCSymbol();
|
|
|
|
}
|
|
|
|
|
2009-05-15 00:11:17 +00:00
|
|
|
/// EmitDifference - Emit the difference between two labels. Some assemblers do
|
|
|
|
/// not behave with absolute expressions with data directives, so there is an
|
|
|
|
/// option (needsSet) to use an intermediary set expression.
|
2010-01-22 22:23:57 +00:00
|
|
|
void DwarfPrinter::EmitDifference(const char *TagHi, unsigned NumberHi,
|
|
|
|
const char *TagLo, unsigned NumberLo,
|
|
|
|
bool IsSmall) {
|
2009-08-22 21:43:10 +00:00
|
|
|
if (MAI->needsSet()) {
|
2009-05-15 00:11:17 +00:00
|
|
|
O << "\t.set\t";
|
|
|
|
PrintLabelName("set", SetCounter, Flavor);
|
|
|
|
O << ",";
|
|
|
|
PrintLabelName(TagHi, NumberHi);
|
|
|
|
O << "-";
|
|
|
|
PrintLabelName(TagLo, NumberLo);
|
|
|
|
O << "\n";
|
|
|
|
|
|
|
|
PrintRelDirective(IsSmall);
|
|
|
|
PrintLabelName("set", SetCounter, Flavor);
|
|
|
|
++SetCounter;
|
|
|
|
} else {
|
|
|
|
PrintRelDirective(IsSmall);
|
|
|
|
PrintLabelName(TagHi, NumberHi);
|
|
|
|
O << "-";
|
|
|
|
PrintLabelName(TagLo, NumberLo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-22 22:23:57 +00:00
|
|
|
void DwarfPrinter::EmitSectionOffset(const char* Label, const char* Section,
|
|
|
|
unsigned LabelNumber,
|
|
|
|
unsigned SectionNumber,
|
|
|
|
bool IsSmall, bool isEH,
|
|
|
|
bool useSet) {
|
2009-05-15 00:11:17 +00:00
|
|
|
bool printAbsolute = false;
|
|
|
|
if (isEH)
|
2009-08-22 21:43:10 +00:00
|
|
|
printAbsolute = MAI->isAbsoluteEHSectionOffsets();
|
2009-05-15 00:11:17 +00:00
|
|
|
else
|
2009-08-22 21:43:10 +00:00
|
|
|
printAbsolute = MAI->isAbsoluteDebugSectionOffsets();
|
2009-05-15 00:11:17 +00:00
|
|
|
|
2009-08-22 21:43:10 +00:00
|
|
|
if (MAI->needsSet() && useSet) {
|
2009-05-15 00:11:17 +00:00
|
|
|
O << "\t.set\t";
|
|
|
|
PrintLabelName("set", SetCounter, Flavor);
|
|
|
|
O << ",";
|
|
|
|
PrintLabelName(Label, LabelNumber);
|
|
|
|
|
|
|
|
if (!printAbsolute) {
|
|
|
|
O << "-";
|
|
|
|
PrintLabelName(Section, SectionNumber);
|
|
|
|
}
|
|
|
|
|
|
|
|
O << "\n";
|
|
|
|
PrintRelDirective(IsSmall);
|
|
|
|
PrintLabelName("set", SetCounter, Flavor);
|
|
|
|
++SetCounter;
|
|
|
|
} else {
|
|
|
|
PrintRelDirective(IsSmall, true);
|
|
|
|
PrintLabelName(Label, LabelNumber);
|
|
|
|
|
|
|
|
if (!printAbsolute) {
|
|
|
|
O << "-";
|
|
|
|
PrintLabelName(Section, SectionNumber);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// EmitFrameMoves - Emit frame instructions to describe the layout of the
|
|
|
|
/// frame.
|
2010-01-22 22:23:57 +00:00
|
|
|
void DwarfPrinter::EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
|
|
|
|
const std::vector<MachineMove> &Moves,
|
|
|
|
bool isEH) {
|
2009-05-15 00:11:17 +00:00
|
|
|
int stackGrowth =
|
|
|
|
Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
|
|
|
|
TargetFrameInfo::StackGrowsUp ?
|
|
|
|
TD->getPointerSize() : -TD->getPointerSize();
|
|
|
|
bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
|
|
|
|
|
|
|
|
for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
|
|
|
|
const MachineMove &Move = Moves[i];
|
|
|
|
unsigned LabelID = Move.getLabelID();
|
|
|
|
|
|
|
|
if (LabelID) {
|
|
|
|
LabelID = MMI->MappedLabel(LabelID);
|
|
|
|
|
|
|
|
// Throw out move if the label is invalid.
|
|
|
|
if (!LabelID) continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const MachineLocation &Dst = Move.getDestination();
|
|
|
|
const MachineLocation &Src = Move.getSource();
|
|
|
|
|
|
|
|
// Advance row if new location.
|
|
|
|
if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
|
2010-01-22 23:40:08 +00:00
|
|
|
EmitCFAByte(dwarf::DW_CFA_advance_loc4);
|
2009-05-15 00:11:17 +00:00
|
|
|
EmitDifference("label", LabelID, BaseLabel, BaseLabelID, true);
|
2010-01-22 22:09:00 +00:00
|
|
|
Asm->O << '\n';
|
2009-05-15 00:11:17 +00:00
|
|
|
|
|
|
|
BaseLabelID = LabelID;
|
|
|
|
BaseLabel = "label";
|
|
|
|
IsLocal = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If advancing cfa.
|
|
|
|
if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
|
|
|
|
if (!Src.isReg()) {
|
|
|
|
if (Src.getReg() == MachineLocation::VirtualFP) {
|
2010-01-22 23:40:08 +00:00
|
|
|
EmitCFAByte(dwarf::DW_CFA_def_cfa_offset);
|
2009-05-15 00:11:17 +00:00
|
|
|
} else {
|
2010-01-22 23:40:08 +00:00
|
|
|
EmitCFAByte(dwarf::DW_CFA_def_cfa);
|
2010-01-22 23:18:42 +00:00
|
|
|
EmitULEB128(RI->getDwarfRegNum(Src.getReg(), isEH), "Register");
|
2009-05-15 00:11:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int Offset = -Src.getOffset();
|
2010-01-22 23:18:42 +00:00
|
|
|
EmitULEB128(Offset, "Offset");
|
2009-05-15 00:11:17 +00:00
|
|
|
} else {
|
2009-07-14 16:55:14 +00:00
|
|
|
llvm_unreachable("Machine move not supported yet.");
|
2009-05-15 00:11:17 +00:00
|
|
|
}
|
|
|
|
} else if (Src.isReg() &&
|
|
|
|
Src.getReg() == MachineLocation::VirtualFP) {
|
|
|
|
if (Dst.isReg()) {
|
2010-01-22 23:40:08 +00:00
|
|
|
EmitCFAByte(dwarf::DW_CFA_def_cfa_register);
|
2010-01-22 23:18:42 +00:00
|
|
|
EmitULEB128(RI->getDwarfRegNum(Dst.getReg(), isEH), "Register");
|
2009-05-15 00:11:17 +00:00
|
|
|
} else {
|
2009-07-14 16:55:14 +00:00
|
|
|
llvm_unreachable("Machine move not supported yet.");
|
2009-05-15 00:11:17 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
|
|
|
|
int Offset = Dst.getOffset() / stackGrowth;
|
|
|
|
|
|
|
|
if (Offset < 0) {
|
2010-01-22 23:40:08 +00:00
|
|
|
EmitCFAByte(dwarf::DW_CFA_offset_extended_sf);
|
2010-01-22 23:18:42 +00:00
|
|
|
EmitULEB128(Reg, "Reg");
|
2010-01-22 22:56:55 +00:00
|
|
|
EmitSLEB128(Offset, "Offset");
|
2009-05-15 00:11:17 +00:00
|
|
|
} else if (Reg < 64) {
|
2010-01-22 23:40:08 +00:00
|
|
|
EmitCFAByte(dwarf::DW_CFA_offset + Reg);
|
2010-01-22 23:18:42 +00:00
|
|
|
EmitULEB128(Offset, "Offset");
|
2009-05-15 00:11:17 +00:00
|
|
|
} else {
|
2010-01-22 23:40:08 +00:00
|
|
|
EmitCFAByte(dwarf::DW_CFA_offset_extended);
|
2010-01-22 23:18:42 +00:00
|
|
|
EmitULEB128(Reg, "Reg");
|
|
|
|
EmitULEB128(Offset, "Offset");
|
2009-05-15 00:11:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|