blob: e7e96ecbb3a01fee9b560f94894f7fc2451b5901 [file] [log] [blame]
Dan Gohman18eafb62017-02-22 01:23:18 +00001//===- lib/MC/MCWasmStreamer.cpp - Wasm Object Output ---------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Dan Gohman18eafb62017-02-22 01:23:18 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file assembles .s files and emits Wasm .o object files.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/MC/MCWasmStreamer.h"
14#include "llvm/ADT/STLExtras.h"
15#include "llvm/ADT/SmallPtrSet.h"
16#include "llvm/MC/MCAsmBackend.h"
Dan Gohman18eafb62017-02-22 01:23:18 +000017#include "llvm/MC/MCAsmLayout.h"
18#include "llvm/MC/MCAssembler.h"
19#include "llvm/MC/MCCodeEmitter.h"
20#include "llvm/MC/MCContext.h"
21#include "llvm/MC/MCExpr.h"
22#include "llvm/MC/MCInst.h"
Dan Gohman18eafb62017-02-22 01:23:18 +000023#include "llvm/MC/MCObjectStreamer.h"
Dan Gohman18eafb62017-02-22 01:23:18 +000024#include "llvm/MC/MCSection.h"
25#include "llvm/MC/MCSectionWasm.h"
26#include "llvm/MC/MCSymbol.h"
27#include "llvm/MC/MCSymbolWasm.h"
28#include "llvm/MC/MCValue.h"
29#include "llvm/Support/Casting.h"
30#include "llvm/Support/Debug.h"
31#include "llvm/Support/ErrorHandling.h"
32#include "llvm/Support/TargetRegistry.h"
33#include "llvm/Support/raw_ostream.h"
34
35using namespace llvm;
36
Heejin Ahn18c56a02019-02-04 19:13:39 +000037MCWasmStreamer::~MCWasmStreamer() = default; // anchor.
Dan Gohman18eafb62017-02-22 01:23:18 +000038
39void MCWasmStreamer::mergeFragment(MCDataFragment *DF, MCDataFragment *EF) {
40 flushPendingLabels(DF, DF->getContents().size());
41
Heejin Ahn18c56a02019-02-04 19:13:39 +000042 for (unsigned I = 0, E = EF->getFixups().size(); I != E; ++I) {
43 EF->getFixups()[I].setOffset(EF->getFixups()[I].getOffset() +
Dan Gohman18eafb62017-02-22 01:23:18 +000044 DF->getContents().size());
Heejin Ahn18c56a02019-02-04 19:13:39 +000045 DF->getFixups().push_back(EF->getFixups()[I]);
Dan Gohman18eafb62017-02-22 01:23:18 +000046 }
Peter Smith57f661b2018-06-06 09:40:06 +000047 if (DF->getSubtargetInfo() == nullptr && EF->getSubtargetInfo())
48 DF->setHasInstructions(*EF->getSubtargetInfo());
Dan Gohman18eafb62017-02-22 01:23:18 +000049 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();
Wouter van Oortmerssende28b5d2018-11-02 22:04:33 +000063 auto *SectionWasm = cast<MCSectionWasm>(Section);
Dan Gohman18eafb62017-02-22 01:23:18 +000064 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:
Dan Gohmanf726e442019-02-01 22:27:34 +0000121 case MCSA_Cold:
Dan Gohmand934cb82017-02-24 23:18:00 +0000122 break;
Sam Cleggb7787fd2017-06-20 04:04:59 +0000123
Sam Cleggd6ef8da2019-02-07 01:24:44 +0000124 case MCSA_NoDeadStrip:
Dan Gohmanda84b682019-08-29 22:40:00 +0000125 Symbol->setNoStrip();
Sam Cleggd6ef8da2019-02-07 01:24:44 +0000126 break;
127
Dan Gohmand934cb82017-02-24 23:18:00 +0000128 default:
129 // unrecognized directive
Sam Cleggb7787fd2017-06-20 04:04:59 +0000130 llvm_unreachable("unexpected MCSymbolAttr");
Dan Gohmand934cb82017-02-24 23:18:00 +0000131 return false;
132 }
Dan Gohman18eafb62017-02-22 01:23:18 +0000133
134 return true;
135}
136
137void MCWasmStreamer::EmitCommonSymbol(MCSymbol *S, uint64_t Size,
138 unsigned ByteAlignment) {
139 llvm_unreachable("Common symbols are not yet implemented for Wasm");
140}
141
Dan Gohmand934cb82017-02-24 23:18:00 +0000142void MCWasmStreamer::emitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
143 cast<MCSymbolWasm>(Symbol)->setSize(Value);
144}
145
Dan Gohman18eafb62017-02-22 01:23:18 +0000146void MCWasmStreamer::EmitLocalCommonSymbol(MCSymbol *S, uint64_t Size,
147 unsigned ByteAlignment) {
148 llvm_unreachable("Local common symbols are not yet implemented for Wasm");
149}
150
151void MCWasmStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
152 SMLoc Loc) {
153 MCObjectStreamer::EmitValueImpl(Value, Size, Loc);
154}
155
156void MCWasmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
157 unsigned ValueSize,
158 unsigned MaxBytesToEmit) {
159 MCObjectStreamer::EmitValueToAlignment(ByteAlignment, Value, ValueSize,
160 MaxBytesToEmit);
161}
162
Dan Gohman18eafb62017-02-22 01:23:18 +0000163void MCWasmStreamer::EmitIdent(StringRef IdentString) {
Sam Clegg4d57fbd2018-05-02 23:11:38 +0000164 // TODO(sbc): Add the ident section once we support mergable strings
165 // sections in the object format
Dan Gohman18eafb62017-02-22 01:23:18 +0000166}
167
168void MCWasmStreamer::EmitInstToFragment(const MCInst &Inst,
169 const MCSubtargetInfo &STI) {
170 this->MCObjectStreamer::EmitInstToFragment(Inst, STI);
171}
172
173void MCWasmStreamer::EmitInstToData(const MCInst &Inst,
174 const MCSubtargetInfo &STI) {
175 MCAssembler &Assembler = getAssembler();
176 SmallVector<MCFixup, 4> Fixups;
177 SmallString<256> Code;
178 raw_svector_ostream VecOS(Code);
179 Assembler.getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
180
181 // Append the encoded instruction to the current data fragment (or create a
182 // new such fragment if the current fragment is not a data fragment).
183 MCDataFragment *DF = getOrCreateDataFragment();
184
185 // Add the fixups and data.
Heejin Ahn18c56a02019-02-04 19:13:39 +0000186 for (unsigned I = 0, E = Fixups.size(); I != E; ++I) {
187 Fixups[I].setOffset(Fixups[I].getOffset() + DF->getContents().size());
188 DF->getFixups().push_back(Fixups[I]);
Dan Gohman18eafb62017-02-22 01:23:18 +0000189 }
Peter Smith57f661b2018-06-06 09:40:06 +0000190 DF->setHasInstructions(STI);
Dan Gohman18eafb62017-02-22 01:23:18 +0000191 DF->getContents().append(Code.begin(), Code.end());
192}
193
194void MCWasmStreamer::FinishImpl() {
195 EmitFrames(nullptr);
196
197 this->MCObjectStreamer::FinishImpl();
198}
199
Lang Hames02d33052017-10-11 01:57:21 +0000200MCStreamer *llvm::createWasmStreamer(MCContext &Context,
201 std::unique_ptr<MCAsmBackend> &&MAB,
Peter Collingbournef7b81db2018-05-18 18:26:45 +0000202 std::unique_ptr<MCObjectWriter> &&OW,
Lang Hames2241ffa2017-10-11 23:34:47 +0000203 std::unique_ptr<MCCodeEmitter> &&CE,
Dan Gohman18eafb62017-02-22 01:23:18 +0000204 bool RelaxAll) {
Lang Hames2241ffa2017-10-11 23:34:47 +0000205 MCWasmStreamer *S =
Peter Collingbournef7b81db2018-05-18 18:26:45 +0000206 new MCWasmStreamer(Context, std::move(MAB), std::move(OW), std::move(CE));
Dan Gohman18eafb62017-02-22 01:23:18 +0000207 if (RelaxAll)
208 S->getAssembler().setRelaxAll(true);
209 return S;
210}
211
212void MCWasmStreamer::EmitThumbFunc(MCSymbol *Func) {
213 llvm_unreachable("Generic Wasm doesn't support this directive");
214}
215
216void MCWasmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
217 llvm_unreachable("Wasm doesn't support this directive");
218}
219
Dan Gohman18eafb62017-02-22 01:23:18 +0000220void MCWasmStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
Francis Visoiu Mistrih4d5b1072018-07-02 17:29:43 +0000221 uint64_t Size, unsigned ByteAlignment,
222 SMLoc Loc) {
Dan Gohman18eafb62017-02-22 01:23:18 +0000223 llvm_unreachable("Wasm doesn't support this directive");
224}
225
226void MCWasmStreamer::EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
227 uint64_t Size, unsigned ByteAlignment) {
228 llvm_unreachable("Wasm doesn't support this directive");
229}