* Calling C function with structure parameter from ADA @ 1995-03-28 17:22 C.G. Williams 1995-03-29 0:00 ` Theodore Dennison 0 siblings, 1 reply; 6+ messages in thread From: C.G. Williams @ 1995-03-28 17:22 UTC (permalink / raw) A friend from China (Dr Kougen Zheng) asked me to post this request... Help Please!!! Q: How can an ADA program call a C function with a structure as a parameter? Any help will be greatly appreciated Please respond to: cgw1@le.ac.uk (I will post a summary of any replies in a few days) Details... The interpreter used is Adaed-1.11.0a What I did was... $ cc -c sub.c $ adacomp -nl runlib -i sub.o -a -b struct.ada $ adaexec runlib Error: core dumped!!! ****************** struct.ada ************************************* procedure first is type Point is record x : INTEGER; y : INTEGER; end record; a : Point; procedure PRINTA ( pp : in Point); pragma INTERFACE ( C, PRINTA); begin PRINTA(a); end; ************** sub.c ************************************* #include <stdio.h> typedef struct { int x; int y; } Point; printa(pp) Point pp; { printf("\n coordinate = ( %d , %d )", pp.x, pp.y); } *********************************************************************** Ceri Williams cgw1@le.ac.uk PhD Research student Geology Dept Leicester University Leicester U.K. ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Calling C function with structure parameter from ADA 1995-03-28 17:22 Calling C function with structure parameter from ADA C.G. Williams @ 1995-03-29 0:00 ` Theodore Dennison 1995-04-06 0:00 ` Robert Dewar 0 siblings, 1 reply; 6+ messages in thread From: Theodore Dennison @ 1995-03-29 0:00 UTC (permalink / raw) To: cgw1 C.G. Williams<cgw1@le.ac.uk> > A friend from China (Dr Kougen Zheng) asked me to post this request... > > Help Please!!! > Q: How can an ADA program call a C function with a structure as a parameter? > Any help will be greatly appreciated > > Please respond to: cgw1@le.ac.uk > (I will post a summary of any replies in a few days) > .. > typedef struct { > int x; > int y; > } Point; > > printa(pp) > Point pp; > > printf("\n coordinate = ( %d , %d )", pp.x, pp.y); > The problem here is that C passes the Point structure to printa by pushing the Point structure on the stack. C (even ANSI C) does not define what order the bytes of Point are pushed on the stack! That means there is no safe way to call printa directly from Ada. In addition to this, Ada does not define weather it pushes Point on the stack, or meerly the Address of Point. However your Ada compiler docs should either say how it passes parameters, or allow you to specify. So... Make sure that your Ada compiler is passing the ADDRESS of Point (not Point itself). Next do either of the following: 2) Change the C printa procedure to read: printa (pp) Point *pp; printf("\n coordinate = ( %d , %d )", pp->x, pp->y); 3) Create a third C routine: printainterface (pp) Point *pp; printa (*pp); and change you Ada program to call printainterface instead. T.E.D. (structured programming bigot) ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Calling C function with structure parameter from ADA 1995-03-29 0:00 ` Theodore Dennison @ 1995-04-06 0:00 ` Robert Dewar 1995-04-07 0:00 ` Chris Warack <sys mgr> [not found] ` <D6pwGs.8wE@thomsoft.com> 0 siblings, 2 replies; 6+ messages in thread From: Robert Dewar @ 1995-04-06 0:00 UTC (permalink / raw) T.E.D. assumes that C passes structures by address. This is wrong, it varies from one machine environment to another. The RM essenstially repeats this misconception by requiring Ada to pass a record by address for Convention C, thus, on some machines ensuring that Ada is incompatible with C. Actually this is not a requirement, it is implementation advice. It is also one of the few cases in which GNAT quite deliberately declines to follow the implementation advice in the RM. In GNAT, if you make a procedure convention C, then we pass records EXACTLY THE SAME WAY THAT C would pass them, guaranteeing compatibility with C -- This seems a much better approach, and for example, simply and directly solves the original poster's problems. I hope other compilers will make this same sensible decision. If not, watch out, your Ada compiler may go out of its way to ensure that it is incompatible with C! Note: in discussion of this point earlier on, the MRT suggested simply changing the C code to pass the structure by pointer, but that is irrelevant to the task of creating bindings to existing C interfaces, where it is essential to be able to model C exactly. I suppose we could have two conventions C C_Done_Right but that seems unnecessary :-) ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Calling C function with structure parameter from ADA 1995-04-06 0:00 ` Robert Dewar @ 1995-04-07 0:00 ` Chris Warack <sys mgr> 1995-04-07 0:00 ` Robert Dewar [not found] ` <D6pwGs.8wE@thomsoft.com> 1 sibling, 1 reply; 6+ messages in thread From: Chris Warack <sys mgr> @ 1995-04-07 0:00 UTC (permalink / raw) In article <dewar.797190489@gnat>, dewar@cs.nyu.edu (Robert Dewar) writes: |> T.E.D. assumes that C passes structures by address. This is wrong, it varies |> from one machine environment to another. |> |> The RM essenstially repeats this misconception by requiring Ada to pass |> a record by address for Convention C, thus, on some machines ensuring |> that Ada is incompatible with C. |> |> Actually this is not a requirement, it is implementation advice. |> |> It is also one of the few cases in which GNAT quite deliberately declines |> to follow the implementation advice in the RM. |> |> In GNAT, if you make a procedure convention C, then we pass records |> EXACTLY THE SAME WAY THAT C would pass them, guaranteeing compatibility ^^^<-- gcc? |> with C -- This seems a much better approach, and for example, simply |> and directly solves the original poster's problems. Shouldn't the above reference refer to gcc's ability to compile c? That is, if I happen to be using a different C compiler to generate the object code that will be linked, then GNAT makes no such guarantee... or does it? If so, I'm duly impressed. :-)) |> I hope other compilers will make this same sensible decision. If not, |> watch out, your Ada compiler may go out of its way to ensure that it |> is incompatible with C! Unless they are guaranteed to be using the same conventions in the first place (by using the same technology perhaps), how is this generally possible? If it is possible, I am truly interested in how. -- Christopher A. Warack, Capt, USAF Computer Science Department, US Air Force Academy cwarack@kirk.usafa.af.mil (719) 472-2401 ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Calling C function with structure parameter from ADA 1995-04-07 0:00 ` Chris Warack <sys mgr> @ 1995-04-07 0:00 ` Robert Dewar 0 siblings, 0 replies; 6+ messages in thread From: Robert Dewar @ 1995-04-07 0:00 UTC (permalink / raw) Christopher, GCC goes out of its way to guarantee data compatibility and calling sequence compatibility with the native C compilers in a given configuration. If you ever find a case where there is an incompatibility, then GCC considers it a bug, and you should report it to the GCC mailing list as a bug. We know of no such bugs currently in any of the several hundred configurations supported by GCC. So When I say that GNAT's calling approach is compatible with C, I really mean that, I don't just mean it is compatible with GCC. Of course there is no guarantee that there is not a bug, but bugs can appear anywhere, and it definitely would be considered to be a bug if GNAT passed a record by value where C would pass it by address. If you find such a case, report it as a bug! ^ permalink raw reply [flat|nested] 6+ messages in thread
[parent not found: <D6pwGs.8wE@thomsoft.com>]
* Re: Calling C function with structure parameter from ADA [not found] ` <D6pwGs.8wE@thomsoft.com> @ 1995-04-19 0:00 ` Fergus Henderson 0 siblings, 0 replies; 6+ messages in thread From: Fergus Henderson @ 1995-04-19 0:00 UTC (permalink / raw) kst@thomsoft.com (Keith Thompson) writes: >In <dewar.797190489@gnat> dewar@cs.nyu.edu (Robert Dewar) writes: >> T.E.D. assumes that C passes structures by address. This is wrong, it varies >> from one machine environment to another. > >It shouldn't. The C standard specifies that all parameters, including >structures, are passed by copy, though of course you can explicitly >pass an address. The C standard just specifies that the semantics must be as if they were passed by copy. Implementations can and often do pass addresses but still maintain call-by-value semantics, e.g. by having the caller make a copy (if necessary) and then passing its address. -- Fergus Henderson | As practiced by computer science, the study of fjh@cs.mu.oz.au | programming is an unholy mixture of mathematics, http://www.cs.mu.oz.au/~fjh | literary criticism, and folklore. - B. A. Sheil ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~1995-04-19 0:00 UTC | newest] Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 1995-03-28 17:22 Calling C function with structure parameter from ADA C.G. Williams 1995-03-29 0:00 ` Theodore Dennison 1995-04-06 0:00 ` Robert Dewar 1995-04-07 0:00 ` Chris Warack <sys mgr> 1995-04-07 0:00 ` Robert Dewar [not found] ` <D6pwGs.8wE@thomsoft.com> 1995-04-19 0:00 ` Fergus Henderson
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox