From 9020d824bd1c16dcb5c1833defde3905757c56f6 Mon Sep 17 00:00:00 2001 From: lyc Date: Tue, 3 Dec 2024 00:31:30 +0800 Subject: [PATCH] rename --- include/Common/Scope.h | 4 +- include/Common/TAC.h | 6 +- include/Common/UseInfo.h | 2 +- include/ICG/StmtICG/ForStmt.h | 4 +- include/ICG/Utils.h | 2 +- include/ICG/myGoListener.h | 6 +- include/Public/Public.h | 26 +++++ include/Public/REG.h | 12 +++ include/Public/Scope.h | 100 ++++++++++++++++++ include/Public/TAC.h | 62 +++++++++++ include/Public/UseInfo.h | 37 +++++++ include/TCG/ASM.h | 4 +- include/TCG/BlockTranslator.h | 4 +- include/TCG/ConstructASM.h | 4 +- .../TCG/SentenceTranslator/AssignTranslator.h | 2 +- .../TCG/SentenceTranslator/BaseTranslator.h | 4 +- .../TCG/SentenceTranslator/CallTranslator.h | 2 +- .../TCG/SentenceTranslator/CommonTranslator.h | 2 +- .../SentenceTranslator/CreatelistTranslator.h | 2 +- .../SentenceTranslator/FunparaTranslator.h | 2 +- .../TCG/SentenceTranslator/FunretTranslator.h | 2 +- .../TCG/SentenceTranslator/GotoTranslator.h | 2 +- include/TCG/SentenceTranslator/IfTranslator.h | 2 +- .../TCG/SentenceTranslator/LabelTranslator.h | 2 +- .../TCG/SentenceTranslator/MulTranslator.h | 4 +- .../TCG/SentenceTranslator/ParaTranslator.h | 2 +- .../TCG/SentenceTranslator/RetTranslator.h | 2 +- include/TCG/SymbolManager.h | 6 +- include/TCG/Translator.h | 6 +- main.cpp | 11 +- src/CMakeLists.txt | 2 +- src/Common/CMakeLists.txt | 6 +- src/Common/REG.cpp | 4 +- src/Common/Scope.cpp | 2 +- src/Common/TAC.cpp | 2 +- src/ICG/CMakeLists.txt | 2 +- src/ICG/myGoListener.cpp | 4 +- src/Public/CMakeLists.txt | 14 +++ src/Public/REG.cpp | 18 ++++ src/Public/Scope.cpp | 57 ++++++++++ src/Public/TAC.cpp | 27 +++++ src/TCG/BlockTranslator.cpp | 5 +- src/TCG/CMakeLists.txt | 2 +- .../SentenceTranslator/AssignTranslator.cpp | 11 +- src/TCG/SentenceTranslator/CallTranslator.cpp | 5 +- .../SentenceTranslator/CommonTranslator.cpp | 6 +- .../CreatelistTranslator.cpp | 2 +- .../SentenceTranslator/FunparaTranslator.cpp | 2 +- .../SentenceTranslator/FunretTranslator.cpp | 5 +- src/TCG/SentenceTranslator/GotoTranslator.cpp | 2 +- src/TCG/SentenceTranslator/IfTranslator.cpp | 5 +- .../SentenceTranslator/LabelTranslator.cpp | 2 +- src/TCG/SentenceTranslator/MulTranslator.cpp | 8 +- src/TCG/SentenceTranslator/ParaTranslator.cpp | 2 +- src/TCG/SentenceTranslator/RetTranslator.cpp | 2 +- src/TCG/SymbolManager.cpp | 2 +- src/TCG/Translator.cpp | 7 +- 57 files changed, 452 insertions(+), 80 deletions(-) create mode 100644 include/Public/Public.h create mode 100644 include/Public/REG.h create mode 100644 include/Public/Scope.h create mode 100644 include/Public/TAC.h create mode 100644 include/Public/UseInfo.h create mode 100644 src/Public/CMakeLists.txt create mode 100644 src/Public/REG.cpp create mode 100644 src/Public/Scope.cpp create mode 100644 src/Public/TAC.cpp diff --git a/include/Common/Scope.h b/include/Common/Scope.h index 62827d5..f43ae2c 100644 --- a/include/Common/Scope.h +++ b/include/Common/Scope.h @@ -1,8 +1,8 @@ #ifndef INCLUDE_COMMON_SCOPE_H_ #define INCLUDE_COMMON_SCOPE_H_ -#include "Common/Common.h" -#include "Common/TAC.h" +#include "Public/Public.h" +#include "Public/TAC.h" #define SUCCESS 1 #define FAIL 0 diff --git a/include/Common/TAC.h b/include/Common/TAC.h index 2752aa8..637afb9 100644 --- a/include/Common/TAC.h +++ b/include/Common/TAC.h @@ -1,9 +1,9 @@ #ifndef INCLUDE_COMMON_TAC_H_ #define INCLUDE_COMMON_TAC_H_ -#include "Common/Common.h" -#include "Common/UseInfo.h" -#include "Common/Scope.h" +#include "Public/Public.h" +#include "Public/UseInfo.h" +#include "Public/Scope.h" struct Scope; diff --git a/include/Common/UseInfo.h b/include/Common/UseInfo.h index 9565d8a..d953f80 100644 --- a/include/Common/UseInfo.h +++ b/include/Common/UseInfo.h @@ -1,7 +1,7 @@ #ifndef INCLUDE_COMMON_USEINFO_H_ #define INCLUDE_COMMON_USEINFO_H_ -#include "Common/Common.h" +#include "Public/Public.h" struct UseInfo { diff --git a/include/ICG/StmtICG/ForStmt.h b/include/ICG/StmtICG/ForStmt.h index ad9a4bf..3faff89 100644 --- a/include/ICG/StmtICG/ForStmt.h +++ b/include/ICG/StmtICG/ForStmt.h @@ -1,7 +1,7 @@ #ifndef INCLUDE_ICG_STMTICG_FORSTMT_H_ #define INCLUDE_ICG_STMTICG_FORSTMT_H_ -#include "Common/Common.h" -#include "Common/TAC.h" +#include "Public/Public.h" +#include "Public/TAC.h" struct ForStmt { diff --git a/include/ICG/Utils.h b/include/ICG/Utils.h index b74dcd9..d0052d2 100644 --- a/include/ICG/Utils.h +++ b/include/ICG/Utils.h @@ -1,4 +1,4 @@ -#include +#include using namespace std; diff --git a/include/ICG/myGoListener.h b/include/ICG/myGoListener.h index 6e53f08..e24418f 100644 --- a/include/ICG/myGoListener.h +++ b/include/ICG/myGoListener.h @@ -1,9 +1,9 @@ #ifndef INCLUDE_ICG_MYGOLIS_H_ #define INCLUDE_ICG_MYGOLIS_H_ -#include "Common/Common.h" -#include "Common/Scope.h" -#include "Common/TAC.h" +#include "Public/Public.h" +#include "Public/Scope.h" +#include "Public/TAC.h" #include "GoParser.h" #include "GoLexer.h" #include "GoParserListener.h" diff --git a/include/Public/Public.h b/include/Public/Public.h new file mode 100644 index 0000000..bb42c92 --- /dev/null +++ b/include/Public/Public.h @@ -0,0 +1,26 @@ +#ifndef INCLUDE_Public_Public_H_ +#define INCLUDE_Public_Public_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_Public_Public_H_ diff --git a/include/Public/REG.h b/include/Public/REG.h new file mode 100644 index 0000000..7b39b6b --- /dev/null +++ b/include/Public/REG.h @@ -0,0 +1,12 @@ +#ifndef INCLUDE_Public_REG_H_ +#define INCLUDE_Public_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/Public/Scope.h b/include/Public/Scope.h new file mode 100644 index 0000000..678d2fa --- /dev/null +++ b/include/Public/Scope.h @@ -0,0 +1,100 @@ +#ifndef INCLUDE_Public_SCOPE_H_ +#define INCLUDE_Public_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/Public/TAC.h b/include/Public/TAC.h new file mode 100644 index 0000000..052ebb8 --- /dev/null +++ b/include/Public/TAC.h @@ -0,0 +1,62 @@ +#ifndef INCLUDE_Public_TAC_H_ +#define INCLUDE_Public_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_Public_TAC_H_ diff --git a/include/Public/UseInfo.h b/include/Public/UseInfo.h new file mode 100644 index 0000000..910d7f5 --- /dev/null +++ b/include/Public/UseInfo.h @@ -0,0 +1,37 @@ +#ifndef INCLUDE_Public_USEINFO_H_ +#define INCLUDE_Public_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/include/TCG/ASM.h b/include/TCG/ASM.h index ec1600d..a71ea41 100644 --- a/include/TCG/ASM.h +++ b/include/TCG/ASM.h @@ -1,8 +1,8 @@ #ifndef INCLUDE_TCG_ASM_H_ #define INCLUDE_TCG_ASM_H_ -#include "Common/Common.h" -#include "Common/REG.h" +#include "Public/Public.h" +#include "Public/REG.h" enum class ASMOP { MOV, PUSH, POP, CALL, JMP, RET, diff --git a/include/TCG/BlockTranslator.h b/include/TCG/BlockTranslator.h index ba1db97..7cd6610 100644 --- a/include/TCG/BlockTranslator.h +++ b/include/TCG/BlockTranslator.h @@ -1,8 +1,8 @@ #ifndef INCLUDE_TCG_BLOCKTRANSLATOR_H_ #define INCLUDE_TCG_BLOCKTRANSLATOR_H_ -#include "Common/Common.h" -#include "Common/TAC.h" +#include "Public/Public.h" +#include "Public/TAC.h" #include "TCG/ASM.h" #include "TCG/SymbolManager.h" diff --git a/include/TCG/ConstructASM.h b/include/TCG/ConstructASM.h index 5685b8b..ff5ed79 100644 --- a/include/TCG/ConstructASM.h +++ b/include/TCG/ConstructASM.h @@ -1,7 +1,7 @@ #ifndef INCLUDE_TCG_CONSTRUCTASM_H_ #define INCLUDE_TCG_CONSTRUCTASM_H_ -#include "Common/Common.h" -#include "Common/REG.h" +#include "Public/Public.h" +#include "Public/REG.h" std::string construct_asm(std::string op, REG dst, REG src); std::string construct_asm(std::string op, REG dst, int src); diff --git a/include/TCG/SentenceTranslator/AssignTranslator.h b/include/TCG/SentenceTranslator/AssignTranslator.h index 380ca93..7051fc2 100644 --- a/include/TCG/SentenceTranslator/AssignTranslator.h +++ b/include/TCG/SentenceTranslator/AssignTranslator.h @@ -1,7 +1,7 @@ #ifndef INCLUDE_TCG_SENTENCETRANSLATOR_ASSIGNTRANSLATOR_H_ #define INCLUDE_TCG_SENTENCETRANSLATOR_ASSIGNTRANSLATOR_H_ -#include "Common/Common.h" +#include "Public/Public.h" #include "TCG/ASM.h" #include "TCG/SentenceTranslator/BaseTranslator.h" diff --git a/include/TCG/SentenceTranslator/BaseTranslator.h b/include/TCG/SentenceTranslator/BaseTranslator.h index 9ea50aa..4afbce0 100644 --- a/include/TCG/SentenceTranslator/BaseTranslator.h +++ b/include/TCG/SentenceTranslator/BaseTranslator.h @@ -1,8 +1,8 @@ #ifndef INCLUDE_TCG_SENTENCETRANSLATOR_BASETRANSLATOR_H_ #define INCLUDE_TCG_SENTENCETRANSLATOR_BASETRANSLATOR_H_ -#include "Common/Common.h" -#include "Common/TAC.h" +#include "Public/Public.h" +#include "Public/TAC.h" #include "TCG/SymbolManager.h" #include "TCG/ASM.h" diff --git a/include/TCG/SentenceTranslator/CallTranslator.h b/include/TCG/SentenceTranslator/CallTranslator.h index 44bb821..12ceb97 100644 --- a/include/TCG/SentenceTranslator/CallTranslator.h +++ b/include/TCG/SentenceTranslator/CallTranslator.h @@ -1,7 +1,7 @@ #ifndef INCLUDE_TCG_SENTENCETRANSLATOR_CALLTRANSLATOR_H_ #define INCLUDE_TCG_SENTENCETRANSLATOR_CALLTRANSLATOR_H_ -#include "Common/Common.h" +#include "Public/Public.h" #include "TCG/ASM.h" #include "TCG/SentenceTranslator/BaseTranslator.h" diff --git a/include/TCG/SentenceTranslator/CommonTranslator.h b/include/TCG/SentenceTranslator/CommonTranslator.h index 6924865..2329ddc 100644 --- a/include/TCG/SentenceTranslator/CommonTranslator.h +++ b/include/TCG/SentenceTranslator/CommonTranslator.h @@ -1,7 +1,7 @@ #ifndef INCLUDE_TCG_SENTENCETRANSLATOR_COMMONTRANSLATOR_H_ #define INCLUDE_TCG_SENTENCETRANSLATOR_COMMONTRANSLATOR_H_ -#include "Common/Common.h" +#include "Public/Public.h" #include "TCG/ASM.h" #include "TCG/SentenceTranslator/BaseTranslator.h" diff --git a/include/TCG/SentenceTranslator/CreatelistTranslator.h b/include/TCG/SentenceTranslator/CreatelistTranslator.h index 30ac8fe..5322855 100644 --- a/include/TCG/SentenceTranslator/CreatelistTranslator.h +++ b/include/TCG/SentenceTranslator/CreatelistTranslator.h @@ -1,7 +1,7 @@ #ifndef INCLUDE_TCG_SENTENCETRANSLATOR_CREATELISTTRANSLATOR_H_ #define INCLUDE_TCG_SENTENCETRANSLATOR_CREATELISTTRANSLATOR_H_ -#include "Common/Common.h" +#include "Public/Public.h" #include "TCG/ASM.h" #include "TCG/SentenceTranslator/BaseTranslator.h" diff --git a/include/TCG/SentenceTranslator/FunparaTranslator.h b/include/TCG/SentenceTranslator/FunparaTranslator.h index d1b7a51..41ea005 100644 --- a/include/TCG/SentenceTranslator/FunparaTranslator.h +++ b/include/TCG/SentenceTranslator/FunparaTranslator.h @@ -1,7 +1,7 @@ #ifndef INCLUDE_TCG_SENTENCETRANSLATOR_FUNPARATRANSLATOR_H_ #define INCLUDE_TCG_SENTENCETRANSLATOR_FUNPARATRANSLATOR_H_ -#include "Common/Common.h" +#include "Public/Public.h" #include "TCG/ASM.h" #include "TCG/SentenceTranslator/BaseTranslator.h" diff --git a/include/TCG/SentenceTranslator/FunretTranslator.h b/include/TCG/SentenceTranslator/FunretTranslator.h index f786fe1..462647d 100644 --- a/include/TCG/SentenceTranslator/FunretTranslator.h +++ b/include/TCG/SentenceTranslator/FunretTranslator.h @@ -1,7 +1,7 @@ #ifndef INCLUDE_TCG_SENTENCETRANSLATOR_FUNRETTRANSLATOR_H_ #define INCLUDE_TCG_SENTENCETRANSLATOR_FUNRETTRANSLATOR_H_ -#include "Common/Common.h" +#include "Public/Public.h" #include "TCG/ASM.h" #include "TCG/SentenceTranslator/BaseTranslator.h" diff --git a/include/TCG/SentenceTranslator/GotoTranslator.h b/include/TCG/SentenceTranslator/GotoTranslator.h index 816ee76..0d5c66d 100644 --- a/include/TCG/SentenceTranslator/GotoTranslator.h +++ b/include/TCG/SentenceTranslator/GotoTranslator.h @@ -1,7 +1,7 @@ #ifndef INCLUDE_TCG_SENTENCETRANSLATOR_GOTOTRANSLATOR_H_ #define INCLUDE_TCG_SENTENCETRANSLATOR_GOTOTRANSLATOR_H_ -#include "Common/Common.h" +#include "Public/Public.h" #include "TCG/ASM.h" #include "TCG/SentenceTranslator/BaseTranslator.h" diff --git a/include/TCG/SentenceTranslator/IfTranslator.h b/include/TCG/SentenceTranslator/IfTranslator.h index 6e6b877..5e4be4d 100644 --- a/include/TCG/SentenceTranslator/IfTranslator.h +++ b/include/TCG/SentenceTranslator/IfTranslator.h @@ -1,7 +1,7 @@ #ifndef INCLUDE_TCG_SENTENCETRANSLATOR_IFTRANSLATOR_H_ #define INCLUDE_TCG_SENTENCETRANSLATOR_IFTRANSLATOR_H_ -#include "Common/Common.h" +#include "Public/Public.h" #include "TCG/ASM.h" #include "TCG/SentenceTranslator/BaseTranslator.h" diff --git a/include/TCG/SentenceTranslator/LabelTranslator.h b/include/TCG/SentenceTranslator/LabelTranslator.h index 2b6de20..ad83601 100644 --- a/include/TCG/SentenceTranslator/LabelTranslator.h +++ b/include/TCG/SentenceTranslator/LabelTranslator.h @@ -1,7 +1,7 @@ #ifndef INCLUDE_TCG_SENTENCETRANSLATOR_LABELTRANSLATOR_H_ #define INCLUDE_TCG_SENTENCETRANSLATOR_LABELTRANSLATOR_H_ -#include "Common/Common.h" +#include "Public/Public.h" #include "TCG/ASM.h" #include "TCG/SentenceTranslator/BaseTranslator.h" diff --git a/include/TCG/SentenceTranslator/MulTranslator.h b/include/TCG/SentenceTranslator/MulTranslator.h index a956229..a86e2c8 100644 --- a/include/TCG/SentenceTranslator/MulTranslator.h +++ b/include/TCG/SentenceTranslator/MulTranslator.h @@ -1,9 +1,9 @@ #ifndef INCLUDE_TCG_SENTENCETRANSLATOR_MULTRANSLATOR_H_ #define INCLUDE_TCG_SENTENCETRANSLATOR_MULTRANSLATOR_H_ -#include "Common/Common.h" +#include "Public/Public.h" -#include "Common/Common.h" +#include "Public/Public.h" #include "TCG/ASM.h" #include "TCG/SentenceTranslator/BaseTranslator.h" diff --git a/include/TCG/SentenceTranslator/ParaTranslator.h b/include/TCG/SentenceTranslator/ParaTranslator.h index 7dab788..6c8b8d0 100644 --- a/include/TCG/SentenceTranslator/ParaTranslator.h +++ b/include/TCG/SentenceTranslator/ParaTranslator.h @@ -1,7 +1,7 @@ #ifndef INCLUDE_TCG_SENTENCETRANSLATOR_PARATRANSLATOR_H_ #define INCLUDE_TCG_SENTENCETRANSLATOR_PARATRANSLATOR_H_ -#include "Common/Common.h" +#include "Public/Public.h" #include "TCG/ASM.h" #include "TCG/SentenceTranslator/BaseTranslator.h" diff --git a/include/TCG/SentenceTranslator/RetTranslator.h b/include/TCG/SentenceTranslator/RetTranslator.h index c0df9b9..b56418c 100644 --- a/include/TCG/SentenceTranslator/RetTranslator.h +++ b/include/TCG/SentenceTranslator/RetTranslator.h @@ -1,7 +1,7 @@ #ifndef INCLUDE_TCG_SENTENCETRANSLATOR_RETTRANSLATOR_H_ #define INCLUDE_TCG_SENTENCETRANSLATOR_RETTRANSLATOR_H_ -#include "Common/Common.h" +#include "Public/Public.h" #include "TCG/ASM.h" #include "TCG/SentenceTranslator/BaseTranslator.h" diff --git a/include/TCG/SymbolManager.h b/include/TCG/SymbolManager.h index 97cf493..408232c 100644 --- a/include/TCG/SymbolManager.h +++ b/include/TCG/SymbolManager.h @@ -1,9 +1,9 @@ #ifndef INCLUDE_TCG_SYMBOLMANAGER_H_ #define INCLUDE_TCG_SYMBOLMANAGER_H_ -#include "Common/Common.h" -#include "Common/Scope.h" -#include "Common/TAC.h" +#include "Public/Public.h" +#include "Public/Scope.h" +#include "Public/TAC.h" #include "TCG/ASM.h" enum class POSTYPE { diff --git a/include/TCG/Translator.h b/include/TCG/Translator.h index 4e55412..52d1c46 100644 --- a/include/TCG/Translator.h +++ b/include/TCG/Translator.h @@ -1,9 +1,9 @@ #ifndef INCLUDE_TCG_TRANSLATOR_H_ #define INCLUDE_TCG_TRANSLATOR_H_ -#include "Common/Common.h" -#include "Common/Scope.h" -#include "Common/TAC.h" +#include "Public/Public.h" +#include "Public/Scope.h" +#include "Public/TAC.h" #include "TCG/ASM.h" #include "TCG/BlockTranslator.h" diff --git a/main.cpp b/main.cpp index ce1d038..6506e23 100644 --- a/main.cpp +++ b/main.cpp @@ -96,7 +96,9 @@ CmdParam read_cmd_param(int argc, char* argv[]) { } int main(int argc, char* argv[]) { - CmdParam cmd_param = read_cmd_param(argc, argv); + try + { + CmdParam cmd_param = read_cmd_param(argc, argv); init_log(cmd_param.log_file_name, cmd_param.log_path); const string& filename = cmd_param.input.front(); @@ -125,4 +127,11 @@ int main(int argc, char* argv[]) { LOG(INFO) << "Translation completed successfully."; return 0; + } + catch(...) + { + LOG(ERROR) << "Translation completed unsuccessfully."; + return -1; + } + } \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5f7e864..5b02718 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,3 +1,3 @@ -add_subdirectory(Common) +add_subdirectory(Public) add_subdirectory(ICG) add_subdirectory(TCG) diff --git a/src/Common/CMakeLists.txt b/src/Common/CMakeLists.txt index 3252df6..34e60c2 100644 --- a/src/Common/CMakeLists.txt +++ b/src/Common/CMakeLists.txt @@ -2,13 +2,13 @@ find_package (glog REQUIRED) # 源文件 file(GLOB_RECURSE src_lib_common *.cpp) -add_library(common +add_library(Public ${src_lib_common} ) # 头文件 -target_include_directories(common PUBLIC +target_include_directories(Public PUBLIC ${CMAKE_SOURCE_DIR}/include ) -target_link_libraries (common glog::glog) +target_link_libraries (Public glog::glog) diff --git a/src/Common/REG.cpp b/src/Common/REG.cpp index 1b76461..dda6388 100644 --- a/src/Common/REG.cpp +++ b/src/Common/REG.cpp @@ -1,5 +1,5 @@ -#include "Common/Common.h" -#include "Common/REG.h" +#include "Public/Public.h" +#include "Public/REG.h" std::string to_string(const REG reg) { diff --git a/src/Common/Scope.cpp b/src/Common/Scope.cpp index 20c906f..ffd30fd 100644 --- a/src/Common/Scope.cpp +++ b/src/Common/Scope.cpp @@ -1,4 +1,4 @@ -#include "Common/Scope.h" +#include "Public/Scope.h" using namespace std; diff --git a/src/Common/TAC.cpp b/src/Common/TAC.cpp index ef7cf63..c2f71cd 100644 --- a/src/Common/TAC.cpp +++ b/src/Common/TAC.cpp @@ -1,4 +1,4 @@ -#include "Common/TAC.h" +#include "Public/TAC.h" std::string to_string(TACOP op) { diff --git a/src/ICG/CMakeLists.txt b/src/ICG/CMakeLists.txt index 754adc7..e3c9432 100644 --- a/src/ICG/CMakeLists.txt +++ b/src/ICG/CMakeLists.txt @@ -27,4 +27,4 @@ target_include_directories(icg PUBLIC ${CMAKE_SOURCE_DIR}/include ) -target_link_libraries(icg antlr common) +target_link_libraries(icg antlr Public) diff --git a/src/ICG/myGoListener.cpp b/src/ICG/myGoListener.cpp index 733646a..9c29114 100644 --- a/src/ICG/myGoListener.cpp +++ b/src/ICG/myGoListener.cpp @@ -1,6 +1,6 @@ #include -#include -#include "Common/TAC.h" +#include +#include "Public/TAC.h" #include "GoParser.h" #include "GoLexer.h" #include "GoParserListener.h" diff --git a/src/Public/CMakeLists.txt b/src/Public/CMakeLists.txt new file mode 100644 index 0000000..da90739 --- /dev/null +++ b/src/Public/CMakeLists.txt @@ -0,0 +1,14 @@ +find_package (glog REQUIRED) + +# 源文件 +file(GLOB_RECURSE src_lib_Public *.cpp) +add_library(Public + ${src_lib_Public} +) + +# 头文件 +target_include_directories(Public PUBLIC + ${CMAKE_SOURCE_DIR}/include +) + +target_link_libraries (Public glog::glog) diff --git a/src/Public/REG.cpp b/src/Public/REG.cpp new file mode 100644 index 0000000..dda6388 --- /dev/null +++ b/src/Public/REG.cpp @@ -0,0 +1,18 @@ +#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/Public/Scope.cpp b/src/Public/Scope.cpp new file mode 100644 index 0000000..ffd30fd --- /dev/null +++ b/src/Public/Scope.cpp @@ -0,0 +1,57 @@ +#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/Public/TAC.cpp b/src/Public/TAC.cpp new file mode 100644 index 0000000..c2f71cd --- /dev/null +++ b/src/Public/TAC.cpp @@ -0,0 +1,27 @@ +#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"; + } +} diff --git a/src/TCG/BlockTranslator.cpp b/src/TCG/BlockTranslator.cpp index aa59355..d7fa80f 100644 --- a/src/TCG/BlockTranslator.cpp +++ b/src/TCG/BlockTranslator.cpp @@ -1,4 +1,4 @@ -#include "Common/Common.h" +#include "Public/Public.h" #include "TCG/BlockTranslator.h" #include "TCG/ConstructASM.h" #include "TCG/SentenceTranslator/BaseTranslator.h" @@ -98,7 +98,8 @@ ASMBlock BlockTranslator::BlockTranslate(SymbolManager& SymbolManager_, std::sha SymbolManager_.set_esp_bias(-4 * stack_len); ASMBlock_.asmlines.push_back(construct_asm("add", REG::ESP, std::to_string(4 * stack_len))); } else if(stack_len < 0) { - LOG(ERROR) << stack_len << "stack overflow"; + LOG(FATAL) << stack_len << "stack overflow"; + throw "ERROR"; } if (SymbolManager_.get_name() == "main") { diff --git a/src/TCG/CMakeLists.txt b/src/TCG/CMakeLists.txt index 42c4b02..31693fa 100644 --- a/src/TCG/CMakeLists.txt +++ b/src/TCG/CMakeLists.txt @@ -5,4 +5,4 @@ add_library(tcg ) target_include_directories(tcg PUBLIC ${CMAKE_SOURCE_DIR}/include) -target_link_libraries(tcg common) +target_link_libraries(tcg Public) diff --git a/src/TCG/SentenceTranslator/AssignTranslator.cpp b/src/TCG/SentenceTranslator/AssignTranslator.cpp index 3320dc2..ceeb905 100644 --- a/src/TCG/SentenceTranslator/AssignTranslator.cpp +++ b/src/TCG/SentenceTranslator/AssignTranslator.cpp @@ -1,4 +1,4 @@ -#include "Common/Common.h" +#include "Public/Public.h" #include "TCG/SentenceTranslator/AssignTranslator.h" #include "TCG/ASM.h" #include "TCG/SymbolManager.h" @@ -54,7 +54,8 @@ ASMLines AssignTranslator::SentenceTranslate_(SymbolManager& SymbolManager_, TAC break; } default: { - LOG(ERROR) << "assign sentence: str1's pos wrong"; + LOG(FATAL) << "assign sentence: str1's pos wrong"; + throw "ERROR"; break; } } @@ -89,7 +90,8 @@ ASMLines AssignTranslator::SentenceTranslate_(SymbolManager& SymbolManager_, TAC break; } default: { - LOG(ERROR) << "assign sentence: str1's pos wrong"; + LOG(FATAL) << "assign sentence: str1's pos wrong"; + throw "ERROR"; break; } } @@ -100,7 +102,8 @@ ASMLines AssignTranslator::SentenceTranslate_(SymbolManager& SymbolManager_, TAC asmlines.push_back(construct_asm("mov", construct_asm_mem(dst_reg, 0, "dword"), construct_asm_mem(src_reg, 0))); } } else { - LOG(ERROR) << "assign sentence: dst' TACOPERANDTYPE wrong"; + LOG(FATAL) << "assign sentence: dst' TACOPERANDTYPE wrong"; + throw "ERROR"; } // TODO: 似乎不需要 如果 dst 也在内存中,更新其内存中的位置 diff --git a/src/TCG/SentenceTranslator/CallTranslator.cpp b/src/TCG/SentenceTranslator/CallTranslator.cpp index 73e0f72..9346908 100644 --- a/src/TCG/SentenceTranslator/CallTranslator.cpp +++ b/src/TCG/SentenceTranslator/CallTranslator.cpp @@ -1,4 +1,4 @@ -#include "Common/Common.h" +#include "Public/Public.h" #include "TCG/SentenceTranslator/CallTranslator.h" #include "TCG/ASM.h" #include "TCG/SymbolManager.h" @@ -15,7 +15,8 @@ ASMLines CallTranslator::SentenceTranslate_(SymbolManager& SymbolManager_, TACLi SymbolManager_.set_esp_bias(-4 * para_num); asmlines.push_back(construct_asm("add", REG::ESP, std::to_string(4 * para_num))); } else { - LOG(ERROR) << "call func error: " << fun_name << std::endl; + LOG(FATAL) << "call func error: " << fun_name << std::endl; + throw "ERROR"; } return asmlines; } diff --git a/src/TCG/SentenceTranslator/CommonTranslator.cpp b/src/TCG/SentenceTranslator/CommonTranslator.cpp index e4739f5..9216ce4 100644 --- a/src/TCG/SentenceTranslator/CommonTranslator.cpp +++ b/src/TCG/SentenceTranslator/CommonTranslator.cpp @@ -1,4 +1,4 @@ -#include "Common/Common.h" +#include "Public/Public.h" #include "TCG/SentenceTranslator/CommonTranslator.h" #include "TCG/ASM.h" #include "TCG/SymbolManager.h" @@ -53,7 +53,7 @@ ASMLines CommonTranslator::SentenceTranslate_(SymbolManager& SymbolManager_, TAC break; } default: { - std::cout << "common default error" << std::endl; + std::cout << "Public default error" << std::endl; break; } } @@ -82,7 +82,7 @@ ASMLines CommonTranslator::SentenceTranslate_(SymbolManager& SymbolManager_, TAC break; } default: { - std::cout << "common default error" << std::endl; + std::cout << "Public default error" << std::endl; break; } } diff --git a/src/TCG/SentenceTranslator/CreatelistTranslator.cpp b/src/TCG/SentenceTranslator/CreatelistTranslator.cpp index f5454e4..fb6b7a1 100644 --- a/src/TCG/SentenceTranslator/CreatelistTranslator.cpp +++ b/src/TCG/SentenceTranslator/CreatelistTranslator.cpp @@ -1,4 +1,4 @@ -#include "Common/Common.h" +#include "Public/Public.h" #include "TCG/SentenceTranslator/CreatelistTranslator.h" #include "TCG/ASM.h" #include "TCG/SymbolManager.h" diff --git a/src/TCG/SentenceTranslator/FunparaTranslator.cpp b/src/TCG/SentenceTranslator/FunparaTranslator.cpp index cdbfc1c..d5fe8dc 100644 --- a/src/TCG/SentenceTranslator/FunparaTranslator.cpp +++ b/src/TCG/SentenceTranslator/FunparaTranslator.cpp @@ -1,4 +1,4 @@ -#include "Common/Common.h" +#include "Public/Public.h" #include "TCG/SentenceTranslator/FunparaTranslator.h" #include "TCG/ASM.h" #include "TCG/SymbolManager.h" diff --git a/src/TCG/SentenceTranslator/FunretTranslator.cpp b/src/TCG/SentenceTranslator/FunretTranslator.cpp index 01f207e..5494b72 100644 --- a/src/TCG/SentenceTranslator/FunretTranslator.cpp +++ b/src/TCG/SentenceTranslator/FunretTranslator.cpp @@ -1,4 +1,4 @@ -#include "Common/Common.h" +#include "Public/Public.h" #include "TCG/SentenceTranslator/FunretTranslator.h" #include "TCG/ASM.h" #include "TCG/SymbolManager.h" @@ -30,7 +30,8 @@ ASMLines FunretTranslator::SentenceTranslate_(SymbolManager& SymbolManager_, TAC break; } default: { - LOG(ERROR) << "funret default error"; + LOG(FATAL) << "funret default error"; + throw "ERROR"; break; } } diff --git a/src/TCG/SentenceTranslator/GotoTranslator.cpp b/src/TCG/SentenceTranslator/GotoTranslator.cpp index f8a5901..5ee0536 100644 --- a/src/TCG/SentenceTranslator/GotoTranslator.cpp +++ b/src/TCG/SentenceTranslator/GotoTranslator.cpp @@ -1,4 +1,4 @@ -#include "Common/Common.h" +#include "Public/Public.h" #include "TCG/SentenceTranslator/GotoTranslator.h" #include "TCG/ASM.h" #include "TCG/SymbolManager.h" diff --git a/src/TCG/SentenceTranslator/IfTranslator.cpp b/src/TCG/SentenceTranslator/IfTranslator.cpp index c1afabb..aa2d5f7 100644 --- a/src/TCG/SentenceTranslator/IfTranslator.cpp +++ b/src/TCG/SentenceTranslator/IfTranslator.cpp @@ -1,4 +1,4 @@ -#include "Common/Common.h" +#include "Public/Public.h" #include "TCG/SentenceTranslator/IfTranslator.h" #include "TCG/ASM.h" #include "TCG/SymbolManager.h" @@ -45,7 +45,8 @@ ASMLines IfTranslator::SentenceTranslate_(SymbolManager& SymbolManager_, TACLine break; } default: { - LOG(ERROR) << "if src1 default error"; + LOG(FATAL) << "if src1 default error"; + throw "ERROR"; break; } } diff --git a/src/TCG/SentenceTranslator/LabelTranslator.cpp b/src/TCG/SentenceTranslator/LabelTranslator.cpp index a889bae..dd51683 100644 --- a/src/TCG/SentenceTranslator/LabelTranslator.cpp +++ b/src/TCG/SentenceTranslator/LabelTranslator.cpp @@ -1,4 +1,4 @@ -#include "Common/Common.h" +#include "Public/Public.h" #include "TCG/SentenceTranslator/LabelTranslator.h" #include "TCG/ASM.h" #include "TCG/SymbolManager.h" diff --git a/src/TCG/SentenceTranslator/MulTranslator.cpp b/src/TCG/SentenceTranslator/MulTranslator.cpp index 8b33508..b6f06c4 100644 --- a/src/TCG/SentenceTranslator/MulTranslator.cpp +++ b/src/TCG/SentenceTranslator/MulTranslator.cpp @@ -1,4 +1,4 @@ -#include "Common/Common.h" +#include "Public/Public.h" #include "TCG/SentenceTranslator/MulTranslator.h" #include "TCG/ASM.h" #include "TCG/SymbolManager.h" @@ -53,7 +53,8 @@ ASMLines MulTranslator::SentenceTranslate_(SymbolManager& SymbolManager_, TACLin break; } default: { - LOG(ERROR) << "mul sentence: str1's pos wrong"; + LOG(FATAL) << "mul sentence: str1's pos wrong"; + throw "ERROR"; break; } } @@ -100,7 +101,8 @@ ASMLines MulTranslator::SentenceTranslate_(SymbolManager& SymbolManager_, TACLin break; } default: { - LOG(ERROR) << "mul sentence: str2's pos wrong"; + LOG(FATAL) << "mul sentence: str2's pos wrong"; + throw "ERROR"; break; } } diff --git a/src/TCG/SentenceTranslator/ParaTranslator.cpp b/src/TCG/SentenceTranslator/ParaTranslator.cpp index 6403b01..2f64944 100644 --- a/src/TCG/SentenceTranslator/ParaTranslator.cpp +++ b/src/TCG/SentenceTranslator/ParaTranslator.cpp @@ -1,4 +1,4 @@ -#include "Common/Common.h" +#include "Public/Public.h" #include "TCG/SentenceTranslator/ParaTranslator.h" #include "TCG/ASM.h" #include "TCG/SymbolManager.h" diff --git a/src/TCG/SentenceTranslator/RetTranslator.cpp b/src/TCG/SentenceTranslator/RetTranslator.cpp index 6cc7e77..27e9acc 100644 --- a/src/TCG/SentenceTranslator/RetTranslator.cpp +++ b/src/TCG/SentenceTranslator/RetTranslator.cpp @@ -1,4 +1,4 @@ -#include "Common/Common.h" +#include "Public/Public.h" #include "TCG/SentenceTranslator/RetTranslator.h" #include "TCG/ASM.h" #include "TCG/SymbolManager.h" diff --git a/src/TCG/SymbolManager.cpp b/src/TCG/SymbolManager.cpp index 07daef7..6c34044 100644 --- a/src/TCG/SymbolManager.cpp +++ b/src/TCG/SymbolManager.cpp @@ -1,5 +1,5 @@ #include "TCG/SymbolManager.h" -#include "Common/Scope.h" +#include "Public/Scope.h" SymbolManager::SymbolManager(std::shared_ptr global_scope, std::string name) : name_(name), global_scope_(global_scope), local_scope_(nullptr), diff --git a/src/TCG/Translator.cpp b/src/TCG/Translator.cpp index c24c625..322aac0 100644 --- a/src/TCG/Translator.cpp +++ b/src/TCG/Translator.cpp @@ -1,4 +1,4 @@ -#include "Common/Common.h" +#include "Public/Public.h" #include "TCG/Translator.h" Translator::Translator(std::shared_ptr TACFile_, std::shared_ptr Global_Scpoe_) : TACFile_(TACFile_), Global_Scope(Global_Scpoe_) { @@ -32,7 +32,8 @@ void Translator::dataTranslate() { break; } default: { - LOG(ERROR) << "global op error: " << to_string((*global)[i].op); + LOG(FATAL) << "global op error: " << to_string((*global)[i].op); + throw "ERROR"; break; } } @@ -60,7 +61,7 @@ void Translator::textTranslate() { /* crTODO: 将 SymbolManager_ 改为 一个快一个 ? ljh 不用 */ // todo 根据函数名到block的map初始化 if (i->first == "global" || i->first == "myprint" || i->second->size() == 0) continue; - LOG(WARNING) << "Function: " << i->first; + LOG(INFO) << "Function: " << i->first; SymbolManager SymbolManager_(Global_Scope, i->first); ASMBlock ASMBlock_ = BlockTranslator_.BlockTranslate(SymbolManager_, i->second); ASMSection_.asmblocks.push_back(ASMBlock_);