LLVM  7.0.0svn
WebAssemblyTargetStreamer.cpp
Go to the documentation of this file.
1 //==-- WebAssemblyTargetStreamer.cpp - WebAssembly Target Streamer Methods --=//
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 /// \file
11 /// This file defines WebAssembly-specific target streamer classes.
12 /// These are for implementing support for target-specific assembly directives.
13 ///
14 //===----------------------------------------------------------------------===//
15 
19 #include "llvm/MC/MCContext.h"
20 #include "llvm/MC/MCSectionELF.h"
21 #include "llvm/MC/MCSectionWasm.h"
23 #include "llvm/MC/MCSymbolELF.h"
24 #include "llvm/MC/MCSymbolWasm.h"
25 #include "llvm/Support/Casting.h"
28 using namespace llvm;
29 
31  : MCTargetStreamer(S) {}
32 
34  Streamer.EmitIntValue(uint8_t(Type), 1);
35 }
36 
39  : WebAssemblyTargetStreamer(S), OS(OS) {}
40 
43 
46 
48  bool First = true;
49  for (MVT Type : Types) {
50  if (First)
51  First = false;
52  else
53  OS << ", ";
55  }
56  OS << '\n';
57 }
58 
60  ArrayRef<MVT> Types) {
61  if (!Types.empty()) {
62  OS << "\t.param \t";
63 
64  // FIXME: Currently this applies to the "current" function; it may
65  // be cleaner to specify an explicit symbol as part of the directive.
66 
67  PrintTypes(OS, Types);
68  }
69 }
70 
72  ArrayRef<MVT> Types) {
73  if (!Types.empty()) {
74  OS << "\t.result \t";
75 
76  // FIXME: Currently this applies to the "current" function; it may
77  // be cleaner to specify an explicit symbol as part of the directive.
78 
79  PrintTypes(OS, Types);
80  }
81 }
82 
84  if (!Types.empty()) {
85  OS << "\t.local \t";
86  PrintTypes(OS, Types);
87  }
88 }
89 
90 void WebAssemblyTargetAsmStreamer::emitEndFunc() { OS << "\t.endfunc\n"; }
91 
94  OS << "\t.functype\t" << Symbol->getName();
95  if (Results.empty())
96  OS << ", void";
97  else {
98  assert(Results.size() == 1);
99  OS << ", " << WebAssembly::TypeToString(Results.front());
100  }
101  for (auto Ty : Params)
102  OS << ", " << WebAssembly::TypeToString(Ty);
103  OS << '\n';
104 }
105 
107  OS << "\t.import_global\t" << name << '\n';
108 }
109 
111  StringRef ModuleName) {
112  OS << "\t.import_module\t" << Sym->getName() << ", " << ModuleName << '\n';
113 }
114 
116  OS << "\t.indidx \t" << *Value << '\n';
117 }
118 
120  ArrayRef<MVT> Types) {
121  // Nothing to emit; params are declared as part of the function signature.
122 }
123 
125  ArrayRef<MVT> Types) {
126  // Nothing to emit; results are declared as part of the function signature.
127 }
128 
131  for (MVT Type : Types)
133 }
134 
137 }
138 
140  llvm_unreachable(".indidx encoding not yet implemented");
141 }
142 
145  // Nothing to emit here. TODO: Re-design how linking works and re-evaluate
146  // whether it's necessary for .o files to declare indirect function types.
147 }
148 
150 }
151 
153  StringRef ModuleName) {
154  llvm_unreachable(".import_module encoding not yet implemented");
155 }
156 
158  ArrayRef<MVT> Types) {
160  for (MVT Ty : Types)
161  Params.push_back(WebAssembly::toValType(Ty));
162 
163  cast<MCSymbolWasm>(Symbol)->setParams(std::move(Params));
164 }
165 
167  ArrayRef<MVT> Types) {
169  for (MVT Ty : Types)
170  Returns.push_back(WebAssembly::toValType(Ty));
171 
172  cast<MCSymbolWasm>(Symbol)->setReturns(std::move(Returns));
173 }
174 
177  for (MVT Type : Types) {
178  if (Grouped.empty() || Grouped.back().first != Type)
179  Grouped.push_back(std::make_pair(Type, 1));
180  else
181  ++Grouped.back().second;
182  }
183 
184  Streamer.EmitULEB128IntValue(Grouped.size());
185  for (auto Pair : Grouped) {
186  Streamer.EmitULEB128IntValue(Pair.second);
188  }
189 }
190 
192  llvm_unreachable(".end_func is not needed for direct wasm output");
193 }
194 
196  llvm_unreachable(".indidx encoding not yet implemented");
197 }
198 
202  MCSymbolWasm *WasmSym = cast<MCSymbolWasm>(Symbol);
203  if (WasmSym->isFunction()) {
204  // Symbol already has its arguments and result set.
205  return;
206  }
207 
209  for (MVT Ty : Params)
210  ValParams.push_back(WebAssembly::toValType(Ty));
211 
213  for (MVT Ty : Results)
214  ValResults.push_back(WebAssembly::toValType(Ty));
215 
216  WasmSym->setParams(std::move(ValParams));
217  WasmSym->setReturns(std::move(ValResults));
219 }
220 
222  llvm_unreachable(".global_import is not needed for direct wasm output");
223 }
224 
226  StringRef ModuleName) {
227  Sym->setModuleName(ModuleName);
228 }
bool isFunction() const
Definition: MCSymbolWasm.h:45
void push_back(const T &Elt)
Definition: SmallVector.h:213
const char * TypeToString(MVT Ty)
void emitLocal(ArrayRef< MVT > Types) override
.local
void setModuleName(StringRef Name)
Definition: MCSymbolWasm.h:62
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
void emitIndirectFunctionType(MCSymbol *Symbol, SmallVectorImpl< MVT > &Params, SmallVectorImpl< MVT > &Results) override
.functype
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:137
formatted_raw_ostream - A raw_ostream that wraps another one and keeps track of line and column posit...
This class prints an WebAssembly MCInst to wasm file syntax.
Target specific streamer interface.
Definition: MCStreamer.h:83
void emitImportModule(MCSymbolWasm *Sym, StringRef ModuleName) override
.import_module
Function Alias Analysis Results
void setParams(SmallVectorImpl< wasm::ValType > &&Pars)
Definition: MCSymbolWasm.h:79
void emitGlobalImport(StringRef name) override
.import_global
void emitLocal(ArrayRef< MVT > Types) override
.local
void emitParam(MCSymbol *Symbol, ArrayRef< MVT > Types) override
.param
void emitIndirectFunctionType(MCSymbol *Symbol, SmallVectorImpl< MVT > &Params, SmallVectorImpl< MVT > &Results) override
.functype
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:36
void emitIndIdx(const MCExpr *Value) override
.indidx
void emitGlobalImport(StringRef name) override
.import_global
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
virtual void EmitIntValue(uint64_t Value, unsigned Size)
Special case of EmitValue that avoids the client having to pass in a MCExpr for constant integers...
Definition: MCStreamer.cpp:112
wasm::ValType toValType(const MVT &Ty)
WebAssembly-specific streamer interface, to implement support WebAssembly-specific assembly directive...
void emitImportModule(MCSymbolWasm *Sym, StringRef ModuleName) override
.import_module
Streaming machine code generation interface.
Definition: MCStreamer.h:183
Machine Value Type.
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
void emitIndIdx(const MCExpr *Value) override
.indidx
This file provides WebAssembly-specific target descriptions.
static void PrintTypes(formatted_raw_ostream &OS, ArrayRef< MVT > Types)
void emitIndIdx(const MCExpr *Value) override
.indidx
void setReturns(SmallVectorImpl< wasm::ValType > &&Rets)
Definition: MCSymbolWasm.h:69
static const unsigned End
void emitResult(MCSymbol *Symbol, ArrayRef< MVT > Types) override
.result
void emitImportModule(MCSymbolWasm *Sym, StringRef ModuleName) override
.import_module
WebAssemblyTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void setType(wasm::WasmSymbolType type)
Definition: MCSymbolWasm.h:50
This file declares WebAssembly-specific target streamer classes.
MCStreamer & Streamer
Definition: MCStreamer.h:85
void emitParam(MCSymbol *Symbol, ArrayRef< MVT > Types) override
.param
void emitIndirectFunctionType(MCSymbol *Symbol, SmallVectorImpl< MVT > &Params, SmallVectorImpl< MVT > &Results) override
.functype
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:62
void emitParam(MCSymbol *Symbol, ArrayRef< MVT > Types) override
.param
void emitLocal(ArrayRef< MVT > Types) override
.local
void emitGlobalImport(StringRef name) override
.import_global
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:203
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void emitResult(MCSymbol *Symbol, ArrayRef< MVT > Types) override
.result
LLVM Value Representation.
Definition: Value.h:73
static const char * name
void emitResult(MCSymbol *Symbol, ArrayRef< MVT > Types) override
.result
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
void EmitULEB128IntValue(uint64_t Value)
Special case of EmitULEB128Value that avoids the client having to pass in a MCExpr for constant integ...
Definition: MCStreamer.cpp:127
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:144