MCDwarf.cpp revision 75c9b9384f50e9387f24dd7ce6af403cbda6d19a
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.
104c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  LineSection->addLineEntry(LineEntry);
105c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby}
106c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
107c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby//
108c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// This helper routine returns an expression of End - Start + IntVal .
1092684d9e3c702b2ef9fd430155d94671d12fa994fJim Grosbach//
1105fad7a99c00980fc30ce16fb51ce09dc435c40adRafael Espindolastatic inline const MCExpr *MakeStartMinusEndExpr(const MCStreamer &MCOS,
1115fad7a99c00980fc30ce16fb51ce09dc435c40adRafael Espindola                                                  const MCSymbol &Start,
1125fad7a99c00980fc30ce16fb51ce09dc435c40adRafael Espindola                                                  const MCSymbol &End,
1135fad7a99c00980fc30ce16fb51ce09dc435c40adRafael Espindola                                                  int IntVal) {
114c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
115c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  const MCExpr *Res =
1165fad7a99c00980fc30ce16fb51ce09dc435c40adRafael Espindola    MCSymbolRefExpr::Create(&End, Variant, MCOS.getContext());
117c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  const MCExpr *RHS =
1185fad7a99c00980fc30ce16fb51ce09dc435c40adRafael Espindola    MCSymbolRefExpr::Create(&Start, Variant, MCOS.getContext());
119c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  const MCExpr *Res1 =
1205fad7a99c00980fc30ce16fb51ce09dc435c40adRafael Espindola    MCBinaryExpr::Create(MCBinaryExpr::Sub, Res, RHS, MCOS.getContext());
121c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  const MCExpr *Res2 =
1225fad7a99c00980fc30ce16fb51ce09dc435c40adRafael Espindola    MCConstantExpr::Create(IntVal, MCOS.getContext());
123c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  const MCExpr *Res3 =
1245fad7a99c00980fc30ce16fb51ce09dc435c40adRafael Espindola    MCBinaryExpr::Create(MCBinaryExpr::Sub, Res1, Res2, MCOS.getContext());
125c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  return Res3;
126c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby}
127c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
128c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby//
129c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// This emits the Dwarf line table for the specified section from the entries
130c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// in the LineSection.
131c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby//
132195a0ce484cd12a5adae9184188f6d0fb52b84c0Rafael Espindolastatic inline void EmitDwarfLineTable(MCStreamer *MCOS,
133c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby                                      const MCSection *Section,
13489b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola                                      const MCLineSection *LineSection) {
135c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  unsigned FileNum = 1;
136c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  unsigned LastLine = 1;
137c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  unsigned Column = 0;
138c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
139c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  unsigned Isa = 0;
140c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCSymbol *LastLabel = NULL;
141c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
142c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Loop through each MCLineEntry and encode the dwarf line number table.
14317fd7bda5ac08f873c063c64e1456f8960a0c765Rafael Espindola  for (MCLineSection::const_iterator
144c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby         it = LineSection->getMCLineEntries()->begin(),
145c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby         ie = LineSection->getMCLineEntries()->end(); it != ie; ++it) {
146c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
147c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    if (FileNum != it->getFileNum()) {
148c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      FileNum = it->getFileNum();
149c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      MCOS->EmitIntValue(dwarf::DW_LNS_set_file, 1);
1503ff57094a7d176a759ddb1e1668489d89064f56cRafael Espindola      MCOS->EmitULEB128IntValue(FileNum);
151c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    }
152c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    if (Column != it->getColumn()) {
153c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      Column = it->getColumn();
154c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      MCOS->EmitIntValue(dwarf::DW_LNS_set_column, 1);
1553ff57094a7d176a759ddb1e1668489d89064f56cRafael Espindola      MCOS->EmitULEB128IntValue(Column);
156c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    }
157c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    if (Isa != it->getIsa()) {
158c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      Isa = it->getIsa();
159c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      MCOS->EmitIntValue(dwarf::DW_LNS_set_isa, 1);
1603ff57094a7d176a759ddb1e1668489d89064f56cRafael Espindola      MCOS->EmitULEB128IntValue(Isa);
161c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    }
162c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    if ((it->getFlags() ^ Flags) & DWARF2_FLAG_IS_STMT) {
163c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      Flags = it->getFlags();
164c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      MCOS->EmitIntValue(dwarf::DW_LNS_negate_stmt, 1);
165c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    }
166c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    if (it->getFlags() & DWARF2_FLAG_BASIC_BLOCK)
167c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      MCOS->EmitIntValue(dwarf::DW_LNS_set_basic_block, 1);
168c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    if (it->getFlags() & DWARF2_FLAG_PROLOGUE_END)
169c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      MCOS->EmitIntValue(dwarf::DW_LNS_set_prologue_end, 1);
170c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    if (it->getFlags() & DWARF2_FLAG_EPILOGUE_BEGIN)
171c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      MCOS->EmitIntValue(dwarf::DW_LNS_set_epilogue_begin, 1);
172c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
17364185cc6090f695c4f97c51cf2adc731f56d1a20Rafael Espindola    int64_t LineDelta = static_cast<int64_t>(it->getLine()) - LastLine;
174c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    MCSymbol *Label = it->getLabel();
175c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
176c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    // At this point we want to emit/create the sequence to encode the delta in
177c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    // line numbers and the increment of the address from the previous Label
178c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    // and the current Label.
1791be0e271a07925b928ba89848934f1ea6f1854e2Evan Cheng    const MCAsmInfo &asmInfo = MCOS->getContext().getAsmInfo();
180672b93a3324cc1da6d374eed4c75c050a9cad7beEvan Cheng    MCOS->EmitDwarfAdvanceLineAddr(LineDelta, LastLabel, Label,
181672b93a3324cc1da6d374eed4c75c050a9cad7beEvan Cheng                                   asmInfo.getPointerSize());
182c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
183c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    LastLine = it->getLine();
184c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    LastLabel = Label;
185c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  }
186c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
187c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Emit a DW_LNE_end_sequence for the end of the section.
188c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Using the pointer Section create a temporary label at the end of the
189c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // section and use that and the LastLabel to compute the address delta
190c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // and use INT64_MAX as the line delta which is the signal that this is
191c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // actually a DW_LNE_end_sequence.
192c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
193c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Switch to the section to be able to create a symbol at its end.
194c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->SwitchSection(Section);
19589b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
19689b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  MCContext &context = MCOS->getContext();
197c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Create a symbol at the end of the section.
19889b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  MCSymbol *SectionEnd = context.CreateTempSymbol();
199c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Set the value of the symbol, as we are at the end of the section.
200c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitLabel(SectionEnd);
201c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
202c8e41c591741b3da1077f7000274ad040bef8002Sylvestre Ledru  // Switch back the dwarf line section.
203e76a33b9567d78a5744dc52fcec3a6056d6fb576Evan Cheng  MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfLineSection());
204195a0ce484cd12a5adae9184188f6d0fb52b84c0Rafael Espindola
2051be0e271a07925b928ba89848934f1ea6f1854e2Evan Cheng  const MCAsmInfo &asmInfo = MCOS->getContext().getAsmInfo();
206672b93a3324cc1da6d374eed4c75c050a9cad7beEvan Cheng  MCOS->EmitDwarfAdvanceLineAddr(INT64_MAX, LastLabel, SectionEnd,
207672b93a3324cc1da6d374eed4c75c050a9cad7beEvan Cheng                                 asmInfo.getPointerSize());
208c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby}
209c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
210c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby//
211c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// This emits the Dwarf file and the line tables.
212c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby//
213489d67927172941bf59b9f4829ab8910814fea24Rafael Espindolaconst MCSymbol *MCDwarfFileTable::Emit(MCStreamer *MCOS) {
21489b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  MCContext &context = MCOS->getContext();
215c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Switch to the section where the table will be emitted into.
216e76a33b9567d78a5744dc52fcec3a6056d6fb576Evan Cheng  MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfLineSection());
217c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
218c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Create a symbol at the beginning of this section.
21989b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  MCSymbol *LineStartSym = context.CreateTempSymbol();
220c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Set the value of the symbol, as we are at the start of the section.
221c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitLabel(LineStartSym);
222c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
223c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Create a symbol for the end of the section (to be set when we get there).
22489b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  MCSymbol *LineEndSym = context.CreateTempSymbol();
225c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
226c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // The first 4 bytes is the total length of the information for this
227c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // compilation unit (not including these 4 bytes for the length).
2285fad7a99c00980fc30ce16fb51ce09dc435c40adRafael Espindola  MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *LineEndSym,4),
2290bbe0b440ee2cef47dcb7b281825eb70341c16ddRafael Espindola                     4);
230c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
231c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Next 2 bytes is the Version, which is Dwarf 2.
232c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(2, 2);
233c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
234c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Create a symbol for the end of the prologue (to be set when we get there).
23589b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  MCSymbol *ProEndSym = context.CreateTempSymbol(); // Lprologue_end
236c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
237c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Length of the prologue, is the next 4 bytes.  Which is the start of the
238c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // section to the end of the prologue.  Not including the 4 bytes for the
239c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // total length, the 2 bytes for the version, and these 4 bytes for the
240c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // length of the prologue.
2415fad7a99c00980fc30ce16fb51ce09dc435c40adRafael Espindola  MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *ProEndSym,
242c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby                                        (4 + 2 + 4)),
2435d4918dbd116b0b5e561c431b1ea527ee1b9302aRafael Espindola                  4, 0);
244c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
245c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Parameters of the state machine, are next.
246c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(DWARF2_LINE_MIN_INSN_LENGTH, 1);
247c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(DWARF2_LINE_DEFAULT_IS_STMT, 1);
248c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(DWARF2_LINE_BASE, 1);
249c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(DWARF2_LINE_RANGE, 1);
250c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(DWARF2_LINE_OPCODE_BASE, 1);
251c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
252c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Standard opcode lengths
253c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(0, 1); // length of DW_LNS_copy
254c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_pc
255c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_line
256c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_file
257c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_column
258c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(0, 1); // length of DW_LNS_negate_stmt
259c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_basic_block
260c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(0, 1); // length of DW_LNS_const_add_pc
261c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(1, 1); // length of DW_LNS_fixed_advance_pc
262c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_prologue_end
263c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_epilogue_begin
264c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(1, 1); // DW_LNS_set_isa
265c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
266c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Put out the directory and file tables.
267c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
268c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // First the directory table.
269c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  const std::vector<StringRef> &MCDwarfDirs =
27089b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola    context.getMCDwarfDirs();
271c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  for (unsigned i = 0; i < MCDwarfDirs.size(); i++) {
27268ca56285f9b6e82eb16ff8ea02a301f2c489faeEric Christopher    MCOS->EmitBytes(MCDwarfDirs[i]); // the DirectoryName
27368ca56285f9b6e82eb16ff8ea02a301f2c489faeEric Christopher    MCOS->EmitBytes(StringRef("\0", 1)); // the null term. of the string
274c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  }
275c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(0, 1); // Terminate the directory list
276c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
277c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Second the file table.
278c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  const std::vector<MCDwarfFile *> &MCDwarfFiles =
279c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    MCOS->getContext().getMCDwarfFiles();
280c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
28168ca56285f9b6e82eb16ff8ea02a301f2c489faeEric Christopher    MCOS->EmitBytes(MCDwarfFiles[i]->getName()); // FileName
28268ca56285f9b6e82eb16ff8ea02a301f2c489faeEric Christopher    MCOS->EmitBytes(StringRef("\0", 1)); // the null term. of the string
2833ff57094a7d176a759ddb1e1668489d89064f56cRafael Espindola    // the Directory num
2843ff57094a7d176a759ddb1e1668489d89064f56cRafael Espindola    MCOS->EmitULEB128IntValue(MCDwarfFiles[i]->getDirIndex());
285c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    MCOS->EmitIntValue(0, 1); // last modification timestamp (always 0)
286c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    MCOS->EmitIntValue(0, 1); // filesize (always 0)
287c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  }
288c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(0, 1); // Terminate the file list
289c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
290c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // This is the end of the prologue, so set the value of the symbol at the
291c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // end of the prologue (that was used in a previous expression).
292c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitLabel(ProEndSym);
293c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
294c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Put out the line tables.
29517fd7bda5ac08f873c063c64e1456f8960a0c765Rafael Espindola  const DenseMap<const MCSection *, MCLineSection *> &MCLineSections =
296c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    MCOS->getContext().getMCLineSections();
29717fd7bda5ac08f873c063c64e1456f8960a0c765Rafael Espindola  const std::vector<const MCSection *> &MCLineSectionOrder =
29817fd7bda5ac08f873c063c64e1456f8960a0c765Rafael Espindola    MCOS->getContext().getMCLineSectionOrder();
29917fd7bda5ac08f873c063c64e1456f8960a0c765Rafael Espindola  for (std::vector<const MCSection*>::const_iterator it =
300c3882164cbf794dbe21816a2946a31bc9e02a419Bill Wendling         MCLineSectionOrder.begin(), ie = MCLineSectionOrder.end(); it != ie;
30117fd7bda5ac08f873c063c64e1456f8960a0c765Rafael Espindola       ++it) {
30217fd7bda5ac08f873c063c64e1456f8960a0c765Rafael Espindola    const MCSection *Sec = *it;
30317fd7bda5ac08f873c063c64e1456f8960a0c765Rafael Espindola    const MCLineSection *Line = MCLineSections.lookup(Sec);
30489b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola    EmitDwarfLineTable(MCOS, Sec, Line);
305c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
306c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    // Now delete the MCLineSections that were created in MCLineEntry::Make()
307c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    // and used to emit the line table.
30817fd7bda5ac08f873c063c64e1456f8960a0c765Rafael Espindola    delete Line;
309c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  }
310c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
311767b1be3900bdc693aa0ad3e554ba034845f67f7Rafael Espindola  if (MCOS->getContext().getAsmInfo().getLinkerRequiresNonEmptyDwarfLines()
312767b1be3900bdc693aa0ad3e554ba034845f67f7Rafael Espindola      && MCLineSectionOrder.begin() == MCLineSectionOrder.end()) {
313a8de83c68aaa03cafa0e3618fb8bf030b48f6540Rafael Espindola    // The darwin9 linker has a bug (see PR8715). For for 32-bit architectures
3142684d9e3c702b2ef9fd430155d94671d12fa994fJim Grosbach    // it requires:
315a8de83c68aaa03cafa0e3618fb8bf030b48f6540Rafael Espindola    // total_length >= prologue_length + 10
316a8de83c68aaa03cafa0e3618fb8bf030b48f6540Rafael Espindola    // We are 4 bytes short, since we have total_length = 51 and
317a8de83c68aaa03cafa0e3618fb8bf030b48f6540Rafael Espindola    // prologue_length = 45
318a8de83c68aaa03cafa0e3618fb8bf030b48f6540Rafael Espindola
319a8de83c68aaa03cafa0e3618fb8bf030b48f6540Rafael Espindola    // The regular end_sequence should be sufficient.
320a8de83c68aaa03cafa0e3618fb8bf030b48f6540Rafael Espindola    MCDwarfLineAddr::Emit(MCOS, INT64_MAX, 0);
3215113cdbfff7df4c7a79a92e5aa971126254202c6Devang Patel  }
3225113cdbfff7df4c7a79a92e5aa971126254202c6Devang Patel
323c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // This is the end of the section, so set the value of the symbol at the end
324c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // of this section (that was used in a previous expression).
325c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitLabel(LineEndSym);
326489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola
327489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola  return LineStartSym;
328c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby}
329c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
330c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby/// Utility function to write the encoding to an object writer.
331c095793b4ab027181605c79c9808df12afe45d63Kevin Enderbyvoid MCDwarfLineAddr::Write(MCObjectWriter *OW, int64_t LineDelta,
332c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby                            uint64_t AddrDelta) {
333c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  SmallString<256> Tmp;
334c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  raw_svector_ostream OS(Tmp);
335c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OS);
336c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  OW->WriteBytes(OS.str());
337c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby}
338c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
339c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby/// Utility function to emit the encoding to a streamer.
340195a0ce484cd12a5adae9184188f6d0fb52b84c0Rafael Espindolavoid MCDwarfLineAddr::Emit(MCStreamer *MCOS, int64_t LineDelta,
341c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby                           uint64_t AddrDelta) {
342c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  SmallString<256> Tmp;
343c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  raw_svector_ostream OS(Tmp);
344c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OS);
34568ca56285f9b6e82eb16ff8ea02a301f2c489faeEric Christopher  MCOS->EmitBytes(OS.str());
346c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby}
347c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
348c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby/// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
349c095793b4ab027181605c79c9808df12afe45d63Kevin Enderbyvoid MCDwarfLineAddr::Encode(int64_t LineDelta, uint64_t AddrDelta,
350c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby                             raw_ostream &OS) {
351c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  uint64_t Temp, Opcode;
352c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  bool NeedCopy = false;
353c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
354c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Scale the address delta by the minimum instruction length.
355c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  AddrDelta = ScaleAddrDelta(AddrDelta);
356c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
357c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // A LineDelta of INT64_MAX is a signal that this is actually a
3582684d9e3c702b2ef9fd430155d94671d12fa994fJim Grosbach  // DW_LNE_end_sequence. We cannot use special opcodes here, since we want the
359c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // end_sequence to emit the matrix entry.
360c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  if (LineDelta == INT64_MAX) {
361c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    if (AddrDelta == MAX_SPECIAL_ADDR_DELTA)
362c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      OS << char(dwarf::DW_LNS_const_add_pc);
363c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    else {
364c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      OS << char(dwarf::DW_LNS_advance_pc);
3652d39a0e52df9ce050bd4e2de3a2ecca8fd9a87c3Jim Grosbach      encodeULEB128(AddrDelta, OS);
366c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    }
367c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    OS << char(dwarf::DW_LNS_extended_op);
368c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    OS << char(1);
369c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    OS << char(dwarf::DW_LNE_end_sequence);
370c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    return;
371c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  }
372c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
373c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Bias the line delta by the base.
374c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  Temp = LineDelta - DWARF2_LINE_BASE;
375c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
376c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // If the line increment is out of range of a special opcode, we must encode
377c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // it with DW_LNS_advance_line.
378c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  if (Temp >= DWARF2_LINE_RANGE) {
379c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    OS << char(dwarf::DW_LNS_advance_line);
3802d39a0e52df9ce050bd4e2de3a2ecca8fd9a87c3Jim Grosbach    encodeSLEB128(LineDelta, OS);
381c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
382c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    LineDelta = 0;
383c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    Temp = 0 - DWARF2_LINE_BASE;
384c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    NeedCopy = true;
385c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  }
386c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
387c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Use DW_LNS_copy instead of a "line +0, addr +0" special opcode.
388c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  if (LineDelta == 0 && AddrDelta == 0) {
389c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    OS << char(dwarf::DW_LNS_copy);
390c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    return;
391c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  }
392c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
393c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Bias the opcode by the special opcode base.
394c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  Temp += DWARF2_LINE_OPCODE_BASE;
395c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
396c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Avoid overflow when addr_delta is large.
397c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  if (AddrDelta < 256 + MAX_SPECIAL_ADDR_DELTA) {
398c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    // Try using a special opcode.
399c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    Opcode = Temp + AddrDelta * DWARF2_LINE_RANGE;
400c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    if (Opcode <= 255) {
401c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      OS << char(Opcode);
402c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      return;
403c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    }
404c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
405c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    // Try using DW_LNS_const_add_pc followed by special op.
406c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    Opcode = Temp + (AddrDelta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
407c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    if (Opcode <= 255) {
408c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      OS << char(dwarf::DW_LNS_const_add_pc);
409c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      OS << char(Opcode);
410c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      return;
411c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    }
412c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  }
413c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
414c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Otherwise use DW_LNS_advance_pc.
415c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  OS << char(dwarf::DW_LNS_advance_pc);
4162d39a0e52df9ce050bd4e2de3a2ecca8fd9a87c3Jim Grosbach  encodeULEB128(AddrDelta, OS);
417c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
418c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  if (NeedCopy)
419c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    OS << char(dwarf::DW_LNS_copy);
420c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  else
421c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    OS << char(Temp);
422c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby}
423c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
4247cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderbyvoid MCDwarfFile::print(raw_ostream &OS) const {
4257cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby  OS << '"' << getName() << '"';
4267cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby}
4277cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby
428286c4dc355b8be6806081b23c3097485821c7642Manman Ren#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
4297cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderbyvoid MCDwarfFile::dump() const {
4307cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby  print(dbgs());
4317cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby}
432cc77eece74c8db09acc2af425e7e6c88a5bb30d1Manman Ren#endif
433c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
43494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby// Utility function to write a tuple for .debug_abbrev.
43594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderbystatic void EmitAbbrev(MCStreamer *MCOS, uint64_t Name, uint64_t Form) {
43694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitULEB128IntValue(Name);
43794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitULEB128IntValue(Form);
43894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby}
43994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
44094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby// When generating dwarf for assembly source files this emits
44194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby// the data for .debug_abbrev section which contains three DIEs.
44294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderbystatic void EmitGenDwarfAbbrev(MCStreamer *MCOS) {
44394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCContext &context = MCOS->getContext();
44494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
44594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
44694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // DW_TAG_compile_unit DIE abbrev (1).
44794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitULEB128IntValue(1);
44894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitULEB128IntValue(dwarf::DW_TAG_compile_unit);
44994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
45094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitAbbrev(MCOS, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4);
45194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
45294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitAbbrev(MCOS, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr);
45394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
45494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitAbbrev(MCOS, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string);
45594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
45694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  if (!DwarfDebugFlags.empty())
45794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    EmitAbbrev(MCOS, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string);
45894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitAbbrev(MCOS, dwarf::DW_AT_producer, dwarf::DW_FORM_string);
45994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitAbbrev(MCOS, dwarf::DW_AT_language, dwarf::DW_FORM_data2);
46094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitAbbrev(MCOS, 0, 0);
46194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
46238fdb7d9fc40e9f29c3156b6625cac8d91d562e1Kevin Enderby  // DW_TAG_label DIE abbrev (2).
46394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitULEB128IntValue(2);
46438fdb7d9fc40e9f29c3156b6625cac8d91d562e1Kevin Enderby  MCOS->EmitULEB128IntValue(dwarf::DW_TAG_label);
46594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
46694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
46794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitAbbrev(MCOS, dwarf::DW_AT_decl_file, dwarf::DW_FORM_data4);
46894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitAbbrev(MCOS, dwarf::DW_AT_decl_line, dwarf::DW_FORM_data4);
46994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
47094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitAbbrev(MCOS, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag);
47194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitAbbrev(MCOS, 0, 0);
47294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
47394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // DW_TAG_unspecified_parameters DIE abbrev (3).
47494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitULEB128IntValue(3);
47594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitULEB128IntValue(dwarf::DW_TAG_unspecified_parameters);
47694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(dwarf::DW_CHILDREN_no, 1);
47794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitAbbrev(MCOS, 0, 0);
47894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
47994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Terminate the abbreviations for this compilation unit.
48094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(0, 1);
48194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby}
48294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
48394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby// When generating dwarf for assembly source files this emits the data for
48494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby// .debug_aranges section.  Which contains a header and a table of pairs of
48594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby// PointerSize'ed values for the address and size of section(s) with line table
48694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby// entries (just the default .text in our case) and a terminating pair of zeros.
48715ab115df57949fa55d92ffdfba491f7d02ed60fAlexey Samsonovstatic void EmitGenDwarfAranges(MCStreamer *MCOS,
48815ab115df57949fa55d92ffdfba491f7d02ed60fAlexey Samsonov                                const MCSymbol *InfoSectionSymbol) {
48994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCContext &context = MCOS->getContext();
49094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
49194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Create a symbol at the end of the section that we are creating the dwarf
49294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // debugging info to use later in here as part of the expression to calculate
49394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // the size of the section for the table.
49494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->SwitchSection(context.getGenDwarfSection());
49594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCSymbol *SectionEndSym = context.CreateTempSymbol();
49694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitLabel(SectionEndSym);
49794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  context.setGenDwarfSectionEndSym(SectionEndSym);
49894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
49994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
50094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
50194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // This will be the length of the .debug_aranges section, first account for
50294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // the size of each item in the header (see below where we emit these items).
50394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  int Length = 4 + 2 + 4 + 1 + 1;
50494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
50594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Figure the padding after the header before the table of address and size
50694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // pairs who's values are PointerSize'ed.
50794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  const MCAsmInfo &asmInfo = context.getAsmInfo();
50894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  int AddrSize = asmInfo.getPointerSize();
50994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  int Pad = 2 * AddrSize - (Length & (2 * AddrSize - 1));
51094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  if (Pad == 2 * AddrSize)
51194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    Pad = 0;
51294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  Length += Pad;
51394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
51494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Add the size of the pair of PointerSize'ed values for the address and size
51594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // of the one default .text section we have in the table.
51694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  Length += 2 * AddrSize;
51794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // And the pair of terminating zeros.
51894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  Length += 2 * AddrSize;
51994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
52094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
52194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Emit the header for this section.
52294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // The 4 byte length not including the 4 byte value for the length.
52394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(Length - 4, 4);
52494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // The 2 byte version, which is 2.
52594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(2, 2);
52694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // The 4 byte offset to the compile unit in the .debug_info from the start
52715ab115df57949fa55d92ffdfba491f7d02ed60fAlexey Samsonov  // of the .debug_info.
52815ab115df57949fa55d92ffdfba491f7d02ed60fAlexey Samsonov  if (InfoSectionSymbol)
52915ab115df57949fa55d92ffdfba491f7d02ed60fAlexey Samsonov    MCOS->EmitSymbolValue(InfoSectionSymbol, 4);
53015ab115df57949fa55d92ffdfba491f7d02ed60fAlexey Samsonov  else
53115ab115df57949fa55d92ffdfba491f7d02ed60fAlexey Samsonov    MCOS->EmitIntValue(0, 4);
53294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // The 1 byte size of an address.
53394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(AddrSize, 1);
53494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // The 1 byte size of a segment descriptor, we use a value of zero.
53594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(0, 1);
53694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Align the header with the padding if needed, before we put out the table.
53794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  for(int i = 0; i < Pad; i++)
53894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    MCOS->EmitIntValue(0, 1);
53994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
54094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Now emit the table of pairs of PointerSize'ed values for the section(s)
54194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // address and size, in our case just the one default .text section.
54294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  const MCExpr *Addr = MCSymbolRefExpr::Create(
54394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    context.getGenDwarfSectionStartSym(), MCSymbolRefExpr::VK_None, context);
54494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  const MCExpr *Size = MakeStartMinusEndExpr(*MCOS,
54594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    *context.getGenDwarfSectionStartSym(), *SectionEndSym, 0);
54694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitAbsValue(Addr, AddrSize);
54794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitAbsValue(Size, AddrSize);
54894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
54994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // And finally the pair of terminating zeros.
55094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(0, AddrSize);
55194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(0, AddrSize);
55294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby}
55394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
55494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby// When generating dwarf for assembly source files this emits the data for
55594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby// .debug_info section which contains three parts.  The header, the compile_unit
55638fdb7d9fc40e9f29c3156b6625cac8d91d562e1Kevin Enderby// DIE and a list of label DIEs.
557489d67927172941bf59b9f4829ab8910814fea24Rafael Espindolastatic void EmitGenDwarfInfo(MCStreamer *MCOS,
558489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola                             const MCSymbol *AbbrevSectionSymbol,
559489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola                             const MCSymbol *LineSectionSymbol) {
56094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCContext &context = MCOS->getContext();
56194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
5622684d9e3c702b2ef9fd430155d94671d12fa994fJim Grosbach  MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
56394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
56494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Create a symbol at the start and end of this section used in here for the
56594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // expression to calculate the length in the header.
56694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCSymbol *InfoStart = context.CreateTempSymbol();
56794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitLabel(InfoStart);
56894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCSymbol *InfoEnd = context.CreateTempSymbol();
56994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
57094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // First part: the header.
57194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
57294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // The 4 byte total length of the information for this compilation unit, not
57394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // including these 4 bytes.
57494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  const MCExpr *Length = MakeStartMinusEndExpr(*MCOS, *InfoStart, *InfoEnd, 4);
57594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitAbsValue(Length, 4);
57694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
57794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // The 2 byte DWARF version, which is 2.
57894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(2, 2);
57994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
58094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // The 4 byte offset to the debug abbrevs from the start of the .debug_abbrev,
58194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // it is at the start of that section so this is zero.
582489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola  if (AbbrevSectionSymbol) {
583489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola    MCOS->EmitSymbolValue(AbbrevSectionSymbol, 4);
584489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola  } else {
585489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola    MCOS->EmitIntValue(0, 4);
586489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola  }
58794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
58894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  const MCAsmInfo &asmInfo = context.getAsmInfo();
58994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  int AddrSize = asmInfo.getPointerSize();
59094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // The 1 byte size of an address.
59194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(AddrSize, 1);
59294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
59394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Second part: the compile_unit DIE.
59494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
59594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // The DW_TAG_compile_unit DIE abbrev (1).
59694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitULEB128IntValue(1);
59794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
59894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // DW_AT_stmt_list, a 4 byte offset from the start of the .debug_line section,
59994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // which is at the start of that section so this is zero.
600489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola  if (LineSectionSymbol) {
601489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola    MCOS->EmitSymbolValue(LineSectionSymbol, 4);
602489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola  } else {
603489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola    MCOS->EmitIntValue(0, 4);
604489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola  }
60594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
60694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // AT_low_pc, the first address of the default .text section.
60794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  const MCExpr *Start = MCSymbolRefExpr::Create(
60894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    context.getGenDwarfSectionStartSym(), MCSymbolRefExpr::VK_None, context);
60994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitAbsValue(Start, AddrSize);
61094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
61194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // AT_high_pc, the last address of the default .text section.
61294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  const MCExpr *End = MCSymbolRefExpr::Create(
61394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    context.getGenDwarfSectionEndSym(), MCSymbolRefExpr::VK_None, context);
61494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitAbsValue(End, AddrSize);
61594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
61694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // AT_name, the name of the source file.  Reconstruct from the first directory
61794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // and file table entries.
61894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  const std::vector<StringRef> &MCDwarfDirs =
61994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    context.getMCDwarfDirs();
62094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  if (MCDwarfDirs.size() > 0) {
62168ca56285f9b6e82eb16ff8ea02a301f2c489faeEric Christopher    MCOS->EmitBytes(MCDwarfDirs[0]);
62268ca56285f9b6e82eb16ff8ea02a301f2c489faeEric Christopher    MCOS->EmitBytes("/");
62394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  }
62494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  const std::vector<MCDwarfFile *> &MCDwarfFiles =
62594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    MCOS->getContext().getMCDwarfFiles();
62668ca56285f9b6e82eb16ff8ea02a301f2c489faeEric Christopher  MCOS->EmitBytes(MCDwarfFiles[1]->getName());
62794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
62894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
62994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // AT_comp_dir, the working directory the assembly was done in.
63068ca56285f9b6e82eb16ff8ea02a301f2c489faeEric Christopher  MCOS->EmitBytes(context.getCompilationDir());
63194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
63294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
63394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // AT_APPLE_flags, the command line arguments of the assembler tool.
63494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
63594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  if (!DwarfDebugFlags.empty()){
63668ca56285f9b6e82eb16ff8ea02a301f2c489faeEric Christopher    MCOS->EmitBytes(DwarfDebugFlags);
63794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
63894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  }
63994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
64094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // AT_producer, the version of the assembler tool.
64175c9b9384f50e9387f24dd7ce6af403cbda6d19aKevin Enderby  StringRef DwarfDebugProducer = context.getDwarfDebugProducer();
64275c9b9384f50e9387f24dd7ce6af403cbda6d19aKevin Enderby  if (!DwarfDebugProducer.empty()){
64375c9b9384f50e9387f24dd7ce6af403cbda6d19aKevin Enderby    MCOS->EmitBytes(DwarfDebugProducer);
64475c9b9384f50e9387f24dd7ce6af403cbda6d19aKevin Enderby  }
64575c9b9384f50e9387f24dd7ce6af403cbda6d19aKevin Enderby  else {
64675c9b9384f50e9387f24dd7ce6af403cbda6d19aKevin Enderby    MCOS->EmitBytes(StringRef("llvm-mc (based on LLVM "));
64775c9b9384f50e9387f24dd7ce6af403cbda6d19aKevin Enderby    MCOS->EmitBytes(StringRef(PACKAGE_VERSION));
64875c9b9384f50e9387f24dd7ce6af403cbda6d19aKevin Enderby    MCOS->EmitBytes(StringRef(")"));
64975c9b9384f50e9387f24dd7ce6af403cbda6d19aKevin Enderby  }
65094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
65194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
65294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // AT_language, a 4 byte value.  We use DW_LANG_Mips_Assembler as the dwarf2
65394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // draft has no standard code for assembler.
65494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(dwarf::DW_LANG_Mips_Assembler, 2);
65594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
65638fdb7d9fc40e9f29c3156b6625cac8d91d562e1Kevin Enderby  // Third part: the list of label DIEs.
65794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
65811c2defa9157bd589cb322218c718c4492ed5746Kevin Enderby  // Loop on saved info for dwarf labels and create the DIEs for them.
65911c2defa9157bd589cb322218c718c4492ed5746Kevin Enderby  const std::vector<const MCGenDwarfLabelEntry *> &Entries =
66011c2defa9157bd589cb322218c718c4492ed5746Kevin Enderby    MCOS->getContext().getMCGenDwarfLabelEntries();
66111c2defa9157bd589cb322218c718c4492ed5746Kevin Enderby  for (std::vector<const MCGenDwarfLabelEntry *>::const_iterator it =
66294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby       Entries.begin(), ie = Entries.end(); it != ie;
66394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby       ++it) {
66411c2defa9157bd589cb322218c718c4492ed5746Kevin Enderby    const MCGenDwarfLabelEntry *Entry = *it;
66594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
66638fdb7d9fc40e9f29c3156b6625cac8d91d562e1Kevin Enderby    // The DW_TAG_label DIE abbrev (2).
66794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    MCOS->EmitULEB128IntValue(2);
66894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
66994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    // AT_name, of the label without any leading underbar.
67068ca56285f9b6e82eb16ff8ea02a301f2c489faeEric Christopher    MCOS->EmitBytes(Entry->getName());
67194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
67294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
67394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    // AT_decl_file, index into the file table.
67494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    MCOS->EmitIntValue(Entry->getFileNumber(), 4);
67594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
67694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    // AT_decl_line, source line number.
67794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    MCOS->EmitIntValue(Entry->getLineNumber(), 4);
67894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
67994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    // AT_low_pc, start address of the label.
68094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    const MCExpr *AT_low_pc = MCSymbolRefExpr::Create(Entry->getLabel(),
68194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby                                             MCSymbolRefExpr::VK_None, context);
68294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    MCOS->EmitAbsValue(AT_low_pc, AddrSize);
68394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
68494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    // DW_AT_prototyped, a one byte flag value of 0 saying we have no prototype.
68594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    MCOS->EmitIntValue(0, 1);
68694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
68794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    // The DW_TAG_unspecified_parameters DIE abbrev (3).
68894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    MCOS->EmitULEB128IntValue(3);
68994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
69094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    // Add the NULL DIE terminating the DW_TAG_unspecified_parameters DIE's.
69194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    MCOS->EmitIntValue(0, 1);
69294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  }
69311c2defa9157bd589cb322218c718c4492ed5746Kevin Enderby  // Deallocate the MCGenDwarfLabelEntry classes that saved away the info
69411c2defa9157bd589cb322218c718c4492ed5746Kevin Enderby  // for the dwarf labels.
69511c2defa9157bd589cb322218c718c4492ed5746Kevin Enderby  for (std::vector<const MCGenDwarfLabelEntry *>::const_iterator it =
69694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby       Entries.begin(), ie = Entries.end(); it != ie;
69794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby       ++it) {
69811c2defa9157bd589cb322218c718c4492ed5746Kevin Enderby    const MCGenDwarfLabelEntry *Entry = *it;
69994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    delete Entry;
70094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  }
70194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
70294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Add the NULL DIE terminating the Compile Unit DIE's.
70394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(0, 1);
70494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
70594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Now set the value of the symbol at the end of the info section.
70694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitLabel(InfoEnd);
70794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby}
70894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
70994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby//
71094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby// When generating dwarf for assembly source files this emits the Dwarf
71194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby// sections.
71294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby//
713489d67927172941bf59b9f4829ab8910814fea24Rafael Espindolavoid MCGenDwarfInfo::Emit(MCStreamer *MCOS, const MCSymbol *LineSectionSymbol) {
71494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Create the dwarf sections in this order (.debug_line already created).
71594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCContext &context = MCOS->getContext();
716489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola  const MCAsmInfo &AsmInfo = context.getAsmInfo();
71715ab115df57949fa55d92ffdfba491f7d02ed60fAlexey Samsonov  bool CreateDwarfSectionSymbols =
71815ab115df57949fa55d92ffdfba491f7d02ed60fAlexey Samsonov      AsmInfo.doesDwarfUseRelocationsAcrossSections();
71915ab115df57949fa55d92ffdfba491f7d02ed60fAlexey Samsonov  if (!CreateDwarfSectionSymbols)
72015ab115df57949fa55d92ffdfba491f7d02ed60fAlexey Samsonov    LineSectionSymbol = NULL;
72115ab115df57949fa55d92ffdfba491f7d02ed60fAlexey Samsonov  MCSymbol *AbbrevSectionSymbol = NULL;
72215ab115df57949fa55d92ffdfba491f7d02ed60fAlexey Samsonov  MCSymbol *InfoSectionSymbol = NULL;
72394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
72415ab115df57949fa55d92ffdfba491f7d02ed60fAlexey Samsonov  if (CreateDwarfSectionSymbols) {
72515ab115df57949fa55d92ffdfba491f7d02ed60fAlexey Samsonov    InfoSectionSymbol = context.CreateTempSymbol();
72615ab115df57949fa55d92ffdfba491f7d02ed60fAlexey Samsonov    MCOS->EmitLabel(InfoSectionSymbol);
72715ab115df57949fa55d92ffdfba491f7d02ed60fAlexey Samsonov  }
72894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
72915ab115df57949fa55d92ffdfba491f7d02ed60fAlexey Samsonov  if (CreateDwarfSectionSymbols) {
730489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola    AbbrevSectionSymbol = context.CreateTempSymbol();
731489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola    MCOS->EmitLabel(AbbrevSectionSymbol);
732489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola  }
73394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
73494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
73594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // If there are no line table entries then do not emit any section contents.
73694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  if (context.getMCLineSections().empty())
73794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    return;
73894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
73994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Output the data for .debug_aranges section.
74015ab115df57949fa55d92ffdfba491f7d02ed60fAlexey Samsonov  EmitGenDwarfAranges(MCOS, InfoSectionSymbol);
74194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
74294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Output the data for .debug_abbrev section.
74394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitGenDwarfAbbrev(MCOS);
74494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
74594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Output the data for .debug_info section.
746489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola  EmitGenDwarfInfo(MCOS, AbbrevSectionSymbol, LineSectionSymbol);
74794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby}
74894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
74994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby//
75094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby// When generating dwarf for assembly source files this is called when symbol
75194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby// for a label is created.  If this symbol is not a temporary and is in the
75294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby// section that dwarf is being generated for, save the needed info to create
75311c2defa9157bd589cb322218c718c4492ed5746Kevin Enderby// a dwarf label.
75494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby//
75511c2defa9157bd589cb322218c718c4492ed5746Kevin Enderbyvoid MCGenDwarfLabelEntry::Make(MCSymbol *Symbol, MCStreamer *MCOS,
75694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby                                     SourceMgr &SrcMgr, SMLoc &Loc) {
7573c3cdcc270515de95a4982db3073b9fdf44a5c2eEric Christopher  // We won't create dwarf labels for temporary symbols or symbols not in
75894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // the default text.
75994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  if (Symbol->isTemporary())
76094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    return;
76194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCContext &context = MCOS->getContext();
76294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  if (context.getGenDwarfSection() != MCOS->getCurrentSection())
76394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    return;
76494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
76511c2defa9157bd589cb322218c718c4492ed5746Kevin Enderby  // The dwarf label's name does not have the symbol name's leading
76694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // underbar if any.
76794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  StringRef Name = Symbol->getName();
76894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  if (Name.startswith("_"))
76994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    Name = Name.substr(1, Name.size()-1);
77094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
77111c2defa9157bd589cb322218c718c4492ed5746Kevin Enderby  // Get the dwarf file number to be used for the dwarf label.
77294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  unsigned FileNumber = context.getGenDwarfFileNumber();
77394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
77494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Finding the line number is the expensive part which is why we just don't
77511c2defa9157bd589cb322218c718c4492ed5746Kevin Enderby  // pass it in as for some symbols we won't create a dwarf label.
77694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  int CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
77794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  unsigned LineNumber = SrcMgr.FindLineNumber(Loc, CurBuffer);
77894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
77994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // We create a temporary symbol for use for the AT_high_pc and AT_low_pc
78094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // values so that they don't have things like an ARM thumb bit from the
78194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // original symbol. So when used they won't get a low bit set after
78294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // relocation.
78394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCSymbol *Label = context.CreateTempSymbol();
78494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitLabel(Label);
78594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
78694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Create and entry for the info and add it to the other entries.
7872684d9e3c702b2ef9fd430155d94671d12fa994fJim Grosbach  MCGenDwarfLabelEntry *Entry =
78811c2defa9157bd589cb322218c718c4492ed5746Kevin Enderby    new MCGenDwarfLabelEntry(Name, FileNumber, LineNumber, Label);
78911c2defa9157bd589cb322218c718c4492ed5746Kevin Enderby  MCOS->getContext().addMCGenDwarfLabelEntry(Entry);
79094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby}
79194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
79289b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindolastatic int getDataAlignmentFactor(MCStreamer &streamer) {
79389b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  MCContext &context = streamer.getContext();
7941be0e271a07925b928ba89848934f1ea6f1854e2Evan Cheng  const MCAsmInfo &asmInfo = context.getAsmInfo();
79589b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  int size = asmInfo.getPointerSize();
7961be0e271a07925b928ba89848934f1ea6f1854e2Evan Cheng  if (asmInfo.isStackGrowthDirectionUp())
79789b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola    return size;
7981be0e271a07925b928ba89848934f1ea6f1854e2Evan Cheng  else
7991be0e271a07925b928ba89848934f1ea6f1854e2Evan Cheng    return -size;
80089b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola}
80189b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
802abf9af60add19a0f037bb2a05433769bfd1e7f6fRafael Espindolastatic unsigned getSizeForEncoding(MCStreamer &streamer,
803abf9af60add19a0f037bb2a05433769bfd1e7f6fRafael Espindola                                   unsigned symbolEncoding) {
80425f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  MCContext &context = streamer.getContext();
80525f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  unsigned format = symbolEncoding & 0x0f;
80625f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  switch (format) {
807858143816d43e58b17bfd11cb1b57afbd7f0f893Craig Topper  default: llvm_unreachable("Unknown Encoding");
80825f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  case dwarf::DW_EH_PE_absptr:
80925f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  case dwarf::DW_EH_PE_signed:
8101be0e271a07925b928ba89848934f1ea6f1854e2Evan Cheng    return context.getAsmInfo().getPointerSize();
81125f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  case dwarf::DW_EH_PE_udata2:
81225f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  case dwarf::DW_EH_PE_sdata2:
813abf9af60add19a0f037bb2a05433769bfd1e7f6fRafael Espindola    return 2;
81425f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  case dwarf::DW_EH_PE_udata4:
81525f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  case dwarf::DW_EH_PE_sdata4:
816abf9af60add19a0f037bb2a05433769bfd1e7f6fRafael Espindola    return 4;
81725f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  case dwarf::DW_EH_PE_udata8:
81825f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  case dwarf::DW_EH_PE_sdata8:
819abf9af60add19a0f037bb2a05433769bfd1e7f6fRafael Espindola    return 8;
82025f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  }
821abf9af60add19a0f037bb2a05433769bfd1e7f6fRafael Espindola}
822abf9af60add19a0f037bb2a05433769bfd1e7f6fRafael Espindola
823abf9af60add19a0f037bb2a05433769bfd1e7f6fRafael Espindolastatic void EmitSymbol(MCStreamer &streamer, const MCSymbol &symbol,
8242541c41f3e2af94585e14353a91f02facd65e415Bill Wendling                       unsigned symbolEncoding, const char *comment = 0) {
825a0057ca13f06b8de08483c3e3a143a7236c67097Rafael Espindola  MCContext &context = streamer.getContext();
826a0057ca13f06b8de08483c3e3a143a7236c67097Rafael Espindola  const MCAsmInfo &asmInfo = context.getAsmInfo();
827a0057ca13f06b8de08483c3e3a143a7236c67097Rafael Espindola  const MCExpr *v = asmInfo.getExprForFDESymbol(&symbol,
828debd7e4e8bc5cfe61bfb71835ce2b1a3fbccc2beRafael Espindola                                                symbolEncoding,
829a0057ca13f06b8de08483c3e3a143a7236c67097Rafael Espindola                                                streamer);
830abf9af60add19a0f037bb2a05433769bfd1e7f6fRafael Espindola  unsigned size = getSizeForEncoding(streamer, symbolEncoding);
8312541c41f3e2af94585e14353a91f02facd65e415Bill Wendling  if (streamer.isVerboseAsm() && comment) streamer.AddComment(comment);
832debd7e4e8bc5cfe61bfb71835ce2b1a3fbccc2beRafael Espindola  streamer.EmitAbsValue(v, size);
83325f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola}
83425f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola
835bfa27cc5d72e061a96efbb461864d40bc8089ec2Rafael Espindolastatic void EmitPersonality(MCStreamer &streamer, const MCSymbol &symbol,
836bfa27cc5d72e061a96efbb461864d40bc8089ec2Rafael Espindola                            unsigned symbolEncoding) {
837bfa27cc5d72e061a96efbb461864d40bc8089ec2Rafael Espindola  MCContext &context = streamer.getContext();
838bfa27cc5d72e061a96efbb461864d40bc8089ec2Rafael Espindola  const MCAsmInfo &asmInfo = context.getAsmInfo();
839debd7e4e8bc5cfe61bfb71835ce2b1a3fbccc2beRafael Espindola  const MCExpr *v = asmInfo.getExprForPersonalitySymbol(&symbol,
840debd7e4e8bc5cfe61bfb71835ce2b1a3fbccc2beRafael Espindola                                                        symbolEncoding,
841debd7e4e8bc5cfe61bfb71835ce2b1a3fbccc2beRafael Espindola                                                        streamer);
842bfa27cc5d72e061a96efbb461864d40bc8089ec2Rafael Espindola  unsigned size = getSizeForEncoding(streamer, symbolEncoding);
843debd7e4e8bc5cfe61bfb71835ce2b1a3fbccc2beRafael Espindola  streamer.EmitValue(v, size);
844bfa27cc5d72e061a96efbb461864d40bc8089ec2Rafael Espindola}
845bfa27cc5d72e061a96efbb461864d40bc8089ec2Rafael Espindola
84625f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindolastatic const MachineLocation TranslateMachineLocation(
8470e6a052331f674dd70e28af41f654a7874405eabEvan Cheng                                                  const MCRegisterInfo &MRI,
84825f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                                                  const MachineLocation &Loc) {
84925f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  unsigned Reg = Loc.getReg() == MachineLocation::VirtualFP ?
85025f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola    MachineLocation::VirtualFP :
8510e6a052331f674dd70e28af41f654a7874405eabEvan Cheng    unsigned(MRI.getDwarfRegNum(Loc.getReg(), true));
85225f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  const MachineLocation &NewLoc = Loc.isReg() ?
85325f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola    MachineLocation(Reg) : MachineLocation(Reg, Loc.getOffset());
85425f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  return NewLoc;
85525f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola}
85625f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola
85725f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindolanamespace {
85825f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  class FrameEmitterImpl {
85925f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola    int CFAOffset;
860514cecc72b23bb75108844285ab8f3616a08bac5Rafael Espindola    int CIENum;
8615426a9ee37667660935d80841c5392d78e254318Rafael Espindola    bool UsingCFI;
86240a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    bool IsEH;
863e3a0e987f3d4f07512cdb64b9034369f966cb448Rafael Espindola    const MCSymbol *SectionStart;
86425f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  public:
8651424c3c3e696868775ac5bfc57d27a0ac3bc6c63Bill Wendling    FrameEmitterImpl(bool usingCFI, bool isEH)
8661424c3c3e696868775ac5bfc57d27a0ac3bc6c63Bill Wendling      : CFAOffset(0), CIENum(0), UsingCFI(usingCFI), IsEH(isEH),
8671424c3c3e696868775ac5bfc57d27a0ac3bc6c63Bill Wendling        SectionStart(0) {}
8681424c3c3e696868775ac5bfc57d27a0ac3bc6c63Bill Wendling
8691424c3c3e696868775ac5bfc57d27a0ac3bc6c63Bill Wendling    void setSectionStart(const MCSymbol *Label) { SectionStart = Label; }
87025f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola
871e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling    /// EmitCompactUnwind - Emit the unwind information in a compact way. If
872e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling    /// we're successful, return 'true'. Otherwise, return 'false' and it will
873e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling    /// emit the normal CIE and FDE.
874e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling    bool EmitCompactUnwind(MCStreamer &streamer,
875e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling                           const MCDwarfFrameInfo &frame);
876e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling
87725f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola    const MCSymbol &EmitCIE(MCStreamer &streamer,
87825f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                            const MCSymbol *personality,
87925f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                            unsigned personalityEncoding,
88025f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                            const MCSymbol *lsda,
88116d7d437e03ce87fdaef7971919302920d54a966Rafael Espindola                            bool IsSignalFrame,
88225f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                            unsigned lsdaEncoding);
88325f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola    MCSymbol *EmitFDE(MCStreamer &streamer,
88425f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                      const MCSymbol &cieStart,
8859f270dadd429771dded5a8572da8c74513771c15Rafael Espindola                      const MCDwarfFrameInfo &frame);
88625f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola    void EmitCFIInstructions(MCStreamer &streamer,
88725f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                             const std::vector<MCCFIInstruction> &Instrs,
88825f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                             MCSymbol *BaseLabel);
88925f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola    void EmitCFIInstruction(MCStreamer &Streamer,
89025f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                            const MCCFIInstruction &Instr);
89125f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  };
8923c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling
8933c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling} // end anonymous namespace
8943c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling
8953c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendlingstatic void EmitEncodingByte(MCStreamer &Streamer, unsigned Encoding,
8963c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling                             StringRef Prefix) {
8973c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling  if (Streamer.isVerboseAsm()) {
898eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer    const char *EncStr;
8993c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling    switch (Encoding) {
900eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer    default: EncStr = "<unknown encoding>"; break;
901eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer    case dwarf::DW_EH_PE_absptr: EncStr = "absptr"; break;
902eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer    case dwarf::DW_EH_PE_omit:   EncStr = "omit"; break;
903eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer    case dwarf::DW_EH_PE_pcrel:  EncStr = "pcrel"; break;
904eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer    case dwarf::DW_EH_PE_udata4: EncStr = "udata4"; break;
905eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer    case dwarf::DW_EH_PE_udata8: EncStr = "udata8"; break;
906eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer    case dwarf::DW_EH_PE_sdata4: EncStr = "sdata4"; break;
907eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer    case dwarf::DW_EH_PE_sdata8: EncStr = "sdata8"; break;
908eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer    case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
909eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer      EncStr = "pcrel udata4";
910eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer      break;
911eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer    case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
912eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer      EncStr = "pcrel sdata4";
913eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer      break;
914eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer    case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
915eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer      EncStr = "pcrel udata8";
916eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer      break;
917eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer    case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
918eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer      EncStr = "screl sdata8";
919eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer      break;
9203c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling    case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata4:
9213c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling      EncStr = "indirect pcrel udata4";
922eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer      break;
9233c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling    case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata4:
9243c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling      EncStr = "indirect pcrel sdata4";
925eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer      break;
9263c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling    case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata8:
9273c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling      EncStr = "indirect pcrel udata8";
928eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer      break;
9293c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling    case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata8:
9303c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling      EncStr = "indirect pcrel sdata8";
931eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer      break;
9323c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling    }
9333c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling
9343c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling    Streamer.AddComment(Twine(Prefix) + " = " + EncStr);
9353c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling  }
9363c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling
9373c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling  Streamer.EmitIntValue(Encoding, 1);
93825f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola}
93925f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola
94025f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindolavoid FrameEmitterImpl::EmitCFIInstruction(MCStreamer &Streamer,
94125f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                                          const MCCFIInstruction &Instr) {
942fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola  int dataAlignmentFactor = getDataAlignmentFactor(Streamer);
943efd24ddbffc1e69fa88089de036d1873c950dfb5Bill Wendling  bool VerboseAsm = Streamer.isVerboseAsm();
94489b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
945fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola  switch (Instr.getOperation()) {
946f4f14f68f6078ea6681ee27b5bf42719d7db3441Rafael Espindola  case MCCFIInstruction::OpRegister: {
947f4f14f68f6078ea6681ee27b5bf42719d7db3441Rafael Espindola    unsigned Reg1 = Instr.getRegister();
948f4f14f68f6078ea6681ee27b5bf42719d7db3441Rafael Espindola    unsigned Reg2 = Instr.getRegister2();
949f4f14f68f6078ea6681ee27b5bf42719d7db3441Rafael Espindola    if (VerboseAsm) {
950f4f14f68f6078ea6681ee27b5bf42719d7db3441Rafael Espindola      Streamer.AddComment("DW_CFA_register");
951f4f14f68f6078ea6681ee27b5bf42719d7db3441Rafael Espindola      Streamer.AddComment(Twine("Reg1 ") + Twine(Reg1));
952f4f14f68f6078ea6681ee27b5bf42719d7db3441Rafael Espindola      Streamer.AddComment(Twine("Reg2 ") + Twine(Reg2));
953f4f14f68f6078ea6681ee27b5bf42719d7db3441Rafael Espindola    }
954f4f14f68f6078ea6681ee27b5bf42719d7db3441Rafael Espindola    Streamer.EmitIntValue(dwarf::DW_CFA_register, 1);
955f4f14f68f6078ea6681ee27b5bf42719d7db3441Rafael Espindola    Streamer.EmitULEB128IntValue(Reg1);
956f4f14f68f6078ea6681ee27b5bf42719d7db3441Rafael Espindola    Streamer.EmitULEB128IntValue(Reg2);
957f4f14f68f6078ea6681ee27b5bf42719d7db3441Rafael Espindola    return;
958f4f14f68f6078ea6681ee27b5bf42719d7db3441Rafael Espindola  }
9597f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola  case MCCFIInstruction::OpUndefined: {
960ff233c9e5c3e439fd1eed84b9a9e88a5370572dbRafael Espindola    unsigned Reg = Instr.getRegister();
961c8fec7e21f5c24303eab8a8592f3b8faff347d86Rafael Espindola    if (VerboseAsm) {
962c8fec7e21f5c24303eab8a8592f3b8faff347d86Rafael Espindola      Streamer.AddComment("DW_CFA_undefined");
963c8fec7e21f5c24303eab8a8592f3b8faff347d86Rafael Espindola      Streamer.AddComment(Twine("Reg ") + Twine(Reg));
964c8fec7e21f5c24303eab8a8592f3b8faff347d86Rafael Espindola    }
965c8fec7e21f5c24303eab8a8592f3b8faff347d86Rafael Espindola    Streamer.EmitIntValue(dwarf::DW_CFA_undefined, 1);
966c8fec7e21f5c24303eab8a8592f3b8faff347d86Rafael Espindola    Streamer.EmitULEB128IntValue(Reg);
967c8fec7e21f5c24303eab8a8592f3b8faff347d86Rafael Espindola    return;
968c8fec7e21f5c24303eab8a8592f3b8faff347d86Rafael Espindola  }
9697f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola  case MCCFIInstruction::OpAdjustCfaOffset:
9707f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola  case MCCFIInstruction::OpDefCfaOffset: {
9717f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola    const bool IsRelative =
9727f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola      Instr.getOperation() == MCCFIInstruction::OpAdjustCfaOffset;
97389b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
9747f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola    if (VerboseAsm)
9757f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola      Streamer.AddComment("DW_CFA_def_cfa_offset");
9767f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola    Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_offset, 1);
9775d7dcd3335234d2a2bc16dc69f86fbb5dcaa8962Rafael Espindola
9787f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola    if (IsRelative)
979ff233c9e5c3e439fd1eed84b9a9e88a5370572dbRafael Espindola      CFAOffset += Instr.getOffset();
9807f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola    else
981ff233c9e5c3e439fd1eed84b9a9e88a5370572dbRafael Espindola      CFAOffset = -Instr.getOffset();
98289b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
9837f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola    if (VerboseAsm)
9847f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola      Streamer.AddComment(Twine("Offset " + Twine(CFAOffset)));
9857f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola    Streamer.EmitULEB128IntValue(CFAOffset);
9867f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola
9877f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola    return;
9887f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola  }
9897f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola  case MCCFIInstruction::OpDefCfa: {
9907f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola    if (VerboseAsm)
9917f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola      Streamer.AddComment("DW_CFA_def_cfa");
9927f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola    Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa, 1);
9937f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola
9947f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola    if (VerboseAsm)
995ff233c9e5c3e439fd1eed84b9a9e88a5370572dbRafael Espindola      Streamer.AddComment(Twine("Reg ") + Twine(Instr.getRegister()));
996ff233c9e5c3e439fd1eed84b9a9e88a5370572dbRafael Espindola    Streamer.EmitULEB128IntValue(Instr.getRegister());
9977f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola
998ff233c9e5c3e439fd1eed84b9a9e88a5370572dbRafael Espindola    CFAOffset = -Instr.getOffset();
9997f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola
10007f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola    if (VerboseAsm)
10017f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola      Streamer.AddComment(Twine("Offset " + Twine(CFAOffset)));
10027f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola    Streamer.EmitULEB128IntValue(CFAOffset);
10037f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola
10047f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola    return;
10057f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola  }
10067f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola
10077f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola  case MCCFIInstruction::OpDefCfaRegister: {
10087f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola    if (VerboseAsm)
10097f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola      Streamer.AddComment("DW_CFA_def_cfa_register");
10107f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola    Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_register, 1);
10117f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola
10127f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola    if (VerboseAsm)
1013ff233c9e5c3e439fd1eed84b9a9e88a5370572dbRafael Espindola      Streamer.AddComment(Twine("Reg ") + Twine(Instr.getRegister()));
1014ff233c9e5c3e439fd1eed84b9a9e88a5370572dbRafael Espindola    Streamer.EmitULEB128IntValue(Instr.getRegister());
10157f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola
10167f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola    return;
10177f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola  }
10187f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola
10197f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola  case MCCFIInstruction::OpOffset:
10207f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola  case MCCFIInstruction::OpRelOffset: {
10217f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola    const bool IsRelative =
10227f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola      Instr.getOperation() == MCCFIInstruction::OpRelOffset;
102389b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
1024ff233c9e5c3e439fd1eed84b9a9e88a5370572dbRafael Espindola    unsigned Reg = Instr.getRegister();
1025ff233c9e5c3e439fd1eed84b9a9e88a5370572dbRafael Espindola    int Offset = Instr.getOffset();
102625f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola    if (IsRelative)
102725f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola      Offset -= CFAOffset;
102825f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola    Offset = Offset / dataAlignmentFactor;
102989b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
103089b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola    if (Offset < 0) {
1031efd24ddbffc1e69fa88089de036d1873c950dfb5Bill Wendling      if (VerboseAsm) Streamer.AddComment("DW_CFA_offset_extended_sf");
1032fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola      Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended_sf, 1);
1033efd24ddbffc1e69fa88089de036d1873c950dfb5Bill Wendling      if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
1034fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola      Streamer.EmitULEB128IntValue(Reg);
1035efd24ddbffc1e69fa88089de036d1873c950dfb5Bill Wendling      if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
1036fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola      Streamer.EmitSLEB128IntValue(Offset);
103789b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola    } else if (Reg < 64) {
1038e08d4335ad29d74008222b4d7ac91c153ed66becBill Wendling      if (VerboseAsm) Streamer.AddComment(Twine("DW_CFA_offset + Reg(") +
1039e08d4335ad29d74008222b4d7ac91c153ed66becBill Wendling                                          Twine(Reg) + ")");
1040fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola      Streamer.EmitIntValue(dwarf::DW_CFA_offset + Reg, 1);
1041efd24ddbffc1e69fa88089de036d1873c950dfb5Bill Wendling      if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
1042eccbad78e0854743e358841b64f9e0f5eba82861Rafael Espindola      Streamer.EmitULEB128IntValue(Offset);
104389b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola    } else {
1044efd24ddbffc1e69fa88089de036d1873c950dfb5Bill Wendling      if (VerboseAsm) Streamer.AddComment("DW_CFA_offset_extended");
1045fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola      Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended, 1);
1046efd24ddbffc1e69fa88089de036d1873c950dfb5Bill Wendling      if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
1047eccbad78e0854743e358841b64f9e0f5eba82861Rafael Espindola      Streamer.EmitULEB128IntValue(Reg);
1048efd24ddbffc1e69fa88089de036d1873c950dfb5Bill Wendling      if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
1049eccbad78e0854743e358841b64f9e0f5eba82861Rafael Espindola      Streamer.EmitULEB128IntValue(Offset);
1050fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola    }
1051fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola    return;
1052fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola  }
10537f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola  case MCCFIInstruction::OpRememberState:
1054efd24ddbffc1e69fa88089de036d1873c950dfb5Bill Wendling    if (VerboseAsm) Streamer.AddComment("DW_CFA_remember_state");
1055fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola    Streamer.EmitIntValue(dwarf::DW_CFA_remember_state, 1);
1056fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola    return;
10577f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola  case MCCFIInstruction::OpRestoreState:
1058efd24ddbffc1e69fa88089de036d1873c950dfb5Bill Wendling    if (VerboseAsm) Streamer.AddComment("DW_CFA_restore_state");
1059fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola    Streamer.EmitIntValue(dwarf::DW_CFA_restore_state, 1);
1060fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola    return;
10617f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola  case MCCFIInstruction::OpSameValue: {
1062ff233c9e5c3e439fd1eed84b9a9e88a5370572dbRafael Espindola    unsigned Reg = Instr.getRegister();
1063efd24ddbffc1e69fa88089de036d1873c950dfb5Bill Wendling    if (VerboseAsm) Streamer.AddComment("DW_CFA_same_value");
1064c57543964d1382d3d3a5005f415b6c0f49671b3aRafael Espindola    Streamer.EmitIntValue(dwarf::DW_CFA_same_value, 1);
1065efd24ddbffc1e69fa88089de036d1873c950dfb5Bill Wendling    if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
1066e8cfbd843d737e1f95c3032c7670c2be3838a6f6Rafael Espindola    Streamer.EmitULEB128IntValue(Reg);
1067c57543964d1382d3d3a5005f415b6c0f49671b3aRafael Espindola    return;
1068c57543964d1382d3d3a5005f415b6c0f49671b3aRafael Espindola  }
10697f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola  case MCCFIInstruction::OpRestore: {
1070ff233c9e5c3e439fd1eed84b9a9e88a5370572dbRafael Espindola    unsigned Reg = Instr.getRegister();
1071ed23bdb65fe86cdb7a38c8c1998ec965e6973966Rafael Espindola    if (VerboseAsm) {
1072ed23bdb65fe86cdb7a38c8c1998ec965e6973966Rafael Espindola      Streamer.AddComment("DW_CFA_restore");
1073ed23bdb65fe86cdb7a38c8c1998ec965e6973966Rafael Espindola      Streamer.AddComment(Twine("Reg ") + Twine(Reg));
1074ed23bdb65fe86cdb7a38c8c1998ec965e6973966Rafael Espindola    }
1075ed23bdb65fe86cdb7a38c8c1998ec965e6973966Rafael Espindola    Streamer.EmitIntValue(dwarf::DW_CFA_restore | Reg, 1);
1076ed23bdb65fe86cdb7a38c8c1998ec965e6973966Rafael Espindola    return;
1077ed23bdb65fe86cdb7a38c8c1998ec965e6973966Rafael Espindola  }
10787f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola  case MCCFIInstruction::OpEscape:
10796f0b181bc70318f8d5d4b9bdead7fc748677fe2aRafael Espindola    if (VerboseAsm) Streamer.AddComment("Escape bytes");
108068ca56285f9b6e82eb16ff8ea02a301f2c489faeEric Christopher    Streamer.EmitBytes(Instr.getValues());
10816f0b181bc70318f8d5d4b9bdead7fc748677fe2aRafael Espindola    return;
1082fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola  }
1083fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola  llvm_unreachable("Unhandled case in switch");
1084fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola}
1085fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola
1086fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola/// EmitFrameMoves - Emit frame instructions to describe the layout of the
1087fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola/// frame.
108825f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindolavoid FrameEmitterImpl::EmitCFIInstructions(MCStreamer &streamer,
108925f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                                    const std::vector<MCCFIInstruction> &Instrs,
109025f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                                           MCSymbol *BaseLabel) {
1091fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola  for (unsigned i = 0, N = Instrs.size(); i < N; ++i) {
1092fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola    const MCCFIInstruction &Instr = Instrs[i];
1093fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola    MCSymbol *Label = Instr.getLabel();
1094fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola    // Throw out move if the label is invalid.
1095fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola    if (Label && !Label->isDefined()) continue; // Not emitted, in dead code.
1096fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola
1097fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola    // Advance row if new location.
1098fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola    if (BaseLabel && Label) {
1099fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola      MCSymbol *ThisSym = Label;
1100fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola      if (ThisSym != BaseLabel) {
1101d221cd676b44d8118b1db152ecc2168538479626Bill Wendling        if (streamer.isVerboseAsm()) streamer.AddComment("DW_CFA_advance_loc4");
1102fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola        streamer.EmitDwarfAdvanceFrameAddr(BaseLabel, ThisSym);
1103fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola        BaseLabel = ThisSym;
1104fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola      }
110589b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola    }
1106fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola
1107b40a71fda188f8ca564e606ac2cb051a44ada311Rafael Espindola    EmitCFIInstruction(streamer, Instr);
110889b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  }
110989b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola}
111089b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
1111e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling/// EmitCompactUnwind - Emit the unwind information in a compact way. If we're
1112e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling/// successful, return 'true'. Otherwise, return 'false' and it will emit the
1113e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling/// normal CIE and FDE.
1114e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendlingbool FrameEmitterImpl::EmitCompactUnwind(MCStreamer &Streamer,
1115e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling                                         const MCDwarfFrameInfo &Frame) {
1116e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  MCContext &Context = Streamer.getContext();
1117e76a33b9567d78a5744dc52fcec3a6056d6fb576Evan Cheng  const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
11183c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling  bool VerboseAsm = Streamer.isVerboseAsm();
1119e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling
1120e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  // range-start range-length  compact-unwind-enc personality-func   lsda
1121e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //  _foo       LfooEnd-_foo  0x00000023          0                 0
1122e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //  _bar       LbarEnd-_bar  0x00000025         __gxx_personality  except_tab1
1123e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //
1124e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //   .section __LD,__compact_unwind,regular,debug
1125e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //
1126e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //   # compact unwind for _foo
1127e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //   .quad _foo
1128e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //   .set L1,LfooEnd-_foo
1129e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //   .long L1
1130e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //   .long 0x01010001
1131e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //   .quad 0
1132e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //   .quad 0
1133e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //
1134e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //   # compact unwind for _bar
1135e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //   .quad _bar
1136e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //   .set L2,LbarEnd-_bar
1137e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //   .long L2
1138e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //   .long 0x01020011
1139e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //   .quad __gxx_personality
1140e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //   .quad except_tab1
1141e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling
1142e52e3f2c020354c8e0e966b719152b08349d0334Bill Wendling  uint32_t Encoding = Frame.CompactUnwindEncoding;
11436a6b8c3e96b9e1ca7092eafd0cfb219cbbfbdfc4Bill Wendling  if (!Encoding) return false;
11446a6b8c3e96b9e1ca7092eafd0cfb219cbbfbdfc4Bill Wendling
11454bb5b037126ea35f35d5a490b03ccffa8677fa5eBill Wendling  // The encoding needs to know we have an LSDA.
11464bb5b037126ea35f35d5a490b03ccffa8677fa5eBill Wendling  if (Frame.Lsda)
11474bb5b037126ea35f35d5a490b03ccffa8677fa5eBill Wendling    Encoding |= 0x40000000;
11484bb5b037126ea35f35d5a490b03ccffa8677fa5eBill Wendling
1149e76a33b9567d78a5744dc52fcec3a6056d6fb576Evan Cheng  Streamer.SwitchSection(MOFI->getCompactUnwindSection());
11503c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling
1151e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  // Range Start
1152203576aa0cb9d8bf2d2e4d910ebab4b7a63262aeEvan Cheng  unsigned FDEEncoding = MOFI->getFDEEncoding(UsingCFI);
11539056e9032001a2d47057cecec5e39895cbc31799Bill Wendling  unsigned Size = getSizeForEncoding(Streamer, FDEEncoding);
11543c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling  if (VerboseAsm) Streamer.AddComment("Range Start");
11559056e9032001a2d47057cecec5e39895cbc31799Bill Wendling  Streamer.EmitSymbolValue(Frame.Function, Size);
1156e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling
1157e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  // Range Length
1158e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  const MCExpr *Range = MakeStartMinusEndExpr(Streamer, *Frame.Begin,
1159e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling                                              *Frame.End, 0);
11603c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling  if (VerboseAsm) Streamer.AddComment("Range Length");
11619287a6eef336585628a55e1f33e1e3ea9d7f81cfBill Wendling  Streamer.EmitAbsValue(Range, 4);
11629287a6eef336585628a55e1f33e1e3ea9d7f81cfBill Wendling
11639287a6eef336585628a55e1f33e1e3ea9d7f81cfBill Wendling  // Compact Encoding
11649287a6eef336585628a55e1f33e1e3ea9d7f81cfBill Wendling  Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_udata4);
116570be28a5adba5bcae0c6dcd63f17592864c351fcBenjamin Kramer  if (VerboseAsm) Streamer.AddComment("Compact Unwind Encoding: 0x" +
116670be28a5adba5bcae0c6dcd63f17592864c351fcBenjamin Kramer                                      Twine::utohexstr(Encoding));
11679287a6eef336585628a55e1f33e1e3ea9d7f81cfBill Wendling  Streamer.EmitIntValue(Encoding, Size);
11689287a6eef336585628a55e1f33e1e3ea9d7f81cfBill Wendling
1169e52e3f2c020354c8e0e966b719152b08349d0334Bill Wendling
11709056e9032001a2d47057cecec5e39895cbc31799Bill Wendling  // Personality Function
11714bb5b037126ea35f35d5a490b03ccffa8677fa5eBill Wendling  Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_absptr);
11723c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling  if (VerboseAsm) Streamer.AddComment("Personality Function");
11734498d39680abe3970dc84dca973aff46d0f9039bBill Wendling  if (Frame.Personality)
11749056e9032001a2d47057cecec5e39895cbc31799Bill Wendling    Streamer.EmitSymbolValue(Frame.Personality, Size);
11754498d39680abe3970dc84dca973aff46d0f9039bBill Wendling  else
11764498d39680abe3970dc84dca973aff46d0f9039bBill Wendling    Streamer.EmitIntValue(0, Size); // No personality fn
11779056e9032001a2d47057cecec5e39895cbc31799Bill Wendling
11789056e9032001a2d47057cecec5e39895cbc31799Bill Wendling  // LSDA
11794498d39680abe3970dc84dca973aff46d0f9039bBill Wendling  Size = getSizeForEncoding(Streamer, Frame.LsdaEncoding);
11803c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling  if (VerboseAsm) Streamer.AddComment("LSDA");
11814498d39680abe3970dc84dca973aff46d0f9039bBill Wendling  if (Frame.Lsda)
11829056e9032001a2d47057cecec5e39895cbc31799Bill Wendling    Streamer.EmitSymbolValue(Frame.Lsda, Size);
11834498d39680abe3970dc84dca973aff46d0f9039bBill Wendling  else
11844498d39680abe3970dc84dca973aff46d0f9039bBill Wendling    Streamer.EmitIntValue(0, Size); // No LSDA
11859056e9032001a2d47057cecec5e39895cbc31799Bill Wendling
1186e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  return true;
1187e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling}
1188e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling
118925f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindolaconst MCSymbol &FrameEmitterImpl::EmitCIE(MCStreamer &streamer,
119025f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                                          const MCSymbol *personality,
119125f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                                          unsigned personalityEncoding,
119225f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                                          const MCSymbol *lsda,
119316d7d437e03ce87fdaef7971919302920d54a966Rafael Espindola                                          bool IsSignalFrame,
119425f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                                          unsigned lsdaEncoding) {
119589b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  MCContext &context = streamer.getContext();
11960e6a052331f674dd70e28af41f654a7874405eabEvan Cheng  const MCRegisterInfo &MRI = context.getRegisterInfo();
1197e76a33b9567d78a5744dc52fcec3a6056d6fb576Evan Cheng  const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
11983c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling  bool verboseAsm = streamer.isVerboseAsm();
1199514cecc72b23bb75108844285ab8f3616a08bac5Rafael Espindola
1200514cecc72b23bb75108844285ab8f3616a08bac5Rafael Espindola  MCSymbol *sectionStart;
1201e76a33b9567d78a5744dc52fcec3a6056d6fb576Evan Cheng  if (MOFI->isFunctionEHFrameSymbolPrivate() || !IsEH)
1202514cecc72b23bb75108844285ab8f3616a08bac5Rafael Espindola    sectionStart = context.CreateTempSymbol();
1203514cecc72b23bb75108844285ab8f3616a08bac5Rafael Espindola  else
1204514cecc72b23bb75108844285ab8f3616a08bac5Rafael Espindola    sectionStart = context.GetOrCreateSymbol(Twine("EH_frame") + Twine(CIENum));
1205514cecc72b23bb75108844285ab8f3616a08bac5Rafael Espindola
12063c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling  streamer.EmitLabel(sectionStart);
1207514cecc72b23bb75108844285ab8f3616a08bac5Rafael Espindola  CIENum++;
1208514cecc72b23bb75108844285ab8f3616a08bac5Rafael Espindola
12096a6b8c3e96b9e1ca7092eafd0cfb219cbbfbdfc4Bill Wendling  MCSymbol *sectionEnd = context.CreateTempSymbol();
121089b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
121189b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  // Length
121289b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  const MCExpr *Length = MakeStartMinusEndExpr(streamer, *sectionStart,
121389b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola                                               *sectionEnd, 4);
12143c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling  if (verboseAsm) streamer.AddComment("CIE Length");
12159266cc400ee2fc86abb24ab0819d9da280fb61c4Rafael Espindola  streamer.EmitAbsValue(Length, 4);
121689b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
121789b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  // CIE ID
121840a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola  unsigned CIE_ID = IsEH ? 0 : -1;
12193c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling  if (verboseAsm) streamer.AddComment("CIE ID Tag");
122040a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola  streamer.EmitIntValue(CIE_ID, 4);
122189b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
122289b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  // Version
12233c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling  if (verboseAsm) streamer.AddComment("DW_CIE_VERSION");
122489b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  streamer.EmitIntValue(dwarf::DW_CIE_VERSION, 1);
122589b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
122689b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  // Augmentation String
122789b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  SmallString<8> Augmentation;
122840a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola  if (IsEH) {
12293c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling    if (verboseAsm) streamer.AddComment("CIE Augmentation");
123040a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    Augmentation += "z";
123140a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    if (personality)
123240a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola      Augmentation += "P";
123340a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    if (lsda)
123440a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola      Augmentation += "L";
123540a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    Augmentation += "R";
123616d7d437e03ce87fdaef7971919302920d54a966Rafael Espindola    if (IsSignalFrame)
123716d7d437e03ce87fdaef7971919302920d54a966Rafael Espindola      Augmentation += "S";
123868ca56285f9b6e82eb16ff8ea02a301f2c489faeEric Christopher    streamer.EmitBytes(Augmentation.str());
123940a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola  }
124089b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  streamer.EmitIntValue(0, 1);
124189b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
124289b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  // Code Alignment Factor
12433c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling  if (verboseAsm) streamer.AddComment("CIE Code Alignment Factor");
124489b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  streamer.EmitULEB128IntValue(1);
124589b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
124689b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  // Data Alignment Factor
12473c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling  if (verboseAsm) streamer.AddComment("CIE Data Alignment Factor");
124889b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  streamer.EmitSLEB128IntValue(getDataAlignmentFactor(streamer));
124989b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
125089b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  // Return Address Register
12513c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling  if (verboseAsm) streamer.AddComment("CIE Return Address Column");
12520e6a052331f674dd70e28af41f654a7874405eabEvan Cheng  streamer.EmitULEB128IntValue(MRI.getDwarfRegNum(MRI.getRARegister(), true));
125389b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
125489b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  // Augmentation Data Length (optional)
12551f6c87554a0d78103b95816fd2c1ce392b366bcfRafael Espindola
12561f6c87554a0d78103b95816fd2c1ce392b366bcfRafael Espindola  unsigned augmentationLength = 0;
125740a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola  if (IsEH) {
125840a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    if (personality) {
125940a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola      // Personality Encoding
126040a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola      augmentationLength += 1;
126140a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola      // Personality
126240a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola      augmentationLength += getSizeForEncoding(streamer, personalityEncoding);
126340a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    }
126440a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    if (lsda)
126540a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola      augmentationLength += 1;
126640a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    // Encoding of the FDE pointers
12671f6c87554a0d78103b95816fd2c1ce392b366bcfRafael Espindola    augmentationLength += 1;
12681f6c87554a0d78103b95816fd2c1ce392b366bcfRafael Espindola
12693c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling    if (verboseAsm) streamer.AddComment("Augmentation Size");
127040a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    streamer.EmitULEB128IntValue(augmentationLength);
127189b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
127240a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    // Augmentation Data (optional)
127340a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    if (personality) {
127440a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola      // Personality Encoding
12753c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling      EmitEncodingByte(streamer, personalityEncoding,
12763c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling                       "Personality Encoding");
127740a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola      // Personality
12783c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling      if (verboseAsm) streamer.AddComment("Personality");
127940a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola      EmitPersonality(streamer, *personality, personalityEncoding);
128040a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    }
12813c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling
128240a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    if (lsda)
12833c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling      EmitEncodingByte(streamer, lsdaEncoding, "LSDA Encoding");
12843c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling
128540a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    // Encoding of the FDE pointers
1286203576aa0cb9d8bf2d2e4d910ebab4b7a63262aeEvan Cheng    EmitEncodingByte(streamer, MOFI->getFDEEncoding(UsingCFI),
12873c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling                     "FDE Encoding");
1288bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola  }
128989b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
129089b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  // Initial Instructions
129189b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
12922d28617de2b0b731c08d1af9e830f31e14ac75b4Evan Cheng  const MCAsmInfo &MAI = context.getAsmInfo();
12932d28617de2b0b731c08d1af9e830f31e14ac75b4Evan Cheng  const std::vector<MachineMove> &Moves = MAI.getInitialFrameState();
1294fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola  std::vector<MCCFIInstruction> Instructions;
1295fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola
1296fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola  for (int i = 0, n = Moves.size(); i != n; ++i) {
1297b40a71fda188f8ca564e606ac2cb051a44ada311Rafael Espindola    MCSymbol *Label = Moves[i].getLabel();
1298b40a71fda188f8ca564e606ac2cb051a44ada311Rafael Espindola    const MachineLocation &Dst =
12990e6a052331f674dd70e28af41f654a7874405eabEvan Cheng      TranslateMachineLocation(MRI, Moves[i].getDestination());
1300b40a71fda188f8ca564e606ac2cb051a44ada311Rafael Espindola    const MachineLocation &Src =
13010e6a052331f674dd70e28af41f654a7874405eabEvan Cheng      TranslateMachineLocation(MRI, Moves[i].getSource());
130228c9ea3c13dfb8f6bb3226ba511d189135fcb140Rafael Espindola
130328c9ea3c13dfb8f6bb3226ba511d189135fcb140Rafael Espindola    if (Dst.isReg()) {
130428c9ea3c13dfb8f6bb3226ba511d189135fcb140Rafael Espindola      assert(Dst.getReg() == MachineLocation::VirtualFP);
130528c9ea3c13dfb8f6bb3226ba511d189135fcb140Rafael Espindola      assert(!Src.isReg());
130628c9ea3c13dfb8f6bb3226ba511d189135fcb140Rafael Espindola      MCCFIInstruction Inst =
130728c9ea3c13dfb8f6bb3226ba511d189135fcb140Rafael Espindola        MCCFIInstruction::createDefCfa(Label, Src.getReg(), -Src.getOffset());
130828c9ea3c13dfb8f6bb3226ba511d189135fcb140Rafael Espindola      Instructions.push_back(Inst);
130928c9ea3c13dfb8f6bb3226ba511d189135fcb140Rafael Espindola    } else {
131028c9ea3c13dfb8f6bb3226ba511d189135fcb140Rafael Espindola      assert(Src.isReg());
131128c9ea3c13dfb8f6bb3226ba511d189135fcb140Rafael Espindola      unsigned Reg = Src.getReg();
131228c9ea3c13dfb8f6bb3226ba511d189135fcb140Rafael Espindola      int Offset = Dst.getOffset();
131328c9ea3c13dfb8f6bb3226ba511d189135fcb140Rafael Espindola      MCCFIInstruction Inst =
13147f74d2c2c197eec76ae2b41fed9c227c0dcc04cbRafael Espindola        MCCFIInstruction::createOffset(Label, Reg, Offset);
131528c9ea3c13dfb8f6bb3226ba511d189135fcb140Rafael Espindola      Instructions.push_back(Inst);
131628c9ea3c13dfb8f6bb3226ba511d189135fcb140Rafael Espindola    }
1317fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola  }
131889b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
1319b40a71fda188f8ca564e606ac2cb051a44ada311Rafael Espindola  EmitCFIInstructions(streamer, Instructions, NULL);
132089b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
132189b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  // Padding
13221be0e271a07925b928ba89848934f1ea6f1854e2Evan Cheng  streamer.EmitValueToAlignment(IsEH
13231be0e271a07925b928ba89848934f1ea6f1854e2Evan Cheng                                ? 4 : context.getAsmInfo().getPointerSize());
132489b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
132589b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  streamer.EmitLabel(sectionEnd);
132689b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  return *sectionStart;
132789b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola}
132889b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
132925f492e77858dc5a95fcd7180e73aff47925b668Rafael EspindolaMCSymbol *FrameEmitterImpl::EmitFDE(MCStreamer &streamer,
133025f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                                    const MCSymbol &cieStart,
13319f270dadd429771dded5a8572da8c74513771c15Rafael Espindola                                    const MCDwarfFrameInfo &frame) {
1332bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola  MCContext &context = streamer.getContext();
1333bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola  MCSymbol *fdeStart = context.CreateTempSymbol();
1334bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola  MCSymbol *fdeEnd = context.CreateTempSymbol();
1335e76a33b9567d78a5744dc52fcec3a6056d6fb576Evan Cheng  const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
13362541c41f3e2af94585e14353a91f02facd65e415Bill Wendling  bool verboseAsm = streamer.isVerboseAsm();
133789b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
13385fbe5e783ee0c5ae27c17490a752d7e603e84ed2Evan Cheng  if (IsEH && frame.Function && !MOFI->isFunctionEHFrameSymbolPrivate()) {
13392541c41f3e2af94585e14353a91f02facd65e415Bill Wendling    MCSymbol *EHSym =
13402541c41f3e2af94585e14353a91f02facd65e415Bill Wendling      context.GetOrCreateSymbol(frame.Function->getName() + Twine(".eh"));
13418bca4106dfc2945723251db10e340183f3e372ddRafael Espindola    streamer.EmitEHSymAttributes(frame.Function, EHSym);
1342a8cfb87fa2176fae2d62785a0dd606e4143cefaeRafael Espindola    streamer.EmitLabel(EHSym);
1343a8cfb87fa2176fae2d62785a0dd606e4143cefaeRafael Espindola  }
1344a8cfb87fa2176fae2d62785a0dd606e4143cefaeRafael Espindola
134589b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  // Length
134689b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  const MCExpr *Length = MakeStartMinusEndExpr(streamer, *fdeStart, *fdeEnd, 0);
13472541c41f3e2af94585e14353a91f02facd65e415Bill Wendling  if (verboseAsm) streamer.AddComment("FDE Length");
13489266cc400ee2fc86abb24ab0819d9da280fb61c4Rafael Espindola  streamer.EmitAbsValue(Length, 4);
134989b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
135089b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  streamer.EmitLabel(fdeStart);
1351e3a0e987f3d4f07512cdb64b9034369f966cb448Rafael Espindola
135289b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  // CIE Pointer
1353e3a0e987f3d4f07512cdb64b9034369f966cb448Rafael Espindola  const MCAsmInfo &asmInfo = context.getAsmInfo();
13540d450dc65906a30beb56aeb1ee22b45b1cd4596cRafael Espindola  if (IsEH) {
13550d450dc65906a30beb56aeb1ee22b45b1cd4596cRafael Espindola    const MCExpr *offset = MakeStartMinusEndExpr(streamer, cieStart, *fdeStart,
13560d450dc65906a30beb56aeb1ee22b45b1cd4596cRafael Espindola                                                 0);
13572541c41f3e2af94585e14353a91f02facd65e415Bill Wendling    if (verboseAsm) streamer.AddComment("FDE CIE Offset");
13580d450dc65906a30beb56aeb1ee22b45b1cd4596cRafael Espindola    streamer.EmitAbsValue(offset, 4);
13592241e51406f7bae369d6103cf3464e70f74c4af9Rafael Espindola  } else if (!asmInfo.doesDwarfUseRelocationsAcrossSections()) {
1360e3a0e987f3d4f07512cdb64b9034369f966cb448Rafael Espindola    const MCExpr *offset = MakeStartMinusEndExpr(streamer, *SectionStart,
1361e3a0e987f3d4f07512cdb64b9034369f966cb448Rafael Espindola                                                 cieStart, 0);
1362e3a0e987f3d4f07512cdb64b9034369f966cb448Rafael Espindola    streamer.EmitAbsValue(offset, 4);
13630d450dc65906a30beb56aeb1ee22b45b1cd4596cRafael Espindola  } else {
13640d450dc65906a30beb56aeb1ee22b45b1cd4596cRafael Espindola    streamer.EmitSymbolValue(&cieStart, 4);
13650d450dc65906a30beb56aeb1ee22b45b1cd4596cRafael Espindola  }
13662541c41f3e2af94585e14353a91f02facd65e415Bill Wendling
136789b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  // PC Begin
13680c5602de8c0dfa716f54306a1c540b98ab803584Nick Lewycky  unsigned PCEncoding = IsEH ? MOFI->getFDEEncoding(UsingCFI)
13690c5602de8c0dfa716f54306a1c540b98ab803584Nick Lewycky                             : (unsigned)dwarf::DW_EH_PE_absptr;
13700c5602de8c0dfa716f54306a1c540b98ab803584Nick Lewycky  unsigned PCSize = getSizeForEncoding(streamer, PCEncoding);
13710c5602de8c0dfa716f54306a1c540b98ab803584Nick Lewycky  EmitSymbol(streamer, *frame.Begin, PCEncoding, "FDE initial location");
137289b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
137389b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  // PC Range
137489b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  const MCExpr *Range = MakeStartMinusEndExpr(streamer, *frame.Begin,
137589b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola                                              *frame.End, 0);
13762541c41f3e2af94585e14353a91f02facd65e415Bill Wendling  if (verboseAsm) streamer.AddComment("FDE address range");
13770c5602de8c0dfa716f54306a1c540b98ab803584Nick Lewycky  streamer.EmitAbsValue(Range, PCSize);
137889b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
137940a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola  if (IsEH) {
138040a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    // Augmentation Data Length
138140a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    unsigned augmentationLength = 0;
13821f6c87554a0d78103b95816fd2c1ce392b366bcfRafael Espindola
138340a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    if (frame.Lsda)
138440a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola      augmentationLength += getSizeForEncoding(streamer, frame.LsdaEncoding);
13851f6c87554a0d78103b95816fd2c1ce392b366bcfRafael Espindola
13862541c41f3e2af94585e14353a91f02facd65e415Bill Wendling    if (verboseAsm) streamer.AddComment("Augmentation size");
138740a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    streamer.EmitULEB128IntValue(augmentationLength);
138889b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
138940a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    // Augmentation Data
139040a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    if (frame.Lsda)
13912541c41f3e2af94585e14353a91f02facd65e415Bill Wendling      EmitSymbol(streamer, *frame.Lsda, frame.LsdaEncoding,
13922541c41f3e2af94585e14353a91f02facd65e415Bill Wendling                 "Language Specific Data Area");
139340a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola  }
13944892dff07e7d99eb37aee868c4f2c91e9c7eba14Rafael Espindola
139589b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  // Call Frame Instructions
139689b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
1397b40a71fda188f8ca564e606ac2cb051a44ada311Rafael Espindola  EmitCFIInstructions(streamer, frame.Instructions, frame.Begin);
13985bba08425374ca36fe5fbc7423ce1a09858e4097Rafael Espindola
139989b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  // Padding
14000c5602de8c0dfa716f54306a1c540b98ab803584Nick Lewycky  streamer.EmitValueToAlignment(PCSize);
1401dfe125cc9cfd524575973daa297c9aad10470390Rafael Espindola
1402dfe125cc9cfd524575973daa297c9aad10470390Rafael Espindola  return fdeEnd;
140389b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola}
140489b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
140519282365147f498a60463d250dbed2f8e1b81861Benjamin Kramernamespace {
140619282365147f498a60463d250dbed2f8e1b81861Benjamin Kramer  struct CIEKey {
140716d7d437e03ce87fdaef7971919302920d54a966Rafael Espindola    static const CIEKey getEmptyKey() { return CIEKey(0, 0, -1, false); }
140816d7d437e03ce87fdaef7971919302920d54a966Rafael Espindola    static const CIEKey getTombstoneKey() { return CIEKey(0, -1, 0, false); }
140919282365147f498a60463d250dbed2f8e1b81861Benjamin Kramer
141019282365147f498a60463d250dbed2f8e1b81861Benjamin Kramer    CIEKey(const MCSymbol* Personality_, unsigned PersonalityEncoding_,
141116d7d437e03ce87fdaef7971919302920d54a966Rafael Espindola           unsigned LsdaEncoding_, bool IsSignalFrame_) :
141216d7d437e03ce87fdaef7971919302920d54a966Rafael Espindola      Personality(Personality_), PersonalityEncoding(PersonalityEncoding_),
141316d7d437e03ce87fdaef7971919302920d54a966Rafael Espindola      LsdaEncoding(LsdaEncoding_), IsSignalFrame(IsSignalFrame_) {
141419282365147f498a60463d250dbed2f8e1b81861Benjamin Kramer    }
141519282365147f498a60463d250dbed2f8e1b81861Benjamin Kramer    const MCSymbol* Personality;
141619282365147f498a60463d250dbed2f8e1b81861Benjamin Kramer    unsigned PersonalityEncoding;
141719282365147f498a60463d250dbed2f8e1b81861Benjamin Kramer    unsigned LsdaEncoding;
141816d7d437e03ce87fdaef7971919302920d54a966Rafael Espindola    bool IsSignalFrame;
141919282365147f498a60463d250dbed2f8e1b81861Benjamin Kramer  };
142019282365147f498a60463d250dbed2f8e1b81861Benjamin Kramer}
1421bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola
1422bdc3167c086dd4358e24692075db5e7784140843Rafael Espindolanamespace llvm {
1423bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola  template <>
1424bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola  struct DenseMapInfo<CIEKey> {
1425bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola    static CIEKey getEmptyKey() {
142619282365147f498a60463d250dbed2f8e1b81861Benjamin Kramer      return CIEKey::getEmptyKey();
1427bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola    }
1428bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola    static CIEKey getTombstoneKey() {
142919282365147f498a60463d250dbed2f8e1b81861Benjamin Kramer      return CIEKey::getTombstoneKey();
1430bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola    }
1431bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola    static unsigned getHashValue(const CIEKey &Key) {
14327484920cf5d95b21fa750229f8ce0d1624c918ecBenjamin Kramer      return static_cast<unsigned>(hash_combine(Key.Personality,
14337484920cf5d95b21fa750229f8ce0d1624c918ecBenjamin Kramer                                                Key.PersonalityEncoding,
14347484920cf5d95b21fa750229f8ce0d1624c918ecBenjamin Kramer                                                Key.LsdaEncoding,
14357484920cf5d95b21fa750229f8ce0d1624c918ecBenjamin Kramer                                                Key.IsSignalFrame));
1436bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola    }
1437bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola    static bool isEqual(const CIEKey &LHS,
1438bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola                        const CIEKey &RHS) {
1439bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola      return LHS.Personality == RHS.Personality &&
1440bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola        LHS.PersonalityEncoding == RHS.PersonalityEncoding &&
144116d7d437e03ce87fdaef7971919302920d54a966Rafael Espindola        LHS.LsdaEncoding == RHS.LsdaEncoding &&
144216d7d437e03ce87fdaef7971919302920d54a966Rafael Espindola        LHS.IsSignalFrame == RHS.IsSignalFrame;
1443bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola    }
1444bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola  };
1445bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola}
1446bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola
1447a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendlingvoid MCDwarfFrameEmitter::Emit(MCStreamer &Streamer,
1448a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling                               bool UsingCFI,
1449a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling                               bool IsEH) {
1450a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling  MCContext &Context = Streamer.getContext();
1451e76a33b9567d78a5744dc52fcec3a6056d6fb576Evan Cheng  MCObjectFileInfo *MOFI =
1452e76a33b9567d78a5744dc52fcec3a6056d6fb576Evan Cheng    const_cast<MCObjectFileInfo*>(Context.getObjectFileInfo());
14531424c3c3e696868775ac5bfc57d27a0ac3bc6c63Bill Wendling  FrameEmitterImpl Emitter(UsingCFI, IsEH);
1454d8ab8e9707e6170218ce82516d15b36f54b4bc1cBill Wendling  ArrayRef<MCDwarfFrameInfo> FrameArray = Streamer.getFrameInfos();
14551424c3c3e696868775ac5bfc57d27a0ac3bc6c63Bill Wendling
1456d8ab8e9707e6170218ce82516d15b36f54b4bc1cBill Wendling  // Emit the compact unwind info if available.
14577a13b72bbbadaa9274f6a58774970b9b95706330Bill Wendling  if (IsEH && MOFI->getCompactUnwindSection())
14581424c3c3e696868775ac5bfc57d27a0ac3bc6c63Bill Wendling    for (unsigned i = 0, n = Streamer.getNumFrameInfos(); i < n; ++i) {
14591424c3c3e696868775ac5bfc57d27a0ac3bc6c63Bill Wendling      const MCDwarfFrameInfo &Frame = Streamer.getFrameInfo(i);
1460a2ff3e2c3c0d8be862525c91f7b0e153480f4f5eBill Wendling      if (Frame.CompactUnwindEncoding)
1461d8ab8e9707e6170218ce82516d15b36f54b4bc1cBill Wendling        Emitter.EmitCompactUnwind(Streamer, Frame);
14621424c3c3e696868775ac5bfc57d27a0ac3bc6c63Bill Wendling    }
14631424c3c3e696868775ac5bfc57d27a0ac3bc6c63Bill Wendling
1464e76a33b9567d78a5744dc52fcec3a6056d6fb576Evan Cheng  const MCSection &Section = IsEH ? *MOFI->getEHFrameSection() :
1465e76a33b9567d78a5744dc52fcec3a6056d6fb576Evan Cheng                                    *MOFI->getDwarfFrameSection();
1466a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling  Streamer.SwitchSection(&Section);
1467a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling  MCSymbol *SectionStart = Context.CreateTempSymbol();
1468a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling  Streamer.EmitLabel(SectionStart);
14691424c3c3e696868775ac5bfc57d27a0ac3bc6c63Bill Wendling  Emitter.setSectionStart(SectionStart);
1470a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling
1471a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling  MCSymbol *FDEEnd = NULL;
1472bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola  DenseMap<CIEKey, const MCSymbol*> CIEStarts;
1473d7c8ccae8e48dce3ab7c3e9b4d8a309998c47961Rafael Espindola
147440a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola  const MCSymbol *DummyDebugKey = NULL;
14751424c3c3e696868775ac5bfc57d27a0ac3bc6c63Bill Wendling  for (unsigned i = 0, n = FrameArray.size(); i < n; ++i) {
14761424c3c3e696868775ac5bfc57d27a0ac3bc6c63Bill Wendling    const MCDwarfFrameInfo &Frame = FrameArray[i];
1477a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling    CIEKey Key(Frame.Personality, Frame.PersonalityEncoding,
147816d7d437e03ce87fdaef7971919302920d54a966Rafael Espindola               Frame.LsdaEncoding, Frame.IsSignalFrame);
1479a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling    const MCSymbol *&CIEStart = IsEH ? CIEStarts[Key] : DummyDebugKey;
1480a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling    if (!CIEStart)
1481a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling      CIEStart = &Emitter.EmitCIE(Streamer, Frame.Personality,
1482a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling                                  Frame.PersonalityEncoding, Frame.Lsda,
148316d7d437e03ce87fdaef7971919302920d54a966Rafael Espindola                                  Frame.IsSignalFrame,
1484a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling                                  Frame.LsdaEncoding);
1485e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling
1486a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling    FDEEnd = Emitter.EmitFDE(Streamer, *CIEStart, Frame);
1487e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling
1488dfe125cc9cfd524575973daa297c9aad10470390Rafael Espindola    if (i != n - 1)
1489a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling      Streamer.EmitLabel(FDEEnd);
1490dfe125cc9cfd524575973daa297c9aad10470390Rafael Espindola  }
1491d7c8ccae8e48dce3ab7c3e9b4d8a309998c47961Rafael Espindola
14921be0e271a07925b928ba89848934f1ea6f1854e2Evan Cheng  Streamer.EmitValueToAlignment(Context.getAsmInfo().getPointerSize());
1493a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling  if (FDEEnd)
1494a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling    Streamer.EmitLabel(FDEEnd);
149589b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola}
1496245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola
1497245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindolavoid MCDwarfFrameEmitter::EmitAdvanceLoc(MCStreamer &Streamer,
1498245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola                                         uint64_t AddrDelta) {
1499245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola  SmallString<256> Tmp;
1500245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola  raw_svector_ostream OS(Tmp);
15014eafe109459eb115f13f1d19c5ff3cb3678e8c7aRafael Espindola  MCDwarfFrameEmitter::EncodeAdvanceLoc(AddrDelta, OS);
150268ca56285f9b6e82eb16ff8ea02a301f2c489faeEric Christopher  Streamer.EmitBytes(OS.str());
1503245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola}
1504245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola
1505245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindolavoid MCDwarfFrameEmitter::EncodeAdvanceLoc(uint64_t AddrDelta,
15064eafe109459eb115f13f1d19c5ff3cb3678e8c7aRafael Espindola                                           raw_ostream &OS) {
1507245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola  // FIXME: Assumes the code alignment factor is 1.
15083b78cdc57ae25ff021fd67171a7c82d533477a19Rafael Espindola  if (AddrDelta == 0) {
15094eafe109459eb115f13f1d19c5ff3cb3678e8c7aRafael Espindola  } else if (isUIntN(6, AddrDelta)) {
1510245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola    uint8_t Opcode = dwarf::DW_CFA_advance_loc | AddrDelta;
1511245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola    OS << Opcode;
15124eafe109459eb115f13f1d19c5ff3cb3678e8c7aRafael Espindola  } else if (isUInt<8>(AddrDelta)) {
1513245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola    OS << uint8_t(dwarf::DW_CFA_advance_loc1);
1514245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola    OS << uint8_t(AddrDelta);
15154eafe109459eb115f13f1d19c5ff3cb3678e8c7aRafael Espindola  } else if (isUInt<16>(AddrDelta)) {
1516a7e450574c45209ca0b05ff715f9e7dddcbd936cRafael Espindola    // FIXME: check what is the correct behavior on a big endian machine.
1517245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola    OS << uint8_t(dwarf::DW_CFA_advance_loc2);
1518a7e450574c45209ca0b05ff715f9e7dddcbd936cRafael Espindola    OS << uint8_t( AddrDelta       & 0xff);
1519a7e450574c45209ca0b05ff715f9e7dddcbd936cRafael Espindola    OS << uint8_t((AddrDelta >> 8) & 0xff);
1520245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola  } else {
1521a7e450574c45209ca0b05ff715f9e7dddcbd936cRafael Espindola    // FIXME: check what is the correct behavior on a big endian machine.
1522245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola    assert(isUInt<32>(AddrDelta));
1523245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola    OS << uint8_t(dwarf::DW_CFA_advance_loc4);
1524a7e450574c45209ca0b05ff715f9e7dddcbd936cRafael Espindola    OS << uint8_t( AddrDelta        & 0xff);
1525a7e450574c45209ca0b05ff715f9e7dddcbd936cRafael Espindola    OS << uint8_t((AddrDelta >> 8)  & 0xff);
1526a7e450574c45209ca0b05ff715f9e7dddcbd936cRafael Espindola    OS << uint8_t((AddrDelta >> 16) & 0xff);
1527a7e450574c45209ca0b05ff715f9e7dddcbd936cRafael Espindola    OS << uint8_t((AddrDelta >> 24) & 0xff);
1528a7e450574c45209ca0b05ff715f9e7dddcbd936cRafael Espindola
1529245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola  }
1530245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola}
1531