comp.lang.ada
 help / color / mirror / Atom feed
* C Interface question
@ 1996-07-09  0:00 Jerry van Dijk
  1996-07-09  0:00 ` Robert Dewar
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Jerry van Dijk @ 1996-07-09  0:00 UTC (permalink / raw)



I need to call a C function that is defined as:

	int *function(int value, int *result);

the function is called with a value and both the returned pointer
and the result pointer contain data.

So how do I interface to this ?

If I use a procedure I lose the returned value and if I use a procedure
I lose the result value.

Of course I could write a interface function in C, but then I might as
well drop Ada althogether and write the whole package in C... :-(

-- 
-----------------------------------------------------------------------
--  Jerry van Dijk       --   e-mail: jerry@jvdsys.nextjk.stuyts.nl  --
--  Banking Consultant   --              Member Team-Ada             -- 
--  Ordina Finance BV    --    Located at Haarlem, The Netherlands   --




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: C Interface question
  1996-07-09  0:00 C Interface question Jerry van Dijk
  1996-07-09  0:00 ` Robert Dewar
@ 1996-07-09  0:00 ` Laurent Guerby
  1996-07-11  0:00   ` Jerry van Dijk
  1996-07-11  0:00 ` Kevin Krieser
  2 siblings, 1 reply; 12+ messages in thread
From: Laurent Guerby @ 1996-07-09  0:00 UTC (permalink / raw)




Jerry> I need to call a C function that is defined as:

Jerry> 	int *function(int value, int *result);

   type int_star is access C.int;
   pragma Convention (C, Int_Star);

   function F (Value : C.int; Result : int_star) return int_star;
   pragma Convention (C, F);

-- 
Laurent Guerby <guerby@gnat.com>, Team Ada.
   "Use the Source, Luke. The Source will be with you, always (GPL)."




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: C Interface question
  1996-07-09  0:00 C Interface question Jerry van Dijk
@ 1996-07-09  0:00 ` Robert Dewar
  1996-07-10  0:00   ` Theodore E. Dennison
                     ` (2 more replies)
  1996-07-09  0:00 ` Laurent Guerby
  1996-07-11  0:00 ` Kevin Krieser
  2 siblings, 3 replies; 12+ messages in thread
From: Robert Dewar @ 1996-07-09  0:00 UTC (permalink / raw)



Jerry asks

        int *function(int value, int *result);

  the function is called with a value and both the returned pointer
  and the result pointer contain data.

  So how do I interface to this ?

  If I use a procedure I lose the returned value and if I use a procedure
  I lose the result value.

There are two obvious answers. You can pass an access to integer value
as the second parameter, defining an access type. Or, if the pointer
will never be null, then you can use a parameter that is access integer
on the Ada side.

  Of course I could write a interface function in C, but then I might as
  well drop Ada althogether and write the whole package in C... :-(

Seems a bit extreme ... but anyway, there is a trivially easy way to
interface as above. Note that indeed passing an access type by value is
in any case closest to the C semantics, which does not have call by
reference.

Note that in general the issue of passing out and in-out parameters to
externally interfaced functions can be a bit irritating. For some reason
people would not go along with the suggestion of allowing out and in out
parameters on functions, even with the concession of restricting it to
imported subporgrams.

Dec has a pragma Import_Valued_Procedure that allows a function in the
external world to be treated as a procedure with an initial out parameter
that is the returned value on the Ada side. Using this pragma, we would
have a third way of interfacing:

   procedure C_Function
     (Return_Val : out int; Value : int; Result : in out int);

   pragma Import (C, C_Function);
   pragma Import_Valued_Procedure
     (Internal  => C_Function,
      External  => "function",
      Mechanism => (value, value, reference));

This pragma (and all other DEC Ada pragmas and attributes) will be fully
implemented in the next version of GNAT (some of them only make sense on
VMS, but many of them, like the above example, are generally useful).
  




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: C Interface question
  1996-07-09  0:00 ` Robert Dewar
@ 1996-07-10  0:00   ` Theodore E. Dennison
  1996-07-11  0:00     ` Robert Dewar
  1996-07-11  0:00   ` Ken Garlington
  1996-07-11  0:00   ` Jerry van Dijk
  2 siblings, 1 reply; 12+ messages in thread
From: Theodore E. Dennison @ 1996-07-10  0:00 UTC (permalink / raw)



Robert Dewar wrote:
> Dec has a pragma Import_Valued_Procedure that allows a function in the
> external world to be treated as a procedure with an initial out parameter
> that is the returned value on the Ada side. Using this pragma, we would
...
> This pragma (and all other DEC Ada pragmas and attributes) will be fully
> implemented in the next version of GNAT (some of them only make sense on
> VMS, but many of them, like the above example, are generally useful).

Cool!! That was one of my favorite DECisms. The best part was the "mechanisims",
field, which allowed "thin" bindings to use ADTs, rather than just integers
or access types. That almost makes thin C bindings usable.

-- 
T.E.D.          
                |  Work - mailto:dennison@escmail.orl.mmc.com  |
                |  Home - mailto:dennison@iag.net              |
                |  URL  - http://www.iag.net/~dennison         |




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: C Interface question
  1996-07-11  0:00   ` Ken Garlington
@ 1996-07-11  0:00     ` Robert Dewar
  0 siblings, 0 replies; 12+ messages in thread
From: Robert Dewar @ 1996-07-11  0:00 UTC (permalink / raw)



Ken asks

"Is there a paper available on which DEC Ada extensions will be implemented
"generally" in GNAT (beyond just the ports to DEC hardware)? There's probably
going to be quite a bit of software written with DEC Ada that will be ported
to non-DEC hardware over the next several years; if GNAT is a good way to
facilitate that migration, you might want to make that generally known..."

there is no current paper, since this is work in progress. When the
version is released, the documentation will specify the details you
are looking for. But generally we will implement everything that makes
sense generally on all targets. Some things like AST_Entry are obviously
non-portable.





^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: C Interface question
  1996-07-10  0:00   ` Theodore E. Dennison
@ 1996-07-11  0:00     ` Robert Dewar
  0 siblings, 0 replies; 12+ messages in thread
From: Robert Dewar @ 1996-07-11  0:00 UTC (permalink / raw)



T.E.D. said

Cool!! That was one of my favorite DECisms. The best part was the "mechanisims",
field, which allowed "thin" bindings to use ADTs, rather than just integers
or access types. That almost makes thin C bindings usable.


Indeed. It is true that the Ada 95 rules (or rather implementation advice)
if followed, allows more consistent and portable interface to C, but the
Import pragma allows precise control, and GNAT does indeed fully implement
the whole pragma (well to be precise, certain very VMS'y features like
First_Optional_Parameter and pass by Descriptor are available on VMS
only, since they make no sense elsewhere, but most of the pragma makes
good sense. Actually come to think of it, maybe we should allow
First_Optional_Parameter and ignore it? well probably it's not worth it,
since it is only really of use for DEC packages like Starlet.

But the important point is that the Value and Reference mechanisms work
fine, and I think that is what Ted is really referring to in his "Cool!!"





^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: C Interface question
  1996-07-11  0:00 ` Kevin Krieser
@ 1996-07-11  0:00   ` Robert Dewar
  0 siblings, 0 replies; 12+ messages in thread
From: Robert Dewar @ 1996-07-11  0:00 UTC (permalink / raw)



Kevin said

"I haven't tried it, but if a 'Address is the same size as an Access type, and ha
s the same value, then you may be able to
do the following:

function C_Function ( value : in Integer; Result : in System.Address ) return Sy
stem.Address;

Then, just use Unchecked conversion to convert the result to an Access type, and
 access it that way."


This is non-portable, and really too low level. Using a named access type
is much cleaner, and cleaner still is just using an in out parameter 
assuming your compiler follows the proper Ada 95 implementation advice.





^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: C Interface question
  1996-07-09  0:00 C Interface question Jerry van Dijk
  1996-07-09  0:00 ` Robert Dewar
  1996-07-09  0:00 ` Laurent Guerby
@ 1996-07-11  0:00 ` Kevin Krieser
  1996-07-11  0:00   ` Robert Dewar
  2 siblings, 1 reply; 12+ messages in thread
From: Kevin Krieser @ 1996-07-11  0:00 UTC (permalink / raw)



 
 
> I need to call a C function that is defined as:  
>    
> 	int *function(int value, int *result);  
>    
> the function is called with a value and both the returned pointer  
> and the result pointer contain data.  
>    
> So how do I interface to this ? 
 
I haven't tried it, but if a 'Address is the same size as an Access type, and has the same value, then you may be able to   
do the following: 
 
function C_Function ( value : in Integer; Result : in System.Address ) return System.Address; 
 
Then, just use Unchecked conversion to convert the result to an Access type, and access it that way.

------------------------------------ 
-- Kevin Krieser 
-- kkrieser@ionet.net




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: C Interface question
  1996-07-09  0:00 ` Robert Dewar
  1996-07-10  0:00   ` Theodore E. Dennison
@ 1996-07-11  0:00   ` Ken Garlington
  1996-07-11  0:00     ` Robert Dewar
  1996-07-11  0:00   ` Jerry van Dijk
  2 siblings, 1 reply; 12+ messages in thread
From: Ken Garlington @ 1996-07-11  0:00 UTC (permalink / raw)



Robert Dewar wrote:

> Dec has a pragma Import_Valued_Procedure that allows a function in the
> external world to be treated as a procedure with an initial out parameter
> that is the returned value on the Ada side. Using this pragma, we would
> have a third way of interfacing:
> 
>    procedure C_Function
>      (Return_Val : out int; Value : int; Result : in out int);
> 
>    pragma Import (C, C_Function);
>    pragma Import_Valued_Procedure
>      (Internal  => C_Function,
>       External  => "function",
>       Mechanism => (value, value, reference));
> 
> This pragma (and all other DEC Ada pragmas and attributes) will be fully
> implemented in the next version of GNAT (some of them only make sense on
> VMS, but many of them, like the above example, are generally useful).

Is there a paper available on which DEC Ada extensions will be implemented
"generally" in GNAT (beyond just the ports to DEC hardware)? There's probably
going to be quite a bit of software written with DEC Ada that will be ported
to non-DEC hardware over the next several years; if GNAT is a good way to
facilitate that migration, you might want to make that generally known...

-- 
LMTAS - "Our Brand Means Quality"




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: C Interface question
  1996-07-09  0:00 ` Laurent Guerby
@ 1996-07-11  0:00   ` Jerry van Dijk
  1996-07-16  0:00     ` David Kirkland
  0 siblings, 1 reply; 12+ messages in thread
From: Jerry van Dijk @ 1996-07-11  0:00 UTC (permalink / raw)



Laurent Guerby (guerby@gnat.com) wrote:

: Jerry> 	int *function(int value, int *result);

:    type int_star is access C.int;
:    pragma Convention (C, Int_Star);

:    function F (Value : C.int; Result : int_star) return int_star;
:    pragma Convention (C, F);

Of course, why do I always overlook the obvious ? This way I will never
become rich and famous :-)

Thanks!
-- 
-----------------------------------------------------------------------
--  Jerry van Dijk       --   e-mail: jerry@jvdsys.nextjk.stuyts.nl  --
--  Banking Consultant   --              Member Team-Ada             -- 
--  Ordina Finance BV    --    Located at Haarlem, The Netherlands   --




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: C Interface question
  1996-07-09  0:00 ` Robert Dewar
  1996-07-10  0:00   ` Theodore E. Dennison
  1996-07-11  0:00   ` Ken Garlington
@ 1996-07-11  0:00   ` Jerry van Dijk
  2 siblings, 0 replies; 12+ messages in thread
From: Jerry van Dijk @ 1996-07-11  0:00 UTC (permalink / raw)



Robert Dewar (dewar@cs.nyu.edu) wrote:

: Dec has a pragma Import_Valued_Procedure

: This pragma (...) will be fully implemented in the next version of GNAT

Great!
 
-- 
-----------------------------------------------------------------------
--  Jerry van Dijk       --   e-mail: jerry@jvdsys.nextjk.stuyts.nl  --
--  Banking Consultant   --              Member Team-Ada             -- 
--  Ordina Finance BV    --    Located at Haarlem, The Netherlands   --




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: C Interface question
  1996-07-11  0:00   ` Jerry van Dijk
@ 1996-07-16  0:00     ` David Kirkland
  0 siblings, 0 replies; 12+ messages in thread
From: David Kirkland @ 1996-07-16  0:00 UTC (permalink / raw)



Jerry van Dijk wrote:
> 
> Laurent Guerby (guerby@gnat.com) wrote:
> 
> : Jerry>        int *function(int value, int *result);
> 
> :    type int_star is access C.int;
> :    pragma Convention (C, Int_Star);
> 
> :    function F (Value : C.int; Result : int_star) return int_star;
> :    pragma Convention (C, F);
> 
> Of course, why do I always overlook the obvious ? This way I will never
> become rich and famous :-)
> 
> Thanks!
> --
> -----------------------------------------------------------------------
> --  Jerry van Dijk       --   e-mail: jerry@jvdsys.nextjk.stuyts.nl  --
> --  Banking Consultant   --              Member Team-Ada             --
> --  Ordina Finance BV    --    Located at Haarlem, The Netherlands   --

Hi all,
I have a similar question. I'm using an Ada83 compiler and have to
interface to a C function similar to the above except it contains
a pointer to a pointer ie.)

int *function(int value, int **result);

Now obviously I can use an access type to an access type, an extention
of what was posted earlier. My question is this - Is an access type
guaranteed to be equal to a C pointer? I know it is the most obvious
implementation but like so many things! Does the Ada LRM specify how
the access type is implemented?

Is there any advantage to using System.Address and then calling
the function with Type'Address ??




^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~1996-07-16  0:00 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-07-09  0:00 C Interface question Jerry van Dijk
1996-07-09  0:00 ` Robert Dewar
1996-07-10  0:00   ` Theodore E. Dennison
1996-07-11  0:00     ` Robert Dewar
1996-07-11  0:00   ` Ken Garlington
1996-07-11  0:00     ` Robert Dewar
1996-07-11  0:00   ` Jerry van Dijk
1996-07-09  0:00 ` Laurent Guerby
1996-07-11  0:00   ` Jerry van Dijk
1996-07-16  0:00     ` David Kirkland
1996-07-11  0:00 ` Kevin Krieser
1996-07-11  0:00   ` Robert Dewar

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