xref: /llvm-project/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCTypeUtilities.h (revision 6bbf7f06d8e4e84bbda9027252b26a0d9ae10cde)
1 //===-- WebAssemblyMCTypeUtilities - WebAssembly Type Utilities-*- C++ -*-====//
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 contains the declaration of the WebAssembly-specific type parsing
11 /// utility functions.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_LIB_TARGET_WEBASSEMBLY_MCTARGETDESC_WEBASSEMBLYMCTYPEUTILITIES_H
16 #define LLVM_LIB_TARGET_WEBASSEMBLY_MCTARGETDESC_WEBASSEMBLYMCTYPEUTILITIES_H
17 
18 #include "llvm/BinaryFormat/Wasm.h"
19 
20 namespace llvm {
21 
22 namespace WebAssembly {
23 
24 /// Used as immediate MachineOperands for block signatures
25 enum class BlockType : unsigned {
26   Invalid = 0x00,
27   Void = 0x40,
28   I32 = unsigned(wasm::ValType::I32),
29   I64 = unsigned(wasm::ValType::I64),
30   F32 = unsigned(wasm::ValType::F32),
31   F64 = unsigned(wasm::ValType::F64),
32   V128 = unsigned(wasm::ValType::V128),
33   Externref = unsigned(wasm::ValType::EXTERNREF),
34   Funcref = unsigned(wasm::ValType::FUNCREF),
35   Exnref = unsigned(wasm::ValType::EXNREF),
36   // Multivalue blocks are emitted in two cases:
37   // 1. When the blocks will never be exited and are at the ends of functions
38   //    (see WebAssemblyCFGStackify::fixEndsAtEndOfFunction). In this case the
39   //    exact multivalue signature can always be inferred from the return type
40   //    of the parent function.
41   // 2. (catch_ref ...) clause in try_table instruction. Currently all tags we
42   //    support (cpp_exception and c_longjmp) throws a single i32, so the
43   //    multivalue signature for this case will be (i32, exnref).
44   // The real multivalue siganture will be added in MCInstLower.
45   Multivalue = 0xffff,
46 };
47 
48 inline bool isRefType(wasm::ValType Type) {
49   return Type == wasm::ValType::EXTERNREF || Type == wasm::ValType::FUNCREF ||
50          Type == wasm::ValType::EXNREF;
51 }
52 
53 // Convert ValType or a list/signature of ValTypes to a string.
54 
55 // Convert an unsigned integer, which can be among wasm::ValType enum, to its
56 // type name string. If the input is not within wasm::ValType, returns
57 // "invalid_type".
58 const char *anyTypeToString(unsigned Type);
59 const char *typeToString(wasm::ValType Type);
60 // Convert a list of ValTypes into a string in the format of
61 // "type0, type1, ... typeN"
62 std::string typeListToString(ArrayRef<wasm::ValType> List);
63 // Convert a wasm signature into a string in the format of
64 // "(params) -> (results)", where params and results are a string of ValType
65 // lists.
66 std::string signatureToString(const wasm::WasmSignature *Sig);
67 
68 // Convert a register class ID to a wasm ValType.
69 wasm::ValType regClassToValType(unsigned RC);
70 
71 // Convert StringRef to ValType / HealType / BlockType
72 
73 std::optional<wasm::ValType> parseType(StringRef Type);
74 BlockType parseBlockType(StringRef Type);
75 
76 } // end namespace WebAssembly
77 } // end namespace llvm
78 
79 #endif
80