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 #include "llvm/Support/CommandLine.h"
20 
21 namespace llvm {
22 
23 class MachineBasicBlock;
24 class MachineInstr;
25 class MachineOperand;
26 class MCContext;
27 class MCSymbolWasm;
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 
47 inline bool isDefaultAddressSpace(unsigned AS) {
48   return AS == WASM_ADDRESS_SPACE_DEFAULT;
49 }
50 inline bool isWasmVarAddressSpace(unsigned AS) {
51   return AS == WASM_ADDRESS_SPACE_VAR;
52 }
53 inline bool isValidAddressSpace(unsigned AS) {
54   return isDefaultAddressSpace(AS) || isWasmVarAddressSpace(AS);
55 }
56 inline bool isFuncrefType(const Type *Ty) {
57   return isa<PointerType>(Ty) &&
58          Ty->getPointerAddressSpace() ==
59              WasmAddressSpace::WASM_ADDRESS_SPACE_FUNCREF;
60 }
61 inline bool isExternrefType(const Type *Ty) {
62   return isa<PointerType>(Ty) &&
63          Ty->getPointerAddressSpace() ==
64              WasmAddressSpace::WASM_ADDRESS_SPACE_EXTERNREF;
65 }
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 handling / setjmp-longjmp handling command-line options
74 extern cl::opt<bool> WasmEnableEmEH;   // asm.js-style EH
75 extern cl::opt<bool> WasmEnableEmSjLj; // asm.js-style SjLJ
76 extern cl::opt<bool> WasmEnableEH;     // EH using Wasm EH instructions
77 extern cl::opt<bool> WasmEnableSjLj;   // SjLj using Wasm EH instructions
78 
79 // Exception-related function names
80 extern const char *const ClangCallTerminateFn;
81 extern const char *const CxaBeginCatchFn;
82 extern const char *const CxaRethrowFn;
83 extern const char *const StdTerminateFn;
84 extern const char *const PersonalityWrapperFn;
85 
86 /// Returns the operand number of a callee, assuming the argument is a call
87 /// instruction.
88 const MachineOperand &getCalleeOp(const MachineInstr &MI);
89 
90 /// Returns the __indirect_function_table, for use in call_indirect and in
91 /// function bitcasts.
92 MCSymbolWasm *
93 getOrCreateFunctionTableSymbol(MCContext &Ctx,
94                                const WebAssemblySubtarget *Subtarget);
95 
96 /// Returns the __funcref_call_table, for use in funcref calls when lowered to
97 /// table.set + call_indirect.
98 MCSymbolWasm *
99 getOrCreateFuncrefCallTableSymbol(MCContext &Ctx,
100                                   const WebAssemblySubtarget *Subtarget);
101 
102 /// Find a catch instruction from an EH pad. Returns null if no catch
103 /// instruction found or the catch is in an invalid location.
104 MachineInstr *findCatch(MachineBasicBlock *EHPad);
105 
106 } // end namespace WebAssembly
107 
108 } // end namespace llvm
109 
110 #endif
111