Daniel Dunbar | 8a3c9d9 | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 1 | //===- lib/MC/MCObjectStreamer.cpp - Object File MCStreamer Interface -----===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "llvm/MC/MCObjectStreamer.h" |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/STLExtras.h" |
Craig Topper | 6e80c28 | 2012-03-26 06:58:25 +0000 | [diff] [blame] | 12 | #include "llvm/MC/MCAsmBackend.h" |
| 13 | #include "llvm/MC/MCAsmInfo.h" |
Daniel Dunbar | 8a3c9d9 | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCAssembler.h" |
Benjamin Kramer | a3e0ddb | 2010-07-29 17:48:06 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCCodeEmitter.h" |
Rafael Espindola | 0a017a6 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCContext.h" |
Rafael Espindola | 72b5488 | 2010-11-01 16:27:31 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCDwarf.h" |
Michael J. Spencer | e2da0a4 | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCExpr.h" |
Craig Topper | 6e80c28 | 2012-03-26 06:58:25 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCObjectWriter.h" |
Serge Pavlov | 24a3ebb | 2013-06-27 14:35:03 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCSection.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCSymbol.h" |
Craig Topper | 6e80c28 | 2012-03-26 06:58:25 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ErrorHandling.h" |
Daniel Dunbar | 8a3c9d9 | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 23 | using namespace llvm; |
| 24 | |
Rafael Espindola | 24ea09e | 2014-01-26 06:06:37 +0000 | [diff] [blame] | 25 | MCObjectStreamer::MCObjectStreamer(MCContext &Context, MCAsmBackend &TAB, |
| 26 | raw_ostream &OS, MCCodeEmitter *Emitter_) |
| 27 | : MCStreamer(Context), |
Chandler Carruth | de093ef | 2013-01-31 23:29:57 +0000 | [diff] [blame] | 28 | Assembler(new MCAssembler(Context, TAB, *Emitter_, |
| 29 | *TAB.createObjectWriter(OS), OS)), |
| 30 | CurSectionData(0) {} |
Daniel Dunbar | 8a3c9d9 | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 31 | |
Rafael Espindola | 24ea09e | 2014-01-26 06:06:37 +0000 | [diff] [blame] | 32 | MCObjectStreamer::MCObjectStreamer(MCContext &Context, MCAsmBackend &TAB, |
| 33 | raw_ostream &OS, MCCodeEmitter *Emitter_, |
Jan Sjödin | 745fc89 | 2011-03-09 17:33:05 +0000 | [diff] [blame] | 34 | MCAssembler *_Assembler) |
Rafael Espindola | 24ea09e | 2014-01-26 06:06:37 +0000 | [diff] [blame] | 35 | : MCStreamer(Context), Assembler(_Assembler), CurSectionData(0) {} |
Jan Sjödin | 745fc89 | 2011-03-09 17:33:05 +0000 | [diff] [blame] | 36 | |
Daniel Dunbar | 8a3c9d9 | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 37 | MCObjectStreamer::~MCObjectStreamer() { |
Benjamin Kramer | a3e0ddb | 2010-07-29 17:48:06 +0000 | [diff] [blame] | 38 | delete &Assembler->getBackend(); |
| 39 | delete &Assembler->getEmitter(); |
Daniel Dunbar | 42a037a | 2010-12-17 02:45:41 +0000 | [diff] [blame] | 40 | delete &Assembler->getWriter(); |
Daniel Dunbar | 8a3c9d9 | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 41 | delete Assembler; |
| 42 | } |
Daniel Dunbar | b2347fe | 2010-06-16 20:04:25 +0000 | [diff] [blame] | 43 | |
Pedro Artigas | 7212ee4 | 2012-12-12 22:59:46 +0000 | [diff] [blame] | 44 | void MCObjectStreamer::reset() { |
Pedro Artigas | b95c53e | 2012-12-14 18:52:11 +0000 | [diff] [blame] | 45 | if (Assembler) |
| 46 | Assembler->reset(); |
Pedro Artigas | 3383225 | 2013-01-04 18:04:42 +0000 | [diff] [blame] | 47 | CurSectionData = 0; |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 48 | CurInsertionPoint = MCSectionData::iterator(); |
Pedro Artigas | 7212ee4 | 2012-12-12 22:59:46 +0000 | [diff] [blame] | 49 | MCStreamer::reset(); |
| 50 | } |
| 51 | |
Michael J. Spencer | e2da0a4 | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 52 | MCFragment *MCObjectStreamer::getCurrentFragment() const { |
| 53 | assert(getCurrentSectionData() && "No current section!"); |
| 54 | |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 55 | if (CurInsertionPoint != getCurrentSectionData()->getFragmentList().begin()) |
| 56 | return prior(CurInsertionPoint); |
Michael J. Spencer | e2da0a4 | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 57 | |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | MCDataFragment *MCObjectStreamer::getOrCreateDataFragment() const { |
| 62 | MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment()); |
Derek Schuff | 8878bcc9 | 2013-02-15 22:50:52 +0000 | [diff] [blame] | 63 | // When bundling is enabled, we don't want to add data to a fragment that |
| 64 | // already has instructions (see MCELFStreamer::EmitInstToData for details) |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 65 | if (!F || (Assembler->isBundlingEnabled() && F->hasInstructions())) { |
| 66 | F = new MCDataFragment(); |
| 67 | insert(F); |
| 68 | } |
Michael J. Spencer | e2da0a4 | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 69 | return F; |
| 70 | } |
| 71 | |
| 72 | const MCExpr *MCObjectStreamer::AddValueSymbols(const MCExpr *Value) { |
| 73 | switch (Value->getKind()) { |
Evan Cheng | 965b3c7 | 2011-01-13 07:58:56 +0000 | [diff] [blame] | 74 | case MCExpr::Target: |
| 75 | cast<MCTargetExpr>(Value)->AddValueSymbols(Assembler); |
| 76 | break; |
| 77 | |
Michael J. Spencer | e2da0a4 | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 78 | case MCExpr::Constant: |
| 79 | break; |
| 80 | |
| 81 | case MCExpr::Binary: { |
| 82 | const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value); |
| 83 | AddValueSymbols(BE->getLHS()); |
| 84 | AddValueSymbols(BE->getRHS()); |
| 85 | break; |
| 86 | } |
| 87 | |
| 88 | case MCExpr::SymbolRef: |
| 89 | Assembler->getOrCreateSymbolData(cast<MCSymbolRefExpr>(Value)->getSymbol()); |
| 90 | break; |
| 91 | |
| 92 | case MCExpr::Unary: |
| 93 | AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr()); |
| 94 | break; |
| 95 | } |
| 96 | |
| 97 | return Value; |
| 98 | } |
| 99 | |
Rafael Espindola | 64e1af8 | 2013-07-02 15:49:13 +0000 | [diff] [blame] | 100 | void MCObjectStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size) { |
Rafael Espindola | a084fd6 | 2010-11-28 23:08:47 +0000 | [diff] [blame] | 101 | MCDataFragment *DF = getOrCreateDataFragment(); |
| 102 | |
Cameron Zwarich | 80cbcd2 | 2013-05-25 21:56:53 +0000 | [diff] [blame] | 103 | MCLineEntry::Make(this, getCurrentSection().first); |
| 104 | |
Rafael Espindola | a084fd6 | 2010-11-28 23:08:47 +0000 | [diff] [blame] | 105 | // Avoid fixups when possible. |
| 106 | int64_t AbsValue; |
Rafael Espindola | 5004f4a | 2010-12-18 03:57:21 +0000 | [diff] [blame] | 107 | if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue, getAssembler())) { |
Rafael Espindola | 64e1af8 | 2013-07-02 15:49:13 +0000 | [diff] [blame] | 108 | EmitIntValue(AbsValue, Size); |
Rafael Espindola | 4c70eea | 2010-12-03 02:54:21 +0000 | [diff] [blame] | 109 | return; |
Rafael Espindola | a084fd6 | 2010-11-28 23:08:47 +0000 | [diff] [blame] | 110 | } |
Eli Bendersky | a31a894 | 2012-12-07 19:13:57 +0000 | [diff] [blame] | 111 | DF->getFixups().push_back( |
| 112 | MCFixup::Create(DF->getContents().size(), Value, |
| 113 | MCFixup::getKindForSize(Size, false))); |
Rafael Espindola | 4c70eea | 2010-12-03 02:54:21 +0000 | [diff] [blame] | 114 | DF->getContents().resize(DF->getContents().size() + Size, 0); |
Rafael Espindola | a084fd6 | 2010-11-28 23:08:47 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Rafael Espindola | 3824120 | 2012-01-07 22:42:19 +0000 | [diff] [blame] | 117 | void MCObjectStreamer::EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame) { |
| 118 | RecordProcStart(Frame); |
| 119 | } |
| 120 | |
Rafael Espindola | f28213c | 2012-01-09 00:17:29 +0000 | [diff] [blame] | 121 | void MCObjectStreamer::EmitCFIEndProcImpl(MCDwarfFrameInfo &Frame) { |
| 122 | RecordProcEnd(Frame); |
| 123 | } |
| 124 | |
Rafael Espindola | e5b7415 | 2010-11-28 17:18:55 +0000 | [diff] [blame] | 125 | void MCObjectStreamer::EmitLabel(MCSymbol *Symbol) { |
Rafael Espindola | 27c0c9b | 2011-04-27 15:21:19 +0000 | [diff] [blame] | 126 | MCStreamer::EmitLabel(Symbol); |
Rafael Espindola | e5b7415 | 2010-11-28 17:18:55 +0000 | [diff] [blame] | 127 | |
| 128 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); |
| 129 | |
| 130 | // FIXME: This is wasteful, we don't necessarily need to create a data |
| 131 | // fragment. Instead, we should mark the symbol as pointing into the data |
| 132 | // fragment if it exists, otherwise we should just queue the label and set its |
| 133 | // fragment pointer when we emit the next fragment. |
| 134 | MCDataFragment *F = getOrCreateDataFragment(); |
| 135 | assert(!SD.getFragment() && "Unexpected fragment on symbol data!"); |
| 136 | SD.setFragment(F); |
| 137 | SD.setOffset(F->getContents().size()); |
| 138 | } |
| 139 | |
Reed Kotler | aee4d5d1 | 2012-12-16 04:00:45 +0000 | [diff] [blame] | 140 | void MCObjectStreamer::EmitDebugLabel(MCSymbol *Symbol) { |
| 141 | EmitLabel(Symbol); |
| 142 | } |
| 143 | |
Rafael Espindola | 6aea592 | 2011-04-21 23:39:26 +0000 | [diff] [blame] | 144 | void MCObjectStreamer::EmitULEB128Value(const MCExpr *Value) { |
Rafael Espindola | 675fbb2 | 2010-12-03 01:19:49 +0000 | [diff] [blame] | 145 | int64_t IntValue; |
Rafael Espindola | 5004f4a | 2010-12-18 03:57:21 +0000 | [diff] [blame] | 146 | if (Value->EvaluateAsAbsolute(IntValue, getAssembler())) { |
Rafael Espindola | 6aea592 | 2011-04-21 23:39:26 +0000 | [diff] [blame] | 147 | EmitULEB128IntValue(IntValue); |
Rafael Espindola | 675fbb2 | 2010-12-03 01:19:49 +0000 | [diff] [blame] | 148 | return; |
| 149 | } |
Rafael Espindola | 8e12929 | 2011-05-19 21:40:34 +0000 | [diff] [blame] | 150 | Value = ForceExpAbs(Value); |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 151 | insert(new MCLEBFragment(*Value, false)); |
Rafael Espindola | 5e87498 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Rafael Espindola | 6aea592 | 2011-04-21 23:39:26 +0000 | [diff] [blame] | 154 | void MCObjectStreamer::EmitSLEB128Value(const MCExpr *Value) { |
Rafael Espindola | 675fbb2 | 2010-12-03 01:19:49 +0000 | [diff] [blame] | 155 | int64_t IntValue; |
Rafael Espindola | 5004f4a | 2010-12-18 03:57:21 +0000 | [diff] [blame] | 156 | if (Value->EvaluateAsAbsolute(IntValue, getAssembler())) { |
Rafael Espindola | 6aea592 | 2011-04-21 23:39:26 +0000 | [diff] [blame] | 157 | EmitSLEB128IntValue(IntValue); |
Rafael Espindola | 675fbb2 | 2010-12-03 01:19:49 +0000 | [diff] [blame] | 158 | return; |
| 159 | } |
Rafael Espindola | 8e12929 | 2011-05-19 21:40:34 +0000 | [diff] [blame] | 160 | Value = ForceExpAbs(Value); |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 161 | insert(new MCLEBFragment(*Value, true)); |
Rafael Espindola | 5e87498 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Rafael Espindola | 1614597 | 2010-11-01 14:28:48 +0000 | [diff] [blame] | 164 | void MCObjectStreamer::EmitWeakReference(MCSymbol *Alias, |
| 165 | const MCSymbol *Symbol) { |
| 166 | report_fatal_error("This file format doesn't support weak aliases."); |
| 167 | } |
| 168 | |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 169 | void MCObjectStreamer::ChangeSection(const MCSection *Section, |
| 170 | const MCExpr *Subsection) { |
Daniel Dunbar | b2347fe | 2010-06-16 20:04:25 +0000 | [diff] [blame] | 171 | assert(Section && "Cannot switch to a null section!"); |
| 172 | |
Daniel Dunbar | b2347fe | 2010-06-16 20:04:25 +0000 | [diff] [blame] | 173 | CurSectionData = &getAssembler().getOrCreateSectionData(*Section); |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 174 | |
| 175 | int64_t IntSubsection = 0; |
| 176 | if (Subsection && |
| 177 | !Subsection->EvaluateAsAbsolute(IntSubsection, getAssembler())) |
| 178 | report_fatal_error("Cannot evaluate subsection number"); |
| 179 | if (IntSubsection < 0 || IntSubsection > 8192) |
| 180 | report_fatal_error("Subsection number out of range"); |
| 181 | CurInsertionPoint = |
| 182 | CurSectionData->getSubsectionInsertionPoint(unsigned(IntSubsection)); |
Daniel Dunbar | b2347fe | 2010-06-16 20:04:25 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Eli Bendersky | ea2824d | 2012-12-07 17:42:41 +0000 | [diff] [blame] | 185 | void MCObjectStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) { |
| 186 | getAssembler().getOrCreateSymbolData(*Symbol); |
| 187 | Symbol->setVariableValue(AddValueSymbols(Value)); |
| 188 | } |
| 189 | |
David Woodhouse | e6c13e4 | 2014-01-28 23:12:42 +0000 | [diff] [blame] | 190 | void MCObjectStreamer::EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) { |
Rafael Espindola | 72b5488 | 2010-11-01 16:27:31 +0000 | [diff] [blame] | 191 | // Scan for values. |
| 192 | for (unsigned i = Inst.getNumOperands(); i--; ) |
| 193 | if (Inst.getOperand(i).isExpr()) |
| 194 | AddValueSymbols(Inst.getOperand(i).getExpr()); |
| 195 | |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 196 | MCSectionData *SD = getCurrentSectionData(); |
| 197 | SD->setHasInstructions(true); |
Rafael Espindola | 72b5488 | 2010-11-01 16:27:31 +0000 | [diff] [blame] | 198 | |
| 199 | // Now that a machine instruction has been assembled into this section, make |
| 200 | // a line entry for any .loc directive that has been seen. |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 201 | MCLineEntry::Make(this, getCurrentSection().first); |
Rafael Espindola | 72b5488 | 2010-11-01 16:27:31 +0000 | [diff] [blame] | 202 | |
| 203 | // If this instruction doesn't need relaxation, just emit it as data. |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 204 | MCAssembler &Assembler = getAssembler(); |
| 205 | if (!Assembler.getBackend().mayNeedRelaxation(Inst)) { |
David Woodhouse | 6f3c73f | 2014-01-28 23:12:49 +0000 | [diff] [blame] | 206 | EmitInstToData(Inst, STI); |
Rafael Espindola | 72b5488 | 2010-11-01 16:27:31 +0000 | [diff] [blame] | 207 | return; |
| 208 | } |
| 209 | |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 210 | // Otherwise, relax and emit it as data if either: |
| 211 | // - The RelaxAll flag was passed |
| 212 | // - Bundling is enabled and this instruction is inside a bundle-locked |
| 213 | // group. We want to emit all such instructions into the same data |
| 214 | // fragment. |
| 215 | if (Assembler.getRelaxAll() || |
| 216 | (Assembler.isBundlingEnabled() && SD->isBundleLocked())) { |
Rafael Espindola | 72b5488 | 2010-11-01 16:27:31 +0000 | [diff] [blame] | 217 | MCInst Relaxed; |
Jim Grosbach | aba3de9 | 2012-01-18 18:52:16 +0000 | [diff] [blame] | 218 | getAssembler().getBackend().relaxInstruction(Inst, Relaxed); |
| 219 | while (getAssembler().getBackend().mayNeedRelaxation(Relaxed)) |
| 220 | getAssembler().getBackend().relaxInstruction(Relaxed, Relaxed); |
David Woodhouse | 6f3c73f | 2014-01-28 23:12:49 +0000 | [diff] [blame] | 221 | EmitInstToData(Relaxed, STI); |
Rafael Espindola | 72b5488 | 2010-11-01 16:27:31 +0000 | [diff] [blame] | 222 | return; |
| 223 | } |
| 224 | |
| 225 | // Otherwise emit to a separate fragment. |
David Woodhouse | 6f3c73f | 2014-01-28 23:12:49 +0000 | [diff] [blame] | 226 | EmitInstToFragment(Inst, STI); |
Rafael Espindola | 72b5488 | 2010-11-01 16:27:31 +0000 | [diff] [blame] | 227 | } |
| 228 | |
David Woodhouse | 6f3c73f | 2014-01-28 23:12:49 +0000 | [diff] [blame] | 229 | void MCObjectStreamer::EmitInstToFragment(const MCInst &Inst, |
| 230 | const MCSubtargetInfo &STI) { |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 231 | // Always create a new, separate fragment here, because its size can change |
| 232 | // during relaxation. |
David Woodhouse | f5199f6 | 2014-01-28 23:12:53 +0000 | [diff] [blame] | 233 | MCRelaxableFragment *IF = new MCRelaxableFragment(Inst, STI); |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 234 | insert(IF); |
Rafael Espindola | 3fa6fc1 | 2010-12-02 05:44:06 +0000 | [diff] [blame] | 235 | |
Eli Friedman | b2545fb | 2011-04-18 20:54:46 +0000 | [diff] [blame] | 236 | SmallString<128> Code; |
| 237 | raw_svector_ostream VecOS(Code); |
David Woodhouse | 9784cef | 2014-01-28 23:13:07 +0000 | [diff] [blame^] | 238 | getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, IF->getFixups(), |
| 239 | STI); |
Eli Friedman | b2545fb | 2011-04-18 20:54:46 +0000 | [diff] [blame] | 240 | VecOS.flush(); |
Eli Bendersky | a31a894 | 2012-12-07 19:13:57 +0000 | [diff] [blame] | 241 | IF->getContents().append(Code.begin(), Code.end()); |
Rafael Espindola | 3fa6fc1 | 2010-12-02 05:44:06 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Matt Beaumont-Gay | 5ae7208 | 2013-02-15 23:12:33 +0000 | [diff] [blame] | 244 | #ifndef NDEBUG |
Craig Topper | d3a34f8 | 2013-07-16 01:17:10 +0000 | [diff] [blame] | 245 | static const char *const BundlingNotImplementedMsg = |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 246 | "Aligned bundling is not implemented for this object format"; |
Matt Beaumont-Gay | 5ae7208 | 2013-02-15 23:12:33 +0000 | [diff] [blame] | 247 | #endif |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 248 | |
| 249 | void MCObjectStreamer::EmitBundleAlignMode(unsigned AlignPow2) { |
| 250 | llvm_unreachable(BundlingNotImplementedMsg); |
| 251 | } |
| 252 | |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 253 | void MCObjectStreamer::EmitBundleLock(bool AlignToEnd) { |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 254 | llvm_unreachable(BundlingNotImplementedMsg); |
| 255 | } |
| 256 | |
| 257 | void MCObjectStreamer::EmitBundleUnlock() { |
| 258 | llvm_unreachable(BundlingNotImplementedMsg); |
| 259 | } |
| 260 | |
Ulrich Weigand | 64f4405 | 2013-06-19 21:27:27 +0000 | [diff] [blame] | 261 | void MCObjectStreamer::EmitDwarfLocDirective(unsigned FileNo, unsigned Line, |
| 262 | unsigned Column, unsigned Flags, |
| 263 | unsigned Isa, |
| 264 | unsigned Discriminator, |
| 265 | StringRef FileName) { |
| 266 | // In case we see two .loc directives in a row, make sure the |
| 267 | // first one gets a line entry. |
| 268 | MCLineEntry::Make(this, getCurrentSection().first); |
| 269 | |
| 270 | this->MCStreamer::EmitDwarfLocDirective(FileNo, Line, Column, Flags, |
| 271 | Isa, Discriminator, FileName); |
| 272 | } |
| 273 | |
Rafael Espindola | 57ab708 | 2010-12-03 00:55:40 +0000 | [diff] [blame] | 274 | void MCObjectStreamer::EmitDwarfAdvanceLineAddr(int64_t LineDelta, |
| 275 | const MCSymbol *LastLabel, |
Evan Cheng | c7ac690 | 2011-07-14 05:43:07 +0000 | [diff] [blame] | 276 | const MCSymbol *Label, |
| 277 | unsigned PointerSize) { |
Rafael Espindola | 57ab708 | 2010-12-03 00:55:40 +0000 | [diff] [blame] | 278 | if (!LastLabel) { |
Rafael Espindola | 57ab708 | 2010-12-03 00:55:40 +0000 | [diff] [blame] | 279 | EmitDwarfSetLineAddr(LineDelta, Label, PointerSize); |
| 280 | return; |
| 281 | } |
Rafael Espindola | 736a35d | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 282 | const MCExpr *AddrDelta = BuildSymbolDiff(getContext(), Label, LastLabel); |
Rafael Espindola | 57ab708 | 2010-12-03 00:55:40 +0000 | [diff] [blame] | 283 | int64_t Res; |
Rafael Espindola | 5004f4a | 2010-12-18 03:57:21 +0000 | [diff] [blame] | 284 | if (AddrDelta->EvaluateAsAbsolute(Res, getAssembler())) { |
Rafael Espindola | 57ab708 | 2010-12-03 00:55:40 +0000 | [diff] [blame] | 285 | MCDwarfLineAddr::Emit(this, LineDelta, Res); |
| 286 | return; |
| 287 | } |
Rafael Espindola | 8e12929 | 2011-05-19 21:40:34 +0000 | [diff] [blame] | 288 | AddrDelta = ForceExpAbs(AddrDelta); |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 289 | insert(new MCDwarfLineAddrFragment(LineDelta, *AddrDelta)); |
Rafael Espindola | 57ab708 | 2010-12-03 00:55:40 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Rafael Espindola | 736a35d | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 292 | void MCObjectStreamer::EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel, |
| 293 | const MCSymbol *Label) { |
| 294 | const MCExpr *AddrDelta = BuildSymbolDiff(getContext(), Label, LastLabel); |
| 295 | int64_t Res; |
| 296 | if (AddrDelta->EvaluateAsAbsolute(Res, getAssembler())) { |
| 297 | MCDwarfFrameEmitter::EmitAdvanceLoc(*this, Res); |
| 298 | return; |
| 299 | } |
Rafael Espindola | 8e12929 | 2011-05-19 21:40:34 +0000 | [diff] [blame] | 300 | AddrDelta = ForceExpAbs(AddrDelta); |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 301 | insert(new MCDwarfCallFrameFragment(*AddrDelta)); |
Rafael Espindola | 736a35d | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Rafael Espindola | 64e1af8 | 2013-07-02 15:49:13 +0000 | [diff] [blame] | 304 | void MCObjectStreamer::EmitBytes(StringRef Data) { |
Peter Collingbourne | 588009e | 2013-10-20 02:16:18 +0000 | [diff] [blame] | 305 | MCLineEntry::Make(this, getCurrentSection().first); |
Benjamin Kramer | 64ddcb0 | 2012-10-04 13:12:43 +0000 | [diff] [blame] | 306 | getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end()); |
| 307 | } |
| 308 | |
| 309 | void MCObjectStreamer::EmitValueToAlignment(unsigned ByteAlignment, |
| 310 | int64_t Value, |
| 311 | unsigned ValueSize, |
| 312 | unsigned MaxBytesToEmit) { |
| 313 | if (MaxBytesToEmit == 0) |
| 314 | MaxBytesToEmit = ByteAlignment; |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 315 | insert(new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit)); |
Benjamin Kramer | 64ddcb0 | 2012-10-04 13:12:43 +0000 | [diff] [blame] | 316 | |
| 317 | // Update the maximum alignment on the current section if necessary. |
| 318 | if (ByteAlignment > getCurrentSectionData()->getAlignment()) |
| 319 | getCurrentSectionData()->setAlignment(ByteAlignment); |
| 320 | } |
| 321 | |
| 322 | void MCObjectStreamer::EmitCodeAlignment(unsigned ByteAlignment, |
| 323 | unsigned MaxBytesToEmit) { |
| 324 | EmitValueToAlignment(ByteAlignment, 0, 1, MaxBytesToEmit); |
| 325 | cast<MCAlignFragment>(getCurrentFragment())->setEmitNops(true); |
| 326 | } |
| 327 | |
Jim Grosbach | b591277 | 2012-01-27 00:37:08 +0000 | [diff] [blame] | 328 | bool MCObjectStreamer::EmitValueToOffset(const MCExpr *Offset, |
Jim Grosbach | 5aac954 | 2012-01-26 23:47:45 +0000 | [diff] [blame] | 329 | unsigned char Value) { |
Rafael Espindola | f849bf8 | 2011-02-20 20:20:07 +0000 | [diff] [blame] | 330 | int64_t Res; |
| 331 | if (Offset->EvaluateAsAbsolute(Res, getAssembler())) { |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 332 | insert(new MCOrgFragment(*Offset, Value)); |
Jim Grosbach | b591277 | 2012-01-27 00:37:08 +0000 | [diff] [blame] | 333 | return false; |
Rafael Espindola | f849bf8 | 2011-02-20 20:20:07 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | MCSymbol *CurrentPos = getContext().CreateTempSymbol(); |
| 337 | EmitLabel(CurrentPos); |
| 338 | MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; |
| 339 | const MCExpr *Ref = |
| 340 | MCSymbolRefExpr::Create(CurrentPos, Variant, getContext()); |
| 341 | const MCExpr *Delta = |
| 342 | MCBinaryExpr::Create(MCBinaryExpr::Sub, Offset, Ref, getContext()); |
| 343 | |
| 344 | if (!Delta->EvaluateAsAbsolute(Res, getAssembler())) |
Jim Grosbach | b591277 | 2012-01-27 00:37:08 +0000 | [diff] [blame] | 345 | return true; |
Eric Christopher | ce0cfce | 2013-01-09 01:35:34 +0000 | [diff] [blame] | 346 | EmitFill(Res, Value); |
Jim Grosbach | b591277 | 2012-01-27 00:37:08 +0000 | [diff] [blame] | 347 | return false; |
Rafael Espindola | 9065bfc6 | 2010-12-02 05:59:38 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Akira Hatanaka | f5ddf13 | 2011-11-23 22:18:04 +0000 | [diff] [blame] | 350 | // Associate GPRel32 fixup with data and resize data area |
| 351 | void MCObjectStreamer::EmitGPRel32Value(const MCExpr *Value) { |
| 352 | MCDataFragment *DF = getOrCreateDataFragment(); |
| 353 | |
Eli Bendersky | a31a894 | 2012-12-07 19:13:57 +0000 | [diff] [blame] | 354 | DF->getFixups().push_back(MCFixup::Create(DF->getContents().size(), |
| 355 | Value, FK_GPRel_4)); |
Akira Hatanaka | f5ddf13 | 2011-11-23 22:18:04 +0000 | [diff] [blame] | 356 | DF->getContents().resize(DF->getContents().size() + 4, 0); |
| 357 | } |
| 358 | |
Jack Carter | 77064c0 | 2012-08-22 00:49:30 +0000 | [diff] [blame] | 359 | // Associate GPRel32 fixup with data and resize data area |
| 360 | void MCObjectStreamer::EmitGPRel64Value(const MCExpr *Value) { |
| 361 | MCDataFragment *DF = getOrCreateDataFragment(); |
| 362 | |
Eli Bendersky | a31a894 | 2012-12-07 19:13:57 +0000 | [diff] [blame] | 363 | DF->getFixups().push_back(MCFixup::Create(DF->getContents().size(), |
| 364 | Value, FK_GPRel_4)); |
Jack Carter | 77064c0 | 2012-08-22 00:49:30 +0000 | [diff] [blame] | 365 | DF->getContents().resize(DF->getContents().size() + 8, 0); |
| 366 | } |
| 367 | |
Rafael Espindola | 64e1af8 | 2013-07-02 15:49:13 +0000 | [diff] [blame] | 368 | void MCObjectStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue) { |
Benjamin Kramer | 4c8af55 | 2012-10-01 15:14:14 +0000 | [diff] [blame] | 369 | // FIXME: A MCFillFragment would be more memory efficient but MCExpr has |
| 370 | // problems evaluating expressions across multiple fragments. |
| 371 | getOrCreateDataFragment()->getContents().append(NumBytes, FillValue); |
| 372 | } |
| 373 | |
Rafael Espindola | 64e1af8 | 2013-07-02 15:49:13 +0000 | [diff] [blame] | 374 | void MCObjectStreamer::EmitZeros(uint64_t NumBytes) { |
Serge Pavlov | 24a3ebb | 2013-06-27 14:35:03 +0000 | [diff] [blame] | 375 | unsigned ItemSize = getCurrentSection().first->isVirtualSection() ? 0 : 1; |
| 376 | insert(new MCFillFragment(0, ItemSize, NumBytes)); |
| 377 | } |
| 378 | |
Rafael Espindola | 0708209 | 2012-01-07 03:13:18 +0000 | [diff] [blame] | 379 | void MCObjectStreamer::FinishImpl() { |
Rafael Espindola | 0a017a6 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 380 | // Dump out the dwarf file & directory tables and line tables. |
Rafael Espindola | c7ecbfb | 2012-03-03 14:24:15 +0000 | [diff] [blame] | 381 | const MCSymbol *LineSectionSymbol = NULL; |
Rafael Espindola | 0a017a6 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 382 | if (getContext().hasDwarfFiles()) |
Rafael Espindola | c22c85c | 2012-02-28 21:13:05 +0000 | [diff] [blame] | 383 | LineSectionSymbol = MCDwarfFileTable::Emit(this); |
Rafael Espindola | 0a017a6 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 384 | |
Kevin Enderby | e7739d4 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 385 | // If we are generating dwarf for assembly source files dump out the sections. |
| 386 | if (getContext().getGenDwarfForAssembly()) |
Rafael Espindola | c22c85c | 2012-02-28 21:13:05 +0000 | [diff] [blame] | 387 | MCGenDwarfInfo::Emit(this, LineSectionSymbol); |
Kevin Enderby | e7739d4 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 388 | |
Daniel Dunbar | b2347fe | 2010-06-16 20:04:25 +0000 | [diff] [blame] | 389 | getAssembler().Finish(); |
| 390 | } |