[LLVMdev] Frontend: How to use Member to Function Pointer as callbacks

Sanjoy Das sanjoy at playingwithpointers.com
Sat Nov 29 23:31:55 PST 2014


Hi Virgile,

> One (rather complicated) option I was thinking is:
> - Define a maximum number of those callback alive (let's say 4096) -- it's
> not per function signature, but global
> - Define i8* thunkTargets[4096]
> - Define i8* ThunkIdToFuncPtr[4096]
> - Define 4096 funcs (not even sure how to do that with LLVM, might need to
> emit assembly?)
>     Thunk0: jmp thunkTargets[0];
>     Thunk1: jmp thunkTargets[1];
>     ...
>     Thunk4095: jmp thunkTargets[4095];
>
> - When I call a C function from C# with a callback, what happens is:
>   - Find an unused slot in this thunk table (X)
>   - Register C# member to function pointer in ThunkIdToFuncPtr[X]
>   - Replace thunkTargets[X] with pointer address to
> "RedirectMethodFuncWithIntParameter" (one such function per callback
> signature)
>     - This redirect method would receive arguments unmodified from C
> functions (since previous call was a simple jmp)
>     - It would check in the call stack the current slot X being called (up
> in the callstack, if call instruction is "call Thunk3" from address Thunk3
> we know X is 3 -- it will need assembly, might be difficult to compute and
> won't be portable...)
>     - ThunkIdToFuncPtr[X] would give us the actual method to forward to
>     - RedirectMethodFuncWithIntParameter would call
> ThunkIdToFuncPtr[X](arg1)

I couldn't quite understand this approach -- at what point do you
figure out what the value of 'this' is?

-- Sanjoy



More information about the llvm-dev mailing list