1 //===-- AsmPrinterDwarf.cpp - AsmPrinter Dwarf Support --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Dwarf emissions parts of AsmPrinter.
12 //===----------------------------------------------------------------------===//
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"
30 //===----------------------------------------------------------------------===//
31 // Dwarf Emission Helper Routines
32 //===----------------------------------------------------------------------===//
34 /// EmitSLEB128 - emit the specified signed leb128 value.
35 void AsmPrinter::EmitSLEB128(int Value, const char *Desc) const {
36 if (isVerbose() && Desc)
37 OutStreamer.AddComment(Desc);
39 if (MAI->hasLEB128()) {
41 OutStreamer.EmitRawText("\t.sleb128\t" + Twine(Value));
45 // If we don't have .sleb128, emit as .bytes.
46 int Sign = Value >> (8 * sizeof(Value) - 1);
50 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
52 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
53 if (IsMore) Byte |= 0x80;
54 OutStreamer.EmitIntValue(Byte, 1, /*addrspace*/0);
58 /// EmitULEB128 - emit the specified signed leb128 value.
59 void AsmPrinter::EmitULEB128(unsigned Value, const char *Desc,
60 unsigned PadTo) const {
61 if (isVerbose() && Desc)
62 OutStreamer.AddComment(Desc);
64 if (MAI->hasLEB128() && PadTo == 0) {
66 OutStreamer.EmitRawText("\t.uleb128\t" + Twine(Value));
70 // If we don't have .uleb128 or we want to emit padding, emit as .bytes.
72 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
74 if (Value || PadTo != 0) Byte |= 0x80;
75 OutStreamer.EmitIntValue(Byte, 1, /*addrspace*/0);
80 OutStreamer.EmitFill(PadTo - 1, 0x80/*fillval*/, 0/*addrspace*/);
81 OutStreamer.EmitFill(1, 0/*fillval*/, 0/*addrspace*/);
85 /// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
86 void AsmPrinter::EmitCFAByte(unsigned Val) const {
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) + ")");
92 OutStreamer.AddComment(dwarf::CallFrameString(Val));
94 OutStreamer.EmitIntValue(Val, 1, 0/*addrspace*/);
97 static const char *DecodeDWARFEncoding(unsigned 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";
120 return "<unknown encoding>";
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").
128 void AsmPrinter::EmitEncodingByte(unsigned Val, const char *Desc) const {
131 OutStreamer.AddComment(Twine(Desc)+" Encoding = " +
132 Twine(DecodeDWARFEncoding(Val)));
134 OutStreamer.AddComment(Twine("Encoding = ") +
135 DecodeDWARFEncoding(Val));
138 OutStreamer.EmitIntValue(Val, 1, 0/*addrspace*/);
141 /// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
142 unsigned AsmPrinter::GetSizeOfEncodedValue(unsigned Encoding) const {
143 if (Encoding == dwarf::DW_EH_PE_omit)
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;
155 void AsmPrinter::EmitReference(const MCSymbol *Sym, unsigned Encoding) const {
156 const TargetLoweringObjectFile &TLOF = getObjFileLowering();
159 TLOF.getExprForDwarfReference(Sym, Mang, MMI, Encoding, OutStreamer);
160 OutStreamer.EmitValue(Exp, GetSizeOfEncodedValue(Encoding), /*addrspace*/0);
163 void AsmPrinter::EmitReference(const GlobalValue *GV, unsigned Encoding)const{
164 const TargetLoweringObjectFile &TLOF = getObjFileLowering();
167 TLOF.getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, OutStreamer);
168 OutStreamer.EmitValue(Exp, GetSizeOfEncodedValue(Encoding), /*addrspace*/0);
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
176 /// SectionLabel is a temporary label emitted at the start of the section that
178 void 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()) {
183 OutStreamer.EmitRawText(SecOffDir + Twine(Label->getName()));
187 // Get the section that we're referring to, based on SectionLabel.
188 const MCSection &Section = SectionLabel->getSection();
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");
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*/);
202 // Otherwise, emit it as a label difference from the start of the section.
203 EmitLabelDifference(Label, SectionLabel, 4);
206 //===----------------------------------------------------------------------===//
207 // Dwarf Lowering Routines
208 //===----------------------------------------------------------------------===//
211 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
213 void AsmPrinter::EmitFrameMoves(const std::vector<MachineMove> &Moves,
214 MCSymbol *BaseLabel, bool isEH) const {
215 const TargetRegisterInfo *RI = TM.getRegisterInfo();
217 int stackGrowth = TM.getTargetData()->getPointerSize();
218 if (TM.getFrameInfo()->getStackGrowthDirection() !=
219 TargetFrameInfo::StackGrowsUp)
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.
228 const MachineLocation &Dst = Move.getDestination();
229 const MachineLocation &Src = Move.getSource();
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);
242 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
243 assert(!Src.isReg() && "Machine move not supported yet.");
245 if (Src.getReg() == MachineLocation::VirtualFP) {
246 EmitCFAByte(dwarf::DW_CFA_def_cfa_offset);
248 EmitCFAByte(dwarf::DW_CFA_def_cfa);
249 EmitULEB128(RI->getDwarfRegNum(Src.getReg(), isEH), "Register");
252 EmitULEB128(-Src.getOffset(), "Offset");
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");
263 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
264 int Offset = Dst.getOffset() / stackGrowth;
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");
274 EmitCFAByte(dwarf::DW_CFA_offset_extended);
275 EmitULEB128(Reg, "Reg");
276 EmitULEB128(Offset, "Offset");