blob: 9d26bec824e681e74c6b46b7b326fcbd3165f617 [file] [log] [blame]
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00001//===- lib/MC/MCDwarf.cpp - MCDwarf implementation ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Rafael Espindola767b1be2010-12-04 00:31:13 +000010#include "llvm/MC/MCAsmInfo.h"
Kevin Enderby7cbf73a2010-07-28 20:55:35 +000011#include "llvm/MC/MCDwarf.h"
Kevin Enderbyc0957932010-09-30 16:52:03 +000012#include "llvm/MC/MCAssembler.h"
Rafael Espindolaad8aaa02010-11-22 11:53:17 +000013#include "llvm/MC/MCStreamer.h"
Kevin Enderbyc0957932010-09-30 16:52:03 +000014#include "llvm/MC/MCSymbol.h"
15#include "llvm/MC/MCExpr.h"
16#include "llvm/MC/MCContext.h"
17#include "llvm/MC/MCObjectWriter.h"
18#include "llvm/ADT/SmallString.h"
Kevin Enderby7cbf73a2010-07-28 20:55:35 +000019#include "llvm/Support/Debug.h"
20#include "llvm/Support/raw_ostream.h"
Kevin Enderbyc0957932010-09-30 16:52:03 +000021#include "llvm/Target/TargetAsmBackend.h"
Rafael Espindola89b93722010-12-10 07:39:47 +000022#include "llvm/Target/TargetAsmInfo.h"
Kevin Enderby7cbf73a2010-07-28 20:55:35 +000023using namespace llvm;
24
Kevin Enderbyc0957932010-09-30 16:52:03 +000025// Given a special op, return the address skip amount (in units of
26// DWARF2_LINE_MIN_INSN_LENGTH.
27#define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE)
28
29// The maximum address skip amount that can be encoded with a special op.
30#define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255)
31
32// First special line opcode - leave room for the standard opcodes.
33// Note: If you want to change this, you'll have to update the
34// "standard_opcode_lengths" table that is emitted in DwarfFileTable::Emit().
35#define DWARF2_LINE_OPCODE_BASE 13
36
37// Minimum line offset in a special line info. opcode. This value
38// was chosen to give a reasonable range of values.
39#define DWARF2_LINE_BASE -5
40
41// Range of line offsets in a special line info. opcode.
42# define DWARF2_LINE_RANGE 14
43
44// Define the architecture-dependent minimum instruction length (in bytes).
45// This value should be rather too small than too big.
46# define DWARF2_LINE_MIN_INSN_LENGTH 1
47
48// Note: when DWARF2_LINE_MIN_INSN_LENGTH == 1 which is the current setting,
49// this routine is a nop and will be optimized away.
50static inline uint64_t ScaleAddrDelta(uint64_t AddrDelta)
51{
52 if (DWARF2_LINE_MIN_INSN_LENGTH == 1)
53 return AddrDelta;
54 if (AddrDelta % DWARF2_LINE_MIN_INSN_LENGTH != 0) {
55 // TODO: report this error, but really only once.
56 ;
57 }
58 return AddrDelta / DWARF2_LINE_MIN_INSN_LENGTH;
59}
60
61//
62// This is called when an instruction is assembled into the specified section
63// and if there is information from the last .loc directive that has yet to have
64// a line entry made for it is made.
65//
Rafael Espindola195a0ce2010-11-19 02:26:16 +000066void MCLineEntry::Make(MCStreamer *MCOS, const MCSection *Section) {
Kevin Enderbyc0957932010-09-30 16:52:03 +000067 if (!MCOS->getContext().getDwarfLocSeen())
68 return;
69
70 // Create a symbol at in the current section for use in the line entry.
71 MCSymbol *LineSym = MCOS->getContext().CreateTempSymbol();
72 // Set the value of the symbol to use for the MCLineEntry.
73 MCOS->EmitLabel(LineSym);
74
75 // Get the current .loc info saved in the context.
76 const MCDwarfLoc &DwarfLoc = MCOS->getContext().getCurrentDwarfLoc();
77
78 // Create a (local) line entry with the symbol and the current .loc info.
79 MCLineEntry LineEntry(LineSym, DwarfLoc);
80
81 // clear DwarfLocSeen saying the current .loc info is now used.
Kevin Enderby3f55c242010-10-04 20:17:24 +000082 MCOS->getContext().ClearDwarfLocSeen();
Kevin Enderbyc0957932010-09-30 16:52:03 +000083
84 // Get the MCLineSection for this section, if one does not exist for this
85 // section create it.
Rafael Espindola17fd7bd2010-11-19 07:41:23 +000086 const DenseMap<const MCSection *, MCLineSection *> &MCLineSections =
Kevin Enderbyc0957932010-09-30 16:52:03 +000087 MCOS->getContext().getMCLineSections();
Rafael Espindola17fd7bd2010-11-19 07:41:23 +000088 MCLineSection *LineSection = MCLineSections.lookup(Section);
Kevin Enderbyc0957932010-09-30 16:52:03 +000089 if (!LineSection) {
90 // Create a new MCLineSection. This will be deleted after the dwarf line
91 // table is created using it by iterating through the MCLineSections
92 // DenseMap.
93 LineSection = new MCLineSection;
94 // Save a pointer to the new LineSection into the MCLineSections DenseMap.
Rafael Espindola17fd7bd2010-11-19 07:41:23 +000095 MCOS->getContext().addMCLineSection(Section, LineSection);
Kevin Enderbyc0957932010-09-30 16:52:03 +000096 }
97
98 // Add the line entry to this section's entries.
99 LineSection->addLineEntry(LineEntry);
100}
101
102//
103// This helper routine returns an expression of End - Start + IntVal .
104//
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000105static inline const MCExpr *MakeStartMinusEndExpr(const MCStreamer &MCOS,
106 const MCSymbol &Start,
107 const MCSymbol &End,
108 int IntVal) {
Kevin Enderbyc0957932010-09-30 16:52:03 +0000109 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
110 const MCExpr *Res =
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000111 MCSymbolRefExpr::Create(&End, Variant, MCOS.getContext());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000112 const MCExpr *RHS =
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000113 MCSymbolRefExpr::Create(&Start, Variant, MCOS.getContext());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000114 const MCExpr *Res1 =
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000115 MCBinaryExpr::Create(MCBinaryExpr::Sub, Res, RHS, MCOS.getContext());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000116 const MCExpr *Res2 =
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000117 MCConstantExpr::Create(IntVal, MCOS.getContext());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000118 const MCExpr *Res3 =
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000119 MCBinaryExpr::Create(MCBinaryExpr::Sub, Res1, Res2, MCOS.getContext());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000120 return Res3;
121}
122
Kevin Enderbyc0957932010-09-30 16:52:03 +0000123//
124// This emits the Dwarf line table for the specified section from the entries
125// in the LineSection.
126//
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000127static inline void EmitDwarfLineTable(MCStreamer *MCOS,
Kevin Enderbyc0957932010-09-30 16:52:03 +0000128 const MCSection *Section,
Rafael Espindola89b93722010-12-10 07:39:47 +0000129 const MCLineSection *LineSection) {
Kevin Enderbyc0957932010-09-30 16:52:03 +0000130 unsigned FileNum = 1;
131 unsigned LastLine = 1;
132 unsigned Column = 0;
133 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
134 unsigned Isa = 0;
Kevin Enderbyc0957932010-09-30 16:52:03 +0000135 MCSymbol *LastLabel = NULL;
Kevin Enderbyc0957932010-09-30 16:52:03 +0000136
137 // Loop through each MCLineEntry and encode the dwarf line number table.
Rafael Espindola17fd7bd2010-11-19 07:41:23 +0000138 for (MCLineSection::const_iterator
Kevin Enderbyc0957932010-09-30 16:52:03 +0000139 it = LineSection->getMCLineEntries()->begin(),
140 ie = LineSection->getMCLineEntries()->end(); it != ie; ++it) {
141
142 if (FileNum != it->getFileNum()) {
143 FileNum = it->getFileNum();
144 MCOS->EmitIntValue(dwarf::DW_LNS_set_file, 1);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000145 MCOS->EmitULEB128IntValue(FileNum);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000146 }
147 if (Column != it->getColumn()) {
148 Column = it->getColumn();
149 MCOS->EmitIntValue(dwarf::DW_LNS_set_column, 1);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000150 MCOS->EmitULEB128IntValue(Column);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000151 }
152 if (Isa != it->getIsa()) {
153 Isa = it->getIsa();
154 MCOS->EmitIntValue(dwarf::DW_LNS_set_isa, 1);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000155 MCOS->EmitULEB128IntValue(Isa);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000156 }
157 if ((it->getFlags() ^ Flags) & DWARF2_FLAG_IS_STMT) {
158 Flags = it->getFlags();
159 MCOS->EmitIntValue(dwarf::DW_LNS_negate_stmt, 1);
160 }
161 if (it->getFlags() & DWARF2_FLAG_BASIC_BLOCK)
162 MCOS->EmitIntValue(dwarf::DW_LNS_set_basic_block, 1);
163 if (it->getFlags() & DWARF2_FLAG_PROLOGUE_END)
164 MCOS->EmitIntValue(dwarf::DW_LNS_set_prologue_end, 1);
165 if (it->getFlags() & DWARF2_FLAG_EPILOGUE_BEGIN)
166 MCOS->EmitIntValue(dwarf::DW_LNS_set_epilogue_begin, 1);
167
Rafael Espindola64185cc2010-11-13 01:06:27 +0000168 int64_t LineDelta = static_cast<int64_t>(it->getLine()) - LastLine;
Kevin Enderbyc0957932010-09-30 16:52:03 +0000169 MCSymbol *Label = it->getLabel();
170
171 // At this point we want to emit/create the sequence to encode the delta in
172 // line numbers and the increment of the address from the previous Label
173 // and the current Label.
Rafael Espindola32a006e2010-12-03 00:55:40 +0000174 MCOS->EmitDwarfAdvanceLineAddr(LineDelta, LastLabel, Label);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000175
176 LastLine = it->getLine();
177 LastLabel = Label;
Kevin Enderbyc0957932010-09-30 16:52:03 +0000178 }
179
180 // Emit a DW_LNE_end_sequence for the end of the section.
181 // Using the pointer Section create a temporary label at the end of the
182 // section and use that and the LastLabel to compute the address delta
183 // and use INT64_MAX as the line delta which is the signal that this is
184 // actually a DW_LNE_end_sequence.
185
186 // Switch to the section to be able to create a symbol at its end.
187 MCOS->SwitchSection(Section);
Rafael Espindola89b93722010-12-10 07:39:47 +0000188
189 MCContext &context = MCOS->getContext();
Kevin Enderbyc0957932010-09-30 16:52:03 +0000190 // Create a symbol at the end of the section.
Rafael Espindola89b93722010-12-10 07:39:47 +0000191 MCSymbol *SectionEnd = context.CreateTempSymbol();
Kevin Enderbyc0957932010-09-30 16:52:03 +0000192 // Set the value of the symbol, as we are at the end of the section.
193 MCOS->EmitLabel(SectionEnd);
194
195 // Switch back the the dwarf line section.
Rafael Espindola89b93722010-12-10 07:39:47 +0000196 MCOS->SwitchSection(context.getTargetAsmInfo().getDwarfLineSection());
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000197
Rafael Espindola32a006e2010-12-03 00:55:40 +0000198 MCOS->EmitDwarfAdvanceLineAddr(INT64_MAX, LastLabel, SectionEnd);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000199}
200
201//
202// This emits the Dwarf file and the line tables.
203//
Rafael Espindola89b93722010-12-10 07:39:47 +0000204void MCDwarfFileTable::Emit(MCStreamer *MCOS) {
205 MCContext &context = MCOS->getContext();
Kevin Enderbyc0957932010-09-30 16:52:03 +0000206 // Switch to the section where the table will be emitted into.
Rafael Espindola89b93722010-12-10 07:39:47 +0000207 MCOS->SwitchSection(context.getTargetAsmInfo().getDwarfLineSection());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000208
209 // Create a symbol at the beginning of this section.
Rafael Espindola89b93722010-12-10 07:39:47 +0000210 MCSymbol *LineStartSym = context.CreateTempSymbol();
Kevin Enderbyc0957932010-09-30 16:52:03 +0000211 // Set the value of the symbol, as we are at the start of the section.
212 MCOS->EmitLabel(LineStartSym);
213
214 // Create a symbol for the end of the section (to be set when we get there).
Rafael Espindola89b93722010-12-10 07:39:47 +0000215 MCSymbol *LineEndSym = context.CreateTempSymbol();
Kevin Enderbyc0957932010-09-30 16:52:03 +0000216
217 // The first 4 bytes is the total length of the information for this
218 // compilation unit (not including these 4 bytes for the length).
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000219 MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *LineEndSym,4),
Rafael Espindola0bbe0b42010-12-06 17:27:56 +0000220 4);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000221
222 // Next 2 bytes is the Version, which is Dwarf 2.
223 MCOS->EmitIntValue(2, 2);
224
225 // Create a symbol for the end of the prologue (to be set when we get there).
Rafael Espindola89b93722010-12-10 07:39:47 +0000226 MCSymbol *ProEndSym = context.CreateTempSymbol(); // Lprologue_end
Kevin Enderbyc0957932010-09-30 16:52:03 +0000227
228 // Length of the prologue, is the next 4 bytes. Which is the start of the
229 // section to the end of the prologue. Not including the 4 bytes for the
230 // total length, the 2 bytes for the version, and these 4 bytes for the
231 // length of the prologue.
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000232 MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *ProEndSym,
Kevin Enderbyc0957932010-09-30 16:52:03 +0000233 (4 + 2 + 4)),
Rafael Espindola5d4918d2010-12-04 03:21:47 +0000234 4, 0);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000235
236 // Parameters of the state machine, are next.
237 MCOS->EmitIntValue(DWARF2_LINE_MIN_INSN_LENGTH, 1);
238 MCOS->EmitIntValue(DWARF2_LINE_DEFAULT_IS_STMT, 1);
239 MCOS->EmitIntValue(DWARF2_LINE_BASE, 1);
240 MCOS->EmitIntValue(DWARF2_LINE_RANGE, 1);
241 MCOS->EmitIntValue(DWARF2_LINE_OPCODE_BASE, 1);
242
243 // Standard opcode lengths
244 MCOS->EmitIntValue(0, 1); // length of DW_LNS_copy
245 MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_pc
246 MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_line
247 MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_file
248 MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_column
249 MCOS->EmitIntValue(0, 1); // length of DW_LNS_negate_stmt
250 MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_basic_block
251 MCOS->EmitIntValue(0, 1); // length of DW_LNS_const_add_pc
252 MCOS->EmitIntValue(1, 1); // length of DW_LNS_fixed_advance_pc
253 MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_prologue_end
254 MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_epilogue_begin
255 MCOS->EmitIntValue(1, 1); // DW_LNS_set_isa
256
257 // Put out the directory and file tables.
258
259 // First the directory table.
260 const std::vector<StringRef> &MCDwarfDirs =
Rafael Espindola89b93722010-12-10 07:39:47 +0000261 context.getMCDwarfDirs();
Kevin Enderbyc0957932010-09-30 16:52:03 +0000262 for (unsigned i = 0; i < MCDwarfDirs.size(); i++) {
263 MCOS->EmitBytes(MCDwarfDirs[i], 0); // the DirectoryName
264 MCOS->EmitBytes(StringRef("\0", 1), 0); // the null term. of the string
265 }
266 MCOS->EmitIntValue(0, 1); // Terminate the directory list
267
268 // Second the file table.
269 const std::vector<MCDwarfFile *> &MCDwarfFiles =
270 MCOS->getContext().getMCDwarfFiles();
271 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
272 MCOS->EmitBytes(MCDwarfFiles[i]->getName(), 0); // FileName
273 MCOS->EmitBytes(StringRef("\0", 1), 0); // the null term. of the string
Rafael Espindola3ff57092010-11-02 17:22:24 +0000274 // the Directory num
275 MCOS->EmitULEB128IntValue(MCDwarfFiles[i]->getDirIndex());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000276 MCOS->EmitIntValue(0, 1); // last modification timestamp (always 0)
277 MCOS->EmitIntValue(0, 1); // filesize (always 0)
278 }
279 MCOS->EmitIntValue(0, 1); // Terminate the file list
280
281 // This is the end of the prologue, so set the value of the symbol at the
282 // end of the prologue (that was used in a previous expression).
283 MCOS->EmitLabel(ProEndSym);
284
285 // Put out the line tables.
Rafael Espindola17fd7bd2010-11-19 07:41:23 +0000286 const DenseMap<const MCSection *, MCLineSection *> &MCLineSections =
Kevin Enderbyc0957932010-09-30 16:52:03 +0000287 MCOS->getContext().getMCLineSections();
Rafael Espindola17fd7bd2010-11-19 07:41:23 +0000288 const std::vector<const MCSection *> &MCLineSectionOrder =
289 MCOS->getContext().getMCLineSectionOrder();
290 for (std::vector<const MCSection*>::const_iterator it =
291 MCLineSectionOrder.begin(), ie = MCLineSectionOrder.end(); it != ie;
292 ++it) {
293 const MCSection *Sec = *it;
294 const MCLineSection *Line = MCLineSections.lookup(Sec);
Rafael Espindola89b93722010-12-10 07:39:47 +0000295 EmitDwarfLineTable(MCOS, Sec, Line);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000296
297 // Now delete the MCLineSections that were created in MCLineEntry::Make()
298 // and used to emit the line table.
Rafael Espindola17fd7bd2010-11-19 07:41:23 +0000299 delete Line;
Kevin Enderbyc0957932010-09-30 16:52:03 +0000300 }
301
Rafael Espindola767b1be2010-12-04 00:31:13 +0000302 if (MCOS->getContext().getAsmInfo().getLinkerRequiresNonEmptyDwarfLines()
303 && MCLineSectionOrder.begin() == MCLineSectionOrder.end()) {
Rafael Espindolaa8de83c2010-12-03 23:36:59 +0000304 // The darwin9 linker has a bug (see PR8715). For for 32-bit architectures
305 // it requires:
306 // total_length >= prologue_length + 10
307 // We are 4 bytes short, since we have total_length = 51 and
308 // prologue_length = 45
Devang Patel5113cdb2010-12-03 00:10:48 +0000309
Rafael Espindolaa8de83c2010-12-03 23:36:59 +0000310 // The regular end_sequence should be sufficient.
311 MCDwarfLineAddr::Emit(MCOS, INT64_MAX, 0);
Devang Patel5113cdb2010-12-03 00:10:48 +0000312 }
313
Kevin Enderbyc0957932010-09-30 16:52:03 +0000314 // This is the end of the section, so set the value of the symbol at the end
315 // of this section (that was used in a previous expression).
316 MCOS->EmitLabel(LineEndSym);
317}
318
Kevin Enderbyc0957932010-09-30 16:52:03 +0000319/// Utility function to write the encoding to an object writer.
320void MCDwarfLineAddr::Write(MCObjectWriter *OW, int64_t LineDelta,
321 uint64_t AddrDelta) {
322 SmallString<256> Tmp;
323 raw_svector_ostream OS(Tmp);
324 MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OS);
325 OW->WriteBytes(OS.str());
326}
327
328/// Utility function to emit the encoding to a streamer.
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000329void MCDwarfLineAddr::Emit(MCStreamer *MCOS, int64_t LineDelta,
Kevin Enderbyc0957932010-09-30 16:52:03 +0000330 uint64_t AddrDelta) {
331 SmallString<256> Tmp;
332 raw_svector_ostream OS(Tmp);
333 MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OS);
334 MCOS->EmitBytes(OS.str(), /*AddrSpace=*/0);
335}
336
337/// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
338void MCDwarfLineAddr::Encode(int64_t LineDelta, uint64_t AddrDelta,
339 raw_ostream &OS) {
340 uint64_t Temp, Opcode;
341 bool NeedCopy = false;
342
343 // Scale the address delta by the minimum instruction length.
344 AddrDelta = ScaleAddrDelta(AddrDelta);
345
346 // A LineDelta of INT64_MAX is a signal that this is actually a
347 // DW_LNE_end_sequence. We cannot use special opcodes here, since we want the
348 // end_sequence to emit the matrix entry.
349 if (LineDelta == INT64_MAX) {
350 if (AddrDelta == MAX_SPECIAL_ADDR_DELTA)
351 OS << char(dwarf::DW_LNS_const_add_pc);
352 else {
353 OS << char(dwarf::DW_LNS_advance_pc);
354 SmallString<32> Tmp;
355 raw_svector_ostream OSE(Tmp);
356 MCObjectWriter::EncodeULEB128(AddrDelta, OSE);
357 OS << OSE.str();
358 }
359 OS << char(dwarf::DW_LNS_extended_op);
360 OS << char(1);
361 OS << char(dwarf::DW_LNE_end_sequence);
362 return;
363 }
364
365 // Bias the line delta by the base.
366 Temp = LineDelta - DWARF2_LINE_BASE;
367
368 // If the line increment is out of range of a special opcode, we must encode
369 // it with DW_LNS_advance_line.
370 if (Temp >= DWARF2_LINE_RANGE) {
371 OS << char(dwarf::DW_LNS_advance_line);
372 SmallString<32> Tmp;
373 raw_svector_ostream OSE(Tmp);
374 MCObjectWriter::EncodeSLEB128(LineDelta, OSE);
375 OS << OSE.str();
376
377 LineDelta = 0;
378 Temp = 0 - DWARF2_LINE_BASE;
379 NeedCopy = true;
380 }
381
382 // Use DW_LNS_copy instead of a "line +0, addr +0" special opcode.
383 if (LineDelta == 0 && AddrDelta == 0) {
384 OS << char(dwarf::DW_LNS_copy);
385 return;
386 }
387
388 // Bias the opcode by the special opcode base.
389 Temp += DWARF2_LINE_OPCODE_BASE;
390
391 // Avoid overflow when addr_delta is large.
392 if (AddrDelta < 256 + MAX_SPECIAL_ADDR_DELTA) {
393 // Try using a special opcode.
394 Opcode = Temp + AddrDelta * DWARF2_LINE_RANGE;
395 if (Opcode <= 255) {
396 OS << char(Opcode);
397 return;
398 }
399
400 // Try using DW_LNS_const_add_pc followed by special op.
401 Opcode = Temp + (AddrDelta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
402 if (Opcode <= 255) {
403 OS << char(dwarf::DW_LNS_const_add_pc);
404 OS << char(Opcode);
405 return;
406 }
407 }
408
409 // Otherwise use DW_LNS_advance_pc.
410 OS << char(dwarf::DW_LNS_advance_pc);
411 SmallString<32> Tmp;
412 raw_svector_ostream OSE(Tmp);
413 MCObjectWriter::EncodeULEB128(AddrDelta, OSE);
414 OS << OSE.str();
415
416 if (NeedCopy)
417 OS << char(dwarf::DW_LNS_copy);
418 else
419 OS << char(Temp);
420}
421
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000422void MCDwarfFile::print(raw_ostream &OS) const {
423 OS << '"' << getName() << '"';
424}
425
426void MCDwarfFile::dump() const {
427 print(dbgs());
428}
Kevin Enderbyc0957932010-09-30 16:52:03 +0000429
Rafael Espindola89b93722010-12-10 07:39:47 +0000430static int getDataAlignmentFactor(MCStreamer &streamer) {
431 MCContext &context = streamer.getContext();
432 const TargetAsmInfo &asmInfo = context.getTargetAsmInfo();
433 int size = asmInfo.getPointerSize();
434 if (asmInfo.getStackGrowthDirection() == TargetFrameInfo::StackGrowsUp)
435 return size;
436 else
437 return -size;
438}
439
440/// EmitFrameMoves - Emit frame instructions to describe the layout of the
441/// frame.
442static void EmitFrameMoves(MCStreamer &streamer,
443 const std::vector<MachineMove> &Moves,
444 MCSymbol *BaseLabel, bool isEH) {
445 MCContext &context = streamer.getContext();
446 const TargetAsmInfo &asmInfo = context.getTargetAsmInfo();
447 int dataAlignmentFactor = getDataAlignmentFactor(streamer);
448
449 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
450 const MachineMove &Move = Moves[i];
451 MCSymbol *Label = Move.getLabel();
452 // Throw out move if the label is invalid.
453 if (Label && !Label->isDefined()) continue; // Not emitted, in dead code.
454
455 const MachineLocation &Dst = Move.getDestination();
456 const MachineLocation &Src = Move.getSource();
457
458 // Advance row if new location.
459 if (BaseLabel && Label) {
460 MCSymbol *ThisSym = Label;
461 if (ThisSym != BaseLabel) {
462 streamer.EmitIntValue(dwarf::DW_CFA_advance_loc4, 1);
463 const MCExpr *Length = MakeStartMinusEndExpr(streamer, *BaseLabel,
464 *ThisSym, 4);
465 streamer.EmitValue(Length, 4);
466 BaseLabel = ThisSym;
467 }
468 }
469
470 // If advancing cfa.
471 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
472 assert(!Src.isReg() && "Machine move not supported yet.");
473
474 if (Src.getReg() == MachineLocation::VirtualFP) {
475 streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_offset, 1);
476 } else {
477 streamer.EmitIntValue(dwarf::DW_CFA_def_cfa, 1);
Rafael Espindolad7c8cca2010-12-26 20:20:31 +0000478 streamer.EmitULEB128IntValue(asmInfo.getDwarfRegNum(Src.getReg(),
479 isEH));
Rafael Espindola89b93722010-12-10 07:39:47 +0000480 }
481
482 streamer.EmitULEB128IntValue(-Src.getOffset(), 1);
483 continue;
484 }
485
486 if (Src.isReg() && Src.getReg() == MachineLocation::VirtualFP) {
487 assert(Dst.isReg() && "Machine move not supported yet.");
488 streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_register, 1);
489 streamer.EmitULEB128IntValue(asmInfo.getDwarfRegNum(Dst.getReg(), isEH));
490 continue;
491 }
492
493 unsigned Reg = asmInfo.getDwarfRegNum(Src.getReg(), isEH);
494 int Offset = Dst.getOffset() / dataAlignmentFactor;
495
496 if (Offset < 0) {
497 streamer.EmitIntValue(dwarf::DW_CFA_offset_extended_sf, 1);
498 streamer.EmitULEB128IntValue(Reg);
499 streamer.EmitSLEB128IntValue(Offset);
500 } else if (Reg < 64) {
501 streamer.EmitIntValue(dwarf::DW_CFA_offset + Reg, 1);
502 streamer.EmitULEB128IntValue(Offset, 1);
503 } else {
504 streamer.EmitIntValue(dwarf::DW_CFA_offset_extended, 1);
505 streamer.EmitULEB128IntValue(Reg, 1);
506 streamer.EmitULEB128IntValue(Offset, 1);
507 }
508 }
509}
510
Rafael Espindolad7c8cca2010-12-26 20:20:31 +0000511static const MCSymbol &EmitCIE(MCStreamer &streamer,
Rafael Espindola3a83c402010-12-27 00:36:05 +0000512 const MCSymbol *personality,
513 unsigned personalityEncoding) {
Rafael Espindola89b93722010-12-10 07:39:47 +0000514 MCContext &context = streamer.getContext();
515 const TargetAsmInfo &asmInfo = context.getTargetAsmInfo();
516 const MCSection &section = *asmInfo.getEHFrameSection();
517 streamer.SwitchSection(&section);
518 MCSymbol *sectionStart = streamer.getContext().CreateTempSymbol();
519 MCSymbol *sectionEnd = streamer.getContext().CreateTempSymbol();
520
521 // Length
522 const MCExpr *Length = MakeStartMinusEndExpr(streamer, *sectionStart,
523 *sectionEnd, 4);
524 streamer.EmitLabel(sectionStart);
525 streamer.EmitValue(Length, 4);
526
527 // CIE ID
528 streamer.EmitIntValue(0, 4);
529
530 // Version
531 streamer.EmitIntValue(dwarf::DW_CIE_VERSION, 1);
532
533 // Augmentation String
534 SmallString<8> Augmentation;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +0000535 Augmentation += "z";
536 if (personality)
537 Augmentation += "P";
538 Augmentation += "R";
Rafael Espindola89b93722010-12-10 07:39:47 +0000539 streamer.EmitBytes(Augmentation.str(), 0);
540 streamer.EmitIntValue(0, 1);
541
542 // Code Alignment Factor
543 streamer.EmitULEB128IntValue(1);
544
545 // Data Alignment Factor
546 streamer.EmitSLEB128IntValue(getDataAlignmentFactor(streamer));
547
548 // Return Address Register
549 streamer.EmitULEB128IntValue(asmInfo.getDwarfRARegNum(true));
550
551 // Augmentation Data Length (optional)
552 MCSymbol *augmentationStart = streamer.getContext().CreateTempSymbol();
553 MCSymbol *augmentationEnd = streamer.getContext().CreateTempSymbol();
554 const MCExpr *augmentationLength = MakeStartMinusEndExpr(streamer,
555 *augmentationStart,
556 *augmentationEnd, 0);
557 streamer.EmitULEB128Value(augmentationLength);
558
559 // Augmentation Data (optional)
560 streamer.EmitLabel(augmentationStart);
Rafael Espindolad7c8cca2010-12-26 20:20:31 +0000561 if (personality) {
562 // Personality Encoding
Rafael Espindola3a83c402010-12-27 00:36:05 +0000563 streamer.EmitIntValue(personalityEncoding, 1);
Rafael Espindolad7c8cca2010-12-26 20:20:31 +0000564 // Personality
Rafael Espindola3a83c402010-12-27 00:36:05 +0000565 unsigned format = personalityEncoding & 0x0f;
566 unsigned application = personalityEncoding & 0xf0;
567 unsigned size;
568 switch (format) {
569 default:
570 assert(0 && "Unknown Encoding");
571 break;
572 case dwarf::DW_EH_PE_absptr:
573 case dwarf::DW_EH_PE_signed:
574 size = asmInfo.getPointerSize();
575 break;
576 case dwarf::DW_EH_PE_udata2:
577 case dwarf::DW_EH_PE_sdata2:
578 size = 2;
579 break;
580 case dwarf::DW_EH_PE_udata4:
581 case dwarf::DW_EH_PE_sdata4:
582 size = 4;
583 break;
584 case dwarf::DW_EH_PE_udata8:
585 case dwarf::DW_EH_PE_sdata8:
586 size = 8;
587 break;
588 }
589 switch (application) {
590 default:
591 assert(0 && "Unknown Encoding");
592 break;
593 case 0:
594 case dwarf::DW_EH_PE_indirect:
595 streamer.EmitSymbolValue(personality, size);
596 break;
597 case dwarf::DW_EH_PE_pcrel:
598 streamer.EmitPCRelSymbolValue(personality, size);
599 break;
600 }
Rafael Espindolad7c8cca2010-12-26 20:20:31 +0000601 }
602 // Encoding of the FDE pointers
Rafael Espindola89b93722010-12-10 07:39:47 +0000603 streamer.EmitIntValue(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4, 1);
604 streamer.EmitLabel(augmentationEnd);
605
606 // Initial Instructions
607
608 const std::vector<MachineMove> Moves = asmInfo.getInitialFrameState();
609
610 EmitFrameMoves(streamer, Moves, NULL, true);
611
612 // Padding
Rafael Espindolad7c8cca2010-12-26 20:20:31 +0000613 streamer.EmitValueToAlignment(4);
Rafael Espindola89b93722010-12-10 07:39:47 +0000614
615 streamer.EmitLabel(sectionEnd);
616 return *sectionStart;
617}
618
Rafael Espindoladfe125c2010-12-17 00:28:02 +0000619static MCSymbol *EmitFDE(MCStreamer &streamer,
620 const MCSymbol &cieStart,
621 const MCDwarfFrameInfo &frame) {
Rafael Espindola89b93722010-12-10 07:39:47 +0000622 MCSymbol *fdeStart = streamer.getContext().CreateTempSymbol();
623 MCSymbol *fdeEnd = streamer.getContext().CreateTempSymbol();
624
625 // Length
626 const MCExpr *Length = MakeStartMinusEndExpr(streamer, *fdeStart, *fdeEnd, 0);
627 streamer.EmitValue(Length, 4);
628
629 streamer.EmitLabel(fdeStart);
630 // CIE Pointer
631 const MCExpr *offset = MakeStartMinusEndExpr(streamer, cieStart, *fdeStart,
632 0);
633 streamer.EmitValue(offset, 4);
634
635 // PC Begin
636 streamer.EmitPCRelSymbolValue(frame.Begin, 4);
637
638 // PC Range
639 const MCExpr *Range = MakeStartMinusEndExpr(streamer, *frame.Begin,
640 *frame.End, 0);
641 streamer.EmitValue(Range, 4);
642
643 // Augmentation Data Length
644 streamer.EmitULEB128IntValue(0);
645
646 // Augmentation Data
647 // Call Frame Instructions
648
649 // Padding
Rafael Espindoladfe125c2010-12-17 00:28:02 +0000650 streamer.EmitValueToAlignment(4);
651
652 return fdeEnd;
Rafael Espindola89b93722010-12-10 07:39:47 +0000653}
654
655void MCDwarfFrameEmitter::Emit(MCStreamer &streamer) {
Rafael Espindoladfe125c2010-12-17 00:28:02 +0000656 const MCContext &context = streamer.getContext();
657 const TargetAsmInfo &asmInfo = context.getTargetAsmInfo();
Rafael Espindoladfe125c2010-12-17 00:28:02 +0000658 MCSymbol *fdeEnd = NULL;
Rafael Espindola3a83c402010-12-27 00:36:05 +0000659 typedef std::pair<const MCSymbol*, unsigned> personalityKey;
660 DenseMap<personalityKey, const MCSymbol*> personalities;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +0000661
Rafael Espindoladfe125c2010-12-17 00:28:02 +0000662 for (unsigned i = 0, n = streamer.getNumFrameInfos(); i < n; ++i) {
Rafael Espindolad7c8cca2010-12-26 20:20:31 +0000663 const MCDwarfFrameInfo &frame = streamer.getFrameInfo(i);
Rafael Espindola3a83c402010-12-27 00:36:05 +0000664 personalityKey key(frame.Personality, frame.PersonalityEncoding);
665 const MCSymbol *&cieStart = personalities[key];
Rafael Espindolad7c8cca2010-12-26 20:20:31 +0000666 if (!cieStart)
Rafael Espindola3a83c402010-12-27 00:36:05 +0000667 cieStart = &EmitCIE(streamer, frame.Personality,
668 frame.PersonalityEncoding);
Rafael Espindolad7c8cca2010-12-26 20:20:31 +0000669 fdeEnd = EmitFDE(streamer, *cieStart, frame);
Rafael Espindoladfe125c2010-12-17 00:28:02 +0000670 if (i != n - 1)
671 streamer.EmitLabel(fdeEnd);
672 }
Rafael Espindolad7c8cca2010-12-26 20:20:31 +0000673
Rafael Espindoladfe125c2010-12-17 00:28:02 +0000674 streamer.EmitValueToAlignment(asmInfo.getPointerSize());
675 if (fdeEnd)
676 streamer.EmitLabel(fdeEnd);
Rafael Espindola89b93722010-12-10 07:39:47 +0000677}