update: readme
This commit is contained in:
parent
ba56bec560
commit
d01963b9e8
14
README.md
14
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 ---------------------------- 测试文件
|
||||
|
||||
```
|
||||
|
|
|
|||
|
|
@ -1,26 +0,0 @@
|
|||
#ifndef INCLUDE_COMMON_COMMON_H_
|
||||
#define INCLUDE_COMMON_COMMON_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_COMMON_COMMON_H_
|
||||
|
|
@ -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
|
||||
|
|
@ -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<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
|
||||
|
|
@ -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> 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_COMMON_TAC_H_
|
||||
|
|
@ -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
|
||||
2
main.cpp
2
main.cpp
|
|
@ -1,5 +1,5 @@
|
|||
#include <antlr4-runtime.h>
|
||||
#include "Common/Common.h"
|
||||
#include "Public/Public.h"
|
||||
#include "ICG/myGoListener.h"
|
||||
#include "GoParser.h"
|
||||
#include "GoLexer.h"
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,57 +0,0 @@
|
|||
#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);
|
||||
}
|
||||
|
|
@ -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";
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue