This is the mail archive of the
fortran@gcc.gnu.org
mailing list for the GNU Fortran project.
How to add intrinsic functions to gfortran?
- From: Steve Kargl <sgk at troutmask dot apl dot washington dot edu>
- To: fortran at gcc dot gnu dot org
- Date: Fri, 28 May 2004 12:08:37 -0700
- Subject: How to add intrinsic functions to gfortran?
Is there a document that describes how to add an
intrinsic function to gfortran?
I've implemented G77's irand(), srand(), and rand()
procedures. srand() is an intrinsic subroutine and
as far as I can tell it is seen, works, and is used by
gfortran. On the other hand, irand() and rand() are
not working and I suspect some name mangling is need.
I'll restrict the discussion to irand() where the Fortran
prototypes is
integer function irand(integer flag)
In my C code, I have tried the following declarations
GFC_INTEGER_4 prefix(irand) (GFC_INTEGER_4 flag)
GFC_INTEGER_4 __irand (GFC_INTEGER_4 flag)
GFC_INTEGER_4 _irand (GFC_INTEGER_4 flag)
GFC_INTEGER_4 irand (GFC_INTEGER_4 flag)
In all cases, gfortran compiles
program cc
integer i
i = irand(0)
end program cc
to the following tree
troutmask:sgk[286] more cc.f90.t03.original
MAIN__ ()
{
int4 i;
i = 0;
}
In intrinsic.c, I have
add_sym_1 ("irand", 0, 1, BT_INTEGER, di,
gfc_check_irand, gfc_simplify_irand, NULL,
i, BT_INTEGER, di, 0);
make_generic ("irand", GFC_ISYM_IRAND);
where GFC_ISYM_IRAND is added to "enum gfc_generic_isym_id" in
gfortran.h. gfc_simplify_irand is required or gfortran yields
an ICE. It looks like
gfc_expr *
gfc_simplify_irand (gfc_expr * e)
{
gfc_expr *result;
if (e->expr_type != EXPR_CONSTANT) return NULL;
result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind(),
&e->where);
mpz_set (result->value.integer, e->value.integer);
return range_check (result, "IRAND");
}
I've tried various version of a gfc_resolve_irand function to
set the function name to each of the above 4 prototype function
names without much luck.
Any hints would be appreciated.
--
Steve