Daniel Dunbar | abc7562 | 2010-11-17 16:06:47 +0000 | [diff] [blame] | 1 | //===- lib/MC/MCPureStreamer.cpp - MC "Pure" Object Output ----------------===// |
| 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/MCStreamer.h" |
| 11 | #include "llvm/MC/MCAssembler.h" |
| 12 | #include "llvm/MC/MCCodeEmitter.h" |
| 13 | #include "llvm/MC/MCContext.h" |
| 14 | #include "llvm/MC/MCExpr.h" |
| 15 | #include "llvm/MC/MCObjectStreamer.h" |
| 16 | // FIXME: Remove this. |
| 17 | #include "llvm/MC/MCSectionMachO.h" |
| 18 | #include "llvm/MC/MCSymbol.h" |
| 19 | #include "llvm/Support/ErrorHandling.h" |
| 20 | |
| 21 | using namespace llvm; |
| 22 | |
| 23 | namespace { |
| 24 | |
| 25 | class MCPureStreamer : public MCObjectStreamer { |
| 26 | private: |
| 27 | virtual void EmitInstToFragment(const MCInst &Inst); |
| 28 | virtual void EmitInstToData(const MCInst &Inst); |
| 29 | |
| 30 | public: |
Evan Cheng | 78c10ee | 2011-07-25 23:24:55 +0000 | [diff] [blame] | 31 | MCPureStreamer(MCContext &Context, MCAsmBackend &TAB, |
Daniel Dunbar | abc7562 | 2010-11-17 16:06:47 +0000 | [diff] [blame] | 32 | raw_ostream &OS, MCCodeEmitter *Emitter) |
Rafael Espindola | 85f2ecc | 2010-12-07 00:27:36 +0000 | [diff] [blame] | 33 | : MCObjectStreamer(Context, TAB, OS, Emitter) {} |
Daniel Dunbar | abc7562 | 2010-11-17 16:06:47 +0000 | [diff] [blame] | 34 | |
| 35 | /// @name MCStreamer Interface |
| 36 | /// @{ |
| 37 | |
| 38 | virtual void InitSections(); |
| 39 | virtual void EmitLabel(MCSymbol *Symbol); |
Reed Kotler | 082b7e6 | 2012-12-17 23:41:45 +0000 | [diff] [blame^] | 40 | virtual void EmitDebugLabel(MCSymbol *Symbol); |
Daniel Dunbar | abc7562 | 2010-11-17 16:06:47 +0000 | [diff] [blame] | 41 | virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0, |
Evan Cheng | c90a1fc | 2012-06-22 20:14:46 +0000 | [diff] [blame] | 42 | uint64_t Size = 0, unsigned ByteAlignment = 0); |
Daniel Dunbar | abc7562 | 2010-11-17 16:06:47 +0000 | [diff] [blame] | 43 | virtual void EmitBytes(StringRef Data, unsigned AddrSpace); |
Daniel Dunbar | abc7562 | 2010-11-17 16:06:47 +0000 | [diff] [blame] | 44 | virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0, |
| 45 | unsigned ValueSize = 1, |
| 46 | unsigned MaxBytesToEmit = 0); |
| 47 | virtual void EmitCodeAlignment(unsigned ByteAlignment, |
| 48 | unsigned MaxBytesToEmit = 0); |
Jim Grosbach | ebd4c05 | 2012-01-27 00:37:08 +0000 | [diff] [blame] | 49 | virtual bool EmitValueToOffset(const MCExpr *Offset, |
Daniel Dunbar | abc7562 | 2010-11-17 16:06:47 +0000 | [diff] [blame] | 50 | unsigned char Value = 0); |
Rafael Espindola | 99b4237 | 2012-01-07 03:13:18 +0000 | [diff] [blame] | 51 | virtual void FinishImpl(); |
Daniel Dunbar | abc7562 | 2010-11-17 16:06:47 +0000 | [diff] [blame] | 52 | |
| 53 | |
| 54 | virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) { |
| 55 | report_fatal_error("unsupported directive in pure streamer"); |
| 56 | } |
| 57 | virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) { |
| 58 | report_fatal_error("unsupported directive in pure streamer"); |
| 59 | } |
| 60 | virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol, |
| 61 | uint64_t Size, unsigned ByteAlignment = 0) { |
| 62 | report_fatal_error("unsupported directive in pure streamer"); |
| 63 | } |
| 64 | virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) { |
| 65 | report_fatal_error("unsupported directive in pure streamer"); |
| 66 | } |
| 67 | virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, |
| 68 | unsigned ByteAlignment) { |
| 69 | report_fatal_error("unsupported directive in pure streamer"); |
| 70 | } |
| 71 | virtual void EmitThumbFunc(MCSymbol *Func) { |
| 72 | report_fatal_error("unsupported directive in pure streamer"); |
| 73 | } |
| 74 | virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) { |
| 75 | report_fatal_error("unsupported directive in pure streamer"); |
| 76 | } |
| 77 | virtual void EmitCOFFSymbolStorageClass(int StorageClass) { |
| 78 | report_fatal_error("unsupported directive in pure streamer"); |
| 79 | } |
| 80 | virtual void EmitCOFFSymbolType(int Type) { |
| 81 | report_fatal_error("unsupported directive in pure streamer"); |
| 82 | } |
| 83 | virtual void EndCOFFSymbolDef() { |
| 84 | report_fatal_error("unsupported directive in pure streamer"); |
| 85 | } |
| 86 | virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) { |
| 87 | report_fatal_error("unsupported directive in pure streamer"); |
| 88 | } |
Benjamin Kramer | 36a1601 | 2011-09-01 23:04:27 +0000 | [diff] [blame] | 89 | virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size, |
| 90 | unsigned ByteAlignment) { |
Daniel Dunbar | abc7562 | 2010-11-17 16:06:47 +0000 | [diff] [blame] | 91 | report_fatal_error("unsupported directive in pure streamer"); |
| 92 | } |
Daniel Dunbar | abc7562 | 2010-11-17 16:06:47 +0000 | [diff] [blame] | 93 | virtual void EmitFileDirective(StringRef Filename) { |
| 94 | report_fatal_error("unsupported directive in pure streamer"); |
| 95 | } |
Nick Lewycky | 44d798d | 2011-10-17 23:05:28 +0000 | [diff] [blame] | 96 | virtual bool EmitDwarfFileDirective(unsigned FileNo, StringRef Directory, |
| 97 | StringRef Filename) { |
Daniel Dunbar | abc7562 | 2010-11-17 16:06:47 +0000 | [diff] [blame] | 98 | report_fatal_error("unsupported directive in pure streamer"); |
Daniel Dunbar | abc7562 | 2010-11-17 16:06:47 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /// @} |
| 102 | }; |
| 103 | |
| 104 | } // end anonymous namespace. |
| 105 | |
| 106 | void MCPureStreamer::InitSections() { |
| 107 | // FIMXE: To what!? |
| 108 | SwitchSection(getContext().getMachOSection("__TEXT", "__text", |
| 109 | MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, |
| 110 | 0, SectionKind::getText())); |
| 111 | |
| 112 | } |
| 113 | |
| 114 | void MCPureStreamer::EmitLabel(MCSymbol *Symbol) { |
| 115 | assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); |
| 116 | assert(!Symbol->isVariable() && "Cannot emit a variable symbol!"); |
Rafael Espindola | 7768a9d | 2011-02-16 01:08:29 +0000 | [diff] [blame] | 117 | assert(getCurrentSection() && "Cannot emit before setting section!"); |
Daniel Dunbar | abc7562 | 2010-11-17 16:06:47 +0000 | [diff] [blame] | 118 | |
Rafael Espindola | 7768a9d | 2011-02-16 01:08:29 +0000 | [diff] [blame] | 119 | Symbol->setSection(*getCurrentSection()); |
Daniel Dunbar | abc7562 | 2010-11-17 16:06:47 +0000 | [diff] [blame] | 120 | |
| 121 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); |
| 122 | |
| 123 | // We have to create a new fragment if this is an atom defining symbol, |
| 124 | // fragments cannot span atoms. |
| 125 | if (getAssembler().isSymbolLinkerVisible(SD.getSymbol())) |
| 126 | new MCDataFragment(getCurrentSectionData()); |
| 127 | |
| 128 | // FIXME: This is wasteful, we don't necessarily need to create a data |
| 129 | // fragment. Instead, we should mark the symbol as pointing into the data |
| 130 | // fragment if it exists, otherwise we should just queue the label and set its |
| 131 | // fragment pointer when we emit the next fragment. |
| 132 | MCDataFragment *F = getOrCreateDataFragment(); |
| 133 | assert(!SD.getFragment() && "Unexpected fragment on symbol data!"); |
| 134 | SD.setFragment(F); |
| 135 | SD.setOffset(F->getContents().size()); |
| 136 | } |
| 137 | |
Reed Kotler | 2c3a464 | 2012-12-16 04:00:45 +0000 | [diff] [blame] | 138 | |
Reed Kotler | 082b7e6 | 2012-12-17 23:41:45 +0000 | [diff] [blame^] | 139 | void MCPureStreamer::EmitDebugLabel(MCSymbol *Symbol) { |
| 140 | EmitLabel(Symbol); |
| 141 | } |
| 142 | |
Daniel Dunbar | abc7562 | 2010-11-17 16:06:47 +0000 | [diff] [blame] | 143 | void MCPureStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol, |
Evan Cheng | c90a1fc | 2012-06-22 20:14:46 +0000 | [diff] [blame] | 144 | uint64_t Size, unsigned ByteAlignment) { |
Daniel Dunbar | abc7562 | 2010-11-17 16:06:47 +0000 | [diff] [blame] | 145 | report_fatal_error("not yet implemented in pure streamer"); |
| 146 | } |
| 147 | |
| 148 | void MCPureStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) { |
| 149 | // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into |
| 150 | // MCObjectStreamer. |
| 151 | getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end()); |
| 152 | } |
| 153 | |
Daniel Dunbar | abc7562 | 2010-11-17 16:06:47 +0000 | [diff] [blame] | 154 | void MCPureStreamer::EmitValueToAlignment(unsigned ByteAlignment, |
| 155 | int64_t Value, unsigned ValueSize, |
| 156 | unsigned MaxBytesToEmit) { |
| 157 | // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into |
| 158 | // MCObjectStreamer. |
| 159 | if (MaxBytesToEmit == 0) |
| 160 | MaxBytesToEmit = ByteAlignment; |
| 161 | new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit, |
| 162 | getCurrentSectionData()); |
| 163 | |
| 164 | // Update the maximum alignment on the current section if necessary. |
| 165 | if (ByteAlignment > getCurrentSectionData()->getAlignment()) |
| 166 | getCurrentSectionData()->setAlignment(ByteAlignment); |
| 167 | } |
| 168 | |
| 169 | void MCPureStreamer::EmitCodeAlignment(unsigned ByteAlignment, |
| 170 | unsigned MaxBytesToEmit) { |
| 171 | // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into |
| 172 | // MCObjectStreamer. |
| 173 | if (MaxBytesToEmit == 0) |
| 174 | MaxBytesToEmit = ByteAlignment; |
| 175 | MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit, |
| 176 | getCurrentSectionData()); |
| 177 | F->setEmitNops(true); |
| 178 | |
| 179 | // Update the maximum alignment on the current section if necessary. |
| 180 | if (ByteAlignment > getCurrentSectionData()->getAlignment()) |
| 181 | getCurrentSectionData()->setAlignment(ByteAlignment); |
| 182 | } |
| 183 | |
Jim Grosbach | ebd4c05 | 2012-01-27 00:37:08 +0000 | [diff] [blame] | 184 | bool MCPureStreamer::EmitValueToOffset(const MCExpr *Offset, |
Daniel Dunbar | abc7562 | 2010-11-17 16:06:47 +0000 | [diff] [blame] | 185 | unsigned char Value) { |
| 186 | new MCOrgFragment(*Offset, Value, getCurrentSectionData()); |
Jim Grosbach | ebd4c05 | 2012-01-27 00:37:08 +0000 | [diff] [blame] | 187 | return false; |
Daniel Dunbar | abc7562 | 2010-11-17 16:06:47 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | void MCPureStreamer::EmitInstToFragment(const MCInst &Inst) { |
| 191 | MCInstFragment *IF = new MCInstFragment(Inst, getCurrentSectionData()); |
| 192 | |
| 193 | // Add the fixups and data. |
| 194 | // |
| 195 | // FIXME: Revisit this design decision when relaxation is done, we may be |
| 196 | // able to get away with not storing any extra data in the MCInst. |
| 197 | SmallVector<MCFixup, 4> Fixups; |
| 198 | SmallString<256> Code; |
| 199 | raw_svector_ostream VecOS(Code); |
| 200 | getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups); |
| 201 | VecOS.flush(); |
| 202 | |
Eli Bendersky | 64d9a32 | 2012-12-07 19:13:57 +0000 | [diff] [blame] | 203 | IF->getContents() = Code; |
Daniel Dunbar | abc7562 | 2010-11-17 16:06:47 +0000 | [diff] [blame] | 204 | IF->getFixups() = Fixups; |
| 205 | } |
| 206 | |
| 207 | void MCPureStreamer::EmitInstToData(const MCInst &Inst) { |
| 208 | MCDataFragment *DF = getOrCreateDataFragment(); |
| 209 | |
| 210 | SmallVector<MCFixup, 4> Fixups; |
| 211 | SmallString<256> Code; |
| 212 | raw_svector_ostream VecOS(Code); |
| 213 | getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups); |
| 214 | VecOS.flush(); |
| 215 | |
| 216 | // Add the fixups and data. |
| 217 | for (unsigned i = 0, e = Fixups.size(); i != e; ++i) { |
| 218 | Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size()); |
Eli Bendersky | 64d9a32 | 2012-12-07 19:13:57 +0000 | [diff] [blame] | 219 | DF->getFixups().push_back(Fixups[i]); |
Daniel Dunbar | abc7562 | 2010-11-17 16:06:47 +0000 | [diff] [blame] | 220 | } |
| 221 | DF->getContents().append(Code.begin(), Code.end()); |
| 222 | } |
| 223 | |
Rafael Espindola | 99b4237 | 2012-01-07 03:13:18 +0000 | [diff] [blame] | 224 | void MCPureStreamer::FinishImpl() { |
Daniel Dunbar | abc7562 | 2010-11-17 16:06:47 +0000 | [diff] [blame] | 225 | // FIXME: Handle DWARF tables? |
| 226 | |
Rafael Espindola | 99b4237 | 2012-01-07 03:13:18 +0000 | [diff] [blame] | 227 | this->MCObjectStreamer::FinishImpl(); |
Daniel Dunbar | abc7562 | 2010-11-17 16:06:47 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Evan Cheng | 78c10ee | 2011-07-25 23:24:55 +0000 | [diff] [blame] | 230 | MCStreamer *llvm::createPureStreamer(MCContext &Context, MCAsmBackend &MAB, |
Daniel Dunbar | abc7562 | 2010-11-17 16:06:47 +0000 | [diff] [blame] | 231 | raw_ostream &OS, MCCodeEmitter *CE) { |
Evan Cheng | 78c10ee | 2011-07-25 23:24:55 +0000 | [diff] [blame] | 232 | return new MCPureStreamer(Context, MAB, OS, CE); |
Daniel Dunbar | abc7562 | 2010-11-17 16:06:47 +0000 | [diff] [blame] | 233 | } |