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