-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgenf90.pl
executable file
·397 lines (362 loc) · 10.3 KB
/
genf90.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#!/usr/bin/env perl
use strict;
my $outfile;
# Beginning with F90, Fortran has strict typing of variables based on "TKR"
# (type, kind, and rank). In many cases we want to write subroutines that
# provide the same functionality for different variable types and ranks. In
# order to do this without cut-and-paste duplication of code, we create a
# template file with the extension ".F90.in", which can be parsed by this script
# to generate F90 code for all of the desired specific types.
#
# Keywords are delimited by curly brackets: {}
#
# {TYPE} and {DIMS} are used to generate the specific subroutine names from the
# generic template
# {TYPE} : Variable type name; implemented types are character, 4 or 8 byte real,
# and 4 or 8 byte integer.
# allowed values: text, real, double, int, long, logical
# default values: text, real, double, int
# {VTYPE} : Used to generate variable declarations to match the specific type.
# if {TYPE}=double then {VTYPE} is "real(r8)"
# {ITYPE}, {ITYPENAME} : Used to generate CPP statements for the specific type.
# {MPITYPE} : Used to generate MPI types corresponding to the specific type.
#
# {DIMS} : Rank of arrays, "0" for scalar.
# allowed values: 0-7
# default values : 0-5
# {DIMSTR} : Generates the parenthesis and colons used for a variable
# declaration of {DIMS} dimensions.
# if {DIMS}=3 then {DIMSTR} is (:,:,:)
# {REPEAT} : Repeats an expression for each number from 1 to {DIMS}, with each
# iteration separated by commas.
# {REPEAT: foo(#, bar)}
# expands to this:
# foo(1, bar), foo(2, bar), foo(3, bar), ...
# defaults
my @types = qw(real double int short int64 text);
my $vtype = {'text' => 'character(len=*)',
'real' => 'real(r4)',
'double' => 'real(r8)',
'int' => 'integer(i4)',
'short' => 'integer(i2)',
'long' => 'integer(i8)',
'int64' => 'integer(i8)',
'logical' => 'logical' };
my $itype = {'text' => 100,
'real' => 101,
'double' => 102,
'int' => 103,
'long' => 104,
'logical' => 105,
'short' => 106,
'int64' => 107};
my $itypename = {'text' => 'TYPETEXT',
'real' => 'TYPEREAL',
'double' => 'TYPEDOUBLE',
'int' => 'TYPEINT',
'short' => 'TYPESHORT',
'long' => 'TYPELONG',
'logical' => 'TYPELOGICAL'};
my $mpitype = {'text' => 'MPI_CHARACTER',
'real' => 'MPI_REAL4',
'short' => 'MPI_SHORT',
'double' => 'MPI_REAL8',
'int' => 'MPI_INTEGER',
'int64' => 'MPI_INTEGER64'};
# Netcdf C datatypes
my $nctype = {'text' => 'text',
'real' => 'float',
'short' => 'short',
'double' => 'double',
'int' => 'int',
'int64' => 'longlong'};
# C interoperability types
my $ctype = {'text' => 'character(C_CHAR)',
'real' => 'real(C_FLOAT)',
'double' => 'real(C_DOUBLE)',
'int' => 'integer(C_INT)',
'short' => 'integer(C_SHORT)',
'int64' => 'integer(C_LONG_LONG)'};
my @dims =(0..5);
my $write_dtypes = "no";
# begin
foreach(@ARGV){
my $infile = $_;
usage() unless($infile =~ /(.*.F90).in/);
$outfile = $1;
open(F,"$infile") || die "$0 Could not open $infile to read";
my @parsetext;
my $cnt=0;
foreach(<F>){
$cnt++;
if(/^\s*contains/i){
push(@parsetext,"# $cnt \"$infile\"\n");
}
if(/^\s*interface/i){
push(@parsetext,"# $cnt \"$infile\"\n");
}
if(/^[^!]*subroutine/i){
push(@parsetext,"# $cnt \"$infile\"\n");
}
if(/^[^!]*function/i){
push(@parsetext,"# $cnt \"$infile\"\n");
}
push(@parsetext,$_);
}
close(F);
my $end;
my $contains=0;
my $in_type_block=0;
my @unit;
my $unitcnt=0;
my $date = localtime();
my $preamble =
"!===================================================
! DO NOT EDIT THIS FILE, it was generated using $0
! Any changes you make to this file may be lost
!===================================================\n";
my @output ;
push(@output,$preamble);
my $line;
my $dimmodifier;
my $typemodifier;
my $itypeflag;
my $block;
my $block_type;
my $cppunit;
foreach $line (@parsetext){
# skip parser comments
next if($line =~ /\s*!pl/);
$itypeflag=1 if($line =~ /{ITYPE}/);
$itypeflag=1 if($line =~ /TYPETEXT/);
$itypeflag=1 if($line =~ /TYPEREAL/);
$itypeflag=1 if($line =~ /TYPEDOUBLE/);
$itypeflag=1 if($line =~ /TYPEINT/);
$itypeflag=1 if($line =~ /TYPELONG/);
if($contains==0){
if($line=~/\s*!\s*DIMS\s+[\d,]+!*/){
$dimmodifier=$line;
next;
}
if($line=~/\s*!\s*TYPE\s+[^!]+!*$/){
$typemodifier=$line;
next;
}
if ((defined $typemodifier or defined $dimmodifier)
and not defined $block and $line=~/^\s*#[^{]*$/) {
push(@output, $line);
next;
}
# Figure out the bounds of a type statement.
# Type blocks start with "type," "type foo" or "type::" but not
# "type(".
$in_type_block=1 if($line=~/^\s*type\s*[,:[:alpha:]]/i);
$in_type_block=0 if($line=~/^\s*end\s*type/i);
if(not defined $block) {
if ($line=~/^\s*type[^[:alnum:]_].*(\{TYPE\}|\{DIMS\})/i or
$line=~/^[^!]*(function|subroutine).*(\{TYPE\}|\{DIMS\})/i) {
$block=$line;
next;
}
if ($line=~/^\s*interface.*(\{TYPE\}|\{DIMS\})/i) {
$block_type="interface";
$block=$line;
next;
}
}
if(not defined $block_type and
($line=~/^\s*end\s+type\s+.*(\{TYPE\}|\{DIMS\})/i or
$line=~/^\s*end\s+(function|subroutine)\s+.*(\{TYPE\}|\{DIMS\})/i)){
$line = $block.$line;
undef $block;
}
if ($line=~/^\s*end\s*interface/i and
defined $block) {
$line = $block.$line;
undef $block;
undef $block_type;
}
if(defined $block){
$block = $block.$line;
next;
}
if(defined $dimmodifier){
$line = $dimmodifier.$line;
undef $dimmodifier;
}
if(defined $typemodifier){
$line = $typemodifier.$line;
undef $typemodifier;
}
push(@output, buildout($line));
if(($line =~ /^\s*contains\s*!*/i && ! $in_type_block) or
($line =~ /^\s*!\s*Not a module/i)){
$contains=1;
next;
}
}
if($line=~/^\s*end module\s*/){
$end = $line;
last;
}
if($contains==1){
# first parse into functions or subroutines
if($cppunit || !(defined($unit[$unitcnt]))){
# Make cpp lines and blanks between routines units.
if($line =~ /^\s*\#(?!\s[[:digit:]]+)/ || $line =~/^\s*$/ || $line=~/^\s*!(?!\s*(TYPE|DIMS))/){
push(@{$unit[$unitcnt]},$line);
$cppunit=1;
next;
} else {
$cppunit=0;
$unitcnt++;
}
}
push(@{$unit[$unitcnt]},$line);
if ($line=~/^\s*interface/i) {
$block_type="interface";
$block=$line;
}
if ($line=~/^\s*end\s*interface/i) {
undef $block_type;
undef $block;
}
unless(defined $block){
if($line =~ /\s*end function/i or $line =~ /\s*end subroutine/i){
$unitcnt++;
}
}
}
}
my $i;
for($i=0;$i<$unitcnt;$i++){
if(defined($unit[$i])){
my $func = join('',@{$unit[$i]});
push(@output, buildout($func));
}
}
push(@output,@{$unit[$#unit]}) if($unitcnt==$#unit);
push(@output, $end);
if($itypeflag==1){
my $str;
$str.="#include \"dtypes.h\"\n";
$write_dtypes = "yes";
print $str;
}
print @output;
writedtypes() if(!(-e "dtypes.h") && $write_dtypes == "yes");
}
sub usage{
die("$0 Expected input filename of the form .*.F90.in");
}
sub build_repeatstr{
my($dims) = @_;
# Create regex to repeat expression DIMS times.
my $repeatstr;
for(my $i=1;$i<=$dims;$i++){
$repeatstr .="\$\{1\}$i\$\{2\},&\n";
}
if(defined $repeatstr){
$repeatstr="\"$repeatstr";
chop $repeatstr;
chop $repeatstr;
chop $repeatstr;
$repeatstr.="\"";
}else{
$repeatstr='';
}
}
sub writedtypes{
open(F,">dtypes.h");
print F
"#define TYPETEXT 100
#define TYPEREAL 101
#define TYPEDOUBLE 102
#define TYPEINT 103
#define TYPELONG 104
#define TYPELOGICAL 105
";
close(F);
}
sub buildout{
my ($func) = @_;
my $outstr;
my(@ldims, @ltypes);
if($func=~/\s*!\s*DIMS\s+([\d,]+)\s*/){
@ldims = split(/,/,$1);
}else{
@ldims = @dims;
}
if($func=~/\s*!\s*TYPE\s+([^!\s]+)\s*/){
@ltypes = split(/,/,$1);
# print ">$func<>@ltypes<\n";
}else{
@ltypes = @types;
}
if(($func =~ /{TYPE}/ && $func =~ /{DIMS}/) ){
my ($type, $dims);
foreach $type (@ltypes){
foreach $dims (@ldims){
my $dimstr;
for(my $i=1;$i<=$dims;$i++){
$dimstr .=':,';
}
if(defined $dimstr){
$dimstr="($dimstr";
chop $dimstr;
$dimstr.=')';
}else{
$dimstr='';
}
my $repeatstr = build_repeatstr($dims);
my $str = $func;
$str =~ s/{TYPE}/$type/g;
$str =~ s/{VTYPE}/$vtype->{$type}/g;
$str =~ s/{ITYPE}/$itype->{$type}/g;
$str =~ s/{MPITYPE}/$mpitype->{$type}/g;
$str =~ s/{NCTYPE}/$nctype->{$type}/g;
$str =~ s/{CTYPE}/$ctype->{$type}/g;
$str =~ s/{DIMS}/$dims/g;
$str =~ s/{DIMSTR}/$dimstr/g;
$str =~ s/{REPEAT:([^#}]*)#([^#}]*)}/$repeatstr/eeg;
$outstr .= $str;
}
}
}elsif($func =~ /{DIMS}/){
my $dims;
foreach $dims (@ldims){
my $dimstr;
for(my $i=1;$i<=$dims;$i++){
$dimstr .=':,';
}
if(defined $dimstr){
$dimstr="($dimstr";
chop $dimstr;
$dimstr.=')';
}else{
$dimstr='';
}
my $repeatstr = build_repeatstr($dims);
my $str = $func;
$str =~ s/{DIMS}/$dims/g;
$str =~ s/{DIMSTR}/$dimstr/g;
$str =~ s/{REPEAT:([^#}]*)#([^#}]*)}/$repeatstr/eeg;
$outstr .= $str;
}
}elsif($func =~ /{TYPE}/){
my ($type);
foreach $type (@ltypes){
my $str = $func;
$str =~ s/{TYPE}/$type/g;
$str =~ s/{VTYPE}/$vtype->{$type}/g;
$str =~ s/{ITYPE}/$itype->{$type}/g;
$str =~ s/{MPITYPE}/$mpitype->{$type}/g;
$str =~ s/{NCTYPE}/$nctype->{$type}/g;
$str =~ s/{CTYPE}/$ctype->{$type}/g;
$outstr.=$str;
}
}else{
$outstr=$func;
}
return $outstr;
}