#pragma once #include "3rdparty/easylogging++.h" #include "antlrgen/SysyBaseVisitor.h" #include "antlrgen/SysyLexer.h" #include "antlrgen/SysyParser.h" #include "common.h" #include "llir_instruction.h" #include "llir_module.h" #include "llir_type.h" #include "llir_value.h" #include "scopetable.h" #include #include #include #include #include #include #include #include namespace CompSysY { #pragma region FactoryFunctionDeclaration std::shared_ptr build_ConstantInt(const std::string &name, int value); std::shared_ptr build_BasicBlock( const std::string &name, std::shared_ptr parent, const BasicBlockListNode_t &cur_bb_itr ); std::shared_ptr build_InstLoad( std::shared_ptr value, TypePtr_t type, std::shared_ptr parent_bb ); std::shared_ptr build_InstStore( std::shared_ptr value, std::shared_ptr pointer, std::shared_ptr parent_bb ); std::shared_ptr build_InstAlloca( const std::string &str, TypePtr_t type, std::shared_ptr parent_bb ); std::shared_ptr build_InstBinary( InstTag inst_tag, std::shared_ptr op1, std::shared_ptr op2, std::shared_ptr parent_bb ); std::shared_ptr build_InstZext(std::shared_ptr op, std::shared_ptr parent_bb); std::shared_ptr build_InstBranch( ValuePtr_t cond, BasicBlockPtr_t true_block, BasicBlockPtr_t false_block, BasicBlockPtr_t parent_bb ); std::shared_ptr build_InstBranch(BasicBlockPtr_t target_block, BasicBlockPtr_t parent_bb); std::shared_ptr build_InstReturn(ValuePtr_t ret_val, BasicBlockPtr_t parent_bb); std::shared_ptr build_InstReturn(BasicBlockPtr_t parent_bb); std::shared_ptr build_InstCall( FunctionPtr_t func, const std::vector &args, BasicBlockPtr_t parent_bb ); std::shared_ptr build_InstGEP( ValuePtr_t pointer, const std::vector &indices, BasicBlockPtr_t parent_bb ); InstPhiPtr_t build_InstPhi( TypePtr_t type, const decltype(Function::bb_list) &incoming_vals, BasicBlockPtr_t parent_bb, const std::string &name ); #pragma endregion class Visitor : public SysyBaseVisitor { private: struct VisitorState { bool isGlobalIint = false; bool isConstInt = false; bool isRealParam = false; bool isCondExp = false; std::shared_ptr current_func = {}; std::shared_ptr current_bb = {}; std::vector *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_stack = {}; }; ScopeTable> _scope_tab; ScopeTable> _func_tab; // var can have same name as func VisitorState _state = {}; inline static std::shared_ptr CONST0 = std::make_shared("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(std::ostream &); }; } // namespace CompSysY