Skip to content

Commit

Permalink
[feature] execution methods added
Browse files Browse the repository at this point in the history
  • Loading branch information
lvntky committed Jun 22, 2024
1 parent 3ae2547 commit e0747b9
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 2 deletions.
3 changes: 2 additions & 1 deletion include/cvm/execute_engine/cvm_execute.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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__
83 changes: 83 additions & 0 deletions src/execute_engine/cvm_execute.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,84 @@
#include "../../include/cvm/execute_engine/cvm_execute.hpp"

#include <stdexcept>

/**
* 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;
}
6 changes: 5 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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;
}

0 comments on commit e0747b9

Please sign in to comment.