blob: 75eaf807ec445426bbd2ec14c18ea6dafbc4fb8c [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"
Benjamin Kramer74849202012-04-11 14:06:39 +000024#include "llvm/ADT/Hashing.h"
Bill Wendling2b2dc7c2011-07-06 22:52:32 +000025#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
Jim Grosbach2684d9e2012-05-11 01:41:30 +000039// "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 .
Jim Grosbach2684d9e2012-05-11 01:41:30 +0000108//
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
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +0000201 // Switch back 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 Espindola489d6792012-02-28 21:13:05 +0000212const MCSymbol *MCDwarfFileTable::Emit(MCStreamer *MCOS) {
Rafael Espindola89b93722010-12-10 07:39:47 +0000213 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
Jim Grosbach2684d9e2012-05-11 01:41:30 +0000313 // it requires:
Rafael Espindolaa8de83c2010-12-03 23:36:59 +0000314 // 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);
Rafael Espindola489d6792012-02-28 21:13:05 +0000325
326 return LineStartSym;
Kevin Enderbyc0957932010-09-30 16:52:03 +0000327}
328
Kevin Enderbyc0957932010-09-30 16:52:03 +0000329/// Utility function to write the encoding to an object writer.
330void MCDwarfLineAddr::Write(MCObjectWriter *OW, int64_t LineDelta,
331 uint64_t AddrDelta) {
332 SmallString<256> Tmp;
333 raw_svector_ostream OS(Tmp);
334 MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OS);
335 OW->WriteBytes(OS.str());
336}
337
338/// Utility function to emit the encoding to a streamer.
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000339void MCDwarfLineAddr::Emit(MCStreamer *MCOS, int64_t LineDelta,
Kevin Enderbyc0957932010-09-30 16:52:03 +0000340 uint64_t AddrDelta) {
341 SmallString<256> Tmp;
342 raw_svector_ostream OS(Tmp);
343 MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OS);
344 MCOS->EmitBytes(OS.str(), /*AddrSpace=*/0);
345}
346
347/// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
348void MCDwarfLineAddr::Encode(int64_t LineDelta, uint64_t AddrDelta,
349 raw_ostream &OS) {
350 uint64_t Temp, Opcode;
351 bool NeedCopy = false;
352
353 // Scale the address delta by the minimum instruction length.
354 AddrDelta = ScaleAddrDelta(AddrDelta);
355
356 // A LineDelta of INT64_MAX is a signal that this is actually a
Jim Grosbach2684d9e2012-05-11 01:41:30 +0000357 // DW_LNE_end_sequence. We cannot use special opcodes here, since we want the
Kevin Enderbyc0957932010-09-30 16:52:03 +0000358 // end_sequence to emit the matrix entry.
359 if (LineDelta == INT64_MAX) {
360 if (AddrDelta == MAX_SPECIAL_ADDR_DELTA)
361 OS << char(dwarf::DW_LNS_const_add_pc);
362 else {
363 OS << char(dwarf::DW_LNS_advance_pc);
Benjamin Kramerdcf0e0c2011-06-18 14:42:47 +0000364 MCObjectWriter::EncodeULEB128(AddrDelta, OS);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000365 }
366 OS << char(dwarf::DW_LNS_extended_op);
367 OS << char(1);
368 OS << char(dwarf::DW_LNE_end_sequence);
369 return;
370 }
371
372 // Bias the line delta by the base.
373 Temp = LineDelta - DWARF2_LINE_BASE;
374
375 // If the line increment is out of range of a special opcode, we must encode
376 // it with DW_LNS_advance_line.
377 if (Temp >= DWARF2_LINE_RANGE) {
378 OS << char(dwarf::DW_LNS_advance_line);
Benjamin Kramer2dd42392011-11-09 13:19:15 +0000379 MCObjectWriter::EncodeSLEB128(LineDelta, OS);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000380
381 LineDelta = 0;
382 Temp = 0 - DWARF2_LINE_BASE;
383 NeedCopy = true;
384 }
385
386 // Use DW_LNS_copy instead of a "line +0, addr +0" special opcode.
387 if (LineDelta == 0 && AddrDelta == 0) {
388 OS << char(dwarf::DW_LNS_copy);
389 return;
390 }
391
392 // Bias the opcode by the special opcode base.
393 Temp += DWARF2_LINE_OPCODE_BASE;
394
395 // Avoid overflow when addr_delta is large.
396 if (AddrDelta < 256 + MAX_SPECIAL_ADDR_DELTA) {
397 // Try using a special opcode.
398 Opcode = Temp + AddrDelta * DWARF2_LINE_RANGE;
399 if (Opcode <= 255) {
400 OS << char(Opcode);
401 return;
402 }
403
404 // Try using DW_LNS_const_add_pc followed by special op.
405 Opcode = Temp + (AddrDelta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
406 if (Opcode <= 255) {
407 OS << char(dwarf::DW_LNS_const_add_pc);
408 OS << char(Opcode);
409 return;
410 }
411 }
412
413 // Otherwise use DW_LNS_advance_pc.
414 OS << char(dwarf::DW_LNS_advance_pc);
Benjamin Kramer2dd42392011-11-09 13:19:15 +0000415 MCObjectWriter::EncodeULEB128(AddrDelta, OS);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000416
417 if (NeedCopy)
418 OS << char(dwarf::DW_LNS_copy);
419 else
420 OS << char(Temp);
421}
422
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000423void MCDwarfFile::print(raw_ostream &OS) const {
424 OS << '"' << getName() << '"';
425}
426
427void MCDwarfFile::dump() const {
428 print(dbgs());
429}
Kevin Enderbyc0957932010-09-30 16:52:03 +0000430
Kevin Enderby94c2e852011-12-09 18:09:40 +0000431// Utility function to write a tuple for .debug_abbrev.
432static void EmitAbbrev(MCStreamer *MCOS, uint64_t Name, uint64_t Form) {
433 MCOS->EmitULEB128IntValue(Name);
434 MCOS->EmitULEB128IntValue(Form);
435}
436
437// When generating dwarf for assembly source files this emits
438// the data for .debug_abbrev section which contains three DIEs.
439static void EmitGenDwarfAbbrev(MCStreamer *MCOS) {
440 MCContext &context = MCOS->getContext();
441 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
442
443 // DW_TAG_compile_unit DIE abbrev (1).
444 MCOS->EmitULEB128IntValue(1);
445 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_compile_unit);
446 MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
447 EmitAbbrev(MCOS, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4);
448 EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
449 EmitAbbrev(MCOS, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr);
450 EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
451 EmitAbbrev(MCOS, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string);
452 StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
453 if (!DwarfDebugFlags.empty())
454 EmitAbbrev(MCOS, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string);
455 EmitAbbrev(MCOS, dwarf::DW_AT_producer, dwarf::DW_FORM_string);
456 EmitAbbrev(MCOS, dwarf::DW_AT_language, dwarf::DW_FORM_data2);
457 EmitAbbrev(MCOS, 0, 0);
458
Kevin Enderby38fdb7d2012-01-10 17:52:29 +0000459 // DW_TAG_label DIE abbrev (2).
Kevin Enderby94c2e852011-12-09 18:09:40 +0000460 MCOS->EmitULEB128IntValue(2);
Kevin Enderby38fdb7d2012-01-10 17:52:29 +0000461 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_label);
Kevin Enderby94c2e852011-12-09 18:09:40 +0000462 MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
463 EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
464 EmitAbbrev(MCOS, dwarf::DW_AT_decl_file, dwarf::DW_FORM_data4);
465 EmitAbbrev(MCOS, dwarf::DW_AT_decl_line, dwarf::DW_FORM_data4);
466 EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
Kevin Enderby94c2e852011-12-09 18:09:40 +0000467 EmitAbbrev(MCOS, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag);
468 EmitAbbrev(MCOS, 0, 0);
469
470 // DW_TAG_unspecified_parameters DIE abbrev (3).
471 MCOS->EmitULEB128IntValue(3);
472 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_unspecified_parameters);
473 MCOS->EmitIntValue(dwarf::DW_CHILDREN_no, 1);
474 EmitAbbrev(MCOS, 0, 0);
475
476 // Terminate the abbreviations for this compilation unit.
477 MCOS->EmitIntValue(0, 1);
478}
479
480// When generating dwarf for assembly source files this emits the data for
481// .debug_aranges section. Which contains a header and a table of pairs of
482// PointerSize'ed values for the address and size of section(s) with line table
483// entries (just the default .text in our case) and a terminating pair of zeros.
484static void EmitGenDwarfAranges(MCStreamer *MCOS) {
485 MCContext &context = MCOS->getContext();
486
487 // Create a symbol at the end of the section that we are creating the dwarf
488 // debugging info to use later in here as part of the expression to calculate
489 // the size of the section for the table.
490 MCOS->SwitchSection(context.getGenDwarfSection());
491 MCSymbol *SectionEndSym = context.CreateTempSymbol();
492 MCOS->EmitLabel(SectionEndSym);
493 context.setGenDwarfSectionEndSym(SectionEndSym);
494
495 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
496
497 // This will be the length of the .debug_aranges section, first account for
498 // the size of each item in the header (see below where we emit these items).
499 int Length = 4 + 2 + 4 + 1 + 1;
500
501 // Figure the padding after the header before the table of address and size
502 // pairs who's values are PointerSize'ed.
503 const MCAsmInfo &asmInfo = context.getAsmInfo();
504 int AddrSize = asmInfo.getPointerSize();
505 int Pad = 2 * AddrSize - (Length & (2 * AddrSize - 1));
506 if (Pad == 2 * AddrSize)
507 Pad = 0;
508 Length += Pad;
509
510 // Add the size of the pair of PointerSize'ed values for the address and size
511 // of the one default .text section we have in the table.
512 Length += 2 * AddrSize;
513 // And the pair of terminating zeros.
514 Length += 2 * AddrSize;
515
516
517 // Emit the header for this section.
518 // The 4 byte length not including the 4 byte value for the length.
519 MCOS->EmitIntValue(Length - 4, 4);
520 // The 2 byte version, which is 2.
521 MCOS->EmitIntValue(2, 2);
522 // The 4 byte offset to the compile unit in the .debug_info from the start
523 // of the .debug_info, it is at the start of that section so this is zero.
524 MCOS->EmitIntValue(0, 4);
525 // The 1 byte size of an address.
526 MCOS->EmitIntValue(AddrSize, 1);
527 // The 1 byte size of a segment descriptor, we use a value of zero.
528 MCOS->EmitIntValue(0, 1);
529 // Align the header with the padding if needed, before we put out the table.
530 for(int i = 0; i < Pad; i++)
531 MCOS->EmitIntValue(0, 1);
532
533 // Now emit the table of pairs of PointerSize'ed values for the section(s)
534 // address and size, in our case just the one default .text section.
535 const MCExpr *Addr = MCSymbolRefExpr::Create(
536 context.getGenDwarfSectionStartSym(), MCSymbolRefExpr::VK_None, context);
537 const MCExpr *Size = MakeStartMinusEndExpr(*MCOS,
538 *context.getGenDwarfSectionStartSym(), *SectionEndSym, 0);
539 MCOS->EmitAbsValue(Addr, AddrSize);
540 MCOS->EmitAbsValue(Size, AddrSize);
541
542 // And finally the pair of terminating zeros.
543 MCOS->EmitIntValue(0, AddrSize);
544 MCOS->EmitIntValue(0, AddrSize);
545}
546
547// When generating dwarf for assembly source files this emits the data for
548// .debug_info section which contains three parts. The header, the compile_unit
Kevin Enderby38fdb7d2012-01-10 17:52:29 +0000549// DIE and a list of label DIEs.
Rafael Espindola489d6792012-02-28 21:13:05 +0000550static void EmitGenDwarfInfo(MCStreamer *MCOS,
551 const MCSymbol *AbbrevSectionSymbol,
552 const MCSymbol *LineSectionSymbol) {
Kevin Enderby94c2e852011-12-09 18:09:40 +0000553 MCContext &context = MCOS->getContext();
554
Jim Grosbach2684d9e2012-05-11 01:41:30 +0000555 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
Kevin Enderby94c2e852011-12-09 18:09:40 +0000556
557 // Create a symbol at the start and end of this section used in here for the
558 // expression to calculate the length in the header.
559 MCSymbol *InfoStart = context.CreateTempSymbol();
560 MCOS->EmitLabel(InfoStart);
561 MCSymbol *InfoEnd = context.CreateTempSymbol();
562
563 // First part: the header.
564
565 // The 4 byte total length of the information for this compilation unit, not
566 // including these 4 bytes.
567 const MCExpr *Length = MakeStartMinusEndExpr(*MCOS, *InfoStart, *InfoEnd, 4);
568 MCOS->EmitAbsValue(Length, 4);
569
570 // The 2 byte DWARF version, which is 2.
571 MCOS->EmitIntValue(2, 2);
572
573 // The 4 byte offset to the debug abbrevs from the start of the .debug_abbrev,
574 // it is at the start of that section so this is zero.
Rafael Espindola489d6792012-02-28 21:13:05 +0000575 if (AbbrevSectionSymbol) {
576 MCOS->EmitSymbolValue(AbbrevSectionSymbol, 4);
577 } else {
578 MCOS->EmitIntValue(0, 4);
579 }
Kevin Enderby94c2e852011-12-09 18:09:40 +0000580
581 const MCAsmInfo &asmInfo = context.getAsmInfo();
582 int AddrSize = asmInfo.getPointerSize();
583 // The 1 byte size of an address.
584 MCOS->EmitIntValue(AddrSize, 1);
585
586 // Second part: the compile_unit DIE.
587
588 // The DW_TAG_compile_unit DIE abbrev (1).
589 MCOS->EmitULEB128IntValue(1);
590
591 // DW_AT_stmt_list, a 4 byte offset from the start of the .debug_line section,
592 // which is at the start of that section so this is zero.
Rafael Espindola489d6792012-02-28 21:13:05 +0000593 if (LineSectionSymbol) {
594 MCOS->EmitSymbolValue(LineSectionSymbol, 4);
595 } else {
596 MCOS->EmitIntValue(0, 4);
597 }
Kevin Enderby94c2e852011-12-09 18:09:40 +0000598
599 // AT_low_pc, the first address of the default .text section.
600 const MCExpr *Start = MCSymbolRefExpr::Create(
601 context.getGenDwarfSectionStartSym(), MCSymbolRefExpr::VK_None, context);
602 MCOS->EmitAbsValue(Start, AddrSize);
603
604 // AT_high_pc, the last address of the default .text section.
605 const MCExpr *End = MCSymbolRefExpr::Create(
606 context.getGenDwarfSectionEndSym(), MCSymbolRefExpr::VK_None, context);
607 MCOS->EmitAbsValue(End, AddrSize);
608
609 // AT_name, the name of the source file. Reconstruct from the first directory
610 // and file table entries.
611 const std::vector<StringRef> &MCDwarfDirs =
612 context.getMCDwarfDirs();
613 if (MCDwarfDirs.size() > 0) {
614 MCOS->EmitBytes(MCDwarfDirs[0], 0);
615 MCOS->EmitBytes("/", 0);
616 }
617 const std::vector<MCDwarfFile *> &MCDwarfFiles =
618 MCOS->getContext().getMCDwarfFiles();
619 MCOS->EmitBytes(MCDwarfFiles[1]->getName(), 0);
620 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
621
622 // AT_comp_dir, the working directory the assembly was done in.
623 llvm::sys::Path CWD = llvm::sys::Path::GetCurrentDirectory();
624 MCOS->EmitBytes(StringRef(CWD.c_str()), 0);
625 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
626
627 // AT_APPLE_flags, the command line arguments of the assembler tool.
628 StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
629 if (!DwarfDebugFlags.empty()){
630 MCOS->EmitBytes(DwarfDebugFlags, 0);
631 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
632 }
633
634 // AT_producer, the version of the assembler tool.
635 MCOS->EmitBytes(StringRef("llvm-mc (based on LLVM "), 0);
636 MCOS->EmitBytes(StringRef(PACKAGE_VERSION), 0);
637 MCOS->EmitBytes(StringRef(")"), 0);
638 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
639
640 // AT_language, a 4 byte value. We use DW_LANG_Mips_Assembler as the dwarf2
641 // draft has no standard code for assembler.
642 MCOS->EmitIntValue(dwarf::DW_LANG_Mips_Assembler, 2);
643
Kevin Enderby38fdb7d2012-01-10 17:52:29 +0000644 // Third part: the list of label DIEs.
Kevin Enderby94c2e852011-12-09 18:09:40 +0000645
Kevin Enderby11c2def2012-01-10 21:12:34 +0000646 // Loop on saved info for dwarf labels and create the DIEs for them.
647 const std::vector<const MCGenDwarfLabelEntry *> &Entries =
648 MCOS->getContext().getMCGenDwarfLabelEntries();
649 for (std::vector<const MCGenDwarfLabelEntry *>::const_iterator it =
Kevin Enderby94c2e852011-12-09 18:09:40 +0000650 Entries.begin(), ie = Entries.end(); it != ie;
651 ++it) {
Kevin Enderby11c2def2012-01-10 21:12:34 +0000652 const MCGenDwarfLabelEntry *Entry = *it;
Kevin Enderby94c2e852011-12-09 18:09:40 +0000653
Kevin Enderby38fdb7d2012-01-10 17:52:29 +0000654 // The DW_TAG_label DIE abbrev (2).
Kevin Enderby94c2e852011-12-09 18:09:40 +0000655 MCOS->EmitULEB128IntValue(2);
656
657 // AT_name, of the label without any leading underbar.
658 MCOS->EmitBytes(Entry->getName(), 0);
659 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
660
661 // AT_decl_file, index into the file table.
662 MCOS->EmitIntValue(Entry->getFileNumber(), 4);
663
664 // AT_decl_line, source line number.
665 MCOS->EmitIntValue(Entry->getLineNumber(), 4);
666
667 // AT_low_pc, start address of the label.
668 const MCExpr *AT_low_pc = MCSymbolRefExpr::Create(Entry->getLabel(),
669 MCSymbolRefExpr::VK_None, context);
670 MCOS->EmitAbsValue(AT_low_pc, AddrSize);
671
Kevin Enderby94c2e852011-12-09 18:09:40 +0000672 // DW_AT_prototyped, a one byte flag value of 0 saying we have no prototype.
673 MCOS->EmitIntValue(0, 1);
674
675 // The DW_TAG_unspecified_parameters DIE abbrev (3).
676 MCOS->EmitULEB128IntValue(3);
677
678 // Add the NULL DIE terminating the DW_TAG_unspecified_parameters DIE's.
679 MCOS->EmitIntValue(0, 1);
680 }
Kevin Enderby11c2def2012-01-10 21:12:34 +0000681 // Deallocate the MCGenDwarfLabelEntry classes that saved away the info
682 // for the dwarf labels.
683 for (std::vector<const MCGenDwarfLabelEntry *>::const_iterator it =
Kevin Enderby94c2e852011-12-09 18:09:40 +0000684 Entries.begin(), ie = Entries.end(); it != ie;
685 ++it) {
Kevin Enderby11c2def2012-01-10 21:12:34 +0000686 const MCGenDwarfLabelEntry *Entry = *it;
Kevin Enderby94c2e852011-12-09 18:09:40 +0000687 delete Entry;
688 }
689
690 // Add the NULL DIE terminating the Compile Unit DIE's.
691 MCOS->EmitIntValue(0, 1);
692
693 // Now set the value of the symbol at the end of the info section.
694 MCOS->EmitLabel(InfoEnd);
695}
696
697//
698// When generating dwarf for assembly source files this emits the Dwarf
699// sections.
700//
Rafael Espindola489d6792012-02-28 21:13:05 +0000701void MCGenDwarfInfo::Emit(MCStreamer *MCOS, const MCSymbol *LineSectionSymbol) {
Kevin Enderby94c2e852011-12-09 18:09:40 +0000702 // Create the dwarf sections in this order (.debug_line already created).
703 MCContext &context = MCOS->getContext();
Rafael Espindola489d6792012-02-28 21:13:05 +0000704 const MCAsmInfo &AsmInfo = context.getAsmInfo();
Kevin Enderby94c2e852011-12-09 18:09:40 +0000705 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
706 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
Rafael Espindola489d6792012-02-28 21:13:05 +0000707 MCSymbol *AbbrevSectionSymbol;
Rafael Espindola2241e512012-06-22 13:24:07 +0000708 if (AsmInfo.doesDwarfUseRelocationsAcrossSections()) {
Rafael Espindola489d6792012-02-28 21:13:05 +0000709 AbbrevSectionSymbol = context.CreateTempSymbol();
710 MCOS->EmitLabel(AbbrevSectionSymbol);
711 } else {
712 AbbrevSectionSymbol = NULL;
713 LineSectionSymbol = NULL;
714 }
Kevin Enderby94c2e852011-12-09 18:09:40 +0000715 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
716
717 // If there are no line table entries then do not emit any section contents.
718 if (context.getMCLineSections().empty())
719 return;
720
721 // Output the data for .debug_aranges section.
722 EmitGenDwarfAranges(MCOS);
723
724 // Output the data for .debug_abbrev section.
725 EmitGenDwarfAbbrev(MCOS);
726
727 // Output the data for .debug_info section.
Rafael Espindola489d6792012-02-28 21:13:05 +0000728 EmitGenDwarfInfo(MCOS, AbbrevSectionSymbol, LineSectionSymbol);
Kevin Enderby94c2e852011-12-09 18:09:40 +0000729}
730
731//
732// When generating dwarf for assembly source files this is called when symbol
733// for a label is created. If this symbol is not a temporary and is in the
734// section that dwarf is being generated for, save the needed info to create
Kevin Enderby11c2def2012-01-10 21:12:34 +0000735// a dwarf label.
Kevin Enderby94c2e852011-12-09 18:09:40 +0000736//
Kevin Enderby11c2def2012-01-10 21:12:34 +0000737void MCGenDwarfLabelEntry::Make(MCSymbol *Symbol, MCStreamer *MCOS,
Kevin Enderby94c2e852011-12-09 18:09:40 +0000738 SourceMgr &SrcMgr, SMLoc &Loc) {
Eric Christopher3c3cdcc2012-02-25 01:02:44 +0000739 // We won't create dwarf labels for temporary symbols or symbols not in
Kevin Enderby94c2e852011-12-09 18:09:40 +0000740 // the default text.
741 if (Symbol->isTemporary())
742 return;
743 MCContext &context = MCOS->getContext();
744 if (context.getGenDwarfSection() != MCOS->getCurrentSection())
745 return;
746
Kevin Enderby11c2def2012-01-10 21:12:34 +0000747 // The dwarf label's name does not have the symbol name's leading
Kevin Enderby94c2e852011-12-09 18:09:40 +0000748 // underbar if any.
749 StringRef Name = Symbol->getName();
750 if (Name.startswith("_"))
751 Name = Name.substr(1, Name.size()-1);
752
Kevin Enderby11c2def2012-01-10 21:12:34 +0000753 // Get the dwarf file number to be used for the dwarf label.
Kevin Enderby94c2e852011-12-09 18:09:40 +0000754 unsigned FileNumber = context.getGenDwarfFileNumber();
755
756 // Finding the line number is the expensive part which is why we just don't
Kevin Enderby11c2def2012-01-10 21:12:34 +0000757 // pass it in as for some symbols we won't create a dwarf label.
Kevin Enderby94c2e852011-12-09 18:09:40 +0000758 int CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
759 unsigned LineNumber = SrcMgr.FindLineNumber(Loc, CurBuffer);
760
761 // We create a temporary symbol for use for the AT_high_pc and AT_low_pc
762 // values so that they don't have things like an ARM thumb bit from the
763 // original symbol. So when used they won't get a low bit set after
764 // relocation.
765 MCSymbol *Label = context.CreateTempSymbol();
766 MCOS->EmitLabel(Label);
767
768 // Create and entry for the info and add it to the other entries.
Jim Grosbach2684d9e2012-05-11 01:41:30 +0000769 MCGenDwarfLabelEntry *Entry =
Kevin Enderby11c2def2012-01-10 21:12:34 +0000770 new MCGenDwarfLabelEntry(Name, FileNumber, LineNumber, Label);
771 MCOS->getContext().addMCGenDwarfLabelEntry(Entry);
Kevin Enderby94c2e852011-12-09 18:09:40 +0000772}
773
Rafael Espindola89b93722010-12-10 07:39:47 +0000774static int getDataAlignmentFactor(MCStreamer &streamer) {
775 MCContext &context = streamer.getContext();
Evan Cheng1be0e272011-07-15 02:09:41 +0000776 const MCAsmInfo &asmInfo = context.getAsmInfo();
Rafael Espindola89b93722010-12-10 07:39:47 +0000777 int size = asmInfo.getPointerSize();
Evan Cheng1be0e272011-07-15 02:09:41 +0000778 if (asmInfo.isStackGrowthDirectionUp())
Rafael Espindola89b93722010-12-10 07:39:47 +0000779 return size;
Evan Cheng1be0e272011-07-15 02:09:41 +0000780 else
781 return -size;
Rafael Espindola89b93722010-12-10 07:39:47 +0000782}
783
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000784static unsigned getSizeForEncoding(MCStreamer &streamer,
785 unsigned symbolEncoding) {
Rafael Espindolabdc31672010-12-27 15:56:22 +0000786 MCContext &context = streamer.getContext();
Rafael Espindolabdc31672010-12-27 15:56:22 +0000787 unsigned format = symbolEncoding & 0x0f;
Rafael Espindolabdc31672010-12-27 15:56:22 +0000788 switch (format) {
Craig Topper85814382012-02-07 05:05:23 +0000789 default: llvm_unreachable("Unknown Encoding");
Rafael Espindolabdc31672010-12-27 15:56:22 +0000790 case dwarf::DW_EH_PE_absptr:
791 case dwarf::DW_EH_PE_signed:
Evan Cheng1be0e272011-07-15 02:09:41 +0000792 return context.getAsmInfo().getPointerSize();
Rafael Espindolabdc31672010-12-27 15:56:22 +0000793 case dwarf::DW_EH_PE_udata2:
794 case dwarf::DW_EH_PE_sdata2:
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000795 return 2;
Rafael Espindolabdc31672010-12-27 15:56:22 +0000796 case dwarf::DW_EH_PE_udata4:
797 case dwarf::DW_EH_PE_sdata4:
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000798 return 4;
Rafael Espindolabdc31672010-12-27 15:56:22 +0000799 case dwarf::DW_EH_PE_udata8:
800 case dwarf::DW_EH_PE_sdata8:
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000801 return 8;
Rafael Espindolabdc31672010-12-27 15:56:22 +0000802 }
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000803}
804
805static void EmitSymbol(MCStreamer &streamer, const MCSymbol &symbol,
Bill Wendling2541c412011-06-30 22:02:20 +0000806 unsigned symbolEncoding, const char *comment = 0) {
Rafael Espindolaa0057ca2011-04-28 21:04:39 +0000807 MCContext &context = streamer.getContext();
808 const MCAsmInfo &asmInfo = context.getAsmInfo();
809 const MCExpr *v = asmInfo.getExprForFDESymbol(&symbol,
Rafael Espindoladebd7e42011-05-01 03:50:49 +0000810 symbolEncoding,
Rafael Espindolaa0057ca2011-04-28 21:04:39 +0000811 streamer);
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000812 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Bill Wendling2541c412011-06-30 22:02:20 +0000813 if (streamer.isVerboseAsm() && comment) streamer.AddComment(comment);
Rafael Espindoladebd7e42011-05-01 03:50:49 +0000814 streamer.EmitAbsValue(v, size);
Rafael Espindolabdc31672010-12-27 15:56:22 +0000815}
816
Rafael Espindolabfa27cc2011-04-28 16:09:09 +0000817static void EmitPersonality(MCStreamer &streamer, const MCSymbol &symbol,
818 unsigned symbolEncoding) {
819 MCContext &context = streamer.getContext();
820 const MCAsmInfo &asmInfo = context.getAsmInfo();
Rafael Espindoladebd7e42011-05-01 03:50:49 +0000821 const MCExpr *v = asmInfo.getExprForPersonalitySymbol(&symbol,
822 symbolEncoding,
823 streamer);
Rafael Espindolabfa27cc2011-04-28 16:09:09 +0000824 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Rafael Espindoladebd7e42011-05-01 03:50:49 +0000825 streamer.EmitValue(v, size);
Rafael Espindolabfa27cc2011-04-28 16:09:09 +0000826}
827
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000828static const MachineLocation TranslateMachineLocation(
Evan Cheng0e6a0522011-07-18 20:57:22 +0000829 const MCRegisterInfo &MRI,
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000830 const MachineLocation &Loc) {
831 unsigned Reg = Loc.getReg() == MachineLocation::VirtualFP ?
832 MachineLocation::VirtualFP :
Evan Cheng0e6a0522011-07-18 20:57:22 +0000833 unsigned(MRI.getDwarfRegNum(Loc.getReg(), true));
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000834 const MachineLocation &NewLoc = Loc.isReg() ?
835 MachineLocation(Reg) : MachineLocation(Reg, Loc.getOffset());
836 return NewLoc;
837}
838
Rafael Espindola25f492e2011-04-12 16:12:03 +0000839namespace {
840 class FrameEmitterImpl {
841 int CFAOffset;
Rafael Espindola514cecc2011-04-28 03:26:11 +0000842 int CIENum;
Rafael Espindola5426a9e2011-05-01 04:49:54 +0000843 bool UsingCFI;
Rafael Espindola40a7dbb2011-05-10 03:54:12 +0000844 bool IsEH;
Rafael Espindolae3a0e982011-05-10 20:59:42 +0000845 const MCSymbol *SectionStart;
Rafael Espindola25f492e2011-04-12 16:12:03 +0000846 public:
Bill Wendling1424c3c2011-07-22 21:18:59 +0000847 FrameEmitterImpl(bool usingCFI, bool isEH)
848 : CFAOffset(0), CIENum(0), UsingCFI(usingCFI), IsEH(isEH),
849 SectionStart(0) {}
850
851 void setSectionStart(const MCSymbol *Label) { SectionStart = Label; }
Rafael Espindola25f492e2011-04-12 16:12:03 +0000852
Bill Wendlinge3cd13f2011-06-23 01:06:23 +0000853 /// EmitCompactUnwind - Emit the unwind information in a compact way. If
854 /// we're successful, return 'true'. Otherwise, return 'false' and it will
855 /// emit the normal CIE and FDE.
856 bool EmitCompactUnwind(MCStreamer &streamer,
857 const MCDwarfFrameInfo &frame);
858
Rafael Espindola25f492e2011-04-12 16:12:03 +0000859 const MCSymbol &EmitCIE(MCStreamer &streamer,
860 const MCSymbol *personality,
861 unsigned personalityEncoding,
862 const MCSymbol *lsda,
Rafael Espindola16d7d432012-01-23 21:51:52 +0000863 bool IsSignalFrame,
Rafael Espindola25f492e2011-04-12 16:12:03 +0000864 unsigned lsdaEncoding);
865 MCSymbol *EmitFDE(MCStreamer &streamer,
866 const MCSymbol &cieStart,
Rafael Espindola9f270da2011-05-10 03:01:39 +0000867 const MCDwarfFrameInfo &frame);
Rafael Espindola25f492e2011-04-12 16:12:03 +0000868 void EmitCFIInstructions(MCStreamer &streamer,
869 const std::vector<MCCFIInstruction> &Instrs,
870 MCSymbol *BaseLabel);
871 void EmitCFIInstruction(MCStreamer &Streamer,
872 const MCCFIInstruction &Instr);
873 };
Bill Wendling3c163cf2011-06-30 21:25:51 +0000874
875} // end anonymous namespace
876
877static void EmitEncodingByte(MCStreamer &Streamer, unsigned Encoding,
878 StringRef Prefix) {
879 if (Streamer.isVerboseAsm()) {
Benjamin Kramereb9fa662012-01-20 14:42:37 +0000880 const char *EncStr;
Bill Wendling3c163cf2011-06-30 21:25:51 +0000881 switch (Encoding) {
Benjamin Kramereb9fa662012-01-20 14:42:37 +0000882 default: EncStr = "<unknown encoding>"; break;
883 case dwarf::DW_EH_PE_absptr: EncStr = "absptr"; break;
884 case dwarf::DW_EH_PE_omit: EncStr = "omit"; break;
885 case dwarf::DW_EH_PE_pcrel: EncStr = "pcrel"; break;
886 case dwarf::DW_EH_PE_udata4: EncStr = "udata4"; break;
887 case dwarf::DW_EH_PE_udata8: EncStr = "udata8"; break;
888 case dwarf::DW_EH_PE_sdata4: EncStr = "sdata4"; break;
889 case dwarf::DW_EH_PE_sdata8: EncStr = "sdata8"; break;
890 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
891 EncStr = "pcrel udata4";
892 break;
893 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
894 EncStr = "pcrel sdata4";
895 break;
896 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
897 EncStr = "pcrel udata8";
898 break;
899 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
900 EncStr = "screl sdata8";
901 break;
Bill Wendling3c163cf2011-06-30 21:25:51 +0000902 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata4:
903 EncStr = "indirect pcrel udata4";
Benjamin Kramereb9fa662012-01-20 14:42:37 +0000904 break;
Bill Wendling3c163cf2011-06-30 21:25:51 +0000905 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata4:
906 EncStr = "indirect pcrel sdata4";
Benjamin Kramereb9fa662012-01-20 14:42:37 +0000907 break;
Bill Wendling3c163cf2011-06-30 21:25:51 +0000908 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata8:
909 EncStr = "indirect pcrel udata8";
Benjamin Kramereb9fa662012-01-20 14:42:37 +0000910 break;
Bill Wendling3c163cf2011-06-30 21:25:51 +0000911 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata8:
912 EncStr = "indirect pcrel sdata8";
Benjamin Kramereb9fa662012-01-20 14:42:37 +0000913 break;
Bill Wendling3c163cf2011-06-30 21:25:51 +0000914 }
915
916 Streamer.AddComment(Twine(Prefix) + " = " + EncStr);
917 }
918
919 Streamer.EmitIntValue(Encoding, 1);
Rafael Espindola25f492e2011-04-12 16:12:03 +0000920}
921
922void FrameEmitterImpl::EmitCFIInstruction(MCStreamer &Streamer,
923 const MCCFIInstruction &Instr) {
924 int dataAlignmentFactor = getDataAlignmentFactor(Streamer);
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000925 bool VerboseAsm = Streamer.isVerboseAsm();
Rafael Espindola25f492e2011-04-12 16:12:03 +0000926
927 switch (Instr.getOperation()) {
928 case MCCFIInstruction::Move:
929 case MCCFIInstruction::RelMove: {
930 const MachineLocation &Dst = Instr.getDestination();
931 const MachineLocation &Src = Instr.getSource();
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000932 const bool IsRelative = Instr.getOperation() == MCCFIInstruction::RelMove;
Rafael Espindola25f492e2011-04-12 16:12:03 +0000933
934 // If advancing cfa.
935 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
Rafael Espindola25f492e2011-04-12 16:12:03 +0000936 if (Src.getReg() == MachineLocation::VirtualFP) {
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000937 if (VerboseAsm) Streamer.AddComment("DW_CFA_def_cfa_offset");
Rafael Espindola25f492e2011-04-12 16:12:03 +0000938 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_offset, 1);
939 } else {
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000940 if (VerboseAsm) Streamer.AddComment("DW_CFA_def_cfa");
Rafael Espindola25f492e2011-04-12 16:12:03 +0000941 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa, 1);
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000942 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") +
943 Twine(Src.getReg()));
Rafael Espindola25f492e2011-04-12 16:12:03 +0000944 Streamer.EmitULEB128IntValue(Src.getReg());
945 }
946
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000947 if (IsRelative)
948 CFAOffset += Src.getOffset();
949 else
950 CFAOffset = -Src.getOffset();
951
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000952 if (VerboseAsm) Streamer.AddComment(Twine("Offset " + Twine(CFAOffset)));
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000953 Streamer.EmitULEB128IntValue(CFAOffset);
Rafael Espindola25f492e2011-04-12 16:12:03 +0000954 return;
955 }
956
957 if (Src.isReg() && Src.getReg() == MachineLocation::VirtualFP) {
958 assert(Dst.isReg() && "Machine move not supported yet.");
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000959 if (VerboseAsm) Streamer.AddComment("DW_CFA_def_cfa_register");
Rafael Espindola25f492e2011-04-12 16:12:03 +0000960 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_register, 1);
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000961 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Dst.getReg()));
Rafael Espindola25f492e2011-04-12 16:12:03 +0000962 Streamer.EmitULEB128IntValue(Dst.getReg());
963 return;
964 }
965
966 unsigned Reg = Src.getReg();
Rafael Espindola25f492e2011-04-12 16:12:03 +0000967 int Offset = Dst.getOffset();
968 if (IsRelative)
969 Offset -= CFAOffset;
970 Offset = Offset / dataAlignmentFactor;
971
972 if (Offset < 0) {
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000973 if (VerboseAsm) Streamer.AddComment("DW_CFA_offset_extended_sf");
Rafael Espindola25f492e2011-04-12 16:12:03 +0000974 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended_sf, 1);
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000975 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
Rafael Espindola25f492e2011-04-12 16:12:03 +0000976 Streamer.EmitULEB128IntValue(Reg);
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000977 if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
Rafael Espindola25f492e2011-04-12 16:12:03 +0000978 Streamer.EmitSLEB128IntValue(Offset);
979 } else if (Reg < 64) {
Bill Wendlinge08d4332011-06-30 23:47:40 +0000980 if (VerboseAsm) Streamer.AddComment(Twine("DW_CFA_offset + Reg(") +
981 Twine(Reg) + ")");
Rafael Espindola25f492e2011-04-12 16:12:03 +0000982 Streamer.EmitIntValue(dwarf::DW_CFA_offset + Reg, 1);
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000983 if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
Rafael Espindolaeccbad72011-04-21 23:26:40 +0000984 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola25f492e2011-04-12 16:12:03 +0000985 } else {
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000986 if (VerboseAsm) Streamer.AddComment("DW_CFA_offset_extended");
Rafael Espindola25f492e2011-04-12 16:12:03 +0000987 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended, 1);
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000988 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
Rafael Espindolaeccbad72011-04-21 23:26:40 +0000989 Streamer.EmitULEB128IntValue(Reg);
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000990 if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
Rafael Espindolaeccbad72011-04-21 23:26:40 +0000991 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola25f492e2011-04-12 16:12:03 +0000992 }
993 return;
994 }
Rafael Espindolac25680f2011-12-29 21:09:08 +0000995 case MCCFIInstruction::RememberState:
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000996 if (VerboseAsm) Streamer.AddComment("DW_CFA_remember_state");
Rafael Espindola25f492e2011-04-12 16:12:03 +0000997 Streamer.EmitIntValue(dwarf::DW_CFA_remember_state, 1);
998 return;
Rafael Espindolac25680f2011-12-29 21:09:08 +0000999 case MCCFIInstruction::RestoreState:
Bill Wendlingefd24dd2011-06-30 21:45:12 +00001000 if (VerboseAsm) Streamer.AddComment("DW_CFA_restore_state");
Rafael Espindola25f492e2011-04-12 16:12:03 +00001001 Streamer.EmitIntValue(dwarf::DW_CFA_restore_state, 1);
1002 return;
1003 case MCCFIInstruction::SameValue: {
1004 unsigned Reg = Instr.getDestination().getReg();
Bill Wendlingefd24dd2011-06-30 21:45:12 +00001005 if (VerboseAsm) Streamer.AddComment("DW_CFA_same_value");
Rafael Espindola25f492e2011-04-12 16:12:03 +00001006 Streamer.EmitIntValue(dwarf::DW_CFA_same_value, 1);
Bill Wendlingefd24dd2011-06-30 21:45:12 +00001007 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
Rafael Espindolae8cfbd82011-04-21 23:39:26 +00001008 Streamer.EmitULEB128IntValue(Reg);
Rafael Espindola25f492e2011-04-12 16:12:03 +00001009 return;
1010 }
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00001011 case MCCFIInstruction::Restore: {
1012 unsigned Reg = Instr.getDestination().getReg();
1013 if (VerboseAsm) {
1014 Streamer.AddComment("DW_CFA_restore");
1015 Streamer.AddComment(Twine("Reg ") + Twine(Reg));
1016 }
1017 Streamer.EmitIntValue(dwarf::DW_CFA_restore | Reg, 1);
1018 return;
1019 }
Rafael Espindola6f0b1812011-12-29 20:24:47 +00001020 case MCCFIInstruction::Escape:
1021 if (VerboseAsm) Streamer.AddComment("Escape bytes");
1022 Streamer.EmitBytes(Instr.getValues(), 0);
1023 return;
Rafael Espindola25f492e2011-04-12 16:12:03 +00001024 }
1025 llvm_unreachable("Unhandled case in switch");
1026}
1027
1028/// EmitFrameMoves - Emit frame instructions to describe the layout of the
1029/// frame.
1030void FrameEmitterImpl::EmitCFIInstructions(MCStreamer &streamer,
1031 const std::vector<MCCFIInstruction> &Instrs,
1032 MCSymbol *BaseLabel) {
1033 for (unsigned i = 0, N = Instrs.size(); i < N; ++i) {
1034 const MCCFIInstruction &Instr = Instrs[i];
1035 MCSymbol *Label = Instr.getLabel();
1036 // Throw out move if the label is invalid.
1037 if (Label && !Label->isDefined()) continue; // Not emitted, in dead code.
1038
1039 // Advance row if new location.
1040 if (BaseLabel && Label) {
1041 MCSymbol *ThisSym = Label;
1042 if (ThisSym != BaseLabel) {
Bill Wendlingd221cd62011-06-30 22:35:49 +00001043 if (streamer.isVerboseAsm()) streamer.AddComment("DW_CFA_advance_loc4");
Rafael Espindola25f492e2011-04-12 16:12:03 +00001044 streamer.EmitDwarfAdvanceFrameAddr(BaseLabel, ThisSym);
1045 BaseLabel = ThisSym;
1046 }
1047 }
1048
1049 EmitCFIInstruction(streamer, Instr);
1050 }
1051}
1052
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001053/// EmitCompactUnwind - Emit the unwind information in a compact way. If we're
1054/// successful, return 'true'. Otherwise, return 'false' and it will emit the
1055/// normal CIE and FDE.
1056bool FrameEmitterImpl::EmitCompactUnwind(MCStreamer &Streamer,
1057 const MCDwarfFrameInfo &Frame) {
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001058 MCContext &Context = Streamer.getContext();
Evan Chenge76a33b2011-07-20 05:58:47 +00001059 const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
Bill Wendling3c163cf2011-06-30 21:25:51 +00001060 bool VerboseAsm = Streamer.isVerboseAsm();
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001061
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001062 // range-start range-length compact-unwind-enc personality-func lsda
1063 // _foo LfooEnd-_foo 0x00000023 0 0
1064 // _bar LbarEnd-_bar 0x00000025 __gxx_personality except_tab1
1065 //
1066 // .section __LD,__compact_unwind,regular,debug
1067 //
1068 // # compact unwind for _foo
1069 // .quad _foo
1070 // .set L1,LfooEnd-_foo
1071 // .long L1
1072 // .long 0x01010001
1073 // .quad 0
1074 // .quad 0
1075 //
1076 // # compact unwind for _bar
1077 // .quad _bar
1078 // .set L2,LbarEnd-_bar
1079 // .long L2
1080 // .long 0x01020011
1081 // .quad __gxx_personality
1082 // .quad except_tab1
1083
Bill Wendlinge52e3f22011-07-19 00:06:12 +00001084 uint32_t Encoding = Frame.CompactUnwindEncoding;
Bill Wendling6a6b8c32011-07-07 00:54:13 +00001085 if (!Encoding) return false;
1086
Bill Wendling4bb5b032011-07-14 23:34:45 +00001087 // The encoding needs to know we have an LSDA.
1088 if (Frame.Lsda)
1089 Encoding |= 0x40000000;
1090
Evan Chenge76a33b2011-07-20 05:58:47 +00001091 Streamer.SwitchSection(MOFI->getCompactUnwindSection());
Bill Wendling3c163cf2011-06-30 21:25:51 +00001092
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001093 // Range Start
Evan Cheng203576a2011-07-20 19:50:42 +00001094 unsigned FDEEncoding = MOFI->getFDEEncoding(UsingCFI);
Bill Wendling9056e902011-06-29 23:49:12 +00001095 unsigned Size = getSizeForEncoding(Streamer, FDEEncoding);
Bill Wendling3c163cf2011-06-30 21:25:51 +00001096 if (VerboseAsm) Streamer.AddComment("Range Start");
Bill Wendling9056e902011-06-29 23:49:12 +00001097 Streamer.EmitSymbolValue(Frame.Function, Size);
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001098
1099 // Range Length
1100 const MCExpr *Range = MakeStartMinusEndExpr(Streamer, *Frame.Begin,
1101 *Frame.End, 0);
Bill Wendling3c163cf2011-06-30 21:25:51 +00001102 if (VerboseAsm) Streamer.AddComment("Range Length");
Bill Wendling9287a6e2011-06-30 00:30:52 +00001103 Streamer.EmitAbsValue(Range, 4);
1104
Bill Wendling9287a6e2011-06-30 00:30:52 +00001105 // Compact Encoding
Bill Wendling9287a6e2011-06-30 00:30:52 +00001106 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_udata4);
Benjamin Kramer70be28a2011-11-07 21:00:59 +00001107 if (VerboseAsm) Streamer.AddComment("Compact Unwind Encoding: 0x" +
1108 Twine::utohexstr(Encoding));
Bill Wendling9287a6e2011-06-30 00:30:52 +00001109 Streamer.EmitIntValue(Encoding, Size);
1110
Bill Wendlinge52e3f22011-07-19 00:06:12 +00001111
Bill Wendling9056e902011-06-29 23:49:12 +00001112 // Personality Function
Bill Wendling4bb5b032011-07-14 23:34:45 +00001113 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_absptr);
Bill Wendling3c163cf2011-06-30 21:25:51 +00001114 if (VerboseAsm) Streamer.AddComment("Personality Function");
Bill Wendling4498d392011-06-29 23:53:16 +00001115 if (Frame.Personality)
Bill Wendling9056e902011-06-29 23:49:12 +00001116 Streamer.EmitSymbolValue(Frame.Personality, Size);
Bill Wendling4498d392011-06-29 23:53:16 +00001117 else
1118 Streamer.EmitIntValue(0, Size); // No personality fn
Bill Wendling9056e902011-06-29 23:49:12 +00001119
1120 // LSDA
Bill Wendling4498d392011-06-29 23:53:16 +00001121 Size = getSizeForEncoding(Streamer, Frame.LsdaEncoding);
Bill Wendling3c163cf2011-06-30 21:25:51 +00001122 if (VerboseAsm) Streamer.AddComment("LSDA");
Bill Wendling4498d392011-06-29 23:53:16 +00001123 if (Frame.Lsda)
Bill Wendling9056e902011-06-29 23:49:12 +00001124 Streamer.EmitSymbolValue(Frame.Lsda, Size);
Bill Wendling4498d392011-06-29 23:53:16 +00001125 else
1126 Streamer.EmitIntValue(0, Size); // No LSDA
Bill Wendling9056e902011-06-29 23:49:12 +00001127
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001128 return true;
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001129}
1130
Rafael Espindola25f492e2011-04-12 16:12:03 +00001131const MCSymbol &FrameEmitterImpl::EmitCIE(MCStreamer &streamer,
1132 const MCSymbol *personality,
1133 unsigned personalityEncoding,
1134 const MCSymbol *lsda,
Rafael Espindola16d7d432012-01-23 21:51:52 +00001135 bool IsSignalFrame,
Rafael Espindola25f492e2011-04-12 16:12:03 +00001136 unsigned lsdaEncoding) {
Rafael Espindola89b93722010-12-10 07:39:47 +00001137 MCContext &context = streamer.getContext();
Evan Cheng0e6a0522011-07-18 20:57:22 +00001138 const MCRegisterInfo &MRI = context.getRegisterInfo();
Evan Chenge76a33b2011-07-20 05:58:47 +00001139 const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
Bill Wendling3c163cf2011-06-30 21:25:51 +00001140 bool verboseAsm = streamer.isVerboseAsm();
Rafael Espindola514cecc2011-04-28 03:26:11 +00001141
1142 MCSymbol *sectionStart;
Evan Chenge76a33b2011-07-20 05:58:47 +00001143 if (MOFI->isFunctionEHFrameSymbolPrivate() || !IsEH)
Rafael Espindola514cecc2011-04-28 03:26:11 +00001144 sectionStart = context.CreateTempSymbol();
1145 else
1146 sectionStart = context.GetOrCreateSymbol(Twine("EH_frame") + Twine(CIENum));
1147
Bill Wendling3c163cf2011-06-30 21:25:51 +00001148 streamer.EmitLabel(sectionStart);
Rafael Espindola514cecc2011-04-28 03:26:11 +00001149 CIENum++;
1150
Bill Wendling6a6b8c32011-07-07 00:54:13 +00001151 MCSymbol *sectionEnd = context.CreateTempSymbol();
Rafael Espindola89b93722010-12-10 07:39:47 +00001152
1153 // Length
1154 const MCExpr *Length = MakeStartMinusEndExpr(streamer, *sectionStart,
1155 *sectionEnd, 4);
Bill Wendling3c163cf2011-06-30 21:25:51 +00001156 if (verboseAsm) streamer.AddComment("CIE Length");
Rafael Espindola9266cc42011-04-27 01:43:49 +00001157 streamer.EmitAbsValue(Length, 4);
Rafael Espindola89b93722010-12-10 07:39:47 +00001158
1159 // CIE ID
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001160 unsigned CIE_ID = IsEH ? 0 : -1;
Bill Wendling3c163cf2011-06-30 21:25:51 +00001161 if (verboseAsm) streamer.AddComment("CIE ID Tag");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001162 streamer.EmitIntValue(CIE_ID, 4);
Rafael Espindola89b93722010-12-10 07:39:47 +00001163
1164 // Version
Bill Wendling3c163cf2011-06-30 21:25:51 +00001165 if (verboseAsm) streamer.AddComment("DW_CIE_VERSION");
Rafael Espindola89b93722010-12-10 07:39:47 +00001166 streamer.EmitIntValue(dwarf::DW_CIE_VERSION, 1);
1167
1168 // Augmentation String
1169 SmallString<8> Augmentation;
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001170 if (IsEH) {
Bill Wendling3c163cf2011-06-30 21:25:51 +00001171 if (verboseAsm) streamer.AddComment("CIE Augmentation");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001172 Augmentation += "z";
1173 if (personality)
1174 Augmentation += "P";
1175 if (lsda)
1176 Augmentation += "L";
1177 Augmentation += "R";
Rafael Espindola16d7d432012-01-23 21:51:52 +00001178 if (IsSignalFrame)
1179 Augmentation += "S";
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001180 streamer.EmitBytes(Augmentation.str(), 0);
1181 }
Rafael Espindola89b93722010-12-10 07:39:47 +00001182 streamer.EmitIntValue(0, 1);
1183
1184 // Code Alignment Factor
Bill Wendling3c163cf2011-06-30 21:25:51 +00001185 if (verboseAsm) streamer.AddComment("CIE Code Alignment Factor");
Rafael Espindola89b93722010-12-10 07:39:47 +00001186 streamer.EmitULEB128IntValue(1);
1187
1188 // Data Alignment Factor
Bill Wendling3c163cf2011-06-30 21:25:51 +00001189 if (verboseAsm) streamer.AddComment("CIE Data Alignment Factor");
Rafael Espindola89b93722010-12-10 07:39:47 +00001190 streamer.EmitSLEB128IntValue(getDataAlignmentFactor(streamer));
1191
1192 // Return Address Register
Bill Wendling3c163cf2011-06-30 21:25:51 +00001193 if (verboseAsm) streamer.AddComment("CIE Return Address Column");
Evan Cheng0e6a0522011-07-18 20:57:22 +00001194 streamer.EmitULEB128IntValue(MRI.getDwarfRegNum(MRI.getRARegister(), true));
Rafael Espindola89b93722010-12-10 07:39:47 +00001195
1196 // Augmentation Data Length (optional)
Rafael Espindola1f6c8752011-04-29 21:50:57 +00001197
1198 unsigned augmentationLength = 0;
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001199 if (IsEH) {
1200 if (personality) {
1201 // Personality Encoding
1202 augmentationLength += 1;
1203 // Personality
1204 augmentationLength += getSizeForEncoding(streamer, personalityEncoding);
1205 }
1206 if (lsda)
1207 augmentationLength += 1;
1208 // Encoding of the FDE pointers
Rafael Espindola1f6c8752011-04-29 21:50:57 +00001209 augmentationLength += 1;
Rafael Espindola1f6c8752011-04-29 21:50:57 +00001210
Bill Wendling3c163cf2011-06-30 21:25:51 +00001211 if (verboseAsm) streamer.AddComment("Augmentation Size");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001212 streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola89b93722010-12-10 07:39:47 +00001213
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001214 // Augmentation Data (optional)
1215 if (personality) {
1216 // Personality Encoding
Bill Wendling3c163cf2011-06-30 21:25:51 +00001217 EmitEncodingByte(streamer, personalityEncoding,
1218 "Personality Encoding");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001219 // Personality
Bill Wendling3c163cf2011-06-30 21:25:51 +00001220 if (verboseAsm) streamer.AddComment("Personality");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001221 EmitPersonality(streamer, *personality, personalityEncoding);
1222 }
Bill Wendling3c163cf2011-06-30 21:25:51 +00001223
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001224 if (lsda)
Bill Wendling3c163cf2011-06-30 21:25:51 +00001225 EmitEncodingByte(streamer, lsdaEncoding, "LSDA Encoding");
1226
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001227 // Encoding of the FDE pointers
Evan Cheng203576a2011-07-20 19:50:42 +00001228 EmitEncodingByte(streamer, MOFI->getFDEEncoding(UsingCFI),
Bill Wendling3c163cf2011-06-30 21:25:51 +00001229 "FDE Encoding");
Rafael Espindolabdc31672010-12-27 15:56:22 +00001230 }
Rafael Espindola89b93722010-12-10 07:39:47 +00001231
1232 // Initial Instructions
1233
Evan Cheng2d286172011-07-18 22:29:13 +00001234 const MCAsmInfo &MAI = context.getAsmInfo();
1235 const std::vector<MachineMove> &Moves = MAI.getInitialFrameState();
Rafael Espindolafe024d02010-12-28 18:36:23 +00001236 std::vector<MCCFIInstruction> Instructions;
Rafael Espindola89b93722010-12-10 07:39:47 +00001237
Rafael Espindolafe024d02010-12-28 18:36:23 +00001238 for (int i = 0, n = Moves.size(); i != n; ++i) {
Rafael Espindolab40a71f2010-12-29 01:42:56 +00001239 MCSymbol *Label = Moves[i].getLabel();
1240 const MachineLocation &Dst =
Evan Cheng0e6a0522011-07-18 20:57:22 +00001241 TranslateMachineLocation(MRI, Moves[i].getDestination());
Rafael Espindolab40a71f2010-12-29 01:42:56 +00001242 const MachineLocation &Src =
Evan Cheng0e6a0522011-07-18 20:57:22 +00001243 TranslateMachineLocation(MRI, Moves[i].getSource());
Rafael Espindolab40a71f2010-12-29 01:42:56 +00001244 MCCFIInstruction Inst(Label, Dst, Src);
Rafael Espindolafe024d02010-12-28 18:36:23 +00001245 Instructions.push_back(Inst);
1246 }
1247
Rafael Espindolab40a71f2010-12-29 01:42:56 +00001248 EmitCFIInstructions(streamer, Instructions, NULL);
Rafael Espindola89b93722010-12-10 07:39:47 +00001249
1250 // Padding
Evan Cheng1be0e272011-07-15 02:09:41 +00001251 streamer.EmitValueToAlignment(IsEH
1252 ? 4 : context.getAsmInfo().getPointerSize());
Rafael Espindola89b93722010-12-10 07:39:47 +00001253
1254 streamer.EmitLabel(sectionEnd);
1255 return *sectionStart;
1256}
1257
Rafael Espindola25f492e2011-04-12 16:12:03 +00001258MCSymbol *FrameEmitterImpl::EmitFDE(MCStreamer &streamer,
1259 const MCSymbol &cieStart,
Rafael Espindola9f270da2011-05-10 03:01:39 +00001260 const MCDwarfFrameInfo &frame) {
Rafael Espindolabdc31672010-12-27 15:56:22 +00001261 MCContext &context = streamer.getContext();
1262 MCSymbol *fdeStart = context.CreateTempSymbol();
1263 MCSymbol *fdeEnd = context.CreateTempSymbol();
Evan Chenge76a33b2011-07-20 05:58:47 +00001264 const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
Bill Wendling2541c412011-06-30 22:02:20 +00001265 bool verboseAsm = streamer.isVerboseAsm();
Rafael Espindola89b93722010-12-10 07:39:47 +00001266
Evan Cheng5fbe5e72011-08-24 22:31:37 +00001267 if (IsEH && frame.Function && !MOFI->isFunctionEHFrameSymbolPrivate()) {
Bill Wendling2541c412011-06-30 22:02:20 +00001268 MCSymbol *EHSym =
1269 context.GetOrCreateSymbol(frame.Function->getName() + Twine(".eh"));
Rafael Espindola8bca4102011-04-28 12:50:37 +00001270 streamer.EmitEHSymAttributes(frame.Function, EHSym);
Rafael Espindolaa8cfb872011-04-28 02:46:42 +00001271 streamer.EmitLabel(EHSym);
1272 }
1273
Rafael Espindola89b93722010-12-10 07:39:47 +00001274 // Length
1275 const MCExpr *Length = MakeStartMinusEndExpr(streamer, *fdeStart, *fdeEnd, 0);
Bill Wendling2541c412011-06-30 22:02:20 +00001276 if (verboseAsm) streamer.AddComment("FDE Length");
Rafael Espindola9266cc42011-04-27 01:43:49 +00001277 streamer.EmitAbsValue(Length, 4);
Rafael Espindola89b93722010-12-10 07:39:47 +00001278
1279 streamer.EmitLabel(fdeStart);
Rafael Espindolae3a0e982011-05-10 20:59:42 +00001280
Rafael Espindola89b93722010-12-10 07:39:47 +00001281 // CIE Pointer
Rafael Espindolae3a0e982011-05-10 20:59:42 +00001282 const MCAsmInfo &asmInfo = context.getAsmInfo();
Rafael Espindola0d450dc2011-05-10 15:20:23 +00001283 if (IsEH) {
1284 const MCExpr *offset = MakeStartMinusEndExpr(streamer, cieStart, *fdeStart,
1285 0);
Bill Wendling2541c412011-06-30 22:02:20 +00001286 if (verboseAsm) streamer.AddComment("FDE CIE Offset");
Rafael Espindola0d450dc2011-05-10 15:20:23 +00001287 streamer.EmitAbsValue(offset, 4);
Rafael Espindola2241e512012-06-22 13:24:07 +00001288 } else if (!asmInfo.doesDwarfUseRelocationsAcrossSections()) {
Rafael Espindolae3a0e982011-05-10 20:59:42 +00001289 const MCExpr *offset = MakeStartMinusEndExpr(streamer, *SectionStart,
1290 cieStart, 0);
1291 streamer.EmitAbsValue(offset, 4);
Rafael Espindola0d450dc2011-05-10 15:20:23 +00001292 } else {
1293 streamer.EmitSymbolValue(&cieStart, 4);
1294 }
Bill Wendling2541c412011-06-30 22:02:20 +00001295
Evan Cheng203576a2011-07-20 19:50:42 +00001296 unsigned fdeEncoding = MOFI->getFDEEncoding(UsingCFI);
Rafael Espindolaabf9af62011-04-22 00:08:43 +00001297 unsigned size = getSizeForEncoding(streamer, fdeEncoding);
Rafael Espindola89b93722010-12-10 07:39:47 +00001298
1299 // PC Begin
Rafael Espindola9989ef42011-05-10 22:28:35 +00001300 unsigned PCBeginEncoding = IsEH ? fdeEncoding :
1301 (unsigned)dwarf::DW_EH_PE_absptr;
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001302 unsigned PCBeginSize = getSizeForEncoding(streamer, PCBeginEncoding);
Bill Wendling2541c412011-06-30 22:02:20 +00001303 EmitSymbol(streamer, *frame.Begin, PCBeginEncoding, "FDE initial location");
Rafael Espindola89b93722010-12-10 07:39:47 +00001304
1305 // PC Range
1306 const MCExpr *Range = MakeStartMinusEndExpr(streamer, *frame.Begin,
1307 *frame.End, 0);
Bill Wendling2541c412011-06-30 22:02:20 +00001308 if (verboseAsm) streamer.AddComment("FDE address range");
Rafael Espindola9266cc42011-04-27 01:43:49 +00001309 streamer.EmitAbsValue(Range, size);
Rafael Espindola89b93722010-12-10 07:39:47 +00001310
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001311 if (IsEH) {
1312 // Augmentation Data Length
1313 unsigned augmentationLength = 0;
Rafael Espindola1f6c8752011-04-29 21:50:57 +00001314
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001315 if (frame.Lsda)
1316 augmentationLength += getSizeForEncoding(streamer, frame.LsdaEncoding);
Rafael Espindola1f6c8752011-04-29 21:50:57 +00001317
Bill Wendling2541c412011-06-30 22:02:20 +00001318 if (verboseAsm) streamer.AddComment("Augmentation size");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001319 streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola89b93722010-12-10 07:39:47 +00001320
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001321 // Augmentation Data
1322 if (frame.Lsda)
Bill Wendling2541c412011-06-30 22:02:20 +00001323 EmitSymbol(streamer, *frame.Lsda, frame.LsdaEncoding,
1324 "Language Specific Data Area");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001325 }
Rafael Espindola4892dff2011-04-29 03:06:29 +00001326
Rafael Espindola89b93722010-12-10 07:39:47 +00001327 // Call Frame Instructions
1328
Rafael Espindolab40a71f2010-12-29 01:42:56 +00001329 EmitCFIInstructions(streamer, frame.Instructions, frame.Begin);
Rafael Espindola5bba0842010-12-28 04:15:37 +00001330
Rafael Espindola89b93722010-12-10 07:39:47 +00001331 // Padding
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001332 streamer.EmitValueToAlignment(PCBeginSize);
Rafael Espindoladfe125c2010-12-17 00:28:02 +00001333
1334 return fdeEnd;
Rafael Espindola89b93722010-12-10 07:39:47 +00001335}
1336
Benjamin Kramer19282362010-12-30 22:34:44 +00001337namespace {
1338 struct CIEKey {
Rafael Espindola16d7d432012-01-23 21:51:52 +00001339 static const CIEKey getEmptyKey() { return CIEKey(0, 0, -1, false); }
1340 static const CIEKey getTombstoneKey() { return CIEKey(0, -1, 0, false); }
Rafael Espindolabdc31672010-12-27 15:56:22 +00001341
Benjamin Kramer19282362010-12-30 22:34:44 +00001342 CIEKey(const MCSymbol* Personality_, unsigned PersonalityEncoding_,
Rafael Espindola16d7d432012-01-23 21:51:52 +00001343 unsigned LsdaEncoding_, bool IsSignalFrame_) :
1344 Personality(Personality_), PersonalityEncoding(PersonalityEncoding_),
1345 LsdaEncoding(LsdaEncoding_), IsSignalFrame(IsSignalFrame_) {
Benjamin Kramer19282362010-12-30 22:34:44 +00001346 }
1347 const MCSymbol* Personality;
1348 unsigned PersonalityEncoding;
1349 unsigned LsdaEncoding;
Rafael Espindola16d7d432012-01-23 21:51:52 +00001350 bool IsSignalFrame;
Benjamin Kramer19282362010-12-30 22:34:44 +00001351 };
1352}
Rafael Espindolabdc31672010-12-27 15:56:22 +00001353
1354namespace llvm {
1355 template <>
1356 struct DenseMapInfo<CIEKey> {
1357 static CIEKey getEmptyKey() {
Benjamin Kramer19282362010-12-30 22:34:44 +00001358 return CIEKey::getEmptyKey();
Rafael Espindolabdc31672010-12-27 15:56:22 +00001359 }
1360 static CIEKey getTombstoneKey() {
Benjamin Kramer19282362010-12-30 22:34:44 +00001361 return CIEKey::getTombstoneKey();
Rafael Espindolabdc31672010-12-27 15:56:22 +00001362 }
1363 static unsigned getHashValue(const CIEKey &Key) {
Benjamin Kramer74849202012-04-11 14:06:39 +00001364 return static_cast<unsigned>(hash_combine(Key.Personality,
1365 Key.PersonalityEncoding,
1366 Key.LsdaEncoding,
1367 Key.IsSignalFrame));
Rafael Espindolabdc31672010-12-27 15:56:22 +00001368 }
1369 static bool isEqual(const CIEKey &LHS,
1370 const CIEKey &RHS) {
1371 return LHS.Personality == RHS.Personality &&
1372 LHS.PersonalityEncoding == RHS.PersonalityEncoding &&
Rafael Espindola16d7d432012-01-23 21:51:52 +00001373 LHS.LsdaEncoding == RHS.LsdaEncoding &&
1374 LHS.IsSignalFrame == RHS.IsSignalFrame;
Rafael Espindolabdc31672010-12-27 15:56:22 +00001375 }
1376 };
1377}
1378
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001379void MCDwarfFrameEmitter::Emit(MCStreamer &Streamer,
1380 bool UsingCFI,
1381 bool IsEH) {
1382 MCContext &Context = Streamer.getContext();
Evan Chenge76a33b2011-07-20 05:58:47 +00001383 MCObjectFileInfo *MOFI =
1384 const_cast<MCObjectFileInfo*>(Context.getObjectFileInfo());
Bill Wendling1424c3c2011-07-22 21:18:59 +00001385 FrameEmitterImpl Emitter(UsingCFI, IsEH);
Bill Wendlingd8ab8e92011-09-06 18:37:11 +00001386 ArrayRef<MCDwarfFrameInfo> FrameArray = Streamer.getFrameInfos();
Bill Wendling1424c3c2011-07-22 21:18:59 +00001387
Bill Wendlingd8ab8e92011-09-06 18:37:11 +00001388 // Emit the compact unwind info if available.
Bill Wendling7a13b722011-12-15 00:14:24 +00001389 if (IsEH && MOFI->getCompactUnwindSection())
Bill Wendling1424c3c2011-07-22 21:18:59 +00001390 for (unsigned i = 0, n = Streamer.getNumFrameInfos(); i < n; ++i) {
1391 const MCDwarfFrameInfo &Frame = Streamer.getFrameInfo(i);
Bill Wendlinga2ff3e22011-11-08 22:23:43 +00001392 if (Frame.CompactUnwindEncoding)
Bill Wendlingd8ab8e92011-09-06 18:37:11 +00001393 Emitter.EmitCompactUnwind(Streamer, Frame);
Bill Wendling1424c3c2011-07-22 21:18:59 +00001394 }
1395
Evan Chenge76a33b2011-07-20 05:58:47 +00001396 const MCSection &Section = IsEH ? *MOFI->getEHFrameSection() :
1397 *MOFI->getDwarfFrameSection();
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001398 Streamer.SwitchSection(&Section);
1399 MCSymbol *SectionStart = Context.CreateTempSymbol();
1400 Streamer.EmitLabel(SectionStart);
Bill Wendling1424c3c2011-07-22 21:18:59 +00001401 Emitter.setSectionStart(SectionStart);
Rafael Espindola90998132011-04-29 02:42:28 +00001402
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001403 MCSymbol *FDEEnd = NULL;
Rafael Espindolabdc31672010-12-27 15:56:22 +00001404 DenseMap<CIEKey, const MCSymbol*> CIEStarts;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00001405
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001406 const MCSymbol *DummyDebugKey = NULL;
Bill Wendling1424c3c2011-07-22 21:18:59 +00001407 for (unsigned i = 0, n = FrameArray.size(); i < n; ++i) {
1408 const MCDwarfFrameInfo &Frame = FrameArray[i];
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001409 CIEKey Key(Frame.Personality, Frame.PersonalityEncoding,
Rafael Espindola16d7d432012-01-23 21:51:52 +00001410 Frame.LsdaEncoding, Frame.IsSignalFrame);
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001411 const MCSymbol *&CIEStart = IsEH ? CIEStarts[Key] : DummyDebugKey;
1412 if (!CIEStart)
1413 CIEStart = &Emitter.EmitCIE(Streamer, Frame.Personality,
1414 Frame.PersonalityEncoding, Frame.Lsda,
Rafael Espindola16d7d432012-01-23 21:51:52 +00001415 Frame.IsSignalFrame,
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001416 Frame.LsdaEncoding);
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001417
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001418 FDEEnd = Emitter.EmitFDE(Streamer, *CIEStart, Frame);
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001419
Rafael Espindoladfe125c2010-12-17 00:28:02 +00001420 if (i != n - 1)
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001421 Streamer.EmitLabel(FDEEnd);
Rafael Espindoladfe125c2010-12-17 00:28:02 +00001422 }
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00001423
Evan Cheng1be0e272011-07-15 02:09:41 +00001424 Streamer.EmitValueToAlignment(Context.getAsmInfo().getPointerSize());
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001425 if (FDEEnd)
1426 Streamer.EmitLabel(FDEEnd);
Rafael Espindola89b93722010-12-10 07:39:47 +00001427}
Rafael Espindola245a1e22010-12-28 05:39:27 +00001428
1429void MCDwarfFrameEmitter::EmitAdvanceLoc(MCStreamer &Streamer,
1430 uint64_t AddrDelta) {
1431 SmallString<256> Tmp;
1432 raw_svector_ostream OS(Tmp);
Rafael Espindola4eafe102011-05-08 14:35:21 +00001433 MCDwarfFrameEmitter::EncodeAdvanceLoc(AddrDelta, OS);
Rafael Espindola245a1e22010-12-28 05:39:27 +00001434 Streamer.EmitBytes(OS.str(), /*AddrSpace=*/0);
1435}
1436
1437void MCDwarfFrameEmitter::EncodeAdvanceLoc(uint64_t AddrDelta,
Rafael Espindola4eafe102011-05-08 14:35:21 +00001438 raw_ostream &OS) {
Rafael Espindola245a1e22010-12-28 05:39:27 +00001439 // FIXME: Assumes the code alignment factor is 1.
Rafael Espindola3b78cdc2010-12-28 23:38:03 +00001440 if (AddrDelta == 0) {
Rafael Espindola4eafe102011-05-08 14:35:21 +00001441 } else if (isUIntN(6, AddrDelta)) {
Rafael Espindola245a1e22010-12-28 05:39:27 +00001442 uint8_t Opcode = dwarf::DW_CFA_advance_loc | AddrDelta;
1443 OS << Opcode;
Rafael Espindola4eafe102011-05-08 14:35:21 +00001444 } else if (isUInt<8>(AddrDelta)) {
Rafael Espindola245a1e22010-12-28 05:39:27 +00001445 OS << uint8_t(dwarf::DW_CFA_advance_loc1);
1446 OS << uint8_t(AddrDelta);
Rafael Espindola4eafe102011-05-08 14:35:21 +00001447 } else if (isUInt<16>(AddrDelta)) {
Rafael Espindolaa7e45052010-12-29 02:30:49 +00001448 // FIXME: check what is the correct behavior on a big endian machine.
Rafael Espindola245a1e22010-12-28 05:39:27 +00001449 OS << uint8_t(dwarf::DW_CFA_advance_loc2);
Rafael Espindolaa7e45052010-12-29 02:30:49 +00001450 OS << uint8_t( AddrDelta & 0xff);
1451 OS << uint8_t((AddrDelta >> 8) & 0xff);
Rafael Espindola245a1e22010-12-28 05:39:27 +00001452 } else {
Rafael Espindolaa7e45052010-12-29 02:30:49 +00001453 // FIXME: check what is the correct behavior on a big endian machine.
Rafael Espindola245a1e22010-12-28 05:39:27 +00001454 assert(isUInt<32>(AddrDelta));
1455 OS << uint8_t(dwarf::DW_CFA_advance_loc4);
Rafael Espindolaa7e45052010-12-29 02:30:49 +00001456 OS << uint8_t( AddrDelta & 0xff);
1457 OS << uint8_t((AddrDelta >> 8) & 0xff);
1458 OS << uint8_t((AddrDelta >> 16) & 0xff);
1459 OS << uint8_t((AddrDelta >> 24) & 0xff);
1460
Rafael Espindola245a1e22010-12-28 05:39:27 +00001461 }
1462}