Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ jobs:
-DCMAKE_CXX_COMPILER=${{matrix.compiler}} \
-DCMAKE_BUILD_TYPE=${{matrix.build_type}} \
-DCMAKE_EXPORT_COMPILE_COMMANDS=${{matrix.export_commands}} \
-DPROT_ENABLE_WERROR=ON
-DPROT_ENABLE_WERROR=ON \
-DPROT_BUILD_TESTS=ON

- name: Build
# Build your program with the given configuration
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
endif()

option(PROT_ENABLE_WERROR "Enable -Werror option (CI)" OFF)
option(PROT_BUILD_TESTS "Enable tests building (CI)" OFF)

enable_testing()

Expand Down
17 changes: 16 additions & 1 deletion cmake/utils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ macro(prot_add_utest src_file)
set(TEST_NAME "${src_file}_test")
add_executable(${TEST_NAME} ${src_file})

target_link_libraries(${TEST_NAME} PRIVATE GMock::gmock_main PROT::defaults)
target_link_libraries(${TEST_NAME} PRIVATE GTest::gmock_main PROT::defaults)
foreach(arg IN LISTS ARGN)
target_link_libraries(${TEST_NAME} PRIVATE ${arg})
endforeach()
Expand All @@ -15,3 +15,18 @@ macro(prot_add_utest src_file)
EXTRA_ARGS --gtest_color=yes
PROPERTIES LABELS unit)
endmacro()

function(prot_add_component_utest COMPONENT_NAME)
file(GLOB TESTLIST RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "*.cc")

foreach(TEST_SRC ${TESTLIST})
set(TEST_NAME "${src_file}_test")
prot_add_utest(${TEST_SRC} ARGN)

set_target_properties(${TEST_NAME}
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/test/${COMPONENT_NAME}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid it could break in future, I suppose it can be done via ctest functionality

)

endforeach()
endfunction()
16 changes: 4 additions & 12 deletions src/interpreter/interp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,14 @@ void executeRegisterRegisterOp(const isa::Instruction &inst, CPUState &state,
state.setReg(inst.rd(), op(rs1, rs2));
}

template <std::regular_invocable<isa::Word, isa::Operand> Func>
template <std::regular_invocable<isa::Word, isa::Word> Func>
void executeRegisterImmOp(const isa::Instruction &inst, CPUState &state,
Func op) {
auto rs1 = state.getReg(inst.rs1());
auto imm = inst.imm();
state.setReg(inst.rd(), op(rs1, imm));
}

template <std::regular_invocable<isa::Word, isa::Operand> Func>
void executeRegisterImmRegOp(const isa::Instruction &inst, CPUState &state,
Func op) {
auto rs1 = state.getReg(inst.rs1());
auto imm = inst.rs2();
state.setReg(inst.rd(), op(rs1, imm));
}

void doADD(const isa::Instruction &inst, CPUState &state) {
executeRegisterRegisterOp(inst, state, std::plus<>{});
}
Expand Down Expand Up @@ -177,7 +169,7 @@ void doSLL(const isa::Instruction &inst, CPUState &state) {
executeRegisterRegisterOp(inst, state, sllHelper);
}
void doSLLI(const isa::Instruction &inst, CPUState &state) {
executeRegisterImmRegOp(inst, state, sllHelper);
executeRegisterImmOp(inst, state, sllHelper);
}

void doSLT(const isa::Instruction &inst, CPUState &state) {
Expand All @@ -203,7 +195,7 @@ void doSRA(const isa::Instruction &inst, CPUState &state) {
executeRegisterRegisterOp(inst, state, sraHelper);
}
void doSRAI(const isa::Instruction &inst, CPUState &state) {
executeRegisterImmRegOp(inst, state, sraHelper);
executeRegisterImmOp(inst, state, sraHelper);
}

isa::Word srlHelper(isa::Word lhs, isa::Word rhs) {
Expand All @@ -214,7 +206,7 @@ void doSRL(const isa::Instruction &inst, CPUState &state) {
executeRegisterRegisterOp(inst, state, srlHelper);
}
void doSRLI(const isa::Instruction &inst, CPUState &state) {
executeRegisterImmRegOp(inst, state, srlHelper);
executeRegisterImmOp(inst, state, srlHelper);
}

void doSUB(const isa::Instruction &inst, CPUState &state) {
Expand Down
1 change: 1 addition & 0 deletions src/isa/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
add_library(prot_isa STATIC isa.cc)
add_subdirectory(tests)
target_include_directories(prot_isa PUBLIC include)
target_link_libraries(prot_isa PRIVATE PROT::defaults)

Expand Down
6 changes: 3 additions & 3 deletions src/isa/isa.cc
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ std::optional<Instruction> Instruction::decode(Word word) {
instr.m_opc = Opcode::kSLLI;
instr.m_rd = std::bit_cast<int32_t>(slice<11, 7>(word) << 0) >> 0;
instr.m_rs1 = std::bit_cast<int32_t>(slice<19, 15>(word) << 0) >> 0;
instr.m_rs2 = std::bit_cast<int32_t>(slice<24, 20>(word) << 0) >> 0;
instr.m_imm = std::bit_cast<int32_t>(slice<24, 20>(word) << 0) >> 0;
return instr;
}
case 0b10000000010011: {
Expand Down Expand Up @@ -334,7 +334,7 @@ std::optional<Instruction> Instruction::decode(Word word) {
instr.m_opc = Opcode::kSRLI;
instr.m_rd = std::bit_cast<int32_t>(slice<11, 7>(word) << 0) >> 0;
instr.m_rs1 = std::bit_cast<int32_t>(slice<19, 15>(word) << 0) >> 0;
instr.m_rs2 = std::bit_cast<int32_t>(slice<24, 20>(word) << 0) >> 0;
instr.m_imm = std::bit_cast<int32_t>(slice<24, 20>(word) << 0) >> 0;
return instr;
}
case 0b1000000000000000101000000010011: {
Expand All @@ -343,7 +343,7 @@ std::optional<Instruction> Instruction::decode(Word word) {
instr.m_opc = Opcode::kSRAI;
instr.m_rd = std::bit_cast<int32_t>(slice<11, 7>(word) << 0) >> 0;
instr.m_rs1 = std::bit_cast<int32_t>(slice<19, 15>(word) << 0) >> 0;
instr.m_rs2 = std::bit_cast<int32_t>(slice<24, 20>(word) << 0) >> 0;
instr.m_imm = std::bit_cast<int32_t>(slice<24, 20>(word) << 0) >> 0;
return instr;
}
case 0b110011: {
Expand Down
5 changes: 5 additions & 0 deletions src/isa/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
set(COMPONENT_NAME isa)

if(PROT_BUILD_TESTS)
prot_add_component_utest(${COMPONENT_NAME} PROT::isa)
endif()
Loading