[Vm-dev] FFI: Vararg function how to tell callee how many arguments i passed?

Igor Stasenko siguctua at gmail.com
Mon Sep 27 20:33:10 UTC 2010


On 27 September 2010 21:21, Bert Freudenberg <bert at freudenbergs.de> wrote:
>
>
> On 27.09.2010, at 19:27, Eliot Miranda wrote:
>
>
> On Mon, Sep 27, 2010 at 2:28 AM, Bert Freudenberg <bert at freudenbergs.de> wrote:
>>
>>
>> On 27.09.2010, at 01:23, Igor Stasenko wrote:
>>
>> >
>> > On 27 September 2010 02:12, Levente Uzonyi <leves at elte.hu> wrote:
>> >>
>> >> On Mon, 27 Sep 2010, Igor Stasenko wrote:
>> >>
>> >>>
>> >>> Suppose i want to call printf() using FFI.
>> >>> But it is a variable argument function. What should do to let it know
>> >>> how many arguments i passed?
>> >>
>> >> I think you don't tell it, it guesses the number of arguments from the first
>> >> argument. If you pass invalid arguments, something bad will happen. :)
>> >>
>> >
>> > Yeah.. i inspected the assembly (gcc -s) for printf call,
>> > and there is only pushes of arguments , no extra info.
>>
>> Vararg functions are specially compiled. They do not use the regular platform calling conventions. That's because the same compiled function needs to be able to take any type of argument. Whereas e.g. floats are normally passed on the float stack, in a vararg function call they might be passed on the regular stack. Also they are converted to doubles at the calling side, just as chars and shorts are promoted to ints.
>>
>> So at the calling site the compiler has to do special magic to construct the argument list. It can only do this if you actually provide a vararg call. That is, you need to literally write a printf() call to make this work. There is no portable way around this - the C FAQ states (*)
>>
>> Q: How can I call a function with an argument list built up at run time?
>> A: There is no guaranteed or portable way to do this.
>>
>> Squeak's FFI does not support vararg functions yet. There are other FFIs that do (CLISP's vacall library, Rubinius FFI), so taking a look there might help if anyone wants to implement this.
>
> While the image-level facilities may not exist the VM provides primitiveCalloutWithArgs which implements ExternalFunction>>invokeWithArguments: and that could be used to do varargs calls.  One has to synthesize the ExternalFunction because the primitive still needs a type vector for the arguments.   But I think it could do the job.
>
> No it can't. Maybe re-read what I wrote when you have a little more time ;)
>
I am actually was thinking that compiler passing a hidden 'number of arguments'
for vararg functions.

But from following example, i can conclude that its not.
Its up to developer to determine how many arguments (and what they types) passed
to function:

double average ( int num, ... )
{
  va_list arguments;                     // A place to store the list
of arguments
  double sum = 0;

  va_start ( arguments, num );           // Initializing arguments to
store all values after num
  for ( int x = 0; x < num; x++ )        // Loop until all numbers are added
    sum += va_arg ( arguments, double ); // Adds the next value in
argument list to sum.
  va_end ( arguments );                  // Cleans up the list

  return sum / num;                      // Returns some number
(typecast prevents truncation)
}

> cheers
> Eliot
>>
>> - Bert -
>>
>> (*) http://c-faq.com/varargs/invvarargs.html
>
> - Bert -
>
>
>



-- 
Best regards,
Igor Stasenko AKA sig.


More information about the Vm-dev mailing list