Dwarf: support for LTO where a single object file can have multiple line tables
[oota-llvm.git] / lib / MC / MCDwarf.cpp
1 //===- 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"
11 #include "llvm/ADT/Hashing.h"
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/ADT/Twine.h"
14 #include "llvm/Config/config.h"
15 #include "llvm/MC/MCAsmInfo.h"
16 #include "llvm/MC/MCContext.h"
17 #include "llvm/MC/MCExpr.h"
18 #include "llvm/MC/MCObjectFileInfo.h"
19 #include "llvm/MC/MCObjectWriter.h"
20 #include "llvm/MC/MCRegisterInfo.h"
21 #include "llvm/MC/MCStreamer.h"
22 #include "llvm/MC/MCSymbol.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/LEB128.h"
26 #include "llvm/Support/Path.h"
27 #include "llvm/Support/SourceMgr.h"
28 #include "llvm/Support/raw_ostream.h"
29 using namespace llvm;
30
31 // Given a special op, return the address skip amount (in units of
32 // DWARF2_LINE_MIN_INSN_LENGTH.
33 #define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE)
34
35 // The maximum address skip amount that can be encoded with a special op.
36 #define MAX_SPECIAL_ADDR_DELTA         SPECIAL_ADDR(255)
37
38 // First special line opcode - leave room for the standard opcodes.
39 // Note: If you want to change this, you'll have to update the
40 // "standard_opcode_lengths" table that is emitted in DwarfFileTable::Emit().
41 #define DWARF2_LINE_OPCODE_BASE         13
42
43 // Minimum line offset in a special line info. opcode.  This value
44 // was chosen to give a reasonable range of values.
45 #define DWARF2_LINE_BASE                -5
46
47 // Range of line offsets in a special line info. opcode.
48 #define DWARF2_LINE_RANGE               14
49
50 // Define the architecture-dependent minimum instruction length (in bytes).
51 // This value should be rather too small than too big.
52 #define DWARF2_LINE_MIN_INSN_LENGTH     1
53
54 // Note: when DWARF2_LINE_MIN_INSN_LENGTH == 1 which is the current setting,
55 // this routine is a nop and will be optimized away.
56 static inline uint64_t ScaleAddrDelta(uint64_t AddrDelta) {
57   if (DWARF2_LINE_MIN_INSN_LENGTH == 1)
58     return AddrDelta;
59   if (AddrDelta % DWARF2_LINE_MIN_INSN_LENGTH != 0) {
60     // TODO: report this error, but really only once.
61     ;
62   }
63   return AddrDelta / DWARF2_LINE_MIN_INSN_LENGTH;
64 }
65
66 //
67 // This is called when an instruction is assembled into the specified section
68 // and if there is information from the last .loc directive that has yet to have
69 // a line entry made for it is made.
70 //
71 void MCLineEntry::Make(MCStreamer *MCOS, const MCSection *Section) {
72   if (!MCOS->getContext().getDwarfLocSeen())
73     return;
74
75   // Create a symbol at in the current section for use in the line entry.
76   MCSymbol *LineSym = MCOS->getContext().CreateTempSymbol();
77   // Set the value of the symbol to use for the MCLineEntry.
78   MCOS->EmitLabel(LineSym);
79
80   // Get the current .loc info saved in the context.
81   const MCDwarfLoc &DwarfLoc = MCOS->getContext().getCurrentDwarfLoc();
82
83   // Create a (local) line entry with the symbol and the current .loc info.
84   MCLineEntry LineEntry(LineSym, DwarfLoc);
85
86   // clear DwarfLocSeen saying the current .loc info is now used.
87   MCOS->getContext().ClearDwarfLocSeen();
88
89   // Get the MCLineSection for this section, if one does not exist for this
90   // section create it.
91   const DenseMap<const MCSection *, MCLineSection *> &MCLineSections =
92     MCOS->getContext().getMCLineSections();
93   MCLineSection *LineSection = MCLineSections.lookup(Section);
94   if (!LineSection) {
95     // Create a new MCLineSection.  This will be deleted after the dwarf line
96     // table is created using it by iterating through the MCLineSections
97     // DenseMap.
98     LineSection = new MCLineSection;
99     // Save a pointer to the new LineSection into the MCLineSections DenseMap.
100     MCOS->getContext().addMCLineSection(Section, LineSection);
101   }
102
103   // Add the line entry to this section's entries.
104   LineSection->addLineEntry(LineEntry,
105                             MCOS->getContext().getDwarfCompileUnitID());
106 }
107
108 //
109 // This helper routine returns an expression of End - Start + IntVal .
110 //
111 static inline const MCExpr *MakeStartMinusEndExpr(const MCStreamer &MCOS,
112                                                   const MCSymbol &Start,
113                                                   const MCSymbol &End,
114                                                   int IntVal) {
115   MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
116   const MCExpr *Res =
117     MCSymbolRefExpr::Create(&End, Variant, MCOS.getContext());
118   const MCExpr *RHS =
119     MCSymbolRefExpr::Create(&Start, Variant, MCOS.getContext());
120   const MCExpr *Res1 =
121     MCBinaryExpr::Create(MCBinaryExpr::Sub, Res, RHS, MCOS.getContext());
122   const MCExpr *Res2 =
123     MCConstantExpr::Create(IntVal, MCOS.getContext());
124   const MCExpr *Res3 =
125     MCBinaryExpr::Create(MCBinaryExpr::Sub, Res1, Res2, MCOS.getContext());
126   return Res3;
127 }
128
129 //
130 // This emits the Dwarf line table for the specified section from the entries
131 // in the LineSection.
132 //
133 static inline void EmitDwarfLineTable(MCStreamer *MCOS,
134                                       const MCSection *Section,
135                                       const MCLineSection *LineSection,
136                                       unsigned CUID) {
137   // This LineSection does not contain any LineEntry for the given Compile Unit.
138   if (!LineSection->containEntriesForID(CUID))
139     return;
140
141   unsigned FileNum = 1;
142   unsigned LastLine = 1;
143   unsigned Column = 0;
144   unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
145   unsigned Isa = 0;
146   MCSymbol *LastLabel = NULL;
147
148   // Loop through each MCLineEntry and encode the dwarf line number table.
149   for (MCLineSection::const_iterator
150          it = LineSection->getMCLineEntries(CUID).begin(),
151          ie = LineSection->getMCLineEntries(CUID).end(); it != ie; ++it) {
152
153     if (FileNum != it->getFileNum()) {
154       FileNum = it->getFileNum();
155       MCOS->EmitIntValue(dwarf::DW_LNS_set_file, 1);
156       MCOS->EmitULEB128IntValue(FileNum);
157     }
158     if (Column != it->getColumn()) {
159       Column = it->getColumn();
160       MCOS->EmitIntValue(dwarf::DW_LNS_set_column, 1);
161       MCOS->EmitULEB128IntValue(Column);
162     }
163     if (Isa != it->getIsa()) {
164       Isa = it->getIsa();
165       MCOS->EmitIntValue(dwarf::DW_LNS_set_isa, 1);
166       MCOS->EmitULEB128IntValue(Isa);
167     }
168     if ((it->getFlags() ^ Flags) & DWARF2_FLAG_IS_STMT) {
169       Flags = it->getFlags();
170       MCOS->EmitIntValue(dwarf::DW_LNS_negate_stmt, 1);
171     }
172     if (it->getFlags() & DWARF2_FLAG_BASIC_BLOCK)
173       MCOS->EmitIntValue(dwarf::DW_LNS_set_basic_block, 1);
174     if (it->getFlags() & DWARF2_FLAG_PROLOGUE_END)
175       MCOS->EmitIntValue(dwarf::DW_LNS_set_prologue_end, 1);
176     if (it->getFlags() & DWARF2_FLAG_EPILOGUE_BEGIN)
177       MCOS->EmitIntValue(dwarf::DW_LNS_set_epilogue_begin, 1);
178
179     int64_t LineDelta = static_cast<int64_t>(it->getLine()) - LastLine;
180     MCSymbol *Label = it->getLabel();
181
182     // At this point we want to emit/create the sequence to encode the delta in
183     // line numbers and the increment of the address from the previous Label
184     // and the current Label.
185     const MCAsmInfo &asmInfo = MCOS->getContext().getAsmInfo();
186     MCOS->EmitDwarfAdvanceLineAddr(LineDelta, LastLabel, Label,
187                                    asmInfo.getPointerSize());
188
189     LastLine = it->getLine();
190     LastLabel = Label;
191   }
192
193   // Emit a DW_LNE_end_sequence for the end of the section.
194   // Using the pointer Section create a temporary label at the end of the
195   // section and use that and the LastLabel to compute the address delta
196   // and use INT64_MAX as the line delta which is the signal that this is
197   // actually a DW_LNE_end_sequence.
198
199   // Switch to the section to be able to create a symbol at its end.
200   MCOS->SwitchSection(Section);
201
202   MCContext &context = MCOS->getContext();
203   // Create a symbol at the end of the section.
204   MCSymbol *SectionEnd = context.CreateTempSymbol();
205   // Set the value of the symbol, as we are at the end of the section.
206   MCOS->EmitLabel(SectionEnd);
207
208   // Switch back the dwarf line section.
209   MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfLineSection());
210
211   const MCAsmInfo &asmInfo = MCOS->getContext().getAsmInfo();
212   MCOS->EmitDwarfAdvanceLineAddr(INT64_MAX, LastLabel, SectionEnd,
213                                  asmInfo.getPointerSize());
214 }
215
216 //
217 // This emits the Dwarf file and the line tables.
218 //
219 const MCSymbol *MCDwarfFileTable::Emit(MCStreamer *MCOS) {
220   MCContext &context = MCOS->getContext();
221   // Switch to the section where the table will be emitted into.
222   MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfLineSection());
223
224   const DenseMap<unsigned, MCSymbol *> &MCLineTableSymbols =
225     MCOS->getContext().getMCLineTableSymbols();
226   // CUID and MCLineTableSymbols are set in DwarfDebug, when DwarfDebug does
227   // not exist, CUID will be 0 and MCLineTableSymbols will be empty.
228   // Handle Compile Unit 0, the line table start symbol is the section symbol.
229   const MCSymbol *LineStartSym = EmitCU(MCOS, 0);
230   // Handle the rest of the Compile Units.
231   for (unsigned Is = 1, Ie = MCLineTableSymbols.size(); Is < Ie; Is++)
232     EmitCU(MCOS, Is);
233
234   // Now delete the MCLineSections that were created in MCLineEntry::Make()
235   // and used to emit the line table.
236   const DenseMap<const MCSection *, MCLineSection *> &MCLineSections =
237     MCOS->getContext().getMCLineSections();
238   for (DenseMap<const MCSection *, MCLineSection *>::const_iterator it =
239        MCLineSections.begin(), ie = MCLineSections.end(); it != ie;
240        ++it)
241     delete it->second;
242
243   return LineStartSym;
244 }
245
246 const MCSymbol *MCDwarfFileTable::EmitCU(MCStreamer *MCOS, unsigned CUID) {
247   MCContext &context = MCOS->getContext();
248
249   // Create a symbol at the beginning of the line table.
250   MCSymbol *LineStartSym = MCOS->getContext().getMCLineTableSymbol(CUID);
251   if (!LineStartSym)
252     LineStartSym = context.CreateTempSymbol();
253   // Set the value of the symbol, as we are at the start of the line table.
254   MCOS->EmitLabel(LineStartSym);
255
256   // Create a symbol for the end of the section (to be set when we get there).
257   MCSymbol *LineEndSym = context.CreateTempSymbol();
258
259   // The first 4 bytes is the total length of the information for this
260   // compilation unit (not including these 4 bytes for the length).
261   MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *LineEndSym,4),
262                      4);
263
264   // Next 2 bytes is the Version, which is Dwarf 2.
265   MCOS->EmitIntValue(2, 2);
266
267   // Create a symbol for the end of the prologue (to be set when we get there).
268   MCSymbol *ProEndSym = context.CreateTempSymbol(); // Lprologue_end
269
270   // Length of the prologue, is the next 4 bytes.  Which is the start of the
271   // section to the end of the prologue.  Not including the 4 bytes for the
272   // total length, the 2 bytes for the version, and these 4 bytes for the
273   // length of the prologue.
274   MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *ProEndSym,
275                                         (4 + 2 + 4)),
276                   4, 0);
277
278   // Parameters of the state machine, are next.
279   MCOS->EmitIntValue(DWARF2_LINE_MIN_INSN_LENGTH, 1);
280   MCOS->EmitIntValue(DWARF2_LINE_DEFAULT_IS_STMT, 1);
281   MCOS->EmitIntValue(DWARF2_LINE_BASE, 1);
282   MCOS->EmitIntValue(DWARF2_LINE_RANGE, 1);
283   MCOS->EmitIntValue(DWARF2_LINE_OPCODE_BASE, 1);
284
285   // Standard opcode lengths
286   MCOS->EmitIntValue(0, 1); // length of DW_LNS_copy
287   MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_pc
288   MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_line
289   MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_file
290   MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_column
291   MCOS->EmitIntValue(0, 1); // length of DW_LNS_negate_stmt
292   MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_basic_block
293   MCOS->EmitIntValue(0, 1); // length of DW_LNS_const_add_pc
294   MCOS->EmitIntValue(1, 1); // length of DW_LNS_fixed_advance_pc
295   MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_prologue_end
296   MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_epilogue_begin
297   MCOS->EmitIntValue(1, 1); // DW_LNS_set_isa
298
299   // Put out the directory and file tables.
300
301   // First the directory table.
302   const std::vector<StringRef> &MCDwarfDirs =
303     context.getMCDwarfDirs();
304   for (unsigned i = 0; i < MCDwarfDirs.size(); i++) {
305     MCOS->EmitBytes(MCDwarfDirs[i]); // the DirectoryName
306     MCOS->EmitBytes(StringRef("\0", 1)); // the null term. of the string
307   }
308   MCOS->EmitIntValue(0, 1); // Terminate the directory list
309
310   // Second the file table.
311   const std::vector<MCDwarfFile *> &MCDwarfFiles =
312     MCOS->getContext().getMCDwarfFiles();
313   for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
314     MCOS->EmitBytes(MCDwarfFiles[i]->getName()); // FileName
315     MCOS->EmitBytes(StringRef("\0", 1)); // the null term. of the string
316     // the Directory num
317     MCOS->EmitULEB128IntValue(MCDwarfFiles[i]->getDirIndex());
318     MCOS->EmitIntValue(0, 1); // last modification timestamp (always 0)
319     MCOS->EmitIntValue(0, 1); // filesize (always 0)
320   }
321   MCOS->EmitIntValue(0, 1); // Terminate the file list
322
323   // This is the end of the prologue, so set the value of the symbol at the
324   // end of the prologue (that was used in a previous expression).
325   MCOS->EmitLabel(ProEndSym);
326
327   // Put out the line tables.
328   const DenseMap<const MCSection *, MCLineSection *> &MCLineSections =
329     MCOS->getContext().getMCLineSections();
330   const std::vector<const MCSection *> &MCLineSectionOrder =
331     MCOS->getContext().getMCLineSectionOrder();
332   for (std::vector<const MCSection*>::const_iterator it =
333          MCLineSectionOrder.begin(), ie = MCLineSectionOrder.end(); it != ie;
334        ++it) {
335     const MCSection *Sec = *it;
336     const MCLineSection *Line = MCLineSections.lookup(Sec);
337     EmitDwarfLineTable(MCOS, Sec, Line, CUID);
338   }
339
340   if (MCOS->getContext().getAsmInfo().getLinkerRequiresNonEmptyDwarfLines()
341       && MCLineSectionOrder.begin() == MCLineSectionOrder.end()) {
342     // The darwin9 linker has a bug (see PR8715). For for 32-bit architectures
343     // it requires:
344     // total_length >= prologue_length + 10
345     // We are 4 bytes short, since we have total_length = 51 and
346     // prologue_length = 45
347
348     // The regular end_sequence should be sufficient.
349     MCDwarfLineAddr::Emit(MCOS, INT64_MAX, 0);
350   }
351
352   // This is the end of the section, so set the value of the symbol at the end
353   // of this section (that was used in a previous expression).
354   MCOS->EmitLabel(LineEndSym);
355
356   return LineStartSym;
357 }
358
359 /// Utility function to write the encoding to an object writer.
360 void MCDwarfLineAddr::Write(MCObjectWriter *OW, int64_t LineDelta,
361                             uint64_t AddrDelta) {
362   SmallString<256> Tmp;
363   raw_svector_ostream OS(Tmp);
364   MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OS);
365   OW->WriteBytes(OS.str());
366 }
367
368 /// Utility function to emit the encoding to a streamer.
369 void MCDwarfLineAddr::Emit(MCStreamer *MCOS, int64_t LineDelta,
370                            uint64_t AddrDelta) {
371   SmallString<256> Tmp;
372   raw_svector_ostream OS(Tmp);
373   MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OS);
374   MCOS->EmitBytes(OS.str());
375 }
376
377 /// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
378 void MCDwarfLineAddr::Encode(int64_t LineDelta, uint64_t AddrDelta,
379                              raw_ostream &OS) {
380   uint64_t Temp, Opcode;
381   bool NeedCopy = false;
382
383   // Scale the address delta by the minimum instruction length.
384   AddrDelta = ScaleAddrDelta(AddrDelta);
385
386   // A LineDelta of INT64_MAX is a signal that this is actually a
387   // DW_LNE_end_sequence. We cannot use special opcodes here, since we want the
388   // end_sequence to emit the matrix entry.
389   if (LineDelta == INT64_MAX) {
390     if (AddrDelta == MAX_SPECIAL_ADDR_DELTA)
391       OS << char(dwarf::DW_LNS_const_add_pc);
392     else {
393       OS << char(dwarf::DW_LNS_advance_pc);
394       encodeULEB128(AddrDelta, OS);
395     }
396     OS << char(dwarf::DW_LNS_extended_op);
397     OS << char(1);
398     OS << char(dwarf::DW_LNE_end_sequence);
399     return;
400   }
401
402   // Bias the line delta by the base.
403   Temp = LineDelta - DWARF2_LINE_BASE;
404
405   // If the line increment is out of range of a special opcode, we must encode
406   // it with DW_LNS_advance_line.
407   if (Temp >= DWARF2_LINE_RANGE) {
408     OS << char(dwarf::DW_LNS_advance_line);
409     encodeSLEB128(LineDelta, OS);
410
411     LineDelta = 0;
412     Temp = 0 - DWARF2_LINE_BASE;
413     NeedCopy = true;
414   }
415
416   // Use DW_LNS_copy instead of a "line +0, addr +0" special opcode.
417   if (LineDelta == 0 && AddrDelta == 0) {
418     OS << char(dwarf::DW_LNS_copy);
419     return;
420   }
421
422   // Bias the opcode by the special opcode base.
423   Temp += DWARF2_LINE_OPCODE_BASE;
424
425   // Avoid overflow when addr_delta is large.
426   if (AddrDelta < 256 + MAX_SPECIAL_ADDR_DELTA) {
427     // Try using a special opcode.
428     Opcode = Temp + AddrDelta * DWARF2_LINE_RANGE;
429     if (Opcode <= 255) {
430       OS << char(Opcode);
431       return;
432     }
433
434     // Try using DW_LNS_const_add_pc followed by special op.
435     Opcode = Temp + (AddrDelta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
436     if (Opcode <= 255) {
437       OS << char(dwarf::DW_LNS_const_add_pc);
438       OS << char(Opcode);
439       return;
440     }
441   }
442
443   // Otherwise use DW_LNS_advance_pc.
444   OS << char(dwarf::DW_LNS_advance_pc);
445   encodeULEB128(AddrDelta, OS);
446
447   if (NeedCopy)
448     OS << char(dwarf::DW_LNS_copy);
449   else
450     OS << char(Temp);
451 }
452
453 void MCDwarfFile::print(raw_ostream &OS) const {
454   OS << '"' << getName() << '"';
455 }
456
457 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
458 void MCDwarfFile::dump() const {
459   print(dbgs());
460 }
461 #endif
462
463 // Utility function to write a tuple for .debug_abbrev.
464 static void EmitAbbrev(MCStreamer *MCOS, uint64_t Name, uint64_t Form) {
465   MCOS->EmitULEB128IntValue(Name);
466   MCOS->EmitULEB128IntValue(Form);
467 }
468
469 // When generating dwarf for assembly source files this emits
470 // the data for .debug_abbrev section which contains three DIEs.
471 static void EmitGenDwarfAbbrev(MCStreamer *MCOS) {
472   MCContext &context = MCOS->getContext();
473   MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
474
475   // DW_TAG_compile_unit DIE abbrev (1).
476   MCOS->EmitULEB128IntValue(1);
477   MCOS->EmitULEB128IntValue(dwarf::DW_TAG_compile_unit);
478   MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
479   EmitAbbrev(MCOS, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4);
480   EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
481   EmitAbbrev(MCOS, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr);
482   EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
483   EmitAbbrev(MCOS, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string);
484   StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
485   if (!DwarfDebugFlags.empty())
486     EmitAbbrev(MCOS, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string);
487   EmitAbbrev(MCOS, dwarf::DW_AT_producer, dwarf::DW_FORM_string);
488   EmitAbbrev(MCOS, dwarf::DW_AT_language, dwarf::DW_FORM_data2);
489   EmitAbbrev(MCOS, 0, 0);
490
491   // DW_TAG_label DIE abbrev (2).
492   MCOS->EmitULEB128IntValue(2);
493   MCOS->EmitULEB128IntValue(dwarf::DW_TAG_label);
494   MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
495   EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
496   EmitAbbrev(MCOS, dwarf::DW_AT_decl_file, dwarf::DW_FORM_data4);
497   EmitAbbrev(MCOS, dwarf::DW_AT_decl_line, dwarf::DW_FORM_data4);
498   EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
499   EmitAbbrev(MCOS, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag);
500   EmitAbbrev(MCOS, 0, 0);
501
502   // DW_TAG_unspecified_parameters DIE abbrev (3).
503   MCOS->EmitULEB128IntValue(3);
504   MCOS->EmitULEB128IntValue(dwarf::DW_TAG_unspecified_parameters);
505   MCOS->EmitIntValue(dwarf::DW_CHILDREN_no, 1);
506   EmitAbbrev(MCOS, 0, 0);
507
508   // Terminate the abbreviations for this compilation unit.
509   MCOS->EmitIntValue(0, 1);
510 }
511
512 // When generating dwarf for assembly source files this emits the data for
513 // .debug_aranges section.  Which contains a header and a table of pairs of
514 // PointerSize'ed values for the address and size of section(s) with line table
515 // entries (just the default .text in our case) and a terminating pair of zeros.
516 static void EmitGenDwarfAranges(MCStreamer *MCOS,
517                                 const MCSymbol *InfoSectionSymbol) {
518   MCContext &context = MCOS->getContext();
519
520   // Create a symbol at the end of the section that we are creating the dwarf
521   // debugging info to use later in here as part of the expression to calculate
522   // the size of the section for the table.
523   MCOS->SwitchSection(context.getGenDwarfSection());
524   MCSymbol *SectionEndSym = context.CreateTempSymbol();
525   MCOS->EmitLabel(SectionEndSym);
526   context.setGenDwarfSectionEndSym(SectionEndSym);
527
528   MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
529
530   // This will be the length of the .debug_aranges section, first account for
531   // the size of each item in the header (see below where we emit these items).
532   int Length = 4 + 2 + 4 + 1 + 1;
533
534   // Figure the padding after the header before the table of address and size
535   // pairs who's values are PointerSize'ed.
536   const MCAsmInfo &asmInfo = context.getAsmInfo();
537   int AddrSize = asmInfo.getPointerSize();
538   int Pad = 2 * AddrSize - (Length & (2 * AddrSize - 1));
539   if (Pad == 2 * AddrSize)
540     Pad = 0;
541   Length += Pad;
542
543   // Add the size of the pair of PointerSize'ed values for the address and size
544   // of the one default .text section we have in the table.
545   Length += 2 * AddrSize;
546   // And the pair of terminating zeros.
547   Length += 2 * AddrSize;
548
549
550   // Emit the header for this section.
551   // The 4 byte length not including the 4 byte value for the length.
552   MCOS->EmitIntValue(Length - 4, 4);
553   // The 2 byte version, which is 2.
554   MCOS->EmitIntValue(2, 2);
555   // The 4 byte offset to the compile unit in the .debug_info from the start
556   // of the .debug_info.
557   if (InfoSectionSymbol)
558     MCOS->EmitSymbolValue(InfoSectionSymbol, 4);
559   else
560     MCOS->EmitIntValue(0, 4);
561   // The 1 byte size of an address.
562   MCOS->EmitIntValue(AddrSize, 1);
563   // The 1 byte size of a segment descriptor, we use a value of zero.
564   MCOS->EmitIntValue(0, 1);
565   // Align the header with the padding if needed, before we put out the table.
566   for(int i = 0; i < Pad; i++)
567     MCOS->EmitIntValue(0, 1);
568
569   // Now emit the table of pairs of PointerSize'ed values for the section(s)
570   // address and size, in our case just the one default .text section.
571   const MCExpr *Addr = MCSymbolRefExpr::Create(
572     context.getGenDwarfSectionStartSym(), MCSymbolRefExpr::VK_None, context);
573   const MCExpr *Size = MakeStartMinusEndExpr(*MCOS,
574     *context.getGenDwarfSectionStartSym(), *SectionEndSym, 0);
575   MCOS->EmitAbsValue(Addr, AddrSize);
576   MCOS->EmitAbsValue(Size, AddrSize);
577
578   // And finally the pair of terminating zeros.
579   MCOS->EmitIntValue(0, AddrSize);
580   MCOS->EmitIntValue(0, AddrSize);
581 }
582
583 // When generating dwarf for assembly source files this emits the data for
584 // .debug_info section which contains three parts.  The header, the compile_unit
585 // DIE and a list of label DIEs.
586 static void EmitGenDwarfInfo(MCStreamer *MCOS,
587                              const MCSymbol *AbbrevSectionSymbol,
588                              const MCSymbol *LineSectionSymbol) {
589   MCContext &context = MCOS->getContext();
590
591   MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
592
593   // Create a symbol at the start and end of this section used in here for the
594   // expression to calculate the length in the header.
595   MCSymbol *InfoStart = context.CreateTempSymbol();
596   MCOS->EmitLabel(InfoStart);
597   MCSymbol *InfoEnd = context.CreateTempSymbol();
598
599   // First part: the header.
600
601   // The 4 byte total length of the information for this compilation unit, not
602   // including these 4 bytes.
603   const MCExpr *Length = MakeStartMinusEndExpr(*MCOS, *InfoStart, *InfoEnd, 4);
604   MCOS->EmitAbsValue(Length, 4);
605
606   // The 2 byte DWARF version, which is 2.
607   MCOS->EmitIntValue(2, 2);
608
609   // The 4 byte offset to the debug abbrevs from the start of the .debug_abbrev,
610   // it is at the start of that section so this is zero.
611   if (AbbrevSectionSymbol) {
612     MCOS->EmitSymbolValue(AbbrevSectionSymbol, 4);
613   } else {
614     MCOS->EmitIntValue(0, 4);
615   }
616
617   const MCAsmInfo &asmInfo = context.getAsmInfo();
618   int AddrSize = asmInfo.getPointerSize();
619   // The 1 byte size of an address.
620   MCOS->EmitIntValue(AddrSize, 1);
621
622   // Second part: the compile_unit DIE.
623
624   // The DW_TAG_compile_unit DIE abbrev (1).
625   MCOS->EmitULEB128IntValue(1);
626
627   // DW_AT_stmt_list, a 4 byte offset from the start of the .debug_line section,
628   // which is at the start of that section so this is zero.
629   if (LineSectionSymbol) {
630     MCOS->EmitSymbolValue(LineSectionSymbol, 4);
631   } else {
632     MCOS->EmitIntValue(0, 4);
633   }
634
635   // AT_low_pc, the first address of the default .text section.
636   const MCExpr *Start = MCSymbolRefExpr::Create(
637     context.getGenDwarfSectionStartSym(), MCSymbolRefExpr::VK_None, context);
638   MCOS->EmitAbsValue(Start, AddrSize);
639
640   // AT_high_pc, the last address of the default .text section.
641   const MCExpr *End = MCSymbolRefExpr::Create(
642     context.getGenDwarfSectionEndSym(), MCSymbolRefExpr::VK_None, context);
643   MCOS->EmitAbsValue(End, AddrSize);
644
645   // AT_name, the name of the source file.  Reconstruct from the first directory
646   // and file table entries.
647   const std::vector<StringRef> &MCDwarfDirs =
648     context.getMCDwarfDirs();
649   if (MCDwarfDirs.size() > 0) {
650     MCOS->EmitBytes(MCDwarfDirs[0]);
651     MCOS->EmitBytes("/");
652   }
653   const std::vector<MCDwarfFile *> &MCDwarfFiles =
654     MCOS->getContext().getMCDwarfFiles();
655   MCOS->EmitBytes(MCDwarfFiles[1]->getName());
656   MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
657
658   // AT_comp_dir, the working directory the assembly was done in.
659   MCOS->EmitBytes(context.getCompilationDir());
660   MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
661
662   // AT_APPLE_flags, the command line arguments of the assembler tool.
663   StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
664   if (!DwarfDebugFlags.empty()){
665     MCOS->EmitBytes(DwarfDebugFlags);
666     MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
667   }
668
669   // AT_producer, the version of the assembler tool.
670   StringRef DwarfDebugProducer = context.getDwarfDebugProducer();
671   if (!DwarfDebugProducer.empty()){
672     MCOS->EmitBytes(DwarfDebugProducer);
673   }
674   else {
675     MCOS->EmitBytes(StringRef("llvm-mc (based on LLVM "));
676     MCOS->EmitBytes(StringRef(PACKAGE_VERSION));
677     MCOS->EmitBytes(StringRef(")"));
678   }
679   MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
680
681   // AT_language, a 4 byte value.  We use DW_LANG_Mips_Assembler as the dwarf2
682   // draft has no standard code for assembler.
683   MCOS->EmitIntValue(dwarf::DW_LANG_Mips_Assembler, 2);
684
685   // Third part: the list of label DIEs.
686
687   // Loop on saved info for dwarf labels and create the DIEs for them.
688   const std::vector<const MCGenDwarfLabelEntry *> &Entries =
689     MCOS->getContext().getMCGenDwarfLabelEntries();
690   for (std::vector<const MCGenDwarfLabelEntry *>::const_iterator it =
691        Entries.begin(), ie = Entries.end(); it != ie;
692        ++it) {
693     const MCGenDwarfLabelEntry *Entry = *it;
694
695     // The DW_TAG_label DIE abbrev (2).
696     MCOS->EmitULEB128IntValue(2);
697
698     // AT_name, of the label without any leading underbar.
699     MCOS->EmitBytes(Entry->getName());
700     MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
701
702     // AT_decl_file, index into the file table.
703     MCOS->EmitIntValue(Entry->getFileNumber(), 4);
704
705     // AT_decl_line, source line number.
706     MCOS->EmitIntValue(Entry->getLineNumber(), 4);
707
708     // AT_low_pc, start address of the label.
709     const MCExpr *AT_low_pc = MCSymbolRefExpr::Create(Entry->getLabel(),
710                                              MCSymbolRefExpr::VK_None, context);
711     MCOS->EmitAbsValue(AT_low_pc, AddrSize);
712
713     // DW_AT_prototyped, a one byte flag value of 0 saying we have no prototype.
714     MCOS->EmitIntValue(0, 1);
715
716     // The DW_TAG_unspecified_parameters DIE abbrev (3).
717     MCOS->EmitULEB128IntValue(3);
718
719     // Add the NULL DIE terminating the DW_TAG_unspecified_parameters DIE's.
720     MCOS->EmitIntValue(0, 1);
721   }
722   // Deallocate the MCGenDwarfLabelEntry classes that saved away the info
723   // for the dwarf labels.
724   for (std::vector<const MCGenDwarfLabelEntry *>::const_iterator it =
725        Entries.begin(), ie = Entries.end(); it != ie;
726        ++it) {
727     const MCGenDwarfLabelEntry *Entry = *it;
728     delete Entry;
729   }
730
731   // Add the NULL DIE terminating the Compile Unit DIE's.
732   MCOS->EmitIntValue(0, 1);
733
734   // Now set the value of the symbol at the end of the info section.
735   MCOS->EmitLabel(InfoEnd);
736 }
737
738 //
739 // When generating dwarf for assembly source files this emits the Dwarf
740 // sections.
741 //
742 void MCGenDwarfInfo::Emit(MCStreamer *MCOS, const MCSymbol *LineSectionSymbol) {
743   // Create the dwarf sections in this order (.debug_line already created).
744   MCContext &context = MCOS->getContext();
745   const MCAsmInfo &AsmInfo = context.getAsmInfo();
746   bool CreateDwarfSectionSymbols =
747       AsmInfo.doesDwarfUseRelocationsAcrossSections();
748   if (!CreateDwarfSectionSymbols)
749     LineSectionSymbol = NULL;
750   MCSymbol *AbbrevSectionSymbol = NULL;
751   MCSymbol *InfoSectionSymbol = NULL;
752   MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
753   if (CreateDwarfSectionSymbols) {
754     InfoSectionSymbol = context.CreateTempSymbol();
755     MCOS->EmitLabel(InfoSectionSymbol);
756   }
757   MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
758   if (CreateDwarfSectionSymbols) {
759     AbbrevSectionSymbol = context.CreateTempSymbol();
760     MCOS->EmitLabel(AbbrevSectionSymbol);
761   }
762   MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
763
764   // If there are no line table entries then do not emit any section contents.
765   if (context.getMCLineSections().empty())
766     return;
767
768   // Output the data for .debug_aranges section.
769   EmitGenDwarfAranges(MCOS, InfoSectionSymbol);
770
771   // Output the data for .debug_abbrev section.
772   EmitGenDwarfAbbrev(MCOS);
773
774   // Output the data for .debug_info section.
775   EmitGenDwarfInfo(MCOS, AbbrevSectionSymbol, LineSectionSymbol);
776 }
777
778 //
779 // When generating dwarf for assembly source files this is called when symbol
780 // for a label is created.  If this symbol is not a temporary and is in the
781 // section that dwarf is being generated for, save the needed info to create
782 // a dwarf label.
783 //
784 void MCGenDwarfLabelEntry::Make(MCSymbol *Symbol, MCStreamer *MCOS,
785                                      SourceMgr &SrcMgr, SMLoc &Loc) {
786   // We won't create dwarf labels for temporary symbols or symbols not in
787   // the default text.
788   if (Symbol->isTemporary())
789     return;
790   MCContext &context = MCOS->getContext();
791   if (context.getGenDwarfSection() != MCOS->getCurrentSection())
792     return;
793
794   // The dwarf label's name does not have the symbol name's leading
795   // underbar if any.
796   StringRef Name = Symbol->getName();
797   if (Name.startswith("_"))
798     Name = Name.substr(1, Name.size()-1);
799
800   // Get the dwarf file number to be used for the dwarf label.
801   unsigned FileNumber = context.getGenDwarfFileNumber();
802
803   // Finding the line number is the expensive part which is why we just don't
804   // pass it in as for some symbols we won't create a dwarf label.
805   int CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
806   unsigned LineNumber = SrcMgr.FindLineNumber(Loc, CurBuffer);
807
808   // We create a temporary symbol for use for the AT_high_pc and AT_low_pc
809   // values so that they don't have things like an ARM thumb bit from the
810   // original symbol. So when used they won't get a low bit set after
811   // relocation.
812   MCSymbol *Label = context.CreateTempSymbol();
813   MCOS->EmitLabel(Label);
814
815   // Create and entry for the info and add it to the other entries.
816   MCGenDwarfLabelEntry *Entry =
817     new MCGenDwarfLabelEntry(Name, FileNumber, LineNumber, Label);
818   MCOS->getContext().addMCGenDwarfLabelEntry(Entry);
819 }
820
821 static int getDataAlignmentFactor(MCStreamer &streamer) {
822   MCContext &context = streamer.getContext();
823   const MCAsmInfo &asmInfo = context.getAsmInfo();
824   int size = asmInfo.getCalleeSaveStackSlotSize();
825   if (asmInfo.isStackGrowthDirectionUp())
826     return size;
827   else
828     return -size;
829 }
830
831 static unsigned getSizeForEncoding(MCStreamer &streamer,
832                                    unsigned symbolEncoding) {
833   MCContext &context = streamer.getContext();
834   unsigned format = symbolEncoding & 0x0f;
835   switch (format) {
836   default: llvm_unreachable("Unknown Encoding");
837   case dwarf::DW_EH_PE_absptr:
838   case dwarf::DW_EH_PE_signed:
839     return context.getAsmInfo().getPointerSize();
840   case dwarf::DW_EH_PE_udata2:
841   case dwarf::DW_EH_PE_sdata2:
842     return 2;
843   case dwarf::DW_EH_PE_udata4:
844   case dwarf::DW_EH_PE_sdata4:
845     return 4;
846   case dwarf::DW_EH_PE_udata8:
847   case dwarf::DW_EH_PE_sdata8:
848     return 8;
849   }
850 }
851
852 static void EmitSymbol(MCStreamer &streamer, const MCSymbol &symbol,
853                        unsigned symbolEncoding, const char *comment = 0) {
854   MCContext &context = streamer.getContext();
855   const MCAsmInfo &asmInfo = context.getAsmInfo();
856   const MCExpr *v = asmInfo.getExprForFDESymbol(&symbol,
857                                                 symbolEncoding,
858                                                 streamer);
859   unsigned size = getSizeForEncoding(streamer, symbolEncoding);
860   if (streamer.isVerboseAsm() && comment) streamer.AddComment(comment);
861   streamer.EmitAbsValue(v, size);
862 }
863
864 static void EmitPersonality(MCStreamer &streamer, const MCSymbol &symbol,
865                             unsigned symbolEncoding) {
866   MCContext &context = streamer.getContext();
867   const MCAsmInfo &asmInfo = context.getAsmInfo();
868   const MCExpr *v = asmInfo.getExprForPersonalitySymbol(&symbol,
869                                                         symbolEncoding,
870                                                         streamer);
871   unsigned size = getSizeForEncoding(streamer, symbolEncoding);
872   streamer.EmitValue(v, size);
873 }
874
875 static const MachineLocation TranslateMachineLocation(
876                                                   const MCRegisterInfo &MRI,
877                                                   const MachineLocation &Loc) {
878   unsigned Reg = Loc.getReg() == MachineLocation::VirtualFP ?
879     MachineLocation::VirtualFP :
880     unsigned(MRI.getDwarfRegNum(Loc.getReg(), true));
881   const MachineLocation &NewLoc = Loc.isReg() ?
882     MachineLocation(Reg) : MachineLocation(Reg, Loc.getOffset());
883   return NewLoc;
884 }
885
886 namespace {
887   class FrameEmitterImpl {
888     int CFAOffset;
889     int CIENum;
890     bool UsingCFI;
891     bool IsEH;
892     const MCSymbol *SectionStart;
893   public:
894     FrameEmitterImpl(bool usingCFI, bool isEH)
895       : CFAOffset(0), CIENum(0), UsingCFI(usingCFI), IsEH(isEH),
896         SectionStart(0) {}
897
898     void setSectionStart(const MCSymbol *Label) { SectionStart = Label; }
899
900     /// EmitCompactUnwind - Emit the unwind information in a compact way. If
901     /// we're successful, return 'true'. Otherwise, return 'false' and it will
902     /// emit the normal CIE and FDE.
903     bool EmitCompactUnwind(MCStreamer &streamer,
904                            const MCDwarfFrameInfo &frame);
905
906     const MCSymbol &EmitCIE(MCStreamer &streamer,
907                             const MCSymbol *personality,
908                             unsigned personalityEncoding,
909                             const MCSymbol *lsda,
910                             bool IsSignalFrame,
911                             unsigned lsdaEncoding);
912     MCSymbol *EmitFDE(MCStreamer &streamer,
913                       const MCSymbol &cieStart,
914                       const MCDwarfFrameInfo &frame);
915     void EmitCFIInstructions(MCStreamer &streamer,
916                              const std::vector<MCCFIInstruction> &Instrs,
917                              MCSymbol *BaseLabel);
918     void EmitCFIInstruction(MCStreamer &Streamer,
919                             const MCCFIInstruction &Instr);
920   };
921
922 } // end anonymous namespace
923
924 static void EmitEncodingByte(MCStreamer &Streamer, unsigned Encoding,
925                              StringRef Prefix) {
926   if (Streamer.isVerboseAsm()) {
927     const char *EncStr;
928     switch (Encoding) {
929     default: EncStr = "<unknown encoding>"; break;
930     case dwarf::DW_EH_PE_absptr: EncStr = "absptr"; break;
931     case dwarf::DW_EH_PE_omit:   EncStr = "omit"; break;
932     case dwarf::DW_EH_PE_pcrel:  EncStr = "pcrel"; break;
933     case dwarf::DW_EH_PE_udata4: EncStr = "udata4"; break;
934     case dwarf::DW_EH_PE_udata8: EncStr = "udata8"; break;
935     case dwarf::DW_EH_PE_sdata4: EncStr = "sdata4"; break;
936     case dwarf::DW_EH_PE_sdata8: EncStr = "sdata8"; break;
937     case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
938       EncStr = "pcrel udata4";
939       break;
940     case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
941       EncStr = "pcrel sdata4";
942       break;
943     case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
944       EncStr = "pcrel udata8";
945       break;
946     case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
947       EncStr = "screl sdata8";
948       break;
949     case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata4:
950       EncStr = "indirect pcrel udata4";
951       break;
952     case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata4:
953       EncStr = "indirect pcrel sdata4";
954       break;
955     case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata8:
956       EncStr = "indirect pcrel udata8";
957       break;
958     case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata8:
959       EncStr = "indirect pcrel sdata8";
960       break;
961     }
962
963     Streamer.AddComment(Twine(Prefix) + " = " + EncStr);
964   }
965
966   Streamer.EmitIntValue(Encoding, 1);
967 }
968
969 void FrameEmitterImpl::EmitCFIInstruction(MCStreamer &Streamer,
970                                           const MCCFIInstruction &Instr) {
971   int dataAlignmentFactor = getDataAlignmentFactor(Streamer);
972   bool VerboseAsm = Streamer.isVerboseAsm();
973
974   switch (Instr.getOperation()) {
975   case MCCFIInstruction::OpRegister: {
976     unsigned Reg1 = Instr.getRegister();
977     unsigned Reg2 = Instr.getRegister2();
978     if (VerboseAsm) {
979       Streamer.AddComment("DW_CFA_register");
980       Streamer.AddComment(Twine("Reg1 ") + Twine(Reg1));
981       Streamer.AddComment(Twine("Reg2 ") + Twine(Reg2));
982     }
983     Streamer.EmitIntValue(dwarf::DW_CFA_register, 1);
984     Streamer.EmitULEB128IntValue(Reg1);
985     Streamer.EmitULEB128IntValue(Reg2);
986     return;
987   }
988   case MCCFIInstruction::OpUndefined: {
989     unsigned Reg = Instr.getRegister();
990     if (VerboseAsm) {
991       Streamer.AddComment("DW_CFA_undefined");
992       Streamer.AddComment(Twine("Reg ") + Twine(Reg));
993     }
994     Streamer.EmitIntValue(dwarf::DW_CFA_undefined, 1);
995     Streamer.EmitULEB128IntValue(Reg);
996     return;
997   }
998   case MCCFIInstruction::OpAdjustCfaOffset:
999   case MCCFIInstruction::OpDefCfaOffset: {
1000     const bool IsRelative =
1001       Instr.getOperation() == MCCFIInstruction::OpAdjustCfaOffset;
1002
1003     if (VerboseAsm)
1004       Streamer.AddComment("DW_CFA_def_cfa_offset");
1005     Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_offset, 1);
1006
1007     if (IsRelative)
1008       CFAOffset += Instr.getOffset();
1009     else
1010       CFAOffset = -Instr.getOffset();
1011
1012     if (VerboseAsm)
1013       Streamer.AddComment(Twine("Offset " + Twine(CFAOffset)));
1014     Streamer.EmitULEB128IntValue(CFAOffset);
1015
1016     return;
1017   }
1018   case MCCFIInstruction::OpDefCfa: {
1019     if (VerboseAsm)
1020       Streamer.AddComment("DW_CFA_def_cfa");
1021     Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa, 1);
1022
1023     if (VerboseAsm)
1024       Streamer.AddComment(Twine("Reg ") + Twine(Instr.getRegister()));
1025     Streamer.EmitULEB128IntValue(Instr.getRegister());
1026
1027     CFAOffset = -Instr.getOffset();
1028
1029     if (VerboseAsm)
1030       Streamer.AddComment(Twine("Offset " + Twine(CFAOffset)));
1031     Streamer.EmitULEB128IntValue(CFAOffset);
1032
1033     return;
1034   }
1035
1036   case MCCFIInstruction::OpDefCfaRegister: {
1037     if (VerboseAsm)
1038       Streamer.AddComment("DW_CFA_def_cfa_register");
1039     Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_register, 1);
1040
1041     if (VerboseAsm)
1042       Streamer.AddComment(Twine("Reg ") + Twine(Instr.getRegister()));
1043     Streamer.EmitULEB128IntValue(Instr.getRegister());
1044
1045     return;
1046   }
1047
1048   case MCCFIInstruction::OpOffset:
1049   case MCCFIInstruction::OpRelOffset: {
1050     const bool IsRelative =
1051       Instr.getOperation() == MCCFIInstruction::OpRelOffset;
1052
1053     unsigned Reg = Instr.getRegister();
1054     int Offset = Instr.getOffset();
1055     if (IsRelative)
1056       Offset -= CFAOffset;
1057     Offset = Offset / dataAlignmentFactor;
1058
1059     if (Offset < 0) {
1060       if (VerboseAsm) Streamer.AddComment("DW_CFA_offset_extended_sf");
1061       Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended_sf, 1);
1062       if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
1063       Streamer.EmitULEB128IntValue(Reg);
1064       if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
1065       Streamer.EmitSLEB128IntValue(Offset);
1066     } else if (Reg < 64) {
1067       if (VerboseAsm) Streamer.AddComment(Twine("DW_CFA_offset + Reg(") +
1068                                           Twine(Reg) + ")");
1069       Streamer.EmitIntValue(dwarf::DW_CFA_offset + Reg, 1);
1070       if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
1071       Streamer.EmitULEB128IntValue(Offset);
1072     } else {
1073       if (VerboseAsm) Streamer.AddComment("DW_CFA_offset_extended");
1074       Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended, 1);
1075       if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
1076       Streamer.EmitULEB128IntValue(Reg);
1077       if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
1078       Streamer.EmitULEB128IntValue(Offset);
1079     }
1080     return;
1081   }
1082   case MCCFIInstruction::OpRememberState:
1083     if (VerboseAsm) Streamer.AddComment("DW_CFA_remember_state");
1084     Streamer.EmitIntValue(dwarf::DW_CFA_remember_state, 1);
1085     return;
1086   case MCCFIInstruction::OpRestoreState:
1087     if (VerboseAsm) Streamer.AddComment("DW_CFA_restore_state");
1088     Streamer.EmitIntValue(dwarf::DW_CFA_restore_state, 1);
1089     return;
1090   case MCCFIInstruction::OpSameValue: {
1091     unsigned Reg = Instr.getRegister();
1092     if (VerboseAsm) Streamer.AddComment("DW_CFA_same_value");
1093     Streamer.EmitIntValue(dwarf::DW_CFA_same_value, 1);
1094     if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
1095     Streamer.EmitULEB128IntValue(Reg);
1096     return;
1097   }
1098   case MCCFIInstruction::OpRestore: {
1099     unsigned Reg = Instr.getRegister();
1100     if (VerboseAsm) {
1101       Streamer.AddComment("DW_CFA_restore");
1102       Streamer.AddComment(Twine("Reg ") + Twine(Reg));
1103     }
1104     Streamer.EmitIntValue(dwarf::DW_CFA_restore | Reg, 1);
1105     return;
1106   }
1107   case MCCFIInstruction::OpEscape:
1108     if (VerboseAsm) Streamer.AddComment("Escape bytes");
1109     Streamer.EmitBytes(Instr.getValues());
1110     return;
1111   }
1112   llvm_unreachable("Unhandled case in switch");
1113 }
1114
1115 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
1116 /// frame.
1117 void FrameEmitterImpl::EmitCFIInstructions(MCStreamer &streamer,
1118                                     const std::vector<MCCFIInstruction> &Instrs,
1119                                            MCSymbol *BaseLabel) {
1120   for (unsigned i = 0, N = Instrs.size(); i < N; ++i) {
1121     const MCCFIInstruction &Instr = Instrs[i];
1122     MCSymbol *Label = Instr.getLabel();
1123     // Throw out move if the label is invalid.
1124     if (Label && !Label->isDefined()) continue; // Not emitted, in dead code.
1125
1126     // Advance row if new location.
1127     if (BaseLabel && Label) {
1128       MCSymbol *ThisSym = Label;
1129       if (ThisSym != BaseLabel) {
1130         if (streamer.isVerboseAsm()) streamer.AddComment("DW_CFA_advance_loc4");
1131         streamer.EmitDwarfAdvanceFrameAddr(BaseLabel, ThisSym);
1132         BaseLabel = ThisSym;
1133       }
1134     }
1135
1136     EmitCFIInstruction(streamer, Instr);
1137   }
1138 }
1139
1140 /// EmitCompactUnwind - Emit the unwind information in a compact way. If we're
1141 /// successful, return 'true'. Otherwise, return 'false' and it will emit the
1142 /// normal CIE and FDE.
1143 bool FrameEmitterImpl::EmitCompactUnwind(MCStreamer &Streamer,
1144                                          const MCDwarfFrameInfo &Frame) {
1145   MCContext &Context = Streamer.getContext();
1146   const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
1147   bool VerboseAsm = Streamer.isVerboseAsm();
1148
1149   // range-start range-length  compact-unwind-enc personality-func   lsda
1150   //  _foo       LfooEnd-_foo  0x00000023          0                 0
1151   //  _bar       LbarEnd-_bar  0x00000025         __gxx_personality  except_tab1
1152   //
1153   //   .section __LD,__compact_unwind,regular,debug
1154   //
1155   //   # compact unwind for _foo
1156   //   .quad _foo
1157   //   .set L1,LfooEnd-_foo
1158   //   .long L1
1159   //   .long 0x01010001
1160   //   .quad 0
1161   //   .quad 0
1162   //
1163   //   # compact unwind for _bar
1164   //   .quad _bar
1165   //   .set L2,LbarEnd-_bar
1166   //   .long L2
1167   //   .long 0x01020011
1168   //   .quad __gxx_personality
1169   //   .quad except_tab1
1170
1171   uint32_t Encoding = Frame.CompactUnwindEncoding;
1172   if (!Encoding) return false;
1173
1174   // The encoding needs to know we have an LSDA.
1175   if (Frame.Lsda)
1176     Encoding |= 0x40000000;
1177
1178   Streamer.SwitchSection(MOFI->getCompactUnwindSection());
1179
1180   // Range Start
1181   unsigned FDEEncoding = MOFI->getFDEEncoding(UsingCFI);
1182   unsigned Size = getSizeForEncoding(Streamer, FDEEncoding);
1183   if (VerboseAsm) Streamer.AddComment("Range Start");
1184   Streamer.EmitSymbolValue(Frame.Function, Size);
1185
1186   // Range Length
1187   const MCExpr *Range = MakeStartMinusEndExpr(Streamer, *Frame.Begin,
1188                                               *Frame.End, 0);
1189   if (VerboseAsm) Streamer.AddComment("Range Length");
1190   Streamer.EmitAbsValue(Range, 4);
1191
1192   // Compact Encoding
1193   Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_udata4);
1194   if (VerboseAsm) Streamer.AddComment("Compact Unwind Encoding: 0x" +
1195                                       Twine::utohexstr(Encoding));
1196   Streamer.EmitIntValue(Encoding, Size);
1197
1198
1199   // Personality Function
1200   Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_absptr);
1201   if (VerboseAsm) Streamer.AddComment("Personality Function");
1202   if (Frame.Personality)
1203     Streamer.EmitSymbolValue(Frame.Personality, Size);
1204   else
1205     Streamer.EmitIntValue(0, Size); // No personality fn
1206
1207   // LSDA
1208   Size = getSizeForEncoding(Streamer, Frame.LsdaEncoding);
1209   if (VerboseAsm) Streamer.AddComment("LSDA");
1210   if (Frame.Lsda)
1211     Streamer.EmitSymbolValue(Frame.Lsda, Size);
1212   else
1213     Streamer.EmitIntValue(0, Size); // No LSDA
1214
1215   return true;
1216 }
1217
1218 const MCSymbol &FrameEmitterImpl::EmitCIE(MCStreamer &streamer,
1219                                           const MCSymbol *personality,
1220                                           unsigned personalityEncoding,
1221                                           const MCSymbol *lsda,
1222                                           bool IsSignalFrame,
1223                                           unsigned lsdaEncoding) {
1224   MCContext &context = streamer.getContext();
1225   const MCRegisterInfo &MRI = context.getRegisterInfo();
1226   const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
1227   bool verboseAsm = streamer.isVerboseAsm();
1228
1229   MCSymbol *sectionStart;
1230   if (MOFI->isFunctionEHFrameSymbolPrivate() || !IsEH)
1231     sectionStart = context.CreateTempSymbol();
1232   else
1233     sectionStart = context.GetOrCreateSymbol(Twine("EH_frame") + Twine(CIENum));
1234
1235   streamer.EmitLabel(sectionStart);
1236   CIENum++;
1237
1238   MCSymbol *sectionEnd = context.CreateTempSymbol();
1239
1240   // Length
1241   const MCExpr *Length = MakeStartMinusEndExpr(streamer, *sectionStart,
1242                                                *sectionEnd, 4);
1243   if (verboseAsm) streamer.AddComment("CIE Length");
1244   streamer.EmitAbsValue(Length, 4);
1245
1246   // CIE ID
1247   unsigned CIE_ID = IsEH ? 0 : -1;
1248   if (verboseAsm) streamer.AddComment("CIE ID Tag");
1249   streamer.EmitIntValue(CIE_ID, 4);
1250
1251   // Version
1252   if (verboseAsm) streamer.AddComment("DW_CIE_VERSION");
1253   streamer.EmitIntValue(dwarf::DW_CIE_VERSION, 1);
1254
1255   // Augmentation String
1256   SmallString<8> Augmentation;
1257   if (IsEH) {
1258     if (verboseAsm) streamer.AddComment("CIE Augmentation");
1259     Augmentation += "z";
1260     if (personality)
1261       Augmentation += "P";
1262     if (lsda)
1263       Augmentation += "L";
1264     Augmentation += "R";
1265     if (IsSignalFrame)
1266       Augmentation += "S";
1267     streamer.EmitBytes(Augmentation.str());
1268   }
1269   streamer.EmitIntValue(0, 1);
1270
1271   // Code Alignment Factor
1272   if (verboseAsm) streamer.AddComment("CIE Code Alignment Factor");
1273   streamer.EmitULEB128IntValue(1);
1274
1275   // Data Alignment Factor
1276   if (verboseAsm) streamer.AddComment("CIE Data Alignment Factor");
1277   streamer.EmitSLEB128IntValue(getDataAlignmentFactor(streamer));
1278
1279   // Return Address Register
1280   if (verboseAsm) streamer.AddComment("CIE Return Address Column");
1281   streamer.EmitULEB128IntValue(MRI.getDwarfRegNum(MRI.getRARegister(), true));
1282
1283   // Augmentation Data Length (optional)
1284
1285   unsigned augmentationLength = 0;
1286   if (IsEH) {
1287     if (personality) {
1288       // Personality Encoding
1289       augmentationLength += 1;
1290       // Personality
1291       augmentationLength += getSizeForEncoding(streamer, personalityEncoding);
1292     }
1293     if (lsda)
1294       augmentationLength += 1;
1295     // Encoding of the FDE pointers
1296     augmentationLength += 1;
1297
1298     if (verboseAsm) streamer.AddComment("Augmentation Size");
1299     streamer.EmitULEB128IntValue(augmentationLength);
1300
1301     // Augmentation Data (optional)
1302     if (personality) {
1303       // Personality Encoding
1304       EmitEncodingByte(streamer, personalityEncoding,
1305                        "Personality Encoding");
1306       // Personality
1307       if (verboseAsm) streamer.AddComment("Personality");
1308       EmitPersonality(streamer, *personality, personalityEncoding);
1309     }
1310
1311     if (lsda)
1312       EmitEncodingByte(streamer, lsdaEncoding, "LSDA Encoding");
1313
1314     // Encoding of the FDE pointers
1315     EmitEncodingByte(streamer, MOFI->getFDEEncoding(UsingCFI),
1316                      "FDE Encoding");
1317   }
1318
1319   // Initial Instructions
1320
1321   const MCAsmInfo &MAI = context.getAsmInfo();
1322   const std::vector<MachineMove> &Moves = MAI.getInitialFrameState();
1323   std::vector<MCCFIInstruction> Instructions;
1324
1325   for (int i = 0, n = Moves.size(); i != n; ++i) {
1326     MCSymbol *Label = Moves[i].getLabel();
1327     const MachineLocation &Dst =
1328       TranslateMachineLocation(MRI, Moves[i].getDestination());
1329     const MachineLocation &Src =
1330       TranslateMachineLocation(MRI, Moves[i].getSource());
1331
1332     if (Dst.isReg()) {
1333       assert(Dst.getReg() == MachineLocation::VirtualFP);
1334       assert(!Src.isReg());
1335       MCCFIInstruction Inst =
1336         MCCFIInstruction::createDefCfa(Label, Src.getReg(), -Src.getOffset());
1337       Instructions.push_back(Inst);
1338     } else {
1339       assert(Src.isReg());
1340       unsigned Reg = Src.getReg();
1341       int Offset = Dst.getOffset();
1342       MCCFIInstruction Inst =
1343         MCCFIInstruction::createOffset(Label, Reg, Offset);
1344       Instructions.push_back(Inst);
1345     }
1346   }
1347
1348   EmitCFIInstructions(streamer, Instructions, NULL);
1349
1350   // Padding
1351   streamer.EmitValueToAlignment(IsEH
1352                                 ? 4 : context.getAsmInfo().getPointerSize());
1353
1354   streamer.EmitLabel(sectionEnd);
1355   return *sectionStart;
1356 }
1357
1358 MCSymbol *FrameEmitterImpl::EmitFDE(MCStreamer &streamer,
1359                                     const MCSymbol &cieStart,
1360                                     const MCDwarfFrameInfo &frame) {
1361   MCContext &context = streamer.getContext();
1362   MCSymbol *fdeStart = context.CreateTempSymbol();
1363   MCSymbol *fdeEnd = context.CreateTempSymbol();
1364   const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
1365   bool verboseAsm = streamer.isVerboseAsm();
1366
1367   if (IsEH && frame.Function && !MOFI->isFunctionEHFrameSymbolPrivate()) {
1368     MCSymbol *EHSym =
1369       context.GetOrCreateSymbol(frame.Function->getName() + Twine(".eh"));
1370     streamer.EmitEHSymAttributes(frame.Function, EHSym);
1371     streamer.EmitLabel(EHSym);
1372   }
1373
1374   // Length
1375   const MCExpr *Length = MakeStartMinusEndExpr(streamer, *fdeStart, *fdeEnd, 0);
1376   if (verboseAsm) streamer.AddComment("FDE Length");
1377   streamer.EmitAbsValue(Length, 4);
1378
1379   streamer.EmitLabel(fdeStart);
1380
1381   // CIE Pointer
1382   const MCAsmInfo &asmInfo = context.getAsmInfo();
1383   if (IsEH) {
1384     const MCExpr *offset = MakeStartMinusEndExpr(streamer, cieStart, *fdeStart,
1385                                                  0);
1386     if (verboseAsm) streamer.AddComment("FDE CIE Offset");
1387     streamer.EmitAbsValue(offset, 4);
1388   } else if (!asmInfo.doesDwarfUseRelocationsAcrossSections()) {
1389     const MCExpr *offset = MakeStartMinusEndExpr(streamer, *SectionStart,
1390                                                  cieStart, 0);
1391     streamer.EmitAbsValue(offset, 4);
1392   } else {
1393     streamer.EmitSymbolValue(&cieStart, 4);
1394   }
1395
1396   // PC Begin
1397   unsigned PCEncoding = IsEH ? MOFI->getFDEEncoding(UsingCFI)
1398                              : (unsigned)dwarf::DW_EH_PE_absptr;
1399   unsigned PCSize = getSizeForEncoding(streamer, PCEncoding);
1400   EmitSymbol(streamer, *frame.Begin, PCEncoding, "FDE initial location");
1401
1402   // PC Range
1403   const MCExpr *Range = MakeStartMinusEndExpr(streamer, *frame.Begin,
1404                                               *frame.End, 0);
1405   if (verboseAsm) streamer.AddComment("FDE address range");
1406   streamer.EmitAbsValue(Range, PCSize);
1407
1408   if (IsEH) {
1409     // Augmentation Data Length
1410     unsigned augmentationLength = 0;
1411
1412     if (frame.Lsda)
1413       augmentationLength += getSizeForEncoding(streamer, frame.LsdaEncoding);
1414
1415     if (verboseAsm) streamer.AddComment("Augmentation size");
1416     streamer.EmitULEB128IntValue(augmentationLength);
1417
1418     // Augmentation Data
1419     if (frame.Lsda)
1420       EmitSymbol(streamer, *frame.Lsda, frame.LsdaEncoding,
1421                  "Language Specific Data Area");
1422   }
1423
1424   // Call Frame Instructions
1425
1426   EmitCFIInstructions(streamer, frame.Instructions, frame.Begin);
1427
1428   // Padding
1429   streamer.EmitValueToAlignment(PCSize);
1430
1431   return fdeEnd;
1432 }
1433
1434 namespace {
1435   struct CIEKey {
1436     static const CIEKey getEmptyKey() { return CIEKey(0, 0, -1, false); }
1437     static const CIEKey getTombstoneKey() { return CIEKey(0, -1, 0, false); }
1438
1439     CIEKey(const MCSymbol* Personality_, unsigned PersonalityEncoding_,
1440            unsigned LsdaEncoding_, bool IsSignalFrame_) :
1441       Personality(Personality_), PersonalityEncoding(PersonalityEncoding_),
1442       LsdaEncoding(LsdaEncoding_), IsSignalFrame(IsSignalFrame_) {
1443     }
1444     const MCSymbol* Personality;
1445     unsigned PersonalityEncoding;
1446     unsigned LsdaEncoding;
1447     bool IsSignalFrame;
1448   };
1449 }
1450
1451 namespace llvm {
1452   template <>
1453   struct DenseMapInfo<CIEKey> {
1454     static CIEKey getEmptyKey() {
1455       return CIEKey::getEmptyKey();
1456     }
1457     static CIEKey getTombstoneKey() {
1458       return CIEKey::getTombstoneKey();
1459     }
1460     static unsigned getHashValue(const CIEKey &Key) {
1461       return static_cast<unsigned>(hash_combine(Key.Personality,
1462                                                 Key.PersonalityEncoding,
1463                                                 Key.LsdaEncoding,
1464                                                 Key.IsSignalFrame));
1465     }
1466     static bool isEqual(const CIEKey &LHS,
1467                         const CIEKey &RHS) {
1468       return LHS.Personality == RHS.Personality &&
1469         LHS.PersonalityEncoding == RHS.PersonalityEncoding &&
1470         LHS.LsdaEncoding == RHS.LsdaEncoding &&
1471         LHS.IsSignalFrame == RHS.IsSignalFrame;
1472     }
1473   };
1474 }
1475
1476 void MCDwarfFrameEmitter::Emit(MCStreamer &Streamer,
1477                                bool UsingCFI,
1478                                bool IsEH) {
1479   MCContext &Context = Streamer.getContext();
1480   MCObjectFileInfo *MOFI =
1481     const_cast<MCObjectFileInfo*>(Context.getObjectFileInfo());
1482   FrameEmitterImpl Emitter(UsingCFI, IsEH);
1483   ArrayRef<MCDwarfFrameInfo> FrameArray = Streamer.getFrameInfos();
1484
1485   // Emit the compact unwind info if available.
1486   if (IsEH && MOFI->getCompactUnwindSection())
1487     for (unsigned i = 0, n = Streamer.getNumFrameInfos(); i < n; ++i) {
1488       const MCDwarfFrameInfo &Frame = Streamer.getFrameInfo(i);
1489       if (Frame.CompactUnwindEncoding)
1490         Emitter.EmitCompactUnwind(Streamer, Frame);
1491     }
1492
1493   const MCSection &Section = IsEH ? *MOFI->getEHFrameSection() :
1494                                     *MOFI->getDwarfFrameSection();
1495   Streamer.SwitchSection(&Section);
1496   MCSymbol *SectionStart = Context.CreateTempSymbol();
1497   Streamer.EmitLabel(SectionStart);
1498   Emitter.setSectionStart(SectionStart);
1499
1500   MCSymbol *FDEEnd = NULL;
1501   DenseMap<CIEKey, const MCSymbol*> CIEStarts;
1502
1503   const MCSymbol *DummyDebugKey = NULL;
1504   for (unsigned i = 0, n = FrameArray.size(); i < n; ++i) {
1505     const MCDwarfFrameInfo &Frame = FrameArray[i];
1506     CIEKey Key(Frame.Personality, Frame.PersonalityEncoding,
1507                Frame.LsdaEncoding, Frame.IsSignalFrame);
1508     const MCSymbol *&CIEStart = IsEH ? CIEStarts[Key] : DummyDebugKey;
1509     if (!CIEStart)
1510       CIEStart = &Emitter.EmitCIE(Streamer, Frame.Personality,
1511                                   Frame.PersonalityEncoding, Frame.Lsda,
1512                                   Frame.IsSignalFrame,
1513                                   Frame.LsdaEncoding);
1514
1515     FDEEnd = Emitter.EmitFDE(Streamer, *CIEStart, Frame);
1516
1517     if (i != n - 1)
1518       Streamer.EmitLabel(FDEEnd);
1519   }
1520
1521   Streamer.EmitValueToAlignment(Context.getAsmInfo().getPointerSize());
1522   if (FDEEnd)
1523     Streamer.EmitLabel(FDEEnd);
1524 }
1525
1526 void MCDwarfFrameEmitter::EmitAdvanceLoc(MCStreamer &Streamer,
1527                                          uint64_t AddrDelta) {
1528   SmallString<256> Tmp;
1529   raw_svector_ostream OS(Tmp);
1530   MCDwarfFrameEmitter::EncodeAdvanceLoc(AddrDelta, OS);
1531   Streamer.EmitBytes(OS.str());
1532 }
1533
1534 void MCDwarfFrameEmitter::EncodeAdvanceLoc(uint64_t AddrDelta,
1535                                            raw_ostream &OS) {
1536   // FIXME: Assumes the code alignment factor is 1.
1537   if (AddrDelta == 0) {
1538   } else if (isUIntN(6, AddrDelta)) {
1539     uint8_t Opcode = dwarf::DW_CFA_advance_loc | AddrDelta;
1540     OS << Opcode;
1541   } else if (isUInt<8>(AddrDelta)) {
1542     OS << uint8_t(dwarf::DW_CFA_advance_loc1);
1543     OS << uint8_t(AddrDelta);
1544   } else if (isUInt<16>(AddrDelta)) {
1545     // FIXME: check what is the correct behavior on a big endian machine.
1546     OS << uint8_t(dwarf::DW_CFA_advance_loc2);
1547     OS << uint8_t( AddrDelta       & 0xff);
1548     OS << uint8_t((AddrDelta >> 8) & 0xff);
1549   } else {
1550     // FIXME: check what is the correct behavior on a big endian machine.
1551     assert(isUInt<32>(AddrDelta));
1552     OS << uint8_t(dwarf::DW_CFA_advance_loc4);
1553     OS << uint8_t( AddrDelta        & 0xff);
1554     OS << uint8_t((AddrDelta >> 8)  & 0xff);
1555     OS << uint8_t((AddrDelta >> 16) & 0xff);
1556     OS << uint8_t((AddrDelta >> 24) & 0xff);
1557
1558   }
1559 }