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/ADT/Optional.h"
19 
20 namespace llvm {
21 
22 class MachineBasicBlock;
23 class MachineInstr;
24 class MachineFunction;
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_WASM_VAR = 1
42 };
43 
44 inline bool isDefaultAddressSpace(unsigned AS) {
45   return AS == WASM_ADDRESS_SPACE_DEFAULT;
46 }
47 inline bool isWasmVarAddressSpace(unsigned AS) {
48   return AS == WASM_ADDRESS_SPACE_WASM_VAR;
49 }
50 inline bool isValidAddressSpace(unsigned AS) {
51   return isDefaultAddressSpace(AS) || isWasmVarAddressSpace(AS);
52 }
53 
54 // Returns the index of the WebAssembly local to which the stack object
55 // FrameIndex in MF should be allocated, or None.
56 Optional<unsigned> getLocalForStackObject(MachineFunction &MF, int FrameIndex);
57 
58 bool isChild(const MachineInstr &MI, const WebAssemblyFunctionInfo &MFI);
59 bool mayThrow(const MachineInstr &MI);
60 
61 // Exception-related function names
62 extern const char *const ClangCallTerminateFn;
63 extern const char *const CxaBeginCatchFn;
64 extern const char *const CxaRethrowFn;
65 extern const char *const StdTerminateFn;
66 extern const char *const PersonalityWrapperFn;
67 
68 /// Returns the operand number of a callee, assuming the argument is a call
69 /// instruction.
70 const MachineOperand &getCalleeOp(const MachineInstr &MI);
71 
72 /// Returns the __indirect_function_table, for use in call_indirect and in
73 /// function bitcasts.
74 MCSymbolWasm *
75 getOrCreateFunctionTableSymbol(MCContext &Ctx,
76                                const WebAssemblySubtarget *Subtarget);
77 
78 /// Find a catch instruction from an EH pad. Returns null if no catch
79 /// instruction found or the catch is in an invalid location.
80 MachineInstr *findCatch(MachineBasicBlock *EHPad);
81 
82 } // end namespace WebAssembly
83 
84 } // end namespace llvm
85 
86 #endif
87