LLVM 19.0.0git
WebAssemblyMCTypeUtilities.cpp
Go to the documentation of this file.
1//===- WebAssemblyMCTypeUtilities.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
18using namespace llvm;
19
20std::optional<wasm::ValType> WebAssembly::parseType(StringRef Type) {
22 .Case("i32", wasm::ValType::I32)
23 .Case("i64", wasm::ValType::I64)
24 .Case("f32", wasm::ValType::F32)
25 .Case("f64", wasm::ValType::F64)
26 .Cases("v128", "i8x16", "i16x8", "i32x4", "i64x2", "f32x4", "f64x2",
27 wasm::ValType::V128)
28 .Case("funcref", wasm::ValType::FUNCREF)
29 .Case("externref", wasm::ValType::EXTERNREF)
30 .Case("exnref", wasm::ValType::EXNREF)
31 .Default(std::nullopt);
32}
33
35 // Multivalue block types are handled separately in parseSignature
37 .Case("i32", WebAssembly::BlockType::I32)
38 .Case("i64", WebAssembly::BlockType::I64)
39 .Case("f32", WebAssembly::BlockType::F32)
40 .Case("f64", WebAssembly::BlockType::F64)
41 .Case("v128", WebAssembly::BlockType::V128)
42 .Case("funcref", WebAssembly::BlockType::Funcref)
43 .Case("externref", WebAssembly::BlockType::Externref)
44 .Case("exnref", WebAssembly::BlockType::Exnref)
45 .Case("void", WebAssembly::BlockType::Void)
46 .Default(WebAssembly::BlockType::Invalid);
47}
48
49// We have various enums representing a subset of these types, use this
50// function to convert any of them to text.
51const char *WebAssembly::anyTypeToString(unsigned Type) {
52 switch (Type) {
54 return "i32";
56 return "i64";
58 return "f32";
60 return "f64";
62 return "v128";
64 return "funcref";
66 return "externref";
68 return "exnref";
70 return "func";
72 return "void";
73 default:
74 return "invalid_type";
75 }
76}
77
79 return anyTypeToString(static_cast<unsigned>(Type));
80}
81
83 std::string S;
84 for (const auto &Type : List) {
85 if (&Type != &List[0])
86 S += ", ";
88 }
89 return S;
90}
91
93 std::string S("(");
94 S += typeListToString(Sig->Params);
95 S += ") -> (";
96 S += typeListToString(Sig->Returns);
97 S += ")";
98 return S;
99}
100
102 switch (RC) {
103 case WebAssembly::I32RegClassID:
104 return wasm::ValType::I32;
105 case WebAssembly::I64RegClassID:
106 return wasm::ValType::I64;
107 case WebAssembly::F32RegClassID:
108 return wasm::ValType::F32;
109 case WebAssembly::F64RegClassID:
110 return wasm::ValType::F64;
111 case WebAssembly::V128RegClassID:
112 return wasm::ValType::V128;
113 case WebAssembly::FUNCREFRegClassID:
114 return wasm::ValType::FUNCREF;
115 case WebAssembly::EXTERNREFRegClassID:
116 return wasm::ValType::EXTERNREF;
117 case WebAssembly::EXNREFRegClassID:
118 return wasm::ValType::EXNREF;
119 default:
120 llvm_unreachable("unexpected type");
121 }
122}
const NodeList & List
Definition: RDFGraph.cpp:201
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
This file provides WebAssembly-specific target descriptions.
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
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
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
const char * typeToString(wasm::ValType Type)
BlockType parseBlockType(StringRef Type)
wasm::ValType regClassToValType(unsigned RC)
BlockType
Used as immediate MachineOperands for block signatures.
std::string signatureToString(const wasm::WasmSignature *Sig)
const char * anyTypeToString(unsigned Type)
std::string typeListToString(ArrayRef< wasm::ValType > List)
std::optional< wasm::ValType > parseType(StringRef Type)
@ WASM_TYPE_I64
Definition: Wasm.h:55
@ WASM_TYPE_F64
Definition: Wasm.h:57
@ WASM_TYPE_FUNCREF
Definition: Wasm.h:63
@ WASM_TYPE_EXTERNREF
Definition: Wasm.h:64
@ WASM_TYPE_FUNC
Definition: Wasm.h:73
@ WASM_TYPE_I32
Definition: Wasm.h:54
@ WASM_TYPE_NORESULT
Definition: Wasm.h:79
@ WASM_TYPE_F32
Definition: Wasm.h:56
@ WASM_TYPE_V128
Definition: Wasm.h:58
@ WASM_TYPE_EXNREF
Definition: Wasm.h:65
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
SmallVector< ValType, 1 > Returns
Definition: Wasm.h:488
SmallVector< ValType, 4 > Params
Definition: Wasm.h:489