-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
90 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters