rename
This commit is contained in:
parent
ee28e8e2ee
commit
9020d824bd
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
#ifndef INCLUDE_COMMON_USEINFO_H_
|
||||
#define INCLUDE_COMMON_USEINFO_H_
|
||||
|
||||
#include "Common/Common.h"
|
||||
#include "Public/Public.h"
|
||||
|
||||
|
||||
struct UseInfo {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
#include <Common/Common.h>
|
||||
#include <Public/Public.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -0,0 +1,26 @@
|
|||
#ifndef INCLUDE_Public_Public_H_
|
||||
#define INCLUDE_Public_Public_H_
|
||||
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
#include <map>
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <fstream>
|
||||
#include <memory>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
#include <unistd.h>
|
||||
#include <glog/logging.h>
|
||||
|
||||
|
||||
#define SUCCESS 1
|
||||
#define FAIL 0
|
||||
#define DELIMITER '#'
|
||||
#define INT_SIZE "4"
|
||||
#endif // INCLUDE_Public_Public_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
|
||||
|
|
@ -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<TACLine>;
|
||||
|
||||
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> scope;
|
||||
// TODO:delete
|
||||
SymbolType symobl_type;
|
||||
// 变量有,函数无
|
||||
Type type;
|
||||
// 函数的return列表
|
||||
std::shared_ptr<std::vector<Type>> fun_ret_type_list;
|
||||
// 函数的parameter列表
|
||||
std::shared_ptr<std::vector<Type>> fun_para_type_list;
|
||||
// 是否是数组 0:否,1:是
|
||||
bool is_array;
|
||||
int array_length;
|
||||
|
||||
Symbol() = default;
|
||||
Symbol(std::string name, std::shared_ptr<Scope> scope, SymbolType symobl_type, Type type)
|
||||
: name(name),scope(scope), symobl_type(symobl_type), type(type) {}
|
||||
Symbol(std::string name, std::shared_ptr<Scope> scope, SymbolType symobl_type, std::shared_ptr<std::vector<Type>> fun_ret_type_list, std::shared_ptr<std::vector<Type>> 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> scope, SymbolType symobl_type, std::shared_ptr<std::vector<Type>> fun_ret_type_list, std::shared_ptr<std::vector<Type>> 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> 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<Scope> enclosing_scope;
|
||||
std::unordered_map<std::string, std::shared_ptr<Symbol>> fun_symbols;
|
||||
std::unordered_map<std::string, std::shared_ptr<Symbol>> para_symbols;
|
||||
|
||||
Scope() : enclosing_scope(nullptr), fun_symbols(),para_symbols() {}
|
||||
Scope(std::shared_ptr<Scope> enclosing_scope) : enclosing_scope(enclosing_scope), fun_symbols(),para_symbols() {}
|
||||
|
||||
void fun_define(std::shared_ptr<Symbol> sym);
|
||||
void para_define(std::shared_ptr<Symbol> sym);
|
||||
void para_delete(std::string str);
|
||||
int resolve(std::string name, std::shared_ptr<Symbol> &ret);
|
||||
std::shared_ptr<Symbol> 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
|
||||
|
|
@ -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> scope;
|
||||
|
||||
TACLine(int64_t line, TACOP op, Operand src1, Operand src2, Operand dst, std::shared_ptr<Scope> scope_)
|
||||
: line(line), op(op), src1(src1), src2(src2), dst(dst), scope(scope_) {}
|
||||
|
||||
TACLine() {}
|
||||
|
||||
std::string to_string() const;
|
||||
};
|
||||
|
||||
|
||||
using TACBlock = std::vector<TACLine>;
|
||||
|
||||
using TACFile = std::unordered_map<std::string,std::shared_ptr<TACBlock>>;
|
||||
|
||||
|
||||
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_
|
||||
|
|
@ -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
|
||||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
||||
|
|
|
|||
9
main.cpp
9
main.cpp
|
|
@ -96,6 +96,8 @@ CmdParam read_cmd_param(int argc, char* argv[]) {
|
|||
}
|
||||
|
||||
int main(int argc, char* 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();
|
||||
|
|
@ -126,3 +128,10 @@ int main(int argc, char* argv[]) {
|
|||
LOG(INFO) << "Translation completed successfully.";
|
||||
return 0;
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
LOG(ERROR) << "Translation completed unsuccessfully.";
|
||||
return -1;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,3 +1,3 @@
|
|||
add_subdirectory(Common)
|
||||
add_subdirectory(Public)
|
||||
add_subdirectory(ICG)
|
||||
add_subdirectory(TCG)
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
#include "Common/Scope.h"
|
||||
#include "Public/Scope.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
#include "Common/TAC.h"
|
||||
#include "Public/TAC.h"
|
||||
|
||||
|
||||
std::string to_string(TACOP op) {
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
#include <antlr4-runtime.h>
|
||||
#include <Common/Common.h>
|
||||
#include "Common/TAC.h"
|
||||
#include <Public/Public.h>
|
||||
#include "Public/TAC.h"
|
||||
#include "GoParser.h"
|
||||
#include "GoLexer.h"
|
||||
#include "GoParserListener.h"
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,57 @@
|
|||
#include "Public/Scope.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
|
||||
|
||||
int Scope::resolve(string name, std::shared_ptr<Symbol>& 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<Symbol> Scope::resolve(string name){
|
||||
std::shared_ptr<Symbol> 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<Symbol> sym){
|
||||
fun_symbols[sym->name] = sym;
|
||||
}
|
||||
|
||||
void Scope::para_define(std::shared_ptr<Symbol> sym){
|
||||
para_symbols[sym->name] = sym;
|
||||
}
|
||||
|
||||
void Scope::para_delete(std::string str){
|
||||
para_symbols.erase(str);
|
||||
}
|
||||
|
|
@ -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";
|
||||
}
|
||||
}
|
||||
|
|
@ -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") {
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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 也在内存中,更新其内存中的位置
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
#include "TCG/SymbolManager.h"
|
||||
#include "Common/Scope.h"
|
||||
#include "Public/Scope.h"
|
||||
|
||||
SymbolManager::SymbolManager(std::shared_ptr<Scope> global_scope, std::string name)
|
||||
: name_(name), global_scope_(global_scope), local_scope_(nullptr),
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
#include "Common/Common.h"
|
||||
#include "Public/Public.h"
|
||||
#include "TCG/Translator.h"
|
||||
|
||||
Translator::Translator(std::shared_ptr<TACFile> TACFile_, std::shared_ptr<Scope> 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_);
|
||||
|
|
|
|||
Loading…
Reference in New Issue