<br><br><div class="gmail_quote">On Mon, Sep 27, 2010 at 2:28 AM, Bert Freudenberg <span dir="ltr">&lt;<a href="mailto:bert@freudenbergs.de">bert@freudenbergs.de</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<br>
<br>
On 27.09.2010, at 01:23, Igor Stasenko wrote:<br>
<br>
&gt;<br>
&gt; On 27 September 2010 02:12, Levente Uzonyi &lt;<a href="mailto:leves@elte.hu">leves@elte.hu</a>&gt; wrote:<br>
&gt;&gt;<br>
&gt;&gt; On Mon, 27 Sep 2010, Igor Stasenko wrote:<br>
&gt;&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; Suppose i want to call printf() using FFI.<br>
&gt;&gt;&gt; But it is a variable argument function. What should do to let it know<br>
&gt;&gt;&gt; how many arguments i passed?<br>
&gt;&gt;<br>
&gt;&gt; I think you don&#39;t tell it, it guesses the number of arguments from the first<br>
&gt;&gt; argument. If you pass invalid arguments, something bad will happen. :)<br>
&gt;&gt;<br>
&gt;<br>
&gt; Yeah.. i inspected the assembly (gcc -s) for printf call,<br>
&gt; and there is only pushes of arguments , no extra info.<br>
<br>
Vararg functions are specially compiled. They do not use the regular platform calling conventions. That&#39;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.<br>

<br>
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 (*)<br>

<br>
Q: How can I call a function with an argument list built up at run time?<br>
A: There is no guaranteed or portable way to do this.<br>
<br>
Squeak&#39;s FFI does not support vararg functions yet. There are other FFIs that do (CLISP&#39;s vacall library, Rubinius FFI), so taking a look there might help if anyone wants to implement this.<br></blockquote><div><br>
</div><div>While the image-level facilities may not exist the VM provides primitiveCalloutWithArgs which implements ExternalFunction&gt;&gt;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.</div>
<div><br></div><div>cheers</div><div>Eliot</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<br>
- Bert -<br>
<br>
(*) <a href="http://c-faq.com/varargs/invvarargs.html" target="_blank">http://c-faq.com/varargs/invvarargs.html</a></blockquote></div><br>