LLVM  16.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"
17 
18 // Get register classes enum.
19 #define GET_REGINFO_ENUM
20 #include "WebAssemblyGenRegisterInfo.inc"
21 
22 using namespace llvm;
23 
25  // FIXME: can't use StringSwitch because wasm::ValType doesn't have a
26  // "invalid" value.
27  if (Type == "i32")
28  return wasm::ValType::I32;
29  if (Type == "i64")
30  return wasm::ValType::I64;
31  if (Type == "f32")
32  return wasm::ValType::F32;
33  if (Type == "f64")
34  return wasm::ValType::F64;
35  if (Type == "v128" || Type == "i8x16" || Type == "i16x8" || Type == "i32x4" ||
36  Type == "i64x2" || Type == "f32x4" || Type == "f64x2")
37  return wasm::ValType::V128;
38  if (Type == "funcref")
40  if (Type == "externref")
42  return None;
43 }
44 
46  // Multivalue block types are handled separately in parseSignature
57 }
58 
60  return StringSwitch<MVT>(Type)
61  .Case("i32", MVT::i32)
62  .Case("i64", MVT::i64)
63  .Case("f32", MVT::f32)
64  .Case("f64", MVT::f64)
65  .Case("i64", MVT::i64)
66  .Case("v16i8", MVT::v16i8)
67  .Case("v8i16", MVT::v8i16)
68  .Case("v4i32", MVT::v4i32)
69  .Case("v2i64", MVT::v2i64)
70  .Case("funcref", MVT::funcref)
71  .Case("externref", MVT::externref)
73 }
74 
75 // We have various enums representing a subset of these types, use this
76 // function to convert any of them to text.
77 const char *WebAssembly::anyTypeToString(unsigned Type) {
78  switch (Type) {
80  return "i32";
82  return "i64";
84  return "f32";
86  return "f64";
88  return "v128";
90  return "funcref";
92  return "externref";
94  return "func";
96  return "void";
97  default:
98  return "invalid_type";
99  }
100 }
101 
103  return anyTypeToString(static_cast<unsigned>(Type));
104 }
105 
107  std::string S;
108  for (const auto &Type : List) {
109  if (&Type != &List[0])
110  S += ", ";
112  }
113  return S;
114 }
115 
117  std::string S("(");
118  S += typeListToString(Sig->Params);
119  S += ") -> (";
120  S += typeListToString(Sig->Returns);
121  S += ")";
122  return S;
123 }
124 
126  switch (Type.SimpleTy) {
127  case MVT::i32:
128  return wasm::ValType::I32;
129  case MVT::i64:
130  return wasm::ValType::I64;
131  case MVT::f32:
132  return wasm::ValType::F32;
133  case MVT::f64:
134  return wasm::ValType::F64;
135  case MVT::v16i8:
136  case MVT::v8i16:
137  case MVT::v4i32:
138  case MVT::v2i64:
139  case MVT::v4f32:
140  case MVT::v2f64:
141  return wasm::ValType::V128;
142  case MVT::funcref:
143  return wasm::ValType::FUNCREF;
144  case MVT::externref:
146  default:
147  llvm_unreachable("unexpected type");
148  }
149 }
150 
152  switch (RC) {
153  case WebAssembly::I32RegClassID:
154  return wasm::ValType::I32;
155  case WebAssembly::I64RegClassID:
156  return wasm::ValType::I64;
157  case WebAssembly::F32RegClassID:
158  return wasm::ValType::F32;
159  case WebAssembly::F64RegClassID:
160  return wasm::ValType::F64;
161  case WebAssembly::V128RegClassID:
162  return wasm::ValType::V128;
163  case WebAssembly::FUNCREFRegClassID:
164  return wasm::ValType::FUNCREF;
165  case WebAssembly::EXTERNREFRegClassID:
167  default:
168  llvm_unreachable("unexpected type");
169  }
170 }
171 
173  assert(RC != nullptr);
174  return regClassToValType(RC->getID());
175 }
176 
178  const SmallVector<MVT, 1> &VTs) {
179  assert(!Sym->getType());
180 
181  // Tables are represented as Arrays in LLVM IR therefore
182  // they reach this point as aggregate Array types with an element type
183  // that is a reference type.
184  wasm::ValType ValTy;
185  bool IsTable = false;
186  if (GlobalVT->isArrayTy() &&
188  IsTable = true;
189  const Type *ElTy = GlobalVT->getArrayElementType();
191  ValTy = wasm::ValType::EXTERNREF;
192  else if (WebAssembly::isFuncrefType(ElTy))
193  ValTy = wasm::ValType::FUNCREF;
194  else
195  report_fatal_error("unhandled reference type");
196  } else if (VTs.size() == 1) {
197  ValTy = WebAssembly::toValType(VTs[0]);
198  } else
199  report_fatal_error("Aggregate globals not yet implemented");
200 
201  if (IsTable) {
203  Sym->setTableType(ValTy);
204  } else {
206  Sym->setGlobalType(wasm::WasmGlobalType{uint8_t(ValTy), /*Mutable=*/true});
207  }
208 }
llvm::StringSwitch::Case
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:70
llvm::wasm::WASM_TYPE_EXTERNREF
@ WASM_TYPE_EXTERNREF
Definition: Wasm.h:267
llvm::WebAssembly::parseMVT
MVT parseMVT(StringRef Type)
Definition: WebAssemblyTypeUtilities.cpp:59
llvm::WebAssembly::BlockType::I64
@ I64
llvm::TargetRegisterClass::getID
unsigned getID() const
Return the register class ID number.
Definition: TargetRegisterInfo.h:75
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::wasm::ValType::I32
@ I32
llvm::MCSymbolWasm::getType
Optional< wasm::WasmSymbolType > getType() const
Definition: MCSymbolWasm.h:51
llvm::WebAssembly::isExternrefType
bool isExternrefType(const Type *Ty)
Definition: WebAssemblyTypeUtilities.h:77
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1199
llvm::WebAssembly::isFuncrefType
bool isFuncrefType(const Type *Ty)
Definition: WebAssemblyTypeUtilities.h:72
llvm::wasm::WASM_SYMBOL_TYPE_TABLE
@ WASM_SYMBOL_TYPE_TABLE
Definition: Wasm.h:388
llvm::wasm::WASM_SYMBOL_TYPE_GLOBAL
@ WASM_SYMBOL_TYPE_GLOBAL
Definition: Wasm.h:385
llvm::WebAssembly::signatureToString
std::string signatureToString(const wasm::WasmSignature *Sig)
Definition: WebAssemblyTypeUtilities.cpp:116
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::WebAssembly::wasmSymbolSetType
void wasmSymbolSetType(MCSymbolWasm *Sym, const Type *GlobalVT, const SmallVector< MVT, 1 > &VTs)
Sets a Wasm Symbol Type.
Definition: WebAssemblyTypeUtilities.cpp:177
llvm::MVT::v2f64
@ v2f64
Definition: MachineValueType.h:181
llvm::WebAssembly::BlockType::F64
@ F64
llvm::WebAssembly::regClassToValType
wasm::ValType regClassToValType(unsigned RC)
Definition: WebAssemblyTypeUtilities.cpp:151
llvm::WebAssembly::BlockType::Void
@ Void
llvm::MVT::funcref
@ funcref
Definition: MachineValueType.h:281
llvm::Type::isArrayTy
bool isArrayTy() const
True if this is an instance of ArrayType.
Definition: Type.h:234
llvm::WebAssembly::parseType
Optional< wasm::ValType > parseType(StringRef Type)
Definition: WebAssemblyTypeUtilities.cpp:24
llvm::wasm::ValType::F64
@ F64
llvm::MVT::f64
@ f64
Definition: MachineValueType.h:58
llvm::TargetRegisterClass
Definition: TargetRegisterInfo.h:46
llvm::wasm::ValType::F32
@ F32
llvm::WebAssembly::toValType
wasm::ValType toValType(MVT Type)
Definition: WebAssemblyTypeUtilities.cpp:125
llvm::WebAssembly::BlockType::V128
@ V128
llvm::wasm::WasmGlobalType
Definition: Wasm.h:112
llvm::MCSymbolWasm::setType
void setType(wasm::WasmSymbolType type)
Definition: MCSymbolWasm.h:53
WebAssemblyTypeUtilities.h
llvm::report_fatal_error
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:145
llvm::wasm::ValType
ValType
Definition: Wasm.h:424
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:106
llvm::wasm::WASM_TYPE_FUNC
@ WASM_TYPE_FUNC
Definition: Wasm.h:268
llvm::WebAssembly::BlockType::Funcref
@ Funcref
llvm::Type::getArrayElementType
Type * getArrayElementType() const
Definition: Type.h:384
llvm::MVT::v16i8
@ v16i8
Definition: MachineValueType.h:89
llvm::MCSymbolWasm::setTableType
void setTableType(wasm::WasmTableType TT)
Definition: MCSymbolWasm.h:142
llvm::MVT::v2i64
@ v2i64
Definition: MachineValueType.h:127
llvm::wasm::ValType::EXTERNREF
@ EXTERNREF
llvm::MVT::externref
@ externref
Definition: MachineValueType.h:282
llvm::wasm::ValType::I64
@ I64
llvm::MCSymbolWasm
Definition: MCSymbolWasm.h:16
llvm::wasm::ValType::FUNCREF
@ FUNCREF
llvm::wasm::WasmSignature::Returns
SmallVector< ValType, 1 > Returns
Definition: Wasm.h:435
llvm::MVT::v4f32
@ v4f32
Definition: MachineValueType.h:166
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::wasm::WASM_TYPE_F64
@ WASM_TYPE_F64
Definition: Wasm.h:264
llvm::WebAssembly::BlockType::Externref
@ Externref
llvm::MVT
Machine Value Type.
Definition: MachineValueType.h:31
llvm::WebAssembly::isRefType
bool isRefType(const Type *Ty)
Definition: WebAssemblyTypeUtilities.h:82
llvm::wasm::WASM_TYPE_F32
@ WASM_TYPE_F32
Definition: Wasm.h:263
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:49
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:143
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:112
llvm::wasm::WASM_TYPE_I64
@ WASM_TYPE_I64
Definition: Wasm.h:262
llvm::None
constexpr std::nullopt_t None
Definition: None.h:27
llvm::MVT::v8i16
@ v8i16
Definition: MachineValueType.h:101
llvm::wasm::WASM_TYPE_I32
@ WASM_TYPE_I32
Definition: Wasm.h:261
llvm::wasm::WASM_TYPE_FUNCREF
@ WASM_TYPE_FUNCREF
Definition: Wasm.h:266
llvm::MVT::i32
@ i32
Definition: MachineValueType.h:48
llvm::wasm::WASM_TYPE_NORESULT
@ WASM_TYPE_NORESULT
Definition: Wasm.h:269
StringSwitch.h
llvm::WebAssembly::parseBlockType
BlockType parseBlockType(StringRef Type)
Definition: WebAssemblyTypeUtilities.cpp:45
llvm::wasm::WASM_TYPE_V128
@ WASM_TYPE_V128
Definition: Wasm.h:265
llvm::WebAssembly::typeToString
const char * typeToString(wasm::ValType Type)
Definition: WebAssemblyTypeUtilities.cpp:102
llvm::wasm::ValType::V128
@ V128
llvm::MCSymbolWasm::setGlobalType
void setGlobalType(wasm::WasmGlobalType GT)
Definition: MCSymbolWasm.h:135
List
const NodeList & List
Definition: RDFGraph.cpp:199
llvm::WebAssembly::BlockType
BlockType
Used as immediate MachineOperands for block signatures.
Definition: WebAssemblyTypeUtilities.h:31
llvm::StringSwitch::Default
R Default(T Value)
Definition: StringSwitch.h:183
llvm::StringSwitch
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:45
llvm::WebAssembly::anyTypeToString
const char * anyTypeToString(unsigned Type)
Definition: WebAssemblyTypeUtilities.cpp:77
llvm::MVT::f32
@ f32
Definition: MachineValueType.h:57
llvm::wasm::WasmSignature::Params
SmallVector< ValType, 4 > Params
Definition: Wasm.h:436
TargetRegisterInfo.h
llvm::wasm::WasmSignature
Definition: Wasm.h:434
llvm::WebAssembly::BlockType::F32
@ F32