blob: 72ce441e1936a9485cc4f8dbfc2e4deae29cbb37 [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 Espindolabdc31672010-12-27 15:56:22 +000010#include "llvm/ADT/FoldingSet.h"
Rafael Espindola767b1be2010-12-04 00:31:13 +000011#include "llvm/MC/MCAsmInfo.h"
Kevin Enderby7cbf73a2010-07-28 20:55:35 +000012#include "llvm/MC/MCDwarf.h"
Kevin Enderbyc0957932010-09-30 16:52:03 +000013#include "llvm/MC/MCAssembler.h"
Rafael Espindolaad8aaa02010-11-22 11:53:17 +000014#include "llvm/MC/MCStreamer.h"
Kevin Enderbyc0957932010-09-30 16:52:03 +000015#include "llvm/MC/MCSymbol.h"
16#include "llvm/MC/MCExpr.h"
17#include "llvm/MC/MCContext.h"
18#include "llvm/MC/MCObjectWriter.h"
19#include "llvm/ADT/SmallString.h"
Rafael Espindolaa8cfb872011-04-28 02:46:42 +000020#include "llvm/ADT/Twine.h"
Kevin Enderby7cbf73a2010-07-28 20:55:35 +000021#include "llvm/Support/Debug.h"
Rafael Espindolafe024d02010-12-28 18:36:23 +000022#include "llvm/Support/ErrorHandling.h"
Kevin Enderby7cbf73a2010-07-28 20:55:35 +000023#include "llvm/Support/raw_ostream.h"
Kevin Enderbyc0957932010-09-30 16:52:03 +000024#include "llvm/Target/TargetAsmBackend.h"
Rafael Espindola89b93722010-12-10 07:39:47 +000025#include "llvm/Target/TargetAsmInfo.h"
Kevin Enderby7cbf73a2010-07-28 20:55:35 +000026using namespace llvm;
27
Kevin Enderbyc0957932010-09-30 16:52:03 +000028// Given a special op, return the address skip amount (in units of
29// DWARF2_LINE_MIN_INSN_LENGTH.
30#define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE)
31
32// The maximum address skip amount that can be encoded with a special op.
33#define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255)
34
35// First special line opcode - leave room for the standard opcodes.
36// Note: If you want to change this, you'll have to update the
37// "standard_opcode_lengths" table that is emitted in DwarfFileTable::Emit().
38#define DWARF2_LINE_OPCODE_BASE 13
39
40// Minimum line offset in a special line info. opcode. This value
41// was chosen to give a reasonable range of values.
42#define DWARF2_LINE_BASE -5
43
44// Range of line offsets in a special line info. opcode.
45# define DWARF2_LINE_RANGE 14
46
47// Define the architecture-dependent minimum instruction length (in bytes).
48// This value should be rather too small than too big.
49# define DWARF2_LINE_MIN_INSN_LENGTH 1
50
51// Note: when DWARF2_LINE_MIN_INSN_LENGTH == 1 which is the current setting,
52// this routine is a nop and will be optimized away.
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +000053static inline uint64_t ScaleAddrDelta(uint64_t AddrDelta) {
Kevin Enderbyc0957932010-09-30 16:52:03 +000054 if (DWARF2_LINE_MIN_INSN_LENGTH == 1)
55 return AddrDelta;
56 if (AddrDelta % DWARF2_LINE_MIN_INSN_LENGTH != 0) {
57 // TODO: report this error, but really only once.
58 ;
59 }
60 return AddrDelta / DWARF2_LINE_MIN_INSN_LENGTH;
61}
62
63//
64// This is called when an instruction is assembled into the specified section
65// and if there is information from the last .loc directive that has yet to have
66// a line entry made for it is made.
67//
Rafael Espindola195a0ce2010-11-19 02:26:16 +000068void MCLineEntry::Make(MCStreamer *MCOS, const MCSection *Section) {
Kevin Enderbyc0957932010-09-30 16:52:03 +000069 if (!MCOS->getContext().getDwarfLocSeen())
70 return;
71
72 // Create a symbol at in the current section for use in the line entry.
73 MCSymbol *LineSym = MCOS->getContext().CreateTempSymbol();
74 // Set the value of the symbol to use for the MCLineEntry.
75 MCOS->EmitLabel(LineSym);
76
77 // Get the current .loc info saved in the context.
78 const MCDwarfLoc &DwarfLoc = MCOS->getContext().getCurrentDwarfLoc();
79
80 // Create a (local) line entry with the symbol and the current .loc info.
81 MCLineEntry LineEntry(LineSym, DwarfLoc);
82
83 // clear DwarfLocSeen saying the current .loc info is now used.
Kevin Enderby3f55c242010-10-04 20:17:24 +000084 MCOS->getContext().ClearDwarfLocSeen();
Kevin Enderbyc0957932010-09-30 16:52:03 +000085
86 // Get the MCLineSection for this section, if one does not exist for this
87 // section create it.
Rafael Espindola17fd7bd2010-11-19 07:41:23 +000088 const DenseMap<const MCSection *, MCLineSection *> &MCLineSections =
Kevin Enderbyc0957932010-09-30 16:52:03 +000089 MCOS->getContext().getMCLineSections();
Rafael Espindola17fd7bd2010-11-19 07:41:23 +000090 MCLineSection *LineSection = MCLineSections.lookup(Section);
Kevin Enderbyc0957932010-09-30 16:52:03 +000091 if (!LineSection) {
92 // Create a new MCLineSection. This will be deleted after the dwarf line
93 // table is created using it by iterating through the MCLineSections
94 // DenseMap.
95 LineSection = new MCLineSection;
96 // Save a pointer to the new LineSection into the MCLineSections DenseMap.
Rafael Espindola17fd7bd2010-11-19 07:41:23 +000097 MCOS->getContext().addMCLineSection(Section, LineSection);
Kevin Enderbyc0957932010-09-30 16:52:03 +000098 }
99
100 // Add the line entry to this section's entries.
101 LineSection->addLineEntry(LineEntry);
102}
103
104//
105// This helper routine returns an expression of End - Start + IntVal .
106//
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000107static inline const MCExpr *MakeStartMinusEndExpr(const MCStreamer &MCOS,
108 const MCSymbol &Start,
109 const MCSymbol &End,
110 int IntVal) {
Kevin Enderbyc0957932010-09-30 16:52:03 +0000111 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
112 const MCExpr *Res =
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000113 MCSymbolRefExpr::Create(&End, Variant, MCOS.getContext());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000114 const MCExpr *RHS =
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000115 MCSymbolRefExpr::Create(&Start, Variant, MCOS.getContext());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000116 const MCExpr *Res1 =
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000117 MCBinaryExpr::Create(MCBinaryExpr::Sub, Res, RHS, MCOS.getContext());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000118 const MCExpr *Res2 =
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000119 MCConstantExpr::Create(IntVal, MCOS.getContext());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000120 const MCExpr *Res3 =
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000121 MCBinaryExpr::Create(MCBinaryExpr::Sub, Res1, Res2, MCOS.getContext());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000122 return Res3;
123}
124
Kevin Enderbyc0957932010-09-30 16:52:03 +0000125//
126// This emits the Dwarf line table for the specified section from the entries
127// in the LineSection.
128//
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000129static inline void EmitDwarfLineTable(MCStreamer *MCOS,
Kevin Enderbyc0957932010-09-30 16:52:03 +0000130 const MCSection *Section,
Rafael Espindola89b93722010-12-10 07:39:47 +0000131 const MCLineSection *LineSection) {
Kevin Enderbyc0957932010-09-30 16:52:03 +0000132 unsigned FileNum = 1;
133 unsigned LastLine = 1;
134 unsigned Column = 0;
135 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
136 unsigned Isa = 0;
Kevin Enderbyc0957932010-09-30 16:52:03 +0000137 MCSymbol *LastLabel = NULL;
Kevin Enderbyc0957932010-09-30 16:52:03 +0000138
139 // Loop through each MCLineEntry and encode the dwarf line number table.
Rafael Espindola17fd7bd2010-11-19 07:41:23 +0000140 for (MCLineSection::const_iterator
Kevin Enderbyc0957932010-09-30 16:52:03 +0000141 it = LineSection->getMCLineEntries()->begin(),
142 ie = LineSection->getMCLineEntries()->end(); it != ie; ++it) {
143
144 if (FileNum != it->getFileNum()) {
145 FileNum = it->getFileNum();
146 MCOS->EmitIntValue(dwarf::DW_LNS_set_file, 1);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000147 MCOS->EmitULEB128IntValue(FileNum);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000148 }
149 if (Column != it->getColumn()) {
150 Column = it->getColumn();
151 MCOS->EmitIntValue(dwarf::DW_LNS_set_column, 1);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000152 MCOS->EmitULEB128IntValue(Column);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000153 }
154 if (Isa != it->getIsa()) {
155 Isa = it->getIsa();
156 MCOS->EmitIntValue(dwarf::DW_LNS_set_isa, 1);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000157 MCOS->EmitULEB128IntValue(Isa);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000158 }
159 if ((it->getFlags() ^ Flags) & DWARF2_FLAG_IS_STMT) {
160 Flags = it->getFlags();
161 MCOS->EmitIntValue(dwarf::DW_LNS_negate_stmt, 1);
162 }
163 if (it->getFlags() & DWARF2_FLAG_BASIC_BLOCK)
164 MCOS->EmitIntValue(dwarf::DW_LNS_set_basic_block, 1);
165 if (it->getFlags() & DWARF2_FLAG_PROLOGUE_END)
166 MCOS->EmitIntValue(dwarf::DW_LNS_set_prologue_end, 1);
167 if (it->getFlags() & DWARF2_FLAG_EPILOGUE_BEGIN)
168 MCOS->EmitIntValue(dwarf::DW_LNS_set_epilogue_begin, 1);
169
Rafael Espindola64185cc2010-11-13 01:06:27 +0000170 int64_t LineDelta = static_cast<int64_t>(it->getLine()) - LastLine;
Kevin Enderbyc0957932010-09-30 16:52:03 +0000171 MCSymbol *Label = it->getLabel();
172
173 // At this point we want to emit/create the sequence to encode the delta in
174 // line numbers and the increment of the address from the previous Label
175 // and the current Label.
Rafael Espindola32a006e2010-12-03 00:55:40 +0000176 MCOS->EmitDwarfAdvanceLineAddr(LineDelta, LastLabel, Label);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000177
178 LastLine = it->getLine();
179 LastLabel = Label;
Kevin Enderbyc0957932010-09-30 16:52:03 +0000180 }
181
182 // Emit a DW_LNE_end_sequence for the end of the section.
183 // Using the pointer Section create a temporary label at the end of the
184 // section and use that and the LastLabel to compute the address delta
185 // and use INT64_MAX as the line delta which is the signal that this is
186 // actually a DW_LNE_end_sequence.
187
188 // Switch to the section to be able to create a symbol at its end.
189 MCOS->SwitchSection(Section);
Rafael Espindola89b93722010-12-10 07:39:47 +0000190
191 MCContext &context = MCOS->getContext();
Kevin Enderbyc0957932010-09-30 16:52:03 +0000192 // Create a symbol at the end of the section.
Rafael Espindola89b93722010-12-10 07:39:47 +0000193 MCSymbol *SectionEnd = context.CreateTempSymbol();
Kevin Enderbyc0957932010-09-30 16:52:03 +0000194 // Set the value of the symbol, as we are at the end of the section.
195 MCOS->EmitLabel(SectionEnd);
196
197 // Switch back the the dwarf line section.
Rafael Espindola89b93722010-12-10 07:39:47 +0000198 MCOS->SwitchSection(context.getTargetAsmInfo().getDwarfLineSection());
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000199
Rafael Espindola32a006e2010-12-03 00:55:40 +0000200 MCOS->EmitDwarfAdvanceLineAddr(INT64_MAX, LastLabel, SectionEnd);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000201}
202
203//
204// This emits the Dwarf file and the line tables.
205//
Rafael Espindola89b93722010-12-10 07:39:47 +0000206void MCDwarfFileTable::Emit(MCStreamer *MCOS) {
207 MCContext &context = MCOS->getContext();
Kevin Enderbyc0957932010-09-30 16:52:03 +0000208 // Switch to the section where the table will be emitted into.
Rafael Espindola89b93722010-12-10 07:39:47 +0000209 MCOS->SwitchSection(context.getTargetAsmInfo().getDwarfLineSection());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000210
211 // Create a symbol at the beginning of this section.
Rafael Espindola89b93722010-12-10 07:39:47 +0000212 MCSymbol *LineStartSym = context.CreateTempSymbol();
Kevin Enderbyc0957932010-09-30 16:52:03 +0000213 // Set the value of the symbol, as we are at the start of the section.
214 MCOS->EmitLabel(LineStartSym);
215
216 // Create a symbol for the end of the section (to be set when we get there).
Rafael Espindola89b93722010-12-10 07:39:47 +0000217 MCSymbol *LineEndSym = context.CreateTempSymbol();
Kevin Enderbyc0957932010-09-30 16:52:03 +0000218
219 // The first 4 bytes is the total length of the information for this
220 // compilation unit (not including these 4 bytes for the length).
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000221 MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *LineEndSym,4),
Rafael Espindola0bbe0b42010-12-06 17:27:56 +0000222 4);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000223
224 // Next 2 bytes is the Version, which is Dwarf 2.
225 MCOS->EmitIntValue(2, 2);
226
227 // Create a symbol for the end of the prologue (to be set when we get there).
Rafael Espindola89b93722010-12-10 07:39:47 +0000228 MCSymbol *ProEndSym = context.CreateTempSymbol(); // Lprologue_end
Kevin Enderbyc0957932010-09-30 16:52:03 +0000229
230 // Length of the prologue, is the next 4 bytes. Which is the start of the
231 // section to the end of the prologue. Not including the 4 bytes for the
232 // total length, the 2 bytes for the version, and these 4 bytes for the
233 // length of the prologue.
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000234 MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *ProEndSym,
Kevin Enderbyc0957932010-09-30 16:52:03 +0000235 (4 + 2 + 4)),
Rafael Espindola5d4918d2010-12-04 03:21:47 +0000236 4, 0);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000237
238 // Parameters of the state machine, are next.
239 MCOS->EmitIntValue(DWARF2_LINE_MIN_INSN_LENGTH, 1);
240 MCOS->EmitIntValue(DWARF2_LINE_DEFAULT_IS_STMT, 1);
241 MCOS->EmitIntValue(DWARF2_LINE_BASE, 1);
242 MCOS->EmitIntValue(DWARF2_LINE_RANGE, 1);
243 MCOS->EmitIntValue(DWARF2_LINE_OPCODE_BASE, 1);
244
245 // Standard opcode lengths
246 MCOS->EmitIntValue(0, 1); // length of DW_LNS_copy
247 MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_pc
248 MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_line
249 MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_file
250 MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_column
251 MCOS->EmitIntValue(0, 1); // length of DW_LNS_negate_stmt
252 MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_basic_block
253 MCOS->EmitIntValue(0, 1); // length of DW_LNS_const_add_pc
254 MCOS->EmitIntValue(1, 1); // length of DW_LNS_fixed_advance_pc
255 MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_prologue_end
256 MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_epilogue_begin
257 MCOS->EmitIntValue(1, 1); // DW_LNS_set_isa
258
259 // Put out the directory and file tables.
260
261 // First the directory table.
262 const std::vector<StringRef> &MCDwarfDirs =
Rafael Espindola89b93722010-12-10 07:39:47 +0000263 context.getMCDwarfDirs();
Kevin Enderbyc0957932010-09-30 16:52:03 +0000264 for (unsigned i = 0; i < MCDwarfDirs.size(); i++) {
265 MCOS->EmitBytes(MCDwarfDirs[i], 0); // the DirectoryName
266 MCOS->EmitBytes(StringRef("\0", 1), 0); // the null term. of the string
267 }
268 MCOS->EmitIntValue(0, 1); // Terminate the directory list
269
270 // Second the file table.
271 const std::vector<MCDwarfFile *> &MCDwarfFiles =
272 MCOS->getContext().getMCDwarfFiles();
273 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
274 MCOS->EmitBytes(MCDwarfFiles[i]->getName(), 0); // FileName
275 MCOS->EmitBytes(StringRef("\0", 1), 0); // the null term. of the string
Rafael Espindola3ff57092010-11-02 17:22:24 +0000276 // the Directory num
277 MCOS->EmitULEB128IntValue(MCDwarfFiles[i]->getDirIndex());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000278 MCOS->EmitIntValue(0, 1); // last modification timestamp (always 0)
279 MCOS->EmitIntValue(0, 1); // filesize (always 0)
280 }
281 MCOS->EmitIntValue(0, 1); // Terminate the file list
282
283 // This is the end of the prologue, so set the value of the symbol at the
284 // end of the prologue (that was used in a previous expression).
285 MCOS->EmitLabel(ProEndSym);
286
287 // Put out the line tables.
Rafael Espindola17fd7bd2010-11-19 07:41:23 +0000288 const DenseMap<const MCSection *, MCLineSection *> &MCLineSections =
Kevin Enderbyc0957932010-09-30 16:52:03 +0000289 MCOS->getContext().getMCLineSections();
Rafael Espindola17fd7bd2010-11-19 07:41:23 +0000290 const std::vector<const MCSection *> &MCLineSectionOrder =
291 MCOS->getContext().getMCLineSectionOrder();
292 for (std::vector<const MCSection*>::const_iterator it =
293 MCLineSectionOrder.begin(), ie = MCLineSectionOrder.end(); it != ie;
294 ++it) {
295 const MCSection *Sec = *it;
296 const MCLineSection *Line = MCLineSections.lookup(Sec);
Rafael Espindola89b93722010-12-10 07:39:47 +0000297 EmitDwarfLineTable(MCOS, Sec, Line);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000298
299 // Now delete the MCLineSections that were created in MCLineEntry::Make()
300 // and used to emit the line table.
Rafael Espindola17fd7bd2010-11-19 07:41:23 +0000301 delete Line;
Kevin Enderbyc0957932010-09-30 16:52:03 +0000302 }
303
Rafael Espindola767b1be2010-12-04 00:31:13 +0000304 if (MCOS->getContext().getAsmInfo().getLinkerRequiresNonEmptyDwarfLines()
305 && MCLineSectionOrder.begin() == MCLineSectionOrder.end()) {
Rafael Espindolaa8de83c2010-12-03 23:36:59 +0000306 // The darwin9 linker has a bug (see PR8715). For for 32-bit architectures
307 // it requires:
308 // total_length >= prologue_length + 10
309 // We are 4 bytes short, since we have total_length = 51 and
310 // prologue_length = 45
Devang Patel5113cdb2010-12-03 00:10:48 +0000311
Rafael Espindolaa8de83c2010-12-03 23:36:59 +0000312 // The regular end_sequence should be sufficient.
313 MCDwarfLineAddr::Emit(MCOS, INT64_MAX, 0);
Devang Patel5113cdb2010-12-03 00:10:48 +0000314 }
315
Kevin Enderbyc0957932010-09-30 16:52:03 +0000316 // This is the end of the section, so set the value of the symbol at the end
317 // of this section (that was used in a previous expression).
318 MCOS->EmitLabel(LineEndSym);
319}
320
Kevin Enderbyc0957932010-09-30 16:52:03 +0000321/// Utility function to write the encoding to an object writer.
322void MCDwarfLineAddr::Write(MCObjectWriter *OW, int64_t LineDelta,
323 uint64_t AddrDelta) {
324 SmallString<256> Tmp;
325 raw_svector_ostream OS(Tmp);
326 MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OS);
327 OW->WriteBytes(OS.str());
328}
329
330/// Utility function to emit the encoding to a streamer.
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000331void MCDwarfLineAddr::Emit(MCStreamer *MCOS, int64_t LineDelta,
Kevin Enderbyc0957932010-09-30 16:52:03 +0000332 uint64_t AddrDelta) {
333 SmallString<256> Tmp;
334 raw_svector_ostream OS(Tmp);
335 MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OS);
336 MCOS->EmitBytes(OS.str(), /*AddrSpace=*/0);
337}
338
339/// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
340void MCDwarfLineAddr::Encode(int64_t LineDelta, uint64_t AddrDelta,
341 raw_ostream &OS) {
342 uint64_t Temp, Opcode;
343 bool NeedCopy = false;
344
345 // Scale the address delta by the minimum instruction length.
346 AddrDelta = ScaleAddrDelta(AddrDelta);
347
348 // A LineDelta of INT64_MAX is a signal that this is actually a
349 // DW_LNE_end_sequence. We cannot use special opcodes here, since we want the
350 // end_sequence to emit the matrix entry.
351 if (LineDelta == INT64_MAX) {
352 if (AddrDelta == MAX_SPECIAL_ADDR_DELTA)
353 OS << char(dwarf::DW_LNS_const_add_pc);
354 else {
355 OS << char(dwarf::DW_LNS_advance_pc);
Benjamin Kramerdcf0e0c2011-06-18 14:42:47 +0000356 MCObjectWriter::EncodeULEB128(AddrDelta, OS);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000357 }
358 OS << char(dwarf::DW_LNS_extended_op);
359 OS << char(1);
360 OS << char(dwarf::DW_LNE_end_sequence);
361 return;
362 }
363
364 // Bias the line delta by the base.
365 Temp = LineDelta - DWARF2_LINE_BASE;
366
367 // If the line increment is out of range of a special opcode, we must encode
368 // it with DW_LNS_advance_line.
369 if (Temp >= DWARF2_LINE_RANGE) {
370 OS << char(dwarf::DW_LNS_advance_line);
371 SmallString<32> Tmp;
372 raw_svector_ostream OSE(Tmp);
373 MCObjectWriter::EncodeSLEB128(LineDelta, OSE);
374 OS << OSE.str();
375
376 LineDelta = 0;
377 Temp = 0 - DWARF2_LINE_BASE;
378 NeedCopy = true;
379 }
380
381 // Use DW_LNS_copy instead of a "line +0, addr +0" special opcode.
382 if (LineDelta == 0 && AddrDelta == 0) {
383 OS << char(dwarf::DW_LNS_copy);
384 return;
385 }
386
387 // Bias the opcode by the special opcode base.
388 Temp += DWARF2_LINE_OPCODE_BASE;
389
390 // Avoid overflow when addr_delta is large.
391 if (AddrDelta < 256 + MAX_SPECIAL_ADDR_DELTA) {
392 // Try using a special opcode.
393 Opcode = Temp + AddrDelta * DWARF2_LINE_RANGE;
394 if (Opcode <= 255) {
395 OS << char(Opcode);
396 return;
397 }
398
399 // Try using DW_LNS_const_add_pc followed by special op.
400 Opcode = Temp + (AddrDelta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
401 if (Opcode <= 255) {
402 OS << char(dwarf::DW_LNS_const_add_pc);
403 OS << char(Opcode);
404 return;
405 }
406 }
407
408 // Otherwise use DW_LNS_advance_pc.
409 OS << char(dwarf::DW_LNS_advance_pc);
410 SmallString<32> Tmp;
411 raw_svector_ostream OSE(Tmp);
412 MCObjectWriter::EncodeULEB128(AddrDelta, OSE);
413 OS << OSE.str();
414
415 if (NeedCopy)
416 OS << char(dwarf::DW_LNS_copy);
417 else
418 OS << char(Temp);
419}
420
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000421void MCDwarfFile::print(raw_ostream &OS) const {
422 OS << '"' << getName() << '"';
423}
424
425void MCDwarfFile::dump() const {
426 print(dbgs());
427}
Kevin Enderbyc0957932010-09-30 16:52:03 +0000428
Rafael Espindola89b93722010-12-10 07:39:47 +0000429static int getDataAlignmentFactor(MCStreamer &streamer) {
430 MCContext &context = streamer.getContext();
431 const TargetAsmInfo &asmInfo = context.getTargetAsmInfo();
432 int size = asmInfo.getPointerSize();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000433 if (asmInfo.getStackGrowthDirection() == TargetFrameLowering::StackGrowsUp)
Rafael Espindola89b93722010-12-10 07:39:47 +0000434 return size;
435 else
436 return -size;
437}
438
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000439static unsigned getSizeForEncoding(MCStreamer &streamer,
440 unsigned symbolEncoding) {
Rafael Espindolabdc31672010-12-27 15:56:22 +0000441 MCContext &context = streamer.getContext();
442 const TargetAsmInfo &asmInfo = context.getTargetAsmInfo();
443 unsigned format = symbolEncoding & 0x0f;
Rafael Espindolabdc31672010-12-27 15:56:22 +0000444 switch (format) {
445 default:
446 assert(0 && "Unknown Encoding");
Rafael Espindolabdc31672010-12-27 15:56:22 +0000447 case dwarf::DW_EH_PE_absptr:
448 case dwarf::DW_EH_PE_signed:
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000449 return asmInfo.getPointerSize();
Rafael Espindolabdc31672010-12-27 15:56:22 +0000450 case dwarf::DW_EH_PE_udata2:
451 case dwarf::DW_EH_PE_sdata2:
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000452 return 2;
Rafael Espindolabdc31672010-12-27 15:56:22 +0000453 case dwarf::DW_EH_PE_udata4:
454 case dwarf::DW_EH_PE_sdata4:
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000455 return 4;
Rafael Espindolabdc31672010-12-27 15:56:22 +0000456 case dwarf::DW_EH_PE_udata8:
457 case dwarf::DW_EH_PE_sdata8:
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000458 return 8;
Rafael Espindolabdc31672010-12-27 15:56:22 +0000459 }
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000460}
461
462static void EmitSymbol(MCStreamer &streamer, const MCSymbol &symbol,
Bill Wendling2541c412011-06-30 22:02:20 +0000463 unsigned symbolEncoding, const char *comment = 0) {
Rafael Espindolaa0057ca2011-04-28 21:04:39 +0000464 MCContext &context = streamer.getContext();
465 const MCAsmInfo &asmInfo = context.getAsmInfo();
466 const MCExpr *v = asmInfo.getExprForFDESymbol(&symbol,
Rafael Espindoladebd7e42011-05-01 03:50:49 +0000467 symbolEncoding,
Rafael Espindolaa0057ca2011-04-28 21:04:39 +0000468 streamer);
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000469 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Bill Wendling2541c412011-06-30 22:02:20 +0000470 if (streamer.isVerboseAsm() && comment) streamer.AddComment(comment);
Rafael Espindoladebd7e42011-05-01 03:50:49 +0000471 streamer.EmitAbsValue(v, size);
Rafael Espindolabdc31672010-12-27 15:56:22 +0000472}
473
Rafael Espindolabfa27cc2011-04-28 16:09:09 +0000474static void EmitPersonality(MCStreamer &streamer, const MCSymbol &symbol,
475 unsigned symbolEncoding) {
476 MCContext &context = streamer.getContext();
477 const MCAsmInfo &asmInfo = context.getAsmInfo();
Rafael Espindoladebd7e42011-05-01 03:50:49 +0000478 const MCExpr *v = asmInfo.getExprForPersonalitySymbol(&symbol,
479 symbolEncoding,
480 streamer);
Rafael Espindolabfa27cc2011-04-28 16:09:09 +0000481 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Rafael Espindoladebd7e42011-05-01 03:50:49 +0000482 streamer.EmitValue(v, size);
Rafael Espindolabfa27cc2011-04-28 16:09:09 +0000483}
484
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000485static const MachineLocation TranslateMachineLocation(
486 const TargetAsmInfo &AsmInfo,
487 const MachineLocation &Loc) {
488 unsigned Reg = Loc.getReg() == MachineLocation::VirtualFP ?
489 MachineLocation::VirtualFP :
490 unsigned(AsmInfo.getDwarfRegNum(Loc.getReg(), true));
491 const MachineLocation &NewLoc = Loc.isReg() ?
492 MachineLocation(Reg) : MachineLocation(Reg, Loc.getOffset());
493 return NewLoc;
494}
495
Rafael Espindola25f492e2011-04-12 16:12:03 +0000496namespace {
497 class FrameEmitterImpl {
498 int CFAOffset;
Rafael Espindola514cecc2011-04-28 03:26:11 +0000499 int CIENum;
Rafael Espindola5426a9e2011-05-01 04:49:54 +0000500 bool UsingCFI;
Rafael Espindola40a7dbb2011-05-10 03:54:12 +0000501 bool IsEH;
Rafael Espindolae3a0e982011-05-10 20:59:42 +0000502 const MCSymbol *SectionStart;
Rafael Espindola25f492e2011-04-12 16:12:03 +0000503
504 public:
Rafael Espindolae3a0e982011-05-10 20:59:42 +0000505 FrameEmitterImpl(bool usingCFI, bool isEH, const MCSymbol *sectionStart) :
506 CFAOffset(0), CIENum(0), UsingCFI(usingCFI), IsEH(isEH),
507 SectionStart(sectionStart) {
Rafael Espindola25f492e2011-04-12 16:12:03 +0000508 }
509
Bill Wendlinge3cd13f2011-06-23 01:06:23 +0000510 /// EmitCompactUnwind - Emit the unwind information in a compact way. If
511 /// we're successful, return 'true'. Otherwise, return 'false' and it will
512 /// emit the normal CIE and FDE.
513 bool EmitCompactUnwind(MCStreamer &streamer,
514 const MCDwarfFrameInfo &frame);
515
Rafael Espindola25f492e2011-04-12 16:12:03 +0000516 const MCSymbol &EmitCIE(MCStreamer &streamer,
517 const MCSymbol *personality,
518 unsigned personalityEncoding,
519 const MCSymbol *lsda,
520 unsigned lsdaEncoding);
521 MCSymbol *EmitFDE(MCStreamer &streamer,
522 const MCSymbol &cieStart,
Rafael Espindola9f270da2011-05-10 03:01:39 +0000523 const MCDwarfFrameInfo &frame);
Rafael Espindola25f492e2011-04-12 16:12:03 +0000524 void EmitCFIInstructions(MCStreamer &streamer,
525 const std::vector<MCCFIInstruction> &Instrs,
526 MCSymbol *BaseLabel);
527 void EmitCFIInstruction(MCStreamer &Streamer,
528 const MCCFIInstruction &Instr);
529 };
Bill Wendling3c163cf2011-06-30 21:25:51 +0000530
531} // end anonymous namespace
532
533static void EmitEncodingByte(MCStreamer &Streamer, unsigned Encoding,
534 StringRef Prefix) {
535 if (Streamer.isVerboseAsm()) {
536 const char *EncStr = 0;
537 switch (Encoding) {
538 default: EncStr = "<unknown encoding>";
539 case dwarf::DW_EH_PE_absptr: EncStr = "absptr";
540 case dwarf::DW_EH_PE_omit: EncStr = "omit";
541 case dwarf::DW_EH_PE_pcrel: EncStr = "pcrel";
542 case dwarf::DW_EH_PE_udata4: EncStr = "udata4";
543 case dwarf::DW_EH_PE_udata8: EncStr = "udata8";
544 case dwarf::DW_EH_PE_sdata4: EncStr = "sdata4";
545 case dwarf::DW_EH_PE_sdata8: EncStr = "sdata8";
546 case dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata4: EncStr = "pcrel udata4";
547 case dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata4: EncStr = "pcrel sdata4";
548 case dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata8: EncStr = "pcrel udata8";
549 case dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata8: EncStr = "pcrel sdata8";
550 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata4:
551 EncStr = "indirect pcrel udata4";
552 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata4:
553 EncStr = "indirect pcrel sdata4";
554 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata8:
555 EncStr = "indirect pcrel udata8";
556 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata8:
557 EncStr = "indirect pcrel sdata8";
558 }
559
560 Streamer.AddComment(Twine(Prefix) + " = " + EncStr);
561 }
562
563 Streamer.EmitIntValue(Encoding, 1);
Rafael Espindola25f492e2011-04-12 16:12:03 +0000564}
565
566void FrameEmitterImpl::EmitCFIInstruction(MCStreamer &Streamer,
567 const MCCFIInstruction &Instr) {
568 int dataAlignmentFactor = getDataAlignmentFactor(Streamer);
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000569 bool VerboseAsm = Streamer.isVerboseAsm();
Rafael Espindola25f492e2011-04-12 16:12:03 +0000570
571 switch (Instr.getOperation()) {
572 case MCCFIInstruction::Move:
573 case MCCFIInstruction::RelMove: {
574 const MachineLocation &Dst = Instr.getDestination();
575 const MachineLocation &Src = Instr.getSource();
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000576 const bool IsRelative = Instr.getOperation() == MCCFIInstruction::RelMove;
Rafael Espindola25f492e2011-04-12 16:12:03 +0000577
578 // If advancing cfa.
579 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
Rafael Espindola25f492e2011-04-12 16:12:03 +0000580 if (Src.getReg() == MachineLocation::VirtualFP) {
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000581 if (VerboseAsm) Streamer.AddComment("DW_CFA_def_cfa_offset");
Rafael Espindola25f492e2011-04-12 16:12:03 +0000582 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_offset, 1);
583 } else {
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000584 if (VerboseAsm) Streamer.AddComment("DW_CFA_def_cfa");
Rafael Espindola25f492e2011-04-12 16:12:03 +0000585 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa, 1);
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000586 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") +
587 Twine(Src.getReg()));
Rafael Espindola25f492e2011-04-12 16:12:03 +0000588 Streamer.EmitULEB128IntValue(Src.getReg());
589 }
590
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000591 if (IsRelative)
592 CFAOffset += Src.getOffset();
593 else
594 CFAOffset = -Src.getOffset();
595
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000596 if (VerboseAsm) Streamer.AddComment(Twine("Offset " + Twine(CFAOffset)));
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000597 Streamer.EmitULEB128IntValue(CFAOffset);
Rafael Espindola25f492e2011-04-12 16:12:03 +0000598 return;
599 }
600
601 if (Src.isReg() && Src.getReg() == MachineLocation::VirtualFP) {
602 assert(Dst.isReg() && "Machine move not supported yet.");
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000603 if (VerboseAsm) Streamer.AddComment("DW_CFA_def_cfa_register");
Rafael Espindola25f492e2011-04-12 16:12:03 +0000604 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_register, 1);
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000605 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Dst.getReg()));
Rafael Espindola25f492e2011-04-12 16:12:03 +0000606 Streamer.EmitULEB128IntValue(Dst.getReg());
607 return;
608 }
609
610 unsigned Reg = Src.getReg();
Rafael Espindola25f492e2011-04-12 16:12:03 +0000611 int Offset = Dst.getOffset();
612 if (IsRelative)
613 Offset -= CFAOffset;
614 Offset = Offset / dataAlignmentFactor;
615
616 if (Offset < 0) {
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000617 if (VerboseAsm) Streamer.AddComment("DW_CFA_offset_extended_sf");
Rafael Espindola25f492e2011-04-12 16:12:03 +0000618 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended_sf, 1);
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000619 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
Rafael Espindola25f492e2011-04-12 16:12:03 +0000620 Streamer.EmitULEB128IntValue(Reg);
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000621 if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
Rafael Espindola25f492e2011-04-12 16:12:03 +0000622 Streamer.EmitSLEB128IntValue(Offset);
623 } else if (Reg < 64) {
Bill Wendlinge08d4332011-06-30 23:47:40 +0000624 if (VerboseAsm) Streamer.AddComment(Twine("DW_CFA_offset + Reg(") +
625 Twine(Reg) + ")");
Rafael Espindola25f492e2011-04-12 16:12:03 +0000626 Streamer.EmitIntValue(dwarf::DW_CFA_offset + Reg, 1);
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000627 if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
Rafael Espindolaeccbad72011-04-21 23:26:40 +0000628 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola25f492e2011-04-12 16:12:03 +0000629 } else {
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000630 if (VerboseAsm) Streamer.AddComment("DW_CFA_offset_extended");
Rafael Espindola25f492e2011-04-12 16:12:03 +0000631 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended, 1);
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000632 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
Rafael Espindolaeccbad72011-04-21 23:26:40 +0000633 Streamer.EmitULEB128IntValue(Reg);
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000634 if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
Rafael Espindolaeccbad72011-04-21 23:26:40 +0000635 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola25f492e2011-04-12 16:12:03 +0000636 }
637 return;
638 }
639 case MCCFIInstruction::Remember:
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000640 if (VerboseAsm) Streamer.AddComment("DW_CFA_remember_state");
Rafael Espindola25f492e2011-04-12 16:12:03 +0000641 Streamer.EmitIntValue(dwarf::DW_CFA_remember_state, 1);
642 return;
643 case MCCFIInstruction::Restore:
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000644 if (VerboseAsm) Streamer.AddComment("DW_CFA_restore_state");
Rafael Espindola25f492e2011-04-12 16:12:03 +0000645 Streamer.EmitIntValue(dwarf::DW_CFA_restore_state, 1);
646 return;
647 case MCCFIInstruction::SameValue: {
648 unsigned Reg = Instr.getDestination().getReg();
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000649 if (VerboseAsm) Streamer.AddComment("DW_CFA_same_value");
Rafael Espindola25f492e2011-04-12 16:12:03 +0000650 Streamer.EmitIntValue(dwarf::DW_CFA_same_value, 1);
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000651 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000652 Streamer.EmitULEB128IntValue(Reg);
Rafael Espindola25f492e2011-04-12 16:12:03 +0000653 return;
654 }
655 }
656 llvm_unreachable("Unhandled case in switch");
657}
658
659/// EmitFrameMoves - Emit frame instructions to describe the layout of the
660/// frame.
661void FrameEmitterImpl::EmitCFIInstructions(MCStreamer &streamer,
662 const std::vector<MCCFIInstruction> &Instrs,
663 MCSymbol *BaseLabel) {
664 for (unsigned i = 0, N = Instrs.size(); i < N; ++i) {
665 const MCCFIInstruction &Instr = Instrs[i];
666 MCSymbol *Label = Instr.getLabel();
667 // Throw out move if the label is invalid.
668 if (Label && !Label->isDefined()) continue; // Not emitted, in dead code.
669
670 // Advance row if new location.
671 if (BaseLabel && Label) {
672 MCSymbol *ThisSym = Label;
673 if (ThisSym != BaseLabel) {
Bill Wendlingd221cd62011-06-30 22:35:49 +0000674 if (streamer.isVerboseAsm()) streamer.AddComment("DW_CFA_advance_loc4");
Rafael Espindola25f492e2011-04-12 16:12:03 +0000675 streamer.EmitDwarfAdvanceFrameAddr(BaseLabel, ThisSym);
676 BaseLabel = ThisSym;
677 }
678 }
679
680 EmitCFIInstruction(streamer, Instr);
681 }
682}
683
Bill Wendlinge3cd13f2011-06-23 01:06:23 +0000684/// EmitCompactUnwind - Emit the unwind information in a compact way. If we're
685/// successful, return 'true'. Otherwise, return 'false' and it will emit the
686/// normal CIE and FDE.
687bool FrameEmitterImpl::EmitCompactUnwind(MCStreamer &Streamer,
688 const MCDwarfFrameInfo &Frame) {
689#if 1
690 return false;
691#else
692 MCContext &Context = Streamer.getContext();
693 const TargetAsmInfo &TAI = Context.getTargetAsmInfo();
Bill Wendling3c163cf2011-06-30 21:25:51 +0000694 bool VerboseAsm = Streamer.isVerboseAsm();
Bill Wendlinge3cd13f2011-06-23 01:06:23 +0000695
Bill Wendlinge3cd13f2011-06-23 01:06:23 +0000696 // range-start range-length compact-unwind-enc personality-func lsda
697 // _foo LfooEnd-_foo 0x00000023 0 0
698 // _bar LbarEnd-_bar 0x00000025 __gxx_personality except_tab1
699 //
700 // .section __LD,__compact_unwind,regular,debug
701 //
702 // # compact unwind for _foo
703 // .quad _foo
704 // .set L1,LfooEnd-_foo
705 // .long L1
706 // .long 0x01010001
707 // .quad 0
708 // .quad 0
709 //
710 // # compact unwind for _bar
711 // .quad _bar
712 // .set L2,LbarEnd-_bar
713 // .long L2
714 // .long 0x01020011
715 // .quad __gxx_personality
716 // .quad except_tab1
717
Bill Wendling3c163cf2011-06-30 21:25:51 +0000718 Streamer.SwitchSection(TAI.getCompactUnwindSection());
719
Bill Wendlinge3cd13f2011-06-23 01:06:23 +0000720 // Range Start
Bill Wendling9056e902011-06-29 23:49:12 +0000721 unsigned FDEEncoding = TAI.getFDEEncoding(UsingCFI);
722 unsigned Size = getSizeForEncoding(Streamer, FDEEncoding);
Bill Wendling3c163cf2011-06-30 21:25:51 +0000723 if (VerboseAsm) Streamer.AddComment("Range Start");
Bill Wendling9056e902011-06-29 23:49:12 +0000724 Streamer.EmitSymbolValue(Frame.Function, Size);
Bill Wendlinge3cd13f2011-06-23 01:06:23 +0000725
726 // Range Length
727 const MCExpr *Range = MakeStartMinusEndExpr(Streamer, *Frame.Begin,
728 *Frame.End, 0);
Bill Wendling3c163cf2011-06-30 21:25:51 +0000729 if (VerboseAsm) Streamer.AddComment("Range Length");
Bill Wendling9287a6e2011-06-30 00:30:52 +0000730 Streamer.EmitAbsValue(Range, 4);
731
732 // FIXME:
733 // Compact Encoding
Bill Wendling3c163cf2011-06-30 21:25:51 +0000734 const std::vector<MachineMove> &Moves = TAI.getInitialFrameState();
Bill Wendling9287a6e2011-06-30 00:30:52 +0000735 uint32_t Encoding = 0;
736 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_udata4);
Bill Wendling3c163cf2011-06-30 21:25:51 +0000737 if (VerboseAsm) Streamer.AddComment("Compact Unwind Encoding");
Bill Wendling9287a6e2011-06-30 00:30:52 +0000738 Streamer.EmitIntValue(Encoding, Size);
739
Bill Wendling9056e902011-06-29 23:49:12 +0000740 // Personality Function
Bill Wendling4498d392011-06-29 23:53:16 +0000741 Size = getSizeForEncoding(Streamer, Frame.PersonalityEncoding);
Bill Wendling3c163cf2011-06-30 21:25:51 +0000742 if (VerboseAsm) Streamer.AddComment("Personality Function");
Bill Wendling4498d392011-06-29 23:53:16 +0000743 if (Frame.Personality)
Bill Wendling9056e902011-06-29 23:49:12 +0000744 Streamer.EmitSymbolValue(Frame.Personality, Size);
Bill Wendling4498d392011-06-29 23:53:16 +0000745 else
746 Streamer.EmitIntValue(0, Size); // No personality fn
Bill Wendling9056e902011-06-29 23:49:12 +0000747
748 // LSDA
Bill Wendling4498d392011-06-29 23:53:16 +0000749 Size = getSizeForEncoding(Streamer, Frame.LsdaEncoding);
Bill Wendling3c163cf2011-06-30 21:25:51 +0000750 if (VerboseAsm) Streamer.AddComment("LSDA");
Bill Wendling4498d392011-06-29 23:53:16 +0000751 if (Frame.Lsda)
Bill Wendling9056e902011-06-29 23:49:12 +0000752 Streamer.EmitSymbolValue(Frame.Lsda, Size);
Bill Wendling4498d392011-06-29 23:53:16 +0000753 else
754 Streamer.EmitIntValue(0, Size); // No LSDA
Bill Wendling9056e902011-06-29 23:49:12 +0000755
Bill Wendlinge3cd13f2011-06-23 01:06:23 +0000756 return true;
757#endif
758}
759
Rafael Espindola25f492e2011-04-12 16:12:03 +0000760const MCSymbol &FrameEmitterImpl::EmitCIE(MCStreamer &streamer,
761 const MCSymbol *personality,
762 unsigned personalityEncoding,
763 const MCSymbol *lsda,
764 unsigned lsdaEncoding) {
Rafael Espindola89b93722010-12-10 07:39:47 +0000765 MCContext &context = streamer.getContext();
766 const TargetAsmInfo &asmInfo = context.getTargetAsmInfo();
Bill Wendling3c163cf2011-06-30 21:25:51 +0000767 bool verboseAsm = streamer.isVerboseAsm();
Rafael Espindola514cecc2011-04-28 03:26:11 +0000768
769 MCSymbol *sectionStart;
Rafael Espindola12f197b2011-05-10 19:51:53 +0000770 if (asmInfo.isFunctionEHFrameSymbolPrivate() || !IsEH)
Rafael Espindola514cecc2011-04-28 03:26:11 +0000771 sectionStart = context.CreateTempSymbol();
772 else
773 sectionStart = context.GetOrCreateSymbol(Twine("EH_frame") + Twine(CIENum));
774
Bill Wendling3c163cf2011-06-30 21:25:51 +0000775 streamer.EmitLabel(sectionStart);
Rafael Espindola514cecc2011-04-28 03:26:11 +0000776 CIENum++;
777
Rafael Espindola89b93722010-12-10 07:39:47 +0000778 MCSymbol *sectionEnd = streamer.getContext().CreateTempSymbol();
779
780 // Length
781 const MCExpr *Length = MakeStartMinusEndExpr(streamer, *sectionStart,
782 *sectionEnd, 4);
Bill Wendling3c163cf2011-06-30 21:25:51 +0000783 if (verboseAsm) streamer.AddComment("CIE Length");
Rafael Espindola9266cc42011-04-27 01:43:49 +0000784 streamer.EmitAbsValue(Length, 4);
Rafael Espindola89b93722010-12-10 07:39:47 +0000785
786 // CIE ID
Rafael Espindola40a7dbb2011-05-10 03:54:12 +0000787 unsigned CIE_ID = IsEH ? 0 : -1;
Bill Wendling3c163cf2011-06-30 21:25:51 +0000788 if (verboseAsm) streamer.AddComment("CIE ID Tag");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +0000789 streamer.EmitIntValue(CIE_ID, 4);
Rafael Espindola89b93722010-12-10 07:39:47 +0000790
791 // Version
Bill Wendling3c163cf2011-06-30 21:25:51 +0000792 if (verboseAsm) streamer.AddComment("DW_CIE_VERSION");
Rafael Espindola89b93722010-12-10 07:39:47 +0000793 streamer.EmitIntValue(dwarf::DW_CIE_VERSION, 1);
794
795 // Augmentation String
796 SmallString<8> Augmentation;
Rafael Espindola40a7dbb2011-05-10 03:54:12 +0000797 if (IsEH) {
Bill Wendling3c163cf2011-06-30 21:25:51 +0000798 if (verboseAsm) streamer.AddComment("CIE Augmentation");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +0000799 Augmentation += "z";
800 if (personality)
801 Augmentation += "P";
802 if (lsda)
803 Augmentation += "L";
804 Augmentation += "R";
805 streamer.EmitBytes(Augmentation.str(), 0);
806 }
Rafael Espindola89b93722010-12-10 07:39:47 +0000807 streamer.EmitIntValue(0, 1);
808
809 // Code Alignment Factor
Bill Wendling3c163cf2011-06-30 21:25:51 +0000810 if (verboseAsm) streamer.AddComment("CIE Code Alignment Factor");
Rafael Espindola89b93722010-12-10 07:39:47 +0000811 streamer.EmitULEB128IntValue(1);
812
813 // Data Alignment Factor
Bill Wendling3c163cf2011-06-30 21:25:51 +0000814 if (verboseAsm) streamer.AddComment("CIE Data Alignment Factor");
Rafael Espindola89b93722010-12-10 07:39:47 +0000815 streamer.EmitSLEB128IntValue(getDataAlignmentFactor(streamer));
816
817 // Return Address Register
Bill Wendling3c163cf2011-06-30 21:25:51 +0000818 if (verboseAsm) streamer.AddComment("CIE Return Address Column");
Rafael Espindola89b93722010-12-10 07:39:47 +0000819 streamer.EmitULEB128IntValue(asmInfo.getDwarfRARegNum(true));
820
821 // Augmentation Data Length (optional)
Rafael Espindola1f6c8752011-04-29 21:50:57 +0000822
823 unsigned augmentationLength = 0;
Rafael Espindola40a7dbb2011-05-10 03:54:12 +0000824 if (IsEH) {
825 if (personality) {
826 // Personality Encoding
827 augmentationLength += 1;
828 // Personality
829 augmentationLength += getSizeForEncoding(streamer, personalityEncoding);
830 }
831 if (lsda)
832 augmentationLength += 1;
833 // Encoding of the FDE pointers
Rafael Espindola1f6c8752011-04-29 21:50:57 +0000834 augmentationLength += 1;
Rafael Espindola1f6c8752011-04-29 21:50:57 +0000835
Bill Wendling3c163cf2011-06-30 21:25:51 +0000836 if (verboseAsm) streamer.AddComment("Augmentation Size");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +0000837 streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola89b93722010-12-10 07:39:47 +0000838
Rafael Espindola40a7dbb2011-05-10 03:54:12 +0000839 // Augmentation Data (optional)
840 if (personality) {
841 // Personality Encoding
Bill Wendling3c163cf2011-06-30 21:25:51 +0000842 EmitEncodingByte(streamer, personalityEncoding,
843 "Personality Encoding");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +0000844 // Personality
Bill Wendling3c163cf2011-06-30 21:25:51 +0000845 if (verboseAsm) streamer.AddComment("Personality");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +0000846 EmitPersonality(streamer, *personality, personalityEncoding);
847 }
Bill Wendling3c163cf2011-06-30 21:25:51 +0000848
Rafael Espindola40a7dbb2011-05-10 03:54:12 +0000849 if (lsda)
Bill Wendling3c163cf2011-06-30 21:25:51 +0000850 EmitEncodingByte(streamer, lsdaEncoding, "LSDA Encoding");
851
Rafael Espindola40a7dbb2011-05-10 03:54:12 +0000852 // Encoding of the FDE pointers
Bill Wendling3c163cf2011-06-30 21:25:51 +0000853 EmitEncodingByte(streamer, asmInfo.getFDEEncoding(UsingCFI),
854 "FDE Encoding");
Rafael Espindolabdc31672010-12-27 15:56:22 +0000855 }
Rafael Espindola89b93722010-12-10 07:39:47 +0000856
857 // Initial Instructions
858
Bill Wendling5b46e8e2011-06-23 07:55:41 +0000859 const std::vector<MachineMove> &Moves = asmInfo.getInitialFrameState();
Rafael Espindolafe024d02010-12-28 18:36:23 +0000860 std::vector<MCCFIInstruction> Instructions;
Rafael Espindola89b93722010-12-10 07:39:47 +0000861
Rafael Espindolafe024d02010-12-28 18:36:23 +0000862 for (int i = 0, n = Moves.size(); i != n; ++i) {
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000863 MCSymbol *Label = Moves[i].getLabel();
864 const MachineLocation &Dst =
865 TranslateMachineLocation(asmInfo, Moves[i].getDestination());
866 const MachineLocation &Src =
867 TranslateMachineLocation(asmInfo, Moves[i].getSource());
868 MCCFIInstruction Inst(Label, Dst, Src);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000869 Instructions.push_back(Inst);
870 }
871
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000872 EmitCFIInstructions(streamer, Instructions, NULL);
Rafael Espindola89b93722010-12-10 07:39:47 +0000873
874 // Padding
Rafael Espindola40a7dbb2011-05-10 03:54:12 +0000875 streamer.EmitValueToAlignment(IsEH ? 4 : asmInfo.getPointerSize());
Rafael Espindola89b93722010-12-10 07:39:47 +0000876
877 streamer.EmitLabel(sectionEnd);
878 return *sectionStart;
879}
880
Rafael Espindola25f492e2011-04-12 16:12:03 +0000881MCSymbol *FrameEmitterImpl::EmitFDE(MCStreamer &streamer,
882 const MCSymbol &cieStart,
Rafael Espindola9f270da2011-05-10 03:01:39 +0000883 const MCDwarfFrameInfo &frame) {
Rafael Espindolabdc31672010-12-27 15:56:22 +0000884 MCContext &context = streamer.getContext();
885 MCSymbol *fdeStart = context.CreateTempSymbol();
886 MCSymbol *fdeEnd = context.CreateTempSymbol();
Rafael Espindolae3a0e982011-05-10 20:59:42 +0000887 const TargetAsmInfo &TAsmInfo = context.getTargetAsmInfo();
Bill Wendling2541c412011-06-30 22:02:20 +0000888 bool verboseAsm = streamer.isVerboseAsm();
Rafael Espindola89b93722010-12-10 07:39:47 +0000889
Rafael Espindolae3a0e982011-05-10 20:59:42 +0000890 if (!TAsmInfo.isFunctionEHFrameSymbolPrivate() && IsEH) {
Bill Wendling2541c412011-06-30 22:02:20 +0000891 MCSymbol *EHSym =
892 context.GetOrCreateSymbol(frame.Function->getName() + Twine(".eh"));
Rafael Espindola8bca4102011-04-28 12:50:37 +0000893 streamer.EmitEHSymAttributes(frame.Function, EHSym);
Rafael Espindolaa8cfb872011-04-28 02:46:42 +0000894 streamer.EmitLabel(EHSym);
895 }
896
Rafael Espindola89b93722010-12-10 07:39:47 +0000897 // Length
898 const MCExpr *Length = MakeStartMinusEndExpr(streamer, *fdeStart, *fdeEnd, 0);
Bill Wendling2541c412011-06-30 22:02:20 +0000899 if (verboseAsm) streamer.AddComment("FDE Length");
Rafael Espindola9266cc42011-04-27 01:43:49 +0000900 streamer.EmitAbsValue(Length, 4);
Rafael Espindola89b93722010-12-10 07:39:47 +0000901
902 streamer.EmitLabel(fdeStart);
Rafael Espindolae3a0e982011-05-10 20:59:42 +0000903
Rafael Espindola89b93722010-12-10 07:39:47 +0000904 // CIE Pointer
Rafael Espindolae3a0e982011-05-10 20:59:42 +0000905 const MCAsmInfo &asmInfo = context.getAsmInfo();
Rafael Espindola0d450dc2011-05-10 15:20:23 +0000906 if (IsEH) {
907 const MCExpr *offset = MakeStartMinusEndExpr(streamer, cieStart, *fdeStart,
908 0);
Bill Wendling2541c412011-06-30 22:02:20 +0000909 if (verboseAsm) streamer.AddComment("FDE CIE Offset");
Rafael Espindola0d450dc2011-05-10 15:20:23 +0000910 streamer.EmitAbsValue(offset, 4);
Rafael Espindolae3a0e982011-05-10 20:59:42 +0000911 } else if (!asmInfo.doesDwarfRequireRelocationForSectionOffset()) {
912 const MCExpr *offset = MakeStartMinusEndExpr(streamer, *SectionStart,
913 cieStart, 0);
914 streamer.EmitAbsValue(offset, 4);
Rafael Espindola0d450dc2011-05-10 15:20:23 +0000915 } else {
916 streamer.EmitSymbolValue(&cieStart, 4);
917 }
Bill Wendling2541c412011-06-30 22:02:20 +0000918
Rafael Espindolae3a0e982011-05-10 20:59:42 +0000919 unsigned fdeEncoding = TAsmInfo.getFDEEncoding(UsingCFI);
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000920 unsigned size = getSizeForEncoding(streamer, fdeEncoding);
Rafael Espindola89b93722010-12-10 07:39:47 +0000921
922 // PC Begin
Rafael Espindola9989ef42011-05-10 22:28:35 +0000923 unsigned PCBeginEncoding = IsEH ? fdeEncoding :
924 (unsigned)dwarf::DW_EH_PE_absptr;
Rafael Espindola40a7dbb2011-05-10 03:54:12 +0000925 unsigned PCBeginSize = getSizeForEncoding(streamer, PCBeginEncoding);
Bill Wendling2541c412011-06-30 22:02:20 +0000926 EmitSymbol(streamer, *frame.Begin, PCBeginEncoding, "FDE initial location");
Rafael Espindola89b93722010-12-10 07:39:47 +0000927
928 // PC Range
929 const MCExpr *Range = MakeStartMinusEndExpr(streamer, *frame.Begin,
930 *frame.End, 0);
Bill Wendling2541c412011-06-30 22:02:20 +0000931 if (verboseAsm) streamer.AddComment("FDE address range");
Rafael Espindola9266cc42011-04-27 01:43:49 +0000932 streamer.EmitAbsValue(Range, size);
Rafael Espindola89b93722010-12-10 07:39:47 +0000933
Rafael Espindola40a7dbb2011-05-10 03:54:12 +0000934 if (IsEH) {
935 // Augmentation Data Length
936 unsigned augmentationLength = 0;
Rafael Espindola1f6c8752011-04-29 21:50:57 +0000937
Rafael Espindola40a7dbb2011-05-10 03:54:12 +0000938 if (frame.Lsda)
939 augmentationLength += getSizeForEncoding(streamer, frame.LsdaEncoding);
Rafael Espindola1f6c8752011-04-29 21:50:57 +0000940
Bill Wendling2541c412011-06-30 22:02:20 +0000941 if (verboseAsm) streamer.AddComment("Augmentation size");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +0000942 streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola89b93722010-12-10 07:39:47 +0000943
Rafael Espindola40a7dbb2011-05-10 03:54:12 +0000944 // Augmentation Data
945 if (frame.Lsda)
Bill Wendling2541c412011-06-30 22:02:20 +0000946 EmitSymbol(streamer, *frame.Lsda, frame.LsdaEncoding,
947 "Language Specific Data Area");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +0000948 }
Rafael Espindola4892dff2011-04-29 03:06:29 +0000949
Rafael Espindola89b93722010-12-10 07:39:47 +0000950 // Call Frame Instructions
951
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000952 EmitCFIInstructions(streamer, frame.Instructions, frame.Begin);
Rafael Espindola5bba0842010-12-28 04:15:37 +0000953
Rafael Espindola89b93722010-12-10 07:39:47 +0000954 // Padding
Rafael Espindola40a7dbb2011-05-10 03:54:12 +0000955 streamer.EmitValueToAlignment(PCBeginSize);
Rafael Espindoladfe125c2010-12-17 00:28:02 +0000956
957 return fdeEnd;
Rafael Espindola89b93722010-12-10 07:39:47 +0000958}
959
Benjamin Kramer19282362010-12-30 22:34:44 +0000960namespace {
961 struct CIEKey {
962 static const CIEKey getEmptyKey() { return CIEKey(0, 0, -1); }
963 static const CIEKey getTombstoneKey() { return CIEKey(0, -1, 0); }
Rafael Espindolabdc31672010-12-27 15:56:22 +0000964
Benjamin Kramer19282362010-12-30 22:34:44 +0000965 CIEKey(const MCSymbol* Personality_, unsigned PersonalityEncoding_,
966 unsigned LsdaEncoding_) : Personality(Personality_),
967 PersonalityEncoding(PersonalityEncoding_),
968 LsdaEncoding(LsdaEncoding_) {
969 }
970 const MCSymbol* Personality;
971 unsigned PersonalityEncoding;
972 unsigned LsdaEncoding;
973 };
974}
Rafael Espindolabdc31672010-12-27 15:56:22 +0000975
976namespace llvm {
977 template <>
978 struct DenseMapInfo<CIEKey> {
979 static CIEKey getEmptyKey() {
Benjamin Kramer19282362010-12-30 22:34:44 +0000980 return CIEKey::getEmptyKey();
Rafael Espindolabdc31672010-12-27 15:56:22 +0000981 }
982 static CIEKey getTombstoneKey() {
Benjamin Kramer19282362010-12-30 22:34:44 +0000983 return CIEKey::getTombstoneKey();
Rafael Espindolabdc31672010-12-27 15:56:22 +0000984 }
985 static unsigned getHashValue(const CIEKey &Key) {
986 FoldingSetNodeID ID;
987 ID.AddPointer(Key.Personality);
988 ID.AddInteger(Key.PersonalityEncoding);
989 ID.AddInteger(Key.LsdaEncoding);
990 return ID.ComputeHash();
991 }
992 static bool isEqual(const CIEKey &LHS,
993 const CIEKey &RHS) {
994 return LHS.Personality == RHS.Personality &&
995 LHS.PersonalityEncoding == RHS.PersonalityEncoding &&
996 LHS.LsdaEncoding == RHS.LsdaEncoding;
997 }
998 };
999}
1000
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001001void MCDwarfFrameEmitter::Emit(MCStreamer &Streamer,
1002 bool UsingCFI,
1003 bool IsEH) {
1004 MCContext &Context = Streamer.getContext();
1005 const TargetAsmInfo &AsmInfo = Context.getTargetAsmInfo();
1006 const MCSection &Section = IsEH ? *AsmInfo.getEHFrameSection() :
1007 *AsmInfo.getDwarfFrameSection();
1008 Streamer.SwitchSection(&Section);
1009 MCSymbol *SectionStart = Context.CreateTempSymbol();
1010 Streamer.EmitLabel(SectionStart);
Rafael Espindola90998132011-04-29 02:42:28 +00001011
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001012 MCSymbol *FDEEnd = NULL;
Rafael Espindolabdc31672010-12-27 15:56:22 +00001013 DenseMap<CIEKey, const MCSymbol*> CIEStarts;
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001014 FrameEmitterImpl Emitter(UsingCFI, IsEH, SectionStart);
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00001015
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001016 const MCSymbol *DummyDebugKey = NULL;
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001017 for (unsigned i = 0, n = Streamer.getNumFrameInfos(); i < n; ++i) {
1018 const MCDwarfFrameInfo &Frame = Streamer.getFrameInfo(i);
1019 if (IsEH && AsmInfo.getCompactUnwindSection() &&
1020 Emitter.EmitCompactUnwind(Streamer, Frame))
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001021 continue;
1022
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001023 CIEKey Key(Frame.Personality, Frame.PersonalityEncoding,
1024 Frame.LsdaEncoding);
1025 const MCSymbol *&CIEStart = IsEH ? CIEStarts[Key] : DummyDebugKey;
1026 if (!CIEStart)
1027 CIEStart = &Emitter.EmitCIE(Streamer, Frame.Personality,
1028 Frame.PersonalityEncoding, Frame.Lsda,
1029 Frame.LsdaEncoding);
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001030
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001031 FDEEnd = Emitter.EmitFDE(Streamer, *CIEStart, Frame);
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001032
Rafael Espindoladfe125c2010-12-17 00:28:02 +00001033 if (i != n - 1)
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001034 Streamer.EmitLabel(FDEEnd);
Rafael Espindoladfe125c2010-12-17 00:28:02 +00001035 }
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00001036
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001037 Streamer.EmitValueToAlignment(AsmInfo.getPointerSize());
1038 if (FDEEnd)
1039 Streamer.EmitLabel(FDEEnd);
Rafael Espindola89b93722010-12-10 07:39:47 +00001040}
Rafael Espindola245a1e22010-12-28 05:39:27 +00001041
1042void MCDwarfFrameEmitter::EmitAdvanceLoc(MCStreamer &Streamer,
1043 uint64_t AddrDelta) {
1044 SmallString<256> Tmp;
1045 raw_svector_ostream OS(Tmp);
Rafael Espindola4eafe102011-05-08 14:35:21 +00001046 MCDwarfFrameEmitter::EncodeAdvanceLoc(AddrDelta, OS);
Rafael Espindola245a1e22010-12-28 05:39:27 +00001047 Streamer.EmitBytes(OS.str(), /*AddrSpace=*/0);
1048}
1049
1050void MCDwarfFrameEmitter::EncodeAdvanceLoc(uint64_t AddrDelta,
Rafael Espindola4eafe102011-05-08 14:35:21 +00001051 raw_ostream &OS) {
Rafael Espindola245a1e22010-12-28 05:39:27 +00001052 // FIXME: Assumes the code alignment factor is 1.
Rafael Espindola3b78cdc2010-12-28 23:38:03 +00001053 if (AddrDelta == 0) {
Rafael Espindola4eafe102011-05-08 14:35:21 +00001054 } else if (isUIntN(6, AddrDelta)) {
Rafael Espindola245a1e22010-12-28 05:39:27 +00001055 uint8_t Opcode = dwarf::DW_CFA_advance_loc | AddrDelta;
1056 OS << Opcode;
Rafael Espindola4eafe102011-05-08 14:35:21 +00001057 } else if (isUInt<8>(AddrDelta)) {
Rafael Espindola245a1e22010-12-28 05:39:27 +00001058 OS << uint8_t(dwarf::DW_CFA_advance_loc1);
1059 OS << uint8_t(AddrDelta);
Rafael Espindola4eafe102011-05-08 14:35:21 +00001060 } else if (isUInt<16>(AddrDelta)) {
Rafael Espindolaa7e45052010-12-29 02:30:49 +00001061 // FIXME: check what is the correct behavior on a big endian machine.
Rafael Espindola245a1e22010-12-28 05:39:27 +00001062 OS << uint8_t(dwarf::DW_CFA_advance_loc2);
Rafael Espindolaa7e45052010-12-29 02:30:49 +00001063 OS << uint8_t( AddrDelta & 0xff);
1064 OS << uint8_t((AddrDelta >> 8) & 0xff);
Rafael Espindola245a1e22010-12-28 05:39:27 +00001065 } else {
Rafael Espindolaa7e45052010-12-29 02:30:49 +00001066 // FIXME: check what is the correct behavior on a big endian machine.
Rafael Espindola245a1e22010-12-28 05:39:27 +00001067 assert(isUInt<32>(AddrDelta));
1068 OS << uint8_t(dwarf::DW_CFA_advance_loc4);
Rafael Espindolaa7e45052010-12-29 02:30:49 +00001069 OS << uint8_t( AddrDelta & 0xff);
1070 OS << uint8_t((AddrDelta >> 8) & 0xff);
1071 OS << uint8_t((AddrDelta >> 16) & 0xff);
1072 OS << uint8_t((AddrDelta >> 24) & 0xff);
1073
Rafael Espindola245a1e22010-12-28 05:39:27 +00001074 }
1075}