Skip to content

Commit

Permalink
#42 condition instr
Browse files Browse the repository at this point in the history
  • Loading branch information
levBagryansky committed Nov 1, 2023
1 parent 9922b98 commit 0950230
Show file tree
Hide file tree
Showing 7 changed files with 448 additions and 91 deletions.
37 changes: 26 additions & 11 deletions include/ChaiVM/interpreter/executor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,33 @@ class Executor {
void icsqrt(Instruction ins);
void icsin(Instruction ins);
void iccos(Instruction ins);
void if_icmpeq(Instruction ins);
void if_icmpne(Instruction ins);
void if_icmpgt(Instruction ins);
void if_icmpge(Instruction ins);
void if_icmplt(Instruction ins);
void if_icmple(Instruction ins);
void if_acmpeq(Instruction ins);
void if_acmpne(Instruction ins);
void cmpgf(Instruction ins);
void cmplf(Instruction ins);
void g0t0(Instruction ins);

static constexpr Handler handlerArr[] = {
&Executor::inv, &Executor::nop, &Executor::ret,
&Executor::mov, &Executor::ldia, &Executor::ldra,
&Executor::star, &Executor::add, &Executor::addi,
&Executor::sub, &Executor::subi, &Executor::mul,
&Executor::muli, &Executor::div, &Executor::divi,
&Executor::ldiaf, &Executor::addf, &Executor::addif,
&Executor::subf, &Executor::subif, &Executor::mulf,
&Executor::mulif, &Executor::divf, &Executor::divif,
&Executor::icprint, &Executor::icscani, &Executor::icscanf,
&Executor::icsqrt, &Executor::icsin, &Executor::iccos};
static constexpr Handler HANDLER_ARR[] = {
&Executor::inv, &Executor::nop, &Executor::ret,
&Executor::mov, &Executor::ldia, &Executor::ldra,
&Executor::star, &Executor::add, &Executor::addi,
&Executor::sub, &Executor::subi, &Executor::mul,
&Executor::muli, &Executor::div, &Executor::divi,
&Executor::ldiaf, &Executor::addf, &Executor::addif,
&Executor::subf, &Executor::subif, &Executor::mulf,
&Executor::mulif, &Executor::divf, &Executor::divif,
&Executor::icprint, &Executor::icscani, &Executor::icscanf,
&Executor::icsqrt, &Executor::icsin, &Executor::iccos,
&Executor::if_icmpeq, &Executor::if_icmpne, &Executor::if_icmpgt,
&Executor::if_icmpge, &Executor::if_icmplt, &Executor::if_icmple,
&Executor::if_acmpeq, &Executor::if_acmpne, &Executor::cmpgf,
&Executor::cmplf, &Executor::g0t0};

private:
CodeManager *codeManager_;
Expand Down
2 changes: 2 additions & 0 deletions include/ChaiVM/utils/instr2Raw.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ chai::bytecode_t instr2Raw(Operation op, Immidiate imm);

chai::bytecode_t instr2Raw(Operation op);

chai::bytecode_t inst2RawRI(Operation op, RegisterId r1, Immidiate imm);

} // namespace chai::utils
2 changes: 1 addition & 1 deletion src/ChaiVM/interpreter/decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace chai::interpreter::decoder {

Instruction parse(bytecode_t word) {
const Opcode opcode = utils::ExtractBits<bytecode_t, 8, 0>(word);
assert(opcode <= IcCos);
assert(opcode <= Goto);
return Instruction{
.operation = static_cast<Operation>(opcode),
.immidiate = utils::ExtractBits<bytecode_t, Immidiate, 16, 16>(word),
Expand Down
87 changes: 86 additions & 1 deletion src/ChaiVM/interpreter/executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace chai::interpreter {
#define DO_NEXT_INS() \
Instruction newIns = \
decoder::parse(codeManager_->getBytecode(regFile_.pc())); \
(this->*handlerArr[newIns.operation])(newIns);
(this->*HANDLER_ARR[newIns.operation])(newIns);

Executor::Executor(CodeManager *manager)
: codeManager_(manager), regFile_(codeManager_->startPC()) {}
Expand Down Expand Up @@ -191,6 +191,91 @@ void Executor::iccos(Instruction ins) {
advancePc();
DO_NEXT_INS()
}
void Executor::if_icmpeq(Instruction ins) {
if (regFile_.acc() == regFile_[ins.r1]) {
static_assert(sizeof(Immidiate) == sizeof(int16_t));
regFile_.pc() += static_cast<int16_t>(ins.immidiate);
} else {
advancePc();
}
DO_NEXT_INS()
}
void Executor::if_icmpne(Instruction ins) {
if (regFile_.acc() != regFile_[ins.r1]) {
regFile_.pc() += static_cast<int16_t>(ins.immidiate);
} else {
advancePc();
}
DO_NEXT_INS()
}
void Executor::if_icmpgt(Instruction ins) {
if (regFile_.acc() > regFile_[ins.r1]) {
regFile_.pc() += static_cast<int16_t>(ins.immidiate);
} else {
advancePc();
}
DO_NEXT_INS()
}
void Executor::if_icmpge(Instruction ins) {
if (regFile_.acc() >= regFile_[ins.r1]) {
regFile_.pc() += static_cast<int16_t>(ins.immidiate);
} else {
advancePc();
}
DO_NEXT_INS()
}
void Executor::if_icmplt(Instruction ins) {
if (regFile_.acc() < regFile_[ins.r1]) {
regFile_.pc() += static_cast<int16_t>(ins.immidiate);
} else {
advancePc();
}
DO_NEXT_INS()
}
void Executor::if_icmple(Instruction ins) {
if (regFile_.acc() <= regFile_[ins.r1]) {
regFile_.pc() += static_cast<int16_t>(ins.immidiate);
} else {
advancePc();
}
DO_NEXT_INS()
}
void Executor::if_acmpeq(Instruction ins) {
/*
* @todo #42:90min Implement the instruction with object ref
* when objects will be introduced in chai.
*/
DO_NEXT_INS()
}
void Executor::if_acmpne(Instruction ins) {
/*
* @todo #42:90min Implement the instruction with object ref
* when chai objects will be introduced.
*/
DO_NEXT_INS()
}
void Executor::cmpgf(Instruction ins) {
double acc_f64 = std::bit_cast<double>(regFile_.acc());
double r1_f64 = std::bit_cast<double>(regFile_[ins.r1]);
regFile_.acc() = (acc_f64 >= r1_f64)
? static_cast<size_t>(acc_f64 != r1_f64)
: static_cast<size_t>(-1);
advancePc();
DO_NEXT_INS()
}
void Executor::cmplf(Instruction ins) {
double acc_f64 = std::bit_cast<double>(regFile_.acc());
double r1_f64 = std::bit_cast<double>(regFile_[ins.r1]);
regFile_.acc() = regFile_.acc() =
(acc_f64 <= r1_f64) ? static_cast<size_t>(acc_f64 != r1_f64)
: static_cast<size_t>(-1);
advancePc();
DO_NEXT_INS()
}
void Executor::g0t0(Instruction ins) {
regFile_.pc() += static_cast<int16_t>(ins.immidiate);
DO_NEXT_INS()
}

InvalidInstruction::InvalidInstruction(const char *msg) : runtime_error(msg) {}
InvalidInstruction::InvalidInstruction(const std::string &msg)
Expand Down
5 changes: 5 additions & 0 deletions src/ChaiVM/utils/instr2Raw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ chai::bytecode_t instr2Raw(Operation op, Immidiate imm) {

chai::bytecode_t instr2Raw(Operation op) { return (operation2opcode(op)); }

chai::bytecode_t inst2RawRI(Operation op, RegisterId r1, Immidiate imm) {
return (operation2opcode(op)) | (static_cast<chai::bytecode_t>(imm) << 16) |
(r1 << 8);
}

} // namespace chai::utils
Loading

3 comments on commit 0950230

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 0950230 Nov 1, 2023

Choose a reason for hiding this comment

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

Puzzle 42-994681db discovered in src/ChaiVM/interpreter/executor.cpp) and submitted as #52. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 0950230 Nov 1, 2023

Choose a reason for hiding this comment

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

Puzzle 42-a69e7a05 discovered in src/ChaiVM/interpreter/executor.cpp) and submitted as #53. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 0950230 Nov 1, 2023

Choose a reason for hiding this comment

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

Puzzle 42-1789e504 discovered in test/ChaiVM/interpreter/executor_test.cpp) and submitted as #54. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.