LLVM  9.0.0svn
WebAssemblyTargetStreamer.cpp
Go to the documentation of this file.
1 //==-- WebAssemblyTargetStreamer.cpp - WebAssembly Target Streamer Methods --=//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// This file defines WebAssembly-specific target streamer classes.
11 /// These are for implementing support for target-specific assembly directives.
12 ///
13 //===----------------------------------------------------------------------===//
14 
18 #include "llvm/MC/MCContext.h"
19 #include "llvm/MC/MCSectionWasm.h"
21 #include "llvm/MC/MCSymbolWasm.h"
22 #include "llvm/Support/Casting.h"
25 using namespace llvm;
26 
28  : MCTargetStreamer(S) {}
29 
31  Streamer.EmitIntValue(uint8_t(Type), 1);
32 }
33 
36  : WebAssemblyTargetStreamer(S), OS(OS) {}
37 
40 
43  bool First = true;
44  for (auto Type : Types) {
45  if (First)
46  First = false;
47  else
48  OS << ", ";
50  }
51  OS << '\n';
52 }
53 
55  if (!Types.empty()) {
56  OS << "\t.local \t";
57  printTypes(OS, Types);
58  }
59 }
60 
61 void WebAssemblyTargetAsmStreamer::emitEndFunc() { OS << "\t.endfunc\n"; }
62 
63 void WebAssemblyTargetAsmStreamer::emitSignature(
64  const wasm::WasmSignature *Sig) {
65  OS << "(";
66  emitParamList(Sig);
67  OS << ") -> (";
68  emitReturnList(Sig);
69  OS << ")";
70 }
71 
72 void WebAssemblyTargetAsmStreamer::emitParamList(
73  const wasm::WasmSignature *Sig) {
74  auto &Params = Sig->Params;
75  for (auto &Ty : Params) {
76  if (&Ty != &Params[0])
77  OS << ", ";
78  OS << WebAssembly::typeToString(Ty);
79  }
80 }
81 
82 void WebAssemblyTargetAsmStreamer::emitReturnList(
83  const wasm::WasmSignature *Sig) {
84  auto &Returns = Sig->Returns;
85  for (auto &Ty : Returns) {
86  if (&Ty != &Returns[0])
87  OS << ", ";
88  OS << WebAssembly::typeToString(Ty);
89  }
90 }
91 
93  assert(Sym->isFunction());
94  OS << "\t.functype\t" << Sym->getName() << " ";
95  emitSignature(Sym->getSignature());
96  OS << "\n";
97 }
98 
100  assert(Sym->isGlobal());
101  OS << "\t.globaltype\t" << Sym->getName() << ", "
103  static_cast<wasm::ValType>(Sym->getGlobalType().Type))
104  << '\n';
105 }
106 
108  assert(Sym->isEvent());
109  OS << "\t.eventtype\t" << Sym->getName() << " ";
110  emitParamList(Sym->getSignature());
111  OS << "\n";
112 }
113 
115  StringRef ImportModule) {
116  OS << "\t.import_module\t" << Sym->getName() << ", "
117  << ImportModule << '\n';
118 }
119 
121  StringRef ImportName) {
122  OS << "\t.import_name\t" << Sym->getName() << ", "
123  << ImportName << '\n';
124 }
125 
127  OS << "\t.indidx \t" << *Value << '\n';
128 }
129 
132  for (auto Type : Types) {
133  if (Grouped.empty() || Grouped.back().first != Type)
134  Grouped.push_back(std::make_pair(Type, 1));
135  else
136  ++Grouped.back().second;
137  }
138 
139  Streamer.EmitULEB128IntValue(Grouped.size());
140  for (auto Pair : Grouped) {
141  Streamer.EmitULEB128IntValue(Pair.second);
142  emitValueType(Pair.first);
143  }
144 }
145 
147  llvm_unreachable(".end_func is not needed for direct wasm output");
148 }
149 
151  llvm_unreachable(".indidx encoding not yet implemented");
152 }
bool isFunction() const
Definition: MCSymbolWasm.h:41
void emitGlobalType(const MCSymbolWasm *Sym) override
.globaltype
This class represents lattice values for constants.
Definition: AllocatorList.h:23
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
const wasm::WasmGlobalType & getGlobalType() const
Definition: MCSymbolWasm.h:84
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:35
SmallVector< ValType, 1 > Returns
Definition: Wasm.h:315
SmallVector< ValType, 4 > Params
Definition: Wasm.h:316
bool isGlobal() const
Definition: MCSymbolWasm.h:43
void emitIndIdx(const MCExpr *Value) override
.indidx
const wasm::WasmSignature * getSignature() const
Definition: MCSymbolWasm.h:81
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
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:123
WebAssembly-specific streamer interface, to implement support WebAssembly-specific assembly directive...
Streaming machine code generation interface.
Definition: MCStreamer.h:188
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:45
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:52
#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:839
MCStreamer & Streamer
Definition: MCStreamer.h:85
void emitFunctionType(const MCSymbolWasm *Sym) override
.functype
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:55
void emitEventType(const MCSymbolWasm *Sym) override
.eventtype
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:202
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:72
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
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:138
const char * typeToString(wasm::ValType Ty)
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:143
bool isEvent() const
Definition: MCSymbolWasm.h:45