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