blob: 287b7cf7b23f12b7cd63975ab8793c78b2a61524 [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"
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
39using namespace llvm;
40
41MCWasmStreamer::~MCWasmStreamer() {}
42
43void 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
55void 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
63void 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
74void 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
82bool 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 Gohmand934cb82017-02-24 23:18:00 +000092 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 Cleggb7787fd2017-06-20 04:04:59 +0000101 case MCSA_Hidden:
Sam Clegg85ddec22017-10-20 17:41:12 +0000102 case MCSA_Protected:
Dan Gohmand934cb82017-02-24 23:18:00 +0000103 return false;
Sam Cleggb7787fd2017-06-20 04:04:59 +0000104
105 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:
116 Symbol->setIsFunction(true);
117 break;
Sam Cleggb7787fd2017-06-20 04:04:59 +0000118
Dan Gohmand934cb82017-02-24 23:18:00 +0000119 case MCSA_ELF_TypeObject:
120 Symbol->setIsFunction(false);
121 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) {
Dan Gohmand934cb82017-02-24 23:18:00 +0000159 MCSection *Comment = getAssembler().getContext().getWasmSection(
Sam Clegg12fd3da2017-10-20 21:28:38 +0000160 ".comment", SectionKind::getMetadata());
Dan Gohmand934cb82017-02-24 23:18:00 +0000161 PushSection();
162 SwitchSection(Comment);
163 if (!SeenIdent) {
164 EmitIntValue(0, 1);
165 SeenIdent = true;
166 }
167 EmitBytes(IdentString);
168 EmitIntValue(0, 1);
169 PopSection();
Dan Gohman18eafb62017-02-22 01:23:18 +0000170}
171
172void MCWasmStreamer::EmitInstToFragment(const MCInst &Inst,
173 const MCSubtargetInfo &STI) {
174 this->MCObjectStreamer::EmitInstToFragment(Inst, STI);
175}
176
177void MCWasmStreamer::EmitInstToData(const MCInst &Inst,
178 const MCSubtargetInfo &STI) {
179 MCAssembler &Assembler = getAssembler();
180 SmallVector<MCFixup, 4> Fixups;
181 SmallString<256> Code;
182 raw_svector_ostream VecOS(Code);
183 Assembler.getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
184
185 // Append the encoded instruction to the current data fragment (or create a
186 // new such fragment if the current fragment is not a data fragment).
187 MCDataFragment *DF = getOrCreateDataFragment();
188
189 // Add the fixups and data.
190 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
191 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
192 DF->getFixups().push_back(Fixups[i]);
193 }
194 DF->setHasInstructions(true);
195 DF->getContents().append(Code.begin(), Code.end());
196}
197
198void MCWasmStreamer::FinishImpl() {
199 EmitFrames(nullptr);
200
201 this->MCObjectStreamer::FinishImpl();
202}
203
Lang Hames02d33052017-10-11 01:57:21 +0000204MCStreamer *llvm::createWasmStreamer(MCContext &Context,
205 std::unique_ptr<MCAsmBackend> &&MAB,
Lang Hames2241ffa2017-10-11 23:34:47 +0000206 raw_pwrite_stream &OS,
207 std::unique_ptr<MCCodeEmitter> &&CE,
Dan Gohman18eafb62017-02-22 01:23:18 +0000208 bool RelaxAll) {
Lang Hames2241ffa2017-10-11 23:34:47 +0000209 MCWasmStreamer *S =
210 new MCWasmStreamer(Context, std::move(MAB), OS, std::move(CE));
Dan Gohman18eafb62017-02-22 01:23:18 +0000211 if (RelaxAll)
212 S->getAssembler().setRelaxAll(true);
213 return S;
214}
215
216void MCWasmStreamer::EmitThumbFunc(MCSymbol *Func) {
217 llvm_unreachable("Generic Wasm doesn't support this directive");
218}
219
220void MCWasmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
221 llvm_unreachable("Wasm doesn't support this directive");
222}
223
Dan Gohman18eafb62017-02-22 01:23:18 +0000224void MCWasmStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
225 uint64_t Size, unsigned ByteAlignment) {
226 llvm_unreachable("Wasm doesn't support this directive");
227}
228
229void MCWasmStreamer::EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
230 uint64_t Size, unsigned ByteAlignment) {
231 llvm_unreachable("Wasm doesn't support this directive");
232}