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