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