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

Virgile Bello virgile.bello at gmail.com
Sat Nov 29 23:46:03 PST 2014


My bad, it would be captured as well during thunk allocation in a
ThunkIdToThis[] array, and last line should be:
    - RedirectMethodFuncWithIntParameter would call
ThunkIdToFuncPtr[X](ThunkIdToThis[X], arg1)
On Nov 30, 2014 4:32 PM, "Sanjoy Das" <sanjoy at playingwithpointers.com>
wrote:

> 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20141130/7e856cf2/attachment.html>


More information about the llvm-dev mailing list