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
36public:
37 CgVisitor(std::shared_ptr<sammine_lang::LLVMRes> resPtr)
38 : resPtr(resPtr), type_converter(*resPtr), jasmine(*resPtr),
39 ref_counter(*resPtr) {}
40
41 void enter_new_scope() override;
42 void exit_new_scope() override;
43
44 virtual void visit(FuncDefAST *) override;
45 // visit
46 // pre order
47 // TODO: Implement these
48 virtual void preorder_walk(ProgramAST *ast) override;
49 virtual void preorder_walk(VarDefAST *ast) override;
50 virtual void preorder_walk(FuncDefAST *ast) override;
51 virtual void preorder_walk(RecordDefAST *ast) override;
52 virtual void preorder_walk(ExternAST *ast) override;
53 virtual void preorder_walk(PrototypeAST *ast) override;
54 virtual void preorder_walk(CallExprAST *ast) override;
55 virtual void preorder_walk(ReturnExprAST *ast) override {}
56 virtual void preorder_walk(BinaryExprAST *ast) override;
57 virtual void preorder_walk(NumberExprAST *ast) override;
58 virtual void preorder_walk(StringExprAST *ast) override;
59 virtual void preorder_walk(BoolExprAST *ast) override;
60 virtual void preorder_walk(VariableExprAST *ast) override;
61 virtual void preorder_walk(BlockAST *ast) override;
62 virtual void preorder_walk(IfExprAST *ast) override;
63 virtual void preorder_walk(UnitExprAST *ast) override;
64 virtual void preorder_walk(TypedVarAST *ast) override;
65
66 // post order
67 // TODO: Implement these?
68 virtual void postorder_walk(ProgramAST *ast) override {}
69 virtual void postorder_walk(VarDefAST *ast) override;
70 virtual void postorder_walk(ExternAST *ast) override {}
71 virtual void postorder_walk(FuncDefAST *ast) override;
72 virtual void postorder_walk(RecordDefAST *ast) override;
73 virtual void postorder_walk(PrototypeAST *ast) override {}
74 virtual void postorder_walk(CallExprAST *ast) override {}
75 virtual void postorder_walk(ReturnExprAST *ast) override;
76 virtual void postorder_walk(BinaryExprAST *ast) override;
77 virtual void postorder_walk(NumberExprAST *ast) override {}
78 virtual void postorder_walk(StringExprAST *ast) override {}
79 virtual void postorder_walk(BoolExprAST *ast) override {}
80 virtual void postorder_walk(VariableExprAST *ast) override {}
81 virtual void postorder_walk(BlockAST *ast) override;
82 virtual void postorder_walk(IfExprAST *ast) override {}
83 virtual void postorder_walk(UnitExprAST *ast) override {}
84 virtual void postorder_walk(TypedVarAST *ast) override {}
85};
86} // 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:93
Definition AstBase.h:145
Definition TypeConverter.h:14
Definition Ast.h:406
A variable definition: "var x = expression;".
Definition Ast.h:231