annotate src/fftw-3.3.8/api/genf03.pl @ 169:223a55898ab9 tip default

Add null config files
author Chris Cannam <cannam@all-day-breakfast.com>
date Mon, 02 Mar 2020 14:03:47 +0000
parents bd3cc4d1df30
children
rev   line source
cannam@167 1 #!/usr/bin/perl -w
cannam@167 2 # Generate Fortran 2003 interfaces from a sequence of C function declarations
cannam@167 3 # of the form (one per line):
cannam@167 4 # extern <type> <name>(...args...)
cannam@167 5 # extern <type> <name>(...args...)
cannam@167 6 # ...
cannam@167 7 # with no line breaks within a given function. (It's too much work to
cannam@167 8 # write a general parser, since we just have to handle FFTW's header files.)
cannam@167 9
cannam@167 10 sub canonicalize_type {
cannam@167 11 my($type);
cannam@167 12 ($type) = @_;
cannam@167 13 $type =~ s/ +/ /g;
cannam@167 14 $type =~ s/^ //;
cannam@167 15 $type =~ s/ $//;
cannam@167 16 $type =~ s/([^\* ])\*/$1 \*/g;
cannam@167 17 return $type;
cannam@167 18 }
cannam@167 19
cannam@167 20 # C->Fortran map of supported return types
cannam@167 21 %return_types = (
cannam@167 22 "int" => "integer(C_INT)",
cannam@167 23 "ptrdiff_t" => "integer(C_INTPTR_T)",
cannam@167 24 "size_t" => "integer(C_SIZE_T)",
cannam@167 25 "double" => "real(C_DOUBLE)",
cannam@167 26 "float" => "real(C_FLOAT)",
cannam@167 27 "long double" => "real(C_LONG_DOUBLE)",
cannam@167 28 "__float128" => "real(16)",
cannam@167 29 "fftw_plan" => "type(C_PTR)",
cannam@167 30 "fftwf_plan" => "type(C_PTR)",
cannam@167 31 "fftwl_plan" => "type(C_PTR)",
cannam@167 32 "fftwq_plan" => "type(C_PTR)",
cannam@167 33 "void *" => "type(C_PTR)",
cannam@167 34 "char *" => "type(C_PTR)",
cannam@167 35 "double *" => "type(C_PTR)",
cannam@167 36 "float *" => "type(C_PTR)",
cannam@167 37 "long double *" => "type(C_PTR)",
cannam@167 38 "__float128 *" => "type(C_PTR)",
cannam@167 39 "fftw_complex *" => "type(C_PTR)",
cannam@167 40 "fftwf_complex *" => "type(C_PTR)",
cannam@167 41 "fftwl_complex *" => "type(C_PTR)",
cannam@167 42 "fftwq_complex *" => "type(C_PTR)",
cannam@167 43 );
cannam@167 44
cannam@167 45 # C->Fortran map of supported argument types
cannam@167 46 %arg_types = (
cannam@167 47 "int" => "integer(C_INT), value",
cannam@167 48 "unsigned" => "integer(C_INT), value",
cannam@167 49 "size_t" => "integer(C_SIZE_T), value",
cannam@167 50 "ptrdiff_t" => "integer(C_INTPTR_T), value",
cannam@167 51
cannam@167 52 "fftw_r2r_kind" => "integer(C_FFTW_R2R_KIND), value",
cannam@167 53 "fftwf_r2r_kind" => "integer(C_FFTW_R2R_KIND), value",
cannam@167 54 "fftwl_r2r_kind" => "integer(C_FFTW_R2R_KIND), value",
cannam@167 55 "fftwq_r2r_kind" => "integer(C_FFTW_R2R_KIND), value",
cannam@167 56
cannam@167 57 "double" => "real(C_DOUBLE), value",
cannam@167 58 "float" => "real(C_FLOAT), value",
cannam@167 59 "long double" => "real(C_LONG_DOUBLE), value",
cannam@167 60 "__float128" => "real(16), value",
cannam@167 61
cannam@167 62 "fftw_complex" => "complex(C_DOUBLE_COMPLEX), value",
cannam@167 63 "fftwf_complex" => "complex(C_DOUBLE_COMPLEX), value",
cannam@167 64 "fftwl_complex" => "complex(C_LONG_DOUBLE), value",
cannam@167 65 "fftwq_complex" => "complex(16), value",
cannam@167 66
cannam@167 67 "fftw_plan" => "type(C_PTR), value",
cannam@167 68 "fftwf_plan" => "type(C_PTR), value",
cannam@167 69 "fftwl_plan" => "type(C_PTR), value",
cannam@167 70 "fftwq_plan" => "type(C_PTR), value",
cannam@167 71 "const fftw_plan" => "type(C_PTR), value",
cannam@167 72 "const fftwf_plan" => "type(C_PTR), value",
cannam@167 73 "const fftwl_plan" => "type(C_PTR), value",
cannam@167 74 "const fftwq_plan" => "type(C_PTR), value",
cannam@167 75
cannam@167 76 "const int *" => "integer(C_INT), dimension(*), intent(in)",
cannam@167 77 "ptrdiff_t *" => "integer(C_INTPTR_T), intent(out)",
cannam@167 78 "const ptrdiff_t *" => "integer(C_INTPTR_T), dimension(*), intent(in)",
cannam@167 79
cannam@167 80 "const fftw_r2r_kind *" => "integer(C_FFTW_R2R_KIND), dimension(*), intent(in)",
cannam@167 81 "const fftwf_r2r_kind *" => "integer(C_FFTW_R2R_KIND), dimension(*), intent(in)",
cannam@167 82 "const fftwl_r2r_kind *" => "integer(C_FFTW_R2R_KIND), dimension(*), intent(in)",
cannam@167 83 "const fftwq_r2r_kind *" => "integer(C_FFTW_R2R_KIND), dimension(*), intent(in)",
cannam@167 84
cannam@167 85 "double *" => "real(C_DOUBLE), dimension(*), intent(out)",
cannam@167 86 "float *" => "real(C_FLOAT), dimension(*), intent(out)",
cannam@167 87 "long double *" => "real(C_LONG_DOUBLE), dimension(*), intent(out)",
cannam@167 88 "__float128 *" => "real(16), dimension(*), intent(out)",
cannam@167 89
cannam@167 90 "fftw_complex *" => "complex(C_DOUBLE_COMPLEX), dimension(*), intent(out)",
cannam@167 91 "fftwf_complex *" => "complex(C_FLOAT_COMPLEX), dimension(*), intent(out)",
cannam@167 92 "fftwl_complex *" => "complex(C_LONG_DOUBLE_COMPLEX), dimension(*), intent(out)",
cannam@167 93 "fftwq_complex *" => "complex(16), dimension(*), intent(out)",
cannam@167 94
cannam@167 95 "const fftw_iodim *" => "type(fftw_iodim), dimension(*), intent(in)",
cannam@167 96 "const fftwf_iodim *" => "type(fftwf_iodim), dimension(*), intent(in)",
cannam@167 97 "const fftwl_iodim *" => "type(fftwl_iodim), dimension(*), intent(in)",
cannam@167 98 "const fftwq_iodim *" => "type(fftwq_iodim), dimension(*), intent(in)",
cannam@167 99
cannam@167 100 "const fftw_iodim64 *" => "type(fftw_iodim64), dimension(*), intent(in)",
cannam@167 101 "const fftwf_iodim64 *" => "type(fftwf_iodim64), dimension(*), intent(in)",
cannam@167 102 "const fftwl_iodim64 *" => "type(fftwl_iodim64), dimension(*), intent(in)",
cannam@167 103 "const fftwq_iodim64 *" => "type(fftwq_iodim64), dimension(*), intent(in)",
cannam@167 104
cannam@167 105 "void *" => "type(C_PTR), value",
cannam@167 106 "FILE *" => "type(C_PTR), value",
cannam@167 107
cannam@167 108 "const char *" => "character(C_CHAR), dimension(*), intent(in)",
cannam@167 109
cannam@167 110 "fftw_write_char_func" => "type(C_FUNPTR), value",
cannam@167 111 "fftwf_write_char_func" => "type(C_FUNPTR), value",
cannam@167 112 "fftwl_write_char_func" => "type(C_FUNPTR), value",
cannam@167 113 "fftwq_write_char_func" => "type(C_FUNPTR), value",
cannam@167 114 "fftw_read_char_func" => "type(C_FUNPTR), value",
cannam@167 115 "fftwf_read_char_func" => "type(C_FUNPTR), value",
cannam@167 116 "fftwl_read_char_func" => "type(C_FUNPTR), value",
cannam@167 117 "fftwq_read_char_func" => "type(C_FUNPTR), value",
cannam@167 118
cannam@167 119 # Although the MPI standard defines this type as simply "integer",
cannam@167 120 # if we use integer without a 'C_' kind in a bind(C) interface then
cannam@167 121 # gfortran complains. Instead, since MPI also requires the C type
cannam@167 122 # MPI_Fint to match Fortran integers, we use the size of this type
cannam@167 123 # (extracted by configure and substituted by the Makefile).
cannam@167 124 "MPI_Comm" => "integer(C_MPI_FINT), value"
cannam@167 125 );
cannam@167 126
cannam@167 127 while (<>) {
cannam@167 128 next if /^ *$/;
cannam@167 129 if (/^ *extern +([a-zA-Z_0-9 ]+[ \*]) *([a-zA-Z_0-9]+) *\((.*)\) *$/) {
cannam@167 130 $ret = &canonicalize_type($1);
cannam@167 131 $name = $2;
cannam@167 132
cannam@167 133 $args = $3;
cannam@167 134 $args =~ s/^ *void *$//;
cannam@167 135
cannam@167 136 $bad = ($ret ne "void") && !exists($return_types{$ret});
cannam@167 137 foreach $arg (split(/ *, */, $args)) {
cannam@167 138 $arg =~ /^([a-zA-Z_0-9 ]+[ \*]) *([a-zA-Z_0-9]+) *$/;
cannam@167 139 $argtype = &canonicalize_type($1);
cannam@167 140 $bad = 1 if !exists($arg_types{$argtype});
cannam@167 141 }
cannam@167 142 if ($bad) {
cannam@167 143 print "! Unable to generate Fortran interface for $name\n";
cannam@167 144 next;
cannam@167 145 }
cannam@167 146
cannam@167 147 # any function taking an MPI_Comm arg needs a C wrapper (grr).
cannam@167 148 if ($args =~ /MPI_Comm/) {
cannam@167 149 $cname = $name . "_f03";
cannam@167 150 }
cannam@167 151 else {
cannam@167 152 $cname = $name;
cannam@167 153 }
cannam@167 154
cannam@167 155 # Fortran has a 132-character line-length limit by default (grr)
cannam@167 156 $len = 0;
cannam@167 157
cannam@167 158 print " "; $len = $len + length(" ");
cannam@167 159 if ($ret eq "void") {
cannam@167 160 $kind = "subroutine"
cannam@167 161 }
cannam@167 162 else {
cannam@167 163 print "$return_types{$ret} ";
cannam@167 164 $len = $len + length("$return_types{$ret} ");
cannam@167 165 $kind = "function"
cannam@167 166 }
cannam@167 167 print "$kind $name("; $len = $len + length("$kind $name(");
cannam@167 168 $len0 = $len;
cannam@167 169
cannam@167 170 $argnames = $args;
cannam@167 171 $argnames =~ s/([a-zA-Z_0-9 ]+[ \*]) *([a-zA-Z_0-9]+) */$2/g;
cannam@167 172 $comma = "";
cannam@167 173 foreach $argname (split(/ *, */, $argnames)) {
cannam@167 174 if ($len + length("$comma$argname") + 3 > 132) {
cannam@167 175 printf ", &\n%*s", $len0, "";
cannam@167 176 $len = $len0;
cannam@167 177 $comma = "";
cannam@167 178 }
cannam@167 179 print "$comma$argname";
cannam@167 180 $len = $len + length("$comma$argname");
cannam@167 181 $comma = ",";
cannam@167 182 }
cannam@167 183 print ") "; $len = $len + 2;
cannam@167 184
cannam@167 185 if ($len + length("bind(C, name='$cname')") > 132) {
cannam@167 186 printf "&\n%*s", $len0 - length("$name("), "";
cannam@167 187 }
cannam@167 188 print "bind(C, name='$cname')\n";
cannam@167 189
cannam@167 190 print " import\n";
cannam@167 191 foreach $arg (split(/ *, */, $args)) {
cannam@167 192 $arg =~ /^([a-zA-Z_0-9 ]+[ \*]) *([a-zA-Z_0-9]+) *$/;
cannam@167 193 $argtype = &canonicalize_type($1);
cannam@167 194 $argname = $2;
cannam@167 195 $ftype = $arg_types{$argtype};
cannam@167 196
cannam@167 197 # Various special cases for argument types:
cannam@167 198 if ($name =~ /_flops$/ && $argtype eq "double *") {
cannam@167 199 $ftype = "real(C_DOUBLE), intent(out)"
cannam@167 200 }
cannam@167 201 if ($name =~ /_execute/ && ($argname eq "ri" ||
cannam@167 202 $argname eq "ii" ||
cannam@167 203 $argname eq "in")) {
cannam@167 204 $ftype =~ s/intent\(out\)/intent(inout)/;
cannam@167 205 }
cannam@167 206
cannam@167 207 print " $ftype :: $argname\n"
cannam@167 208 }
cannam@167 209
cannam@167 210 print " end $kind $name\n";
cannam@167 211 print " \n";
cannam@167 212 }
cannam@167 213 }