-
Notifications
You must be signed in to change notification settings - Fork 0
/
codegen.hpp
94 lines (90 loc) · 5.66 KB
/
codegen.hpp
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
85
86
87
88
89
90
91
92
93
94
#ifndef PJPPROJECT_CODEGEN_HPP
#define PJPPROJECT_CODEGEN_HPP
#include <cstdint>
#include <memory>
#include <string>
#include <vector>
#include <stack>
#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Verifier.h"
#include "visitor.hpp"
#include "astnode.hpp"
/* Simle codegen for mila. Some things are not done properly. For example constant are set
*/
//=================================================================================================
class CCodeGen : public CVisitor
{
public:
CCodeGen ( void );
virtual ~CCodeGen ( void ) override = default;
static void Generate ( std::vector<AST::ANode> & ns );
void VisitGroup ( AST::CGroupNode & n ) override;
void VisitInt ( AST::CIntNode & n ) override;
void VisitString ( AST::CStringNode & n ) override;
void VisitIdentifier ( AST::CIdentifierNode & n ) override;
void VisitCall ( AST::CCallNode & n ) override;
void VisitAssign ( AST::CAssignNode & n ) override;
void VisitNot ( AST::CNotNode & n ) override;
void VisitPos ( AST::CPosNode & n ) override;
void VisitNeg ( AST::CNegNode & n ) override;
void VisitEqual ( AST::CEqualNode & n ) override;
void VisitNotEq ( AST::CNotEqNode & n ) override;
void VisitGreat ( AST::CGreatNode & n ) override;
void VisitGreatEq ( AST::CGreatEqNode & n ) override;
void VisitLess ( AST::CLessNode & n ) override;
void VisitLessEq ( AST::CLessEqNode & n ) override;
void VisitPlus ( AST::CPlusNode & n ) override;
void VisitMinus ( AST::CMinusNode & n ) override;
void VisitMul ( AST::CMulNode & n ) override;
void VisitDiv ( AST::CDivNode & n ) override;
void VisitOr ( AST::COrNode & n ) override;
void VisitAnd ( AST::CAndNode & n ) override;
void VisitMod ( AST::CModNode & n ) override;
void VisitExprStmt ( AST::CExprStmtNode & n ) override;
void VisitWrtInt ( AST::CWrtIntNode & n ) override;
void VisitWrtStr ( AST::CWrtStrNode & n ) override;
void VisitRdlStmt ( AST::CRdlStmtNode & n ) override;
void VisitVar ( AST::CVarNode & n ) override;
void VisitConst ( AST::CConstNode & n ) override;
void VisitVars ( AST::CVarsNode & n ) override;
void VisitConsts ( AST::CConstsNode & n ) override;
void VisitBlock ( AST::CBlockNode & n ) override;
void VisitIf ( AST::CIfNode & n ) override;
void VisitWhile ( AST::CWhileNode & n ) override;
void VisitFunDecl ( AST::CFunDeclNode & n ) override;
void VisitExit ( AST::CExitNode & n ) override;
void VisitBreak ( AST::CBreakNode & n ) override;
private:
class CCodegenException : public std::exception
{
const char * what ( void ) const noexcept override;
};
CCodegenException error ( std::string message );
void createIO ( void );
llvm::AllocaInst * createEntryBAlloca ( llvm::Function * function,
const std::string & name );
llvm::Value * findByName ( AST::CIdentifierNode & n );
llvm::Function * prototype ( AST::CFunDeclNode & n );
private:
llvm::LLVMContext m_Context;
llvm::IRBuilder<> m_Builder;
std::unique_ptr<llvm::Module> m_Module;
std::map<std::string, llvm::GlobalValue *> m_GlobalVars;
std::map<std::string, llvm::AllocaInst*> m_LocalVars;
std::map<std::string, llvm::GlobalValue *> m_Constants;
std::stack<llvm::BasicBlock*> m_Scopes;
llvm::Value * m_Value;
};
//=================================================================================================
#endif //PJPPROJECT_CODEGEN_HPP