OSDN Git Service

2010-04-26 Iain Sandoe <iain.sandoe@sandoe-acoustics.co.uk>
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / lib / objc-torture.exp
1 # Copyright (C) 1992-1998, 1999, 2000, 2007, 2008
2 # Free Software Foundation, Inc.
3
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with GCC; see the file COPYING3.  If not see
16 # <http://www.gnu.org/licenses/>.
17
18 # This file was written by Rob Savoye. (rob@cygnus.com)
19
20 load_lib file-format.exp
21 load_lib target-supports.exp
22
23 # Make sure that the runtime list is re-evaluated for each multilib.
24 proc objc-set-runtime-options { dowhat args } {
25     global OBJC_RUNTIME_OPTIONS
26     if ![info exists OBJC_RUNTIME_OPTIONS] {
27         set rtlist [list "-fgnu-runtime" "-fnext-runtime" ]
28     } else {
29         set rtlist [list "-fgnu-runtime" "-fnext-runtime" ]
30         foreach other $OBJC_RUNTIME_OPTIONS {
31             # Don't do tests twice...
32             if { ( $other == "-fnext-runtime"  || $other == "-fgnu-runtime" ) } {
33                 continue
34             }
35             lappend rtlist $other
36         }
37     }
38
39     set OBJC_RUNTIME_OPTIONS ""
40
41     foreach type $rtlist {
42         global srcdir subdir target_triplet tmpdir
43
44         set options "additional_flags=$type"
45         if [info exists args] {
46             lappend options $args
47         }
48         verbose "options $options"
49         if [info exists dowhat] {
50           switch $dowhat {
51             "compile" {
52               set compile_type "assembly"
53               set output_file "trivial.s"
54               set comp_output [objc_target_compile \
55                 "$srcdir/$subdir/trivial.m" "$output_file" "$compile_type" $options]
56
57               remote_file build delete $output_file
58               # If we get any error, then we failed.
59               if ![string match "" $comp_output] then {
60                 continue;
61               }
62             }
63             "execute" {
64               set test_obj "trivial.exe"
65               set comp_output [objc_target_compile \
66                 "$srcdir/$subdir/trivial.m" $test_obj "executable" $options]
67
68               # If we get any error, then we failed.
69               if ![string match "" $comp_output] then {
70                 remote_file build delete $test_obj
71                 continue;
72               }
73               set result [objc_load "$tmpdir/$test_obj" "" ""]
74               set status [lindex $result 0]
75               set output [lindex $result 1]
76               remote_file build delete $test_obj
77               if { $status != "pass" } {
78                 verbose -log "trivial execute failed with $status $output"
79                 continue;
80               }
81             }
82             default {
83               perror "$dowhat: not a valid objc-torture action"
84               return ""
85             }
86           }
87         } else {
88           set test_obj "trivial.exe"
89           set comp_output [objc_target_compile \
90           "$srcdir/$subdir/trivial.m" $test_obj executable $options]
91
92           # If we get any error, then we failed.
93           remote_file build delete $test_obj
94           if ![string match "" $comp_output] then {
95               continue;
96           }
97         }
98         lappend OBJC_RUNTIME_OPTIONS $type
99     }
100
101     verbose -log "Using the following runtimes: $OBJC_RUNTIME_OPTIONS"
102 }
103
104 # The default option list can be overridden by
105 # TORTURE_OPTIONS="{ { list1 } ... { listN } }"
106
107 if [info exists TORTURE_OPTIONS] {
108     set OBJC_TORTURE_OPTIONS $TORTURE_OPTIONS
109 } else {
110     # It is theoretically beneficial to group all of the O2/O3 options together,
111     # as in many cases the compiler will generate identical executables for
112     # all of them--and the objc-torture testsuite will skip testing identical
113     # executables multiple times.
114     # Also note that -finline-functions is explicitly included in one of the
115     # items below, even though -O3 is also specified, because some ports may
116     # choose to disable inlining functions by default, even when optimizing.
117     set OBJC_TORTURE_OPTIONS [list \
118         " -O0 " \
119         " -O1 " \
120         " -O2 " \
121         " -O3 -fomit-frame-pointer " \
122         " -O3 -fomit-frame-pointer -funroll-loops " \
123         " -O3 -fomit-frame-pointer -funroll-all-loops -finline-functions " \
124         " -O3 -g " \
125         " -Os " ]
126 }
127
128 if [info exists ADDITIONAL_TORTURE_OPTIONS] {
129     set OBJC_TORTURE_OPTIONS \
130         [concat $OBJC_TORTURE_OPTIONS $ADDITIONAL_TORTURE_OPTIONS]
131 }
132
133 #
134 # objc-torture-compile -- runs the Tege OBJC-torture test
135 #
136 # SRC is the full pathname of the testcase.
137 # OPTION is the specific compiler flag we're testing (eg: -O2).
138 #
139 proc objc-torture-compile { src option } {
140     global output
141     global srcdir tmpdir
142     global host_triplet
143
144     set output "$tmpdir/[file tail [file rootname $src]].o"
145
146     regsub "(?q)$srcdir/" $src "" testcase
147     # If we couldn't rip $srcdir out of `src' then just do the best we can.
148     # The point is to reduce the unnecessary noise in the logs.  Don't strip
149     # out too much because different testcases with the same name can confuse
150     # `test-tool'.
151     if [string match "/*" $testcase] {
152         set testcase "[file tail [file dirname $src]]/[file tail $src]"
153     }
154
155     verbose "Testing $testcase, $option" 1
156
157     # Run the compiler and analyze the results.
158     set options ""
159     lappend options "additional_flags=-w $option"
160
161     set comp_output [objc_target_compile "$src" "$output" object $options]
162     objc_check_compile $testcase $option $output $comp_output
163     remote_file build delete $output
164 }
165
166 #
167 # objc-torture-execute -- utility to compile and execute a testcase
168 #
169 # SRC is the full pathname of the testcase.
170 #
171 # If the testcase has an associated .x file, we source that to run the
172 # test instead.  We use .x so that we don't lengthen the existing filename
173 # to more than 14 chars.
174 #
175 proc objc-torture-execute { src args } {
176     global tmpdir tool srcdir output compiler_conditional_xfail_data
177
178     if { [llength $args] > 0 } {
179         set additional_flags [lindex $args 0]
180     } else {
181         set additional_flags ""
182     }
183     # Check for alternate driver.
184     if [file exists [file rootname $src].x] {
185         verbose "Using alternate driver [file rootname [file tail $src]].x" 2
186         set done_p 0
187         catch "set done_p \[source [file rootname $src].x\]"
188         if { $done_p } {
189             return
190         }
191     }
192
193     # Look for a loop within the source code - if we don't find one,
194     # don't pass -funroll[-all]-loops.
195     global torture_with_loops torture_without_loops
196     if [expr [search_for $src "for*("]+[search_for $src "while*("]] then {
197         set option_list $torture_with_loops
198     } else {
199         set option_list $torture_without_loops
200     }
201
202     set executable $tmpdir/[file tail [file rootname $src].x]
203
204     regsub "(?q)$srcdir/" $src "" testcase
205     # If we couldn't rip $srcdir out of `src' then just do the best we can.
206     # The point is to reduce the unnecessary noise in the logs.  Don't strip
207     # out too much because different testcases with the same name can confuse
208     # `test-tool'.
209     if [string match "/*" $testcase] {
210         set testcase "[file tail [file dirname $src]]/[file tail $src]"
211     }
212
213     set count 0
214     set oldstatus "foo"
215     foreach option $option_list {
216         if { $count > 0 } {
217             if [info exists oldexec] {
218                 remote_file build delete $oldexec
219             }
220             set oldexec $execname
221         }
222         set execname "${executable}${count}"
223         incr count
224
225         # torture_{compile,execute}_xfail are set by the .x script
226         # (if present)
227         if [info exists torture_compile_xfail] {
228             setup_xfail $torture_compile_xfail
229         }
230
231         # torture_execute_before_{compile,execute} can be set by the .x script
232         # (if present)
233         if [info exists torture_eval_before_compile] {
234           set ignore_me [eval $torture_eval_before_compile]
235         }
236
237         remote_file build delete $execname
238         verbose "Testing $testcase, $option" 1
239
240         set options ""
241         lappend options "additional_flags=-w $option"
242         if { $additional_flags != "" } {
243             lappend options "additional_flags=$additional_flags"
244         }
245         set comp_output [objc_target_compile "$src" "${execname}" executable $options]
246
247         if ![objc_check_compile "$testcase compilation" $option $execname $comp_output] {
248             unresolved "$testcase execution, $option"
249             remote_file build delete $execname
250             continue
251         }
252
253         # See if this source file uses "long long" types, if it does, and
254         # no_long_long is set, skip execution of the test.
255         if [target_info exists no_long_long] then {
256             if [expr [search_for $src "long long"]] then {
257                 unsupported "$testcase execution, $option"
258                 remote_file build delete $execname
259                 continue
260             }
261         }
262
263         if [info exists torture_execute_xfail] {
264             setup_xfail $torture_execute_xfail
265         }
266
267         if [info exists torture_eval_before_execute] {
268             set ignore_me [eval $torture_eval_before_execute]
269         }
270
271
272         # Sometimes we end up creating identical executables for two
273         # consecutive sets of different of compiler options.
274         #
275         # In such cases we know the result of this test will be identical
276         # to the result of the last test.
277         #
278         # So in cases where the time to load and run/simulate the test
279         # is relatively high, compare the two binaries and avoid rerunning
280         # tests if the executables are identical.
281         #
282         # Do not do this for native testing since the cost to load/execute
283         # the test is fairly small and the comparison step actually slows
284         # the entire process down because it usually does not "hit".
285         set skip 0
286         if { ![isnative] && [info exists oldexec] } {
287             if { [remote_file build cmp $oldexec $execname] == 0 } {
288                 set skip 1
289                 set status $oldstatus
290             }
291         }
292         if { $skip == 0 } {
293             set result [objc_load "$execname" "" ""]
294             set status [lindex $result 0]
295             set output [lindex $result 1]
296         }
297         $status "$testcase execution, $option"
298         set oldstatus $status
299     # for each option
300     } 
301     # tidy up
302     if [info exists execname] {
303         remote_file build delete $execname
304     }
305     if [info exists oldexec] {
306         remote_file build delete $oldexec
307     }
308 }
309
310 #
311 # search_for -- looks for a string match in a file
312 #
313 proc search_for { file pattern } {
314     set fd [open $file r]
315     while { [gets $fd cur_line]>=0 } {
316         if [string match "*$pattern*" $cur_line] then {
317             close $fd
318             return 1
319         }
320     }
321     close $fd
322     return 0
323 }
324
325 #
326 # objc-torture -- the objc-torture testcase source file processor
327 #
328 # This runs compilation only tests (no execute tests).
329 # SRC is the full pathname of the testcase, or just a file name in which case
330 # we prepend $srcdir/$subdir.
331 #
332 # If the testcase has an associated .x file, we source that to run the
333 # test instead.  We use .x so that we don't lengthen the existing filename
334 # to more than 14 chars.
335 #
336 proc objc-torture { args } {
337     global srcdir subdir compiler_conditional_xfail_data
338
339     set src [lindex $args 0]
340     if { [llength $args] > 1 } {
341         set options [lindex $args 1]
342     } else {
343         set options ""
344     }
345
346     # Prepend $srdir/$subdir if missing.
347     if ![string match "*/*" $src] {
348         set src "$srcdir/$subdir/$src"
349     }
350
351     # Check for alternate driver.
352     if [file exists [file rootname $src].x] {
353         verbose "Using alternate driver [file rootname [file tail $src]].x" 2
354         set done_p 0
355         catch "set done_p \[source [file rootname $src].x\]"
356         if { $done_p } {
357             return
358         }
359     }
360
361     # Look for a loop within the source code - if we don't find one,
362     # don't pass -funroll[-all]-loops.
363     global torture_with_loops torture_without_loops
364     if [expr [search_for $src "for*("]+[search_for $src "while*("]] then {
365             set option_list $torture_with_loops
366     } else {
367             set option_list $torture_without_loops
368     }
369
370     # loop through all the options
371     foreach option $option_list {
372         # torture_compile_xfail is set by the .x script (if present)
373         if [info exists torture_compile_xfail] {
374             setup_xfail $torture_compile_xfail
375         }
376
377         # torture_execute_before_compile is set by the .x script (if present)
378         if [info exists torture_eval_before_compile] {
379             set ignore_me [eval $torture_eval_before_compile]
380         }
381
382         objc-torture-compile $src "$option $options"
383     }
384 }