OSDN Git Service

73eb396b2c3c31b60af7b01786f8f3b65401ddf8
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / lib / scanasm.exp
1 # Copyright (C) 2000, 2002, 2003, 2007, 2008, 2010, 2011
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 # Various utilities for scanning assembler output, used by gcc-dg.exp and
19 # g++-dg.exp.
20
21 # Utility for scanning compiler result, invoked via dg-final.
22
23 # Transform newline and similar characters into their escaped form.
24 proc make_pattern_printable { pattern } {
25     return [string map {\t \\t \n \\n \r \\r \\ \\\\} $pattern]
26 }
27
28 # Scan the OUTPUT_FILE for a pattern.  If it is present and POSITIVE
29 # is non-zero, or it is not present and POSITIVE is zero, the test
30 # passes.  The ORIG_ARGS is the list of arguments provided by dg-final
31 # to scan-assembler.  The first element in ORIG_ARGS is the regular
32 # expression to look for in the file.  The second element, if present,
33 # is a DejaGNU target selector.
34
35 proc dg-scan { name positive testcase output_file orig_args } {
36     if { [llength $orig_args] < 1 } {
37         error "$name: too few arguments"
38         return
39     }
40     if { [llength $orig_args] > 2 } {
41         error "$name: too many arguments"
42         return
43     }
44     if { [llength $orig_args] >= 2 } {
45         switch [dg-process-target [lindex $orig_args 1]] {
46             "S" { }
47             "N" { return }
48             "F" { setup_xfail "*-*-*" }
49             "P" { }
50         }
51     }
52
53     set pattern [lindex $orig_args 0]
54     set printable_pattern [make_pattern_printable $pattern]
55
56     if { [is_remote host] } {
57         remote_upload host "$output_file"
58     } 
59     set files [glob -nocomplain $output_file]
60     if { $files == "" } {
61         verbose -log "$testcase: output file does not exist"
62         unresolved "$testcase $name $printable_pattern"
63         return
64     }
65     set fd [open $output_file r]
66     set text [read $fd]
67     close $fd
68
69     set match [regexp -- $pattern $text]
70     if { $match == $positive } {
71         pass "$testcase $name $printable_pattern"
72     } else {
73         fail "$testcase $name $printable_pattern"
74     }
75 }
76
77 # Look for a pattern in the .s file produced by the compiler.  See
78 # dg-scan for details.
79
80 proc scan-assembler { args } {
81     upvar 2 name testcase
82     set testcase [lindex $testcase 0]
83     set output_file "[file rootname [file tail $testcase]].s"
84
85     dg-scan "scan-assembler" 1 $testcase $output_file $args
86 }
87
88 # Check that a pattern is not present in the .s file produced by the
89 # compiler.  See dg-scan for details.
90
91 proc scan-assembler-not { args } {
92     upvar 2 name testcase
93     set testcase [lindex $testcase 0]
94     set output_file "[file rootname [file tail $testcase]].s"
95
96     dg-scan "scan-assembler-not" 0 $testcase $output_file $args
97 }
98
99 # Return the scan for the assembly for hidden visibility. 
100
101 proc hidden-scan-for { symbol } {
102
103     set objformat [gcc_target_object_format]
104
105     switch $objformat {
106         elf      { return "hidden\[ \t_\]*$symbol" }
107         mach-o   { return "private_extern\[ \t_\]*_?$symbol" }
108         default  { return "" }
109     }
110
111 }
112
113
114 # Check that a symbol is defined as a hidden symbol in the .s file
115 # produced by the compiler.
116
117 proc scan-hidden { args } {
118     upvar 2 name testcase
119     set testcase [lindex $testcase 0]
120     set output_file "[file rootname [file tail $testcase]].s"
121
122     set symbol [lindex $args 0]
123
124     set hidden_scan [hidden-scan-for $symbol]
125
126     set args [lreplace $args 0 0 "$hidden_scan"]
127
128     dg-scan "scan-hidden" 1 $testcase $output_file $args
129 }
130
131 # Check that a symbol is not defined as a hidden symbol in the .s file
132 # produced by the compiler.
133
134 proc scan-not-hidden { args } {
135     upvar 2 name testcase
136     set testcase [lindex $testcase 0]
137     set output_file "[file rootname [file tail $testcase]].s"
138
139     set symbol [lindex $args 0]
140     set hidden_scan [hidden-scan-for $symbol]
141
142     set args [lreplace $args 0 0 "$hidden_scan"]
143
144     dg-scan "scan-not-hidden" 0 $testcase $output_file $args
145 }
146
147 # Look for a pattern in OUTPUT_FILE.  See dg-scan for details.
148
149 proc scan-file { output_file args } {
150     upvar 2 name testcase
151     set testcase [lindex $testcase 0]
152     dg-scan "scan-file" 1 $testcase $output_file $args
153 }
154
155 # Check that a pattern is not present in the OUTPUT_FILE.  See dg-scan
156 # for details.
157
158 proc scan-file-not { output_file args } {
159     upvar 2 name testcase
160     set testcase [lindex $testcase 0]
161     dg-scan "scan-file-not" 0 $testcase $output_file $args
162 }
163
164 # Look for a pattern in the .su file produced by the compiler.  See
165 # dg-scan for details.
166
167 proc scan-stack-usage { args } {
168     upvar 2 name testcase
169     set testcase [lindex $testcase 0]
170     set output_file "[file rootname [file tail $testcase]].su"
171
172     dg-scan "scan-file" 1 $testcase $output_file $args
173 }
174
175 # Check that a pattern is not present in the .su file produced by the
176 # compiler.  See dg-scan for details.
177
178 proc scan-stack-usage-not { args } {
179     upvar 2 name testcase
180     set testcase [lindex $testcase 0]
181     set output_file "[file rootname [file tail $testcase]].su"
182
183     dg-scan "scan-file-not" 0 $testcase $output_file $args
184 }
185
186 # Call pass if pattern is present given number of times, otherwise fail.
187 proc scan-assembler-times { args } {
188     if { [llength $args] < 2 } {
189         error "scan-assembler: too few arguments"
190         return
191     }
192     if { [llength $args] > 3 } {
193         error "scan-assembler: too many arguments"
194         return
195     }
196     if { [llength $args] >= 3 } {
197         switch [dg-process-target [lindex $args 2]] {
198             "S" { }
199             "N" { return }
200             "F" { setup_xfail "*-*-*" }
201             "P" { }
202         }
203     }
204
205     # This assumes that we are two frames down from dg-test, and that
206     # it still stores the filename of the testcase in a local variable "name".
207     # A cleaner solution would require a new dejagnu release.
208     upvar 2 name testcase
209     set testcase [lindex $testcase 0]
210
211     set pattern [lindex $args 0]
212     set pp_pattern [make_pattern_printable $pattern]
213
214     # This must match the rule in gcc-dg.exp.
215     set output_file "[file rootname [file tail $testcase]].s"
216
217     set files [glob -nocomplain $output_file]
218     if { $files == "" } {
219         verbose -log "$testcase: output file does not exist"
220         unresolved "$testcase scan-assembler-times $pp_pattern [lindex $args 1]"
221         return
222     }
223
224     set fd [open $output_file r]
225     set text [read $fd]
226     close $fd
227
228     if { [llength [regexp -inline -all -- $pattern $text]] == [lindex $args 1]} {
229         pass "$testcase scan-assembler-times $pp_pattern [lindex $args 1]"
230     } else {
231         fail "$testcase scan-assembler-times $pp_pattern [lindex $args 1]"
232     }
233 }
234
235 # Utility for scanning demangled compiler result, invoked via dg-final.
236 # Call pass if pattern is present, otherwise fail.
237 proc scan-assembler-dem { args } {
238     global cxxfilt
239     global base_dir
240
241     if { [llength $args] < 1 } {
242         error "scan-assembler-dem: too few arguments"
243         return
244     }
245     if { [llength $args] > 2 } {
246         error "scan-assembler-dem: too many arguments"
247         return
248     }
249     if { [llength $args] >= 2 } {
250         switch [dg-process-target [lindex $args 1]] {
251             "S" { }
252             "N" { return }
253             "F" { setup_xfail "*-*-*" }
254             "P" { }
255         }
256     }
257
258     # Find c++filt like we find g++ in g++.exp.
259     if ![info exists cxxfilt]  {
260         set cxxfilt [findfile $base_dir/../../../binutils/cxxfilt \
261                      $base_dir/../../../binutils/cxxfilt \
262                      [findfile $base_dir/../../c++filt $base_dir/../../c++filt \
263                       [findfile $base_dir/c++filt $base_dir/c++filt \
264                        [transform c++filt]]]]
265         verbose -log "c++filt is $cxxfilt"
266     }
267
268     upvar 2 name testcase
269     set testcase [lindex $testcase 0]
270     set pattern [lindex $args 0]
271     set pp_pattern [make_pattern_printable $pattern]
272     set output_file "[file rootname [file tail $testcase]].s"
273
274     set files [glob -nocomplain $output_file]
275     if { $files == "" } {
276         verbose -log "$testcase: output file does not exist"
277         unresolved "$testcase scan-assembler-dem $pp_pattern"
278         return
279     }
280
281     set output [remote_exec host "$cxxfilt" "" "$output_file"]
282     set text [lindex $output 1]
283
284     if [regexp -- $pattern $text] {
285         pass "$testcase scan-assembler-dem $pp_pattern"
286     } else {
287         fail "$testcase scan-assembler-dem $pp_pattern"
288     }
289 }
290
291 # Call pass if demangled pattern is not present, otherwise fail.
292 proc scan-assembler-dem-not { args } {
293     global cxxfilt
294     global base_dir
295
296     if { [llength $args] < 1 } {
297         error "scan-assembler-dem-not: too few arguments"
298         return
299     }
300     if { [llength $args] > 2 } {
301         error "scan-assembler-dem-not: too many arguments"
302         return
303     }
304     if { [llength $args] >= 2 } {
305         switch [dg-process-target [lindex $args 1]] {
306             "S" { }
307             "N" { return }
308             "F" { setup_xfail "*-*-*" }
309             "P" { }
310         }
311     }
312
313     # Find c++filt like we find g++ in g++.exp.
314     if ![info exists cxxfilt]  {
315         set cxxfilt [findfile $base_dir/../../../binutils/cxxfilt \
316                      $base_dir/../../../binutils/cxxfilt \
317                      [findfile $base_dir/../../c++filt $base_dir/../../c++filt \
318                       [findfile $base_dir/c++filt $base_dir/c++filt \
319                        [transform c++filt]]]]
320         verbose -log "c++filt is $cxxfilt"
321     }
322
323     upvar 2 name testcase
324     set testcase [lindex $testcase 0]
325     set pattern [lindex $args 0]
326     set pp_pattern [make_pattern_printable $pattern]
327     set output_file "[file rootname [file tail $testcase]].s"
328
329     set files [glob -nocomplain $output_file]
330     if { $files == "" } {
331         verbose -log "$testcase: output file does not exist"
332         unresolved "$testcase scan-assembler-dem-not $pp_pattern"
333         return
334     }
335
336     set output [remote_exec host "$cxxfilt" "" "$output_file"]
337     set text [lindex $output 1]
338
339     if ![regexp -- $pattern $text] {
340         pass "$testcase scan-assembler-dem-not $pp_pattern"
341     } else {
342         fail "$testcase scan-assembler-dem-not $pp_pattern"
343     }
344 }
345
346 # Call pass if object size is ok, otherwise fail.
347 # example: /* { dg-final { object-size text <= 54 } } */
348 proc object-size { args } {
349     global size
350     global base_dir
351
352     if { [llength $args] < 3 } {
353         error "object-size: too few arguments"
354         return
355     }
356     if { [llength $args] > 4 } {
357         error "object-size: too many arguments"
358         return
359     }
360     if { [llength $args] >= 4 } {
361         switch [dg-process-target [lindex $args 3]] {
362             "S" { }
363             "N" { return }
364             "F" { setup_xfail "*-*-*" }
365             "P" { }
366         }
367     }
368
369     # Find size like we find g++ in g++.exp.
370     if ![info exists size]  {
371         set size [findfile $base_dir/../../../binutils/size \
372                   $base_dir/../../../binutils/size \
373                   [findfile $base_dir/../../size $base_dir/../../size \
374                    [findfile $base_dir/size $base_dir/size \
375                     [transform size]]]]
376         verbose -log "size is $size"
377     }
378
379     upvar 2 name testcase
380     set testcase [lindex $testcase 0]
381
382     set what [lindex $args 0]
383     set where [lsearch { text data bss total } $what]
384     if { $where == -1 } {
385         error "object-size: illegal argument: $what"
386         return
387     }
388     set cmp [lindex $args 1]
389     if { [lsearch { < > <= >= == != } $cmp] == -1 } {
390         error "object-size: illegal argument: $cmp"
391         return
392     }
393     set with [lindex $args 2]
394     if ![string is integer $with ] {
395         error "object-size: illegal argument: $with"
396         return
397     }
398
399     set output_file "[file rootname [file tail $testcase]].o"
400     if ![file_on_host exists $output_file] {
401         verbose -log "$testcase: $output_file does not exist"
402         unresolved "$testcase object-size $what $cmp $with"
403         return
404     }
405     set output [remote_exec host "$size" "$output_file"]
406     set status [lindex $output 0]
407     if { $status != 0 } {
408         verbose -log "$testcase object-size: $size failed"
409         unresolved "$testcase object-size $what $cmp $with"
410         return
411     }
412
413     set text [lindex $output 1]
414     set lines [split $text "\n"]
415
416     set line0 [lindex $lines 0]
417     if ![regexp {^\s*text\s+data\s+bss\s+dec\s+hex\s+filename\s*$} $line0] {
418         verbose -log "$testcase object-size: $size did not produce expected first line: $line0"
419         unresolved "$testcase object-size $what $cmp $with"
420         return
421     }
422
423     set line1 [lindex $lines 1]
424     if ![regexp {^\s*\d+\s+\d+\s+\d+\s+\d+\s+[\da-fA-F]+\s+} $line1] {
425         verbose -log "$testcase object-size: $size did not produce expected second line: $line1"
426         unresolved "$testcase object-size $what $cmp $with"
427         return
428     }
429
430     set actual [lindex $line1 $where]
431     verbose -log "$what size is $actual"
432
433     if [expr $actual $cmp $with] {
434         pass "$testcase object-size $what $cmp $with"
435     } else {
436         fail "$testcase object-size $what $cmp $with"
437     }
438 }
439
440 # Utility for testing that a function is defined on the current line.
441 # Call pass if so, otherwise fail.  Invoked directly; the file must
442 # have been compiled with -g -dA.
443 #
444 # Argument 0 is the current line, passed implicitly by dejagnu
445 # Argument 1 is the function to check
446 # Argument 2 handles expected failures and the like
447 # Argument 3 is "." to match the current line, or an integer to match
448 # an explicit line.
449 proc dg-function-on-line { args } {
450     # Upvar from dg-final:
451     upvar dg-final-code final-code
452
453     set line [lindex $args 0]
454     set symbol [lindex $args 1]
455     set failures [lindex $args 2]
456
457     if { [llength $args] >= 4 } {
458         switch [lindex $args 3] {
459             "." { }
460             "default" { set line [lindex $args 3] }
461         }
462     }
463
464     if { [istarget "hppa*-*-*"] } {
465         set pattern [format {\t;[^:]+:%d\n(\t[^\t]+\n)+%s:\n\t.PROC} \
466                      $line $symbol]
467     } elseif { [istarget "mips-sgi-irix*"] } {
468         set pattern [format {\t\.loc [0-9]+ %d 0( [^\n]*)?\n\t\.set\t(no)?mips16\n\t\.ent\t%s\n\t\.type\t%s, @function\n%s:\n} \
469                      $line $symbol $symbol $symbol]
470     } else {
471         set pattern [format {%s:[^\t]*(\t.(fnstart|frame|mask|file)[^\t]*)*\t[^:]+:%d\n} \
472                      $symbol $line]
473     }
474
475     # The lack of spaces around $pattern is important, since they'd
476     # become part of the regex scan-assembler tries to match.
477     set cmd "scan-assembler {$pattern}"
478     if { [llength $args] >= 3 } {
479         set cmd "$cmd {$failures}"
480     }
481
482     append final-code "$cmd\n"
483 }