* lib/gas-defs.exp (regexp_diff): Allow '#...' to mean "some
[binutils.git] / gas / testsuite / lib / gas-defs.exp
blobffa5f68faa64eff627392a34ada83587d54eadb4
1 # Copyright (C) 1993, 1994, 1997 Free Software Foundation, Inc.
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.
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, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 # Please email any bugs, comments, and/or additions to this file to:
18 # DejaGnu@cygnus.com
20 # This file was written by Ken Raeburn (raeburn@cygnus.com).
22 proc gas_version {} {
23 global AS
24 catch "exec $AS -version < /dev/null" tmp
25 # Should find a way to discard constant parts, keep whatever's
26 # left, so the version string could be almost anything at all...
27 regexp "\[^\n\]* (cygnus-|)(\[-0-9.a-zA-Z-\]+)\[\r\n\].*" $tmp version cyg number
28 if ![info exists number] then {
29 return "[which $AS] (no version number)\n"
31 clone_output "[which $AS] $number\n"
32 unset version
35 proc gas_run { prog as_opts redir } {
36 global AS
37 global ASFLAGS
38 global comp_output
39 global srcdir
40 global subdir
41 global host_triplet
43 verbose "Executing $srcdir/lib/run $AS $ASFLAGS $as_opts $srcdir/$subdir/$prog $redir"
44 catch "exec $srcdir/lib/run $AS $ASFLAGS $as_opts $srcdir/$subdir/$prog $redir" comp_output
45 set comp_output [prune_warnings $comp_output]
46 verbose "output was $comp_output"
47 return [list $comp_output ""];
50 proc all_ones { args } {
51 foreach x $args { if [expr $x!=1] { return 0 } }
52 return 1
55 proc gas_start { prog as_opts } {
56 global AS
57 global ASFLAGS
58 global srcdir
59 global subdir
60 global spawn_id
62 verbose "Starting $AS $ASFLAGS $as_opts $prog" 2
63 catch {
64 spawn -noecho -nottycopy $srcdir/lib/run $AS $ASFLAGS $as_opts $srcdir/$subdir/$prog
65 } foo
66 if ![regexp {^[0-9]+} $foo] then {
67 perror "Can't run $subdir/$prog: $foo"
71 proc gas_finish { } {
72 global spawn_id
74 catch "close"
75 catch "wait"
78 proc want_no_output { testname } {
79 global comp_output
81 if ![string match "" $comp_output] then {
82 send_log "$comp_output\n"
83 verbose "$comp_output" 3
85 if [string match "" $comp_output] then {
86 pass "$testname"
87 return 1
88 } else {
89 fail "$testname"
90 return 0
94 proc gas_test_old { file as_opts testname } {
95 gas_run $file $as_opts ""
96 return [want_no_output $testname]
99 proc gas_test { file as_opts var_opts testname } {
100 global comp_output
102 set i 0
103 foreach word $var_opts {
104 set ignore_stdout($i) [string match "*>" $word]
105 set opt($i) [string trim $word {>}]
106 incr i
108 set max [expr 1<<$i]
109 for {set i 0} {[expr $i<$max]} {incr i} {
110 set maybe_ignore_stdout ""
111 set extra_opts ""
112 for {set bit 0} {(1<<$bit)<$max} {incr bit} {
113 set num [expr 1<<$bit]
114 if [expr $i&$num] then {
115 set extra_opts "$extra_opts $opt($bit)"
116 if $ignore_stdout($bit) then {
117 set maybe_ignore_stdout ">/dev/null"
121 set extra_opts [string trim $extra_opts]
122 gas_run $file "$as_opts $extra_opts" $maybe_ignore_stdout
124 # Should I be able to use a conditional expression here?
125 if [string match "" $extra_opts] then {
126 want_no_output $testname
127 } else {
128 want_no_output "$testname ($extra_opts)"
131 if [info exists errorInfo] then {
132 unset errorInfo
136 proc gas_test_ignore_stdout { file as_opts testname } {
137 global comp_output
139 gas_run $file $as_opts ">/dev/null"
140 want_no_output $testname
143 proc gas_test_error { file as_opts testname } {
144 global comp_output
146 gas_run $file $as_opts ">/dev/null"
147 if ![string match "" $comp_output] then {
148 send_log "$comp_output\n"
149 verbose "$comp_output" 3
151 if [string match "" $comp_output] then {
152 fail "$testname"
153 } else {
154 pass "$testname"
158 proc gas_exit {} {}
160 proc gas_init { args } {
161 global target_cpu
162 global target_cpu_family
163 global target_family
164 global target_vendor
165 global target_os
166 global stdoptlist
168 case "$target_cpu" in {
169 "m68???" { set target_cpu_family m68k }
170 "i[34]86" { set target_cpu_family i386 }
171 default { set target_cpu_family $target_cpu }
174 set target_family "$target_cpu_family-$target_vendor-$target_os"
175 set stdoptlist "-a>"
177 if ![istarget "*-*-*"] {
178 perror "Target name [istarget] is not a triple."
180 # Need to return an empty string.
181 return
185 # run_dump_test FILE
187 # Assemble a .s file, then run some utility on it and check the output.
189 # There should be an assembly language file named FILE.s in the test
190 # suite directory, and a pattern file called FILE.d. `run_dump_test'
191 # will assemble FILE.s, run some tool like `objdump', `objcopy', or
192 # `nm' on the .o file to produce textual output, and then analyze that
193 # with regexps. The FILE.d file specifies what program to run, and
194 # what to expect in its output.
196 # The FILE.d file begins with zero or more option lines, which specify
197 # flags to pass to the assembler, the program to run to dump the
198 # assembler's output, and the options it wants. The option lines have
199 # the syntax:
201 # # OPTION: VALUE
203 # OPTION is the name of some option, like "name" or "objdump", and
204 # VALUE is OPTION's value. The valid options are described below.
205 # Whitespace is ignored everywhere, except within VALUE. The option
206 # list ends with the first line that doesn't match the above syntax
207 # (hmm, not great for error detection).
209 # The interesting options are:
211 # name: TEST-NAME
212 # The name of this test, passed to DejaGNU's `pass' and `fail'
213 # commands. If omitted, this defaults to FILE, the root of the
214 # .s and .d files' names.
216 # as: FLAGS
217 # When assembling FILE.s, pass FLAGS to the assembler.
219 # PROG: PROGRAM-NAME
220 # The name of the program to run to analyze the .o file produced
221 # by the assembler. This can be omitted; run_dump_test will guess
222 # which program to run by seeing which of the flags options below
223 # is present.
225 # objdump: FLAGS
226 # nm: FLAGS
227 # objcopy: FLAGS
228 # Use the specified program to analyze the .o file, and pass it
229 # FLAGS, in addition to the .o file name.
231 # source: SOURCE
232 # Assemble the file SOURCE.s. If omitted, this defaults to FILE.s.
233 # This is useful if several .d files want to share a .s file.
235 # Each option may occur at most once.
237 # After the option lines come regexp lines. `run_dump_test' calls
238 # `regexp_diff' to compare the output of the dumping tool against the
239 # regexps in FILE.d. `regexp_diff' is defined later in this file; see
240 # further comments there.
242 proc run_dump_test { name } {
243 global subdir srcdir
244 global OBJDUMP NM AS OBJCOPY
245 global OBJDUMPFLAGS NMFLAGS ASFLAGS OBJCOPYFLAGS
246 global host_triplet
248 if [string match "*/*" $name] {
249 set file $name
250 set name [file tail $name]
251 } else {
252 set file "$srcdir/$subdir/$name"
254 set opt_array [slurp_options "${file}.d"]
255 if { $opt_array == -1 } {
256 perror "error reading options from $file.d"
257 unresolved $subdir/$name
258 return
260 set opts(as) {}
261 set opts(objdump) {}
262 set opts(nm) {}
263 set opts(objcopy) {}
264 set opts(name) {}
265 set opts(PROG) {}
266 set opts(source) {}
268 foreach i $opt_array {
269 set opt_name [lindex $i 0]
270 set opt_val [lindex $i 1]
271 if ![info exists opts($opt_name)] {
272 perror "unknown option $opt_name in file $file.d"
273 unresolved $subdir/$name
274 return
276 if [string length $opts($opt_name)] {
277 perror "option $opt_name multiply set in $file.d"
278 unresolved $subdir/$name
279 return
281 set opts($opt_name) $opt_val
284 if {$opts(PROG) != ""} {
285 switch -- $opts(PROG) {
286 objdump
287 { set program objdump }
289 { set program nm }
290 objcopy
291 { set program objcopy }
292 default
293 { perror "unrecognized program option $opts(PROG) in $file.d"
294 unresolved $subdir/$name
295 return }
297 } else {
298 # Guess which program to run, by seeing which option was specified.
299 set program ""
300 foreach p {objdump objcopy nm} {
301 if {$opts($p) != ""} {
302 if {$program != ""} {
303 perror "ambiguous dump program in $file.d"
304 unresolved $subdir/$name
305 return
306 } else {
307 set program $p
311 if {$program == ""} {
312 perror "dump program unspecified in $file.d"
313 unresolved $subdir/$name
314 return
318 set progopts1 $opts($program)
319 eval set progopts \$[string toupper $program]FLAGS
320 eval set binary \$[string toupper $program]
321 if { $opts(name) == "" } {
322 set testname "$subdir/$name"
323 } else {
324 set testname $opts(name)
327 if { $opts(source) == "" } {
328 set sourcefile ${file}.s
329 } else {
330 set sourcefile $srcdir/$subdir/$opts(source)
333 send_log "$AS $ASFLAGS $opts(as) -o dump.o $sourcefile\n"
334 catch "exec $srcdir/lib/run $AS $ASFLAGS $opts(as) -o dump.o $sourcefile" comp_output
335 set comp_output [prune_warnings $comp_output]
337 if ![string match "" $comp_output] then {
338 send_log "$comp_output\n"
339 verbose "$comp_output" 3
340 fail $testname
341 return
344 if { [which $binary] == 0 } {
345 untested $testname
346 return
349 if { $progopts1 == "" } { set $progopts1 "-r" }
350 verbose "running $binary $progopts $progopts1" 3
352 # Objcopy, unlike the other two, won't send its output to stdout,
353 # so we have to run it specially.
354 if { $program == "objcopy" } {
355 send_log "$binary $progopts $progopts1 dump.o dump.out\n"
356 catch "exec $binary $progopts $progopts1 dump.o dump.out" comp_output
357 set comp_output [prune_warnings $comp_output]
358 if ![string match "" $comp_output] then {
359 send_log "$comp_output\n"
360 fail $testname
361 return
363 } else {
364 send_log "$binary $progopts $progopts1 dump.o > dump.out\n"
365 catch "exec $binary $progopts $progopts1 dump.o > dump.out" comp_output
366 set comp_output [prune_warnings $comp_output]
367 if ![string match "" $comp_output] then {
368 send_log "$comp_output\n"
369 fail $testname
370 return
374 verbose_eval {[file_contents "dump.out"]} 3
375 if { [regexp_diff "dump.out" "${file}.d"] } then {
376 fail $testname
377 verbose "output is [file_contents "dump.out"]" 2
378 return
381 pass $testname
384 proc slurp_options { file } {
385 if [catch { set f [open $file r] } x] {
386 #perror "couldn't open `$file': $x"
387 perror "$x"
388 return -1
390 set opt_array {}
391 # whitespace expression
392 set ws {[ ]*}
393 set nws {[^ ]*}
394 # whitespace is ignored anywhere except within the options list;
395 # option names are alphabetic only
396 set pat "^#${ws}(\[a-zA-Z\]*)$ws:${ws}(.*)$ws\$"
397 while { [gets $f line] != -1 } {
398 set line [string trim $line]
399 # Whitespace here is space-tab.
400 if [regexp $pat $line xxx opt_name opt_val] {
401 # match!
402 lappend opt_array [list $opt_name $opt_val]
403 } else {
404 break
407 close $f
408 return $opt_array
411 proc objdump { opts } {
412 global OBJDUMP
413 global comp_output
414 global host_triplet
416 catch "exec $OBJDUMP $opts" comp_output
417 set comp_output [prune_warnings $comp_output]
418 verbose "objdump output=$comp_output\n" 3
421 proc objdump_start_no_subdir { prog opts } {
422 global OBJDUMP
423 global srcdir
424 global spawn_id
426 verbose "Starting $OBJDUMP $opts $prog" 2
427 catch {
428 spawn -noecho -nottyinit $srcdir/lib/run $OBJDUMP $opts $prog
429 } foo
430 if ![regexp {^[0-9]+} $foo] then {
431 perror "Can't run $prog: $foo"
435 proc objdump_finish { } {
436 global spawn_id
438 catch "close"
439 catch "wait"
442 # Default timeout is 10 seconds, loses on a slow machine. But some
443 # configurations of dejagnu may override it.
444 if {$timeout<120} then { set timeout 120 }
446 expect_after -i {
447 timeout { perror "timeout" }
448 "virtual memory exhausted" { perror "virtual memory exhausted" }
449 buffer_full { perror "buffer full" }
450 eof { perror "eof" }
453 # regexp_diff, based on simple_diff taken from ld test suite
454 # compares two files line-by-line
455 # file1 contains strings, file2 contains regexps and #-comments
456 # blank lines are ignored in either file
457 # returns non-zero if differences exist
459 proc regexp_diff { file_1 file_2 } {
461 set eof -1
462 set end_1 0
463 set end_2 0
464 set differences 0
465 set diff_pass 0
467 if [file exists $file_1] then {
468 set file_a [open $file_1 r]
469 } else {
470 warning "$file_1 doesn't exist"
471 return 1
474 if [file exists $file_2] then {
475 set file_b [open $file_2 r]
476 } else {
477 fail "$file_2 doesn't exist"
478 close $file_a
479 return 1
482 verbose " Regexp-diff'ing: $file_1 $file_2" 2
484 while { 1 } {
485 set line_a ""
486 set line_b ""
487 while { [string length $line_a] == 0 } {
488 if { [gets $file_a line_a] == $eof } {
489 set end_1 1
490 break
493 while { [string length $line_b] == 0 || [string match "#*" $line_b] } {
494 if [ string match "#pass" $line_b ] {
495 set end_2 1
496 set diff_pass 1
497 break
498 } elseif [ string match "#..." $line_b ] {
499 if { [gets $file_b line_b] == $eof } {
500 set end_2 1
501 break
503 verbose "looking for \"^$line_b$\"" 3
504 while { ![regexp "^$line_b$" "$line_a"] } {
505 verbose "skipping \"$line_a\"" 3
506 if { [gets $file_a line_a] == $eof } {
507 set end_1 1
508 break
511 break
513 if { [gets $file_b line_b] == $eof } {
514 set end_2 1
515 break
519 if { $diff_pass } {
520 break
521 } elseif { $end_1 && $end_2 } {
522 break
523 } elseif { $end_1 } {
524 send_log "extra regexps in $file_2 starting with \"^$line_b$\"\nEOF from $file_1\n"
525 verbose "extra regexps in $file_2 starting with \"^$line_b$\"\nEOF from $file_1" 3
526 set differences 1
527 break
528 } elseif { $end_2 } {
529 send_log "extra lines in $file_1 starting with \"^$line_a$\"\nEOF from $file_2\n"
530 verbose "extra lines in $file_1 starting with \"^$line_a$\"\nEOF from $file_2\n" 3
531 set differences 1
532 break
533 } else {
534 verbose "regexp \"^$line_b$\"\nline \"$line_a\"" 3
535 if ![regexp "^$line_b$" "$line_a"] {
536 send_log "regexp_diff match failure\n"
537 send_log "regexp \"^$line_b$\"\nline \"$line_a\"\n"
538 set differences 1
539 break
544 if { $differences == 0 && !$diff_pass && [eof $file_a] != [eof $file_b] } {
545 send_log "$file_1 and $file_2 are different lengths\n"
546 verbose "$file_1 and $file_2 are different lengths" 3
547 set differences 1
550 close $file_a
551 close $file_b
553 return $differences
556 proc file_contents { filename } {
557 set file [open $filename r]
558 set contents [read $file]
559 close $file
560 return $contents
563 proc verbose_eval { expr { level 1 } } {
564 global verbose
565 if $verbose>$level then { eval verbose "$expr" $level }
568 # This definition is taken from an unreleased version of DejaGnu. Once
569 # that version gets released, and has been out in the world for a few
570 # months at least, it may be safe to delete this copy.
571 if ![string length [info proc prune_warnings]] {
573 # prune_warnings -- delete various system verbosities from TEXT.
575 # An example is:
576 # ld.so: warning: /usr/lib/libc.so.1.8.1 has older revision than expected 9
578 # Sites with particular verbose os's may wish to override this in site.exp.
580 proc prune_warnings { text } {
581 # This is from sun4's. Do it for all machines for now.
582 # The "\\1" is to try to preserve a "\n" but only if necessary.
583 regsub -all "(^|\n)(ld.so: warning:\[^\n\]*\n?)+" $text "\\1" text
585 # It might be tempting to get carried away and delete blank lines, etc.
586 # Just delete *exactly* what we're ask to, and that's it.
587 return $text