<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br><div><div>On 27.09.2010, at 19:27, Eliot Miranda wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><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'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'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'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.<br></blockquote><div><br>
</div><div>While the image-level facilities may not exist the VM provides&nbsp;primitiveCalloutWithArgs which implements ExternalFunction&gt;&gt;invokeWithArguments: and that could be used to do varargs calls. &nbsp;One has to synthesize the ExternalFunction because the primitive still needs a type vector for the arguments. &nbsp; But I think it could do the job.</div></div></blockquote><div><br></div><div>No it can't. Maybe re-read what I wrote when you have a little more time ;)</div><br><blockquote type="cite"><div class="gmail_quote"><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>
</blockquote></div><br><div>
<span class="Apple-style-span" style="border-collapse: separate; border-spacing: 0px 0px; color: rgb(0, 0, 0); font-family: Lucida Grande; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-align: auto; -khtml-text-decorations-in-effect: none; text-indent: 0px; -apple-text-size-adjust: auto; text-transform: none; orphans: 2; white-space: normal; widows: 2; word-spacing: 0px; "><div style="font-family: Helvetica; "><span class="Apple-style-span" style="font-family: Helvetica; ">- Bert -</span></div><br class="Apple-interchange-newline"></span>
</div>
<br></body></html>