rename namespace
This commit is contained in:
parent
44d42d3163
commit
3191c3ad10
@ -23,6 +23,10 @@ clang_format(format ${MY_SOURCES} ${MY_HEADERS})
|
||||
# clang_format_check(format_check ${MY_SOURCES} ${MY_HEADERS})
|
||||
# add_compile_options(-fsanitize=address)
|
||||
# add_link_options(-fsanitize=address)
|
||||
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||
add_compile_options(-fstandalone-debug)
|
||||
endif()
|
||||
|
||||
add_executable(sysy ${SOURCES})
|
||||
# message(STATUS "${SOURCES}")
|
||||
target_link_libraries(sysy antlr4_static)
|
||||
|
||||
@ -3,9 +3,9 @@
|
||||
#include "llir.h"
|
||||
#include "passes.h"
|
||||
|
||||
namespace antlrSysY {
|
||||
namespace CompSysY {
|
||||
void gen_dominance(FunctionPtr_t func);
|
||||
void gen_dominance_frontier(FunctionPtr_t func);
|
||||
void update_dfs_numbers(BasicBlockPtr_t bb, bool rst);
|
||||
|
||||
} // namespace antlrSysY
|
||||
} // namespace CompSysY
|
||||
|
||||
@ -8,7 +8,7 @@
|
||||
#include "SysyVisitor.h"
|
||||
|
||||
|
||||
namespace antlrSysY {
|
||||
namespace CompSysY {
|
||||
|
||||
/**
|
||||
* This class provides an empty implementation of SysyVisitor, which can be
|
||||
@ -184,4 +184,4 @@ public:
|
||||
|
||||
};
|
||||
|
||||
} // namespace antlrSysY
|
||||
} // namespace CompSysY
|
||||
|
||||
@ -7,7 +7,7 @@
|
||||
#include "antlr4-runtime.h"
|
||||
|
||||
|
||||
namespace antlrSysY {
|
||||
namespace CompSysY {
|
||||
|
||||
|
||||
class SysyLexer : public antlr4::Lexer {
|
||||
@ -54,4 +54,4 @@ private:
|
||||
|
||||
};
|
||||
|
||||
} // namespace antlrSysY
|
||||
} // namespace CompSysY
|
||||
|
||||
@ -9,7 +9,7 @@
|
||||
#include <memory>
|
||||
|
||||
|
||||
namespace antlrSysY {
|
||||
namespace CompSysY {
|
||||
|
||||
|
||||
class SysyParser : public antlr4::Parser {
|
||||
@ -750,4 +750,4 @@ public:
|
||||
private:
|
||||
};
|
||||
|
||||
} // namespace antlrSysY
|
||||
} // namespace CompSysY
|
||||
|
||||
@ -8,7 +8,7 @@
|
||||
#include "SysyParser.h"
|
||||
|
||||
|
||||
namespace antlrSysY {
|
||||
namespace CompSysY {
|
||||
|
||||
/**
|
||||
* This class defines an abstract visitor for a parse tree
|
||||
@ -105,4 +105,4 @@ public:
|
||||
|
||||
};
|
||||
|
||||
} // namespace antlrSysY
|
||||
} // namespace CompSysY
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
#include "3rdparty/easylogging++.h"
|
||||
#include <any>
|
||||
#include <exception>
|
||||
#include <iterator>
|
||||
#include <list>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
@ -10,7 +11,6 @@
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <vector>
|
||||
#include <iterator>
|
||||
|
||||
#define uptr(type) std::unique_ptr<type>
|
||||
#define sptr(type) std::shared_ptr<type>
|
||||
@ -36,7 +36,7 @@ inline sptr(DST) shared_cast(SRC src) {
|
||||
class type; \
|
||||
typedef std::shared_ptr<type> type##Ptr_t
|
||||
|
||||
namespace antlrSysY {
|
||||
namespace CompSysY {
|
||||
|
||||
template <typename T>
|
||||
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;
|
||||
};
|
||||
|
||||
} // namespace antlrSysY
|
||||
} // namespace CompSysY
|
||||
@ -6,7 +6,7 @@
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
namespace antlrSysY {
|
||||
namespace CompSysY {
|
||||
|
||||
DEF_PTR_T(InstAlloca);
|
||||
DEF_PTR_T(InstStore);
|
||||
@ -238,22 +238,19 @@ public:
|
||||
|
||||
// get the inner
|
||||
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 = pointer_type->pointed_type;
|
||||
if (pointed_type->type_tag == Type::TypeTag::IntegerType) {
|
||||
auto pointed_type = shared_cast<PointerType>(pointer->type)->pointed_type;
|
||||
if (Type::isType<IntegerType>(pointed_type)) {
|
||||
return pointed_type;
|
||||
}
|
||||
else if (Type::isType<ArrayType>(pointed_type)) {
|
||||
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;
|
||||
}
|
||||
else {
|
||||
LOG(ERROR) << "unexpected type: " << pointer->to_string();
|
||||
assert(0);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
class InstPhi : public Instruction {
|
||||
@ -278,4 +275,4 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace antlrSysY
|
||||
} // namespace CompSysY
|
||||
@ -5,10 +5,10 @@
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
namespace antlrSysY {
|
||||
namespace CompSysY {
|
||||
struct Module {
|
||||
std::vector<std::shared_ptr<Function>> function_list;
|
||||
std::vector<std::shared_ptr<GlobalVar>> global_var_list;
|
||||
std::vector<std::shared_ptr<Instruction>> instruction_list;
|
||||
};
|
||||
} // namespace antlrSysY
|
||||
} // namespace CompSysY
|
||||
@ -6,7 +6,7 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace antlrSysY {
|
||||
namespace CompSysY {
|
||||
|
||||
DEF_PTR_T(Type);
|
||||
DEF_PTR_T(IntegerType);
|
||||
@ -68,6 +68,15 @@ public:
|
||||
bool isI1() {
|
||||
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 {
|
||||
if (isI1())
|
||||
return "i1";
|
||||
@ -94,15 +103,6 @@ public:
|
||||
|
||||
class TypeHelper {
|
||||
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_I1 = std::make_shared<IntegerType>(IntegerType::IntTypes::I1);
|
||||
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
|
||||
@ -12,7 +12,7 @@
|
||||
#include <unordered_set>
|
||||
#include <vector>
|
||||
|
||||
namespace antlrSysY {
|
||||
namespace CompSysY {
|
||||
|
||||
DEF_PTR_T(Value);
|
||||
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 {
|
||||
public:
|
||||
@ -259,4 +259,4 @@ std::shared_ptr<ConstantArr> gen_arr_hierarchy(
|
||||
int length
|
||||
);
|
||||
|
||||
} // namespace antlrSysY
|
||||
} // namespace CompSysY
|
||||
@ -3,7 +3,7 @@
|
||||
#include "common.h"
|
||||
#include "llir_module.h"
|
||||
|
||||
namespace antlrSysY {
|
||||
namespace CompSysY {
|
||||
class Pass {
|
||||
public:
|
||||
std::string pass_name;
|
||||
@ -18,4 +18,4 @@ public:
|
||||
virtual void run(const Module &module) override;
|
||||
};
|
||||
|
||||
} // namespace antlrSysY
|
||||
} // namespace CompSysY
|
||||
@ -3,10 +3,10 @@
|
||||
#include "llir_module.h"
|
||||
#include "passes.h"
|
||||
|
||||
namespace antlrSysY {
|
||||
namespace CompSysY {
|
||||
class PassBuildCFG : public Pass {
|
||||
public:
|
||||
PassBuildCFG() : Pass("build_cfg") {}
|
||||
virtual void run(const Module &module) override;
|
||||
};
|
||||
} // namespace antlrSysY
|
||||
} // namespace CompSysY
|
||||
@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
#include "common.h"
|
||||
|
||||
namespace antlrSysY {
|
||||
namespace CompSysY {
|
||||
template <typename T_V>
|
||||
class ScopeTable {
|
||||
typedef std::map<std::string, T_V> ScopeTableEntry_t;
|
||||
@ -58,4 +58,4 @@ public:
|
||||
return _scope_list.size() - 1;
|
||||
}
|
||||
};
|
||||
} // namespace antlrSysY
|
||||
} // namespace CompSysY
|
||||
@ -18,7 +18,7 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace antlrSysY {
|
||||
namespace CompSysY {
|
||||
|
||||
#pragma region FactoryFunctionDeclaration
|
||||
std::shared_ptr<ConstantInt> build_ConstantInt(const std::string &name, int value);
|
||||
@ -90,7 +90,7 @@ InstPhiPtr_t build_InstPhi(
|
||||
|
||||
#pragma endregion
|
||||
|
||||
class Visitor : public antlrSysY::SysyBaseVisitor {
|
||||
class Visitor : public SysyBaseVisitor {
|
||||
private:
|
||||
struct VisitorState {
|
||||
bool isGlobalIint = false;
|
||||
@ -198,4 +198,4 @@ public:
|
||||
void llir_gen(std::ostream &);
|
||||
};
|
||||
|
||||
} // namespace antlrSysY
|
||||
} // namespace CompSysY
|
||||
@ -5,7 +5,7 @@ mkdir -p include
|
||||
mkdir -p src
|
||||
rm -fv 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 include/antlrgen
|
||||
cp build/antlr-gen/*.cpp src/antlrgen/
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
#include "llir.h"
|
||||
#include "visitor.h"
|
||||
|
||||
namespace antlrSysY {
|
||||
namespace CompSysY {
|
||||
|
||||
static void _bitwise_and(std::vector<bool> &op1, const std::vector<bool> &op2) {
|
||||
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++;
|
||||
}
|
||||
|
||||
} // namespace antlrSysY
|
||||
} // namespace CompSysY
|
||||
@ -5,5 +5,5 @@
|
||||
#include "SysyBaseVisitor.h"
|
||||
|
||||
|
||||
using namespace antlrSysY;
|
||||
using namespace CompSysY;
|
||||
|
||||
|
||||
@ -7,7 +7,7 @@
|
||||
|
||||
using namespace antlr4;
|
||||
|
||||
using namespace antlrSysY;
|
||||
using namespace CompSysY;
|
||||
|
||||
|
||||
using namespace antlr4;
|
||||
|
||||
@ -8,7 +8,7 @@
|
||||
|
||||
|
||||
using namespace antlrcpp;
|
||||
using namespace antlrSysY;
|
||||
using namespace CompSysY;
|
||||
|
||||
using namespace antlr4;
|
||||
|
||||
|
||||
@ -5,5 +5,5 @@
|
||||
#include "SysyVisitor.h"
|
||||
|
||||
|
||||
using namespace antlrSysY;
|
||||
using namespace CompSysY;
|
||||
|
||||
|
||||
@ -6,9 +6,9 @@
|
||||
#include "antlrgen/SysyParser.h"
|
||||
#include "common.h"
|
||||
#include "llir.h"
|
||||
#include "machcode.h"
|
||||
#include "passes.h"
|
||||
#include "visitor.h"
|
||||
#include "machcode.h"
|
||||
#include <3rdparty/argparse.hpp>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
@ -17,9 +17,9 @@
|
||||
|
||||
INITIALIZE_EASYLOGGINGPP
|
||||
|
||||
using namespace antlrSysY;
|
||||
using namespace CompSysY;
|
||||
using namespace antlr4;
|
||||
using namespace codegen;
|
||||
using namespace CompSysY;
|
||||
|
||||
class AbortErrorListener : public BaseErrorListener {
|
||||
public:
|
||||
@ -127,7 +127,7 @@ int main(int argc, const char **argv) {
|
||||
// std::cout << tree->toStringTree(&parser) << std::endl << std::endl;
|
||||
|
||||
MCModule mc_module;
|
||||
mc_module.IR2MC(visitor.module);
|
||||
// mc_module.IR2MC(visitor.module);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -4,7 +4,7 @@
|
||||
#include "passes.h"
|
||||
#include <unordered_set>
|
||||
|
||||
namespace antlrSysY {
|
||||
namespace CompSysY {
|
||||
|
||||
static void print_cfg_info(BasicBlockPtr_t bb) {
|
||||
std::cout << "---" << bb->name << "---\n";
|
||||
@ -94,4 +94,4 @@ void PassBuildCFG::run(const Module &module) {
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace antlrSysY
|
||||
} // namespace CompSysY
|
||||
@ -9,7 +9,7 @@
|
||||
#include <unordered_set>
|
||||
#include <vector>
|
||||
|
||||
namespace antlrSysY {
|
||||
namespace CompSysY {
|
||||
|
||||
struct AllocaInfo {
|
||||
std::vector<BasicBlockPtr_t> def_blocks = {};
|
||||
@ -329,4 +329,4 @@ void PassMem2Reg::run(const Module &module) {
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace antlrSysY
|
||||
} // namespace CompSysY
|
||||
@ -13,7 +13,7 @@
|
||||
// nothing
|
||||
// beyond base class implement
|
||||
|
||||
namespace antlrSysY {
|
||||
namespace CompSysY {
|
||||
|
||||
#define ANY2VALUE(_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()) {
|
||||
// evaluate to const int
|
||||
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);
|
||||
}
|
||||
else {
|
||||
@ -468,7 +468,7 @@ std::any Visitor::visitRelExp(SysyParser::RelExpContext *ctx) {
|
||||
if (ctx->relExp()) {
|
||||
auto rel_exp = any_to_Value(visitRelExp(ctx->relExp()));
|
||||
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()) {
|
||||
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;
|
||||
auto eq_exp = any_to_Value(visitEqExp(ctx->eqExp()));
|
||||
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;
|
||||
}
|
||||
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);
|
||||
}
|
||||
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);
|
||||
}
|
||||
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 eq_exp = any_to_Value(visitEqExp(eq_exp_list[i]));
|
||||
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);
|
||||
build_InstBranch(condition, next_block, ctx->false_block, _state.current_bb);
|
||||
_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);
|
||||
offset = inst_add; // finally, we get the offset
|
||||
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
|
||||
auto arr_elem_ptr = build_InstGEP(ptr, {offset}, _state.current_bb);
|
||||
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);
|
||||
offset = inst_add; // finally, we get the offset
|
||||
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
|
||||
auto arr_elem_ptr = build_InstGEP(ptr, {offset}, _state.current_bb);
|
||||
return arr_elem_ptr;
|
||||
@ -1066,4 +1066,4 @@ std::any Visitor::visitReturnStmt(SysyParser::ReturnStmtContext *ctx) {
|
||||
return {};
|
||||
}
|
||||
|
||||
} // namespace antlrSysY
|
||||
} // namespace CompSysY
|
||||
@ -4,7 +4,7 @@
|
||||
#include <cassert>
|
||||
#include <memory>
|
||||
|
||||
namespace antlrSysY {
|
||||
namespace CompSysY {
|
||||
|
||||
std::shared_ptr<ConstantInt> build_ConstantInt(const std::string &name, int value) {
|
||||
auto lala = std::make_shared<ConstantInt>(name, value);
|
||||
@ -152,4 +152,4 @@ InstPhiPtr_t build_InstPhi(
|
||||
return inst;
|
||||
}
|
||||
|
||||
} // namespace antlrSysY
|
||||
} // namespace CompSysY
|
||||
@ -9,7 +9,7 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace antlrSysY {
|
||||
namespace CompSysY {
|
||||
|
||||
const std::array<std::string, 8> libfunc_list =
|
||||
{"getint", "getch", "getarray", "putint", "putch", "putarray", "starttime", "stoptime"};
|
||||
@ -562,4 +562,4 @@ void Visitor::llir_gen(std::ostream &ostr) {
|
||||
#pragma endregion
|
||||
}
|
||||
|
||||
} // namespace antlrSysY
|
||||
} // namespace CompSysY
|
||||
Loading…
Reference in New Issue
Block a user