LLVM 20.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
16
17// Get register classes enum.
18#define GET_REGINFO_ENUM
19#include "WebAssemblyGenRegisterInfo.inc"
20
21using namespace llvm;
22
25 .Case("i32", MVT::i32)
26 .Case("i64", MVT::i64)
27 .Case("f32", MVT::f32)
28 .Case("f64", MVT::f64)
29 .Case("i64", MVT::i64)
30 .Case("v16i8", MVT::v16i8)
31 .Case("v8i16", MVT::v8i16)
32 .Case("v4i32", MVT::v4i32)
33 .Case("v2i64", MVT::v2i64)
34 .Case("funcref", MVT::funcref)
35 .Case("externref", MVT::externref)
36 .Case("exnref", MVT::exnref)
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::v8f16:
55 case MVT::v4f32:
56 case MVT::v2f64:
57 return wasm::ValType::V128;
58 case MVT::funcref:
59 return wasm::ValType::FUNCREF;
60 case MVT::externref:
61 return wasm::ValType::EXTERNREF;
62 case MVT::exnref:
63 return wasm::ValType::EXNREF;
64 default:
65 llvm_unreachable("unexpected type");
66 }
67}
68
70 ArrayRef<MVT> VTs) {
71 assert(!Sym->getType());
72
73 // Tables are represented as Arrays in LLVM IR therefore
74 // they reach this point as aggregate Array types with an element type
75 // that is a reference type.
76 wasm::ValType ValTy;
77 bool IsTable = false;
79 IsTable = true;
80 const Type *ElTy = GlobalVT->getArrayElementType();
82 ValTy = wasm::ValType::EXTERNREF;
84 ValTy = wasm::ValType::FUNCREF;
85 else
86 report_fatal_error("unhandled reference type");
87 } else if (VTs.size() == 1) {
88 ValTy = WebAssembly::toValType(VTs[0]);
89 } else
90 report_fatal_error("Aggregate globals not yet implemented");
91
92 if (IsTable) {
94 Sym->setTableType(ValTy);
95 } else {
97 Sym->setGlobalType(wasm::WasmGlobalType{uint8_t(ValTy), /*Mutable=*/true});
98 }
99}
Symbol * Sym
Definition: ELF_riscv.cpp:479
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.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:165
Machine Value Type.
@ INVALID_SIMPLE_VALUE_TYPE
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
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
Type * getArrayElementType() const
Definition: Type.h:399
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
bool isWebAssemblyExternrefType(const Type *Ty)
Return true if this is a WebAssembly Externref Type.
bool isWebAssemblyFuncrefType(const Type *Ty)
Return true if this is a WebAssembly Funcref Type.
wasm::ValType toValType(MVT Type)
bool isWebAssemblyTableType(const Type *Ty)
Return true if the table represents a WebAssembly table type.
void wasmSymbolSetType(MCSymbolWasm *Sym, const Type *GlobalVT, ArrayRef< MVT > VTs)
Sets a Wasm Symbol Type.
MVT parseMVT(StringRef Type)
@ WASM_SYMBOL_TYPE_GLOBAL
Definition: Wasm.h:211
@ WASM_SYMBOL_TYPE_TABLE
Definition: Wasm.h:214
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:167