1 //===-- WebAssemblyTypeUtilities - 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_UTILS_WEBASSEMBLYTYPEUTILITIES_H 16 #define LLVM_LIB_TARGET_WEBASSEMBLY_UTILS_WEBASSEMBLYTYPEUTILITIES_H 17 18 #include "llvm/ADT/Optional.h" 19 #include "llvm/BinaryFormat/Wasm.h" 20 #include "llvm/IR/DerivedTypes.h" 21 #include "llvm/MC/MCSymbolWasm.h" 22 #include "llvm/Support/MachineValueType.h" 23 24 namespace llvm { 25 namespace WebAssembly { 26 27 /// Used as immediate MachineOperands for block signatures 28 enum class BlockType : unsigned { 29 Invalid = 0x00, 30 Void = 0x40, 31 I32 = unsigned(wasm::ValType::I32), 32 I64 = unsigned(wasm::ValType::I64), 33 F32 = unsigned(wasm::ValType::F32), 34 F64 = unsigned(wasm::ValType::F64), 35 V128 = unsigned(wasm::ValType::V128), 36 Externref = unsigned(wasm::ValType::EXTERNREF), 37 Funcref = unsigned(wasm::ValType::FUNCREF), 38 // Multivalue blocks (and other non-void blocks) are only emitted when the 39 // blocks will never be exited and are at the ends of functions (see 40 // WebAssemblyCFGStackify::fixEndsAtEndOfFunction). They also are never made 41 // to pop values off the stack, so the exact multivalue signature can always 42 // be inferred from the return type of the parent function in MCInstLower. 43 Multivalue = 0xffff, 44 }; 45 46 enum WasmAddressSpace : unsigned { 47 // Default address space, for pointers to linear memory (stack, heap, data). 48 WASM_ADDRESS_SPACE_DEFAULT = 0, 49 // A non-integral address space for pointers to named objects outside of 50 // linear memory: WebAssembly globals or WebAssembly locals. Loads and stores 51 // to these pointers are lowered to global.get / global.set or local.get / 52 // local.set, as appropriate. 53 WASM_ADDRESS_SPACE_VAR = 1, 54 // A non-integral address space for externref values 55 WASM_ADDRESS_SPACE_EXTERNREF = 10, 56 // A non-integral address space for funcref values 57 WASM_ADDRESS_SPACE_FUNCREF = 20, 58 }; 59 60 inline bool isDefaultAddressSpace(unsigned AS) { 61 return AS == WASM_ADDRESS_SPACE_DEFAULT; 62 } 63 inline bool isWasmVarAddressSpace(unsigned AS) { 64 return AS == WASM_ADDRESS_SPACE_VAR; 65 } 66 inline bool isValidAddressSpace(unsigned AS) { 67 return isDefaultAddressSpace(AS) || isWasmVarAddressSpace(AS); 68 } 69 inline bool isFuncrefType(const Type *Ty) { 70 return isa<PointerType>(Ty) && 71 Ty->getPointerAddressSpace() == 72 WasmAddressSpace::WASM_ADDRESS_SPACE_FUNCREF; 73 } 74 inline bool isExternrefType(const Type *Ty) { 75 return isa<PointerType>(Ty) && 76 Ty->getPointerAddressSpace() == 77 WasmAddressSpace::WASM_ADDRESS_SPACE_EXTERNREF; 78 } 79 inline bool isRefType(const Type *Ty) { 80 return isFuncrefType(Ty) || isExternrefType(Ty); 81 } 82 83 // Convert StringRef to ValType / HealType / BlockType 84 85 Optional<wasm::ValType> parseType(StringRef Type); 86 BlockType parseBlockType(StringRef Type); 87 MVT parseMVT(StringRef Type); 88 89 // Convert ValType or a list/signature of ValTypes to a string. 90 91 // Convert an unsinged integer, which can be among wasm::ValType enum, to its 92 // type name string. If the input is not within wasm::ValType, returns 93 // "invalid_type". 94 const char *anyTypeToString(unsigned Type); 95 const char *typeToString(wasm::ValType Type); 96 // Convert a list of ValTypes into a string in the format of 97 // "type0, type1, ... typeN" 98 std::string typeListToString(ArrayRef<wasm::ValType> List); 99 // Convert a wasm signature into a string in the format of 100 // "(params) -> (results)", where params and results are a string of ValType 101 // lists. 102 std::string signatureToString(const wasm::WasmSignature *Sig); 103 104 // Convert a MVT into its corresponding wasm ValType. 105 wasm::ValType toValType(MVT Type); 106 107 // Convert a register class to a wasm ValType. 108 wasm::ValType regClassToValType(unsigned RC); 109 110 /// Sets a Wasm Symbol Type. 111 void wasmSymbolSetType(MCSymbolWasm *Sym, const Type *GlobalVT, 112 const SmallVector<MVT, 1> &VTs); 113 114 } // end namespace WebAssembly 115 } // end namespace llvm 116 117 #endif 118