blob: a74d37529cc563cecb1ff8d74ec4769430aff22b [file] [log] [blame]
Dan Gohman18eafb62017-02-22 01:23:18 +00001//===- 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 Gohman18eafb62017-02-22 01:23:18 +000018#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 Gohman18eafb62017-02-22 01:23:18 +000024#include "llvm/MC/MCObjectStreamer.h"
Dan Gohman18eafb62017-02-22 01:23:18 +000025#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
36using namespace llvm;
37
38MCWasmStreamer::~MCWasmStreamer() {}
39
40void 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 }
48 DF->setHasInstructions(true);
49 DF->getContents().append(EF->getContents().begin(), EF->getContents().end());
50}
51
52void MCWasmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
53 // Let the target do whatever target specific stuff it needs to do.
54 getAssembler().getBackend().handleAssemblerFlag(Flag);
55
56 // Do any generic stuff we need to do.
57 llvm_unreachable("invalid assembler flag!");
58}
59
60void MCWasmStreamer::ChangeSection(MCSection *Section,
61 const MCExpr *Subsection) {
62 MCAssembler &Asm = getAssembler();
63 auto *SectionWasm = static_cast<const MCSectionWasm *>(Section);
64 const MCSymbol *Grp = SectionWasm->getGroup();
65 if (Grp)
66 Asm.registerSymbol(*Grp);
67
68 this->MCObjectStreamer::ChangeSection(Section, Subsection);
Sam Cleggb210c642018-05-10 17:38:35 +000069 Asm.registerSymbol(*Section->getBeginSymbol());
Dan Gohman18eafb62017-02-22 01:23:18 +000070}
71
72void MCWasmStreamer::EmitWeakReference(MCSymbol *Alias,
73 const MCSymbol *Symbol) {
74 getAssembler().registerSymbol(*Symbol);
75 const MCExpr *Value = MCSymbolRefExpr::create(
76 Symbol, MCSymbolRefExpr::VK_WEAKREF, getContext());
77 Alias->setVariableValue(Value);
78}
79
80bool MCWasmStreamer::EmitSymbolAttribute(MCSymbol *S, MCSymbolAttr Attribute) {
81 assert(Attribute != MCSA_IndirectSymbol && "indirect symbols not supported");
82
83 auto *Symbol = cast<MCSymbolWasm>(S);
84
Nicholas Wilson5170b542018-02-12 13:17:09 +000085 // Adding a symbol attribute always introduces the symbol; note that an
86 // important side effect of calling registerSymbol here is to register the
87 // symbol with the assembler.
Dan Gohman18eafb62017-02-22 01:23:18 +000088 getAssembler().registerSymbol(*Symbol);
89
Dan Gohmand934cb82017-02-24 23:18:00 +000090 switch (Attribute) {
91 case MCSA_LazyReference:
92 case MCSA_Reference:
93 case MCSA_SymbolResolver:
94 case MCSA_PrivateExtern:
95 case MCSA_WeakDefinition:
96 case MCSA_WeakDefAutoPrivate:
97 case MCSA_Invalid:
98 case MCSA_IndirectSymbol:
Sam Clegg85ddec22017-10-20 17:41:12 +000099 case MCSA_Protected:
Dan Gohmand934cb82017-02-24 23:18:00 +0000100 return false;
Sam Cleggb7787fd2017-06-20 04:04:59 +0000101
Sam Clegga2b35da2017-12-03 01:19:23 +0000102 case MCSA_Hidden:
103 Symbol->setHidden(true);
104 break;
105
Sam Cleggb7787fd2017-06-20 04:04:59 +0000106 case MCSA_Weak:
107 case MCSA_WeakReference:
108 Symbol->setWeak(true);
109 Symbol->setExternal(true);
110 break;
111
Dan Gohmand934cb82017-02-24 23:18:00 +0000112 case MCSA_Global:
113 Symbol->setExternal(true);
114 break;
Sam Cleggb7787fd2017-06-20 04:04:59 +0000115
Dan Gohmand934cb82017-02-24 23:18:00 +0000116 case MCSA_ELF_TypeFunction:
Sam Clegg6c899ba2018-02-23 05:08:34 +0000117 Symbol->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
Dan Gohmand934cb82017-02-24 23:18:00 +0000118 break;
Sam Cleggb7787fd2017-06-20 04:04:59 +0000119
Dan Gohmand934cb82017-02-24 23:18:00 +0000120 case MCSA_ELF_TypeObject:
Sam Clegg6c899ba2018-02-23 05:08:34 +0000121 Symbol->setType(wasm::WASM_SYMBOL_TYPE_DATA);
Dan Gohmand934cb82017-02-24 23:18:00 +0000122 break;
Sam Cleggb7787fd2017-06-20 04:04:59 +0000123
Dan Gohmand934cb82017-02-24 23:18:00 +0000124 default:
125 // unrecognized directive
Sam Cleggb7787fd2017-06-20 04:04:59 +0000126 llvm_unreachable("unexpected MCSymbolAttr");
Dan Gohmand934cb82017-02-24 23:18:00 +0000127 return false;
128 }
Dan Gohman18eafb62017-02-22 01:23:18 +0000129
130 return true;
131}
132
133void MCWasmStreamer::EmitCommonSymbol(MCSymbol *S, uint64_t Size,
134 unsigned ByteAlignment) {
135 llvm_unreachable("Common symbols are not yet implemented for Wasm");
136}
137
Dan Gohmand934cb82017-02-24 23:18:00 +0000138void MCWasmStreamer::emitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
139 cast<MCSymbolWasm>(Symbol)->setSize(Value);
140}
141
Dan Gohman18eafb62017-02-22 01:23:18 +0000142void MCWasmStreamer::EmitLocalCommonSymbol(MCSymbol *S, uint64_t Size,
143 unsigned ByteAlignment) {
144 llvm_unreachable("Local common symbols are not yet implemented for Wasm");
145}
146
147void MCWasmStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
148 SMLoc Loc) {
149 MCObjectStreamer::EmitValueImpl(Value, Size, Loc);
150}
151
152void MCWasmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
153 unsigned ValueSize,
154 unsigned MaxBytesToEmit) {
155 MCObjectStreamer::EmitValueToAlignment(ByteAlignment, Value, ValueSize,
156 MaxBytesToEmit);
157}
158
Dan Gohman18eafb62017-02-22 01:23:18 +0000159void MCWasmStreamer::EmitIdent(StringRef IdentString) {
Sam Clegg4d57fbd2018-05-02 23:11:38 +0000160 // TODO(sbc): Add the ident section once we support mergable strings
161 // sections in the object format
Dan Gohman18eafb62017-02-22 01:23:18 +0000162}
163
164void MCWasmStreamer::EmitInstToFragment(const MCInst &Inst,
165 const MCSubtargetInfo &STI) {
166 this->MCObjectStreamer::EmitInstToFragment(Inst, STI);
167}
168
169void 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 }
186 DF->setHasInstructions(true);
187 DF->getContents().append(Code.begin(), Code.end());
188}
189
190void MCWasmStreamer::FinishImpl() {
191 EmitFrames(nullptr);
192
Sam Clegg87cc4db2018-05-02 23:01:10 +0000193 // Set fragment atoms so we can map from code fragment to defining symbol
194 addFragmentAtoms();
195
Dan Gohman18eafb62017-02-22 01:23:18 +0000196 this->MCObjectStreamer::FinishImpl();
197}
198
Lang Hames02d33052017-10-11 01:57:21 +0000199MCStreamer *llvm::createWasmStreamer(MCContext &Context,
200 std::unique_ptr<MCAsmBackend> &&MAB,
Peter Collingbournef7b81db2018-05-18 18:26:45 +0000201 std::unique_ptr<MCObjectWriter> &&OW,
Lang Hames2241ffa2017-10-11 23:34:47 +0000202 std::unique_ptr<MCCodeEmitter> &&CE,
Dan Gohman18eafb62017-02-22 01:23:18 +0000203 bool RelaxAll) {
Lang Hames2241ffa2017-10-11 23:34:47 +0000204 MCWasmStreamer *S =
Peter Collingbournef7b81db2018-05-18 18:26:45 +0000205 new MCWasmStreamer(Context, std::move(MAB), std::move(OW), std::move(CE));
Dan Gohman18eafb62017-02-22 01:23:18 +0000206 if (RelaxAll)
207 S->getAssembler().setRelaxAll(true);
208 return S;
209}
210
211void MCWasmStreamer::EmitThumbFunc(MCSymbol *Func) {
212 llvm_unreachable("Generic Wasm doesn't support this directive");
213}
214
215void MCWasmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
216 llvm_unreachable("Wasm doesn't support this directive");
217}
218
Dan Gohman18eafb62017-02-22 01:23:18 +0000219void MCWasmStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
220 uint64_t Size, unsigned ByteAlignment) {
221 llvm_unreachable("Wasm doesn't support this directive");
222}
223
224void MCWasmStreamer::EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
225 uint64_t Size, unsigned ByteAlignment) {
226 llvm_unreachable("Wasm doesn't support this directive");
227}