KeystoneAssembler.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #include "KeystoneAssembler.hpp"
  2. namespace nkg {
  3. KeystoneAssembler::KeystoneAssembler(const KeystoneEngine& Engine) noexcept :
  4. m_Engine(Engine) {}
  5. [[nodiscard]]
  6. std::vector<uint8_t> KeystoneAssembler::GenerateMachineCode(std::string_view AssemblyCode, uint64_t Address) const {
  7. ARL::ResourceWrapper pbMachineCode(ARL::ResourceTraits::KeystoneMalloc{});
  8. size_t cbMachineCode = 0;
  9. size_t InstructionsProcessed = 0;
  10. if (ks_asm(m_Engine, AssemblyCode.data(), Address, pbMachineCode.GetAddressOf(), &cbMachineCode, &InstructionsProcessed) != 0) {
  11. throw ARL::KeystoneError(__BASE_FILE__, __LINE__, ks_errno(m_Engine), "ks_asm failed.");
  12. }
  13. return std::vector<uint8_t>(pbMachineCode.Get(), pbMachineCode.Get() + cbMachineCode);
  14. }
  15. KeystoneEngine::KeystoneEngine(ks_arch ArchType, ks_mode Mode) {
  16. auto err = ks_open(ArchType, Mode, GetAddressOf());
  17. if (err != KS_ERR_OK) {
  18. throw ARL::KeystoneError(__BASE_FILE__, __LINE__, err, "ks_open failed.");
  19. }
  20. }
  21. void KeystoneEngine::Option(ks_opt_type Type, ks_opt_value Value) {
  22. auto err = ks_option(Get(), Type, Value);
  23. if (err != KS_ERR_OK) {
  24. throw ARL::KeystoneError(__BASE_FILE__, __LINE__, err, "ks_option failed.");
  25. }
  26. }
  27. KeystoneAssembler KeystoneEngine::CreateAssembler() const {
  28. return KeystoneAssembler(*this);
  29. }
  30. }