blob: 38cedb07f088c564064ef21871e9cae8a28145f8 [file] [log] [blame]
Jim Cownie18d84732014-05-10 17:02:09 +00001#!/usr/bin/perl -w
2
3# functions.pm
4# This package contains a set of subroutines to modify the templates for the openMP Testuite.
5
6
7################################################################################
8# subroutines to extract, modify or delete tags from the template
9################################################################################
10
11# LIST get_tag_values( $tagname, $string )
12# subrutine to get the text encloded by a tag.
13# Returns a list containing the inner texts of the found tags
14sub get_tag_values
15{
16 my ( $tagname, $string );
17 ( $tagname, $string ) = @_;
18 my (@tmp,@tmp2);
19 @tmp = split(/\<$tagname\>/,$string);
20 foreach $_(@tmp){
21 push(@tmp2,split(/\<\/$tagname\>/));
22 }
23 my(@result,$i);
24 $i=1; # couter to get only every second item
25 foreach $_(@tmp2){
26 if($i%2 eq 0){
27 push(@result,$_);
28 }
29 $i++;
30 }
31 return @result;
32}
33
34# LIST replace_tags( $tagname, $replacestring, @list )
35# subrutine to replace tags by a replacestring.
36# Returns a list of the srings after conversion.
37sub replace_tags
38{
39 my ($tagname, $replacestring, @stringlist, @result);
40 ($tagname, $replacestring, @stringlist) = @_;
41 foreach $_(@stringlist) {
42 s#\<$tagname\>(.*?)\<\/$tagname\>#$replacestring#gs;
43 push(@result,$_);
44 }
45 return @result;
46}
47
48# LIST enlarge_tags( $tagname, $before, $after, @list )
49# subrutine to replace tags by the tags added by a string before and after.
50# Returns a list of the srings after conversion.
51sub enlarge_tags
52{
53 my ($tagname, $before, $after, @stringlist,@result);
54 ($tagname, $before, $after, @stringlist) = @_;
55 foreach $_(@stringlist) {
56 s#\<$tagname\>(.*?)\<\/$tagname\>#$before$1$after#gs;
57 push(@result,$_);
58 }
59 return @result;
60}
61
62# LIST delete_tags( $tagname, @list )
63# subrutine to delete tags in a string.
64# Returns a list of the cleared strings
65sub delete_tags
66{
67 my($tagname,@stringlist);
68 ($tagname, @stringlist) = @_;
69 my(@result);
70 foreach $_(@stringlist) {
71 s#\<$tagname\>(.*?)\<\/$tagname\>##gs;
72 push(@result,$_);
73 }
74 return @result;
75}
76
77
78
79################################################################################
80# subroutines for generating "orpahned" tests
81################################################################################
82
83# SCALAR create_orph_cfunctions( $prefix, $code )
84# returns a string containing the definitions of the functions for the
85# orphan regions.
86sub create_orph_cfunctions
87{
88 my ($code,@defs);
89 ($code) = @_;
90 @defs = get_tag_values('ompts:orphan',$code);
91 ($functionname) = get_tag_values('ompts:testcode:functionname',$code);
92 my ( @result,$functionsrc, $i);
93 $functionsrc = "\n/* Automatically generated definitions of the orphan functions */\n";
94
95 $i = 1;
96 foreach (@defs) {
97 $functionsrc .= "\nvoid orph$i\_$functionname (FILE * logFile) {";
98 $functionsrc .= $_;
99 $functionsrc .= "\n}\n";
100 $i++;
101 }
102 $functionsrc .= "/* End of automatically generated definitions */\n";
103 return $functionsrc;
104}
105
106# SCALAR create_orph_fortranfunctions( $prefix, $code )
107# returns a string containing the definitions of the functions for the
108# orphan regions.
109sub create_orph_fortranfunctions
110{
111 my ($prefix,$code,@defs,$orphan_parms);
112 ($prefix,$code,$orphan_parms) = @_;
113 @defs = get_tag_values('ompts:orphan',$code);
114
115 #to remove space and put a single space
116 if($orphan_parms ne "")
117 {
118 $orphan_parms =~ s/[ \t]+//sg;
119 $orphan_parms =~ s/[ \t]+\n/\n/sg;
120 }
121
122 ($orphanvarsdefs) = get_tag_values('ompts:orphan:vars',$code);
123 foreach (@varsdef) {
124 if (not /[^ \n$]*/){ $orphanvarsdefs = join("\n",$orphanvarsdef,$_);}
125 }
126 ($functionname) = get_tag_values('ompts:testcode:functionname',$code);
127 my ( @result,$functionsrc, $i);
128 $functionsrc = "\n! Definitions of the orphan functions\n";
129 $i = 1;
130 foreach $_(@defs)
131 {
132 $functionsrc .= "\n SUBROUTINE orph$i\_$prefix\_$functionname\($orphan_parms\)\n ";
133 $functionsrc .= "INCLUDE \"omp_testsuite.f\"\n";
134 $functionsrc .= $orphanvarsdefs."\n";
135 $functionsrc .= $_;
136 $functionsrc .= "\n";
137 $functionsrc .= " END SUBROUTINE\n! End of definition\n\n";
138 $i++;
139 }
140 return $functionsrc;
141}
142
143# LIST orphan_regions2cfunctions( $prefix, @code )
144# replaces orphan regions by functioncalls in C/C++.
145sub orphan_regions2cfunctions
146{
147 my ($code, $i, $functionname);
148 ($code) = @_;
149 $i = 1;
150 ($functionname) = get_tag_values('ompts:testcode:functionname',$code);
151 while( /\<ompts\:orphan\>(.*)\<\/ompts\:orphan\>/s) {
152 s#\<ompts\:orphan\>(.*?)\<\/ompts\:orphan\>#orph$i\_$functionname (logFile);#s;
153 $i++;
154 }
155 return $code;
156}
157
158# LIST orphan_regions2fortranfunctions( $prefix, @code )
159# replaces orphan regions by functioncalls in fortran
160sub orphan_regions2fortranfunctions
161{
162 my ( $prefix, @code, $my_parms, $i, $functionname);
163 ($prefix, ($code), $my_parms) = @_;
164 $i = 1;
165 ($functionname) = get_tag_values('ompts:testcode:functionname',$code);
166 foreach $_(($code))
167 {
168 while( /\<ompts\:orphan\>(.*)\<\/ompts\:orphan\>/s)
169 {
170 s#\<ompts\:orphan\>(.*?)\<\/ompts\:orphan\># CALL orph$i\_$prefix\_$functionname\($my_parms\);#s;
171 $i++;
172 }
173 }
174 return ($code);
175}
176
177# SCALAR orph_functions_declarations( $prefix, $code )
178# returns a sring including the declaration of the functions used
179# in the orphan regions. The function names are generated using
180# the $prefix as prefix for the functionname.
181sub orph_functions_declarations
182{
183 my ($prefix, $code);
184 ($prefix, $code) = @_;
185 my ( @defs, $result );
186
187 # creating declarations for later used functions
188 $result .= "\n\n/* Declaration of the functions containing the code for the orphan regions */\n#include <stdio.h>\n";
189 @defs = get_tag_values('ompts:orphan',$code);
190 my ($functionname,$i);
191 ($functionname) = get_tag_values('ompts:testcode:functionname',$code);
192 $i = 1;
193 foreach $_(@defs) {
194 $result .= "\nvoid orph$i\_$prefix\_$functionname ( FILE * logFile );";
195 $i++;
196 }
197 $result .= "\n\n/* End of declaration */\n\n";
198 return $result;
199}
200
201# SCALAR make_global_vars_definition( $code )
202# returns a sring including the declaration for the vars needed to
203# be declared global for the orphan region.
204sub make_global_vars_def
205{
206 my ( $code );
207 ($code) = @_;
208 my ( @defs, $result, @tmp, @tmp2 ,$predefinitions);
209
210 # creating global declarations for the variables.
211 $result = "\n\n/* Declaration of the variables used in the orphan region. */\n";
212
213 # get all tags containing the variable definitions
214 @defs = get_tag_values('ompts:orphan:vars',$code);
215 foreach $_(@defs)
216 {
217 # cutting the different declarations in the same tag by the ';' as cuttmark
218 @tmp = split(/;/,$_);
219 foreach $_(@tmp)
220 {
221 # replacing newlines and double spaces
222 s/\n//gs;
223 s/ //gs;
224 # put the new declaration at the end of $result
225 if($_ ne ""){ $result .= "\n $_;"; }
226 }
227 }
228 $result .= "\n\n/* End of declaration. */\n\n";
229 return $result;
230}
231
232# SCALAR extern_vars_definition( $code )
233# returns a sring including the declaration for the vars needed to
234# be declared extern for the orphan region.
235sub extern_vars_def
236{
237 my ( $code );
238 ($code) = @_;
239 my ( @defs, $result, @tmp, @tmp2 ,$predefinitions);
240
241 # creating declarations for the extern variables.
242 $result = "\n\n/* Declaration of the extern variables used in the orphan region. */\n";
243 # $result .= "\n#include <stdio.h>\n#include <omp.h>\n";
244 $result .= "\nextern FILE * logFile;";
245
246 # get all tags containing the variable definitions
247 @defs = get_tag_values('ompts:orphan:vars',$code);
248 foreach $_(@defs)
249 {
250 # cutting the different declarations in the same tag by the ';' as cuttmark
251 @tmp = split(/;/,$_);
252 foreach $_(@tmp)
253 {
254 # replacing newlines and double spaces
255 s/\n//gs;
256 s/ //gs;
257 # cutting off definitions
258 @tmp2 = split("=",$_);
259 # put the new declaration at the end of $result
260 $result .= "\nextern $tmp2[0];";
261 }
262 }
263 $result .= "\n\n/* End of declaration. */\n\n";
264 return $result;
265}
266
267sub leave_single_space
268{
269 my($str);
270 ($str)=@_;
271 if($str ne "")
272 {
273 $str =~ s/^[ \t]+/ /;
274 $str =~ s/[ \t]+\n$/\n/;
275 $str =~ s/[ \t]+//g;
276 }
277 return $str;
278}
279
280return 1;