blob: 98a1bf2f1ce4f8218a52c7b460acbfdc57b6b64b [file] [log] [blame]
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001//===-- 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"
Chris Lattner02b86b92010-04-04 23:41:46 +000016#include "llvm/CodeGen/MachineLocation.h"
Chris Lattner7e1a8f82010-04-04 19:09:29 +000017#include "llvm/MC/MCAsmInfo.h"
Chris Lattner6189ed12010-04-04 23:25:33 +000018#include "llvm/MC/MCSection.h"
Chris Lattner7e1a8f82010-04-04 19:09:29 +000019#include "llvm/MC/MCStreamer.h"
Chris Lattner6189ed12010-04-04 23:25:33 +000020#include "llvm/MC/MCSymbol.h"
Chris Lattnerd2af7852010-04-04 20:20:50 +000021#include "llvm/Target/TargetData.h"
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000022#include "llvm/Target/TargetFrameLowering.h"
Chris Lattnerd2af7852010-04-04 20:20:50 +000023#include "llvm/Target/TargetLoweringObjectFile.h"
24#include "llvm/Target/TargetMachine.h"
Chris Lattner02b86b92010-04-04 23:41:46 +000025#include "llvm/Target/TargetRegisterInfo.h"
Chris Lattner7e1a8f82010-04-04 19:09:29 +000026#include "llvm/ADT/Twine.h"
Chris Lattner7a101f42010-04-04 20:01:25 +000027#include "llvm/Support/Dwarf.h"
Chris Lattner7e1a8f82010-04-04 19:09:29 +000028using namespace llvm;
29
Chris Lattner02b86b92010-04-04 23:41:46 +000030//===----------------------------------------------------------------------===//
31// Dwarf Emission Helper Routines
32//===----------------------------------------------------------------------===//
33
Chris Lattner7e1a8f82010-04-04 19:09:29 +000034/// 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
Rafael Espindola73873452010-11-04 18:17:08 +000039 if (MAI->hasLEB128()) {
40 OutStreamer.EmitSLEB128IntValue(Value);
Chris Lattner7e1a8f82010-04-04 19:09:29 +000041 return;
42 }
43
44 // If we don't have .sleb128, emit as .bytes.
45 int Sign = Value >> (8 * sizeof(Value) - 1);
46 bool IsMore;
47
48 do {
49 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
50 Value >>= 7;
51 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
52 if (IsMore) Byte |= 0x80;
53 OutStreamer.EmitIntValue(Byte, 1, /*addrspace*/0);
54 } while (IsMore);
55}
56
57/// EmitULEB128 - emit the specified signed leb128 value.
58void AsmPrinter::EmitULEB128(unsigned Value, const char *Desc,
59 unsigned PadTo) const {
60 if (isVerbose() && Desc)
61 OutStreamer.AddComment(Desc);
Rafael Espindola73873452010-11-04 18:17:08 +000062
63 // FIXME: Should we add a PadTo option to the streamer?
64 if (MAI->hasLEB128() && PadTo == 0) {
65 OutStreamer.EmitULEB128IntValue(Value);
Chris Lattner7e1a8f82010-04-04 19:09:29 +000066 return;
67 }
68
69 // If we don't have .uleb128 or we want to emit padding, emit as .bytes.
70 do {
71 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
72 Value >>= 7;
73 if (Value || PadTo != 0) Byte |= 0x80;
74 OutStreamer.EmitIntValue(Byte, 1, /*addrspace*/0);
75 } while (Value);
76
77 if (PadTo) {
78 if (PadTo > 1)
79 OutStreamer.EmitFill(PadTo - 1, 0x80/*fillval*/, 0/*addrspace*/);
80 OutStreamer.EmitFill(1, 0/*fillval*/, 0/*addrspace*/);
81 }
82}
83
Chris Lattner7a101f42010-04-04 20:01:25 +000084/// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
85void AsmPrinter::EmitCFAByte(unsigned Val) const {
86 if (isVerbose()) {
87 if (Val >= dwarf::DW_CFA_offset && Val < dwarf::DW_CFA_offset+64)
88 OutStreamer.AddComment("DW_CFA_offset + Reg (" +
89 Twine(Val-dwarf::DW_CFA_offset) + ")");
90 else
91 OutStreamer.AddComment(dwarf::CallFrameString(Val));
92 }
93 OutStreamer.EmitIntValue(Val, 1, 0/*addrspace*/);
94}
95
Chris Lattnerca6190b2010-04-04 20:04:21 +000096static const char *DecodeDWARFEncoding(unsigned Encoding) {
97 switch (Encoding) {
98 case dwarf::DW_EH_PE_absptr: return "absptr";
99 case dwarf::DW_EH_PE_omit: return "omit";
100 case dwarf::DW_EH_PE_pcrel: return "pcrel";
101 case dwarf::DW_EH_PE_udata4: return "udata4";
102 case dwarf::DW_EH_PE_udata8: return "udata8";
103 case dwarf::DW_EH_PE_sdata4: return "sdata4";
104 case dwarf::DW_EH_PE_sdata8: return "sdata8";
105 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4: return "pcrel udata4";
106 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4: return "pcrel sdata4";
107 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8: return "pcrel udata8";
108 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8: return "pcrel sdata8";
109 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata4:
110 return "indirect pcrel udata4";
111 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata4:
112 return "indirect pcrel sdata4";
113 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata8:
114 return "indirect pcrel udata8";
115 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata8:
116 return "indirect pcrel sdata8";
117 }
118
119 return "<unknown encoding>";
120}
121
122
123/// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
124/// encoding. If verbose assembly output is enabled, we output comments
125/// describing the encoding. Desc is an optional string saying what the
126/// encoding is specifying (e.g. "LSDA").
Chris Lattner02b86b92010-04-04 23:41:46 +0000127void AsmPrinter::EmitEncodingByte(unsigned Val, const char *Desc) const {
Chris Lattnerca6190b2010-04-04 20:04:21 +0000128 if (isVerbose()) {
129 if (Desc != 0)
130 OutStreamer.AddComment(Twine(Desc)+" Encoding = " +
131 Twine(DecodeDWARFEncoding(Val)));
132 else
133 OutStreamer.AddComment(Twine("Encoding = ") +
134 DecodeDWARFEncoding(Val));
135 }
136
137 OutStreamer.EmitIntValue(Val, 1, 0/*addrspace*/);
138}
139
Chris Lattnerd2af7852010-04-04 20:20:50 +0000140/// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
141unsigned AsmPrinter::GetSizeOfEncodedValue(unsigned Encoding) const {
142 if (Encoding == dwarf::DW_EH_PE_omit)
143 return 0;
144
145 switch (Encoding & 0x07) {
146 default: assert(0 && "Invalid encoded value.");
147 case dwarf::DW_EH_PE_absptr: return TM.getTargetData()->getPointerSize();
148 case dwarf::DW_EH_PE_udata2: return 2;
149 case dwarf::DW_EH_PE_udata4: return 4;
150 case dwarf::DW_EH_PE_udata8: return 8;
151 }
152}
153
154void AsmPrinter::EmitReference(const MCSymbol *Sym, unsigned Encoding) const {
155 const TargetLoweringObjectFile &TLOF = getObjFileLowering();
156
157 const MCExpr *Exp =
158 TLOF.getExprForDwarfReference(Sym, Mang, MMI, Encoding, OutStreamer);
Rafael Espindola0bbe0b42010-12-06 17:27:56 +0000159 OutStreamer.EmitAbsValue(Exp, GetSizeOfEncodedValue(Encoding));
Chris Lattnerd2af7852010-04-04 20:20:50 +0000160}
161
162void AsmPrinter::EmitReference(const GlobalValue *GV, unsigned Encoding)const{
163 const TargetLoweringObjectFile &TLOF = getObjFileLowering();
164
165 const MCExpr *Exp =
166 TLOF.getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, OutStreamer);
167 OutStreamer.EmitValue(Exp, GetSizeOfEncodedValue(Encoding), /*addrspace*/0);
168}
Chris Lattner6189ed12010-04-04 23:25:33 +0000169
170/// EmitSectionOffset - Emit the 4-byte offset of Label from the start of its
171/// section. This can be done with a special directive if the target supports
172/// it (e.g. cygwin) or by emitting it as an offset from a label at the start
173/// of the section.
174///
175/// SectionLabel is a temporary label emitted at the start of the section that
176/// Label lives in.
177void AsmPrinter::EmitSectionOffset(const MCSymbol *Label,
178 const MCSymbol *SectionLabel) const {
179 // On COFF targets, we have to emit the special .secrel32 directive.
180 if (const char *SecOffDir = MAI->getDwarfSectionOffsetDirective()) {
181 // FIXME: MCize.
182 OutStreamer.EmitRawText(SecOffDir + Twine(Label->getName()));
183 return;
184 }
185
186 // Get the section that we're referring to, based on SectionLabel.
187 const MCSection &Section = SectionLabel->getSection();
188
189 // If Label has already been emitted, verify that it is in the same section as
190 // section label for sanity.
191 assert((!Label->isInSection() || &Label->getSection() == &Section) &&
192 "Section offset using wrong section base for label");
193
194 // If the section in question will end up with an address of 0 anyway, we can
195 // just emit an absolute reference to save a relocation.
196 if (Section.isBaseAddressKnownZero()) {
197 OutStreamer.EmitSymbolValue(Label, 4, 0/*AddrSpace*/);
198 return;
199 }
200
201 // Otherwise, emit it as a label difference from the start of the section.
202 EmitLabelDifference(Label, SectionLabel, 4);
203}
204
Chris Lattner02b86b92010-04-04 23:41:46 +0000205//===----------------------------------------------------------------------===//
206// Dwarf Lowering Routines
207//===----------------------------------------------------------------------===//
Chris Lattner6189ed12010-04-04 23:25:33 +0000208
Chris Lattner02b86b92010-04-04 23:41:46 +0000209
210/// EmitFrameMoves - Emit frame instructions to describe the layout of the
211/// frame.
212void AsmPrinter::EmitFrameMoves(const std::vector<MachineMove> &Moves,
213 MCSymbol *BaseLabel, bool isEH) const {
214 const TargetRegisterInfo *RI = TM.getRegisterInfo();
215
216 int stackGrowth = TM.getTargetData()->getPointerSize();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000217 if (TM.getFrameLowering()->getStackGrowthDirection() !=
218 TargetFrameLowering::StackGrowsUp)
Chris Lattner02b86b92010-04-04 23:41:46 +0000219 stackGrowth *= -1;
220
221 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
222 const MachineMove &Move = Moves[i];
223 MCSymbol *Label = Move.getLabel();
224 // Throw out move if the label is invalid.
225 if (Label && !Label->isDefined()) continue; // Not emitted, in dead code.
226
227 const MachineLocation &Dst = Move.getDestination();
228 const MachineLocation &Src = Move.getSource();
229
230 // Advance row if new location.
231 if (BaseLabel && Label) {
232 MCSymbol *ThisSym = Label;
233 if (ThisSym != BaseLabel) {
234 EmitCFAByte(dwarf::DW_CFA_advance_loc4);
235 EmitLabelDifference(ThisSym, BaseLabel, 4);
236 BaseLabel = ThisSym;
237 }
238 }
239
240 // If advancing cfa.
241 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
242 assert(!Src.isReg() && "Machine move not supported yet.");
243
244 if (Src.getReg() == MachineLocation::VirtualFP) {
245 EmitCFAByte(dwarf::DW_CFA_def_cfa_offset);
246 } else {
247 EmitCFAByte(dwarf::DW_CFA_def_cfa);
248 EmitULEB128(RI->getDwarfRegNum(Src.getReg(), isEH), "Register");
249 }
250
251 EmitULEB128(-Src.getOffset(), "Offset");
252 continue;
253 }
254
255 if (Src.isReg() && Src.getReg() == MachineLocation::VirtualFP) {
256 assert(Dst.isReg() && "Machine move not supported yet.");
257 EmitCFAByte(dwarf::DW_CFA_def_cfa_register);
258 EmitULEB128(RI->getDwarfRegNum(Dst.getReg(), isEH), "Register");
259 continue;
260 }
261
262 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
263 int Offset = Dst.getOffset() / stackGrowth;
264
265 if (Offset < 0) {
266 EmitCFAByte(dwarf::DW_CFA_offset_extended_sf);
267 EmitULEB128(Reg, "Reg");
268 EmitSLEB128(Offset, "Offset");
269 } else if (Reg < 64) {
270 EmitCFAByte(dwarf::DW_CFA_offset + Reg);
271 EmitULEB128(Offset, "Offset");
272 } else {
273 EmitCFAByte(dwarf::DW_CFA_offset_extended);
274 EmitULEB128(Reg, "Reg");
275 EmitULEB128(Offset, "Offset");
276 }
277 }
278}
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +0000279
280/// EmitFrameMoves - Emit frame instructions to describe the layout of the
281/// frame.
282void AsmPrinter::EmitCFIFrameMoves(const std::vector<MachineMove> &Moves) const {
283 const TargetRegisterInfo *RI = TM.getRegisterInfo();
284
285 int stackGrowth = TM.getTargetData()->getPointerSize();
286 if (TM.getFrameLowering()->getStackGrowthDirection() !=
287 TargetFrameLowering::StackGrowsUp)
288 stackGrowth *= -1;
289
290 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
291 const MachineMove &Move = Moves[i];
292 MCSymbol *Label = Move.getLabel();
293 // Throw out move if the label is invalid.
294 if (Label && !Label->isDefined()) continue; // Not emitted, in dead code.
295
296 const MachineLocation &Dst = Move.getDestination();
297 const MachineLocation &Src = Move.getSource();
298
299 // If advancing cfa.
300 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
301 assert(!Src.isReg() && "Machine move not supported yet.");
302
303 if (Src.getReg() == MachineLocation::VirtualFP) {
304 OutStreamer.EmitCFIDefCfaOffset(-Src.getOffset());
305 } else {
306 assert("Machine move not supported yet");
307 // Reg + Offset
308 }
309 } else if (Src.isReg() && Src.getReg() == MachineLocation::VirtualFP) {
310 assert(Dst.isReg() && "Machine move not supported yet.");
311 OutStreamer.EmitCFIDefCfaRegister(RI->getDwarfRegNum(Dst.getReg(), true));
312 } else {
313 assert(!Dst.isReg() && "Machine move not supported yet.");
314 OutStreamer.EmitCFIOffset(RI->getDwarfRegNum(Src.getReg(), true),
315 Dst.getOffset());
316 }
317 }
318}