Initial support for the cfi directives. This is just enough to get
[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/MCAssembler.h"
13 #include "llvm/MC/MCStreamer.h"
14 #include "llvm/MC/MCSymbol.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCContext.h"
17 #include "llvm/MC/MCObjectWriter.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include "llvm/Target/TargetAsmBackend.h"
22 #include "llvm/Target/TargetAsmInfo.h"
23 using namespace llvm;
24
25 // Given a special op, return the address skip amount (in units of
26 // DWARF2_LINE_MIN_INSN_LENGTH.
27 #define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE)
28
29 // The maximum address skip amount that can be encoded with a special op.
30 #define MAX_SPECIAL_ADDR_DELTA          SPECIAL_ADDR(255)
31
32 // First special line opcode - leave room for the standard opcodes.
33 // Note: If you want to change this, you'll have to update the
34 // "standard_opcode_lengths" table that is emitted in DwarfFileTable::Emit().  
35 #define DWARF2_LINE_OPCODE_BASE         13
36
37 // Minimum line offset in a special line info. opcode.  This value
38 // was chosen to give a reasonable range of values.
39 #define DWARF2_LINE_BASE                -5
40
41 // Range of line offsets in a special line info. opcode.
42 # define DWARF2_LINE_RANGE              14
43
44 // Define the architecture-dependent minimum instruction length (in bytes).
45 // This value should be rather too small than too big.
46 # define DWARF2_LINE_MIN_INSN_LENGTH    1
47
48 // Note: when DWARF2_LINE_MIN_INSN_LENGTH == 1 which is the current setting,
49 // this routine is a nop and will be optimized away.
50 static inline uint64_t ScaleAddrDelta(uint64_t AddrDelta)
51 {
52   if (DWARF2_LINE_MIN_INSN_LENGTH == 1)
53     return AddrDelta;
54   if (AddrDelta % DWARF2_LINE_MIN_INSN_LENGTH != 0) {
55     // TODO: report this error, but really only once.
56     ;
57   }
58   return AddrDelta / DWARF2_LINE_MIN_INSN_LENGTH;
59 }
60
61 //
62 // This is called when an instruction is assembled into the specified section
63 // and if there is information from the last .loc directive that has yet to have
64 // a line entry made for it is made.
65 //
66 void MCLineEntry::Make(MCStreamer *MCOS, const MCSection *Section) {
67   if (!MCOS->getContext().getDwarfLocSeen())
68     return;
69
70   // Create a symbol at in the current section for use in the line entry.
71   MCSymbol *LineSym = MCOS->getContext().CreateTempSymbol();
72   // Set the value of the symbol to use for the MCLineEntry.
73   MCOS->EmitLabel(LineSym);
74
75   // Get the current .loc info saved in the context.
76   const MCDwarfLoc &DwarfLoc = MCOS->getContext().getCurrentDwarfLoc();
77
78   // Create a (local) line entry with the symbol and the current .loc info.
79   MCLineEntry LineEntry(LineSym, DwarfLoc);
80
81   // clear DwarfLocSeen saying the current .loc info is now used.
82   MCOS->getContext().ClearDwarfLocSeen();
83
84   // Get the MCLineSection for this section, if one does not exist for this
85   // section create it.
86   const DenseMap<const MCSection *, MCLineSection *> &MCLineSections =
87     MCOS->getContext().getMCLineSections();
88   MCLineSection *LineSection = MCLineSections.lookup(Section);
89   if (!LineSection) {
90     // Create a new MCLineSection.  This will be deleted after the dwarf line
91     // table is created using it by iterating through the MCLineSections
92     // DenseMap.
93     LineSection = new MCLineSection;
94     // Save a pointer to the new LineSection into the MCLineSections DenseMap.
95     MCOS->getContext().addMCLineSection(Section, LineSection);
96   }
97
98   // Add the line entry to this section's entries.
99   LineSection->addLineEntry(LineEntry);
100 }
101
102 //
103 // This helper routine returns an expression of End - Start + IntVal .
104 // 
105 static inline const MCExpr *MakeStartMinusEndExpr(const MCStreamer &MCOS,
106                                                   const MCSymbol &Start,
107                                                   const MCSymbol &End,
108                                                   int IntVal) {
109   MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
110   const MCExpr *Res =
111     MCSymbolRefExpr::Create(&End, Variant, MCOS.getContext());
112   const MCExpr *RHS =
113     MCSymbolRefExpr::Create(&Start, Variant, MCOS.getContext());
114   const MCExpr *Res1 =
115     MCBinaryExpr::Create(MCBinaryExpr::Sub, Res, RHS, MCOS.getContext());
116   const MCExpr *Res2 =
117     MCConstantExpr::Create(IntVal, MCOS.getContext());
118   const MCExpr *Res3 =
119     MCBinaryExpr::Create(MCBinaryExpr::Sub, Res1, Res2, MCOS.getContext());
120   return Res3;
121 }
122
123 //
124 // This emits the Dwarf line table for the specified section from the entries
125 // in the LineSection.
126 //
127 static inline void EmitDwarfLineTable(MCStreamer *MCOS,
128                                       const MCSection *Section,
129                                       const MCLineSection *LineSection) {
130   unsigned FileNum = 1;
131   unsigned LastLine = 1;
132   unsigned Column = 0;
133   unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
134   unsigned Isa = 0;
135   MCSymbol *LastLabel = NULL;
136
137   // Loop through each MCLineEntry and encode the dwarf line number table.
138   for (MCLineSection::const_iterator
139          it = LineSection->getMCLineEntries()->begin(),
140          ie = LineSection->getMCLineEntries()->end(); it != ie; ++it) {
141
142     if (FileNum != it->getFileNum()) {
143       FileNum = it->getFileNum();
144       MCOS->EmitIntValue(dwarf::DW_LNS_set_file, 1);
145       MCOS->EmitULEB128IntValue(FileNum);
146     }
147     if (Column != it->getColumn()) {
148       Column = it->getColumn();
149       MCOS->EmitIntValue(dwarf::DW_LNS_set_column, 1);
150       MCOS->EmitULEB128IntValue(Column);
151     }
152     if (Isa != it->getIsa()) {
153       Isa = it->getIsa();
154       MCOS->EmitIntValue(dwarf::DW_LNS_set_isa, 1);
155       MCOS->EmitULEB128IntValue(Isa);
156     }
157     if ((it->getFlags() ^ Flags) & DWARF2_FLAG_IS_STMT) {
158       Flags = it->getFlags();
159       MCOS->EmitIntValue(dwarf::DW_LNS_negate_stmt, 1);
160     }
161     if (it->getFlags() & DWARF2_FLAG_BASIC_BLOCK)
162       MCOS->EmitIntValue(dwarf::DW_LNS_set_basic_block, 1);
163     if (it->getFlags() & DWARF2_FLAG_PROLOGUE_END)
164       MCOS->EmitIntValue(dwarf::DW_LNS_set_prologue_end, 1);
165     if (it->getFlags() & DWARF2_FLAG_EPILOGUE_BEGIN)
166       MCOS->EmitIntValue(dwarf::DW_LNS_set_epilogue_begin, 1);
167
168     int64_t LineDelta = static_cast<int64_t>(it->getLine()) - LastLine;
169     MCSymbol *Label = it->getLabel();
170
171     // At this point we want to emit/create the sequence to encode the delta in
172     // line numbers and the increment of the address from the previous Label
173     // and the current Label.
174     MCOS->EmitDwarfAdvanceLineAddr(LineDelta, LastLabel, Label);
175
176     LastLine = it->getLine();
177     LastLabel = Label;
178   }
179
180   // Emit a DW_LNE_end_sequence for the end of the section.
181   // Using the pointer Section create a temporary label at the end of the
182   // section and use that and the LastLabel to compute the address delta
183   // and use INT64_MAX as the line delta which is the signal that this is
184   // actually a DW_LNE_end_sequence.
185
186   // Switch to the section to be able to create a symbol at its end.
187   MCOS->SwitchSection(Section);
188
189   MCContext &context = MCOS->getContext();
190   // Create a symbol at the end of the section.
191   MCSymbol *SectionEnd = context.CreateTempSymbol();
192   // Set the value of the symbol, as we are at the end of the section.
193   MCOS->EmitLabel(SectionEnd);
194
195   // Switch back the the dwarf line section.
196   MCOS->SwitchSection(context.getTargetAsmInfo().getDwarfLineSection());
197
198   MCOS->EmitDwarfAdvanceLineAddr(INT64_MAX, LastLabel, SectionEnd);
199 }
200
201 //
202 // This emits the Dwarf file and the line tables.
203 //
204 void MCDwarfFileTable::Emit(MCStreamer *MCOS) {
205   MCContext &context = MCOS->getContext();
206   // Switch to the section where the table will be emitted into.
207   MCOS->SwitchSection(context.getTargetAsmInfo().getDwarfLineSection());
208
209   // Create a symbol at the beginning of this section.
210   MCSymbol *LineStartSym = context.CreateTempSymbol();
211   // Set the value of the symbol, as we are at the start of the section.
212   MCOS->EmitLabel(LineStartSym);
213
214   // Create a symbol for the end of the section (to be set when we get there).
215   MCSymbol *LineEndSym = context.CreateTempSymbol();
216
217   // The first 4 bytes is the total length of the information for this
218   // compilation unit (not including these 4 bytes for the length).
219   MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *LineEndSym,4),
220                      4);
221
222   // Next 2 bytes is the Version, which is Dwarf 2.
223   MCOS->EmitIntValue(2, 2);
224
225   // Create a symbol for the end of the prologue (to be set when we get there).
226   MCSymbol *ProEndSym = context.CreateTempSymbol(); // Lprologue_end
227
228   // Length of the prologue, is the next 4 bytes.  Which is the start of the
229   // section to the end of the prologue.  Not including the 4 bytes for the
230   // total length, the 2 bytes for the version, and these 4 bytes for the
231   // length of the prologue.
232   MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *ProEndSym,
233                                         (4 + 2 + 4)),
234                   4, 0);
235
236   // Parameters of the state machine, are next.
237   MCOS->EmitIntValue(DWARF2_LINE_MIN_INSN_LENGTH, 1);
238   MCOS->EmitIntValue(DWARF2_LINE_DEFAULT_IS_STMT, 1);
239   MCOS->EmitIntValue(DWARF2_LINE_BASE, 1);
240   MCOS->EmitIntValue(DWARF2_LINE_RANGE, 1);
241   MCOS->EmitIntValue(DWARF2_LINE_OPCODE_BASE, 1);
242
243   // Standard opcode lengths
244   MCOS->EmitIntValue(0, 1); // length of DW_LNS_copy
245   MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_pc
246   MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_line
247   MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_file
248   MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_column
249   MCOS->EmitIntValue(0, 1); // length of DW_LNS_negate_stmt
250   MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_basic_block
251   MCOS->EmitIntValue(0, 1); // length of DW_LNS_const_add_pc
252   MCOS->EmitIntValue(1, 1); // length of DW_LNS_fixed_advance_pc
253   MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_prologue_end
254   MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_epilogue_begin
255   MCOS->EmitIntValue(1, 1); // DW_LNS_set_isa
256
257   // Put out the directory and file tables.
258
259   // First the directory table.
260   const std::vector<StringRef> &MCDwarfDirs =
261     context.getMCDwarfDirs();
262   for (unsigned i = 0; i < MCDwarfDirs.size(); i++) {
263     MCOS->EmitBytes(MCDwarfDirs[i], 0); // the DirectoryName
264     MCOS->EmitBytes(StringRef("\0", 1), 0); // the null term. of the string
265   }
266   MCOS->EmitIntValue(0, 1); // Terminate the directory list
267
268   // Second the file table.
269   const std::vector<MCDwarfFile *> &MCDwarfFiles =
270     MCOS->getContext().getMCDwarfFiles();
271   for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
272     MCOS->EmitBytes(MCDwarfFiles[i]->getName(), 0); // FileName
273     MCOS->EmitBytes(StringRef("\0", 1), 0); // the null term. of the string
274     // the Directory num
275     MCOS->EmitULEB128IntValue(MCDwarfFiles[i]->getDirIndex());
276     MCOS->EmitIntValue(0, 1); // last modification timestamp (always 0)
277     MCOS->EmitIntValue(0, 1); // filesize (always 0)
278   }
279   MCOS->EmitIntValue(0, 1); // Terminate the file list
280
281   // This is the end of the prologue, so set the value of the symbol at the
282   // end of the prologue (that was used in a previous expression).
283   MCOS->EmitLabel(ProEndSym);
284
285   // Put out the line tables.
286   const DenseMap<const MCSection *, MCLineSection *> &MCLineSections =
287     MCOS->getContext().getMCLineSections();
288   const std::vector<const MCSection *> &MCLineSectionOrder =
289     MCOS->getContext().getMCLineSectionOrder();
290   for (std::vector<const MCSection*>::const_iterator it =
291         MCLineSectionOrder.begin(), ie = MCLineSectionOrder.end(); it != ie;
292        ++it) {
293     const MCSection *Sec = *it;
294     const MCLineSection *Line = MCLineSections.lookup(Sec);
295     EmitDwarfLineTable(MCOS, Sec, Line);
296
297     // Now delete the MCLineSections that were created in MCLineEntry::Make()
298     // and used to emit the line table.
299     delete Line;
300   }
301
302   if (MCOS->getContext().getAsmInfo().getLinkerRequiresNonEmptyDwarfLines()
303       && MCLineSectionOrder.begin() == MCLineSectionOrder.end()) {
304     // The darwin9 linker has a bug (see PR8715). For for 32-bit architectures
305     // it requires:  
306     // total_length >= prologue_length + 10
307     // We are 4 bytes short, since we have total_length = 51 and
308     // prologue_length = 45
309
310     // The regular end_sequence should be sufficient.
311     MCDwarfLineAddr::Emit(MCOS, INT64_MAX, 0);
312   }
313
314   // This is the end of the section, so set the value of the symbol at the end
315   // of this section (that was used in a previous expression).
316   MCOS->EmitLabel(LineEndSym);
317 }
318
319 /// Utility function to write the encoding to an object writer.
320 void MCDwarfLineAddr::Write(MCObjectWriter *OW, int64_t LineDelta,
321                             uint64_t AddrDelta) {
322   SmallString<256> Tmp;
323   raw_svector_ostream OS(Tmp);
324   MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OS);
325   OW->WriteBytes(OS.str());
326 }
327
328 /// Utility function to emit the encoding to a streamer.
329 void MCDwarfLineAddr::Emit(MCStreamer *MCOS, int64_t LineDelta,
330                            uint64_t AddrDelta) {
331   SmallString<256> Tmp;
332   raw_svector_ostream OS(Tmp);
333   MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OS);
334   MCOS->EmitBytes(OS.str(), /*AddrSpace=*/0);
335 }
336
337 /// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
338 void MCDwarfLineAddr::Encode(int64_t LineDelta, uint64_t AddrDelta,
339                              raw_ostream &OS) {
340   uint64_t Temp, Opcode;
341   bool NeedCopy = false;
342
343   // Scale the address delta by the minimum instruction length.
344   AddrDelta = ScaleAddrDelta(AddrDelta);
345
346   // A LineDelta of INT64_MAX is a signal that this is actually a
347   // DW_LNE_end_sequence. We cannot use special opcodes here, since we want the 
348   // end_sequence to emit the matrix entry.
349   if (LineDelta == INT64_MAX) {
350     if (AddrDelta == MAX_SPECIAL_ADDR_DELTA)
351       OS << char(dwarf::DW_LNS_const_add_pc);
352     else {
353       OS << char(dwarf::DW_LNS_advance_pc);
354       SmallString<32> Tmp;
355       raw_svector_ostream OSE(Tmp);
356       MCObjectWriter::EncodeULEB128(AddrDelta, OSE);
357       OS << OSE.str();
358     }
359     OS << char(dwarf::DW_LNS_extended_op);
360     OS << char(1);
361     OS << char(dwarf::DW_LNE_end_sequence);
362     return;
363   }
364
365   // Bias the line delta by the base.
366   Temp = LineDelta - DWARF2_LINE_BASE;
367
368   // If the line increment is out of range of a special opcode, we must encode
369   // it with DW_LNS_advance_line.
370   if (Temp >= DWARF2_LINE_RANGE) {
371     OS << char(dwarf::DW_LNS_advance_line);
372     SmallString<32> Tmp;
373     raw_svector_ostream OSE(Tmp);
374     MCObjectWriter::EncodeSLEB128(LineDelta, OSE);
375     OS << OSE.str();
376
377     LineDelta = 0;
378     Temp = 0 - DWARF2_LINE_BASE;
379     NeedCopy = true;
380   }
381
382   // Use DW_LNS_copy instead of a "line +0, addr +0" special opcode.
383   if (LineDelta == 0 && AddrDelta == 0) {
384     OS << char(dwarf::DW_LNS_copy);
385     return;
386   }
387
388   // Bias the opcode by the special opcode base.
389   Temp += DWARF2_LINE_OPCODE_BASE;
390
391   // Avoid overflow when addr_delta is large.
392   if (AddrDelta < 256 + MAX_SPECIAL_ADDR_DELTA) {
393     // Try using a special opcode.
394     Opcode = Temp + AddrDelta * DWARF2_LINE_RANGE;
395     if (Opcode <= 255) {
396       OS << char(Opcode);
397       return;
398     }
399
400     // Try using DW_LNS_const_add_pc followed by special op.
401     Opcode = Temp + (AddrDelta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
402     if (Opcode <= 255) {
403       OS << char(dwarf::DW_LNS_const_add_pc);
404       OS << char(Opcode);
405       return;
406     }
407   }
408
409   // Otherwise use DW_LNS_advance_pc.
410   OS << char(dwarf::DW_LNS_advance_pc);
411   SmallString<32> Tmp;
412   raw_svector_ostream OSE(Tmp);
413   MCObjectWriter::EncodeULEB128(AddrDelta, OSE);
414   OS << OSE.str();
415
416   if (NeedCopy)
417     OS << char(dwarf::DW_LNS_copy);
418   else
419     OS << char(Temp);
420 }
421
422 void MCDwarfFile::print(raw_ostream &OS) const {
423   OS << '"' << getName() << '"';
424 }
425
426 void MCDwarfFile::dump() const {
427   print(dbgs());
428 }
429
430 static int getDataAlignmentFactor(MCStreamer &streamer) {
431   MCContext &context = streamer.getContext();
432   const TargetAsmInfo &asmInfo = context.getTargetAsmInfo();
433   int size = asmInfo.getPointerSize();
434   if (asmInfo.getStackGrowthDirection() == TargetFrameInfo::StackGrowsUp)
435     return size;
436  else
437    return -size;
438 }
439
440 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
441 /// frame.
442 static void EmitFrameMoves(MCStreamer &streamer,
443                            const std::vector<MachineMove> &Moves,
444                            MCSymbol *BaseLabel, bool isEH) {
445   MCContext &context = streamer.getContext();
446   const TargetAsmInfo &asmInfo = context.getTargetAsmInfo();
447   int dataAlignmentFactor = getDataAlignmentFactor(streamer);
448
449   for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
450     const MachineMove &Move = Moves[i];
451     MCSymbol *Label = Move.getLabel();
452     // Throw out move if the label is invalid.
453     if (Label && !Label->isDefined()) continue; // Not emitted, in dead code.
454
455     const MachineLocation &Dst = Move.getDestination();
456     const MachineLocation &Src = Move.getSource();
457
458     // Advance row if new location.
459     if (BaseLabel && Label) {
460       MCSymbol *ThisSym = Label;
461       if (ThisSym != BaseLabel) {
462         streamer.EmitIntValue(dwarf::DW_CFA_advance_loc4, 1);
463         const MCExpr *Length = MakeStartMinusEndExpr(streamer, *BaseLabel,
464                                                      *ThisSym, 4);
465         streamer.EmitValue(Length, 4);
466         BaseLabel = ThisSym;
467       }
468     }
469
470     // If advancing cfa.
471     if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
472       assert(!Src.isReg() && "Machine move not supported yet.");
473
474       if (Src.getReg() == MachineLocation::VirtualFP) {
475         streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_offset, 1);
476       } else {
477         streamer.EmitIntValue(dwarf::DW_CFA_def_cfa, 1);
478         streamer.EmitULEB128IntValue(asmInfo.getDwarfRegNum(Src.getReg(), isEH),
479                                      1);
480       }
481
482       streamer.EmitULEB128IntValue(-Src.getOffset(), 1);
483       continue;
484     }
485
486     if (Src.isReg() && Src.getReg() == MachineLocation::VirtualFP) {
487       assert(Dst.isReg() && "Machine move not supported yet.");
488       streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_register, 1);
489       streamer.EmitULEB128IntValue(asmInfo.getDwarfRegNum(Dst.getReg(), isEH));
490       continue;
491     }
492
493     unsigned Reg = asmInfo.getDwarfRegNum(Src.getReg(), isEH);
494     int Offset = Dst.getOffset() / dataAlignmentFactor;
495
496     if (Offset < 0) {
497       streamer.EmitIntValue(dwarf::DW_CFA_offset_extended_sf, 1);
498       streamer.EmitULEB128IntValue(Reg);
499       streamer.EmitSLEB128IntValue(Offset);
500     } else if (Reg < 64) {
501       streamer.EmitIntValue(dwarf::DW_CFA_offset + Reg, 1);
502       streamer.EmitULEB128IntValue(Offset, 1);
503     } else {
504       streamer.EmitIntValue(dwarf::DW_CFA_offset_extended, 1);
505       streamer.EmitULEB128IntValue(Reg, 1);
506       streamer.EmitULEB128IntValue(Offset, 1);
507     }
508   }
509 }
510
511 static const MCSymbol &EmitCIE(MCStreamer &streamer) {
512   MCContext &context = streamer.getContext();
513   const TargetAsmInfo &asmInfo = context.getTargetAsmInfo();
514   const MCSection &section = *asmInfo.getEHFrameSection();
515   streamer.SwitchSection(&section);
516   MCSymbol *sectionStart = streamer.getContext().CreateTempSymbol();
517   MCSymbol *sectionEnd = streamer.getContext().CreateTempSymbol();
518
519   // Length
520   const MCExpr *Length = MakeStartMinusEndExpr(streamer, *sectionStart,
521                                                *sectionEnd, 4);
522   streamer.EmitLabel(sectionStart);
523   streamer.EmitValue(Length, 4);
524
525   // CIE ID
526   streamer.EmitIntValue(0, 4);
527
528   // Version
529   streamer.EmitIntValue(dwarf::DW_CIE_VERSION, 1);
530
531   // Augmentation String
532   SmallString<8> Augmentation;
533   Augmentation += "zR";
534   streamer.EmitBytes(Augmentation.str(), 0);
535   streamer.EmitIntValue(0, 1);
536
537   // Code Alignment Factor
538   streamer.EmitULEB128IntValue(1);
539
540   // Data Alignment Factor
541   streamer.EmitSLEB128IntValue(getDataAlignmentFactor(streamer));
542
543   // Return Address Register
544   streamer.EmitULEB128IntValue(asmInfo.getDwarfRARegNum(true));
545
546   // Augmentation Data Length (optional)
547   MCSymbol *augmentationStart = streamer.getContext().CreateTempSymbol();
548   MCSymbol *augmentationEnd = streamer.getContext().CreateTempSymbol();
549   const MCExpr *augmentationLength = MakeStartMinusEndExpr(streamer,
550                                                            *augmentationStart,
551                                                            *augmentationEnd, 0);
552   streamer.EmitULEB128Value(augmentationLength);
553
554   // Augmentation Data (optional)
555   streamer.EmitLabel(augmentationStart);
556   streamer.EmitIntValue(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4, 1);
557   streamer.EmitLabel(augmentationEnd);
558
559   // Initial Instructions
560
561   const std::vector<MachineMove> Moves = asmInfo.getInitialFrameState();
562
563   EmitFrameMoves(streamer, Moves, NULL, true);
564
565   // Padding
566   streamer.EmitValueToAlignment(asmInfo.getPointerSize());
567
568   streamer.EmitLabel(sectionEnd);
569   return *sectionStart;
570 }
571
572 static void EmitFDE(MCStreamer &streamer,
573                     const MCSymbol &cieStart,
574                     const MCDwarfFrameInfo &frame) {
575   MCContext &context = streamer.getContext();
576   const TargetAsmInfo &asmInfo = context.getTargetAsmInfo();
577   MCSymbol *fdeStart = streamer.getContext().CreateTempSymbol();
578   MCSymbol *fdeEnd = streamer.getContext().CreateTempSymbol();
579
580   // Length
581   const MCExpr *Length = MakeStartMinusEndExpr(streamer, *fdeStart, *fdeEnd, 0);
582   streamer.EmitValue(Length, 4);
583
584   streamer.EmitLabel(fdeStart);
585   // CIE Pointer
586   const MCExpr *offset = MakeStartMinusEndExpr(streamer, cieStart, *fdeStart,
587                                                0);
588   streamer.EmitValue(offset, 4);
589
590   // PC Begin
591   streamer.EmitPCRelSymbolValue(frame.Begin, 4);
592
593   // PC Range
594   const MCExpr *Range = MakeStartMinusEndExpr(streamer, *frame.Begin,
595                                               *frame.End, 0);
596   streamer.EmitValue(Range, 4);
597
598   // Augmentation Data Length
599   streamer.EmitULEB128IntValue(0);
600
601   // Augmentation Data
602   // Call Frame Instructions
603
604   // Padding
605   streamer.EmitValueToAlignment(asmInfo.getPointerSize());
606   streamer.EmitLabel(fdeEnd);
607 }
608
609 void MCDwarfFrameEmitter::Emit(MCStreamer &streamer) {
610   const MCSymbol &cieStart = EmitCIE(streamer);
611   for (unsigned i = 0, n = streamer.getNumFrameInfos(); i < n; ++i)
612     EmitFDE(streamer, cieStart, streamer.getFrameInfo(i));
613 }