aboutsummaryrefslogtreecommitdiff
path: root/include/cps/CodeGen.h
blob: 262179571158a295b5c23ca786583457dfcb0aec (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

#pragma once
#include "cps/AST.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include <map>

namespace cps {

class CodeGen {
    std::unique_ptr<llvm::LLVMContext> TheContext;
    std::unique_ptr<llvm::Module> TheModule;
    std::unique_ptr<llvm::IRBuilder<>> Builder;
    std::map<std::string, llvm::AllocaInst*> NamedValues;

    llvm::AllocaInst *CreateEntryBlockAlloca(llvm::Function *TheFunction, 
                                            const std::string &VarName);

    llvm::FunctionCallee PrintfFunc;
    llvm::FunctionCallee ScanfFunc;
    llvm::Value *PrintfFormatStr;
    llvm::Value *ScanfFormatStr;

    void SetupExternalFunctions();

public:
    CodeGen();
    void compile(const std::vector<std::unique_ptr<StmtAST>> &Statements);
    void print();
};

} // namespace cps