LLVM  8.0.1
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 
44  bool First = true;
45  for (auto Type : Types) {
46  if (First)
47  First = false;
48  else
49  OS << ", ";
51  }
52  OS << '\n';
53 }
54 
56  if (!Types.empty()) {
57  OS << "\t.local \t";
58  printTypes(OS, Types);
59  }
60 }
61 
62 void WebAssemblyTargetAsmStreamer::emitEndFunc() { OS << "\t.endfunc\n"; }
63 
64 void WebAssemblyTargetAsmStreamer::emitSignature(
65  const wasm::WasmSignature *Sig) {
66  OS << "(";
67  emitParamList(Sig);
68  OS << ") -> (";
69  emitReturnList(Sig);
70  OS << ")";
71 }
72 
73 void WebAssemblyTargetAsmStreamer::emitParamList(
74  const wasm::WasmSignature *Sig) {
75  auto &Params = Sig->Params;
76  for (auto &Ty : Params) {
77  if (&Ty != &Params[0])
78  OS << ", ";
79  OS << WebAssembly::typeToString(Ty);
80  }
81 }
82 
83 void WebAssemblyTargetAsmStreamer::emitReturnList(
84  const wasm::WasmSignature *Sig) {
85  auto &Returns = Sig->Returns;
86  for (auto &Ty : Returns) {
87  if (&Ty != &Returns[0])
88  OS << ", ";
89  OS << WebAssembly::typeToString(Ty);
90  }
91 }
92 
94  assert(Sym->isFunction());
95  OS << "\t.functype\t" << Sym->getName() << " ";
96  emitSignature(Sym->getSignature());
97  OS << "\n";
98 }
99 
101  assert(Sym->isGlobal());
102  OS << "\t.globaltype\t" << Sym->getName() << ", "
104  static_cast<wasm::ValType>(Sym->getGlobalType().Type))
105  << '\n';
106 }
107 
109  assert(Sym->isEvent());
110  OS << "\t.eventtype\t" << Sym->getName() << " ";
111  emitParamList(Sym->getSignature());
112  OS << "\n";
113 }
114 
116  StringRef ImportModule) {
117  OS << "\t.import_module\t" << Sym->getName() << ", "
118  << ImportModule << '\n';
119 }
120 
122  StringRef ImportName) {
123  OS << "\t.import_name\t" << Sym->getName() << ", "
124  << ImportName << '\n';
125 }
126 
128  OS << "\t.indidx \t" << *Value << '\n';
129 }
130 
133  for (auto Type : Types) {
134  if (Grouped.empty() || Grouped.back().first != Type)
135  Grouped.push_back(std::make_pair(Type, 1));
136  else
137  ++Grouped.back().second;
138  }
139 
140  Streamer.EmitULEB128IntValue(Grouped.size());
141  for (auto Pair : Grouped) {
142  Streamer.EmitULEB128IntValue(Pair.second);
143  emitValueType(Pair.first);
144  }
145 }
146 
148  llvm_unreachable(".end_func is not needed for direct wasm output");
149 }
150 
152  llvm_unreachable(".indidx encoding not yet implemented");
153 }
bool isFunction() const
Definition: MCSymbolWasm.h:42
void emitGlobalType(const MCSymbolWasm *Sym) override
.globaltype
This class represents lattice values for constants.
Definition: AllocatorList.h:24
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:84
SmallVector< wasm::ValType, 1 > Returns
Definition: Wasm.h:309
const wasm::WasmGlobalType & getGlobalType() const
Definition: MCSymbolWasm.h:78
static void printTypes(formatted_raw_ostream &OS, ArrayRef< wasm::ValType > Types)
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:36
bool isGlobal() const
Definition: MCSymbolWasm.h:44
void emitIndIdx(const MCExpr *Value) override
.indidx
const wasm::WasmSignature * getSignature() const
Definition: MCSymbolWasm.h:75
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
void emitImportName(const MCSymbolWasm *Sym, StringRef ImportName) override
.import_name
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:124
WebAssembly-specific streamer interface, to implement support WebAssembly-specific assembly directive...
Streaming machine code generation interface.
Definition: MCStreamer.h:189
void emitImportModule(const MCSymbolWasm *Sym, StringRef ImportModule) override
.import_module
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.
void emitIndIdx(const MCExpr *Value) override
.indidx
WebAssemblyTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS)
void emitLocal(ArrayRef< wasm::ValType > Types) override
.local
size_t size() const
Definition: SmallVector.h:53
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This file declares WebAssembly-specific target streamer classes.
void emitLocal(ArrayRef< wasm::ValType > Types) override
.local
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:847
MCStreamer & Streamer
Definition: MCStreamer.h:86
void emitFunctionType(const MCSymbolWasm *Sym) override
.functype
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:56
void emitEventType(const MCSymbolWasm *Sym) override
.eventtype
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:203
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:73
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:139
const char * typeToString(wasm::ValType Ty)
SmallVector< wasm::ValType, 4 > Params
Definition: Wasm.h:310
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:144
bool isEvent() const
Definition: MCSymbolWasm.h:46