LLVM 19.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)
37}
38
40 switch (Type.SimpleTy) {
41 case MVT::i32:
42 return wasm::ValType::I32;
43 case MVT::i64:
44 return wasm::ValType::I64;
45 case MVT::f32:
46 return wasm::ValType::F32;
47 case MVT::f64:
48 return wasm::ValType::F64;
49 case MVT::v16i8:
50 case MVT::v8i16:
51 case MVT::v4i32:
52 case MVT::v2i64:
53 case MVT::v4f32:
54 case MVT::v2f64:
55 return wasm::ValType::V128;
56 case MVT::funcref:
57 return wasm::ValType::FUNCREF;
58 case MVT::externref:
59 return wasm::ValType::EXTERNREF;
60 default:
61 llvm_unreachable("unexpected type");
62 }
63}
64
66 ArrayRef<MVT> VTs) {
67 assert(!Sym->getType());
68
69 // Tables are represented as Arrays in LLVM IR therefore
70 // they reach this point as aggregate Array types with an element type
71 // that is a reference type.
72 wasm::ValType ValTy;
73 bool IsTable = false;
75 IsTable = true;
76 const Type *ElTy = GlobalVT->getArrayElementType();
78 ValTy = wasm::ValType::EXTERNREF;
80 ValTy = wasm::ValType::FUNCREF;
81 else
82 report_fatal_error("unhandled reference type");
83 } else if (VTs.size() == 1) {
84 ValTy = WebAssembly::toValType(VTs[0]);
85 } else
86 report_fatal_error("Aggregate globals not yet implemented");
87
88 if (IsTable) {
90 Sym->setTableType(ValTy);
91 } else {
93 Sym->setGlobalType(wasm::WasmGlobalType{uint8_t(ValTy), /*Mutable=*/true});
94 }
95}
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:404
#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:210
@ WASM_SYMBOL_TYPE_TABLE
Definition: Wasm.h:213
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:156