From 77719e07e7247f9fb16f37561d134ca58f122346 Mon Sep 17 00:00:00 2001 From: ZhuangYumin Date: Wed, 23 Oct 2024 09:25:13 +0000 Subject: [PATCH] slightly optimize and refactor --- include/IR/IRBuilder.h | 2 +- include/IR/IR_basic.h | 5 ++--- include/ast/astnode.h | 6 +----- include/ast/scope.hpp | 9 ++------- include/ast/semantic_visitor.h | 2 +- include/ast/structural_astnode.h | 16 +++------------- src/opt/regalloc.cpp | 1 + 7 files changed, 11 insertions(+), 30 deletions(-) diff --git a/include/IR/IRBuilder.h b/include/IR/IRBuilder.h index 4e91ff0..90c5d58 100644 --- a/include/IR/IRBuilder.h +++ b/include/IR/IRBuilder.h @@ -8,7 +8,7 @@ #include "ast/scope.hpp" #include "tools.h" class IRBuilder : public ASTNodeVirturalVisitor { - friend std::shared_ptr BuildIR(std::shared_ptr src); + public: std::shared_ptr prog; std::shared_ptr cur_class; std::shared_ptr cur_func; diff --git a/include/IR/IR_basic.h b/include/IR/IR_basic.h index d3b5a6f..14915c9 100644 --- a/include/IR/IR_basic.h +++ b/include/IR/IR_basic.h @@ -20,7 +20,7 @@ class LLVMIRItemBase { }; class TypeDefItem : public LLVMIRItemBase { - friend class IRBuilder; + public: std::string class_name_raw; std::vector elements; @@ -411,8 +411,7 @@ class FunctionDefItem : public LLVMIRItemBase { } }; class FunctionDeclareItem : public LLVMIRItemBase { - friend class IRBuilder; - friend std::shared_ptr BuildIR(std::shared_ptr src); + public: LLVMType return_type; std::string func_name_raw; std::vector args; diff --git a/include/ast/astnode.h b/include/ast/astnode.h index e90edfe..278d8ba 100644 --- a/include/ast/astnode.h +++ b/include/ast/astnode.h @@ -8,11 +8,7 @@ #include "tools.h" class ASTNodeBase { - friend Visitor; - friend std::shared_ptr CheckAndDecorate(std::shared_ptr src); - friend std::shared_ptr BuildIR(std::shared_ptr src); - - protected: + public: std::shared_ptr current_scope; ASTNodeType type; // std::vector> children; diff --git a/include/ast/scope.hpp b/include/ast/scope.hpp index bc34b5f..e9bcc44 100644 --- a/include/ast/scope.hpp +++ b/include/ast/scope.hpp @@ -88,10 +88,7 @@ struct FunctionSchema { std::vector> arguments; }; class FunctionScope : public ScopeBase { - friend std::shared_ptr CheckAndDecorate(std::shared_ptr src); - friend class Visitor; - friend class ASTSemanticCheckVisitor; - friend class GlobalScope; + public: FunctionSchema schema; bool add_variable([[maybe_unused]] const std::string &name, [[maybe_unused]] const ExprTypeInfo &type) override { throw std::runtime_error("FunctionScope does not support add_variable"); @@ -132,9 +129,7 @@ class FunctionScope : public ScopeBase { } }; class ClassDefScope : public ScopeBase { - friend class Visitor; - friend class GlobalScope; - friend std::shared_ptr CheckAndDecorate(std::shared_ptr src); + public: std::unordered_map member_variables; std::unordered_map> member_functions; IRClassInfo llvm_class_info; diff --git a/include/ast/semantic_visitor.h b/include/ast/semantic_visitor.h index 928ba45..2b22378 100644 --- a/include/ast/semantic_visitor.h +++ b/include/ast/semantic_visitor.h @@ -6,6 +6,7 @@ #include "structural_astnode.h" class ASTSemanticCheckVisitor : public ASTNodeVirturalVisitor { + public: bool is_in_func_def; bool has_return; FunctionSchema cur_func_schema; @@ -13,7 +14,6 @@ class ASTSemanticCheckVisitor : public ASTNodeVirturalVisitor { bool is_in_class_def; size_t loop_level; std::shared_ptr global_scope; - friend std::shared_ptr CheckAndDecorate(std::shared_ptr src); bool ClassExists(const std::string &name) { if (name == "int" || name == "bool") return true; diff --git a/include/ast/structural_astnode.h b/include/ast/structural_astnode.h index 35d250e..d936df1 100644 --- a/include/ast/structural_astnode.h +++ b/include/ast/structural_astnode.h @@ -8,9 +8,7 @@ #include "expr_astnode.h" #include "statement_astnode.h" class FuncDef_ASTNode : public ASTNodeBase { - friend Visitor; - friend class ASTSemanticCheckVisitor; - friend class IRBuilder; + public: bool is_constructor; IdentifierType func_name; ExprTypeInfo return_type; @@ -22,11 +20,7 @@ class FuncDef_ASTNode : public ASTNodeBase { virtual void accept(class ASTNodeVisitorBase *visitor) override; }; class ClassDef_ASTNode : public ASTNodeBase { - friend Visitor; - friend class ASTSemanticCheckVisitor; - friend class IRBuilder; - - private: + public: std::string class_name; std::vector> member_variables; std::vector> member_functions; @@ -38,16 +32,12 @@ class ClassDef_ASTNode : public ASTNodeBase { virtual void accept(class ASTNodeVisitorBase *visitor) override; }; class Program_ASTNode : public ASTNodeBase { - friend std::shared_ptr BuildIR(std::shared_ptr src); - friend Visitor; - friend class ASTSemanticCheckVisitor; - friend class IRBuilder; + public: std::vector> global_variables; std::vector> classes; std::vector> functions; std::vector> sorted_children; - public: Program_ASTNode() = default; virtual void accept(class ASTNodeVisitorBase *visitor) override; }; diff --git a/src/opt/regalloc.cpp b/src/opt/regalloc.cpp index 2cdf714..d6af305 100644 --- a/src/opt/regalloc.cpp +++ b/src/opt/regalloc.cpp @@ -372,6 +372,7 @@ void ConductRegAllocForFunction(std::shared_ptr func) { } while (ConductColoring(func, cfg, confgraph)); TranslateColorResult(func, cfg, confgraph); RemoveCallingConventionKeeper(func, cfg, confgraph); + PairMoveEliminate(func, cfg, confgraph); // func->RecursivePrint(std::cerr); }