Use the CompactUnwindEncoding from the Frame, if it's defined.
[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/MCAsmInfo.h"
11 #include "llvm/MC/MCDwarf.h"
12 #include "llvm/MC/MCStreamer.h"
13 #include "llvm/MC/MCSymbol.h"
14 #include "llvm/MC/MCExpr.h"
15 #include "llvm/MC/MCContext.h"
16 #include "llvm/MC/MCObjectWriter.h"
17 #include "llvm/Support/Debug.h"
18 #include "llvm/Support/ErrorHandling.h"
19 #include "llvm/Support/raw_ostream.h"
20 #include "llvm/Target/TargetAsmInfo.h"
21 #include "llvm/ADT/FoldingSet.h"
22 #include "llvm/ADT/SmallString.h"
23 #include "llvm/ADT/StringExtras.h"
24 #include "llvm/ADT/Twine.h"
25 using namespace llvm;
26
27 // Given a special op, return the address skip amount (in units of
28 // DWARF2_LINE_MIN_INSN_LENGTH.
29 #define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE)
30
31 // The maximum address skip amount that can be encoded with a special op.
32 #define MAX_SPECIAL_ADDR_DELTA         SPECIAL_ADDR(255)
33
34 // First special line opcode - leave room for the standard opcodes.
35 // Note: If you want to change this, you'll have to update the
36 // "standard_opcode_lengths" table that is emitted in DwarfFileTable::Emit().  
37 #define DWARF2_LINE_OPCODE_BASE         13
38
39 // Minimum line offset in a special line info. opcode.  This value
40 // was chosen to give a reasonable range of values.
41 #define DWARF2_LINE_BASE                -5
42
43 // Range of line offsets in a special line info. opcode.
44 #define DWARF2_LINE_RANGE               14
45
46 // Define the architecture-dependent minimum instruction length (in bytes).
47 // This value should be rather too small than too big.
48 #define DWARF2_LINE_MIN_INSN_LENGTH     1
49
50 // Note: when DWARF2_LINE_MIN_INSN_LENGTH == 1 which is the current setting,
51 // this routine is a nop and will be optimized away.
52 static inline uint64_t ScaleAddrDelta(uint64_t AddrDelta) {
53   if (DWARF2_LINE_MIN_INSN_LENGTH == 1)
54     return AddrDelta;
55   if (AddrDelta % DWARF2_LINE_MIN_INSN_LENGTH != 0) {
56     // TODO: report this error, but really only once.
57     ;
58   }
59   return AddrDelta / DWARF2_LINE_MIN_INSN_LENGTH;
60 }
61
62 //
63 // This is called when an instruction is assembled into the specified section
64 // and if there is information from the last .loc directive that has yet to have
65 // a line entry made for it is made.
66 //
67 void MCLineEntry::Make(MCStreamer *MCOS, const MCSection *Section) {
68   if (!MCOS->getContext().getDwarfLocSeen())
69     return;
70
71   // Create a symbol at in the current section for use in the line entry.
72   MCSymbol *LineSym = MCOS->getContext().CreateTempSymbol();
73   // Set the value of the symbol to use for the MCLineEntry.
74   MCOS->EmitLabel(LineSym);
75
76   // Get the current .loc info saved in the context.
77   const MCDwarfLoc &DwarfLoc = MCOS->getContext().getCurrentDwarfLoc();
78
79   // Create a (local) line entry with the symbol and the current .loc info.
80   MCLineEntry LineEntry(LineSym, DwarfLoc);
81
82   // clear DwarfLocSeen saying the current .loc info is now used.
83   MCOS->getContext().ClearDwarfLocSeen();
84
85   // Get the MCLineSection for this section, if one does not exist for this
86   // section create it.
87   const DenseMap<const MCSection *, MCLineSection *> &MCLineSections =
88     MCOS->getContext().getMCLineSections();
89   MCLineSection *LineSection = MCLineSections.lookup(Section);
90   if (!LineSection) {
91     // Create a new MCLineSection.  This will be deleted after the dwarf line
92     // table is created using it by iterating through the MCLineSections
93     // DenseMap.
94     LineSection = new MCLineSection;
95     // Save a pointer to the new LineSection into the MCLineSections DenseMap.
96     MCOS->getContext().addMCLineSection(Section, LineSection);
97   }
98
99   // Add the line entry to this section's entries.
100   LineSection->addLineEntry(LineEntry);
101 }
102
103 //
104 // This helper routine returns an expression of End - Start + IntVal .
105 // 
106 static inline const MCExpr *MakeStartMinusEndExpr(const MCStreamer &MCOS,
107                                                   const MCSymbol &Start,
108                                                   const MCSymbol &End,
109                                                   int IntVal) {
110   MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
111   const MCExpr *Res =
112     MCSymbolRefExpr::Create(&End, Variant, MCOS.getContext());
113   const MCExpr *RHS =
114     MCSymbolRefExpr::Create(&Start, Variant, MCOS.getContext());
115   const MCExpr *Res1 =
116     MCBinaryExpr::Create(MCBinaryExpr::Sub, Res, RHS, MCOS.getContext());
117   const MCExpr *Res2 =
118     MCConstantExpr::Create(IntVal, MCOS.getContext());
119   const MCExpr *Res3 =
120     MCBinaryExpr::Create(MCBinaryExpr::Sub, Res1, Res2, MCOS.getContext());
121   return Res3;
122 }
123
124 //
125 // This emits the Dwarf line table for the specified section from the entries
126 // in the LineSection.
127 //
128 static inline void EmitDwarfLineTable(MCStreamer *MCOS,
129                                       const MCSection *Section,
130                                       const MCLineSection *LineSection) {
131   unsigned FileNum = 1;
132   unsigned LastLine = 1;
133   unsigned Column = 0;
134   unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
135   unsigned Isa = 0;
136   MCSymbol *LastLabel = NULL;
137
138   // Loop through each MCLineEntry and encode the dwarf line number table.
139   for (MCLineSection::const_iterator
140          it = LineSection->getMCLineEntries()->begin(),
141          ie = LineSection->getMCLineEntries()->end(); it != ie; ++it) {
142
143     if (FileNum != it->getFileNum()) {
144       FileNum = it->getFileNum();
145       MCOS->EmitIntValue(dwarf::DW_LNS_set_file, 1);
146       MCOS->EmitULEB128IntValue(FileNum);
147     }
148     if (Column != it->getColumn()) {
149       Column = it->getColumn();
150       MCOS->EmitIntValue(dwarf::DW_LNS_set_column, 1);
151       MCOS->EmitULEB128IntValue(Column);
152     }
153     if (Isa != it->getIsa()) {
154       Isa = it->getIsa();
155       MCOS->EmitIntValue(dwarf::DW_LNS_set_isa, 1);
156       MCOS->EmitULEB128IntValue(Isa);
157     }
158     if ((it->getFlags() ^ Flags) & DWARF2_FLAG_IS_STMT) {
159       Flags = it->getFlags();
160       MCOS->EmitIntValue(dwarf::DW_LNS_negate_stmt, 1);
161     }
162     if (it->getFlags() & DWARF2_FLAG_BASIC_BLOCK)
163       MCOS->EmitIntValue(dwarf::DW_LNS_set_basic_block, 1);
164     if (it->getFlags() & DWARF2_FLAG_PROLOGUE_END)
165       MCOS->EmitIntValue(dwarf::DW_LNS_set_prologue_end, 1);
166     if (it->getFlags() & DWARF2_FLAG_EPILOGUE_BEGIN)
167       MCOS->EmitIntValue(dwarf::DW_LNS_set_epilogue_begin, 1);
168
169     int64_t LineDelta = static_cast<int64_t>(it->getLine()) - LastLine;
170     MCSymbol *Label = it->getLabel();
171
172     // At this point we want to emit/create the sequence to encode the delta in
173     // line numbers and the increment of the address from the previous Label
174     // and the current Label.
175     const MCAsmInfo &asmInfo = MCOS->getContext().getAsmInfo();
176     MCOS->EmitDwarfAdvanceLineAddr(LineDelta, LastLabel, Label,
177                                    asmInfo.getPointerSize());
178
179     LastLine = it->getLine();
180     LastLabel = Label;
181   }
182
183   // Emit a DW_LNE_end_sequence for the end of the section.
184   // Using the pointer Section create a temporary label at the end of the
185   // section and use that and the LastLabel to compute the address delta
186   // and use INT64_MAX as the line delta which is the signal that this is
187   // actually a DW_LNE_end_sequence.
188
189   // Switch to the section to be able to create a symbol at its end.
190   MCOS->SwitchSection(Section);
191
192   MCContext &context = MCOS->getContext();
193   // Create a symbol at the end of the section.
194   MCSymbol *SectionEnd = context.CreateTempSymbol();
195   // Set the value of the symbol, as we are at the end of the section.
196   MCOS->EmitLabel(SectionEnd);
197
198   // Switch back the the dwarf line section.
199   MCOS->SwitchSection(context.getTargetAsmInfo().getDwarfLineSection());
200
201   const MCAsmInfo &asmInfo = MCOS->getContext().getAsmInfo();
202   MCOS->EmitDwarfAdvanceLineAddr(INT64_MAX, LastLabel, SectionEnd,
203                                  asmInfo.getPointerSize());
204 }
205
206 //
207 // This emits the Dwarf file and the line tables.
208 //
209 void MCDwarfFileTable::Emit(MCStreamer *MCOS) {
210   MCContext &context = MCOS->getContext();
211   // Switch to the section where the table will be emitted into.
212   MCOS->SwitchSection(context.getTargetAsmInfo().getDwarfLineSection());
213
214   // Create a symbol at the beginning of this section.
215   MCSymbol *LineStartSym = context.CreateTempSymbol();
216   // Set the value of the symbol, as we are at the start of the section.
217   MCOS->EmitLabel(LineStartSym);
218
219   // Create a symbol for the end of the section (to be set when we get there).
220   MCSymbol *LineEndSym = context.CreateTempSymbol();
221
222   // The first 4 bytes is the total length of the information for this
223   // compilation unit (not including these 4 bytes for the length).
224   MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *LineEndSym,4),
225                      4);
226
227   // Next 2 bytes is the Version, which is Dwarf 2.
228   MCOS->EmitIntValue(2, 2);
229
230   // Create a symbol for the end of the prologue (to be set when we get there).
231   MCSymbol *ProEndSym = context.CreateTempSymbol(); // Lprologue_end
232
233   // Length of the prologue, is the next 4 bytes.  Which is the start of the
234   // section to the end of the prologue.  Not including the 4 bytes for the
235   // total length, the 2 bytes for the version, and these 4 bytes for the
236   // length of the prologue.
237   MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *ProEndSym,
238                                         (4 + 2 + 4)),
239                   4, 0);
240
241   // Parameters of the state machine, are next.
242   MCOS->EmitIntValue(DWARF2_LINE_MIN_INSN_LENGTH, 1);
243   MCOS->EmitIntValue(DWARF2_LINE_DEFAULT_IS_STMT, 1);
244   MCOS->EmitIntValue(DWARF2_LINE_BASE, 1);
245   MCOS->EmitIntValue(DWARF2_LINE_RANGE, 1);
246   MCOS->EmitIntValue(DWARF2_LINE_OPCODE_BASE, 1);
247
248   // Standard opcode lengths
249   MCOS->EmitIntValue(0, 1); // length of DW_LNS_copy
250   MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_pc
251   MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_line
252   MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_file
253   MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_column
254   MCOS->EmitIntValue(0, 1); // length of DW_LNS_negate_stmt
255   MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_basic_block
256   MCOS->EmitIntValue(0, 1); // length of DW_LNS_const_add_pc
257   MCOS->EmitIntValue(1, 1); // length of DW_LNS_fixed_advance_pc
258   MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_prologue_end
259   MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_epilogue_begin
260   MCOS->EmitIntValue(1, 1); // DW_LNS_set_isa
261
262   // Put out the directory and file tables.
263
264   // First the directory table.
265   const std::vector<StringRef> &MCDwarfDirs =
266     context.getMCDwarfDirs();
267   for (unsigned i = 0; i < MCDwarfDirs.size(); i++) {
268     MCOS->EmitBytes(MCDwarfDirs[i], 0); // the DirectoryName
269     MCOS->EmitBytes(StringRef("\0", 1), 0); // the null term. of the string
270   }
271   MCOS->EmitIntValue(0, 1); // Terminate the directory list
272
273   // Second the file table.
274   const std::vector<MCDwarfFile *> &MCDwarfFiles =
275     MCOS->getContext().getMCDwarfFiles();
276   for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
277     MCOS->EmitBytes(MCDwarfFiles[i]->getName(), 0); // FileName
278     MCOS->EmitBytes(StringRef("\0", 1), 0); // the null term. of the string
279     // the Directory num
280     MCOS->EmitULEB128IntValue(MCDwarfFiles[i]->getDirIndex());
281     MCOS->EmitIntValue(0, 1); // last modification timestamp (always 0)
282     MCOS->EmitIntValue(0, 1); // filesize (always 0)
283   }
284   MCOS->EmitIntValue(0, 1); // Terminate the file list
285
286   // This is the end of the prologue, so set the value of the symbol at the
287   // end of the prologue (that was used in a previous expression).
288   MCOS->EmitLabel(ProEndSym);
289
290   // Put out the line tables.
291   const DenseMap<const MCSection *, MCLineSection *> &MCLineSections =
292     MCOS->getContext().getMCLineSections();
293   const std::vector<const MCSection *> &MCLineSectionOrder =
294     MCOS->getContext().getMCLineSectionOrder();
295   for (std::vector<const MCSection*>::const_iterator it =
296          MCLineSectionOrder.begin(), ie = MCLineSectionOrder.end(); it != ie;
297        ++it) {
298     const MCSection *Sec = *it;
299     const MCLineSection *Line = MCLineSections.lookup(Sec);
300     EmitDwarfLineTable(MCOS, Sec, Line);
301
302     // Now delete the MCLineSections that were created in MCLineEntry::Make()
303     // and used to emit the line table.
304     delete Line;
305   }
306
307   if (MCOS->getContext().getAsmInfo().getLinkerRequiresNonEmptyDwarfLines()
308       && MCLineSectionOrder.begin() == MCLineSectionOrder.end()) {
309     // The darwin9 linker has a bug (see PR8715). For for 32-bit architectures
310     // it requires:  
311     // total_length >= prologue_length + 10
312     // We are 4 bytes short, since we have total_length = 51 and
313     // prologue_length = 45
314
315     // The regular end_sequence should be sufficient.
316     MCDwarfLineAddr::Emit(MCOS, INT64_MAX, 0);
317   }
318
319   // This is the end of the section, so set the value of the symbol at the end
320   // of this section (that was used in a previous expression).
321   MCOS->EmitLabel(LineEndSym);
322 }
323
324 /// Utility function to write the encoding to an object writer.
325 void MCDwarfLineAddr::Write(MCObjectWriter *OW, int64_t LineDelta,
326                             uint64_t AddrDelta) {
327   SmallString<256> Tmp;
328   raw_svector_ostream OS(Tmp);
329   MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OS);
330   OW->WriteBytes(OS.str());
331 }
332
333 /// Utility function to emit the encoding to a streamer.
334 void MCDwarfLineAddr::Emit(MCStreamer *MCOS, int64_t LineDelta,
335                            uint64_t AddrDelta) {
336   SmallString<256> Tmp;
337   raw_svector_ostream OS(Tmp);
338   MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OS);
339   MCOS->EmitBytes(OS.str(), /*AddrSpace=*/0);
340 }
341
342 /// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
343 void MCDwarfLineAddr::Encode(int64_t LineDelta, uint64_t AddrDelta,
344                              raw_ostream &OS) {
345   uint64_t Temp, Opcode;
346   bool NeedCopy = false;
347
348   // Scale the address delta by the minimum instruction length.
349   AddrDelta = ScaleAddrDelta(AddrDelta);
350
351   // A LineDelta of INT64_MAX is a signal that this is actually a
352   // DW_LNE_end_sequence. We cannot use special opcodes here, since we want the 
353   // end_sequence to emit the matrix entry.
354   if (LineDelta == INT64_MAX) {
355     if (AddrDelta == MAX_SPECIAL_ADDR_DELTA)
356       OS << char(dwarf::DW_LNS_const_add_pc);
357     else {
358       OS << char(dwarf::DW_LNS_advance_pc);
359       MCObjectWriter::EncodeULEB128(AddrDelta, OS);
360     }
361     OS << char(dwarf::DW_LNS_extended_op);
362     OS << char(1);
363     OS << char(dwarf::DW_LNE_end_sequence);
364     return;
365   }
366
367   // Bias the line delta by the base.
368   Temp = LineDelta - DWARF2_LINE_BASE;
369
370   // If the line increment is out of range of a special opcode, we must encode
371   // it with DW_LNS_advance_line.
372   if (Temp >= DWARF2_LINE_RANGE) {
373     OS << char(dwarf::DW_LNS_advance_line);
374     SmallString<32> Tmp;
375     raw_svector_ostream OSE(Tmp);
376     MCObjectWriter::EncodeSLEB128(LineDelta, OSE);
377     OS << OSE.str();
378
379     LineDelta = 0;
380     Temp = 0 - DWARF2_LINE_BASE;
381     NeedCopy = true;
382   }
383
384   // Use DW_LNS_copy instead of a "line +0, addr +0" special opcode.
385   if (LineDelta == 0 && AddrDelta == 0) {
386     OS << char(dwarf::DW_LNS_copy);
387     return;
388   }
389
390   // Bias the opcode by the special opcode base.
391   Temp += DWARF2_LINE_OPCODE_BASE;
392
393   // Avoid overflow when addr_delta is large.
394   if (AddrDelta < 256 + MAX_SPECIAL_ADDR_DELTA) {
395     // Try using a special opcode.
396     Opcode = Temp + AddrDelta * DWARF2_LINE_RANGE;
397     if (Opcode <= 255) {
398       OS << char(Opcode);
399       return;
400     }
401
402     // Try using DW_LNS_const_add_pc followed by special op.
403     Opcode = Temp + (AddrDelta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
404     if (Opcode <= 255) {
405       OS << char(dwarf::DW_LNS_const_add_pc);
406       OS << char(Opcode);
407       return;
408     }
409   }
410
411   // Otherwise use DW_LNS_advance_pc.
412   OS << char(dwarf::DW_LNS_advance_pc);
413   SmallString<32> Tmp;
414   raw_svector_ostream OSE(Tmp);
415   MCObjectWriter::EncodeULEB128(AddrDelta, OSE);
416   OS << OSE.str();
417
418   if (NeedCopy)
419     OS << char(dwarf::DW_LNS_copy);
420   else
421     OS << char(Temp);
422 }
423
424 void MCDwarfFile::print(raw_ostream &OS) const {
425   OS << '"' << getName() << '"';
426 }
427
428 void MCDwarfFile::dump() const {
429   print(dbgs());
430 }
431
432 static int getDataAlignmentFactor(MCStreamer &streamer) {
433   MCContext &context = streamer.getContext();
434   const MCAsmInfo &asmInfo = context.getAsmInfo();
435   int size = asmInfo.getPointerSize();
436   if (asmInfo.isStackGrowthDirectionUp())
437     return size;
438   else
439     return -size;
440 }
441
442 static unsigned getSizeForEncoding(MCStreamer &streamer,
443                                    unsigned symbolEncoding) {
444   MCContext &context = streamer.getContext();
445   unsigned format = symbolEncoding & 0x0f;
446   switch (format) {
447   default:
448     assert(0 && "Unknown Encoding");
449   case dwarf::DW_EH_PE_absptr:
450   case dwarf::DW_EH_PE_signed:
451     return context.getAsmInfo().getPointerSize();
452   case dwarf::DW_EH_PE_udata2:
453   case dwarf::DW_EH_PE_sdata2:
454     return 2;
455   case dwarf::DW_EH_PE_udata4:
456   case dwarf::DW_EH_PE_sdata4:
457     return 4;
458   case dwarf::DW_EH_PE_udata8:
459   case dwarf::DW_EH_PE_sdata8:
460     return 8;
461   }
462 }
463
464 static void EmitSymbol(MCStreamer &streamer, const MCSymbol &symbol,
465                        unsigned symbolEncoding, const char *comment = 0) {
466   MCContext &context = streamer.getContext();
467   const MCAsmInfo &asmInfo = context.getAsmInfo();
468   const MCExpr *v = asmInfo.getExprForFDESymbol(&symbol,
469                                                 symbolEncoding,
470                                                 streamer);
471   unsigned size = getSizeForEncoding(streamer, symbolEncoding);
472   if (streamer.isVerboseAsm() && comment) streamer.AddComment(comment);
473   streamer.EmitAbsValue(v, size);
474 }
475
476 static void EmitPersonality(MCStreamer &streamer, const MCSymbol &symbol,
477                             unsigned symbolEncoding) {
478   MCContext &context = streamer.getContext();
479   const MCAsmInfo &asmInfo = context.getAsmInfo();
480   const MCExpr *v = asmInfo.getExprForPersonalitySymbol(&symbol,
481                                                         symbolEncoding,
482                                                         streamer);
483   unsigned size = getSizeForEncoding(streamer, symbolEncoding);
484   streamer.EmitValue(v, size);
485 }
486
487 static const MachineLocation TranslateMachineLocation(
488                                                   const MCRegisterInfo &MRI,
489                                                   const MachineLocation &Loc) {
490   unsigned Reg = Loc.getReg() == MachineLocation::VirtualFP ?
491     MachineLocation::VirtualFP :
492     unsigned(MRI.getDwarfRegNum(Loc.getReg(), true));
493   const MachineLocation &NewLoc = Loc.isReg() ?
494     MachineLocation(Reg) : MachineLocation(Reg, Loc.getOffset());
495   return NewLoc;
496 }
497
498 namespace {
499   class FrameEmitterImpl {
500     int CFAOffset;
501     int CIENum;
502     bool UsingCFI;
503     bool IsEH;
504     const MCSymbol *SectionStart;
505   public:
506     FrameEmitterImpl(bool usingCFI, bool isEH, const MCSymbol *sectionStart) :
507       CFAOffset(0), CIENum(0), UsingCFI(usingCFI), IsEH(isEH),
508       SectionStart(sectionStart) {
509     }
510
511     /// EmitCompactUnwind - Emit the unwind information in a compact way. If
512     /// we're successful, return 'true'. Otherwise, return 'false' and it will
513     /// emit the normal CIE and FDE.
514     bool EmitCompactUnwind(MCStreamer &streamer,
515                            const MCDwarfFrameInfo &frame);
516
517     const MCSymbol &EmitCIE(MCStreamer &streamer,
518                             const MCSymbol *personality,
519                             unsigned personalityEncoding,
520                             const MCSymbol *lsda,
521                             unsigned lsdaEncoding);
522     MCSymbol *EmitFDE(MCStreamer &streamer,
523                       const MCSymbol &cieStart,
524                       const MCDwarfFrameInfo &frame);
525     void EmitCFIInstructions(MCStreamer &streamer,
526                              const std::vector<MCCFIInstruction> &Instrs,
527                              MCSymbol *BaseLabel);
528     void EmitCFIInstruction(MCStreamer &Streamer,
529                             const MCCFIInstruction &Instr);
530   };
531
532 } // end anonymous namespace
533
534 static void EmitEncodingByte(MCStreamer &Streamer, unsigned Encoding,
535                              StringRef Prefix) {
536   if (Streamer.isVerboseAsm()) {
537     const char *EncStr = 0;
538     switch (Encoding) {
539     default: EncStr = "<unknown encoding>";
540     case dwarf::DW_EH_PE_absptr: EncStr = "absptr";
541     case dwarf::DW_EH_PE_omit:   EncStr = "omit";
542     case dwarf::DW_EH_PE_pcrel:  EncStr = "pcrel";
543     case dwarf::DW_EH_PE_udata4: EncStr = "udata4";
544     case dwarf::DW_EH_PE_udata8: EncStr = "udata8";
545     case dwarf::DW_EH_PE_sdata4: EncStr = "sdata4";
546     case dwarf::DW_EH_PE_sdata8: EncStr = "sdata8";
547     case dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata4: EncStr = "pcrel udata4";
548     case dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata4: EncStr = "pcrel sdata4";
549     case dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata8: EncStr = "pcrel udata8";
550     case dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata8: EncStr = "pcrel sdata8";
551     case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata4:
552       EncStr = "indirect pcrel udata4";
553     case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata4:
554       EncStr = "indirect pcrel sdata4";
555     case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata8:
556       EncStr = "indirect pcrel udata8";
557     case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata8:
558       EncStr = "indirect pcrel sdata8";
559     }
560
561     Streamer.AddComment(Twine(Prefix) + " = " + EncStr);
562   }
563
564   Streamer.EmitIntValue(Encoding, 1);
565 }
566
567 void FrameEmitterImpl::EmitCFIInstruction(MCStreamer &Streamer,
568                                           const MCCFIInstruction &Instr) {
569   int dataAlignmentFactor = getDataAlignmentFactor(Streamer);
570   bool VerboseAsm = Streamer.isVerboseAsm();
571
572   switch (Instr.getOperation()) {
573   case MCCFIInstruction::Move:
574   case MCCFIInstruction::RelMove: {
575     const MachineLocation &Dst = Instr.getDestination();
576     const MachineLocation &Src = Instr.getSource();
577     const bool IsRelative = Instr.getOperation() == MCCFIInstruction::RelMove;
578
579     // If advancing cfa.
580     if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
581       if (Src.getReg() == MachineLocation::VirtualFP) {
582         if (VerboseAsm) Streamer.AddComment("DW_CFA_def_cfa_offset");
583         Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_offset, 1);
584       } else {
585         if (VerboseAsm) Streamer.AddComment("DW_CFA_def_cfa");
586         Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa, 1);
587         if (VerboseAsm) Streamer.AddComment(Twine("Reg ") +
588                                             Twine(Src.getReg()));
589         Streamer.EmitULEB128IntValue(Src.getReg());
590       }
591
592       if (IsRelative)
593         CFAOffset += Src.getOffset();
594       else
595         CFAOffset = -Src.getOffset();
596
597       if (VerboseAsm) Streamer.AddComment(Twine("Offset " + Twine(CFAOffset)));
598       Streamer.EmitULEB128IntValue(CFAOffset);
599       return;
600     }
601
602     if (Src.isReg() && Src.getReg() == MachineLocation::VirtualFP) {
603       assert(Dst.isReg() && "Machine move not supported yet.");
604       if (VerboseAsm) Streamer.AddComment("DW_CFA_def_cfa_register");
605       Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_register, 1);
606       if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Dst.getReg()));
607       Streamer.EmitULEB128IntValue(Dst.getReg());
608       return;
609     }
610
611     unsigned Reg = Src.getReg();
612     int Offset = Dst.getOffset();
613     if (IsRelative)
614       Offset -= CFAOffset;
615     Offset = Offset / dataAlignmentFactor;
616
617     if (Offset < 0) {
618       if (VerboseAsm) Streamer.AddComment("DW_CFA_offset_extended_sf");
619       Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended_sf, 1);
620       if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
621       Streamer.EmitULEB128IntValue(Reg);
622       if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
623       Streamer.EmitSLEB128IntValue(Offset);
624     } else if (Reg < 64) {
625       if (VerboseAsm) Streamer.AddComment(Twine("DW_CFA_offset + Reg(") +
626                                           Twine(Reg) + ")");
627       Streamer.EmitIntValue(dwarf::DW_CFA_offset + Reg, 1);
628       if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
629       Streamer.EmitULEB128IntValue(Offset);
630     } else {
631       if (VerboseAsm) Streamer.AddComment("DW_CFA_offset_extended");
632       Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended, 1);
633       if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
634       Streamer.EmitULEB128IntValue(Reg);
635       if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
636       Streamer.EmitULEB128IntValue(Offset);
637     }
638     return;
639   }
640   case MCCFIInstruction::Remember:
641     if (VerboseAsm) Streamer.AddComment("DW_CFA_remember_state");
642     Streamer.EmitIntValue(dwarf::DW_CFA_remember_state, 1);
643     return;
644   case MCCFIInstruction::Restore:
645     if (VerboseAsm) Streamer.AddComment("DW_CFA_restore_state");
646     Streamer.EmitIntValue(dwarf::DW_CFA_restore_state, 1);
647     return;
648   case MCCFIInstruction::SameValue: {
649     unsigned Reg = Instr.getDestination().getReg();
650     if (VerboseAsm) Streamer.AddComment("DW_CFA_same_value");
651     Streamer.EmitIntValue(dwarf::DW_CFA_same_value, 1);
652     if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
653     Streamer.EmitULEB128IntValue(Reg);
654     return;
655   }
656   }
657   llvm_unreachable("Unhandled case in switch");
658 }
659
660 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
661 /// frame.
662 void FrameEmitterImpl::EmitCFIInstructions(MCStreamer &streamer,
663                                     const std::vector<MCCFIInstruction> &Instrs,
664                                            MCSymbol *BaseLabel) {
665   for (unsigned i = 0, N = Instrs.size(); i < N; ++i) {
666     const MCCFIInstruction &Instr = Instrs[i];
667     MCSymbol *Label = Instr.getLabel();
668     // Throw out move if the label is invalid.
669     if (Label && !Label->isDefined()) continue; // Not emitted, in dead code.
670
671     // Advance row if new location.
672     if (BaseLabel && Label) {
673       MCSymbol *ThisSym = Label;
674       if (ThisSym != BaseLabel) {
675         if (streamer.isVerboseAsm()) streamer.AddComment("DW_CFA_advance_loc4");
676         streamer.EmitDwarfAdvanceFrameAddr(BaseLabel, ThisSym);
677         BaseLabel = ThisSym;
678       }
679     }
680
681     EmitCFIInstruction(streamer, Instr);
682   }
683 }
684
685 /// EmitCompactUnwind - Emit the unwind information in a compact way. If we're
686 /// successful, return 'true'. Otherwise, return 'false' and it will emit the
687 /// normal CIE and FDE.
688 bool FrameEmitterImpl::EmitCompactUnwind(MCStreamer &Streamer,
689                                          const MCDwarfFrameInfo &Frame) {
690   MCContext &Context = Streamer.getContext();
691   const TargetAsmInfo &TAI = Context.getTargetAsmInfo();
692   bool VerboseAsm = Streamer.isVerboseAsm();
693
694   // range-start range-length  compact-unwind-enc personality-func   lsda
695   //  _foo       LfooEnd-_foo  0x00000023          0                 0
696   //  _bar       LbarEnd-_bar  0x00000025         __gxx_personality  except_tab1
697   //
698   //   .section __LD,__compact_unwind,regular,debug
699   //
700   //   # compact unwind for _foo
701   //   .quad _foo
702   //   .set L1,LfooEnd-_foo
703   //   .long L1
704   //   .long 0x01010001
705   //   .quad 0
706   //   .quad 0
707   //
708   //   # compact unwind for _bar
709   //   .quad _bar
710   //   .set L2,LbarEnd-_bar
711   //   .long L2
712   //   .long 0x01020011
713   //   .quad __gxx_personality
714   //   .quad except_tab1
715
716   uint32_t Encoding = Frame.CompactUnwindEncoding;
717   if (!Encoding) return false;
718
719   // The encoding needs to know we have an LSDA.
720   if (Frame.Lsda)
721     Encoding |= 0x40000000;
722
723   Streamer.SwitchSection(TAI.getCompactUnwindSection());
724
725   // Range Start
726   unsigned FDEEncoding = TAI.getFDEEncoding(UsingCFI);
727   unsigned Size = getSizeForEncoding(Streamer, FDEEncoding);
728   if (VerboseAsm) Streamer.AddComment("Range Start");
729   Streamer.EmitSymbolValue(Frame.Function, Size);
730
731   // Range Length
732   const MCExpr *Range = MakeStartMinusEndExpr(Streamer, *Frame.Begin,
733                                               *Frame.End, 0);
734   if (VerboseAsm) Streamer.AddComment("Range Length");
735   Streamer.EmitAbsValue(Range, 4);
736
737   // Compact Encoding
738   Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_udata4);
739   if (VerboseAsm) Streamer.AddComment(Twine("Compact Unwind Encoding: 0x") +
740                                       Twine(llvm::utohexstr(Encoding)));
741   Streamer.EmitIntValue(Encoding, Size);
742
743
744   // Personality Function
745   Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_absptr);
746   if (VerboseAsm) Streamer.AddComment("Personality Function");
747   if (Frame.Personality)
748     Streamer.EmitSymbolValue(Frame.Personality, Size);
749   else
750     Streamer.EmitIntValue(0, Size); // No personality fn
751
752   // LSDA
753   Size = getSizeForEncoding(Streamer, Frame.LsdaEncoding);
754   if (VerboseAsm) Streamer.AddComment("LSDA");
755   if (Frame.Lsda)
756     Streamer.EmitSymbolValue(Frame.Lsda, Size);
757   else
758     Streamer.EmitIntValue(0, Size); // No LSDA
759
760   return true;
761 }
762
763 const MCSymbol &FrameEmitterImpl::EmitCIE(MCStreamer &streamer,
764                                           const MCSymbol *personality,
765                                           unsigned personalityEncoding,
766                                           const MCSymbol *lsda,
767                                           unsigned lsdaEncoding) {
768   MCContext &context = streamer.getContext();
769   const MCRegisterInfo &MRI = context.getRegisterInfo();
770   const TargetAsmInfo &TAI = context.getTargetAsmInfo();
771   bool verboseAsm = streamer.isVerboseAsm();
772
773   MCSymbol *sectionStart;
774   if (TAI.isFunctionEHFrameSymbolPrivate() || !IsEH)
775     sectionStart = context.CreateTempSymbol();
776   else
777     sectionStart = context.GetOrCreateSymbol(Twine("EH_frame") + Twine(CIENum));
778
779   streamer.EmitLabel(sectionStart);
780   CIENum++;
781
782   MCSymbol *sectionEnd = context.CreateTempSymbol();
783
784   // Length
785   const MCExpr *Length = MakeStartMinusEndExpr(streamer, *sectionStart,
786                                                *sectionEnd, 4);
787   if (verboseAsm) streamer.AddComment("CIE Length");
788   streamer.EmitAbsValue(Length, 4);
789
790   // CIE ID
791   unsigned CIE_ID = IsEH ? 0 : -1;
792   if (verboseAsm) streamer.AddComment("CIE ID Tag");
793   streamer.EmitIntValue(CIE_ID, 4);
794
795   // Version
796   if (verboseAsm) streamer.AddComment("DW_CIE_VERSION");
797   streamer.EmitIntValue(dwarf::DW_CIE_VERSION, 1);
798
799   // Augmentation String
800   SmallString<8> Augmentation;
801   if (IsEH) {
802     if (verboseAsm) streamer.AddComment("CIE Augmentation");
803     Augmentation += "z";
804     if (personality)
805       Augmentation += "P";
806     if (lsda)
807       Augmentation += "L";
808     Augmentation += "R";
809     streamer.EmitBytes(Augmentation.str(), 0);
810   }
811   streamer.EmitIntValue(0, 1);
812
813   // Code Alignment Factor
814   if (verboseAsm) streamer.AddComment("CIE Code Alignment Factor");
815   streamer.EmitULEB128IntValue(1);
816
817   // Data Alignment Factor
818   if (verboseAsm) streamer.AddComment("CIE Data Alignment Factor");
819   streamer.EmitSLEB128IntValue(getDataAlignmentFactor(streamer));
820
821   // Return Address Register
822   if (verboseAsm) streamer.AddComment("CIE Return Address Column");
823   streamer.EmitULEB128IntValue(MRI.getDwarfRegNum(MRI.getRARegister(), true));
824
825   // Augmentation Data Length (optional)
826
827   unsigned augmentationLength = 0;
828   if (IsEH) {
829     if (personality) {
830       // Personality Encoding
831       augmentationLength += 1;
832       // Personality
833       augmentationLength += getSizeForEncoding(streamer, personalityEncoding);
834     }
835     if (lsda)
836       augmentationLength += 1;
837     // Encoding of the FDE pointers
838     augmentationLength += 1;
839
840     if (verboseAsm) streamer.AddComment("Augmentation Size");
841     streamer.EmitULEB128IntValue(augmentationLength);
842
843     // Augmentation Data (optional)
844     if (personality) {
845       // Personality Encoding
846       EmitEncodingByte(streamer, personalityEncoding,
847                        "Personality Encoding");
848       // Personality
849       if (verboseAsm) streamer.AddComment("Personality");
850       EmitPersonality(streamer, *personality, personalityEncoding);
851     }
852
853     if (lsda)
854       EmitEncodingByte(streamer, lsdaEncoding, "LSDA Encoding");
855
856     // Encoding of the FDE pointers
857     EmitEncodingByte(streamer, TAI.getFDEEncoding(UsingCFI),
858                      "FDE Encoding");
859   }
860
861   // Initial Instructions
862
863   const MCAsmInfo &MAI = context.getAsmInfo();
864   const std::vector<MachineMove> &Moves = MAI.getInitialFrameState();
865   std::vector<MCCFIInstruction> Instructions;
866
867   for (int i = 0, n = Moves.size(); i != n; ++i) {
868     MCSymbol *Label = Moves[i].getLabel();
869     const MachineLocation &Dst =
870       TranslateMachineLocation(MRI, Moves[i].getDestination());
871     const MachineLocation &Src =
872       TranslateMachineLocation(MRI, Moves[i].getSource());
873     MCCFIInstruction Inst(Label, Dst, Src);
874     Instructions.push_back(Inst);
875   }
876
877   EmitCFIInstructions(streamer, Instructions, NULL);
878
879   // Padding
880   streamer.EmitValueToAlignment(IsEH
881                                 ? 4 : context.getAsmInfo().getPointerSize());
882
883   streamer.EmitLabel(sectionEnd);
884   return *sectionStart;
885 }
886
887 MCSymbol *FrameEmitterImpl::EmitFDE(MCStreamer &streamer,
888                                     const MCSymbol &cieStart,
889                                     const MCDwarfFrameInfo &frame) {
890   MCContext &context = streamer.getContext();
891   MCSymbol *fdeStart = context.CreateTempSymbol();
892   MCSymbol *fdeEnd = context.CreateTempSymbol();
893   const TargetAsmInfo &TAI = context.getTargetAsmInfo();
894   bool verboseAsm = streamer.isVerboseAsm();
895
896   if (!TAI.isFunctionEHFrameSymbolPrivate() && IsEH) {
897     MCSymbol *EHSym =
898       context.GetOrCreateSymbol(frame.Function->getName() + Twine(".eh"));
899     streamer.EmitEHSymAttributes(frame.Function, EHSym);
900     streamer.EmitLabel(EHSym);
901   }
902
903   // Length
904   const MCExpr *Length = MakeStartMinusEndExpr(streamer, *fdeStart, *fdeEnd, 0);
905   if (verboseAsm) streamer.AddComment("FDE Length");
906   streamer.EmitAbsValue(Length, 4);
907
908   streamer.EmitLabel(fdeStart);
909
910   // CIE Pointer
911   const MCAsmInfo &asmInfo = context.getAsmInfo();
912   if (IsEH) {
913     const MCExpr *offset = MakeStartMinusEndExpr(streamer, cieStart, *fdeStart,
914                                                  0);
915     if (verboseAsm) streamer.AddComment("FDE CIE Offset");
916     streamer.EmitAbsValue(offset, 4);
917   } else if (!asmInfo.doesDwarfRequireRelocationForSectionOffset()) {
918     const MCExpr *offset = MakeStartMinusEndExpr(streamer, *SectionStart,
919                                                  cieStart, 0);
920     streamer.EmitAbsValue(offset, 4);
921   } else {
922     streamer.EmitSymbolValue(&cieStart, 4);
923   }
924
925   unsigned fdeEncoding = TAI.getFDEEncoding(UsingCFI);
926   unsigned size = getSizeForEncoding(streamer, fdeEncoding);
927
928   // PC Begin
929   unsigned PCBeginEncoding = IsEH ? fdeEncoding :
930     (unsigned)dwarf::DW_EH_PE_absptr;
931   unsigned PCBeginSize = getSizeForEncoding(streamer, PCBeginEncoding);
932   EmitSymbol(streamer, *frame.Begin, PCBeginEncoding, "FDE initial location");
933
934   // PC Range
935   const MCExpr *Range = MakeStartMinusEndExpr(streamer, *frame.Begin,
936                                               *frame.End, 0);
937   if (verboseAsm) streamer.AddComment("FDE address range");
938   streamer.EmitAbsValue(Range, size);
939
940   if (IsEH) {
941     // Augmentation Data Length
942     unsigned augmentationLength = 0;
943
944     if (frame.Lsda)
945       augmentationLength += getSizeForEncoding(streamer, frame.LsdaEncoding);
946
947     if (verboseAsm) streamer.AddComment("Augmentation size");
948     streamer.EmitULEB128IntValue(augmentationLength);
949
950     // Augmentation Data
951     if (frame.Lsda)
952       EmitSymbol(streamer, *frame.Lsda, frame.LsdaEncoding,
953                  "Language Specific Data Area");
954   }
955
956   // Call Frame Instructions
957
958   EmitCFIInstructions(streamer, frame.Instructions, frame.Begin);
959
960   // Padding
961   streamer.EmitValueToAlignment(PCBeginSize);
962
963   return fdeEnd;
964 }
965
966 namespace {
967   struct CIEKey {
968     static const CIEKey getEmptyKey() { return CIEKey(0, 0, -1); }
969     static const CIEKey getTombstoneKey() { return CIEKey(0, -1, 0); }
970
971     CIEKey(const MCSymbol* Personality_, unsigned PersonalityEncoding_,
972            unsigned LsdaEncoding_) : Personality(Personality_),
973                                      PersonalityEncoding(PersonalityEncoding_),
974                                      LsdaEncoding(LsdaEncoding_) {
975     }
976     const MCSymbol* Personality;
977     unsigned PersonalityEncoding;
978     unsigned LsdaEncoding;
979   };
980 }
981
982 namespace llvm {
983   template <>
984   struct DenseMapInfo<CIEKey> {
985     static CIEKey getEmptyKey() {
986       return CIEKey::getEmptyKey();
987     }
988     static CIEKey getTombstoneKey() {
989       return CIEKey::getTombstoneKey();
990     }
991     static unsigned getHashValue(const CIEKey &Key) {
992       FoldingSetNodeID ID;
993       ID.AddPointer(Key.Personality);
994       ID.AddInteger(Key.PersonalityEncoding);
995       ID.AddInteger(Key.LsdaEncoding);
996       return ID.ComputeHash();
997     }
998     static bool isEqual(const CIEKey &LHS,
999                         const CIEKey &RHS) {
1000       return LHS.Personality == RHS.Personality &&
1001         LHS.PersonalityEncoding == RHS.PersonalityEncoding &&
1002         LHS.LsdaEncoding == RHS.LsdaEncoding;
1003     }
1004   };
1005 }
1006
1007 void MCDwarfFrameEmitter::Emit(MCStreamer &Streamer,
1008                                bool UsingCFI,
1009                                bool IsEH) {
1010   MCContext &Context = Streamer.getContext();
1011   const TargetAsmInfo &TAI = Context.getTargetAsmInfo();
1012   const MCSection &Section = IsEH ? *TAI.getEHFrameSection() :
1013                                     *TAI.getDwarfFrameSection();
1014   Streamer.SwitchSection(&Section);
1015   MCSymbol *SectionStart = Context.CreateTempSymbol();
1016   Streamer.EmitLabel(SectionStart);
1017
1018   MCSymbol *FDEEnd = NULL;
1019   DenseMap<CIEKey, const MCSymbol*> CIEStarts;
1020   FrameEmitterImpl Emitter(UsingCFI, IsEH, SectionStart);
1021
1022   const MCSymbol *DummyDebugKey = NULL;
1023   for (unsigned i = 0, n = Streamer.getNumFrameInfos(); i < n; ++i) {
1024     const MCDwarfFrameInfo &Frame = Streamer.getFrameInfo(i);
1025     if (IsEH && TAI.getCompactUnwindSection() &&
1026         Emitter.EmitCompactUnwind(Streamer, Frame)) {
1027       FDEEnd = NULL;
1028       continue;
1029     }
1030
1031     CIEKey Key(Frame.Personality, Frame.PersonalityEncoding,
1032                Frame.LsdaEncoding);
1033     const MCSymbol *&CIEStart = IsEH ? CIEStarts[Key] : DummyDebugKey;
1034     if (!CIEStart)
1035       CIEStart = &Emitter.EmitCIE(Streamer, Frame.Personality,
1036                                   Frame.PersonalityEncoding, Frame.Lsda,
1037                                   Frame.LsdaEncoding);
1038
1039     FDEEnd = Emitter.EmitFDE(Streamer, *CIEStart, Frame);
1040
1041     if (i != n - 1)
1042       Streamer.EmitLabel(FDEEnd);
1043   }
1044
1045   Streamer.EmitValueToAlignment(Context.getAsmInfo().getPointerSize());
1046   if (FDEEnd)
1047     Streamer.EmitLabel(FDEEnd);
1048 }
1049
1050 void MCDwarfFrameEmitter::EmitAdvanceLoc(MCStreamer &Streamer,
1051                                          uint64_t AddrDelta) {
1052   SmallString<256> Tmp;
1053   raw_svector_ostream OS(Tmp);
1054   MCDwarfFrameEmitter::EncodeAdvanceLoc(AddrDelta, OS);
1055   Streamer.EmitBytes(OS.str(), /*AddrSpace=*/0);
1056 }
1057
1058 void MCDwarfFrameEmitter::EncodeAdvanceLoc(uint64_t AddrDelta,
1059                                            raw_ostream &OS) {
1060   // FIXME: Assumes the code alignment factor is 1.
1061   if (AddrDelta == 0) {
1062   } else if (isUIntN(6, AddrDelta)) {
1063     uint8_t Opcode = dwarf::DW_CFA_advance_loc | AddrDelta;
1064     OS << Opcode;
1065   } else if (isUInt<8>(AddrDelta)) {
1066     OS << uint8_t(dwarf::DW_CFA_advance_loc1);
1067     OS << uint8_t(AddrDelta);
1068   } else if (isUInt<16>(AddrDelta)) {
1069     // FIXME: check what is the correct behavior on a big endian machine.
1070     OS << uint8_t(dwarf::DW_CFA_advance_loc2);
1071     OS << uint8_t( AddrDelta       & 0xff);
1072     OS << uint8_t((AddrDelta >> 8) & 0xff);
1073   } else {
1074     // FIXME: check what is the correct behavior on a big endian machine.
1075     assert(isUInt<32>(AddrDelta));
1076     OS << uint8_t(dwarf::DW_CFA_advance_loc4);
1077     OS << uint8_t( AddrDelta        & 0xff);
1078     OS << uint8_t((AddrDelta >> 8)  & 0xff);
1079     OS << uint8_t((AddrDelta >> 16) & 0xff);
1080     OS << uint8_t((AddrDelta >> 24) & 0xff);
1081
1082   }
1083 }