LLVM 17.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
17
18// Get register classes enum.
19#define GET_REGINFO_ENUM
20#include "WebAssemblyGenRegisterInfo.inc"
21
22using namespace llvm;
23
26 .Case("i32", MVT::i32)
27 .Case("i64", MVT::i64)
28 .Case("f32", MVT::f32)
29 .Case("f64", MVT::f64)
30 .Case("i64", MVT::i64)
31 .Case("v16i8", MVT::v16i8)
32 .Case("v8i16", MVT::v8i16)
33 .Case("v4i32", MVT::v4i32)
34 .Case("v2i64", MVT::v2i64)
35 .Case("funcref", MVT::funcref)
36 .Case("externref", MVT::externref)
38}
39
41 switch (Type.SimpleTy) {
42 case MVT::i32:
43 return wasm::ValType::I32;
44 case MVT::i64:
45 return wasm::ValType::I64;
46 case MVT::f32:
47 return wasm::ValType::F32;
48 case MVT::f64:
49 return wasm::ValType::F64;
50 case MVT::v16i8:
51 case MVT::v8i16:
52 case MVT::v4i32:
53 case MVT::v2i64:
54 case MVT::v4f32:
55 case MVT::v2f64:
56 return wasm::ValType::V128;
57 case MVT::funcref:
58 return wasm::ValType::FUNCREF;
59 case MVT::externref:
60 return wasm::ValType::EXTERNREF;
61 default:
62 llvm_unreachable("unexpected type");
63 }
64}
65
67 assert(RC != nullptr);
68 return regClassToValType(RC->getID());
69}
70
72 const SmallVector<MVT, 1> &VTs) {
73 assert(!Sym->getType());
74
75 // Tables are represented as Arrays in LLVM IR therefore
76 // they reach this point as aggregate Array types with an element type
77 // that is a reference type.
78 wasm::ValType ValTy;
79 bool IsTable = false;
80 if (GlobalVT->isArrayTy() &&
82 IsTable = true;
83 const Type *ElTy = GlobalVT->getArrayElementType();
85 ValTy = wasm::ValType::EXTERNREF;
86 else if (WebAssembly::isFuncrefType(ElTy))
87 ValTy = wasm::ValType::FUNCREF;
88 else
89 report_fatal_error("unhandled reference type");
90 } else if (VTs.size() == 1) {
91 ValTy = WebAssembly::toValType(VTs[0]);
92 } else
93 report_fatal_error("Aggregate globals not yet implemented");
94
95 if (IsTable) {
97 Sym->setTableType(ValTy);
98 } else {
100 Sym->setGlobalType(wasm::WasmGlobalType{uint8_t(ValTy), /*Mutable=*/true});
101 }
102}
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
This file contains the declaration of the WebAssembly-specific type parsing utility functions.
void setTableType(wasm::WasmTableType TT)
Definition: MCSymbolWasm.h:142
void setType(wasm::WasmSymbolType type)
Definition: MCSymbolWasm.h:53
void setGlobalType(wasm::WasmGlobalType GT)
Definition: MCSymbolWasm.h:135
std::optional< wasm::WasmSymbolType > getType() const
Definition: MCSymbolWasm.h:51
Machine Value Type.
@ INVALID_SIMPLE_VALUE_TYPE
size_t size() const
Definition: SmallVector.h:91
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:44
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:69
R Default(T Value)
Definition: StringSwitch.h:182
unsigned getID() const
Return the register class ID number.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
bool isArrayTy() const
True if this is an instance of ArrayType.
Definition: Type.h:255
Type * getArrayElementType() const
Definition: Type.h:406
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
wasm::ValType regClassToValType(unsigned RC)
wasm::ValType toValType(MVT Type)
bool isRefType(wasm::ValType Type)
bool isExternrefType(const Type *Ty)
void wasmSymbolSetType(MCSymbolWasm *Sym, const Type *GlobalVT, const SmallVector< MVT, 1 > &VTs)
Sets a Wasm Symbol Type.
bool isFuncrefType(const Type *Ty)
MVT parseMVT(StringRef Type)
@ WASM_SYMBOL_TYPE_GLOBAL
Definition: Wasm.h:385
@ WASM_SYMBOL_TYPE_TABLE
Definition: Wasm.h:388
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:145