From 3191c3ad102760f69f976ad3b318f5cff4e0fa48 Mon Sep 17 00:00:00 2001 From: ridethepig Date: Wed, 24 May 2023 20:40:45 +0800 Subject: [PATCH] rename namespace --- CMakeLists.txt | 4 ++++ include/algos.h | 4 ++-- include/antlrgen/SysyBaseVisitor.h | 4 ++-- include/antlrgen/SysyLexer.h | 4 ++-- include/antlrgen/SysyParser.h | 4 ++-- include/antlrgen/SysyVisitor.h | 4 ++-- include/common.h | 6 +++--- include/llir_instruction.h | 17 +++++++---------- include/llir_module.h | 4 ++-- include/llir_type.h | 24 +++++++++++++----------- include/llir_value.h | 6 +++--- include/pass.h | 4 ++-- include/pass_build_cfg.h | 4 ++-- include/scopetable.h | 4 ++-- include/visitor.h | 6 +++--- scripts/antlr4-gen.sh | 2 +- src/algo_dominance.cpp | 4 ++-- src/antlrgen/SysyBaseVisitor.cpp | 2 +- src/antlrgen/SysyLexer.cpp | 2 +- src/antlrgen/SysyParser.cpp | 2 +- src/antlrgen/SysyVisitor.cpp | 2 +- src/main.cpp | 8 ++++---- src/pass_build_cfg.cpp | 4 ++-- src/pass_mem2reg.cpp | 4 ++-- src/visitor.cpp | 26 +++++++++++++------------- src/visitor_factory.cpp | 4 ++-- src/visitor_llir_gen.cpp | 4 ++-- 27 files changed, 83 insertions(+), 80 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 32313b5..0da17a6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,6 +23,10 @@ clang_format(format ${MY_SOURCES} ${MY_HEADERS}) # clang_format_check(format_check ${MY_SOURCES} ${MY_HEADERS}) # add_compile_options(-fsanitize=address) # add_link_options(-fsanitize=address) +if(CMAKE_BUILD_TYPE STREQUAL "Debug") + add_compile_options(-fstandalone-debug) +endif() + add_executable(sysy ${SOURCES}) # message(STATUS "${SOURCES}") target_link_libraries(sysy antlr4_static) diff --git a/include/algos.h b/include/algos.h index b4eaf0f..ae26110 100644 --- a/include/algos.h +++ b/include/algos.h @@ -3,9 +3,9 @@ #include "llir.h" #include "passes.h" -namespace antlrSysY { +namespace CompSysY { void gen_dominance(FunctionPtr_t func); void gen_dominance_frontier(FunctionPtr_t func); void update_dfs_numbers(BasicBlockPtr_t bb, bool rst); -} // namespace antlrSysY +} // namespace CompSysY diff --git a/include/antlrgen/SysyBaseVisitor.h b/include/antlrgen/SysyBaseVisitor.h index f8d2ece..cfba87b 100644 --- a/include/antlrgen/SysyBaseVisitor.h +++ b/include/antlrgen/SysyBaseVisitor.h @@ -8,7 +8,7 @@ #include "SysyVisitor.h" -namespace antlrSysY { +namespace CompSysY { /** * This class provides an empty implementation of SysyVisitor, which can be @@ -184,4 +184,4 @@ public: }; -} // namespace antlrSysY +} // namespace CompSysY diff --git a/include/antlrgen/SysyLexer.h b/include/antlrgen/SysyLexer.h index e1606f1..0628b9b 100644 --- a/include/antlrgen/SysyLexer.h +++ b/include/antlrgen/SysyLexer.h @@ -7,7 +7,7 @@ #include "antlr4-runtime.h" -namespace antlrSysY { +namespace CompSysY { class SysyLexer : public antlr4::Lexer { @@ -54,4 +54,4 @@ private: }; -} // namespace antlrSysY +} // namespace CompSysY diff --git a/include/antlrgen/SysyParser.h b/include/antlrgen/SysyParser.h index 0703b7a..0c709a2 100644 --- a/include/antlrgen/SysyParser.h +++ b/include/antlrgen/SysyParser.h @@ -9,7 +9,7 @@ #include -namespace antlrSysY { +namespace CompSysY { class SysyParser : public antlr4::Parser { @@ -750,4 +750,4 @@ public: private: }; -} // namespace antlrSysY +} // namespace CompSysY diff --git a/include/antlrgen/SysyVisitor.h b/include/antlrgen/SysyVisitor.h index cf99d93..25d1ebc 100644 --- a/include/antlrgen/SysyVisitor.h +++ b/include/antlrgen/SysyVisitor.h @@ -8,7 +8,7 @@ #include "SysyParser.h" -namespace antlrSysY { +namespace CompSysY { /** * This class defines an abstract visitor for a parse tree @@ -105,4 +105,4 @@ public: }; -} // namespace antlrSysY +} // namespace CompSysY diff --git a/include/common.h b/include/common.h index 58182a5..307f42b 100644 --- a/include/common.h +++ b/include/common.h @@ -2,6 +2,7 @@ #include "3rdparty/easylogging++.h" #include #include +#include #include #include #include @@ -10,7 +11,6 @@ #include #include #include -#include #define uptr(type) std::unique_ptr #define sptr(type) std::shared_ptr @@ -36,7 +36,7 @@ inline sptr(DST) shared_cast(SRC src) { class type; \ typedef std::shared_ptr type##Ptr_t -namespace antlrSysY { +namespace CompSysY { template std::ostream &operator<<(typename std::enable_if::value, std::ostream>::type &stream, const T &e) { @@ -63,4 +63,4 @@ private: std::string message; }; -} // namespace antlrSysY \ No newline at end of file +} // namespace CompSysY \ No newline at end of file diff --git a/include/llir_instruction.h b/include/llir_instruction.h index 796d83d..8cc09a4 100644 --- a/include/llir_instruction.h +++ b/include/llir_instruction.h @@ -6,7 +6,7 @@ #include #include -namespace antlrSysY { +namespace CompSysY { DEF_PTR_T(InstAlloca); DEF_PTR_T(InstStore); @@ -238,21 +238,18 @@ public: // get the inner static TypePtr_t extract_type(ValuePtr_t pointer, const std::vector &indices) { - auto pointer_type = std::dynamic_pointer_cast(pointer->type); - auto pointed_type = pointer_type->pointed_type; - if (pointed_type->type_tag == Type::TypeTag::IntegerType) { + auto pointed_type = shared_cast(pointer->type)->pointed_type; + if (Type::isType(pointed_type)) { return pointed_type; } else if (Type::isType(pointed_type)) { for (int i = 1; i < indices.size(); ++i) { - pointed_type = std::dynamic_pointer_cast(pointed_type)->element_type; + pointed_type = shared_cast(pointed_type)->element_type; } return pointed_type; } - else { - LOG(ERROR) << "unexpected type: " << pointer->to_string(); - assert(0); - } + LOG(ERROR) << "unexpected type: " << pointer->to_string(); + assert(0); } }; @@ -278,4 +275,4 @@ public: } }; -} // namespace antlrSysY \ No newline at end of file +} // namespace CompSysY \ No newline at end of file diff --git a/include/llir_module.h b/include/llir_module.h index 9a45138..16581ca 100644 --- a/include/llir_module.h +++ b/include/llir_module.h @@ -5,10 +5,10 @@ #include #include -namespace antlrSysY { +namespace CompSysY { struct Module { std::vector> function_list; std::vector> global_var_list; std::vector> instruction_list; }; -} // namespace antlrSysY \ No newline at end of file +} // namespace CompSysY \ No newline at end of file diff --git a/include/llir_type.h b/include/llir_type.h index b18fd9a..ddedf57 100644 --- a/include/llir_type.h +++ b/include/llir_type.h @@ -6,7 +6,7 @@ #include #include -namespace antlrSysY { +namespace CompSysY { DEF_PTR_T(Type); DEF_PTR_T(IntegerType); @@ -68,6 +68,15 @@ public: bool isI1() { return int_type == IntTypes::I1; } + static bool isIntegerType(TypePtr_t type) { + return (type->type_tag == Type::TypeTag::IntegerType); + } + static bool isIntegerTypeI32(TypePtr_t type) { + return isIntegerType(type) && std::dynamic_pointer_cast(type)->isI32(); + } + static bool isIntegerTypeI1(TypePtr_t &type) { + return isIntegerType(type) && std::dynamic_pointer_cast(type)->isI1(); + } virtual std::string to_IR_string() override { if (isI1()) return "i1"; @@ -94,15 +103,6 @@ public: class TypeHelper { public: - static bool isIntegerType(TypePtr_t type) { - return (type->type_tag == Type::TypeTag::IntegerType); - } - static bool isIntegerTypeI32(TypePtr_t type) { - return isIntegerType(type) && std::dynamic_pointer_cast(type)->isI32(); - } - static bool isIntegerTypeI1(TypePtr_t &type) { - return isIntegerType(type) && std::dynamic_pointer_cast(type)->isI1(); - } inline static TypePtr_t TYPE_I32 = std::make_shared(); inline static TypePtr_t TYPE_I1 = std::make_shared(IntegerType::IntTypes::I1); inline static TypePtr_t TYPE_LABEL = std::make_shared(); @@ -166,4 +166,6 @@ public: } }; -} // namespace antlrSysY \ No newline at end of file +TypePtr_t pointed_type(TypePtr_t ptr); + +} // namespace CompSysY \ No newline at end of file diff --git a/include/llir_value.h b/include/llir_value.h index c522657..3e19d22 100644 --- a/include/llir_value.h +++ b/include/llir_value.h @@ -12,7 +12,7 @@ #include #include -namespace antlrSysY { +namespace CompSysY { DEF_PTR_T(Value); DEF_PTR_T(BasicBlock); @@ -130,7 +130,7 @@ public: } }; -typedef std::list>::iterator BasicBlockListNode_t; +typedef std::list>::iterator BasicBlockListNode_t; class Function : public Value { public: @@ -259,4 +259,4 @@ std::shared_ptr gen_arr_hierarchy( int length ); -} // namespace antlrSysY \ No newline at end of file +} // namespace CompSysY \ No newline at end of file diff --git a/include/pass.h b/include/pass.h index 826ff3d..05b09b9 100644 --- a/include/pass.h +++ b/include/pass.h @@ -3,7 +3,7 @@ #include "common.h" #include "llir_module.h" -namespace antlrSysY { +namespace CompSysY { class Pass { public: std::string pass_name; @@ -18,4 +18,4 @@ public: virtual void run(const Module &module) override; }; -} // namespace antlrSysY \ No newline at end of file +} // namespace CompSysY \ No newline at end of file diff --git a/include/pass_build_cfg.h b/include/pass_build_cfg.h index 77c94e9..2160cc1 100644 --- a/include/pass_build_cfg.h +++ b/include/pass_build_cfg.h @@ -3,10 +3,10 @@ #include "llir_module.h" #include "passes.h" -namespace antlrSysY { +namespace CompSysY { class PassBuildCFG : public Pass { public: PassBuildCFG() : Pass("build_cfg") {} virtual void run(const Module &module) override; }; -} // namespace antlrSysY \ No newline at end of file +} // namespace CompSysY \ No newline at end of file diff --git a/include/scopetable.h b/include/scopetable.h index b19e652..692be0f 100644 --- a/include/scopetable.h +++ b/include/scopetable.h @@ -1,7 +1,7 @@ #pragma once #include "common.h" -namespace antlrSysY { +namespace CompSysY { template class ScopeTable { typedef std::map ScopeTableEntry_t; @@ -58,4 +58,4 @@ public: return _scope_list.size() - 1; } }; -} // namespace antlrSysY \ No newline at end of file +} // namespace CompSysY \ No newline at end of file diff --git a/include/visitor.h b/include/visitor.h index cb6f8e0..dbb19cb 100644 --- a/include/visitor.h +++ b/include/visitor.h @@ -18,7 +18,7 @@ #include #include -namespace antlrSysY { +namespace CompSysY { #pragma region FactoryFunctionDeclaration std::shared_ptr build_ConstantInt(const std::string &name, int value); @@ -90,7 +90,7 @@ InstPhiPtr_t build_InstPhi( #pragma endregion -class Visitor : public antlrSysY::SysyBaseVisitor { +class Visitor : public SysyBaseVisitor { private: struct VisitorState { bool isGlobalIint = false; @@ -198,4 +198,4 @@ public: void llir_gen(std::ostream &); }; -} // namespace antlrSysY \ No newline at end of file +} // namespace CompSysY \ No newline at end of file diff --git a/scripts/antlr4-gen.sh b/scripts/antlr4-gen.sh index be4afd2..9e6687a 100755 --- a/scripts/antlr4-gen.sh +++ b/scripts/antlr4-gen.sh @@ -5,7 +5,7 @@ mkdir -p include mkdir -p src rm -fv build/antlr-gen/* cp $filename build/antlr-gen/ -java -jar ./tools/antlr-4.12.0-complete.jar -no-listener -visitor -Dlanguage=Cpp -package antlrSysY -o build/antlr-gen $filename +java -jar ./tools/antlr-4.12.0-complete.jar -no-listener -visitor -Dlanguage=Cpp -package CompSysY -o build/antlr-gen $filename mkdir -p src/antlrgen mkdir -p include/antlrgen cp build/antlr-gen/*.cpp src/antlrgen/ diff --git a/src/algo_dominance.cpp b/src/algo_dominance.cpp index f5768c8..ac1a3c9 100644 --- a/src/algo_dominance.cpp +++ b/src/algo_dominance.cpp @@ -1,7 +1,7 @@ #include "llir.h" #include "visitor.h" -namespace antlrSysY { +namespace CompSysY { static void _bitwise_and(std::vector &op1, const std::vector &op2) { for (int i = 0; i < op1.size(); ++i) { @@ -134,4 +134,4 @@ void update_dfs_numbers(BasicBlockPtr_t bb, bool rst) { bb->dom_dfs_out = dfs_num++; } -} // namespace antlrSysY \ No newline at end of file +} // namespace CompSysY \ No newline at end of file diff --git a/src/antlrgen/SysyBaseVisitor.cpp b/src/antlrgen/SysyBaseVisitor.cpp index fe1e55c..4ba353d 100644 --- a/src/antlrgen/SysyBaseVisitor.cpp +++ b/src/antlrgen/SysyBaseVisitor.cpp @@ -5,5 +5,5 @@ #include "SysyBaseVisitor.h" -using namespace antlrSysY; +using namespace CompSysY; diff --git a/src/antlrgen/SysyLexer.cpp b/src/antlrgen/SysyLexer.cpp index 7429cbf..2f5c23f 100644 --- a/src/antlrgen/SysyLexer.cpp +++ b/src/antlrgen/SysyLexer.cpp @@ -7,7 +7,7 @@ using namespace antlr4; -using namespace antlrSysY; +using namespace CompSysY; using namespace antlr4; diff --git a/src/antlrgen/SysyParser.cpp b/src/antlrgen/SysyParser.cpp index 24874e8..1a39b68 100644 --- a/src/antlrgen/SysyParser.cpp +++ b/src/antlrgen/SysyParser.cpp @@ -8,7 +8,7 @@ using namespace antlrcpp; -using namespace antlrSysY; +using namespace CompSysY; using namespace antlr4; diff --git a/src/antlrgen/SysyVisitor.cpp b/src/antlrgen/SysyVisitor.cpp index de2868f..203edba 100644 --- a/src/antlrgen/SysyVisitor.cpp +++ b/src/antlrgen/SysyVisitor.cpp @@ -5,5 +5,5 @@ #include "SysyVisitor.h" -using namespace antlrSysY; +using namespace CompSysY; diff --git a/src/main.cpp b/src/main.cpp index e507fbe..53a1d5a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,9 +6,9 @@ #include "antlrgen/SysyParser.h" #include "common.h" #include "llir.h" +#include "machcode.h" #include "passes.h" #include "visitor.h" -#include "machcode.h" #include <3rdparty/argparse.hpp> #include #include @@ -17,9 +17,9 @@ INITIALIZE_EASYLOGGINGPP -using namespace antlrSysY; +using namespace CompSysY; using namespace antlr4; -using namespace codegen; +using namespace CompSysY; class AbortErrorListener : public BaseErrorListener { public: @@ -127,7 +127,7 @@ int main(int argc, const char **argv) { // std::cout << tree->toStringTree(&parser) << std::endl << std::endl; MCModule mc_module; - mc_module.IR2MC(visitor.module); + // mc_module.IR2MC(visitor.module); return 0; } \ No newline at end of file diff --git a/src/pass_build_cfg.cpp b/src/pass_build_cfg.cpp index ca25ccf..6f982d2 100644 --- a/src/pass_build_cfg.cpp +++ b/src/pass_build_cfg.cpp @@ -4,7 +4,7 @@ #include "passes.h" #include -namespace antlrSysY { +namespace CompSysY { static void print_cfg_info(BasicBlockPtr_t bb) { std::cout << "---" << bb->name << "---\n"; @@ -94,4 +94,4 @@ void PassBuildCFG::run(const Module &module) { } } -} // namespace antlrSysY \ No newline at end of file +} // namespace CompSysY \ No newline at end of file diff --git a/src/pass_mem2reg.cpp b/src/pass_mem2reg.cpp index f1ed4be..3da947f 100644 --- a/src/pass_mem2reg.cpp +++ b/src/pass_mem2reg.cpp @@ -9,7 +9,7 @@ #include #include -namespace antlrSysY { +namespace CompSysY { struct AllocaInfo { std::vector def_blocks = {}; @@ -329,4 +329,4 @@ void PassMem2Reg::run(const Module &module) { } } -} // namespace antlrSysY \ No newline at end of file +} // namespace CompSysY \ No newline at end of file diff --git a/src/visitor.cpp b/src/visitor.cpp index dfaa86e..6718663 100644 --- a/src/visitor.cpp +++ b/src/visitor.cpp @@ -13,7 +13,7 @@ // nothing // beyond base class implement -namespace antlrSysY { +namespace CompSysY { #define ANY2VALUE(_my_type_) \ if (fuck_any.type() == typeid(std::shared_ptr<_my_type_>)) { \ @@ -340,7 +340,7 @@ std::any Visitor::visitConstInitVal(SysyParser::ConstInitValContext *ctx) { if (init_val->constExp()) { // evaluate to const int auto const_value = any_to_Value(visitConstInitVal(init_val)); - sysy_assert(TypeHelper::isIntegerType(const_value->type)); + sysy_assert(shared_cast(const_value->type)); cur_arr.push_back(const_value); } else { @@ -468,7 +468,7 @@ std::any Visitor::visitRelExp(SysyParser::RelExpContext *ctx) { if (ctx->relExp()) { auto rel_exp = any_to_Value(visitRelExp(ctx->relExp())); auto add_exp = any_to_Value(visitAddExp(ctx->addExp())); - sysy_assert(TypeHelper::isIntegerTypeI32(rel_exp->type)); + sysy_assert(IntegerType::isIntegerTypeI32(rel_exp->type)); if (ctx->LE()) { add_exp = build_InstBinary(InstTag::Le, rel_exp, add_exp, _state.current_bb); } @@ -498,13 +498,13 @@ std::any Visitor::visitEqExp(SysyParser::EqExpContext *ctx) { bool need_zext = false; auto eq_exp = any_to_Value(visitEqExp(ctx->eqExp())); auto rel_exp = any_to_Value(visitRelExp(ctx->relExp())); - if (TypeHelper::isIntegerTypeI32(eq_exp->type) || TypeHelper::isIntegerTypeI32(rel_exp->type)) { + if (IntegerType::isIntegerTypeI32(eq_exp->type) || IntegerType::isIntegerTypeI32(rel_exp->type)) { need_zext = true; } - if (need_zext && TypeHelper::isIntegerTypeI1(eq_exp->type)) { + if (need_zext && IntegerType::isIntegerTypeI1(eq_exp->type)) { eq_exp = build_InstZext(eq_exp, _state.current_bb); } - if (need_zext && TypeHelper::isIntegerTypeI1(rel_exp->type)) { + if (need_zext && IntegerType::isIntegerTypeI1(rel_exp->type)) { rel_exp = build_InstZext(rel_exp, _state.current_bb); } if (ctx->EQ()) { @@ -534,7 +534,7 @@ std::any Visitor::visitLAndExp(SysyParser::LAndExpContext *ctx) { auto next_block = build_BasicBlock("", _state.current_func, _state.current_bb->itr); auto eq_exp = any_to_Value(visitEqExp(eq_exp_list[i])); auto condition = eq_exp; - if (!TypeHelper::isIntegerTypeI1(eq_exp->type)) + if (!IntegerType::isIntegerTypeI1(eq_exp->type)) condition = build_InstBinary(InstTag::Ne, eq_exp, CONST0, _state.current_bb); build_InstBranch(condition, next_block, ctx->false_block, _state.current_bb); _state.current_bb = next_block; @@ -797,7 +797,7 @@ std::any Visitor::visitLVal(SysyParser::LValContext *ctx) { auto inst_add = build_InstBinary(InstTag::Add, offset, exp_val, _state.current_bb); offset = inst_add; // finally, we get the offset pointed_type = std::dynamic_pointer_cast(ptr->type)->pointed_type; - if (TypeHelper::isIntegerType(pointed_type)) { + if (shared_cast(pointed_type)) { // return the address of the array element auto arr_elem_ptr = build_InstGEP(ptr, {offset}, _state.current_bb); return arr_elem_ptr; @@ -811,10 +811,10 @@ std::any Visitor::visitLVal(SysyParser::LValContext *ctx) { // for array type in fparam, say a[][5][4], the type looks like: // alloca->pointer->array[5x4]->array[4] auto exp_list = ctx->exp(); - auto inst_load = build_InstLoad(lval, lval_pointed_type, _state.current_bb); + auto inst_load = build_InstLoad(lval, lval_pointed_type, _state.current_bb); ValuePtr_t ptr = inst_load; - auto exp_val = any_to_Value(visitExp(exp_list.front())); - ptr = build_InstGEP(ptr, {exp_val}, _state.current_bb); + auto exp_val = any_to_Value(visitExp(exp_list.front())); + ptr = build_InstGEP(ptr, {exp_val}, _state.current_bb); for (int i = 1; i < exp_list.size(); ++i) { auto exp_val = any_to_Value(visitExp(exp_list[i])); ptr = build_InstGEP(ptr, {CONST0, exp_val}, _state.current_bb); @@ -849,7 +849,7 @@ std::any Visitor::visitLVal(SysyParser::LValContext *ctx) { auto inst_add = build_InstBinary(InstTag::Add, offset, exp_val, _state.current_bb); offset = inst_add; // finally, we get the offset pointed_type = std::dynamic_pointer_cast(ptr->type)->pointed_type; - if (TypeHelper::isIntegerType(pointed_type)) { + if (shared_cast(pointed_type)) { // return the address of the array element auto arr_elem_ptr = build_InstGEP(ptr, {offset}, _state.current_bb); return arr_elem_ptr; @@ -1066,4 +1066,4 @@ std::any Visitor::visitReturnStmt(SysyParser::ReturnStmtContext *ctx) { return {}; } -} // namespace antlrSysY \ No newline at end of file +} // namespace CompSysY \ No newline at end of file diff --git a/src/visitor_factory.cpp b/src/visitor_factory.cpp index 7ac5451..0c627f2 100644 --- a/src/visitor_factory.cpp +++ b/src/visitor_factory.cpp @@ -4,7 +4,7 @@ #include #include -namespace antlrSysY { +namespace CompSysY { std::shared_ptr build_ConstantInt(const std::string &name, int value) { auto lala = std::make_shared(name, value); @@ -152,4 +152,4 @@ InstPhiPtr_t build_InstPhi( return inst; } -} // namespace antlrSysY \ No newline at end of file +} // namespace CompSysY \ No newline at end of file diff --git a/src/visitor_llir_gen.cpp b/src/visitor_llir_gen.cpp index ac09aed..82c4127 100644 --- a/src/visitor_llir_gen.cpp +++ b/src/visitor_llir_gen.cpp @@ -9,7 +9,7 @@ #include #include -namespace antlrSysY { +namespace CompSysY { const std::array libfunc_list = {"getint", "getch", "getarray", "putint", "putch", "putarray", "starttime", "stoptime"}; @@ -562,4 +562,4 @@ void Visitor::llir_gen(std::ostream &ostr) { #pragma endregion } -} // namespace antlrSysY \ No newline at end of file +} // namespace CompSysY \ No newline at end of file