diff: example of calling Fortran from Mercury

Fergus Henderson fjh at cs.mu.OZ.AU
Tue May 19 20:06:00 AEST 1998


samples/c_interface/mercury_calls_fortran/*:
	Add an example of interfacing to Fortran code from Mercury.

Index: Mmakefile
===================================================================
RCS file: Mmakefile
diff -N Mmakefile
--- /dev/null	Wed May 28 10:49:58 1997
+++ Mmakefile	Tue May 19 19:50:10 1998
@@ -0,0 +1,31 @@
+#-----------------------------------------------------------------------------#
+# This source file is hereby placed in the public domain.  -fjh (the author).
+#-----------------------------------------------------------------------------#
+
+# Define rules for compiling Fortran
+
+FC=g77
+FFLAGS=-ffree-form
+
+.SUFFIXES: .f
+
+.f.o:
+	$(FC) $(FFLAGS) -c $<
+
+#-----------------------------------------------------------------------------#
+
+MAIN_TARGET=all
+
+depend: mercury_main.depend
+all: mercury_main
+
+# tell the Mercury linker to link in fortran_main.o and libf2c
+MLLIBS=fortran_main.o -lf2c
+
+# tell mmake that it needs to make fortran_main.o before it can make
+# mercury_main
+mercury_main: fortran_main.o
+
+# make sure that `mmake clean' removes fortran_main.o
+clean:
+	rm -f fortran_main.o
Index: fortran_main.f
===================================================================
RCS file: fortran_main.f
diff -N fortran_main.f
--- /dev/null	Wed May 28 10:49:58 1997
+++ fortran_main.f	Tue May 19 19:50:10 1998
@@ -0,0 +1,3 @@
+SUBROUTINE FORTRAN_MAIN
+	PRINT *, 'In FORTRAN_MAIN'
+END
Index: fortran_main_int.m
===================================================================
RCS file: fortran_main_int.m
diff -N fortran_main_int.m
--- /dev/null	Wed May 28 10:49:58 1997
+++ fortran_main_int.m	Tue May 19 19:50:10 1998
@@ -0,0 +1,31 @@
+% This module fortran_main_int defines a Mercury predicate fortran_main
+% which acts as an interface to the Fortran subroutine FORTRAN_MAIN,
+% which is defined in fortran_main.f.
+
+% This source file is hereby placed in the public domain.  -fjh (the author).
+
+:- module fortran_main_int.
+
+:- interface.
+:- import_module io.
+
+% Since the fortran_main() function has side effects, we declare the
+% corresponding Mercury predicate as one that takes an io__state pair. 
+% If we didn't do this, the Mercury compiler might optimize away calls to it!
+
+:- pred fortran_main(io__state::di, io__state::uo) is det.
+
+:- implementation.
+
+% Define the Mercury predicate fortran_main to call the Fortran
+% function FORTRAN_MAIN.  Note that g77 mangles names by converting
+% them to lowercase, and appending one or two underscores
+% (two if the name already contains any underscores, one otherwise).
+% So we need to use the name "fortran_main__" rather than "FORTRAN_MAIN".
+
+:- pragma import(fortran_main(di, uo), "fortran_main__").
+
+% Alternatively, you could use the "-fno-underscoring" and "-fcase-preserve"
+% options to g77 when compiling the Fortran.  Then the above declaration
+% could be like this instead:
+%	:- pragma import(fortran_main(di, uo), "FORTRAN_MAIN").
Index: mercury_main.m
===================================================================
RCS file: mercury_main.m
diff -N mercury_main.m
--- /dev/null	Wed May 28 10:49:58 1997
+++ mercury_main.m	Tue May 19 19:50:10 1998
@@ -0,0 +1,19 @@
+% This source file is hereby placed in the public domain.  -fjh (the author).
+
+:- module mercury_main.
+:- interface.
+:- import_module io.
+
+:- pred main(io__state::di, io__state::uo) is det.
+
+:- implementation.
+
+% import the module which defines the Mercury interface to the
+% Fortran procedure `FORTRAN_MAIN'.
+:- import_module fortran_main_int.
+
+% main just invokes fortran_main
+main -->
+	io__write_string("In Mercury main, about to call fortran_main...\n"),
+	fortran_main,
+	io__write_string("Back in Mercury main.\n").

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>  |  of excellence is a lethal habit"
PGP: finger fjh at 128.250.37.3        |     -- the last words of T. S. Garp.



More information about the developers mailing list