From d01963b9e8a474f8ca2416b3d3ece4dfce080e1f Mon Sep 17 00:00:00 2001 From: lyc Date: Tue, 17 Dec 2024 21:02:24 +0800 Subject: [PATCH] update: readme --- README.md | 14 ++---- include/Common/Common.h | 26 ---------- include/Common/REG.h | 12 ----- include/Common/Scope.h | 100 -------------------------------------- include/Common/TAC.h | 62 ----------------------- include/Common/UseInfo.h | 37 -------------- main.cpp | 2 +- src/Common/CMakeLists.txt | 14 ------ src/Common/REG.cpp | 18 ------- src/Common/Scope.cpp | 57 ---------------------- src/Common/TAC.cpp | 27 ---------- 11 files changed, 6 insertions(+), 363 deletions(-) delete mode 100644 include/Common/Common.h delete mode 100644 include/Common/REG.h delete mode 100644 include/Common/Scope.h delete mode 100644 include/Common/TAC.h delete mode 100644 include/Common/UseInfo.h delete mode 100644 src/Common/CMakeLists.txt delete mode 100644 src/Common/REG.cpp delete mode 100644 src/Common/Scope.cpp delete mode 100644 src/Common/TAC.cpp diff --git a/README.md b/README.md index 665223c..8997990 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ - openJDK version "17.0.12" - NASM version 2.16.01 - antlr complete & runtime Version: 4.9.3 -- glog +- glog - `请使用C++17以上标准` # 构建 @@ -42,18 +42,14 @@ bash test_all.sh ├── grammar ------------------------ 存放 Go 语言语法文件和 antlr 针对 Cpp 的解析结果 │ └── cpp ├── include ------------------------ 代码头文件 -│ ├── Common │ ├── ICG -│ │ └── StmtICG │ ├── Public │ └── TCG -│ └── SentenceTranslator ├── src ----------------------------- 源代码 -│ ├── Common -│ ├── ICG -│ ├── Public -│ └── TCG -│ └── SentenceTranslator +│ ├── ICG ------------------------- 中间代码生成相关,对接 antlr +│ ├── Public ---------------------- 定义了一些公共宏,三地址操作,符号解析和管理 +│ └── TCG ------------------------- 目标代码生成相关 +│ └── test ---------------------------- 测试文件 ``` diff --git a/include/Common/Common.h b/include/Common/Common.h deleted file mode 100644 index 5abc2ef..0000000 --- a/include/Common/Common.h +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef INCLUDE_COMMON_COMMON_H_ -#define INCLUDE_COMMON_COMMON_H_ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - - -#define SUCCESS 1 -#define FAIL 0 -#define DELIMITER '#' -#define INT_SIZE "4" -#endif // INCLUDE_COMMON_COMMON_H_ diff --git a/include/Common/REG.h b/include/Common/REG.h deleted file mode 100644 index 1bd0a0b..0000000 --- a/include/Common/REG.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef INCLUDE_COMMON_REG_H_ -#define INCLUDE_COMMON_REG_H_ - -enum class REG { - EAX, EBX, ECX, EDX, ESI, None, /* None 不代表寄存器, 数值上等于可用通用 REG 个数 */ - EDI, // 保留REG - ESP, EBP, EIP -}; - -std::string to_string(const REG reg); - -#endif diff --git a/include/Common/Scope.h b/include/Common/Scope.h deleted file mode 100644 index f43ae2c..0000000 --- a/include/Common/Scope.h +++ /dev/null @@ -1,100 +0,0 @@ -#ifndef INCLUDE_COMMON_SCOPE_H_ -#define INCLUDE_COMMON_SCOPE_H_ - -#include "Public/Public.h" -#include "Public/TAC.h" - -#define SUCCESS 1 -#define FAIL 0 - - - -struct Scope; -struct TACLine; - -using TACBlock = std::vector; - -struct Symbol{ - - enum class SymbolType{ - VAR, - FUN, - }; - - enum class Type { - /* 布尔 */ - BOOL, - /* 数字 */ - INT8, INT16, INT32, INT64, INT, - UINT8, UINT16, UINT32, UINT64, UINT, - FLOAT32, FLOAT64, - COMPLEX64, COMPLEX128, - BYTE, /* 等价于 uint8 */ - RUNE, /* 等价于 int32 */ - /* 字符串 */ - STRING, - /* crTODO: 其他牛逼类型 */ - }; - - std::string name; - std::shared_ptr scope; - // TODO:delete - SymbolType symobl_type; - // 变量有,函数无 - Type type; - // 函数的return列表 - std::shared_ptr> fun_ret_type_list; - // 函数的parameter列表 - std::shared_ptr> fun_para_type_list; - // 是否是数组 0:否,1:是 - bool is_array; - int array_length; - - Symbol() = default; - Symbol(std::string name, std::shared_ptr scope, SymbolType symobl_type, Type type) - : name(name),scope(scope), symobl_type(symobl_type), type(type) {} - Symbol(std::string name, std::shared_ptr scope, SymbolType symobl_type, std::shared_ptr> fun_ret_type_list, std::shared_ptr> fun_para_type_list) - : name(name),scope(scope), fun_ret_type_list(fun_ret_type_list), fun_para_type_list(fun_para_type_list), symobl_type(symobl_type){} - // Symbol(std::string name, std::shared_ptr scope, SymbolType symobl_type, std::shared_ptr> fun_ret_type_list, std::shared_ptr> fun_para_type_list, bool is_array, int length) - // : name(name),scope(scope), fun_ret_type_list(fun_ret_type_list), fun_para_type_list(fun_para_type_list), symobl_type(symobl_type), is_array(is_array), array_length(length){} - Symbol(std::string name, std::shared_ptr scope, SymbolType symobl_type, Type type, bool is_array, int length) - : name(name),scope(scope), symobl_type(symobl_type), type(type), is_array(is_array), array_length(length) {} - bool isVar(); - bool isFun(); - - static Type toType(std::string s){ - if(s == "int") { - return Type::INT; - } - } -}; - - -struct Scope{ - std::shared_ptr enclosing_scope; - std::unordered_map> fun_symbols; - std::unordered_map> para_symbols; - - Scope() : enclosing_scope(nullptr), fun_symbols(),para_symbols() {} - Scope(std::shared_ptr enclosing_scope) : enclosing_scope(enclosing_scope), fun_symbols(),para_symbols() {} - - void fun_define(std::shared_ptr sym); - void para_define(std::shared_ptr sym); - void para_delete(std::string str); - int resolve(std::string name, std::shared_ptr &ret); - std::shared_ptr resolve(std::string name); - int cur_resolve(std::string name); -}; - - -inline bool Symbol::isVar() { - return symobl_type == SymbolType::VAR; -} - - -inline bool Symbol::isFun() { - return symobl_type == SymbolType::FUN; -} - - -#endif \ No newline at end of file diff --git a/include/Common/TAC.h b/include/Common/TAC.h deleted file mode 100644 index 637afb9..0000000 --- a/include/Common/TAC.h +++ /dev/null @@ -1,62 +0,0 @@ -#ifndef INCLUDE_COMMON_TAC_H_ -#define INCLUDE_COMMON_TAC_H_ - -#include "Public/Public.h" -#include "Public/UseInfo.h" -#include "Public/Scope.h" - -struct Scope; - - -enum class TACOP { - ADD, SUB, MUL, DIV, ASSIGN, CALL, PARA, RET, ENDCALL, FUN_RET, FUN_PARA, GOTO, - IFGT, IFGE, IFLT, IFLE, IFEQ, IFNEQ, LABEL, CREATLIST -}; - -std::string to_string(const TACOP op); - - -enum class TACOPERANDTYPE { - IMM, VAR, NULL_, PTR, - LABEL // eg: ELSE0 -}; - -struct Operand { - std::string value; - UseInfo use_info; - TACOPERANDTYPE OperType; - - - Operand(std::string value, TACOPERANDTYPE type) - : value(value), use_info(), OperType(type) {} - - Operand() {} -}; - - -struct TACLine { - int64_t line; /* 行号 */ - TACOP op; - Operand src1, src2; - Operand dst; - std::shared_ptr scope; - - TACLine(int64_t line, TACOP op, Operand src1, Operand src2, Operand dst, std::shared_ptr scope_) - : line(line), op(op), src1(src1), src2(src2), dst(dst), scope(scope_) {} - - TACLine() {} - - std::string to_string() const; -}; - - -using TACBlock = std::vector; - -using TACFile = std::unordered_map>; - - -inline std::string TACLine::to_string() const { - return std::to_string(line) + ":(" + ::to_string(op) + "," + src1.value + "," + src2.value + "," + dst.value + ")"; -} - -#endif // INCLUDE_COMMON_TAC_H_ diff --git a/include/Common/UseInfo.h b/include/Common/UseInfo.h deleted file mode 100644 index d953f80..0000000 --- a/include/Common/UseInfo.h +++ /dev/null @@ -1,37 +0,0 @@ -#ifndef INCLUDE_COMMON_USEINFO_H_ -#define INCLUDE_COMMON_USEINFO_H_ - -#include "Public/Public.h" - - -struct UseInfo { - int64_t next_use; // 待用信息 (0 表示不再使用, 其他数表示下一次使用的位置) - bool active; // 活跃信息 (true 表示还要使用, false 表示不需要了) - - UseInfo(int64_t next_use=0, bool active=false) : next_use(next_use), active(active) {} - - UseInfo& operator=(const UseInfo& rhs); - friend std::ostream& operator<<(std::ostream& os, const UseInfo& rhs); - - bool no_use() const; -}; - - -inline UseInfo& UseInfo::operator=(const UseInfo& rhs) { - next_use = rhs.next_use; - active = rhs.active; - return *this; -} - - -inline std::ostream& operator<<(std::ostream& os, const UseInfo& rhs) { - os << "(" << rhs.next_use << "," << ((rhs.active) ? "Y" : "^") << ")"; - return os; -} - - -inline bool UseInfo::no_use() const { - return (next_use == 0 && active == false); -} - -#endif diff --git a/main.cpp b/main.cpp index bbe5502..5ed33b1 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,5 @@ #include -#include "Common/Common.h" +#include "Public/Public.h" #include "ICG/myGoListener.h" #include "GoParser.h" #include "GoLexer.h" diff --git a/src/Common/CMakeLists.txt b/src/Common/CMakeLists.txt deleted file mode 100644 index 34e60c2..0000000 --- a/src/Common/CMakeLists.txt +++ /dev/null @@ -1,14 +0,0 @@ -find_package (glog REQUIRED) - -# 源文件 -file(GLOB_RECURSE src_lib_common *.cpp) -add_library(Public - ${src_lib_common} -) - -# 头文件 -target_include_directories(Public PUBLIC - ${CMAKE_SOURCE_DIR}/include -) - -target_link_libraries (Public glog::glog) diff --git a/src/Common/REG.cpp b/src/Common/REG.cpp deleted file mode 100644 index dda6388..0000000 --- a/src/Common/REG.cpp +++ /dev/null @@ -1,18 +0,0 @@ -#include "Public/Public.h" -#include "Public/REG.h" - - -std::string to_string(const REG reg) { - switch (reg) { - case REG::EAX: return "eax";break; - case REG::EBP: return "ebp";break; - case REG::EBX: return "ebx";break; - case REG::ECX: return "ecx";break; - case REG::EDI: return "edi";break; - case REG::EDX: return "edx";break; - case REG::EIP: return "eip";break; - case REG::ESI: return "esi";break; - case REG::ESP: return "esp";break; - default: return "none";break; - } -} diff --git a/src/Common/Scope.cpp b/src/Common/Scope.cpp deleted file mode 100644 index ffd30fd..0000000 --- a/src/Common/Scope.cpp +++ /dev/null @@ -1,57 +0,0 @@ -#include "Public/Scope.h" - -using namespace std; - - - - -int Scope::resolve(string name, std::shared_ptr& ret){ - // std::cout << - if (fun_symbols.count(name)==1){ - ret = fun_symbols.at(name); - return SUCCESS; - } - else if (para_symbols.count(name)==1){ - ret = para_symbols.at(name); - return SUCCESS; - } - // if not here, check any enclosing scope - if (enclosing_scope != nullptr ){ - int ret_code=enclosing_scope->resolve(name, ret); - return ret_code; - } - // assert(0); - return FAIL; // not found -} - -std::shared_ptr Scope::resolve(string name){ - std::shared_ptr ret; - int success = resolve(name, ret); - if (success == SUCCESS) { - return ret; - } else { - return nullptr; - } -} - -int Scope::cur_resolve(string name){ - if (fun_symbols.count(name)==1){ - return SUCCESS; - } - else if (para_symbols.count(name)==1){ - return SUCCESS; - } - return FAIL; // not found -} - -void Scope::fun_define(std::shared_ptr sym){ - fun_symbols[sym->name] = sym; -} - -void Scope::para_define(std::shared_ptr sym){ - para_symbols[sym->name] = sym; -} - -void Scope::para_delete(std::string str){ - para_symbols.erase(str); -} diff --git a/src/Common/TAC.cpp b/src/Common/TAC.cpp deleted file mode 100644 index c2f71cd..0000000 --- a/src/Common/TAC.cpp +++ /dev/null @@ -1,27 +0,0 @@ -#include "Public/TAC.h" - - -std::string to_string(TACOP op) { - switch (op) { - case TACOP::ADD: return "add"; - case TACOP::SUB: return "sub"; - case TACOP::MUL: return "mul"; - case TACOP::DIV: return "div"; - case TACOP::ASSIGN: return "assign"; - case TACOP::CALL: return "call"; - case TACOP::PARA: return "para"; - case TACOP::RET: return "ret"; - case TACOP::ENDCALL: return "endcall"; - case TACOP::FUN_RET: return "funret"; - case TACOP::FUN_PARA: return "funpara"; - case TACOP::GOTO: return "goto"; - case TACOP::IFGT: return "ifgt"; - case TACOP::IFGE: return "ifge"; - case TACOP::IFLT: return "iflt"; - case TACOP::IFLE: return "ifle"; - case TACOP::IFEQ: return "ifeq"; - case TACOP::IFNEQ: return "ifneq"; - case TACOP::LABEL: return "label"; - default: return "error_op"; - } -}