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