Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ErrorHandler : public asmjit::ErrorHandler {
- public:
- void handleError(asmjit::Error error, const char* msg, asmjit::BaseEmitter* emitter) {
- std::cerr << error << ' ' << msg << '\n';
- }
- };
- struct object {
- uint64_t one;
- uint64_t two;
- };
- namespace x86 = asmjit::x86;
- using map = std::unordered_map<std::string, object>;
- object* get_from_table(map* table, std::string* str) {
- return &(*table)[*str];
- }
- int main() {
- std::unordered_map<std::string, object> table;
- std::string index = "Hello, world!";
- table[index] = { 1, 1 };
- asmjit::JitRuntime runtime;
- asmjit::CodeHolder code;
- code.init(runtime.environment());
- code.setErrorHandler(new ErrorHandler);
- x86::Compiler c(&code);
- c.addFunc(asmjit::FuncSignatureT<object*>());
- c.mov(x86::rax, (uint64_t)&table);
- c.mov(x86::rbx, (uint64_t)&index);
- asmjit::InvokeNode* node;
- c.invoke(&node, (uint64_t)get_from_table, asmjit::FuncSignatureT<object*, map*, std::string*>());
- node->setArg(0, x86::rax);
- node->setArg(1, x86::rbx);
- node->setRet(0, x86::rax);
- c.ret(x86::rax);
- c.endFunc();
- c.finalize();
- object* (*function)();
- runtime.add(&function, &code);
- function();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement