aboutsummaryrefslogtreecommitdiff
path: root/include/cps/ExceptionHandler.h
blob: f55526bfc04958e2f0f71edbeaf4bf0c4e681038 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

#pragma once
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Module.h"
#include <map>
#include <functional>
#include <vector>

namespace cps {

// C++ ABI exception emission. State is keyed by function because file helper
// bodies and user functions are emitted using the same builder as their caller.
class ExceptionHandler {
    struct Handler {
        llvm::BasicBlock *Target;
        llvm::AllocaInst *Exception;
        unsigned CatchDepth;
    };
    struct FunctionState {
        std::vector<Handler> Handlers;
        unsigned CatchDepth = 0;
        bool HasCleanups = false;
    };

    struct CleanupPoint {
        llvm::Instruction *Before;
        FunctionState State;
        bool Unwinding;
        bool TemporariesOnly = false;
    };
    std::map<llvm::Function *, std::vector<CleanupPoint>> CleanupPoints;
    bool FillingUnwind = false;
    llvm::LLVMContext &Context;
    llvm::IRBuilder<> &Builder;
    llvm::Module &Module;
    std::map<llvm::Function *, FunctionState> States;
    llvm::FunctionCallee Allocate, Throw, BeginCatch, EndCatch, Personality;
    llvm::GlobalVariable *IntTypeInfo = nullptr;

    void ensureRuntime();
    llvm::BasicBlock *emitUnwindDestination();

public:
    ExceptionHandler(llvm::LLVMContext &C, llvm::IRBuilder<> &B, llvm::Module &M)
        : Context(C), Builder(B), Module(M) {}

    void prepareFunction(llvm::Function *F);
    void pushHandler(llvm::BasicBlock *Target);
    llvm::AllocaInst *popHandler();
    void beginCatch(llvm::AllocaInst *Exception);
    void endCatch();
    llvm::Instruction *emitReturnCleanup();
    void enableCleanups(llvm::Function *F);
    void recordExit(llvm::Instruction *Before, bool Unwinding = false);
    void fillCleanups(llvm::Function *F, const std::function<void(bool)> &Destroy);

    llvm::Value *emitCall(llvm::FunctionCallee Callee, llvm::ArrayRef<llvm::Value *> Args,
                          const llvm::Twine &Name = "");
    void emitThrow();
};

} // namespace cps