summaryrefslogtreecommitdiff
path: root/node/DeclNode.cc
blob: d22c8479ed456d550154340aad4d1cd458e3e3e9 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <llvm/IR/IRBuilder.h>

#include "DeclNode.hh"
#include "Log.hh"
#include "TypeIdentifier.hh"

namespace pcc {
DeclNode::DeclNode(Context* context, VariableList* scope)
    : BaseNode(context)
    , scope_(scope) {}

void DeclNode::SetScope(VariableList* scope) { scope_ = scope; }

VarDeclNode::VarDeclNode(Context* context, VariableList* scope,
                         std::list<Declaration>&& decls)
    : DeclNode(context, scope)
    , decls_(std::move(decls)) {}

// if the scope is nullptr, this vardecl is in global scope. global variables
// are static, while local variables are allocated on stack.
Value VarDeclNode::CodeGen() {
    llvm::IRBuilder<>* builder = context_->GetBuilder();
    TypeManager* manager = context_->GetTypeManager();
    if (scope_ != nullptr) {
        for (auto&& decl : decls_) {
            std::shared_ptr<Type> type = std::get<1>(decl)->GetType();
            Value value = builder->CreateAlloca(type->GetLLVMType());
            scope_->AddVariable(std::get<0>(decl),
                                {manager->GetPointerType(type), value});
        }
        return nullptr;
    }
    // global variable case
    VariableList* globals = context_->GetGlobals();
    for (auto&& decl : decls_) {
        std::shared_ptr<Type> type = std::get<1>(decl)->GetType();
        llvm::GlobalValue* value = new llvm::GlobalVariable(
            *context_->GetModule(), type->GetLLVMType(), false,
            llvm::GlobalVariable::PrivateLinkage, type->GetZeroInitializer());
        value->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
        globals->AddVariable(std::get<0>(decl),
                             {manager->GetPointerType(type), value});
    }
    return nullptr;
}

ConstDeclNode::ConstDeclNode(Context* context, VariableList* scope,
                             std::list<Constant>&& decls)
    : DeclNode(context, scope)
    , decls_(std::move(decls)) {}

// for constants, we don't need to allocate them on stack, both global and local
// constants are static.
Value ConstDeclNode::CodeGen() {
    TypeManager* manager = context_->GetTypeManager();
    scope_ = (scope_ == nullptr ? context_->GetGlobals() : scope_);
    for (auto&& decl : decls_) {
        std::shared_ptr<ExprNode> ValueNode = std::get<1>(decl);
        Value value = ValueNode->CodeGen();
        std::shared_ptr<Type> type = ValueNode->GetType();
        llvm::GlobalValue* constant = new llvm::GlobalVariable(
            *context_->GetModule(), type->GetLLVMType(), true,
            llvm::GlobalValue::PrivateLinkage,
            llvm::cast<llvm::Constant>(value));
        constant->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
        scope_->AddVariable(std::get<0>(decl),
                            {manager->GetPointerType(type), constant});
    }
    return nullptr;
}

TypeDeclNode::TypeDeclNode(Context* context, VariableList* scope,
                           std::list<Declaration>&& decls)
    : DeclNode(context, scope)
    , decls_(std::move(decls)) {}

Value TypeDeclNode::CodeGen() {
    VariableList* scope = (scope_ == nullptr ? context_->GetGlobals() : scope_);
    for (auto&& decl : decls_) {
        scope->AddTypeAlias(std::get<0>(decl), std::get<1>(decl)->GetType());
    }
    return nullptr;
}
} // namespace pcc