Q: Interfacing C/ADA: Indirect Calls 
Author Message
 Q: Interfacing C/ADA: Indirect Calls

Hello,

here comes another question concerning Ada interfacing C.
The idea is to call 2 C-functions inidrectly from an Ada program. The
test
program I wrote does not compile (I am using gnat running on linux):

gnatmake -c  use_indirect.adb -o use_indirect.o  
gcc -c use_indirect.adb
use_indirect.adb:10:08: subprogram has invalid convention for context
use_indirect.adb:16:08: subprogram has invalid convention for context
gnatmake: "use_indirect.adb" compilation error
make: *** [use_indirect.o] Error 4

The user guide nor the reference manual seem not to explicitly cover
this (at least
I did not find anything about indirect calls to code in other
languages).

Thanks in anticipation,

Tilman

========================================================================
/* C procedures */
#include <stdio.h>

int function1(int p1,int p2) {
 return(p1+p2);

Quote:
}

int function2(int p1, int p2) {
 return(p1*p2);

Quote:
}    

-- --------------- pacakage ind_call -----------------
with Interfaces.C;use Interfaces.C;
package ind_call is
 type Indirect_Func is access function(param_1 : in Int;
                                      param_2 : in Int)
                                     return Int;
 function C_Function1(p1: in Int; p2: in Int) return Int;
 function C_Function2(p1: in Int; p2: in Int) return Int;
 pragma Import(C,C_Function1,"function1");
 pragma Import(C,C_Function2,"function2");
end ind_call;  

----------- "main" program -----------------
with TEXT_IO;use TEXT_io;
with Interfaces.C; use Interfaces.C;
with ind_call;use ind_call;

procedure use_indirect is
 package Int_io is new INTEGER_IO(Int);
 f1: Indirect_Func;
 I:  Int;
begin
 f1 := C_Function1'Access;  -- this does not compile
 I  := f1(2,3);
 Text_io.Put("Function1 = ");
 Int_io.Put(I);
 New_line;
 f1 := C_Function2'Access;  -- this does not compile
 I  := f1(2,3);
 Text_io.Put("Function2 = ");
 Int_io.Put(I);
 New_line;
end use_indirect;



Sun, 05 Oct 2003 20:54:25 GMT  
 Q: Interfacing C/ADA: Indirect Calls

Quote:

> use_indirect.adb:10:08: subprogram has invalid convention for context
> use_indirect.adb:16:08: subprogram has invalid convention for context

I think you have to insert

   pragma Convention (C, Indirect_Func);

to change the convention of the access-to-subprogram type.



Sun, 05 Oct 2003 19:46:51 GMT  
 Q: Interfacing C/ADA: Indirect Calls
That works --  Great.

For my curiousity: What exactly is an access type ? Apperently, it is not a
reference to an object by memory address (like C references an object).

Thanks,

Tilman



Quote:

> > use_indirect.adb:10:08: subprogram has invalid convention for context
> > use_indirect.adb:16:08: subprogram has invalid convention for context

> I think you have to insert

>    pragma Convention (C, Indirect_Func);

> to change the convention of the access-to-subprogram type.



Sun, 05 Oct 2003 21:56:28 GMT  
 Q: Interfacing C/ADA: Indirect Calls
It is very much like a reference to an object by memory address. In some
implementations of Ada, it will degenerate to exactly that. (As has been
observed in the past, access types are not simply addresses, because they do
more and may contain more information than just an address) For practical
usage, you can *think* of them as if they were just addresses, only with a
bunch of restrictions. An access object can only contain an object address
of some specific type. (Could be to a class-wide tagged type, so it is a bit
more flexible than might initially appear.) They used to be used pretty much
exclusively for dynamically allocated objects (roughly equivalent to
malloc's in C) but in Ada95 they can now point to just about anything. The
language rules that apply to access types are there for your safety. (You
can circumvent them, but you should have a good reason to do so.) They
attempt to guarantee that you can't leave around dangling references to
things that no longer exist.

Access types can be very powerful, but are not the most simple part of the
language. There is often a temptation on the part of C/C++ programmers to
over-utilize them. (For example, in passing parameters to subroutines to
achieve "pass by reference" when Ada is perfectly willing to handle all this
for you without you're having to get pointers to everything in sight.) If
you employ the use of access types, try to limit their scope and isolate
them as best you can. (For example, in building a linked list package) You
*really* don't need them anywhere near as much as you do in C/C++ so if you
find yourself using them all over the place, check your assumptions.
Otherwise, you can generally think of them as being similar to SomeType*
variables.

MDC
--
Marin David Condic
Senior Software Engineer
Pace Micro Technology Americas    www.pacemicro.com
Enabling the digital revolution

Web:      http://www.mcondic.com/


Quote:
> That works --  Great.

> For my curiousity: What exactly is an access type ? Apperently, it is not
a
> reference to an object by memory address (like C references an object).

> Thanks,

> Tilman




> > > use_indirect.adb:10:08: subprogram has invalid convention for context
> > > use_indirect.adb:16:08: subprogram has invalid convention for context

> > I think you have to insert

> >    pragma Convention (C, Indirect_Func);

> > to change the convention of the access-to-subprogram type.



Sun, 05 Oct 2003 22:53:10 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Indirect calls using MASM 6.1 (how?)

2. indirect call with MASM?

3. Help - indirect call

4. indirect function calls and variable variables

5. Indirect function member calls

6. Indirect calls

7. Indirect calls

8. indirect function calls and variable variables

9. Question regarding indirect function calls

10. indirect **keyword calls

11. Indirect Ada Reference in EETimes

12. Interfacing Ada to Ethernet Unix Calls

 

 
Powered by phpBB® Forum Software