This is the mail archive of the libffi-discuss@sourceware.org mailing list for the libffi project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Closures without VA-stuff


On Mon, Dec 21, 2009 at 11:54:20AM +0000, Andrew Haley wrote:
<snip>
> I love the way a simple example
> 
>   struct delegate del = DELEGATE_INITIALIZER;
>   int m = 3;
>   int n = 4;
> 
>   delegate(&del, &plus, m, n);
>   ffi_arg res = invoke(&del);
> 
> expands into some nightmarish C code (I won't reproduce it here) which then
> collapses into

I had to go through a few interations of macro solutions. The first time I
got it all working the macro expansions were out-of-control, and could
increase compilation time considerably. With some reordering (which may or
may not have improved readability; I forget) they're just about optimal.

>   del = {};
>   m = 3;
>   n = 4;
>   {
>     {
>       int32_t argT;
> 
>       D.3399 = n;
>       D.3575 = &del.args + 16;
>       memcpy (D.3575, &D.3399, 4);
>       del.types[1] = &ffi_type_sint32;
>       D.3400 = del.types[1];
>     }
>     {
>       int32_t argT;
> 
>       D.3415 = m;
>       D.3576 = (int32_t * {ref-all}) &del.args;
>       D.3577 = D.3415;
>       *D.3576 = D.3577;
>       del.types[0] = &ffi_type_sint32;
>       D.3416 = del.types[0];
>     }
>     plus.17 = (void (*<T132>) (void)) plus;
>     D.3579 = delegate_init (&del, &ffi_type_sint32, plus.17, 2);
>     D.3573 = D.3579;
>   }
>   res.18 = invoke (&del);
> 
> (I can't help wondering, though, if C++ template metaprogramming
> wouldn't be a more natural home for this kind of thing.  But that's
> just me :-)
> 

I don't know C++ but you may be right. However the ternary statement tree
and other code for resolving the FFI type is long but fairly
straight-forward. Likewise statement expressions are pretty regular
GCC'isms. The real problem was code generation for the argument list in
general, not for each argument individually.

Rather than bother learning C++ I'll just continue with these hacks, biding
my time until Apple's Blocks makes it into GCC mainline. From what I've read
C++'s lambda expressions have scoping issues which make them useless for my
uses so I don't have much incentive there.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]