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: |
| 31 | MCPureStreamer(MCContext &Context, TargetAsmBackend &TAB, |
| 32 | raw_ostream &OS, MCCodeEmitter *Emitter) |
| 33 | : MCObjectStreamer(Context, TAB, OS, Emitter, |
| 34 | /*PadSectionToAlignment=*/true) {} |
| 35 | |
| 36 | /// @name MCStreamer Interface |
| 37 | /// @{ |
| 38 | |
| 39 | virtual void InitSections(); |
| 40 | virtual void EmitLabel(MCSymbol *Symbol); |
| 41 | virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value); |
| 42 | virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0, |
| 43 | unsigned Size = 0, unsigned ByteAlignment = 0); |
| 44 | virtual void EmitBytes(StringRef Data, unsigned AddrSpace); |
| 45 | virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace); |
| 46 | virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0, |
| 47 | unsigned ValueSize = 1, |
| 48 | unsigned MaxBytesToEmit = 0); |
| 49 | virtual void EmitCodeAlignment(unsigned ByteAlignment, |
| 50 | unsigned MaxBytesToEmit = 0); |
| 51 | virtual void EmitValueToOffset(const MCExpr *Offset, |
| 52 | unsigned char Value = 0); |
| 53 | virtual void Finish(); |
| 54 | |
| 55 | |
| 56 | virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) { |
| 57 | report_fatal_error("unsupported directive in pure streamer"); |
| 58 | } |
| 59 | virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) { |
| 60 | report_fatal_error("unsupported directive in pure streamer"); |
| 61 | } |
| 62 | virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol, |
| 63 | uint64_t Size, unsigned ByteAlignment = 0) { |
| 64 | report_fatal_error("unsupported directive in pure streamer"); |
| 65 | } |
| 66 | virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) { |
| 67 | report_fatal_error("unsupported directive in pure streamer"); |
| 68 | } |
| 69 | virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, |
| 70 | unsigned ByteAlignment) { |
| 71 | report_fatal_error("unsupported directive in pure streamer"); |
| 72 | } |
| 73 | virtual void EmitThumbFunc(MCSymbol *Func) { |
| 74 | report_fatal_error("unsupported directive in pure streamer"); |
| 75 | } |
| 76 | virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) { |
| 77 | report_fatal_error("unsupported directive in pure streamer"); |
| 78 | } |
| 79 | virtual void EmitCOFFSymbolStorageClass(int StorageClass) { |
| 80 | report_fatal_error("unsupported directive in pure streamer"); |
| 81 | } |
| 82 | virtual void EmitCOFFSymbolType(int Type) { |
| 83 | report_fatal_error("unsupported directive in pure streamer"); |
| 84 | } |
| 85 | virtual void EndCOFFSymbolDef() { |
| 86 | report_fatal_error("unsupported directive in pure streamer"); |
| 87 | } |
| 88 | virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) { |
| 89 | report_fatal_error("unsupported directive in pure streamer"); |
| 90 | } |
| 91 | virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) { |
| 92 | report_fatal_error("unsupported directive in pure streamer"); |
| 93 | } |
| 94 | virtual void EmitGPRel32Value(const MCExpr *Value) { |
| 95 | report_fatal_error("unsupported directive in pure streamer"); |
| 96 | } |
| 97 | virtual void EmitFileDirective(StringRef Filename) { |
| 98 | report_fatal_error("unsupported directive in pure streamer"); |
| 99 | } |
| 100 | virtual bool EmitDwarfFileDirective(unsigned FileNo, StringRef Filename) { |
| 101 | report_fatal_error("unsupported directive in pure streamer"); |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | /// @} |
| 106 | }; |
| 107 | |
| 108 | } // end anonymous namespace. |
| 109 | |
| 110 | void MCPureStreamer::InitSections() { |
| 111 | // FIMXE: To what!? |
| 112 | SwitchSection(getContext().getMachOSection("__TEXT", "__text", |
| 113 | MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, |
| 114 | 0, SectionKind::getText())); |
| 115 | |
| 116 | } |
| 117 | |
| 118 | void MCPureStreamer::EmitLabel(MCSymbol *Symbol) { |
| 119 | assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); |
| 120 | assert(!Symbol->isVariable() && "Cannot emit a variable symbol!"); |
| 121 | assert(CurSection && "Cannot emit before setting section!"); |
| 122 | |
| 123 | Symbol->setSection(*CurSection); |
| 124 | |
| 125 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); |
| 126 | |
| 127 | // We have to create a new fragment if this is an atom defining symbol, |
| 128 | // fragments cannot span atoms. |
| 129 | if (getAssembler().isSymbolLinkerVisible(SD.getSymbol())) |
| 130 | new MCDataFragment(getCurrentSectionData()); |
| 131 | |
| 132 | // FIXME: This is wasteful, we don't necessarily need to create a data |
| 133 | // fragment. Instead, we should mark the symbol as pointing into the data |
| 134 | // fragment if it exists, otherwise we should just queue the label and set its |
| 135 | // fragment pointer when we emit the next fragment. |
| 136 | MCDataFragment *F = getOrCreateDataFragment(); |
| 137 | assert(!SD.getFragment() && "Unexpected fragment on symbol data!"); |
| 138 | SD.setFragment(F); |
| 139 | SD.setOffset(F->getContents().size()); |
| 140 | } |
| 141 | |
| 142 | void MCPureStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) { |
| 143 | // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into |
| 144 | // MCObjectStreamer. |
| 145 | // FIXME: Lift context changes into super class. |
| 146 | getAssembler().getOrCreateSymbolData(*Symbol); |
| 147 | Symbol->setVariableValue(AddValueSymbols(Value)); |
| 148 | } |
| 149 | |
| 150 | void MCPureStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol, |
| 151 | unsigned Size, unsigned ByteAlignment) { |
| 152 | report_fatal_error("not yet implemented in pure streamer"); |
| 153 | } |
| 154 | |
| 155 | void MCPureStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) { |
| 156 | // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into |
| 157 | // MCObjectStreamer. |
| 158 | getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end()); |
| 159 | } |
| 160 | |
| 161 | void MCPureStreamer::EmitValue(const MCExpr *Value, unsigned Size, |
| 162 | unsigned AddrSpace) { |
| 163 | // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into |
| 164 | // MCObjectStreamer. |
| 165 | MCDataFragment *DF = getOrCreateDataFragment(); |
| 166 | |
| 167 | // Avoid fixups when possible. |
| 168 | int64_t AbsValue; |
| 169 | if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue)) { |
| 170 | // FIXME: Endianness assumption. |
| 171 | for (unsigned i = 0; i != Size; ++i) |
| 172 | DF->getContents().push_back(uint8_t(AbsValue >> (i * 8))); |
| 173 | } else { |
| 174 | DF->addFixup(MCFixup::Create(DF->getContents().size(), |
| 175 | AddValueSymbols(Value), |
| 176 | MCFixup::getKindForSize(Size))); |
| 177 | DF->getContents().resize(DF->getContents().size() + Size, 0); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | void MCPureStreamer::EmitValueToAlignment(unsigned ByteAlignment, |
| 182 | int64_t Value, unsigned ValueSize, |
| 183 | unsigned MaxBytesToEmit) { |
| 184 | // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into |
| 185 | // MCObjectStreamer. |
| 186 | if (MaxBytesToEmit == 0) |
| 187 | MaxBytesToEmit = ByteAlignment; |
| 188 | new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit, |
| 189 | getCurrentSectionData()); |
| 190 | |
| 191 | // Update the maximum alignment on the current section if necessary. |
| 192 | if (ByteAlignment > getCurrentSectionData()->getAlignment()) |
| 193 | getCurrentSectionData()->setAlignment(ByteAlignment); |
| 194 | } |
| 195 | |
| 196 | void MCPureStreamer::EmitCodeAlignment(unsigned ByteAlignment, |
| 197 | unsigned MaxBytesToEmit) { |
| 198 | // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into |
| 199 | // MCObjectStreamer. |
| 200 | if (MaxBytesToEmit == 0) |
| 201 | MaxBytesToEmit = ByteAlignment; |
| 202 | MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit, |
| 203 | getCurrentSectionData()); |
| 204 | F->setEmitNops(true); |
| 205 | |
| 206 | // Update the maximum alignment on the current section if necessary. |
| 207 | if (ByteAlignment > getCurrentSectionData()->getAlignment()) |
| 208 | getCurrentSectionData()->setAlignment(ByteAlignment); |
| 209 | } |
| 210 | |
| 211 | void MCPureStreamer::EmitValueToOffset(const MCExpr *Offset, |
| 212 | unsigned char Value) { |
| 213 | new MCOrgFragment(*Offset, Value, getCurrentSectionData()); |
| 214 | } |
| 215 | |
| 216 | void MCPureStreamer::EmitInstToFragment(const MCInst &Inst) { |
| 217 | MCInstFragment *IF = new MCInstFragment(Inst, getCurrentSectionData()); |
| 218 | |
| 219 | // Add the fixups and data. |
| 220 | // |
| 221 | // FIXME: Revisit this design decision when relaxation is done, we may be |
| 222 | // able to get away with not storing any extra data in the MCInst. |
| 223 | SmallVector<MCFixup, 4> Fixups; |
| 224 | SmallString<256> Code; |
| 225 | raw_svector_ostream VecOS(Code); |
| 226 | getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups); |
| 227 | VecOS.flush(); |
| 228 | |
| 229 | IF->getCode() = Code; |
| 230 | IF->getFixups() = Fixups; |
| 231 | } |
| 232 | |
| 233 | void MCPureStreamer::EmitInstToData(const MCInst &Inst) { |
| 234 | MCDataFragment *DF = getOrCreateDataFragment(); |
| 235 | |
| 236 | SmallVector<MCFixup, 4> Fixups; |
| 237 | SmallString<256> Code; |
| 238 | raw_svector_ostream VecOS(Code); |
| 239 | getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups); |
| 240 | VecOS.flush(); |
| 241 | |
| 242 | // Add the fixups and data. |
| 243 | for (unsigned i = 0, e = Fixups.size(); i != e; ++i) { |
| 244 | Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size()); |
| 245 | DF->addFixup(Fixups[i]); |
| 246 | } |
| 247 | DF->getContents().append(Code.begin(), Code.end()); |
| 248 | } |
| 249 | |
| 250 | void MCPureStreamer::Finish() { |
| 251 | // FIXME: Handle DWARF tables? |
| 252 | |
| 253 | this->MCObjectStreamer::Finish(); |
| 254 | } |
| 255 | |
| 256 | MCStreamer *llvm::createPureStreamer(MCContext &Context, TargetAsmBackend &TAB, |
| 257 | raw_ostream &OS, MCCodeEmitter *CE) { |
| 258 | return new MCPureStreamer(Context, TAB, OS, CE); |
| 259 | } |