[comp.lang.ada] Fortran and Ada

emery%aries@MITRE-BEDFORD.ARPA (David Emery) (11/09/87)

Mary McNabb writes about a problem she has in trying to pass data
between Ada and fortran.  I think the problem is not with the external
object thisandthat, but with the parameter passed to fortran from Ada.
In a nutshell, I suspect that she was a bit too clever...

One thing in her code that is a bit wierd is the following:

  value : system.address;
  procedure fsub(value : system.address);
  pragma interface (fortran, fsub, "_fsub");

  begin
    value := system.address(3);	-- what does this mean?
				-- if system.address is private, this is
				-- illegal.  
				-- if system.address is a numeric type,
				-- then this is the memory address 3.
  				-- this is NOT the address of the constant
				-- "3"
    fsub(value);
  end;

Therefore, when calling fortran fsub(i), where I is an integer, and
given fortran passing parameters by address, what we have is a reference
to memory address 3, which is deep in the kernel, and causes unix to burp!

Here is what I think she meant:

  value : integer;
  procedure fsub(value : system.address);
  pragma interface (fortran, fsub, "_fsub");
  
  begin
    value := 3;
    fsub(value'address);
  end;

There are some potential problems here, too.  The Ada declaration of fsub is

  procedure fsub(value : in system.address);
  pragma interface (fortran, fsub, "_fsub");
  
and the Fortran declaration is
      subroutine fsub(i)
      integer i
And these may not be the same.  In particular, depending on the exact 
semantics of pragma interface for Fortran, what you may get is 2 level 
indirect addressing, in the following sense.

"fsub is a fortran routine.  fortran routines pass all values by address.
 Therefore, what we give fortran is the address of the parameter.  Since
 the Ada parameter type is system.address, what we will pass fortran is 
 the address of this address.  So what we do is the following:  
    1.  allocate some memory (stack or heap), and put the parameter there.
    2.  pass the address of this memory to fortran
 So, fortran sees the address of the address of the value, not the address
 of the value."

This interpretation is consistent with the value of I being a very
large number when seen in fortran.  I is an address somewhere deep in
the program (probably on the callers stack...)

So, really what I suspect we should have is the following:
(Isn't this what she really wants to say???)

  procedure fsub (value : integer);
  pragma interface (fortran, fsub, "_fsub");

  value : integer;

  begin
    value := 3;
    fsub(value);
  end;

I hope this helps...

				dave emery
				emery@mitre-bedford.arpa