annotate src/fftw-3.3.5/api/genf03.pl @ 73:02caadb7509e

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