<br><br><div class="gmail_quote">On Mon, Sep 27, 2010 at 3:12 PM, Igor Stasenko <span dir="ltr">&lt;<a href="mailto:siguctua@gmail.com">siguctua@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<br>
On 28 September 2010 00:22, Bert Freudenberg &lt;<a href="mailto:bert@freudenbergs.de">bert@freudenbergs.de</a>&gt; wrote:<br>
<div class="im">&gt;<br>
&gt;<br>
&gt; On 27.09.2010, at 20:21, Bert Freudenberg wrote:<br>
&gt;<br>
&gt; On 27.09.2010, at 19:27, Eliot Miranda wrote:<br>
&gt;<br>
&gt;<br>
</div><div><div></div><div class="h5">&gt; On Mon, Sep 27, 2010 at 2:28 AM, Bert Freudenberg &lt;<a href="mailto:bert@freudenbergs.de">bert@freudenbergs.de</a>&gt; wrote:<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; On 27.09.2010, at 01:23, Igor Stasenko wrote:<br>
&gt;&gt;<br>
&gt;&gt; &gt;<br>
&gt;&gt; &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; &gt;&gt;<br>
&gt;&gt; &gt;&gt; On Mon, 27 Sep 2010, Igor Stasenko wrote:<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt;&gt;<br>
&gt;&gt; &gt;&gt;&gt; Suppose i want to call printf() using FFI.<br>
&gt;&gt; &gt;&gt;&gt; But it is a variable argument function. What should do to let it know<br>
&gt;&gt; &gt;&gt;&gt; how many arguments i passed?<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; I think you don&#39;t tell it, it guesses the number of arguments from the first<br>
&gt;&gt; &gt;&gt; argument. If you pass invalid arguments, something bad will happen. :)<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; Yeah.. i inspected the assembly (gcc -s) for printf call,<br>
&gt;&gt; &gt; and there is only pushes of arguments , no extra info.<br>
&gt;&gt;<br>
&gt;&gt; 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>

&gt;&gt;<br>
&gt;&gt; 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>

&gt;&gt;<br>
&gt;&gt; Q: How can I call a function with an argument list built up at run time?<br>
&gt;&gt; A: There is no guaranteed or portable way to do this.<br>
&gt;&gt;<br>
&gt;&gt; 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>
&gt;<br>
&gt; 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.<br>

&gt;<br>
</div></div><div class="im">&gt; No it can&#39;t. Maybe re-read what I wrote when you have a little more time ;)<br>
&gt;<br>
</div>&gt; Retract that.<br>
&gt; A little off-line conversation with Eliot convinced me that the parameter passing isn&#39;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 :)<br>

&gt; Anyone up for trying?<br>
<br>
Well, i made a callout to printf() via NativeBoost in Linux,<br>
but was unable to determine if it works, because when i run it, it<br>
prints nothing on console.<br>
Probably because stdout is closed by default and i need to reopen it first.<br></blockquote><div><br></div><div>Did you try using a console VM (if you&#39;re using Cog that&#39;s CroquetConsole.exe)?</div><div><br></div>
<div>e.</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div><div></div><div class="h5"><br>
&gt; - Bert -<br>
&gt;<br>
<br>
--<br>
Best regards,<br>
Igor Stasenko AKA sig.<br>
</div></div></blockquote></div><br>