From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id 218DCBC37 for ; Thu, 29 Oct 2009 22:18:32 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Av0BAPmi6UrRVdvNkGdsb2JhbACRZ4kkPwEBAQEJCQwHEwOzGoE5hjmIaAEDAwWEOAQ X-IronPort-AV: E=Sophos;i="4.44,648,1249250400"; d="scan'208";a="49486813" Received: from mail-ew0-f205.google.com ([209.85.219.205]) by mail4-smtp-sop.national.inria.fr with ESMTP; 29 Oct 2009 22:18:31 +0100 Received: by ewy1 with SMTP id 1so2201730ewy.27 for ; Thu, 29 Oct 2009 14:18:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:content-type; bh=Y6HFYCgwVwxpaWvIh10rg1Qj7ZHfNXLo7Oef3hTXwnA=; b=xI0bWf4QUKp8wcHCvA93p14hJ3X8EIaCSSkLUH6Hpc8RLEKGMlUfuiksP7SluQYsZw jaIlFEsBaCJbDq20jQgFZ1p4QoryJXUapAsV7T6LSLQAcS80hHpFLsoDHgPxrY/pF1s0 yf9GPS1Yhz4jN9pl4GiMoJNK3hnU/F/ogONlE= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; b=TUHbdTs8dqWFdCuLQFKDUFuxEEeXJNQVgCBlMBZNwM1cEpN9YodpqScA6HzJ/MZoAu UQbmHgAlx1LqSqvnw2fklwkLOMyBOYkwFkIl+JIxlEzb8Mn21aldJBVD7V/zIz/1sf6E acW9j/zjDRtM8MObR2YXfNQCtnw6pwbf1hYME= MIME-Version: 1.0 Received: by 10.211.130.19 with SMTP id h19mr718924ebn.54.1256851111203; Thu, 29 Oct 2009 14:18:31 -0700 (PDT) In-Reply-To: <4AE8C987.8020100@starynkevitch.net> References: <666572260910281507q74cb1c1at157c9e3c86070c69@mail.gmail.com> <4AE8C987.8020100@starynkevitch.net> Date: Thu, 29 Oct 2009 22:18:30 +0100 Message-ID: <666572260910291418p585b0aa9laadc14f6589c9772@mail.gmail.com> Subject: Re: [Caml-list] Binding C libraries which use variable arguments (stdarg.h) From: Adrien To: Caml Mailing List Content-Type: text/plain; charset=ISO-8859-1 X-Spam: no; 0.00; basile:01 basile:01 foo:01 stub:01 foo:01 varargs:01 ocaml:01 failwith:01 ocaml:01 compile-time:01 variants:01 varargs:01 weis:01 printf:01 sizeof:01 On 28/10/2009, Basile STARYNKEVITCH wrote: > Adrien wrote: >> Hi, >> >> I am currently trying to bind a C function that takes variables >> arguments, like foo(int a, ...). I can't find how to make a C stub for >> that function. > > I am assuming that the a is the number of actual arguments, so you call > foo(3, x, y, z) > foo(5, t, t+1, t+3, 0, 4) > foo(0) Well, actually the only reason was that stdarg.h needs at least one non-variable argument (as opposed to varargs.h). ;-) Anyway, that doesn't change anything. >> Any other idea? Hint^WPointer? (sorry for the bad joke ;-) ) > > First, you could suppose that the a has a reasonable limit, say 100. > > Then you could generate the glue code for each value of the argument a. > I mean generate ocaml code like > > external f0: void -> uit = "f_0" > external f1: int -> unit = "f_1" > external f2: int -> int -> unit = "f_2" > external f3: int -> int -> int -> unit = "f_3" > > let f a = match Array.length a with > 0 -> f0 () > | 1 -> f1 a.[0] > | 2 -> f2 a.[0] a.[1] > | 3 -> f3 a.[0] a.[1] a.[2] > .... > | _ -> failwith "too many components for f" > > and generate C code for each of f_0 f_1 ... > > and call f with an array ... > > The specialized code generator is reasonably written in Ocaml I'll probably go for that. The API won't be as nice as it could but at least, it's going to be safe and checked at compile-time. > There are more crazy variants, including > > try Ocaml varargs like Pierre Weis did in printf.ml. For plain mortals > like me this is white magic. That could make a nicer API. Maybe for ocaml-gir 3 or 4. :-) > Assuming a Linux system, you could lazily generate the glue code and > invoke dynamic linker on it. So the general case would be to call the > code generator. On linux and x86, I could probably use an array where I would move by sizeof(some_type). I really want portability however (I want to use these bindings at least on windows too). > Time to go to bed. I am saying lot of non-sense. > > Bye! Thanks. :-) --- Adrien Nader