Skip to content

Instantly share code, notes, and snippets.

@7shi
Created September 19, 2011 05:03
Show Gist options
  • Save 7shi/1225997 to your computer and use it in GitHub Desktop.
Save 7shi/1225997 to your computer and use it in GitHub Desktop.
make thunk with JIT wrapper
#include <stdio.h>
#include "xbyak/xbyak.h"
template <class T>
struct Delegate : public Xbyak::CodeGenerator {
Delegate(T *t, void (T::*p)()) {
mov(ecx, reinterpret_cast<intptr_t>(t));
jmp(*reinterpret_cast<void **>(&p));
}
void (*getPtr())() {
return reinterpret_cast<void (*)()>(
const_cast<Xbyak::uint8 *>(getCode()));
}
};
struct Test1 {
int n;
Test1(int n) : n(n) {}
void show() {
printf("%d\n", n);
}
};
struct Test2 {
int n;
Test2(int n) : n(n) {}
void show() {
printf("%d\n", n);
}
};
struct Test3 {
static void show() {
printf("?\n");
}
};
void show() {
printf("!\n");
}
void call(void (*f)()) { f(); }
int main() {
Test1 t1(1);
Test2 t2(2);
Delegate<Test1> d1(&t1, &Test1::show);
Delegate<Test2> d2(&t2, &Test2::show);
call(d1.getPtr());
call(d2.getPtr());
call(Test3::show);
call(show);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment