From e0747b982d1d5f147d2b29fbfcb4fb41b6706237 Mon Sep 17 00:00:00 2001 From: lvntky Date: Sat, 22 Jun 2024 14:12:22 +0300 Subject: [PATCH] [feature] execution methods added --- include/cvm/execute_engine/cvm_execute.hpp | 3 +- src/execute_engine/cvm_execute.cpp | 83 ++++++++++++++++++++++ src/main.cpp | 6 +- 3 files changed, 90 insertions(+), 2 deletions(-) diff --git a/include/cvm/execute_engine/cvm_execute.hpp b/include/cvm/execute_engine/cvm_execute.hpp index 4de2f97..24af29a 100644 --- a/include/cvm/execute_engine/cvm_execute.hpp +++ b/include/cvm/execute_engine/cvm_execute.hpp @@ -17,7 +17,8 @@ class CVM private: std::string getUtf8FromConstantPool(const Classfile& cf, uint16_t index); const method_info* findMehodByName(const Classfile& cf, const std::string& methodName); - const uint8_t* getByteCode(const method_info* methodInfo); + const uint8_t* getByteCode(const Classfile& cf, const method_info* methodInfo); + void interprete(const uint8_t* byteCode, const Classfile& cf); }; #endif //__CVM_EXECUTE_HPP__ diff --git a/src/execute_engine/cvm_execute.cpp b/src/execute_engine/cvm_execute.cpp index 159f0c2..50ad202 100644 --- a/src/execute_engine/cvm_execute.cpp +++ b/src/execute_engine/cvm_execute.cpp @@ -1 +1,84 @@ #include "../../include/cvm/execute_engine/cvm_execute.hpp" + +#include + +/** + * Helper methods + * for validating execution and + * interpreting + */ + +void validateMethod(const method_info* methodInfo, std::string methodName) +{ + if (methodInfo != nullptr) + { + spdlog::info("{} The given [method: {}] has been found on classfile", LOG_OK, methodName); + } + else + { + spdlog::error("{} The given [method: {}] cannot be foudn on classfile", LOG_NOK, methodName); + throw std::runtime_error("Method not found"); + } +} + +void validateByteCode(const uint8_t* byteCode) +{ + if (byteCode != nullptr) + { + spdlog::info("{} Bytcode is obtained from given method:", LOG_OK); + } + else + { + throw std::runtime_error("Could not get bytecode for given method"); + } +} + +/** + * Helper methods ends + */ + +void CVM::execute(const Classfile& cf, const std::string& methodName) +{ + spdlog::info("CVM executing method: {} on parsed classfile.", methodName); + const method_info* methodInfo = findMehodByName(cf, methodName); + validateMethod(methodInfo, methodName); + + const uint8_t* bytecode = getByteCode(cf, methodInfo); + validateByteCode(bytecode); +} + +std::string CVM::getUtf8FromConstantPool(const Classfile& cf, uint16_t index) +{ + const cp_info& cpEntry = cf.constant_pool.at(index - 1); + if (cpEntry.tag == CONSTANT_Utf8) + { + return std::string(cpEntry.info.Utf8.bytes, cpEntry.info.Utf8.bytes + cpEntry.info.Utf8.length); + } + return ""; +} + +const method_info* CVM::findMehodByName(const Classfile& cf, const std::string& methodName) +{ + for (auto& method : cf.methods) + { + std::string name = getUtf8FromConstantPool(cf, method.name_index); + if (name == methodName) + { + return &method; + } + } + return nullptr; +} + +const uint8_t* CVM::getByteCode(const Classfile& cf, const method_info* methodInfo) +{ + for (auto& attr : methodInfo->attributes) + { + std::string codeAttribute = getUtf8FromConstantPool(cf, attr.attribute_name_index); + if (codeAttribute == "Code") + { + return attr.info; + } + } + return nullptr; +} diff --git a/src/main.cpp b/src/main.cpp index e43523b..47f7053 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,8 @@ #include "../include/cvm/banner.hpp" #include "../include/cvm/classfile/classfile.hpp" +#include "../include/cvm/execute_engine/cvm_execute.hpp" #include "../include/cvm/fmt_commons.hpp" +#include "../include/cvm/stack/cvm_stack.hpp" #include "../include/cvm/stack/frame.hpp" int main(int argc, char** argv) @@ -35,7 +37,9 @@ int main(int argc, char** argv) Classfile cf; cf = cf.parseClassfile(buffer.data(), fileSize); - classFile.close(); + CVM cvm; + cvm.execute(cf, "add"); + classFile.close(); return 0; }