LLVM  15.0.0git
WebAssemblyTypeUtilities.h
Go to the documentation of this file.
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"
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),
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).
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.
54  // A non-integral address space for externref values
56  // A non-integral address space for funcref values
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) {
68 }
69 inline bool isFuncrefType(const Type *Ty) {
70  return isa<PointerType>(Ty) &&
71  Ty->getPointerAddressSpace() ==
73 }
74 inline bool isExternrefType(const Type *Ty) {
75  return isa<PointerType>(Ty) &&
76  Ty->getPointerAddressSpace() ==
78 }
79 inline bool isRefType(const Type *Ty) {
80  return isFuncrefType(Ty) || isExternrefType(Ty);
81 }
82 
85 }
86 
87 // Convert StringRef to ValType / HealType / BlockType
88 
92 
93 // Convert ValType or a list/signature of ValTypes to a string.
94 
95 // Convert an unsinged integer, which can be among wasm::ValType enum, to its
96 // type name string. If the input is not within wasm::ValType, returns
97 // "invalid_type".
98 const char *anyTypeToString(unsigned Type);
99 const char *typeToString(wasm::ValType Type);
100 // Convert a list of ValTypes into a string in the format of
101 // "type0, type1, ... typeN"
102 std::string typeListToString(ArrayRef<wasm::ValType> List);
103 // Convert a wasm signature into a string in the format of
104 // "(params) -> (results)", where params and results are a string of ValType
105 // lists.
106 std::string signatureToString(const wasm::WasmSignature *Sig);
107 
108 // Convert a MVT into its corresponding wasm ValType.
110 
111 // Convert a register class to a wasm ValType.
112 wasm::ValType regClassToValType(unsigned RC);
113 
114 /// Sets a Wasm Symbol Type.
115 void wasmSymbolSetType(MCSymbolWasm *Sym, const Type *GlobalVT,
116  const SmallVector<MVT, 1> &VTs);
117 
118 } // end namespace WebAssembly
119 } // end namespace llvm
120 
121 #endif
llvm::WebAssembly::parseMVT
MVT parseMVT(StringRef Type)
Definition: WebAssemblyTypeUtilities.cpp:58
llvm::WebAssembly::BlockType::I64
@ I64
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::wasm::ValType::I32
@ I32
Optional.h
llvm::WebAssembly::isExternrefType
bool isExternrefType(const Type *Ty)
Definition: WebAssemblyTypeUtilities.h:74
llvm::WebAssembly::WASM_ADDRESS_SPACE_FUNCREF
@ WASM_ADDRESS_SPACE_FUNCREF
Definition: WebAssemblyTypeUtilities.h:57
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1185
llvm::WebAssembly::isFuncrefType
bool isFuncrefType(const Type *Ty)
Definition: WebAssemblyTypeUtilities.h:69
llvm::Type::getPointerAddressSpace
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
Definition: DerivedTypes.h:729
llvm::WebAssembly::signatureToString
std::string signatureToString(const wasm::WasmSignature *Sig)
Definition: WebAssemblyTypeUtilities.cpp:115
llvm::WebAssembly::WasmAddressSpace
WasmAddressSpace
Definition: WebAssemblyTypeUtilities.h:46
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
llvm::WebAssembly::WASM_ADDRESS_SPACE_EXTERNREF
@ WASM_ADDRESS_SPACE_EXTERNREF
Definition: WebAssemblyTypeUtilities.h:55
llvm::Optional
Definition: APInt.h:33
llvm::WebAssembly::BlockType::Invalid
@ Invalid
llvm::WebAssembly::isDefaultAddressSpace
bool isDefaultAddressSpace(unsigned AS)
Definition: WebAssemblyTypeUtilities.h:60
llvm::WebAssembly::wasmSymbolSetType
void wasmSymbolSetType(MCSymbolWasm *Sym, const Type *GlobalVT, const SmallVector< MVT, 1 > &VTs)
Sets a Wasm Symbol Type.
Definition: WebAssemblyTypeUtilities.cpp:171
llvm::WebAssembly::BlockType::F64
@ F64
llvm::WebAssembly::regClassToValType
wasm::ValType regClassToValType(unsigned RC)
Definition: WebAssemblyTypeUtilities.cpp:150
llvm::WebAssembly::BlockType::Void
@ Void
MachineValueType.h
llvm::WebAssembly::parseType
Optional< wasm::ValType > parseType(StringRef Type)
Definition: WebAssemblyTypeUtilities.cpp:23
llvm::wasm::ValType::F64
@ F64
MCSymbolWasm.h
llvm::wasm::ValType::F32
@ F32
llvm::WebAssembly::toValType
wasm::ValType toValType(MVT Type)
Definition: WebAssemblyTypeUtilities.cpp:124
llvm::WebAssembly::BlockType::V128
@ V128
llvm::wasm::ValType
ValType
Definition: Wasm.h:423
llvm::WebAssembly::typeListToString
std::string typeListToString(ArrayRef< wasm::ValType > List)
Definition: WebAssemblyTypeUtilities.cpp:105
llvm::WebAssembly::BlockType::Funcref
@ Funcref
llvm::wasm::ValType::EXTERNREF
@ EXTERNREF
llvm::wasm::ValType::I64
@ I64
llvm::MCSymbolWasm
Definition: MCSymbolWasm.h:16
llvm::wasm::ValType::FUNCREF
@ FUNCREF
llvm::WebAssembly::BlockType::Multivalue
@ Multivalue
llvm::WebAssembly::WASM_ADDRESS_SPACE_VAR
@ WASM_ADDRESS_SPACE_VAR
Definition: WebAssemblyTypeUtilities.h:53
llvm::WebAssembly::BlockType::Externref
@ Externref
llvm::MVT
Machine Value Type.
Definition: MachineValueType.h:31
llvm::WebAssembly::isRefType
bool isRefType(const Type *Ty)
Definition: WebAssemblyTypeUtilities.h:79
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::WebAssembly::BlockType::I32
@ I32
llvm::WebAssembly::WASM_ADDRESS_SPACE_DEFAULT
@ WASM_ADDRESS_SPACE_DEFAULT
Definition: WebAssemblyTypeUtilities.h:48
Wasm.h
llvm::WebAssembly::parseBlockType
BlockType parseBlockType(StringRef Type)
Definition: WebAssemblyTypeUtilities.cpp:44
llvm::WebAssembly::typeToString
const char * typeToString(wasm::ValType Type)
Definition: WebAssemblyTypeUtilities.cpp:101
llvm::wasm::ValType::V128
@ V128
llvm::WebAssembly::isWasmVarAddressSpace
bool isWasmVarAddressSpace(unsigned AS)
Definition: WebAssemblyTypeUtilities.h:63
llvm::WebAssembly::BlockType
BlockType
Used as immediate MachineOperands for block signatures.
Definition: WebAssemblyTypeUtilities.h:28
DerivedTypes.h
llvm::WebAssembly::anyTypeToString
const char * anyTypeToString(unsigned Type)
Definition: WebAssemblyTypeUtilities.cpp:76
llvm::WebAssembly::isValidAddressSpace
bool isValidAddressSpace(unsigned AS)
Definition: WebAssemblyTypeUtilities.h:66
llvm::wasm::WasmSignature
Definition: Wasm.h:433
llvm::WebAssembly::BlockType::F32
@ F32