FFI: Vararg function how to tell callee how many arguments i passed?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

FFI: Vararg function how to tell callee how many arguments i passed?

Igor Stasenko
 
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?

--
Best regards,
Igor Stasenko AKA sig.
Reply | Threaded
Open this post in threaded view
|

Re: FFI: Vararg function how to tell callee how many arguments i passed?

Levente Uzonyi-2
 
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.
:)


Levente

>
> --
> Best regards,
> Igor Stasenko AKA sig.
>
Reply | Threaded
Open this post in threaded view
|

Re: FFI: Vararg function how to tell callee how many arguments i passed?

Igor Stasenko
 
On 27 September 2010 02:12, Levente Uzonyi <[hidden email]> 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.

>
> Levente
>
>>
>> --
>> Best regards,
>> Igor Stasenko AKA sig.
>>
>



--
Best regards,
Igor Stasenko AKA sig.
Reply | Threaded
Open this post in threaded view
|

Re: FFI: Vararg function how to tell callee how many arguments i passed?

Bert Freudenberg


On 27.09.2010, at 01:23, Igor Stasenko wrote:

>
> On 27 September 2010 02:12, Levente Uzonyi <[hidden email]> 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.

- Bert -

(*) http://c-faq.com/varargs/invvarargs.html
Reply | Threaded
Open this post in threaded view
|

Re: FFI: Vararg function how to tell callee how many arguments i passed?

Eliot Miranda-2
 


On Mon, Sep 27, 2010 at 2:28 AM, Bert Freudenberg <[hidden email]> wrote:


On 27.09.2010, at 01:23, Igor Stasenko wrote:

>
> On 27 September 2010 02:12, Levente Uzonyi <[hidden email]> 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.

cheers
Eliot

- Bert -

(*) http://c-faq.com/varargs/invvarargs.html

Reply | Threaded
Open this post in threaded view
|

Re: FFI: Vararg function how to tell callee how many arguments i passed?

Bert Freudenberg
 

On 27.09.2010, at 19:27, Eliot Miranda wrote:



On Mon, Sep 27, 2010 at 2:28 AM, Bert Freudenberg <[hidden email]> wrote:


On 27.09.2010, at 01:23, Igor Stasenko wrote:

>
> On 27 September 2010 02:12, Levente Uzonyi <[hidden email]> 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 ;)



- Bert -


Reply | Threaded
Open this post in threaded view
|

Re: FFI: Vararg function how to tell callee how many arguments i passed?

Igor Stasenko

On 27 September 2010 21:21, Bert Freudenberg <[hidden email]> wrote:

>
>
> On 27.09.2010, at 19:27, Eliot Miranda wrote:
>
>
> On Mon, Sep 27, 2010 at 2:28 AM, Bert Freudenberg <[hidden email]> wrote:
>>
>>
>> On 27.09.2010, at 01:23, Igor Stasenko wrote:
>>
>> >
>> > On 27 September 2010 02:12, Levente Uzonyi <[hidden email]> 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.
Reply | Threaded
Open this post in threaded view
|

Re: FFI: Vararg function how to tell callee how many arguments i passed?

Bert Freudenberg
In reply to this post by Bert Freudenberg
 

On 27.09.2010, at 20:21, Bert Freudenberg wrote:


On 27.09.2010, at 19:27, Eliot Miranda wrote:



On Mon, Sep 27, 2010 at 2:28 AM, Bert Freudenberg <[hidden email]> wrote:


On 27.09.2010, at 01:23, Igor Stasenko wrote:

>
> On 27 September 2010 02:12, Levente Uzonyi <[hidden email]> 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 ;)

Retract that.

A little off-line conversation with Eliot convinced me that the parameter passing isn't actually that much different between regular and varagrs calls (if at all). The FFI already does all the non-portable ABI-dependent trickery needed to set up the call. So this might in fact just work :)

Anyone up for trying?

- Bert -


Reply | Threaded
Open this post in threaded view
|

Re: FFI: Vararg function how to tell callee how many arguments i passed?

Igor Stasenko

On 28 September 2010 00:22, Bert Freudenberg <[hidden email]> wrote:

>
>
> On 27.09.2010, at 20:21, Bert Freudenberg wrote:
>
> On 27.09.2010, at 19:27, Eliot Miranda wrote:
>
>
> On Mon, Sep 27, 2010 at 2:28 AM, Bert Freudenberg <[hidden email]> wrote:
>>
>>
>> On 27.09.2010, at 01:23, Igor Stasenko wrote:
>>
>> >
>> > On 27 September 2010 02:12, Levente Uzonyi <[hidden email]> 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 ;)
>
> Retract that.
> A little off-line conversation with Eliot convinced me that the parameter passing isn't actually that much different between regular and varagrs calls (if at all). The FFI already does all the non-portable ABI-dependent trickery needed to set up the call. So this might in fact just work :)
> Anyone up for trying?

Well, i made a callout to printf() via NativeBoost in Linux,
but was unable to determine if it works, because when i run it, it
prints nothing on console.
Probably because stdout is closed by default and i need to reopen it first.

> - Bert -
>

--
Best regards,
Igor Stasenko AKA sig.
Reply | Threaded
Open this post in threaded view
|

Re: FFI: Vararg function how to tell callee how many arguments i passed?

Eliot Miranda-2
 


On Mon, Sep 27, 2010 at 3:12 PM, Igor Stasenko <[hidden email]> wrote:

On 28 September 2010 00:22, Bert Freudenberg <[hidden email]> wrote:
>
>
> On 27.09.2010, at 20:21, Bert Freudenberg wrote:
>
> On 27.09.2010, at 19:27, Eliot Miranda wrote:
>
>
> On Mon, Sep 27, 2010 at 2:28 AM, Bert Freudenberg <[hidden email]> wrote:
>>
>>
>> On 27.09.2010, at 01:23, Igor Stasenko wrote:
>>
>> >
>> > On 27 September 2010 02:12, Levente Uzonyi <[hidden email]> 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 ;)
>
> Retract that.
> A little off-line conversation with Eliot convinced me that the parameter passing isn't actually that much different between regular and varagrs calls (if at all). The FFI already does all the non-portable ABI-dependent trickery needed to set up the call. So this might in fact just work :)
> Anyone up for trying?

Well, i made a callout to printf() via NativeBoost in Linux,
but was unable to determine if it works, because when i run it, it
prints nothing on console.
Probably because stdout is closed by default and i need to reopen it first.

Did you try using a console VM (if you're using Cog that's CroquetConsole.exe)?

e.


> - Bert -
>

--
Best regards,
Igor Stasenko AKA sig.

Reply | Threaded
Open this post in threaded view
|

Re: FFI: Vararg function how to tell callee how many arguments i passed?

Levente Uzonyi-2
In reply to this post by Igor Stasenko
 
On Tue, 28 Sep 2010, Igor Stasenko wrote:

>
> On 28 September 2010 00:22, Bert Freudenberg <[hidden email]> wrote:
>>
>>
>> On 27.09.2010, at 20:21, Bert Freudenberg wrote:
>>
>> On 27.09.2010, at 19:27, Eliot Miranda wrote:
>>
>>
>> On Mon, Sep 27, 2010 at 2:28 AM, Bert Freudenberg <[hidden email]> wrote:
>>>
>>>
>>> On 27.09.2010, at 01:23, Igor Stasenko wrote:
>>>
>>>>
>>>> On 27 September 2010 02:12, Levente Uzonyi <[hidden email]> 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 ;)
>>
>> Retract that.
>> A little off-line conversation with Eliot convinced me that the parameter passing isn't actually that much different between regular and varagrs calls (if at all). The FFI already does all the non-portable ABI-dependent trickery needed to set up the call. So this might in fact just work :)
>> Anyone up for trying?
>
> Well, i made a callout to printf() via NativeBoost in Linux,
> but was unable to determine if it works, because when i run it, it
> prints nothing on console.
> Probably because stdout is closed by default and i need to reopen it first.
You can always use fprintf :). A few years ago I wrote an incomplete API
for stdio on windows which worked like this:

fprintf := ExternalLibraryFunction
  name: 'fprintf'
  module: 'msvcrt.dll'
  callType: ExternalFunction callTypeCDecl
  returnType: ExternalType signedLong
  argumentTypes: {
  (ExternalType structTypeNamed: #FILE) asPointerType.
  ExternalType string.
  ExternalType signedLong }.
file := Stdio default fopenWith: 'test.txt' with: 'w'.
fprintf invokeWith: file with: 'Your number is %d.' with: 42.
Stdio default fcloseWith: file.

I didn't bother writing a parser which extracts the type information
from the format string, but if that's done it's pretty easy to do the
rest. Caching the functions probably improves the performance a lot. I can
imagine two caches (for *printf functions):
- a larger cache which has typeInfo -> externalFunction mapping
- a smaller cache which maps formatString -> typeInfo

The problem with this method is that FILE structure is platform specific.


Levente


>
>> - Bert -
>>
>
> --
> Best regards,
> Igor Stasenko AKA sig.
>
Reply | Threaded
Open this post in threaded view
|

Re: FFI: Vararg function how to tell callee how many arguments i passed?

Igor Stasenko
In reply to this post by Eliot Miranda-2

On 28 September 2010 01:57, Eliot Miranda <[hidden email]> wrote:

>
>
>
> On Mon, Sep 27, 2010 at 3:12 PM, Igor Stasenko <[hidden email]> wrote:
>>
>> On 28 September 2010 00:22, Bert Freudenberg <[hidden email]> wrote:
>> >
>> >
>> > On 27.09.2010, at 20:21, Bert Freudenberg wrote:
>> >
>> > On 27.09.2010, at 19:27, Eliot Miranda wrote:
>> >
>> >
>> > On Mon, Sep 27, 2010 at 2:28 AM, Bert Freudenberg <[hidden email]> wrote:
>> >>
>> >>
>> >> On 27.09.2010, at 01:23, Igor Stasenko wrote:
>> >>
>> >> >
>> >> > On 27 September 2010 02:12, Levente Uzonyi <[hidden email]> 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 ;)
>> >
>> > Retract that.
>> > A little off-line conversation with Eliot convinced me that the parameter passing isn't actually that much different between regular and varagrs calls (if at all). The FFI already does all the non-portable ABI-dependent trickery needed to set up the call. So this might in fact just work :)
>> > Anyone up for trying?
>>
>> Well, i made a callout to printf() via NativeBoost in Linux,
>> but was unable to determine if it works, because when i run it, it
>> prints nothing on console.
>> Probably because stdout is closed by default and i need to reopen it first.
>
> Did you try using a console VM (if you're using Cog that's CroquetConsole.exe)?

Oh. I didn't not yet started adopting NB with Cog.
But im about to do it in few days.

> e.
>>
>> > - Bert -
>> >
>>
>> --
>> Best regards,
>> Igor Stasenko AKA sig.
>
>
>



--
Best regards,
Igor Stasenko AKA sig.