From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,235855e3822c83d1 X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Received: by 10.68.236.170 with SMTP id uv10mr11839728pbc.4.1334090323141; Tue, 10 Apr 2012 13:38:43 -0700 (PDT) Path: r9ni41462pbh.0!nntp.google.com!news2.google.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail From: sbelmont700@gmail.com Newsgroups: comp.lang.ada Subject: Re: Importing C function with variable argument list Date: Tue, 10 Apr 2012 13:25:03 -0700 (PDT) Organization: http://groups.google.com Message-ID: <32075438.738.1334089503590.JavaMail.geo-discussion-forums@ynjw14> References: <610ee323-4c7f-413d-8568-aed4955f5152@z38g2000vbu.googlegroups.com> NNTP-Posting-Host: 206.53.78.59 Mime-Version: 1.0 X-Trace: posting.google.com 1334090323 19586 127.0.0.1 (10 Apr 2012 20:38:43 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Tue, 10 Apr 2012 20:38:43 +0000 (UTC) In-Reply-To: <610ee323-4c7f-413d-8568-aed4955f5152@z38g2000vbu.googlegroups.com> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=206.53.78.59; posting-account=ShYTIAoAAABytvcS76ZrG9GdaV-nXYKy User-Agent: G2/1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Date: 2012-04-10T13:25:03-07:00 List-Id: Hi, I often have occasion to suffer through using a C API with variable argumen= t in Ada, and there really is no good choice. I have found basically two o= ptions: 1. Import the C function with a fixed-set of arguments, with a defaulted-to= -zero parameter at the end. or 2. Roll-your-own. The first is cheap and easy, but clearly not extensible, as each version of= the function must be overloaded, giving you an infinite number of prototyp= es to maintain. The second is better, but hardware and implementation spec= ific. For instance, for x86, it might look something like this (where D is= an array of unsigned longs, or whatever the C function needs): procedure V_A (D : Array_Type) is procedure Variable_C_Function; pragma Import (C, Variable_C_Function, "Something"); Result : Interfaces.C.int :=3D 0; =20 begin Machine_Code.Asm (Template =3D> "push %0", Inputs =3D> Interfaces.C.unsigned_long'Asm_Input("g", 0= ), Volatile =3D> true); =20 for i in reverse D'range loop =20 Machine_Code.Asm (Template =3D> "push %0", Inputs =3D> Interfaces.C.unsigned_long'Asm_Input("g",= Data(i)), Volatile =3D> true); end loop; Variable_C_Function; =20 Machine_Code.Asm (Template =3D> "sub %%esp, %0", Inputs =3D> Integer'Asm_Input("g", ((D'last + 2) * 4)), Volatile =3D> true); =20 end V_A; Of course, this is completly non-portable and would depend greatly on both = the hardware, O/S, and how the C function is setup, but it's perhaps the le= ast-worst way of dealing a truly awful kludge. -sb