blob: 4e79cfa3fcf3502921a4343e0f13c5c16acfefd5 [file] [log] [blame]
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00001//===- lib/MC/MCDwarf.cpp - MCDwarf implementation ------------------------===//
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
Rafael Espindola767b1be2010-12-04 00:31:13 +000010#include "llvm/MC/MCAsmInfo.h"
Kevin Enderby7cbf73a2010-07-28 20:55:35 +000011#include "llvm/MC/MCDwarf.h"
Rafael Espindolaad8aaa02010-11-22 11:53:17 +000012#include "llvm/MC/MCStreamer.h"
Kevin Enderbyc0957932010-09-30 16:52:03 +000013#include "llvm/MC/MCSymbol.h"
14#include "llvm/MC/MCExpr.h"
15#include "llvm/MC/MCContext.h"
16#include "llvm/MC/MCObjectWriter.h"
Kevin Enderby7cbf73a2010-07-28 20:55:35 +000017#include "llvm/Support/Debug.h"
Rafael Espindolafe024d02010-12-28 18:36:23 +000018#include "llvm/Support/ErrorHandling.h"
Kevin Enderby7cbf73a2010-07-28 20:55:35 +000019#include "llvm/Support/raw_ostream.h"
Rafael Espindola89b93722010-12-10 07:39:47 +000020#include "llvm/Target/TargetAsmInfo.h"
Bill Wendling2b2dc7c2011-07-06 22:52:32 +000021#include "llvm/ADT/FoldingSet.h"
22#include "llvm/ADT/SmallString.h"
23#include "llvm/ADT/StringExtras.h"
24#include "llvm/ADT/Twine.h"
Kevin Enderby7cbf73a2010-07-28 20:55:35 +000025using namespace llvm;
26
Kevin Enderbyc0957932010-09-30 16:52:03 +000027// Given a special op, return the address skip amount (in units of
28// DWARF2_LINE_MIN_INSN_LENGTH.
29#define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE)
30
31// The maximum address skip amount that can be encoded with a special op.
Bill Wendlingc3882162011-06-30 23:59:38 +000032#define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255)
Kevin Enderbyc0957932010-09-30 16:52:03 +000033
34// First special line opcode - leave room for the standard opcodes.
35// Note: If you want to change this, you'll have to update the
36// "standard_opcode_lengths" table that is emitted in DwarfFileTable::Emit().
Bill Wendlingc3882162011-06-30 23:59:38 +000037#define DWARF2_LINE_OPCODE_BASE 13
Kevin Enderbyc0957932010-09-30 16:52:03 +000038
39// Minimum line offset in a special line info. opcode. This value
40// was chosen to give a reasonable range of values.
Bill Wendlingc3882162011-06-30 23:59:38 +000041#define DWARF2_LINE_BASE -5
Kevin Enderbyc0957932010-09-30 16:52:03 +000042
43// Range of line offsets in a special line info. opcode.
Bill Wendlingc3882162011-06-30 23:59:38 +000044#define DWARF2_LINE_RANGE 14
Kevin Enderbyc0957932010-09-30 16:52:03 +000045
46// Define the architecture-dependent minimum instruction length (in bytes).
47// This value should be rather too small than too big.
Bill Wendlingc3882162011-06-30 23:59:38 +000048#define DWARF2_LINE_MIN_INSN_LENGTH 1
Kevin Enderbyc0957932010-09-30 16:52:03 +000049
50// Note: when DWARF2_LINE_MIN_INSN_LENGTH == 1 which is the current setting,
51// this routine is a nop and will be optimized away.
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +000052static inline uint64_t ScaleAddrDelta(uint64_t AddrDelta) {
Kevin Enderbyc0957932010-09-30 16:52:03 +000053 if (DWARF2_LINE_MIN_INSN_LENGTH == 1)
54 return AddrDelta;
55 if (AddrDelta % DWARF2_LINE_MIN_INSN_LENGTH != 0) {
56 // TODO: report this error, but really only once.
57 ;
58 }
59 return AddrDelta / DWARF2_LINE_MIN_INSN_LENGTH;
60}
61
62//
63// This is called when an instruction is assembled into the specified section
64// and if there is information from the last .loc directive that has yet to have
65// a line entry made for it is made.
66//
Rafael Espindola195a0ce2010-11-19 02:26:16 +000067void MCLineEntry::Make(MCStreamer *MCOS, const MCSection *Section) {
Kevin Enderbyc0957932010-09-30 16:52:03 +000068 if (!MCOS->getContext().getDwarfLocSeen())
69 return;
70
71 // Create a symbol at in the current section for use in the line entry.
72 MCSymbol *LineSym = MCOS->getContext().CreateTempSymbol();
73 // Set the value of the symbol to use for the MCLineEntry.
74 MCOS->EmitLabel(LineSym);
75
76 // Get the current .loc info saved in the context.
77 const MCDwarfLoc &DwarfLoc = MCOS->getContext().getCurrentDwarfLoc();
78
79 // Create a (local) line entry with the symbol and the current .loc info.
80 MCLineEntry LineEntry(LineSym, DwarfLoc);
81
82 // clear DwarfLocSeen saying the current .loc info is now used.
Kevin Enderby3f55c242010-10-04 20:17:24 +000083 MCOS->getContext().ClearDwarfLocSeen();
Kevin Enderbyc0957932010-09-30 16:52:03 +000084
85 // Get the MCLineSection for this section, if one does not exist for this
86 // section create it.
Rafael Espindola17fd7bd2010-11-19 07:41:23 +000087 const DenseMap<const MCSection *, MCLineSection *> &MCLineSections =
Kevin Enderbyc0957932010-09-30 16:52:03 +000088 MCOS->getContext().getMCLineSections();
Rafael Espindola17fd7bd2010-11-19 07:41:23 +000089 MCLineSection *LineSection = MCLineSections.lookup(Section);
Kevin Enderbyc0957932010-09-30 16:52:03 +000090 if (!LineSection) {
91 // Create a new MCLineSection. This will be deleted after the dwarf line
92 // table is created using it by iterating through the MCLineSections
93 // DenseMap.
94 LineSection = new MCLineSection;
95 // Save a pointer to the new LineSection into the MCLineSections DenseMap.
Rafael Espindola17fd7bd2010-11-19 07:41:23 +000096 MCOS->getContext().addMCLineSection(Section, LineSection);
Kevin Enderbyc0957932010-09-30 16:52:03 +000097 }
98
99 // Add the line entry to this section's entries.
100 LineSection->addLineEntry(LineEntry);
101}
102
103//
104// This helper routine returns an expression of End - Start + IntVal .
105//
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000106static inline const MCExpr *MakeStartMinusEndExpr(const MCStreamer &MCOS,
107 const MCSymbol &Start,
108 const MCSymbol &End,
109 int IntVal) {
Kevin Enderbyc0957932010-09-30 16:52:03 +0000110 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
111 const MCExpr *Res =
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000112 MCSymbolRefExpr::Create(&End, Variant, MCOS.getContext());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000113 const MCExpr *RHS =
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000114 MCSymbolRefExpr::Create(&Start, Variant, MCOS.getContext());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000115 const MCExpr *Res1 =
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000116 MCBinaryExpr::Create(MCBinaryExpr::Sub, Res, RHS, MCOS.getContext());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000117 const MCExpr *Res2 =
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000118 MCConstantExpr::Create(IntVal, MCOS.getContext());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000119 const MCExpr *Res3 =
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000120 MCBinaryExpr::Create(MCBinaryExpr::Sub, Res1, Res2, MCOS.getContext());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000121 return Res3;
122}
123
Kevin Enderbyc0957932010-09-30 16:52:03 +0000124//
125// This emits the Dwarf line table for the specified section from the entries
126// in the LineSection.
127//
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000128static inline void EmitDwarfLineTable(MCStreamer *MCOS,
Kevin Enderbyc0957932010-09-30 16:52:03 +0000129 const MCSection *Section,
Rafael Espindola89b93722010-12-10 07:39:47 +0000130 const MCLineSection *LineSection) {
Kevin Enderbyc0957932010-09-30 16:52:03 +0000131 unsigned FileNum = 1;
132 unsigned LastLine = 1;
133 unsigned Column = 0;
134 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
135 unsigned Isa = 0;
Kevin Enderbyc0957932010-09-30 16:52:03 +0000136 MCSymbol *LastLabel = NULL;
Kevin Enderbyc0957932010-09-30 16:52:03 +0000137
138 // Loop through each MCLineEntry and encode the dwarf line number table.
Rafael Espindola17fd7bd2010-11-19 07:41:23 +0000139 for (MCLineSection::const_iterator
Kevin Enderbyc0957932010-09-30 16:52:03 +0000140 it = LineSection->getMCLineEntries()->begin(),
141 ie = LineSection->getMCLineEntries()->end(); it != ie; ++it) {
142
143 if (FileNum != it->getFileNum()) {
144 FileNum = it->getFileNum();
145 MCOS->EmitIntValue(dwarf::DW_LNS_set_file, 1);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000146 MCOS->EmitULEB128IntValue(FileNum);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000147 }
148 if (Column != it->getColumn()) {
149 Column = it->getColumn();
150 MCOS->EmitIntValue(dwarf::DW_LNS_set_column, 1);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000151 MCOS->EmitULEB128IntValue(Column);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000152 }
153 if (Isa != it->getIsa()) {
154 Isa = it->getIsa();
155 MCOS->EmitIntValue(dwarf::DW_LNS_set_isa, 1);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000156 MCOS->EmitULEB128IntValue(Isa);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000157 }
158 if ((it->getFlags() ^ Flags) & DWARF2_FLAG_IS_STMT) {
159 Flags = it->getFlags();
160 MCOS->EmitIntValue(dwarf::DW_LNS_negate_stmt, 1);
161 }
162 if (it->getFlags() & DWARF2_FLAG_BASIC_BLOCK)
163 MCOS->EmitIntValue(dwarf::DW_LNS_set_basic_block, 1);
164 if (it->getFlags() & DWARF2_FLAG_PROLOGUE_END)
165 MCOS->EmitIntValue(dwarf::DW_LNS_set_prologue_end, 1);
166 if (it->getFlags() & DWARF2_FLAG_EPILOGUE_BEGIN)
167 MCOS->EmitIntValue(dwarf::DW_LNS_set_epilogue_begin, 1);
168
Rafael Espindola64185cc2010-11-13 01:06:27 +0000169 int64_t LineDelta = static_cast<int64_t>(it->getLine()) - LastLine;
Kevin Enderbyc0957932010-09-30 16:52:03 +0000170 MCSymbol *Label = it->getLabel();
171
172 // At this point we want to emit/create the sequence to encode the delta in
173 // line numbers and the increment of the address from the previous Label
174 // and the current Label.
Rafael Espindola32a006e2010-12-03 00:55:40 +0000175 MCOS->EmitDwarfAdvanceLineAddr(LineDelta, LastLabel, Label);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000176
177 LastLine = it->getLine();
178 LastLabel = Label;
Kevin Enderbyc0957932010-09-30 16:52:03 +0000179 }
180
181 // Emit a DW_LNE_end_sequence for the end of the section.
182 // Using the pointer Section create a temporary label at the end of the
183 // section and use that and the LastLabel to compute the address delta
184 // and use INT64_MAX as the line delta which is the signal that this is
185 // actually a DW_LNE_end_sequence.
186
187 // Switch to the section to be able to create a symbol at its end.
188 MCOS->SwitchSection(Section);
Rafael Espindola89b93722010-12-10 07:39:47 +0000189
190 MCContext &context = MCOS->getContext();
Kevin Enderbyc0957932010-09-30 16:52:03 +0000191 // Create a symbol at the end of the section.
Rafael Espindola89b93722010-12-10 07:39:47 +0000192 MCSymbol *SectionEnd = context.CreateTempSymbol();
Kevin Enderbyc0957932010-09-30 16:52:03 +0000193 // Set the value of the symbol, as we are at the end of the section.
194 MCOS->EmitLabel(SectionEnd);
195
196 // Switch back the the dwarf line section.
Rafael Espindola89b93722010-12-10 07:39:47 +0000197 MCOS->SwitchSection(context.getTargetAsmInfo().getDwarfLineSection());
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000198
Rafael Espindola32a006e2010-12-03 00:55:40 +0000199 MCOS->EmitDwarfAdvanceLineAddr(INT64_MAX, LastLabel, SectionEnd);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000200}
201
202//
203// This emits the Dwarf file and the line tables.
204//
Rafael Espindola89b93722010-12-10 07:39:47 +0000205void MCDwarfFileTable::Emit(MCStreamer *MCOS) {
206 MCContext &context = MCOS->getContext();
Kevin Enderbyc0957932010-09-30 16:52:03 +0000207 // Switch to the section where the table will be emitted into.
Rafael Espindola89b93722010-12-10 07:39:47 +0000208 MCOS->SwitchSection(context.getTargetAsmInfo().getDwarfLineSection());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000209
210 // Create a symbol at the beginning of this section.
Rafael Espindola89b93722010-12-10 07:39:47 +0000211 MCSymbol *LineStartSym = context.CreateTempSymbol();
Kevin Enderbyc0957932010-09-30 16:52:03 +0000212 // Set the value of the symbol, as we are at the start of the section.
213 MCOS->EmitLabel(LineStartSym);
214
215 // Create a symbol for the end of the section (to be set when we get there).
Rafael Espindola89b93722010-12-10 07:39:47 +0000216 MCSymbol *LineEndSym = context.CreateTempSymbol();
Kevin Enderbyc0957932010-09-30 16:52:03 +0000217
218 // The first 4 bytes is the total length of the information for this
219 // compilation unit (not including these 4 bytes for the length).
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000220 MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *LineEndSym,4),
Rafael Espindola0bbe0b42010-12-06 17:27:56 +0000221 4);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000222
223 // Next 2 bytes is the Version, which is Dwarf 2.
224 MCOS->EmitIntValue(2, 2);
225
226 // Create a symbol for the end of the prologue (to be set when we get there).
Rafael Espindola89b93722010-12-10 07:39:47 +0000227 MCSymbol *ProEndSym = context.CreateTempSymbol(); // Lprologue_end
Kevin Enderbyc0957932010-09-30 16:52:03 +0000228
229 // Length of the prologue, is the next 4 bytes. Which is the start of the
230 // section to the end of the prologue. Not including the 4 bytes for the
231 // total length, the 2 bytes for the version, and these 4 bytes for the
232 // length of the prologue.
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000233 MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *ProEndSym,
Kevin Enderbyc0957932010-09-30 16:52:03 +0000234 (4 + 2 + 4)),
Rafael Espindola5d4918d2010-12-04 03:21:47 +0000235 4, 0);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000236
237 // Parameters of the state machine, are next.
238 MCOS->EmitIntValue(DWARF2_LINE_MIN_INSN_LENGTH, 1);
239 MCOS->EmitIntValue(DWARF2_LINE_DEFAULT_IS_STMT, 1);
240 MCOS->EmitIntValue(DWARF2_LINE_BASE, 1);
241 MCOS->EmitIntValue(DWARF2_LINE_RANGE, 1);
242 MCOS->EmitIntValue(DWARF2_LINE_OPCODE_BASE, 1);
243
244 // Standard opcode lengths
245 MCOS->EmitIntValue(0, 1); // length of DW_LNS_copy
246 MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_pc
247 MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_line
248 MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_file
249 MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_column
250 MCOS->EmitIntValue(0, 1); // length of DW_LNS_negate_stmt
251 MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_basic_block
252 MCOS->EmitIntValue(0, 1); // length of DW_LNS_const_add_pc
253 MCOS->EmitIntValue(1, 1); // length of DW_LNS_fixed_advance_pc
254 MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_prologue_end
255 MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_epilogue_begin
256 MCOS->EmitIntValue(1, 1); // DW_LNS_set_isa
257
258 // Put out the directory and file tables.
259
260 // First the directory table.
261 const std::vector<StringRef> &MCDwarfDirs =
Rafael Espindola89b93722010-12-10 07:39:47 +0000262 context.getMCDwarfDirs();
Kevin Enderbyc0957932010-09-30 16:52:03 +0000263 for (unsigned i = 0; i < MCDwarfDirs.size(); i++) {
264 MCOS->EmitBytes(MCDwarfDirs[i], 0); // the DirectoryName
265 MCOS->EmitBytes(StringRef("\0", 1), 0); // the null term. of the string
266 }
267 MCOS->EmitIntValue(0, 1); // Terminate the directory list
268
269 // Second the file table.
270 const std::vector<MCDwarfFile *> &MCDwarfFiles =
271 MCOS->getContext().getMCDwarfFiles();
272 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
273 MCOS->EmitBytes(MCDwarfFiles[i]->getName(), 0); // FileName
274 MCOS->EmitBytes(StringRef("\0", 1), 0); // the null term. of the string
Rafael Espindola3ff57092010-11-02 17:22:24 +0000275 // the Directory num
276 MCOS->EmitULEB128IntValue(MCDwarfFiles[i]->getDirIndex());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000277 MCOS->EmitIntValue(0, 1); // last modification timestamp (always 0)
278 MCOS->EmitIntValue(0, 1); // filesize (always 0)
279 }
280 MCOS->EmitIntValue(0, 1); // Terminate the file list
281
282 // This is the end of the prologue, so set the value of the symbol at the
283 // end of the prologue (that was used in a previous expression).
284 MCOS->EmitLabel(ProEndSym);
285
286 // Put out the line tables.
Rafael Espindola17fd7bd2010-11-19 07:41:23 +0000287 const DenseMap<const MCSection *, MCLineSection *> &MCLineSections =
Kevin Enderbyc0957932010-09-30 16:52:03 +0000288 MCOS->getContext().getMCLineSections();
Rafael Espindola17fd7bd2010-11-19 07:41:23 +0000289 const std::vector<const MCSection *> &MCLineSectionOrder =
290 MCOS->getContext().getMCLineSectionOrder();
291 for (std::vector<const MCSection*>::const_iterator it =
Bill Wendlingc3882162011-06-30 23:59:38 +0000292 MCLineSectionOrder.begin(), ie = MCLineSectionOrder.end(); it != ie;
Rafael Espindola17fd7bd2010-11-19 07:41:23 +0000293 ++it) {
294 const MCSection *Sec = *it;
295 const MCLineSection *Line = MCLineSections.lookup(Sec);
Rafael Espindola89b93722010-12-10 07:39:47 +0000296 EmitDwarfLineTable(MCOS, Sec, Line);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000297
298 // Now delete the MCLineSections that were created in MCLineEntry::Make()
299 // and used to emit the line table.
Rafael Espindola17fd7bd2010-11-19 07:41:23 +0000300 delete Line;
Kevin Enderbyc0957932010-09-30 16:52:03 +0000301 }
302
Rafael Espindola767b1be2010-12-04 00:31:13 +0000303 if (MCOS->getContext().getAsmInfo().getLinkerRequiresNonEmptyDwarfLines()
304 && MCLineSectionOrder.begin() == MCLineSectionOrder.end()) {
Rafael Espindolaa8de83c2010-12-03 23:36:59 +0000305 // The darwin9 linker has a bug (see PR8715). For for 32-bit architectures
306 // it requires:
307 // total_length >= prologue_length + 10
308 // We are 4 bytes short, since we have total_length = 51 and
309 // prologue_length = 45
Devang Patel5113cdb2010-12-03 00:10:48 +0000310
Rafael Espindolaa8de83c2010-12-03 23:36:59 +0000311 // The regular end_sequence should be sufficient.
312 MCDwarfLineAddr::Emit(MCOS, INT64_MAX, 0);
Devang Patel5113cdb2010-12-03 00:10:48 +0000313 }
314
Kevin Enderbyc0957932010-09-30 16:52:03 +0000315 // This is the end of the section, so set the value of the symbol at the end
316 // of this section (that was used in a previous expression).
317 MCOS->EmitLabel(LineEndSym);
318}
319
Kevin Enderbyc0957932010-09-30 16:52:03 +0000320/// Utility function to write the encoding to an object writer.
321void MCDwarfLineAddr::Write(MCObjectWriter *OW, int64_t LineDelta,
322 uint64_t AddrDelta) {
323 SmallString<256> Tmp;
324 raw_svector_ostream OS(Tmp);
325 MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OS);
326 OW->WriteBytes(OS.str());
327}
328
329/// Utility function to emit the encoding to a streamer.
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000330void MCDwarfLineAddr::Emit(MCStreamer *MCOS, int64_t LineDelta,
Kevin Enderbyc0957932010-09-30 16:52:03 +0000331 uint64_t AddrDelta) {
332 SmallString<256> Tmp;
333 raw_svector_ostream OS(Tmp);
334 MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OS);
335 MCOS->EmitBytes(OS.str(), /*AddrSpace=*/0);
336}
337
338/// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
339void MCDwarfLineAddr::Encode(int64_t LineDelta, uint64_t AddrDelta,
340 raw_ostream &OS) {
341 uint64_t Temp, Opcode;
342 bool NeedCopy = false;
343
344 // Scale the address delta by the minimum instruction length.
345 AddrDelta = ScaleAddrDelta(AddrDelta);
346
347 // A LineDelta of INT64_MAX is a signal that this is actually a
348 // DW_LNE_end_sequence. We cannot use special opcodes here, since we want the
349 // end_sequence to emit the matrix entry.
350 if (LineDelta == INT64_MAX) {
351 if (AddrDelta == MAX_SPECIAL_ADDR_DELTA)
352 OS << char(dwarf::DW_LNS_const_add_pc);
353 else {
354 OS << char(dwarf::DW_LNS_advance_pc);
Benjamin Kramerdcf0e0c2011-06-18 14:42:47 +0000355 MCObjectWriter::EncodeULEB128(AddrDelta, OS);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000356 }
357 OS << char(dwarf::DW_LNS_extended_op);
358 OS << char(1);
359 OS << char(dwarf::DW_LNE_end_sequence);
360 return;
361 }
362
363 // Bias the line delta by the base.
364 Temp = LineDelta - DWARF2_LINE_BASE;
365
366 // If the line increment is out of range of a special opcode, we must encode
367 // it with DW_LNS_advance_line.
368 if (Temp >= DWARF2_LINE_RANGE) {
369 OS << char(dwarf::DW_LNS_advance_line);
370 SmallString<32> Tmp;
371 raw_svector_ostream OSE(Tmp);
372 MCObjectWriter::EncodeSLEB128(LineDelta, OSE);
373 OS << OSE.str();
374
375 LineDelta = 0;
376 Temp = 0 - DWARF2_LINE_BASE;
377 NeedCopy = true;
378 }
379
380 // Use DW_LNS_copy instead of a "line +0, addr +0" special opcode.
381 if (LineDelta == 0 && AddrDelta == 0) {
382 OS << char(dwarf::DW_LNS_copy);
383 return;
384 }
385
386 // Bias the opcode by the special opcode base.
387 Temp += DWARF2_LINE_OPCODE_BASE;
388
389 // Avoid overflow when addr_delta is large.
390 if (AddrDelta < 256 + MAX_SPECIAL_ADDR_DELTA) {
391 // Try using a special opcode.
392 Opcode = Temp + AddrDelta * DWARF2_LINE_RANGE;
393 if (Opcode <= 255) {
394 OS << char(Opcode);
395 return;
396 }
397
398 // Try using DW_LNS_const_add_pc followed by special op.
399 Opcode = Temp + (AddrDelta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
400 if (Opcode <= 255) {
401 OS << char(dwarf::DW_LNS_const_add_pc);
402 OS << char(Opcode);
403 return;
404 }
405 }
406
407 // Otherwise use DW_LNS_advance_pc.
408 OS << char(dwarf::DW_LNS_advance_pc);
409 SmallString<32> Tmp;
410 raw_svector_ostream OSE(Tmp);
411 MCObjectWriter::EncodeULEB128(AddrDelta, OSE);
412 OS << OSE.str();
413
414 if (NeedCopy)
415 OS << char(dwarf::DW_LNS_copy);
416 else
417 OS << char(Temp);
418}
419
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000420void MCDwarfFile::print(raw_ostream &OS) const {
421 OS << '"' << getName() << '"';
422}
423
424void MCDwarfFile::dump() const {
425 print(dbgs());
426}
Kevin Enderbyc0957932010-09-30 16:52:03 +0000427
Rafael Espindola89b93722010-12-10 07:39:47 +0000428static int getDataAlignmentFactor(MCStreamer &streamer) {
429 MCContext &context = streamer.getContext();
430 const TargetAsmInfo &asmInfo = context.getTargetAsmInfo();
431 int size = asmInfo.getPointerSize();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000432 if (asmInfo.getStackGrowthDirection() == TargetFrameLowering::StackGrowsUp)
Rafael Espindola89b93722010-12-10 07:39:47 +0000433 return size;
434 else
435 return -size;
436}
437
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000438static unsigned getSizeForEncoding(MCStreamer &streamer,
439 unsigned symbolEncoding) {
Rafael Espindolabdc31672010-12-27 15:56:22 +0000440 MCContext &context = streamer.getContext();
441 const TargetAsmInfo &asmInfo = context.getTargetAsmInfo();
442 unsigned format = symbolEncoding & 0x0f;
Rafael Espindolabdc31672010-12-27 15:56:22 +0000443 switch (format) {
444 default:
445 assert(0 && "Unknown Encoding");
Rafael Espindolabdc31672010-12-27 15:56:22 +0000446 case dwarf::DW_EH_PE_absptr:
447 case dwarf::DW_EH_PE_signed:
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000448 return asmInfo.getPointerSize();
Rafael Espindolabdc31672010-12-27 15:56:22 +0000449 case dwarf::DW_EH_PE_udata2:
450 case dwarf::DW_EH_PE_sdata2:
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000451 return 2;
Rafael Espindolabdc31672010-12-27 15:56:22 +0000452 case dwarf::DW_EH_PE_udata4:
453 case dwarf::DW_EH_PE_sdata4:
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000454 return 4;
Rafael Espindolabdc31672010-12-27 15:56:22 +0000455 case dwarf::DW_EH_PE_udata8:
456 case dwarf::DW_EH_PE_sdata8:
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000457 return 8;
Rafael Espindolabdc31672010-12-27 15:56:22 +0000458 }
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000459}
460
461static void EmitSymbol(MCStreamer &streamer, const MCSymbol &symbol,
Bill Wendling2541c412011-06-30 22:02:20 +0000462 unsigned symbolEncoding, const char *comment = 0) {
Rafael Espindolaa0057ca2011-04-28 21:04:39 +0000463 MCContext &context = streamer.getContext();
464 const MCAsmInfo &asmInfo = context.getAsmInfo();
465 const MCExpr *v = asmInfo.getExprForFDESymbol(&symbol,
Rafael Espindoladebd7e42011-05-01 03:50:49 +0000466 symbolEncoding,
Rafael Espindolaa0057ca2011-04-28 21:04:39 +0000467 streamer);
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000468 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Bill Wendling2541c412011-06-30 22:02:20 +0000469 if (streamer.isVerboseAsm() && comment) streamer.AddComment(comment);
Rafael Espindoladebd7e42011-05-01 03:50:49 +0000470 streamer.EmitAbsValue(v, size);
Rafael Espindolabdc31672010-12-27 15:56:22 +0000471}
472
Rafael Espindolabfa27cc2011-04-28 16:09:09 +0000473static void EmitPersonality(MCStreamer &streamer, const MCSymbol &symbol,
474 unsigned symbolEncoding) {
475 MCContext &context = streamer.getContext();
476 const MCAsmInfo &asmInfo = context.getAsmInfo();
Rafael Espindoladebd7e42011-05-01 03:50:49 +0000477 const MCExpr *v = asmInfo.getExprForPersonalitySymbol(&symbol,
478 symbolEncoding,
479 streamer);
Rafael Espindolabfa27cc2011-04-28 16:09:09 +0000480 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Rafael Espindoladebd7e42011-05-01 03:50:49 +0000481 streamer.EmitValue(v, size);
Rafael Espindolabfa27cc2011-04-28 16:09:09 +0000482}
483
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000484static const MachineLocation TranslateMachineLocation(
485 const TargetAsmInfo &AsmInfo,
486 const MachineLocation &Loc) {
487 unsigned Reg = Loc.getReg() == MachineLocation::VirtualFP ?
488 MachineLocation::VirtualFP :
489 unsigned(AsmInfo.getDwarfRegNum(Loc.getReg(), true));
490 const MachineLocation &NewLoc = Loc.isReg() ?
491 MachineLocation(Reg) : MachineLocation(Reg, Loc.getOffset());
492 return NewLoc;
493}
494
Rafael Espindola25f492e2011-04-12 16:12:03 +0000495namespace {
496 class FrameEmitterImpl {
497 int CFAOffset;
Rafael Espindola514cecc2011-04-28 03:26:11 +0000498 int CIENum;
Rafael Espindola5426a9e2011-05-01 04:49:54 +0000499 bool UsingCFI;
Rafael Espindola40a7dbb2011-05-10 03:54:12 +0000500 bool IsEH;
Rafael Espindolae3a0e982011-05-10 20:59:42 +0000501 const MCSymbol *SectionStart;
Rafael Espindola25f492e2011-04-12 16:12:03 +0000502 public:
Rafael Espindolae3a0e982011-05-10 20:59:42 +0000503 FrameEmitterImpl(bool usingCFI, bool isEH, const MCSymbol *sectionStart) :
504 CFAOffset(0), CIENum(0), UsingCFI(usingCFI), IsEH(isEH),
505 SectionStart(sectionStart) {
Rafael Espindola25f492e2011-04-12 16:12:03 +0000506 }
507
Bill Wendlinge3cd13f2011-06-23 01:06:23 +0000508 /// EmitCompactUnwind - Emit the unwind information in a compact way. If
509 /// we're successful, return 'true'. Otherwise, return 'false' and it will
510 /// emit the normal CIE and FDE.
511 bool EmitCompactUnwind(MCStreamer &streamer,
512 const MCDwarfFrameInfo &frame);
513
Rafael Espindola25f492e2011-04-12 16:12:03 +0000514 const MCSymbol &EmitCIE(MCStreamer &streamer,
515 const MCSymbol *personality,
516 unsigned personalityEncoding,
517 const MCSymbol *lsda,
518 unsigned lsdaEncoding);
519 MCSymbol *EmitFDE(MCStreamer &streamer,
520 const MCSymbol &cieStart,
Rafael Espindola9f270da2011-05-10 03:01:39 +0000521 const MCDwarfFrameInfo &frame);
Rafael Espindola25f492e2011-04-12 16:12:03 +0000522 void EmitCFIInstructions(MCStreamer &streamer,
523 const std::vector<MCCFIInstruction> &Instrs,
524 MCSymbol *BaseLabel);
525 void EmitCFIInstruction(MCStreamer &Streamer,
526 const MCCFIInstruction &Instr);
527 };
Bill Wendling3c163cf2011-06-30 21:25:51 +0000528
529} // end anonymous namespace
530
531static void EmitEncodingByte(MCStreamer &Streamer, unsigned Encoding,
532 StringRef Prefix) {
533 if (Streamer.isVerboseAsm()) {
534 const char *EncStr = 0;
535 switch (Encoding) {
536 default: EncStr = "<unknown encoding>";
537 case dwarf::DW_EH_PE_absptr: EncStr = "absptr";
538 case dwarf::DW_EH_PE_omit: EncStr = "omit";
539 case dwarf::DW_EH_PE_pcrel: EncStr = "pcrel";
540 case dwarf::DW_EH_PE_udata4: EncStr = "udata4";
541 case dwarf::DW_EH_PE_udata8: EncStr = "udata8";
542 case dwarf::DW_EH_PE_sdata4: EncStr = "sdata4";
543 case dwarf::DW_EH_PE_sdata8: EncStr = "sdata8";
544 case dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata4: EncStr = "pcrel udata4";
545 case dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata4: EncStr = "pcrel sdata4";
546 case dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata8: EncStr = "pcrel udata8";
547 case dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata8: EncStr = "pcrel sdata8";
548 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata4:
549 EncStr = "indirect pcrel udata4";
550 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata4:
551 EncStr = "indirect pcrel sdata4";
552 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata8:
553 EncStr = "indirect pcrel udata8";
554 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata8:
555 EncStr = "indirect pcrel sdata8";
556 }
557
558 Streamer.AddComment(Twine(Prefix) + " = " + EncStr);
559 }
560
561 Streamer.EmitIntValue(Encoding, 1);
Rafael Espindola25f492e2011-04-12 16:12:03 +0000562}
563
564void FrameEmitterImpl::EmitCFIInstruction(MCStreamer &Streamer,
565 const MCCFIInstruction &Instr) {
566 int dataAlignmentFactor = getDataAlignmentFactor(Streamer);
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000567 bool VerboseAsm = Streamer.isVerboseAsm();
Rafael Espindola25f492e2011-04-12 16:12:03 +0000568
569 switch (Instr.getOperation()) {
570 case MCCFIInstruction::Move:
571 case MCCFIInstruction::RelMove: {
572 const MachineLocation &Dst = Instr.getDestination();
573 const MachineLocation &Src = Instr.getSource();
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000574 const bool IsRelative = Instr.getOperation() == MCCFIInstruction::RelMove;
Rafael Espindola25f492e2011-04-12 16:12:03 +0000575
576 // If advancing cfa.
577 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
Rafael Espindola25f492e2011-04-12 16:12:03 +0000578 if (Src.getReg() == MachineLocation::VirtualFP) {
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000579 if (VerboseAsm) Streamer.AddComment("DW_CFA_def_cfa_offset");
Rafael Espindola25f492e2011-04-12 16:12:03 +0000580 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_offset, 1);
581 } else {
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000582 if (VerboseAsm) Streamer.AddComment("DW_CFA_def_cfa");
Rafael Espindola25f492e2011-04-12 16:12:03 +0000583 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa, 1);
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000584 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") +
585 Twine(Src.getReg()));
Rafael Espindola25f492e2011-04-12 16:12:03 +0000586 Streamer.EmitULEB128IntValue(Src.getReg());
587 }
588
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000589 if (IsRelative)
590 CFAOffset += Src.getOffset();
591 else
592 CFAOffset = -Src.getOffset();
593
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000594 if (VerboseAsm) Streamer.AddComment(Twine("Offset " + Twine(CFAOffset)));
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000595 Streamer.EmitULEB128IntValue(CFAOffset);
Rafael Espindola25f492e2011-04-12 16:12:03 +0000596 return;
597 }
598
599 if (Src.isReg() && Src.getReg() == MachineLocation::VirtualFP) {
600 assert(Dst.isReg() && "Machine move not supported yet.");
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000601 if (VerboseAsm) Streamer.AddComment("DW_CFA_def_cfa_register");
Rafael Espindola25f492e2011-04-12 16:12:03 +0000602 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_register, 1);
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000603 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Dst.getReg()));
Rafael Espindola25f492e2011-04-12 16:12:03 +0000604 Streamer.EmitULEB128IntValue(Dst.getReg());
605 return;
606 }
607
608 unsigned Reg = Src.getReg();
Rafael Espindola25f492e2011-04-12 16:12:03 +0000609 int Offset = Dst.getOffset();
610 if (IsRelative)
611 Offset -= CFAOffset;
612 Offset = Offset / dataAlignmentFactor;
613
614 if (Offset < 0) {
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000615 if (VerboseAsm) Streamer.AddComment("DW_CFA_offset_extended_sf");
Rafael Espindola25f492e2011-04-12 16:12:03 +0000616 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended_sf, 1);
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000617 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
Rafael Espindola25f492e2011-04-12 16:12:03 +0000618 Streamer.EmitULEB128IntValue(Reg);
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000619 if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
Rafael Espindola25f492e2011-04-12 16:12:03 +0000620 Streamer.EmitSLEB128IntValue(Offset);
621 } else if (Reg < 64) {
Bill Wendlinge08d4332011-06-30 23:47:40 +0000622 if (VerboseAsm) Streamer.AddComment(Twine("DW_CFA_offset + Reg(") +
623 Twine(Reg) + ")");
Rafael Espindola25f492e2011-04-12 16:12:03 +0000624 Streamer.EmitIntValue(dwarf::DW_CFA_offset + Reg, 1);
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000625 if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
Rafael Espindolaeccbad72011-04-21 23:26:40 +0000626 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola25f492e2011-04-12 16:12:03 +0000627 } else {
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000628 if (VerboseAsm) Streamer.AddComment("DW_CFA_offset_extended");
Rafael Espindola25f492e2011-04-12 16:12:03 +0000629 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended, 1);
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000630 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
Rafael Espindolaeccbad72011-04-21 23:26:40 +0000631 Streamer.EmitULEB128IntValue(Reg);
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000632 if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
Rafael Espindolaeccbad72011-04-21 23:26:40 +0000633 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola25f492e2011-04-12 16:12:03 +0000634 }
635 return;
636 }
637 case MCCFIInstruction::Remember:
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000638 if (VerboseAsm) Streamer.AddComment("DW_CFA_remember_state");
Rafael Espindola25f492e2011-04-12 16:12:03 +0000639 Streamer.EmitIntValue(dwarf::DW_CFA_remember_state, 1);
640 return;
641 case MCCFIInstruction::Restore:
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000642 if (VerboseAsm) Streamer.AddComment("DW_CFA_restore_state");
Rafael Espindola25f492e2011-04-12 16:12:03 +0000643 Streamer.EmitIntValue(dwarf::DW_CFA_restore_state, 1);
644 return;
645 case MCCFIInstruction::SameValue: {
646 unsigned Reg = Instr.getDestination().getReg();
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000647 if (VerboseAsm) Streamer.AddComment("DW_CFA_same_value");
Rafael Espindola25f492e2011-04-12 16:12:03 +0000648 Streamer.EmitIntValue(dwarf::DW_CFA_same_value, 1);
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000649 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000650 Streamer.EmitULEB128IntValue(Reg);
Rafael Espindola25f492e2011-04-12 16:12:03 +0000651 return;
652 }
653 }
654 llvm_unreachable("Unhandled case in switch");
655}
656
657/// EmitFrameMoves - Emit frame instructions to describe the layout of the
658/// frame.
659void FrameEmitterImpl::EmitCFIInstructions(MCStreamer &streamer,
660 const std::vector<MCCFIInstruction> &Instrs,
661 MCSymbol *BaseLabel) {
662 for (unsigned i = 0, N = Instrs.size(); i < N; ++i) {
663 const MCCFIInstruction &Instr = Instrs[i];
664 MCSymbol *Label = Instr.getLabel();
665 // Throw out move if the label is invalid.
666 if (Label && !Label->isDefined()) continue; // Not emitted, in dead code.
667
668 // Advance row if new location.
669 if (BaseLabel && Label) {
670 MCSymbol *ThisSym = Label;
671 if (ThisSym != BaseLabel) {
Bill Wendlingd221cd62011-06-30 22:35:49 +0000672 if (streamer.isVerboseAsm()) streamer.AddComment("DW_CFA_advance_loc4");
Rafael Espindola25f492e2011-04-12 16:12:03 +0000673 streamer.EmitDwarfAdvanceFrameAddr(BaseLabel, ThisSym);
674 BaseLabel = ThisSym;
675 }
676 }
677
678 EmitCFIInstruction(streamer, Instr);
679 }
680}
681
Bill Wendlinge3cd13f2011-06-23 01:06:23 +0000682/// EmitCompactUnwind - Emit the unwind information in a compact way. If we're
683/// successful, return 'true'. Otherwise, return 'false' and it will emit the
684/// normal CIE and FDE.
685bool FrameEmitterImpl::EmitCompactUnwind(MCStreamer &Streamer,
686 const MCDwarfFrameInfo &Frame) {
687#if 1
688 return false;
689#else
690 MCContext &Context = Streamer.getContext();
691 const TargetAsmInfo &TAI = Context.getTargetAsmInfo();
Bill Wendling3c163cf2011-06-30 21:25:51 +0000692 bool VerboseAsm = Streamer.isVerboseAsm();
Bill Wendlinge3cd13f2011-06-23 01:06:23 +0000693
Bill Wendlinge3cd13f2011-06-23 01:06:23 +0000694 // range-start range-length compact-unwind-enc personality-func lsda
695 // _foo LfooEnd-_foo 0x00000023 0 0
696 // _bar LbarEnd-_bar 0x00000025 __gxx_personality except_tab1
697 //
698 // .section __LD,__compact_unwind,regular,debug
699 //
700 // # compact unwind for _foo
701 // .quad _foo
702 // .set L1,LfooEnd-_foo
703 // .long L1
704 // .long 0x01010001
705 // .quad 0
706 // .quad 0
707 //
708 // # compact unwind for _bar
709 // .quad _bar
710 // .set L2,LbarEnd-_bar
711 // .long L2
712 // .long 0x01020011
713 // .quad __gxx_personality
714 // .quad except_tab1
715
Bill Wendling6a6b8c32011-07-07 00:54:13 +0000716 uint32_t Encoding =
717 TAI.getCompactUnwindEncoding(Frame.Instructions,
718 getDataAlignmentFactor(Streamer), IsEH);
719 if (!Encoding) return false;
720
Bill Wendling3c163cf2011-06-30 21:25:51 +0000721 Streamer.SwitchSection(TAI.getCompactUnwindSection());
722
Bill Wendlinge3cd13f2011-06-23 01:06:23 +0000723 // Range Start
Bill Wendling9056e902011-06-29 23:49:12 +0000724 unsigned FDEEncoding = TAI.getFDEEncoding(UsingCFI);
725 unsigned Size = getSizeForEncoding(Streamer, FDEEncoding);
Bill Wendling3c163cf2011-06-30 21:25:51 +0000726 if (VerboseAsm) Streamer.AddComment("Range Start");
Bill Wendling9056e902011-06-29 23:49:12 +0000727 Streamer.EmitSymbolValue(Frame.Function, Size);
Bill Wendlinge3cd13f2011-06-23 01:06:23 +0000728
729 // Range Length
730 const MCExpr *Range = MakeStartMinusEndExpr(Streamer, *Frame.Begin,
731 *Frame.End, 0);
Bill Wendling3c163cf2011-06-30 21:25:51 +0000732 if (VerboseAsm) Streamer.AddComment("Range Length");
Bill Wendling9287a6e2011-06-30 00:30:52 +0000733 Streamer.EmitAbsValue(Range, 4);
734
Bill Wendling9287a6e2011-06-30 00:30:52 +0000735 // Compact Encoding
Bill Wendling9287a6e2011-06-30 00:30:52 +0000736 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_udata4);
Bill Wendling6a6b8c32011-07-07 00:54:13 +0000737 if (VerboseAsm) Streamer.AddComment(Twine("Compact Unwind Encoding: 0x") +
738 Twine(llvm::utohexstr(Encoding)));
Bill Wendling9287a6e2011-06-30 00:30:52 +0000739 Streamer.EmitIntValue(Encoding, Size);
740
Bill Wendling9056e902011-06-29 23:49:12 +0000741 // Personality Function
Bill Wendling4498d392011-06-29 23:53:16 +0000742 Size = getSizeForEncoding(Streamer, Frame.PersonalityEncoding);
Bill Wendling3c163cf2011-06-30 21:25:51 +0000743 if (VerboseAsm) Streamer.AddComment("Personality Function");
Bill Wendling4498d392011-06-29 23:53:16 +0000744 if (Frame.Personality)
Bill Wendling9056e902011-06-29 23:49:12 +0000745 Streamer.EmitSymbolValue(Frame.Personality, Size);
Bill Wendling4498d392011-06-29 23:53:16 +0000746 else
747 Streamer.EmitIntValue(0, Size); // No personality fn
Bill Wendling9056e902011-06-29 23:49:12 +0000748
749 // LSDA
Bill Wendling4498d392011-06-29 23:53:16 +0000750 Size = getSizeForEncoding(Streamer, Frame.LsdaEncoding);
Bill Wendling3c163cf2011-06-30 21:25:51 +0000751 if (VerboseAsm) Streamer.AddComment("LSDA");
Bill Wendling4498d392011-06-29 23:53:16 +0000752 if (Frame.Lsda)
Bill Wendling9056e902011-06-29 23:49:12 +0000753 Streamer.EmitSymbolValue(Frame.Lsda, Size);
Bill Wendling4498d392011-06-29 23:53:16 +0000754 else
755 Streamer.EmitIntValue(0, Size); // No LSDA
Bill Wendling9056e902011-06-29 23:49:12 +0000756
Bill Wendlinge3cd13f2011-06-23 01:06:23 +0000757 return true;
758#endif
759}
760
Rafael Espindola25f492e2011-04-12 16:12:03 +0000761const MCSymbol &FrameEmitterImpl::EmitCIE(MCStreamer &streamer,
762 const MCSymbol *personality,
763 unsigned personalityEncoding,
764 const MCSymbol *lsda,
765 unsigned lsdaEncoding) {
Rafael Espindola89b93722010-12-10 07:39:47 +0000766 MCContext &context = streamer.getContext();
767 const TargetAsmInfo &asmInfo = context.getTargetAsmInfo();
Bill Wendling3c163cf2011-06-30 21:25:51 +0000768 bool verboseAsm = streamer.isVerboseAsm();
Rafael Espindola514cecc2011-04-28 03:26:11 +0000769
770 MCSymbol *sectionStart;
Rafael Espindola12f197b2011-05-10 19:51:53 +0000771 if (asmInfo.isFunctionEHFrameSymbolPrivate() || !IsEH)
Rafael Espindola514cecc2011-04-28 03:26:11 +0000772 sectionStart = context.CreateTempSymbol();
773 else
774 sectionStart = context.GetOrCreateSymbol(Twine("EH_frame") + Twine(CIENum));
775
Bill Wendling3c163cf2011-06-30 21:25:51 +0000776 streamer.EmitLabel(sectionStart);
Rafael Espindola514cecc2011-04-28 03:26:11 +0000777 CIENum++;
778
Bill Wendling6a6b8c32011-07-07 00:54:13 +0000779 MCSymbol *sectionEnd = context.CreateTempSymbol();
Rafael Espindola89b93722010-12-10 07:39:47 +0000780
781 // Length
782 const MCExpr *Length = MakeStartMinusEndExpr(streamer, *sectionStart,
783 *sectionEnd, 4);
Bill Wendling3c163cf2011-06-30 21:25:51 +0000784 if (verboseAsm) streamer.AddComment("CIE Length");
Rafael Espindola9266cc42011-04-27 01:43:49 +0000785 streamer.EmitAbsValue(Length, 4);
Rafael Espindola89b93722010-12-10 07:39:47 +0000786
787 // CIE ID
Rafael Espindola40a7dbb2011-05-10 03:54:12 +0000788 unsigned CIE_ID = IsEH ? 0 : -1;
Bill Wendling3c163cf2011-06-30 21:25:51 +0000789 if (verboseAsm) streamer.AddComment("CIE ID Tag");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +0000790 streamer.EmitIntValue(CIE_ID, 4);
Rafael Espindola89b93722010-12-10 07:39:47 +0000791
792 // Version
Bill Wendling3c163cf2011-06-30 21:25:51 +0000793 if (verboseAsm) streamer.AddComment("DW_CIE_VERSION");
Rafael Espindola89b93722010-12-10 07:39:47 +0000794 streamer.EmitIntValue(dwarf::DW_CIE_VERSION, 1);
795
796 // Augmentation String
797 SmallString<8> Augmentation;
Rafael Espindola40a7dbb2011-05-10 03:54:12 +0000798 if (IsEH) {
Bill Wendling3c163cf2011-06-30 21:25:51 +0000799 if (verboseAsm) streamer.AddComment("CIE Augmentation");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +0000800 Augmentation += "z";
801 if (personality)
802 Augmentation += "P";
803 if (lsda)
804 Augmentation += "L";
805 Augmentation += "R";
806 streamer.EmitBytes(Augmentation.str(), 0);
807 }
Rafael Espindola89b93722010-12-10 07:39:47 +0000808 streamer.EmitIntValue(0, 1);
809
810 // Code Alignment Factor
Bill Wendling3c163cf2011-06-30 21:25:51 +0000811 if (verboseAsm) streamer.AddComment("CIE Code Alignment Factor");
Rafael Espindola89b93722010-12-10 07:39:47 +0000812 streamer.EmitULEB128IntValue(1);
813
814 // Data Alignment Factor
Bill Wendling3c163cf2011-06-30 21:25:51 +0000815 if (verboseAsm) streamer.AddComment("CIE Data Alignment Factor");
Rafael Espindola89b93722010-12-10 07:39:47 +0000816 streamer.EmitSLEB128IntValue(getDataAlignmentFactor(streamer));
817
818 // Return Address Register
Bill Wendling3c163cf2011-06-30 21:25:51 +0000819 if (verboseAsm) streamer.AddComment("CIE Return Address Column");
Rafael Espindola89b93722010-12-10 07:39:47 +0000820 streamer.EmitULEB128IntValue(asmInfo.getDwarfRARegNum(true));
821
822 // Augmentation Data Length (optional)
Rafael Espindola1f6c8752011-04-29 21:50:57 +0000823
824 unsigned augmentationLength = 0;
Rafael Espindola40a7dbb2011-05-10 03:54:12 +0000825 if (IsEH) {
826 if (personality) {
827 // Personality Encoding
828 augmentationLength += 1;
829 // Personality
830 augmentationLength += getSizeForEncoding(streamer, personalityEncoding);
831 }
832 if (lsda)
833 augmentationLength += 1;
834 // Encoding of the FDE pointers
Rafael Espindola1f6c8752011-04-29 21:50:57 +0000835 augmentationLength += 1;
Rafael Espindola1f6c8752011-04-29 21:50:57 +0000836
Bill Wendling3c163cf2011-06-30 21:25:51 +0000837 if (verboseAsm) streamer.AddComment("Augmentation Size");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +0000838 streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola89b93722010-12-10 07:39:47 +0000839
Rafael Espindola40a7dbb2011-05-10 03:54:12 +0000840 // Augmentation Data (optional)
841 if (personality) {
842 // Personality Encoding
Bill Wendling3c163cf2011-06-30 21:25:51 +0000843 EmitEncodingByte(streamer, personalityEncoding,
844 "Personality Encoding");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +0000845 // Personality
Bill Wendling3c163cf2011-06-30 21:25:51 +0000846 if (verboseAsm) streamer.AddComment("Personality");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +0000847 EmitPersonality(streamer, *personality, personalityEncoding);
848 }
Bill Wendling3c163cf2011-06-30 21:25:51 +0000849
Rafael Espindola40a7dbb2011-05-10 03:54:12 +0000850 if (lsda)
Bill Wendling3c163cf2011-06-30 21:25:51 +0000851 EmitEncodingByte(streamer, lsdaEncoding, "LSDA Encoding");
852
Rafael Espindola40a7dbb2011-05-10 03:54:12 +0000853 // Encoding of the FDE pointers
Bill Wendling3c163cf2011-06-30 21:25:51 +0000854 EmitEncodingByte(streamer, asmInfo.getFDEEncoding(UsingCFI),
855 "FDE Encoding");
Rafael Espindolabdc31672010-12-27 15:56:22 +0000856 }
Rafael Espindola89b93722010-12-10 07:39:47 +0000857
858 // Initial Instructions
859
Bill Wendling5b46e8e2011-06-23 07:55:41 +0000860 const std::vector<MachineMove> &Moves = asmInfo.getInitialFrameState();
Rafael Espindolafe024d02010-12-28 18:36:23 +0000861 std::vector<MCCFIInstruction> Instructions;
Rafael Espindola89b93722010-12-10 07:39:47 +0000862
Rafael Espindolafe024d02010-12-28 18:36:23 +0000863 for (int i = 0, n = Moves.size(); i != n; ++i) {
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000864 MCSymbol *Label = Moves[i].getLabel();
865 const MachineLocation &Dst =
866 TranslateMachineLocation(asmInfo, Moves[i].getDestination());
867 const MachineLocation &Src =
868 TranslateMachineLocation(asmInfo, Moves[i].getSource());
869 MCCFIInstruction Inst(Label, Dst, Src);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000870 Instructions.push_back(Inst);
871 }
872
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000873 EmitCFIInstructions(streamer, Instructions, NULL);
Rafael Espindola89b93722010-12-10 07:39:47 +0000874
875 // Padding
Rafael Espindola40a7dbb2011-05-10 03:54:12 +0000876 streamer.EmitValueToAlignment(IsEH ? 4 : asmInfo.getPointerSize());
Rafael Espindola89b93722010-12-10 07:39:47 +0000877
878 streamer.EmitLabel(sectionEnd);
879 return *sectionStart;
880}
881
Rafael Espindola25f492e2011-04-12 16:12:03 +0000882MCSymbol *FrameEmitterImpl::EmitFDE(MCStreamer &streamer,
883 const MCSymbol &cieStart,
Rafael Espindola9f270da2011-05-10 03:01:39 +0000884 const MCDwarfFrameInfo &frame) {
Rafael Espindolabdc31672010-12-27 15:56:22 +0000885 MCContext &context = streamer.getContext();
886 MCSymbol *fdeStart = context.CreateTempSymbol();
887 MCSymbol *fdeEnd = context.CreateTempSymbol();
Rafael Espindolae3a0e982011-05-10 20:59:42 +0000888 const TargetAsmInfo &TAsmInfo = context.getTargetAsmInfo();
Bill Wendling2541c412011-06-30 22:02:20 +0000889 bool verboseAsm = streamer.isVerboseAsm();
Rafael Espindola89b93722010-12-10 07:39:47 +0000890
Rafael Espindolae3a0e982011-05-10 20:59:42 +0000891 if (!TAsmInfo.isFunctionEHFrameSymbolPrivate() && IsEH) {
Bill Wendling2541c412011-06-30 22:02:20 +0000892 MCSymbol *EHSym =
893 context.GetOrCreateSymbol(frame.Function->getName() + Twine(".eh"));
Rafael Espindola8bca4102011-04-28 12:50:37 +0000894 streamer.EmitEHSymAttributes(frame.Function, EHSym);
Rafael Espindolaa8cfb872011-04-28 02:46:42 +0000895 streamer.EmitLabel(EHSym);
896 }
897
Rafael Espindola89b93722010-12-10 07:39:47 +0000898 // Length
899 const MCExpr *Length = MakeStartMinusEndExpr(streamer, *fdeStart, *fdeEnd, 0);
Bill Wendling2541c412011-06-30 22:02:20 +0000900 if (verboseAsm) streamer.AddComment("FDE Length");
Rafael Espindola9266cc42011-04-27 01:43:49 +0000901 streamer.EmitAbsValue(Length, 4);
Rafael Espindola89b93722010-12-10 07:39:47 +0000902
903 streamer.EmitLabel(fdeStart);
Rafael Espindolae3a0e982011-05-10 20:59:42 +0000904
Rafael Espindola89b93722010-12-10 07:39:47 +0000905 // CIE Pointer
Rafael Espindolae3a0e982011-05-10 20:59:42 +0000906 const MCAsmInfo &asmInfo = context.getAsmInfo();
Rafael Espindola0d450dc2011-05-10 15:20:23 +0000907 if (IsEH) {
908 const MCExpr *offset = MakeStartMinusEndExpr(streamer, cieStart, *fdeStart,
909 0);
Bill Wendling2541c412011-06-30 22:02:20 +0000910 if (verboseAsm) streamer.AddComment("FDE CIE Offset");
Rafael Espindola0d450dc2011-05-10 15:20:23 +0000911 streamer.EmitAbsValue(offset, 4);
Rafael Espindolae3a0e982011-05-10 20:59:42 +0000912 } else if (!asmInfo.doesDwarfRequireRelocationForSectionOffset()) {
913 const MCExpr *offset = MakeStartMinusEndExpr(streamer, *SectionStart,
914 cieStart, 0);
915 streamer.EmitAbsValue(offset, 4);
Rafael Espindola0d450dc2011-05-10 15:20:23 +0000916 } else {
917 streamer.EmitSymbolValue(&cieStart, 4);
918 }
Bill Wendling2541c412011-06-30 22:02:20 +0000919
Rafael Espindolae3a0e982011-05-10 20:59:42 +0000920 unsigned fdeEncoding = TAsmInfo.getFDEEncoding(UsingCFI);
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000921 unsigned size = getSizeForEncoding(streamer, fdeEncoding);
Rafael Espindola89b93722010-12-10 07:39:47 +0000922
923 // PC Begin
Rafael Espindola9989ef42011-05-10 22:28:35 +0000924 unsigned PCBeginEncoding = IsEH ? fdeEncoding :
925 (unsigned)dwarf::DW_EH_PE_absptr;
Rafael Espindola40a7dbb2011-05-10 03:54:12 +0000926 unsigned PCBeginSize = getSizeForEncoding(streamer, PCBeginEncoding);
Bill Wendling2541c412011-06-30 22:02:20 +0000927 EmitSymbol(streamer, *frame.Begin, PCBeginEncoding, "FDE initial location");
Rafael Espindola89b93722010-12-10 07:39:47 +0000928
929 // PC Range
930 const MCExpr *Range = MakeStartMinusEndExpr(streamer, *frame.Begin,
931 *frame.End, 0);
Bill Wendling2541c412011-06-30 22:02:20 +0000932 if (verboseAsm) streamer.AddComment("FDE address range");
Rafael Espindola9266cc42011-04-27 01:43:49 +0000933 streamer.EmitAbsValue(Range, size);
Rafael Espindola89b93722010-12-10 07:39:47 +0000934
Rafael Espindola40a7dbb2011-05-10 03:54:12 +0000935 if (IsEH) {
936 // Augmentation Data Length
937 unsigned augmentationLength = 0;
Rafael Espindola1f6c8752011-04-29 21:50:57 +0000938
Rafael Espindola40a7dbb2011-05-10 03:54:12 +0000939 if (frame.Lsda)
940 augmentationLength += getSizeForEncoding(streamer, frame.LsdaEncoding);
Rafael Espindola1f6c8752011-04-29 21:50:57 +0000941
Bill Wendling2541c412011-06-30 22:02:20 +0000942 if (verboseAsm) streamer.AddComment("Augmentation size");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +0000943 streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola89b93722010-12-10 07:39:47 +0000944
Rafael Espindola40a7dbb2011-05-10 03:54:12 +0000945 // Augmentation Data
946 if (frame.Lsda)
Bill Wendling2541c412011-06-30 22:02:20 +0000947 EmitSymbol(streamer, *frame.Lsda, frame.LsdaEncoding,
948 "Language Specific Data Area");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +0000949 }
Rafael Espindola4892dff2011-04-29 03:06:29 +0000950
Rafael Espindola89b93722010-12-10 07:39:47 +0000951 // Call Frame Instructions
952
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000953 EmitCFIInstructions(streamer, frame.Instructions, frame.Begin);
Rafael Espindola5bba0842010-12-28 04:15:37 +0000954
Rafael Espindola89b93722010-12-10 07:39:47 +0000955 // Padding
Rafael Espindola40a7dbb2011-05-10 03:54:12 +0000956 streamer.EmitValueToAlignment(PCBeginSize);
Rafael Espindoladfe125c2010-12-17 00:28:02 +0000957
958 return fdeEnd;
Rafael Espindola89b93722010-12-10 07:39:47 +0000959}
960
Benjamin Kramer19282362010-12-30 22:34:44 +0000961namespace {
962 struct CIEKey {
963 static const CIEKey getEmptyKey() { return CIEKey(0, 0, -1); }
964 static const CIEKey getTombstoneKey() { return CIEKey(0, -1, 0); }
Rafael Espindolabdc31672010-12-27 15:56:22 +0000965
Benjamin Kramer19282362010-12-30 22:34:44 +0000966 CIEKey(const MCSymbol* Personality_, unsigned PersonalityEncoding_,
967 unsigned LsdaEncoding_) : Personality(Personality_),
968 PersonalityEncoding(PersonalityEncoding_),
969 LsdaEncoding(LsdaEncoding_) {
970 }
971 const MCSymbol* Personality;
972 unsigned PersonalityEncoding;
973 unsigned LsdaEncoding;
974 };
975}
Rafael Espindolabdc31672010-12-27 15:56:22 +0000976
977namespace llvm {
978 template <>
979 struct DenseMapInfo<CIEKey> {
980 static CIEKey getEmptyKey() {
Benjamin Kramer19282362010-12-30 22:34:44 +0000981 return CIEKey::getEmptyKey();
Rafael Espindolabdc31672010-12-27 15:56:22 +0000982 }
983 static CIEKey getTombstoneKey() {
Benjamin Kramer19282362010-12-30 22:34:44 +0000984 return CIEKey::getTombstoneKey();
Rafael Espindolabdc31672010-12-27 15:56:22 +0000985 }
986 static unsigned getHashValue(const CIEKey &Key) {
987 FoldingSetNodeID ID;
988 ID.AddPointer(Key.Personality);
989 ID.AddInteger(Key.PersonalityEncoding);
990 ID.AddInteger(Key.LsdaEncoding);
991 return ID.ComputeHash();
992 }
993 static bool isEqual(const CIEKey &LHS,
994 const CIEKey &RHS) {
995 return LHS.Personality == RHS.Personality &&
996 LHS.PersonalityEncoding == RHS.PersonalityEncoding &&
997 LHS.LsdaEncoding == RHS.LsdaEncoding;
998 }
999 };
1000}
1001
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001002void MCDwarfFrameEmitter::Emit(MCStreamer &Streamer,
1003 bool UsingCFI,
1004 bool IsEH) {
1005 MCContext &Context = Streamer.getContext();
1006 const TargetAsmInfo &AsmInfo = Context.getTargetAsmInfo();
1007 const MCSection &Section = IsEH ? *AsmInfo.getEHFrameSection() :
1008 *AsmInfo.getDwarfFrameSection();
1009 Streamer.SwitchSection(&Section);
1010 MCSymbol *SectionStart = Context.CreateTempSymbol();
1011 Streamer.EmitLabel(SectionStart);
Rafael Espindola90998132011-04-29 02:42:28 +00001012
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001013 MCSymbol *FDEEnd = NULL;
Rafael Espindolabdc31672010-12-27 15:56:22 +00001014 DenseMap<CIEKey, const MCSymbol*> CIEStarts;
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001015 FrameEmitterImpl Emitter(UsingCFI, IsEH, SectionStart);
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00001016
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001017 const MCSymbol *DummyDebugKey = NULL;
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001018 for (unsigned i = 0, n = Streamer.getNumFrameInfos(); i < n; ++i) {
1019 const MCDwarfFrameInfo &Frame = Streamer.getFrameInfo(i);
1020 if (IsEH && AsmInfo.getCompactUnwindSection() &&
Bill Wendling8440fe22011-07-13 00:49:09 +00001021 Emitter.EmitCompactUnwind(Streamer, Frame)) {
1022 FDEEnd = NULL;
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001023 continue;
Bill Wendling8440fe22011-07-13 00:49:09 +00001024 }
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001025
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001026 CIEKey Key(Frame.Personality, Frame.PersonalityEncoding,
1027 Frame.LsdaEncoding);
1028 const MCSymbol *&CIEStart = IsEH ? CIEStarts[Key] : DummyDebugKey;
1029 if (!CIEStart)
1030 CIEStart = &Emitter.EmitCIE(Streamer, Frame.Personality,
1031 Frame.PersonalityEncoding, Frame.Lsda,
1032 Frame.LsdaEncoding);
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001033
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001034 FDEEnd = Emitter.EmitFDE(Streamer, *CIEStart, Frame);
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001035
Rafael Espindoladfe125c2010-12-17 00:28:02 +00001036 if (i != n - 1)
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001037 Streamer.EmitLabel(FDEEnd);
Rafael Espindoladfe125c2010-12-17 00:28:02 +00001038 }
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00001039
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001040 Streamer.EmitValueToAlignment(AsmInfo.getPointerSize());
1041 if (FDEEnd)
1042 Streamer.EmitLabel(FDEEnd);
Rafael Espindola89b93722010-12-10 07:39:47 +00001043}
Rafael Espindola245a1e22010-12-28 05:39:27 +00001044
1045void MCDwarfFrameEmitter::EmitAdvanceLoc(MCStreamer &Streamer,
1046 uint64_t AddrDelta) {
1047 SmallString<256> Tmp;
1048 raw_svector_ostream OS(Tmp);
Rafael Espindola4eafe102011-05-08 14:35:21 +00001049 MCDwarfFrameEmitter::EncodeAdvanceLoc(AddrDelta, OS);
Rafael Espindola245a1e22010-12-28 05:39:27 +00001050 Streamer.EmitBytes(OS.str(), /*AddrSpace=*/0);
1051}
1052
1053void MCDwarfFrameEmitter::EncodeAdvanceLoc(uint64_t AddrDelta,
Rafael Espindola4eafe102011-05-08 14:35:21 +00001054 raw_ostream &OS) {
Rafael Espindola245a1e22010-12-28 05:39:27 +00001055 // FIXME: Assumes the code alignment factor is 1.
Rafael Espindola3b78cdc2010-12-28 23:38:03 +00001056 if (AddrDelta == 0) {
Rafael Espindola4eafe102011-05-08 14:35:21 +00001057 } else if (isUIntN(6, AddrDelta)) {
Rafael Espindola245a1e22010-12-28 05:39:27 +00001058 uint8_t Opcode = dwarf::DW_CFA_advance_loc | AddrDelta;
1059 OS << Opcode;
Rafael Espindola4eafe102011-05-08 14:35:21 +00001060 } else if (isUInt<8>(AddrDelta)) {
Rafael Espindola245a1e22010-12-28 05:39:27 +00001061 OS << uint8_t(dwarf::DW_CFA_advance_loc1);
1062 OS << uint8_t(AddrDelta);
Rafael Espindola4eafe102011-05-08 14:35:21 +00001063 } else if (isUInt<16>(AddrDelta)) {
Rafael Espindolaa7e45052010-12-29 02:30:49 +00001064 // FIXME: check what is the correct behavior on a big endian machine.
Rafael Espindola245a1e22010-12-28 05:39:27 +00001065 OS << uint8_t(dwarf::DW_CFA_advance_loc2);
Rafael Espindolaa7e45052010-12-29 02:30:49 +00001066 OS << uint8_t( AddrDelta & 0xff);
1067 OS << uint8_t((AddrDelta >> 8) & 0xff);
Rafael Espindola245a1e22010-12-28 05:39:27 +00001068 } else {
Rafael Espindolaa7e45052010-12-29 02:30:49 +00001069 // FIXME: check what is the correct behavior on a big endian machine.
Rafael Espindola245a1e22010-12-28 05:39:27 +00001070 assert(isUInt<32>(AddrDelta));
1071 OS << uint8_t(dwarf::DW_CFA_advance_loc4);
Rafael Espindolaa7e45052010-12-29 02:30:49 +00001072 OS << uint8_t( AddrDelta & 0xff);
1073 OS << uint8_t((AddrDelta >> 8) & 0xff);
1074 OS << uint8_t((AddrDelta >> 16) & 0xff);
1075 OS << uint8_t((AddrDelta >> 24) & 0xff);
1076
Rafael Espindola245a1e22010-12-28 05:39:27 +00001077 }
1078}