LLVM  16.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 
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),
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).
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.
57  // A non-integral address space for externref values
59  // A non-integral address space for funcref values
61 };
62 
63 inline bool isDefaultAddressSpace(unsigned AS) {
64  return AS == WASM_ADDRESS_SPACE_DEFAULT;
65 }
66 inline bool isWasmVarAddressSpace(unsigned AS) {
67  return AS == WASM_ADDRESS_SPACE_VAR;
68 }
69 inline bool isValidAddressSpace(unsigned AS) {
71 }
72 inline bool isFuncrefType(const Type *Ty) {
73  return isa<PointerType>(Ty) &&
74  Ty->getPointerAddressSpace() ==
76 }
77 inline bool isExternrefType(const Type *Ty) {
78  return isa<PointerType>(Ty) &&
79  Ty->getPointerAddressSpace() ==
81 }
82 inline bool isRefType(const Type *Ty) {
83  return isFuncrefType(Ty) || isExternrefType(Ty);
84 }
85 
88 }
89 
90 // Convert StringRef to ValType / HealType / BlockType
91 
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.
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.
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
llvm::WebAssembly::parseMVT
MVT parseMVT(StringRef Type)
Definition: WebAssemblyTypeUtilities.cpp:59
llvm::WebAssembly::BlockType::I64
@ I64
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::wasm::ValType::I32
@ I32
Optional.h
llvm::WebAssembly::isExternrefType
bool isExternrefType(const Type *Ty)
Definition: WebAssemblyTypeUtilities.h:77
llvm::WebAssembly::WASM_ADDRESS_SPACE_FUNCREF
@ WASM_ADDRESS_SPACE_FUNCREF
Definition: WebAssemblyTypeUtilities.h:60
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1199
llvm::WebAssembly::isFuncrefType
bool isFuncrefType(const Type *Ty)
Definition: WebAssemblyTypeUtilities.h:72
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:116
llvm::WebAssembly::WasmAddressSpace
WasmAddressSpace
Definition: WebAssemblyTypeUtilities.h:49
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:58
llvm::Optional
Definition: APInt.h:33
llvm::WebAssembly::BlockType::Invalid
@ Invalid
llvm::WebAssembly::isDefaultAddressSpace
bool isDefaultAddressSpace(unsigned AS)
Definition: WebAssemblyTypeUtilities.h:63
llvm::WebAssembly::wasmSymbolSetType
void wasmSymbolSetType(MCSymbolWasm *Sym, const Type *GlobalVT, const SmallVector< MVT, 1 > &VTs)
Sets a Wasm Symbol Type.
Definition: WebAssemblyTypeUtilities.cpp:177
llvm::WebAssembly::BlockType::F64
@ F64
llvm::WebAssembly::regClassToValType
wasm::ValType regClassToValType(unsigned RC)
Definition: WebAssemblyTypeUtilities.cpp:151
llvm::WebAssembly::BlockType::Void
@ Void
MachineValueType.h
llvm::WebAssembly::parseType
Optional< wasm::ValType > parseType(StringRef Type)
Definition: WebAssemblyTypeUtilities.cpp:24
llvm::wasm::ValType::F64
@ F64
MCSymbolWasm.h
llvm::TargetRegisterClass
Definition: TargetRegisterInfo.h:46
llvm::wasm::ValType::F32
@ F32
llvm::WebAssembly::toValType
wasm::ValType toValType(MVT Type)
Definition: WebAssemblyTypeUtilities.cpp:125
llvm::WebAssembly::BlockType::V128
@ V128
llvm::wasm::ValType
ValType
Definition: Wasm.h:424
llvm::WebAssembly::typeListToString
std::string typeListToString(ArrayRef< wasm::ValType > List)
Definition: WebAssemblyTypeUtilities.cpp:106
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:56
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:82
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:50
llvm::WebAssembly::BlockType::I32
@ I32
llvm::WebAssembly::WASM_ADDRESS_SPACE_DEFAULT
@ WASM_ADDRESS_SPACE_DEFAULT
Definition: WebAssemblyTypeUtilities.h:51
Wasm.h
llvm::WebAssembly::parseBlockType
BlockType parseBlockType(StringRef Type)
Definition: WebAssemblyTypeUtilities.cpp:45
llvm::WebAssembly::typeToString
const char * typeToString(wasm::ValType Type)
Definition: WebAssemblyTypeUtilities.cpp:102
llvm::wasm::ValType::V128
@ V128
llvm::WebAssembly::isWasmVarAddressSpace
bool isWasmVarAddressSpace(unsigned AS)
Definition: WebAssemblyTypeUtilities.h:66
llvm::WebAssembly::BlockType
BlockType
Used as immediate MachineOperands for block signatures.
Definition: WebAssemblyTypeUtilities.h:31
DerivedTypes.h
llvm::WebAssembly::anyTypeToString
const char * anyTypeToString(unsigned Type)
Definition: WebAssemblyTypeUtilities.cpp:77
llvm::WebAssembly::isValidAddressSpace
bool isValidAddressSpace(unsigned AS)
Definition: WebAssemblyTypeUtilities.h:69
llvm::wasm::WasmSignature
Definition: Wasm.h:434
llvm::WebAssembly::BlockType::F32
@ F32