comp.lang.ada
 help / color / mirror / Atom feed
* calling ada from c
@ 1988-10-16  0:23 Maureen Cragg
  1988-10-17 17:21 ` Maureen Cragg
  0 siblings, 1 reply; 27+ messages in thread
From: Maureen Cragg @ 1988-10-16  0:23 UTC (permalink / raw)


i have successfully called c functions from ada (the pragma import works fine),
but i'm having more difficulty the other way...

i'm trying to pass a string from c to ada:

extern void adaprint_s(char *str);
char str[81];
main(){
   puts("enter a string:");
   gets(str);
   adaprint_s(str);
}

package adaprint is

type string_ptr is access string(1..81);

procedure adaprint_s(
                     str : IN string_ptr );
pragma export_procedure(internal=>adaprint_s,external=>adaprint_s,
                        parameter_types=>(string_ptr));
end;

with text_io;
use text_io;
package body adaprint is

procedure adaprint_s(
                     str : IN string_ptr )
is
begin
   put("the string is:");
   put_line(str.all);
end;
end;

now it looks like i'm passing the address of a string to ada, which is expect-
ing a string pointer, but somehow my adaprint.str is set to the first 4 char-
acters of the string, in reverse order! (i've inspected the values with debug).

if i pass the address of the string pointer ( adaprint_s(&str); ) everything
works fine, but i'm at a loss to understand this behavior...

if anyone out there can explain this to me, i'll sleep better tonight;-)

and can anyone tell me how to pass a simple integer? i'm having less luck with
that...

adTHANXvance,
the air drummer

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

* Re: calling ada from c
  1988-10-16  0:23 calling ada from c Maureen Cragg
@ 1988-10-17 17:21 ` Maureen Cragg
  0 siblings, 0 replies; 27+ messages in thread
From: Maureen Cragg @ 1988-10-17 17:21 UTC (permalink / raw)


In article <1512@gmu90x.UUCP>, mcragg@gmu90x.UUCP (Maureen Cragg) writes:
> and can anyone tell me how to pass a simple integer? i'm having less luck with
> that...

i've succeeded in passing an integer: adaprint(&i) where adaprint accepts
an integer...anyone care to explain that?

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

* calling Ada from C
@ 1991-02-23 16:01 David B Lightstone
  0 siblings, 0 replies; 27+ messages in thread
From: David B Lightstone @ 1991-02-23 16:01 UTC (permalink / raw)


There is a possible backdoor strategy which may be used. I previously 
discussed this with Telesoft tech support cerca their version 1.3 and
version 1.5 (Motorola 68000 target, Vax host). 

The problem arose when I needed an interrupt handler, but needed
a response time on the order of 10 - 20 micro seconds. 

The strategy was never implemented, so I can not say if it will be 
successful. It was straight forward. When the assembler interrupt was
ready to complete  spin off another interrupt handler by changing
machine priority, restoring the registers, enabling interrupts and 
branching to a standard Ada Interrupt.

What prevents the C program from calling a software interrupt which
will be fielded by Ada? There will be logistical problem relatable to
exchanging data, so conventions will have to be established.

I don't consider this a clean solution.

When I discussed this approach with Telesoft is was not rejected out
of hand. Their implementation has changed substancially since then, so
there should be a much better solution somewhere.

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

* Calling Ada from C
@ 1995-03-22 13:26 Roger L Costello
  1995-03-22 13:58 ` David Paton
  1995-03-23 17:22 ` Theodore Dennison
  0 siblings, 2 replies; 27+ messages in thread
From: Roger L Costello @ 1995-03-22 13:26 UTC (permalink / raw)


Hi Folks,
    I am trying (unsuccessfully) to call an Ada program from a C program.
I am using the SUN (VADS) Ada, and SUN C.  I am running on a SUN SPARC 10.

Here's my simple C program:

extern hello()'

main ()
{
   hello();
}

My intent is for this C program to call the following hello world Ada
program:

with text_io;

package hello_pkg is

   procedure hello;

pragma EXTERNAL (C, hello);
pragma EXTERNAL_NAME (hello, "hello");

end hello_pkg;

package body hello_pkg is


procedure hello is
i : integer := 0;
begin

    text_io.put_line ("Hello World");

end hello;

end hello_pkg;

I compiled the Ada program.  It generated hello02 and hello03 in the
.objects directory.

When I tried compiling/linking the C program:

cc test.c .objects/hello03

I got the following errors:

Undefined                       first referenced
 symbol                             in file
STACK_LIMIT                         ./.objects/hello03
_A_put_line.123S12.text_io          ./.objects/hello03
ld: fatal: Symbol referencing errors. No output written to a.out

I really haven't a clue what to do.  The SUN documenation hasn't
been of much help.  Can you assist?  Thanks in advance.  /Roger ~

Please respond to: costello@mitre.org




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

* Re: Calling Ada from C
  1995-03-22 13:26 Calling Ada from C Roger L Costello
@ 1995-03-22 13:58 ` David Paton
  1995-03-23 17:22 ` Theodore Dennison
  1 sibling, 0 replies; 27+ messages in thread
From: David Paton @ 1995-03-22 13:58 UTC (permalink / raw)


Roger L Costello (costello@mbunix.mitre.org) wrote:
: Hi Folks,
:     I am trying (unsuccessfully) to call an Ada program from a C program.
: I am using the SUN (VADS) Ada, and SUN C.  I am running on a SUN SPARC 10.

[body deleted]

: Please respond to: costello@mitre.org



How about posting the reponse so that others could benefit?



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

* Re: Calling Ada from C
  1995-03-22 13:26 Calling Ada from C Roger L Costello
  1995-03-22 13:58 ` David Paton
@ 1995-03-23 17:22 ` Theodore Dennison
  1995-03-24 17:14   ` Larry Kilgallen, LJK Software
  1 sibling, 1 reply; 27+ messages in thread
From: Theodore Dennison @ 1995-03-23 17:22 UTC (permalink / raw)
  To: costello

Roger L Costello <costello@mbunix.mitre.org> writes:
>     I am trying (unsuccessfully) to call an Ada program from a C program.
> I am using the SUN (VADS) Ada, and SUN C.  I am running on a SUN SPARC 10.
..
> 
>     text_io.put_line ("Hello World");
..
> 
> I got the following errors:
> 
> Undefined                       first referenced
>  symbol                             in file
> STACK_LIMIT                         ./.objects/hello03
> _A_put_line.123S12.text_io          ./.objects/hello03
> ld: fatal: Symbol referencing errors. No output written to a.out
> 

All Ada compilers come with their own executive to perform stuff
like tasking, dynamic memory allocation/deallocation, and elaboration.
As a result, certain operations are not available when your "main" 
program is a C program, not an Ada program. I/O for SunAda appears to
be one of those things.Despite your protestations that "the Sun 
documentation hasn't been much help", Section 4.3 (Calling Ada From
Other Languages) in the Sun Ada programer's guide goes over this in 
great detail.

If you make your main program an Ada program, you can call C routines
with little trouble. These C routines in turn can call Ada routines 
with no trouble. This "wrapper" approach is specificly suggested by 
the SunAda manual (section 4.2). I have used this approach in the
past in real-world applications (gory details available upon request).

If you need any further info, just ask (I love this stuff).

T.E.D.




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

* Re: Calling Ada from C
  1995-03-23 17:22 ` Theodore Dennison
@ 1995-03-24 17:14   ` Larry Kilgallen, LJK Software
  1995-03-26 11:53     ` Robert Dewar
  1995-03-27 19:48     ` Robert I. Eachus
  0 siblings, 2 replies; 27+ messages in thread
From: Larry Kilgallen, LJK Software @ 1995-03-24 17:14 UTC (permalink / raw)


In article <3ksanp$bab@theopolis.orl.mmc.com>, Theodore Dennison <dennison@escmail.orl.mmc.com> writes:

> All Ada compilers come with their own executive to perform stuff
> like tasking, dynamic memory allocation/deallocation, and elaboration.

Agreed.

> As a result, certain operations are not available when your "main" 
> program is a C program, not an Ada program.

I am not convinced that is true for "all Ada compilers".  I cannot think
of a reason why it should be true for DEC Ada, and looking at the Run-Time
Reference Manual for running on VMS, I do not see such a restriction in
the chapter on mixed language programming. (The Run-Time library for
DEC Ada is included by the linker, regardless of the language for the
main program.)

If anyone knows of a failure of this sort for DEC Ada running on VMS,
I would be interested to hear about it.



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

* Re: Calling Ada from C
  1995-03-24 17:14   ` Larry Kilgallen, LJK Software
@ 1995-03-26 11:53     ` Robert Dewar
  1995-03-27 14:47       ` Theodore Dennison
  1995-03-27 19:48     ` Robert I. Eachus
  1 sibling, 1 reply; 27+ messages in thread
From: Robert Dewar @ 1995-03-26 11:53 UTC (permalink / raw)


GNAT certainly allows the main program to be in a language other than C
with no restrictions (in fact the main program of the *all* GNAT programs
is typically in C, since that's what the binder generates). But you can
also have your own explicit main program.




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

* Re: Calling Ada from C
  1995-03-26 11:53     ` Robert Dewar
@ 1995-03-27 14:47       ` Theodore Dennison
  1995-03-28  0:00         ` Robert Dewar
                           ` (2 more replies)
  0 siblings, 3 replies; 27+ messages in thread
From: Theodore Dennison @ 1995-03-27 14:47 UTC (permalink / raw)


Robert Dewar <dewar@cs.nyu.edu> writes:

> GNAT certainly allows the main program to be in a language other than C
> with no restrictions (in fact the main program of the *all* GNAT programs
> is typically in C, since that's what the binder generates). But you can
> also have your own explicit main program.

With NO restrictions? Including tasking? The C program doesn't have to make
any special calls to elaborate everything?

(I know, I know, I should try it myself. But I'm kinda lazy^H^H^H^Hbusy 
these days).

For the record, I would expect GNAT to be better than most compilers in
this regard. After all, it is built around a C (and Pascal, and Modula-2,
and ...) compiler.

T.E.D.




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

* Re: Calling Ada from C
  1995-03-24 17:14   ` Larry Kilgallen, LJK Software
  1995-03-26 11:53     ` Robert Dewar
@ 1995-03-27 19:48     ` Robert I. Eachus
  1995-03-29  0:00       ` Larry Kilgallen, LJK Software
  1 sibling, 1 reply; 27+ messages in thread
From: Robert I. Eachus @ 1995-03-27 19:48 UTC (permalink / raw)


In article <1995Mar24.121422.9469@eisner> kilgallen@eisner.decus.org (Larry Kilgallen, LJK Software) writes:

 > I am not convinced that is true for "all Ada compilers".  I cannot think
 > of a reason why it should be true for DEC Ada, and looking at the Run-Time
 > Reference Manual for running on VMS, I do not see such a restriction in
 > the chapter on mixed language programming. (The Run-Time library for
 > DEC Ada is included by the linker, regardless of the language for the
 > main program.)

 > If anyone knows of a failure of this sort for DEC Ada running on VMS,
 > I would be interested to hear about it.

   DEC made the decision to build the DEC Ada run-time into VMS, so
that Ada programs do not pay a large penalty and can be freely mixed
with other DEC supported languages.  They might have decided to do it
otherwise had they no already integrated PL/I in the same way.

--

					Robert I. Eachus

with Standard_Disclaimer;
use  Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...



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

* Re: Calling Ada from C
  1995-03-27 14:47       ` Theodore Dennison
  1995-03-28  0:00         ` Robert Dewar
@ 1995-03-28  0:00         ` Cyrille Comar
  1995-03-29  2:47         ` Larry Kilgallen, LJK Software
  2 siblings, 0 replies; 27+ messages in thread
From: Cyrille Comar @ 1995-03-28  0:00 UTC (permalink / raw)


dennison@escmail.orl.mmc.com (Theodore Dennison) writes:
: Robert Dewar <dewar@cs.nyu.edu> writes:
: 
: > GNAT certainly allows the main program to be in a language other than C
: > with no restrictions (in fact the main program of the *all* GNAT programs
: > is typically in C, since that's what the binder generates). But you can
: > also have your own explicit main program.
: 
: With NO restrictions? 

What kind of restrictions would you like? I don't see any need for
restrictions. "gnat1" the core of the gnat compiler is such a program,
for instance...

:Including tasking? 

Why not? I don't see any problem with that.

: The C program doesn't have to make
: any special calls to elaborate everything?
: 

You need to elaborate the non-preelaborable compilation units you want
to use. The binder (gnatbind) generates a procedure with the
appropriate calls, there is a special option to use when the main
program is not Ada.  You also need to finalize the library level
controlled objects if you want a fully consistant system. This part is
not very documented yet but it is quite simple to mimic the code
generated by the binder for the case where the main program is an Ada
procedure...
-- 
------------------------------------------------------------------------
Cyrille Comar,                                  E-mail: comar@cs.nyu.edu
Gnat Project                                    US phone: (212) 998-3489





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

* Re: Calling Ada from C
  1995-03-27 14:47       ` Theodore Dennison
@ 1995-03-28  0:00         ` Robert Dewar
  1995-03-28  0:00         ` Cyrille Comar
  1995-03-29  2:47         ` Larry Kilgallen, LJK Software
  2 siblings, 0 replies; 27+ messages in thread
From: Robert Dewar @ 1995-03-28  0:00 UTC (permalink / raw)



OK, Ted, to be more accurate, if the main program is in C, and the Ada
packages are not all preelaborable, then you do indeed need right now
to call an elaboration routine, as recommended in the RM. However it is
in our plans to use Collect2 so that this elaboration is performed
automatically (it's the same problem as getting C++ constructors 
elaborated, so it's a solved problem to do this automatically, we just
haven't integrated this solution yet).

Also rememember that these days, with Ada 95, you have to concern yourself
not just with elaboration of library packages, but also with finaliation
of library packages.





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

* Re: Calling Ada from C
  1995-03-27 19:48     ` Robert I. Eachus
@ 1995-03-29  0:00       ` Larry Kilgallen, LJK Software
  0 siblings, 0 replies; 27+ messages in thread
From: Larry Kilgallen, LJK Software @ 1995-03-29  0:00 UTC (permalink / raw)


In article <EACHUS.95Mar27144807@spectre.mitre.org>, eachus@spectre.mitre.org (Robert I. Eachus) writes:

>    DEC made the decision to build the DEC Ada run-time into VMS, so
> that Ada programs do not pay a large penalty and can be freely mixed
> with other DEC supported languages.  They might have decided to do it
> otherwise had they no already integrated PL/I in the same way.

Not just PL/I, but Fortran, Dibol, Cobol, C, Pascal RPG and Basic have
this support "built in" for VMS systems.

Aside from common debugger support, however, the method in which these
are "built-in" uses only techniques which are also available to a third
party vendor to add a compiler of their own choice.  DEC will not take
care of distributing the "Run-Time Library" image to every VMS system
in the world, but that is easily countered by a vendor allowing those
who have the compiler to freely distributed the "Run-Time Library" image
along with the software they produce with that compiler.  In fact a
few years ago DEC authorized their own compiler customers to do that
as an additional distribution strategy for cases with version skew
issues.

Larry Kilgallen




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

* Re: Calling Ada from C
  1995-03-29  2:47         ` Larry Kilgallen, LJK Software
@ 1995-03-29  0:00           ` Theodore Dennison
  1995-04-04  0:00             ` Robert Dewar
  0 siblings, 1 reply; 27+ messages in thread
From: Theodore Dennison @ 1995-03-29  0:00 UTC (permalink / raw)
  To: kilgallen

Larry Kilgallen <kilgallen@eisner.decus.org> writes:
> In article <3l6j5k$h05@theopolis.orl.mmc.com>, Theodore Dennison <dennison@escmail.orl.mmc.com> writes:
> > Robert Dewar <dewar@cs.nyu.edu> writes:
> 
> >> GNAT certainly allows the main program to be in a language other than C
> >> with no restrictions (in fact the main program of the *all* GNAT programs
> >> is typically in C, since that's what the binder generates). But you can
> >> also have your own explicit main program.
> 
> > With NO restrictions? Including tasking? The C program doesn't have to make
> > any special calls to elaborate everything?
> 
> We have had two examples, both claiming that the respective compilers
> did not have such a restriction.  Perhaps it would be more of a service
> to list those implementations which _do_ have such a restriction.

Well, my claim is that gnat DOES have such a restriction. If your main
routine is not Ada (or rather, gnat's automaticly genereated C main
routine) then you have to manually call the routines to elaborate your
code (assuming you have elaboration code). 

SunAda requires you go through roughly the same process. Since SunAda
is VERDIX, there are probably many more implementations with the same
approach.

This is all getting a little off of the subject, which was helping 
someone with a SunAda problem. For my own (rather large) part in dragging
us off that subject, I apologize.

T.E.D. (structured programming bigot)





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

* Re: Calling Ada from C
  1995-03-27 14:47       ` Theodore Dennison
  1995-03-28  0:00         ` Robert Dewar
  1995-03-28  0:00         ` Cyrille Comar
@ 1995-03-29  2:47         ` Larry Kilgallen, LJK Software
  1995-03-29  0:00           ` Theodore Dennison
  2 siblings, 1 reply; 27+ messages in thread
From: Larry Kilgallen, LJK Software @ 1995-03-29  2:47 UTC (permalink / raw)


In article <3l6j5k$h05@theopolis.orl.mmc.com>, Theodore Dennison <dennison@escmail.orl.mmc.com> writes:
> Robert Dewar <dewar@cs.nyu.edu> writes:
> 
>> GNAT certainly allows the main program to be in a language other than C
>> with no restrictions (in fact the main program of the *all* GNAT programs
>> is typically in C, since that's what the binder generates). But you can
>> also have your own explicit main program.
> 
> With NO restrictions? Including tasking? The C program doesn't have to make
> any special calls to elaborate everything?

We have had two examples, both claiming that the respective compilers
did not have such a restriction.  Perhaps it would be more of a service
to list those implementations which _do_ have such a restriction.



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

* Re: Calling Ada from C
  1995-03-29  0:00           ` Theodore Dennison
@ 1995-04-04  0:00             ` Robert Dewar
  0 siblings, 0 replies; 27+ messages in thread
From: Robert Dewar @ 1995-04-04  0:00 UTC (permalink / raw)


T.E.D. notes that GNAT has the "restriction" that if the main program is
not in Ada, then you must "manually
 call the elaboration routine.

It is true that this is the case, and indeed thuis is according to
Implementation Advice in the RM, which even suggests the name of the
routine to be called "adainit".

We can in fact eventually hook into the collect2 mechanism that is used
to automatically elaborate C++ constructors. However, I am not sure that
this "removal of the restriction" is such a good thing. It seems to me
to have advantages to control exactly where elaboration takes place.





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

* calling ADA from C
  1996-10-29  0:00 How is an ADA compiler done? Robert Dewar
@ 1996-11-08  0:00 ` EDSTAM Mikael
  1996-11-14  0:00   ` Robert Dewar
  0 siblings, 1 reply; 27+ messages in thread
From: EDSTAM Mikael @ 1996-11-08  0:00 UTC (permalink / raw)



Is there anyone who knows how to call an ADA procedure from a C program, using gcc ?

Thanks
Mikael Edstam








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

* Re: calling ADA from C
  1996-11-08  0:00 ` calling ADA from C EDSTAM Mikael
@ 1996-11-14  0:00   ` Robert Dewar
  0 siblings, 0 replies; 27+ messages in thread
From: Robert Dewar @ 1996-11-14  0:00 UTC (permalink / raw)



Mikael asks

"Is there anyone who knows how to call an ADA procedure from a C program, using g
cc ?

Thanks
Mikael Edstam"


If there is such a person, perhaps they have access to the secret document
gnatinfo.txt which has a section called

   Building a Program where the Main Program is not in Ada

:-)

To locate this secret document, check your GNAT distribution (all 
distributions of GNAT include this file).

Now of course if you are truying to execute programs of the Americal
Dental Association from C, that's harder, but I assume you mean Ada
and not ADA :-)





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

* Calling Ada from C
@ 2000-08-25  0:00 Maxwelton
  0 siblings, 0 replies; 27+ messages in thread
From: Maxwelton @ 2000-08-25  0:00 UTC (permalink / raw)


I am missing something in a program I am working on. I am using a C
function to call a function in Ada.  I have the function defined in
a ".h" file as an "extern". It takes a string as a parameter. In Ada I
have a function with the same name defined along with the export pragma
in the .ads file. The C function name is the 3rd parameter. If I were
calling "C" from Ada I would use the import pragma. The .adb file has
the function containing
       procedure To_Ada (Item     : in char_array;
                         Target   : out String;
                         Count    : out Natural;
                         Trim_Nul : in Boolean := True);

I am using the Interfaces.C package in my ada files. My files compile
but the C compilation generates an error that states "undefined
function". What am I not doing?

--
Maxwelton


Sent via Deja.com http://www.deja.com/
Before you buy.




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

* Calling Ada from C
@ 2007-02-22 15:46 hannibal.holm
  2007-02-22 16:17 ` Ludovic Brenta
                   ` (3 more replies)
  0 siblings, 4 replies; 27+ messages in thread
From: hannibal.holm @ 2007-02-22 15:46 UTC (permalink / raw)


I have a slight problem trying to call an Ada function from a C
function. I need to pass in an unconstrained array to the Ada
function. The problem is how I specify the size.

This is probably very simple, but I am more or less just getting
started with Ada, comming from a C-background.

My code yealds warnings like this (when compiled with GNAT):

foo.ads:50:23: warning: type of argument "Insert_C.Packet" is
unconstrained array
foo.ads:50:23: warning: foreign caller must pass bounds explicitly

I have been searching a lot in order to figure out what to do about
this, but all the FFI documentation is for calling C-functions from
Ada, and some very simple examples of how to call ada functions that
take primitive arguments (ints, and similar items).

Anyone who know how to pass in the bounds explicitly?




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

* Re: Calling Ada from C
  2007-02-22 15:46 hannibal.holm
@ 2007-02-22 16:17 ` Ludovic Brenta
  2007-02-22 17:28 ` Adam Beneschan
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 27+ messages in thread
From: Ludovic Brenta @ 2007-02-22 16:17 UTC (permalink / raw)


Hannibal Holm writes:
> I have a slight problem trying to call an Ada function from a C
> function. I need to pass in an unconstrained array to the Ada
> function. The problem is how I specify the size.
>
> This is probably very simple, but I am more or less just getting
> started with Ada, comming from a C-background.
>
> My code yealds warnings like this (when compiled with GNAT):
>
> foo.ads:50:23: warning: type of argument "Insert_C.Packet" is
> unconstrained array
> foo.ads:50:23: warning: foreign caller must pass bounds explicitly
>
> I have been searching a lot in order to figure out what to do about
> this, but all the FFI documentation is for calling C-functions from
> Ada, and some very simple examples of how to call ada functions that
> take primitive arguments (ints, and similar items).
>
> Anyone who know how to pass in the bounds explicitly?

You have three options:
- pass two extra parameters, First and Last
- pass the length
- declare that the lower bound is some fixed "known" value and pass the
  upper bound (this is subtly different than the above...)

-- 
Ludovic Brenta.



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

* Re: Calling Ada from C
  2007-02-22 15:46 hannibal.holm
  2007-02-22 16:17 ` Ludovic Brenta
@ 2007-02-22 17:28 ` Adam Beneschan
  2007-02-22 20:40 ` Aurele
  2007-02-23 13:53 ` Stephen Leake
  3 siblings, 0 replies; 27+ messages in thread
From: Adam Beneschan @ 2007-02-22 17:28 UTC (permalink / raw)


On Feb 22, 7:46 am, "hannibal.h...@gmail.com"
<hannibal.h...@gmail.com> wrote:
> I have a slight problem trying to call an Ada function from a C
> function. I need to pass in an unconstrained array to the Ada
> function. The problem is how I specify the size.
>
> This is probably very simple, but I am more or less just getting
> started with Ada, comming from a C-background.
>
> My code yealds warnings like this (when compiled with GNAT):
>
> foo.ads:50:23: warning: type of argument "Insert_C.Packet" is
> unconstrained array
> foo.ads:50:23: warning: foreign caller must pass bounds explicitly
>
> I have been searching a lot in order to figure out what to do about
> this, but all the FFI documentation is for calling C-functions from
> Ada, and some very simple examples of how to call ada functions that
> take primitive arguments (ints, and similar items).
>
> Anyone who know how to pass in the bounds explicitly?

Don't use an unconstrained array.  If I were to write an procedure P
to be called from C that would take, say, an unknown-size array of
integers, I'd do it like this:

   type Int_Array is array (Integer range <>) of Integer;
      -- this is the unconstrained array

   subtype Constrained_Int_Array is Int_Array (0 .. Integer'Last);
      -- or 1 .. Integer'Last, if you prefer

   procedure P (Arr : in out Constrained_Int_Array;  Len : in
Integer);

Then, inside P, just make sure you use Len; don't use Arr'Range or
Arr'Last which will be useless.  Not the safest thing, since now your
Ada program won't do the automatic bounds checking, but it's portable.

Or, you could write a second Ada routine to do all the work:

   procedure P2 (Arr : in Int_Array);  -- use the unconstrained array

and make P simply a wrapper:

   procedure P (Arr : in out Constrained_Int_Array;  Len : in Integer)
is
   begin
      P2 (Arr (Arr'First .. Arr'First + Len - 1));
   end P;


When you pass unconstrained arrays around as parameters from one Ada
subprogram to another, the subprograms will know the Ada conventions
for passing those arrays around.  A C program won't know how to do
this, unless you find out how your particular Ada implementation does
this and make your C program set up the data in the same way, but that
will make your program non-portable.  That's why I would stick to
constrained arrays when calling Ada from non-Ada (or vice versa).

                          -- Adam








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

* Re: Calling Ada from C
  2007-02-22 15:46 hannibal.holm
  2007-02-22 16:17 ` Ludovic Brenta
  2007-02-22 17:28 ` Adam Beneschan
@ 2007-02-22 20:40 ` Aurele
  2007-02-23 13:53 ` Stephen Leake
  3 siblings, 0 replies; 27+ messages in thread
From: Aurele @ 2007-02-22 20:40 UTC (permalink / raw)


On Feb 22, 10:46 am, "hannibal.h...@gmail.com"
<hannibal.h...@gmail.com> wrote:
> I have a slight problem trying to call an Ada function from a C
> function. I need to pass in an unconstrained array to the Ada
> function. The problem is how I specify the size.
>
> This is probably very simple, but I am more or less just getting
> started with Ada, comming from a C-background.
>
> My code yealds warnings like this (when compiled with GNAT):
>
> foo.ads:50:23: warning: type of argument "Insert_C.Packet" is
> unconstrained array
> foo.ads:50:23: warning: foreign caller must pass bounds explicitly
>
> I have been searching a lot in order to figure out what to do about
> this, but all the FFI documentation is for calling C-functions from
> Ada, and some very simple examples of how to call ada functions that
> take primitive arguments (ints, and similar items).
>
> Anyone who know how to pass in the bounds explicitly?

Have you thought of passing it as an acces "pointer" to your array?




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

* Re: Calling Ada from C
  2007-02-22 15:46 hannibal.holm
                   ` (2 preceding siblings ...)
  2007-02-22 20:40 ` Aurele
@ 2007-02-23 13:53 ` Stephen Leake
  2007-02-27 12:49   ` hannibal.holm
  3 siblings, 1 reply; 27+ messages in thread
From: Stephen Leake @ 2007-02-23 13:53 UTC (permalink / raw)


"hannibal.holm@gmail.com" <hannibal.holm@gmail.com> writes:

> I have a slight problem trying to call an Ada function from a C
> function. I need to pass in an unconstrained array to the Ada
> function. The problem is how I specify the size.

Since the concept "unconstrained array" doesn't exist in C, this can't
possibly be a precise description of what you are doing.

Can you give more context, and an example of the code? Especially the
C declarations of the data structure you are passing to Ada.

-- 
-- Stephe



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

* Re: Calling Ada from C
  2007-02-23 13:53 ` Stephen Leake
@ 2007-02-27 12:49   ` hannibal.holm
  2007-02-27 18:03     ` Adam Beneschan
  2007-02-28  2:54     ` Jeffrey R. Carter
  0 siblings, 2 replies; 27+ messages in thread
From: hannibal.holm @ 2007-02-27 12:49 UTC (permalink / raw)


On Feb 23, 2:53 pm, Stephen Leake <stephen_le...@stephe-leake.org>
wrote:
> "hannibal.h...@gmail.com" <hannibal.h...@gmail.com> writes:
> > I have a slight problem trying to call an Ada function from a C
> > function. I need to pass in an unconstrained array to the Ada
> > function. The problem is how I specify the size.
>
> Since the concept "unconstrained array" doesn't exist in C, this can't
> possibly be a precise description of what you are doing.
>
> Can you give more context, and an example of the code? Especially the
> C declarations of the data structure you are passing to Ada.
>
> --
> -- Stephe

The data structure in C is just a pointer to a block of data to be
sent over a network link. This need to be converted to a byte array
defined as:

Byte_Size   : constant := 8;

type Byte is range 0 .. 2**Byte_Size - 1;
for Byte'Size use Byte_Size;

type Byte_Array is array (Natural range <>) of Byte;
pragma Pack (Byte_Array);

As said, I am not extremly familiar with Ada and come from a C-
background, so I have a few problems trying to figure out what the ada
runtime and type system actually does. I tried something like

-- This proc is exported as with C-conventions
procedure Some_Ada_Proc(Msg : in Byte_Array;
                        Len : in Natural) is
  Msg_Constr : Byte_Array := Msg(0 .. Length - 1);
begin
...
end Some_Ada_Proc;

This is called with:

char foo[] = {0xde, 0xad, 0xbe, 0xef};
some_ada_proc(foo, sizeof(foo)); // sizeof static array = 4

But that result in a crash which I assume is due to how Ada treats
arrays passed as arguments.

I would be very greatful for any help, and an explanation on what is
actually going on in the Ada runtime system, e.g. descriptions on how
the Ada ABI works in this case.

Preferably I would like to do a toll free conversion from the C-
pointer to the Ada array, is this possible?




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

* Re: Calling Ada from C
  2007-02-27 12:49   ` hannibal.holm
@ 2007-02-27 18:03     ` Adam Beneschan
  2007-02-28  2:54     ` Jeffrey R. Carter
  1 sibling, 0 replies; 27+ messages in thread
From: Adam Beneschan @ 2007-02-27 18:03 UTC (permalink / raw)


On Feb 27, 4:49 am, "hannibal.h...@gmail.com"
<hannibal.h...@gmail.com> wrote:
> On Feb 23, 2:53 pm, Stephen Leake <stephen_le...@stephe-leake.org>
> wrote:
>
> > "hannibal.h...@gmail.com" <hannibal.h...@gmail.com> writes:
> > > I have a slight problem trying to call an Ada function from a C
> > > function. I need to pass in an unconstrained array to the Ada
> > > function. The problem is how I specify the size.
>
> > Since the concept "unconstrained array" doesn't exist in C, this can't
> > possibly be a precise description of what you are doing.
>
> > Can you give more context, and an example of the code? Especially the
> > C declarations of the data structure you are passing to Ada.
>
> > --
> > -- Stephe
>
> The data structure in C is just a pointer to a block of data to be
> sent over a network link. This need to be converted to a byte array
> defined as:
>
> Byte_Size   : constant := 8;
>
> type Byte is range 0 .. 2**Byte_Size - 1;
> for Byte'Size use Byte_Size;
>
> type Byte_Array is array (Natural range <>) of Byte;
> pragma Pack (Byte_Array);
>
> As said, I am not extremly familiar with Ada and come from a C-
> background, so I have a few problems trying to figure out what the ada
> runtime and type system actually does. I tried something like
>
> -- This proc is exported as with C-conventions
> procedure Some_Ada_Proc(Msg : in Byte_Array;
>                         Len : in Natural) is
>   Msg_Constr : Byte_Array := Msg(0 .. Length - 1);
> begin
> ...
> end Some_Ada_Proc;
>
> This is called with:
>
> char foo[] = {0xde, 0xad, 0xbe, 0xef};
> some_ada_proc(foo, sizeof(foo)); // sizeof static array = 4
>
> But that result in a crash which I assume is due to how Ada treats
> arrays passed as arguments.

No, it's due to how Ada treats *unconstrained* arrays passed as
arguments.  When an Ada procedure has an unconstrained array as the
argument, it needs the bounds (the 'First and 'Last of the actual
array), so those have to be passed in somehow.

When the array parameter is declared with a *constrained* array type
[technically, an array subtype], the Ada routine knows the bounds
already, so they don't have to be passed in.  So there normally isn't
anything funny about how they get passed in; the Ada routine will
normally just take the address of the first element of the array, the
way you'd expect any other language to do it.

So instead of making Msg a Byte_Array, which is unconstrained, make it
some constrained subtype:

 type Byte_Array is array (Natural range <>) of Byte;
 pragma Pack (Byte_Array);

 subtype Byte_Array_Constrained is Byte_Array (Natural'First ..
Natural'Last);
   -- pick a better name than this

 procedure Some_Ada_Proc (Msg : in Byte_Array_Constrained; ...

Please see my earlier post for more details.

                          -- Adam




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

* Re: Calling Ada from C
  2007-02-27 12:49   ` hannibal.holm
  2007-02-27 18:03     ` Adam Beneschan
@ 2007-02-28  2:54     ` Jeffrey R. Carter
  1 sibling, 0 replies; 27+ messages in thread
From: Jeffrey R. Carter @ 2007-02-28  2:54 UTC (permalink / raw)


hannibal.holm@gmail.com wrote:
> 
> The data structure in C is just a pointer to a block of data to be
> sent over a network link. This need to be converted to a byte array
> defined as:
> 
> Byte_Size   : constant := 8;
> 
> type Byte is range 0 .. 2**Byte_Size - 1;
> for Byte'Size use Byte_Size;

Anything you use to talk to C should be convention C. Since, from below, 
in C this seems to be char, you should use Interfaces.C.Char and 
Interfaces.C.Char_Array. Since Char_Array is convention C, Ada won't 
expect it to have bounds associated with it.

> 
> type Byte_Array is array (Natural range <>) of Byte;
> pragma Pack (Byte_Array);
> 
> As said, I am not extremly familiar with Ada and come from a C-
> background, so I have a few problems trying to figure out what the ada
> runtime and type system actually does. I tried something like
> 
> -- This proc is exported as with C-conventions
> procedure Some_Ada_Proc(Msg : in Byte_Array;
>                         Len : in Natural) is
>   Msg_Constr : Byte_Array := Msg(0 .. Length - 1);

Again, you should use Interfaces.C.Int instead of Natural.

Where does Length come from?

> This is called with:
> 
> char foo[] = {0xde, 0xad, 0xbe, 0xef};
> some_ada_proc(foo, sizeof(foo)); // sizeof static array = 4

This is where I get char from above.

If using Char_Array still crashes, you can use a very large constrained 
subtype of Char_Array:

subtype Many_Chars is Interfaces.C.Char_Array (Interfaces.C.Size_T);

and slice the result with 0 .. Interfaces.C.Size_T (Len). This can then 
be converted into your array of Bytes.

-- 
Jeff Carter
"I would never want to belong to any club that
would have someone like me for a member."
Annie Hall
41



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

end of thread, other threads:[~2007-02-28  2:54 UTC | newest]

Thread overview: 27+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1995-03-22 13:26 Calling Ada from C Roger L Costello
1995-03-22 13:58 ` David Paton
1995-03-23 17:22 ` Theodore Dennison
1995-03-24 17:14   ` Larry Kilgallen, LJK Software
1995-03-26 11:53     ` Robert Dewar
1995-03-27 14:47       ` Theodore Dennison
1995-03-28  0:00         ` Robert Dewar
1995-03-28  0:00         ` Cyrille Comar
1995-03-29  2:47         ` Larry Kilgallen, LJK Software
1995-03-29  0:00           ` Theodore Dennison
1995-04-04  0:00             ` Robert Dewar
1995-03-27 19:48     ` Robert I. Eachus
1995-03-29  0:00       ` Larry Kilgallen, LJK Software
  -- strict thread matches above, loose matches on Subject: below --
2007-02-22 15:46 hannibal.holm
2007-02-22 16:17 ` Ludovic Brenta
2007-02-22 17:28 ` Adam Beneschan
2007-02-22 20:40 ` Aurele
2007-02-23 13:53 ` Stephen Leake
2007-02-27 12:49   ` hannibal.holm
2007-02-27 18:03     ` Adam Beneschan
2007-02-28  2:54     ` Jeffrey R. Carter
2000-08-25  0:00 Maxwelton
1996-10-29  0:00 How is an ADA compiler done? Robert Dewar
1996-11-08  0:00 ` calling ADA from C EDSTAM Mikael
1996-11-14  0:00   ` Robert Dewar
1991-02-23 16:01 calling Ada " David B Lightstone
1988-10-16  0:23 calling ada from c Maureen Cragg
1988-10-17 17:21 ` Maureen Cragg

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