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 // Multivalue blocks (and other non-void blocks) are only emitted when the 36 // blocks will never be exited and are at the ends of functions (see 37 // WebAssemblyCFGStackify::fixEndsAtEndOfFunction). They also are never made 38 // to pop values off the stack, so the exact multivalue signature can always 39 // be inferred from the return type of the parent function in MCInstLower. 40 Multivalue = 0xffff, 41 }; 42 43 inline bool isRefType(wasm::ValType Type) { 44 return Type == wasm::ValType::EXTERNREF || Type == wasm::ValType::FUNCREF; 45 } 46 47 // Convert ValType or a list/signature of ValTypes to a string. 48 49 // Convert an unsinged integer, which can be among wasm::ValType enum, to its 50 // type name string. If the input is not within wasm::ValType, returns 51 // "invalid_type". 52 const char *anyTypeToString(unsigned Type); 53 const char *typeToString(wasm::ValType Type); 54 // Convert a list of ValTypes into a string in the format of 55 // "type0, type1, ... typeN" 56 std::string typeListToString(ArrayRef<wasm::ValType> List); 57 // Convert a wasm signature into a string in the format of 58 // "(params) -> (results)", where params and results are a string of ValType 59 // lists. 60 std::string signatureToString(const wasm::WasmSignature *Sig); 61 62 // Convert a register class ID to a wasm ValType. 63 wasm::ValType regClassToValType(unsigned RC); 64 65 // Convert StringRef to ValType / HealType / BlockType 66 67 std::optional<wasm::ValType> parseType(StringRef Type); 68 BlockType parseBlockType(StringRef Type); 69 70 } // end namespace WebAssembly 71 } // end namespace llvm 72 73 #endif 74