1# Copyright (C) 1992-1998, 1999, 2000 Free Software Foundation, Inc.
2
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; either version 2 of the License, or
6# (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program; if not, write to the Free Software
15# Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16
17# Please email any bugs, comments, and/or additions to this file to:
18# bug-dejagnu@gnu.org.
19
20# This file was written by Rob Savoye. (rob@cygnus.com)
21
22load_lib file-format.exp
23
24# The default option list can be overridden by
25# TORTURE_OPTIONS="{ { list1 } ... { listN } }"
26
27if ![info exists TORTURE_OPTIONS] {
28    # It is theoretically beneficial to group all of the O2/O3 options together,
29    # as in many cases the compiler will generate identical executables for
30    # all of them--and the c-torture testsuite will skip testing identical
31    # executables multiple times.
32    # Also note that -finline-functions is explicitly included in one of the
33    # items below, even though -O3 is also specified, because some ports may
34    # choose to disable inlining functions by default, even when optimizing.
35    set TORTURE_OPTIONS [list \
36	{ -O0 } \
37	{ -O1 } \
38	{ -O2 } \
39	{ -O3 -fomit-frame-pointer } \
40	{ -O3 -fomit-frame-pointer -funroll-loops } \
41	{ -O3 -fomit-frame-pointer -funroll-all-loops -finline-functions } \
42	{ -O3 -g } \
43	{ -Os } ]
44}
45
46
47# Split TORTURE_OPTIONS into two choices: one for testcases with loops and
48# one for testcases without loops.
49
50set torture_with_loops $TORTURE_OPTIONS
51set torture_without_loops ""
52foreach option $TORTURE_OPTIONS {
53    if ![string match "*loop*" $option] {
54	lappend torture_without_loops $option
55    }
56}
57
58#
59# c-torture-compile -- runs the Tege C-torture test
60#
61# SRC is the full pathname of the testcase.
62# OPTION is the specific compiler flag we're testing (eg: -O2).
63#
64proc c-torture-compile { src option } {
65    global output
66    global srcdir tmpdir
67    global host_triplet
68
69    set output "$tmpdir/[file tail [file rootname $src]].o"
70
71    regsub "^$srcdir/?" $src "" testcase
72    # If we couldn't rip $srcdir out of `src' then just do the best we can.
73    # The point is to reduce the unnecessary noise in the logs.  Don't strip
74    # out too much because different testcases with the same name can confuse
75    # `test-tool'.
76    if [string match "/*" $testcase] {
77	set testcase "[file tail [file dirname $src]]/[file tail $src]"
78    }
79
80    verbose "Testing $testcase, $option" 1
81
82    # Run the compiler and analyze the results.
83    set options ""
84    lappend options "additional_flags=-w $option"
85
86    set comp_output [gcc_target_compile "$src" "$output" object $options];
87    gcc_check_compile $testcase $option $output $comp_output
88    remote_file build delete $output
89}
90
91#
92# c-torture-execute -- utility to compile and execute a testcase
93#
94# SOURCES is a list of full pathnames to the test source files.
95# The first filename in this list forms the "testcase".
96#
97# If the testcase has an associated .x file, we source that to run the
98# test instead.  We use .x so that we don't lengthen the existing filename
99# to more than 14 chars.
100#
101proc c-torture-execute { sources args } {
102    global tmpdir tool srcdir output compiler_conditional_xfail_data
103
104    # Use the first source filename given as the filename under test.
105    set src [lindex $sources 0]
106
107    if { [llength $args] > 0 } {
108	set additional_flags [lindex $args 0];
109    } else {
110	set additional_flags "";
111    }
112    # Check for alternate driver.
113    if [file exists [file rootname $src].x] {
114	verbose "Using alternate driver [file rootname [file tail $src]].x" 2
115	set done_p 0
116	catch "set done_p \[source [file rootname $src].x\]"
117	if { $done_p } {
118	    return
119	}
120    }
121
122    # Look for a loop within the source code - if we don't find one,
123    # don't pass -funroll[-all]-loops.
124    global torture_with_loops torture_without_loops
125    if [expr [search_for $src "for*("]+[search_for $src "while*("]] then {
126	set option_list $torture_with_loops
127    } else {
128	set option_list $torture_without_loops
129    }
130
131    set executable $tmpdir/[file tail [file rootname $src].x]
132
133    regsub "^$srcdir/?" $src "" testcase
134    # If we couldn't rip $srcdir out of `src' then just do the best we can.
135    # The point is to reduce the unnecessary noise in the logs.  Don't strip
136    # out too much because different testcases with the same name can confuse
137    # `test-tool'.
138    if [string match "/*" $testcase] {
139	set testcase "[file tail [file dirname $src]]/[file tail $src]"
140    }
141
142    set count 0;
143    set oldstatus "foo";
144    foreach option $option_list {
145	if { $count > 0 } {
146	    set oldexec $execname;
147	}
148	set execname "${executable}${count}";
149	incr count;
150
151	# torture_{compile,execute}_xfail are set by the .x script
152	# (if present)
153	if [info exists torture_compile_xfail] {
154	    setup_xfail $torture_compile_xfail
155	}
156
157	# torture_execute_before_{compile,execute} can be set by the .x script
158	# (if present)
159	if [info exists torture_eval_before_compile] {
160            set ignore_me [eval $torture_eval_before_compile]
161	}
162
163	remote_file build delete $execname;
164	verbose "Testing $testcase, $option" 1
165
166	set options ""
167	lappend options "additional_flags=-w $option"
168	if { $additional_flags != "" } {
169	    lappend options "additional_flags=$additional_flags";
170	}
171	set comp_output [gcc_target_compile "$sources" "${execname}" executable $options];
172
173	if ![gcc_check_compile "$testcase compilation" $option $execname $comp_output] {
174	    unresolved "$testcase execution, $option"
175	    remote_file build delete $execname
176	    continue
177	}
178
179	# See if this source file uses "long long" types, if it does, and
180	# no_long_long is set, skip execution of the test.
181	if [target_info exists no_long_long] then {
182	    if [expr [search_for $src "long long"]] then {
183		unsupported "$testcase execution, $option"
184		continue
185	    }
186	}
187
188	if [info exists torture_execute_xfail] {
189	    setup_xfail $torture_execute_xfail
190	}
191
192	if [info exists torture_eval_before_execute] {
193            set ignore_me [eval $torture_eval_before_execute]
194	}
195
196
197	# Sometimes we end up creating identical executables for two
198	# consecutive sets of different of compiler options.
199	#
200	# In such cases we know the result of this test will be identical
201	# to the result of the last test.
202	#
203	# So in cases where the time to load and run/simulate the test
204	# is relatively high, compare the two binaries and avoid rerunning
205	# tests if the executables are identical.
206	#
207	# Do not do this for native testing since the cost to load/execute
208	# the test is fairly small and the comparison step actually slows
209	# the entire process down because it usually does not "hit".
210	set skip 0;
211	if { ![isnative] && [info exists oldexec] } {
212	    if { [remote_file build cmp $oldexec $execname] == 0 } {
213		set skip 1;
214	    }
215	}
216	if { $skip == 0 } {
217	    set result [gcc_load "$execname" "" ""]
218	    set status [lindex $result 0];
219	    set output [lindex $result 1];
220	}
221        if { $oldstatus == "pass" } {
222	    remote_file build delete $oldexec;
223        }
224	$status "$testcase execution, $option"
225	set oldstatus $status;
226    }
227    if [info exists status] {
228	if { $status == "pass" } {
229	    remote_file build delete $execname;
230	}
231    }
232}
233
234#
235# search_for -- looks for a string match in a file
236#
237proc search_for { file pattern } {
238    set fd [open $file r]
239    while { [gets $fd cur_line]>=0 } {
240	if [string match "*$pattern*" $cur_line] then {
241	    close $fd
242	    return 1
243	}
244    }
245    close $fd
246    return 0
247}
248
249#
250# c-torture -- the c-torture testcase source file processor
251#
252# This runs compilation only tests (no execute tests).
253# SRC is the full pathname of the testcase, or just a file name in which case
254# we prepend $srcdir/$subdir.
255#
256# If the testcase has an associated .x file, we source that to run the
257# test instead.  We use .x so that we don't lengthen the existing filename
258# to more than 14 chars.
259#
260proc c-torture { args } {
261    global srcdir subdir compiler_conditional_xfail_data
262
263    set src [lindex $args 0];
264    if { [llength $args] > 1 } {
265	set options [lindex $args 1];
266    } else {
267	set options ""
268    }
269
270    # Prepend $srdir/$subdir if missing.
271    if ![string match "*/*" $src] {
272	set src "$srcdir/$subdir/$src"
273    }
274
275    # Check for alternate driver.
276    if [file exists [file rootname $src].x] {
277	verbose "Using alternate driver [file rootname [file tail $src]].x" 2
278	set done_p 0
279	catch "set done_p \[source [file rootname $src].x\]"
280	if { $done_p } {
281	    return
282	}
283    }
284
285    # Look for a loop within the source code - if we don't find one,
286    # don't pass -funroll[-all]-loops.
287    global torture_with_loops torture_without_loops
288    if [expr [search_for $src "for*("]+[search_for $src "while*("]] then {
289	set option_list $torture_with_loops
290    } else {
291	set option_list $torture_without_loops
292    }
293
294    # loop through all the options
295    foreach option $option_list {
296	# torture_compile_xfail is set by the .x script (if present)
297	if [info exists torture_compile_xfail] {
298	    setup_xfail $torture_compile_xfail
299	}
300
301	# torture_execute_before_compile is set by the .x script (if present)
302	if [info exists torture_eval_before_compile] {
303            set ignore_me [eval $torture_eval_before_compile]
304	}
305
306	c-torture-compile $src "$option $options"
307    }
308}
309