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" |
Rafael Espindola | 105270f | 2015-03-18 19:08:20 +0000 | [diff] [blame] | 23 | #include "llvm/Support/TargetRegistry.h" |
Daniel Dunbar | 8a3c9d9 | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | |
Rafael Espindola | 24ea09e | 2014-01-26 06:06:37 +0000 | [diff] [blame] | 26 | MCObjectStreamer::MCObjectStreamer(MCContext &Context, MCAsmBackend &TAB, |
Rafael Espindola | 5560a4c | 2015-04-14 22:14:34 +0000 | [diff] [blame] | 27 | raw_pwrite_stream &OS, |
| 28 | MCCodeEmitter *Emitter_) |
Rafael Espindola | 24ea09e | 2014-01-26 06:06:37 +0000 | [diff] [blame] | 29 | : MCStreamer(Context), |
Chandler Carruth | de093ef | 2013-01-31 23:29:57 +0000 | [diff] [blame] | 30 | Assembler(new MCAssembler(Context, TAB, *Emitter_, |
| 31 | *TAB.createObjectWriter(OS), OS)), |
Rafael Espindola | 3dd8ef6 | 2014-05-12 14:02:44 +0000 | [diff] [blame] | 32 | CurSectionData(nullptr), EmitEHFrame(true), EmitDebugFrame(false) {} |
Daniel Dunbar | 8a3c9d9 | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 33 | |
| 34 | MCObjectStreamer::~MCObjectStreamer() { |
Benjamin Kramer | a3e0ddb | 2010-07-29 17:48:06 +0000 | [diff] [blame] | 35 | delete &Assembler->getBackend(); |
| 36 | delete &Assembler->getEmitter(); |
Daniel Dunbar | 42a037a | 2010-12-17 02:45:41 +0000 | [diff] [blame] | 37 | delete &Assembler->getWriter(); |
Daniel Dunbar | 8a3c9d9 | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 38 | delete Assembler; |
| 39 | } |
Daniel Dunbar | b2347fe | 2010-06-16 20:04:25 +0000 | [diff] [blame] | 40 | |
Petr Hosek | 9e0c890 | 2015-04-12 23:42:25 +0000 | [diff] [blame] | 41 | void MCObjectStreamer::flushPendingLabels(MCFragment *F, uint64_t FOffset) { |
Derek Schuff | 5f708e5 | 2014-10-22 22:38:06 +0000 | [diff] [blame] | 42 | if (PendingLabels.size()) { |
| 43 | if (!F) { |
| 44 | F = new MCDataFragment(); |
| 45 | CurSectionData->getFragmentList().insert(CurInsertionPoint, F); |
| 46 | F->setParent(CurSectionData); |
| 47 | } |
| 48 | for (MCSymbolData *SD : PendingLabels) { |
| 49 | SD->setFragment(F); |
Petr Hosek | 9e0c890 | 2015-04-12 23:42:25 +0000 | [diff] [blame] | 50 | SD->setOffset(FOffset); |
Derek Schuff | 5f708e5 | 2014-10-22 22:38:06 +0000 | [diff] [blame] | 51 | } |
| 52 | PendingLabels.clear(); |
| 53 | } |
| 54 | } |
| 55 | |
Duncan P. N. Exon Smith | 0b73d71 | 2015-05-21 02:41:23 +0000 | [diff] [blame^] | 56 | bool MCObjectStreamer::emitAbsoluteSymbolDiff(const MCSymbol *Hi, |
| 57 | const MCSymbol *Lo, |
| 58 | unsigned Size) { |
| 59 | // Must have symbol data. |
| 60 | if (!Assembler->hasSymbolData(*Hi) || !Assembler->hasSymbolData(*Lo)) |
| 61 | return false; |
| 62 | auto &HiD = Assembler->getSymbolData(*Hi); |
| 63 | auto &LoD = Assembler->getSymbolData(*Lo); |
| 64 | |
| 65 | // Must both be assigned to the same (valid) fragment. |
| 66 | if (!HiD.getFragment() || HiD.getFragment() != LoD.getFragment()) |
| 67 | return false; |
| 68 | |
| 69 | // Must be a data fragment. |
| 70 | if (!isa<MCDataFragment>(HiD.getFragment())) |
| 71 | return false; |
| 72 | |
| 73 | assert(HiD.getOffset() >= LoD.getOffset() && |
| 74 | "Expected Hi to be greater than Lo"); |
| 75 | EmitIntValue(HiD.getOffset() - LoD.getOffset(), Size); |
| 76 | return true; |
| 77 | } |
| 78 | |
Pedro Artigas | 7212ee4 | 2012-12-12 22:59:46 +0000 | [diff] [blame] | 79 | void MCObjectStreamer::reset() { |
Pedro Artigas | b95c53e | 2012-12-14 18:52:11 +0000 | [diff] [blame] | 80 | if (Assembler) |
| 81 | Assembler->reset(); |
Craig Topper | bb694de | 2014-04-13 04:57:38 +0000 | [diff] [blame] | 82 | CurSectionData = nullptr; |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 83 | CurInsertionPoint = MCSectionData::iterator(); |
Rafael Espindola | 3dd8ef6 | 2014-05-12 14:02:44 +0000 | [diff] [blame] | 84 | EmitEHFrame = true; |
| 85 | EmitDebugFrame = false; |
Derek Schuff | 5f708e5 | 2014-10-22 22:38:06 +0000 | [diff] [blame] | 86 | PendingLabels.clear(); |
Pedro Artigas | 7212ee4 | 2012-12-12 22:59:46 +0000 | [diff] [blame] | 87 | MCStreamer::reset(); |
| 88 | } |
| 89 | |
Rafael Espindola | 3dd8ef6 | 2014-05-12 14:02:44 +0000 | [diff] [blame] | 90 | void MCObjectStreamer::EmitFrames(MCAsmBackend *MAB) { |
| 91 | if (!getNumFrameInfos()) |
| 92 | return; |
| 93 | |
| 94 | if (EmitEHFrame) |
| 95 | MCDwarfFrameEmitter::Emit(*this, MAB, true); |
| 96 | |
| 97 | if (EmitDebugFrame) |
| 98 | MCDwarfFrameEmitter::Emit(*this, MAB, false); |
| 99 | } |
| 100 | |
Michael J. Spencer | e2da0a4 | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 101 | MCFragment *MCObjectStreamer::getCurrentFragment() const { |
| 102 | assert(getCurrentSectionData() && "No current section!"); |
| 103 | |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 104 | if (CurInsertionPoint != getCurrentSectionData()->getFragmentList().begin()) |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 105 | return std::prev(CurInsertionPoint); |
Michael J. Spencer | e2da0a4 | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 106 | |
Craig Topper | bb694de | 2014-04-13 04:57:38 +0000 | [diff] [blame] | 107 | return nullptr; |
Michael J. Spencer | e2da0a4 | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Derek Schuff | 5f708e5 | 2014-10-22 22:38:06 +0000 | [diff] [blame] | 110 | MCDataFragment *MCObjectStreamer::getOrCreateDataFragment() { |
Michael J. Spencer | e2da0a4 | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 111 | MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment()); |
Derek Schuff | 8878bcc9 | 2013-02-15 22:50:52 +0000 | [diff] [blame] | 112 | // When bundling is enabled, we don't want to add data to a fragment that |
| 113 | // already has instructions (see MCELFStreamer::EmitInstToData for details) |
Petr Hosek | 9e0c890 | 2015-04-12 23:42:25 +0000 | [diff] [blame] | 114 | if (!F || (Assembler->isBundlingEnabled() && !Assembler->getRelaxAll() && |
| 115 | F->hasInstructions())) { |
David Blaikie | 4d3b043 | 2014-04-10 21:53:47 +0000 | [diff] [blame] | 116 | F = new MCDataFragment(); |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 117 | insert(F); |
| 118 | } |
Michael J. Spencer | e2da0a4 | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 119 | return F; |
| 120 | } |
| 121 | |
Rafael Espindola | 2be1281 | 2014-06-25 15:29:54 +0000 | [diff] [blame] | 122 | void MCObjectStreamer::visitUsedSymbol(const MCSymbol &Sym) { |
| 123 | Assembler->getOrCreateSymbolData(Sym); |
| 124 | } |
| 125 | |
Rafael Espindola | 3dd8ef6 | 2014-05-12 14:02:44 +0000 | [diff] [blame] | 126 | void MCObjectStreamer::EmitCFISections(bool EH, bool Debug) { |
| 127 | MCStreamer::EmitCFISections(EH, Debug); |
| 128 | EmitEHFrame = EH; |
| 129 | EmitDebugFrame = Debug; |
| 130 | } |
| 131 | |
Kevin Enderby | 96918bc | 2014-04-22 17:27:29 +0000 | [diff] [blame] | 132 | void MCObjectStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size, |
| 133 | const SMLoc &Loc) { |
Rafael Espindola | 591c641 | 2014-06-25 18:37:33 +0000 | [diff] [blame] | 134 | MCStreamer::EmitValueImpl(Value, Size, Loc); |
Rafael Espindola | a084fd6 | 2010-11-28 23:08:47 +0000 | [diff] [blame] | 135 | MCDataFragment *DF = getOrCreateDataFragment(); |
| 136 | |
Cameron Zwarich | 80cbcd2 | 2013-05-25 21:56:53 +0000 | [diff] [blame] | 137 | MCLineEntry::Make(this, getCurrentSection().first); |
| 138 | |
Rafael Espindola | a084fd6 | 2010-11-28 23:08:47 +0000 | [diff] [blame] | 139 | // Avoid fixups when possible. |
| 140 | int64_t AbsValue; |
Rafael Espindola | 9364ac6 | 2014-06-25 14:42:14 +0000 | [diff] [blame] | 141 | if (Value->EvaluateAsAbsolute(AbsValue, getAssembler())) { |
Rafael Espindola | 64e1af8 | 2013-07-02 15:49:13 +0000 | [diff] [blame] | 142 | EmitIntValue(AbsValue, Size); |
Rafael Espindola | 4c70eea | 2010-12-03 02:54:21 +0000 | [diff] [blame] | 143 | return; |
Rafael Espindola | a084fd6 | 2010-11-28 23:08:47 +0000 | [diff] [blame] | 144 | } |
Eli Bendersky | a31a894 | 2012-12-07 19:13:57 +0000 | [diff] [blame] | 145 | DF->getFixups().push_back( |
Jim Grosbach | 63661f8 | 2015-05-15 19:13:05 +0000 | [diff] [blame] | 146 | MCFixup::create(DF->getContents().size(), Value, |
Kevin Enderby | 96918bc | 2014-04-22 17:27:29 +0000 | [diff] [blame] | 147 | MCFixup::getKindForSize(Size, false), Loc)); |
Rafael Espindola | 4c70eea | 2010-12-03 02:54:21 +0000 | [diff] [blame] | 148 | DF->getContents().resize(DF->getContents().size() + Size, 0); |
Rafael Espindola | a084fd6 | 2010-11-28 23:08:47 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Oliver Stannard | cf6bfb1 | 2014-11-03 12:19:03 +0000 | [diff] [blame] | 151 | void MCObjectStreamer::EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame) { |
| 152 | // We need to create a local symbol to avoid relocations. |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 153 | Frame.Begin = getContext().createTempSymbol(); |
Oliver Stannard | cf6bfb1 | 2014-11-03 12:19:03 +0000 | [diff] [blame] | 154 | EmitLabel(Frame.Begin); |
Rafael Espindola | 3824120 | 2012-01-07 22:42:19 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Rafael Espindola | f28213c | 2012-01-09 00:17:29 +0000 | [diff] [blame] | 157 | void MCObjectStreamer::EmitCFIEndProcImpl(MCDwarfFrameInfo &Frame) { |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 158 | Frame.End = getContext().createTempSymbol(); |
Rafael Espindola | 49bbfd0 | 2014-06-25 00:13:59 +0000 | [diff] [blame] | 159 | EmitLabel(Frame.End); |
Rafael Espindola | f28213c | 2012-01-09 00:17:29 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Rafael Espindola | e5b7415 | 2010-11-28 17:18:55 +0000 | [diff] [blame] | 162 | void MCObjectStreamer::EmitLabel(MCSymbol *Symbol) { |
Rafael Espindola | 27c0c9b | 2011-04-27 15:21:19 +0000 | [diff] [blame] | 163 | MCStreamer::EmitLabel(Symbol); |
Rafael Espindola | e5b7415 | 2010-11-28 17:18:55 +0000 | [diff] [blame] | 164 | |
| 165 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); |
Rafael Espindola | e5b7415 | 2010-11-28 17:18:55 +0000 | [diff] [blame] | 166 | assert(!SD.getFragment() && "Unexpected fragment on symbol data!"); |
Derek Schuff | 5f708e5 | 2014-10-22 22:38:06 +0000 | [diff] [blame] | 167 | |
| 168 | // If there is a current fragment, mark the symbol as pointing into it. |
| 169 | // Otherwise queue the label and set its fragment pointer when we emit the |
| 170 | // next fragment. |
Petr Hosek | 9e0c890 | 2015-04-12 23:42:25 +0000 | [diff] [blame] | 171 | auto *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment()); |
| 172 | if (F && !(getAssembler().isBundlingEnabled() && |
| 173 | getAssembler().getRelaxAll())) { |
Derek Schuff | 5f708e5 | 2014-10-22 22:38:06 +0000 | [diff] [blame] | 174 | SD.setFragment(F); |
| 175 | SD.setOffset(F->getContents().size()); |
| 176 | } else { |
| 177 | PendingLabels.push_back(&SD); |
| 178 | } |
Rafael Espindola | e5b7415 | 2010-11-28 17:18:55 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Rafael Espindola | 6aea592 | 2011-04-21 23:39:26 +0000 | [diff] [blame] | 181 | void MCObjectStreamer::EmitULEB128Value(const MCExpr *Value) { |
Rafael Espindola | 675fbb2 | 2010-12-03 01:19:49 +0000 | [diff] [blame] | 182 | int64_t IntValue; |
Rafael Espindola | 5004f4a | 2010-12-18 03:57:21 +0000 | [diff] [blame] | 183 | if (Value->EvaluateAsAbsolute(IntValue, getAssembler())) { |
Rafael Espindola | 6aea592 | 2011-04-21 23:39:26 +0000 | [diff] [blame] | 184 | EmitULEB128IntValue(IntValue); |
Rafael Espindola | 675fbb2 | 2010-12-03 01:19:49 +0000 | [diff] [blame] | 185 | return; |
| 186 | } |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 187 | insert(new MCLEBFragment(*Value, false)); |
Rafael Espindola | 5e87498 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Rafael Espindola | 6aea592 | 2011-04-21 23:39:26 +0000 | [diff] [blame] | 190 | void MCObjectStreamer::EmitSLEB128Value(const MCExpr *Value) { |
Rafael Espindola | 675fbb2 | 2010-12-03 01:19:49 +0000 | [diff] [blame] | 191 | int64_t IntValue; |
Rafael Espindola | 5004f4a | 2010-12-18 03:57:21 +0000 | [diff] [blame] | 192 | if (Value->EvaluateAsAbsolute(IntValue, getAssembler())) { |
Rafael Espindola | 6aea592 | 2011-04-21 23:39:26 +0000 | [diff] [blame] | 193 | EmitSLEB128IntValue(IntValue); |
Rafael Espindola | 675fbb2 | 2010-12-03 01:19:49 +0000 | [diff] [blame] | 194 | return; |
| 195 | } |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 196 | insert(new MCLEBFragment(*Value, true)); |
Rafael Espindola | 5e87498 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Rafael Espindola | 1614597 | 2010-11-01 14:28:48 +0000 | [diff] [blame] | 199 | void MCObjectStreamer::EmitWeakReference(MCSymbol *Alias, |
| 200 | const MCSymbol *Symbol) { |
| 201 | report_fatal_error("This file format doesn't support weak aliases."); |
| 202 | } |
| 203 | |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 204 | void MCObjectStreamer::ChangeSection(const MCSection *Section, |
| 205 | const MCExpr *Subsection) { |
Rafael Espindola | 36a15cb | 2015-03-20 20:00:01 +0000 | [diff] [blame] | 206 | changeSectionImpl(Section, Subsection); |
| 207 | } |
| 208 | |
| 209 | bool MCObjectStreamer::changeSectionImpl(const MCSection *Section, |
| 210 | const MCExpr *Subsection) { |
Daniel Dunbar | b2347fe | 2010-06-16 20:04:25 +0000 | [diff] [blame] | 211 | assert(Section && "Cannot switch to a null section!"); |
Derek Schuff | 5f708e5 | 2014-10-22 22:38:06 +0000 | [diff] [blame] | 212 | flushPendingLabels(nullptr); |
Daniel Dunbar | b2347fe | 2010-06-16 20:04:25 +0000 | [diff] [blame] | 213 | |
Rafael Espindola | 36a15cb | 2015-03-20 20:00:01 +0000 | [diff] [blame] | 214 | bool Created; |
| 215 | CurSectionData = &getAssembler().getOrCreateSectionData(*Section, &Created); |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 216 | |
| 217 | int64_t IntSubsection = 0; |
| 218 | if (Subsection && |
| 219 | !Subsection->EvaluateAsAbsolute(IntSubsection, getAssembler())) |
| 220 | report_fatal_error("Cannot evaluate subsection number"); |
| 221 | if (IntSubsection < 0 || IntSubsection > 8192) |
| 222 | report_fatal_error("Subsection number out of range"); |
| 223 | CurInsertionPoint = |
| 224 | CurSectionData->getSubsectionInsertionPoint(unsigned(IntSubsection)); |
Rafael Espindola | 36a15cb | 2015-03-20 20:00:01 +0000 | [diff] [blame] | 225 | return Created; |
Daniel Dunbar | b2347fe | 2010-06-16 20:04:25 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Eli Bendersky | ea2824d | 2012-12-07 17:42:41 +0000 | [diff] [blame] | 228 | void MCObjectStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) { |
| 229 | getAssembler().getOrCreateSymbolData(*Symbol); |
Zoran Jovanovic | 28221d8 | 2014-03-20 09:44:49 +0000 | [diff] [blame] | 230 | MCStreamer::EmitAssignment(Symbol, Value); |
Eli Bendersky | ea2824d | 2012-12-07 17:42:41 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Rafael Espindola | 591c641 | 2014-06-25 18:37:33 +0000 | [diff] [blame] | 233 | void MCObjectStreamer::EmitInstruction(const MCInst &Inst, |
| 234 | const MCSubtargetInfo &STI) { |
| 235 | MCStreamer::EmitInstruction(Inst, STI); |
Rafael Espindola | 72b5488 | 2010-11-01 16:27:31 +0000 | [diff] [blame] | 236 | |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 237 | MCSectionData *SD = getCurrentSectionData(); |
| 238 | SD->setHasInstructions(true); |
Rafael Espindola | 72b5488 | 2010-11-01 16:27:31 +0000 | [diff] [blame] | 239 | |
| 240 | // Now that a machine instruction has been assembled into this section, make |
| 241 | // a line entry for any .loc directive that has been seen. |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 242 | MCLineEntry::Make(this, getCurrentSection().first); |
Rafael Espindola | 72b5488 | 2010-11-01 16:27:31 +0000 | [diff] [blame] | 243 | |
| 244 | // If this instruction doesn't need relaxation, just emit it as data. |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 245 | MCAssembler &Assembler = getAssembler(); |
| 246 | if (!Assembler.getBackend().mayNeedRelaxation(Inst)) { |
David Woodhouse | 6f3c73f | 2014-01-28 23:12:49 +0000 | [diff] [blame] | 247 | EmitInstToData(Inst, STI); |
Rafael Espindola | 72b5488 | 2010-11-01 16:27:31 +0000 | [diff] [blame] | 248 | return; |
| 249 | } |
| 250 | |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 251 | // Otherwise, relax and emit it as data if either: |
| 252 | // - The RelaxAll flag was passed |
| 253 | // - Bundling is enabled and this instruction is inside a bundle-locked |
| 254 | // group. We want to emit all such instructions into the same data |
| 255 | // fragment. |
| 256 | if (Assembler.getRelaxAll() || |
| 257 | (Assembler.isBundlingEnabled() && SD->isBundleLocked())) { |
Rafael Espindola | 72b5488 | 2010-11-01 16:27:31 +0000 | [diff] [blame] | 258 | MCInst Relaxed; |
Jim Grosbach | aba3de9 | 2012-01-18 18:52:16 +0000 | [diff] [blame] | 259 | getAssembler().getBackend().relaxInstruction(Inst, Relaxed); |
| 260 | while (getAssembler().getBackend().mayNeedRelaxation(Relaxed)) |
| 261 | getAssembler().getBackend().relaxInstruction(Relaxed, Relaxed); |
David Woodhouse | 6f3c73f | 2014-01-28 23:12:49 +0000 | [diff] [blame] | 262 | EmitInstToData(Relaxed, STI); |
Rafael Espindola | 72b5488 | 2010-11-01 16:27:31 +0000 | [diff] [blame] | 263 | return; |
| 264 | } |
| 265 | |
| 266 | // Otherwise emit to a separate fragment. |
David Woodhouse | 6f3c73f | 2014-01-28 23:12:49 +0000 | [diff] [blame] | 267 | EmitInstToFragment(Inst, STI); |
Rafael Espindola | 72b5488 | 2010-11-01 16:27:31 +0000 | [diff] [blame] | 268 | } |
| 269 | |
David Woodhouse | 6f3c73f | 2014-01-28 23:12:49 +0000 | [diff] [blame] | 270 | void MCObjectStreamer::EmitInstToFragment(const MCInst &Inst, |
| 271 | const MCSubtargetInfo &STI) { |
Petr Hosek | 9e0c890 | 2015-04-12 23:42:25 +0000 | [diff] [blame] | 272 | if (getAssembler().getRelaxAll() && getAssembler().isBundlingEnabled()) |
| 273 | llvm_unreachable("All instructions should have already been relaxed"); |
| 274 | |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 275 | // Always create a new, separate fragment here, because its size can change |
| 276 | // during relaxation. |
David Woodhouse | f5199f6 | 2014-01-28 23:12:53 +0000 | [diff] [blame] | 277 | MCRelaxableFragment *IF = new MCRelaxableFragment(Inst, STI); |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 278 | insert(IF); |
Rafael Espindola | 3fa6fc1 | 2010-12-02 05:44:06 +0000 | [diff] [blame] | 279 | |
Eli Friedman | b2545fb | 2011-04-18 20:54:46 +0000 | [diff] [blame] | 280 | SmallString<128> Code; |
| 281 | raw_svector_ostream VecOS(Code); |
Jim Grosbach | 91df21f | 2015-05-15 19:13:16 +0000 | [diff] [blame] | 282 | getAssembler().getEmitter().encodeInstruction(Inst, VecOS, IF->getFixups(), |
David Woodhouse | 9784cef | 2014-01-28 23:13:07 +0000 | [diff] [blame] | 283 | STI); |
Eli Friedman | b2545fb | 2011-04-18 20:54:46 +0000 | [diff] [blame] | 284 | VecOS.flush(); |
Eli Bendersky | a31a894 | 2012-12-07 19:13:57 +0000 | [diff] [blame] | 285 | IF->getContents().append(Code.begin(), Code.end()); |
Rafael Espindola | 3fa6fc1 | 2010-12-02 05:44:06 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Matt Beaumont-Gay | 5ae7208 | 2013-02-15 23:12:33 +0000 | [diff] [blame] | 288 | #ifndef NDEBUG |
Craig Topper | d3a34f8 | 2013-07-16 01:17:10 +0000 | [diff] [blame] | 289 | static const char *const BundlingNotImplementedMsg = |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 290 | "Aligned bundling is not implemented for this object format"; |
Matt Beaumont-Gay | 5ae7208 | 2013-02-15 23:12:33 +0000 | [diff] [blame] | 291 | #endif |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 292 | |
| 293 | void MCObjectStreamer::EmitBundleAlignMode(unsigned AlignPow2) { |
| 294 | llvm_unreachable(BundlingNotImplementedMsg); |
| 295 | } |
| 296 | |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 297 | void MCObjectStreamer::EmitBundleLock(bool AlignToEnd) { |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 298 | llvm_unreachable(BundlingNotImplementedMsg); |
| 299 | } |
| 300 | |
| 301 | void MCObjectStreamer::EmitBundleUnlock() { |
| 302 | llvm_unreachable(BundlingNotImplementedMsg); |
| 303 | } |
| 304 | |
Ulrich Weigand | 64f4405 | 2013-06-19 21:27:27 +0000 | [diff] [blame] | 305 | void MCObjectStreamer::EmitDwarfLocDirective(unsigned FileNo, unsigned Line, |
| 306 | unsigned Column, unsigned Flags, |
| 307 | unsigned Isa, |
| 308 | unsigned Discriminator, |
| 309 | StringRef FileName) { |
| 310 | // In case we see two .loc directives in a row, make sure the |
| 311 | // first one gets a line entry. |
| 312 | MCLineEntry::Make(this, getCurrentSection().first); |
| 313 | |
| 314 | this->MCStreamer::EmitDwarfLocDirective(FileNo, Line, Column, Flags, |
| 315 | Isa, Discriminator, FileName); |
| 316 | } |
| 317 | |
Rafael Espindola | 3851808 | 2014-08-15 14:31:47 +0000 | [diff] [blame] | 318 | static const MCExpr *buildSymbolDiff(MCObjectStreamer &OS, const MCSymbol *A, |
| 319 | const MCSymbol *B) { |
| 320 | MCContext &Context = OS.getContext(); |
| 321 | MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; |
| 322 | const MCExpr *ARef = MCSymbolRefExpr::Create(A, Variant, Context); |
| 323 | const MCExpr *BRef = MCSymbolRefExpr::Create(B, Variant, Context); |
| 324 | const MCExpr *AddrDelta = |
| 325 | MCBinaryExpr::Create(MCBinaryExpr::Sub, ARef, BRef, Context); |
| 326 | return AddrDelta; |
| 327 | } |
| 328 | |
Rafael Espindola | 5e955fb | 2014-08-15 14:43:02 +0000 | [diff] [blame] | 329 | static void emitDwarfSetLineAddr(MCObjectStreamer &OS, int64_t LineDelta, |
| 330 | const MCSymbol *Label, int PointerSize) { |
| 331 | // emit the sequence to set the address |
| 332 | OS.EmitIntValue(dwarf::DW_LNS_extended_op, 1); |
| 333 | OS.EmitULEB128IntValue(PointerSize + 1); |
| 334 | OS.EmitIntValue(dwarf::DW_LNE_set_address, 1); |
| 335 | OS.EmitSymbolValue(Label, PointerSize); |
| 336 | |
| 337 | // emit the sequence for the LineDelta (from 1) and a zero address delta. |
| 338 | MCDwarfLineAddr::Emit(&OS, LineDelta, 0); |
| 339 | } |
| 340 | |
Rafael Espindola | 57ab708 | 2010-12-03 00:55:40 +0000 | [diff] [blame] | 341 | void MCObjectStreamer::EmitDwarfAdvanceLineAddr(int64_t LineDelta, |
| 342 | const MCSymbol *LastLabel, |
Evan Cheng | c7ac690 | 2011-07-14 05:43:07 +0000 | [diff] [blame] | 343 | const MCSymbol *Label, |
| 344 | unsigned PointerSize) { |
Rafael Espindola | 57ab708 | 2010-12-03 00:55:40 +0000 | [diff] [blame] | 345 | if (!LastLabel) { |
Rafael Espindola | 5e955fb | 2014-08-15 14:43:02 +0000 | [diff] [blame] | 346 | emitDwarfSetLineAddr(*this, LineDelta, Label, PointerSize); |
Rafael Espindola | 57ab708 | 2010-12-03 00:55:40 +0000 | [diff] [blame] | 347 | return; |
| 348 | } |
Rafael Espindola | 3851808 | 2014-08-15 14:31:47 +0000 | [diff] [blame] | 349 | const MCExpr *AddrDelta = buildSymbolDiff(*this, Label, LastLabel); |
Rafael Espindola | 57ab708 | 2010-12-03 00:55:40 +0000 | [diff] [blame] | 350 | int64_t Res; |
Rafael Espindola | 5004f4a | 2010-12-18 03:57:21 +0000 | [diff] [blame] | 351 | if (AddrDelta->EvaluateAsAbsolute(Res, getAssembler())) { |
Rafael Espindola | 57ab708 | 2010-12-03 00:55:40 +0000 | [diff] [blame] | 352 | MCDwarfLineAddr::Emit(this, LineDelta, Res); |
| 353 | return; |
| 354 | } |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 355 | insert(new MCDwarfLineAddrFragment(LineDelta, *AddrDelta)); |
Rafael Espindola | 57ab708 | 2010-12-03 00:55:40 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Rafael Espindola | 736a35d | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 358 | void MCObjectStreamer::EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel, |
| 359 | const MCSymbol *Label) { |
Rafael Espindola | 3851808 | 2014-08-15 14:31:47 +0000 | [diff] [blame] | 360 | const MCExpr *AddrDelta = buildSymbolDiff(*this, Label, LastLabel); |
Rafael Espindola | 736a35d | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 361 | int64_t Res; |
| 362 | if (AddrDelta->EvaluateAsAbsolute(Res, getAssembler())) { |
| 363 | MCDwarfFrameEmitter::EmitAdvanceLoc(*this, Res); |
| 364 | return; |
| 365 | } |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 366 | insert(new MCDwarfCallFrameFragment(*AddrDelta)); |
Rafael Espindola | 736a35d | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Rafael Espindola | 64e1af8 | 2013-07-02 15:49:13 +0000 | [diff] [blame] | 369 | void MCObjectStreamer::EmitBytes(StringRef Data) { |
Peter Collingbourne | 588009e | 2013-10-20 02:16:18 +0000 | [diff] [blame] | 370 | MCLineEntry::Make(this, getCurrentSection().first); |
Benjamin Kramer | 64ddcb0 | 2012-10-04 13:12:43 +0000 | [diff] [blame] | 371 | getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end()); |
| 372 | } |
| 373 | |
| 374 | void MCObjectStreamer::EmitValueToAlignment(unsigned ByteAlignment, |
| 375 | int64_t Value, |
| 376 | unsigned ValueSize, |
| 377 | unsigned MaxBytesToEmit) { |
| 378 | if (MaxBytesToEmit == 0) |
| 379 | MaxBytesToEmit = ByteAlignment; |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 380 | insert(new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit)); |
Benjamin Kramer | 64ddcb0 | 2012-10-04 13:12:43 +0000 | [diff] [blame] | 381 | |
| 382 | // Update the maximum alignment on the current section if necessary. |
| 383 | if (ByteAlignment > getCurrentSectionData()->getAlignment()) |
| 384 | getCurrentSectionData()->setAlignment(ByteAlignment); |
| 385 | } |
| 386 | |
| 387 | void MCObjectStreamer::EmitCodeAlignment(unsigned ByteAlignment, |
| 388 | unsigned MaxBytesToEmit) { |
| 389 | EmitValueToAlignment(ByteAlignment, 0, 1, MaxBytesToEmit); |
| 390 | cast<MCAlignFragment>(getCurrentFragment())->setEmitNops(true); |
| 391 | } |
| 392 | |
Jim Grosbach | b591277 | 2012-01-27 00:37:08 +0000 | [diff] [blame] | 393 | bool MCObjectStreamer::EmitValueToOffset(const MCExpr *Offset, |
Jim Grosbach | 5aac954 | 2012-01-26 23:47:45 +0000 | [diff] [blame] | 394 | unsigned char Value) { |
Rafael Espindola | f849bf8 | 2011-02-20 20:20:07 +0000 | [diff] [blame] | 395 | int64_t Res; |
| 396 | if (Offset->EvaluateAsAbsolute(Res, getAssembler())) { |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 397 | insert(new MCOrgFragment(*Offset, Value)); |
Jim Grosbach | b591277 | 2012-01-27 00:37:08 +0000 | [diff] [blame] | 398 | return false; |
Rafael Espindola | f849bf8 | 2011-02-20 20:20:07 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 401 | MCSymbol *CurrentPos = getContext().createTempSymbol(); |
Rafael Espindola | f849bf8 | 2011-02-20 20:20:07 +0000 | [diff] [blame] | 402 | EmitLabel(CurrentPos); |
| 403 | MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; |
| 404 | const MCExpr *Ref = |
| 405 | MCSymbolRefExpr::Create(CurrentPos, Variant, getContext()); |
| 406 | const MCExpr *Delta = |
| 407 | MCBinaryExpr::Create(MCBinaryExpr::Sub, Offset, Ref, getContext()); |
| 408 | |
| 409 | if (!Delta->EvaluateAsAbsolute(Res, getAssembler())) |
Jim Grosbach | b591277 | 2012-01-27 00:37:08 +0000 | [diff] [blame] | 410 | return true; |
Eric Christopher | ce0cfce | 2013-01-09 01:35:34 +0000 | [diff] [blame] | 411 | EmitFill(Res, Value); |
Jim Grosbach | b591277 | 2012-01-27 00:37:08 +0000 | [diff] [blame] | 412 | return false; |
Rafael Espindola | 9065bfc6 | 2010-12-02 05:59:38 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Akira Hatanaka | f5ddf13 | 2011-11-23 22:18:04 +0000 | [diff] [blame] | 415 | // Associate GPRel32 fixup with data and resize data area |
| 416 | void MCObjectStreamer::EmitGPRel32Value(const MCExpr *Value) { |
| 417 | MCDataFragment *DF = getOrCreateDataFragment(); |
| 418 | |
Jim Grosbach | 63661f8 | 2015-05-15 19:13:05 +0000 | [diff] [blame] | 419 | DF->getFixups().push_back(MCFixup::create(DF->getContents().size(), |
Eli Bendersky | a31a894 | 2012-12-07 19:13:57 +0000 | [diff] [blame] | 420 | Value, FK_GPRel_4)); |
Akira Hatanaka | f5ddf13 | 2011-11-23 22:18:04 +0000 | [diff] [blame] | 421 | DF->getContents().resize(DF->getContents().size() + 4, 0); |
| 422 | } |
| 423 | |
Jack Carter | 77064c0 | 2012-08-22 00:49:30 +0000 | [diff] [blame] | 424 | // Associate GPRel32 fixup with data and resize data area |
| 425 | void MCObjectStreamer::EmitGPRel64Value(const MCExpr *Value) { |
| 426 | MCDataFragment *DF = getOrCreateDataFragment(); |
| 427 | |
Jim Grosbach | 63661f8 | 2015-05-15 19:13:05 +0000 | [diff] [blame] | 428 | DF->getFixups().push_back(MCFixup::create(DF->getContents().size(), |
Eli Bendersky | a31a894 | 2012-12-07 19:13:57 +0000 | [diff] [blame] | 429 | Value, FK_GPRel_4)); |
Jack Carter | 77064c0 | 2012-08-22 00:49:30 +0000 | [diff] [blame] | 430 | DF->getContents().resize(DF->getContents().size() + 8, 0); |
| 431 | } |
| 432 | |
Rafael Espindola | 64e1af8 | 2013-07-02 15:49:13 +0000 | [diff] [blame] | 433 | void MCObjectStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue) { |
Benjamin Kramer | 4c8af55 | 2012-10-01 15:14:14 +0000 | [diff] [blame] | 434 | // FIXME: A MCFillFragment would be more memory efficient but MCExpr has |
| 435 | // problems evaluating expressions across multiple fragments. |
| 436 | getOrCreateDataFragment()->getContents().append(NumBytes, FillValue); |
| 437 | } |
| 438 | |
Rafael Espindola | 64e1af8 | 2013-07-02 15:49:13 +0000 | [diff] [blame] | 439 | void MCObjectStreamer::EmitZeros(uint64_t NumBytes) { |
Michael Ilseman | 5be22a1 | 2014-12-12 21:48:03 +0000 | [diff] [blame] | 440 | const MCSection *Sec = getCurrentSection().first; |
| 441 | assert(Sec && "need a section"); |
| 442 | unsigned ItemSize = Sec->isVirtualSection() ? 0 : 1; |
Serge Pavlov | 24a3ebb | 2013-06-27 14:35:03 +0000 | [diff] [blame] | 443 | insert(new MCFillFragment(0, ItemSize, NumBytes)); |
| 444 | } |
| 445 | |
Rafael Espindola | 0708209 | 2012-01-07 03:13:18 +0000 | [diff] [blame] | 446 | void MCObjectStreamer::FinishImpl() { |
Kevin Enderby | e7739d4 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 447 | // If we are generating dwarf for assembly source files dump out the sections. |
| 448 | if (getContext().getGenDwarfForAssembly()) |
David Blaikie | 8bf66c4 | 2014-04-01 07:35:52 +0000 | [diff] [blame] | 449 | MCGenDwarfInfo::Emit(this); |
| 450 | |
| 451 | // Dump out the dwarf file & directory tables and line tables. |
| 452 | MCDwarfLineTable::Emit(this); |
Kevin Enderby | e7739d4 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 453 | |
Derek Schuff | 5f708e5 | 2014-10-22 22:38:06 +0000 | [diff] [blame] | 454 | flushPendingLabels(nullptr); |
Daniel Dunbar | b2347fe | 2010-06-16 20:04:25 +0000 | [diff] [blame] | 455 | getAssembler().Finish(); |
| 456 | } |