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
35public:
36 CgVisitor(std::shared_ptr<sammine_lang::LLVMRes> resPtr)
37 : resPtr(resPtr), type_converter(*resPtr->Context.get()),
38 jasmine(*resPtr->Module.get(), *resPtr->Context.get(),
39 *resPtr->Builder.get()) {
40 assert(this->resPtr);
41 }
42 llvm::AllocaInst *CreateEntryBlockAlloca(llvm::Function *TheFunction,
43 const std::string &VarName,
44 llvm::Type *);
45
46 void enter_new_scope() override;
47 void exit_new_scope() override;
48
49 virtual void visit(FuncDefAST *) override;
50 virtual void visit(IfExprAST *ast) override;
51 // visit
52 // pre order
53 // TODO: Implement these
54 virtual void preorder_walk(ProgramAST *ast) override;
55 virtual void preorder_walk(VarDefAST *ast) override;
56 virtual void preorder_walk(FuncDefAST *ast) override;
57 virtual void preorder_walk(RecordDefAST *ast) override;
58 virtual void preorder_walk(ExternAST *ast) override;
59 virtual void preorder_walk(PrototypeAST *ast) override;
60 virtual void preorder_walk(CallExprAST *ast) override;
61 virtual void preorder_walk(ReturnExprAST *ast) override {}
62 virtual void preorder_walk(BinaryExprAST *ast) override;
63 virtual void preorder_walk(NumberExprAST *ast) override;
64 virtual void preorder_walk(StringExprAST *ast) override;
65 virtual void preorder_walk(BoolExprAST *ast) override;
66 virtual void preorder_walk(VariableExprAST *ast) override;
67 virtual void preorder_walk(BlockAST *ast) override;
68 virtual void preorder_walk(IfExprAST *ast) override;
69 virtual void preorder_walk(UnitExprAST *ast) override;
70 virtual void preorder_walk(TypedVarAST *ast) override;
71
72 // post order
73 // TODO: Implement these?
74 virtual void postorder_walk(ProgramAST *ast) override {}
75 virtual void postorder_walk(VarDefAST *ast) override;
76 virtual void postorder_walk(ExternAST *ast) override {}
77 virtual void postorder_walk(FuncDefAST *ast) override;
78 virtual void postorder_walk(RecordDefAST *ast) override;
79 virtual void postorder_walk(PrototypeAST *ast) override {}
80 virtual void postorder_walk(CallExprAST *ast) override {}
81 virtual void postorder_walk(ReturnExprAST *ast) override;
82 virtual void postorder_walk(BinaryExprAST *ast) override;
83 virtual void postorder_walk(NumberExprAST *ast) override {}
84 virtual void postorder_walk(StringExprAST *ast) override {}
85 virtual void postorder_walk(BoolExprAST *ast) override {}
86 virtual void postorder_walk(VariableExprAST *ast) override {}
87 virtual void postorder_walk(BlockAST *ast) override;
88 virtual void postorder_walk(IfExprAST *ast) override {}
89 virtual void postorder_walk(UnitExprAST *ast) override {}
90 virtual void postorder_walk(TypedVarAST *ast) override {}
91};
92} // namespace sammine_lang::AST
Defines the AST Abstract class for printing out AST Nodes.
Holds declaration for all the AST Nodes.
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:50
llvm::AllocaInst * CreateEntryBlockAlloca(llvm::Function *TheFunction, const std::string &VarName, llvm::Type *)
Definition CodegenVisitor.cpp:32
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 AstBase.h:145
Definition TypeConverter.h:13
Definition Ast.h:406
A variable definition: "var x = expression;".
Definition Ast.h:231