LLVM  13.0.0git
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 
64  assert(Sym->isFunction());
65  OS << "\t.functype\t" << Sym->getName() << " ";
67  OS << "\n";
68 }
69 
71  assert(Sym->isGlobal());
72  OS << "\t.globaltype\t" << Sym->getName() << ", "
74  static_cast<wasm::ValType>(Sym->getGlobalType().Type));
75  if (!Sym->getGlobalType().Mutable)
76  OS << ", immutable";
77  OS << '\n';
78 }
79 
81  assert(Sym->isTable());
82  const wasm::WasmTableType &Type = Sym->getTableType();
83  OS << "\t.tabletype\t" << Sym->getName() << ", "
84  << WebAssembly::typeToString(static_cast<wasm::ValType>(Type.ElemType));
85  bool HasMaximum = Type.Limits.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX;
86  if (Type.Limits.Minimum != 0 || HasMaximum) {
87  OS << ", " << Type.Limits.Minimum;
88  if (HasMaximum)
89  OS << ", " << Type.Limits.Maximum;
90  }
91  OS << '\n';
92 }
93 
95  assert(Sym->isEvent());
96  OS << "\t.eventtype\t" << Sym->getName() << " ";
98  OS << "\n";
99 }
100 
102  StringRef ImportModule) {
103  OS << "\t.import_module\t" << Sym->getName() << ", "
104  << ImportModule << '\n';
105 }
106 
108  StringRef ImportName) {
109  OS << "\t.import_name\t" << Sym->getName() << ", "
110  << ImportName << '\n';
111 }
112 
114  StringRef ExportName) {
115  OS << "\t.export_name\t" << Sym->getName() << ", "
116  << ExportName << '\n';
117 }
118 
120  OS << "\t.indidx \t" << *Value << '\n';
121 }
122 
125  for (auto Type : Types) {
126  if (Grouped.empty() || Grouped.back().first != Type)
127  Grouped.push_back(std::make_pair(Type, 1));
128  else
129  ++Grouped.back().second;
130  }
131 
132  Streamer.emitULEB128IntValue(Grouped.size());
133  for (auto Pair : Grouped) {
134  Streamer.emitULEB128IntValue(Pair.second);
135  emitValueType(Pair.first);
136  }
137 }
138 
140  llvm_unreachable(".end_func is not needed for direct wasm output");
141 }
142 
144  llvm_unreachable(".indidx encoding not yet implemented");
145 }
llvm
Definition: AllocatorList.h:23
llvm::WebAssemblyTargetStreamer
WebAssembly-specific streamer interface, to implement support WebAssembly-specific assembly directive...
Definition: WebAssemblyTargetStreamer.h:29
llvm::WebAssemblyTargetWasmStreamer::emitIndIdx
void emitIndIdx(const MCExpr *Value) override
.indidx
Definition: WebAssemblyTargetStreamer.cpp:143
llvm::WebAssemblyTargetWasmStreamer::emitLocal
void emitLocal(ArrayRef< wasm::ValType > Types) override
.local
Definition: WebAssemblyTargetStreamer.cpp:123
llvm::WebAssemblyTargetAsmStreamer::emitImportName
void emitImportName(const MCSymbolWasm *Sym, StringRef ImportName) override
.import_name
Definition: WebAssemblyTargetStreamer.cpp:107
llvm::WebAssemblyTargetAsmStreamer::emitFunctionType
void emitFunctionType(const MCSymbolWasm *Sym) override
.functype
Definition: WebAssemblyTargetStreamer.cpp:63
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1167
ErrorHandling.h
llvm::MCSymbolWasm::getTableType
const wasm::WasmTableType & getTableType() const
Definition: MCSymbolWasm.h:134
llvm::WebAssemblyTargetWasmStreamer::emitEndFunc
void emitEndFunc() override
.endfunc
Definition: WebAssemblyTargetStreamer.cpp:139
llvm::WebAssembly::signatureToString
std::string signatureToString(const wasm::WasmSignature *Sig)
Definition: WebAssemblyTypeUtilities.cpp:118
llvm::MCSymbolWasm::isGlobal
bool isGlobal() const
Definition: MCSymbolWasm.h:47
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:46
llvm::WebAssemblyTargetAsmStreamer::emitEndFunc
void emitEndFunc() override
.endfunc
Definition: WebAssemblyTargetStreamer.cpp:61
llvm::WebAssemblyTargetAsmStreamer::WebAssemblyTargetAsmStreamer
WebAssemblyTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS)
Definition: WebAssemblyTargetStreamer.cpp:34
llvm::ArrayRef::empty
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:160
FormattedStream.h
llvm::MCStreamer
Streaming machine code generation interface.
Definition: MCStreamer.h:197
llvm::wasm::WasmGlobalType::Mutable
bool Mutable
Definition: Wasm.h:94
MCSymbolWasm.h
MCContext.h
llvm::wasm::WASM_LIMITS_FLAG_HAS_MAX
@ WASM_LIMITS_FLAG_HAS_MAX
Definition: Wasm.h:302
First
into llvm powi allowing the code generator to produce balanced multiplication trees First
Definition: README.txt:54
MCSubtargetInfo.h
WebAssemblyTypeUtilities.h
llvm::wasm::ValType
ValType
Definition: Wasm.h:392
llvm::MCTargetStreamer
Target specific streamer interface.
Definition: MCStreamer.h:91
llvm::MCSymbol::getName
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:197
llvm::MCSymbolWasm::getGlobalType
const wasm::WasmGlobalType & getGlobalType() const
Definition: MCSymbolWasm.h:127
llvm::WebAssembly::typeListToString
std::string typeListToString(ArrayRef< wasm::ValType > List)
Definition: WebAssemblyTypeUtilities.cpp:108
llvm::WebAssemblyTargetAsmStreamer::emitExportName
void emitExportName(const MCSymbolWasm *Sym, StringRef ExportName) override
.export_name
Definition: WebAssemblyTargetStreamer.cpp:113
WebAssemblyMCTargetDesc.h
WebAssemblyTargetStreamer.h
llvm::WebAssemblyTargetAsmStreamer::emitLocal
void emitLocal(ArrayRef< wasm::ValType > Types) override
.local
Definition: WebAssemblyTargetStreamer.cpp:54
llvm::MCTargetStreamer::Streamer
MCStreamer & Streamer
Definition: MCStreamer.h:93
llvm::WebAssemblyTargetAsmStreamer::emitIndIdx
void emitIndIdx(const MCExpr *Value) override
.indidx
Definition: WebAssemblyTargetStreamer.cpp:119
llvm::MCSymbolWasm
Definition: MCSymbolWasm.h:16
llvm::MCSymbolWasm::isEvent
bool isEvent() const
Definition: MCSymbolWasm.h:50
llvm::formatted_raw_ostream
formatted_raw_ostream - A raw_ostream that wraps another one and keeps track of line and column posit...
Definition: FormattedStream.h:30
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::MCStreamer::emitIntValue
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:131
llvm::WebAssemblyTargetAsmStreamer::emitTableType
void emitTableType(const MCSymbolWasm *Sym) override
.tabletype
Definition: WebAssemblyTargetStreamer.cpp:80
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
llvm::WebAssemblyTargetAsmStreamer::emitEventType
void emitEventType(const MCSymbolWasm *Sym) override
.eventtype
Definition: WebAssemblyTargetStreamer.cpp:94
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:136
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::WebAssemblyTargetAsmStreamer::emitImportModule
void emitImportModule(const MCSymbolWasm *Sym, StringRef ImportModule) override
.import_module
Definition: WebAssemblyTargetStreamer.cpp:101
llvm::WebAssemblyTargetStreamer::WebAssemblyTargetStreamer
WebAssemblyTargetStreamer(MCStreamer &S)
Definition: WebAssemblyTargetStreamer.cpp:27
llvm::wasm::WasmGlobalType::Type
uint8_t Type
Definition: Wasm.h:93
Casting.h
llvm::MCSymbolWasm::getSignature
const wasm::WasmSignature * getSignature() const
Definition: MCSymbolWasm.h:124
llvm::WebAssembly::typeToString
const char * typeToString(wasm::ValType Type)
Definition: WebAssemblyTypeUtilities.cpp:104
llvm::MCStreamer::emitULEB128IntValue
void emitULEB128IntValue(uint64_t Value, unsigned PadTo=0)
Special case of EmitULEB128Value that avoids the client having to pass in a MCExpr for constant integ...
Definition: MCStreamer.cpp:159
llvm::MCSymbolWasm::isFunction
bool isFunction() const
Definition: MCSymbolWasm.h:44
llvm::MCSymbolWasm::isTable
bool isTable() const
Definition: MCSymbolWasm.h:48
llvm::WebAssemblyTargetWasmStreamer::WebAssemblyTargetWasmStreamer
WebAssemblyTargetWasmStreamer(MCStreamer &S)
Definition: WebAssemblyTargetStreamer.cpp:38
llvm::WebAssemblyTargetStreamer::emitValueType
void emitValueType(wasm::ValType Type)
Definition: WebAssemblyTargetStreamer.cpp:30
printTypes
static void printTypes(formatted_raw_ostream &OS, ArrayRef< wasm::ValType > Types)
Definition: WebAssemblyTargetStreamer.cpp:41
llvm::wasm::WasmTableType
Definition: Wasm.h:70
llvm::Value
LLVM Value Representation.
Definition: Value.h:75
llvm::wasm::WasmSignature::Params
SmallVector< ValType, 4 > Params
Definition: Wasm.h:404
llvm::MCExpr
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
llvm::WebAssemblyTargetAsmStreamer::emitGlobalType
void emitGlobalType(const MCSymbolWasm *Sym) override
.globaltype
Definition: WebAssemblyTargetStreamer.cpp:70
MCSectionWasm.h