-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodegen_visitor.h
More file actions
83 lines (67 loc) · 2.37 KB
/
codegen_visitor.h
File metadata and controls
83 lines (67 loc) · 2.37 KB
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
#ifndef __CODE_GEN_VISITOR_H__
#define __CODE_GEN_VISITOR_H__ 1
#include <memory>
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Value.h"
#include "./include/KaleidoscopeJIT.h"
#include "ast.h"
namespace Rubiee {
class ASTNodeVisitor {
public:
virtual ~ASTNodeVisitor() = default;
virtual void visit(Expr &expr) = 0;
virtual void visit(Statement &stmt) = 0;
virtual void visit(IntConst &int_const) = 0;
virtual void visit(BinaryExpr &binary_expr) = 0;
virtual void visit(ComparisonExpr &comparison_expr) = 0;
virtual void visit(IfExpr &if_expr) = 0;
virtual void visit(ForLoopExpr &for_loop_expr) = 0;
virtual void visit(Variable &var) = 0;
virtual void visit(VariableAssignment &var_assignment) = 0;
virtual void visit(FunctionCall &function_call) = 0;
virtual void visit(FunctionPrototype &function_prototype) = 0;
virtual void visit(TopLevelExpr &top_level_expr) = 0;
virtual void visit(Function &function) = 0;
};
class CodeGenVisitor : public ASTNodeVisitor {
public:
CodeGenVisitor();
void visit(Expr &expr);
void visit(Statement &stmt);
void visit(IntConst &int_const);
void visit(BinaryExpr &binary_expr);
void visit(ComparisonExpr &comparison_expr);
void visit(IfExpr &if_expr);
void visit(ForLoopExpr &for_loop_expr);
void visit(Variable &var);
void visit(VariableAssignment &var_assignment);
void visit(FunctionCall &function_call);
void visit(FunctionPrototype &function_prototype);
void visit(TopLevelExpr &top_level_expr);
void visit(Function &function);
void executeCode();
private:
// LLVM-related variables
llvm::LLVMContext context;
llvm::IRBuilder<> builder;
std::unique_ptr<llvm::Module> module;
// Store the intermediate codegen result
llvm::Value *generated_value;
llvm::Function *generated_function;
// Symbol table
std::map<std::string, llvm::AllocaInst *> variables;
// JIT
std::unique_ptr<llvm::orc::KaleidoscopeJIT> jit;
// Functions
std::map<std::string, llvm::Function*> stdlib_functions;
// llvm::BasicBlock *main_function;
llvm::Function *main_function;
// Methods
void initModule(std::unique_ptr<llvm::Module> &module, std::string module_name);
void initStandardLibraryFunctions();
void initTopLevelExpr();
};
}
#endif