rename namespace

This commit is contained in:
ridethepig 2023-05-24 20:40:45 +08:00
parent 44d42d3163
commit 3191c3ad10
27 changed files with 83 additions and 80 deletions

View File

@ -23,6 +23,10 @@ clang_format(format ${MY_SOURCES} ${MY_HEADERS})
# clang_format_check(format_check ${MY_SOURCES} ${MY_HEADERS}) # clang_format_check(format_check ${MY_SOURCES} ${MY_HEADERS})
# add_compile_options(-fsanitize=address) # add_compile_options(-fsanitize=address)
# add_link_options(-fsanitize=address) # add_link_options(-fsanitize=address)
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
add_compile_options(-fstandalone-debug)
endif()
add_executable(sysy ${SOURCES}) add_executable(sysy ${SOURCES})
# message(STATUS "${SOURCES}") # message(STATUS "${SOURCES}")
target_link_libraries(sysy antlr4_static) target_link_libraries(sysy antlr4_static)

View File

@ -3,9 +3,9 @@
#include "llir.h" #include "llir.h"
#include "passes.h" #include "passes.h"
namespace antlrSysY { namespace CompSysY {
void gen_dominance(FunctionPtr_t func); void gen_dominance(FunctionPtr_t func);
void gen_dominance_frontier(FunctionPtr_t func); void gen_dominance_frontier(FunctionPtr_t func);
void update_dfs_numbers(BasicBlockPtr_t bb, bool rst); void update_dfs_numbers(BasicBlockPtr_t bb, bool rst);
} // namespace antlrSysY } // namespace CompSysY

View File

@ -8,7 +8,7 @@
#include "SysyVisitor.h" #include "SysyVisitor.h"
namespace antlrSysY { namespace CompSysY {
/** /**
* This class provides an empty implementation of SysyVisitor, which can be * This class provides an empty implementation of SysyVisitor, which can be
@ -184,4 +184,4 @@ public:
}; };
} // namespace antlrSysY } // namespace CompSysY

View File

@ -7,7 +7,7 @@
#include "antlr4-runtime.h" #include "antlr4-runtime.h"
namespace antlrSysY { namespace CompSysY {
class SysyLexer : public antlr4::Lexer { class SysyLexer : public antlr4::Lexer {
@ -54,4 +54,4 @@ private:
}; };
} // namespace antlrSysY } // namespace CompSysY

View File

@ -9,7 +9,7 @@
#include <memory> #include <memory>
namespace antlrSysY { namespace CompSysY {
class SysyParser : public antlr4::Parser { class SysyParser : public antlr4::Parser {
@ -750,4 +750,4 @@ public:
private: private:
}; };
} // namespace antlrSysY } // namespace CompSysY

View File

@ -8,7 +8,7 @@
#include "SysyParser.h" #include "SysyParser.h"
namespace antlrSysY { namespace CompSysY {
/** /**
* This class defines an abstract visitor for a parse tree * This class defines an abstract visitor for a parse tree
@ -105,4 +105,4 @@ public:
}; };
} // namespace antlrSysY } // namespace CompSysY

View File

@ -2,6 +2,7 @@
#include "3rdparty/easylogging++.h" #include "3rdparty/easylogging++.h"
#include <any> #include <any>
#include <exception> #include <exception>
#include <iterator>
#include <list> #include <list>
#include <memory> #include <memory>
#include <optional> #include <optional>
@ -10,7 +11,6 @@
#include <unordered_map> #include <unordered_map>
#include <unordered_set> #include <unordered_set>
#include <vector> #include <vector>
#include <iterator>
#define uptr(type) std::unique_ptr<type> #define uptr(type) std::unique_ptr<type>
#define sptr(type) std::shared_ptr<type> #define sptr(type) std::shared_ptr<type>
@ -36,7 +36,7 @@ inline sptr(DST) shared_cast(SRC src) {
class type; \ class type; \
typedef std::shared_ptr<type> type##Ptr_t typedef std::shared_ptr<type> type##Ptr_t
namespace antlrSysY { namespace CompSysY {
template <typename T> template <typename T>
std::ostream &operator<<(typename std::enable_if<std::is_enum<T>::value, std::ostream>::type &stream, const T &e) { std::ostream &operator<<(typename std::enable_if<std::is_enum<T>::value, std::ostream>::type &stream, const T &e) {
@ -63,4 +63,4 @@ private:
std::string message; std::string message;
}; };
} // namespace antlrSysY } // namespace CompSysY

View File

@ -6,7 +6,7 @@
#include <sstream> #include <sstream>
#include <string> #include <string>
namespace antlrSysY { namespace CompSysY {
DEF_PTR_T(InstAlloca); DEF_PTR_T(InstAlloca);
DEF_PTR_T(InstStore); DEF_PTR_T(InstStore);
@ -238,22 +238,19 @@ public:
// get the inner // get the inner
static TypePtr_t extract_type(ValuePtr_t pointer, const std::vector<ValuePtr_t> &indices) { static TypePtr_t extract_type(ValuePtr_t pointer, const std::vector<ValuePtr_t> &indices) {
auto pointer_type = std::dynamic_pointer_cast<PointerType>(pointer->type); auto pointed_type = shared_cast<PointerType>(pointer->type)->pointed_type;
auto pointed_type = pointer_type->pointed_type; if (Type::isType<IntegerType>(pointed_type)) {
if (pointed_type->type_tag == Type::TypeTag::IntegerType) {
return pointed_type; return pointed_type;
} }
else if (Type::isType<ArrayType>(pointed_type)) { else if (Type::isType<ArrayType>(pointed_type)) {
for (int i = 1; i < indices.size(); ++i) { for (int i = 1; i < indices.size(); ++i) {
pointed_type = std::dynamic_pointer_cast<ArrayType>(pointed_type)->element_type; pointed_type = shared_cast<ArrayType>(pointed_type)->element_type;
} }
return pointed_type; return pointed_type;
} }
else {
LOG(ERROR) << "unexpected type: " << pointer->to_string(); LOG(ERROR) << "unexpected type: " << pointer->to_string();
assert(0); assert(0);
} }
}
}; };
class InstPhi : public Instruction { class InstPhi : public Instruction {
@ -278,4 +275,4 @@ public:
} }
}; };
} // namespace antlrSysY } // namespace CompSysY

View File

@ -5,10 +5,10 @@
#include <memory> #include <memory>
#include <vector> #include <vector>
namespace antlrSysY { namespace CompSysY {
struct Module { struct Module {
std::vector<std::shared_ptr<Function>> function_list; std::vector<std::shared_ptr<Function>> function_list;
std::vector<std::shared_ptr<GlobalVar>> global_var_list; std::vector<std::shared_ptr<GlobalVar>> global_var_list;
std::vector<std::shared_ptr<Instruction>> instruction_list; std::vector<std::shared_ptr<Instruction>> instruction_list;
}; };
} // namespace antlrSysY } // namespace CompSysY

View File

@ -6,7 +6,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
namespace antlrSysY { namespace CompSysY {
DEF_PTR_T(Type); DEF_PTR_T(Type);
DEF_PTR_T(IntegerType); DEF_PTR_T(IntegerType);
@ -68,6 +68,15 @@ public:
bool isI1() { bool isI1() {
return int_type == IntTypes::I1; return int_type == IntTypes::I1;
} }
static bool isIntegerType(TypePtr_t type) {
return (type->type_tag == Type::TypeTag::IntegerType);
}
static bool isIntegerTypeI32(TypePtr_t type) {
return isIntegerType(type) && std::dynamic_pointer_cast<IntegerType>(type)->isI32();
}
static bool isIntegerTypeI1(TypePtr_t &type) {
return isIntegerType(type) && std::dynamic_pointer_cast<IntegerType>(type)->isI1();
}
virtual std::string to_IR_string() override { virtual std::string to_IR_string() override {
if (isI1()) if (isI1())
return "i1"; return "i1";
@ -94,15 +103,6 @@ public:
class TypeHelper { class TypeHelper {
public: public:
static bool isIntegerType(TypePtr_t type) {
return (type->type_tag == Type::TypeTag::IntegerType);
}
static bool isIntegerTypeI32(TypePtr_t type) {
return isIntegerType(type) && std::dynamic_pointer_cast<IntegerType>(type)->isI32();
}
static bool isIntegerTypeI1(TypePtr_t &type) {
return isIntegerType(type) && std::dynamic_pointer_cast<IntegerType>(type)->isI1();
}
inline static TypePtr_t TYPE_I32 = std::make_shared<IntegerType>(); inline static TypePtr_t TYPE_I32 = std::make_shared<IntegerType>();
inline static TypePtr_t TYPE_I1 = std::make_shared<IntegerType>(IntegerType::IntTypes::I1); inline static TypePtr_t TYPE_I1 = std::make_shared<IntegerType>(IntegerType::IntTypes::I1);
inline static TypePtr_t TYPE_LABEL = std::make_shared<LabelType>(); inline static TypePtr_t TYPE_LABEL = std::make_shared<LabelType>();
@ -166,4 +166,6 @@ public:
} }
}; };
} // namespace antlrSysY TypePtr_t pointed_type(TypePtr_t ptr);
} // namespace CompSysY

View File

@ -12,7 +12,7 @@
#include <unordered_set> #include <unordered_set>
#include <vector> #include <vector>
namespace antlrSysY { namespace CompSysY {
DEF_PTR_T(Value); DEF_PTR_T(Value);
DEF_PTR_T(BasicBlock); DEF_PTR_T(BasicBlock);
@ -130,7 +130,7 @@ public:
} }
}; };
typedef std::list<std::shared_ptr<antlrSysY::BasicBlock>>::iterator BasicBlockListNode_t; typedef std::list<std::shared_ptr<BasicBlock>>::iterator BasicBlockListNode_t;
class Function : public Value { class Function : public Value {
public: public:
@ -259,4 +259,4 @@ std::shared_ptr<ConstantArr> gen_arr_hierarchy(
int length int length
); );
} // namespace antlrSysY } // namespace CompSysY

View File

@ -3,7 +3,7 @@
#include "common.h" #include "common.h"
#include "llir_module.h" #include "llir_module.h"
namespace antlrSysY { namespace CompSysY {
class Pass { class Pass {
public: public:
std::string pass_name; std::string pass_name;
@ -18,4 +18,4 @@ public:
virtual void run(const Module &module) override; virtual void run(const Module &module) override;
}; };
} // namespace antlrSysY } // namespace CompSysY

View File

@ -3,10 +3,10 @@
#include "llir_module.h" #include "llir_module.h"
#include "passes.h" #include "passes.h"
namespace antlrSysY { namespace CompSysY {
class PassBuildCFG : public Pass { class PassBuildCFG : public Pass {
public: public:
PassBuildCFG() : Pass("build_cfg") {} PassBuildCFG() : Pass("build_cfg") {}
virtual void run(const Module &module) override; virtual void run(const Module &module) override;
}; };
} // namespace antlrSysY } // namespace CompSysY

View File

@ -1,7 +1,7 @@
#pragma once #pragma once
#include "common.h" #include "common.h"
namespace antlrSysY { namespace CompSysY {
template <typename T_V> template <typename T_V>
class ScopeTable { class ScopeTable {
typedef std::map<std::string, T_V> ScopeTableEntry_t; typedef std::map<std::string, T_V> ScopeTableEntry_t;
@ -58,4 +58,4 @@ public:
return _scope_list.size() - 1; return _scope_list.size() - 1;
} }
}; };
} // namespace antlrSysY } // namespace CompSysY

View File

@ -18,7 +18,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
namespace antlrSysY { namespace CompSysY {
#pragma region FactoryFunctionDeclaration #pragma region FactoryFunctionDeclaration
std::shared_ptr<ConstantInt> build_ConstantInt(const std::string &name, int value); std::shared_ptr<ConstantInt> build_ConstantInt(const std::string &name, int value);
@ -90,7 +90,7 @@ InstPhiPtr_t build_InstPhi(
#pragma endregion #pragma endregion
class Visitor : public antlrSysY::SysyBaseVisitor { class Visitor : public SysyBaseVisitor {
private: private:
struct VisitorState { struct VisitorState {
bool isGlobalIint = false; bool isGlobalIint = false;
@ -198,4 +198,4 @@ public:
void llir_gen(std::ostream &); void llir_gen(std::ostream &);
}; };
} // namespace antlrSysY } // namespace CompSysY

View File

@ -5,7 +5,7 @@ mkdir -p include
mkdir -p src mkdir -p src
rm -fv build/antlr-gen/* rm -fv build/antlr-gen/*
cp $filename build/antlr-gen/ cp $filename build/antlr-gen/
java -jar ./tools/antlr-4.12.0-complete.jar -no-listener -visitor -Dlanguage=Cpp -package antlrSysY -o build/antlr-gen $filename java -jar ./tools/antlr-4.12.0-complete.jar -no-listener -visitor -Dlanguage=Cpp -package CompSysY -o build/antlr-gen $filename
mkdir -p src/antlrgen mkdir -p src/antlrgen
mkdir -p include/antlrgen mkdir -p include/antlrgen
cp build/antlr-gen/*.cpp src/antlrgen/ cp build/antlr-gen/*.cpp src/antlrgen/

View File

@ -1,7 +1,7 @@
#include "llir.h" #include "llir.h"
#include "visitor.h" #include "visitor.h"
namespace antlrSysY { namespace CompSysY {
static void _bitwise_and(std::vector<bool> &op1, const std::vector<bool> &op2) { static void _bitwise_and(std::vector<bool> &op1, const std::vector<bool> &op2) {
for (int i = 0; i < op1.size(); ++i) { for (int i = 0; i < op1.size(); ++i) {
@ -134,4 +134,4 @@ void update_dfs_numbers(BasicBlockPtr_t bb, bool rst) {
bb->dom_dfs_out = dfs_num++; bb->dom_dfs_out = dfs_num++;
} }
} // namespace antlrSysY } // namespace CompSysY

View File

@ -5,5 +5,5 @@
#include "SysyBaseVisitor.h" #include "SysyBaseVisitor.h"
using namespace antlrSysY; using namespace CompSysY;

View File

@ -7,7 +7,7 @@
using namespace antlr4; using namespace antlr4;
using namespace antlrSysY; using namespace CompSysY;
using namespace antlr4; using namespace antlr4;

View File

@ -8,7 +8,7 @@
using namespace antlrcpp; using namespace antlrcpp;
using namespace antlrSysY; using namespace CompSysY;
using namespace antlr4; using namespace antlr4;

View File

@ -5,5 +5,5 @@
#include "SysyVisitor.h" #include "SysyVisitor.h"
using namespace antlrSysY; using namespace CompSysY;

View File

@ -6,9 +6,9 @@
#include "antlrgen/SysyParser.h" #include "antlrgen/SysyParser.h"
#include "common.h" #include "common.h"
#include "llir.h" #include "llir.h"
#include "machcode.h"
#include "passes.h" #include "passes.h"
#include "visitor.h" #include "visitor.h"
#include "machcode.h"
#include <3rdparty/argparse.hpp> #include <3rdparty/argparse.hpp>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
@ -17,9 +17,9 @@
INITIALIZE_EASYLOGGINGPP INITIALIZE_EASYLOGGINGPP
using namespace antlrSysY; using namespace CompSysY;
using namespace antlr4; using namespace antlr4;
using namespace codegen; using namespace CompSysY;
class AbortErrorListener : public BaseErrorListener { class AbortErrorListener : public BaseErrorListener {
public: public:
@ -127,7 +127,7 @@ int main(int argc, const char **argv) {
// std::cout << tree->toStringTree(&parser) << std::endl << std::endl; // std::cout << tree->toStringTree(&parser) << std::endl << std::endl;
MCModule mc_module; MCModule mc_module;
mc_module.IR2MC(visitor.module); // mc_module.IR2MC(visitor.module);
return 0; return 0;
} }

View File

@ -4,7 +4,7 @@
#include "passes.h" #include "passes.h"
#include <unordered_set> #include <unordered_set>
namespace antlrSysY { namespace CompSysY {
static void print_cfg_info(BasicBlockPtr_t bb) { static void print_cfg_info(BasicBlockPtr_t bb) {
std::cout << "---" << bb->name << "---\n"; std::cout << "---" << bb->name << "---\n";
@ -94,4 +94,4 @@ void PassBuildCFG::run(const Module &module) {
} }
} }
} // namespace antlrSysY } // namespace CompSysY

View File

@ -9,7 +9,7 @@
#include <unordered_set> #include <unordered_set>
#include <vector> #include <vector>
namespace antlrSysY { namespace CompSysY {
struct AllocaInfo { struct AllocaInfo {
std::vector<BasicBlockPtr_t> def_blocks = {}; std::vector<BasicBlockPtr_t> def_blocks = {};
@ -329,4 +329,4 @@ void PassMem2Reg::run(const Module &module) {
} }
} }
} // namespace antlrSysY } // namespace CompSysY

View File

@ -13,7 +13,7 @@
// nothing // nothing
// beyond base class implement // beyond base class implement
namespace antlrSysY { namespace CompSysY {
#define ANY2VALUE(_my_type_) \ #define ANY2VALUE(_my_type_) \
if (fuck_any.type() == typeid(std::shared_ptr<_my_type_>)) { \ if (fuck_any.type() == typeid(std::shared_ptr<_my_type_>)) { \
@ -340,7 +340,7 @@ std::any Visitor::visitConstInitVal(SysyParser::ConstInitValContext *ctx) {
if (init_val->constExp()) { if (init_val->constExp()) {
// evaluate to const int // evaluate to const int
auto const_value = any_to_Value(visitConstInitVal(init_val)); auto const_value = any_to_Value(visitConstInitVal(init_val));
sysy_assert(TypeHelper::isIntegerType(const_value->type)); sysy_assert(shared_cast<IntegerType>(const_value->type));
cur_arr.push_back(const_value); cur_arr.push_back(const_value);
} }
else { else {
@ -468,7 +468,7 @@ std::any Visitor::visitRelExp(SysyParser::RelExpContext *ctx) {
if (ctx->relExp()) { if (ctx->relExp()) {
auto rel_exp = any_to_Value(visitRelExp(ctx->relExp())); auto rel_exp = any_to_Value(visitRelExp(ctx->relExp()));
auto add_exp = any_to_Value(visitAddExp(ctx->addExp())); auto add_exp = any_to_Value(visitAddExp(ctx->addExp()));
sysy_assert(TypeHelper::isIntegerTypeI32(rel_exp->type)); sysy_assert(IntegerType::isIntegerTypeI32(rel_exp->type));
if (ctx->LE()) { if (ctx->LE()) {
add_exp = build_InstBinary(InstTag::Le, rel_exp, add_exp, _state.current_bb); add_exp = build_InstBinary(InstTag::Le, rel_exp, add_exp, _state.current_bb);
} }
@ -498,13 +498,13 @@ std::any Visitor::visitEqExp(SysyParser::EqExpContext *ctx) {
bool need_zext = false; bool need_zext = false;
auto eq_exp = any_to_Value(visitEqExp(ctx->eqExp())); auto eq_exp = any_to_Value(visitEqExp(ctx->eqExp()));
auto rel_exp = any_to_Value(visitRelExp(ctx->relExp())); auto rel_exp = any_to_Value(visitRelExp(ctx->relExp()));
if (TypeHelper::isIntegerTypeI32(eq_exp->type) || TypeHelper::isIntegerTypeI32(rel_exp->type)) { if (IntegerType::isIntegerTypeI32(eq_exp->type) || IntegerType::isIntegerTypeI32(rel_exp->type)) {
need_zext = true; need_zext = true;
} }
if (need_zext && TypeHelper::isIntegerTypeI1(eq_exp->type)) { if (need_zext && IntegerType::isIntegerTypeI1(eq_exp->type)) {
eq_exp = build_InstZext(eq_exp, _state.current_bb); eq_exp = build_InstZext(eq_exp, _state.current_bb);
} }
if (need_zext && TypeHelper::isIntegerTypeI1(rel_exp->type)) { if (need_zext && IntegerType::isIntegerTypeI1(rel_exp->type)) {
rel_exp = build_InstZext(rel_exp, _state.current_bb); rel_exp = build_InstZext(rel_exp, _state.current_bb);
} }
if (ctx->EQ()) { if (ctx->EQ()) {
@ -534,7 +534,7 @@ std::any Visitor::visitLAndExp(SysyParser::LAndExpContext *ctx) {
auto next_block = build_BasicBlock("", _state.current_func, _state.current_bb->itr); auto next_block = build_BasicBlock("", _state.current_func, _state.current_bb->itr);
auto eq_exp = any_to_Value(visitEqExp(eq_exp_list[i])); auto eq_exp = any_to_Value(visitEqExp(eq_exp_list[i]));
auto condition = eq_exp; auto condition = eq_exp;
if (!TypeHelper::isIntegerTypeI1(eq_exp->type)) if (!IntegerType::isIntegerTypeI1(eq_exp->type))
condition = build_InstBinary(InstTag::Ne, eq_exp, CONST0, _state.current_bb); condition = build_InstBinary(InstTag::Ne, eq_exp, CONST0, _state.current_bb);
build_InstBranch(condition, next_block, ctx->false_block, _state.current_bb); build_InstBranch(condition, next_block, ctx->false_block, _state.current_bb);
_state.current_bb = next_block; _state.current_bb = next_block;
@ -797,7 +797,7 @@ std::any Visitor::visitLVal(SysyParser::LValContext *ctx) {
auto inst_add = build_InstBinary(InstTag::Add, offset, exp_val, _state.current_bb); auto inst_add = build_InstBinary(InstTag::Add, offset, exp_val, _state.current_bb);
offset = inst_add; // finally, we get the offset offset = inst_add; // finally, we get the offset
pointed_type = std::dynamic_pointer_cast<PointerType>(ptr->type)->pointed_type; pointed_type = std::dynamic_pointer_cast<PointerType>(ptr->type)->pointed_type;
if (TypeHelper::isIntegerType(pointed_type)) { if (shared_cast<IntegerType>(pointed_type)) {
// return the address of the array element // return the address of the array element
auto arr_elem_ptr = build_InstGEP(ptr, {offset}, _state.current_bb); auto arr_elem_ptr = build_InstGEP(ptr, {offset}, _state.current_bb);
return arr_elem_ptr; return arr_elem_ptr;
@ -849,7 +849,7 @@ std::any Visitor::visitLVal(SysyParser::LValContext *ctx) {
auto inst_add = build_InstBinary(InstTag::Add, offset, exp_val, _state.current_bb); auto inst_add = build_InstBinary(InstTag::Add, offset, exp_val, _state.current_bb);
offset = inst_add; // finally, we get the offset offset = inst_add; // finally, we get the offset
pointed_type = std::dynamic_pointer_cast<PointerType>(ptr->type)->pointed_type; pointed_type = std::dynamic_pointer_cast<PointerType>(ptr->type)->pointed_type;
if (TypeHelper::isIntegerType(pointed_type)) { if (shared_cast<IntegerType>(pointed_type)) {
// return the address of the array element // return the address of the array element
auto arr_elem_ptr = build_InstGEP(ptr, {offset}, _state.current_bb); auto arr_elem_ptr = build_InstGEP(ptr, {offset}, _state.current_bb);
return arr_elem_ptr; return arr_elem_ptr;
@ -1066,4 +1066,4 @@ std::any Visitor::visitReturnStmt(SysyParser::ReturnStmtContext *ctx) {
return {}; return {};
} }
} // namespace antlrSysY } // namespace CompSysY

View File

@ -4,7 +4,7 @@
#include <cassert> #include <cassert>
#include <memory> #include <memory>
namespace antlrSysY { namespace CompSysY {
std::shared_ptr<ConstantInt> build_ConstantInt(const std::string &name, int value) { std::shared_ptr<ConstantInt> build_ConstantInt(const std::string &name, int value) {
auto lala = std::make_shared<ConstantInt>(name, value); auto lala = std::make_shared<ConstantInt>(name, value);
@ -152,4 +152,4 @@ InstPhiPtr_t build_InstPhi(
return inst; return inst;
} }
} // namespace antlrSysY } // namespace CompSysY

View File

@ -9,7 +9,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
namespace antlrSysY { namespace CompSysY {
const std::array<std::string, 8> libfunc_list = const std::array<std::string, 8> libfunc_list =
{"getint", "getch", "getarray", "putint", "putch", "putarray", "starttime", "stoptime"}; {"getint", "getch", "getarray", "putint", "putch", "putarray", "starttime", "stoptime"};
@ -562,4 +562,4 @@ void Visitor::llir_gen(std::ostream &ostr) {
#pragma endregion #pragma endregion
} }
} // namespace antlrSysY } // namespace CompSysY