AsmPrinterDwarf.cpp revision 7abe37e4aee38cc79d91dd069a37d7e91d5bef53
1//===-- AsmPrinterDwarf.cpp - AsmPrinter Dwarf Support --------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Dwarf emissions parts of AsmPrinter.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "asm-printer"
15#include "llvm/CodeGen/AsmPrinter.h"
16#include "llvm/CodeGen/MachineLocation.h"
17#include "llvm/MC/MCAsmInfo.h"
18#include "llvm/MC/MCSection.h"
19#include "llvm/MC/MCStreamer.h"
20#include "llvm/MC/MCSymbol.h"
21#include "llvm/Target/TargetData.h"
22#include "llvm/Target/TargetFrameInfo.h"
23#include "llvm/Target/TargetLoweringObjectFile.h"
24#include "llvm/Target/TargetMachine.h"
25#include "llvm/Target/TargetRegisterInfo.h"
26#include "llvm/ADT/Twine.h"
27#include "llvm/Support/Dwarf.h"
28using namespace llvm;
29
30//===----------------------------------------------------------------------===//
31// Dwarf Emission Helper Routines
32//===----------------------------------------------------------------------===//
33
34/// EmitSLEB128 - emit the specified signed leb128 value.
35void AsmPrinter::EmitSLEB128(int Value, const char *Desc) const {
36  if (isVerbose() && Desc)
37    OutStreamer.AddComment(Desc);
38
39  if (MAI->hasLEB128()) {
40    // FIXME: MCize.
41    OutStreamer.EmitRawText("\t.sleb128\t" + Twine(Value));
42    return;
43  }
44
45  // If we don't have .sleb128, emit as .bytes.
46  int Sign = Value >> (8 * sizeof(Value) - 1);
47  bool IsMore;
48
49  do {
50    unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
51    Value >>= 7;
52    IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
53    if (IsMore) Byte |= 0x80;
54    OutStreamer.EmitIntValue(Byte, 1, /*addrspace*/0);
55  } while (IsMore);
56}
57
58/// EmitULEB128 - emit the specified signed leb128 value.
59void AsmPrinter::EmitULEB128(unsigned Value, const char *Desc,
60                             unsigned PadTo) const {
61  if (isVerbose() && Desc)
62    OutStreamer.AddComment(Desc);
63
64  if (MAI->hasLEB128() && PadTo == 0) {
65    // FIXME: MCize.
66    OutStreamer.EmitRawText("\t.uleb128\t" + Twine(Value));
67    return;
68  }
69
70  // If we don't have .uleb128 or we want to emit padding, emit as .bytes.
71  do {
72    unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
73    Value >>= 7;
74    if (Value || PadTo != 0) Byte |= 0x80;
75    OutStreamer.EmitIntValue(Byte, 1, /*addrspace*/0);
76  } while (Value);
77
78  if (PadTo) {
79    if (PadTo > 1)
80      OutStreamer.EmitFill(PadTo - 1, 0x80/*fillval*/, 0/*addrspace*/);
81    OutStreamer.EmitFill(1, 0/*fillval*/, 0/*addrspace*/);
82  }
83}
84
85/// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
86void AsmPrinter::EmitCFAByte(unsigned Val) const {
87  if (isVerbose()) {
88    if (Val >= dwarf::DW_CFA_offset && Val < dwarf::DW_CFA_offset+64)
89      OutStreamer.AddComment("DW_CFA_offset + Reg (" +
90                             Twine(Val-dwarf::DW_CFA_offset) + ")");
91    else
92      OutStreamer.AddComment(dwarf::CallFrameString(Val));
93  }
94  OutStreamer.EmitIntValue(Val, 1, 0/*addrspace*/);
95}
96
97static const char *DecodeDWARFEncoding(unsigned Encoding) {
98  switch (Encoding) {
99  case dwarf::DW_EH_PE_absptr: return "absptr";
100  case dwarf::DW_EH_PE_omit:   return "omit";
101  case dwarf::DW_EH_PE_pcrel:  return "pcrel";
102  case dwarf::DW_EH_PE_udata4: return "udata4";
103  case dwarf::DW_EH_PE_udata8: return "udata8";
104  case dwarf::DW_EH_PE_sdata4: return "sdata4";
105  case dwarf::DW_EH_PE_sdata8: return "sdata8";
106  case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4: return "pcrel udata4";
107  case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4: return "pcrel sdata4";
108  case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8: return "pcrel udata8";
109  case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8: return "pcrel sdata8";
110  case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata4:
111    return "indirect pcrel udata4";
112  case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata4:
113    return "indirect pcrel sdata4";
114  case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata8:
115    return "indirect pcrel udata8";
116  case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata8:
117    return "indirect pcrel sdata8";
118  }
119
120  return "<unknown encoding>";
121}
122
123
124/// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
125/// encoding.  If verbose assembly output is enabled, we output comments
126/// describing the encoding.  Desc is an optional string saying what the
127/// encoding is specifying (e.g. "LSDA").
128void AsmPrinter::EmitEncodingByte(unsigned Val, const char *Desc) const {
129  if (isVerbose()) {
130    if (Desc != 0)
131      OutStreamer.AddComment(Twine(Desc)+" Encoding = " +
132                             Twine(DecodeDWARFEncoding(Val)));
133    else
134      OutStreamer.AddComment(Twine("Encoding = ") +
135                             DecodeDWARFEncoding(Val));
136  }
137
138  OutStreamer.EmitIntValue(Val, 1, 0/*addrspace*/);
139}
140
141/// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
142unsigned AsmPrinter::GetSizeOfEncodedValue(unsigned Encoding) const {
143  if (Encoding == dwarf::DW_EH_PE_omit)
144    return 0;
145
146  switch (Encoding & 0x07) {
147  default: assert(0 && "Invalid encoded value.");
148  case dwarf::DW_EH_PE_absptr: return TM.getTargetData()->getPointerSize();
149  case dwarf::DW_EH_PE_udata2: return 2;
150  case dwarf::DW_EH_PE_udata4: return 4;
151  case dwarf::DW_EH_PE_udata8: return 8;
152  }
153}
154
155void AsmPrinter::EmitReference(const MCSymbol *Sym, unsigned Encoding) const {
156  const TargetLoweringObjectFile &TLOF = getObjFileLowering();
157
158  const MCExpr *Exp =
159    TLOF.getExprForDwarfReference(Sym, Mang, MMI, Encoding, OutStreamer);
160  OutStreamer.EmitValue(Exp, GetSizeOfEncodedValue(Encoding), /*addrspace*/0);
161}
162
163void AsmPrinter::EmitReference(const GlobalValue *GV, unsigned Encoding)const{
164  const TargetLoweringObjectFile &TLOF = getObjFileLowering();
165
166  const MCExpr *Exp =
167    TLOF.getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, OutStreamer);
168  OutStreamer.EmitValue(Exp, GetSizeOfEncodedValue(Encoding), /*addrspace*/0);
169}
170
171/// EmitSectionOffset - Emit the 4-byte offset of Label from the start of its
172/// section.  This can be done with a special directive if the target supports
173/// it (e.g. cygwin) or by emitting it as an offset from a label at the start
174/// of the section.
175///
176/// SectionLabel is a temporary label emitted at the start of the section that
177/// Label lives in.
178void AsmPrinter::EmitSectionOffset(const MCSymbol *Label,
179                                   const MCSymbol *SectionLabel) const {
180  // On COFF targets, we have to emit the special .secrel32 directive.
181  if (const char *SecOffDir = MAI->getDwarfSectionOffsetDirective()) {
182    // FIXME: MCize.
183    OutStreamer.EmitRawText(SecOffDir + Twine(Label->getName()));
184    return;
185  }
186
187  // Get the section that we're referring to, based on SectionLabel.
188  const MCSection &Section = SectionLabel->getSection();
189
190  // If Label has already been emitted, verify that it is in the same section as
191  // section label for sanity.
192  assert((!Label->isInSection() || &Label->getSection() == &Section) &&
193         "Section offset using wrong section base for label");
194
195  // If the section in question will end up with an address of 0 anyway, we can
196  // just emit an absolute reference to save a relocation.
197  if (Section.isBaseAddressKnownZero()) {
198    OutStreamer.EmitSymbolValue(Label, 4, 0/*AddrSpace*/);
199    return;
200  }
201
202  // Otherwise, emit it as a label difference from the start of the section.
203  EmitLabelDifference(Label, SectionLabel, 4);
204}
205
206//===----------------------------------------------------------------------===//
207// Dwarf Lowering Routines
208//===----------------------------------------------------------------------===//
209
210
211/// EmitFrameMoves - Emit frame instructions to describe the layout of the
212/// frame.
213void AsmPrinter::EmitFrameMoves(const std::vector<MachineMove> &Moves,
214                                MCSymbol *BaseLabel, bool isEH) const {
215  const TargetRegisterInfo *RI = TM.getRegisterInfo();
216
217  int stackGrowth = TM.getTargetData()->getPointerSize();
218  if (TM.getFrameInfo()->getStackGrowthDirection() !=
219      TargetFrameInfo::StackGrowsUp)
220    stackGrowth *= -1;
221
222  for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
223    const MachineMove &Move = Moves[i];
224    MCSymbol *Label = Move.getLabel();
225    // Throw out move if the label is invalid.
226    if (Label && !Label->isDefined()) continue; // Not emitted, in dead code.
227
228    const MachineLocation &Dst = Move.getDestination();
229    const MachineLocation &Src = Move.getSource();
230
231    // Advance row if new location.
232    if (BaseLabel && Label) {
233      MCSymbol *ThisSym = Label;
234      if (ThisSym != BaseLabel) {
235        EmitCFAByte(dwarf::DW_CFA_advance_loc4);
236        EmitLabelDifference(ThisSym, BaseLabel, 4);
237        BaseLabel = ThisSym;
238      }
239    }
240
241    // If advancing cfa.
242    if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
243      assert(!Src.isReg() && "Machine move not supported yet.");
244
245      if (Src.getReg() == MachineLocation::VirtualFP) {
246        EmitCFAByte(dwarf::DW_CFA_def_cfa_offset);
247      } else {
248        EmitCFAByte(dwarf::DW_CFA_def_cfa);
249        EmitULEB128(RI->getDwarfRegNum(Src.getReg(), isEH), "Register");
250      }
251
252      EmitULEB128(-Src.getOffset(), "Offset");
253      continue;
254    }
255
256    if (Src.isReg() && Src.getReg() == MachineLocation::VirtualFP) {
257      assert(Dst.isReg() && "Machine move not supported yet.");
258      EmitCFAByte(dwarf::DW_CFA_def_cfa_register);
259      EmitULEB128(RI->getDwarfRegNum(Dst.getReg(), isEH), "Register");
260      continue;
261    }
262
263    unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
264    int Offset = Dst.getOffset() / stackGrowth;
265
266    if (Offset < 0) {
267      EmitCFAByte(dwarf::DW_CFA_offset_extended_sf);
268      EmitULEB128(Reg, "Reg");
269      EmitSLEB128(Offset, "Offset");
270    } else if (Reg < 64) {
271      EmitCFAByte(dwarf::DW_CFA_offset + Reg);
272      EmitULEB128(Offset, "Offset");
273    } else {
274      EmitCFAByte(dwarf::DW_CFA_offset_extended);
275      EmitULEB128(Reg, "Reg");
276      EmitULEB128(Offset, "Offset");
277    }
278  }
279}
280