diff --git a/CMakeLists.txt b/CMakeLists.txt index 5f3017e..301640f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.5) +cmake_minimum_required(VERSION 3.15) project(SysYFCompiler) @@ -23,6 +23,15 @@ else() message(WARNING "Unknown build type: ${CMAKE_BUILD_TYPE}. Using default flags.") endif() +include(FetchContent) +FetchContent_Declare( + googletest + URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip +) +# For Windows: Prevent overriding the parent project's compiler/linker settings +set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) +FetchContent_MakeAvailable(googletest) + # for ninja color, see https://stackoverflow.com/questions/73349743/ninja-build-system-gcc-clang-doesnt-output-diagnostic-colors set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fdiagnostics-color=always") @@ -43,6 +52,8 @@ add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/src/SysYFIR) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/src/SysYFIRBuilder) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/Student/task2) +add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/unittest) + add_executable( SysYFCompiler ${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp diff --git a/Student/task2/CMakeLists.txt b/Student/task2/CMakeLists.txt index 4e7c863..bbc5620 100644 --- a/Student/task2/CMakeLists.txt +++ b/Student/task2/CMakeLists.txt @@ -1,6 +1,6 @@ project(task2) -cmake_minimum_required(VERSION 3.5) +cmake_minimum_required(VERSION 3.15) set(CMAKE_CXX_STANDARD 14) diff --git a/include/SysYFIR/User.h b/include/SysYFIR/User.h index c0cc4cb..d5a3816 100644 --- a/include/SysYFIR/User.h +++ b/include/SysYFIR/User.h @@ -32,7 +32,6 @@ class User : public Value private: std::vector> operands_; // operands of this value - unsigned num_ops_; }; } diff --git a/src/SysYFIR/User.cpp b/src/SysYFIR/User.cpp index 08fb528..e9d69bd 100644 --- a/src/SysYFIR/User.cpp +++ b/src/SysYFIR/User.cpp @@ -1,6 +1,7 @@ #include "User.h" #include "internal_types.h" #include +#include #ifdef DEBUG #include #endif @@ -10,11 +11,11 @@ namespace SysYF namespace IR { User::User(Ptr ty, const std::string &name , unsigned num_ops) - : Value(ty, name), num_ops_(num_ops) + : Value(ty, name) { // if (num_ops_ > 0) // operands_.reset(new std::list >()); - operands_.resize(num_ops_); + operands_.resize(num_ops); } Ptr User::get_operand(unsigned i) const @@ -35,13 +36,12 @@ void User::set_operand(unsigned i, Ptr v) void User::add_operand(Ptr v) { operands_.push_back(v); - v->add_use(shared_from_this(), num_ops_); - num_ops_++; + v->add_use(shared_from_this(), operands_.size()-1); } unsigned User::get_num_operand() const { - return num_ops_; + return operands_.size(); } void User::remove_use_of_ops() @@ -51,13 +51,19 @@ void User::remove_use_of_ops() } } -void User::remove_operands(int index1,int index2){ - for(int i=index1;i<=index2;i++){ - operands_[i].lock()->remove_use(shared_from_this()); +void User::remove_operands(int index1, int index2) { + auto backup = std::vector>{}; + for (auto iter = operands_.cbegin() + index2 + 1; iter != operands_.cend(); iter++) { + backup.push_back(*iter); + } + for (auto iter = operands_.cbegin() + index1; iter != operands_.cend(); iter++) { + iter->lock()->remove_use(shared_from_this()); + } + operands_.erase(operands_.begin()+index1, operands_.end()); + for (auto op: backup) { + add_operand(op.lock()); } - operands_.erase(operands_.begin()+index1,operands_.begin()+index2+1); // std::cout< +#include + +#include "Constant.h" +#include "Function.h" +#include "Instruction.h" +#include "Module.h" +#include "Type.h" + +using namespace SysYF::IR; + +TEST(IRTest, UserRemoveOperands) { + // https://github.com/ustc-compiler/IRGen/issues/2 + std::cout << "user remove operands"; + auto m = Module::create("test"); + auto fn = Function::create(FunctionType::create(Type::create(SysYF::IR::Type::VoidTyID, m), {}, m), "foo", m); + auto bb = BasicBlock::create(m, "entry", fn); + auto const_1 = ConstantInt::create(1, m); + auto const_2 = ConstantInt::create(2, m); + auto add = BinaryInst::create_add(const_1, const_2, bb, m); + add->remove_operands(0, 0); // remove the first operand + for (unsigned int i = 0; i < add->get_num_operand(); i++) { + auto op = add->get_operand(i); + ASSERT_EQ(op->get_use_list().front().arg_no_, i); + } +} +