blob: 3b816682d6bb3f68382ec8f11ecb7ff76ed328c0 [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.
53static inline uint64_t ScaleAddrDelta(uint64_t AddrDelta)
54{
55 if (DWARF2_LINE_MIN_INSN_LENGTH == 1)
56 return AddrDelta;
57 if (AddrDelta % DWARF2_LINE_MIN_INSN_LENGTH != 0) {
58 // TODO: report this error, but really only once.
59 ;
60 }
61 return AddrDelta / DWARF2_LINE_MIN_INSN_LENGTH;
62}
63
64//
65// This is called when an instruction is assembled into the specified section
66// and if there is information from the last .loc directive that has yet to have
67// a line entry made for it is made.
68//
Rafael Espindola195a0ce2010-11-19 02:26:16 +000069void MCLineEntry::Make(MCStreamer *MCOS, const MCSection *Section) {
Kevin Enderbyc0957932010-09-30 16:52:03 +000070 if (!MCOS->getContext().getDwarfLocSeen())
71 return;
72
73 // Create a symbol at in the current section for use in the line entry.
74 MCSymbol *LineSym = MCOS->getContext().CreateTempSymbol();
75 // Set the value of the symbol to use for the MCLineEntry.
76 MCOS->EmitLabel(LineSym);
77
78 // Get the current .loc info saved in the context.
79 const MCDwarfLoc &DwarfLoc = MCOS->getContext().getCurrentDwarfLoc();
80
81 // Create a (local) line entry with the symbol and the current .loc info.
82 MCLineEntry LineEntry(LineSym, DwarfLoc);
83
84 // clear DwarfLocSeen saying the current .loc info is now used.
Kevin Enderby3f55c242010-10-04 20:17:24 +000085 MCOS->getContext().ClearDwarfLocSeen();
Kevin Enderbyc0957932010-09-30 16:52:03 +000086
87 // Get the MCLineSection for this section, if one does not exist for this
88 // section create it.
Rafael Espindola17fd7bd2010-11-19 07:41:23 +000089 const DenseMap<const MCSection *, MCLineSection *> &MCLineSections =
Kevin Enderbyc0957932010-09-30 16:52:03 +000090 MCOS->getContext().getMCLineSections();
Rafael Espindola17fd7bd2010-11-19 07:41:23 +000091 MCLineSection *LineSection = MCLineSections.lookup(Section);
Kevin Enderbyc0957932010-09-30 16:52:03 +000092 if (!LineSection) {
93 // Create a new MCLineSection. This will be deleted after the dwarf line
94 // table is created using it by iterating through the MCLineSections
95 // DenseMap.
96 LineSection = new MCLineSection;
97 // Save a pointer to the new LineSection into the MCLineSections DenseMap.
Rafael Espindola17fd7bd2010-11-19 07:41:23 +000098 MCOS->getContext().addMCLineSection(Section, LineSection);
Kevin Enderbyc0957932010-09-30 16:52:03 +000099 }
100
101 // Add the line entry to this section's entries.
102 LineSection->addLineEntry(LineEntry);
103}
104
105//
106// This helper routine returns an expression of End - Start + IntVal .
107//
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000108static inline const MCExpr *MakeStartMinusEndExpr(const MCStreamer &MCOS,
109 const MCSymbol &Start,
110 const MCSymbol &End,
111 int IntVal) {
Kevin Enderbyc0957932010-09-30 16:52:03 +0000112 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
113 const MCExpr *Res =
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000114 MCSymbolRefExpr::Create(&End, Variant, MCOS.getContext());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000115 const MCExpr *RHS =
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000116 MCSymbolRefExpr::Create(&Start, Variant, MCOS.getContext());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000117 const MCExpr *Res1 =
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000118 MCBinaryExpr::Create(MCBinaryExpr::Sub, Res, RHS, MCOS.getContext());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000119 const MCExpr *Res2 =
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000120 MCConstantExpr::Create(IntVal, MCOS.getContext());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000121 const MCExpr *Res3 =
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000122 MCBinaryExpr::Create(MCBinaryExpr::Sub, Res1, Res2, MCOS.getContext());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000123 return Res3;
124}
125
Kevin Enderbyc0957932010-09-30 16:52:03 +0000126//
127// This emits the Dwarf line table for the specified section from the entries
128// in the LineSection.
129//
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000130static inline void EmitDwarfLineTable(MCStreamer *MCOS,
Kevin Enderbyc0957932010-09-30 16:52:03 +0000131 const MCSection *Section,
Rafael Espindola89b93722010-12-10 07:39:47 +0000132 const MCLineSection *LineSection) {
Kevin Enderbyc0957932010-09-30 16:52:03 +0000133 unsigned FileNum = 1;
134 unsigned LastLine = 1;
135 unsigned Column = 0;
136 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
137 unsigned Isa = 0;
Kevin Enderbyc0957932010-09-30 16:52:03 +0000138 MCSymbol *LastLabel = NULL;
Kevin Enderbyc0957932010-09-30 16:52:03 +0000139
140 // Loop through each MCLineEntry and encode the dwarf line number table.
Rafael Espindola17fd7bd2010-11-19 07:41:23 +0000141 for (MCLineSection::const_iterator
Kevin Enderbyc0957932010-09-30 16:52:03 +0000142 it = LineSection->getMCLineEntries()->begin(),
143 ie = LineSection->getMCLineEntries()->end(); it != ie; ++it) {
144
145 if (FileNum != it->getFileNum()) {
146 FileNum = it->getFileNum();
147 MCOS->EmitIntValue(dwarf::DW_LNS_set_file, 1);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000148 MCOS->EmitULEB128IntValue(FileNum);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000149 }
150 if (Column != it->getColumn()) {
151 Column = it->getColumn();
152 MCOS->EmitIntValue(dwarf::DW_LNS_set_column, 1);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000153 MCOS->EmitULEB128IntValue(Column);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000154 }
155 if (Isa != it->getIsa()) {
156 Isa = it->getIsa();
157 MCOS->EmitIntValue(dwarf::DW_LNS_set_isa, 1);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000158 MCOS->EmitULEB128IntValue(Isa);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000159 }
160 if ((it->getFlags() ^ Flags) & DWARF2_FLAG_IS_STMT) {
161 Flags = it->getFlags();
162 MCOS->EmitIntValue(dwarf::DW_LNS_negate_stmt, 1);
163 }
164 if (it->getFlags() & DWARF2_FLAG_BASIC_BLOCK)
165 MCOS->EmitIntValue(dwarf::DW_LNS_set_basic_block, 1);
166 if (it->getFlags() & DWARF2_FLAG_PROLOGUE_END)
167 MCOS->EmitIntValue(dwarf::DW_LNS_set_prologue_end, 1);
168 if (it->getFlags() & DWARF2_FLAG_EPILOGUE_BEGIN)
169 MCOS->EmitIntValue(dwarf::DW_LNS_set_epilogue_begin, 1);
170
Rafael Espindola64185cc2010-11-13 01:06:27 +0000171 int64_t LineDelta = static_cast<int64_t>(it->getLine()) - LastLine;
Kevin Enderbyc0957932010-09-30 16:52:03 +0000172 MCSymbol *Label = it->getLabel();
173
174 // At this point we want to emit/create the sequence to encode the delta in
175 // line numbers and the increment of the address from the previous Label
176 // and the current Label.
Rafael Espindola32a006e2010-12-03 00:55:40 +0000177 MCOS->EmitDwarfAdvanceLineAddr(LineDelta, LastLabel, Label);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000178
179 LastLine = it->getLine();
180 LastLabel = Label;
Kevin Enderbyc0957932010-09-30 16:52:03 +0000181 }
182
183 // Emit a DW_LNE_end_sequence for the end of the section.
184 // Using the pointer Section create a temporary label at the end of the
185 // section and use that and the LastLabel to compute the address delta
186 // and use INT64_MAX as the line delta which is the signal that this is
187 // actually a DW_LNE_end_sequence.
188
189 // Switch to the section to be able to create a symbol at its end.
190 MCOS->SwitchSection(Section);
Rafael Espindola89b93722010-12-10 07:39:47 +0000191
192 MCContext &context = MCOS->getContext();
Kevin Enderbyc0957932010-09-30 16:52:03 +0000193 // Create a symbol at the end of the section.
Rafael Espindola89b93722010-12-10 07:39:47 +0000194 MCSymbol *SectionEnd = context.CreateTempSymbol();
Kevin Enderbyc0957932010-09-30 16:52:03 +0000195 // Set the value of the symbol, as we are at the end of the section.
196 MCOS->EmitLabel(SectionEnd);
197
198 // Switch back the the dwarf line section.
Rafael Espindola89b93722010-12-10 07:39:47 +0000199 MCOS->SwitchSection(context.getTargetAsmInfo().getDwarfLineSection());
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000200
Rafael Espindola32a006e2010-12-03 00:55:40 +0000201 MCOS->EmitDwarfAdvanceLineAddr(INT64_MAX, LastLabel, SectionEnd);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000202}
203
204//
205// This emits the Dwarf file and the line tables.
206//
Rafael Espindola89b93722010-12-10 07:39:47 +0000207void MCDwarfFileTable::Emit(MCStreamer *MCOS) {
208 MCContext &context = MCOS->getContext();
Kevin Enderbyc0957932010-09-30 16:52:03 +0000209 // Switch to the section where the table will be emitted into.
Rafael Espindola89b93722010-12-10 07:39:47 +0000210 MCOS->SwitchSection(context.getTargetAsmInfo().getDwarfLineSection());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000211
212 // Create a symbol at the beginning of this section.
Rafael Espindola89b93722010-12-10 07:39:47 +0000213 MCSymbol *LineStartSym = context.CreateTempSymbol();
Kevin Enderbyc0957932010-09-30 16:52:03 +0000214 // Set the value of the symbol, as we are at the start of the section.
215 MCOS->EmitLabel(LineStartSym);
216
217 // Create a symbol for the end of the section (to be set when we get there).
Rafael Espindola89b93722010-12-10 07:39:47 +0000218 MCSymbol *LineEndSym = context.CreateTempSymbol();
Kevin Enderbyc0957932010-09-30 16:52:03 +0000219
220 // The first 4 bytes is the total length of the information for this
221 // compilation unit (not including these 4 bytes for the length).
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000222 MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *LineEndSym,4),
Rafael Espindola0bbe0b42010-12-06 17:27:56 +0000223 4);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000224
225 // Next 2 bytes is the Version, which is Dwarf 2.
226 MCOS->EmitIntValue(2, 2);
227
228 // Create a symbol for the end of the prologue (to be set when we get there).
Rafael Espindola89b93722010-12-10 07:39:47 +0000229 MCSymbol *ProEndSym = context.CreateTempSymbol(); // Lprologue_end
Kevin Enderbyc0957932010-09-30 16:52:03 +0000230
231 // Length of the prologue, is the next 4 bytes. Which is the start of the
232 // section to the end of the prologue. Not including the 4 bytes for the
233 // total length, the 2 bytes for the version, and these 4 bytes for the
234 // length of the prologue.
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000235 MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *ProEndSym,
Kevin Enderbyc0957932010-09-30 16:52:03 +0000236 (4 + 2 + 4)),
Rafael Espindola5d4918d2010-12-04 03:21:47 +0000237 4, 0);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000238
239 // Parameters of the state machine, are next.
240 MCOS->EmitIntValue(DWARF2_LINE_MIN_INSN_LENGTH, 1);
241 MCOS->EmitIntValue(DWARF2_LINE_DEFAULT_IS_STMT, 1);
242 MCOS->EmitIntValue(DWARF2_LINE_BASE, 1);
243 MCOS->EmitIntValue(DWARF2_LINE_RANGE, 1);
244 MCOS->EmitIntValue(DWARF2_LINE_OPCODE_BASE, 1);
245
246 // Standard opcode lengths
247 MCOS->EmitIntValue(0, 1); // length of DW_LNS_copy
248 MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_pc
249 MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_line
250 MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_file
251 MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_column
252 MCOS->EmitIntValue(0, 1); // length of DW_LNS_negate_stmt
253 MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_basic_block
254 MCOS->EmitIntValue(0, 1); // length of DW_LNS_const_add_pc
255 MCOS->EmitIntValue(1, 1); // length of DW_LNS_fixed_advance_pc
256 MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_prologue_end
257 MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_epilogue_begin
258 MCOS->EmitIntValue(1, 1); // DW_LNS_set_isa
259
260 // Put out the directory and file tables.
261
262 // First the directory table.
263 const std::vector<StringRef> &MCDwarfDirs =
Rafael Espindola89b93722010-12-10 07:39:47 +0000264 context.getMCDwarfDirs();
Kevin Enderbyc0957932010-09-30 16:52:03 +0000265 for (unsigned i = 0; i < MCDwarfDirs.size(); i++) {
266 MCOS->EmitBytes(MCDwarfDirs[i], 0); // the DirectoryName
267 MCOS->EmitBytes(StringRef("\0", 1), 0); // the null term. of the string
268 }
269 MCOS->EmitIntValue(0, 1); // Terminate the directory list
270
271 // Second the file table.
272 const std::vector<MCDwarfFile *> &MCDwarfFiles =
273 MCOS->getContext().getMCDwarfFiles();
274 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
275 MCOS->EmitBytes(MCDwarfFiles[i]->getName(), 0); // FileName
276 MCOS->EmitBytes(StringRef("\0", 1), 0); // the null term. of the string
Rafael Espindola3ff57092010-11-02 17:22:24 +0000277 // the Directory num
278 MCOS->EmitULEB128IntValue(MCDwarfFiles[i]->getDirIndex());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000279 MCOS->EmitIntValue(0, 1); // last modification timestamp (always 0)
280 MCOS->EmitIntValue(0, 1); // filesize (always 0)
281 }
282 MCOS->EmitIntValue(0, 1); // Terminate the file list
283
284 // This is the end of the prologue, so set the value of the symbol at the
285 // end of the prologue (that was used in a previous expression).
286 MCOS->EmitLabel(ProEndSym);
287
288 // Put out the line tables.
Rafael Espindola17fd7bd2010-11-19 07:41:23 +0000289 const DenseMap<const MCSection *, MCLineSection *> &MCLineSections =
Kevin Enderbyc0957932010-09-30 16:52:03 +0000290 MCOS->getContext().getMCLineSections();
Rafael Espindola17fd7bd2010-11-19 07:41:23 +0000291 const std::vector<const MCSection *> &MCLineSectionOrder =
292 MCOS->getContext().getMCLineSectionOrder();
293 for (std::vector<const MCSection*>::const_iterator it =
294 MCLineSectionOrder.begin(), ie = MCLineSectionOrder.end(); it != ie;
295 ++it) {
296 const MCSection *Sec = *it;
297 const MCLineSection *Line = MCLineSections.lookup(Sec);
Rafael Espindola89b93722010-12-10 07:39:47 +0000298 EmitDwarfLineTable(MCOS, Sec, Line);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000299
300 // Now delete the MCLineSections that were created in MCLineEntry::Make()
301 // and used to emit the line table.
Rafael Espindola17fd7bd2010-11-19 07:41:23 +0000302 delete Line;
Kevin Enderbyc0957932010-09-30 16:52:03 +0000303 }
304
Rafael Espindola767b1be2010-12-04 00:31:13 +0000305 if (MCOS->getContext().getAsmInfo().getLinkerRequiresNonEmptyDwarfLines()
306 && MCLineSectionOrder.begin() == MCLineSectionOrder.end()) {
Rafael Espindolaa8de83c2010-12-03 23:36:59 +0000307 // The darwin9 linker has a bug (see PR8715). For for 32-bit architectures
308 // it requires:
309 // total_length >= prologue_length + 10
310 // We are 4 bytes short, since we have total_length = 51 and
311 // prologue_length = 45
Devang Patel5113cdb2010-12-03 00:10:48 +0000312
Rafael Espindolaa8de83c2010-12-03 23:36:59 +0000313 // The regular end_sequence should be sufficient.
314 MCDwarfLineAddr::Emit(MCOS, INT64_MAX, 0);
Devang Patel5113cdb2010-12-03 00:10:48 +0000315 }
316
Kevin Enderbyc0957932010-09-30 16:52:03 +0000317 // This is the end of the section, so set the value of the symbol at the end
318 // of this section (that was used in a previous expression).
319 MCOS->EmitLabel(LineEndSym);
320}
321
Kevin Enderbyc0957932010-09-30 16:52:03 +0000322/// Utility function to write the encoding to an object writer.
323void MCDwarfLineAddr::Write(MCObjectWriter *OW, int64_t LineDelta,
324 uint64_t AddrDelta) {
325 SmallString<256> Tmp;
326 raw_svector_ostream OS(Tmp);
327 MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OS);
328 OW->WriteBytes(OS.str());
329}
330
331/// Utility function to emit the encoding to a streamer.
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000332void MCDwarfLineAddr::Emit(MCStreamer *MCOS, int64_t LineDelta,
Kevin Enderbyc0957932010-09-30 16:52:03 +0000333 uint64_t AddrDelta) {
334 SmallString<256> Tmp;
335 raw_svector_ostream OS(Tmp);
336 MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OS);
337 MCOS->EmitBytes(OS.str(), /*AddrSpace=*/0);
338}
339
340/// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
341void MCDwarfLineAddr::Encode(int64_t LineDelta, uint64_t AddrDelta,
342 raw_ostream &OS) {
343 uint64_t Temp, Opcode;
344 bool NeedCopy = false;
345
346 // Scale the address delta by the minimum instruction length.
347 AddrDelta = ScaleAddrDelta(AddrDelta);
348
349 // A LineDelta of INT64_MAX is a signal that this is actually a
350 // DW_LNE_end_sequence. We cannot use special opcodes here, since we want the
351 // end_sequence to emit the matrix entry.
352 if (LineDelta == INT64_MAX) {
353 if (AddrDelta == MAX_SPECIAL_ADDR_DELTA)
354 OS << char(dwarf::DW_LNS_const_add_pc);
355 else {
356 OS << char(dwarf::DW_LNS_advance_pc);
357 SmallString<32> Tmp;
358 raw_svector_ostream OSE(Tmp);
359 MCObjectWriter::EncodeULEB128(AddrDelta, OSE);
360 OS << OSE.str();
361 }
362 OS << char(dwarf::DW_LNS_extended_op);
363 OS << char(1);
364 OS << char(dwarf::DW_LNE_end_sequence);
365 return;
366 }
367
368 // Bias the line delta by the base.
369 Temp = LineDelta - DWARF2_LINE_BASE;
370
371 // If the line increment is out of range of a special opcode, we must encode
372 // it with DW_LNS_advance_line.
373 if (Temp >= DWARF2_LINE_RANGE) {
374 OS << char(dwarf::DW_LNS_advance_line);
375 SmallString<32> Tmp;
376 raw_svector_ostream OSE(Tmp);
377 MCObjectWriter::EncodeSLEB128(LineDelta, OSE);
378 OS << OSE.str();
379
380 LineDelta = 0;
381 Temp = 0 - DWARF2_LINE_BASE;
382 NeedCopy = true;
383 }
384
385 // Use DW_LNS_copy instead of a "line +0, addr +0" special opcode.
386 if (LineDelta == 0 && AddrDelta == 0) {
387 OS << char(dwarf::DW_LNS_copy);
388 return;
389 }
390
391 // Bias the opcode by the special opcode base.
392 Temp += DWARF2_LINE_OPCODE_BASE;
393
394 // Avoid overflow when addr_delta is large.
395 if (AddrDelta < 256 + MAX_SPECIAL_ADDR_DELTA) {
396 // Try using a special opcode.
397 Opcode = Temp + AddrDelta * DWARF2_LINE_RANGE;
398 if (Opcode <= 255) {
399 OS << char(Opcode);
400 return;
401 }
402
403 // Try using DW_LNS_const_add_pc followed by special op.
404 Opcode = Temp + (AddrDelta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
405 if (Opcode <= 255) {
406 OS << char(dwarf::DW_LNS_const_add_pc);
407 OS << char(Opcode);
408 return;
409 }
410 }
411
412 // Otherwise use DW_LNS_advance_pc.
413 OS << char(dwarf::DW_LNS_advance_pc);
414 SmallString<32> Tmp;
415 raw_svector_ostream OSE(Tmp);
416 MCObjectWriter::EncodeULEB128(AddrDelta, OSE);
417 OS << OSE.str();
418
419 if (NeedCopy)
420 OS << char(dwarf::DW_LNS_copy);
421 else
422 OS << char(Temp);
423}
424
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000425void MCDwarfFile::print(raw_ostream &OS) const {
426 OS << '"' << getName() << '"';
427}
428
429void MCDwarfFile::dump() const {
430 print(dbgs());
431}
Kevin Enderbyc0957932010-09-30 16:52:03 +0000432
Rafael Espindola89b93722010-12-10 07:39:47 +0000433static int getDataAlignmentFactor(MCStreamer &streamer) {
434 MCContext &context = streamer.getContext();
435 const TargetAsmInfo &asmInfo = context.getTargetAsmInfo();
436 int size = asmInfo.getPointerSize();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000437 if (asmInfo.getStackGrowthDirection() == TargetFrameLowering::StackGrowsUp)
Rafael Espindola89b93722010-12-10 07:39:47 +0000438 return size;
439 else
440 return -size;
441}
442
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000443static unsigned getSizeForEncoding(MCStreamer &streamer,
444 unsigned symbolEncoding) {
Rafael Espindolabdc31672010-12-27 15:56:22 +0000445 MCContext &context = streamer.getContext();
446 const TargetAsmInfo &asmInfo = context.getTargetAsmInfo();
447 unsigned format = symbolEncoding & 0x0f;
Rafael Espindolabdc31672010-12-27 15:56:22 +0000448 switch (format) {
449 default:
450 assert(0 && "Unknown Encoding");
Rafael Espindolabdc31672010-12-27 15:56:22 +0000451 case dwarf::DW_EH_PE_absptr:
452 case dwarf::DW_EH_PE_signed:
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000453 return asmInfo.getPointerSize();
Rafael Espindolabdc31672010-12-27 15:56:22 +0000454 case dwarf::DW_EH_PE_udata2:
455 case dwarf::DW_EH_PE_sdata2:
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000456 return 2;
Rafael Espindolabdc31672010-12-27 15:56:22 +0000457 case dwarf::DW_EH_PE_udata4:
458 case dwarf::DW_EH_PE_sdata4:
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000459 return 4;
Rafael Espindolabdc31672010-12-27 15:56:22 +0000460 case dwarf::DW_EH_PE_udata8:
461 case dwarf::DW_EH_PE_sdata8:
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000462 return 8;
Rafael Espindolabdc31672010-12-27 15:56:22 +0000463 }
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000464}
465
466static void EmitSymbol(MCStreamer &streamer, const MCSymbol &symbol,
467 unsigned symbolEncoding) {
Rafael Espindolaa0057ca2011-04-28 21:04:39 +0000468 MCContext &context = streamer.getContext();
469 const MCAsmInfo &asmInfo = context.getAsmInfo();
470 const MCExpr *v = asmInfo.getExprForFDESymbol(&symbol,
471 streamer);
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000472 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
473 unsigned application = symbolEncoding & 0x70;
Rafael Espindolaa0057ca2011-04-28 21:04:39 +0000474 if (isa<MCSymbolRefExpr>(v) && application == dwarf::DW_EH_PE_pcrel)
475 streamer.EmitPCRelValue(v, size);
476 else
477 streamer.EmitAbsValue(v, size);
Rafael Espindolabdc31672010-12-27 15:56:22 +0000478}
479
Rafael Espindolabfa27cc2011-04-28 16:09:09 +0000480static void EmitPersonality(MCStreamer &streamer, const MCSymbol &symbol,
481 unsigned symbolEncoding) {
482 MCContext &context = streamer.getContext();
483 const MCAsmInfo &asmInfo = context.getAsmInfo();
484 const MCExpr *v = asmInfo.getExprForPersonalitySymbol(&symbol, streamer);
485 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
486 unsigned application = symbolEncoding & 0x70;
487 if (isa<MCSymbolRefExpr>(v) && application == dwarf::DW_EH_PE_pcrel)
488 streamer.EmitPCRelValue(v, size);
489 else
490 streamer.EmitValue(v, size);
491}
492
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000493static const MachineLocation TranslateMachineLocation(
494 const TargetAsmInfo &AsmInfo,
495 const MachineLocation &Loc) {
496 unsigned Reg = Loc.getReg() == MachineLocation::VirtualFP ?
497 MachineLocation::VirtualFP :
498 unsigned(AsmInfo.getDwarfRegNum(Loc.getReg(), true));
499 const MachineLocation &NewLoc = Loc.isReg() ?
500 MachineLocation(Reg) : MachineLocation(Reg, Loc.getOffset());
501 return NewLoc;
502}
503
Rafael Espindola25f492e2011-04-12 16:12:03 +0000504namespace {
505 class FrameEmitterImpl {
506 int CFAOffset;
Rafael Espindola514cecc2011-04-28 03:26:11 +0000507 int CIENum;
Rafael Espindola25f492e2011-04-12 16:12:03 +0000508
509 public:
Rafael Espindola514cecc2011-04-28 03:26:11 +0000510 FrameEmitterImpl() : CFAOffset(0), CIENum(0) {
Rafael Espindola25f492e2011-04-12 16:12:03 +0000511 }
512
513 const MCSymbol &EmitCIE(MCStreamer &streamer,
514 const MCSymbol *personality,
515 unsigned personalityEncoding,
516 const MCSymbol *lsda,
517 unsigned lsdaEncoding);
518 MCSymbol *EmitFDE(MCStreamer &streamer,
519 const MCSymbol &cieStart,
Rafael Espindola4892dff2011-04-29 03:06:29 +0000520 const MCDwarfFrameInfo &frame,
521 bool forceLsda);
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 };
528}
529
530void FrameEmitterImpl::EmitCFIInstruction(MCStreamer &Streamer,
531 const MCCFIInstruction &Instr) {
532 int dataAlignmentFactor = getDataAlignmentFactor(Streamer);
533
534 switch (Instr.getOperation()) {
535 case MCCFIInstruction::Move:
536 case MCCFIInstruction::RelMove: {
537 const MachineLocation &Dst = Instr.getDestination();
538 const MachineLocation &Src = Instr.getSource();
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000539 const bool IsRelative = Instr.getOperation() == MCCFIInstruction::RelMove;
Rafael Espindola25f492e2011-04-12 16:12:03 +0000540
541 // If advancing cfa.
542 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
543 assert(!Src.isReg() && "Machine move not supported yet.");
544
545 if (Src.getReg() == MachineLocation::VirtualFP) {
546 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_offset, 1);
547 } else {
548 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa, 1);
549 Streamer.EmitULEB128IntValue(Src.getReg());
550 }
551
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000552 if (IsRelative)
553 CFAOffset += Src.getOffset();
554 else
555 CFAOffset = -Src.getOffset();
556
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000557 Streamer.EmitULEB128IntValue(CFAOffset);
Rafael Espindola25f492e2011-04-12 16:12:03 +0000558 return;
559 }
560
561 if (Src.isReg() && Src.getReg() == MachineLocation::VirtualFP) {
562 assert(Dst.isReg() && "Machine move not supported yet.");
563 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_register, 1);
564 Streamer.EmitULEB128IntValue(Dst.getReg());
565 return;
566 }
567
568 unsigned Reg = Src.getReg();
569
Rafael Espindola25f492e2011-04-12 16:12:03 +0000570 int Offset = Dst.getOffset();
571 if (IsRelative)
572 Offset -= CFAOffset;
573 Offset = Offset / dataAlignmentFactor;
574
575 if (Offset < 0) {
576 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended_sf, 1);
577 Streamer.EmitULEB128IntValue(Reg);
578 Streamer.EmitSLEB128IntValue(Offset);
579 } else if (Reg < 64) {
580 Streamer.EmitIntValue(dwarf::DW_CFA_offset + Reg, 1);
Rafael Espindolaeccbad72011-04-21 23:26:40 +0000581 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola25f492e2011-04-12 16:12:03 +0000582 } else {
583 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended, 1);
Rafael Espindolaeccbad72011-04-21 23:26:40 +0000584 Streamer.EmitULEB128IntValue(Reg);
585 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola25f492e2011-04-12 16:12:03 +0000586 }
587 return;
588 }
589 case MCCFIInstruction::Remember:
590 Streamer.EmitIntValue(dwarf::DW_CFA_remember_state, 1);
591 return;
592 case MCCFIInstruction::Restore:
593 Streamer.EmitIntValue(dwarf::DW_CFA_restore_state, 1);
594 return;
595 case MCCFIInstruction::SameValue: {
596 unsigned Reg = Instr.getDestination().getReg();
597 Streamer.EmitIntValue(dwarf::DW_CFA_same_value, 1);
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000598 Streamer.EmitULEB128IntValue(Reg);
Rafael Espindola25f492e2011-04-12 16:12:03 +0000599 return;
600 }
601 }
602 llvm_unreachable("Unhandled case in switch");
603}
604
605/// EmitFrameMoves - Emit frame instructions to describe the layout of the
606/// frame.
607void FrameEmitterImpl::EmitCFIInstructions(MCStreamer &streamer,
608 const std::vector<MCCFIInstruction> &Instrs,
609 MCSymbol *BaseLabel) {
610 for (unsigned i = 0, N = Instrs.size(); i < N; ++i) {
611 const MCCFIInstruction &Instr = Instrs[i];
612 MCSymbol *Label = Instr.getLabel();
613 // Throw out move if the label is invalid.
614 if (Label && !Label->isDefined()) continue; // Not emitted, in dead code.
615
616 // Advance row if new location.
617 if (BaseLabel && Label) {
618 MCSymbol *ThisSym = Label;
619 if (ThisSym != BaseLabel) {
620 streamer.EmitDwarfAdvanceFrameAddr(BaseLabel, ThisSym);
621 BaseLabel = ThisSym;
622 }
623 }
624
625 EmitCFIInstruction(streamer, Instr);
626 }
627}
628
629const MCSymbol &FrameEmitterImpl::EmitCIE(MCStreamer &streamer,
630 const MCSymbol *personality,
631 unsigned personalityEncoding,
632 const MCSymbol *lsda,
633 unsigned lsdaEncoding) {
Rafael Espindola89b93722010-12-10 07:39:47 +0000634 MCContext &context = streamer.getContext();
635 const TargetAsmInfo &asmInfo = context.getTargetAsmInfo();
636 const MCSection &section = *asmInfo.getEHFrameSection();
637 streamer.SwitchSection(&section);
Rafael Espindola514cecc2011-04-28 03:26:11 +0000638
639 MCSymbol *sectionStart;
640 if (asmInfo.isFunctionEHFrameSymbolPrivate())
641 sectionStart = context.CreateTempSymbol();
642 else
643 sectionStart = context.GetOrCreateSymbol(Twine("EH_frame") + Twine(CIENum));
644
645 CIENum++;
646
Rafael Espindola89b93722010-12-10 07:39:47 +0000647 MCSymbol *sectionEnd = streamer.getContext().CreateTempSymbol();
648
649 // Length
650 const MCExpr *Length = MakeStartMinusEndExpr(streamer, *sectionStart,
651 *sectionEnd, 4);
652 streamer.EmitLabel(sectionStart);
Rafael Espindola9266cc42011-04-27 01:43:49 +0000653 streamer.EmitAbsValue(Length, 4);
Rafael Espindola89b93722010-12-10 07:39:47 +0000654
655 // CIE ID
656 streamer.EmitIntValue(0, 4);
657
658 // Version
659 streamer.EmitIntValue(dwarf::DW_CIE_VERSION, 1);
660
661 // Augmentation String
662 SmallString<8> Augmentation;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +0000663 Augmentation += "z";
664 if (personality)
665 Augmentation += "P";
Rafael Espindolabdc31672010-12-27 15:56:22 +0000666 if (lsda)
667 Augmentation += "L";
Rafael Espindolad7c8cca2010-12-26 20:20:31 +0000668 Augmentation += "R";
Rafael Espindola89b93722010-12-10 07:39:47 +0000669 streamer.EmitBytes(Augmentation.str(), 0);
670 streamer.EmitIntValue(0, 1);
671
672 // Code Alignment Factor
673 streamer.EmitULEB128IntValue(1);
674
675 // Data Alignment Factor
676 streamer.EmitSLEB128IntValue(getDataAlignmentFactor(streamer));
677
678 // Return Address Register
679 streamer.EmitULEB128IntValue(asmInfo.getDwarfRARegNum(true));
680
681 // Augmentation Data Length (optional)
682 MCSymbol *augmentationStart = streamer.getContext().CreateTempSymbol();
683 MCSymbol *augmentationEnd = streamer.getContext().CreateTempSymbol();
684 const MCExpr *augmentationLength = MakeStartMinusEndExpr(streamer,
685 *augmentationStart,
686 *augmentationEnd, 0);
687 streamer.EmitULEB128Value(augmentationLength);
688
689 // Augmentation Data (optional)
690 streamer.EmitLabel(augmentationStart);
Rafael Espindolad7c8cca2010-12-26 20:20:31 +0000691 if (personality) {
692 // Personality Encoding
Rafael Espindola3a83c402010-12-27 00:36:05 +0000693 streamer.EmitIntValue(personalityEncoding, 1);
Rafael Espindolad7c8cca2010-12-26 20:20:31 +0000694 // Personality
Rafael Espindolabfa27cc2011-04-28 16:09:09 +0000695 EmitPersonality(streamer, *personality, personalityEncoding);
Rafael Espindolabdc31672010-12-27 15:56:22 +0000696 }
697 if (lsda) {
698 // LSDA Encoding
699 streamer.EmitIntValue(lsdaEncoding, 1);
Rafael Espindolad7c8cca2010-12-26 20:20:31 +0000700 }
701 // Encoding of the FDE pointers
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000702 streamer.EmitIntValue(asmInfo.getFDEEncoding(), 1);
Rafael Espindola89b93722010-12-10 07:39:47 +0000703 streamer.EmitLabel(augmentationEnd);
704
705 // Initial Instructions
706
707 const std::vector<MachineMove> Moves = asmInfo.getInitialFrameState();
Rafael Espindolafe024d02010-12-28 18:36:23 +0000708 std::vector<MCCFIInstruction> Instructions;
Rafael Espindola89b93722010-12-10 07:39:47 +0000709
Rafael Espindolafe024d02010-12-28 18:36:23 +0000710 for (int i = 0, n = Moves.size(); i != n; ++i) {
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000711 MCSymbol *Label = Moves[i].getLabel();
712 const MachineLocation &Dst =
713 TranslateMachineLocation(asmInfo, Moves[i].getDestination());
714 const MachineLocation &Src =
715 TranslateMachineLocation(asmInfo, Moves[i].getSource());
716 MCCFIInstruction Inst(Label, Dst, Src);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000717 Instructions.push_back(Inst);
718 }
719
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000720 EmitCFIInstructions(streamer, Instructions, NULL);
Rafael Espindola89b93722010-12-10 07:39:47 +0000721
722 // Padding
Rafael Espindolad7c8cca2010-12-26 20:20:31 +0000723 streamer.EmitValueToAlignment(4);
Rafael Espindola89b93722010-12-10 07:39:47 +0000724
725 streamer.EmitLabel(sectionEnd);
726 return *sectionStart;
727}
728
Rafael Espindola25f492e2011-04-12 16:12:03 +0000729MCSymbol *FrameEmitterImpl::EmitFDE(MCStreamer &streamer,
730 const MCSymbol &cieStart,
Rafael Espindola4892dff2011-04-29 03:06:29 +0000731 const MCDwarfFrameInfo &frame,
732 bool forceLsda) {
Rafael Espindolabdc31672010-12-27 15:56:22 +0000733 MCContext &context = streamer.getContext();
734 MCSymbol *fdeStart = context.CreateTempSymbol();
735 MCSymbol *fdeEnd = context.CreateTempSymbol();
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000736 const TargetAsmInfo &asmInfo = context.getTargetAsmInfo();
Rafael Espindola89b93722010-12-10 07:39:47 +0000737
Rafael Espindolaa8cfb872011-04-28 02:46:42 +0000738 if (!asmInfo.isFunctionEHFrameSymbolPrivate()) {
739 Twine EHName = frame.Function->getName() + Twine(".eh");
740 MCSymbol *EHSym = context.GetOrCreateSymbol(EHName);
Rafael Espindola8bca4102011-04-28 12:50:37 +0000741 streamer.EmitEHSymAttributes(frame.Function, EHSym);
Rafael Espindolaa8cfb872011-04-28 02:46:42 +0000742 streamer.EmitLabel(EHSym);
743 }
744
Rafael Espindola89b93722010-12-10 07:39:47 +0000745 // Length
746 const MCExpr *Length = MakeStartMinusEndExpr(streamer, *fdeStart, *fdeEnd, 0);
Rafael Espindola9266cc42011-04-27 01:43:49 +0000747 streamer.EmitAbsValue(Length, 4);
Rafael Espindola89b93722010-12-10 07:39:47 +0000748
749 streamer.EmitLabel(fdeStart);
750 // CIE Pointer
751 const MCExpr *offset = MakeStartMinusEndExpr(streamer, cieStart, *fdeStart,
752 0);
Rafael Espindola9266cc42011-04-27 01:43:49 +0000753 streamer.EmitAbsValue(offset, 4);
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000754 unsigned fdeEncoding = asmInfo.getFDEEncoding();
755 unsigned size = getSizeForEncoding(streamer, fdeEncoding);
Rafael Espindola89b93722010-12-10 07:39:47 +0000756
757 // PC Begin
Rafael Espindolaa0057ca2011-04-28 21:04:39 +0000758 EmitSymbol(streamer, *frame.Begin, fdeEncoding);
Rafael Espindola89b93722010-12-10 07:39:47 +0000759
760 // PC Range
761 const MCExpr *Range = MakeStartMinusEndExpr(streamer, *frame.Begin,
762 *frame.End, 0);
Rafael Espindola9266cc42011-04-27 01:43:49 +0000763 streamer.EmitAbsValue(Range, size);
Rafael Espindola89b93722010-12-10 07:39:47 +0000764
765 // Augmentation Data Length
Rafael Espindolabdc31672010-12-27 15:56:22 +0000766 MCSymbol *augmentationStart = streamer.getContext().CreateTempSymbol();
767 MCSymbol *augmentationEnd = streamer.getContext().CreateTempSymbol();
768 const MCExpr *augmentationLength = MakeStartMinusEndExpr(streamer,
769 *augmentationStart,
770 *augmentationEnd, 0);
771 streamer.EmitULEB128Value(augmentationLength);
Rafael Espindola89b93722010-12-10 07:39:47 +0000772
773 // Augmentation Data
Rafael Espindolabdc31672010-12-27 15:56:22 +0000774 streamer.EmitLabel(augmentationStart);
Rafael Espindola4892dff2011-04-29 03:06:29 +0000775
776 // When running in "CodeGen compatibility mode" a FDE with no LSDA can be
777 // assigned to a CIE that requires one. In that case we output a 0 (as does
778 // CodeGen).
Rafael Espindolabdc31672010-12-27 15:56:22 +0000779 if (frame.Lsda)
780 EmitSymbol(streamer, *frame.Lsda, frame.LsdaEncoding);
Rafael Espindola4892dff2011-04-29 03:06:29 +0000781 else if (forceLsda)
782 streamer.EmitIntValue(0, size);
783
Rafael Espindolabdc31672010-12-27 15:56:22 +0000784 streamer.EmitLabel(augmentationEnd);
Rafael Espindola89b93722010-12-10 07:39:47 +0000785 // Call Frame Instructions
786
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000787 EmitCFIInstructions(streamer, frame.Instructions, frame.Begin);
Rafael Espindola5bba0842010-12-28 04:15:37 +0000788
Rafael Espindola89b93722010-12-10 07:39:47 +0000789 // Padding
Rafael Espindola9266cc42011-04-27 01:43:49 +0000790 streamer.EmitValueToAlignment(size);
Rafael Espindoladfe125c2010-12-17 00:28:02 +0000791
792 return fdeEnd;
Rafael Espindola89b93722010-12-10 07:39:47 +0000793}
794
Benjamin Kramer19282362010-12-30 22:34:44 +0000795namespace {
796 struct CIEKey {
797 static const CIEKey getEmptyKey() { return CIEKey(0, 0, -1); }
798 static const CIEKey getTombstoneKey() { return CIEKey(0, -1, 0); }
Rafael Espindolabdc31672010-12-27 15:56:22 +0000799
Benjamin Kramer19282362010-12-30 22:34:44 +0000800 CIEKey(const MCSymbol* Personality_, unsigned PersonalityEncoding_,
801 unsigned LsdaEncoding_) : Personality(Personality_),
802 PersonalityEncoding(PersonalityEncoding_),
803 LsdaEncoding(LsdaEncoding_) {
804 }
805 const MCSymbol* Personality;
806 unsigned PersonalityEncoding;
807 unsigned LsdaEncoding;
808 };
809}
Rafael Espindolabdc31672010-12-27 15:56:22 +0000810
811namespace llvm {
812 template <>
813 struct DenseMapInfo<CIEKey> {
814 static CIEKey getEmptyKey() {
Benjamin Kramer19282362010-12-30 22:34:44 +0000815 return CIEKey::getEmptyKey();
Rafael Espindolabdc31672010-12-27 15:56:22 +0000816 }
817 static CIEKey getTombstoneKey() {
Benjamin Kramer19282362010-12-30 22:34:44 +0000818 return CIEKey::getTombstoneKey();
Rafael Espindolabdc31672010-12-27 15:56:22 +0000819 }
820 static unsigned getHashValue(const CIEKey &Key) {
821 FoldingSetNodeID ID;
822 ID.AddPointer(Key.Personality);
823 ID.AddInteger(Key.PersonalityEncoding);
824 ID.AddInteger(Key.LsdaEncoding);
825 return ID.ComputeHash();
826 }
827 static bool isEqual(const CIEKey &LHS,
828 const CIEKey &RHS) {
829 return LHS.Personality == RHS.Personality &&
830 LHS.PersonalityEncoding == RHS.PersonalityEncoding &&
831 LHS.LsdaEncoding == RHS.LsdaEncoding;
832 }
833 };
834}
835
Rafael Espindola90998132011-04-29 02:42:28 +0000836// This is an implementation of CIE and FDE emission that is bug by bug
837// compatible with the one in CodeGen. It is useful during the transition
838// to make it easy to compare the outputs, but should probably be removed
839// afterwards.
840void MCDwarfFrameEmitter::EmitDarwin(MCStreamer &streamer) {
841 FrameEmitterImpl Emitter;
842 DenseMap<const MCSymbol*, const MCSymbol*> Personalities;
843 const MCSymbol *aCIE = NULL;
844 const MCDwarfFrameInfo *aFrame = NULL;
845
846 for (unsigned i = 0, n = streamer.getNumFrameInfos(); i < n; ++i) {
847 const MCDwarfFrameInfo &frame = streamer.getFrameInfo(i);
848 if (!frame.Personality)
849 continue;
850 if (Personalities.count(frame.Personality))
851 continue;
852
853 const MCSymbol *cieStart = &Emitter.EmitCIE(streamer, frame.Personality,
854 frame.PersonalityEncoding,
855 frame.Lsda,
856 frame.LsdaEncoding);
857 aCIE = cieStart;
858 aFrame = &frame;
859 Personalities[frame.Personality] = cieStart;
860 }
861
862 if (Personalities.empty()) {
863 const MCDwarfFrameInfo &frame = streamer.getFrameInfo(0);
864 aCIE = &Emitter.EmitCIE(streamer, frame.Personality,
865 frame.PersonalityEncoding, frame.Lsda,
866 frame.LsdaEncoding);
867 aFrame = &frame;
868 }
869
870 MCSymbol *fdeEnd = NULL;
871 for (unsigned i = 0, n = streamer.getNumFrameInfos(); i < n; ++i) {
872 const MCDwarfFrameInfo &frame = streamer.getFrameInfo(i);
873 const MCSymbol *cieStart = Personalities[frame.Personality];
Rafael Espindola4892dff2011-04-29 03:06:29 +0000874 bool hasLSDA;
875 if (!cieStart) {
Rafael Espindola90998132011-04-29 02:42:28 +0000876 cieStart = aCIE;
Rafael Espindola4892dff2011-04-29 03:06:29 +0000877 hasLSDA = aFrame->Lsda;
878 } else {
879 hasLSDA = true;
880 }
Rafael Espindola90998132011-04-29 02:42:28 +0000881
Rafael Espindola4892dff2011-04-29 03:06:29 +0000882 fdeEnd = Emitter.EmitFDE(streamer, *cieStart, frame,
883 hasLSDA);
Rafael Espindola90998132011-04-29 02:42:28 +0000884 if (i != n - 1)
885 streamer.EmitLabel(fdeEnd);
886 }
887
888 const MCContext &context = streamer.getContext();
889 const TargetAsmInfo &asmInfo = context.getTargetAsmInfo();
890 streamer.EmitValueToAlignment(asmInfo.getPointerSize());
891 if (fdeEnd)
892 streamer.EmitLabel(fdeEnd);
893}
894
Rafael Espindola89b93722010-12-10 07:39:47 +0000895void MCDwarfFrameEmitter::Emit(MCStreamer &streamer) {
Rafael Espindoladfe125c2010-12-17 00:28:02 +0000896 const MCContext &context = streamer.getContext();
897 const TargetAsmInfo &asmInfo = context.getTargetAsmInfo();
Rafael Espindola90998132011-04-29 02:42:28 +0000898 if (!asmInfo.isFunctionEHFrameSymbolPrivate()) {
899 EmitDarwin(streamer);
900 return;
901 }
902
Rafael Espindoladfe125c2010-12-17 00:28:02 +0000903 MCSymbol *fdeEnd = NULL;
Rafael Espindolabdc31672010-12-27 15:56:22 +0000904 DenseMap<CIEKey, const MCSymbol*> CIEStarts;
Rafael Espindola25f492e2011-04-12 16:12:03 +0000905 FrameEmitterImpl Emitter;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +0000906
Rafael Espindoladfe125c2010-12-17 00:28:02 +0000907 for (unsigned i = 0, n = streamer.getNumFrameInfos(); i < n; ++i) {
Rafael Espindolad7c8cca2010-12-26 20:20:31 +0000908 const MCDwarfFrameInfo &frame = streamer.getFrameInfo(i);
Rafael Espindolabdc31672010-12-27 15:56:22 +0000909 CIEKey key(frame.Personality, frame.PersonalityEncoding,
910 frame.LsdaEncoding);
911 const MCSymbol *&cieStart = CIEStarts[key];
Rafael Espindolad7c8cca2010-12-26 20:20:31 +0000912 if (!cieStart)
Rafael Espindola25f492e2011-04-12 16:12:03 +0000913 cieStart = &Emitter.EmitCIE(streamer, frame.Personality,
914 frame.PersonalityEncoding, frame.Lsda,
915 frame.LsdaEncoding);
Rafael Espindola4892dff2011-04-29 03:06:29 +0000916 fdeEnd = Emitter.EmitFDE(streamer, *cieStart, frame, false);
Rafael Espindoladfe125c2010-12-17 00:28:02 +0000917 if (i != n - 1)
918 streamer.EmitLabel(fdeEnd);
919 }
Rafael Espindolad7c8cca2010-12-26 20:20:31 +0000920
Rafael Espindoladfe125c2010-12-17 00:28:02 +0000921 streamer.EmitValueToAlignment(asmInfo.getPointerSize());
922 if (fdeEnd)
923 streamer.EmitLabel(fdeEnd);
Rafael Espindola89b93722010-12-10 07:39:47 +0000924}
Rafael Espindola245a1e22010-12-28 05:39:27 +0000925
926void MCDwarfFrameEmitter::EmitAdvanceLoc(MCStreamer &Streamer,
927 uint64_t AddrDelta) {
928 SmallString<256> Tmp;
929 raw_svector_ostream OS(Tmp);
Rafael Espindola235b8302011-04-28 04:04:14 +0000930 const TargetAsmInfo &AsmInfo = Streamer.getContext().getTargetAsmInfo();
931 MCDwarfFrameEmitter::EncodeAdvanceLoc(AddrDelta, OS, AsmInfo);
Rafael Espindola245a1e22010-12-28 05:39:27 +0000932 Streamer.EmitBytes(OS.str(), /*AddrSpace=*/0);
933}
934
935void MCDwarfFrameEmitter::EncodeAdvanceLoc(uint64_t AddrDelta,
Rafael Espindola235b8302011-04-28 04:04:14 +0000936 raw_ostream &OS,
937 const TargetAsmInfo &AsmInfo) {
938 // This is a small hack to facilitate the transition to CFI on OS X. It
939 // relaxes all address advances which lets us produces identical output
940 // to the one produce by CodeGen.
941 const bool Relax = !AsmInfo.isFunctionEHFrameSymbolPrivate();
942
Rafael Espindola245a1e22010-12-28 05:39:27 +0000943 // FIXME: Assumes the code alignment factor is 1.
Rafael Espindola3b78cdc2010-12-28 23:38:03 +0000944 if (AddrDelta == 0) {
Rafael Espindola235b8302011-04-28 04:04:14 +0000945 } else if (isUIntN(6, AddrDelta) && !Relax) {
Rafael Espindola245a1e22010-12-28 05:39:27 +0000946 uint8_t Opcode = dwarf::DW_CFA_advance_loc | AddrDelta;
947 OS << Opcode;
Rafael Espindola235b8302011-04-28 04:04:14 +0000948 } else if (isUInt<8>(AddrDelta) && !Relax) {
Rafael Espindola245a1e22010-12-28 05:39:27 +0000949 OS << uint8_t(dwarf::DW_CFA_advance_loc1);
950 OS << uint8_t(AddrDelta);
Rafael Espindola235b8302011-04-28 04:04:14 +0000951 } else if (isUInt<16>(AddrDelta) && !Relax) {
Rafael Espindolaa7e45052010-12-29 02:30:49 +0000952 // FIXME: check what is the correct behavior on a big endian machine.
Rafael Espindola245a1e22010-12-28 05:39:27 +0000953 OS << uint8_t(dwarf::DW_CFA_advance_loc2);
Rafael Espindolaa7e45052010-12-29 02:30:49 +0000954 OS << uint8_t( AddrDelta & 0xff);
955 OS << uint8_t((AddrDelta >> 8) & 0xff);
Rafael Espindola245a1e22010-12-28 05:39:27 +0000956 } else {
Rafael Espindolaa7e45052010-12-29 02:30:49 +0000957 // FIXME: check what is the correct behavior on a big endian machine.
Rafael Espindola245a1e22010-12-28 05:39:27 +0000958 assert(isUInt<32>(AddrDelta));
959 OS << uint8_t(dwarf::DW_CFA_advance_loc4);
Rafael Espindolaa7e45052010-12-29 02:30:49 +0000960 OS << uint8_t( AddrDelta & 0xff);
961 OS << uint8_t((AddrDelta >> 8) & 0xff);
962 OS << uint8_t((AddrDelta >> 16) & 0xff);
963 OS << uint8_t((AddrDelta >> 24) & 0xff);
964
Rafael Espindola245a1e22010-12-28 05:39:27 +0000965 }
966}