LLVM  13.0.0git
WebAssemblyTypeUtilities.cpp
Go to the documentation of this file.
1 //===-- WebAssemblyTypeUtilities.cpp - WebAssembly Type Utility Functions -===//
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 implements several utility functions for WebAssembly type parsing.
11 ///
12 //===----------------------------------------------------------------------===//
13 
15 #include "llvm/ADT/StringSwitch.h"
16 
17 using namespace llvm;
18 
20  // FIXME: can't use StringSwitch because wasm::ValType doesn't have a
21  // "invalid" value.
22  if (Type == "i32")
23  return wasm::ValType::I32;
24  if (Type == "i64")
25  return wasm::ValType::I64;
26  if (Type == "f32")
27  return wasm::ValType::F32;
28  if (Type == "f64")
29  return wasm::ValType::F64;
30  if (Type == "v128" || Type == "i8x16" || Type == "i16x8" || Type == "i32x4" ||
31  Type == "i64x2" || Type == "f32x4" || Type == "f64x2")
32  return wasm::ValType::V128;
33  if (Type == "funcref")
35  if (Type == "externref")
37  return Optional<wasm::ValType>();
38 }
39 
45 }
46 
48  // Multivalue block types are handled separately in parseSignature
59 }
60 
62  return StringSwitch<MVT>(Type)
63  .Case("i32", MVT::i32)
64  .Case("i64", MVT::i64)
65  .Case("f32", MVT::f32)
66  .Case("f64", MVT::f64)
67  .Case("i64", MVT::i64)
68  .Case("v16i8", MVT::v16i8)
69  .Case("v8i16", MVT::v8i16)
70  .Case("v4i32", MVT::v4i32)
71  .Case("v2i64", MVT::v2i64)
72  .Case("funcref", MVT::funcref)
73  .Case("externref", MVT::externref)
75 }
76 
77 // We have various enums representing a subset of these types, use this
78 // function to convert any of them to text.
79 const char *WebAssembly::anyTypeToString(unsigned Type) {
80  switch (Type) {
82  return "i32";
84  return "i64";
86  return "f32";
88  return "f64";
90  return "v128";
92  return "funcref";
94  return "externref";
96  return "func";
98  return "void";
99  default:
100  return "invalid_type";
101  }
102 }
103 
105  return anyTypeToString(static_cast<unsigned>(Type));
106 }
107 
109  std::string S;
110  for (const auto &Type : List) {
111  if (&Type != &List[0])
112  S += ", ";
114  }
115  return S;
116 }
117 
119  std::string S("(");
120  S += typeListToString(Sig->Params);
121  S += ") -> (";
122  S += typeListToString(Sig->Returns);
123  S += ")";
124  return S;
125 }
126 
128  switch (Type.SimpleTy) {
129  case MVT::i32:
130  return wasm::ValType::I32;
131  case MVT::i64:
132  return wasm::ValType::I64;
133  case MVT::f32:
134  return wasm::ValType::F32;
135  case MVT::f64:
136  return wasm::ValType::F64;
137  case MVT::v16i8:
138  case MVT::v8i16:
139  case MVT::v4i32:
140  case MVT::v2i64:
141  case MVT::v4f32:
142  case MVT::v2f64:
143  return wasm::ValType::V128;
144  case MVT::funcref:
145  return wasm::ValType::FUNCREF;
146  case MVT::externref:
148  default:
149  llvm_unreachable("unexpected type");
150  }
151 }
llvm::StringSwitch::Case
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:67
llvm::WebAssembly::parseMVT
MVT parseMVT(StringRef Type)
Definition: WebAssemblyTypeUtilities.cpp:61
llvm::WebAssembly::BlockType::I64
@ I64
llvm
Definition: AllocatorList.h:23
llvm::wasm::ValType::I32
@ I32
llvm::WebAssembly::HeapType
HeapType
Used as immediate MachineOperands for heap types, e.g. for ref.null.
Definition: WebAssemblyTypeUtilities.h:45
llvm::StringSwitch::Default
LLVM_NODISCARD R Default(T Value)
Definition: StringSwitch.h:181
llvm::WebAssembly::HeapType::Funcref
@ Funcref
llvm::WebAssembly::signatureToString
std::string signatureToString(const wasm::WasmSignature *Sig)
Definition: WebAssemblyTypeUtilities.cpp:118
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:46
llvm::Optional
Definition: APInt.h:33
llvm::WebAssembly::BlockType::Invalid
@ Invalid
llvm::MVT::v2f64
@ v2f64
Definition: MachineValueType.h:160
llvm::WebAssembly::BlockType::F64
@ F64
llvm::WebAssembly::BlockType::Void
@ Void
llvm::wasm::WASM_TYPE_NORESULT
@ WASM_TYPE_NORESULT
Definition: Wasm.h:254
llvm::MVT::funcref
@ funcref
Definition: MachineValueType.h:257
llvm::WebAssembly::parseType
Optional< wasm::ValType > parseType(StringRef Type)
Definition: WebAssemblyTypeUtilities.cpp:19
llvm::wasm::ValType::F64
@ F64
llvm::MVT::f64
@ f64
Definition: MachineValueType.h:53
llvm::wasm::ValType::F32
@ F32
llvm::WebAssembly::toValType
wasm::ValType toValType(MVT Type)
Definition: WebAssemblyTypeUtilities.cpp:127
llvm::WebAssembly::BlockType::V128
@ V128
WebAssemblyTypeUtilities.h
llvm::wasm::ValType
ValType
Definition: Wasm.h:392
llvm::MVT::INVALID_SIMPLE_VALUE_TYPE
@ INVALID_SIMPLE_VALUE_TYPE
Definition: MachineValueType.h:35
llvm::WebAssembly::typeListToString
std::string typeListToString(ArrayRef< wasm::ValType > List)
Definition: WebAssemblyTypeUtilities.cpp:108
llvm::wasm::WASM_TYPE_FUNC
@ WASM_TYPE_FUNC
Definition: Wasm.h:253
llvm::WebAssembly::BlockType::Funcref
@ Funcref
llvm::MVT::v16i8
@ v16i8
Definition: MachineValueType.h:77
llvm::MVT::v2i64
@ v2i64
Definition: MachineValueType.h:110
llvm::wasm::WASM_TYPE_V128
@ WASM_TYPE_V128
Definition: Wasm.h:250
llvm::wasm::ValType::EXTERNREF
@ EXTERNREF
llvm::MVT::externref
@ externref
Definition: MachineValueType.h:258
llvm::wasm::ValType::I64
@ I64
llvm::wasm::ValType::FUNCREF
@ FUNCREF
llvm::wasm::WasmSignature::Returns
SmallVector< ValType, 1 > Returns
Definition: Wasm.h:403
llvm::MVT::v4f32
@ v4f32
Definition: MachineValueType.h:147
llvm::WebAssembly::BlockType::Externref
@ Externref
llvm::MVT
Machine Value Type.
Definition: MachineValueType.h:30
llvm::wasm::WASM_TYPE_EXTERNREF
@ WASM_TYPE_EXTERNREF
Definition: Wasm.h:252
llvm::wasm::WASM_TYPE_F32
@ WASM_TYPE_F32
Definition: Wasm.h:248
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
llvm::MVT::i64
@ i64
Definition: MachineValueType.h:44
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
llvm::WebAssembly::BlockType::I32
@ I32
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::MVT::v4i32
@ v4i32
Definition: MachineValueType.h:97
llvm::wasm::WASM_TYPE_FUNCREF
@ WASM_TYPE_FUNCREF
Definition: Wasm.h:251
llvm::wasm::WASM_TYPE_I64
@ WASM_TYPE_I64
Definition: Wasm.h:247
llvm::wasm::WASM_TYPE_I32
@ WASM_TYPE_I32
Definition: Wasm.h:246
llvm::WebAssembly::HeapType::Externref
@ Externref
llvm::wasm::WASM_TYPE_F64
@ WASM_TYPE_F64
Definition: Wasm.h:249
llvm::MVT::v8i16
@ v8i16
Definition: MachineValueType.h:87
llvm::MVT::i32
@ i32
Definition: MachineValueType.h:43
StringSwitch.h
llvm::WebAssembly::parseBlockType
BlockType parseBlockType(StringRef Type)
Definition: WebAssemblyTypeUtilities.cpp:47
llvm::WebAssembly::typeToString
const char * typeToString(wasm::ValType Type)
Definition: WebAssemblyTypeUtilities.cpp:104
llvm::wasm::ValType::V128
@ V128
llvm::WebAssembly::HeapType::Invalid
@ Invalid
List
const NodeList & List
Definition: RDFGraph.cpp:201
llvm::WebAssembly::BlockType
BlockType
Used as immediate MachineOperands for block signatures.
Definition: WebAssemblyTypeUtilities.h:26
llvm::StringSwitch
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:42
llvm::WebAssembly::anyTypeToString
const char * anyTypeToString(unsigned Type)
Definition: WebAssemblyTypeUtilities.cpp:79
llvm::MVT::f32
@ f32
Definition: MachineValueType.h:52
llvm::wasm::WasmSignature::Params
SmallVector< ValType, 4 > Params
Definition: Wasm.h:404
llvm::wasm::WasmSignature
Definition: Wasm.h:402
llvm::WebAssembly::BlockType::F32
@ F32
llvm::WebAssembly::parseHeapType
HeapType parseHeapType(StringRef Type)
Definition: WebAssemblyTypeUtilities.cpp:40