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