(j3.2006) Interesting F2003-ism

Andy Vaught andyv
Thu Feb 22 15:08:32 EST 2007

On Thu, 22 Feb 2007, Michael I. wrote:

> Just for the sake of argument, here's an implementation
> that will choke on your interface:
> Separate integer and floating point registers.
> Pointers and integers passed in integer registers (only) when no ellipsis.
> Reals passed in floating point registers (only) when no ellipsis.
> Elided arguments always passed on the stack whether real or integer or 
> pointer.
>The interface then entails the self-contradictory assertion
>        the 2nd argument of the routine with binding label 'printf' is
>                passed only in integer registers and
>                is passed only in floating point registers

  But you can't implement a C ABI that way.  Variable argument lists have
to be passed in the same manner as fixed argument lists.  Otherwise, a
variable argument list function won't work when invoked through a function
pointer that has been cast around a bit.

  I agree that it is common practice to pass parameters in registers, but
the va_arg() macros sort things out.  I tried the following on a processor
that does this, the sparc:

andy at sparc % cat tst.f90
program main
use iso_c_binding
     subroutine p1(f, a1, a2, a3, a4) bind(c, name='printf')
       import :: c_ptr
       type(c_ptr), value :: f
       integer, value :: a1, a3
       double precision, value :: a2, a4
     end subroutine p1

     subroutine p2(f, a1, a2, a3, a4) bind(c, name='printf')
       import :: c_ptr
       type(c_ptr), value :: f
       double precision, value :: a1, a3
       integer, value :: a2, a4
     end subroutine p2
  end interface

  type(c_ptr) :: f_ptr
  character(len=20), target :: format

  f_ptr = c_loc(format(1:1))

  format = 'Hello %d %f %d %f\n' // char(0)
  call p1(f_ptr, 10, 1.23d0, 20, 2.46d0)

  format = 'World %f %d %f %d\n' // char(0)
  call p2(f_ptr, 1.23d0, 10, 2.46d0, 20)
end program main

andy at sparc % g95 tst.f90
andy at sparc % ./a.out
Hello 10 1.230000 20 2.460000
World 1.230000 10 2.460000 20
andy at sparc %

  Works as expected.


More information about the J3 mailing list