LLVM  14.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 // Get register classes enum.
18 #define GET_REGINFO_ENUM
19 #include "WebAssemblyGenRegisterInfo.inc"
20 
21 using namespace llvm;
22 
24  // FIXME: can't use StringSwitch because wasm::ValType doesn't have a
25  // "invalid" value.
26  if (Type == "i32")
27  return wasm::ValType::I32;
28  if (Type == "i64")
29  return wasm::ValType::I64;
30  if (Type == "f32")
31  return wasm::ValType::F32;
32  if (Type == "f64")
33  return wasm::ValType::F64;
34  if (Type == "v128" || Type == "i8x16" || Type == "i16x8" || Type == "i32x4" ||
35  Type == "i64x2" || Type == "f32x4" || Type == "f64x2")
36  return wasm::ValType::V128;
37  if (Type == "funcref")
39  if (Type == "externref")
41  return Optional<wasm::ValType>();
42 }
43 
45  // Multivalue block types are handled separately in parseSignature
56 }
57 
59  return StringSwitch<MVT>(Type)
60  .Case("i32", MVT::i32)
61  .Case("i64", MVT::i64)
62  .Case("f32", MVT::f32)
63  .Case("f64", MVT::f64)
64  .Case("i64", MVT::i64)
65  .Case("v16i8", MVT::v16i8)
66  .Case("v8i16", MVT::v8i16)
67  .Case("v4i32", MVT::v4i32)
68  .Case("v2i64", MVT::v2i64)
69  .Case("funcref", MVT::funcref)
70  .Case("externref", MVT::externref)
72 }
73 
74 // We have various enums representing a subset of these types, use this
75 // function to convert any of them to text.
76 const char *WebAssembly::anyTypeToString(unsigned Type) {
77  switch (Type) {
79  return "i32";
81  return "i64";
83  return "f32";
85  return "f64";
87  return "v128";
89  return "funcref";
91  return "externref";
93  return "func";
95  return "void";
96  default:
97  return "invalid_type";
98  }
99 }
100 
102  return anyTypeToString(static_cast<unsigned>(Type));
103 }
104 
106  std::string S;
107  for (const auto &Type : List) {
108  if (&Type != &List[0])
109  S += ", ";
111  }
112  return S;
113 }
114 
116  std::string S("(");
117  S += typeListToString(Sig->Params);
118  S += ") -> (";
119  S += typeListToString(Sig->Returns);
120  S += ")";
121  return S;
122 }
123 
125  switch (Type.SimpleTy) {
126  case MVT::i32:
127  return wasm::ValType::I32;
128  case MVT::i64:
129  return wasm::ValType::I64;
130  case MVT::f32:
131  return wasm::ValType::F32;
132  case MVT::f64:
133  return wasm::ValType::F64;
134  case MVT::v16i8:
135  case MVT::v8i16:
136  case MVT::v4i32:
137  case MVT::v2i64:
138  case MVT::v4f32:
139  case MVT::v2f64:
140  return wasm::ValType::V128;
141  case MVT::funcref:
142  return wasm::ValType::FUNCREF;
143  case MVT::externref:
145  default:
146  llvm_unreachable("unexpected type");
147  }
148 }
149 
151  switch (RC) {
152  case WebAssembly::I32RegClassID:
153  return wasm::ValType::I32;
154  case WebAssembly::I64RegClassID:
155  return wasm::ValType::I64;
156  case WebAssembly::F32RegClassID:
157  return wasm::ValType::F32;
158  case WebAssembly::F64RegClassID:
159  return wasm::ValType::F64;
160  case WebAssembly::V128RegClassID:
161  return wasm::ValType::V128;
162  case WebAssembly::FUNCREFRegClassID:
163  return wasm::ValType::FUNCREF;
164  case WebAssembly::EXTERNREFRegClassID:
166  default:
167  llvm_unreachable("unexpected type");
168  }
169 }
llvm::StringSwitch::Case
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:68
llvm::wasm::WASM_TYPE_I32
@ WASM_TYPE_I32
Definition: Wasm.h:253
llvm::WebAssembly::parseMVT
MVT parseMVT(StringRef Type)
Definition: WebAssemblyTypeUtilities.cpp:58
llvm::WebAssembly::BlockType::I64
@ I64
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:22
llvm::wasm::ValType::I32
@ I32
llvm::StringSwitch::Default
LLVM_NODISCARD R Default(T Value)
Definition: StringSwitch.h:182
llvm::WebAssembly::signatureToString
std::string signatureToString(const wasm::WasmSignature *Sig)
Definition: WebAssemblyTypeUtilities.cpp:115
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
llvm::Optional
Definition: APInt.h:33
llvm::WebAssembly::BlockType::Invalid
@ Invalid
llvm::MVT::v2f64
@ v2f64
Definition: MachineValueType.h:172
llvm::WebAssembly::BlockType::F64
@ F64
llvm::wasm::WASM_TYPE_FUNCREF
@ WASM_TYPE_FUNCREF
Definition: Wasm.h:258
llvm::WebAssembly::regClassToValType
wasm::ValType regClassToValType(unsigned RC)
Definition: WebAssemblyTypeUtilities.cpp:150
llvm::WebAssembly::BlockType::Void
@ Void
llvm::MVT::funcref
@ funcref
Definition: MachineValueType.h:270
llvm::WebAssembly::parseType
Optional< wasm::ValType > parseType(StringRef Type)
Definition: WebAssemblyTypeUtilities.cpp:23
llvm::wasm::WASM_TYPE_F32
@ WASM_TYPE_F32
Definition: Wasm.h:255
llvm::wasm::ValType::F64
@ F64
llvm::MVT::f64
@ f64
Definition: MachineValueType.h:56
llvm::wasm::WASM_TYPE_V128
@ WASM_TYPE_V128
Definition: Wasm.h:257
llvm::wasm::ValType::F32
@ F32
llvm::WebAssembly::toValType
wasm::ValType toValType(MVT Type)
Definition: WebAssemblyTypeUtilities.cpp:124
llvm::WebAssembly::BlockType::V128
@ V128
WebAssemblyTypeUtilities.h
llvm::wasm::ValType
ValType
Definition: Wasm.h:411
llvm::MVT::INVALID_SIMPLE_VALUE_TYPE
@ INVALID_SIMPLE_VALUE_TYPE
Definition: MachineValueType.h:38
llvm::WebAssembly::typeListToString
std::string typeListToString(ArrayRef< wasm::ValType > List)
Definition: WebAssemblyTypeUtilities.cpp:105
llvm::WebAssembly::BlockType::Funcref
@ Funcref
llvm::MVT::v16i8
@ v16i8
Definition: MachineValueType.h:80
llvm::MVT::v2i64
@ v2i64
Definition: MachineValueType.h:118
llvm::wasm::ValType::EXTERNREF
@ EXTERNREF
llvm::MVT::externref
@ externref
Definition: MachineValueType.h:271
llvm::wasm::ValType::I64
@ I64
llvm::wasm::ValType::FUNCREF
@ FUNCREF
llvm::wasm::WasmSignature::Returns
SmallVector< ValType, 1 > Returns
Definition: Wasm.h:422
llvm::wasm::WASM_TYPE_F64
@ WASM_TYPE_F64
Definition: Wasm.h:256
llvm::MVT::v4f32
@ v4f32
Definition: MachineValueType.h:157
llvm::WebAssembly::BlockType::Externref
@ Externref
llvm::MVT
Machine Value Type.
Definition: MachineValueType.h:31
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:47
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:134
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:103
llvm::wasm::WASM_TYPE_EXTERNREF
@ WASM_TYPE_EXTERNREF
Definition: Wasm.h:259
llvm::wasm::WASM_TYPE_FUNC
@ WASM_TYPE_FUNC
Definition: Wasm.h:260
llvm::MVT::v8i16
@ v8i16
Definition: MachineValueType.h:92
llvm::MVT::i32
@ i32
Definition: MachineValueType.h:46
StringSwitch.h
llvm::WebAssembly::parseBlockType
BlockType parseBlockType(StringRef Type)
Definition: WebAssemblyTypeUtilities.cpp:44
llvm::WebAssembly::typeToString
const char * typeToString(wasm::ValType Type)
Definition: WebAssemblyTypeUtilities.cpp:101
llvm::wasm::ValType::V128
@ V128
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:43
llvm::WebAssembly::anyTypeToString
const char * anyTypeToString(unsigned Type)
Definition: WebAssemblyTypeUtilities.cpp:76
llvm::wasm::WASM_TYPE_NORESULT
@ WASM_TYPE_NORESULT
Definition: Wasm.h:261
llvm::MVT::f32
@ f32
Definition: MachineValueType.h:55
llvm::wasm::WasmSignature::Params
SmallVector< ValType, 4 > Params
Definition: Wasm.h:423
llvm::wasm::WasmSignature
Definition: Wasm.h:421
llvm::wasm::WASM_TYPE_I64
@ WASM_TYPE_I64
Definition: Wasm.h:254
llvm::WebAssembly::BlockType::F32
@ F32