<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE message PUBLIC
  "-//MLarc//DTD MLarc output files//EN"
  "../../mlarc.dtd"[
  <!ATTLIST message
    listname CDATA #REQUIRED
    title CDATA #REQUIRED
  >
]>

  <?xml-stylesheet href="../../mlarc.xsl" type="text/xsl"?>


<message 
  url="2009/10/1dba359e027f2371c3353f1093257973"
  from="Goswin von Brederlow &lt;goswin-v-b@w...&gt;"
  author="Goswin von Brederlow"
  date="2009-10-29T08:55:45"
  subject="Re: [Caml-list] Binding C libraries which use variable arguments (stdarg.h)"
  prev="2009/10/bf5823ce7568616f364570cd8ba70476"
  next="2009/10/4101b452ec63b2748ed42ae54995fa07"
  prev-in-thread="2009/10/17b019522632b07b514e5be4e75fa7b5"
  next-in-thread="2009/10/c3d6a2b5703ff6f6470263a84a2e2610"
  prev-thread="2009/10/bbb95d9e0194e1762c52367d01c48687"
  next-thread="2009/11/e2224339ec0b2824c1fc28558cc04bee"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="[Caml-list] Binding C libraries which use variable arguments  (stdarg.h)">
<msg 
  url="2009/10/63300d21e98ae03b875913a3e95f0dea"
  from="Adrien &lt;camaradetux@g...&gt;"
  author="Adrien"
  date="2009-10-28T22:07:50"
  subject="[Caml-list] Binding C libraries which use variable arguments  (stdarg.h)">
<msg 
  url="2009/10/17b019522632b07b514e5be4e75fa7b5"
  from="Basile STARYNKEVITCH &lt;basile@s...&gt;"
  author="Basile STARYNKEVITCH"
  date="2009-10-28T22:45:27"
  subject="Re: [Caml-list] Binding C libraries which use variable arguments (stdarg.h)">
<msg 
  url="2009/10/1dba359e027f2371c3353f1093257973"
  from="Goswin von Brederlow &lt;goswin-v-b@w...&gt;"
  author="Goswin von Brederlow"
  date="2009-10-29T08:55:45"
  subject="Re: [Caml-list] Binding C libraries which use variable arguments (stdarg.h)">
<msg 
  url="2009/10/c3d6a2b5703ff6f6470263a84a2e2610"
  from="Adrien &lt;camaradetux@g...&gt;"
  author="Adrien"
  date="2009-10-29T21:37:16"
  subject="Re: [Caml-list] Binding C libraries which use variable arguments  (stdarg.h)">
<msg 
  url="2009/10/220b341f580c2f05dcd1fe43af8195cf"
  from="Goswin von Brederlow &lt;goswin-v-b@w...&gt;"
  author="Goswin von Brederlow"
  date="2009-10-29T23:31:43"
  subject="Re: [Caml-list] Binding C libraries which use variable arguments  (stdarg.h)">
</msg>
</msg>
</msg>
<msg 
  url="2009/10/88641fef6c963d890d6c941607d4dd4c"
  from="Adrien &lt;camaradetux@g...&gt;"
  author="Adrien"
  date="2009-10-29T21:18:32"
  subject="Re: [Caml-list] Binding C libraries which use variable arguments  (stdarg.h)">
<msg 
  url="2009/10/695b35c7584e6179c596cc18c578c36c"
  from="Xavier Leroy &lt;Xavier.Leroy@i...&gt;"
  author="Xavier Leroy"
  date="2009-10-30T09:35:03"
  subject="Re: [Caml-list] Binding C libraries which use variable arguments (stdarg.h)">
<msg 
  url="2009/11/c25b776f5816dc40e98663c70d4d8196"
  from="Adrien &lt;camaradetux@g...&gt;"
  author="Adrien"
  date="2009-11-01T09:31:22"
  subject="Re: [Caml-list] Binding C libraries which use variable arguments  (stdarg.h)">
<msg 
  url="2009/11/c9f5cdae61a375516b1c7f58f7c8a24c"
  from="Florian Weimer &lt;fw@d...&gt;"
  author="Florian Weimer"
  date="2009-11-01T11:07:48"
  subject="Re: [Caml-list] Binding C libraries which use variable arguments  (stdarg.h)">
<msg 
  url="2009/11/30d1a6db7b740e167f15d4d3fc8aff0a"
  from="Adrien &lt;camaradetux@g...&gt;"
  author="Adrien"
  date="2009-11-01T12:12:36"
  subject="Re: [Caml-list] Binding C libraries which use variable arguments  (stdarg.h)">
<msg 
  url="2009/11/9b89b9c7f37c9669467ce973d1ce2cd9"
  from="Florian Weimer &lt;fw@d...&gt;"
  author="Florian Weimer"
  date="2009-11-01T12:16:30"
  subject="Re: [Caml-list] Binding C libraries which use variable arguments  (stdarg.h)">
<msg 
  url="2009/11/6c1b3d299b19d3da9f11ba1908e5969f"
  from="Adrien &lt;camaradetux@g...&gt;"
  author="Adrien"
  date="2009-11-01T14:58:48"
  subject="Re: [Caml-list] Binding C libraries which use variable arguments  (stdarg.h)">
<msg 
  url="2009/11/8bc8d525d56eeb42e638223e6eac0c92"
  from="Richard Jones &lt;rich@a...&gt;"
  author="Richard Jones"
  date="2009-11-01T20:06:49"
  subject="Re: [Caml-list] Binding C libraries which use variable arguments (stdarg.h)">
</msg>
<msg 
  url="2009/11/5272114d3fb3e7ccacc5340cfb864f2e"
  from="Florian Weimer &lt;fw@d...&gt;"
  author="Florian Weimer"
  date="2009-11-03T19:33:20"
  subject="Re: [Caml-list] Binding C libraries which use variable arguments  (stdarg.h)">
</msg>
</msg>
</msg>
</msg>
</msg>
</msg>
</msg>
</msg>
</msg>
</msg>
</thread>

<contents>
Basile STARYNKEVITCH &lt;basile@starynkevitch.net&gt; writes:

&gt; Adrien wrote:
&gt;&gt; Hi,
&gt;&gt;
&gt;&gt; I am currently trying to bind a C function that takes variables
&gt;&gt; arguments, like foo(int a, ...). I can't find how to make a C stub for
&gt;&gt; that function.
&gt;
&gt;  I am assuming that the a is the number of actual arguments, so you call
&gt; foo(3, x, y, z)
&gt; foo(5, t, t+1, t+3, 0, 4)
&gt; foo(0)
&gt;
&gt;&gt;
&gt;&gt; Any other idea? Hint^WPointer? (sorry for the bad joke ;-) )
&gt;
&gt; First, you could suppose that the a has a reasonable limit, say 100.
&gt;
&gt; Then you could generate the glue code for each value of the argument
&gt; a. I mean generate ocaml code like
&gt;
&gt; external f0: void -&gt; uit = "f_0"
&gt; external f1: int -&gt; unit = "f_1"
&gt; external f2: int -&gt; int -&gt; unit = "f_2"
&gt; external f3: int -&gt; int -&gt; int -&gt; unit = "f_3"
&gt;
&gt; let f a = match Array.length a with
&gt;  0 -&gt; f0 ()
&gt; | 1 -&gt; f1 a.[0]
&gt; | 2 -&gt; f2 a.[0] a.[1]
&gt; | 3 -&gt; f3 a.[0] a.[1] a.[2]
&gt; ....
&gt; | _ -&gt; failwith "too many components for f"
&gt;
&gt; and generate C code for each of f_0 f_1 ...
&gt;
&gt; and call f with an array ...
&gt;
&gt; The specialized code generator is reasonably written in Ocaml
&gt;
&gt; There are more crazy variants, including
&gt;
&gt; try Ocaml varargs like Pierre Weis did in printf.ml. For plain mortals
&gt; like me this is white magic.
&gt;
&gt; Assuming a Linux system, you could lazily generate the glue code and
&gt; invoke dynamic linker on it. So the general case would be to call the
&gt; code generator.
&gt;
&gt; Time to go to bed. I am saying lot of non-sense.
&gt;
&gt; Bye!

Since ocaml functions with more than 5 args use an array you only do
that in ocaml for a few arguments and then you need to do this in
C. So do it in C for all. The stub takes an array and then switches on
the lentgh to call the real function.

Unfortunately you can not convert an array or list into a va_list. You
need to specifically catch each length and call foo(4, a[0], a[1],
a[2], a[3]) for each length.

MfG
        Goswin

</contents>

</message>

