sammine-lang
Loading...
Searching...
No Matches
CodegenVisitor.h
Go to the documentation of this file.
1//
2// Created by Jasmine Tang on 3/27/24.
3//
4
5#pragma once
6#include "TypeConverter.h"
7#include "ast/AstBase.h"
8#include "ast/AstDecl.h"
9#include "codegen/Garbage.h"
10#include "codegen/LLVMRes.h"
11#include <llvm/IR/Function.h>
12#include <llvm/IR/Type.h>
13#include <llvm/IR/Value.h>
14
17namespace sammine_lang::AST {
18class CgVisitor : public ScopedASTVisitor {
19
20private:
21 std::shared_ptr<sammine_lang::LLVMRes> resPtr;
22 std::stack<std::map<std::string, llvm::AllocaInst *>> allocaValues;
23
24 llvm::Function *current_func;
25 llvm::Function *getCurrentFunction();
26
27 void setCurrentFunction(llvm::Function *);
28
29 TypeConverter type_converter;
30
31 // INFO: The collector is named Jasmine because she said on her discord status
32 // once that she's a garbage woman lol
34 RefCounter ref_counter;
35
36 void cleanUpGarbageBeforeExit() {
37 // Before function exit, call the reference count visitor to clean up any zero-ref objects
38 llvm::Function *refcntVisitorFunc = resPtr->Module->getFunction("refcnt_visitor");
39 if (refcntVisitorFunc) {
40 // Call refcnt_visitor() to check all GC roots in shadow stack
41 resPtr->Builder->CreateCall(refcntVisitorFunc, {});
42 }
43
44 // Clean up the shadow stack entry for this function
45 jasmine.relieveStackEntry();
46 }
47public:
48 CgVisitor(std::shared_ptr<sammine_lang::LLVMRes> resPtr)
49 : resPtr(resPtr), type_converter(*resPtr), jasmine(*resPtr),
50 ref_counter(*resPtr) {}
51
52 void enter_new_scope() override;
53 void exit_new_scope() override;
54
55 virtual void visit(FuncDefAST *) override;
56 // visit
57 // pre order
58 // TODO: Implement these
59 virtual void preorder_walk(ProgramAST *ast) override;
60 virtual void preorder_walk(VarDefAST *ast) override;
61 virtual void preorder_walk(FuncDefAST *ast) override;
62 virtual void preorder_walk(RecordDefAST *ast) override;
63 virtual void preorder_walk(ExternAST *ast) override;
64 virtual void preorder_walk(PrototypeAST *ast) override;
65 virtual void preorder_walk(CallExprAST *ast) override;
66 virtual void preorder_walk(ReturnExprAST *ast) override {}
67 virtual void preorder_walk(BinaryExprAST *ast) override;
68 virtual void preorder_walk(NumberExprAST *ast) override;
69 virtual void preorder_walk(StringExprAST *ast) override;
70 virtual void preorder_walk(BoolExprAST *ast) override;
71 virtual void preorder_walk(VariableExprAST *ast) override;
72 virtual void preorder_walk(BlockAST *ast) override;
73 virtual void preorder_walk(IfExprAST *ast) override;
74 virtual void preorder_walk(UnitExprAST *ast) override;
75 virtual void preorder_walk(TypedVarAST *ast) override;
76
77 // post order
78 // TODO: Implement these?
79 virtual void postorder_walk(ProgramAST *ast) override {}
80 virtual void postorder_walk(VarDefAST *ast) override;
81 virtual void postorder_walk(ExternAST *ast) override {}
82 virtual void postorder_walk(FuncDefAST *ast) override;
83 virtual void postorder_walk(RecordDefAST *ast) override;
84 virtual void postorder_walk(PrototypeAST *ast) override {}
85 virtual void postorder_walk(CallExprAST *ast) override {}
86 virtual void postorder_walk(ReturnExprAST *ast) override;
87 virtual void postorder_walk(BinaryExprAST *ast) override;
88 virtual void postorder_walk(NumberExprAST *ast) override {}
89 virtual void postorder_walk(StringExprAST *ast) override;
90 virtual void postorder_walk(BoolExprAST *ast) override {}
91 virtual void postorder_walk(VariableExprAST *ast) override {}
92 virtual void postorder_walk(BlockAST *ast) override;
93 virtual void postorder_walk(IfExprAST *ast) override {}
94 virtual void postorder_walk(UnitExprAST *ast) override {}
95 virtual void postorder_walk(TypedVarAST *ast) override {}
96};
97} // namespace sammine_lang::AST
Defines the AST Abstract class for printing out AST Nodes.
Holds declaration for all the AST Nodes.
Houses ShadowGarbageCollector scheme as well as the ref counter.
Defined LLVMRes, which encapsulates the state of LLVM (Context, Modules, IRBuilder,...
Defines the TypeConverter, which holds the characistics of converting our AST types into LLVM IR type...
An AST to simulate a { } code block.
Definition Ast.h:158
Definition Ast.h:301
Definition Ast.h:375
virtual void visit(FuncDefAST *) override
Definition CodegenVisitor.cpp:42
A Function Definition that has the prototype and definition in terms of a block.
Definition Ast.h:129
Definition Ast.h:175
Definition Ast.h:432
A prototype to present "func func_name(...) -> type;".
Definition Ast.h:76
Definition Garbage.h:91
Definition AstBase.h:145
Definition TypeConverter.h:14
Definition Ast.h:406
A variable definition: "var x = expression;".
Definition Ast.h:231