Daniel Dunbar | 8dc68ab | 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 | df39be6 | 2013-04-17 21:18:16 +0000 | [diff] [blame^] | 11 | #include "llvm/ADT/STLExtras.h" |
Craig Topper | f1d0f77 | 2012-03-26 06:58:25 +0000 | [diff] [blame] | 12 | #include "llvm/MC/MCAsmBackend.h" |
| 13 | #include "llvm/MC/MCAsmInfo.h" |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCAssembler.h" |
Benjamin Kramer | 1abcd06 | 2010-07-29 17:48:06 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCCodeEmitter.h" |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCContext.h" |
Rafael Espindola | f89671d | 2010-11-01 16:27:31 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCDwarf.h" |
Michael J. Spencer | 8067adc | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCExpr.h" |
Craig Topper | f1d0f77 | 2012-03-26 06:58:25 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCObjectWriter.h" |
Rafael Espindola | ea4afa9 | 2010-11-28 17:18:55 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCSymbol.h" |
Craig Topper | f1d0f77 | 2012-03-26 06:58:25 +0000 | [diff] [blame] | 21 | #include "llvm/Support/ErrorHandling.h" |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| 23 | |
Chandler Carruth | 5da3665 | 2013-01-31 23:29:57 +0000 | [diff] [blame] | 24 | MCObjectStreamer::MCObjectStreamer(StreamerKind Kind, MCContext &Context, |
| 25 | MCAsmBackend &TAB, raw_ostream &OS, |
| 26 | MCCodeEmitter *Emitter_) |
| 27 | : MCStreamer(Kind, Context), |
| 28 | Assembler(new MCAssembler(Context, TAB, *Emitter_, |
| 29 | *TAB.createObjectWriter(OS), OS)), |
| 30 | CurSectionData(0) {} |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 31 | |
Chandler Carruth | 5da3665 | 2013-01-31 23:29:57 +0000 | [diff] [blame] | 32 | MCObjectStreamer::MCObjectStreamer(StreamerKind Kind, MCContext &Context, |
| 33 | MCAsmBackend &TAB, raw_ostream &OS, |
| 34 | MCCodeEmitter *Emitter_, |
Jan Sjödin | 01dff96 | 2011-03-09 17:33:05 +0000 | [diff] [blame] | 35 | MCAssembler *_Assembler) |
Chandler Carruth | 5da3665 | 2013-01-31 23:29:57 +0000 | [diff] [blame] | 36 | : MCStreamer(Kind, Context), Assembler(_Assembler), CurSectionData(0) {} |
Jan Sjödin | 01dff96 | 2011-03-09 17:33:05 +0000 | [diff] [blame] | 37 | |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 38 | MCObjectStreamer::~MCObjectStreamer() { |
Benjamin Kramer | 1abcd06 | 2010-07-29 17:48:06 +0000 | [diff] [blame] | 39 | delete &Assembler->getBackend(); |
| 40 | delete &Assembler->getEmitter(); |
Daniel Dunbar | feb7ba3 | 2010-12-17 02:45:41 +0000 | [diff] [blame] | 41 | delete &Assembler->getWriter(); |
Daniel Dunbar | 8dc68ab | 2010-06-16 20:04:22 +0000 | [diff] [blame] | 42 | delete Assembler; |
| 43 | } |
Daniel Dunbar | 83b4671 | 2010-06-16 20:04:25 +0000 | [diff] [blame] | 44 | |
Pedro Artigas | 5399d25 | 2012-12-12 22:59:46 +0000 | [diff] [blame] | 45 | void MCObjectStreamer::reset() { |
Pedro Artigas | 99cbdde | 2012-12-14 18:52:11 +0000 | [diff] [blame] | 46 | if (Assembler) |
| 47 | Assembler->reset(); |
Pedro Artigas | b9d1005 | 2013-01-04 18:04:42 +0000 | [diff] [blame] | 48 | CurSectionData = 0; |
Peter Collingbourne | df39be6 | 2013-04-17 21:18:16 +0000 | [diff] [blame^] | 49 | CurInsertionPoint = MCSectionData::iterator(); |
Pedro Artigas | 5399d25 | 2012-12-12 22:59:46 +0000 | [diff] [blame] | 50 | MCStreamer::reset(); |
| 51 | } |
| 52 | |
Michael J. Spencer | 8067adc | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 53 | MCFragment *MCObjectStreamer::getCurrentFragment() const { |
| 54 | assert(getCurrentSectionData() && "No current section!"); |
| 55 | |
Peter Collingbourne | df39be6 | 2013-04-17 21:18:16 +0000 | [diff] [blame^] | 56 | if (CurInsertionPoint != getCurrentSectionData()->getFragmentList().begin()) |
| 57 | return prior(CurInsertionPoint); |
Michael J. Spencer | 8067adc | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | MCDataFragment *MCObjectStreamer::getOrCreateDataFragment() const { |
| 63 | MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment()); |
Derek Schuff | 67144e3 | 2013-02-15 22:50:52 +0000 | [diff] [blame] | 64 | // When bundling is enabled, we don't want to add data to a fragment that |
| 65 | // already has instructions (see MCELFStreamer::EmitInstToData for details) |
Peter Collingbourne | df39be6 | 2013-04-17 21:18:16 +0000 | [diff] [blame^] | 66 | if (!F || (Assembler->isBundlingEnabled() && F->hasInstructions())) { |
| 67 | F = new MCDataFragment(); |
| 68 | insert(F); |
| 69 | } |
Michael J. Spencer | 8067adc | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 70 | return F; |
| 71 | } |
| 72 | |
| 73 | const MCExpr *MCObjectStreamer::AddValueSymbols(const MCExpr *Value) { |
| 74 | switch (Value->getKind()) { |
Evan Cheng | 7597212 | 2011-01-13 07:58:56 +0000 | [diff] [blame] | 75 | case MCExpr::Target: |
| 76 | cast<MCTargetExpr>(Value)->AddValueSymbols(Assembler); |
| 77 | break; |
| 78 | |
Michael J. Spencer | 8067adc | 2010-07-19 06:13:10 +0000 | [diff] [blame] | 79 | case MCExpr::Constant: |
| 80 | break; |
| 81 | |
| 82 | case MCExpr::Binary: { |
| 83 | const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value); |
| 84 | AddValueSymbols(BE->getLHS()); |
| 85 | AddValueSymbols(BE->getRHS()); |
| 86 | break; |
| 87 | } |
| 88 | |
| 89 | case MCExpr::SymbolRef: |
| 90 | Assembler->getOrCreateSymbolData(cast<MCSymbolRefExpr>(Value)->getSymbol()); |
| 91 | break; |
| 92 | |
| 93 | case MCExpr::Unary: |
| 94 | AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr()); |
| 95 | break; |
| 96 | } |
| 97 | |
| 98 | return Value; |
| 99 | } |
| 100 | |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 101 | void MCObjectStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size, |
Rafael Espindola | debd7e4 | 2011-05-01 03:50:49 +0000 | [diff] [blame] | 102 | unsigned AddrSpace) { |
Rafael Espindola | 6f95023 | 2010-11-28 23:08:47 +0000 | [diff] [blame] | 103 | assert(AddrSpace == 0 && "Address space must be 0!"); |
| 104 | MCDataFragment *DF = getOrCreateDataFragment(); |
| 105 | |
| 106 | // Avoid fixups when possible. |
| 107 | int64_t AbsValue; |
Rafael Espindola | d076482 | 2010-12-18 03:57:21 +0000 | [diff] [blame] | 108 | if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue, getAssembler())) { |
Rafael Espindola | 2df042c | 2010-12-03 02:54:21 +0000 | [diff] [blame] | 109 | EmitIntValue(AbsValue, Size, AddrSpace); |
| 110 | return; |
Rafael Espindola | 6f95023 | 2010-11-28 23:08:47 +0000 | [diff] [blame] | 111 | } |
Eli Bendersky | 64d9a32 | 2012-12-07 19:13:57 +0000 | [diff] [blame] | 112 | DF->getFixups().push_back( |
| 113 | MCFixup::Create(DF->getContents().size(), Value, |
| 114 | MCFixup::getKindForSize(Size, false))); |
Rafael Espindola | 2df042c | 2010-12-03 02:54:21 +0000 | [diff] [blame] | 115 | DF->getContents().resize(DF->getContents().size() + Size, 0); |
Rafael Espindola | 6f95023 | 2010-11-28 23:08:47 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Rafael Espindola | 547be26 | 2012-01-07 22:42:19 +0000 | [diff] [blame] | 118 | void MCObjectStreamer::EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame) { |
| 119 | RecordProcStart(Frame); |
| 120 | } |
| 121 | |
Rafael Espindola | 1fe9737 | 2012-01-09 00:17:29 +0000 | [diff] [blame] | 122 | void MCObjectStreamer::EmitCFIEndProcImpl(MCDwarfFrameInfo &Frame) { |
| 123 | RecordProcEnd(Frame); |
| 124 | } |
| 125 | |
Rafael Espindola | ea4afa9 | 2010-11-28 17:18:55 +0000 | [diff] [blame] | 126 | void MCObjectStreamer::EmitLabel(MCSymbol *Symbol) { |
Rafael Espindola | ed708f9 | 2011-04-27 15:21:19 +0000 | [diff] [blame] | 127 | MCStreamer::EmitLabel(Symbol); |
Rafael Espindola | ea4afa9 | 2010-11-28 17:18:55 +0000 | [diff] [blame] | 128 | |
| 129 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); |
| 130 | |
| 131 | // FIXME: This is wasteful, we don't necessarily need to create a data |
| 132 | // fragment. Instead, we should mark the symbol as pointing into the data |
| 133 | // fragment if it exists, otherwise we should just queue the label and set its |
| 134 | // fragment pointer when we emit the next fragment. |
| 135 | MCDataFragment *F = getOrCreateDataFragment(); |
| 136 | assert(!SD.getFragment() && "Unexpected fragment on symbol data!"); |
| 137 | SD.setFragment(F); |
| 138 | SD.setOffset(F->getContents().size()); |
| 139 | } |
| 140 | |
Reed Kotler | 2c3a464 | 2012-12-16 04:00:45 +0000 | [diff] [blame] | 141 | void MCObjectStreamer::EmitDebugLabel(MCSymbol *Symbol) { |
| 142 | EmitLabel(Symbol); |
| 143 | } |
| 144 | |
Rafael Espindola | e8cfbd8 | 2011-04-21 23:39:26 +0000 | [diff] [blame] | 145 | void MCObjectStreamer::EmitULEB128Value(const MCExpr *Value) { |
Rafael Espindola | 660b5fc | 2010-12-03 01:19:49 +0000 | [diff] [blame] | 146 | int64_t IntValue; |
Rafael Espindola | d076482 | 2010-12-18 03:57:21 +0000 | [diff] [blame] | 147 | if (Value->EvaluateAsAbsolute(IntValue, getAssembler())) { |
Rafael Espindola | e8cfbd8 | 2011-04-21 23:39:26 +0000 | [diff] [blame] | 148 | EmitULEB128IntValue(IntValue); |
Rafael Espindola | 660b5fc | 2010-12-03 01:19:49 +0000 | [diff] [blame] | 149 | return; |
| 150 | } |
Rafael Espindola | a6f2678 | 2011-05-19 21:40:34 +0000 | [diff] [blame] | 151 | Value = ForceExpAbs(Value); |
Peter Collingbourne | df39be6 | 2013-04-17 21:18:16 +0000 | [diff] [blame^] | 152 | insert(new MCLEBFragment(*Value, false)); |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Rafael Espindola | e8cfbd8 | 2011-04-21 23:39:26 +0000 | [diff] [blame] | 155 | void MCObjectStreamer::EmitSLEB128Value(const MCExpr *Value) { |
Rafael Espindola | 660b5fc | 2010-12-03 01:19:49 +0000 | [diff] [blame] | 156 | int64_t IntValue; |
Rafael Espindola | d076482 | 2010-12-18 03:57:21 +0000 | [diff] [blame] | 157 | if (Value->EvaluateAsAbsolute(IntValue, getAssembler())) { |
Rafael Espindola | e8cfbd8 | 2011-04-21 23:39:26 +0000 | [diff] [blame] | 158 | EmitSLEB128IntValue(IntValue); |
Rafael Espindola | 660b5fc | 2010-12-03 01:19:49 +0000 | [diff] [blame] | 159 | return; |
| 160 | } |
Rafael Espindola | a6f2678 | 2011-05-19 21:40:34 +0000 | [diff] [blame] | 161 | Value = ForceExpAbs(Value); |
Peter Collingbourne | df39be6 | 2013-04-17 21:18:16 +0000 | [diff] [blame^] | 162 | insert(new MCLEBFragment(*Value, true)); |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Rafael Espindola | 484291c | 2010-11-01 14:28:48 +0000 | [diff] [blame] | 165 | void MCObjectStreamer::EmitWeakReference(MCSymbol *Alias, |
| 166 | const MCSymbol *Symbol) { |
| 167 | report_fatal_error("This file format doesn't support weak aliases."); |
| 168 | } |
| 169 | |
Peter Collingbourne | df39be6 | 2013-04-17 21:18:16 +0000 | [diff] [blame^] | 170 | void MCObjectStreamer::ChangeSection(const MCSection *Section, |
| 171 | const MCExpr *Subsection) { |
Daniel Dunbar | 83b4671 | 2010-06-16 20:04:25 +0000 | [diff] [blame] | 172 | assert(Section && "Cannot switch to a null section!"); |
| 173 | |
Daniel Dunbar | 83b4671 | 2010-06-16 20:04:25 +0000 | [diff] [blame] | 174 | CurSectionData = &getAssembler().getOrCreateSectionData(*Section); |
Peter Collingbourne | df39be6 | 2013-04-17 21:18:16 +0000 | [diff] [blame^] | 175 | |
| 176 | int64_t IntSubsection = 0; |
| 177 | if (Subsection && |
| 178 | !Subsection->EvaluateAsAbsolute(IntSubsection, getAssembler())) |
| 179 | report_fatal_error("Cannot evaluate subsection number"); |
| 180 | if (IntSubsection < 0 || IntSubsection > 8192) |
| 181 | report_fatal_error("Subsection number out of range"); |
| 182 | CurInsertionPoint = |
| 183 | CurSectionData->getSubsectionInsertionPoint(unsigned(IntSubsection)); |
Daniel Dunbar | 83b4671 | 2010-06-16 20:04:25 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Eli Bendersky | ef76b27 | 2012-12-07 17:42:41 +0000 | [diff] [blame] | 186 | void MCObjectStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) { |
| 187 | getAssembler().getOrCreateSymbolData(*Symbol); |
| 188 | Symbol->setVariableValue(AddValueSymbols(Value)); |
| 189 | } |
| 190 | |
Rafael Espindola | f89671d | 2010-11-01 16:27:31 +0000 | [diff] [blame] | 191 | void MCObjectStreamer::EmitInstruction(const MCInst &Inst) { |
| 192 | // Scan for values. |
| 193 | for (unsigned i = Inst.getNumOperands(); i--; ) |
| 194 | if (Inst.getOperand(i).isExpr()) |
| 195 | AddValueSymbols(Inst.getOperand(i).getExpr()); |
| 196 | |
Eli Bendersky | 4766ef4 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 197 | MCSectionData *SD = getCurrentSectionData(); |
| 198 | SD->setHasInstructions(true); |
Rafael Espindola | f89671d | 2010-11-01 16:27:31 +0000 | [diff] [blame] | 199 | |
| 200 | // Now that a machine instruction has been assembled into this section, make |
| 201 | // a line entry for any .loc directive that has been seen. |
Peter Collingbourne | df39be6 | 2013-04-17 21:18:16 +0000 | [diff] [blame^] | 202 | MCLineEntry::Make(this, getCurrentSection().first); |
Rafael Espindola | f89671d | 2010-11-01 16:27:31 +0000 | [diff] [blame] | 203 | |
| 204 | // If this instruction doesn't need relaxation, just emit it as data. |
Eli Bendersky | 4766ef4 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 205 | MCAssembler &Assembler = getAssembler(); |
| 206 | if (!Assembler.getBackend().mayNeedRelaxation(Inst)) { |
Rafael Espindola | f89671d | 2010-11-01 16:27:31 +0000 | [diff] [blame] | 207 | EmitInstToData(Inst); |
| 208 | return; |
| 209 | } |
| 210 | |
Eli Bendersky | 4766ef4 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 211 | // Otherwise, relax and emit it as data if either: |
| 212 | // - The RelaxAll flag was passed |
| 213 | // - Bundling is enabled and this instruction is inside a bundle-locked |
| 214 | // group. We want to emit all such instructions into the same data |
| 215 | // fragment. |
| 216 | if (Assembler.getRelaxAll() || |
| 217 | (Assembler.isBundlingEnabled() && SD->isBundleLocked())) { |
Rafael Espindola | f89671d | 2010-11-01 16:27:31 +0000 | [diff] [blame] | 218 | MCInst Relaxed; |
Jim Grosbach | ec34338 | 2012-01-18 18:52:16 +0000 | [diff] [blame] | 219 | getAssembler().getBackend().relaxInstruction(Inst, Relaxed); |
| 220 | while (getAssembler().getBackend().mayNeedRelaxation(Relaxed)) |
| 221 | getAssembler().getBackend().relaxInstruction(Relaxed, Relaxed); |
Rafael Espindola | f89671d | 2010-11-01 16:27:31 +0000 | [diff] [blame] | 222 | EmitInstToData(Relaxed); |
| 223 | return; |
| 224 | } |
| 225 | |
| 226 | // Otherwise emit to a separate fragment. |
| 227 | EmitInstToFragment(Inst); |
| 228 | } |
| 229 | |
Rafael Espindola | dedb045 | 2010-12-02 05:44:06 +0000 | [diff] [blame] | 230 | void MCObjectStreamer::EmitInstToFragment(const MCInst &Inst) { |
Eli Bendersky | 4766ef4 | 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. |
Peter Collingbourne | df39be6 | 2013-04-17 21:18:16 +0000 | [diff] [blame^] | 233 | MCRelaxableFragment *IF = new MCRelaxableFragment(Inst); |
| 234 | insert(IF); |
Rafael Espindola | dedb045 | 2010-12-02 05:44:06 +0000 | [diff] [blame] | 235 | |
Eli Friedman | 50ebe53 | 2011-04-18 20:54:46 +0000 | [diff] [blame] | 236 | SmallString<128> Code; |
| 237 | raw_svector_ostream VecOS(Code); |
Rafael Espindola | dedb045 | 2010-12-02 05:44:06 +0000 | [diff] [blame] | 238 | getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, IF->getFixups()); |
Eli Friedman | 50ebe53 | 2011-04-18 20:54:46 +0000 | [diff] [blame] | 239 | VecOS.flush(); |
Eli Bendersky | 64d9a32 | 2012-12-07 19:13:57 +0000 | [diff] [blame] | 240 | IF->getContents().append(Code.begin(), Code.end()); |
Rafael Espindola | dedb045 | 2010-12-02 05:44:06 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Matt Beaumont-Gay | f787085 | 2013-02-15 23:12:33 +0000 | [diff] [blame] | 243 | #ifndef NDEBUG |
Benjamin Kramer | 74b3c8d | 2013-02-15 12:30:38 +0000 | [diff] [blame] | 244 | static const char *BundlingNotImplementedMsg = |
Eli Bendersky | 4766ef4 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 245 | "Aligned bundling is not implemented for this object format"; |
Matt Beaumont-Gay | f787085 | 2013-02-15 23:12:33 +0000 | [diff] [blame] | 246 | #endif |
Eli Bendersky | 4766ef4 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 247 | |
| 248 | void MCObjectStreamer::EmitBundleAlignMode(unsigned AlignPow2) { |
| 249 | llvm_unreachable(BundlingNotImplementedMsg); |
| 250 | } |
| 251 | |
Eli Bendersky | 6c1d497 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 252 | void MCObjectStreamer::EmitBundleLock(bool AlignToEnd) { |
Eli Bendersky | 4766ef4 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 253 | llvm_unreachable(BundlingNotImplementedMsg); |
| 254 | } |
| 255 | |
| 256 | void MCObjectStreamer::EmitBundleUnlock() { |
| 257 | llvm_unreachable(BundlingNotImplementedMsg); |
| 258 | } |
| 259 | |
Rafael Espindola | 32a006e | 2010-12-03 00:55:40 +0000 | [diff] [blame] | 260 | void MCObjectStreamer::EmitDwarfAdvanceLineAddr(int64_t LineDelta, |
| 261 | const MCSymbol *LastLabel, |
Evan Cheng | 672b93a | 2011-07-14 05:43:07 +0000 | [diff] [blame] | 262 | const MCSymbol *Label, |
| 263 | unsigned PointerSize) { |
Rafael Espindola | 32a006e | 2010-12-03 00:55:40 +0000 | [diff] [blame] | 264 | if (!LastLabel) { |
Rafael Espindola | 32a006e | 2010-12-03 00:55:40 +0000 | [diff] [blame] | 265 | EmitDwarfSetLineAddr(LineDelta, Label, PointerSize); |
| 266 | return; |
| 267 | } |
Rafael Espindola | 245a1e2 | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 268 | const MCExpr *AddrDelta = BuildSymbolDiff(getContext(), Label, LastLabel); |
Rafael Espindola | 32a006e | 2010-12-03 00:55:40 +0000 | [diff] [blame] | 269 | int64_t Res; |
Rafael Espindola | d076482 | 2010-12-18 03:57:21 +0000 | [diff] [blame] | 270 | if (AddrDelta->EvaluateAsAbsolute(Res, getAssembler())) { |
Rafael Espindola | 32a006e | 2010-12-03 00:55:40 +0000 | [diff] [blame] | 271 | MCDwarfLineAddr::Emit(this, LineDelta, Res); |
| 272 | return; |
| 273 | } |
Rafael Espindola | a6f2678 | 2011-05-19 21:40:34 +0000 | [diff] [blame] | 274 | AddrDelta = ForceExpAbs(AddrDelta); |
Peter Collingbourne | df39be6 | 2013-04-17 21:18:16 +0000 | [diff] [blame^] | 275 | insert(new MCDwarfLineAddrFragment(LineDelta, *AddrDelta)); |
Rafael Espindola | 32a006e | 2010-12-03 00:55:40 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Rafael Espindola | 245a1e2 | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 278 | void MCObjectStreamer::EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel, |
| 279 | const MCSymbol *Label) { |
| 280 | const MCExpr *AddrDelta = BuildSymbolDiff(getContext(), Label, LastLabel); |
| 281 | int64_t Res; |
| 282 | if (AddrDelta->EvaluateAsAbsolute(Res, getAssembler())) { |
| 283 | MCDwarfFrameEmitter::EmitAdvanceLoc(*this, Res); |
| 284 | return; |
| 285 | } |
Rafael Espindola | a6f2678 | 2011-05-19 21:40:34 +0000 | [diff] [blame] | 286 | AddrDelta = ForceExpAbs(AddrDelta); |
Peter Collingbourne | df39be6 | 2013-04-17 21:18:16 +0000 | [diff] [blame^] | 287 | insert(new MCDwarfCallFrameFragment(*AddrDelta)); |
Rafael Espindola | 245a1e2 | 2010-12-28 05:39:27 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Benjamin Kramer | e660fc1 | 2012-10-04 13:12:43 +0000 | [diff] [blame] | 290 | void MCObjectStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) { |
| 291 | assert(AddrSpace == 0 && "Address space must be 0!"); |
| 292 | getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end()); |
| 293 | } |
| 294 | |
| 295 | void MCObjectStreamer::EmitValueToAlignment(unsigned ByteAlignment, |
| 296 | int64_t Value, |
| 297 | unsigned ValueSize, |
| 298 | unsigned MaxBytesToEmit) { |
| 299 | if (MaxBytesToEmit == 0) |
| 300 | MaxBytesToEmit = ByteAlignment; |
Peter Collingbourne | df39be6 | 2013-04-17 21:18:16 +0000 | [diff] [blame^] | 301 | insert(new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit)); |
Benjamin Kramer | e660fc1 | 2012-10-04 13:12:43 +0000 | [diff] [blame] | 302 | |
| 303 | // Update the maximum alignment on the current section if necessary. |
| 304 | if (ByteAlignment > getCurrentSectionData()->getAlignment()) |
| 305 | getCurrentSectionData()->setAlignment(ByteAlignment); |
| 306 | } |
| 307 | |
| 308 | void MCObjectStreamer::EmitCodeAlignment(unsigned ByteAlignment, |
| 309 | unsigned MaxBytesToEmit) { |
| 310 | EmitValueToAlignment(ByteAlignment, 0, 1, MaxBytesToEmit); |
| 311 | cast<MCAlignFragment>(getCurrentFragment())->setEmitNops(true); |
| 312 | } |
| 313 | |
Jim Grosbach | ebd4c05 | 2012-01-27 00:37:08 +0000 | [diff] [blame] | 314 | bool MCObjectStreamer::EmitValueToOffset(const MCExpr *Offset, |
Jim Grosbach | 660a4d9 | 2012-01-26 23:47:45 +0000 | [diff] [blame] | 315 | unsigned char Value) { |
Rafael Espindola | f7ad048 | 2011-02-20 20:20:07 +0000 | [diff] [blame] | 316 | int64_t Res; |
| 317 | if (Offset->EvaluateAsAbsolute(Res, getAssembler())) { |
Peter Collingbourne | df39be6 | 2013-04-17 21:18:16 +0000 | [diff] [blame^] | 318 | insert(new MCOrgFragment(*Offset, Value)); |
Jim Grosbach | ebd4c05 | 2012-01-27 00:37:08 +0000 | [diff] [blame] | 319 | return false; |
Rafael Espindola | f7ad048 | 2011-02-20 20:20:07 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | MCSymbol *CurrentPos = getContext().CreateTempSymbol(); |
| 323 | EmitLabel(CurrentPos); |
| 324 | MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; |
| 325 | const MCExpr *Ref = |
| 326 | MCSymbolRefExpr::Create(CurrentPos, Variant, getContext()); |
| 327 | const MCExpr *Delta = |
| 328 | MCBinaryExpr::Create(MCBinaryExpr::Sub, Offset, Ref, getContext()); |
| 329 | |
| 330 | if (!Delta->EvaluateAsAbsolute(Res, getAssembler())) |
Jim Grosbach | ebd4c05 | 2012-01-27 00:37:08 +0000 | [diff] [blame] | 331 | return true; |
Eric Christopher | ca1dd05 | 2013-01-09 01:35:34 +0000 | [diff] [blame] | 332 | EmitFill(Res, Value); |
Jim Grosbach | ebd4c05 | 2012-01-27 00:37:08 +0000 | [diff] [blame] | 333 | return false; |
Rafael Espindola | e239305 | 2010-12-02 05:59:38 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Akira Hatanaka | 84bfc2f | 2011-11-23 22:18:04 +0000 | [diff] [blame] | 336 | // Associate GPRel32 fixup with data and resize data area |
| 337 | void MCObjectStreamer::EmitGPRel32Value(const MCExpr *Value) { |
| 338 | MCDataFragment *DF = getOrCreateDataFragment(); |
| 339 | |
Eli Bendersky | 64d9a32 | 2012-12-07 19:13:57 +0000 | [diff] [blame] | 340 | DF->getFixups().push_back(MCFixup::Create(DF->getContents().size(), |
| 341 | Value, FK_GPRel_4)); |
Akira Hatanaka | 84bfc2f | 2011-11-23 22:18:04 +0000 | [diff] [blame] | 342 | DF->getContents().resize(DF->getContents().size() + 4, 0); |
| 343 | } |
| 344 | |
Jack Carter | 101771b | 2012-08-22 00:49:30 +0000 | [diff] [blame] | 345 | // Associate GPRel32 fixup with data and resize data area |
| 346 | void MCObjectStreamer::EmitGPRel64Value(const MCExpr *Value) { |
| 347 | MCDataFragment *DF = getOrCreateDataFragment(); |
| 348 | |
Eli Bendersky | 64d9a32 | 2012-12-07 19:13:57 +0000 | [diff] [blame] | 349 | DF->getFixups().push_back(MCFixup::Create(DF->getContents().size(), |
| 350 | Value, FK_GPRel_4)); |
Jack Carter | 101771b | 2012-08-22 00:49:30 +0000 | [diff] [blame] | 351 | DF->getContents().resize(DF->getContents().size() + 8, 0); |
| 352 | } |
| 353 | |
Benjamin Kramer | f0070f2 | 2012-10-01 15:14:14 +0000 | [diff] [blame] | 354 | void MCObjectStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue, |
| 355 | unsigned AddrSpace) { |
| 356 | assert(AddrSpace == 0 && "Address space must be 0!"); |
| 357 | // FIXME: A MCFillFragment would be more memory efficient but MCExpr has |
| 358 | // problems evaluating expressions across multiple fragments. |
| 359 | getOrCreateDataFragment()->getContents().append(NumBytes, FillValue); |
| 360 | } |
| 361 | |
Rafael Espindola | 99b4237 | 2012-01-07 03:13:18 +0000 | [diff] [blame] | 362 | void MCObjectStreamer::FinishImpl() { |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 363 | // Dump out the dwarf file & directory tables and line tables. |
Rafael Espindola | e6ec02e | 2012-03-03 14:24:15 +0000 | [diff] [blame] | 364 | const MCSymbol *LineSectionSymbol = NULL; |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 365 | if (getContext().hasDwarfFiles()) |
Rafael Espindola | 489d679 | 2012-02-28 21:13:05 +0000 | [diff] [blame] | 366 | LineSectionSymbol = MCDwarfFileTable::Emit(this); |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 367 | |
Kevin Enderby | 94c2e85 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 368 | // If we are generating dwarf for assembly source files dump out the sections. |
| 369 | if (getContext().getGenDwarfForAssembly()) |
Rafael Espindola | 489d679 | 2012-02-28 21:13:05 +0000 | [diff] [blame] | 370 | MCGenDwarfInfo::Emit(this, LineSectionSymbol); |
Kevin Enderby | 94c2e85 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 371 | |
Daniel Dunbar | 83b4671 | 2010-06-16 20:04:25 +0000 | [diff] [blame] | 372 | getAssembler().Finish(); |
| 373 | } |