OSDN Git Service

9e9084d1b687c4960dc7c29acc0b09610d577244
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / lib / lto.exp
1 # Copyright (C) 2009 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 # Contributed by Diego Novillo <dnovillo@google.com>
18
19
20 # lto_init -- called at the start of each subdir of tests
21
22 proc lto_init { args } {
23     global LTO_OPTIONS
24
25     if {[info exists args] && $args == "no-mathlib"} {
26         global board_info
27         global saved_mathlib
28
29         set dest [target_info name]
30         if [board_info $dest exists mathlib] {
31             set saved_mathlib [board_info $dest mathlib]
32         }
33         set board_info($dest,mathlib) " "
34     }
35
36     # Each test is run with the compiler options from this list.
37     # The default option lists can be overridden by LTO_OPTIONS="[list
38     # {opts_1} {opts_2}... {opts_n}]" where opts_i are lists of options.
39     # You can put this in the environment before site.exp is written or
40     # add it to site.exp directly.
41     if ![info exists LTO_OPTIONS] {
42         set LTO_OPTIONS [list   \
43             {-O0 -fwhopr}       \
44             {-O2 -fwhopr}       \
45             {-O0 -flto}         \
46             {-O2 -flto}         \
47         ]
48     }
49 }
50
51 #
52 # lto_finish -- called at the end of each subdir of tests if mathlib is
53 #               changed.
54 #
55
56 proc lto_finish { } {
57     global board_info
58     global saved_mathlib
59
60     set dest [target_info name]
61     if [info exists saved_mathlib] {
62         set board_info($dest,mathlib) $saved_mathlib
63     } elseif [board_info $dest exists mathlib] {
64         unset board_info($dest,mathlib)
65     }
66 }
67
68 # Subsets of tests can be selectively disabled by members of this list:
69 #  - ATTRIBUTE: disable all tests using the __attribute__ extension,
70 #  - COMPLEX: disable all tests using the complex types feature,
71 #  - COMPLEX_INT: disable all tests using the complex integral types extension,
72 #  - VA: disable all tests using the variable number of arguments feature,
73 #  - VLA_IN_STRUCT: disable all tests using the variable-length arrays as
74 #    structure members extension,
75 #  - ZERO_ARRAY: disable all tests using the zero-sized arrays extension.
76 # The default skip lists can be overriden by
77 # LTO_SKIPS="[list {skip_1}...{skip_n}]"
78 # where skip_i are skip identifiers.  You can put this in the environment
79 # before site.exp is written or add it to site.exp directly.
80 if ![info exists LTO_SKIPS] {
81     set LTO_SKIPS [list {}]
82 }
83
84 global lto_skip_list
85 set lto_skip_list $LTO_SKIPS
86
87 load_lib dg.exp
88 load_lib gcc-dg.exp
89
90 # lto-obj -- compile to an object file
91 #
92 # SOURCE is the source file
93 # DEST is the object file
94 # OPTALL is the list of compiler options to use with all tests
95 # OPTFILE is the list of compiler options to use with this file
96 # OPTSTR is the options to print with test messages
97 # XFAILDATA is the xfail data to be passed to the compiler
98 proc lto-obj { source dest optall optfile optstr xfaildata } {
99     global testcase
100     global tool
101     global compiler_conditional_xfail_data
102     global lto_skip_list
103
104     # Add the skip specifiers.
105     foreach skip $lto_skip_list {
106         if { ![string match $skip ""] } {
107             lappend optall "-DSKIP_$skip"
108         }
109     }
110
111     # Set up the options for compiling this file.
112     set options ""
113     lappend options "additional_flags=$optall $optfile"
114
115     set compiler_conditional_xfail_data $xfaildata
116     set comp_output [${tool}_target_compile "$source" "$dest" object $options]
117     ${tool}_check_compile "$testcase $dest assemble" $optstr $dest $comp_output
118 }
119
120 # lto-link-and-maybe-run -- link the object files and run the executable
121 # if compile_type is set to "run"
122 #
123 # TESTNAME is the mixture of object files to link
124 # OBJLIST is the list of object files to link
125 # DEST is the name of the executable
126 # OPTALL is a list of compiler and linker options to use for all tests
127 # OPTFILE is a list of compiler and linker options to use for this test
128 # OPTSTR is the list of options to list in messages
129 proc lto-link-and-maybe-run { testname objlist dest optall optfile optstr } {
130     global testcase
131     global tool
132     global compile_type
133
134     # Check that all of the objects were built successfully.
135     foreach obj [split $objlist] {
136         if ![file_on_host exists $obj] then {
137             unresolved "$testcase $testname link $optstr"
138             unresolved "$testcase $testname execute $optstr"
139             return
140         }
141     }
142
143     # Set up the options for linking this test.
144     set options ""
145     lappend options "additional_flags=$optall $optfile"
146
147     # Link the objects into an executable.
148     set comp_output [${tool}_target_compile "$objlist" $dest executable \
149                      "$options"]
150     if ![${tool}_check_compile "$testcase $testname link" "" \
151          $dest $comp_output] then {
152         unresolved "$testcase $testname execute $optstr"
153         return
154     }
155
156     # Return if we only needed to link.
157     if { ![string compare "link" $compile_type] } {
158         return
159     }
160
161     # Run the self-checking executable.
162     if ![string match "*/*" $dest] then {
163         set dest "./$dest"
164     }
165     set result [${tool}_load $dest "" ""]
166     set status [lindex $result 0]
167     if { $status == "pass" } then {
168         file_on_host delete $dest
169     }
170     $status "$testcase $testname execute $optstr"
171 }
172
173 # lto-get-options-main -- get target requirements for a test and
174 # options for the primary source file and the test as a whole
175 #
176 # SRC is the full pathname of the primary source file.
177 proc lto-get-options-main { src } {
178     global compile_type
179     global dg-extra-ld-options
180     global dg-suppress-ld-options
181
182     set dg-extra-ld-options ""
183     set dg-suppress-ld-options ""
184
185     # dg-options sets a variable called dg-extra-tool-flags.
186     set dg-extra-tool-flags ""
187
188     # dg-options sets a variable called tool_flags.
189     set tool_flags ""
190
191     # dg-require-* sets dg-do-what.
192     upvar dg-do-what dg-do-what 
193     upvar dg-final-code dg-final-code
194     set dg-final-code ""
195
196     set tmp [dg-get-options $src]
197     verbose "getting options for $src: $tmp"
198     foreach op $tmp {
199         set cmd [lindex $op 0]
200         verbose "cmd is $cmd"
201         if { [string match "dg-skip-if" $cmd] \
202              || [string match "dg-require-*" $cmd] } {
203             set status [catch "$op" errmsg]
204             if { $status != 0 } {
205                 perror "src: $errmsg for \"$op\"\n"
206                 unresolved "$src: $errmsg for \"$op\""
207                 return
208             }
209         } elseif { [string match "dg-lto-options" $cmd] } {
210             set op [lreplace $op 0 0 "dg-options"]
211             set status [catch "$op" errmsg]
212             if { $status != 0 } {
213                 perror "src: $errmsg for \"$op\"\n"
214                 unresolved "$src: $errmsg for \"$op\""
215                 return
216             }
217         } elseif { ![string compare "dg-xfail-if" $cmd] \
218                    || ![string compare "dg-options" $cmd] } {
219             warning "lto.exp does not support $cmd in primary source file"
220         } elseif { ![string compare "dg-lto-do" $cmd] } {
221             if { [llength $op] > 3 } {
222                 set kw [lindex [lindex $op 3] 0]
223                 if [string match "target" $kw] {
224                     perror "$src: dg-lto-do does not support \"target\""
225                 } elseif [string match "xfail" $kw] {
226                     perror "$src: dg-lto-do does not support \"xfail\""
227                 } else {
228                     perror "$src: dg-lto-do takes a single argument"
229                 }
230             }
231             set dgdo [lindex $op 2]
232             verbose "dg-lto-do command for \"$op\" is $dgdo"
233             if { ![string compare "assemble" $dgdo] } {
234                 set compile_type "assemble"
235             } elseif { ![string compare "run" $dgdo] } {
236                 set compile_type "run"
237             } elseif { ![string compare "link" $dgdo] } {
238                 set compile_type "link"
239             } else {
240                 warning "lto.exp does not support dg-lto-do $dgdo"
241             }
242         } elseif { ![string compare "dg-extra-ld-options" $cmd] } {
243             set dg-extra-ld-options [lindex $op 2]
244             verbose "dg-extra-ld-options for main is ${dg-extra-ld-options}"
245         } elseif { ![string compare "dg-suppress-ld-options" $cmd] } {
246             set dg-suppress-ld-options [lindex $op 2]
247             verbose \
248               "dg-suppress-ld-options for main is ${dg-suppress-ld-options}"
249         } elseif { ![string compare "dg-final" $cmd] } {
250             if { [llength $op] > 3 } {
251                 error "[lindex $op 0]: too many arguments"
252             } else {
253                 append dg-final-code "[lindex $op 2]\n"
254             }
255         } else {
256             # Ignore unrecognized dg- commands, but warn about them.
257             warning "lto.exp does not support $cmd"
258         }
259     }
260
261     # Return flags to use for compiling the primary source file and for
262     # linking.
263     verbose "dg-extra-tool-flags for main is ${dg-extra-tool-flags}"
264     return ${dg-extra-tool-flags}
265 }
266
267
268 # lto-get-options -- get special tool flags to use for a secondary
269 # source file
270 #
271 # SRC is the full pathname of the source file.
272 # The result is a list of options to use.
273 #
274 # This code is copied from proc dg-test in dg.exp from DejaGNU.
275 proc lto-get-options { src } {
276     # dg-options sets a variable called dg-extra-tool-flags.
277     set dg-extra-tool-flags ""
278
279     # dg-xfail-if sets compiler_conditional_xfail_data.
280     global compiler_conditional_xfail_data
281     set compiler_conditional_xfail_data ""
282
283     # dg-xfail-if needs access to dg-do-what.
284     upvar dg-do-what dg-do-what 
285
286     set tmp [dg-get-options $src]
287     foreach op $tmp {
288         set cmd [lindex $op 0]
289         if { ![string compare "dg-options" $cmd] \
290              || ![string compare "dg-xfail-if" $cmd] } {
291             set status [catch "$op" errmsg]
292             if { $status != 0 } {
293                 perror "src: $errmsg for \"$op\"\n"
294                 unresolved "$src: $errmsg for \"$op\""
295                 return
296             }
297         } elseif { [string match "dg-require-*" $cmd] } {
298             warning "lto.exp does not support $cmd in secondary source files"
299         } else {
300             # Ignore unrecognized dg- commands, but warn about them.
301             warning "lto.exp does not support $cmd in secondary source files"
302         }
303     }
304
305     return ${dg-extra-tool-flags}
306 }
307
308 # lto-execute -- compile multi-file tests
309 #
310 # SRC1 is the full pathname of the main file of the testcase.
311 # SID identifies a test suite in the names of temporary files.
312 proc lto-execute { src1 sid } {
313     global srcdir tmpdir
314     global option_list
315     global tool
316     global verbose
317     global testcase
318     global gluefile
319     global compiler_conditional_xfail_data
320     global dg-do-what-default
321     global compile_type
322     global dg-extra-ld-options
323     global dg-suppress-ld-options
324     global LTO_OPTIONS
325     global dg-final-code
326
327     # Get extra flags for this test from the primary source file, and
328     # process other dg-* options that this suite supports.  Warn about
329     # unsupported flags.
330     verbose "lto-execute: $src1" 1
331     set compile_type "run"
332     set dg-do-what [list ${dg-do-what-default} "" P]
333     set extra_flags(0) [lto-get-options-main $src1]
334     set compile_xfail(0) "" 
335
336     # If the main file defines dg-options, those flags are used to
337     # overwrite the default option_list taken from LTO_OPTIONS.
338     if { [string length $extra_flags(0)] > 0 } {
339         set option_list $extra_flags(0)
340         set extra_flags(0) ""
341     } else {
342         set option_list $LTO_OPTIONS
343     }
344
345     # Check whether this test is supported for this target.
346     if { [lindex ${dg-do-what} 1 ] == "N" } {
347         unsupported "$src1"
348         verbose "$src1 not supported on this target, skipping it" 3
349         return
350     }
351
352     # Set up the names of the other source files.
353     set dir [file dirname $src1]
354     set base [file rootname $src1]
355     set base [string range $base [string length $dir] end]
356     regsub "_0" $base "" base
357     regsub "/" $base "" base
358     set src_list $src1
359     set i 1
360     set done 0
361     while { !$done } {
362         set names [glob -nocomplain -types f -- "${dir}/${base}_${i}.*"]
363         if { [llength ${names}] > 1 } {
364             warning "lto-execute: more than one file matched ${dir}/${base}_${i}.*"
365         }
366         if { [llength ${names}] == 1 } {
367             lappend src_list [lindex ${names} 0]
368             incr i
369         } else {
370             set num_srcs ${i}
371             set done 1
372         }
373     }
374
375     # Use the dg-options mechanism to specify extra flags for each
376     # of the secondary files.
377     # The extra flags in each file are used to compile that file, and the
378     # extra flags in *_0.* are also used for linking.
379     verbose "\tsrc_list is: $src_list"
380     for {set i 1} {$i < $num_srcs} {incr i} {
381         set extra_flags($i) [lto-get-options [lindex $src_list $i]]
382         set compile_xfail($i) $compiler_conditional_xfail_data
383     }
384
385     # Define the names of the object files.
386     set obj_list ""
387     for {set i 0} {$i < $num_srcs} {incr i} {
388         lappend obj_list "${sid}_${base}_${i}.o"
389     }
390
391     # Get the base name of this test, for use in messages.
392     set testcase [lindex ${src_list} 0]
393
394     # Remove the $srcdir and $tmpdir prefixes from $src1.  (It would
395     # be possible to use "regsub" here, if we were careful to escape
396     # all regular expression characters in $srcdir and $tmpdir, but
397     # that would be more complicated that this approach.) 
398     if {[string first "$srcdir/" "${testcase}"] == 0} {
399         set testcase [string range "${testcase}" [string length "$srcdir/"] end]
400     }
401     if {[string first "$tmpdir/" "$testcase"] == 0} {
402         set testcase [string range "$testcase" [string length "$tmpdir/"] end]
403         set testcase "tmpdir-$testcase"
404     }
405     regsub "_0.*" $testcase "" testcase
406
407     # Set up the base name of executable files so they'll be unique.
408     regsub -all "\[./\]" $testcase "-" execbase
409
410     # If we couldn't rip $srcdir out of `src1' then just do the best we can.
411     # The point is to reduce the unnecessary noise in the logs.  Don't strip
412     # out too much because different testcases with the same name can confuse
413     # `test-tool'.
414     if [string match "/*" $testcase] then {
415         set testcase "[file tail [file dirname $src1]]/[file tail $src1]"
416     }
417
418     # Loop through all of the option lists used for this test.
419     set count 0
420     foreach option $option_list {
421         verbose "Testing $testcase, $option"
422
423         # There's a unique name for each executable we generate.
424         set execname "${execbase}-${count}1"
425         incr count
426
427         file_on_host delete $execname
428
429         # Compile pieces with the compiler under test.
430         set i 0
431         foreach src $src_list obj $obj_list {
432             lto-obj $src $obj $option $extra_flags($i) $option \
433                     $compile_xfail($i)
434             incr i
435         }
436
437         # Link (using the compiler under test), run, and clean up tests.
438         if { ![string compare "run" $compile_type] \
439              || ![string compare "link" $compile_type] } {
440
441             # Filter out any link options we were asked to suppress.
442             set reduced {}
443             foreach x [split $option] {
444               if {[lsearch ${dg-suppress-ld-options} $x] == -1} {
445                 lappend reduced $x
446               }
447             }
448             set filtered [join $reduced " "]
449
450             lto-link-and-maybe-run \
451                     "[lindex $obj_list 0]-[lindex $obj_list end]" \
452                     $obj_list $execname $filtered ${dg-extra-ld-options} \
453                     $filtered
454         }
455
456
457         # Are there any further tests to perform?
458         # Note that if the program has special run-time requirements, running
459         # of the program can be delayed until here.  Ditto for other situations.
460         # It would be a bit cumbersome though.
461
462         if ![string match ${dg-final-code} ""] {
463             regsub -all "\\\\(\[{}\])" ${dg-final-code} "\\1" dg-final-code
464             # Note that the use of `args' here makes this a varargs proc.
465             proc dg-final-proc { args } ${dg-final-code}
466             verbose "Running dg-final tests." 3
467             verbose "dg-final-proc:\n[info body dg-final-proc]" 4
468             if [catch "dg-final-proc $src1" errmsg] {
469                 perror "$name: error executing dg-final: $errmsg"
470                 # ??? The call to unresolved here is necessary to clear
471                 # `errcnt'.  What we really need is a proc like perror that
472                 # doesn't set errcnt.  It should also set exit_status to 1.
473                 unresolved "$name: error executing dg-final: $errmsg"
474             }
475         }
476
477         # Clean up object files.
478         set files [glob -nocomplain ${sid}_*.o]
479         if { $files != "" } {
480             foreach objfile $files {
481                 if { ![info exists gluefile] || $objfile != $gluefile } {
482                     eval "file_on_host delete $objfile"
483                 }
484             }
485         }
486
487         if { ![string compare "run" $compile_type] \
488              || ![string compare "link" $compile_type] } {
489             file_on_host delete $execname
490         }
491     }
492 }
493
494 # Utility for scanning a symbol in the final executable, invoked via dg-final.
495 # Call pass if pattern is present, otherwise fail.
496 #
497 # Argument 0 is the regexp to match.
498 # Argument 1 handles expected failures and the like
499 proc scan-symbol { args } {
500     global nm
501     global base_dir
502     upvar 2 execname execname
503
504     if { [llength $args] >= 2 } {
505         switch [dg-process-target [lindex $args 1]] {
506             "S" { }
507             "N" { return }
508             "F" { setup_xfail "*-*-*" }
509             "P" { }
510         }
511     }
512
513     # Find nm like we find g++ in g++.exp.
514     if ![info exists nm]  {
515         set nm [findfile $base_dir/../../../binutils/nm \
516                 $base_dir/../../../binutils/nm \
517                 [findfile $base_dir/../../nm $base_dir/../../nm \
518                       [findfile $base_dir/nm $base_dir/nm \
519                        [transform nm]]]]
520         verbose -log "nm is $nm"
521     }
522
523     set output_file "[glob -nocomplain $execname]"
524     if { $output_file == "" } {
525         fail "scan-symbol $args: dump file does not exist"
526         return
527     }
528
529     set fd [open "| $nm $output_file" r]
530     set text [read $fd]
531     close $fd
532
533     if [regexp -- [lindex $args 0] $text] {
534         pass "scan-symbol $args"
535     } else {
536         fail "scan-symbol $args"
537     }
538 }