slightly optimize and refactor

This commit is contained in:
2024-10-23 09:25:13 +00:00
parent c6e529ec0f
commit 77719e07e7
7 changed files with 11 additions and 30 deletions

View File

@ -8,7 +8,7 @@
#include "ast/scope.hpp" #include "ast/scope.hpp"
#include "tools.h" #include "tools.h"
class IRBuilder : public ASTNodeVirturalVisitor { class IRBuilder : public ASTNodeVirturalVisitor {
friend std::shared_ptr<ModuleItem> BuildIR(std::shared_ptr<Program_ASTNode> src); public:
std::shared_ptr<ModuleItem> prog; std::shared_ptr<ModuleItem> prog;
std::shared_ptr<TypeDefItem> cur_class; std::shared_ptr<TypeDefItem> cur_class;
std::shared_ptr<FunctionDefItem> cur_func; std::shared_ptr<FunctionDefItem> cur_func;

View File

@ -20,7 +20,7 @@ class LLVMIRItemBase {
}; };
class TypeDefItem : public LLVMIRItemBase { class TypeDefItem : public LLVMIRItemBase {
friend class IRBuilder; public:
std::string class_name_raw; std::string class_name_raw;
std::vector<LLVMType> elements; std::vector<LLVMType> elements;
@ -411,8 +411,7 @@ class FunctionDefItem : public LLVMIRItemBase {
} }
}; };
class FunctionDeclareItem : public LLVMIRItemBase { class FunctionDeclareItem : public LLVMIRItemBase {
friend class IRBuilder; public:
friend std::shared_ptr<class ModuleItem> BuildIR(std::shared_ptr<Program_ASTNode> src);
LLVMType return_type; LLVMType return_type;
std::string func_name_raw; std::string func_name_raw;
std::vector<LLVMType> args; std::vector<LLVMType> args;

View File

@ -8,11 +8,7 @@
#include "tools.h" #include "tools.h"
class ASTNodeBase { class ASTNodeBase {
friend Visitor; public:
friend std::shared_ptr<Program_ASTNode> CheckAndDecorate(std::shared_ptr<Program_ASTNode> src);
friend std::shared_ptr<class ModuleItem> BuildIR(std::shared_ptr<Program_ASTNode> src);
protected:
std::shared_ptr<ScopeBase> current_scope; std::shared_ptr<ScopeBase> current_scope;
ASTNodeType type; ASTNodeType type;
// std::vector<std::shared_ptr<ASTNodeBase>> children; // std::vector<std::shared_ptr<ASTNodeBase>> children;

View File

@ -88,10 +88,7 @@ struct FunctionSchema {
std::vector<std::pair<ExprTypeInfo, std::string>> arguments; std::vector<std::pair<ExprTypeInfo, std::string>> arguments;
}; };
class FunctionScope : public ScopeBase { class FunctionScope : public ScopeBase {
friend std::shared_ptr<class Program_ASTNode> CheckAndDecorate(std::shared_ptr<class Program_ASTNode> src); public:
friend class Visitor;
friend class ASTSemanticCheckVisitor;
friend class GlobalScope;
FunctionSchema schema; FunctionSchema schema;
bool add_variable([[maybe_unused]] const std::string &name, [[maybe_unused]] const ExprTypeInfo &type) override { 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"); throw std::runtime_error("FunctionScope does not support add_variable");
@ -132,9 +129,7 @@ class FunctionScope : public ScopeBase {
} }
}; };
class ClassDefScope : public ScopeBase { class ClassDefScope : public ScopeBase {
friend class Visitor; public:
friend class GlobalScope;
friend std::shared_ptr<Program_ASTNode> CheckAndDecorate(std::shared_ptr<Program_ASTNode> src);
std::unordered_map<std::string, ExprTypeInfo> member_variables; std::unordered_map<std::string, ExprTypeInfo> member_variables;
std::unordered_map<std::string, std::shared_ptr<FunctionScope>> member_functions; std::unordered_map<std::string, std::shared_ptr<FunctionScope>> member_functions;
IRClassInfo llvm_class_info; IRClassInfo llvm_class_info;

View File

@ -6,6 +6,7 @@
#include "structural_astnode.h" #include "structural_astnode.h"
class ASTSemanticCheckVisitor : public ASTNodeVirturalVisitor { class ASTSemanticCheckVisitor : public ASTNodeVirturalVisitor {
public:
bool is_in_func_def; bool is_in_func_def;
bool has_return; bool has_return;
FunctionSchema cur_func_schema; FunctionSchema cur_func_schema;
@ -13,7 +14,6 @@ class ASTSemanticCheckVisitor : public ASTNodeVirturalVisitor {
bool is_in_class_def; bool is_in_class_def;
size_t loop_level; size_t loop_level;
std::shared_ptr<GlobalScope> global_scope; std::shared_ptr<GlobalScope> global_scope;
friend std::shared_ptr<Program_ASTNode> CheckAndDecorate(std::shared_ptr<Program_ASTNode> src);
bool ClassExists(const std::string &name) { bool ClassExists(const std::string &name) {
if (name == "int" || name == "bool") return true; if (name == "int" || name == "bool") return true;

View File

@ -8,9 +8,7 @@
#include "expr_astnode.h" #include "expr_astnode.h"
#include "statement_astnode.h" #include "statement_astnode.h"
class FuncDef_ASTNode : public ASTNodeBase { class FuncDef_ASTNode : public ASTNodeBase {
friend Visitor; public:
friend class ASTSemanticCheckVisitor;
friend class IRBuilder;
bool is_constructor; bool is_constructor;
IdentifierType func_name; IdentifierType func_name;
ExprTypeInfo return_type; ExprTypeInfo return_type;
@ -22,11 +20,7 @@ class FuncDef_ASTNode : public ASTNodeBase {
virtual void accept(class ASTNodeVisitorBase *visitor) override; virtual void accept(class ASTNodeVisitorBase *visitor) override;
}; };
class ClassDef_ASTNode : public ASTNodeBase { class ClassDef_ASTNode : public ASTNodeBase {
friend Visitor; public:
friend class ASTSemanticCheckVisitor;
friend class IRBuilder;
private:
std::string class_name; std::string class_name;
std::vector<std::shared_ptr<DefinitionStatement_ASTNode>> member_variables; std::vector<std::shared_ptr<DefinitionStatement_ASTNode>> member_variables;
std::vector<std::shared_ptr<FuncDef_ASTNode>> member_functions; std::vector<std::shared_ptr<FuncDef_ASTNode>> member_functions;
@ -38,16 +32,12 @@ class ClassDef_ASTNode : public ASTNodeBase {
virtual void accept(class ASTNodeVisitorBase *visitor) override; virtual void accept(class ASTNodeVisitorBase *visitor) override;
}; };
class Program_ASTNode : public ASTNodeBase { class Program_ASTNode : public ASTNodeBase {
friend std::shared_ptr<class ModuleItem> BuildIR(std::shared_ptr<Program_ASTNode> src); public:
friend Visitor;
friend class ASTSemanticCheckVisitor;
friend class IRBuilder;
std::vector<std::shared_ptr<DefinitionStatement_ASTNode>> global_variables; std::vector<std::shared_ptr<DefinitionStatement_ASTNode>> global_variables;
std::vector<std::shared_ptr<ClassDef_ASTNode>> classes; std::vector<std::shared_ptr<ClassDef_ASTNode>> classes;
std::vector<std::shared_ptr<FuncDef_ASTNode>> functions; std::vector<std::shared_ptr<FuncDef_ASTNode>> functions;
std::vector<std::shared_ptr<ASTNodeBase>> sorted_children; std::vector<std::shared_ptr<ASTNodeBase>> sorted_children;
public:
Program_ASTNode() = default; Program_ASTNode() = default;
virtual void accept(class ASTNodeVisitorBase *visitor) override; virtual void accept(class ASTNodeVisitorBase *visitor) override;
}; };

View File

@ -372,6 +372,7 @@ void ConductRegAllocForFunction(std::shared_ptr<FunctionDefItem> func) {
} while (ConductColoring(func, cfg, confgraph)); } while (ConductColoring(func, cfg, confgraph));
TranslateColorResult(func, cfg, confgraph); TranslateColorResult(func, cfg, confgraph);
RemoveCallingConventionKeeper(func, cfg, confgraph); RemoveCallingConventionKeeper(func, cfg, confgraph);
PairMoveEliminate(func, cfg, confgraph);
// func->RecursivePrint(std::cerr); // func->RecursivePrint(std::cerr);
} }