From e03ee8b12b23e1ac2d8007aa421ddd67e3ff2b09 Mon Sep 17 00:00:00 2001 From: lvntky Date: Sun, 23 Jun 2024 12:32:30 +0300 Subject: [PATCH] [feature] mroe opcodes --- src/execute_engine/cvm_execute.cpp | 50 +++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 8 deletions(-) diff --git a/src/execute_engine/cvm_execute.cpp b/src/execute_engine/cvm_execute.cpp index 08dfe9a..85c2f22 100644 --- a/src/execute_engine/cvm_execute.cpp +++ b/src/execute_engine/cvm_execute.cpp @@ -30,6 +30,33 @@ void validateByteCode(const uint8_t* byteCode) { } } +std::string printOperandStack(std::vector 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& operandStack, + std::vector& operandStackPrint, int value) { + operandStack.push(value); + operandStackPrint.push_back(value); +} + +int popOperandStack(std::stack& operandStack, + std::vector operandStackPrint) { + int value = operandStack.top(); + operandStack.pop(); + + operandStackPrint.pop_back(); + + return value; +} /** * Helper methods ends */ @@ -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 operandStack; + std::vector + operandStackPrint; // for printing elements of original operandStack while (pc < codeLength) { uint8_t opcode = byteCode[pc++]; @@ -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: " - "", - 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: ", - 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);