comp.lang.ada
 help / color / mirror / Atom feed
From: Natasha Kerensikova <lithiumcat@gmail.com>
Subject: Re: Importing C function with variable argument list
Date: Fri, 27 Apr 2012 08:24:59 +0000 (UTC)
Date: 2012-04-27T08:24:59+00:00	[thread overview]
Message-ID: <slrnjpklu5.1lme.lithiumcat@sigil.instinctive.eu> (raw)
In-Reply-To: jm7f0m$ugp$1@munin.nbi.dk

Hello,

On 2012-04-12, Randy Brukardt <randy@rrsoftware.com> wrote:
> "Adam Beneschan" <adam@irvine.com> wrote in message 
> news:1288794.275.1334249936588.JavaMail.geo-discussion-forums@ynlp2...
> On Thursday, April 12, 2012 3:08:28 AM UTC-7, Markus Schöpflin wrote:
> ...
>>> So the note from the Ada RM does not seem to be followed by GNAT
>>
>>I don't think GNAT has a choice whether to follow it or not, since GNAT 
>>can't be expected
>> to know what the code of the imported C subprogram is going to look like. 
>> All it can do is
>> obey what the user tells it to do.
>
> Right. If the calling convention is different for normal C subprograms and 
> vararg ones, then clearly there needs to be a different convention name for 
> varargs. (As I noted, I've never worked on such a machine, but it doesn't 
> surprise me that much that they exist.)
>
> Probably the RM ought to suggest such a convention name as Implementation 
> Advice (something like C_Varargs) in order to increase portability.
>
> Someone ought to submit a suggestion to Ada-Comment so that it gets on the 
> ARG's radar/agenda.

Sorry for the "necromancy", but I thought it might be useful to add:

Another significant difference between varargs and regular arguments is
"type promotion". Briefly, char and short types are promoted to int when
they are part of a variadic argument, while they are not when they are
an explicit argument.

So the following will probably fail more reliably than previous
examples:

function Print_Short (Format : in Interfaces.C.char_array;
                      Value : in Interfaces.C.short)
   return Interfaces.C.int;
pragma Import (C, Print_Short, "printf");


Would you all mind testing this on various platforms?


Moreover, the argument type promotion also happens in non-prototyped
functions (e.g. K&R style). For example consider the following C
function:

void print_k_r_short ();


void print_k_r_short (s)
   short s;
{
   printf ("%hd\n", s);
}


Then the following Ada import is unlikely to work:

   procedure Print_K_R_Short (S : in Interfaces.C.short);
   pragma Import (C, Print_K_R_Short, "print_k_r_short");

Though in that case, using Interfaces.C.int would work.

Of course, on platforms where calling convention transmits C.short
arguments exactly like C.int (e.g. for improved alignment, or because
the platform has C.short equivalent to C.int (but then you could use
C.char instead of C.short, since C.char is guaranteed to be strictly
smaller than C.int on conforming hosted implementations)).



Hoping this helps,
Natasha



  parent reply	other threads:[~2012-04-27  8:24 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-04-10  9:21 Importing C function with variable argument list Maxim Reznik
2012-04-10 11:22 ` Patrick
2012-04-10 13:09   ` Maxim Reznik
2012-04-10 13:50     ` Georg Bauhaus
2012-04-10 13:18 ` Markus Schöpflin
2012-04-10 16:47   ` Simon Wright
2012-04-12 10:08     ` Markus Schöpflin
2012-04-12 16:58       ` Adam Beneschan
2012-04-12 20:47         ` Randy Brukardt
2012-04-13  8:43           ` Markus Schöpflin
2012-04-13 23:01             ` Randy Brukardt
2012-04-27  8:24           ` Natasha Kerensikova [this message]
2012-04-27  9:18             ` Jacob Sparre Andersen
2012-04-27 16:48               ` Natasha Kerensikova
2012-05-03 20:15             ` sbelmont700
2012-05-13  4:18               ` David Thompson
2012-05-13  9:03                 ` Simon Wright
2012-05-13 17:01                   ` Jeffrey Carter
2012-05-13 18:20                     ` Simon Wright
2012-05-13 19:11                       ` Jeffrey Carter
2012-05-13 19:55                         ` Ludovic Brenta
2012-05-14  0:52                           ` Jeffrey Carter
2012-05-13 21:12                         ` Simon Wright
2012-05-13 23:57                           ` Georg Bauhaus
2012-05-14  0:54                           ` Jeffrey Carter
2012-05-14  8:10                             ` Nomen Nescio
2012-05-14 15:21               ` Natasha Kerensikova
2012-05-14 20:53                 ` sbelmont700
2012-04-13  4:08         ` Tero Koskinen
2012-04-13  8:04           ` Markus Schöpflin
2012-04-10 20:02 ` Tero Koskinen
2012-04-13  3:28   ` Tero Koskinen
2012-04-10 20:25 ` sbelmont700
2012-04-11 23:24 ` Randy Brukardt
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox