blob: 679f4eeec1f9a43267c90b9d0522ef2504bc610a [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"
Kevin Enderbyc0957932010-09-30 16:52:03 +000011#include "llvm/MC/MCAssembler.h"
12#include "llvm/MC/MCSymbol.h"
13#include "llvm/MC/MCExpr.h"
14#include "llvm/MC/MCContext.h"
15#include "llvm/MC/MCObjectWriter.h"
16#include "llvm/ADT/SmallString.h"
Kevin Enderby7cbf73a2010-07-28 20:55:35 +000017#include "llvm/Support/Debug.h"
18#include "llvm/Support/raw_ostream.h"
Kevin Enderbyc0957932010-09-30 16:52:03 +000019#include "llvm/Target/TargetAsmBackend.h"
Kevin Enderby7cbf73a2010-07-28 20:55:35 +000020using namespace llvm;
21
Kevin Enderbyc0957932010-09-30 16:52:03 +000022// Given a special op, return the address skip amount (in units of
23// DWARF2_LINE_MIN_INSN_LENGTH.
24#define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE)
25
26// The maximum address skip amount that can be encoded with a special op.
27#define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255)
28
29// First special line opcode - leave room for the standard opcodes.
30// Note: If you want to change this, you'll have to update the
31// "standard_opcode_lengths" table that is emitted in DwarfFileTable::Emit().
32#define DWARF2_LINE_OPCODE_BASE 13
33
34// Minimum line offset in a special line info. opcode. This value
35// was chosen to give a reasonable range of values.
36#define DWARF2_LINE_BASE -5
37
38// Range of line offsets in a special line info. opcode.
39# define DWARF2_LINE_RANGE 14
40
41// Define the architecture-dependent minimum instruction length (in bytes).
42// This value should be rather too small than too big.
43# define DWARF2_LINE_MIN_INSN_LENGTH 1
44
45// Note: when DWARF2_LINE_MIN_INSN_LENGTH == 1 which is the current setting,
46// this routine is a nop and will be optimized away.
47static inline uint64_t ScaleAddrDelta(uint64_t AddrDelta)
48{
49 if (DWARF2_LINE_MIN_INSN_LENGTH == 1)
50 return AddrDelta;
51 if (AddrDelta % DWARF2_LINE_MIN_INSN_LENGTH != 0) {
52 // TODO: report this error, but really only once.
53 ;
54 }
55 return AddrDelta / DWARF2_LINE_MIN_INSN_LENGTH;
56}
57
58//
59// This is called when an instruction is assembled into the specified section
60// and if there is information from the last .loc directive that has yet to have
61// a line entry made for it is made.
62//
63void MCLineEntry::Make(MCObjectStreamer *MCOS, const MCSection *Section) {
64 if (!MCOS->getContext().getDwarfLocSeen())
65 return;
66
67 // Create a symbol at in the current section for use in the line entry.
68 MCSymbol *LineSym = MCOS->getContext().CreateTempSymbol();
69 // Set the value of the symbol to use for the MCLineEntry.
70 MCOS->EmitLabel(LineSym);
71
72 // Get the current .loc info saved in the context.
73 const MCDwarfLoc &DwarfLoc = MCOS->getContext().getCurrentDwarfLoc();
74
75 // Create a (local) line entry with the symbol and the current .loc info.
76 MCLineEntry LineEntry(LineSym, DwarfLoc);
77
78 // clear DwarfLocSeen saying the current .loc info is now used.
Kevin Enderby3f55c242010-10-04 20:17:24 +000079 MCOS->getContext().ClearDwarfLocSeen();
Kevin Enderbyc0957932010-09-30 16:52:03 +000080
81 // Get the MCLineSection for this section, if one does not exist for this
82 // section create it.
83 DenseMap<const MCSection *, MCLineSection *> &MCLineSections =
84 MCOS->getContext().getMCLineSections();
85 MCLineSection *LineSection = MCLineSections[Section];
86 if (!LineSection) {
87 // Create a new MCLineSection. This will be deleted after the dwarf line
88 // table is created using it by iterating through the MCLineSections
89 // DenseMap.
90 LineSection = new MCLineSection;
91 // Save a pointer to the new LineSection into the MCLineSections DenseMap.
92 MCLineSections[Section] = LineSection;
93 }
94
95 // Add the line entry to this section's entries.
96 LineSection->addLineEntry(LineEntry);
97}
98
99//
100// This helper routine returns an expression of End - Start + IntVal .
101//
102static inline const MCExpr *MakeStartMinusEndExpr(MCObjectStreamer *MCOS,
103 MCSymbol *Start,
104 MCSymbol *End, int IntVal) {
105 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
106 const MCExpr *Res =
107 MCSymbolRefExpr::Create(End, Variant, MCOS->getContext());
108 const MCExpr *RHS =
109 MCSymbolRefExpr::Create(Start, Variant, MCOS->getContext());
110 const MCExpr *Res1 =
111 MCBinaryExpr::Create(MCBinaryExpr::Sub, Res, RHS, MCOS->getContext());
112 const MCExpr *Res2 =
113 MCConstantExpr::Create(IntVal, MCOS->getContext());
114 const MCExpr *Res3 =
115 MCBinaryExpr::Create(MCBinaryExpr::Sub, Res1, Res2, MCOS->getContext());
116 return Res3;
117}
118
119//
120// This emits an "absolute" address used in the start of a dwarf line number
121// table. This will result in a relocatation entry for the address.
122//
123static inline void EmitDwarfSetAddress(MCObjectStreamer *MCOS,
124 MCSymbol *Symbol) {
125 MCOS->EmitIntValue(dwarf::DW_LNS_extended_op, 1);
126
127 int sizeof_address = MCOS->getAssembler().getBackend().getPointerSize();
Rafael Espindola3ff57092010-11-02 17:22:24 +0000128 MCOS->EmitULEB128IntValue(sizeof_address + 1);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000129
130 MCOS->EmitIntValue(dwarf::DW_LNE_set_address, 1);
131 MCOS->EmitSymbolValue(Symbol, sizeof_address);
132}
133
134//
135// This emits the Dwarf line table for the specified section from the entries
136// in the LineSection.
137//
Rafael Espindolabac0ad92010-11-12 18:41:26 +0000138static inline void EmitDwarfLineTable(MCObjectStreamer *MCOS,
Kevin Enderbyc0957932010-09-30 16:52:03 +0000139 const MCSection *Section,
140 MCLineSection *LineSection,
141 const MCSection *DwarfLineSection) {
142 unsigned FileNum = 1;
143 unsigned LastLine = 1;
144 unsigned Column = 0;
145 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
146 unsigned Isa = 0;
Kevin Enderbyc0957932010-09-30 16:52:03 +0000147 MCSymbol *LastLabel = NULL;
148 MCSectionData &DLS =
149 MCOS->getAssembler().getOrCreateSectionData(*DwarfLineSection);
150
151 // Loop through each MCLineEntry and encode the dwarf line number table.
152 for (MCLineSection::iterator
153 it = LineSection->getMCLineEntries()->begin(),
154 ie = LineSection->getMCLineEntries()->end(); it != ie; ++it) {
155
156 if (FileNum != it->getFileNum()) {
157 FileNum = it->getFileNum();
158 MCOS->EmitIntValue(dwarf::DW_LNS_set_file, 1);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000159 MCOS->EmitULEB128IntValue(FileNum);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000160 }
161 if (Column != it->getColumn()) {
162 Column = it->getColumn();
163 MCOS->EmitIntValue(dwarf::DW_LNS_set_column, 1);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000164 MCOS->EmitULEB128IntValue(Column);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000165 }
166 if (Isa != it->getIsa()) {
167 Isa = it->getIsa();
168 MCOS->EmitIntValue(dwarf::DW_LNS_set_isa, 1);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000169 MCOS->EmitULEB128IntValue(Isa);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000170 }
171 if ((it->getFlags() ^ Flags) & DWARF2_FLAG_IS_STMT) {
172 Flags = it->getFlags();
173 MCOS->EmitIntValue(dwarf::DW_LNS_negate_stmt, 1);
174 }
175 if (it->getFlags() & DWARF2_FLAG_BASIC_BLOCK)
176 MCOS->EmitIntValue(dwarf::DW_LNS_set_basic_block, 1);
177 if (it->getFlags() & DWARF2_FLAG_PROLOGUE_END)
178 MCOS->EmitIntValue(dwarf::DW_LNS_set_prologue_end, 1);
179 if (it->getFlags() & DWARF2_FLAG_EPILOGUE_BEGIN)
180 MCOS->EmitIntValue(dwarf::DW_LNS_set_epilogue_begin, 1);
181
Rafael Espindola64185cc2010-11-13 01:06:27 +0000182 int64_t LineDelta = static_cast<int64_t>(it->getLine()) - LastLine;
Kevin Enderbyc0957932010-09-30 16:52:03 +0000183 MCSymbol *Label = it->getLabel();
184
185 // At this point we want to emit/create the sequence to encode the delta in
186 // line numbers and the increment of the address from the previous Label
187 // and the current Label.
188 if (LastLabel == NULL) {
189 // emit the sequence to set the address
190 EmitDwarfSetAddress(MCOS, Label);
191 // emit the sequence for the LineDelta (from 1) and a zero address delta.
192 MCDwarfLineAddr::Emit(MCOS, LineDelta, 0);
193 }
194 else {
195 // Create an expression for the address delta from the LastLabel and
196 // this Label (plus 0).
197 const MCExpr *AddrDelta = MakeStartMinusEndExpr(MCOS, LastLabel, Label,0);
198 // Create a Dwarf Line fragment for the LineDelta and AddrDelta.
199 new MCDwarfLineAddrFragment(LineDelta, *AddrDelta, &DLS);
200 }
201
202 LastLine = it->getLine();
203 LastLabel = Label;
Kevin Enderbyc0957932010-09-30 16:52:03 +0000204 }
205
206 // Emit a DW_LNE_end_sequence for the end of the section.
207 // Using the pointer Section create a temporary label at the end of the
208 // section and use that and the LastLabel to compute the address delta
209 // and use INT64_MAX as the line delta which is the signal that this is
210 // actually a DW_LNE_end_sequence.
211
212 // Switch to the section to be able to create a symbol at its end.
213 MCOS->SwitchSection(Section);
214 // Create a symbol at the end of the section.
215 MCSymbol *SectionEnd = MCOS->getContext().CreateTempSymbol();
216 // Set the value of the symbol, as we are at the end of the section.
217 MCOS->EmitLabel(SectionEnd);
218
219 // Switch back the the dwarf line section.
220 MCOS->SwitchSection(DwarfLineSection);
221 // Create an expression for the address delta from the LastLabel and this
222 // SectionEnd label.
223 const MCExpr *AddrDelta = MakeStartMinusEndExpr(MCOS, LastLabel, SectionEnd,
224 0);
225 // Create a Dwarf Line fragment for the LineDelta and AddrDelta.
226 new MCDwarfLineAddrFragment(INT64_MAX, *AddrDelta, &DLS);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000227}
228
229//
230// This emits the Dwarf file and the line tables.
231//
232void MCDwarfFileTable::Emit(MCObjectStreamer *MCOS,
233 const MCSection *DwarfLineSection) {
234 // Switch to the section where the table will be emitted into.
235 MCOS->SwitchSection(DwarfLineSection);
236
237 // Create a symbol at the beginning of this section.
238 MCSymbol *LineStartSym = MCOS->getContext().CreateTempSymbol();
239 // Set the value of the symbol, as we are at the start of the section.
240 MCOS->EmitLabel(LineStartSym);
241
242 // Create a symbol for the end of the section (to be set when we get there).
243 MCSymbol *LineEndSym = MCOS->getContext().CreateTempSymbol();
244
245 // The first 4 bytes is the total length of the information for this
246 // compilation unit (not including these 4 bytes for the length).
247 MCOS->EmitValue(MakeStartMinusEndExpr(MCOS, LineStartSym, LineEndSym, 4),
248 4, 0);
249
250 // Next 2 bytes is the Version, which is Dwarf 2.
251 MCOS->EmitIntValue(2, 2);
252
253 // Create a symbol for the end of the prologue (to be set when we get there).
254 MCSymbol *ProEndSym = MCOS->getContext().CreateTempSymbol(); // Lprologue_end
255
256 // Length of the prologue, is the next 4 bytes. Which is the start of the
257 // section to the end of the prologue. Not including the 4 bytes for the
258 // total length, the 2 bytes for the version, and these 4 bytes for the
259 // length of the prologue.
260 MCOS->EmitValue(MakeStartMinusEndExpr(MCOS, LineStartSym, ProEndSym,
261 (4 + 2 + 4)),
262 4, 0);
263
264 // Parameters of the state machine, are next.
265 MCOS->EmitIntValue(DWARF2_LINE_MIN_INSN_LENGTH, 1);
266 MCOS->EmitIntValue(DWARF2_LINE_DEFAULT_IS_STMT, 1);
267 MCOS->EmitIntValue(DWARF2_LINE_BASE, 1);
268 MCOS->EmitIntValue(DWARF2_LINE_RANGE, 1);
269 MCOS->EmitIntValue(DWARF2_LINE_OPCODE_BASE, 1);
270
271 // Standard opcode lengths
272 MCOS->EmitIntValue(0, 1); // length of DW_LNS_copy
273 MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_pc
274 MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_line
275 MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_file
276 MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_column
277 MCOS->EmitIntValue(0, 1); // length of DW_LNS_negate_stmt
278 MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_basic_block
279 MCOS->EmitIntValue(0, 1); // length of DW_LNS_const_add_pc
280 MCOS->EmitIntValue(1, 1); // length of DW_LNS_fixed_advance_pc
281 MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_prologue_end
282 MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_epilogue_begin
283 MCOS->EmitIntValue(1, 1); // DW_LNS_set_isa
284
285 // Put out the directory and file tables.
286
287 // First the directory table.
288 const std::vector<StringRef> &MCDwarfDirs =
289 MCOS->getContext().getMCDwarfDirs();
290 for (unsigned i = 0; i < MCDwarfDirs.size(); i++) {
291 MCOS->EmitBytes(MCDwarfDirs[i], 0); // the DirectoryName
292 MCOS->EmitBytes(StringRef("\0", 1), 0); // the null term. of the string
293 }
294 MCOS->EmitIntValue(0, 1); // Terminate the directory list
295
296 // Second the file table.
297 const std::vector<MCDwarfFile *> &MCDwarfFiles =
298 MCOS->getContext().getMCDwarfFiles();
299 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
300 MCOS->EmitBytes(MCDwarfFiles[i]->getName(), 0); // FileName
301 MCOS->EmitBytes(StringRef("\0", 1), 0); // the null term. of the string
Rafael Espindola3ff57092010-11-02 17:22:24 +0000302 // the Directory num
303 MCOS->EmitULEB128IntValue(MCDwarfFiles[i]->getDirIndex());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000304 MCOS->EmitIntValue(0, 1); // last modification timestamp (always 0)
305 MCOS->EmitIntValue(0, 1); // filesize (always 0)
306 }
307 MCOS->EmitIntValue(0, 1); // Terminate the file list
308
309 // This is the end of the prologue, so set the value of the symbol at the
310 // end of the prologue (that was used in a previous expression).
311 MCOS->EmitLabel(ProEndSym);
312
313 // Put out the line tables.
Kevin Enderbyc0957932010-09-30 16:52:03 +0000314 DenseMap<const MCSection *, MCLineSection *> &MCLineSections =
315 MCOS->getContext().getMCLineSections();
316 for (DenseMap<const MCSection *, MCLineSection *>::iterator it =
317 MCLineSections.begin(), ie = MCLineSections.end(); it != ie; ++it) {
Rafael Espindolabac0ad92010-11-12 18:41:26 +0000318 EmitDwarfLineTable(MCOS, it->first, it->second, DwarfLineSection);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000319
320 // Now delete the MCLineSections that were created in MCLineEntry::Make()
321 // and used to emit the line table.
322 delete it->second;
323 }
324
Kevin Enderbyc0957932010-09-30 16:52:03 +0000325 // This is the end of the section, so set the value of the symbol at the end
326 // of this section (that was used in a previous expression).
327 MCOS->EmitLabel(LineEndSym);
328}
329
330/// Utility function to compute the size of the encoding.
331uint64_t MCDwarfLineAddr::ComputeSize(int64_t LineDelta, uint64_t AddrDelta) {
332 SmallString<256> Tmp;
333 raw_svector_ostream OS(Tmp);
334 MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OS);
335 return OS.GetNumBytesInBuffer();
336}
337
338/// Utility function to write the encoding to an object writer.
339void MCDwarfLineAddr::Write(MCObjectWriter *OW, int64_t LineDelta,
340 uint64_t AddrDelta) {
341 SmallString<256> Tmp;
342 raw_svector_ostream OS(Tmp);
343 MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OS);
344 OW->WriteBytes(OS.str());
345}
346
347/// Utility function to emit the encoding to a streamer.
348void MCDwarfLineAddr::Emit(MCObjectStreamer *MCOS, int64_t LineDelta,
349 uint64_t AddrDelta) {
350 SmallString<256> Tmp;
351 raw_svector_ostream OS(Tmp);
352 MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OS);
353 MCOS->EmitBytes(OS.str(), /*AddrSpace=*/0);
354}
355
356/// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
357void MCDwarfLineAddr::Encode(int64_t LineDelta, uint64_t AddrDelta,
358 raw_ostream &OS) {
359 uint64_t Temp, Opcode;
360 bool NeedCopy = false;
361
362 // Scale the address delta by the minimum instruction length.
363 AddrDelta = ScaleAddrDelta(AddrDelta);
364
365 // A LineDelta of INT64_MAX is a signal that this is actually a
366 // DW_LNE_end_sequence. We cannot use special opcodes here, since we want the
367 // end_sequence to emit the matrix entry.
368 if (LineDelta == INT64_MAX) {
369 if (AddrDelta == MAX_SPECIAL_ADDR_DELTA)
370 OS << char(dwarf::DW_LNS_const_add_pc);
371 else {
372 OS << char(dwarf::DW_LNS_advance_pc);
373 SmallString<32> Tmp;
374 raw_svector_ostream OSE(Tmp);
375 MCObjectWriter::EncodeULEB128(AddrDelta, OSE);
376 OS << OSE.str();
377 }
378 OS << char(dwarf::DW_LNS_extended_op);
379 OS << char(1);
380 OS << char(dwarf::DW_LNE_end_sequence);
381 return;
382 }
383
384 // Bias the line delta by the base.
385 Temp = LineDelta - DWARF2_LINE_BASE;
386
387 // If the line increment is out of range of a special opcode, we must encode
388 // it with DW_LNS_advance_line.
389 if (Temp >= DWARF2_LINE_RANGE) {
390 OS << char(dwarf::DW_LNS_advance_line);
391 SmallString<32> Tmp;
392 raw_svector_ostream OSE(Tmp);
393 MCObjectWriter::EncodeSLEB128(LineDelta, OSE);
394 OS << OSE.str();
395
396 LineDelta = 0;
397 Temp = 0 - DWARF2_LINE_BASE;
398 NeedCopy = true;
399 }
400
401 // Use DW_LNS_copy instead of a "line +0, addr +0" special opcode.
402 if (LineDelta == 0 && AddrDelta == 0) {
403 OS << char(dwarf::DW_LNS_copy);
404 return;
405 }
406
407 // Bias the opcode by the special opcode base.
408 Temp += DWARF2_LINE_OPCODE_BASE;
409
410 // Avoid overflow when addr_delta is large.
411 if (AddrDelta < 256 + MAX_SPECIAL_ADDR_DELTA) {
412 // Try using a special opcode.
413 Opcode = Temp + AddrDelta * DWARF2_LINE_RANGE;
414 if (Opcode <= 255) {
415 OS << char(Opcode);
416 return;
417 }
418
419 // Try using DW_LNS_const_add_pc followed by special op.
420 Opcode = Temp + (AddrDelta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
421 if (Opcode <= 255) {
422 OS << char(dwarf::DW_LNS_const_add_pc);
423 OS << char(Opcode);
424 return;
425 }
426 }
427
428 // Otherwise use DW_LNS_advance_pc.
429 OS << char(dwarf::DW_LNS_advance_pc);
430 SmallString<32> Tmp;
431 raw_svector_ostream OSE(Tmp);
432 MCObjectWriter::EncodeULEB128(AddrDelta, OSE);
433 OS << OSE.str();
434
435 if (NeedCopy)
436 OS << char(dwarf::DW_LNS_copy);
437 else
438 OS << char(Temp);
439}
440
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000441void MCDwarfFile::print(raw_ostream &OS) const {
442 OS << '"' << getName() << '"';
443}
444
445void MCDwarfFile::dump() const {
446 print(dbgs());
447}
Kevin Enderbyc0957932010-09-30 16:52:03 +0000448