1 //===- lib/MC/MCDwarf.cpp - MCDwarf implementation ------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/MC/MCDwarf.h"
11 #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"
17 #include "llvm/Support/Debug.h"
18 #include "llvm/Support/raw_ostream.h"
19 #include "llvm/Target/TargetAsmBackend.h"
22 // 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)
26 // The maximum address skip amount that can be encoded with a special op.
27 #define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255)
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
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
38 // Range of line offsets in a special line info. opcode.
39 # define DWARF2_LINE_RANGE 14
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
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.
47 static inline uint64_t ScaleAddrDelta(uint64_t AddrDelta
)
49 if (DWARF2_LINE_MIN_INSN_LENGTH
== 1)
51 if (AddrDelta
% DWARF2_LINE_MIN_INSN_LENGTH
!= 0) {
52 // TODO: report this error, but really only once.
55 return AddrDelta
/ DWARF2_LINE_MIN_INSN_LENGTH
;
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.
63 void MCLineEntry::Make(MCStreamer
*MCOS
, const MCSection
*Section
) {
64 if (!MCOS
->getContext().getDwarfLocSeen())
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
);
72 // Get the current .loc info saved in the context.
73 const MCDwarfLoc
&DwarfLoc
= MCOS
->getContext().getCurrentDwarfLoc();
75 // Create a (local) line entry with the symbol and the current .loc info.
76 MCLineEntry
LineEntry(LineSym
, DwarfLoc
);
78 // clear DwarfLocSeen saying the current .loc info is now used.
79 MCOS
->getContext().ClearDwarfLocSeen();
81 // Get the MCLineSection for this section, if one does not exist for this
83 const DenseMap
<const MCSection
*, MCLineSection
*> &MCLineSections
=
84 MCOS
->getContext().getMCLineSections();
85 MCLineSection
*LineSection
= MCLineSections
.lookup(Section
);
87 // Create a new MCLineSection. This will be deleted after the dwarf line
88 // table is created using it by iterating through the MCLineSections
90 LineSection
= new MCLineSection
;
91 // Save a pointer to the new LineSection into the MCLineSections DenseMap.
92 MCOS
->getContext().addMCLineSection(Section
, LineSection
);
95 // Add the line entry to this section's entries.
96 LineSection
->addLineEntry(LineEntry
);
100 // This helper routine returns an expression of End - Start + IntVal .
102 static inline const MCExpr
*MakeStartMinusEndExpr(MCStreamer
*MCOS
,
104 MCSymbol
*End
, int IntVal
) {
105 MCSymbolRefExpr::VariantKind Variant
= MCSymbolRefExpr::VK_None
;
107 MCSymbolRefExpr::Create(End
, Variant
, MCOS
->getContext());
109 MCSymbolRefExpr::Create(Start
, Variant
, MCOS
->getContext());
111 MCBinaryExpr::Create(MCBinaryExpr::Sub
, Res
, RHS
, MCOS
->getContext());
113 MCConstantExpr::Create(IntVal
, MCOS
->getContext());
115 MCBinaryExpr::Create(MCBinaryExpr::Sub
, Res1
, Res2
, MCOS
->getContext());
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.
123 static inline void EmitDwarfSetAddress(MCStreamer
*MCOS
,
126 MCOS
->EmitIntValue(dwarf::DW_LNS_extended_op
, 1);
128 MCOS
->EmitULEB128IntValue(PointerSize
+ 1);
130 MCOS
->EmitIntValue(dwarf::DW_LNE_set_address
, 1);
131 MCOS
->EmitSymbolValue(Symbol
, PointerSize
);
135 // This emits the Dwarf line table for the specified section from the entries
136 // in the LineSection.
138 static inline void EmitDwarfLineTable(MCStreamer
*MCOS
,
139 const MCSection
*Section
,
140 const MCLineSection
*LineSection
,
141 const MCSection
*DwarfLineSection
,
144 unsigned FileNum
= 1;
145 unsigned LastLine
= 1;
147 unsigned Flags
= DWARF2_LINE_DEFAULT_IS_STMT
? DWARF2_FLAG_IS_STMT
: 0;
149 MCSymbol
*LastLabel
= NULL
;
151 // Loop through each MCLineEntry and encode the dwarf line number table.
152 for (MCLineSection::const_iterator
153 it
= LineSection
->getMCLineEntries()->begin(),
154 ie
= LineSection
->getMCLineEntries()->end(); it
!= ie
; ++it
) {
156 if (FileNum
!= it
->getFileNum()) {
157 FileNum
= it
->getFileNum();
158 MCOS
->EmitIntValue(dwarf::DW_LNS_set_file
, 1);
159 MCOS
->EmitULEB128IntValue(FileNum
);
161 if (Column
!= it
->getColumn()) {
162 Column
= it
->getColumn();
163 MCOS
->EmitIntValue(dwarf::DW_LNS_set_column
, 1);
164 MCOS
->EmitULEB128IntValue(Column
);
166 if (Isa
!= it
->getIsa()) {
168 MCOS
->EmitIntValue(dwarf::DW_LNS_set_isa
, 1);
169 MCOS
->EmitULEB128IntValue(Isa
);
171 if ((it
->getFlags() ^ Flags
) & DWARF2_FLAG_IS_STMT
) {
172 Flags
= it
->getFlags();
173 MCOS
->EmitIntValue(dwarf::DW_LNS_negate_stmt
, 1);
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);
182 int64_t LineDelta
= static_cast<int64_t>(it
->getLine()) - LastLine
;
183 MCSymbol
*Label
= it
->getLabel();
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
|| DLS
== NULL
) {
189 // emit the sequence to set the address
190 EmitDwarfSetAddress(MCOS
, Label
, PointerSize
);
191 // emit the sequence for the LineDelta (from 1) and a zero address delta.
192 MCDwarfLineAddr::Emit(MCOS
, LineDelta
, 0);
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
);
202 LastLine
= it
->getLine();
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.
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
);
219 // Switch back the the dwarf line section.
220 MCOS
->SwitchSection(DwarfLineSection
);
223 // emit the sequence to set the address
224 EmitDwarfSetAddress(MCOS
, SectionEnd
, PointerSize
);
225 // emit the sequence for the LineDelta (from 1) and a zero address delta.
226 MCDwarfLineAddr::Emit(MCOS
, INT64_MAX
, 0);
228 // Create an expression for the address delta from the LastLabel and this
230 const MCExpr
*AddrDelta
= MakeStartMinusEndExpr(MCOS
, LastLabel
, SectionEnd
,
232 // Create a Dwarf Line fragment for the LineDelta and AddrDelta.
233 new MCDwarfLineAddrFragment(INT64_MAX
, *AddrDelta
, DLS
);
238 // This emits the Dwarf file and the line tables.
240 void MCDwarfFileTable::Emit(MCStreamer
*MCOS
,
241 const MCSection
*DwarfLineSection
,
244 // Switch to the section where the table will be emitted into.
245 MCOS
->SwitchSection(DwarfLineSection
);
247 // Create a symbol at the beginning of this section.
248 MCSymbol
*LineStartSym
= MCOS
->getContext().CreateTempSymbol();
249 // Set the value of the symbol, as we are at the start of the section.
250 MCOS
->EmitLabel(LineStartSym
);
252 // Create a symbol for the end of the section (to be set when we get there).
253 MCSymbol
*LineEndSym
= MCOS
->getContext().CreateTempSymbol();
255 // The first 4 bytes is the total length of the information for this
256 // compilation unit (not including these 4 bytes for the length).
257 MCOS
->EmitValue(MakeStartMinusEndExpr(MCOS
, LineStartSym
, LineEndSym
, 4),
260 // Next 2 bytes is the Version, which is Dwarf 2.
261 MCOS
->EmitIntValue(2, 2);
263 // Create a symbol for the end of the prologue (to be set when we get there).
264 MCSymbol
*ProEndSym
= MCOS
->getContext().CreateTempSymbol(); // Lprologue_end
266 // Length of the prologue, is the next 4 bytes. Which is the start of the
267 // section to the end of the prologue. Not including the 4 bytes for the
268 // total length, the 2 bytes for the version, and these 4 bytes for the
269 // length of the prologue.
270 MCOS
->EmitValue(MakeStartMinusEndExpr(MCOS
, LineStartSym
, ProEndSym
,
274 // Parameters of the state machine, are next.
275 MCOS
->EmitIntValue(DWARF2_LINE_MIN_INSN_LENGTH
, 1);
276 MCOS
->EmitIntValue(DWARF2_LINE_DEFAULT_IS_STMT
, 1);
277 MCOS
->EmitIntValue(DWARF2_LINE_BASE
, 1);
278 MCOS
->EmitIntValue(DWARF2_LINE_RANGE
, 1);
279 MCOS
->EmitIntValue(DWARF2_LINE_OPCODE_BASE
, 1);
281 // Standard opcode lengths
282 MCOS
->EmitIntValue(0, 1); // length of DW_LNS_copy
283 MCOS
->EmitIntValue(1, 1); // length of DW_LNS_advance_pc
284 MCOS
->EmitIntValue(1, 1); // length of DW_LNS_advance_line
285 MCOS
->EmitIntValue(1, 1); // length of DW_LNS_set_file
286 MCOS
->EmitIntValue(1, 1); // length of DW_LNS_set_column
287 MCOS
->EmitIntValue(0, 1); // length of DW_LNS_negate_stmt
288 MCOS
->EmitIntValue(0, 1); // length of DW_LNS_set_basic_block
289 MCOS
->EmitIntValue(0, 1); // length of DW_LNS_const_add_pc
290 MCOS
->EmitIntValue(1, 1); // length of DW_LNS_fixed_advance_pc
291 MCOS
->EmitIntValue(0, 1); // length of DW_LNS_set_prologue_end
292 MCOS
->EmitIntValue(0, 1); // length of DW_LNS_set_epilogue_begin
293 MCOS
->EmitIntValue(1, 1); // DW_LNS_set_isa
295 // Put out the directory and file tables.
297 // First the directory table.
298 const std::vector
<StringRef
> &MCDwarfDirs
=
299 MCOS
->getContext().getMCDwarfDirs();
300 for (unsigned i
= 0; i
< MCDwarfDirs
.size(); i
++) {
301 MCOS
->EmitBytes(MCDwarfDirs
[i
], 0); // the DirectoryName
302 MCOS
->EmitBytes(StringRef("\0", 1), 0); // the null term. of the string
304 MCOS
->EmitIntValue(0, 1); // Terminate the directory list
306 // Second the file table.
307 const std::vector
<MCDwarfFile
*> &MCDwarfFiles
=
308 MCOS
->getContext().getMCDwarfFiles();
309 for (unsigned i
= 1; i
< MCDwarfFiles
.size(); i
++) {
310 MCOS
->EmitBytes(MCDwarfFiles
[i
]->getName(), 0); // FileName
311 MCOS
->EmitBytes(StringRef("\0", 1), 0); // the null term. of the string
313 MCOS
->EmitULEB128IntValue(MCDwarfFiles
[i
]->getDirIndex());
314 MCOS
->EmitIntValue(0, 1); // last modification timestamp (always 0)
315 MCOS
->EmitIntValue(0, 1); // filesize (always 0)
317 MCOS
->EmitIntValue(0, 1); // Terminate the file list
319 // This is the end of the prologue, so set the value of the symbol at the
320 // end of the prologue (that was used in a previous expression).
321 MCOS
->EmitLabel(ProEndSym
);
323 // Put out the line tables.
324 const DenseMap
<const MCSection
*, MCLineSection
*> &MCLineSections
=
325 MCOS
->getContext().getMCLineSections();
326 const std::vector
<const MCSection
*> &MCLineSectionOrder
=
327 MCOS
->getContext().getMCLineSectionOrder();
328 for (std::vector
<const MCSection
*>::const_iterator it
=
329 MCLineSectionOrder
.begin(), ie
= MCLineSectionOrder
.end(); it
!= ie
;
331 const MCSection
*Sec
= *it
;
332 const MCLineSection
*Line
= MCLineSections
.lookup(Sec
);
333 EmitDwarfLineTable(MCOS
, Sec
, Line
, DwarfLineSection
, DLS
,
336 // Now delete the MCLineSections that were created in MCLineEntry::Make()
337 // and used to emit the line table.
341 // This is the end of the section, so set the value of the symbol at the end
342 // of this section (that was used in a previous expression).
343 MCOS
->EmitLabel(LineEndSym
);
346 /// Utility function to compute the size of the encoding.
347 uint64_t MCDwarfLineAddr::ComputeSize(int64_t LineDelta
, uint64_t AddrDelta
) {
348 SmallString
<256> Tmp
;
349 raw_svector_ostream
OS(Tmp
);
350 MCDwarfLineAddr::Encode(LineDelta
, AddrDelta
, OS
);
351 return OS
.GetNumBytesInBuffer();
354 /// Utility function to write the encoding to an object writer.
355 void MCDwarfLineAddr::Write(MCObjectWriter
*OW
, int64_t LineDelta
,
356 uint64_t AddrDelta
) {
357 SmallString
<256> Tmp
;
358 raw_svector_ostream
OS(Tmp
);
359 MCDwarfLineAddr::Encode(LineDelta
, AddrDelta
, OS
);
360 OW
->WriteBytes(OS
.str());
363 /// Utility function to emit the encoding to a streamer.
364 void MCDwarfLineAddr::Emit(MCStreamer
*MCOS
, int64_t LineDelta
,
365 uint64_t AddrDelta
) {
366 SmallString
<256> Tmp
;
367 raw_svector_ostream
OS(Tmp
);
368 MCDwarfLineAddr::Encode(LineDelta
, AddrDelta
, OS
);
369 MCOS
->EmitBytes(OS
.str(), /*AddrSpace=*/0);
372 /// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
373 void MCDwarfLineAddr::Encode(int64_t LineDelta
, uint64_t AddrDelta
,
375 uint64_t Temp
, Opcode
;
376 bool NeedCopy
= false;
378 // Scale the address delta by the minimum instruction length.
379 AddrDelta
= ScaleAddrDelta(AddrDelta
);
381 // A LineDelta of INT64_MAX is a signal that this is actually a
382 // DW_LNE_end_sequence. We cannot use special opcodes here, since we want the
383 // end_sequence to emit the matrix entry.
384 if (LineDelta
== INT64_MAX
) {
385 if (AddrDelta
== MAX_SPECIAL_ADDR_DELTA
)
386 OS
<< char(dwarf::DW_LNS_const_add_pc
);
388 OS
<< char(dwarf::DW_LNS_advance_pc
);
390 raw_svector_ostream
OSE(Tmp
);
391 MCObjectWriter::EncodeULEB128(AddrDelta
, OSE
);
394 OS
<< char(dwarf::DW_LNS_extended_op
);
396 OS
<< char(dwarf::DW_LNE_end_sequence
);
400 // Bias the line delta by the base.
401 Temp
= LineDelta
- DWARF2_LINE_BASE
;
403 // If the line increment is out of range of a special opcode, we must encode
404 // it with DW_LNS_advance_line.
405 if (Temp
>= DWARF2_LINE_RANGE
) {
406 OS
<< char(dwarf::DW_LNS_advance_line
);
408 raw_svector_ostream
OSE(Tmp
);
409 MCObjectWriter::EncodeSLEB128(LineDelta
, OSE
);
413 Temp
= 0 - DWARF2_LINE_BASE
;
417 // Use DW_LNS_copy instead of a "line +0, addr +0" special opcode.
418 if (LineDelta
== 0 && AddrDelta
== 0) {
419 OS
<< char(dwarf::DW_LNS_copy
);
423 // Bias the opcode by the special opcode base.
424 Temp
+= DWARF2_LINE_OPCODE_BASE
;
426 // Avoid overflow when addr_delta is large.
427 if (AddrDelta
< 256 + MAX_SPECIAL_ADDR_DELTA
) {
428 // Try using a special opcode.
429 Opcode
= Temp
+ AddrDelta
* DWARF2_LINE_RANGE
;
435 // Try using DW_LNS_const_add_pc followed by special op.
436 Opcode
= Temp
+ (AddrDelta
- MAX_SPECIAL_ADDR_DELTA
) * DWARF2_LINE_RANGE
;
438 OS
<< char(dwarf::DW_LNS_const_add_pc
);
444 // Otherwise use DW_LNS_advance_pc.
445 OS
<< char(dwarf::DW_LNS_advance_pc
);
447 raw_svector_ostream
OSE(Tmp
);
448 MCObjectWriter::EncodeULEB128(AddrDelta
, OSE
);
452 OS
<< char(dwarf::DW_LNS_copy
);
457 void MCDwarfFile::print(raw_ostream
&OS
) const {
458 OS
<< '"' << getName() << '"';
461 void MCDwarfFile::dump() const {