LLVM  8.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/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 
90  OS << "\t.functype\t" << Symbol->getName();
91  if (Results.empty())
92  OS << ", void";
93  else {
94  assert(Results.size() == 1);
95  OS << ", " << WebAssembly::TypeToString(Results.front());
96  }
97  for (auto Ty : Params)
98  OS << ", " << WebAssembly::TypeToString(Ty);
99  OS << '\n';
100 }
101 
103  OS << "\t.import_global\t" << name << '\n';
104 }
105 
107  StringRef ModuleName) {
108  OS << "\t.import_module\t" << Sym->getName() << ", " << ModuleName << '\n';
109 }
110 
112  OS << "\t.indidx \t" << *Value << '\n';
113 }
114 
116  ArrayRef<MVT> Types) {
118  for (MVT Ty : Types)
119  Params.push_back(WebAssembly::toValType(Ty));
120 
121  cast<MCSymbolWasm>(Symbol)->setParams(std::move(Params));
122 }
123 
125  ArrayRef<MVT> Types) {
127  for (MVT Ty : Types)
128  Returns.push_back(WebAssembly::toValType(Ty));
129 
130  cast<MCSymbolWasm>(Symbol)->setReturns(std::move(Returns));
131 }
132 
135  for (MVT Type : Types) {
136  if (Grouped.empty() || Grouped.back().first != Type)
137  Grouped.push_back(std::make_pair(Type, 1));
138  else
139  ++Grouped.back().second;
140  }
141 
142  Streamer.EmitULEB128IntValue(Grouped.size());
143  for (auto Pair : Grouped) {
144  Streamer.EmitULEB128IntValue(Pair.second);
146  }
147 }
148 
150  llvm_unreachable(".end_func is not needed for direct wasm output");
151 }
152 
154  llvm_unreachable(".indidx encoding not yet implemented");
155 }
156 
160  MCSymbolWasm *WasmSym = cast<MCSymbolWasm>(Symbol);
161  if (WasmSym->isFunction()) {
162  // Symbol already has its arguments and result set.
163  return;
164  }
165 
167  for (MVT Ty : Params)
168  ValParams.push_back(WebAssembly::toValType(Ty));
169 
171  for (MVT Ty : Results)
172  ValResults.push_back(WebAssembly::toValType(Ty));
173 
174  WasmSym->setParams(std::move(ValParams));
175  WasmSym->setReturns(std::move(ValResults));
177 }
178 
180  llvm_unreachable(".global_import is not needed for direct wasm output");
181 }
182 
184  StringRef ModuleName) {
185  Sym->setModuleName(ModuleName);
186 }
bool isFunction() const
Definition: MCSymbolWasm.h:44
void push_back(const T &Elt)
Definition: SmallVector.h:218
const char * TypeToString(MVT Ty)
void setModuleName(StringRef Name)
Definition: MCSymbolWasm.h:61
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:78
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:68
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:49
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