summaryrefslogtreecommitdiff
path: root/Context.cc
blob: 2462b64cb8ce46c08af364c4f998aa6637b660ee (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
62
63
64
65
66
67
68
69
#include <llvm/IR/LLVMContext.h>

#include "Context.hh"
#include "Log.hh"

namespace pcc {
// llvm needs a LLVMContext for every thread, but we only have one thread, so we
// just use this global context.
llvm::LLVMContext GlobalLLVMContext;

Context::Context(const char* name)
    : builder_(GlobalLLVMContext)
    , module_(name, GlobalLLVMContext)
    , JumpingFlag_(false) {
    scopes_.push(&globals_);
}

TypeManager* Context::GetTypeManager() { return &TypeManager_; }

VariableList* Context::GetGlobals() { return &globals_; }

llvm::Module* Context::GetModule() { return &module_; }

llvm::IRBuilder<>* Context::GetBuilder() { return &builder_; }

VariableList* Context::GetCurrentScope() { return scopes_.top(); }

void Context::PushScope(VariableList* scope) { scopes_.push(scope); }

void Context::PopScope() { scopes_.pop(); }

std::shared_ptr<FunctionType> Context::GetFunctionType(std::string name) {
    decltype(FuncTypes_)::iterator result = FuncTypes_.find(name);
    if (result == FuncTypes_.end()) {
        return nullptr;
    }
    return std::get<1>(*result);
}

void Context::AddFunctionType(std::string name,
                              std::shared_ptr<FunctionType> type) {
    FuncTypes_.insert({name, type});
}

void Context::PushBreakDestination(llvm::BasicBlock* dest) {
    BreakDestinations_.push(dest);
}

void Context::PopBreakDestination() { BreakDestinations_.pop(); }

llvm::BasicBlock* Context::GetBreakDestination() {
    return BreakDestinations_.empty() ? nullptr : BreakDestinations_.top();
}

void Context::PushContinueDestination(llvm::BasicBlock* dest) {
    ContinueDestinations_.push(dest);
}

void Context::PopContinueDestination() { ContinueDestinations_.pop(); }

llvm::BasicBlock* Context::GetContinueDestination() {
    return ContinueDestinations_.empty() ? nullptr
                                         : ContinueDestinations_.top();
}

void Context::SetJumpingFlag(bool flag) { JumpingFlag_ = flag; }

bool Context::GetJumpingFlag() { return JumpingFlag_; }
} // namespace pcc