Skip to content

Commit

Permalink
[feature] mroe opcodes
Browse files Browse the repository at this point in the history
  • Loading branch information
lvntky committed Jun 23, 2024
1 parent baf9986 commit e03ee8b
Showing 1 changed file with 42 additions and 8 deletions.
50 changes: 42 additions & 8 deletions src/execute_engine/cvm_execute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,33 @@ void validateByteCode(const uint8_t* byteCode) {
}
}

std::string printOperandStack(std::vector<int> osp) {
std::string osStr = "[";
for (size_t i = 0; i < osp.size(); i++) {
osStr += std::to_string(osp.at(i));
if (i != osp.size() - 1) {
osStr += ", ";
}
}
osStr += "]";
return osStr;
}

void pushOperandStack(std::stack<int>& operandStack,
std::vector<int>& operandStackPrint, int value) {
operandStack.push(value);
operandStackPrint.push_back(value);
}

int popOperandStack(std::stack<int>& operandStack,
std::vector<int> operandStackPrint) {
int value = operandStack.top();
operandStack.pop();

operandStackPrint.pop_back();

return value;
}
/**
* Helper methods ends
*/
Expand Down Expand Up @@ -83,6 +110,8 @@ const uint8_t* CVM::getByteCode(const Classfile& cf,
void CVM::interprete(const uint8_t* byteCode, const Classfile& cf) {
size_t pc = 0;
std::stack<int> operandStack;
std::vector<int>
operandStackPrint; // for printing elements of original operandStack

while (pc < codeLength) {
uint8_t opcode = byteCode[pc++];
Expand All @@ -91,18 +120,23 @@ void CVM::interprete(const uint8_t* byteCode, const Classfile& cf) {
spdlog::info("==> Opcode: {:#X} - NOP - DO NOTHING", opcode);
break;
case 0x2:
operandStack.push(-1);
pushOperandStack(operandStack, operandStackPrint, -1);
spdlog::info(
"==> Opcode {:#X} - iconst_m1 - Load m1 to the operand stack: "
"<TOP: {}>",
opcode, operandStack.top());
"==> Opcode {:#X} - iconst_m1 - Load m1 to the operand stack: {}",
opcode, printOperandStack(operandStackPrint));
break;
case 0x3:
operandStack.push(0);
pushOperandStack(operandStack, operandStackPrint, 0);

spdlog::info(
"==> Opcode {:#X} - iconst_0 - Load 0 to the operand stack: {}",
opcode, printOperandStack(operandStackPrint));
break;
case 0x4:
pushOperandStack(operandStack, operandStackPrint, 1);
spdlog::info(
"==> Opcode {:#X} - iconst_0 - Load 0 to the operand stack: <TOP: "
"{}>",
opcode, operandStack.top());
"==> Opcode {:#X} - iconst_1 - Load 1 to the operand stack: {}",
opcode, printOperandStack(operandStackPrint));
break;
default:
spdlog::error("Unknown Opcode: {:#x} at PC: {}", opcode, pc);
Expand Down

0 comments on commit e03ee8b

Please sign in to comment.