blob: 65f1ad1e176e2e97206b0dcfb99a3aaa5f3904b2 [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
10#include "llvm/MC/MCDwarf.h"
Evan Chenge76a33b2011-07-20 05:58:47 +000011#include "llvm/MC/MCAsmInfo.h"
12#include "llvm/MC/MCContext.h"
13#include "llvm/MC/MCObjectFileInfo.h"
14#include "llvm/MC/MCObjectWriter.h"
15#include "llvm/MC/MCRegisterInfo.h"
Rafael Espindolaad8aaa02010-11-22 11:53:17 +000016#include "llvm/MC/MCStreamer.h"
Kevin Enderbyc0957932010-09-30 16:52:03 +000017#include "llvm/MC/MCSymbol.h"
18#include "llvm/MC/MCExpr.h"
Kevin Enderby7cbf73a2010-07-28 20:55:35 +000019#include "llvm/Support/Debug.h"
Rafael Espindolafe024d02010-12-28 18:36:23 +000020#include "llvm/Support/ErrorHandling.h"
Kevin Enderby7cbf73a2010-07-28 20:55:35 +000021#include "llvm/Support/raw_ostream.h"
Kevin Enderby94c2e852011-12-09 18:09:40 +000022#include "llvm/Support/Path.h"
23#include "llvm/Support/SourceMgr.h"
Bill Wendling2b2dc7c2011-07-06 22:52:32 +000024#include "llvm/ADT/FoldingSet.h"
25#include "llvm/ADT/SmallString.h"
Bill Wendling2b2dc7c2011-07-06 22:52:32 +000026#include "llvm/ADT/Twine.h"
Kevin Enderby94c2e852011-12-09 18:09:40 +000027#include "llvm/Config/config.h"
Kevin Enderby7cbf73a2010-07-28 20:55:35 +000028using namespace llvm;
29
Kevin Enderbyc0957932010-09-30 16:52:03 +000030// Given a special op, return the address skip amount (in units of
31// DWARF2_LINE_MIN_INSN_LENGTH.
32#define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE)
33
34// The maximum address skip amount that can be encoded with a special op.
Bill Wendlingc3882162011-06-30 23:59:38 +000035#define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255)
Kevin Enderbyc0957932010-09-30 16:52:03 +000036
37// First special line opcode - leave room for the standard opcodes.
38// Note: If you want to change this, you'll have to update the
39// "standard_opcode_lengths" table that is emitted in DwarfFileTable::Emit().
Bill Wendlingc3882162011-06-30 23:59:38 +000040#define DWARF2_LINE_OPCODE_BASE 13
Kevin Enderbyc0957932010-09-30 16:52:03 +000041
42// Minimum line offset in a special line info. opcode. This value
43// was chosen to give a reasonable range of values.
Bill Wendlingc3882162011-06-30 23:59:38 +000044#define DWARF2_LINE_BASE -5
Kevin Enderbyc0957932010-09-30 16:52:03 +000045
46// Range of line offsets in a special line info. opcode.
Bill Wendlingc3882162011-06-30 23:59:38 +000047#define DWARF2_LINE_RANGE 14
Kevin Enderbyc0957932010-09-30 16:52:03 +000048
49// Define the architecture-dependent minimum instruction length (in bytes).
50// This value should be rather too small than too big.
Bill Wendlingc3882162011-06-30 23:59:38 +000051#define DWARF2_LINE_MIN_INSN_LENGTH 1
Kevin Enderbyc0957932010-09-30 16:52:03 +000052
53// Note: when DWARF2_LINE_MIN_INSN_LENGTH == 1 which is the current setting,
54// this routine is a nop and will be optimized away.
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +000055static inline uint64_t ScaleAddrDelta(uint64_t AddrDelta) {
Kevin Enderbyc0957932010-09-30 16:52:03 +000056 if (DWARF2_LINE_MIN_INSN_LENGTH == 1)
57 return AddrDelta;
58 if (AddrDelta % DWARF2_LINE_MIN_INSN_LENGTH != 0) {
59 // TODO: report this error, but really only once.
60 ;
61 }
62 return AddrDelta / DWARF2_LINE_MIN_INSN_LENGTH;
63}
64
65//
66// This is called when an instruction is assembled into the specified section
67// and if there is information from the last .loc directive that has yet to have
68// a line entry made for it is made.
69//
Rafael Espindola195a0ce2010-11-19 02:26:16 +000070void MCLineEntry::Make(MCStreamer *MCOS, const MCSection *Section) {
Kevin Enderbyc0957932010-09-30 16:52:03 +000071 if (!MCOS->getContext().getDwarfLocSeen())
72 return;
73
74 // Create a symbol at in the current section for use in the line entry.
75 MCSymbol *LineSym = MCOS->getContext().CreateTempSymbol();
76 // Set the value of the symbol to use for the MCLineEntry.
77 MCOS->EmitLabel(LineSym);
78
79 // Get the current .loc info saved in the context.
80 const MCDwarfLoc &DwarfLoc = MCOS->getContext().getCurrentDwarfLoc();
81
82 // Create a (local) line entry with the symbol and the current .loc info.
83 MCLineEntry LineEntry(LineSym, DwarfLoc);
84
85 // clear DwarfLocSeen saying the current .loc info is now used.
Kevin Enderby3f55c242010-10-04 20:17:24 +000086 MCOS->getContext().ClearDwarfLocSeen();
Kevin Enderbyc0957932010-09-30 16:52:03 +000087
88 // Get the MCLineSection for this section, if one does not exist for this
89 // section create it.
Rafael Espindola17fd7bd2010-11-19 07:41:23 +000090 const DenseMap<const MCSection *, MCLineSection *> &MCLineSections =
Kevin Enderbyc0957932010-09-30 16:52:03 +000091 MCOS->getContext().getMCLineSections();
Rafael Espindola17fd7bd2010-11-19 07:41:23 +000092 MCLineSection *LineSection = MCLineSections.lookup(Section);
Kevin Enderbyc0957932010-09-30 16:52:03 +000093 if (!LineSection) {
94 // Create a new MCLineSection. This will be deleted after the dwarf line
95 // table is created using it by iterating through the MCLineSections
96 // DenseMap.
97 LineSection = new MCLineSection;
98 // Save a pointer to the new LineSection into the MCLineSections DenseMap.
Rafael Espindola17fd7bd2010-11-19 07:41:23 +000099 MCOS->getContext().addMCLineSection(Section, LineSection);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000100 }
101
102 // Add the line entry to this section's entries.
103 LineSection->addLineEntry(LineEntry);
104}
105
106//
107// This helper routine returns an expression of End - Start + IntVal .
108//
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000109static inline const MCExpr *MakeStartMinusEndExpr(const MCStreamer &MCOS,
110 const MCSymbol &Start,
111 const MCSymbol &End,
112 int IntVal) {
Kevin Enderbyc0957932010-09-30 16:52:03 +0000113 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
114 const MCExpr *Res =
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000115 MCSymbolRefExpr::Create(&End, Variant, MCOS.getContext());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000116 const MCExpr *RHS =
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000117 MCSymbolRefExpr::Create(&Start, Variant, MCOS.getContext());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000118 const MCExpr *Res1 =
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000119 MCBinaryExpr::Create(MCBinaryExpr::Sub, Res, RHS, MCOS.getContext());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000120 const MCExpr *Res2 =
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000121 MCConstantExpr::Create(IntVal, MCOS.getContext());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000122 const MCExpr *Res3 =
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000123 MCBinaryExpr::Create(MCBinaryExpr::Sub, Res1, Res2, MCOS.getContext());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000124 return Res3;
125}
126
Kevin Enderbyc0957932010-09-30 16:52:03 +0000127//
128// This emits the Dwarf line table for the specified section from the entries
129// in the LineSection.
130//
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000131static inline void EmitDwarfLineTable(MCStreamer *MCOS,
Kevin Enderbyc0957932010-09-30 16:52:03 +0000132 const MCSection *Section,
Rafael Espindola89b93722010-12-10 07:39:47 +0000133 const MCLineSection *LineSection) {
Kevin Enderbyc0957932010-09-30 16:52:03 +0000134 unsigned FileNum = 1;
135 unsigned LastLine = 1;
136 unsigned Column = 0;
137 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
138 unsigned Isa = 0;
Kevin Enderbyc0957932010-09-30 16:52:03 +0000139 MCSymbol *LastLabel = NULL;
Kevin Enderbyc0957932010-09-30 16:52:03 +0000140
141 // Loop through each MCLineEntry and encode the dwarf line number table.
Rafael Espindola17fd7bd2010-11-19 07:41:23 +0000142 for (MCLineSection::const_iterator
Kevin Enderbyc0957932010-09-30 16:52:03 +0000143 it = LineSection->getMCLineEntries()->begin(),
144 ie = LineSection->getMCLineEntries()->end(); it != ie; ++it) {
145
146 if (FileNum != it->getFileNum()) {
147 FileNum = it->getFileNum();
148 MCOS->EmitIntValue(dwarf::DW_LNS_set_file, 1);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000149 MCOS->EmitULEB128IntValue(FileNum);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000150 }
151 if (Column != it->getColumn()) {
152 Column = it->getColumn();
153 MCOS->EmitIntValue(dwarf::DW_LNS_set_column, 1);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000154 MCOS->EmitULEB128IntValue(Column);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000155 }
156 if (Isa != it->getIsa()) {
157 Isa = it->getIsa();
158 MCOS->EmitIntValue(dwarf::DW_LNS_set_isa, 1);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000159 MCOS->EmitULEB128IntValue(Isa);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000160 }
161 if ((it->getFlags() ^ Flags) & DWARF2_FLAG_IS_STMT) {
162 Flags = it->getFlags();
163 MCOS->EmitIntValue(dwarf::DW_LNS_negate_stmt, 1);
164 }
165 if (it->getFlags() & DWARF2_FLAG_BASIC_BLOCK)
166 MCOS->EmitIntValue(dwarf::DW_LNS_set_basic_block, 1);
167 if (it->getFlags() & DWARF2_FLAG_PROLOGUE_END)
168 MCOS->EmitIntValue(dwarf::DW_LNS_set_prologue_end, 1);
169 if (it->getFlags() & DWARF2_FLAG_EPILOGUE_BEGIN)
170 MCOS->EmitIntValue(dwarf::DW_LNS_set_epilogue_begin, 1);
171
Rafael Espindola64185cc2010-11-13 01:06:27 +0000172 int64_t LineDelta = static_cast<int64_t>(it->getLine()) - LastLine;
Kevin Enderbyc0957932010-09-30 16:52:03 +0000173 MCSymbol *Label = it->getLabel();
174
175 // At this point we want to emit/create the sequence to encode the delta in
176 // line numbers and the increment of the address from the previous Label
177 // and the current Label.
Evan Cheng1be0e272011-07-15 02:09:41 +0000178 const MCAsmInfo &asmInfo = MCOS->getContext().getAsmInfo();
Evan Cheng672b93a2011-07-14 05:43:07 +0000179 MCOS->EmitDwarfAdvanceLineAddr(LineDelta, LastLabel, Label,
180 asmInfo.getPointerSize());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000181
182 LastLine = it->getLine();
183 LastLabel = Label;
Kevin Enderbyc0957932010-09-30 16:52:03 +0000184 }
185
186 // Emit a DW_LNE_end_sequence for the end of the section.
187 // Using the pointer Section create a temporary label at the end of the
188 // section and use that and the LastLabel to compute the address delta
189 // and use INT64_MAX as the line delta which is the signal that this is
190 // actually a DW_LNE_end_sequence.
191
192 // Switch to the section to be able to create a symbol at its end.
193 MCOS->SwitchSection(Section);
Rafael Espindola89b93722010-12-10 07:39:47 +0000194
195 MCContext &context = MCOS->getContext();
Kevin Enderbyc0957932010-09-30 16:52:03 +0000196 // Create a symbol at the end of the section.
Rafael Espindola89b93722010-12-10 07:39:47 +0000197 MCSymbol *SectionEnd = context.CreateTempSymbol();
Kevin Enderbyc0957932010-09-30 16:52:03 +0000198 // Set the value of the symbol, as we are at the end of the section.
199 MCOS->EmitLabel(SectionEnd);
200
201 // Switch back the the dwarf line section.
Evan Chenge76a33b2011-07-20 05:58:47 +0000202 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfLineSection());
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000203
Evan Cheng1be0e272011-07-15 02:09:41 +0000204 const MCAsmInfo &asmInfo = MCOS->getContext().getAsmInfo();
Evan Cheng672b93a2011-07-14 05:43:07 +0000205 MCOS->EmitDwarfAdvanceLineAddr(INT64_MAX, LastLabel, SectionEnd,
206 asmInfo.getPointerSize());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000207}
208
209//
210// This emits the Dwarf file and the line tables.
211//
Rafael Espindola89b93722010-12-10 07:39:47 +0000212void MCDwarfFileTable::Emit(MCStreamer *MCOS) {
213 MCContext &context = MCOS->getContext();
Kevin Enderbyc0957932010-09-30 16:52:03 +0000214 // Switch to the section where the table will be emitted into.
Evan Chenge76a33b2011-07-20 05:58:47 +0000215 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfLineSection());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000216
217 // Create a symbol at the beginning of this section.
Rafael Espindola89b93722010-12-10 07:39:47 +0000218 MCSymbol *LineStartSym = context.CreateTempSymbol();
Kevin Enderbyc0957932010-09-30 16:52:03 +0000219 // Set the value of the symbol, as we are at the start of the section.
220 MCOS->EmitLabel(LineStartSym);
221
222 // Create a symbol for the end of the section (to be set when we get there).
Rafael Espindola89b93722010-12-10 07:39:47 +0000223 MCSymbol *LineEndSym = context.CreateTempSymbol();
Kevin Enderbyc0957932010-09-30 16:52:03 +0000224
225 // The first 4 bytes is the total length of the information for this
226 // compilation unit (not including these 4 bytes for the length).
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000227 MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *LineEndSym,4),
Rafael Espindola0bbe0b42010-12-06 17:27:56 +0000228 4);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000229
230 // Next 2 bytes is the Version, which is Dwarf 2.
231 MCOS->EmitIntValue(2, 2);
232
233 // Create a symbol for the end of the prologue (to be set when we get there).
Rafael Espindola89b93722010-12-10 07:39:47 +0000234 MCSymbol *ProEndSym = context.CreateTempSymbol(); // Lprologue_end
Kevin Enderbyc0957932010-09-30 16:52:03 +0000235
236 // Length of the prologue, is the next 4 bytes. Which is the start of the
237 // section to the end of the prologue. Not including the 4 bytes for the
238 // total length, the 2 bytes for the version, and these 4 bytes for the
239 // length of the prologue.
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000240 MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *ProEndSym,
Kevin Enderbyc0957932010-09-30 16:52:03 +0000241 (4 + 2 + 4)),
Rafael Espindola5d4918d2010-12-04 03:21:47 +0000242 4, 0);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000243
244 // Parameters of the state machine, are next.
245 MCOS->EmitIntValue(DWARF2_LINE_MIN_INSN_LENGTH, 1);
246 MCOS->EmitIntValue(DWARF2_LINE_DEFAULT_IS_STMT, 1);
247 MCOS->EmitIntValue(DWARF2_LINE_BASE, 1);
248 MCOS->EmitIntValue(DWARF2_LINE_RANGE, 1);
249 MCOS->EmitIntValue(DWARF2_LINE_OPCODE_BASE, 1);
250
251 // Standard opcode lengths
252 MCOS->EmitIntValue(0, 1); // length of DW_LNS_copy
253 MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_pc
254 MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_line
255 MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_file
256 MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_column
257 MCOS->EmitIntValue(0, 1); // length of DW_LNS_negate_stmt
258 MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_basic_block
259 MCOS->EmitIntValue(0, 1); // length of DW_LNS_const_add_pc
260 MCOS->EmitIntValue(1, 1); // length of DW_LNS_fixed_advance_pc
261 MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_prologue_end
262 MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_epilogue_begin
263 MCOS->EmitIntValue(1, 1); // DW_LNS_set_isa
264
265 // Put out the directory and file tables.
266
267 // First the directory table.
268 const std::vector<StringRef> &MCDwarfDirs =
Rafael Espindola89b93722010-12-10 07:39:47 +0000269 context.getMCDwarfDirs();
Kevin Enderbyc0957932010-09-30 16:52:03 +0000270 for (unsigned i = 0; i < MCDwarfDirs.size(); i++) {
271 MCOS->EmitBytes(MCDwarfDirs[i], 0); // the DirectoryName
272 MCOS->EmitBytes(StringRef("\0", 1), 0); // the null term. of the string
273 }
274 MCOS->EmitIntValue(0, 1); // Terminate the directory list
275
276 // Second the file table.
277 const std::vector<MCDwarfFile *> &MCDwarfFiles =
278 MCOS->getContext().getMCDwarfFiles();
279 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
280 MCOS->EmitBytes(MCDwarfFiles[i]->getName(), 0); // FileName
281 MCOS->EmitBytes(StringRef("\0", 1), 0); // the null term. of the string
Rafael Espindola3ff57092010-11-02 17:22:24 +0000282 // the Directory num
283 MCOS->EmitULEB128IntValue(MCDwarfFiles[i]->getDirIndex());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000284 MCOS->EmitIntValue(0, 1); // last modification timestamp (always 0)
285 MCOS->EmitIntValue(0, 1); // filesize (always 0)
286 }
287 MCOS->EmitIntValue(0, 1); // Terminate the file list
288
289 // This is the end of the prologue, so set the value of the symbol at the
290 // end of the prologue (that was used in a previous expression).
291 MCOS->EmitLabel(ProEndSym);
292
293 // Put out the line tables.
Rafael Espindola17fd7bd2010-11-19 07:41:23 +0000294 const DenseMap<const MCSection *, MCLineSection *> &MCLineSections =
Kevin Enderbyc0957932010-09-30 16:52:03 +0000295 MCOS->getContext().getMCLineSections();
Rafael Espindola17fd7bd2010-11-19 07:41:23 +0000296 const std::vector<const MCSection *> &MCLineSectionOrder =
297 MCOS->getContext().getMCLineSectionOrder();
298 for (std::vector<const MCSection*>::const_iterator it =
Bill Wendlingc3882162011-06-30 23:59:38 +0000299 MCLineSectionOrder.begin(), ie = MCLineSectionOrder.end(); it != ie;
Rafael Espindola17fd7bd2010-11-19 07:41:23 +0000300 ++it) {
301 const MCSection *Sec = *it;
302 const MCLineSection *Line = MCLineSections.lookup(Sec);
Rafael Espindola89b93722010-12-10 07:39:47 +0000303 EmitDwarfLineTable(MCOS, Sec, Line);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000304
305 // Now delete the MCLineSections that were created in MCLineEntry::Make()
306 // and used to emit the line table.
Rafael Espindola17fd7bd2010-11-19 07:41:23 +0000307 delete Line;
Kevin Enderbyc0957932010-09-30 16:52:03 +0000308 }
309
Rafael Espindola767b1be2010-12-04 00:31:13 +0000310 if (MCOS->getContext().getAsmInfo().getLinkerRequiresNonEmptyDwarfLines()
311 && MCLineSectionOrder.begin() == MCLineSectionOrder.end()) {
Rafael Espindolaa8de83c2010-12-03 23:36:59 +0000312 // The darwin9 linker has a bug (see PR8715). For for 32-bit architectures
313 // it requires:
314 // total_length >= prologue_length + 10
315 // We are 4 bytes short, since we have total_length = 51 and
316 // prologue_length = 45
Devang Patel5113cdb2010-12-03 00:10:48 +0000317
Rafael Espindolaa8de83c2010-12-03 23:36:59 +0000318 // The regular end_sequence should be sufficient.
319 MCDwarfLineAddr::Emit(MCOS, INT64_MAX, 0);
Devang Patel5113cdb2010-12-03 00:10:48 +0000320 }
321
Kevin Enderbyc0957932010-09-30 16:52:03 +0000322 // This is the end of the section, so set the value of the symbol at the end
323 // of this section (that was used in a previous expression).
324 MCOS->EmitLabel(LineEndSym);
325}
326
Kevin Enderbyc0957932010-09-30 16:52:03 +0000327/// Utility function to write the encoding to an object writer.
328void MCDwarfLineAddr::Write(MCObjectWriter *OW, int64_t LineDelta,
329 uint64_t AddrDelta) {
330 SmallString<256> Tmp;
331 raw_svector_ostream OS(Tmp);
332 MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OS);
333 OW->WriteBytes(OS.str());
334}
335
336/// Utility function to emit the encoding to a streamer.
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000337void MCDwarfLineAddr::Emit(MCStreamer *MCOS, int64_t LineDelta,
Kevin Enderbyc0957932010-09-30 16:52:03 +0000338 uint64_t AddrDelta) {
339 SmallString<256> Tmp;
340 raw_svector_ostream OS(Tmp);
341 MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OS);
342 MCOS->EmitBytes(OS.str(), /*AddrSpace=*/0);
343}
344
345/// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
346void MCDwarfLineAddr::Encode(int64_t LineDelta, uint64_t AddrDelta,
347 raw_ostream &OS) {
348 uint64_t Temp, Opcode;
349 bool NeedCopy = false;
350
351 // Scale the address delta by the minimum instruction length.
352 AddrDelta = ScaleAddrDelta(AddrDelta);
353
354 // A LineDelta of INT64_MAX is a signal that this is actually a
355 // DW_LNE_end_sequence. We cannot use special opcodes here, since we want the
356 // end_sequence to emit the matrix entry.
357 if (LineDelta == INT64_MAX) {
358 if (AddrDelta == MAX_SPECIAL_ADDR_DELTA)
359 OS << char(dwarf::DW_LNS_const_add_pc);
360 else {
361 OS << char(dwarf::DW_LNS_advance_pc);
Benjamin Kramerdcf0e0c2011-06-18 14:42:47 +0000362 MCObjectWriter::EncodeULEB128(AddrDelta, OS);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000363 }
364 OS << char(dwarf::DW_LNS_extended_op);
365 OS << char(1);
366 OS << char(dwarf::DW_LNE_end_sequence);
367 return;
368 }
369
370 // Bias the line delta by the base.
371 Temp = LineDelta - DWARF2_LINE_BASE;
372
373 // If the line increment is out of range of a special opcode, we must encode
374 // it with DW_LNS_advance_line.
375 if (Temp >= DWARF2_LINE_RANGE) {
376 OS << char(dwarf::DW_LNS_advance_line);
Benjamin Kramer2dd42392011-11-09 13:19:15 +0000377 MCObjectWriter::EncodeSLEB128(LineDelta, OS);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000378
379 LineDelta = 0;
380 Temp = 0 - DWARF2_LINE_BASE;
381 NeedCopy = true;
382 }
383
384 // Use DW_LNS_copy instead of a "line +0, addr +0" special opcode.
385 if (LineDelta == 0 && AddrDelta == 0) {
386 OS << char(dwarf::DW_LNS_copy);
387 return;
388 }
389
390 // Bias the opcode by the special opcode base.
391 Temp += DWARF2_LINE_OPCODE_BASE;
392
393 // Avoid overflow when addr_delta is large.
394 if (AddrDelta < 256 + MAX_SPECIAL_ADDR_DELTA) {
395 // Try using a special opcode.
396 Opcode = Temp + AddrDelta * DWARF2_LINE_RANGE;
397 if (Opcode <= 255) {
398 OS << char(Opcode);
399 return;
400 }
401
402 // Try using DW_LNS_const_add_pc followed by special op.
403 Opcode = Temp + (AddrDelta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
404 if (Opcode <= 255) {
405 OS << char(dwarf::DW_LNS_const_add_pc);
406 OS << char(Opcode);
407 return;
408 }
409 }
410
411 // Otherwise use DW_LNS_advance_pc.
412 OS << char(dwarf::DW_LNS_advance_pc);
Benjamin Kramer2dd42392011-11-09 13:19:15 +0000413 MCObjectWriter::EncodeULEB128(AddrDelta, OS);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000414
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
Kevin Enderby94c2e852011-12-09 18:09:40 +0000429// Utility function to write a tuple for .debug_abbrev.
430static void EmitAbbrev(MCStreamer *MCOS, uint64_t Name, uint64_t Form) {
431 MCOS->EmitULEB128IntValue(Name);
432 MCOS->EmitULEB128IntValue(Form);
433}
434
435// When generating dwarf for assembly source files this emits
436// the data for .debug_abbrev section which contains three DIEs.
437static void EmitGenDwarfAbbrev(MCStreamer *MCOS) {
438 MCContext &context = MCOS->getContext();
439 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
440
441 // DW_TAG_compile_unit DIE abbrev (1).
442 MCOS->EmitULEB128IntValue(1);
443 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_compile_unit);
444 MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
445 EmitAbbrev(MCOS, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4);
446 EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
447 EmitAbbrev(MCOS, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr);
448 EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
449 EmitAbbrev(MCOS, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string);
450 StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
451 if (!DwarfDebugFlags.empty())
452 EmitAbbrev(MCOS, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string);
453 EmitAbbrev(MCOS, dwarf::DW_AT_producer, dwarf::DW_FORM_string);
454 EmitAbbrev(MCOS, dwarf::DW_AT_language, dwarf::DW_FORM_data2);
455 EmitAbbrev(MCOS, 0, 0);
456
Kevin Enderby38fdb7d2012-01-10 17:52:29 +0000457 // DW_TAG_label DIE abbrev (2).
Kevin Enderby94c2e852011-12-09 18:09:40 +0000458 MCOS->EmitULEB128IntValue(2);
Kevin Enderby38fdb7d2012-01-10 17:52:29 +0000459 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_label);
Kevin Enderby94c2e852011-12-09 18:09:40 +0000460 MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
461 EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
462 EmitAbbrev(MCOS, dwarf::DW_AT_decl_file, dwarf::DW_FORM_data4);
463 EmitAbbrev(MCOS, dwarf::DW_AT_decl_line, dwarf::DW_FORM_data4);
464 EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
Kevin Enderby94c2e852011-12-09 18:09:40 +0000465 EmitAbbrev(MCOS, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag);
466 EmitAbbrev(MCOS, 0, 0);
467
468 // DW_TAG_unspecified_parameters DIE abbrev (3).
469 MCOS->EmitULEB128IntValue(3);
470 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_unspecified_parameters);
471 MCOS->EmitIntValue(dwarf::DW_CHILDREN_no, 1);
472 EmitAbbrev(MCOS, 0, 0);
473
474 // Terminate the abbreviations for this compilation unit.
475 MCOS->EmitIntValue(0, 1);
476}
477
478// When generating dwarf for assembly source files this emits the data for
479// .debug_aranges section. Which contains a header and a table of pairs of
480// PointerSize'ed values for the address and size of section(s) with line table
481// entries (just the default .text in our case) and a terminating pair of zeros.
482static void EmitGenDwarfAranges(MCStreamer *MCOS) {
483 MCContext &context = MCOS->getContext();
484
485 // Create a symbol at the end of the section that we are creating the dwarf
486 // debugging info to use later in here as part of the expression to calculate
487 // the size of the section for the table.
488 MCOS->SwitchSection(context.getGenDwarfSection());
489 MCSymbol *SectionEndSym = context.CreateTempSymbol();
490 MCOS->EmitLabel(SectionEndSym);
491 context.setGenDwarfSectionEndSym(SectionEndSym);
492
493 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
494
495 // This will be the length of the .debug_aranges section, first account for
496 // the size of each item in the header (see below where we emit these items).
497 int Length = 4 + 2 + 4 + 1 + 1;
498
499 // Figure the padding after the header before the table of address and size
500 // pairs who's values are PointerSize'ed.
501 const MCAsmInfo &asmInfo = context.getAsmInfo();
502 int AddrSize = asmInfo.getPointerSize();
503 int Pad = 2 * AddrSize - (Length & (2 * AddrSize - 1));
504 if (Pad == 2 * AddrSize)
505 Pad = 0;
506 Length += Pad;
507
508 // Add the size of the pair of PointerSize'ed values for the address and size
509 // of the one default .text section we have in the table.
510 Length += 2 * AddrSize;
511 // And the pair of terminating zeros.
512 Length += 2 * AddrSize;
513
514
515 // Emit the header for this section.
516 // The 4 byte length not including the 4 byte value for the length.
517 MCOS->EmitIntValue(Length - 4, 4);
518 // The 2 byte version, which is 2.
519 MCOS->EmitIntValue(2, 2);
520 // The 4 byte offset to the compile unit in the .debug_info from the start
521 // of the .debug_info, it is at the start of that section so this is zero.
522 MCOS->EmitIntValue(0, 4);
523 // The 1 byte size of an address.
524 MCOS->EmitIntValue(AddrSize, 1);
525 // The 1 byte size of a segment descriptor, we use a value of zero.
526 MCOS->EmitIntValue(0, 1);
527 // Align the header with the padding if needed, before we put out the table.
528 for(int i = 0; i < Pad; i++)
529 MCOS->EmitIntValue(0, 1);
530
531 // Now emit the table of pairs of PointerSize'ed values for the section(s)
532 // address and size, in our case just the one default .text section.
533 const MCExpr *Addr = MCSymbolRefExpr::Create(
534 context.getGenDwarfSectionStartSym(), MCSymbolRefExpr::VK_None, context);
535 const MCExpr *Size = MakeStartMinusEndExpr(*MCOS,
536 *context.getGenDwarfSectionStartSym(), *SectionEndSym, 0);
537 MCOS->EmitAbsValue(Addr, AddrSize);
538 MCOS->EmitAbsValue(Size, AddrSize);
539
540 // And finally the pair of terminating zeros.
541 MCOS->EmitIntValue(0, AddrSize);
542 MCOS->EmitIntValue(0, AddrSize);
543}
544
545// When generating dwarf for assembly source files this emits the data for
546// .debug_info section which contains three parts. The header, the compile_unit
Kevin Enderby38fdb7d2012-01-10 17:52:29 +0000547// DIE and a list of label DIEs.
Kevin Enderby94c2e852011-12-09 18:09:40 +0000548static void EmitGenDwarfInfo(MCStreamer *MCOS) {
549 MCContext &context = MCOS->getContext();
550
551 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
552
553 // Create a symbol at the start and end of this section used in here for the
554 // expression to calculate the length in the header.
555 MCSymbol *InfoStart = context.CreateTempSymbol();
556 MCOS->EmitLabel(InfoStart);
557 MCSymbol *InfoEnd = context.CreateTempSymbol();
558
559 // First part: the header.
560
561 // The 4 byte total length of the information for this compilation unit, not
562 // including these 4 bytes.
563 const MCExpr *Length = MakeStartMinusEndExpr(*MCOS, *InfoStart, *InfoEnd, 4);
564 MCOS->EmitAbsValue(Length, 4);
565
566 // The 2 byte DWARF version, which is 2.
567 MCOS->EmitIntValue(2, 2);
568
569 // The 4 byte offset to the debug abbrevs from the start of the .debug_abbrev,
570 // it is at the start of that section so this is zero.
571 MCOS->EmitIntValue(0, 4);
572
573 const MCAsmInfo &asmInfo = context.getAsmInfo();
574 int AddrSize = asmInfo.getPointerSize();
575 // The 1 byte size of an address.
576 MCOS->EmitIntValue(AddrSize, 1);
577
578 // Second part: the compile_unit DIE.
579
580 // The DW_TAG_compile_unit DIE abbrev (1).
581 MCOS->EmitULEB128IntValue(1);
582
583 // DW_AT_stmt_list, a 4 byte offset from the start of the .debug_line section,
584 // which is at the start of that section so this is zero.
585 MCOS->EmitIntValue(0, 4);
586
587 // AT_low_pc, the first address of the default .text section.
588 const MCExpr *Start = MCSymbolRefExpr::Create(
589 context.getGenDwarfSectionStartSym(), MCSymbolRefExpr::VK_None, context);
590 MCOS->EmitAbsValue(Start, AddrSize);
591
592 // AT_high_pc, the last address of the default .text section.
593 const MCExpr *End = MCSymbolRefExpr::Create(
594 context.getGenDwarfSectionEndSym(), MCSymbolRefExpr::VK_None, context);
595 MCOS->EmitAbsValue(End, AddrSize);
596
597 // AT_name, the name of the source file. Reconstruct from the first directory
598 // and file table entries.
599 const std::vector<StringRef> &MCDwarfDirs =
600 context.getMCDwarfDirs();
601 if (MCDwarfDirs.size() > 0) {
602 MCOS->EmitBytes(MCDwarfDirs[0], 0);
603 MCOS->EmitBytes("/", 0);
604 }
605 const std::vector<MCDwarfFile *> &MCDwarfFiles =
606 MCOS->getContext().getMCDwarfFiles();
607 MCOS->EmitBytes(MCDwarfFiles[1]->getName(), 0);
608 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
609
610 // AT_comp_dir, the working directory the assembly was done in.
611 llvm::sys::Path CWD = llvm::sys::Path::GetCurrentDirectory();
612 MCOS->EmitBytes(StringRef(CWD.c_str()), 0);
613 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
614
615 // AT_APPLE_flags, the command line arguments of the assembler tool.
616 StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
617 if (!DwarfDebugFlags.empty()){
618 MCOS->EmitBytes(DwarfDebugFlags, 0);
619 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
620 }
621
622 // AT_producer, the version of the assembler tool.
623 MCOS->EmitBytes(StringRef("llvm-mc (based on LLVM "), 0);
624 MCOS->EmitBytes(StringRef(PACKAGE_VERSION), 0);
625 MCOS->EmitBytes(StringRef(")"), 0);
626 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
627
628 // AT_language, a 4 byte value. We use DW_LANG_Mips_Assembler as the dwarf2
629 // draft has no standard code for assembler.
630 MCOS->EmitIntValue(dwarf::DW_LANG_Mips_Assembler, 2);
631
Kevin Enderby38fdb7d2012-01-10 17:52:29 +0000632 // Third part: the list of label DIEs.
Kevin Enderby94c2e852011-12-09 18:09:40 +0000633
Kevin Enderby11c2def2012-01-10 21:12:34 +0000634 // Loop on saved info for dwarf labels and create the DIEs for them.
635 const std::vector<const MCGenDwarfLabelEntry *> &Entries =
636 MCOS->getContext().getMCGenDwarfLabelEntries();
637 for (std::vector<const MCGenDwarfLabelEntry *>::const_iterator it =
Kevin Enderby94c2e852011-12-09 18:09:40 +0000638 Entries.begin(), ie = Entries.end(); it != ie;
639 ++it) {
Kevin Enderby11c2def2012-01-10 21:12:34 +0000640 const MCGenDwarfLabelEntry *Entry = *it;
Kevin Enderby94c2e852011-12-09 18:09:40 +0000641
Kevin Enderby38fdb7d2012-01-10 17:52:29 +0000642 // The DW_TAG_label DIE abbrev (2).
Kevin Enderby94c2e852011-12-09 18:09:40 +0000643 MCOS->EmitULEB128IntValue(2);
644
645 // AT_name, of the label without any leading underbar.
646 MCOS->EmitBytes(Entry->getName(), 0);
647 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
648
649 // AT_decl_file, index into the file table.
650 MCOS->EmitIntValue(Entry->getFileNumber(), 4);
651
652 // AT_decl_line, source line number.
653 MCOS->EmitIntValue(Entry->getLineNumber(), 4);
654
655 // AT_low_pc, start address of the label.
656 const MCExpr *AT_low_pc = MCSymbolRefExpr::Create(Entry->getLabel(),
657 MCSymbolRefExpr::VK_None, context);
658 MCOS->EmitAbsValue(AT_low_pc, AddrSize);
659
Kevin Enderby94c2e852011-12-09 18:09:40 +0000660 // DW_AT_prototyped, a one byte flag value of 0 saying we have no prototype.
661 MCOS->EmitIntValue(0, 1);
662
663 // The DW_TAG_unspecified_parameters DIE abbrev (3).
664 MCOS->EmitULEB128IntValue(3);
665
666 // Add the NULL DIE terminating the DW_TAG_unspecified_parameters DIE's.
667 MCOS->EmitIntValue(0, 1);
668 }
Kevin Enderby11c2def2012-01-10 21:12:34 +0000669 // Deallocate the MCGenDwarfLabelEntry classes that saved away the info
670 // for the dwarf labels.
671 for (std::vector<const MCGenDwarfLabelEntry *>::const_iterator it =
Kevin Enderby94c2e852011-12-09 18:09:40 +0000672 Entries.begin(), ie = Entries.end(); it != ie;
673 ++it) {
Kevin Enderby11c2def2012-01-10 21:12:34 +0000674 const MCGenDwarfLabelEntry *Entry = *it;
Kevin Enderby94c2e852011-12-09 18:09:40 +0000675 delete Entry;
676 }
677
678 // Add the NULL DIE terminating the Compile Unit DIE's.
679 MCOS->EmitIntValue(0, 1);
680
681 // Now set the value of the symbol at the end of the info section.
682 MCOS->EmitLabel(InfoEnd);
683}
684
685//
686// When generating dwarf for assembly source files this emits the Dwarf
687// sections.
688//
689void MCGenDwarfInfo::Emit(MCStreamer *MCOS) {
690 // Create the dwarf sections in this order (.debug_line already created).
691 MCContext &context = MCOS->getContext();
692 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
693 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
694 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
695
696 // If there are no line table entries then do not emit any section contents.
697 if (context.getMCLineSections().empty())
698 return;
699
700 // Output the data for .debug_aranges section.
701 EmitGenDwarfAranges(MCOS);
702
703 // Output the data for .debug_abbrev section.
704 EmitGenDwarfAbbrev(MCOS);
705
706 // Output the data for .debug_info section.
707 EmitGenDwarfInfo(MCOS);
708}
709
710//
711// When generating dwarf for assembly source files this is called when symbol
712// for a label is created. If this symbol is not a temporary and is in the
713// section that dwarf is being generated for, save the needed info to create
Kevin Enderby11c2def2012-01-10 21:12:34 +0000714// a dwarf label.
Kevin Enderby94c2e852011-12-09 18:09:40 +0000715//
Kevin Enderby11c2def2012-01-10 21:12:34 +0000716void MCGenDwarfLabelEntry::Make(MCSymbol *Symbol, MCStreamer *MCOS,
Kevin Enderby94c2e852011-12-09 18:09:40 +0000717 SourceMgr &SrcMgr, SMLoc &Loc) {
Kevin Enderby11c2def2012-01-10 21:12:34 +0000718 // We won't create dwarf label's for temporary symbols or symbols not in
Kevin Enderby94c2e852011-12-09 18:09:40 +0000719 // the default text.
720 if (Symbol->isTemporary())
721 return;
722 MCContext &context = MCOS->getContext();
723 if (context.getGenDwarfSection() != MCOS->getCurrentSection())
724 return;
725
Kevin Enderby11c2def2012-01-10 21:12:34 +0000726 // The dwarf label's name does not have the symbol name's leading
Kevin Enderby94c2e852011-12-09 18:09:40 +0000727 // underbar if any.
728 StringRef Name = Symbol->getName();
729 if (Name.startswith("_"))
730 Name = Name.substr(1, Name.size()-1);
731
Kevin Enderby11c2def2012-01-10 21:12:34 +0000732 // Get the dwarf file number to be used for the dwarf label.
Kevin Enderby94c2e852011-12-09 18:09:40 +0000733 unsigned FileNumber = context.getGenDwarfFileNumber();
734
735 // Finding the line number is the expensive part which is why we just don't
Kevin Enderby11c2def2012-01-10 21:12:34 +0000736 // pass it in as for some symbols we won't create a dwarf label.
Kevin Enderby94c2e852011-12-09 18:09:40 +0000737 int CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
738 unsigned LineNumber = SrcMgr.FindLineNumber(Loc, CurBuffer);
739
740 // We create a temporary symbol for use for the AT_high_pc and AT_low_pc
741 // values so that they don't have things like an ARM thumb bit from the
742 // original symbol. So when used they won't get a low bit set after
743 // relocation.
744 MCSymbol *Label = context.CreateTempSymbol();
745 MCOS->EmitLabel(Label);
746
747 // Create and entry for the info and add it to the other entries.
Kevin Enderby11c2def2012-01-10 21:12:34 +0000748 MCGenDwarfLabelEntry *Entry =
749 new MCGenDwarfLabelEntry(Name, FileNumber, LineNumber, Label);
750 MCOS->getContext().addMCGenDwarfLabelEntry(Entry);
Kevin Enderby94c2e852011-12-09 18:09:40 +0000751}
752
Rafael Espindola89b93722010-12-10 07:39:47 +0000753static int getDataAlignmentFactor(MCStreamer &streamer) {
754 MCContext &context = streamer.getContext();
Evan Cheng1be0e272011-07-15 02:09:41 +0000755 const MCAsmInfo &asmInfo = context.getAsmInfo();
Rafael Espindola89b93722010-12-10 07:39:47 +0000756 int size = asmInfo.getPointerSize();
Evan Cheng1be0e272011-07-15 02:09:41 +0000757 if (asmInfo.isStackGrowthDirectionUp())
Rafael Espindola89b93722010-12-10 07:39:47 +0000758 return size;
Evan Cheng1be0e272011-07-15 02:09:41 +0000759 else
760 return -size;
Rafael Espindola89b93722010-12-10 07:39:47 +0000761}
762
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000763static unsigned getSizeForEncoding(MCStreamer &streamer,
764 unsigned symbolEncoding) {
Rafael Espindolabdc31672010-12-27 15:56:22 +0000765 MCContext &context = streamer.getContext();
Rafael Espindolabdc31672010-12-27 15:56:22 +0000766 unsigned format = symbolEncoding & 0x0f;
Rafael Espindolabdc31672010-12-27 15:56:22 +0000767 switch (format) {
768 default:
769 assert(0 && "Unknown Encoding");
Rafael Espindolabdc31672010-12-27 15:56:22 +0000770 case dwarf::DW_EH_PE_absptr:
771 case dwarf::DW_EH_PE_signed:
Evan Cheng1be0e272011-07-15 02:09:41 +0000772 return context.getAsmInfo().getPointerSize();
Rafael Espindolabdc31672010-12-27 15:56:22 +0000773 case dwarf::DW_EH_PE_udata2:
774 case dwarf::DW_EH_PE_sdata2:
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000775 return 2;
Rafael Espindolabdc31672010-12-27 15:56:22 +0000776 case dwarf::DW_EH_PE_udata4:
777 case dwarf::DW_EH_PE_sdata4:
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000778 return 4;
Rafael Espindolabdc31672010-12-27 15:56:22 +0000779 case dwarf::DW_EH_PE_udata8:
780 case dwarf::DW_EH_PE_sdata8:
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000781 return 8;
Rafael Espindolabdc31672010-12-27 15:56:22 +0000782 }
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000783}
784
785static void EmitSymbol(MCStreamer &streamer, const MCSymbol &symbol,
Bill Wendling2541c412011-06-30 22:02:20 +0000786 unsigned symbolEncoding, const char *comment = 0) {
Rafael Espindolaa0057ca2011-04-28 21:04:39 +0000787 MCContext &context = streamer.getContext();
788 const MCAsmInfo &asmInfo = context.getAsmInfo();
789 const MCExpr *v = asmInfo.getExprForFDESymbol(&symbol,
Rafael Espindoladebd7e42011-05-01 03:50:49 +0000790 symbolEncoding,
Rafael Espindolaa0057ca2011-04-28 21:04:39 +0000791 streamer);
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000792 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Bill Wendling2541c412011-06-30 22:02:20 +0000793 if (streamer.isVerboseAsm() && comment) streamer.AddComment(comment);
Rafael Espindoladebd7e42011-05-01 03:50:49 +0000794 streamer.EmitAbsValue(v, size);
Rafael Espindolabdc31672010-12-27 15:56:22 +0000795}
796
Rafael Espindolabfa27cc2011-04-28 16:09:09 +0000797static void EmitPersonality(MCStreamer &streamer, const MCSymbol &symbol,
798 unsigned symbolEncoding) {
799 MCContext &context = streamer.getContext();
800 const MCAsmInfo &asmInfo = context.getAsmInfo();
Rafael Espindoladebd7e42011-05-01 03:50:49 +0000801 const MCExpr *v = asmInfo.getExprForPersonalitySymbol(&symbol,
802 symbolEncoding,
803 streamer);
Rafael Espindolabfa27cc2011-04-28 16:09:09 +0000804 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Rafael Espindoladebd7e42011-05-01 03:50:49 +0000805 streamer.EmitValue(v, size);
Rafael Espindolabfa27cc2011-04-28 16:09:09 +0000806}
807
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000808static const MachineLocation TranslateMachineLocation(
Evan Cheng0e6a0522011-07-18 20:57:22 +0000809 const MCRegisterInfo &MRI,
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000810 const MachineLocation &Loc) {
811 unsigned Reg = Loc.getReg() == MachineLocation::VirtualFP ?
812 MachineLocation::VirtualFP :
Evan Cheng0e6a0522011-07-18 20:57:22 +0000813 unsigned(MRI.getDwarfRegNum(Loc.getReg(), true));
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000814 const MachineLocation &NewLoc = Loc.isReg() ?
815 MachineLocation(Reg) : MachineLocation(Reg, Loc.getOffset());
816 return NewLoc;
817}
818
Rafael Espindola25f492e2011-04-12 16:12:03 +0000819namespace {
820 class FrameEmitterImpl {
821 int CFAOffset;
Rafael Espindola514cecc2011-04-28 03:26:11 +0000822 int CIENum;
Rafael Espindola5426a9e2011-05-01 04:49:54 +0000823 bool UsingCFI;
Rafael Espindola40a7dbb2011-05-10 03:54:12 +0000824 bool IsEH;
Rafael Espindolae3a0e982011-05-10 20:59:42 +0000825 const MCSymbol *SectionStart;
Rafael Espindola25f492e2011-04-12 16:12:03 +0000826 public:
Bill Wendling1424c3c2011-07-22 21:18:59 +0000827 FrameEmitterImpl(bool usingCFI, bool isEH)
828 : CFAOffset(0), CIENum(0), UsingCFI(usingCFI), IsEH(isEH),
829 SectionStart(0) {}
830
831 void setSectionStart(const MCSymbol *Label) { SectionStart = Label; }
Rafael Espindola25f492e2011-04-12 16:12:03 +0000832
Bill Wendlinge3cd13f2011-06-23 01:06:23 +0000833 /// EmitCompactUnwind - Emit the unwind information in a compact way. If
834 /// we're successful, return 'true'. Otherwise, return 'false' and it will
835 /// emit the normal CIE and FDE.
836 bool EmitCompactUnwind(MCStreamer &streamer,
837 const MCDwarfFrameInfo &frame);
838
Rafael Espindola25f492e2011-04-12 16:12:03 +0000839 const MCSymbol &EmitCIE(MCStreamer &streamer,
840 const MCSymbol *personality,
841 unsigned personalityEncoding,
842 const MCSymbol *lsda,
Rafael Espindola16d7d432012-01-23 21:51:52 +0000843 bool IsSignalFrame,
Rafael Espindola25f492e2011-04-12 16:12:03 +0000844 unsigned lsdaEncoding);
845 MCSymbol *EmitFDE(MCStreamer &streamer,
846 const MCSymbol &cieStart,
Rafael Espindola9f270da2011-05-10 03:01:39 +0000847 const MCDwarfFrameInfo &frame);
Rafael Espindola25f492e2011-04-12 16:12:03 +0000848 void EmitCFIInstructions(MCStreamer &streamer,
849 const std::vector<MCCFIInstruction> &Instrs,
850 MCSymbol *BaseLabel);
851 void EmitCFIInstruction(MCStreamer &Streamer,
852 const MCCFIInstruction &Instr);
853 };
Bill Wendling3c163cf2011-06-30 21:25:51 +0000854
855} // end anonymous namespace
856
857static void EmitEncodingByte(MCStreamer &Streamer, unsigned Encoding,
858 StringRef Prefix) {
859 if (Streamer.isVerboseAsm()) {
Benjamin Kramereb9fa662012-01-20 14:42:37 +0000860 const char *EncStr;
Bill Wendling3c163cf2011-06-30 21:25:51 +0000861 switch (Encoding) {
Benjamin Kramereb9fa662012-01-20 14:42:37 +0000862 default: EncStr = "<unknown encoding>"; break;
863 case dwarf::DW_EH_PE_absptr: EncStr = "absptr"; break;
864 case dwarf::DW_EH_PE_omit: EncStr = "omit"; break;
865 case dwarf::DW_EH_PE_pcrel: EncStr = "pcrel"; break;
866 case dwarf::DW_EH_PE_udata4: EncStr = "udata4"; break;
867 case dwarf::DW_EH_PE_udata8: EncStr = "udata8"; break;
868 case dwarf::DW_EH_PE_sdata4: EncStr = "sdata4"; break;
869 case dwarf::DW_EH_PE_sdata8: EncStr = "sdata8"; break;
870 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
871 EncStr = "pcrel udata4";
872 break;
873 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
874 EncStr = "pcrel sdata4";
875 break;
876 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
877 EncStr = "pcrel udata8";
878 break;
879 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
880 EncStr = "screl sdata8";
881 break;
Bill Wendling3c163cf2011-06-30 21:25:51 +0000882 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata4:
883 EncStr = "indirect pcrel udata4";
Benjamin Kramereb9fa662012-01-20 14:42:37 +0000884 break;
Bill Wendling3c163cf2011-06-30 21:25:51 +0000885 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata4:
886 EncStr = "indirect pcrel sdata4";
Benjamin Kramereb9fa662012-01-20 14:42:37 +0000887 break;
Bill Wendling3c163cf2011-06-30 21:25:51 +0000888 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata8:
889 EncStr = "indirect pcrel udata8";
Benjamin Kramereb9fa662012-01-20 14:42:37 +0000890 break;
Bill Wendling3c163cf2011-06-30 21:25:51 +0000891 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata8:
892 EncStr = "indirect pcrel sdata8";
Benjamin Kramereb9fa662012-01-20 14:42:37 +0000893 break;
Bill Wendling3c163cf2011-06-30 21:25:51 +0000894 }
895
896 Streamer.AddComment(Twine(Prefix) + " = " + EncStr);
897 }
898
899 Streamer.EmitIntValue(Encoding, 1);
Rafael Espindola25f492e2011-04-12 16:12:03 +0000900}
901
902void FrameEmitterImpl::EmitCFIInstruction(MCStreamer &Streamer,
903 const MCCFIInstruction &Instr) {
904 int dataAlignmentFactor = getDataAlignmentFactor(Streamer);
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000905 bool VerboseAsm = Streamer.isVerboseAsm();
Rafael Espindola25f492e2011-04-12 16:12:03 +0000906
907 switch (Instr.getOperation()) {
908 case MCCFIInstruction::Move:
909 case MCCFIInstruction::RelMove: {
910 const MachineLocation &Dst = Instr.getDestination();
911 const MachineLocation &Src = Instr.getSource();
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000912 const bool IsRelative = Instr.getOperation() == MCCFIInstruction::RelMove;
Rafael Espindola25f492e2011-04-12 16:12:03 +0000913
914 // If advancing cfa.
915 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
Rafael Espindola25f492e2011-04-12 16:12:03 +0000916 if (Src.getReg() == MachineLocation::VirtualFP) {
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000917 if (VerboseAsm) Streamer.AddComment("DW_CFA_def_cfa_offset");
Rafael Espindola25f492e2011-04-12 16:12:03 +0000918 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_offset, 1);
919 } else {
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000920 if (VerboseAsm) Streamer.AddComment("DW_CFA_def_cfa");
Rafael Espindola25f492e2011-04-12 16:12:03 +0000921 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa, 1);
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000922 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") +
923 Twine(Src.getReg()));
Rafael Espindola25f492e2011-04-12 16:12:03 +0000924 Streamer.EmitULEB128IntValue(Src.getReg());
925 }
926
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000927 if (IsRelative)
928 CFAOffset += Src.getOffset();
929 else
930 CFAOffset = -Src.getOffset();
931
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000932 if (VerboseAsm) Streamer.AddComment(Twine("Offset " + Twine(CFAOffset)));
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000933 Streamer.EmitULEB128IntValue(CFAOffset);
Rafael Espindola25f492e2011-04-12 16:12:03 +0000934 return;
935 }
936
937 if (Src.isReg() && Src.getReg() == MachineLocation::VirtualFP) {
938 assert(Dst.isReg() && "Machine move not supported yet.");
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000939 if (VerboseAsm) Streamer.AddComment("DW_CFA_def_cfa_register");
Rafael Espindola25f492e2011-04-12 16:12:03 +0000940 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_register, 1);
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000941 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Dst.getReg()));
Rafael Espindola25f492e2011-04-12 16:12:03 +0000942 Streamer.EmitULEB128IntValue(Dst.getReg());
943 return;
944 }
945
946 unsigned Reg = Src.getReg();
Rafael Espindola25f492e2011-04-12 16:12:03 +0000947 int Offset = Dst.getOffset();
948 if (IsRelative)
949 Offset -= CFAOffset;
950 Offset = Offset / dataAlignmentFactor;
951
952 if (Offset < 0) {
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000953 if (VerboseAsm) Streamer.AddComment("DW_CFA_offset_extended_sf");
Rafael Espindola25f492e2011-04-12 16:12:03 +0000954 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended_sf, 1);
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000955 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
Rafael Espindola25f492e2011-04-12 16:12:03 +0000956 Streamer.EmitULEB128IntValue(Reg);
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000957 if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
Rafael Espindola25f492e2011-04-12 16:12:03 +0000958 Streamer.EmitSLEB128IntValue(Offset);
959 } else if (Reg < 64) {
Bill Wendlinge08d4332011-06-30 23:47:40 +0000960 if (VerboseAsm) Streamer.AddComment(Twine("DW_CFA_offset + Reg(") +
961 Twine(Reg) + ")");
Rafael Espindola25f492e2011-04-12 16:12:03 +0000962 Streamer.EmitIntValue(dwarf::DW_CFA_offset + Reg, 1);
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000963 if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
Rafael Espindolaeccbad72011-04-21 23:26:40 +0000964 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola25f492e2011-04-12 16:12:03 +0000965 } else {
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000966 if (VerboseAsm) Streamer.AddComment("DW_CFA_offset_extended");
Rafael Espindola25f492e2011-04-12 16:12:03 +0000967 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended, 1);
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000968 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
Rafael Espindolaeccbad72011-04-21 23:26:40 +0000969 Streamer.EmitULEB128IntValue(Reg);
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000970 if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
Rafael Espindolaeccbad72011-04-21 23:26:40 +0000971 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola25f492e2011-04-12 16:12:03 +0000972 }
973 return;
974 }
Rafael Espindolac25680f2011-12-29 21:09:08 +0000975 case MCCFIInstruction::RememberState:
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000976 if (VerboseAsm) Streamer.AddComment("DW_CFA_remember_state");
Rafael Espindola25f492e2011-04-12 16:12:03 +0000977 Streamer.EmitIntValue(dwarf::DW_CFA_remember_state, 1);
978 return;
Rafael Espindolac25680f2011-12-29 21:09:08 +0000979 case MCCFIInstruction::RestoreState:
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000980 if (VerboseAsm) Streamer.AddComment("DW_CFA_restore_state");
Rafael Espindola25f492e2011-04-12 16:12:03 +0000981 Streamer.EmitIntValue(dwarf::DW_CFA_restore_state, 1);
982 return;
983 case MCCFIInstruction::SameValue: {
984 unsigned Reg = Instr.getDestination().getReg();
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000985 if (VerboseAsm) Streamer.AddComment("DW_CFA_same_value");
Rafael Espindola25f492e2011-04-12 16:12:03 +0000986 Streamer.EmitIntValue(dwarf::DW_CFA_same_value, 1);
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000987 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000988 Streamer.EmitULEB128IntValue(Reg);
Rafael Espindola25f492e2011-04-12 16:12:03 +0000989 return;
990 }
Rafael Espindolaed23bdb2011-12-29 21:43:03 +0000991 case MCCFIInstruction::Restore: {
992 unsigned Reg = Instr.getDestination().getReg();
993 if (VerboseAsm) {
994 Streamer.AddComment("DW_CFA_restore");
995 Streamer.AddComment(Twine("Reg ") + Twine(Reg));
996 }
997 Streamer.EmitIntValue(dwarf::DW_CFA_restore | Reg, 1);
998 return;
999 }
Rafael Espindola6f0b1812011-12-29 20:24:47 +00001000 case MCCFIInstruction::Escape:
1001 if (VerboseAsm) Streamer.AddComment("Escape bytes");
1002 Streamer.EmitBytes(Instr.getValues(), 0);
1003 return;
Rafael Espindola25f492e2011-04-12 16:12:03 +00001004 }
1005 llvm_unreachable("Unhandled case in switch");
1006}
1007
1008/// EmitFrameMoves - Emit frame instructions to describe the layout of the
1009/// frame.
1010void FrameEmitterImpl::EmitCFIInstructions(MCStreamer &streamer,
1011 const std::vector<MCCFIInstruction> &Instrs,
1012 MCSymbol *BaseLabel) {
1013 for (unsigned i = 0, N = Instrs.size(); i < N; ++i) {
1014 const MCCFIInstruction &Instr = Instrs[i];
1015 MCSymbol *Label = Instr.getLabel();
1016 // Throw out move if the label is invalid.
1017 if (Label && !Label->isDefined()) continue; // Not emitted, in dead code.
1018
1019 // Advance row if new location.
1020 if (BaseLabel && Label) {
1021 MCSymbol *ThisSym = Label;
1022 if (ThisSym != BaseLabel) {
Bill Wendlingd221cd62011-06-30 22:35:49 +00001023 if (streamer.isVerboseAsm()) streamer.AddComment("DW_CFA_advance_loc4");
Rafael Espindola25f492e2011-04-12 16:12:03 +00001024 streamer.EmitDwarfAdvanceFrameAddr(BaseLabel, ThisSym);
1025 BaseLabel = ThisSym;
1026 }
1027 }
1028
1029 EmitCFIInstruction(streamer, Instr);
1030 }
1031}
1032
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001033/// EmitCompactUnwind - Emit the unwind information in a compact way. If we're
1034/// successful, return 'true'. Otherwise, return 'false' and it will emit the
1035/// normal CIE and FDE.
1036bool FrameEmitterImpl::EmitCompactUnwind(MCStreamer &Streamer,
1037 const MCDwarfFrameInfo &Frame) {
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001038 MCContext &Context = Streamer.getContext();
Evan Chenge76a33b2011-07-20 05:58:47 +00001039 const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
Bill Wendling3c163cf2011-06-30 21:25:51 +00001040 bool VerboseAsm = Streamer.isVerboseAsm();
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001041
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001042 // range-start range-length compact-unwind-enc personality-func lsda
1043 // _foo LfooEnd-_foo 0x00000023 0 0
1044 // _bar LbarEnd-_bar 0x00000025 __gxx_personality except_tab1
1045 //
1046 // .section __LD,__compact_unwind,regular,debug
1047 //
1048 // # compact unwind for _foo
1049 // .quad _foo
1050 // .set L1,LfooEnd-_foo
1051 // .long L1
1052 // .long 0x01010001
1053 // .quad 0
1054 // .quad 0
1055 //
1056 // # compact unwind for _bar
1057 // .quad _bar
1058 // .set L2,LbarEnd-_bar
1059 // .long L2
1060 // .long 0x01020011
1061 // .quad __gxx_personality
1062 // .quad except_tab1
1063
Bill Wendlinge52e3f22011-07-19 00:06:12 +00001064 uint32_t Encoding = Frame.CompactUnwindEncoding;
Bill Wendling6a6b8c32011-07-07 00:54:13 +00001065 if (!Encoding) return false;
1066
Bill Wendling4bb5b032011-07-14 23:34:45 +00001067 // The encoding needs to know we have an LSDA.
1068 if (Frame.Lsda)
1069 Encoding |= 0x40000000;
1070
Evan Chenge76a33b2011-07-20 05:58:47 +00001071 Streamer.SwitchSection(MOFI->getCompactUnwindSection());
Bill Wendling3c163cf2011-06-30 21:25:51 +00001072
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001073 // Range Start
Evan Cheng203576a2011-07-20 19:50:42 +00001074 unsigned FDEEncoding = MOFI->getFDEEncoding(UsingCFI);
Bill Wendling9056e902011-06-29 23:49:12 +00001075 unsigned Size = getSizeForEncoding(Streamer, FDEEncoding);
Bill Wendling3c163cf2011-06-30 21:25:51 +00001076 if (VerboseAsm) Streamer.AddComment("Range Start");
Bill Wendling9056e902011-06-29 23:49:12 +00001077 Streamer.EmitSymbolValue(Frame.Function, Size);
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001078
1079 // Range Length
1080 const MCExpr *Range = MakeStartMinusEndExpr(Streamer, *Frame.Begin,
1081 *Frame.End, 0);
Bill Wendling3c163cf2011-06-30 21:25:51 +00001082 if (VerboseAsm) Streamer.AddComment("Range Length");
Bill Wendling9287a6e2011-06-30 00:30:52 +00001083 Streamer.EmitAbsValue(Range, 4);
1084
Bill Wendling9287a6e2011-06-30 00:30:52 +00001085 // Compact Encoding
Bill Wendling9287a6e2011-06-30 00:30:52 +00001086 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_udata4);
Benjamin Kramer70be28a2011-11-07 21:00:59 +00001087 if (VerboseAsm) Streamer.AddComment("Compact Unwind Encoding: 0x" +
1088 Twine::utohexstr(Encoding));
Bill Wendling9287a6e2011-06-30 00:30:52 +00001089 Streamer.EmitIntValue(Encoding, Size);
1090
Bill Wendlinge52e3f22011-07-19 00:06:12 +00001091
Bill Wendling9056e902011-06-29 23:49:12 +00001092 // Personality Function
Bill Wendling4bb5b032011-07-14 23:34:45 +00001093 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_absptr);
Bill Wendling3c163cf2011-06-30 21:25:51 +00001094 if (VerboseAsm) Streamer.AddComment("Personality Function");
Bill Wendling4498d392011-06-29 23:53:16 +00001095 if (Frame.Personality)
Bill Wendling9056e902011-06-29 23:49:12 +00001096 Streamer.EmitSymbolValue(Frame.Personality, Size);
Bill Wendling4498d392011-06-29 23:53:16 +00001097 else
1098 Streamer.EmitIntValue(0, Size); // No personality fn
Bill Wendling9056e902011-06-29 23:49:12 +00001099
1100 // LSDA
Bill Wendling4498d392011-06-29 23:53:16 +00001101 Size = getSizeForEncoding(Streamer, Frame.LsdaEncoding);
Bill Wendling3c163cf2011-06-30 21:25:51 +00001102 if (VerboseAsm) Streamer.AddComment("LSDA");
Bill Wendling4498d392011-06-29 23:53:16 +00001103 if (Frame.Lsda)
Bill Wendling9056e902011-06-29 23:49:12 +00001104 Streamer.EmitSymbolValue(Frame.Lsda, Size);
Bill Wendling4498d392011-06-29 23:53:16 +00001105 else
1106 Streamer.EmitIntValue(0, Size); // No LSDA
Bill Wendling9056e902011-06-29 23:49:12 +00001107
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001108 return true;
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001109}
1110
Rafael Espindola25f492e2011-04-12 16:12:03 +00001111const MCSymbol &FrameEmitterImpl::EmitCIE(MCStreamer &streamer,
1112 const MCSymbol *personality,
1113 unsigned personalityEncoding,
1114 const MCSymbol *lsda,
Rafael Espindola16d7d432012-01-23 21:51:52 +00001115 bool IsSignalFrame,
Rafael Espindola25f492e2011-04-12 16:12:03 +00001116 unsigned lsdaEncoding) {
Rafael Espindola89b93722010-12-10 07:39:47 +00001117 MCContext &context = streamer.getContext();
Evan Cheng0e6a0522011-07-18 20:57:22 +00001118 const MCRegisterInfo &MRI = context.getRegisterInfo();
Evan Chenge76a33b2011-07-20 05:58:47 +00001119 const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
Bill Wendling3c163cf2011-06-30 21:25:51 +00001120 bool verboseAsm = streamer.isVerboseAsm();
Rafael Espindola514cecc2011-04-28 03:26:11 +00001121
1122 MCSymbol *sectionStart;
Evan Chenge76a33b2011-07-20 05:58:47 +00001123 if (MOFI->isFunctionEHFrameSymbolPrivate() || !IsEH)
Rafael Espindola514cecc2011-04-28 03:26:11 +00001124 sectionStart = context.CreateTempSymbol();
1125 else
1126 sectionStart = context.GetOrCreateSymbol(Twine("EH_frame") + Twine(CIENum));
1127
Bill Wendling3c163cf2011-06-30 21:25:51 +00001128 streamer.EmitLabel(sectionStart);
Rafael Espindola514cecc2011-04-28 03:26:11 +00001129 CIENum++;
1130
Bill Wendling6a6b8c32011-07-07 00:54:13 +00001131 MCSymbol *sectionEnd = context.CreateTempSymbol();
Rafael Espindola89b93722010-12-10 07:39:47 +00001132
1133 // Length
1134 const MCExpr *Length = MakeStartMinusEndExpr(streamer, *sectionStart,
1135 *sectionEnd, 4);
Bill Wendling3c163cf2011-06-30 21:25:51 +00001136 if (verboseAsm) streamer.AddComment("CIE Length");
Rafael Espindola9266cc42011-04-27 01:43:49 +00001137 streamer.EmitAbsValue(Length, 4);
Rafael Espindola89b93722010-12-10 07:39:47 +00001138
1139 // CIE ID
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001140 unsigned CIE_ID = IsEH ? 0 : -1;
Bill Wendling3c163cf2011-06-30 21:25:51 +00001141 if (verboseAsm) streamer.AddComment("CIE ID Tag");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001142 streamer.EmitIntValue(CIE_ID, 4);
Rafael Espindola89b93722010-12-10 07:39:47 +00001143
1144 // Version
Bill Wendling3c163cf2011-06-30 21:25:51 +00001145 if (verboseAsm) streamer.AddComment("DW_CIE_VERSION");
Rafael Espindola89b93722010-12-10 07:39:47 +00001146 streamer.EmitIntValue(dwarf::DW_CIE_VERSION, 1);
1147
1148 // Augmentation String
1149 SmallString<8> Augmentation;
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001150 if (IsEH) {
Bill Wendling3c163cf2011-06-30 21:25:51 +00001151 if (verboseAsm) streamer.AddComment("CIE Augmentation");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001152 Augmentation += "z";
1153 if (personality)
1154 Augmentation += "P";
1155 if (lsda)
1156 Augmentation += "L";
1157 Augmentation += "R";
Rafael Espindola16d7d432012-01-23 21:51:52 +00001158 if (IsSignalFrame)
1159 Augmentation += "S";
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001160 streamer.EmitBytes(Augmentation.str(), 0);
1161 }
Rafael Espindola89b93722010-12-10 07:39:47 +00001162 streamer.EmitIntValue(0, 1);
1163
1164 // Code Alignment Factor
Bill Wendling3c163cf2011-06-30 21:25:51 +00001165 if (verboseAsm) streamer.AddComment("CIE Code Alignment Factor");
Rafael Espindola89b93722010-12-10 07:39:47 +00001166 streamer.EmitULEB128IntValue(1);
1167
1168 // Data Alignment Factor
Bill Wendling3c163cf2011-06-30 21:25:51 +00001169 if (verboseAsm) streamer.AddComment("CIE Data Alignment Factor");
Rafael Espindola89b93722010-12-10 07:39:47 +00001170 streamer.EmitSLEB128IntValue(getDataAlignmentFactor(streamer));
1171
1172 // Return Address Register
Bill Wendling3c163cf2011-06-30 21:25:51 +00001173 if (verboseAsm) streamer.AddComment("CIE Return Address Column");
Evan Cheng0e6a0522011-07-18 20:57:22 +00001174 streamer.EmitULEB128IntValue(MRI.getDwarfRegNum(MRI.getRARegister(), true));
Rafael Espindola89b93722010-12-10 07:39:47 +00001175
1176 // Augmentation Data Length (optional)
Rafael Espindola1f6c8752011-04-29 21:50:57 +00001177
1178 unsigned augmentationLength = 0;
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001179 if (IsEH) {
1180 if (personality) {
1181 // Personality Encoding
1182 augmentationLength += 1;
1183 // Personality
1184 augmentationLength += getSizeForEncoding(streamer, personalityEncoding);
1185 }
1186 if (lsda)
1187 augmentationLength += 1;
1188 // Encoding of the FDE pointers
Rafael Espindola1f6c8752011-04-29 21:50:57 +00001189 augmentationLength += 1;
Rafael Espindola1f6c8752011-04-29 21:50:57 +00001190
Bill Wendling3c163cf2011-06-30 21:25:51 +00001191 if (verboseAsm) streamer.AddComment("Augmentation Size");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001192 streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola89b93722010-12-10 07:39:47 +00001193
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001194 // Augmentation Data (optional)
1195 if (personality) {
1196 // Personality Encoding
Bill Wendling3c163cf2011-06-30 21:25:51 +00001197 EmitEncodingByte(streamer, personalityEncoding,
1198 "Personality Encoding");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001199 // Personality
Bill Wendling3c163cf2011-06-30 21:25:51 +00001200 if (verboseAsm) streamer.AddComment("Personality");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001201 EmitPersonality(streamer, *personality, personalityEncoding);
1202 }
Bill Wendling3c163cf2011-06-30 21:25:51 +00001203
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001204 if (lsda)
Bill Wendling3c163cf2011-06-30 21:25:51 +00001205 EmitEncodingByte(streamer, lsdaEncoding, "LSDA Encoding");
1206
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001207 // Encoding of the FDE pointers
Evan Cheng203576a2011-07-20 19:50:42 +00001208 EmitEncodingByte(streamer, MOFI->getFDEEncoding(UsingCFI),
Bill Wendling3c163cf2011-06-30 21:25:51 +00001209 "FDE Encoding");
Rafael Espindolabdc31672010-12-27 15:56:22 +00001210 }
Rafael Espindola89b93722010-12-10 07:39:47 +00001211
1212 // Initial Instructions
1213
Evan Cheng2d286172011-07-18 22:29:13 +00001214 const MCAsmInfo &MAI = context.getAsmInfo();
1215 const std::vector<MachineMove> &Moves = MAI.getInitialFrameState();
Rafael Espindolafe024d02010-12-28 18:36:23 +00001216 std::vector<MCCFIInstruction> Instructions;
Rafael Espindola89b93722010-12-10 07:39:47 +00001217
Rafael Espindolafe024d02010-12-28 18:36:23 +00001218 for (int i = 0, n = Moves.size(); i != n; ++i) {
Rafael Espindolab40a71f2010-12-29 01:42:56 +00001219 MCSymbol *Label = Moves[i].getLabel();
1220 const MachineLocation &Dst =
Evan Cheng0e6a0522011-07-18 20:57:22 +00001221 TranslateMachineLocation(MRI, Moves[i].getDestination());
Rafael Espindolab40a71f2010-12-29 01:42:56 +00001222 const MachineLocation &Src =
Evan Cheng0e6a0522011-07-18 20:57:22 +00001223 TranslateMachineLocation(MRI, Moves[i].getSource());
Rafael Espindolab40a71f2010-12-29 01:42:56 +00001224 MCCFIInstruction Inst(Label, Dst, Src);
Rafael Espindolafe024d02010-12-28 18:36:23 +00001225 Instructions.push_back(Inst);
1226 }
1227
Rafael Espindolab40a71f2010-12-29 01:42:56 +00001228 EmitCFIInstructions(streamer, Instructions, NULL);
Rafael Espindola89b93722010-12-10 07:39:47 +00001229
1230 // Padding
Evan Cheng1be0e272011-07-15 02:09:41 +00001231 streamer.EmitValueToAlignment(IsEH
1232 ? 4 : context.getAsmInfo().getPointerSize());
Rafael Espindola89b93722010-12-10 07:39:47 +00001233
1234 streamer.EmitLabel(sectionEnd);
1235 return *sectionStart;
1236}
1237
Rafael Espindola25f492e2011-04-12 16:12:03 +00001238MCSymbol *FrameEmitterImpl::EmitFDE(MCStreamer &streamer,
1239 const MCSymbol &cieStart,
Rafael Espindola9f270da2011-05-10 03:01:39 +00001240 const MCDwarfFrameInfo &frame) {
Rafael Espindolabdc31672010-12-27 15:56:22 +00001241 MCContext &context = streamer.getContext();
1242 MCSymbol *fdeStart = context.CreateTempSymbol();
1243 MCSymbol *fdeEnd = context.CreateTempSymbol();
Evan Chenge76a33b2011-07-20 05:58:47 +00001244 const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
Bill Wendling2541c412011-06-30 22:02:20 +00001245 bool verboseAsm = streamer.isVerboseAsm();
Rafael Espindola89b93722010-12-10 07:39:47 +00001246
Evan Cheng5fbe5e72011-08-24 22:31:37 +00001247 if (IsEH && frame.Function && !MOFI->isFunctionEHFrameSymbolPrivate()) {
Bill Wendling2541c412011-06-30 22:02:20 +00001248 MCSymbol *EHSym =
1249 context.GetOrCreateSymbol(frame.Function->getName() + Twine(".eh"));
Rafael Espindola8bca4102011-04-28 12:50:37 +00001250 streamer.EmitEHSymAttributes(frame.Function, EHSym);
Rafael Espindolaa8cfb872011-04-28 02:46:42 +00001251 streamer.EmitLabel(EHSym);
1252 }
1253
Rafael Espindola89b93722010-12-10 07:39:47 +00001254 // Length
1255 const MCExpr *Length = MakeStartMinusEndExpr(streamer, *fdeStart, *fdeEnd, 0);
Bill Wendling2541c412011-06-30 22:02:20 +00001256 if (verboseAsm) streamer.AddComment("FDE Length");
Rafael Espindola9266cc42011-04-27 01:43:49 +00001257 streamer.EmitAbsValue(Length, 4);
Rafael Espindola89b93722010-12-10 07:39:47 +00001258
1259 streamer.EmitLabel(fdeStart);
Rafael Espindolae3a0e982011-05-10 20:59:42 +00001260
Rafael Espindola89b93722010-12-10 07:39:47 +00001261 // CIE Pointer
Rafael Espindolae3a0e982011-05-10 20:59:42 +00001262 const MCAsmInfo &asmInfo = context.getAsmInfo();
Rafael Espindola0d450dc2011-05-10 15:20:23 +00001263 if (IsEH) {
1264 const MCExpr *offset = MakeStartMinusEndExpr(streamer, cieStart, *fdeStart,
1265 0);
Bill Wendling2541c412011-06-30 22:02:20 +00001266 if (verboseAsm) streamer.AddComment("FDE CIE Offset");
Rafael Espindola0d450dc2011-05-10 15:20:23 +00001267 streamer.EmitAbsValue(offset, 4);
Rafael Espindolae3a0e982011-05-10 20:59:42 +00001268 } else if (!asmInfo.doesDwarfRequireRelocationForSectionOffset()) {
1269 const MCExpr *offset = MakeStartMinusEndExpr(streamer, *SectionStart,
1270 cieStart, 0);
1271 streamer.EmitAbsValue(offset, 4);
Rafael Espindola0d450dc2011-05-10 15:20:23 +00001272 } else {
1273 streamer.EmitSymbolValue(&cieStart, 4);
1274 }
Bill Wendling2541c412011-06-30 22:02:20 +00001275
Evan Cheng203576a2011-07-20 19:50:42 +00001276 unsigned fdeEncoding = MOFI->getFDEEncoding(UsingCFI);
Rafael Espindolaabf9af62011-04-22 00:08:43 +00001277 unsigned size = getSizeForEncoding(streamer, fdeEncoding);
Rafael Espindola89b93722010-12-10 07:39:47 +00001278
1279 // PC Begin
Rafael Espindola9989ef42011-05-10 22:28:35 +00001280 unsigned PCBeginEncoding = IsEH ? fdeEncoding :
1281 (unsigned)dwarf::DW_EH_PE_absptr;
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001282 unsigned PCBeginSize = getSizeForEncoding(streamer, PCBeginEncoding);
Bill Wendling2541c412011-06-30 22:02:20 +00001283 EmitSymbol(streamer, *frame.Begin, PCBeginEncoding, "FDE initial location");
Rafael Espindola89b93722010-12-10 07:39:47 +00001284
1285 // PC Range
1286 const MCExpr *Range = MakeStartMinusEndExpr(streamer, *frame.Begin,
1287 *frame.End, 0);
Bill Wendling2541c412011-06-30 22:02:20 +00001288 if (verboseAsm) streamer.AddComment("FDE address range");
Rafael Espindola9266cc42011-04-27 01:43:49 +00001289 streamer.EmitAbsValue(Range, size);
Rafael Espindola89b93722010-12-10 07:39:47 +00001290
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001291 if (IsEH) {
1292 // Augmentation Data Length
1293 unsigned augmentationLength = 0;
Rafael Espindola1f6c8752011-04-29 21:50:57 +00001294
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001295 if (frame.Lsda)
1296 augmentationLength += getSizeForEncoding(streamer, frame.LsdaEncoding);
Rafael Espindola1f6c8752011-04-29 21:50:57 +00001297
Bill Wendling2541c412011-06-30 22:02:20 +00001298 if (verboseAsm) streamer.AddComment("Augmentation size");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001299 streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola89b93722010-12-10 07:39:47 +00001300
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001301 // Augmentation Data
1302 if (frame.Lsda)
Bill Wendling2541c412011-06-30 22:02:20 +00001303 EmitSymbol(streamer, *frame.Lsda, frame.LsdaEncoding,
1304 "Language Specific Data Area");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001305 }
Rafael Espindola4892dff2011-04-29 03:06:29 +00001306
Rafael Espindola89b93722010-12-10 07:39:47 +00001307 // Call Frame Instructions
1308
Rafael Espindolab40a71f2010-12-29 01:42:56 +00001309 EmitCFIInstructions(streamer, frame.Instructions, frame.Begin);
Rafael Espindola5bba0842010-12-28 04:15:37 +00001310
Rafael Espindola89b93722010-12-10 07:39:47 +00001311 // Padding
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001312 streamer.EmitValueToAlignment(PCBeginSize);
Rafael Espindoladfe125c2010-12-17 00:28:02 +00001313
1314 return fdeEnd;
Rafael Espindola89b93722010-12-10 07:39:47 +00001315}
1316
Benjamin Kramer19282362010-12-30 22:34:44 +00001317namespace {
1318 struct CIEKey {
Rafael Espindola16d7d432012-01-23 21:51:52 +00001319 static const CIEKey getEmptyKey() { return CIEKey(0, 0, -1, false); }
1320 static const CIEKey getTombstoneKey() { return CIEKey(0, -1, 0, false); }
Rafael Espindolabdc31672010-12-27 15:56:22 +00001321
Benjamin Kramer19282362010-12-30 22:34:44 +00001322 CIEKey(const MCSymbol* Personality_, unsigned PersonalityEncoding_,
Rafael Espindola16d7d432012-01-23 21:51:52 +00001323 unsigned LsdaEncoding_, bool IsSignalFrame_) :
1324 Personality(Personality_), PersonalityEncoding(PersonalityEncoding_),
1325 LsdaEncoding(LsdaEncoding_), IsSignalFrame(IsSignalFrame_) {
Benjamin Kramer19282362010-12-30 22:34:44 +00001326 }
1327 const MCSymbol* Personality;
1328 unsigned PersonalityEncoding;
1329 unsigned LsdaEncoding;
Rafael Espindola16d7d432012-01-23 21:51:52 +00001330 bool IsSignalFrame;
Benjamin Kramer19282362010-12-30 22:34:44 +00001331 };
1332}
Rafael Espindolabdc31672010-12-27 15:56:22 +00001333
1334namespace llvm {
1335 template <>
1336 struct DenseMapInfo<CIEKey> {
1337 static CIEKey getEmptyKey() {
Benjamin Kramer19282362010-12-30 22:34:44 +00001338 return CIEKey::getEmptyKey();
Rafael Espindolabdc31672010-12-27 15:56:22 +00001339 }
1340 static CIEKey getTombstoneKey() {
Benjamin Kramer19282362010-12-30 22:34:44 +00001341 return CIEKey::getTombstoneKey();
Rafael Espindolabdc31672010-12-27 15:56:22 +00001342 }
1343 static unsigned getHashValue(const CIEKey &Key) {
1344 FoldingSetNodeID ID;
1345 ID.AddPointer(Key.Personality);
1346 ID.AddInteger(Key.PersonalityEncoding);
1347 ID.AddInteger(Key.LsdaEncoding);
Rafael Espindola16d7d432012-01-23 21:51:52 +00001348 ID.AddBoolean(Key.IsSignalFrame);
Rafael Espindolabdc31672010-12-27 15:56:22 +00001349 return ID.ComputeHash();
1350 }
1351 static bool isEqual(const CIEKey &LHS,
1352 const CIEKey &RHS) {
1353 return LHS.Personality == RHS.Personality &&
1354 LHS.PersonalityEncoding == RHS.PersonalityEncoding &&
Rafael Espindola16d7d432012-01-23 21:51:52 +00001355 LHS.LsdaEncoding == RHS.LsdaEncoding &&
1356 LHS.IsSignalFrame == RHS.IsSignalFrame;
Rafael Espindolabdc31672010-12-27 15:56:22 +00001357 }
1358 };
1359}
1360
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001361void MCDwarfFrameEmitter::Emit(MCStreamer &Streamer,
1362 bool UsingCFI,
1363 bool IsEH) {
1364 MCContext &Context = Streamer.getContext();
Evan Chenge76a33b2011-07-20 05:58:47 +00001365 MCObjectFileInfo *MOFI =
1366 const_cast<MCObjectFileInfo*>(Context.getObjectFileInfo());
Bill Wendling1424c3c2011-07-22 21:18:59 +00001367 FrameEmitterImpl Emitter(UsingCFI, IsEH);
Bill Wendlingd8ab8e92011-09-06 18:37:11 +00001368 ArrayRef<MCDwarfFrameInfo> FrameArray = Streamer.getFrameInfos();
Bill Wendling1424c3c2011-07-22 21:18:59 +00001369
Bill Wendlingd8ab8e92011-09-06 18:37:11 +00001370 // Emit the compact unwind info if available.
Bill Wendling7a13b722011-12-15 00:14:24 +00001371 if (IsEH && MOFI->getCompactUnwindSection())
Bill Wendling1424c3c2011-07-22 21:18:59 +00001372 for (unsigned i = 0, n = Streamer.getNumFrameInfos(); i < n; ++i) {
1373 const MCDwarfFrameInfo &Frame = Streamer.getFrameInfo(i);
Bill Wendlinga2ff3e22011-11-08 22:23:43 +00001374 if (Frame.CompactUnwindEncoding)
Bill Wendlingd8ab8e92011-09-06 18:37:11 +00001375 Emitter.EmitCompactUnwind(Streamer, Frame);
Bill Wendling1424c3c2011-07-22 21:18:59 +00001376 }
1377
Evan Chenge76a33b2011-07-20 05:58:47 +00001378 const MCSection &Section = IsEH ? *MOFI->getEHFrameSection() :
1379 *MOFI->getDwarfFrameSection();
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001380 Streamer.SwitchSection(&Section);
1381 MCSymbol *SectionStart = Context.CreateTempSymbol();
1382 Streamer.EmitLabel(SectionStart);
Bill Wendling1424c3c2011-07-22 21:18:59 +00001383 Emitter.setSectionStart(SectionStart);
Rafael Espindola90998132011-04-29 02:42:28 +00001384
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001385 MCSymbol *FDEEnd = NULL;
Rafael Espindolabdc31672010-12-27 15:56:22 +00001386 DenseMap<CIEKey, const MCSymbol*> CIEStarts;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00001387
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001388 const MCSymbol *DummyDebugKey = NULL;
Bill Wendling1424c3c2011-07-22 21:18:59 +00001389 for (unsigned i = 0, n = FrameArray.size(); i < n; ++i) {
1390 const MCDwarfFrameInfo &Frame = FrameArray[i];
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001391 CIEKey Key(Frame.Personality, Frame.PersonalityEncoding,
Rafael Espindola16d7d432012-01-23 21:51:52 +00001392 Frame.LsdaEncoding, Frame.IsSignalFrame);
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001393 const MCSymbol *&CIEStart = IsEH ? CIEStarts[Key] : DummyDebugKey;
1394 if (!CIEStart)
1395 CIEStart = &Emitter.EmitCIE(Streamer, Frame.Personality,
1396 Frame.PersonalityEncoding, Frame.Lsda,
Rafael Espindola16d7d432012-01-23 21:51:52 +00001397 Frame.IsSignalFrame,
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001398 Frame.LsdaEncoding);
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001399
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001400 FDEEnd = Emitter.EmitFDE(Streamer, *CIEStart, Frame);
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001401
Rafael Espindoladfe125c2010-12-17 00:28:02 +00001402 if (i != n - 1)
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001403 Streamer.EmitLabel(FDEEnd);
Rafael Espindoladfe125c2010-12-17 00:28:02 +00001404 }
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00001405
Evan Cheng1be0e272011-07-15 02:09:41 +00001406 Streamer.EmitValueToAlignment(Context.getAsmInfo().getPointerSize());
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001407 if (FDEEnd)
1408 Streamer.EmitLabel(FDEEnd);
Rafael Espindola89b93722010-12-10 07:39:47 +00001409}
Rafael Espindola245a1e22010-12-28 05:39:27 +00001410
1411void MCDwarfFrameEmitter::EmitAdvanceLoc(MCStreamer &Streamer,
1412 uint64_t AddrDelta) {
1413 SmallString<256> Tmp;
1414 raw_svector_ostream OS(Tmp);
Rafael Espindola4eafe102011-05-08 14:35:21 +00001415 MCDwarfFrameEmitter::EncodeAdvanceLoc(AddrDelta, OS);
Rafael Espindola245a1e22010-12-28 05:39:27 +00001416 Streamer.EmitBytes(OS.str(), /*AddrSpace=*/0);
1417}
1418
1419void MCDwarfFrameEmitter::EncodeAdvanceLoc(uint64_t AddrDelta,
Rafael Espindola4eafe102011-05-08 14:35:21 +00001420 raw_ostream &OS) {
Rafael Espindola245a1e22010-12-28 05:39:27 +00001421 // FIXME: Assumes the code alignment factor is 1.
Rafael Espindola3b78cdc2010-12-28 23:38:03 +00001422 if (AddrDelta == 0) {
Rafael Espindola4eafe102011-05-08 14:35:21 +00001423 } else if (isUIntN(6, AddrDelta)) {
Rafael Espindola245a1e22010-12-28 05:39:27 +00001424 uint8_t Opcode = dwarf::DW_CFA_advance_loc | AddrDelta;
1425 OS << Opcode;
Rafael Espindola4eafe102011-05-08 14:35:21 +00001426 } else if (isUInt<8>(AddrDelta)) {
Rafael Espindola245a1e22010-12-28 05:39:27 +00001427 OS << uint8_t(dwarf::DW_CFA_advance_loc1);
1428 OS << uint8_t(AddrDelta);
Rafael Espindola4eafe102011-05-08 14:35:21 +00001429 } else if (isUInt<16>(AddrDelta)) {
Rafael Espindolaa7e45052010-12-29 02:30:49 +00001430 // FIXME: check what is the correct behavior on a big endian machine.
Rafael Espindola245a1e22010-12-28 05:39:27 +00001431 OS << uint8_t(dwarf::DW_CFA_advance_loc2);
Rafael Espindolaa7e45052010-12-29 02:30:49 +00001432 OS << uint8_t( AddrDelta & 0xff);
1433 OS << uint8_t((AddrDelta >> 8) & 0xff);
Rafael Espindola245a1e22010-12-28 05:39:27 +00001434 } else {
Rafael Espindolaa7e45052010-12-29 02:30:49 +00001435 // FIXME: check what is the correct behavior on a big endian machine.
Rafael Espindola245a1e22010-12-28 05:39:27 +00001436 assert(isUInt<32>(AddrDelta));
1437 OS << uint8_t(dwarf::DW_CFA_advance_loc4);
Rafael Espindolaa7e45052010-12-29 02:30:49 +00001438 OS << uint8_t( AddrDelta & 0xff);
1439 OS << uint8_t((AddrDelta >> 8) & 0xff);
1440 OS << uint8_t((AddrDelta >> 16) & 0xff);
1441 OS << uint8_t((AddrDelta >> 24) & 0xff);
1442
Rafael Espindola245a1e22010-12-28 05:39:27 +00001443 }
1444}