MCDwarf.cpp revision fa2b25c57300dc9c518ea35bbc9dc8658fdbf454
17cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby//===- lib/MC/MCDwarf.cpp - MCDwarf implementation ------------------------===//
27cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby//
37cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby//                     The LLVM Compiler Infrastructure
47cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby//
57cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby// This file is distributed under the University of Illinois Open Source
67cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby// License. See LICENSE.TXT for details.
77cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby//
87cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby//===----------------------------------------------------------------------===//
97cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby
107cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby#include "llvm/MC/MCDwarf.h"
11d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/ADT/Hashing.h"
12d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/ADT/SmallString.h"
13d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/ADT/Twine.h"
14d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Config/config.h"
15e76a33b9567d78a5744dc52fcec3a6056d6fb576Evan Cheng#include "llvm/MC/MCAsmInfo.h"
16e76a33b9567d78a5744dc52fcec3a6056d6fb576Evan Cheng#include "llvm/MC/MCContext.h"
17d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/MC/MCExpr.h"
18e76a33b9567d78a5744dc52fcec3a6056d6fb576Evan Cheng#include "llvm/MC/MCObjectFileInfo.h"
19e76a33b9567d78a5744dc52fcec3a6056d6fb576Evan Cheng#include "llvm/MC/MCObjectWriter.h"
20e76a33b9567d78a5744dc52fcec3a6056d6fb576Evan Cheng#include "llvm/MC/MCRegisterInfo.h"
21ad8aaa069cfdb3bdc32b1becc8881f67b5272e14Rafael Espindola#include "llvm/MC/MCStreamer.h"
22c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby#include "llvm/MC/MCSymbol.h"
237cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby#include "llvm/Support/Debug.h"
24fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola#include "llvm/Support/ErrorHandling.h"
252d39a0e52df9ce050bd4e2de3a2ecca8fd9a87c3Jim Grosbach#include "llvm/Support/LEB128.h"
2694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby#include "llvm/Support/Path.h"
2794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby#include "llvm/Support/SourceMgr.h"
28d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Support/raw_ostream.h"
297cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderbyusing namespace llvm;
307cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby
31c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// Given a special op, return the address skip amount (in units of
32c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// DWARF2_LINE_MIN_INSN_LENGTH.
33c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby#define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE)
34c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
35c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// The maximum address skip amount that can be encoded with a special op.
36c3882164cbf794dbe21816a2946a31bc9e02a419Bill Wendling#define MAX_SPECIAL_ADDR_DELTA         SPECIAL_ADDR(255)
37c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
38c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// First special line opcode - leave room for the standard opcodes.
39c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// Note: If you want to change this, you'll have to update the
402684d9e3c702b2ef9fd430155d94671d12fa994fJim Grosbach// "standard_opcode_lengths" table that is emitted in DwarfFileTable::Emit().
41c3882164cbf794dbe21816a2946a31bc9e02a419Bill Wendling#define DWARF2_LINE_OPCODE_BASE         13
42c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
43c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// Minimum line offset in a special line info. opcode.  This value
44c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// was chosen to give a reasonable range of values.
45c3882164cbf794dbe21816a2946a31bc9e02a419Bill Wendling#define DWARF2_LINE_BASE                -5
46c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
47c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// Range of line offsets in a special line info. opcode.
48c3882164cbf794dbe21816a2946a31bc9e02a419Bill Wendling#define DWARF2_LINE_RANGE               14
49c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
50c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// Define the architecture-dependent minimum instruction length (in bytes).
51c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// This value should be rather too small than too big.
52c3882164cbf794dbe21816a2946a31bc9e02a419Bill Wendling#define DWARF2_LINE_MIN_INSN_LENGTH     1
53c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
54c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// Note: when DWARF2_LINE_MIN_INSN_LENGTH == 1 which is the current setting,
55c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// this routine is a nop and will be optimized away.
56a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendlingstatic inline uint64_t ScaleAddrDelta(uint64_t AddrDelta) {
57c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  if (DWARF2_LINE_MIN_INSN_LENGTH == 1)
58c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    return AddrDelta;
59c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  if (AddrDelta % DWARF2_LINE_MIN_INSN_LENGTH != 0) {
60c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    // TODO: report this error, but really only once.
61c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    ;
62c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  }
63c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  return AddrDelta / DWARF2_LINE_MIN_INSN_LENGTH;
64c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby}
65c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
66c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby//
67c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// This is called when an instruction is assembled into the specified section
68c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// and if there is information from the last .loc directive that has yet to have
69c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// a line entry made for it is made.
70c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby//
71195a0ce484cd12a5adae9184188f6d0fb52b84c0Rafael Espindolavoid MCLineEntry::Make(MCStreamer *MCOS, const MCSection *Section) {
72c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  if (!MCOS->getContext().getDwarfLocSeen())
73c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    return;
74c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
75c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Create a symbol at in the current section for use in the line entry.
76c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCSymbol *LineSym = MCOS->getContext().CreateTempSymbol();
77c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Set the value of the symbol to use for the MCLineEntry.
78c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitLabel(LineSym);
79c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
80c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Get the current .loc info saved in the context.
81c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  const MCDwarfLoc &DwarfLoc = MCOS->getContext().getCurrentDwarfLoc();
82c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
83c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Create a (local) line entry with the symbol and the current .loc info.
84c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCLineEntry LineEntry(LineSym, DwarfLoc);
85c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
86c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // clear DwarfLocSeen saying the current .loc info is now used.
873f55c24df9527de345f6cc960944840a7a101c6aKevin Enderby  MCOS->getContext().ClearDwarfLocSeen();
88c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
89c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Get the MCLineSection for this section, if one does not exist for this
90c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // section create it.
9117fd7bda5ac08f873c063c64e1456f8960a0c765Rafael Espindola  const DenseMap<const MCSection *, MCLineSection *> &MCLineSections =
92c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    MCOS->getContext().getMCLineSections();
9317fd7bda5ac08f873c063c64e1456f8960a0c765Rafael Espindola  MCLineSection *LineSection = MCLineSections.lookup(Section);
94c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  if (!LineSection) {
95c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    // Create a new MCLineSection.  This will be deleted after the dwarf line
96c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    // table is created using it by iterating through the MCLineSections
97c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    // DenseMap.
98c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    LineSection = new MCLineSection;
99c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    // Save a pointer to the new LineSection into the MCLineSections DenseMap.
10017fd7bda5ac08f873c063c64e1456f8960a0c765Rafael Espindola    MCOS->getContext().addMCLineSection(Section, LineSection);
101c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  }
102c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
103c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Add the line entry to this section's entries.
10443213cf1ac05b4198fcf9fa85d7da85477daafd1Manman Ren  LineSection->addLineEntry(LineEntry,
10543213cf1ac05b4198fcf9fa85d7da85477daafd1Manman Ren                            MCOS->getContext().getDwarfCompileUnitID());
106c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby}
107c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
108c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby//
109c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// This helper routine returns an expression of End - Start + IntVal .
1102684d9e3c702b2ef9fd430155d94671d12fa994fJim Grosbach//
1115fad7a99c00980fc30ce16fb51ce09dc435c40adRafael Espindolastatic inline const MCExpr *MakeStartMinusEndExpr(const MCStreamer &MCOS,
1125fad7a99c00980fc30ce16fb51ce09dc435c40adRafael Espindola                                                  const MCSymbol &Start,
1135fad7a99c00980fc30ce16fb51ce09dc435c40adRafael Espindola                                                  const MCSymbol &End,
1145fad7a99c00980fc30ce16fb51ce09dc435c40adRafael Espindola                                                  int IntVal) {
115c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
116c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  const MCExpr *Res =
1175fad7a99c00980fc30ce16fb51ce09dc435c40adRafael Espindola    MCSymbolRefExpr::Create(&End, Variant, MCOS.getContext());
118c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  const MCExpr *RHS =
1195fad7a99c00980fc30ce16fb51ce09dc435c40adRafael Espindola    MCSymbolRefExpr::Create(&Start, Variant, MCOS.getContext());
120c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  const MCExpr *Res1 =
1215fad7a99c00980fc30ce16fb51ce09dc435c40adRafael Espindola    MCBinaryExpr::Create(MCBinaryExpr::Sub, Res, RHS, MCOS.getContext());
122c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  const MCExpr *Res2 =
1235fad7a99c00980fc30ce16fb51ce09dc435c40adRafael Espindola    MCConstantExpr::Create(IntVal, MCOS.getContext());
124c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  const MCExpr *Res3 =
1255fad7a99c00980fc30ce16fb51ce09dc435c40adRafael Espindola    MCBinaryExpr::Create(MCBinaryExpr::Sub, Res1, Res2, MCOS.getContext());
126c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  return Res3;
127c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby}
128c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
129c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby//
130c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// This emits the Dwarf line table for the specified section from the entries
131c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// in the LineSection.
132c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby//
133195a0ce484cd12a5adae9184188f6d0fb52b84c0Rafael Espindolastatic inline void EmitDwarfLineTable(MCStreamer *MCOS,
134c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby                                      const MCSection *Section,
13543213cf1ac05b4198fcf9fa85d7da85477daafd1Manman Ren                                      const MCLineSection *LineSection,
13643213cf1ac05b4198fcf9fa85d7da85477daafd1Manman Ren                                      unsigned CUID) {
13743213cf1ac05b4198fcf9fa85d7da85477daafd1Manman Ren  // This LineSection does not contain any LineEntry for the given Compile Unit.
13843213cf1ac05b4198fcf9fa85d7da85477daafd1Manman Ren  if (!LineSection->containEntriesForID(CUID))
13943213cf1ac05b4198fcf9fa85d7da85477daafd1Manman Ren    return;
14043213cf1ac05b4198fcf9fa85d7da85477daafd1Manman Ren
141c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  unsigned FileNum = 1;
142c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  unsigned LastLine = 1;
143c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  unsigned Column = 0;
144c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
145c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  unsigned Isa = 0;
146c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCSymbol *LastLabel = NULL;
147c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
148c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Loop through each MCLineEntry and encode the dwarf line number table.
14917fd7bda5ac08f873c063c64e1456f8960a0c765Rafael Espindola  for (MCLineSection::const_iterator
15043213cf1ac05b4198fcf9fa85d7da85477daafd1Manman Ren         it = LineSection->getMCLineEntries(CUID).begin(),
15143213cf1ac05b4198fcf9fa85d7da85477daafd1Manman Ren         ie = LineSection->getMCLineEntries(CUID).end(); it != ie; ++it) {
152c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
153c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    if (FileNum != it->getFileNum()) {
154c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      FileNum = it->getFileNum();
155c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      MCOS->EmitIntValue(dwarf::DW_LNS_set_file, 1);
1563ff57094a7d176a759ddb1e1668489d89064f56cRafael Espindola      MCOS->EmitULEB128IntValue(FileNum);
157c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    }
158c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    if (Column != it->getColumn()) {
159c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      Column = it->getColumn();
160c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      MCOS->EmitIntValue(dwarf::DW_LNS_set_column, 1);
1613ff57094a7d176a759ddb1e1668489d89064f56cRafael Espindola      MCOS->EmitULEB128IntValue(Column);
162c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    }
163c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    if (Isa != it->getIsa()) {
164c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      Isa = it->getIsa();
165c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      MCOS->EmitIntValue(dwarf::DW_LNS_set_isa, 1);
1663ff57094a7d176a759ddb1e1668489d89064f56cRafael Espindola      MCOS->EmitULEB128IntValue(Isa);
167c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    }
168c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    if ((it->getFlags() ^ Flags) & DWARF2_FLAG_IS_STMT) {
169c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      Flags = it->getFlags();
170c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      MCOS->EmitIntValue(dwarf::DW_LNS_negate_stmt, 1);
171c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    }
172c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    if (it->getFlags() & DWARF2_FLAG_BASIC_BLOCK)
173c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      MCOS->EmitIntValue(dwarf::DW_LNS_set_basic_block, 1);
174c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    if (it->getFlags() & DWARF2_FLAG_PROLOGUE_END)
175c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      MCOS->EmitIntValue(dwarf::DW_LNS_set_prologue_end, 1);
176c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    if (it->getFlags() & DWARF2_FLAG_EPILOGUE_BEGIN)
177c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      MCOS->EmitIntValue(dwarf::DW_LNS_set_epilogue_begin, 1);
178c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
17964185cc6090f695c4f97c51cf2adc731f56d1a20Rafael Espindola    int64_t LineDelta = static_cast<int64_t>(it->getLine()) - LastLine;
180c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    MCSymbol *Label = it->getLabel();
181c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
182c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    // At this point we want to emit/create the sequence to encode the delta in
183c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    // line numbers and the increment of the address from the previous Label
184c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    // and the current Label.
1851be0e271a07925b928ba89848934f1ea6f1854e2Evan Cheng    const MCAsmInfo &asmInfo = MCOS->getContext().getAsmInfo();
186672b93a3324cc1da6d374eed4c75c050a9cad7beEvan Cheng    MCOS->EmitDwarfAdvanceLineAddr(LineDelta, LastLabel, Label,
187672b93a3324cc1da6d374eed4c75c050a9cad7beEvan Cheng                                   asmInfo.getPointerSize());
188c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
189c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    LastLine = it->getLine();
190c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    LastLabel = Label;
191c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  }
192c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
193c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Emit a DW_LNE_end_sequence for the end of the section.
194c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Using the pointer Section create a temporary label at the end of the
195c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // section and use that and the LastLabel to compute the address delta
196c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // and use INT64_MAX as the line delta which is the signal that this is
197c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // actually a DW_LNE_end_sequence.
198c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
199c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Switch to the section to be able to create a symbol at its end.
200df39be6cb4eb44011db3d3e86f8fe463f81ce127Peter Collingbourne  // TODO: keep track of the last subsection so that this symbol appears in the
201df39be6cb4eb44011db3d3e86f8fe463f81ce127Peter Collingbourne  // correct place.
202c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->SwitchSection(Section);
20389b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
20489b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  MCContext &context = MCOS->getContext();
205c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Create a symbol at the end of the section.
20689b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  MCSymbol *SectionEnd = context.CreateTempSymbol();
207c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Set the value of the symbol, as we are at the end of the section.
208c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitLabel(SectionEnd);
209c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
210c8e41c591741b3da1077f7000274ad040bef8002Sylvestre Ledru  // Switch back the dwarf line section.
211e76a33b9567d78a5744dc52fcec3a6056d6fb576Evan Cheng  MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfLineSection());
212195a0ce484cd12a5adae9184188f6d0fb52b84c0Rafael Espindola
2131be0e271a07925b928ba89848934f1ea6f1854e2Evan Cheng  const MCAsmInfo &asmInfo = MCOS->getContext().getAsmInfo();
214672b93a3324cc1da6d374eed4c75c050a9cad7beEvan Cheng  MCOS->EmitDwarfAdvanceLineAddr(INT64_MAX, LastLabel, SectionEnd,
215672b93a3324cc1da6d374eed4c75c050a9cad7beEvan Cheng                                 asmInfo.getPointerSize());
216c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby}
217c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
218c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby//
219c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// This emits the Dwarf file and the line tables.
220c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby//
221489d67927172941bf59b9f4829ab8910814fea24Rafael Espindolaconst MCSymbol *MCDwarfFileTable::Emit(MCStreamer *MCOS) {
22289b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  MCContext &context = MCOS->getContext();
223c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Switch to the section where the table will be emitted into.
224e76a33b9567d78a5744dc52fcec3a6056d6fb576Evan Cheng  MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfLineSection());
225c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
22643213cf1ac05b4198fcf9fa85d7da85477daafd1Manman Ren  const DenseMap<unsigned, MCSymbol *> &MCLineTableSymbols =
22743213cf1ac05b4198fcf9fa85d7da85477daafd1Manman Ren    MCOS->getContext().getMCLineTableSymbols();
22843213cf1ac05b4198fcf9fa85d7da85477daafd1Manman Ren  // CUID and MCLineTableSymbols are set in DwarfDebug, when DwarfDebug does
22943213cf1ac05b4198fcf9fa85d7da85477daafd1Manman Ren  // not exist, CUID will be 0 and MCLineTableSymbols will be empty.
23043213cf1ac05b4198fcf9fa85d7da85477daafd1Manman Ren  // Handle Compile Unit 0, the line table start symbol is the section symbol.
23143213cf1ac05b4198fcf9fa85d7da85477daafd1Manman Ren  const MCSymbol *LineStartSym = EmitCU(MCOS, 0);
23243213cf1ac05b4198fcf9fa85d7da85477daafd1Manman Ren  // Handle the rest of the Compile Units.
23343213cf1ac05b4198fcf9fa85d7da85477daafd1Manman Ren  for (unsigned Is = 1, Ie = MCLineTableSymbols.size(); Is < Ie; Is++)
23443213cf1ac05b4198fcf9fa85d7da85477daafd1Manman Ren    EmitCU(MCOS, Is);
23543213cf1ac05b4198fcf9fa85d7da85477daafd1Manman Ren
23643213cf1ac05b4198fcf9fa85d7da85477daafd1Manman Ren  // Now delete the MCLineSections that were created in MCLineEntry::Make()
23743213cf1ac05b4198fcf9fa85d7da85477daafd1Manman Ren  // and used to emit the line table.
23843213cf1ac05b4198fcf9fa85d7da85477daafd1Manman Ren  const DenseMap<const MCSection *, MCLineSection *> &MCLineSections =
23943213cf1ac05b4198fcf9fa85d7da85477daafd1Manman Ren    MCOS->getContext().getMCLineSections();
24043213cf1ac05b4198fcf9fa85d7da85477daafd1Manman Ren  for (DenseMap<const MCSection *, MCLineSection *>::const_iterator it =
24143213cf1ac05b4198fcf9fa85d7da85477daafd1Manman Ren       MCLineSections.begin(), ie = MCLineSections.end(); it != ie;
24243213cf1ac05b4198fcf9fa85d7da85477daafd1Manman Ren       ++it)
24343213cf1ac05b4198fcf9fa85d7da85477daafd1Manman Ren    delete it->second;
24443213cf1ac05b4198fcf9fa85d7da85477daafd1Manman Ren
24543213cf1ac05b4198fcf9fa85d7da85477daafd1Manman Ren  return LineStartSym;
24643213cf1ac05b4198fcf9fa85d7da85477daafd1Manman Ren}
24743213cf1ac05b4198fcf9fa85d7da85477daafd1Manman Ren
24843213cf1ac05b4198fcf9fa85d7da85477daafd1Manman Renconst MCSymbol *MCDwarfFileTable::EmitCU(MCStreamer *MCOS, unsigned CUID) {
24943213cf1ac05b4198fcf9fa85d7da85477daafd1Manman Ren  MCContext &context = MCOS->getContext();
25043213cf1ac05b4198fcf9fa85d7da85477daafd1Manman Ren
25143213cf1ac05b4198fcf9fa85d7da85477daafd1Manman Ren  // Create a symbol at the beginning of the line table.
25243213cf1ac05b4198fcf9fa85d7da85477daafd1Manman Ren  MCSymbol *LineStartSym = MCOS->getContext().getMCLineTableSymbol(CUID);
25343213cf1ac05b4198fcf9fa85d7da85477daafd1Manman Ren  if (!LineStartSym)
25443213cf1ac05b4198fcf9fa85d7da85477daafd1Manman Ren    LineStartSym = context.CreateTempSymbol();
25543213cf1ac05b4198fcf9fa85d7da85477daafd1Manman Ren  // Set the value of the symbol, as we are at the start of the line table.
256c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitLabel(LineStartSym);
257c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
258c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Create a symbol for the end of the section (to be set when we get there).
25989b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  MCSymbol *LineEndSym = context.CreateTempSymbol();
260c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
261c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // The first 4 bytes is the total length of the information for this
262c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // compilation unit (not including these 4 bytes for the length).
2635fad7a99c00980fc30ce16fb51ce09dc435c40adRafael Espindola  MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *LineEndSym,4),
2640bbe0b440ee2cef47dcb7b281825eb70341c16ddRafael Espindola                     4);
265c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
266c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Next 2 bytes is the Version, which is Dwarf 2.
267c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(2, 2);
268c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
269c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Create a symbol for the end of the prologue (to be set when we get there).
27089b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  MCSymbol *ProEndSym = context.CreateTempSymbol(); // Lprologue_end
271c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
272c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Length of the prologue, is the next 4 bytes.  Which is the start of the
273c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // section to the end of the prologue.  Not including the 4 bytes for the
274c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // total length, the 2 bytes for the version, and these 4 bytes for the
275c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // length of the prologue.
2765fad7a99c00980fc30ce16fb51ce09dc435c40adRafael Espindola  MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *ProEndSym,
277e0501e866ca15601f4da2dc6b5572dc2de53abc8Eric Christopher                                           (4 + 2 + 4)), 4, 0);
278c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
279c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Parameters of the state machine, are next.
280c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(DWARF2_LINE_MIN_INSN_LENGTH, 1);
281c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(DWARF2_LINE_DEFAULT_IS_STMT, 1);
282c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(DWARF2_LINE_BASE, 1);
283c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(DWARF2_LINE_RANGE, 1);
284c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(DWARF2_LINE_OPCODE_BASE, 1);
285c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
286c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Standard opcode lengths
287c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(0, 1); // length of DW_LNS_copy
288c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_pc
289c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_line
290c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_file
291c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_column
292c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(0, 1); // length of DW_LNS_negate_stmt
293c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_basic_block
294c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(0, 1); // length of DW_LNS_const_add_pc
295c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(1, 1); // length of DW_LNS_fixed_advance_pc
296c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_prologue_end
297c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_epilogue_begin
298c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(1, 1); // DW_LNS_set_isa
299c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
300c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Put out the directory and file tables.
301c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
302c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // First the directory table.
3039e999adb48beb61663f6abca667b8c85068ee585Manman Ren  const SmallVectorImpl<StringRef> &MCDwarfDirs =
3043de61b4c0144748e4b9157e2c22fe4ea685981a2Manman Ren    context.getMCDwarfDirs(CUID);
305c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  for (unsigned i = 0; i < MCDwarfDirs.size(); i++) {
30668ca56285f9b6e82eb16ff8ea02a301f2c489faeEric Christopher    MCOS->EmitBytes(MCDwarfDirs[i]); // the DirectoryName
30768ca56285f9b6e82eb16ff8ea02a301f2c489faeEric Christopher    MCOS->EmitBytes(StringRef("\0", 1)); // the null term. of the string
308c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  }
309c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(0, 1); // Terminate the directory list
310c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
311c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Second the file table.
3129e999adb48beb61663f6abca667b8c85068ee585Manman Ren  const SmallVectorImpl<MCDwarfFile *> &MCDwarfFiles =
3133de61b4c0144748e4b9157e2c22fe4ea685981a2Manman Ren    MCOS->getContext().getMCDwarfFiles(CUID);
314c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
31568ca56285f9b6e82eb16ff8ea02a301f2c489faeEric Christopher    MCOS->EmitBytes(MCDwarfFiles[i]->getName()); // FileName
31668ca56285f9b6e82eb16ff8ea02a301f2c489faeEric Christopher    MCOS->EmitBytes(StringRef("\0", 1)); // the null term. of the string
3173ff57094a7d176a759ddb1e1668489d89064f56cRafael Espindola    // the Directory num
3183ff57094a7d176a759ddb1e1668489d89064f56cRafael Espindola    MCOS->EmitULEB128IntValue(MCDwarfFiles[i]->getDirIndex());
319c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    MCOS->EmitIntValue(0, 1); // last modification timestamp (always 0)
320c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    MCOS->EmitIntValue(0, 1); // filesize (always 0)
321c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  }
322c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(0, 1); // Terminate the file list
323c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
324c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // This is the end of the prologue, so set the value of the symbol at the
325c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // end of the prologue (that was used in a previous expression).
326c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitLabel(ProEndSym);
327c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
328c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Put out the line tables.
32917fd7bda5ac08f873c063c64e1456f8960a0c765Rafael Espindola  const DenseMap<const MCSection *, MCLineSection *> &MCLineSections =
330c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    MCOS->getContext().getMCLineSections();
33117fd7bda5ac08f873c063c64e1456f8960a0c765Rafael Espindola  const std::vector<const MCSection *> &MCLineSectionOrder =
33217fd7bda5ac08f873c063c64e1456f8960a0c765Rafael Espindola    MCOS->getContext().getMCLineSectionOrder();
33317fd7bda5ac08f873c063c64e1456f8960a0c765Rafael Espindola  for (std::vector<const MCSection*>::const_iterator it =
334c3882164cbf794dbe21816a2946a31bc9e02a419Bill Wendling         MCLineSectionOrder.begin(), ie = MCLineSectionOrder.end(); it != ie;
33517fd7bda5ac08f873c063c64e1456f8960a0c765Rafael Espindola       ++it) {
33617fd7bda5ac08f873c063c64e1456f8960a0c765Rafael Espindola    const MCSection *Sec = *it;
33717fd7bda5ac08f873c063c64e1456f8960a0c765Rafael Espindola    const MCLineSection *Line = MCLineSections.lookup(Sec);
33843213cf1ac05b4198fcf9fa85d7da85477daafd1Manman Ren    EmitDwarfLineTable(MCOS, Sec, Line, CUID);
339c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  }
340c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
341767b1be3900bdc693aa0ad3e554ba034845f67f7Rafael Espindola  if (MCOS->getContext().getAsmInfo().getLinkerRequiresNonEmptyDwarfLines()
342767b1be3900bdc693aa0ad3e554ba034845f67f7Rafael Espindola      && MCLineSectionOrder.begin() == MCLineSectionOrder.end()) {
343a8de83c68aaa03cafa0e3618fb8bf030b48f6540Rafael Espindola    // The darwin9 linker has a bug (see PR8715). For for 32-bit architectures
3442684d9e3c702b2ef9fd430155d94671d12fa994fJim Grosbach    // it requires:
345a8de83c68aaa03cafa0e3618fb8bf030b48f6540Rafael Espindola    // total_length >= prologue_length + 10
346a8de83c68aaa03cafa0e3618fb8bf030b48f6540Rafael Espindola    // We are 4 bytes short, since we have total_length = 51 and
347a8de83c68aaa03cafa0e3618fb8bf030b48f6540Rafael Espindola    // prologue_length = 45
348a8de83c68aaa03cafa0e3618fb8bf030b48f6540Rafael Espindola
349a8de83c68aaa03cafa0e3618fb8bf030b48f6540Rafael Espindola    // The regular end_sequence should be sufficient.
350a8de83c68aaa03cafa0e3618fb8bf030b48f6540Rafael Espindola    MCDwarfLineAddr::Emit(MCOS, INT64_MAX, 0);
3515113cdbfff7df4c7a79a92e5aa971126254202c6Devang Patel  }
3525113cdbfff7df4c7a79a92e5aa971126254202c6Devang Patel
353c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // This is the end of the section, so set the value of the symbol at the end
354c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // of this section (that was used in a previous expression).
355c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitLabel(LineEndSym);
356489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola
357489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola  return LineStartSym;
358c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby}
359c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
360c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby/// Utility function to write the encoding to an object writer.
361c095793b4ab027181605c79c9808df12afe45d63Kevin Enderbyvoid MCDwarfLineAddr::Write(MCObjectWriter *OW, int64_t LineDelta,
362c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby                            uint64_t AddrDelta) {
363c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  SmallString<256> Tmp;
364c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  raw_svector_ostream OS(Tmp);
365c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OS);
366c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  OW->WriteBytes(OS.str());
367c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby}
368c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
369c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby/// Utility function to emit the encoding to a streamer.
370195a0ce484cd12a5adae9184188f6d0fb52b84c0Rafael Espindolavoid MCDwarfLineAddr::Emit(MCStreamer *MCOS, int64_t LineDelta,
371c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby                           uint64_t AddrDelta) {
372c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  SmallString<256> Tmp;
373c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  raw_svector_ostream OS(Tmp);
374c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OS);
37568ca56285f9b6e82eb16ff8ea02a301f2c489faeEric Christopher  MCOS->EmitBytes(OS.str());
376c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby}
377c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
378c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby/// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
379c095793b4ab027181605c79c9808df12afe45d63Kevin Enderbyvoid MCDwarfLineAddr::Encode(int64_t LineDelta, uint64_t AddrDelta,
380c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby                             raw_ostream &OS) {
381c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  uint64_t Temp, Opcode;
382c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  bool NeedCopy = false;
383c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
384c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Scale the address delta by the minimum instruction length.
385c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  AddrDelta = ScaleAddrDelta(AddrDelta);
386c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
387c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // A LineDelta of INT64_MAX is a signal that this is actually a
3882684d9e3c702b2ef9fd430155d94671d12fa994fJim Grosbach  // DW_LNE_end_sequence. We cannot use special opcodes here, since we want the
389c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // end_sequence to emit the matrix entry.
390c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  if (LineDelta == INT64_MAX) {
391c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    if (AddrDelta == MAX_SPECIAL_ADDR_DELTA)
392c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      OS << char(dwarf::DW_LNS_const_add_pc);
393c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    else {
394c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      OS << char(dwarf::DW_LNS_advance_pc);
3952d39a0e52df9ce050bd4e2de3a2ecca8fd9a87c3Jim Grosbach      encodeULEB128(AddrDelta, OS);
396c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    }
397c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    OS << char(dwarf::DW_LNS_extended_op);
398c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    OS << char(1);
399c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    OS << char(dwarf::DW_LNE_end_sequence);
400c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    return;
401c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  }
402c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
403c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Bias the line delta by the base.
404c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  Temp = LineDelta - DWARF2_LINE_BASE;
405c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
406c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // If the line increment is out of range of a special opcode, we must encode
407c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // it with DW_LNS_advance_line.
408c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  if (Temp >= DWARF2_LINE_RANGE) {
409c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    OS << char(dwarf::DW_LNS_advance_line);
4102d39a0e52df9ce050bd4e2de3a2ecca8fd9a87c3Jim Grosbach    encodeSLEB128(LineDelta, OS);
411c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
412c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    LineDelta = 0;
413c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    Temp = 0 - DWARF2_LINE_BASE;
414c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    NeedCopy = true;
415c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  }
416c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
417c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Use DW_LNS_copy instead of a "line +0, addr +0" special opcode.
418c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  if (LineDelta == 0 && AddrDelta == 0) {
419c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    OS << char(dwarf::DW_LNS_copy);
420c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    return;
421c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  }
422c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
423c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Bias the opcode by the special opcode base.
424c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  Temp += DWARF2_LINE_OPCODE_BASE;
425c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
426c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Avoid overflow when addr_delta is large.
427c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  if (AddrDelta < 256 + MAX_SPECIAL_ADDR_DELTA) {
428c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    // Try using a special opcode.
429c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    Opcode = Temp + AddrDelta * DWARF2_LINE_RANGE;
430c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    if (Opcode <= 255) {
431c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      OS << char(Opcode);
432c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      return;
433c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    }
434c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
435c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    // Try using DW_LNS_const_add_pc followed by special op.
436c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    Opcode = Temp + (AddrDelta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
437c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    if (Opcode <= 255) {
438c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      OS << char(dwarf::DW_LNS_const_add_pc);
439c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      OS << char(Opcode);
440c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      return;
441c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    }
442c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  }
443c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
444c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Otherwise use DW_LNS_advance_pc.
445c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  OS << char(dwarf::DW_LNS_advance_pc);
4462d39a0e52df9ce050bd4e2de3a2ecca8fd9a87c3Jim Grosbach  encodeULEB128(AddrDelta, OS);
447c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
448c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  if (NeedCopy)
449c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    OS << char(dwarf::DW_LNS_copy);
450c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  else
451c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    OS << char(Temp);
452c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby}
453c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
4547cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderbyvoid MCDwarfFile::print(raw_ostream &OS) const {
4557cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby  OS << '"' << getName() << '"';
4567cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby}
4577cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby
458286c4dc355b8be6806081b23c3097485821c7642Manman Ren#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
4597cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderbyvoid MCDwarfFile::dump() const {
4607cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby  print(dbgs());
4617cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby}
462cc77eece74c8db09acc2af425e7e6c88a5bb30d1Manman Ren#endif
463c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
46494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby// Utility function to write a tuple for .debug_abbrev.
46594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderbystatic void EmitAbbrev(MCStreamer *MCOS, uint64_t Name, uint64_t Form) {
46694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitULEB128IntValue(Name);
46794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitULEB128IntValue(Form);
46894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby}
46994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
47094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby// When generating dwarf for assembly source files this emits
47194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby// the data for .debug_abbrev section which contains three DIEs.
47294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderbystatic void EmitGenDwarfAbbrev(MCStreamer *MCOS) {
47394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCContext &context = MCOS->getContext();
47494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
47594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
47694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // DW_TAG_compile_unit DIE abbrev (1).
47794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitULEB128IntValue(1);
47894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitULEB128IntValue(dwarf::DW_TAG_compile_unit);
47994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
48094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitAbbrev(MCOS, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4);
48194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
48294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitAbbrev(MCOS, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr);
48394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
48494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitAbbrev(MCOS, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string);
48594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
48694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  if (!DwarfDebugFlags.empty())
48794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    EmitAbbrev(MCOS, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string);
48894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitAbbrev(MCOS, dwarf::DW_AT_producer, dwarf::DW_FORM_string);
48994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitAbbrev(MCOS, dwarf::DW_AT_language, dwarf::DW_FORM_data2);
49094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitAbbrev(MCOS, 0, 0);
49194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
49238fdb7d9fc40e9f29c3156b6625cac8d91d562e1Kevin Enderby  // DW_TAG_label DIE abbrev (2).
49394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitULEB128IntValue(2);
49438fdb7d9fc40e9f29c3156b6625cac8d91d562e1Kevin Enderby  MCOS->EmitULEB128IntValue(dwarf::DW_TAG_label);
49594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
49694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
49794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitAbbrev(MCOS, dwarf::DW_AT_decl_file, dwarf::DW_FORM_data4);
49894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitAbbrev(MCOS, dwarf::DW_AT_decl_line, dwarf::DW_FORM_data4);
49994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
50094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitAbbrev(MCOS, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag);
50194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitAbbrev(MCOS, 0, 0);
50294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
50394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // DW_TAG_unspecified_parameters DIE abbrev (3).
50494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitULEB128IntValue(3);
50594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitULEB128IntValue(dwarf::DW_TAG_unspecified_parameters);
50694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(dwarf::DW_CHILDREN_no, 1);
50794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitAbbrev(MCOS, 0, 0);
50894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
50994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Terminate the abbreviations for this compilation unit.
51094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(0, 1);
51194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby}
51294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
51394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby// When generating dwarf for assembly source files this emits the data for
51494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby// .debug_aranges section.  Which contains a header and a table of pairs of
51594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby// PointerSize'ed values for the address and size of section(s) with line table
51694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby// entries (just the default .text in our case) and a terminating pair of zeros.
51715ab115df57949fa55d92ffdfba491f7d02ed60fAlexey Samsonovstatic void EmitGenDwarfAranges(MCStreamer *MCOS,
51815ab115df57949fa55d92ffdfba491f7d02ed60fAlexey Samsonov                                const MCSymbol *InfoSectionSymbol) {
51994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCContext &context = MCOS->getContext();
52094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
52194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Create a symbol at the end of the section that we are creating the dwarf
52294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // debugging info to use later in here as part of the expression to calculate
52394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // the size of the section for the table.
52494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->SwitchSection(context.getGenDwarfSection());
52594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCSymbol *SectionEndSym = context.CreateTempSymbol();
52694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitLabel(SectionEndSym);
52794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  context.setGenDwarfSectionEndSym(SectionEndSym);
52894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
52994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
53094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
53194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // This will be the length of the .debug_aranges section, first account for
53294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // the size of each item in the header (see below where we emit these items).
53394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  int Length = 4 + 2 + 4 + 1 + 1;
53494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
53594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Figure the padding after the header before the table of address and size
53694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // pairs who's values are PointerSize'ed.
53794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  const MCAsmInfo &asmInfo = context.getAsmInfo();
53894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  int AddrSize = asmInfo.getPointerSize();
53994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  int Pad = 2 * AddrSize - (Length & (2 * AddrSize - 1));
54094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  if (Pad == 2 * AddrSize)
54194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    Pad = 0;
54294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  Length += Pad;
54394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
54494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Add the size of the pair of PointerSize'ed values for the address and size
54594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // of the one default .text section we have in the table.
54694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  Length += 2 * AddrSize;
54794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // And the pair of terminating zeros.
54894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  Length += 2 * AddrSize;
54994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
55094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
55194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Emit the header for this section.
55294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // The 4 byte length not including the 4 byte value for the length.
55394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(Length - 4, 4);
55494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // The 2 byte version, which is 2.
55594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(2, 2);
55694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // The 4 byte offset to the compile unit in the .debug_info from the start
55715ab115df57949fa55d92ffdfba491f7d02ed60fAlexey Samsonov  // of the .debug_info.
55815ab115df57949fa55d92ffdfba491f7d02ed60fAlexey Samsonov  if (InfoSectionSymbol)
55915ab115df57949fa55d92ffdfba491f7d02ed60fAlexey Samsonov    MCOS->EmitSymbolValue(InfoSectionSymbol, 4);
56015ab115df57949fa55d92ffdfba491f7d02ed60fAlexey Samsonov  else
56115ab115df57949fa55d92ffdfba491f7d02ed60fAlexey Samsonov    MCOS->EmitIntValue(0, 4);
56294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // The 1 byte size of an address.
56394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(AddrSize, 1);
56494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // The 1 byte size of a segment descriptor, we use a value of zero.
56594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(0, 1);
56694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Align the header with the padding if needed, before we put out the table.
56794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  for(int i = 0; i < Pad; i++)
56894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    MCOS->EmitIntValue(0, 1);
56994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
57094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Now emit the table of pairs of PointerSize'ed values for the section(s)
57194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // address and size, in our case just the one default .text section.
57294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  const MCExpr *Addr = MCSymbolRefExpr::Create(
57394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    context.getGenDwarfSectionStartSym(), MCSymbolRefExpr::VK_None, context);
57494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  const MCExpr *Size = MakeStartMinusEndExpr(*MCOS,
57594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    *context.getGenDwarfSectionStartSym(), *SectionEndSym, 0);
57694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitAbsValue(Addr, AddrSize);
57794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitAbsValue(Size, AddrSize);
57894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
57994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // And finally the pair of terminating zeros.
58094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(0, AddrSize);
58194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(0, AddrSize);
58294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby}
58394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
58494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby// When generating dwarf for assembly source files this emits the data for
58594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby// .debug_info section which contains three parts.  The header, the compile_unit
58638fdb7d9fc40e9f29c3156b6625cac8d91d562e1Kevin Enderby// DIE and a list of label DIEs.
587489d67927172941bf59b9f4829ab8910814fea24Rafael Espindolastatic void EmitGenDwarfInfo(MCStreamer *MCOS,
588489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola                             const MCSymbol *AbbrevSectionSymbol,
589489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola                             const MCSymbol *LineSectionSymbol) {
59094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCContext &context = MCOS->getContext();
59194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
5922684d9e3c702b2ef9fd430155d94671d12fa994fJim Grosbach  MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
59394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
59494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Create a symbol at the start and end of this section used in here for the
59594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // expression to calculate the length in the header.
59694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCSymbol *InfoStart = context.CreateTempSymbol();
59794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitLabel(InfoStart);
59894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCSymbol *InfoEnd = context.CreateTempSymbol();
59994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
60094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // First part: the header.
60194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
60294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // The 4 byte total length of the information for this compilation unit, not
60394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // including these 4 bytes.
60494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  const MCExpr *Length = MakeStartMinusEndExpr(*MCOS, *InfoStart, *InfoEnd, 4);
60594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitAbsValue(Length, 4);
60694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
60794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // The 2 byte DWARF version, which is 2.
60894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(2, 2);
60994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
61094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // The 4 byte offset to the debug abbrevs from the start of the .debug_abbrev,
61194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // it is at the start of that section so this is zero.
612489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola  if (AbbrevSectionSymbol) {
613489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola    MCOS->EmitSymbolValue(AbbrevSectionSymbol, 4);
614489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola  } else {
615489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola    MCOS->EmitIntValue(0, 4);
616489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola  }
61794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
61894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  const MCAsmInfo &asmInfo = context.getAsmInfo();
61994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  int AddrSize = asmInfo.getPointerSize();
62094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // The 1 byte size of an address.
62194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(AddrSize, 1);
62294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
62394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Second part: the compile_unit DIE.
62494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
62594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // The DW_TAG_compile_unit DIE abbrev (1).
62694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitULEB128IntValue(1);
62794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
62894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // DW_AT_stmt_list, a 4 byte offset from the start of the .debug_line section,
62994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // which is at the start of that section so this is zero.
630489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola  if (LineSectionSymbol) {
631489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola    MCOS->EmitSymbolValue(LineSectionSymbol, 4);
632489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola  } else {
633489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola    MCOS->EmitIntValue(0, 4);
634489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola  }
63594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
63694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // AT_low_pc, the first address of the default .text section.
63794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  const MCExpr *Start = MCSymbolRefExpr::Create(
63894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    context.getGenDwarfSectionStartSym(), MCSymbolRefExpr::VK_None, context);
63994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitAbsValue(Start, AddrSize);
64094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
64194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // AT_high_pc, the last address of the default .text section.
64294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  const MCExpr *End = MCSymbolRefExpr::Create(
64394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    context.getGenDwarfSectionEndSym(), MCSymbolRefExpr::VK_None, context);
64494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitAbsValue(End, AddrSize);
64594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
64694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // AT_name, the name of the source file.  Reconstruct from the first directory
64794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // and file table entries.
6489e999adb48beb61663f6abca667b8c85068ee585Manman Ren  const SmallVectorImpl<StringRef> &MCDwarfDirs =
64994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    context.getMCDwarfDirs();
65094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  if (MCDwarfDirs.size() > 0) {
65168ca56285f9b6e82eb16ff8ea02a301f2c489faeEric Christopher    MCOS->EmitBytes(MCDwarfDirs[0]);
65268ca56285f9b6e82eb16ff8ea02a301f2c489faeEric Christopher    MCOS->EmitBytes("/");
65394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  }
6549e999adb48beb61663f6abca667b8c85068ee585Manman Ren  const SmallVectorImpl<MCDwarfFile *> &MCDwarfFiles =
65594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    MCOS->getContext().getMCDwarfFiles();
65668ca56285f9b6e82eb16ff8ea02a301f2c489faeEric Christopher  MCOS->EmitBytes(MCDwarfFiles[1]->getName());
65794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
65894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
65994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // AT_comp_dir, the working directory the assembly was done in.
66068ca56285f9b6e82eb16ff8ea02a301f2c489faeEric Christopher  MCOS->EmitBytes(context.getCompilationDir());
66194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
66294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
66394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // AT_APPLE_flags, the command line arguments of the assembler tool.
66494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
66594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  if (!DwarfDebugFlags.empty()){
66668ca56285f9b6e82eb16ff8ea02a301f2c489faeEric Christopher    MCOS->EmitBytes(DwarfDebugFlags);
66794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
66894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  }
66994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
67094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // AT_producer, the version of the assembler tool.
67175c9b9384f50e9387f24dd7ce6af403cbda6d19aKevin Enderby  StringRef DwarfDebugProducer = context.getDwarfDebugProducer();
67275c9b9384f50e9387f24dd7ce6af403cbda6d19aKevin Enderby  if (!DwarfDebugProducer.empty()){
67375c9b9384f50e9387f24dd7ce6af403cbda6d19aKevin Enderby    MCOS->EmitBytes(DwarfDebugProducer);
67475c9b9384f50e9387f24dd7ce6af403cbda6d19aKevin Enderby  }
67575c9b9384f50e9387f24dd7ce6af403cbda6d19aKevin Enderby  else {
67675c9b9384f50e9387f24dd7ce6af403cbda6d19aKevin Enderby    MCOS->EmitBytes(StringRef("llvm-mc (based on LLVM "));
67775c9b9384f50e9387f24dd7ce6af403cbda6d19aKevin Enderby    MCOS->EmitBytes(StringRef(PACKAGE_VERSION));
67875c9b9384f50e9387f24dd7ce6af403cbda6d19aKevin Enderby    MCOS->EmitBytes(StringRef(")"));
67975c9b9384f50e9387f24dd7ce6af403cbda6d19aKevin Enderby  }
68094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
68194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
68294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // AT_language, a 4 byte value.  We use DW_LANG_Mips_Assembler as the dwarf2
68394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // draft has no standard code for assembler.
68494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(dwarf::DW_LANG_Mips_Assembler, 2);
68594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
68638fdb7d9fc40e9f29c3156b6625cac8d91d562e1Kevin Enderby  // Third part: the list of label DIEs.
68794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
68811c2defa9157bd589cb322218c718c4492ed5746Kevin Enderby  // Loop on saved info for dwarf labels and create the DIEs for them.
68911c2defa9157bd589cb322218c718c4492ed5746Kevin Enderby  const std::vector<const MCGenDwarfLabelEntry *> &Entries =
69011c2defa9157bd589cb322218c718c4492ed5746Kevin Enderby    MCOS->getContext().getMCGenDwarfLabelEntries();
69111c2defa9157bd589cb322218c718c4492ed5746Kevin Enderby  for (std::vector<const MCGenDwarfLabelEntry *>::const_iterator it =
69294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby       Entries.begin(), ie = Entries.end(); it != ie;
69394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby       ++it) {
69411c2defa9157bd589cb322218c718c4492ed5746Kevin Enderby    const MCGenDwarfLabelEntry *Entry = *it;
69594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
69638fdb7d9fc40e9f29c3156b6625cac8d91d562e1Kevin Enderby    // The DW_TAG_label DIE abbrev (2).
69794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    MCOS->EmitULEB128IntValue(2);
69894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
69994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    // AT_name, of the label without any leading underbar.
70068ca56285f9b6e82eb16ff8ea02a301f2c489faeEric Christopher    MCOS->EmitBytes(Entry->getName());
70194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
70294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
70394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    // AT_decl_file, index into the file table.
70494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    MCOS->EmitIntValue(Entry->getFileNumber(), 4);
70594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
70694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    // AT_decl_line, source line number.
70794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    MCOS->EmitIntValue(Entry->getLineNumber(), 4);
70894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
70994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    // AT_low_pc, start address of the label.
71094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    const MCExpr *AT_low_pc = MCSymbolRefExpr::Create(Entry->getLabel(),
71194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby                                             MCSymbolRefExpr::VK_None, context);
71294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    MCOS->EmitAbsValue(AT_low_pc, AddrSize);
71394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
71494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    // DW_AT_prototyped, a one byte flag value of 0 saying we have no prototype.
71594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    MCOS->EmitIntValue(0, 1);
71694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
71794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    // The DW_TAG_unspecified_parameters DIE abbrev (3).
71894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    MCOS->EmitULEB128IntValue(3);
71994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
72094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    // Add the NULL DIE terminating the DW_TAG_unspecified_parameters DIE's.
72194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    MCOS->EmitIntValue(0, 1);
72294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  }
72311c2defa9157bd589cb322218c718c4492ed5746Kevin Enderby  // Deallocate the MCGenDwarfLabelEntry classes that saved away the info
72411c2defa9157bd589cb322218c718c4492ed5746Kevin Enderby  // for the dwarf labels.
72511c2defa9157bd589cb322218c718c4492ed5746Kevin Enderby  for (std::vector<const MCGenDwarfLabelEntry *>::const_iterator it =
72694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby       Entries.begin(), ie = Entries.end(); it != ie;
72794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby       ++it) {
72811c2defa9157bd589cb322218c718c4492ed5746Kevin Enderby    const MCGenDwarfLabelEntry *Entry = *it;
72994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    delete Entry;
73094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  }
73194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
73294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Add the NULL DIE terminating the Compile Unit DIE's.
73394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(0, 1);
73494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
73594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Now set the value of the symbol at the end of the info section.
73694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitLabel(InfoEnd);
73794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby}
73894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
73994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby//
74094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby// When generating dwarf for assembly source files this emits the Dwarf
74194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby// sections.
74294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby//
743489d67927172941bf59b9f4829ab8910814fea24Rafael Espindolavoid MCGenDwarfInfo::Emit(MCStreamer *MCOS, const MCSymbol *LineSectionSymbol) {
74494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Create the dwarf sections in this order (.debug_line already created).
74594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCContext &context = MCOS->getContext();
746489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola  const MCAsmInfo &AsmInfo = context.getAsmInfo();
74715ab115df57949fa55d92ffdfba491f7d02ed60fAlexey Samsonov  bool CreateDwarfSectionSymbols =
74815ab115df57949fa55d92ffdfba491f7d02ed60fAlexey Samsonov      AsmInfo.doesDwarfUseRelocationsAcrossSections();
74915ab115df57949fa55d92ffdfba491f7d02ed60fAlexey Samsonov  if (!CreateDwarfSectionSymbols)
75015ab115df57949fa55d92ffdfba491f7d02ed60fAlexey Samsonov    LineSectionSymbol = NULL;
75115ab115df57949fa55d92ffdfba491f7d02ed60fAlexey Samsonov  MCSymbol *AbbrevSectionSymbol = NULL;
75215ab115df57949fa55d92ffdfba491f7d02ed60fAlexey Samsonov  MCSymbol *InfoSectionSymbol = NULL;
75394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
75415ab115df57949fa55d92ffdfba491f7d02ed60fAlexey Samsonov  if (CreateDwarfSectionSymbols) {
75515ab115df57949fa55d92ffdfba491f7d02ed60fAlexey Samsonov    InfoSectionSymbol = context.CreateTempSymbol();
75615ab115df57949fa55d92ffdfba491f7d02ed60fAlexey Samsonov    MCOS->EmitLabel(InfoSectionSymbol);
75715ab115df57949fa55d92ffdfba491f7d02ed60fAlexey Samsonov  }
75894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
75915ab115df57949fa55d92ffdfba491f7d02ed60fAlexey Samsonov  if (CreateDwarfSectionSymbols) {
760489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola    AbbrevSectionSymbol = context.CreateTempSymbol();
761489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola    MCOS->EmitLabel(AbbrevSectionSymbol);
762489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola  }
76394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
76494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
76594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // If there are no line table entries then do not emit any section contents.
76694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  if (context.getMCLineSections().empty())
76794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    return;
76894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
76994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Output the data for .debug_aranges section.
77015ab115df57949fa55d92ffdfba491f7d02ed60fAlexey Samsonov  EmitGenDwarfAranges(MCOS, InfoSectionSymbol);
77194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
77294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Output the data for .debug_abbrev section.
77394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitGenDwarfAbbrev(MCOS);
77494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
77594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Output the data for .debug_info section.
776489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola  EmitGenDwarfInfo(MCOS, AbbrevSectionSymbol, LineSectionSymbol);
77794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby}
77894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
77994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby//
78094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby// When generating dwarf for assembly source files this is called when symbol
78194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby// for a label is created.  If this symbol is not a temporary and is in the
78294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby// section that dwarf is being generated for, save the needed info to create
78311c2defa9157bd589cb322218c718c4492ed5746Kevin Enderby// a dwarf label.
78494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby//
78511c2defa9157bd589cb322218c718c4492ed5746Kevin Enderbyvoid MCGenDwarfLabelEntry::Make(MCSymbol *Symbol, MCStreamer *MCOS,
78694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby                                     SourceMgr &SrcMgr, SMLoc &Loc) {
7873c3cdcc270515de95a4982db3073b9fdf44a5c2eEric Christopher  // We won't create dwarf labels for temporary symbols or symbols not in
78894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // the default text.
78994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  if (Symbol->isTemporary())
79094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    return;
79194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCContext &context = MCOS->getContext();
792df39be6cb4eb44011db3d3e86f8fe463f81ce127Peter Collingbourne  if (context.getGenDwarfSection() != MCOS->getCurrentSection().first)
79394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    return;
79494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
79511c2defa9157bd589cb322218c718c4492ed5746Kevin Enderby  // The dwarf label's name does not have the symbol name's leading
79694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // underbar if any.
79794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  StringRef Name = Symbol->getName();
79894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  if (Name.startswith("_"))
79994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    Name = Name.substr(1, Name.size()-1);
80094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
80111c2defa9157bd589cb322218c718c4492ed5746Kevin Enderby  // Get the dwarf file number to be used for the dwarf label.
80294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  unsigned FileNumber = context.getGenDwarfFileNumber();
80394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
80494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Finding the line number is the expensive part which is why we just don't
80511c2defa9157bd589cb322218c718c4492ed5746Kevin Enderby  // pass it in as for some symbols we won't create a dwarf label.
80694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  int CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
80794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  unsigned LineNumber = SrcMgr.FindLineNumber(Loc, CurBuffer);
80894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
80994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // We create a temporary symbol for use for the AT_high_pc and AT_low_pc
81094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // values so that they don't have things like an ARM thumb bit from the
81194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // original symbol. So when used they won't get a low bit set after
81294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // relocation.
81394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCSymbol *Label = context.CreateTempSymbol();
81494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitLabel(Label);
81594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
81694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Create and entry for the info and add it to the other entries.
8172684d9e3c702b2ef9fd430155d94671d12fa994fJim Grosbach  MCGenDwarfLabelEntry *Entry =
81811c2defa9157bd589cb322218c718c4492ed5746Kevin Enderby    new MCGenDwarfLabelEntry(Name, FileNumber, LineNumber, Label);
81911c2defa9157bd589cb322218c718c4492ed5746Kevin Enderby  MCOS->getContext().addMCGenDwarfLabelEntry(Entry);
82094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby}
82194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
82289b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindolastatic int getDataAlignmentFactor(MCStreamer &streamer) {
82389b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  MCContext &context = streamer.getContext();
8241be0e271a07925b928ba89848934f1ea6f1854e2Evan Cheng  const MCAsmInfo &asmInfo = context.getAsmInfo();
8259dd2a3b1f2c253e20262535bb89b1ab6cc680eceEli Bendersky  int size = asmInfo.getCalleeSaveStackSlotSize();
8261be0e271a07925b928ba89848934f1ea6f1854e2Evan Cheng  if (asmInfo.isStackGrowthDirectionUp())
82789b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola    return size;
8281be0e271a07925b928ba89848934f1ea6f1854e2Evan Cheng  else
8291be0e271a07925b928ba89848934f1ea6f1854e2Evan Cheng    return -size;
83089b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola}
83189b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
832abf9af60add19a0f037bb2a05433769bfd1e7f6fRafael Espindolastatic unsigned getSizeForEncoding(MCStreamer &streamer,
833abf9af60add19a0f037bb2a05433769bfd1e7f6fRafael Espindola                                   unsigned symbolEncoding) {
83425f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  MCContext &context = streamer.getContext();
83525f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  unsigned format = symbolEncoding & 0x0f;
83625f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  switch (format) {
837858143816d43e58b17bfd11cb1b57afbd7f0f893Craig Topper  default: llvm_unreachable("Unknown Encoding");
83825f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  case dwarf::DW_EH_PE_absptr:
83925f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  case dwarf::DW_EH_PE_signed:
8401be0e271a07925b928ba89848934f1ea6f1854e2Evan Cheng    return context.getAsmInfo().getPointerSize();
84125f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  case dwarf::DW_EH_PE_udata2:
84225f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  case dwarf::DW_EH_PE_sdata2:
843abf9af60add19a0f037bb2a05433769bfd1e7f6fRafael Espindola    return 2;
84425f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  case dwarf::DW_EH_PE_udata4:
84525f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  case dwarf::DW_EH_PE_sdata4:
846abf9af60add19a0f037bb2a05433769bfd1e7f6fRafael Espindola    return 4;
84725f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  case dwarf::DW_EH_PE_udata8:
84825f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  case dwarf::DW_EH_PE_sdata8:
849abf9af60add19a0f037bb2a05433769bfd1e7f6fRafael Espindola    return 8;
85025f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  }
851abf9af60add19a0f037bb2a05433769bfd1e7f6fRafael Espindola}
852abf9af60add19a0f037bb2a05433769bfd1e7f6fRafael Espindola
853abf9af60add19a0f037bb2a05433769bfd1e7f6fRafael Espindolastatic void EmitSymbol(MCStreamer &streamer, const MCSymbol &symbol,
8542541c41f3e2af94585e14353a91f02facd65e415Bill Wendling                       unsigned symbolEncoding, const char *comment = 0) {
855a0057ca13f06b8de08483c3e3a143a7236c67097Rafael Espindola  MCContext &context = streamer.getContext();
856a0057ca13f06b8de08483c3e3a143a7236c67097Rafael Espindola  const MCAsmInfo &asmInfo = context.getAsmInfo();
857a0057ca13f06b8de08483c3e3a143a7236c67097Rafael Espindola  const MCExpr *v = asmInfo.getExprForFDESymbol(&symbol,
858debd7e4e8bc5cfe61bfb71835ce2b1a3fbccc2beRafael Espindola                                                symbolEncoding,
859a0057ca13f06b8de08483c3e3a143a7236c67097Rafael Espindola                                                streamer);
860abf9af60add19a0f037bb2a05433769bfd1e7f6fRafael Espindola  unsigned size = getSizeForEncoding(streamer, symbolEncoding);
8612541c41f3e2af94585e14353a91f02facd65e415Bill Wendling  if (streamer.isVerboseAsm() && comment) streamer.AddComment(comment);
862debd7e4e8bc5cfe61bfb71835ce2b1a3fbccc2beRafael Espindola  streamer.EmitAbsValue(v, size);
86325f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola}
86425f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola
865bfa27cc5d72e061a96efbb461864d40bc8089ec2Rafael Espindolastatic void EmitPersonality(MCStreamer &streamer, const MCSymbol &symbol,
866bfa27cc5d72e061a96efbb461864d40bc8089ec2Rafael Espindola                            unsigned symbolEncoding) {
867bfa27cc5d72e061a96efbb461864d40bc8089ec2Rafael Espindola  MCContext &context = streamer.getContext();
868bfa27cc5d72e061a96efbb461864d40bc8089ec2Rafael Espindola  const MCAsmInfo &asmInfo = context.getAsmInfo();
869debd7e4e8bc5cfe61bfb71835ce2b1a3fbccc2beRafael Espindola  const MCExpr *v = asmInfo.getExprForPersonalitySymbol(&symbol,
870debd7e4e8bc5cfe61bfb71835ce2b1a3fbccc2beRafael Espindola                                                        symbolEncoding,
871debd7e4e8bc5cfe61bfb71835ce2b1a3fbccc2beRafael Espindola                                                        streamer);
872bfa27cc5d72e061a96efbb461864d40bc8089ec2Rafael Espindola  unsigned size = getSizeForEncoding(streamer, symbolEncoding);
873debd7e4e8bc5cfe61bfb71835ce2b1a3fbccc2beRafael Espindola  streamer.EmitValue(v, size);
874bfa27cc5d72e061a96efbb461864d40bc8089ec2Rafael Espindola}
875bfa27cc5d72e061a96efbb461864d40bc8089ec2Rafael Espindola
87625f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindolastatic const MachineLocation TranslateMachineLocation(
8770e6a052331f674dd70e28af41f654a7874405eabEvan Cheng                                                  const MCRegisterInfo &MRI,
87825f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                                                  const MachineLocation &Loc) {
87925f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  unsigned Reg = Loc.getReg() == MachineLocation::VirtualFP ?
88025f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola    MachineLocation::VirtualFP :
8810e6a052331f674dd70e28af41f654a7874405eabEvan Cheng    unsigned(MRI.getDwarfRegNum(Loc.getReg(), true));
88225f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  const MachineLocation &NewLoc = Loc.isReg() ?
88325f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola    MachineLocation(Reg) : MachineLocation(Reg, Loc.getOffset());
88425f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  return NewLoc;
88525f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola}
88625f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola
88725f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindolanamespace {
88825f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  class FrameEmitterImpl {
88925f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola    int CFAOffset;
890514cecc72b23bb75108844285ab8f3616a08bac5Rafael Espindola    int CIENum;
8915426a9ee37667660935d80841c5392d78e254318Rafael Espindola    bool UsingCFI;
89240a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    bool IsEH;
893e3a0e987f3d4f07512cdb64b9034369f966cb448Rafael Espindola    const MCSymbol *SectionStart;
89425f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  public:
8951424c3c3e696868775ac5bfc57d27a0ac3bc6c63Bill Wendling    FrameEmitterImpl(bool usingCFI, bool isEH)
8961424c3c3e696868775ac5bfc57d27a0ac3bc6c63Bill Wendling      : CFAOffset(0), CIENum(0), UsingCFI(usingCFI), IsEH(isEH),
8971424c3c3e696868775ac5bfc57d27a0ac3bc6c63Bill Wendling        SectionStart(0) {}
8981424c3c3e696868775ac5bfc57d27a0ac3bc6c63Bill Wendling
8991424c3c3e696868775ac5bfc57d27a0ac3bc6c63Bill Wendling    void setSectionStart(const MCSymbol *Label) { SectionStart = Label; }
90025f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola
901e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling    /// EmitCompactUnwind - Emit the unwind information in a compact way. If
902e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling    /// we're successful, return 'true'. Otherwise, return 'false' and it will
903e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling    /// emit the normal CIE and FDE.
9043c08eef20b9cfc31d4ae3a43f1c9a57a4ab2cad7Bill Wendling    void EmitCompactUnwind(MCStreamer &streamer,
905e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling                           const MCDwarfFrameInfo &frame);
906e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling
90725f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola    const MCSymbol &EmitCIE(MCStreamer &streamer,
90825f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                            const MCSymbol *personality,
90925f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                            unsigned personalityEncoding,
91025f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                            const MCSymbol *lsda,
91116d7d437e03ce87fdaef7971919302920d54a966Rafael Espindola                            bool IsSignalFrame,
91225f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                            unsigned lsdaEncoding);
91325f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola    MCSymbol *EmitFDE(MCStreamer &streamer,
91425f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                      const MCSymbol &cieStart,
9159f270dadd429771dded5a8572da8c74513771c15Rafael Espindola                      const MCDwarfFrameInfo &frame);
91625f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola    void EmitCFIInstructions(MCStreamer &streamer,
91725f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                             const std::vector<MCCFIInstruction> &Instrs,
91825f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                             MCSymbol *BaseLabel);
91925f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola    void EmitCFIInstruction(MCStreamer &Streamer,
92025f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                            const MCCFIInstruction &Instr);
92125f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  };
9223c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling
9233c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling} // end anonymous namespace
9243c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling
9253c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendlingstatic void EmitEncodingByte(MCStreamer &Streamer, unsigned Encoding,
9263c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling                             StringRef Prefix) {
9273c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling  if (Streamer.isVerboseAsm()) {
928eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer    const char *EncStr;
9293c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling    switch (Encoding) {
930eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer    default: EncStr = "<unknown encoding>"; break;
931eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer    case dwarf::DW_EH_PE_absptr: EncStr = "absptr"; break;
932eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer    case dwarf::DW_EH_PE_omit:   EncStr = "omit"; break;
933eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer    case dwarf::DW_EH_PE_pcrel:  EncStr = "pcrel"; break;
934eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer    case dwarf::DW_EH_PE_udata4: EncStr = "udata4"; break;
935eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer    case dwarf::DW_EH_PE_udata8: EncStr = "udata8"; break;
936eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer    case dwarf::DW_EH_PE_sdata4: EncStr = "sdata4"; break;
937eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer    case dwarf::DW_EH_PE_sdata8: EncStr = "sdata8"; break;
938eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer    case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
939eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer      EncStr = "pcrel udata4";
940eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer      break;
941eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer    case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
942eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer      EncStr = "pcrel sdata4";
943eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer      break;
944eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer    case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
945eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer      EncStr = "pcrel udata8";
946eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer      break;
947eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer    case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
948eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer      EncStr = "screl sdata8";
949eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer      break;
9503c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling    case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata4:
9513c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling      EncStr = "indirect pcrel udata4";
952eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer      break;
9533c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling    case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata4:
9543c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling      EncStr = "indirect pcrel sdata4";
955eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer      break;
9563c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling    case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata8:
9573c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling      EncStr = "indirect pcrel udata8";
958eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer      break;
9593c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling    case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata8:
9603c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling      EncStr = "indirect pcrel sdata8";
961eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer      break;
9623c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling    }
9633c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling
9643c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling    Streamer.AddComment(Twine(Prefix) + " = " + EncStr);
9653c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling  }
9663c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling
9673c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling  Streamer.EmitIntValue(Encoding, 1);
96825f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola}
96925f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola
97025f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindolavoid FrameEmitterImpl::EmitCFIInstruction(MCStreamer &Streamer,
97125f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                                          const MCCFIInstruction &Instr) {
972fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola  int dataAlignmentFactor = getDataAlignmentFactor(Streamer);
973efd24ddbffc1e69fa88089de036d1873c950dfb5Bill Wendling  bool VerboseAsm = Streamer.isVerboseAsm();
97489b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
975fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola  switch (Instr.getOperation()) {
976f4f14f68f6078ea6681ee27b5bf42719d7db3441Rafael Espindola  case MCCFIInstruction::OpRegister: {
977f4f14f68f6078ea6681ee27b5bf42719d7db3441Rafael Espindola    unsigned Reg1 = Instr.getRegister();
978f4f14f68f6078ea6681ee27b5bf42719d7db3441Rafael Espindola    unsigned Reg2 = Instr.getRegister2();
979f4f14f68f6078ea6681ee27b5bf42719d7db3441Rafael Espindola    if (VerboseAsm) {
980f4f14f68f6078ea6681ee27b5bf42719d7db3441Rafael Espindola      Streamer.AddComment("DW_CFA_register");
981f4f14f68f6078ea6681ee27b5bf42719d7db3441Rafael Espindola      Streamer.AddComment(Twine("Reg1 ") + Twine(Reg1));
982f4f14f68f6078ea6681ee27b5bf42719d7db3441Rafael Espindola      Streamer.AddComment(Twine("Reg2 ") + Twine(Reg2));
983f4f14f68f6078ea6681ee27b5bf42719d7db3441Rafael Espindola    }
984f4f14f68f6078ea6681ee27b5bf42719d7db3441Rafael Espindola    Streamer.EmitIntValue(dwarf::DW_CFA_register, 1);
985f4f14f68f6078ea6681ee27b5bf42719d7db3441Rafael Espindola    Streamer.EmitULEB128IntValue(Reg1);
986f4f14f68f6078ea6681ee27b5bf42719d7db3441Rafael Espindola    Streamer.EmitULEB128IntValue(Reg2);
987f4f14f68f6078ea6681ee27b5bf42719d7db3441Rafael Espindola    return;
988f4f14f68f6078ea6681ee27b5bf42719d7db3441Rafael Espindola  }
9897f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola  case MCCFIInstruction::OpUndefined: {
990ff233c9e5c3e439fd1eed84b9a9e88a5370572dbRafael Espindola    unsigned Reg = Instr.getRegister();
991c8fec7e21f5c24303eab8a8592f3b8faff347d86Rafael Espindola    if (VerboseAsm) {
992c8fec7e21f5c24303eab8a8592f3b8faff347d86Rafael Espindola      Streamer.AddComment("DW_CFA_undefined");
993c8fec7e21f5c24303eab8a8592f3b8faff347d86Rafael Espindola      Streamer.AddComment(Twine("Reg ") + Twine(Reg));
994c8fec7e21f5c24303eab8a8592f3b8faff347d86Rafael Espindola    }
995c8fec7e21f5c24303eab8a8592f3b8faff347d86Rafael Espindola    Streamer.EmitIntValue(dwarf::DW_CFA_undefined, 1);
996c8fec7e21f5c24303eab8a8592f3b8faff347d86Rafael Espindola    Streamer.EmitULEB128IntValue(Reg);
997c8fec7e21f5c24303eab8a8592f3b8faff347d86Rafael Espindola    return;
998c8fec7e21f5c24303eab8a8592f3b8faff347d86Rafael Espindola  }
9997f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola  case MCCFIInstruction::OpAdjustCfaOffset:
10007f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola  case MCCFIInstruction::OpDefCfaOffset: {
10017f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola    const bool IsRelative =
10027f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola      Instr.getOperation() == MCCFIInstruction::OpAdjustCfaOffset;
100389b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
10047f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola    if (VerboseAsm)
10057f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola      Streamer.AddComment("DW_CFA_def_cfa_offset");
10067f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola    Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_offset, 1);
10075d7dcd3335234d2a2bc16dc69f86fbb5dcaa8962Rafael Espindola
10087f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola    if (IsRelative)
1009ff233c9e5c3e439fd1eed84b9a9e88a5370572dbRafael Espindola      CFAOffset += Instr.getOffset();
10107f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola    else
1011ff233c9e5c3e439fd1eed84b9a9e88a5370572dbRafael Espindola      CFAOffset = -Instr.getOffset();
101289b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
10137f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola    if (VerboseAsm)
10147f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola      Streamer.AddComment(Twine("Offset " + Twine(CFAOffset)));
10157f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola    Streamer.EmitULEB128IntValue(CFAOffset);
10167f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola
10177f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola    return;
10187f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola  }
10197f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola  case MCCFIInstruction::OpDefCfa: {
10207f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola    if (VerboseAsm)
10217f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola      Streamer.AddComment("DW_CFA_def_cfa");
10227f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola    Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa, 1);
10237f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola
10247f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola    if (VerboseAsm)
1025ff233c9e5c3e439fd1eed84b9a9e88a5370572dbRafael Espindola      Streamer.AddComment(Twine("Reg ") + Twine(Instr.getRegister()));
1026ff233c9e5c3e439fd1eed84b9a9e88a5370572dbRafael Espindola    Streamer.EmitULEB128IntValue(Instr.getRegister());
10277f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola
1028ff233c9e5c3e439fd1eed84b9a9e88a5370572dbRafael Espindola    CFAOffset = -Instr.getOffset();
10297f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola
10307f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola    if (VerboseAsm)
10317f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola      Streamer.AddComment(Twine("Offset " + Twine(CFAOffset)));
10327f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola    Streamer.EmitULEB128IntValue(CFAOffset);
10337f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola
10347f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola    return;
10357f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola  }
10367f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola
10377f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola  case MCCFIInstruction::OpDefCfaRegister: {
10387f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola    if (VerboseAsm)
10397f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola      Streamer.AddComment("DW_CFA_def_cfa_register");
10407f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola    Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_register, 1);
10417f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola
10427f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola    if (VerboseAsm)
1043ff233c9e5c3e439fd1eed84b9a9e88a5370572dbRafael Espindola      Streamer.AddComment(Twine("Reg ") + Twine(Instr.getRegister()));
1044ff233c9e5c3e439fd1eed84b9a9e88a5370572dbRafael Espindola    Streamer.EmitULEB128IntValue(Instr.getRegister());
10457f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola
10467f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola    return;
10477f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola  }
10487f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola
10497f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola  case MCCFIInstruction::OpOffset:
10507f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola  case MCCFIInstruction::OpRelOffset: {
10517f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola    const bool IsRelative =
10527f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola      Instr.getOperation() == MCCFIInstruction::OpRelOffset;
105389b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
1054ff233c9e5c3e439fd1eed84b9a9e88a5370572dbRafael Espindola    unsigned Reg = Instr.getRegister();
1055ff233c9e5c3e439fd1eed84b9a9e88a5370572dbRafael Espindola    int Offset = Instr.getOffset();
105625f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola    if (IsRelative)
105725f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola      Offset -= CFAOffset;
105825f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola    Offset = Offset / dataAlignmentFactor;
105989b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
106089b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola    if (Offset < 0) {
1061efd24ddbffc1e69fa88089de036d1873c950dfb5Bill Wendling      if (VerboseAsm) Streamer.AddComment("DW_CFA_offset_extended_sf");
1062fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola      Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended_sf, 1);
1063efd24ddbffc1e69fa88089de036d1873c950dfb5Bill Wendling      if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
1064fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola      Streamer.EmitULEB128IntValue(Reg);
1065efd24ddbffc1e69fa88089de036d1873c950dfb5Bill Wendling      if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
1066fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola      Streamer.EmitSLEB128IntValue(Offset);
106789b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola    } else if (Reg < 64) {
1068e08d4335ad29d74008222b4d7ac91c153ed66becBill Wendling      if (VerboseAsm) Streamer.AddComment(Twine("DW_CFA_offset + Reg(") +
1069e08d4335ad29d74008222b4d7ac91c153ed66becBill Wendling                                          Twine(Reg) + ")");
1070fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola      Streamer.EmitIntValue(dwarf::DW_CFA_offset + Reg, 1);
1071efd24ddbffc1e69fa88089de036d1873c950dfb5Bill Wendling      if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
1072eccbad78e0854743e358841b64f9e0f5eba82861Rafael Espindola      Streamer.EmitULEB128IntValue(Offset);
107389b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola    } else {
1074efd24ddbffc1e69fa88089de036d1873c950dfb5Bill Wendling      if (VerboseAsm) Streamer.AddComment("DW_CFA_offset_extended");
1075fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola      Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended, 1);
1076efd24ddbffc1e69fa88089de036d1873c950dfb5Bill Wendling      if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
1077eccbad78e0854743e358841b64f9e0f5eba82861Rafael Espindola      Streamer.EmitULEB128IntValue(Reg);
1078efd24ddbffc1e69fa88089de036d1873c950dfb5Bill Wendling      if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
1079eccbad78e0854743e358841b64f9e0f5eba82861Rafael Espindola      Streamer.EmitULEB128IntValue(Offset);
1080fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola    }
1081fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola    return;
1082fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola  }
10837f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola  case MCCFIInstruction::OpRememberState:
1084efd24ddbffc1e69fa88089de036d1873c950dfb5Bill Wendling    if (VerboseAsm) Streamer.AddComment("DW_CFA_remember_state");
1085fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola    Streamer.EmitIntValue(dwarf::DW_CFA_remember_state, 1);
1086fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola    return;
10877f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola  case MCCFIInstruction::OpRestoreState:
1088efd24ddbffc1e69fa88089de036d1873c950dfb5Bill Wendling    if (VerboseAsm) Streamer.AddComment("DW_CFA_restore_state");
1089fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola    Streamer.EmitIntValue(dwarf::DW_CFA_restore_state, 1);
1090fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola    return;
10917f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola  case MCCFIInstruction::OpSameValue: {
1092ff233c9e5c3e439fd1eed84b9a9e88a5370572dbRafael Espindola    unsigned Reg = Instr.getRegister();
1093efd24ddbffc1e69fa88089de036d1873c950dfb5Bill Wendling    if (VerboseAsm) Streamer.AddComment("DW_CFA_same_value");
1094c57543964d1382d3d3a5005f415b6c0f49671b3aRafael Espindola    Streamer.EmitIntValue(dwarf::DW_CFA_same_value, 1);
1095efd24ddbffc1e69fa88089de036d1873c950dfb5Bill Wendling    if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
1096e8cfbd843d737e1f95c3032c7670c2be3838a6f6Rafael Espindola    Streamer.EmitULEB128IntValue(Reg);
1097c57543964d1382d3d3a5005f415b6c0f49671b3aRafael Espindola    return;
1098c57543964d1382d3d3a5005f415b6c0f49671b3aRafael Espindola  }
10997f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola  case MCCFIInstruction::OpRestore: {
1100ff233c9e5c3e439fd1eed84b9a9e88a5370572dbRafael Espindola    unsigned Reg = Instr.getRegister();
1101ed23bdb65fe86cdb7a38c8c1998ec965e6973966Rafael Espindola    if (VerboseAsm) {
1102ed23bdb65fe86cdb7a38c8c1998ec965e6973966Rafael Espindola      Streamer.AddComment("DW_CFA_restore");
1103ed23bdb65fe86cdb7a38c8c1998ec965e6973966Rafael Espindola      Streamer.AddComment(Twine("Reg ") + Twine(Reg));
1104ed23bdb65fe86cdb7a38c8c1998ec965e6973966Rafael Espindola    }
1105ed23bdb65fe86cdb7a38c8c1998ec965e6973966Rafael Espindola    Streamer.EmitIntValue(dwarf::DW_CFA_restore | Reg, 1);
1106ed23bdb65fe86cdb7a38c8c1998ec965e6973966Rafael Espindola    return;
1107ed23bdb65fe86cdb7a38c8c1998ec965e6973966Rafael Espindola  }
11087f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola  case MCCFIInstruction::OpEscape:
11096f0b181bc70318f8d5d4b9bdead7fc748677fe2aRafael Espindola    if (VerboseAsm) Streamer.AddComment("Escape bytes");
111068ca56285f9b6e82eb16ff8ea02a301f2c489faeEric Christopher    Streamer.EmitBytes(Instr.getValues());
11116f0b181bc70318f8d5d4b9bdead7fc748677fe2aRafael Espindola    return;
1112fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola  }
1113fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola  llvm_unreachable("Unhandled case in switch");
1114fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola}
1115fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola
1116fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola/// EmitFrameMoves - Emit frame instructions to describe the layout of the
1117fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola/// frame.
111825f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindolavoid FrameEmitterImpl::EmitCFIInstructions(MCStreamer &streamer,
111925f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                                    const std::vector<MCCFIInstruction> &Instrs,
112025f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                                           MCSymbol *BaseLabel) {
1121fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola  for (unsigned i = 0, N = Instrs.size(); i < N; ++i) {
1122fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola    const MCCFIInstruction &Instr = Instrs[i];
1123fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola    MCSymbol *Label = Instr.getLabel();
1124fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola    // Throw out move if the label is invalid.
1125fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola    if (Label && !Label->isDefined()) continue; // Not emitted, in dead code.
1126fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola
1127fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola    // Advance row if new location.
1128fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola    if (BaseLabel && Label) {
1129fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola      MCSymbol *ThisSym = Label;
1130fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola      if (ThisSym != BaseLabel) {
1131d221cd676b44d8118b1db152ecc2168538479626Bill Wendling        if (streamer.isVerboseAsm()) streamer.AddComment("DW_CFA_advance_loc4");
1132fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola        streamer.EmitDwarfAdvanceFrameAddr(BaseLabel, ThisSym);
1133fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola        BaseLabel = ThisSym;
1134fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola      }
113589b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola    }
1136fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola
1137b40a71fda188f8ca564e606ac2cb051a44ada311Rafael Espindola    EmitCFIInstruction(streamer, Instr);
113889b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  }
113989b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola}
114089b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
1141e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling/// EmitCompactUnwind - Emit the unwind information in a compact way. If we're
1142e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling/// successful, return 'true'. Otherwise, return 'false' and it will emit the
1143e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling/// normal CIE and FDE.
11443c08eef20b9cfc31d4ae3a43f1c9a57a4ab2cad7Bill Wendlingvoid FrameEmitterImpl::EmitCompactUnwind(MCStreamer &Streamer,
1145e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling                                         const MCDwarfFrameInfo &Frame) {
1146e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  MCContext &Context = Streamer.getContext();
1147e76a33b9567d78a5744dc52fcec3a6056d6fb576Evan Cheng  const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
11483c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling  bool VerboseAsm = Streamer.isVerboseAsm();
1149e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling
1150e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  // range-start range-length  compact-unwind-enc personality-func   lsda
1151e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //  _foo       LfooEnd-_foo  0x00000023          0                 0
1152e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //  _bar       LbarEnd-_bar  0x00000025         __gxx_personality  except_tab1
1153e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //
1154e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //   .section __LD,__compact_unwind,regular,debug
1155e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //
1156e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //   # compact unwind for _foo
1157e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //   .quad _foo
1158e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //   .set L1,LfooEnd-_foo
1159e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //   .long L1
1160e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //   .long 0x01010001
1161e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //   .quad 0
1162e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //   .quad 0
1163e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //
1164e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //   # compact unwind for _bar
1165e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //   .quad _bar
1166e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //   .set L2,LbarEnd-_bar
1167e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //   .long L2
1168e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //   .long 0x01020011
1169e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //   .quad __gxx_personality
1170e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //   .quad except_tab1
1171e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling
1172e52e3f2c020354c8e0e966b719152b08349d0334Bill Wendling  uint32_t Encoding = Frame.CompactUnwindEncoding;
1173fa2b25c57300dc9c518ea35bbc9dc8658fdbf454Bill Wendling  if (!Encoding) return;
117462c75ad4301f07c94d7a231d2da9f9cd47b7824cBill Wendling  bool DwarfEHFrameOnly = (Encoding == MOFI->getCompactUnwindDwarfEHFrameOnly());
11756a6b8c3e96b9e1ca7092eafd0cfb219cbbfbdfc4Bill Wendling
11764bb5b037126ea35f35d5a490b03ccffa8677fa5eBill Wendling  // The encoding needs to know we have an LSDA.
117762c75ad4301f07c94d7a231d2da9f9cd47b7824cBill Wendling  if (!DwarfEHFrameOnly && Frame.Lsda)
11784bb5b037126ea35f35d5a490b03ccffa8677fa5eBill Wendling    Encoding |= 0x40000000;
11794bb5b037126ea35f35d5a490b03ccffa8677fa5eBill Wendling
1180e76a33b9567d78a5744dc52fcec3a6056d6fb576Evan Cheng  Streamer.SwitchSection(MOFI->getCompactUnwindSection());
11813c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling
1182e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  // Range Start
1183203576aa0cb9d8bf2d2e4d910ebab4b7a63262aeEvan Cheng  unsigned FDEEncoding = MOFI->getFDEEncoding(UsingCFI);
11849056e9032001a2d47057cecec5e39895cbc31799Bill Wendling  unsigned Size = getSizeForEncoding(Streamer, FDEEncoding);
11853c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling  if (VerboseAsm) Streamer.AddComment("Range Start");
11869056e9032001a2d47057cecec5e39895cbc31799Bill Wendling  Streamer.EmitSymbolValue(Frame.Function, Size);
1187e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling
1188e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  // Range Length
1189e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  const MCExpr *Range = MakeStartMinusEndExpr(Streamer, *Frame.Begin,
1190e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling                                              *Frame.End, 0);
11913c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling  if (VerboseAsm) Streamer.AddComment("Range Length");
11929287a6eef336585628a55e1f33e1e3ea9d7f81cfBill Wendling  Streamer.EmitAbsValue(Range, 4);
11939287a6eef336585628a55e1f33e1e3ea9d7f81cfBill Wendling
11949287a6eef336585628a55e1f33e1e3ea9d7f81cfBill Wendling  // Compact Encoding
11959287a6eef336585628a55e1f33e1e3ea9d7f81cfBill Wendling  Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_udata4);
119670be28a5adba5bcae0c6dcd63f17592864c351fcBenjamin Kramer  if (VerboseAsm) Streamer.AddComment("Compact Unwind Encoding: 0x" +
119770be28a5adba5bcae0c6dcd63f17592864c351fcBenjamin Kramer                                      Twine::utohexstr(Encoding));
11989287a6eef336585628a55e1f33e1e3ea9d7f81cfBill Wendling  Streamer.EmitIntValue(Encoding, Size);
11999287a6eef336585628a55e1f33e1e3ea9d7f81cfBill Wendling
12009056e9032001a2d47057cecec5e39895cbc31799Bill Wendling  // Personality Function
12014bb5b037126ea35f35d5a490b03ccffa8677fa5eBill Wendling  Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_absptr);
12023c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling  if (VerboseAsm) Streamer.AddComment("Personality Function");
120362c75ad4301f07c94d7a231d2da9f9cd47b7824cBill Wendling  if (!DwarfEHFrameOnly && Frame.Personality)
12049056e9032001a2d47057cecec5e39895cbc31799Bill Wendling    Streamer.EmitSymbolValue(Frame.Personality, Size);
12054498d39680abe3970dc84dca973aff46d0f9039bBill Wendling  else
12064498d39680abe3970dc84dca973aff46d0f9039bBill Wendling    Streamer.EmitIntValue(0, Size); // No personality fn
12079056e9032001a2d47057cecec5e39895cbc31799Bill Wendling
12089056e9032001a2d47057cecec5e39895cbc31799Bill Wendling  // LSDA
12094498d39680abe3970dc84dca973aff46d0f9039bBill Wendling  Size = getSizeForEncoding(Streamer, Frame.LsdaEncoding);
12103c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling  if (VerboseAsm) Streamer.AddComment("LSDA");
121162c75ad4301f07c94d7a231d2da9f9cd47b7824cBill Wendling  if (!DwarfEHFrameOnly && Frame.Lsda)
12129056e9032001a2d47057cecec5e39895cbc31799Bill Wendling    Streamer.EmitSymbolValue(Frame.Lsda, Size);
12134498d39680abe3970dc84dca973aff46d0f9039bBill Wendling  else
12144498d39680abe3970dc84dca973aff46d0f9039bBill Wendling    Streamer.EmitIntValue(0, Size); // No LSDA
1215e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling}
1216e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling
121725f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindolaconst MCSymbol &FrameEmitterImpl::EmitCIE(MCStreamer &streamer,
121825f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                                          const MCSymbol *personality,
121925f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                                          unsigned personalityEncoding,
122025f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                                          const MCSymbol *lsda,
122116d7d437e03ce87fdaef7971919302920d54a966Rafael Espindola                                          bool IsSignalFrame,
122225f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                                          unsigned lsdaEncoding) {
122389b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  MCContext &context = streamer.getContext();
12240e6a052331f674dd70e28af41f654a7874405eabEvan Cheng  const MCRegisterInfo &MRI = context.getRegisterInfo();
1225e76a33b9567d78a5744dc52fcec3a6056d6fb576Evan Cheng  const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
12263c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling  bool verboseAsm = streamer.isVerboseAsm();
1227514cecc72b23bb75108844285ab8f3616a08bac5Rafael Espindola
1228514cecc72b23bb75108844285ab8f3616a08bac5Rafael Espindola  MCSymbol *sectionStart;
1229e76a33b9567d78a5744dc52fcec3a6056d6fb576Evan Cheng  if (MOFI->isFunctionEHFrameSymbolPrivate() || !IsEH)
1230514cecc72b23bb75108844285ab8f3616a08bac5Rafael Espindola    sectionStart = context.CreateTempSymbol();
1231514cecc72b23bb75108844285ab8f3616a08bac5Rafael Espindola  else
1232514cecc72b23bb75108844285ab8f3616a08bac5Rafael Espindola    sectionStart = context.GetOrCreateSymbol(Twine("EH_frame") + Twine(CIENum));
1233514cecc72b23bb75108844285ab8f3616a08bac5Rafael Espindola
12343c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling  streamer.EmitLabel(sectionStart);
1235514cecc72b23bb75108844285ab8f3616a08bac5Rafael Espindola  CIENum++;
1236514cecc72b23bb75108844285ab8f3616a08bac5Rafael Espindola
12376a6b8c3e96b9e1ca7092eafd0cfb219cbbfbdfc4Bill Wendling  MCSymbol *sectionEnd = context.CreateTempSymbol();
123889b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
123989b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  // Length
124089b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  const MCExpr *Length = MakeStartMinusEndExpr(streamer, *sectionStart,
124189b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola                                               *sectionEnd, 4);
12423c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling  if (verboseAsm) streamer.AddComment("CIE Length");
12439266cc400ee2fc86abb24ab0819d9da280fb61c4Rafael Espindola  streamer.EmitAbsValue(Length, 4);
124489b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
124589b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  // CIE ID
124640a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola  unsigned CIE_ID = IsEH ? 0 : -1;
12473c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling  if (verboseAsm) streamer.AddComment("CIE ID Tag");
124840a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola  streamer.EmitIntValue(CIE_ID, 4);
124989b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
125089b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  // Version
12513c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling  if (verboseAsm) streamer.AddComment("DW_CIE_VERSION");
125289b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  streamer.EmitIntValue(dwarf::DW_CIE_VERSION, 1);
125389b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
125489b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  // Augmentation String
125589b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  SmallString<8> Augmentation;
125640a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola  if (IsEH) {
12573c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling    if (verboseAsm) streamer.AddComment("CIE Augmentation");
125840a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    Augmentation += "z";
125940a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    if (personality)
126040a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola      Augmentation += "P";
126140a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    if (lsda)
126240a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola      Augmentation += "L";
126340a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    Augmentation += "R";
126416d7d437e03ce87fdaef7971919302920d54a966Rafael Espindola    if (IsSignalFrame)
126516d7d437e03ce87fdaef7971919302920d54a966Rafael Espindola      Augmentation += "S";
126668ca56285f9b6e82eb16ff8ea02a301f2c489faeEric Christopher    streamer.EmitBytes(Augmentation.str());
126740a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola  }
126889b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  streamer.EmitIntValue(0, 1);
126989b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
127089b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  // Code Alignment Factor
12713c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling  if (verboseAsm) streamer.AddComment("CIE Code Alignment Factor");
127289b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  streamer.EmitULEB128IntValue(1);
127389b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
127489b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  // Data Alignment Factor
12753c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling  if (verboseAsm) streamer.AddComment("CIE Data Alignment Factor");
127689b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  streamer.EmitSLEB128IntValue(getDataAlignmentFactor(streamer));
127789b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
127889b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  // Return Address Register
12793c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling  if (verboseAsm) streamer.AddComment("CIE Return Address Column");
12800e6a052331f674dd70e28af41f654a7874405eabEvan Cheng  streamer.EmitULEB128IntValue(MRI.getDwarfRegNum(MRI.getRARegister(), true));
128189b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
128289b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  // Augmentation Data Length (optional)
12831f6c87554a0d78103b95816fd2c1ce392b366bcfRafael Espindola
12841f6c87554a0d78103b95816fd2c1ce392b366bcfRafael Espindola  unsigned augmentationLength = 0;
128540a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola  if (IsEH) {
128640a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    if (personality) {
128740a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola      // Personality Encoding
128840a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola      augmentationLength += 1;
128940a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola      // Personality
129040a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola      augmentationLength += getSizeForEncoding(streamer, personalityEncoding);
129140a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    }
129240a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    if (lsda)
129340a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola      augmentationLength += 1;
129440a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    // Encoding of the FDE pointers
12951f6c87554a0d78103b95816fd2c1ce392b366bcfRafael Espindola    augmentationLength += 1;
12961f6c87554a0d78103b95816fd2c1ce392b366bcfRafael Espindola
12973c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling    if (verboseAsm) streamer.AddComment("Augmentation Size");
129840a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    streamer.EmitULEB128IntValue(augmentationLength);
129989b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
130040a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    // Augmentation Data (optional)
130140a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    if (personality) {
130240a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola      // Personality Encoding
13033c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling      EmitEncodingByte(streamer, personalityEncoding,
13043c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling                       "Personality Encoding");
130540a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola      // Personality
13063c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling      if (verboseAsm) streamer.AddComment("Personality");
130740a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola      EmitPersonality(streamer, *personality, personalityEncoding);
130840a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    }
13093c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling
131040a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    if (lsda)
13113c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling      EmitEncodingByte(streamer, lsdaEncoding, "LSDA Encoding");
13123c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling
131340a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    // Encoding of the FDE pointers
1314203576aa0cb9d8bf2d2e4d910ebab4b7a63262aeEvan Cheng    EmitEncodingByte(streamer, MOFI->getFDEEncoding(UsingCFI),
13153c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling                     "FDE Encoding");
1316bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola  }
131789b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
131889b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  // Initial Instructions
131989b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
13202d28617de2b0b731c08d1af9e830f31e14ac75b4Evan Cheng  const MCAsmInfo &MAI = context.getAsmInfo();
13212d28617de2b0b731c08d1af9e830f31e14ac75b4Evan Cheng  const std::vector<MachineMove> &Moves = MAI.getInitialFrameState();
1322fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola  std::vector<MCCFIInstruction> Instructions;
1323fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola
1324fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola  for (int i = 0, n = Moves.size(); i != n; ++i) {
1325b40a71fda188f8ca564e606ac2cb051a44ada311Rafael Espindola    MCSymbol *Label = Moves[i].getLabel();
1326b40a71fda188f8ca564e606ac2cb051a44ada311Rafael Espindola    const MachineLocation &Dst =
13270e6a052331f674dd70e28af41f654a7874405eabEvan Cheng      TranslateMachineLocation(MRI, Moves[i].getDestination());
1328b40a71fda188f8ca564e606ac2cb051a44ada311Rafael Espindola    const MachineLocation &Src =
13290e6a052331f674dd70e28af41f654a7874405eabEvan Cheng      TranslateMachineLocation(MRI, Moves[i].getSource());
133028c9ea3c13dfb8f6bb3226ba511d189135fcb140Rafael Espindola
133128c9ea3c13dfb8f6bb3226ba511d189135fcb140Rafael Espindola    if (Dst.isReg()) {
133228c9ea3c13dfb8f6bb3226ba511d189135fcb140Rafael Espindola      assert(Dst.getReg() == MachineLocation::VirtualFP);
133328c9ea3c13dfb8f6bb3226ba511d189135fcb140Rafael Espindola      assert(!Src.isReg());
133428c9ea3c13dfb8f6bb3226ba511d189135fcb140Rafael Espindola      MCCFIInstruction Inst =
133528c9ea3c13dfb8f6bb3226ba511d189135fcb140Rafael Espindola        MCCFIInstruction::createDefCfa(Label, Src.getReg(), -Src.getOffset());
133628c9ea3c13dfb8f6bb3226ba511d189135fcb140Rafael Espindola      Instructions.push_back(Inst);
133728c9ea3c13dfb8f6bb3226ba511d189135fcb140Rafael Espindola    } else {
133828c9ea3c13dfb8f6bb3226ba511d189135fcb140Rafael Espindola      assert(Src.isReg());
133928c9ea3c13dfb8f6bb3226ba511d189135fcb140Rafael Espindola      unsigned Reg = Src.getReg();
134028c9ea3c13dfb8f6bb3226ba511d189135fcb140Rafael Espindola      int Offset = Dst.getOffset();
134128c9ea3c13dfb8f6bb3226ba511d189135fcb140Rafael Espindola      MCCFIInstruction Inst =
13427f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola        MCCFIInstruction::createOffset(Label, Reg, Offset);
134328c9ea3c13dfb8f6bb3226ba511d189135fcb140Rafael Espindola      Instructions.push_back(Inst);
134428c9ea3c13dfb8f6bb3226ba511d189135fcb140Rafael Espindola    }
1345fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola  }
134689b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
1347b40a71fda188f8ca564e606ac2cb051a44ada311Rafael Espindola  EmitCFIInstructions(streamer, Instructions, NULL);
134889b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
134989b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  // Padding
13501be0e271a07925b928ba89848934f1ea6f1854e2Evan Cheng  streamer.EmitValueToAlignment(IsEH
13511be0e271a07925b928ba89848934f1ea6f1854e2Evan Cheng                                ? 4 : context.getAsmInfo().getPointerSize());
135289b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
135389b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  streamer.EmitLabel(sectionEnd);
135489b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  return *sectionStart;
135589b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola}
135689b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
135725f492e77858dc5a95fcd7180e73aff47925b668Rafael EspindolaMCSymbol *FrameEmitterImpl::EmitFDE(MCStreamer &streamer,
135825f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                                    const MCSymbol &cieStart,
13599f270dadd429771dded5a8572da8c74513771c15Rafael Espindola                                    const MCDwarfFrameInfo &frame) {
1360bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola  MCContext &context = streamer.getContext();
1361bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola  MCSymbol *fdeStart = context.CreateTempSymbol();
1362bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola  MCSymbol *fdeEnd = context.CreateTempSymbol();
1363e76a33b9567d78a5744dc52fcec3a6056d6fb576Evan Cheng  const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
13642541c41f3e2af94585e14353a91f02facd65e415Bill Wendling  bool verboseAsm = streamer.isVerboseAsm();
136589b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
13665fbe5e783ee0c5ae27c17490a752d7e603e84ed2Evan Cheng  if (IsEH && frame.Function && !MOFI->isFunctionEHFrameSymbolPrivate()) {
13672541c41f3e2af94585e14353a91f02facd65e415Bill Wendling    MCSymbol *EHSym =
13682541c41f3e2af94585e14353a91f02facd65e415Bill Wendling      context.GetOrCreateSymbol(frame.Function->getName() + Twine(".eh"));
13698bca4106dfc2945723251db10e340183f3e372ddRafael Espindola    streamer.EmitEHSymAttributes(frame.Function, EHSym);
1370a8cfb87fa2176fae2d62785a0dd606e4143cefaeRafael Espindola    streamer.EmitLabel(EHSym);
1371a8cfb87fa2176fae2d62785a0dd606e4143cefaeRafael Espindola  }
1372a8cfb87fa2176fae2d62785a0dd606e4143cefaeRafael Espindola
137389b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  // Length
137489b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  const MCExpr *Length = MakeStartMinusEndExpr(streamer, *fdeStart, *fdeEnd, 0);
13752541c41f3e2af94585e14353a91f02facd65e415Bill Wendling  if (verboseAsm) streamer.AddComment("FDE Length");
13769266cc400ee2fc86abb24ab0819d9da280fb61c4Rafael Espindola  streamer.EmitAbsValue(Length, 4);
137789b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
137889b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  streamer.EmitLabel(fdeStart);
1379e3a0e987f3d4f07512cdb64b9034369f966cb448Rafael Espindola
138089b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  // CIE Pointer
1381e3a0e987f3d4f07512cdb64b9034369f966cb448Rafael Espindola  const MCAsmInfo &asmInfo = context.getAsmInfo();
13820d450dc65906a30beb56aeb1ee22b45b1cd4596cRafael Espindola  if (IsEH) {
13830d450dc65906a30beb56aeb1ee22b45b1cd4596cRafael Espindola    const MCExpr *offset = MakeStartMinusEndExpr(streamer, cieStart, *fdeStart,
13840d450dc65906a30beb56aeb1ee22b45b1cd4596cRafael Espindola                                                 0);
13852541c41f3e2af94585e14353a91f02facd65e415Bill Wendling    if (verboseAsm) streamer.AddComment("FDE CIE Offset");
13860d450dc65906a30beb56aeb1ee22b45b1cd4596cRafael Espindola    streamer.EmitAbsValue(offset, 4);
13872241e51406f7bae369d6103cf3464e70f74c4af9Rafael Espindola  } else if (!asmInfo.doesDwarfUseRelocationsAcrossSections()) {
1388e3a0e987f3d4f07512cdb64b9034369f966cb448Rafael Espindola    const MCExpr *offset = MakeStartMinusEndExpr(streamer, *SectionStart,
1389e3a0e987f3d4f07512cdb64b9034369f966cb448Rafael Espindola                                                 cieStart, 0);
1390e3a0e987f3d4f07512cdb64b9034369f966cb448Rafael Espindola    streamer.EmitAbsValue(offset, 4);
13910d450dc65906a30beb56aeb1ee22b45b1cd4596cRafael Espindola  } else {
13920d450dc65906a30beb56aeb1ee22b45b1cd4596cRafael Espindola    streamer.EmitSymbolValue(&cieStart, 4);
13930d450dc65906a30beb56aeb1ee22b45b1cd4596cRafael Espindola  }
13942541c41f3e2af94585e14353a91f02facd65e415Bill Wendling
139589b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  // PC Begin
13960c5602de8c0dfa716f54306a1c540b98ab803584Nick Lewycky  unsigned PCEncoding = IsEH ? MOFI->getFDEEncoding(UsingCFI)
13970c5602de8c0dfa716f54306a1c540b98ab803584Nick Lewycky                             : (unsigned)dwarf::DW_EH_PE_absptr;
13980c5602de8c0dfa716f54306a1c540b98ab803584Nick Lewycky  unsigned PCSize = getSizeForEncoding(streamer, PCEncoding);
13990c5602de8c0dfa716f54306a1c540b98ab803584Nick Lewycky  EmitSymbol(streamer, *frame.Begin, PCEncoding, "FDE initial location");
140089b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
140189b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  // PC Range
140289b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  const MCExpr *Range = MakeStartMinusEndExpr(streamer, *frame.Begin,
140389b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola                                              *frame.End, 0);
14042541c41f3e2af94585e14353a91f02facd65e415Bill Wendling  if (verboseAsm) streamer.AddComment("FDE address range");
14050c5602de8c0dfa716f54306a1c540b98ab803584Nick Lewycky  streamer.EmitAbsValue(Range, PCSize);
140689b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
140740a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola  if (IsEH) {
140840a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    // Augmentation Data Length
140940a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    unsigned augmentationLength = 0;
14101f6c87554a0d78103b95816fd2c1ce392b366bcfRafael Espindola
141140a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    if (frame.Lsda)
141240a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola      augmentationLength += getSizeForEncoding(streamer, frame.LsdaEncoding);
14131f6c87554a0d78103b95816fd2c1ce392b366bcfRafael Espindola
14142541c41f3e2af94585e14353a91f02facd65e415Bill Wendling    if (verboseAsm) streamer.AddComment("Augmentation size");
141540a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    streamer.EmitULEB128IntValue(augmentationLength);
141689b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
141740a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    // Augmentation Data
141840a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    if (frame.Lsda)
14192541c41f3e2af94585e14353a91f02facd65e415Bill Wendling      EmitSymbol(streamer, *frame.Lsda, frame.LsdaEncoding,
14202541c41f3e2af94585e14353a91f02facd65e415Bill Wendling                 "Language Specific Data Area");
142140a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola  }
14224892dff07e7d99eb37aee868c4f2c91e9c7eba14Rafael Espindola
142389b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  // Call Frame Instructions
142489b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
1425b40a71fda188f8ca564e606ac2cb051a44ada311Rafael Espindola  EmitCFIInstructions(streamer, frame.Instructions, frame.Begin);
14265bba08425374ca36fe5fbc7423ce1a09858e4097Rafael Espindola
142789b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  // Padding
14280c5602de8c0dfa716f54306a1c540b98ab803584Nick Lewycky  streamer.EmitValueToAlignment(PCSize);
1429dfe125cc9cfd524575973daa297c9aad10470390Rafael Espindola
1430dfe125cc9cfd524575973daa297c9aad10470390Rafael Espindola  return fdeEnd;
143189b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola}
143289b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
143319282365147f498a60463d250dbed2f8e1b81861Benjamin Kramernamespace {
143419282365147f498a60463d250dbed2f8e1b81861Benjamin Kramer  struct CIEKey {
143516d7d437e03ce87fdaef7971919302920d54a966Rafael Espindola    static const CIEKey getEmptyKey() { return CIEKey(0, 0, -1, false); }
143616d7d437e03ce87fdaef7971919302920d54a966Rafael Espindola    static const CIEKey getTombstoneKey() { return CIEKey(0, -1, 0, false); }
143719282365147f498a60463d250dbed2f8e1b81861Benjamin Kramer
143819282365147f498a60463d250dbed2f8e1b81861Benjamin Kramer    CIEKey(const MCSymbol* Personality_, unsigned PersonalityEncoding_,
143916d7d437e03ce87fdaef7971919302920d54a966Rafael Espindola           unsigned LsdaEncoding_, bool IsSignalFrame_) :
144016d7d437e03ce87fdaef7971919302920d54a966Rafael Espindola      Personality(Personality_), PersonalityEncoding(PersonalityEncoding_),
144116d7d437e03ce87fdaef7971919302920d54a966Rafael Espindola      LsdaEncoding(LsdaEncoding_), IsSignalFrame(IsSignalFrame_) {
144219282365147f498a60463d250dbed2f8e1b81861Benjamin Kramer    }
144319282365147f498a60463d250dbed2f8e1b81861Benjamin Kramer    const MCSymbol* Personality;
144419282365147f498a60463d250dbed2f8e1b81861Benjamin Kramer    unsigned PersonalityEncoding;
144519282365147f498a60463d250dbed2f8e1b81861Benjamin Kramer    unsigned LsdaEncoding;
144616d7d437e03ce87fdaef7971919302920d54a966Rafael Espindola    bool IsSignalFrame;
144719282365147f498a60463d250dbed2f8e1b81861Benjamin Kramer  };
144819282365147f498a60463d250dbed2f8e1b81861Benjamin Kramer}
1449bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola
1450bdc3167c086dd4358e24692075db5e7784140843Rafael Espindolanamespace llvm {
1451bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola  template <>
1452bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola  struct DenseMapInfo<CIEKey> {
1453bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola    static CIEKey getEmptyKey() {
145419282365147f498a60463d250dbed2f8e1b81861Benjamin Kramer      return CIEKey::getEmptyKey();
1455bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola    }
1456bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola    static CIEKey getTombstoneKey() {
145719282365147f498a60463d250dbed2f8e1b81861Benjamin Kramer      return CIEKey::getTombstoneKey();
1458bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola    }
1459bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola    static unsigned getHashValue(const CIEKey &Key) {
14607484920cf5d95b21fa750229f8ce0d1624c918ecBenjamin Kramer      return static_cast<unsigned>(hash_combine(Key.Personality,
14617484920cf5d95b21fa750229f8ce0d1624c918ecBenjamin Kramer                                                Key.PersonalityEncoding,
14627484920cf5d95b21fa750229f8ce0d1624c918ecBenjamin Kramer                                                Key.LsdaEncoding,
14637484920cf5d95b21fa750229f8ce0d1624c918ecBenjamin Kramer                                                Key.IsSignalFrame));
1464bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola    }
1465bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola    static bool isEqual(const CIEKey &LHS,
1466bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola                        const CIEKey &RHS) {
1467bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola      return LHS.Personality == RHS.Personality &&
1468bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola        LHS.PersonalityEncoding == RHS.PersonalityEncoding &&
146916d7d437e03ce87fdaef7971919302920d54a966Rafael Espindola        LHS.LsdaEncoding == RHS.LsdaEncoding &&
147016d7d437e03ce87fdaef7971919302920d54a966Rafael Espindola        LHS.IsSignalFrame == RHS.IsSignalFrame;
1471bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola    }
1472bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola  };
1473bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola}
1474bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola
1475a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendlingvoid MCDwarfFrameEmitter::Emit(MCStreamer &Streamer,
1476a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling                               bool UsingCFI,
1477a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling                               bool IsEH) {
1478a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling  MCContext &Context = Streamer.getContext();
1479e76a33b9567d78a5744dc52fcec3a6056d6fb576Evan Cheng  MCObjectFileInfo *MOFI =
1480e76a33b9567d78a5744dc52fcec3a6056d6fb576Evan Cheng    const_cast<MCObjectFileInfo*>(Context.getObjectFileInfo());
14811424c3c3e696868775ac5bfc57d27a0ac3bc6c63Bill Wendling  FrameEmitterImpl Emitter(UsingCFI, IsEH);
1482d8ab8e9707e6170218ce82516d15b36f54b4bc1cBill Wendling  ArrayRef<MCDwarfFrameInfo> FrameArray = Streamer.getFrameInfos();
14831424c3c3e696868775ac5bfc57d27a0ac3bc6c63Bill Wendling
1484d8ab8e9707e6170218ce82516d15b36f54b4bc1cBill Wendling  // Emit the compact unwind info if available.
14857a13b72bbbadaa9274f6a58774970b9b95706330Bill Wendling  if (IsEH && MOFI->getCompactUnwindSection())
14861424c3c3e696868775ac5bfc57d27a0ac3bc6c63Bill Wendling    for (unsigned i = 0, n = Streamer.getNumFrameInfos(); i < n; ++i) {
14871424c3c3e696868775ac5bfc57d27a0ac3bc6c63Bill Wendling      const MCDwarfFrameInfo &Frame = Streamer.getFrameInfo(i);
148818014d4e4c6722aec4fa387d995b618f5d3111cdBill Wendling      Emitter.EmitCompactUnwind(Streamer, Frame);
14891424c3c3e696868775ac5bfc57d27a0ac3bc6c63Bill Wendling    }
14901424c3c3e696868775ac5bfc57d27a0ac3bc6c63Bill Wendling
1491e76a33b9567d78a5744dc52fcec3a6056d6fb576Evan Cheng  const MCSection &Section = IsEH ? *MOFI->getEHFrameSection() :
1492e76a33b9567d78a5744dc52fcec3a6056d6fb576Evan Cheng                                    *MOFI->getDwarfFrameSection();
1493a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling  Streamer.SwitchSection(&Section);
1494a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling  MCSymbol *SectionStart = Context.CreateTempSymbol();
1495a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling  Streamer.EmitLabel(SectionStart);
14961424c3c3e696868775ac5bfc57d27a0ac3bc6c63Bill Wendling  Emitter.setSectionStart(SectionStart);
1497a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling
1498a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling  MCSymbol *FDEEnd = NULL;
1499bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola  DenseMap<CIEKey, const MCSymbol*> CIEStarts;
1500d7c8ccae8e48dce3ab7c3e9b4d8a309998c47961Rafael Espindola
150140a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola  const MCSymbol *DummyDebugKey = NULL;
15021424c3c3e696868775ac5bfc57d27a0ac3bc6c63Bill Wendling  for (unsigned i = 0, n = FrameArray.size(); i < n; ++i) {
15031424c3c3e696868775ac5bfc57d27a0ac3bc6c63Bill Wendling    const MCDwarfFrameInfo &Frame = FrameArray[i];
1504a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling    CIEKey Key(Frame.Personality, Frame.PersonalityEncoding,
150516d7d437e03ce87fdaef7971919302920d54a966Rafael Espindola               Frame.LsdaEncoding, Frame.IsSignalFrame);
1506a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling    const MCSymbol *&CIEStart = IsEH ? CIEStarts[Key] : DummyDebugKey;
1507a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling    if (!CIEStart)
1508a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling      CIEStart = &Emitter.EmitCIE(Streamer, Frame.Personality,
1509a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling                                  Frame.PersonalityEncoding, Frame.Lsda,
151016d7d437e03ce87fdaef7971919302920d54a966Rafael Espindola                                  Frame.IsSignalFrame,
1511a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling                                  Frame.LsdaEncoding);
1512e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling
1513a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling    FDEEnd = Emitter.EmitFDE(Streamer, *CIEStart, Frame);
1514e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling
1515dfe125cc9cfd524575973daa297c9aad10470390Rafael Espindola    if (i != n - 1)
1516a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling      Streamer.EmitLabel(FDEEnd);
1517dfe125cc9cfd524575973daa297c9aad10470390Rafael Espindola  }
1518d7c8ccae8e48dce3ab7c3e9b4d8a309998c47961Rafael Espindola
15191be0e271a07925b928ba89848934f1ea6f1854e2Evan Cheng  Streamer.EmitValueToAlignment(Context.getAsmInfo().getPointerSize());
1520a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling  if (FDEEnd)
1521a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling    Streamer.EmitLabel(FDEEnd);
152289b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola}
1523245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola
1524245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindolavoid MCDwarfFrameEmitter::EmitAdvanceLoc(MCStreamer &Streamer,
1525245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola                                         uint64_t AddrDelta) {
1526245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola  SmallString<256> Tmp;
1527245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola  raw_svector_ostream OS(Tmp);
15284eafe109459eb115f13f1d19c5ff3cb3678e8c7aRafael Espindola  MCDwarfFrameEmitter::EncodeAdvanceLoc(AddrDelta, OS);
152968ca56285f9b6e82eb16ff8ea02a301f2c489faeEric Christopher  Streamer.EmitBytes(OS.str());
1530245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola}
1531245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola
1532245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindolavoid MCDwarfFrameEmitter::EncodeAdvanceLoc(uint64_t AddrDelta,
15334eafe109459eb115f13f1d19c5ff3cb3678e8c7aRafael Espindola                                           raw_ostream &OS) {
1534245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola  // FIXME: Assumes the code alignment factor is 1.
15353b78cdc57ae25ff021fd67171a7c82d533477a19Rafael Espindola  if (AddrDelta == 0) {
15364eafe109459eb115f13f1d19c5ff3cb3678e8c7aRafael Espindola  } else if (isUIntN(6, AddrDelta)) {
1537245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola    uint8_t Opcode = dwarf::DW_CFA_advance_loc | AddrDelta;
1538245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola    OS << Opcode;
15394eafe109459eb115f13f1d19c5ff3cb3678e8c7aRafael Espindola  } else if (isUInt<8>(AddrDelta)) {
1540245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola    OS << uint8_t(dwarf::DW_CFA_advance_loc1);
1541245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola    OS << uint8_t(AddrDelta);
15424eafe109459eb115f13f1d19c5ff3cb3678e8c7aRafael Espindola  } else if (isUInt<16>(AddrDelta)) {
1543a7e450574c45209ca0b05ff715f9e7dddcbd936cRafael Espindola    // FIXME: check what is the correct behavior on a big endian machine.
1544245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola    OS << uint8_t(dwarf::DW_CFA_advance_loc2);
1545a7e450574c45209ca0b05ff715f9e7dddcbd936cRafael Espindola    OS << uint8_t( AddrDelta       & 0xff);
1546a7e450574c45209ca0b05ff715f9e7dddcbd936cRafael Espindola    OS << uint8_t((AddrDelta >> 8) & 0xff);
1547245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola  } else {
1548a7e450574c45209ca0b05ff715f9e7dddcbd936cRafael Espindola    // FIXME: check what is the correct behavior on a big endian machine.
1549245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola    assert(isUInt<32>(AddrDelta));
1550245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola    OS << uint8_t(dwarf::DW_CFA_advance_loc4);
1551a7e450574c45209ca0b05ff715f9e7dddcbd936cRafael Espindola    OS << uint8_t( AddrDelta        & 0xff);
1552a7e450574c45209ca0b05ff715f9e7dddcbd936cRafael Espindola    OS << uint8_t((AddrDelta >> 8)  & 0xff);
1553a7e450574c45209ca0b05ff715f9e7dddcbd936cRafael Espindola    OS << uint8_t((AddrDelta >> 16) & 0xff);
1554a7e450574c45209ca0b05ff715f9e7dddcbd936cRafael Espindola    OS << uint8_t((AddrDelta >> 24) & 0xff);
1555a7e450574c45209ca0b05ff715f9e7dddcbd936cRafael Espindola
1556245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola  }
1557245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola}
1558