blob: 59b62b8d37c30447d1a1bc731dd7b7790aaa8e9f [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:
101 return false;
102 case MCSA_Global:
103 Symbol->setExternal(true);
104 break;
105 case MCSA_ELF_TypeFunction:
106 Symbol->setIsFunction(true);
107 break;
108 case MCSA_ELF_TypeObject:
109 Symbol->setIsFunction(false);
110 break;
111 default:
112 // unrecognized directive
113 return false;
114 }
Dan Gohman18eafb62017-02-22 01:23:18 +0000115
116 return true;
117}
118
119void MCWasmStreamer::EmitCommonSymbol(MCSymbol *S, uint64_t Size,
120 unsigned ByteAlignment) {
121 llvm_unreachable("Common symbols are not yet implemented for Wasm");
122}
123
Dan Gohmand934cb82017-02-24 23:18:00 +0000124void MCWasmStreamer::emitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
125 cast<MCSymbolWasm>(Symbol)->setSize(Value);
126}
127
Dan Gohman18eafb62017-02-22 01:23:18 +0000128void MCWasmStreamer::EmitLocalCommonSymbol(MCSymbol *S, uint64_t Size,
129 unsigned ByteAlignment) {
130 llvm_unreachable("Local common symbols are not yet implemented for Wasm");
131}
132
133void MCWasmStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
134 SMLoc Loc) {
135 MCObjectStreamer::EmitValueImpl(Value, Size, Loc);
136}
137
138void MCWasmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
139 unsigned ValueSize,
140 unsigned MaxBytesToEmit) {
141 MCObjectStreamer::EmitValueToAlignment(ByteAlignment, Value, ValueSize,
142 MaxBytesToEmit);
143}
144
Dan Gohman18eafb62017-02-22 01:23:18 +0000145void MCWasmStreamer::EmitIdent(StringRef IdentString) {
Dan Gohmand934cb82017-02-24 23:18:00 +0000146 MCSection *Comment = getAssembler().getContext().getWasmSection(
147 ".comment", 0, 0);
148 PushSection();
149 SwitchSection(Comment);
150 if (!SeenIdent) {
151 EmitIntValue(0, 1);
152 SeenIdent = true;
153 }
154 EmitBytes(IdentString);
155 EmitIntValue(0, 1);
156 PopSection();
Dan Gohman18eafb62017-02-22 01:23:18 +0000157}
158
159void MCWasmStreamer::EmitInstToFragment(const MCInst &Inst,
160 const MCSubtargetInfo &STI) {
161 this->MCObjectStreamer::EmitInstToFragment(Inst, STI);
162}
163
164void MCWasmStreamer::EmitInstToData(const MCInst &Inst,
165 const MCSubtargetInfo &STI) {
166 MCAssembler &Assembler = getAssembler();
167 SmallVector<MCFixup, 4> Fixups;
168 SmallString<256> Code;
169 raw_svector_ostream VecOS(Code);
170 Assembler.getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
171
172 // Append the encoded instruction to the current data fragment (or create a
173 // new such fragment if the current fragment is not a data fragment).
174 MCDataFragment *DF = getOrCreateDataFragment();
175
176 // Add the fixups and data.
177 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
178 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
179 DF->getFixups().push_back(Fixups[i]);
180 }
181 DF->setHasInstructions(true);
182 DF->getContents().append(Code.begin(), Code.end());
183}
184
185void MCWasmStreamer::FinishImpl() {
186 EmitFrames(nullptr);
187
188 this->MCObjectStreamer::FinishImpl();
189}
190
191MCStreamer *llvm::createWasmStreamer(MCContext &Context, MCAsmBackend &MAB,
192 raw_pwrite_stream &OS, MCCodeEmitter *CE,
193 bool RelaxAll) {
194 MCWasmStreamer *S = new MCWasmStreamer(Context, MAB, OS, CE);
195 if (RelaxAll)
196 S->getAssembler().setRelaxAll(true);
197 return S;
198}
199
200void MCWasmStreamer::EmitThumbFunc(MCSymbol *Func) {
201 llvm_unreachable("Generic Wasm doesn't support this directive");
202}
203
204void MCWasmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
205 llvm_unreachable("Wasm doesn't support this directive");
206}
207
Dan Gohman18eafb62017-02-22 01:23:18 +0000208void MCWasmStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
209 uint64_t Size, unsigned ByteAlignment) {
210 llvm_unreachable("Wasm doesn't support this directive");
211}
212
213void MCWasmStreamer::EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
214 uint64_t Size, unsigned ByteAlignment) {
215 llvm_unreachable("Wasm doesn't support this directive");
216}