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