1 //===-- WebAssemblyUtilities - WebAssembly Utility Functions ---*- 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
11 /// utility functions.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_LIB_TARGET_WEBASSEMBLY_UTILS_WEBASSEMBLYUTILITIES_H
16 #define LLVM_LIB_TARGET_WEBASSEMBLY_UTILS_WEBASSEMBLYUTILITIES_H
17 
18 #include "llvm/IR/DerivedTypes.h"
19 
20 namespace llvm {
21 
22 class MachineBasicBlock;
23 class MachineInstr;
24 class MachineOperand;
25 class MCContext;
26 class MCSymbolWasm;
27 class StringRef;
28 class WebAssemblyFunctionInfo;
29 class WebAssemblySubtarget;
30 
31 namespace WebAssembly {
32 
33 enum WasmAddressSpace : unsigned {
34   // Default address space, for pointers to linear memory (stack, heap, data).
35   WASM_ADDRESS_SPACE_DEFAULT = 0,
36   // A non-integral address space for pointers to named objects outside of
37   // linear memory: WebAssembly globals or WebAssembly locals.  Loads and stores
38   // to these pointers are lowered to global.get / global.set or local.get /
39   // local.set, as appropriate.
40   WASM_ADDRESS_SPACE_VAR = 1,
41   // A non-integral address space for externref values
42   WASM_ADDRESS_SPACE_EXTERNREF = 10,
43   // A non-integral address space for funcref values
44   WASM_ADDRESS_SPACE_FUNCREF = 20,
45 };
46 
isDefaultAddressSpace(unsigned AS)47 inline bool isDefaultAddressSpace(unsigned AS) {
48   return AS == WASM_ADDRESS_SPACE_DEFAULT;
49 }
isWasmVarAddressSpace(unsigned AS)50 inline bool isWasmVarAddressSpace(unsigned AS) {
51   return AS == WASM_ADDRESS_SPACE_VAR;
52 }
isValidAddressSpace(unsigned AS)53 inline bool isValidAddressSpace(unsigned AS) {
54   return isDefaultAddressSpace(AS) || isWasmVarAddressSpace(AS);
55 }
isFuncrefType(const Type * Ty)56 inline bool isFuncrefType(const Type *Ty) {
57   return isa<PointerType>(Ty) &&
58          Ty->getPointerAddressSpace() ==
59              WasmAddressSpace::WASM_ADDRESS_SPACE_FUNCREF;
60 }
isExternrefType(const Type * Ty)61 inline bool isExternrefType(const Type *Ty) {
62   return isa<PointerType>(Ty) &&
63          Ty->getPointerAddressSpace() ==
64              WasmAddressSpace::WASM_ADDRESS_SPACE_EXTERNREF;
65 }
isRefType(const Type * Ty)66 inline bool isRefType(const Type *Ty) {
67   return isFuncrefType(Ty) || isExternrefType(Ty);
68 }
69 
70 bool isChild(const MachineInstr &MI, const WebAssemblyFunctionInfo &MFI);
71 bool mayThrow(const MachineInstr &MI);
72 
73 // Exception-related function names
74 extern const char *const ClangCallTerminateFn;
75 extern const char *const CxaBeginCatchFn;
76 extern const char *const CxaRethrowFn;
77 extern const char *const StdTerminateFn;
78 extern const char *const PersonalityWrapperFn;
79 
80 /// Returns the operand number of a callee, assuming the argument is a call
81 /// instruction.
82 const MachineOperand &getCalleeOp(const MachineInstr &MI);
83 
84 /// Returns the __indirect_function_table, for use in call_indirect and in
85 /// function bitcasts.
86 MCSymbolWasm *
87 getOrCreateFunctionTableSymbol(MCContext &Ctx,
88                                const WebAssemblySubtarget *Subtarget);
89 
90 /// Returns the __funcref_call_table, for use in funcref calls when lowered to
91 /// table.set + call_indirect.
92 MCSymbolWasm *
93 getOrCreateFuncrefCallTableSymbol(MCContext &Ctx,
94                                   const WebAssemblySubtarget *Subtarget);
95 
96 /// Find a catch instruction from an EH pad. Returns null if no catch
97 /// instruction found or the catch is in an invalid location.
98 MachineInstr *findCatch(MachineBasicBlock *EHPad);
99 
100 } // end namespace WebAssembly
101 
102 } // end namespace llvm
103 
104 #endif
105