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 .Default(std::nullopt);
31}
32
34 // Multivalue block types are handled separately in parseSignature
36 .Case("i32", WebAssembly::BlockType::I32)
37 .Case("i64", WebAssembly::BlockType::I64)
38 .Case("f32", WebAssembly::BlockType::F32)
39 .Case("f64", WebAssembly::BlockType::F64)
40 .Case("v128", WebAssembly::BlockType::V128)
41 .Case("funcref", WebAssembly::BlockType::Funcref)
42 .Case("externref", WebAssembly::BlockType::Externref)
43 .Case("void", WebAssembly::BlockType::Void)
44 .Default(WebAssembly::BlockType::Invalid);
45}
46
47// We have various enums representing a subset of these types, use this
48// function to convert any of them to text.
49const char *WebAssembly::anyTypeToString(unsigned Type) {
50 switch (Type) {
52 return "i32";
54 return "i64";
56 return "f32";
58 return "f64";
60 return "v128";
62 return "funcref";
64 return "externref";
66 return "func";
68 return "void";
69 default:
70 return "invalid_type";
71 }
72}
73
75 return anyTypeToString(static_cast<unsigned>(Type));
76}
77
79 std::string S;
80 for (const auto &Type : List) {
81 if (&Type != &List[0])
82 S += ", ";
84 }
85 return S;
86}
87
89 std::string S("(");
90 S += typeListToString(Sig->Params);
91 S += ") -> (";
92 S += typeListToString(Sig->Returns);
93 S += ")";
94 return S;
95}
96
98 switch (RC) {
99 case WebAssembly::I32RegClassID:
100 return wasm::ValType::I32;
101 case WebAssembly::I64RegClassID:
102 return wasm::ValType::I64;
103 case WebAssembly::F32RegClassID:
104 return wasm::ValType::F32;
105 case WebAssembly::F64RegClassID:
106 return wasm::ValType::F64;
107 case WebAssembly::V128RegClassID:
108 return wasm::ValType::V128;
109 case WebAssembly::FUNCREFRegClassID:
110 return wasm::ValType::FUNCREF;
111 case WebAssembly::EXTERNREFRegClassID:
112 return wasm::ValType::EXTERNREF;
113 default:
114 llvm_unreachable("unexpected type");
115 }
116}
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:62
@ WASM_TYPE_EXTERNREF
Definition: Wasm.h:63
@ WASM_TYPE_FUNC
Definition: Wasm.h:72
@ WASM_TYPE_I32
Definition: Wasm.h:54
@ WASM_TYPE_NORESULT
Definition: Wasm.h:78
@ WASM_TYPE_F32
Definition: Wasm.h:56
@ WASM_TYPE_V128
Definition: Wasm.h:58
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
SmallVector< ValType, 1 > Returns
Definition: Wasm.h:485
SmallVector< ValType, 4 > Params
Definition: Wasm.h:486