CompilerSysY/include/visitor.h
2023-05-11 10:19:03 +08:00

165 lines
5.7 KiB
C++

#pragma once
#include "3rdparty/easylogging++.h"
#include "antlrgen/SysyBaseVisitor.h"
#include <any>
#include <cstddef>
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <vector>
#include "antlrgen/SysyLexer.h"
#include "antlrgen/SysyParser.h"
#include "common.h"
#include "llir_module.h"
#include "llir_type.h"
#include "llir_value.h"
#include "llir_instruction.h"
#include "scopetable.h"
namespace antlrSysY {
#pragma region FactoryFunctionDeclaration
std::shared_ptr<ConstantInt> build_ConstantInt(const std::string &name, int value);
std::shared_ptr<BasicBlock> build_BasicBlock(const std::string &name, std::shared_ptr<Function> parent);
std::shared_ptr<InstLoad> build_InstLoad(std::shared_ptr<Value> value, TypePtr_t type,
std::shared_ptr<BasicBlock> parent_bb);
std::shared_ptr<InstStore> build_InstStore(std::shared_ptr<Value> value, std::shared_ptr<Value> pointer,
std::shared_ptr<BasicBlock> parent_bb);
std::shared_ptr<InstAlloca> build_InstAlloca(TypePtr_t type, std::shared_ptr<BasicBlock> parent_bb);
std::shared_ptr<InstBinary> build_InstBinary(InstTag inst_tag, std::shared_ptr<Value> op1, std::shared_ptr<Value> op2,
std::shared_ptr<BasicBlock> parent_bb);
std::shared_ptr<InstZext> build_InstZext(std::shared_ptr<Value> op, std::shared_ptr<BasicBlock> parent_bb);
std::shared_ptr<InstBranch> build_InstBranch(ValuePtr_t cond, BasicBlockPtr_t true_block, BasicBlockPtr_t false_block,
BasicBlockPtr_t parent_bb);
std::shared_ptr<InstBranch> build_InstBranch(BasicBlockPtr_t target_block, BasicBlockPtr_t parent_bb);
std::shared_ptr<InstReturn> build_InstReturn(ValuePtr_t ret_val, BasicBlockPtr_t parent_bb);
std::shared_ptr<InstReturn> build_InstReturn(BasicBlockPtr_t parent_bb);
std::shared_ptr<InstCall> build_InstCall(FunctionPtr_t func, const std::vector<ValuePtr_t> &args,
BasicBlockPtr_t parent_bb);
std::shared_ptr<InstGEP> build_InstGEP(ValuePtr_t pointer, const std::vector<ValuePtr_t> &indices,
BasicBlockPtr_t parent_bb);
#pragma endregion
class Visitor : public antlrSysY::SysyBaseVisitor {
private:
struct VisitorState {
bool isGlobalIint = false;
bool isConstInt = false;
bool isRealParam = false;
bool isCondExp = false;
std::shared_ptr<Function> current_func = {};
std::shared_ptr<BasicBlock> current_bb = {};
std::vector<int> *arr_dim_list = nullptr;
int arr_dim_index = 0;
struct loop_record {
BasicBlockPtr_t cond, body, next;
int id;
};
int loop_stmt_count = 0;
std::vector<loop_record> loop_stack = {};
};
ScopeTable<std::shared_ptr<Value>> _scope_tab;
ScopeTable<std::shared_ptr<Function>> _func_tab; // var can have same name as func
VisitorState _state = {};
inline static std::shared_ptr<ConstantInt> CONST0 = std::make_shared<ConstantInt>("CONST0", 0);
public:
Module module = {};
Visitor(SysyLexer &lexer);
// std::any visitProgram(SysyParser::ProgramContext *ctx) override;
// std::any visitCompUnit(SysyParser::CompUnitContext *ctx) override;
// std::any visitDecl(SysyParser::DeclContext *ctx) override;
std::any visitConstDecl(SysyParser::ConstDeclContext *ctx) override;
// std::any visitVarDecl(SysyParser::VarDeclContext *ctx) override;
std::any visitVarDef(SysyParser::VarDefContext *ctx) override;
std::any visitInitVal(SysyParser::InitValContext *ctx) override;
std::any visitConstDef(SysyParser::ConstDefContext *ctx) override;
std::any visitConstInitVal(SysyParser::ConstInitValContext *ctx) override;
std::any visitConstExp(SysyParser::ConstExpContext *ctx) override;
std::any visitAddExp(SysyParser::AddExpContext *ctx) override;
std::any visitMulExp(SysyParser::MulExpContext *ctx) override;
std::any visitUnaryExp(SysyParser::UnaryExpContext *ctx) override;
std::any visitPrimaryExp(SysyParser::PrimaryExpContext *ctx) override;
// std::any visitExp(SysyParser::ExpContext *ctx) override { }
std::any visitNumber(SysyParser::NumberContext *ctx) override;
std::any visitIntConst(SysyParser::IntConstContext *ctx) override;
std::any visitLVal(SysyParser::LValContext *) override;
std::any visitFuncDef(SysyParser::FuncDefContext *ctx) override;
std::any visitFuncFParams(SysyParser::FuncFParamsContext *ctx) override;
std::any visitFuncFParam(SysyParser::FuncFParamContext *ctx) override;
std::any visitBlock(SysyParser::BlockContext *ctx) override;
// std::any visitBlockItem(SysyParser::BlockItemContext *ctx) override;
std::any visitAssignStmt(SysyParser::AssignStmtContext *ctx) override;
// std::any visitExpStmt(SysyParser::ExpStmtContext *ctx) override;
// std::any visitBlockStmt(SysyParser::BlockStmtContext *ctx) override;
std::any visitIfStmt(SysyParser::IfStmtContext *ctx) override;
std::any visitWhileStmt(SysyParser::WhileStmtContext *ctx) override;
std::any visitBreakStmt(SysyParser::BreakStmtContext *ctx) override;
std::any visitContinueStmt(SysyParser::ContinueStmtContext *ctx) override;
std::any visitReturnStmt(SysyParser::ReturnStmtContext *ctx) override;
std::any visitCond(SysyParser::CondContext *ctx) override;
// std::any visitFuncRParams(SysyParser::FuncRParamsContext *ctx) override;
// std::any visitFuncRParam(SysyParser::FuncRParamContext *ctx) override;
std::any visitRelExp(SysyParser::RelExpContext *ctx) override;
std::any visitEqExp(SysyParser::EqExpContext *ctx) override;
std::any visitLAndExp(SysyParser::LAndExpContext *ctx) override;
std::any visitLOrExp(SysyParser::LOrExpContext *ctx) override;
void llir_gen();
};
} // namespace antlrSysY