MCDwarf.cpp revision ad8aaa069cfdb3bdc32b1becc8881f67b5272e14
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"
11c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby#include "llvm/MC/MCAssembler.h"
12ad8aaa069cfdb3bdc32b1becc8881f67b5272e14Rafael Espindola#include "llvm/MC/MCStreamer.h"
13c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby#include "llvm/MC/MCSymbol.h"
14c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby#include "llvm/MC/MCExpr.h"
15c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby#include "llvm/MC/MCContext.h"
16c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby#include "llvm/MC/MCObjectWriter.h"
17c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby#include "llvm/ADT/SmallString.h"
187cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby#include "llvm/Support/Debug.h"
197cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby#include "llvm/Support/raw_ostream.h"
20c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby#include "llvm/Target/TargetAsmBackend.h"
217cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderbyusing namespace llvm;
227cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby
23c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// Given a special op, return the address skip amount (in units of
24c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// DWARF2_LINE_MIN_INSN_LENGTH.
25c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby#define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE)
26c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
27c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// The maximum address skip amount that can be encoded with a special op.
28c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby#define MAX_SPECIAL_ADDR_DELTA		SPECIAL_ADDR(255)
29c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
30c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// First special line opcode - leave room for the standard opcodes.
31c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// Note: If you want to change this, you'll have to update the
32c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// "standard_opcode_lengths" table that is emitted in DwarfFileTable::Emit().
33c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby#define DWARF2_LINE_OPCODE_BASE		13
34c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
35c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// Minimum line offset in a special line info. opcode.  This value
36c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// was chosen to give a reasonable range of values.
37c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby#define DWARF2_LINE_BASE		-5
38c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
39c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// Range of line offsets in a special line info. opcode.
40c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby# define DWARF2_LINE_RANGE		14
41c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
42c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// Define the architecture-dependent minimum instruction length (in bytes).
43c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// This value should be rather too small than too big.
44c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby# define DWARF2_LINE_MIN_INSN_LENGTH	1
45c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
46c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// Note: when DWARF2_LINE_MIN_INSN_LENGTH == 1 which is the current setting,
47c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// this routine is a nop and will be optimized away.
48c095793b4ab027181605c79c9808df12afe45d63Kevin Enderbystatic inline uint64_t ScaleAddrDelta(uint64_t AddrDelta)
49c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby{
50c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  if (DWARF2_LINE_MIN_INSN_LENGTH == 1)
51c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    return AddrDelta;
52c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  if (AddrDelta % DWARF2_LINE_MIN_INSN_LENGTH != 0) {
53c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    // TODO: report this error, but really only once.
54c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    ;
55c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  }
56c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  return AddrDelta / DWARF2_LINE_MIN_INSN_LENGTH;
57c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby}
58c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
59c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby//
60c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// This is called when an instruction is assembled into the specified section
61c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// and if there is information from the last .loc directive that has yet to have
62c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// a line entry made for it is made.
63c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby//
64195a0ce484cd12a5adae9184188f6d0fb52b84c0Rafael Espindolavoid MCLineEntry::Make(MCStreamer *MCOS, const MCSection *Section) {
65c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  if (!MCOS->getContext().getDwarfLocSeen())
66c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    return;
67c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
68c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Create a symbol at in the current section for use in the line entry.
69c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCSymbol *LineSym = MCOS->getContext().CreateTempSymbol();
70c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Set the value of the symbol to use for the MCLineEntry.
71c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitLabel(LineSym);
72c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
73c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Get the current .loc info saved in the context.
74c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  const MCDwarfLoc &DwarfLoc = MCOS->getContext().getCurrentDwarfLoc();
75c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
76c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Create a (local) line entry with the symbol and the current .loc info.
77c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCLineEntry LineEntry(LineSym, DwarfLoc);
78c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
79c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // clear DwarfLocSeen saying the current .loc info is now used.
803f55c24df9527de345f6cc960944840a7a101c6aKevin Enderby  MCOS->getContext().ClearDwarfLocSeen();
81c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
82c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Get the MCLineSection for this section, if one does not exist for this
83c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // section create it.
8417fd7bda5ac08f873c063c64e1456f8960a0c765Rafael Espindola  const DenseMap<const MCSection *, MCLineSection *> &MCLineSections =
85c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    MCOS->getContext().getMCLineSections();
8617fd7bda5ac08f873c063c64e1456f8960a0c765Rafael Espindola  MCLineSection *LineSection = MCLineSections.lookup(Section);
87c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  if (!LineSection) {
88c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    // Create a new MCLineSection.  This will be deleted after the dwarf line
89c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    // table is created using it by iterating through the MCLineSections
90c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    // DenseMap.
91c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    LineSection = new MCLineSection;
92c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    // Save a pointer to the new LineSection into the MCLineSections DenseMap.
9317fd7bda5ac08f873c063c64e1456f8960a0c765Rafael Espindola    MCOS->getContext().addMCLineSection(Section, LineSection);
94c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  }
95c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
96c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Add the line entry to this section's entries.
97c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  LineSection->addLineEntry(LineEntry);
98c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby}
99c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
100c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby//
101c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// This helper routine returns an expression of End - Start + IntVal .
102c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby//
103195a0ce484cd12a5adae9184188f6d0fb52b84c0Rafael Espindolastatic inline const MCExpr *MakeStartMinusEndExpr(MCStreamer *MCOS,
104c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby                                                  MCSymbol *Start,
105c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby                                                  MCSymbol *End, int IntVal) {
106c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
107c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  const MCExpr *Res =
108c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    MCSymbolRefExpr::Create(End, Variant, MCOS->getContext());
109c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  const MCExpr *RHS =
110c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    MCSymbolRefExpr::Create(Start, Variant, MCOS->getContext());
111c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  const MCExpr *Res1 =
112c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    MCBinaryExpr::Create(MCBinaryExpr::Sub, Res, RHS, MCOS->getContext());
113c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  const MCExpr *Res2 =
114c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    MCConstantExpr::Create(IntVal, MCOS->getContext());
115c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  const MCExpr *Res3 =
116c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    MCBinaryExpr::Create(MCBinaryExpr::Sub, Res1, Res2, MCOS->getContext());
117c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  return Res3;
118c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby}
119c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
120c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby//
121c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// This emits an "absolute" address used in the start of a dwarf line number
122c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// table.  This will result in a relocatation entry for the address.
123c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby//
124195a0ce484cd12a5adae9184188f6d0fb52b84c0Rafael Espindolastatic inline void EmitDwarfSetAddress(MCStreamer *MCOS,
125195a0ce484cd12a5adae9184188f6d0fb52b84c0Rafael Espindola                                       MCSymbol *Symbol,
126195a0ce484cd12a5adae9184188f6d0fb52b84c0Rafael Espindola                                       int PointerSize) {
127c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(dwarf::DW_LNS_extended_op, 1);
128c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
129195a0ce484cd12a5adae9184188f6d0fb52b84c0Rafael Espindola  MCOS->EmitULEB128IntValue(PointerSize + 1);
130c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
131c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(dwarf::DW_LNE_set_address, 1);
132195a0ce484cd12a5adae9184188f6d0fb52b84c0Rafael Espindola  MCOS->EmitSymbolValue(Symbol, PointerSize);
133c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby}
134c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
135c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby//
136c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// This emits the Dwarf line table for the specified section from the entries
137c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// in the LineSection.
138c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby//
139195a0ce484cd12a5adae9184188f6d0fb52b84c0Rafael Espindolastatic inline void EmitDwarfLineTable(MCStreamer *MCOS,
140c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby                                      const MCSection *Section,
14117fd7bda5ac08f873c063c64e1456f8960a0c765Rafael Espindola                                      const MCLineSection *LineSection,
142195a0ce484cd12a5adae9184188f6d0fb52b84c0Rafael Espindola                                      const MCSection *DwarfLineSection,
143195a0ce484cd12a5adae9184188f6d0fb52b84c0Rafael Espindola                                      MCSectionData *DLS,
144195a0ce484cd12a5adae9184188f6d0fb52b84c0Rafael Espindola                                      int PointerSize) {
145c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  unsigned FileNum = 1;
146c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  unsigned LastLine = 1;
147c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  unsigned Column = 0;
148c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
149c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  unsigned Isa = 0;
150c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCSymbol *LastLabel = NULL;
151c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
152c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Loop through each MCLineEntry and encode the dwarf line number table.
15317fd7bda5ac08f873c063c64e1456f8960a0c765Rafael Espindola  for (MCLineSection::const_iterator
154c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby         it = LineSection->getMCLineEntries()->begin(),
155c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby         ie = LineSection->getMCLineEntries()->end(); it != ie; ++it) {
156c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
157c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    if (FileNum != it->getFileNum()) {
158c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      FileNum = it->getFileNum();
159c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      MCOS->EmitIntValue(dwarf::DW_LNS_set_file, 1);
1603ff57094a7d176a759ddb1e1668489d89064f56cRafael Espindola      MCOS->EmitULEB128IntValue(FileNum);
161c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    }
162c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    if (Column != it->getColumn()) {
163c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      Column = it->getColumn();
164c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      MCOS->EmitIntValue(dwarf::DW_LNS_set_column, 1);
1653ff57094a7d176a759ddb1e1668489d89064f56cRafael Espindola      MCOS->EmitULEB128IntValue(Column);
166c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    }
167c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    if (Isa != it->getIsa()) {
168c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      Isa = it->getIsa();
169c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      MCOS->EmitIntValue(dwarf::DW_LNS_set_isa, 1);
1703ff57094a7d176a759ddb1e1668489d89064f56cRafael Espindola      MCOS->EmitULEB128IntValue(Isa);
171c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    }
172c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    if ((it->getFlags() ^ Flags) & DWARF2_FLAG_IS_STMT) {
173c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      Flags = it->getFlags();
174c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      MCOS->EmitIntValue(dwarf::DW_LNS_negate_stmt, 1);
175c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    }
176c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    if (it->getFlags() & DWARF2_FLAG_BASIC_BLOCK)
177c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      MCOS->EmitIntValue(dwarf::DW_LNS_set_basic_block, 1);
178c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    if (it->getFlags() & DWARF2_FLAG_PROLOGUE_END)
179c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      MCOS->EmitIntValue(dwarf::DW_LNS_set_prologue_end, 1);
180c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    if (it->getFlags() & DWARF2_FLAG_EPILOGUE_BEGIN)
181c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      MCOS->EmitIntValue(dwarf::DW_LNS_set_epilogue_begin, 1);
182c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
18364185cc6090f695c4f97c51cf2adc731f56d1a20Rafael Espindola    int64_t LineDelta = static_cast<int64_t>(it->getLine()) - LastLine;
184c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    MCSymbol *Label = it->getLabel();
185c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
186c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    // At this point we want to emit/create the sequence to encode the delta in
187c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    // line numbers and the increment of the address from the previous Label
188c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    // and the current Label.
189195a0ce484cd12a5adae9184188f6d0fb52b84c0Rafael Espindola    if (LastLabel == NULL || DLS == NULL) {
190c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      // emit the sequence to set the address
191195a0ce484cd12a5adae9184188f6d0fb52b84c0Rafael Espindola      EmitDwarfSetAddress(MCOS, Label, PointerSize);
192c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      // emit the sequence for the LineDelta (from 1) and a zero address delta.
193c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      MCDwarfLineAddr::Emit(MCOS, LineDelta, 0);
194c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    }
195c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    else {
196c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      // Create an expression for the address delta from the LastLabel and
197c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      // this Label (plus 0).
198c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      const MCExpr *AddrDelta = MakeStartMinusEndExpr(MCOS, LastLabel, Label,0);
199c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      // Create a Dwarf Line fragment for the LineDelta and AddrDelta.
200195a0ce484cd12a5adae9184188f6d0fb52b84c0Rafael Espindola      new MCDwarfLineAddrFragment(LineDelta, *AddrDelta, DLS);
201c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    }
202c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
203c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    LastLine = it->getLine();
204c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    LastLabel = Label;
205c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  }
206c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
207c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Emit a DW_LNE_end_sequence for the end of the section.
208c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Using the pointer Section create a temporary label at the end of the
209c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // section and use that and the LastLabel to compute the address delta
210c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // and use INT64_MAX as the line delta which is the signal that this is
211c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // actually a DW_LNE_end_sequence.
212c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
213c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Switch to the section to be able to create a symbol at its end.
214c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->SwitchSection(Section);
215c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Create a symbol at the end of the section.
216c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCSymbol *SectionEnd = MCOS->getContext().CreateTempSymbol();
217c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Set the value of the symbol, as we are at the end of the section.
218c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitLabel(SectionEnd);
219c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
220c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Switch back the the dwarf line section.
221c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->SwitchSection(DwarfLineSection);
222195a0ce484cd12a5adae9184188f6d0fb52b84c0Rafael Espindola
223195a0ce484cd12a5adae9184188f6d0fb52b84c0Rafael Espindola  if (DLS == NULL) {
224195a0ce484cd12a5adae9184188f6d0fb52b84c0Rafael Espindola    // emit the sequence to set the address
225195a0ce484cd12a5adae9184188f6d0fb52b84c0Rafael Espindola    EmitDwarfSetAddress(MCOS, SectionEnd, PointerSize);
226195a0ce484cd12a5adae9184188f6d0fb52b84c0Rafael Espindola    // emit the sequence for the LineDelta (from 1) and a zero address delta.
227195a0ce484cd12a5adae9184188f6d0fb52b84c0Rafael Espindola    MCDwarfLineAddr::Emit(MCOS, INT64_MAX, 0);
228195a0ce484cd12a5adae9184188f6d0fb52b84c0Rafael Espindola  } else {
229195a0ce484cd12a5adae9184188f6d0fb52b84c0Rafael Espindola    // Create an expression for the address delta from the LastLabel and this
230195a0ce484cd12a5adae9184188f6d0fb52b84c0Rafael Espindola    // SectionEnd label.
231195a0ce484cd12a5adae9184188f6d0fb52b84c0Rafael Espindola    const MCExpr *AddrDelta = MakeStartMinusEndExpr(MCOS, LastLabel, SectionEnd,
232195a0ce484cd12a5adae9184188f6d0fb52b84c0Rafael Espindola                                                    0);
233195a0ce484cd12a5adae9184188f6d0fb52b84c0Rafael Espindola    // Create a Dwarf Line fragment for the LineDelta and AddrDelta.
234195a0ce484cd12a5adae9184188f6d0fb52b84c0Rafael Espindola    new MCDwarfLineAddrFragment(INT64_MAX, *AddrDelta, DLS);
235195a0ce484cd12a5adae9184188f6d0fb52b84c0Rafael Espindola  }
236c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby}
237c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
238c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby//
239c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby// This emits the Dwarf file and the line tables.
240c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby//
241195a0ce484cd12a5adae9184188f6d0fb52b84c0Rafael Espindolavoid MCDwarfFileTable::Emit(MCStreamer *MCOS,
242195a0ce484cd12a5adae9184188f6d0fb52b84c0Rafael Espindola                            const MCSection *DwarfLineSection,
243195a0ce484cd12a5adae9184188f6d0fb52b84c0Rafael Espindola                            MCSectionData *DLS,
244195a0ce484cd12a5adae9184188f6d0fb52b84c0Rafael Espindola                            int PointerSize) {
245c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Switch to the section where the table will be emitted into.
246c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->SwitchSection(DwarfLineSection);
247c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
248c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Create a symbol at the beginning of this section.
249c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCSymbol *LineStartSym = MCOS->getContext().CreateTempSymbol();
250c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Set the value of the symbol, as we are at the start of the section.
251c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitLabel(LineStartSym);
252c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
253c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Create a symbol for the end of the section (to be set when we get there).
254c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCSymbol *LineEndSym = MCOS->getContext().CreateTempSymbol();
255c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
256c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // The first 4 bytes is the total length of the information for this
257c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // compilation unit (not including these 4 bytes for the length).
258c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitValue(MakeStartMinusEndExpr(MCOS, LineStartSym, LineEndSym, 4),
259c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby                  4, 0);
260c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
261c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Next 2 bytes is the Version, which is Dwarf 2.
262c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(2, 2);
263c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
264c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Create a symbol for the end of the prologue (to be set when we get there).
265c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCSymbol *ProEndSym = MCOS->getContext().CreateTempSymbol(); // Lprologue_end
266c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
267c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Length of the prologue, is the next 4 bytes.  Which is the start of the
268c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // section to the end of the prologue.  Not including the 4 bytes for the
269c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // total length, the 2 bytes for the version, and these 4 bytes for the
270c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // length of the prologue.
271c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitValue(MakeStartMinusEndExpr(MCOS, LineStartSym, ProEndSym,
272c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby                                        (4 + 2 + 4)),
273c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby                  4, 0);
274c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
275c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Parameters of the state machine, are next.
276c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(DWARF2_LINE_MIN_INSN_LENGTH, 1);
277c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(DWARF2_LINE_DEFAULT_IS_STMT, 1);
278c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(DWARF2_LINE_BASE, 1);
279c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(DWARF2_LINE_RANGE, 1);
280c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(DWARF2_LINE_OPCODE_BASE, 1);
281c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
282c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Standard opcode lengths
283c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(0, 1); // length of DW_LNS_copy
284c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_pc
285c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_line
286c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_file
287c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_column
288c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(0, 1); // length of DW_LNS_negate_stmt
289c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_basic_block
290c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(0, 1); // length of DW_LNS_const_add_pc
291c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(1, 1); // length of DW_LNS_fixed_advance_pc
292c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_prologue_end
293c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_epilogue_begin
294c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(1, 1); // DW_LNS_set_isa
295c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
296c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Put out the directory and file tables.
297c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
298c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // First the directory table.
299c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  const std::vector<StringRef> &MCDwarfDirs =
300c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    MCOS->getContext().getMCDwarfDirs();
301c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  for (unsigned i = 0; i < MCDwarfDirs.size(); i++) {
302c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    MCOS->EmitBytes(MCDwarfDirs[i], 0); // the DirectoryName
303c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    MCOS->EmitBytes(StringRef("\0", 1), 0); // the null term. of the string
304c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  }
305c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(0, 1); // Terminate the directory list
306c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
307c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Second the file table.
308c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  const std::vector<MCDwarfFile *> &MCDwarfFiles =
309c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    MCOS->getContext().getMCDwarfFiles();
310c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
311c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    MCOS->EmitBytes(MCDwarfFiles[i]->getName(), 0); // FileName
312c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    MCOS->EmitBytes(StringRef("\0", 1), 0); // the null term. of the string
3133ff57094a7d176a759ddb1e1668489d89064f56cRafael Espindola    // the Directory num
3143ff57094a7d176a759ddb1e1668489d89064f56cRafael Espindola    MCOS->EmitULEB128IntValue(MCDwarfFiles[i]->getDirIndex());
315c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    MCOS->EmitIntValue(0, 1); // last modification timestamp (always 0)
316c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    MCOS->EmitIntValue(0, 1); // filesize (always 0)
317c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  }
318c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitIntValue(0, 1); // Terminate the file list
319c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
320c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // This is the end of the prologue, so set the value of the symbol at the
321c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // end of the prologue (that was used in a previous expression).
322c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitLabel(ProEndSym);
323c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
324c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Put out the line tables.
32517fd7bda5ac08f873c063c64e1456f8960a0c765Rafael Espindola  const DenseMap<const MCSection *, MCLineSection *> &MCLineSections =
326c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    MCOS->getContext().getMCLineSections();
32717fd7bda5ac08f873c063c64e1456f8960a0c765Rafael Espindola  const std::vector<const MCSection *> &MCLineSectionOrder =
32817fd7bda5ac08f873c063c64e1456f8960a0c765Rafael Espindola    MCOS->getContext().getMCLineSectionOrder();
32917fd7bda5ac08f873c063c64e1456f8960a0c765Rafael Espindola  for (std::vector<const MCSection*>::const_iterator it =
33017fd7bda5ac08f873c063c64e1456f8960a0c765Rafael Espindola	MCLineSectionOrder.begin(), ie = MCLineSectionOrder.end(); it != ie;
33117fd7bda5ac08f873c063c64e1456f8960a0c765Rafael Espindola       ++it) {
33217fd7bda5ac08f873c063c64e1456f8960a0c765Rafael Espindola    const MCSection *Sec = *it;
33317fd7bda5ac08f873c063c64e1456f8960a0c765Rafael Espindola    const MCLineSection *Line = MCLineSections.lookup(Sec);
33417fd7bda5ac08f873c063c64e1456f8960a0c765Rafael Espindola    EmitDwarfLineTable(MCOS, Sec, Line, DwarfLineSection, DLS,
335195a0ce484cd12a5adae9184188f6d0fb52b84c0Rafael Espindola                       PointerSize);
336c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
337c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    // Now delete the MCLineSections that were created in MCLineEntry::Make()
338c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    // and used to emit the line table.
33917fd7bda5ac08f873c063c64e1456f8960a0c765Rafael Espindola    delete Line;
340c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  }
341c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
342c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // This is the end of the section, so set the value of the symbol at the end
343c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // of this section (that was used in a previous expression).
344c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitLabel(LineEndSym);
345c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby}
346c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
347c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby/// Utility function to compute the size of the encoding.
348c095793b4ab027181605c79c9808df12afe45d63Kevin Enderbyuint64_t MCDwarfLineAddr::ComputeSize(int64_t LineDelta, uint64_t AddrDelta) {
349c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  SmallString<256> Tmp;
350c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  raw_svector_ostream OS(Tmp);
351c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OS);
352c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  return OS.GetNumBytesInBuffer();
353c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby}
354c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
355c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby/// Utility function to write the encoding to an object writer.
356c095793b4ab027181605c79c9808df12afe45d63Kevin Enderbyvoid MCDwarfLineAddr::Write(MCObjectWriter *OW, int64_t LineDelta,
357c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby                            uint64_t AddrDelta) {
358c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  SmallString<256> Tmp;
359c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  raw_svector_ostream OS(Tmp);
360c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OS);
361c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  OW->WriteBytes(OS.str());
362c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby}
363c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
364c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby/// Utility function to emit the encoding to a streamer.
365195a0ce484cd12a5adae9184188f6d0fb52b84c0Rafael Espindolavoid MCDwarfLineAddr::Emit(MCStreamer *MCOS, int64_t LineDelta,
366c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby                           uint64_t AddrDelta) {
367c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  SmallString<256> Tmp;
368c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  raw_svector_ostream OS(Tmp);
369c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OS);
370c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCOS->EmitBytes(OS.str(), /*AddrSpace=*/0);
371c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby}
372c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
373c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby/// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
374c095793b4ab027181605c79c9808df12afe45d63Kevin Enderbyvoid MCDwarfLineAddr::Encode(int64_t LineDelta, uint64_t AddrDelta,
375c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby                             raw_ostream &OS) {
376c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  uint64_t Temp, Opcode;
377c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  bool NeedCopy = false;
378c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
379c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Scale the address delta by the minimum instruction length.
380c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  AddrDelta = ScaleAddrDelta(AddrDelta);
381c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
382c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // A LineDelta of INT64_MAX is a signal that this is actually a
383c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // DW_LNE_end_sequence. We cannot use special opcodes here, since we want the
384c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // end_sequence to emit the matrix entry.
385c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  if (LineDelta == INT64_MAX) {
386c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    if (AddrDelta == MAX_SPECIAL_ADDR_DELTA)
387c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      OS << char(dwarf::DW_LNS_const_add_pc);
388c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    else {
389c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      OS << char(dwarf::DW_LNS_advance_pc);
390c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      SmallString<32> Tmp;
391c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      raw_svector_ostream OSE(Tmp);
392c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      MCObjectWriter::EncodeULEB128(AddrDelta, OSE);
393c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      OS << OSE.str();
394c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    }
395c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    OS << char(dwarf::DW_LNS_extended_op);
396c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    OS << char(1);
397c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    OS << char(dwarf::DW_LNE_end_sequence);
398c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    return;
399c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  }
400c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
401c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Bias the line delta by the base.
402c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  Temp = LineDelta - DWARF2_LINE_BASE;
403c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
404c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // If the line increment is out of range of a special opcode, we must encode
405c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // it with DW_LNS_advance_line.
406c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  if (Temp >= DWARF2_LINE_RANGE) {
407c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    OS << char(dwarf::DW_LNS_advance_line);
408c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    SmallString<32> Tmp;
409c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    raw_svector_ostream OSE(Tmp);
410c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    MCObjectWriter::EncodeSLEB128(LineDelta, OSE);
411c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    OS << OSE.str();
412c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
413c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    LineDelta = 0;
414c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    Temp = 0 - DWARF2_LINE_BASE;
415c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    NeedCopy = true;
416c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  }
417c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
418c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Use DW_LNS_copy instead of a "line +0, addr +0" special opcode.
419c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  if (LineDelta == 0 && AddrDelta == 0) {
420c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    OS << char(dwarf::DW_LNS_copy);
421c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    return;
422c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  }
423c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
424c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Bias the opcode by the special opcode base.
425c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  Temp += DWARF2_LINE_OPCODE_BASE;
426c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
427c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Avoid overflow when addr_delta is large.
428c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  if (AddrDelta < 256 + MAX_SPECIAL_ADDR_DELTA) {
429c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    // Try using a special opcode.
430c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    Opcode = Temp + AddrDelta * DWARF2_LINE_RANGE;
431c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    if (Opcode <= 255) {
432c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      OS << char(Opcode);
433c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      return;
434c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    }
435c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
436c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    // Try using DW_LNS_const_add_pc followed by special op.
437c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    Opcode = Temp + (AddrDelta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
438c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    if (Opcode <= 255) {
439c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      OS << char(dwarf::DW_LNS_const_add_pc);
440c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      OS << char(Opcode);
441c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby      return;
442c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    }
443c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  }
444c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
445c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  // Otherwise use DW_LNS_advance_pc.
446c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  OS << char(dwarf::DW_LNS_advance_pc);
447c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  SmallString<32> Tmp;
448c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  raw_svector_ostream OSE(Tmp);
449c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  MCObjectWriter::EncodeULEB128(AddrDelta, OSE);
450c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  OS << OSE.str();
451c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
452c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  if (NeedCopy)
453c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    OS << char(dwarf::DW_LNS_copy);
454c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby  else
455c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby    OS << char(Temp);
456c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby}
457c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
4587cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderbyvoid MCDwarfFile::print(raw_ostream &OS) const {
4597cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby  OS << '"' << getName() << '"';
4607cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby}
4617cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby
4627cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderbyvoid MCDwarfFile::dump() const {
4637cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby  print(dbgs());
4647cbf73a73f296167b6e978dbd919ed249e88eeb5Kevin Enderby}
465c095793b4ab027181605c79c9808df12afe45d63Kevin Enderby
466