Advertisement
Guest User

ASMJIT Code

a guest
Oct 11th, 2020
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.17 KB | None | 0 0
  1. class ErrorHandler : public asmjit::ErrorHandler {
  2. public:
  3.     void handleError(asmjit::Error error, const char* msg, asmjit::BaseEmitter* emitter) {
  4.         std::cerr << error << ' ' << msg << '\n';
  5.     }
  6. };
  7.  
  8. struct object {
  9.     uint64_t one;
  10.     uint64_t two;
  11. };
  12.  
  13. namespace x86 = asmjit::x86;
  14.  
  15. using map = std::unordered_map<std::string, object>;
  16.  
  17. object* get_from_table(map* table, std::string* str) {
  18.     return &(*table)[*str];
  19. }
  20.  
  21. int main() {
  22.     std::unordered_map<std::string, object> table;
  23.     std::string index = "Hello, world!";
  24.     table[index] = { 1, 1 };
  25.  
  26.     asmjit::JitRuntime runtime;
  27.     asmjit::CodeHolder code;
  28.     code.init(runtime.environment());
  29.     code.setErrorHandler(new ErrorHandler);
  30.     x86::Compiler c(&code);
  31.  
  32.     c.addFunc(asmjit::FuncSignatureT<object*>());
  33.  
  34.     c.mov(x86::rax, (uint64_t)&table);
  35.     c.mov(x86::rbx, (uint64_t)&index);
  36.  
  37.     asmjit::InvokeNode* node;
  38.     c.invoke(&node, (uint64_t)get_from_table, asmjit::FuncSignatureT<object*, map*, std::string*>());
  39.     node->setArg(0, x86::rax);
  40.     node->setArg(1, x86::rbx);
  41.     node->setRet(0, x86::rax);
  42.  
  43.     c.ret(x86::rax);
  44.     c.endFunc();
  45.     c.finalize();
  46.  
  47.     object* (*function)();
  48.  
  49.     runtime.add(&function, &code);
  50.  
  51.     function();
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement