Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 1 | //===- lib/MC/MCWasmStreamer.cpp - Wasm 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 | // This file assembles .s files and emits Wasm .o object files. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/MC/MCWasmStreamer.h" |
| 15 | #include "llvm/ADT/STLExtras.h" |
| 16 | #include "llvm/ADT/SmallPtrSet.h" |
| 17 | #include "llvm/MC/MCAsmBackend.h" |
| 18 | #include "llvm/MC/MCAsmInfo.h" |
| 19 | #include "llvm/MC/MCAsmLayout.h" |
| 20 | #include "llvm/MC/MCAssembler.h" |
| 21 | #include "llvm/MC/MCCodeEmitter.h" |
| 22 | #include "llvm/MC/MCContext.h" |
| 23 | #include "llvm/MC/MCExpr.h" |
| 24 | #include "llvm/MC/MCInst.h" |
| 25 | #include "llvm/MC/MCObjectFileInfo.h" |
| 26 | #include "llvm/MC/MCObjectStreamer.h" |
| 27 | #include "llvm/MC/MCObjectWriter.h" |
| 28 | #include "llvm/MC/MCSection.h" |
| 29 | #include "llvm/MC/MCSectionWasm.h" |
| 30 | #include "llvm/MC/MCSymbol.h" |
| 31 | #include "llvm/MC/MCSymbolWasm.h" |
| 32 | #include "llvm/MC/MCValue.h" |
| 33 | #include "llvm/Support/Casting.h" |
| 34 | #include "llvm/Support/Debug.h" |
| 35 | #include "llvm/Support/ErrorHandling.h" |
| 36 | #include "llvm/Support/TargetRegistry.h" |
| 37 | #include "llvm/Support/raw_ostream.h" |
| 38 | |
| 39 | using namespace llvm; |
| 40 | |
| 41 | MCWasmStreamer::~MCWasmStreamer() {} |
| 42 | |
| 43 | void MCWasmStreamer::mergeFragment(MCDataFragment *DF, MCDataFragment *EF) { |
| 44 | flushPendingLabels(DF, DF->getContents().size()); |
| 45 | |
| 46 | for (unsigned i = 0, e = EF->getFixups().size(); i != e; ++i) { |
| 47 | EF->getFixups()[i].setOffset(EF->getFixups()[i].getOffset() + |
| 48 | DF->getContents().size()); |
| 49 | DF->getFixups().push_back(EF->getFixups()[i]); |
| 50 | } |
| 51 | DF->setHasInstructions(true); |
| 52 | DF->getContents().append(EF->getContents().begin(), EF->getContents().end()); |
| 53 | } |
| 54 | |
| 55 | void MCWasmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) { |
| 56 | // Let the target do whatever target specific stuff it needs to do. |
| 57 | getAssembler().getBackend().handleAssemblerFlag(Flag); |
| 58 | |
| 59 | // Do any generic stuff we need to do. |
| 60 | llvm_unreachable("invalid assembler flag!"); |
| 61 | } |
| 62 | |
| 63 | void MCWasmStreamer::ChangeSection(MCSection *Section, |
| 64 | const MCExpr *Subsection) { |
| 65 | MCAssembler &Asm = getAssembler(); |
| 66 | auto *SectionWasm = static_cast<const MCSectionWasm *>(Section); |
| 67 | const MCSymbol *Grp = SectionWasm->getGroup(); |
| 68 | if (Grp) |
| 69 | Asm.registerSymbol(*Grp); |
| 70 | |
| 71 | this->MCObjectStreamer::ChangeSection(Section, Subsection); |
| 72 | } |
| 73 | |
| 74 | void MCWasmStreamer::EmitWeakReference(MCSymbol *Alias, |
| 75 | const MCSymbol *Symbol) { |
| 76 | getAssembler().registerSymbol(*Symbol); |
| 77 | const MCExpr *Value = MCSymbolRefExpr::create( |
| 78 | Symbol, MCSymbolRefExpr::VK_WEAKREF, getContext()); |
| 79 | Alias->setVariableValue(Value); |
| 80 | } |
| 81 | |
| 82 | bool MCWasmStreamer::EmitSymbolAttribute(MCSymbol *S, MCSymbolAttr Attribute) { |
| 83 | assert(Attribute != MCSA_IndirectSymbol && "indirect symbols not supported"); |
| 84 | |
| 85 | auto *Symbol = cast<MCSymbolWasm>(S); |
| 86 | |
| 87 | // Adding a symbol attribute always introduces the symbol, note that an |
| 88 | // important side effect of calling registerSymbol here is to register |
| 89 | // the symbol with the assembler. |
| 90 | getAssembler().registerSymbol(*Symbol); |
| 91 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 92 | switch (Attribute) { |
| 93 | case MCSA_LazyReference: |
| 94 | case MCSA_Reference: |
| 95 | case MCSA_SymbolResolver: |
| 96 | case MCSA_PrivateExtern: |
| 97 | case MCSA_WeakDefinition: |
| 98 | case MCSA_WeakDefAutoPrivate: |
| 99 | case MCSA_Invalid: |
| 100 | case MCSA_IndirectSymbol: |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame^] | 101 | case MCSA_Hidden: |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 102 | return false; |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame^] | 103 | |
| 104 | case MCSA_Weak: |
| 105 | case MCSA_WeakReference: |
| 106 | Symbol->setWeak(true); |
| 107 | Symbol->setExternal(true); |
| 108 | break; |
| 109 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 110 | case MCSA_Global: |
| 111 | Symbol->setExternal(true); |
| 112 | break; |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame^] | 113 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 114 | case MCSA_ELF_TypeFunction: |
| 115 | Symbol->setIsFunction(true); |
| 116 | break; |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame^] | 117 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 118 | case MCSA_ELF_TypeObject: |
| 119 | Symbol->setIsFunction(false); |
| 120 | break; |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame^] | 121 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 122 | default: |
| 123 | // unrecognized directive |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame^] | 124 | llvm_unreachable("unexpected MCSymbolAttr"); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 125 | return false; |
| 126 | } |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 127 | |
| 128 | return true; |
| 129 | } |
| 130 | |
| 131 | void MCWasmStreamer::EmitCommonSymbol(MCSymbol *S, uint64_t Size, |
| 132 | unsigned ByteAlignment) { |
| 133 | llvm_unreachable("Common symbols are not yet implemented for Wasm"); |
| 134 | } |
| 135 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 136 | void MCWasmStreamer::emitELFSize(MCSymbol *Symbol, const MCExpr *Value) { |
| 137 | cast<MCSymbolWasm>(Symbol)->setSize(Value); |
| 138 | } |
| 139 | |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 140 | void MCWasmStreamer::EmitLocalCommonSymbol(MCSymbol *S, uint64_t Size, |
| 141 | unsigned ByteAlignment) { |
| 142 | llvm_unreachable("Local common symbols are not yet implemented for Wasm"); |
| 143 | } |
| 144 | |
| 145 | void MCWasmStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size, |
| 146 | SMLoc Loc) { |
| 147 | MCObjectStreamer::EmitValueImpl(Value, Size, Loc); |
| 148 | } |
| 149 | |
| 150 | void MCWasmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value, |
| 151 | unsigned ValueSize, |
| 152 | unsigned MaxBytesToEmit) { |
| 153 | MCObjectStreamer::EmitValueToAlignment(ByteAlignment, Value, ValueSize, |
| 154 | MaxBytesToEmit); |
| 155 | } |
| 156 | |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 157 | void MCWasmStreamer::EmitIdent(StringRef IdentString) { |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 158 | MCSection *Comment = getAssembler().getContext().getWasmSection( |
| 159 | ".comment", 0, 0); |
| 160 | PushSection(); |
| 161 | SwitchSection(Comment); |
| 162 | if (!SeenIdent) { |
| 163 | EmitIntValue(0, 1); |
| 164 | SeenIdent = true; |
| 165 | } |
| 166 | EmitBytes(IdentString); |
| 167 | EmitIntValue(0, 1); |
| 168 | PopSection(); |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | void MCWasmStreamer::EmitInstToFragment(const MCInst &Inst, |
| 172 | const MCSubtargetInfo &STI) { |
| 173 | this->MCObjectStreamer::EmitInstToFragment(Inst, STI); |
| 174 | } |
| 175 | |
| 176 | void MCWasmStreamer::EmitInstToData(const MCInst &Inst, |
| 177 | const MCSubtargetInfo &STI) { |
| 178 | MCAssembler &Assembler = getAssembler(); |
| 179 | SmallVector<MCFixup, 4> Fixups; |
| 180 | SmallString<256> Code; |
| 181 | raw_svector_ostream VecOS(Code); |
| 182 | Assembler.getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI); |
| 183 | |
| 184 | // Append the encoded instruction to the current data fragment (or create a |
| 185 | // new such fragment if the current fragment is not a data fragment). |
| 186 | MCDataFragment *DF = getOrCreateDataFragment(); |
| 187 | |
| 188 | // Add the fixups and data. |
| 189 | for (unsigned i = 0, e = Fixups.size(); i != e; ++i) { |
| 190 | Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size()); |
| 191 | DF->getFixups().push_back(Fixups[i]); |
| 192 | } |
| 193 | DF->setHasInstructions(true); |
| 194 | DF->getContents().append(Code.begin(), Code.end()); |
| 195 | } |
| 196 | |
| 197 | void MCWasmStreamer::FinishImpl() { |
| 198 | EmitFrames(nullptr); |
| 199 | |
| 200 | this->MCObjectStreamer::FinishImpl(); |
| 201 | } |
| 202 | |
| 203 | MCStreamer *llvm::createWasmStreamer(MCContext &Context, MCAsmBackend &MAB, |
| 204 | raw_pwrite_stream &OS, MCCodeEmitter *CE, |
| 205 | bool RelaxAll) { |
| 206 | MCWasmStreamer *S = new MCWasmStreamer(Context, MAB, OS, CE); |
| 207 | if (RelaxAll) |
| 208 | S->getAssembler().setRelaxAll(true); |
| 209 | return S; |
| 210 | } |
| 211 | |
| 212 | void MCWasmStreamer::EmitThumbFunc(MCSymbol *Func) { |
| 213 | llvm_unreachable("Generic Wasm doesn't support this directive"); |
| 214 | } |
| 215 | |
| 216 | void MCWasmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) { |
| 217 | llvm_unreachable("Wasm doesn't support this directive"); |
| 218 | } |
| 219 | |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 220 | void MCWasmStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol, |
| 221 | uint64_t Size, unsigned ByteAlignment) { |
| 222 | llvm_unreachable("Wasm doesn't support this directive"); |
| 223 | } |
| 224 | |
| 225 | void MCWasmStreamer::EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol, |
| 226 | uint64_t Size, unsigned ByteAlignment) { |
| 227 | llvm_unreachable("Wasm doesn't support this directive"); |
| 228 | } |