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 
49 }
50 
52  // Multivalue block types are handled separately in parseSignature
63 }
64 
66  return StringSwitch<MVT>(Type)
67  .Case("i32", MVT::i32)
68  .Case("i64", MVT::i64)
69  .Case("f32", MVT::f32)
70  .Case("f64", MVT::f64)
71  .Case("i64", MVT::i64)
72  .Case("v16i8", MVT::v16i8)
73  .Case("v8i16", MVT::v8i16)
74  .Case("v4i32", MVT::v4i32)
75  .Case("v2i64", MVT::v2i64)
76  .Case("funcref", MVT::funcref)
77  .Case("externref", MVT::externref)
79 }
80 
81 // We have various enums representing a subset of these types, use this
82 // function to convert any of them to text.
83 const char *WebAssembly::anyTypeToString(unsigned Type) {
84  switch (Type) {
86  return "i32";
88  return "i64";
90  return "f32";
92  return "f64";
94  return "v128";
96  return "funcref";
98  return "externref";
100  return "func";
102  return "void";
103  default:
104  return "invalid_type";
105  }
106 }
107 
109  return anyTypeToString(static_cast<unsigned>(Type));
110 }
111 
113  std::string S;
114  for (const auto &Type : List) {
115  if (&Type != &List[0])
116  S += ", ";
118  }
119  return S;
120 }
121 
123  std::string S("(");
124  S += typeListToString(Sig->Params);
125  S += ") -> (";
126  S += typeListToString(Sig->Returns);
127  S += ")";
128  return S;
129 }
130 
132  switch (Type.SimpleTy) {
133  case MVT::i32:
134  return wasm::ValType::I32;
135  case MVT::i64:
136  return wasm::ValType::I64;
137  case MVT::f32:
138  return wasm::ValType::F32;
139  case MVT::f64:
140  return wasm::ValType::F64;
141  case MVT::v16i8:
142  case MVT::v8i16:
143  case MVT::v4i32:
144  case MVT::v2i64:
145  case MVT::v4f32:
146  case MVT::v2f64:
147  return wasm::ValType::V128;
148  case MVT::funcref:
149  return wasm::ValType::FUNCREF;
150  case MVT::externref:
152  default:
153  llvm_unreachable("unexpected type");
154  }
155 }
156 
158  switch (RC) {
159  case WebAssembly::I32RegClassID:
160  return wasm::ValType::I32;
161  case WebAssembly::I64RegClassID:
162  return wasm::ValType::I64;
163  case WebAssembly::F32RegClassID:
164  return wasm::ValType::F32;
165  case WebAssembly::F64RegClassID:
166  return wasm::ValType::F64;
167  case WebAssembly::V128RegClassID:
168  return wasm::ValType::V128;
169  case WebAssembly::FUNCREFRegClassID:
170  return wasm::ValType::FUNCREF;
171  case WebAssembly::EXTERNREFRegClassID:
173  default:
174  llvm_unreachable("unexpected type");
175  }
176 }
llvm::StringSwitch::Case
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:67
llvm::wasm::WASM_TYPE_I64
@ WASM_TYPE_I64
Definition: Wasm.h:253
llvm::WebAssembly::parseMVT
MVT parseMVT(StringRef Type)
Definition: WebAssemblyTypeUtilities.cpp:65
llvm::WebAssembly::BlockType::I64
@ I64
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::wasm::ValType::I32
@ I32
llvm::wasm::WASM_TYPE_FUNC
@ WASM_TYPE_FUNC
Definition: Wasm.h:259
llvm::wasm::WASM_TYPE_V128
@ WASM_TYPE_V128
Definition: Wasm.h:256
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::wasm::WASM_TYPE_FUNCREF
@ WASM_TYPE_FUNCREF
Definition: Wasm.h:257
llvm::WebAssembly::HeapType::Funcref
@ Funcref
llvm::WebAssembly::signatureToString
std::string signatureToString(const wasm::WasmSignature *Sig)
Definition: WebAssemblyTypeUtilities.cpp:122
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_EXTERNREF
@ WASM_TYPE_EXTERNREF
Definition: Wasm.h:258
llvm::WebAssembly::regClassToValType
wasm::ValType regClassToValType(unsigned RC)
Definition: WebAssemblyTypeUtilities.cpp:157
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::ValType::F64
@ F64
llvm::MVT::f64
@ f64
Definition: MachineValueType.h:56
llvm::wasm::ValType::F32
@ F32
llvm::WebAssembly::toValType
wasm::ValType toValType(MVT Type)
Definition: WebAssemblyTypeUtilities.cpp:131
llvm::wasm::WASM_TYPE_F64
@ WASM_TYPE_F64
Definition: Wasm.h:255
llvm::WebAssembly::BlockType::V128
@ V128
WebAssemblyTypeUtilities.h
llvm::wasm::ValType
ValType
Definition: Wasm.h:408
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:112
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:419
llvm::MVT::v4f32
@ v4f32
Definition: MachineValueType.h:157
llvm::wasm::WASM_TYPE_F32
@ WASM_TYPE_F32
Definition: Wasm.h:254
llvm::wasm::WASM_TYPE_I32
@ WASM_TYPE_I32
Definition: Wasm.h:252
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: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:103
llvm::wasm::WASM_TYPE_NORESULT
@ WASM_TYPE_NORESULT
Definition: Wasm.h:260
llvm::WebAssembly::HeapType::Externref
@ Externref
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:51
llvm::WebAssembly::typeToString
const char * typeToString(wasm::ValType Type)
Definition: WebAssemblyTypeUtilities.cpp:108
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:83
llvm::MVT::f32
@ f32
Definition: MachineValueType.h:55
llvm::wasm::WasmSignature::Params
SmallVector< ValType, 4 > Params
Definition: Wasm.h:420
llvm::wasm::WasmSignature
Definition: Wasm.h:418
llvm::WebAssembly::BlockType::F32
@ F32
llvm::WebAssembly::parseHeapType
HeapType parseHeapType(StringRef Type)
Definition: WebAssemblyTypeUtilities.cpp:44