blob: 153ea2523057971c7e787ea9ad10080296774c69 [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);
69}
70
71void MCWasmStreamer::EmitWeakReference(MCSymbol *Alias,
72 const MCSymbol *Symbol) {
73 getAssembler().registerSymbol(*Symbol);
74 const MCExpr *Value = MCSymbolRefExpr::create(
75 Symbol, MCSymbolRefExpr::VK_WEAKREF, getContext());
76 Alias->setVariableValue(Value);
77}
78
79bool MCWasmStreamer::EmitSymbolAttribute(MCSymbol *S, MCSymbolAttr Attribute) {
80 assert(Attribute != MCSA_IndirectSymbol && "indirect symbols not supported");
81
82 auto *Symbol = cast<MCSymbolWasm>(S);
83
Nicholas Wilson5170b542018-02-12 13:17:09 +000084 // Adding a symbol attribute always introduces the symbol; note that an
85 // important side effect of calling registerSymbol here is to register the
86 // symbol with the assembler.
Dan Gohman18eafb62017-02-22 01:23:18 +000087 getAssembler().registerSymbol(*Symbol);
88
Dan Gohmand934cb82017-02-24 23:18:00 +000089 switch (Attribute) {
90 case MCSA_LazyReference:
91 case MCSA_Reference:
92 case MCSA_SymbolResolver:
93 case MCSA_PrivateExtern:
94 case MCSA_WeakDefinition:
95 case MCSA_WeakDefAutoPrivate:
96 case MCSA_Invalid:
97 case MCSA_IndirectSymbol:
Sam Clegg85ddec22017-10-20 17:41:12 +000098 case MCSA_Protected:
Dan Gohmand934cb82017-02-24 23:18:00 +000099 return false;
Sam Cleggb7787fd2017-06-20 04:04:59 +0000100
Sam Clegga2b35da2017-12-03 01:19:23 +0000101 case MCSA_Hidden:
102 Symbol->setHidden(true);
103 break;
104
Sam Cleggb7787fd2017-06-20 04:04:59 +0000105 case MCSA_Weak:
106 case MCSA_WeakReference:
107 Symbol->setWeak(true);
108 Symbol->setExternal(true);
109 break;
110
Dan Gohmand934cb82017-02-24 23:18:00 +0000111 case MCSA_Global:
112 Symbol->setExternal(true);
113 break;
Sam Cleggb7787fd2017-06-20 04:04:59 +0000114
Dan Gohmand934cb82017-02-24 23:18:00 +0000115 case MCSA_ELF_TypeFunction:
Sam Clegg6c899ba2018-02-23 05:08:34 +0000116 Symbol->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
Dan Gohmand934cb82017-02-24 23:18:00 +0000117 break;
Sam Cleggb7787fd2017-06-20 04:04:59 +0000118
Dan Gohmand934cb82017-02-24 23:18:00 +0000119 case MCSA_ELF_TypeObject:
Sam Clegg6c899ba2018-02-23 05:08:34 +0000120 Symbol->setType(wasm::WASM_SYMBOL_TYPE_DATA);
Dan Gohmand934cb82017-02-24 23:18:00 +0000121 break;
Sam Cleggb7787fd2017-06-20 04:04:59 +0000122
Dan Gohmand934cb82017-02-24 23:18:00 +0000123 default:
124 // unrecognized directive
Sam Cleggb7787fd2017-06-20 04:04:59 +0000125 llvm_unreachable("unexpected MCSymbolAttr");
Dan Gohmand934cb82017-02-24 23:18:00 +0000126 return false;
127 }
Dan Gohman18eafb62017-02-22 01:23:18 +0000128
129 return true;
130}
131
132void MCWasmStreamer::EmitCommonSymbol(MCSymbol *S, uint64_t Size,
133 unsigned ByteAlignment) {
134 llvm_unreachable("Common symbols are not yet implemented for Wasm");
135}
136
Dan Gohmand934cb82017-02-24 23:18:00 +0000137void MCWasmStreamer::emitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
138 cast<MCSymbolWasm>(Symbol)->setSize(Value);
139}
140
Dan Gohman18eafb62017-02-22 01:23:18 +0000141void MCWasmStreamer::EmitLocalCommonSymbol(MCSymbol *S, uint64_t Size,
142 unsigned ByteAlignment) {
143 llvm_unreachable("Local common symbols are not yet implemented for Wasm");
144}
145
146void MCWasmStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
147 SMLoc Loc) {
148 MCObjectStreamer::EmitValueImpl(Value, Size, Loc);
149}
150
151void MCWasmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
152 unsigned ValueSize,
153 unsigned MaxBytesToEmit) {
154 MCObjectStreamer::EmitValueToAlignment(ByteAlignment, Value, ValueSize,
155 MaxBytesToEmit);
156}
157
Dan Gohman18eafb62017-02-22 01:23:18 +0000158void MCWasmStreamer::EmitIdent(StringRef IdentString) {
Sam Clegg4d57fbd2018-05-02 23:11:38 +0000159 // TODO(sbc): Add the ident section once we support mergable strings
160 // sections in the object format
Dan Gohman18eafb62017-02-22 01:23:18 +0000161}
162
163void MCWasmStreamer::EmitInstToFragment(const MCInst &Inst,
164 const MCSubtargetInfo &STI) {
165 this->MCObjectStreamer::EmitInstToFragment(Inst, STI);
166}
167
168void MCWasmStreamer::EmitInstToData(const MCInst &Inst,
169 const MCSubtargetInfo &STI) {
170 MCAssembler &Assembler = getAssembler();
171 SmallVector<MCFixup, 4> Fixups;
172 SmallString<256> Code;
173 raw_svector_ostream VecOS(Code);
174 Assembler.getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
175
176 // Append the encoded instruction to the current data fragment (or create a
177 // new such fragment if the current fragment is not a data fragment).
178 MCDataFragment *DF = getOrCreateDataFragment();
179
180 // Add the fixups and data.
181 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
182 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
183 DF->getFixups().push_back(Fixups[i]);
184 }
185 DF->setHasInstructions(true);
186 DF->getContents().append(Code.begin(), Code.end());
187}
188
189void MCWasmStreamer::FinishImpl() {
190 EmitFrames(nullptr);
191
Sam Clegg87cc4db2018-05-02 23:01:10 +0000192 // Set fragment atoms so we can map from code fragment to defining symbol
193 addFragmentAtoms();
194
Dan Gohman18eafb62017-02-22 01:23:18 +0000195 this->MCObjectStreamer::FinishImpl();
196}
197
Lang Hames02d33052017-10-11 01:57:21 +0000198MCStreamer *llvm::createWasmStreamer(MCContext &Context,
199 std::unique_ptr<MCAsmBackend> &&MAB,
Lang Hames2241ffa2017-10-11 23:34:47 +0000200 raw_pwrite_stream &OS,
201 std::unique_ptr<MCCodeEmitter> &&CE,
Dan Gohman18eafb62017-02-22 01:23:18 +0000202 bool RelaxAll) {
Lang Hames2241ffa2017-10-11 23:34:47 +0000203 MCWasmStreamer *S =
204 new MCWasmStreamer(Context, std::move(MAB), OS, std::move(CE));
Dan Gohman18eafb62017-02-22 01:23:18 +0000205 if (RelaxAll)
206 S->getAssembler().setRelaxAll(true);
207 return S;
208}
209
210void MCWasmStreamer::EmitThumbFunc(MCSymbol *Func) {
211 llvm_unreachable("Generic Wasm doesn't support this directive");
212}
213
214void MCWasmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
215 llvm_unreachable("Wasm doesn't support this directive");
216}
217
Dan Gohman18eafb62017-02-22 01:23:18 +0000218void MCWasmStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
219 uint64_t Size, unsigned ByteAlignment) {
220 llvm_unreachable("Wasm doesn't support this directive");
221}
222
223void MCWasmStreamer::EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
224 uint64_t Size, unsigned ByteAlignment) {
225 llvm_unreachable("Wasm doesn't support this directive");
226}