MCDwarf.cpp revision 2d39a0e52df9ce050bd4e2de3a2ecca8fd9a87c3
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"
11e76a33b9567d78a5744dc52fcec3a6056d6fb576Evan Cheng#include "llvm/MC/MCAsmInfo.h"
12e76a33b9567d78a5744dc52fcec3a6056d6fb576Evan Cheng#include "llvm/MC/MCContext.h"
13e76a33b9567d78a5744dc52fcec3a6056d6fb576Evan Cheng#include "llvm/MC/MCObjectFileInfo.h"
14e76a33b9567d78a5744dc52fcec3a6056d6fb576Evan Cheng#include "llvm/MC/MCObjectWriter.h"
15e76a33b9567d78a5744dc52fcec3a6056d6fb576Evan Cheng#include "llvm/MC/MCRegisterInfo.h"
16ad8aaa069cfdb3bdc32b1becc8881f67b5272e14Rafael Espindola#include "llvm/MC/MCStreamer.h"
17c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby#include "llvm/MC/MCSymbol.h"
18c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby#include "llvm/MC/MCExpr.h"
197cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby#include "llvm/Support/Debug.h"
20fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola#include "llvm/Support/ErrorHandling.h"
217cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby#include "llvm/Support/raw_ostream.h"
222d39a0e52df9ce050bd4e2de3a2ecca8fd9a87c3Jim Grosbach#include "llvm/Support/LEB128.h"
2394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby#include "llvm/Support/Path.h"
2494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby#include "llvm/Support/SourceMgr.h"
257484920cf5d95b21fa750229f8ce0d1624c918ecBenjamin Kramer#include "llvm/ADT/Hashing.h"
262b2dc7c4bee01071410df0e3027117b47ce48cedBill Wendling#include "llvm/ADT/SmallString.h"
272b2dc7c4bee01071410df0e3027117b47ce48cedBill Wendling#include "llvm/ADT/Twine.h"
2894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby#include "llvm/Config/config.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++) {
272c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    MCOS->EmitBytes(MCDwarfDirs[i], 0); // the DirectoryName
273c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    MCOS->EmitBytes(StringRef("\0", 1), 0); // 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++) {
281c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    MCOS->EmitBytes(MCDwarfFiles[i]->getName(), 0); // FileName
282c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    MCOS->EmitBytes(StringRef("\0", 1), 0); // 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);
345c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitBytes(OS.str(), /*AddrSpace=*/0);
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
4287cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderbyvoid MCDwarfFile::dump() const {
4297cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby  print(dbgs());
4307cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby}
431c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
43294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby// Utility function to write a tuple for .debug_abbrev.
43394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderbystatic void EmitAbbrev(MCStreamer *MCOS, uint64_t Name, uint64_t Form) {
43494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitULEB128IntValue(Name);
43594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitULEB128IntValue(Form);
43694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby}
43794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
43894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby// When generating dwarf for assembly source files this emits
43994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby// the data for .debug_abbrev section which contains three DIEs.
44094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderbystatic void EmitGenDwarfAbbrev(MCStreamer *MCOS) {
44194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCContext &context = MCOS->getContext();
44294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
44394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
44494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // DW_TAG_compile_unit DIE abbrev (1).
44594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitULEB128IntValue(1);
44694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitULEB128IntValue(dwarf::DW_TAG_compile_unit);
44794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
44894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitAbbrev(MCOS, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4);
44994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
45094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitAbbrev(MCOS, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr);
45194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
45294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitAbbrev(MCOS, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string);
45394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
45494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  if (!DwarfDebugFlags.empty())
45594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    EmitAbbrev(MCOS, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string);
45694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitAbbrev(MCOS, dwarf::DW_AT_producer, dwarf::DW_FORM_string);
45794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitAbbrev(MCOS, dwarf::DW_AT_language, dwarf::DW_FORM_data2);
45894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitAbbrev(MCOS, 0, 0);
45994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
46038fdb7d9fc40e9f29c3156b6625cac8d91d562e1Kevin Enderby  // DW_TAG_label DIE abbrev (2).
46194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitULEB128IntValue(2);
46238fdb7d9fc40e9f29c3156b6625cac8d91d562e1Kevin Enderby  MCOS->EmitULEB128IntValue(dwarf::DW_TAG_label);
46394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
46494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
46594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitAbbrev(MCOS, dwarf::DW_AT_decl_file, dwarf::DW_FORM_data4);
46694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitAbbrev(MCOS, dwarf::DW_AT_decl_line, dwarf::DW_FORM_data4);
46794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
46894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitAbbrev(MCOS, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag);
46994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitAbbrev(MCOS, 0, 0);
47094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
47194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // DW_TAG_unspecified_parameters DIE abbrev (3).
47294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitULEB128IntValue(3);
47394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitULEB128IntValue(dwarf::DW_TAG_unspecified_parameters);
47494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(dwarf::DW_CHILDREN_no, 1);
47594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitAbbrev(MCOS, 0, 0);
47694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
47794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Terminate the abbreviations for this compilation unit.
47894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(0, 1);
47994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby}
48094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
48194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby// When generating dwarf for assembly source files this emits the data for
48294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby// .debug_aranges section.  Which contains a header and a table of pairs of
48394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby// PointerSize'ed values for the address and size of section(s) with line table
48494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby// entries (just the default .text in our case) and a terminating pair of zeros.
48594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderbystatic void EmitGenDwarfAranges(MCStreamer *MCOS) {
48694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCContext &context = MCOS->getContext();
48794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
48894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Create a symbol at the end of the section that we are creating the dwarf
48994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // debugging info to use later in here as part of the expression to calculate
49094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // the size of the section for the table.
49194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->SwitchSection(context.getGenDwarfSection());
49294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCSymbol *SectionEndSym = context.CreateTempSymbol();
49394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitLabel(SectionEndSym);
49494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  context.setGenDwarfSectionEndSym(SectionEndSym);
49594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
49694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
49794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
49894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // This will be the length of the .debug_aranges section, first account for
49994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // the size of each item in the header (see below where we emit these items).
50094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  int Length = 4 + 2 + 4 + 1 + 1;
50194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
50294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Figure the padding after the header before the table of address and size
50394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // pairs who's values are PointerSize'ed.
50494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  const MCAsmInfo &asmInfo = context.getAsmInfo();
50594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  int AddrSize = asmInfo.getPointerSize();
50694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  int Pad = 2 * AddrSize - (Length & (2 * AddrSize - 1));
50794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  if (Pad == 2 * AddrSize)
50894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    Pad = 0;
50994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  Length += Pad;
51094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
51194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Add the size of the pair of PointerSize'ed values for the address and size
51294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // of the one default .text section we have in the table.
51394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  Length += 2 * AddrSize;
51494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // And the pair of terminating zeros.
51594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  Length += 2 * AddrSize;
51694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
51794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
51894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Emit the header for this section.
51994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // The 4 byte length not including the 4 byte value for the length.
52094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(Length - 4, 4);
52194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // The 2 byte version, which is 2.
52294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(2, 2);
52394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // The 4 byte offset to the compile unit in the .debug_info from the start
52494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // of the .debug_info, it is at the start of that section so this is zero.
52594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(0, 4);
52694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // The 1 byte size of an address.
52794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(AddrSize, 1);
52894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // The 1 byte size of a segment descriptor, we use a value of zero.
52994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(0, 1);
53094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Align the header with the padding if needed, before we put out the table.
53194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  for(int i = 0; i < Pad; i++)
53294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    MCOS->EmitIntValue(0, 1);
53394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
53494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Now emit the table of pairs of PointerSize'ed values for the section(s)
53594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // address and size, in our case just the one default .text section.
53694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  const MCExpr *Addr = MCSymbolRefExpr::Create(
53794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    context.getGenDwarfSectionStartSym(), MCSymbolRefExpr::VK_None, context);
53894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  const MCExpr *Size = MakeStartMinusEndExpr(*MCOS,
53994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    *context.getGenDwarfSectionStartSym(), *SectionEndSym, 0);
54094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitAbsValue(Addr, AddrSize);
54194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitAbsValue(Size, AddrSize);
54294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
54394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // And finally the pair of terminating zeros.
54494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(0, AddrSize);
54594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(0, AddrSize);
54694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby}
54794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
54894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby// When generating dwarf for assembly source files this emits the data for
54994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby// .debug_info section which contains three parts.  The header, the compile_unit
55038fdb7d9fc40e9f29c3156b6625cac8d91d562e1Kevin Enderby// DIE and a list of label DIEs.
551489d67927172941bf59b9f4829ab8910814fea24Rafael Espindolastatic void EmitGenDwarfInfo(MCStreamer *MCOS,
552489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola                             const MCSymbol *AbbrevSectionSymbol,
553489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola                             const MCSymbol *LineSectionSymbol) {
55494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCContext &context = MCOS->getContext();
55594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
5562684d9e3c702b2ef9fd430155d94671d12fa994fJim Grosbach  MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
55794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
55894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Create a symbol at the start and end of this section used in here for the
55994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // expression to calculate the length in the header.
56094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCSymbol *InfoStart = context.CreateTempSymbol();
56194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitLabel(InfoStart);
56294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCSymbol *InfoEnd = context.CreateTempSymbol();
56394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
56494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // First part: the header.
56594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
56694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // The 4 byte total length of the information for this compilation unit, not
56794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // including these 4 bytes.
56894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  const MCExpr *Length = MakeStartMinusEndExpr(*MCOS, *InfoStart, *InfoEnd, 4);
56994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitAbsValue(Length, 4);
57094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
57194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // The 2 byte DWARF version, which is 2.
57294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(2, 2);
57394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
57494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // The 4 byte offset to the debug abbrevs from the start of the .debug_abbrev,
57594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // it is at the start of that section so this is zero.
576489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola  if (AbbrevSectionSymbol) {
577489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola    MCOS->EmitSymbolValue(AbbrevSectionSymbol, 4);
578489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola  } else {
579489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola    MCOS->EmitIntValue(0, 4);
580489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola  }
58194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
58294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  const MCAsmInfo &asmInfo = context.getAsmInfo();
58394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  int AddrSize = asmInfo.getPointerSize();
58494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // The 1 byte size of an address.
58594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(AddrSize, 1);
58694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
58794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Second part: the compile_unit DIE.
58894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
58994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // The DW_TAG_compile_unit DIE abbrev (1).
59094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitULEB128IntValue(1);
59194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
59294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // DW_AT_stmt_list, a 4 byte offset from the start of the .debug_line section,
59394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // which is at the start of that section so this is zero.
594489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola  if (LineSectionSymbol) {
595489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola    MCOS->EmitSymbolValue(LineSectionSymbol, 4);
596489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola  } else {
597489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola    MCOS->EmitIntValue(0, 4);
598489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola  }
59994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
60094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // AT_low_pc, the first address of the default .text section.
60194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  const MCExpr *Start = MCSymbolRefExpr::Create(
60294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    context.getGenDwarfSectionStartSym(), MCSymbolRefExpr::VK_None, context);
60394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitAbsValue(Start, AddrSize);
60494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
60594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // AT_high_pc, the last address of the default .text section.
60694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  const MCExpr *End = MCSymbolRefExpr::Create(
60794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    context.getGenDwarfSectionEndSym(), MCSymbolRefExpr::VK_None, context);
60894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitAbsValue(End, AddrSize);
60994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
61094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // AT_name, the name of the source file.  Reconstruct from the first directory
61194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // and file table entries.
61294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  const std::vector<StringRef> &MCDwarfDirs =
61394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    context.getMCDwarfDirs();
61494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  if (MCDwarfDirs.size() > 0) {
61594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    MCOS->EmitBytes(MCDwarfDirs[0], 0);
61694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    MCOS->EmitBytes("/", 0);
61794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  }
61894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  const std::vector<MCDwarfFile *> &MCDwarfFiles =
61994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    MCOS->getContext().getMCDwarfFiles();
62094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitBytes(MCDwarfFiles[1]->getName(), 0);
62194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
62294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
62394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // AT_comp_dir, the working directory the assembly was done in.
62494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  llvm::sys::Path CWD = llvm::sys::Path::GetCurrentDirectory();
62594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitBytes(StringRef(CWD.c_str()), 0);
62694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
62794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
62894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // AT_APPLE_flags, the command line arguments of the assembler tool.
62994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
63094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  if (!DwarfDebugFlags.empty()){
63194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    MCOS->EmitBytes(DwarfDebugFlags, 0);
63294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
63394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  }
63494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
63594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // AT_producer, the version of the assembler tool.
63694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitBytes(StringRef("llvm-mc (based on LLVM "), 0);
63794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitBytes(StringRef(PACKAGE_VERSION), 0);
63894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitBytes(StringRef(")"), 0);
63994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
64094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
64194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // AT_language, a 4 byte value.  We use DW_LANG_Mips_Assembler as the dwarf2
64294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // draft has no standard code for assembler.
64394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(dwarf::DW_LANG_Mips_Assembler, 2);
64494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
64538fdb7d9fc40e9f29c3156b6625cac8d91d562e1Kevin Enderby  // Third part: the list of label DIEs.
64694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
64711c2defa9157bd589cb322218c718c4492ed5746Kevin Enderby  // Loop on saved info for dwarf labels and create the DIEs for them.
64811c2defa9157bd589cb322218c718c4492ed5746Kevin Enderby  const std::vector<const MCGenDwarfLabelEntry *> &Entries =
64911c2defa9157bd589cb322218c718c4492ed5746Kevin Enderby    MCOS->getContext().getMCGenDwarfLabelEntries();
65011c2defa9157bd589cb322218c718c4492ed5746Kevin Enderby  for (std::vector<const MCGenDwarfLabelEntry *>::const_iterator it =
65194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby       Entries.begin(), ie = Entries.end(); it != ie;
65294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby       ++it) {
65311c2defa9157bd589cb322218c718c4492ed5746Kevin Enderby    const MCGenDwarfLabelEntry *Entry = *it;
65494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
65538fdb7d9fc40e9f29c3156b6625cac8d91d562e1Kevin Enderby    // The DW_TAG_label DIE abbrev (2).
65694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    MCOS->EmitULEB128IntValue(2);
65794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
65894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    // AT_name, of the label without any leading underbar.
65994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    MCOS->EmitBytes(Entry->getName(), 0);
66094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
66194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
66294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    // AT_decl_file, index into the file table.
66394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    MCOS->EmitIntValue(Entry->getFileNumber(), 4);
66494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
66594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    // AT_decl_line, source line number.
66694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    MCOS->EmitIntValue(Entry->getLineNumber(), 4);
66794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
66894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    // AT_low_pc, start address of the label.
66994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    const MCExpr *AT_low_pc = MCSymbolRefExpr::Create(Entry->getLabel(),
67094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby                                             MCSymbolRefExpr::VK_None, context);
67194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    MCOS->EmitAbsValue(AT_low_pc, AddrSize);
67294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
67394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    // DW_AT_prototyped, a one byte flag value of 0 saying we have no prototype.
67494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    MCOS->EmitIntValue(0, 1);
67594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
67694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    // The DW_TAG_unspecified_parameters DIE abbrev (3).
67794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    MCOS->EmitULEB128IntValue(3);
67894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
67994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    // Add the NULL DIE terminating the DW_TAG_unspecified_parameters DIE's.
68094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    MCOS->EmitIntValue(0, 1);
68194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  }
68211c2defa9157bd589cb322218c718c4492ed5746Kevin Enderby  // Deallocate the MCGenDwarfLabelEntry classes that saved away the info
68311c2defa9157bd589cb322218c718c4492ed5746Kevin Enderby  // for the dwarf labels.
68411c2defa9157bd589cb322218c718c4492ed5746Kevin Enderby  for (std::vector<const MCGenDwarfLabelEntry *>::const_iterator it =
68594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby       Entries.begin(), ie = Entries.end(); it != ie;
68694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby       ++it) {
68711c2defa9157bd589cb322218c718c4492ed5746Kevin Enderby    const MCGenDwarfLabelEntry *Entry = *it;
68894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    delete Entry;
68994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  }
69094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
69194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Add the NULL DIE terminating the Compile Unit DIE's.
69294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitIntValue(0, 1);
69394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
69494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Now set the value of the symbol at the end of the info section.
69594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitLabel(InfoEnd);
69694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby}
69794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
69894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby//
69994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby// When generating dwarf for assembly source files this emits the Dwarf
70094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby// sections.
70194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby//
702489d67927172941bf59b9f4829ab8910814fea24Rafael Espindolavoid MCGenDwarfInfo::Emit(MCStreamer *MCOS, const MCSymbol *LineSectionSymbol) {
70394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Create the dwarf sections in this order (.debug_line already created).
70494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCContext &context = MCOS->getContext();
705489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola  const MCAsmInfo &AsmInfo = context.getAsmInfo();
70694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
70794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
708489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola  MCSymbol *AbbrevSectionSymbol;
7092241e51406f7bae369d6103cf3464e70f74c4af9Rafael Espindola  if (AsmInfo.doesDwarfUseRelocationsAcrossSections()) {
710489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola    AbbrevSectionSymbol = context.CreateTempSymbol();
711489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola    MCOS->EmitLabel(AbbrevSectionSymbol);
712489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola  } else {
713489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola    AbbrevSectionSymbol = NULL;
714489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola    LineSectionSymbol = NULL;
715489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola  }
71694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
71794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
71894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // If there are no line table entries then do not emit any section contents.
71994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  if (context.getMCLineSections().empty())
72094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    return;
72194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
72294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Output the data for .debug_aranges section.
72394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitGenDwarfAranges(MCOS);
72494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
72594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Output the data for .debug_abbrev section.
72694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  EmitGenDwarfAbbrev(MCOS);
72794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
72894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Output the data for .debug_info section.
729489d67927172941bf59b9f4829ab8910814fea24Rafael Espindola  EmitGenDwarfInfo(MCOS, AbbrevSectionSymbol, LineSectionSymbol);
73094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby}
73194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
73294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby//
73394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby// When generating dwarf for assembly source files this is called when symbol
73494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby// for a label is created.  If this symbol is not a temporary and is in the
73594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby// section that dwarf is being generated for, save the needed info to create
73611c2defa9157bd589cb322218c718c4492ed5746Kevin Enderby// a dwarf label.
73794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby//
73811c2defa9157bd589cb322218c718c4492ed5746Kevin Enderbyvoid MCGenDwarfLabelEntry::Make(MCSymbol *Symbol, MCStreamer *MCOS,
73994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby                                     SourceMgr &SrcMgr, SMLoc &Loc) {
7403c3cdcc270515de95a4982db3073b9fdf44a5c2eEric Christopher  // We won't create dwarf labels for temporary symbols or symbols not in
74194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // the default text.
74294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  if (Symbol->isTemporary())
74394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    return;
74494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCContext &context = MCOS->getContext();
74594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  if (context.getGenDwarfSection() != MCOS->getCurrentSection())
74694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    return;
74794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
74811c2defa9157bd589cb322218c718c4492ed5746Kevin Enderby  // The dwarf label's name does not have the symbol name's leading
74994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // underbar if any.
75094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  StringRef Name = Symbol->getName();
75194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  if (Name.startswith("_"))
75294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby    Name = Name.substr(1, Name.size()-1);
75394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
75411c2defa9157bd589cb322218c718c4492ed5746Kevin Enderby  // Get the dwarf file number to be used for the dwarf label.
75594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  unsigned FileNumber = context.getGenDwarfFileNumber();
75694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
75794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Finding the line number is the expensive part which is why we just don't
75811c2defa9157bd589cb322218c718c4492ed5746Kevin Enderby  // pass it in as for some symbols we won't create a dwarf label.
75994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  int CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
76094c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  unsigned LineNumber = SrcMgr.FindLineNumber(Loc, CurBuffer);
76194c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
76294c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // We create a temporary symbol for use for the AT_high_pc and AT_low_pc
76394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // values so that they don't have things like an ARM thumb bit from the
76494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // original symbol. So when used they won't get a low bit set after
76594c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // relocation.
76694c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCSymbol *Label = context.CreateTempSymbol();
76794c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  MCOS->EmitLabel(Label);
76894c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
76994c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby  // Create and entry for the info and add it to the other entries.
7702684d9e3c702b2ef9fd430155d94671d12fa994fJim Grosbach  MCGenDwarfLabelEntry *Entry =
77111c2defa9157bd589cb322218c718c4492ed5746Kevin Enderby    new MCGenDwarfLabelEntry(Name, FileNumber, LineNumber, Label);
77211c2defa9157bd589cb322218c718c4492ed5746Kevin Enderby  MCOS->getContext().addMCGenDwarfLabelEntry(Entry);
77394c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby}
77494c2e85bea1ab1b837a4c055ccc83d5cd32dd027Kevin Enderby
77589b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindolastatic int getDataAlignmentFactor(MCStreamer &streamer) {
77689b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  MCContext &context = streamer.getContext();
7771be0e271a07925b928ba89848934f1ea6f1854e2Evan Cheng  const MCAsmInfo &asmInfo = context.getAsmInfo();
77889b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  int size = asmInfo.getPointerSize();
7791be0e271a07925b928ba89848934f1ea6f1854e2Evan Cheng  if (asmInfo.isStackGrowthDirectionUp())
78089b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola    return size;
7811be0e271a07925b928ba89848934f1ea6f1854e2Evan Cheng  else
7821be0e271a07925b928ba89848934f1ea6f1854e2Evan Cheng    return -size;
78389b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola}
78489b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
785abf9af60add19a0f037bb2a05433769bfd1e7f6fRafael Espindolastatic unsigned getSizeForEncoding(MCStreamer &streamer,
786abf9af60add19a0f037bb2a05433769bfd1e7f6fRafael Espindola                                   unsigned symbolEncoding) {
78725f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  MCContext &context = streamer.getContext();
78825f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  unsigned format = symbolEncoding & 0x0f;
78925f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  switch (format) {
790858143816d43e58b17bfd11cb1b57afbd7f0f893Craig Topper  default: llvm_unreachable("Unknown Encoding");
79125f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  case dwarf::DW_EH_PE_absptr:
79225f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  case dwarf::DW_EH_PE_signed:
7931be0e271a07925b928ba89848934f1ea6f1854e2Evan Cheng    return context.getAsmInfo().getPointerSize();
79425f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  case dwarf::DW_EH_PE_udata2:
79525f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  case dwarf::DW_EH_PE_sdata2:
796abf9af60add19a0f037bb2a05433769bfd1e7f6fRafael Espindola    return 2;
79725f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  case dwarf::DW_EH_PE_udata4:
79825f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  case dwarf::DW_EH_PE_sdata4:
799abf9af60add19a0f037bb2a05433769bfd1e7f6fRafael Espindola    return 4;
80025f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  case dwarf::DW_EH_PE_udata8:
80125f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  case dwarf::DW_EH_PE_sdata8:
802abf9af60add19a0f037bb2a05433769bfd1e7f6fRafael Espindola    return 8;
80325f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  }
804abf9af60add19a0f037bb2a05433769bfd1e7f6fRafael Espindola}
805abf9af60add19a0f037bb2a05433769bfd1e7f6fRafael Espindola
806abf9af60add19a0f037bb2a05433769bfd1e7f6fRafael Espindolastatic void EmitSymbol(MCStreamer &streamer, const MCSymbol &symbol,
8072541c41f3e2af94585e14353a91f02facd65e415Bill Wendling                       unsigned symbolEncoding, const char *comment = 0) {
808a0057ca13f06b8de08483c3e3a143a7236c67097Rafael Espindola  MCContext &context = streamer.getContext();
809a0057ca13f06b8de08483c3e3a143a7236c67097Rafael Espindola  const MCAsmInfo &asmInfo = context.getAsmInfo();
810a0057ca13f06b8de08483c3e3a143a7236c67097Rafael Espindola  const MCExpr *v = asmInfo.getExprForFDESymbol(&symbol,
811debd7e4e8bc5cfe61bfb71835ce2b1a3fbccc2beRafael Espindola                                                symbolEncoding,
812a0057ca13f06b8de08483c3e3a143a7236c67097Rafael Espindola                                                streamer);
813abf9af60add19a0f037bb2a05433769bfd1e7f6fRafael Espindola  unsigned size = getSizeForEncoding(streamer, symbolEncoding);
8142541c41f3e2af94585e14353a91f02facd65e415Bill Wendling  if (streamer.isVerboseAsm() && comment) streamer.AddComment(comment);
815debd7e4e8bc5cfe61bfb71835ce2b1a3fbccc2beRafael Espindola  streamer.EmitAbsValue(v, size);
81625f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola}
81725f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola
818bfa27cc5d72e061a96efbb461864d40bc8089ec2Rafael Espindolastatic void EmitPersonality(MCStreamer &streamer, const MCSymbol &symbol,
819bfa27cc5d72e061a96efbb461864d40bc8089ec2Rafael Espindola                            unsigned symbolEncoding) {
820bfa27cc5d72e061a96efbb461864d40bc8089ec2Rafael Espindola  MCContext &context = streamer.getContext();
821bfa27cc5d72e061a96efbb461864d40bc8089ec2Rafael Espindola  const MCAsmInfo &asmInfo = context.getAsmInfo();
822debd7e4e8bc5cfe61bfb71835ce2b1a3fbccc2beRafael Espindola  const MCExpr *v = asmInfo.getExprForPersonalitySymbol(&symbol,
823debd7e4e8bc5cfe61bfb71835ce2b1a3fbccc2beRafael Espindola                                                        symbolEncoding,
824debd7e4e8bc5cfe61bfb71835ce2b1a3fbccc2beRafael Espindola                                                        streamer);
825bfa27cc5d72e061a96efbb461864d40bc8089ec2Rafael Espindola  unsigned size = getSizeForEncoding(streamer, symbolEncoding);
826debd7e4e8bc5cfe61bfb71835ce2b1a3fbccc2beRafael Espindola  streamer.EmitValue(v, size);
827bfa27cc5d72e061a96efbb461864d40bc8089ec2Rafael Espindola}
828bfa27cc5d72e061a96efbb461864d40bc8089ec2Rafael Espindola
82925f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindolastatic const MachineLocation TranslateMachineLocation(
8300e6a052331f674dd70e28af41f654a7874405eabEvan Cheng                                                  const MCRegisterInfo &MRI,
83125f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                                                  const MachineLocation &Loc) {
83225f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  unsigned Reg = Loc.getReg() == MachineLocation::VirtualFP ?
83325f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola    MachineLocation::VirtualFP :
8340e6a052331f674dd70e28af41f654a7874405eabEvan Cheng    unsigned(MRI.getDwarfRegNum(Loc.getReg(), true));
83525f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  const MachineLocation &NewLoc = Loc.isReg() ?
83625f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola    MachineLocation(Reg) : MachineLocation(Reg, Loc.getOffset());
83725f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  return NewLoc;
83825f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola}
83925f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola
84025f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindolanamespace {
84125f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  class FrameEmitterImpl {
84225f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola    int CFAOffset;
843514cecc72b23bb75108844285ab8f3616a08bac5Rafael Espindola    int CIENum;
8445426a9ee37667660935d80841c5392d78e254318Rafael Espindola    bool UsingCFI;
84540a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    bool IsEH;
846e3a0e987f3d4f07512cdb64b9034369f966cb448Rafael Espindola    const MCSymbol *SectionStart;
84725f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  public:
8481424c3c3e696868775ac5bfc57d27a0ac3bc6c63Bill Wendling    FrameEmitterImpl(bool usingCFI, bool isEH)
8491424c3c3e696868775ac5bfc57d27a0ac3bc6c63Bill Wendling      : CFAOffset(0), CIENum(0), UsingCFI(usingCFI), IsEH(isEH),
8501424c3c3e696868775ac5bfc57d27a0ac3bc6c63Bill Wendling        SectionStart(0) {}
8511424c3c3e696868775ac5bfc57d27a0ac3bc6c63Bill Wendling
8521424c3c3e696868775ac5bfc57d27a0ac3bc6c63Bill Wendling    void setSectionStart(const MCSymbol *Label) { SectionStart = Label; }
85325f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola
854e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling    /// EmitCompactUnwind - Emit the unwind information in a compact way. If
855e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling    /// we're successful, return 'true'. Otherwise, return 'false' and it will
856e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling    /// emit the normal CIE and FDE.
857e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling    bool EmitCompactUnwind(MCStreamer &streamer,
858e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling                           const MCDwarfFrameInfo &frame);
859e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling
86025f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola    const MCSymbol &EmitCIE(MCStreamer &streamer,
86125f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                            const MCSymbol *personality,
86225f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                            unsigned personalityEncoding,
86325f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                            const MCSymbol *lsda,
86416d7d437e03ce87fdaef7971919302920d54a966Rafael Espindola                            bool IsSignalFrame,
86525f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                            unsigned lsdaEncoding);
86625f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola    MCSymbol *EmitFDE(MCStreamer &streamer,
86725f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                      const MCSymbol &cieStart,
8689f270dadd429771dded5a8572da8c74513771c15Rafael Espindola                      const MCDwarfFrameInfo &frame);
86925f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola    void EmitCFIInstructions(MCStreamer &streamer,
87025f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                             const std::vector<MCCFIInstruction> &Instrs,
87125f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                             MCSymbol *BaseLabel);
87225f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola    void EmitCFIInstruction(MCStreamer &Streamer,
87325f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                            const MCCFIInstruction &Instr);
87425f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  };
8753c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling
8763c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling} // end anonymous namespace
8773c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling
8783c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendlingstatic void EmitEncodingByte(MCStreamer &Streamer, unsigned Encoding,
8793c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling                             StringRef Prefix) {
8803c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling  if (Streamer.isVerboseAsm()) {
881eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer    const char *EncStr;
8823c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling    switch (Encoding) {
883eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer    default: EncStr = "<unknown encoding>"; break;
884eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer    case dwarf::DW_EH_PE_absptr: EncStr = "absptr"; break;
885eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer    case dwarf::DW_EH_PE_omit:   EncStr = "omit"; break;
886eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer    case dwarf::DW_EH_PE_pcrel:  EncStr = "pcrel"; break;
887eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer    case dwarf::DW_EH_PE_udata4: EncStr = "udata4"; break;
888eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer    case dwarf::DW_EH_PE_udata8: EncStr = "udata8"; break;
889eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer    case dwarf::DW_EH_PE_sdata4: EncStr = "sdata4"; break;
890eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer    case dwarf::DW_EH_PE_sdata8: EncStr = "sdata8"; break;
891eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer    case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
892eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer      EncStr = "pcrel udata4";
893eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer      break;
894eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer    case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
895eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer      EncStr = "pcrel sdata4";
896eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer      break;
897eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer    case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
898eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer      EncStr = "pcrel udata8";
899eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer      break;
900eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer    case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
901eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer      EncStr = "screl sdata8";
902eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer      break;
9033c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling    case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata4:
9043c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling      EncStr = "indirect pcrel udata4";
905eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer      break;
9063c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling    case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata4:
9073c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling      EncStr = "indirect pcrel sdata4";
908eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer      break;
9093c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling    case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata8:
9103c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling      EncStr = "indirect pcrel udata8";
911eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer      break;
9123c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling    case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata8:
9133c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling      EncStr = "indirect pcrel sdata8";
914eb9fa666f9ea27334aa728d7b6b435b5c20a2c4cBenjamin Kramer      break;
9153c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling    }
9163c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling
9173c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling    Streamer.AddComment(Twine(Prefix) + " = " + EncStr);
9183c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling  }
9193c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling
9203c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling  Streamer.EmitIntValue(Encoding, 1);
92125f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola}
92225f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola
92325f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindolavoid FrameEmitterImpl::EmitCFIInstruction(MCStreamer &Streamer,
92425f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                                          const MCCFIInstruction &Instr) {
925fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola  int dataAlignmentFactor = getDataAlignmentFactor(Streamer);
926efd24ddbffc1e69fa88089de036d1873c950dfb5Bill Wendling  bool VerboseAsm = Streamer.isVerboseAsm();
92789b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
928fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola  switch (Instr.getOperation()) {
92925f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  case MCCFIInstruction::Move:
93025f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola  case MCCFIInstruction::RelMove: {
931fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola    const MachineLocation &Dst = Instr.getDestination();
932fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola    const MachineLocation &Src = Instr.getSource();
9335d7dcd3335234d2a2bc16dc69f86fbb5dcaa8962Rafael Espindola    const bool IsRelative = Instr.getOperation() == MCCFIInstruction::RelMove;
93489b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
93589b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola    // If advancing cfa.
93689b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola    if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
93789b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola      if (Src.getReg() == MachineLocation::VirtualFP) {
938efd24ddbffc1e69fa88089de036d1873c950dfb5Bill Wendling        if (VerboseAsm) Streamer.AddComment("DW_CFA_def_cfa_offset");
939fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola        Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_offset, 1);
94089b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola      } else {
941efd24ddbffc1e69fa88089de036d1873c950dfb5Bill Wendling        if (VerboseAsm) Streamer.AddComment("DW_CFA_def_cfa");
942fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola        Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa, 1);
943efd24ddbffc1e69fa88089de036d1873c950dfb5Bill Wendling        if (VerboseAsm) Streamer.AddComment(Twine("Reg ") +
944efd24ddbffc1e69fa88089de036d1873c950dfb5Bill Wendling                                            Twine(Src.getReg()));
945b40a71fda188f8ca564e606ac2cb051a44ada311Rafael Espindola        Streamer.EmitULEB128IntValue(Src.getReg());
94689b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola      }
94789b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
9485d7dcd3335234d2a2bc16dc69f86fbb5dcaa8962Rafael Espindola      if (IsRelative)
9495d7dcd3335234d2a2bc16dc69f86fbb5dcaa8962Rafael Espindola        CFAOffset += Src.getOffset();
9505d7dcd3335234d2a2bc16dc69f86fbb5dcaa8962Rafael Espindola      else
9515d7dcd3335234d2a2bc16dc69f86fbb5dcaa8962Rafael Espindola        CFAOffset = -Src.getOffset();
9525d7dcd3335234d2a2bc16dc69f86fbb5dcaa8962Rafael Espindola
953efd24ddbffc1e69fa88089de036d1873c950dfb5Bill Wendling      if (VerboseAsm) Streamer.AddComment(Twine("Offset " + Twine(CFAOffset)));
954e8cfbd843d737e1f95c3032c7670c2be3838a6f6Rafael Espindola      Streamer.EmitULEB128IntValue(CFAOffset);
955fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola      return;
95689b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola    }
95789b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
95889b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola    if (Src.isReg() && Src.getReg() == MachineLocation::VirtualFP) {
95989b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola      assert(Dst.isReg() && "Machine move not supported yet.");
960efd24ddbffc1e69fa88089de036d1873c950dfb5Bill Wendling      if (VerboseAsm) Streamer.AddComment("DW_CFA_def_cfa_register");
961fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola      Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_register, 1);
962efd24ddbffc1e69fa88089de036d1873c950dfb5Bill Wendling      if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Dst.getReg()));
963b40a71fda188f8ca564e606ac2cb051a44ada311Rafael Espindola      Streamer.EmitULEB128IntValue(Dst.getReg());
964fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola      return;
96589b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola    }
96689b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
967b40a71fda188f8ca564e606ac2cb051a44ada311Rafael Espindola    unsigned Reg = Src.getReg();
96825f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola    int Offset = Dst.getOffset();
96925f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola    if (IsRelative)
97025f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola      Offset -= CFAOffset;
97125f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola    Offset = Offset / dataAlignmentFactor;
97289b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
97389b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola    if (Offset < 0) {
974efd24ddbffc1e69fa88089de036d1873c950dfb5Bill Wendling      if (VerboseAsm) Streamer.AddComment("DW_CFA_offset_extended_sf");
975fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola      Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended_sf, 1);
976efd24ddbffc1e69fa88089de036d1873c950dfb5Bill Wendling      if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
977fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola      Streamer.EmitULEB128IntValue(Reg);
978efd24ddbffc1e69fa88089de036d1873c950dfb5Bill Wendling      if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
979fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola      Streamer.EmitSLEB128IntValue(Offset);
98089b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola    } else if (Reg < 64) {
981e08d4335ad29d74008222b4d7ac91c153ed66becBill Wendling      if (VerboseAsm) Streamer.AddComment(Twine("DW_CFA_offset + Reg(") +
982e08d4335ad29d74008222b4d7ac91c153ed66becBill Wendling                                          Twine(Reg) + ")");
983fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola      Streamer.EmitIntValue(dwarf::DW_CFA_offset + Reg, 1);
984efd24ddbffc1e69fa88089de036d1873c950dfb5Bill Wendling      if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
985eccbad78e0854743e358841b64f9e0f5eba82861Rafael Espindola      Streamer.EmitULEB128IntValue(Offset);
98689b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola    } else {
987efd24ddbffc1e69fa88089de036d1873c950dfb5Bill Wendling      if (VerboseAsm) Streamer.AddComment("DW_CFA_offset_extended");
988fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola      Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended, 1);
989efd24ddbffc1e69fa88089de036d1873c950dfb5Bill Wendling      if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
990eccbad78e0854743e358841b64f9e0f5eba82861Rafael Espindola      Streamer.EmitULEB128IntValue(Reg);
991efd24ddbffc1e69fa88089de036d1873c950dfb5Bill Wendling      if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
992eccbad78e0854743e358841b64f9e0f5eba82861Rafael Espindola      Streamer.EmitULEB128IntValue(Offset);
993fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola    }
994fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola    return;
995fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola  }
996c25680f728f89dd43939b00049ee524fbf59b7beRafael Espindola  case MCCFIInstruction::RememberState:
997efd24ddbffc1e69fa88089de036d1873c950dfb5Bill Wendling    if (VerboseAsm) Streamer.AddComment("DW_CFA_remember_state");
998fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola    Streamer.EmitIntValue(dwarf::DW_CFA_remember_state, 1);
999fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola    return;
1000c25680f728f89dd43939b00049ee524fbf59b7beRafael Espindola  case MCCFIInstruction::RestoreState:
1001efd24ddbffc1e69fa88089de036d1873c950dfb5Bill Wendling    if (VerboseAsm) Streamer.AddComment("DW_CFA_restore_state");
1002fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola    Streamer.EmitIntValue(dwarf::DW_CFA_restore_state, 1);
1003fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola    return;
1004c57543964d1382d3d3a5005f415b6c0f49671b3aRafael Espindola  case MCCFIInstruction::SameValue: {
1005c57543964d1382d3d3a5005f415b6c0f49671b3aRafael Espindola    unsigned Reg = Instr.getDestination().getReg();
1006efd24ddbffc1e69fa88089de036d1873c950dfb5Bill Wendling    if (VerboseAsm) Streamer.AddComment("DW_CFA_same_value");
1007c57543964d1382d3d3a5005f415b6c0f49671b3aRafael Espindola    Streamer.EmitIntValue(dwarf::DW_CFA_same_value, 1);
1008efd24ddbffc1e69fa88089de036d1873c950dfb5Bill Wendling    if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
1009e8cfbd843d737e1f95c3032c7670c2be3838a6f6Rafael Espindola    Streamer.EmitULEB128IntValue(Reg);
1010c57543964d1382d3d3a5005f415b6c0f49671b3aRafael Espindola    return;
1011c57543964d1382d3d3a5005f415b6c0f49671b3aRafael Espindola  }
1012ed23bdb65fe86cdb7a38c8c1998ec965e6973966Rafael Espindola  case MCCFIInstruction::Restore: {
1013ed23bdb65fe86cdb7a38c8c1998ec965e6973966Rafael Espindola    unsigned Reg = Instr.getDestination().getReg();
1014ed23bdb65fe86cdb7a38c8c1998ec965e6973966Rafael Espindola    if (VerboseAsm) {
1015ed23bdb65fe86cdb7a38c8c1998ec965e6973966Rafael Espindola      Streamer.AddComment("DW_CFA_restore");
1016ed23bdb65fe86cdb7a38c8c1998ec965e6973966Rafael Espindola      Streamer.AddComment(Twine("Reg ") + Twine(Reg));
1017ed23bdb65fe86cdb7a38c8c1998ec965e6973966Rafael Espindola    }
1018ed23bdb65fe86cdb7a38c8c1998ec965e6973966Rafael Espindola    Streamer.EmitIntValue(dwarf::DW_CFA_restore | Reg, 1);
1019ed23bdb65fe86cdb7a38c8c1998ec965e6973966Rafael Espindola    return;
1020ed23bdb65fe86cdb7a38c8c1998ec965e6973966Rafael Espindola  }
10216f0b181bc70318f8d5d4b9bdead7fc748677fe2aRafael Espindola  case MCCFIInstruction::Escape:
10226f0b181bc70318f8d5d4b9bdead7fc748677fe2aRafael Espindola    if (VerboseAsm) Streamer.AddComment("Escape bytes");
10236f0b181bc70318f8d5d4b9bdead7fc748677fe2aRafael Espindola    Streamer.EmitBytes(Instr.getValues(), 0);
10246f0b181bc70318f8d5d4b9bdead7fc748677fe2aRafael Espindola    return;
1025fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola  }
1026fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola  llvm_unreachable("Unhandled case in switch");
1027fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola}
1028fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola
1029fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola/// EmitFrameMoves - Emit frame instructions to describe the layout of the
1030fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola/// frame.
103125f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindolavoid FrameEmitterImpl::EmitCFIInstructions(MCStreamer &streamer,
103225f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                                    const std::vector<MCCFIInstruction> &Instrs,
103325f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                                           MCSymbol *BaseLabel) {
1034fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola  for (unsigned i = 0, N = Instrs.size(); i < N; ++i) {
1035fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola    const MCCFIInstruction &Instr = Instrs[i];
1036fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola    MCSymbol *Label = Instr.getLabel();
1037fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola    // Throw out move if the label is invalid.
1038fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola    if (Label && !Label->isDefined()) continue; // Not emitted, in dead code.
1039fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola
1040fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola    // Advance row if new location.
1041fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola    if (BaseLabel && Label) {
1042fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola      MCSymbol *ThisSym = Label;
1043fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola      if (ThisSym != BaseLabel) {
1044d221cd676b44d8118b1db152ecc2168538479626Bill Wendling        if (streamer.isVerboseAsm()) streamer.AddComment("DW_CFA_advance_loc4");
1045fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola        streamer.EmitDwarfAdvanceFrameAddr(BaseLabel, ThisSym);
1046fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola        BaseLabel = ThisSym;
1047fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola      }
104889b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola    }
1049fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola
1050b40a71fda188f8ca564e606ac2cb051a44ada311Rafael Espindola    EmitCFIInstruction(streamer, Instr);
105189b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  }
105289b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola}
105389b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
1054e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling/// EmitCompactUnwind - Emit the unwind information in a compact way. If we're
1055e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling/// successful, return 'true'. Otherwise, return 'false' and it will emit the
1056e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling/// normal CIE and FDE.
1057e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendlingbool FrameEmitterImpl::EmitCompactUnwind(MCStreamer &Streamer,
1058e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling                                         const MCDwarfFrameInfo &Frame) {
1059e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  MCContext &Context = Streamer.getContext();
1060e76a33b9567d78a5744dc52fcec3a6056d6fb576Evan Cheng  const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
10613c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling  bool VerboseAsm = Streamer.isVerboseAsm();
1062e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling
1063e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  // range-start range-length  compact-unwind-enc personality-func   lsda
1064e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //  _foo       LfooEnd-_foo  0x00000023          0                 0
1065e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //  _bar       LbarEnd-_bar  0x00000025         __gxx_personality  except_tab1
1066e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //
1067e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //   .section __LD,__compact_unwind,regular,debug
1068e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //
1069e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //   # compact unwind for _foo
1070e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //   .quad _foo
1071e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //   .set L1,LfooEnd-_foo
1072e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //   .long L1
1073e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //   .long 0x01010001
1074e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //   .quad 0
1075e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //   .quad 0
1076e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //
1077e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //   # compact unwind for _bar
1078e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //   .quad _bar
1079e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //   .set L2,LbarEnd-_bar
1080e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //   .long L2
1081e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //   .long 0x01020011
1082e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //   .quad __gxx_personality
1083e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  //   .quad except_tab1
1084e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling
1085e52e3f2c020354c8e0e966b719152b08349d0334Bill Wendling  uint32_t Encoding = Frame.CompactUnwindEncoding;
10866a6b8c3e96b9e1ca7092eafd0cfb219cbbfbdfc4Bill Wendling  if (!Encoding) return false;
10876a6b8c3e96b9e1ca7092eafd0cfb219cbbfbdfc4Bill Wendling
10884bb5b037126ea35f35d5a490b03ccffa8677fa5eBill Wendling  // The encoding needs to know we have an LSDA.
10894bb5b037126ea35f35d5a490b03ccffa8677fa5eBill Wendling  if (Frame.Lsda)
10904bb5b037126ea35f35d5a490b03ccffa8677fa5eBill Wendling    Encoding |= 0x40000000;
10914bb5b037126ea35f35d5a490b03ccffa8677fa5eBill Wendling
1092e76a33b9567d78a5744dc52fcec3a6056d6fb576Evan Cheng  Streamer.SwitchSection(MOFI->getCompactUnwindSection());
10933c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling
1094e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  // Range Start
1095203576aa0cb9d8bf2d2e4d910ebab4b7a63262aeEvan Cheng  unsigned FDEEncoding = MOFI->getFDEEncoding(UsingCFI);
10969056e9032001a2d47057cecec5e39895cbc31799Bill Wendling  unsigned Size = getSizeForEncoding(Streamer, FDEEncoding);
10973c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling  if (VerboseAsm) Streamer.AddComment("Range Start");
10989056e9032001a2d47057cecec5e39895cbc31799Bill Wendling  Streamer.EmitSymbolValue(Frame.Function, Size);
1099e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling
1100e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  // Range Length
1101e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  const MCExpr *Range = MakeStartMinusEndExpr(Streamer, *Frame.Begin,
1102e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling                                              *Frame.End, 0);
11033c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling  if (VerboseAsm) Streamer.AddComment("Range Length");
11049287a6eef336585628a55e1f33e1e3ea9d7f81cfBill Wendling  Streamer.EmitAbsValue(Range, 4);
11059287a6eef336585628a55e1f33e1e3ea9d7f81cfBill Wendling
11069287a6eef336585628a55e1f33e1e3ea9d7f81cfBill Wendling  // Compact Encoding
11079287a6eef336585628a55e1f33e1e3ea9d7f81cfBill Wendling  Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_udata4);
110870be28a5adba5bcae0c6dcd63f17592864c351fcBenjamin Kramer  if (VerboseAsm) Streamer.AddComment("Compact Unwind Encoding: 0x" +
110970be28a5adba5bcae0c6dcd63f17592864c351fcBenjamin Kramer                                      Twine::utohexstr(Encoding));
11109287a6eef336585628a55e1f33e1e3ea9d7f81cfBill Wendling  Streamer.EmitIntValue(Encoding, Size);
11119287a6eef336585628a55e1f33e1e3ea9d7f81cfBill Wendling
1112e52e3f2c020354c8e0e966b719152b08349d0334Bill Wendling
11139056e9032001a2d47057cecec5e39895cbc31799Bill Wendling  // Personality Function
11144bb5b037126ea35f35d5a490b03ccffa8677fa5eBill Wendling  Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_absptr);
11153c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling  if (VerboseAsm) Streamer.AddComment("Personality Function");
11164498d39680abe3970dc84dca973aff46d0f9039bBill Wendling  if (Frame.Personality)
11179056e9032001a2d47057cecec5e39895cbc31799Bill Wendling    Streamer.EmitSymbolValue(Frame.Personality, Size);
11184498d39680abe3970dc84dca973aff46d0f9039bBill Wendling  else
11194498d39680abe3970dc84dca973aff46d0f9039bBill Wendling    Streamer.EmitIntValue(0, Size); // No personality fn
11209056e9032001a2d47057cecec5e39895cbc31799Bill Wendling
11219056e9032001a2d47057cecec5e39895cbc31799Bill Wendling  // LSDA
11224498d39680abe3970dc84dca973aff46d0f9039bBill Wendling  Size = getSizeForEncoding(Streamer, Frame.LsdaEncoding);
11233c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling  if (VerboseAsm) Streamer.AddComment("LSDA");
11244498d39680abe3970dc84dca973aff46d0f9039bBill Wendling  if (Frame.Lsda)
11259056e9032001a2d47057cecec5e39895cbc31799Bill Wendling    Streamer.EmitSymbolValue(Frame.Lsda, Size);
11264498d39680abe3970dc84dca973aff46d0f9039bBill Wendling  else
11274498d39680abe3970dc84dca973aff46d0f9039bBill Wendling    Streamer.EmitIntValue(0, Size); // No LSDA
11289056e9032001a2d47057cecec5e39895cbc31799Bill Wendling
1129e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling  return true;
1130e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling}
1131e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling
113225f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindolaconst MCSymbol &FrameEmitterImpl::EmitCIE(MCStreamer &streamer,
113325f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                                          const MCSymbol *personality,
113425f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                                          unsigned personalityEncoding,
113525f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                                          const MCSymbol *lsda,
113616d7d437e03ce87fdaef7971919302920d54a966Rafael Espindola                                          bool IsSignalFrame,
113725f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                                          unsigned lsdaEncoding) {
113889b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  MCContext &context = streamer.getContext();
11390e6a052331f674dd70e28af41f654a7874405eabEvan Cheng  const MCRegisterInfo &MRI = context.getRegisterInfo();
1140e76a33b9567d78a5744dc52fcec3a6056d6fb576Evan Cheng  const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
11413c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling  bool verboseAsm = streamer.isVerboseAsm();
1142514cecc72b23bb75108844285ab8f3616a08bac5Rafael Espindola
1143514cecc72b23bb75108844285ab8f3616a08bac5Rafael Espindola  MCSymbol *sectionStart;
1144e76a33b9567d78a5744dc52fcec3a6056d6fb576Evan Cheng  if (MOFI->isFunctionEHFrameSymbolPrivate() || !IsEH)
1145514cecc72b23bb75108844285ab8f3616a08bac5Rafael Espindola    sectionStart = context.CreateTempSymbol();
1146514cecc72b23bb75108844285ab8f3616a08bac5Rafael Espindola  else
1147514cecc72b23bb75108844285ab8f3616a08bac5Rafael Espindola    sectionStart = context.GetOrCreateSymbol(Twine("EH_frame") + Twine(CIENum));
1148514cecc72b23bb75108844285ab8f3616a08bac5Rafael Espindola
11493c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling  streamer.EmitLabel(sectionStart);
1150514cecc72b23bb75108844285ab8f3616a08bac5Rafael Espindola  CIENum++;
1151514cecc72b23bb75108844285ab8f3616a08bac5Rafael Espindola
11526a6b8c3e96b9e1ca7092eafd0cfb219cbbfbdfc4Bill Wendling  MCSymbol *sectionEnd = context.CreateTempSymbol();
115389b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
115489b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  // Length
115589b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  const MCExpr *Length = MakeStartMinusEndExpr(streamer, *sectionStart,
115689b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola                                               *sectionEnd, 4);
11573c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling  if (verboseAsm) streamer.AddComment("CIE Length");
11589266cc400ee2fc86abb24ab0819d9da280fb61c4Rafael Espindola  streamer.EmitAbsValue(Length, 4);
115989b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
116089b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  // CIE ID
116140a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola  unsigned CIE_ID = IsEH ? 0 : -1;
11623c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling  if (verboseAsm) streamer.AddComment("CIE ID Tag");
116340a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola  streamer.EmitIntValue(CIE_ID, 4);
116489b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
116589b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  // Version
11663c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling  if (verboseAsm) streamer.AddComment("DW_CIE_VERSION");
116789b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  streamer.EmitIntValue(dwarf::DW_CIE_VERSION, 1);
116889b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
116989b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  // Augmentation String
117089b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  SmallString<8> Augmentation;
117140a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola  if (IsEH) {
11723c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling    if (verboseAsm) streamer.AddComment("CIE Augmentation");
117340a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    Augmentation += "z";
117440a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    if (personality)
117540a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola      Augmentation += "P";
117640a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    if (lsda)
117740a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola      Augmentation += "L";
117840a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    Augmentation += "R";
117916d7d437e03ce87fdaef7971919302920d54a966Rafael Espindola    if (IsSignalFrame)
118016d7d437e03ce87fdaef7971919302920d54a966Rafael Espindola      Augmentation += "S";
118140a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    streamer.EmitBytes(Augmentation.str(), 0);
118240a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola  }
118389b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  streamer.EmitIntValue(0, 1);
118489b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
118589b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  // Code Alignment Factor
11863c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling  if (verboseAsm) streamer.AddComment("CIE Code Alignment Factor");
118789b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  streamer.EmitULEB128IntValue(1);
118889b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
118989b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  // Data Alignment Factor
11903c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling  if (verboseAsm) streamer.AddComment("CIE Data Alignment Factor");
119189b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  streamer.EmitSLEB128IntValue(getDataAlignmentFactor(streamer));
119289b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
119389b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  // Return Address Register
11943c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling  if (verboseAsm) streamer.AddComment("CIE Return Address Column");
11950e6a052331f674dd70e28af41f654a7874405eabEvan Cheng  streamer.EmitULEB128IntValue(MRI.getDwarfRegNum(MRI.getRARegister(), true));
119689b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
119789b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  // Augmentation Data Length (optional)
11981f6c87554a0d78103b95816fd2c1ce392b366bcfRafael Espindola
11991f6c87554a0d78103b95816fd2c1ce392b366bcfRafael Espindola  unsigned augmentationLength = 0;
120040a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola  if (IsEH) {
120140a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    if (personality) {
120240a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola      // Personality Encoding
120340a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola      augmentationLength += 1;
120440a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola      // Personality
120540a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola      augmentationLength += getSizeForEncoding(streamer, personalityEncoding);
120640a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    }
120740a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    if (lsda)
120840a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola      augmentationLength += 1;
120940a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    // Encoding of the FDE pointers
12101f6c87554a0d78103b95816fd2c1ce392b366bcfRafael Espindola    augmentationLength += 1;
12111f6c87554a0d78103b95816fd2c1ce392b366bcfRafael Espindola
12123c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling    if (verboseAsm) streamer.AddComment("Augmentation Size");
121340a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    streamer.EmitULEB128IntValue(augmentationLength);
121489b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
121540a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    // Augmentation Data (optional)
121640a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    if (personality) {
121740a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola      // Personality Encoding
12183c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling      EmitEncodingByte(streamer, personalityEncoding,
12193c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling                       "Personality Encoding");
122040a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola      // Personality
12213c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling      if (verboseAsm) streamer.AddComment("Personality");
122240a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola      EmitPersonality(streamer, *personality, personalityEncoding);
122340a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    }
12243c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling
122540a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    if (lsda)
12263c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling      EmitEncodingByte(streamer, lsdaEncoding, "LSDA Encoding");
12273c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling
122840a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    // Encoding of the FDE pointers
1229203576aa0cb9d8bf2d2e4d910ebab4b7a63262aeEvan Cheng    EmitEncodingByte(streamer, MOFI->getFDEEncoding(UsingCFI),
12303c163cfa60f9f196cd6a5a04e07baec3947fee0bBill Wendling                     "FDE Encoding");
1231bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola  }
123289b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
123389b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  // Initial Instructions
123489b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
12352d28617de2b0b731c08d1af9e830f31e14ac75b4Evan Cheng  const MCAsmInfo &MAI = context.getAsmInfo();
12362d28617de2b0b731c08d1af9e830f31e14ac75b4Evan Cheng  const std::vector<MachineMove> &Moves = MAI.getInitialFrameState();
1237fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola  std::vector<MCCFIInstruction> Instructions;
1238fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola
1239fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola  for (int i = 0, n = Moves.size(); i != n; ++i) {
1240b40a71fda188f8ca564e606ac2cb051a44ada311Rafael Espindola    MCSymbol *Label = Moves[i].getLabel();
1241b40a71fda188f8ca564e606ac2cb051a44ada311Rafael Espindola    const MachineLocation &Dst =
12420e6a052331f674dd70e28af41f654a7874405eabEvan Cheng      TranslateMachineLocation(MRI, Moves[i].getDestination());
1243b40a71fda188f8ca564e606ac2cb051a44ada311Rafael Espindola    const MachineLocation &Src =
12440e6a052331f674dd70e28af41f654a7874405eabEvan Cheng      TranslateMachineLocation(MRI, Moves[i].getSource());
1245b40a71fda188f8ca564e606ac2cb051a44ada311Rafael Espindola    MCCFIInstruction Inst(Label, Dst, Src);
1246fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola    Instructions.push_back(Inst);
1247fe024d0a624404ada11fb330e7360abc5f88742eRafael Espindola  }
124889b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
1249b40a71fda188f8ca564e606ac2cb051a44ada311Rafael Espindola  EmitCFIInstructions(streamer, Instructions, NULL);
125089b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
125189b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  // Padding
12521be0e271a07925b928ba89848934f1ea6f1854e2Evan Cheng  streamer.EmitValueToAlignment(IsEH
12531be0e271a07925b928ba89848934f1ea6f1854e2Evan Cheng                                ? 4 : context.getAsmInfo().getPointerSize());
125489b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
125589b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  streamer.EmitLabel(sectionEnd);
125689b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  return *sectionStart;
125789b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola}
125889b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
125925f492e77858dc5a95fcd7180e73aff47925b668Rafael EspindolaMCSymbol *FrameEmitterImpl::EmitFDE(MCStreamer &streamer,
126025f492e77858dc5a95fcd7180e73aff47925b668Rafael Espindola                                    const MCSymbol &cieStart,
12619f270dadd429771dded5a8572da8c74513771c15Rafael Espindola                                    const MCDwarfFrameInfo &frame) {
1262bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola  MCContext &context = streamer.getContext();
1263bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola  MCSymbol *fdeStart = context.CreateTempSymbol();
1264bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola  MCSymbol *fdeEnd = context.CreateTempSymbol();
1265e76a33b9567d78a5744dc52fcec3a6056d6fb576Evan Cheng  const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
12662541c41f3e2af94585e14353a91f02facd65e415Bill Wendling  bool verboseAsm = streamer.isVerboseAsm();
126789b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
12685fbe5e783ee0c5ae27c17490a752d7e603e84ed2Evan Cheng  if (IsEH && frame.Function && !MOFI->isFunctionEHFrameSymbolPrivate()) {
12692541c41f3e2af94585e14353a91f02facd65e415Bill Wendling    MCSymbol *EHSym =
12702541c41f3e2af94585e14353a91f02facd65e415Bill Wendling      context.GetOrCreateSymbol(frame.Function->getName() + Twine(".eh"));
12718bca4106dfc2945723251db10e340183f3e372ddRafael Espindola    streamer.EmitEHSymAttributes(frame.Function, EHSym);
1272a8cfb87fa2176fae2d62785a0dd606e4143cefaeRafael Espindola    streamer.EmitLabel(EHSym);
1273a8cfb87fa2176fae2d62785a0dd606e4143cefaeRafael Espindola  }
1274a8cfb87fa2176fae2d62785a0dd606e4143cefaeRafael Espindola
127589b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  // Length
127689b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  const MCExpr *Length = MakeStartMinusEndExpr(streamer, *fdeStart, *fdeEnd, 0);
12772541c41f3e2af94585e14353a91f02facd65e415Bill Wendling  if (verboseAsm) streamer.AddComment("FDE Length");
12789266cc400ee2fc86abb24ab0819d9da280fb61c4Rafael Espindola  streamer.EmitAbsValue(Length, 4);
127989b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
128089b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  streamer.EmitLabel(fdeStart);
1281e3a0e987f3d4f07512cdb64b9034369f966cb448Rafael Espindola
128289b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  // CIE Pointer
1283e3a0e987f3d4f07512cdb64b9034369f966cb448Rafael Espindola  const MCAsmInfo &asmInfo = context.getAsmInfo();
12840d450dc65906a30beb56aeb1ee22b45b1cd4596cRafael Espindola  if (IsEH) {
12850d450dc65906a30beb56aeb1ee22b45b1cd4596cRafael Espindola    const MCExpr *offset = MakeStartMinusEndExpr(streamer, cieStart, *fdeStart,
12860d450dc65906a30beb56aeb1ee22b45b1cd4596cRafael Espindola                                                 0);
12872541c41f3e2af94585e14353a91f02facd65e415Bill Wendling    if (verboseAsm) streamer.AddComment("FDE CIE Offset");
12880d450dc65906a30beb56aeb1ee22b45b1cd4596cRafael Espindola    streamer.EmitAbsValue(offset, 4);
12892241e51406f7bae369d6103cf3464e70f74c4af9Rafael Espindola  } else if (!asmInfo.doesDwarfUseRelocationsAcrossSections()) {
1290e3a0e987f3d4f07512cdb64b9034369f966cb448Rafael Espindola    const MCExpr *offset = MakeStartMinusEndExpr(streamer, *SectionStart,
1291e3a0e987f3d4f07512cdb64b9034369f966cb448Rafael Espindola                                                 cieStart, 0);
1292e3a0e987f3d4f07512cdb64b9034369f966cb448Rafael Espindola    streamer.EmitAbsValue(offset, 4);
12930d450dc65906a30beb56aeb1ee22b45b1cd4596cRafael Espindola  } else {
12940d450dc65906a30beb56aeb1ee22b45b1cd4596cRafael Espindola    streamer.EmitSymbolValue(&cieStart, 4);
12950d450dc65906a30beb56aeb1ee22b45b1cd4596cRafael Espindola  }
12962541c41f3e2af94585e14353a91f02facd65e415Bill Wendling
1297203576aa0cb9d8bf2d2e4d910ebab4b7a63262aeEvan Cheng  unsigned fdeEncoding = MOFI->getFDEEncoding(UsingCFI);
1298abf9af60add19a0f037bb2a05433769bfd1e7f6fRafael Espindola  unsigned size = getSizeForEncoding(streamer, fdeEncoding);
129989b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
130089b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  // PC Begin
13019989ef43faf1cd7eb01a6d45063f5040e9c8bb3eRafael Espindola  unsigned PCBeginEncoding = IsEH ? fdeEncoding :
13029989ef43faf1cd7eb01a6d45063f5040e9c8bb3eRafael Espindola    (unsigned)dwarf::DW_EH_PE_absptr;
130340a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola  unsigned PCBeginSize = getSizeForEncoding(streamer, PCBeginEncoding);
13042541c41f3e2af94585e14353a91f02facd65e415Bill Wendling  EmitSymbol(streamer, *frame.Begin, PCBeginEncoding, "FDE initial location");
130589b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
130689b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  // PC Range
130789b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  const MCExpr *Range = MakeStartMinusEndExpr(streamer, *frame.Begin,
130889b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola                                              *frame.End, 0);
13092541c41f3e2af94585e14353a91f02facd65e415Bill Wendling  if (verboseAsm) streamer.AddComment("FDE address range");
13109266cc400ee2fc86abb24ab0819d9da280fb61c4Rafael Espindola  streamer.EmitAbsValue(Range, size);
131189b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
131240a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola  if (IsEH) {
131340a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    // Augmentation Data Length
131440a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    unsigned augmentationLength = 0;
13151f6c87554a0d78103b95816fd2c1ce392b366bcfRafael Espindola
131640a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    if (frame.Lsda)
131740a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola      augmentationLength += getSizeForEncoding(streamer, frame.LsdaEncoding);
13181f6c87554a0d78103b95816fd2c1ce392b366bcfRafael Espindola
13192541c41f3e2af94585e14353a91f02facd65e415Bill Wendling    if (verboseAsm) streamer.AddComment("Augmentation size");
132040a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    streamer.EmitULEB128IntValue(augmentationLength);
132189b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
132240a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    // Augmentation Data
132340a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola    if (frame.Lsda)
13242541c41f3e2af94585e14353a91f02facd65e415Bill Wendling      EmitSymbol(streamer, *frame.Lsda, frame.LsdaEncoding,
13252541c41f3e2af94585e14353a91f02facd65e415Bill Wendling                 "Language Specific Data Area");
132640a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola  }
13274892dff07e7d99eb37aee868c4f2c91e9c7eba14Rafael Espindola
132889b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  // Call Frame Instructions
132989b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
1330b40a71fda188f8ca564e606ac2cb051a44ada311Rafael Espindola  EmitCFIInstructions(streamer, frame.Instructions, frame.Begin);
13315bba08425374ca36fe5fbc7423ce1a09858e4097Rafael Espindola
133289b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola  // Padding
133340a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola  streamer.EmitValueToAlignment(PCBeginSize);
1334dfe125cc9cfd524575973daa297c9aad10470390Rafael Espindola
1335dfe125cc9cfd524575973daa297c9aad10470390Rafael Espindola  return fdeEnd;
133689b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola}
133789b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola
133819282365147f498a60463d250dbed2f8e1b81861Benjamin Kramernamespace {
133919282365147f498a60463d250dbed2f8e1b81861Benjamin Kramer  struct CIEKey {
134016d7d437e03ce87fdaef7971919302920d54a966Rafael Espindola    static const CIEKey getEmptyKey() { return CIEKey(0, 0, -1, false); }
134116d7d437e03ce87fdaef7971919302920d54a966Rafael Espindola    static const CIEKey getTombstoneKey() { return CIEKey(0, -1, 0, false); }
134219282365147f498a60463d250dbed2f8e1b81861Benjamin Kramer
134319282365147f498a60463d250dbed2f8e1b81861Benjamin Kramer    CIEKey(const MCSymbol* Personality_, unsigned PersonalityEncoding_,
134416d7d437e03ce87fdaef7971919302920d54a966Rafael Espindola           unsigned LsdaEncoding_, bool IsSignalFrame_) :
134516d7d437e03ce87fdaef7971919302920d54a966Rafael Espindola      Personality(Personality_), PersonalityEncoding(PersonalityEncoding_),
134616d7d437e03ce87fdaef7971919302920d54a966Rafael Espindola      LsdaEncoding(LsdaEncoding_), IsSignalFrame(IsSignalFrame_) {
134719282365147f498a60463d250dbed2f8e1b81861Benjamin Kramer    }
134819282365147f498a60463d250dbed2f8e1b81861Benjamin Kramer    const MCSymbol* Personality;
134919282365147f498a60463d250dbed2f8e1b81861Benjamin Kramer    unsigned PersonalityEncoding;
135019282365147f498a60463d250dbed2f8e1b81861Benjamin Kramer    unsigned LsdaEncoding;
135116d7d437e03ce87fdaef7971919302920d54a966Rafael Espindola    bool IsSignalFrame;
135219282365147f498a60463d250dbed2f8e1b81861Benjamin Kramer  };
135319282365147f498a60463d250dbed2f8e1b81861Benjamin Kramer}
1354bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola
1355bdc3167c086dd4358e24692075db5e7784140843Rafael Espindolanamespace llvm {
1356bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola  template <>
1357bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola  struct DenseMapInfo<CIEKey> {
1358bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola    static CIEKey getEmptyKey() {
135919282365147f498a60463d250dbed2f8e1b81861Benjamin Kramer      return CIEKey::getEmptyKey();
1360bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola    }
1361bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola    static CIEKey getTombstoneKey() {
136219282365147f498a60463d250dbed2f8e1b81861Benjamin Kramer      return CIEKey::getTombstoneKey();
1363bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola    }
1364bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola    static unsigned getHashValue(const CIEKey &Key) {
13657484920cf5d95b21fa750229f8ce0d1624c918ecBenjamin Kramer      return static_cast<unsigned>(hash_combine(Key.Personality,
13667484920cf5d95b21fa750229f8ce0d1624c918ecBenjamin Kramer                                                Key.PersonalityEncoding,
13677484920cf5d95b21fa750229f8ce0d1624c918ecBenjamin Kramer                                                Key.LsdaEncoding,
13687484920cf5d95b21fa750229f8ce0d1624c918ecBenjamin Kramer                                                Key.IsSignalFrame));
1369bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola    }
1370bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola    static bool isEqual(const CIEKey &LHS,
1371bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola                        const CIEKey &RHS) {
1372bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola      return LHS.Personality == RHS.Personality &&
1373bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola        LHS.PersonalityEncoding == RHS.PersonalityEncoding &&
137416d7d437e03ce87fdaef7971919302920d54a966Rafael Espindola        LHS.LsdaEncoding == RHS.LsdaEncoding &&
137516d7d437e03ce87fdaef7971919302920d54a966Rafael Espindola        LHS.IsSignalFrame == RHS.IsSignalFrame;
1376bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola    }
1377bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola  };
1378bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola}
1379bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola
1380a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendlingvoid MCDwarfFrameEmitter::Emit(MCStreamer &Streamer,
1381a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling                               bool UsingCFI,
1382a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling                               bool IsEH) {
1383a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling  MCContext &Context = Streamer.getContext();
1384e76a33b9567d78a5744dc52fcec3a6056d6fb576Evan Cheng  MCObjectFileInfo *MOFI =
1385e76a33b9567d78a5744dc52fcec3a6056d6fb576Evan Cheng    const_cast<MCObjectFileInfo*>(Context.getObjectFileInfo());
13861424c3c3e696868775ac5bfc57d27a0ac3bc6c63Bill Wendling  FrameEmitterImpl Emitter(UsingCFI, IsEH);
1387d8ab8e9707e6170218ce82516d15b36f54b4bc1cBill Wendling  ArrayRef<MCDwarfFrameInfo> FrameArray = Streamer.getFrameInfos();
13881424c3c3e696868775ac5bfc57d27a0ac3bc6c63Bill Wendling
1389d8ab8e9707e6170218ce82516d15b36f54b4bc1cBill Wendling  // Emit the compact unwind info if available.
13907a13b72bbbadaa9274f6a58774970b9b95706330Bill Wendling  if (IsEH && MOFI->getCompactUnwindSection())
13911424c3c3e696868775ac5bfc57d27a0ac3bc6c63Bill Wendling    for (unsigned i = 0, n = Streamer.getNumFrameInfos(); i < n; ++i) {
13921424c3c3e696868775ac5bfc57d27a0ac3bc6c63Bill Wendling      const MCDwarfFrameInfo &Frame = Streamer.getFrameInfo(i);
1393a2ff3e2c3c0d8be862525c91f7b0e153480f4f5eBill Wendling      if (Frame.CompactUnwindEncoding)
1394d8ab8e9707e6170218ce82516d15b36f54b4bc1cBill Wendling        Emitter.EmitCompactUnwind(Streamer, Frame);
13951424c3c3e696868775ac5bfc57d27a0ac3bc6c63Bill Wendling    }
13961424c3c3e696868775ac5bfc57d27a0ac3bc6c63Bill Wendling
1397e76a33b9567d78a5744dc52fcec3a6056d6fb576Evan Cheng  const MCSection &Section = IsEH ? *MOFI->getEHFrameSection() :
1398e76a33b9567d78a5744dc52fcec3a6056d6fb576Evan Cheng                                    *MOFI->getDwarfFrameSection();
1399a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling  Streamer.SwitchSection(&Section);
1400a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling  MCSymbol *SectionStart = Context.CreateTempSymbol();
1401a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling  Streamer.EmitLabel(SectionStart);
14021424c3c3e696868775ac5bfc57d27a0ac3bc6c63Bill Wendling  Emitter.setSectionStart(SectionStart);
1403a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling
1404a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling  MCSymbol *FDEEnd = NULL;
1405bdc3167c086dd4358e24692075db5e7784140843Rafael Espindola  DenseMap<CIEKey, const MCSymbol*> CIEStarts;
1406d7c8ccae8e48dce3ab7c3e9b4d8a309998c47961Rafael Espindola
140740a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08Rafael Espindola  const MCSymbol *DummyDebugKey = NULL;
14081424c3c3e696868775ac5bfc57d27a0ac3bc6c63Bill Wendling  for (unsigned i = 0, n = FrameArray.size(); i < n; ++i) {
14091424c3c3e696868775ac5bfc57d27a0ac3bc6c63Bill Wendling    const MCDwarfFrameInfo &Frame = FrameArray[i];
1410a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling    CIEKey Key(Frame.Personality, Frame.PersonalityEncoding,
141116d7d437e03ce87fdaef7971919302920d54a966Rafael Espindola               Frame.LsdaEncoding, Frame.IsSignalFrame);
1412a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling    const MCSymbol *&CIEStart = IsEH ? CIEStarts[Key] : DummyDebugKey;
1413a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling    if (!CIEStart)
1414a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling      CIEStart = &Emitter.EmitCIE(Streamer, Frame.Personality,
1415a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling                                  Frame.PersonalityEncoding, Frame.Lsda,
141616d7d437e03ce87fdaef7971919302920d54a966Rafael Espindola                                  Frame.IsSignalFrame,
1417a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling                                  Frame.LsdaEncoding);
1418e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling
1419a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling    FDEEnd = Emitter.EmitFDE(Streamer, *CIEStart, Frame);
1420e3cd13f0e2d4e2ef30f8a57c0a82d328102eeb26Bill Wendling
1421dfe125cc9cfd524575973daa297c9aad10470390Rafael Espindola    if (i != n - 1)
1422a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling      Streamer.EmitLabel(FDEEnd);
1423dfe125cc9cfd524575973daa297c9aad10470390Rafael Espindola  }
1424d7c8ccae8e48dce3ab7c3e9b4d8a309998c47961Rafael Espindola
14251be0e271a07925b928ba89848934f1ea6f1854e2Evan Cheng  Streamer.EmitValueToAlignment(Context.getAsmInfo().getPointerSize());
1426a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling  if (FDEEnd)
1427a8c9e6a943bc0873d1ce781d2444a6d6137f0e17Bill Wendling    Streamer.EmitLabel(FDEEnd);
142889b9372605db2ce3b0085c84089e389f7bc1fbddRafael Espindola}
1429245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola
1430245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindolavoid MCDwarfFrameEmitter::EmitAdvanceLoc(MCStreamer &Streamer,
1431245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola                                         uint64_t AddrDelta) {
1432245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola  SmallString<256> Tmp;
1433245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola  raw_svector_ostream OS(Tmp);
14344eafe109459eb115f13f1d19c5ff3cb3678e8c7aRafael Espindola  MCDwarfFrameEmitter::EncodeAdvanceLoc(AddrDelta, OS);
1435245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola  Streamer.EmitBytes(OS.str(), /*AddrSpace=*/0);
1436245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola}
1437245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola
1438245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindolavoid MCDwarfFrameEmitter::EncodeAdvanceLoc(uint64_t AddrDelta,
14394eafe109459eb115f13f1d19c5ff3cb3678e8c7aRafael Espindola                                           raw_ostream &OS) {
1440245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola  // FIXME: Assumes the code alignment factor is 1.
14413b78cdc57ae25ff021fd67171a7c82d533477a19Rafael Espindola  if (AddrDelta == 0) {
14424eafe109459eb115f13f1d19c5ff3cb3678e8c7aRafael Espindola  } else if (isUIntN(6, AddrDelta)) {
1443245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola    uint8_t Opcode = dwarf::DW_CFA_advance_loc | AddrDelta;
1444245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola    OS << Opcode;
14454eafe109459eb115f13f1d19c5ff3cb3678e8c7aRafael Espindola  } else if (isUInt<8>(AddrDelta)) {
1446245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola    OS << uint8_t(dwarf::DW_CFA_advance_loc1);
1447245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola    OS << uint8_t(AddrDelta);
14484eafe109459eb115f13f1d19c5ff3cb3678e8c7aRafael Espindola  } else if (isUInt<16>(AddrDelta)) {
1449a7e450574c45209ca0b05ff715f9e7dddcbd936cRafael Espindola    // FIXME: check what is the correct behavior on a big endian machine.
1450245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola    OS << uint8_t(dwarf::DW_CFA_advance_loc2);
1451a7e450574c45209ca0b05ff715f9e7dddcbd936cRafael Espindola    OS << uint8_t( AddrDelta       & 0xff);
1452a7e450574c45209ca0b05ff715f9e7dddcbd936cRafael Espindola    OS << uint8_t((AddrDelta >> 8) & 0xff);
1453245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola  } else {
1454a7e450574c45209ca0b05ff715f9e7dddcbd936cRafael Espindola    // FIXME: check what is the correct behavior on a big endian machine.
1455245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola    assert(isUInt<32>(AddrDelta));
1456245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola    OS << uint8_t(dwarf::DW_CFA_advance_loc4);
1457a7e450574c45209ca0b05ff715f9e7dddcbd936cRafael Espindola    OS << uint8_t( AddrDelta        & 0xff);
1458a7e450574c45209ca0b05ff715f9e7dddcbd936cRafael Espindola    OS << uint8_t((AddrDelta >> 8)  & 0xff);
1459a7e450574c45209ca0b05ff715f9e7dddcbd936cRafael Espindola    OS << uint8_t((AddrDelta >> 16) & 0xff);
1460a7e450574c45209ca0b05ff715f9e7dddcbd936cRafael Espindola    OS << uint8_t((AddrDelta >> 24) & 0xff);
1461a7e450574c45209ca0b05ff715f9e7dddcbd936cRafael Espindola
1462245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola  }
1463245a1e20419aa5a3c833d7a8e89168e19d5f4d2cRafael Espindola}
1464