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
14 #include "WebAssemblyMCTypeUtilities.h"
15 #include "WebAssemblyMCTargetDesc.h"
16 #include "llvm/ADT/StringSwitch.h"
17
18 using namespace llvm;
19
parseType(StringRef Type)20 std::optional<wasm::ValType> WebAssembly::parseType(StringRef Type) {
21 return llvm::StringSwitch<std::optional<wasm::ValType>>{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
parseBlockType(StringRef Type)34 WebAssembly::BlockType WebAssembly::parseBlockType(StringRef Type) {
35 // Multivalue block types are handled separately in parseSignature
36 return StringSwitch<WebAssembly::BlockType>(Type)
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.
anyTypeToString(unsigned Type)51 const char *WebAssembly::anyTypeToString(unsigned Type) {
52 switch (Type) {
53 case wasm::WASM_TYPE_I32:
54 return "i32";
55 case wasm::WASM_TYPE_I64:
56 return "i64";
57 case wasm::WASM_TYPE_F32:
58 return "f32";
59 case wasm::WASM_TYPE_F64:
60 return "f64";
61 case wasm::WASM_TYPE_V128:
62 return "v128";
63 case wasm::WASM_TYPE_FUNCREF:
64 return "funcref";
65 case wasm::WASM_TYPE_EXTERNREF:
66 return "externref";
67 case wasm::WASM_TYPE_EXNREF:
68 return "exnref";
69 case wasm::WASM_TYPE_FUNC:
70 return "func";
71 case wasm::WASM_TYPE_NORESULT:
72 return "void";
73 default:
74 return "invalid_type";
75 }
76 }
77
typeToString(wasm::ValType Type)78 const char *WebAssembly::typeToString(wasm::ValType Type) {
79 return anyTypeToString(static_cast<unsigned>(Type));
80 }
81
typeListToString(ArrayRef<wasm::ValType> List)82 std::string WebAssembly::typeListToString(ArrayRef<wasm::ValType> List) {
83 std::string S;
84 for (const auto &Type : List) {
85 if (&Type != &List[0])
86 S += ", ";
87 S += WebAssembly::typeToString(Type);
88 }
89 return S;
90 }
91
signatureToString(const wasm::WasmSignature * Sig)92 std::string WebAssembly::signatureToString(const wasm::WasmSignature *Sig) {
93 std::string S("(");
94 S += typeListToString(Sig->Params);
95 S += ") -> (";
96 S += typeListToString(Sig->Returns);
97 S += ")";
98 return S;
99 }
100
regClassToValType(unsigned RC)101 wasm::ValType WebAssembly::regClassToValType(unsigned RC) {
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 }
123