LLVM  7.0.0
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/MCSectionWasm.h"
22 #include "llvm/MC/MCSymbolWasm.h"
23 #include "llvm/Support/Casting.h"
26 using namespace llvm;
27 
29  : MCTargetStreamer(S) {}
30 
32  Streamer.EmitIntValue(uint8_t(Type), 1);
33 }
34 
37  : WebAssemblyTargetStreamer(S), OS(OS) {}
38 
41 
43  bool First = true;
44  for (MVT Type : Types) {
45  if (First)
46  First = false;
47  else
48  OS << ", ";
50  }
51  OS << '\n';
52 }
53 
55  ArrayRef<MVT> Types) {
56  if (!Types.empty()) {
57  OS << "\t.param \t";
58 
59  // FIXME: Currently this applies to the "current" function; it may
60  // be cleaner to specify an explicit symbol as part of the directive.
61 
62  PrintTypes(OS, Types);
63  }
64 }
65 
67  ArrayRef<MVT> Types) {
68  if (!Types.empty()) {
69  OS << "\t.result \t";
70 
71  // FIXME: Currently this applies to the "current" function; it may
72  // be cleaner to specify an explicit symbol as part of the directive.
73 
74  PrintTypes(OS, Types);
75  }
76 }
77 
79  if (!Types.empty()) {
80  OS << "\t.local \t";
81  PrintTypes(OS, Types);
82  }
83 }
84 
85 void WebAssemblyTargetAsmStreamer::emitEndFunc() { OS << "\t.endfunc\n"; }
86 
89  OS << "\t.functype\t" << Symbol->getName();
90  if (Results.empty())
91  OS << ", void";
92  else {
93  assert(Results.size() == 1);
94  OS << ", " << WebAssembly::TypeToString(Results.front());
95  }
96  for (auto Ty : Params)
97  OS << ", " << WebAssembly::TypeToString(Ty);
98  OS << '\n';
99 }
100 
102  OS << "\t.import_global\t" << name << '\n';
103 }
104 
106  StringRef ModuleName) {
107  OS << "\t.import_module\t" << Sym->getName() << ", " << ModuleName << '\n';
108 }
109 
111  OS << "\t.indidx \t" << *Value << '\n';
112 }
113 
115  ArrayRef<MVT> Types) {
117  for (MVT Ty : Types)
118  Params.push_back(WebAssembly::toValType(Ty));
119 
120  cast<MCSymbolWasm>(Symbol)->setParams(std::move(Params));
121 }
122 
124  ArrayRef<MVT> Types) {
126  for (MVT Ty : Types)
127  Returns.push_back(WebAssembly::toValType(Ty));
128 
129  cast<MCSymbolWasm>(Symbol)->setReturns(std::move(Returns));
130 }
131 
134  for (MVT Type : Types) {
135  if (Grouped.empty() || Grouped.back().first != Type)
136  Grouped.push_back(std::make_pair(Type, 1));
137  else
138  ++Grouped.back().second;
139  }
140 
141  Streamer.EmitULEB128IntValue(Grouped.size());
142  for (auto Pair : Grouped) {
143  Streamer.EmitULEB128IntValue(Pair.second);
145  }
146 }
147 
149  llvm_unreachable(".end_func is not needed for direct wasm output");
150 }
151 
153  llvm_unreachable(".indidx encoding not yet implemented");
154 }
155 
159  MCSymbolWasm *WasmSym = cast<MCSymbolWasm>(Symbol);
160  if (WasmSym->isFunction()) {
161  // Symbol already has its arguments and result set.
162  return;
163  }
164 
166  for (MVT Ty : Params)
167  ValParams.push_back(WebAssembly::toValType(Ty));
168 
170  for (MVT Ty : Results)
171  ValResults.push_back(WebAssembly::toValType(Ty));
172 
173  WasmSym->setParams(std::move(ValParams));
174  WasmSym->setReturns(std::move(ValResults));
176 }
177 
179  llvm_unreachable(".global_import is not needed for direct wasm output");
180 }
181 
183  StringRef ModuleName) {
184  Sym->setModuleName(ModuleName);
185 }
bool isFunction() const
Definition: MCSymbolWasm.h:45
void push_back(const T &Elt)
Definition: SmallVector.h:218
const char * TypeToString(MVT Ty)
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
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
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...
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
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
void emitImportModule(MCSymbolWasm *Sym, StringRef ModuleName) override
.import_module
WebAssemblyTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS)
size_t size() const
Definition: SmallVector.h:53
#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
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:56
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