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