Initial revision
[binutils.git] / gas / testsuite / lib / gas-defs.exp
blob757718ffa476909987f696592d0dfbf4a4cd4a29
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 unresolved $subdir/$name
257 return
259 set opts(as) {}
260 set opts(objdump) {}
261 set opts(nm) {}
262 set opts(objcopy) {}
263 set opts(name) {}
264 set opts(PROG) {}
265 set opts(source) {}
267 foreach i $opt_array {
268 set opt_name [lindex $i 0]
269 set opt_val [lindex $i 1]
270 if ![info exists opts($opt_name)] {
271 perror "unknown option $opt_name in file $file.d"
272 unresolved $subdir/$name
273 return
275 if [string length $opts($opt_name)] {
276 perror "option $opt_name multiply set in $file.d"
277 unresolved $subdir/$name
278 return
280 set opts($opt_name) $opt_val
283 if {$opts(PROG) != ""} {
284 switch -- $opts(PROG) {
285 objdump
286 { set program objdump }
288 { set program nm }
289 objcopy
290 { set program objcopy }
291 default
292 { perror "unrecognized program option $opts(PROG) in $file.d"
293 unresolved $subdir/$name
294 return }
296 } else {
297 # Guess which program to run, by seeing which option was specified.
298 set program ""
299 foreach p {objdump objcopy nm} {
300 if {$opts($p) != ""} {
301 if {$program != ""} {
302 perror "ambiguous dump program in $file.d"
303 unresolved $subdir/$name
304 return
305 } else {
306 set program $p
310 if {$program == ""} {
311 perror "dump program unspecified in $file.d"
312 unresolved $subdir/$name
313 return
317 set progopts1 $opts($program)
318 eval set progopts \$[string toupper $program]FLAGS
319 eval set binary \$[string toupper $program]
320 if { $opts(name) == "" } {
321 set testname "$subdir/$name"
322 } else {
323 set testname $opts(name)
326 if { $opts(source) == "" } {
327 set sourcefile ${file}.s
328 } else {
329 set sourcefile $srcdir/$subdir/$opts(source)
332 send_log "$AS $ASFLAGS $opts(as) -o dump.o $sourcefile\n"
333 catch "exec $srcdir/lib/run $AS $ASFLAGS $opts(as) -o dump.o $sourcefile" comp_output
334 set comp_output [prune_warnings $comp_output]
336 if ![string match "" $comp_output] then {
337 send_log "$comp_output\n"
338 verbose "$comp_output" 3
339 fail $testname
340 return
343 if { [which $binary] == 0 } {
344 untested $testname
345 return
348 if { $progopts1 == "" } { set $progopts1 "-r" }
349 verbose "running $binary $progopts $progopts1" 3
351 # Objcopy, unlike the other two, won't send its output to stdout,
352 # so we have to run it specially.
353 if { $program == "objcopy" } {
354 send_log "$binary $progopts $progopts1 dump.o dump.out\n"
355 catch "exec $binary $progopts $progopts1 dump.o dump.out" comp_output
356 set comp_output [prune_warnings $comp_output]
357 if ![string match "" $comp_output] then {
358 send_log "$comp_output\n"
359 fail $testname
360 return
362 } else {
363 send_log "$binary $progopts $progopts1 dump.o > dump.out\n"
364 catch "exec $binary $progopts $progopts1 dump.o > dump.out" comp_output
365 set comp_output [prune_warnings $comp_output]
366 if ![string match "" $comp_output] then {
367 send_log "$comp_output\n"
368 fail $testname
369 return
373 verbose_eval {[file_contents "dump.out"]} 3
374 if { [regexp_diff "dump.out" "${file}.d"] } then {
375 fail $testname
376 verbose "output is [file_contents "dump.out"]" 2
377 return
380 pass $testname
383 proc slurp_options { file } {
384 if [catch { set f [open $file r] } x] {
385 #perror "couldn't open `$file': $x"
386 perror "$x"
387 return -1
389 set opt_array {}
390 # whitespace expression
391 set ws {[ ]*}
392 set nws {[^ ]*}
393 # whitespace is ignored anywhere except within the options list;
394 # option names are alphabetic only
395 set pat "^#${ws}(\[a-zA-Z\]*)$ws:${ws}(.*)$ws\$"
396 while { [gets $f line] != -1 } {
397 set line [string trim $line]
398 # Whitespace here is space-tab.
399 if [regexp $pat $line xxx opt_name opt_val] {
400 # match!
401 lappend opt_array [list $opt_name $opt_val]
402 } else {
403 break
406 close $f
407 return $opt_array
410 proc objdump { opts } {
411 global OBJDUMP
412 global comp_output
413 global host_triplet
415 catch "exec $OBJDUMP $opts" comp_output
416 set comp_output [prune_warnings $comp_output]
417 verbose "objdump output=$comp_output\n" 3
420 proc objdump_start_no_subdir { prog opts } {
421 global OBJDUMP
422 global srcdir
423 global spawn_id
425 verbose "Starting $OBJDUMP $opts $prog" 2
426 catch {
427 spawn -noecho -nottyinit $srcdir/lib/run $OBJDUMP $opts $prog
428 } foo
429 if ![regexp {^[0-9]+} $foo] then {
430 perror "Can't run $prog: $foo"
434 proc objdump_finish { } {
435 global spawn_id
437 catch "close"
438 catch "wait"
441 # Default timeout is 10 seconds, loses on a slow machine. But some
442 # configurations of dejagnu may override it.
443 if {$timeout<120} then { set timeout 120 }
445 expect_after -i {
446 timeout { perror "timeout" }
447 "virtual memory exhausted" { perror "virtual memory exhausted" }
448 buffer_full { perror "buffer full" }
449 eof { perror "eof" }
452 # regexp_diff, based on simple_diff taken from ld test suite
453 # compares two files line-by-line
454 # file1 contains strings, file2 contains regexps and #-comments
455 # blank lines are ignored in either file
456 # returns non-zero if differences exist
458 proc regexp_diff { file_1 file_2 } {
460 set eof -1
461 set end_1 0
462 set end_2 0
463 set differences 0
464 set diff_pass 0
466 if [file exists $file_1] then {
467 set file_a [open $file_1 r]
468 } else {
469 warning "$file_1 doesn't exist"
470 return 1
473 if [file exists $file_2] then {
474 set file_b [open $file_2 r]
475 } else {
476 fail "$file_2 doesn't exist"
477 close $file_a
478 return 1
481 verbose " Regexp-diff'ing: $file_1 $file_2" 2
483 while { 1 } {
484 set line_a ""
485 set line_b ""
486 while { [string length $line_a] == 0 } {
487 if { [gets $file_a line_a] == $eof } {
488 set end_1 1
489 break
492 while { [string length $line_b] == 0 || [string match "#*" $line_b] } {
493 if [ string match "#pass" $line_b ] {
494 set end_2 1
495 set diff_pass 1
496 break
498 if { [gets $file_b line_b] == $eof } {
499 set end_2 1
500 break
504 if { $diff_pass } {
505 break
506 } elseif { $end_1 && $end_2 } {
507 break
508 } elseif { $end_1 } {
509 send_log "extra regexps in $file_2 starting with \"^$line_b$\"\nEOF from $file_1\n"
510 verbose "extra regexps in $file_2 starting with \"^$line_b$\"\nEOF from $file_1" 3
511 set differences 1
512 break
513 } elseif { $end_2 } {
514 send_log "extra lines in $file_1 starting with \"^$line_a$\"\nEOF from $file_2\n"
515 verbose "extra lines in $file_1 starting with \"^$line_a$\"\nEOF from $file_2\n" 3
516 set differences 1
517 break
518 } else {
519 verbose "regexp \"^$line_b$\"\nline \"$line_a\"" 3
520 if ![regexp "^$line_b$" "$line_a"] {
521 send_log "regexp_diff match failure\n"
522 send_log "regexp \"^$line_b$\"\nline \"$line_a\"\n"
523 set differences 1
524 break
529 if { $differences == 0 && !$diff_pass && [eof $file_a] != [eof $file_b] } {
530 send_log "$file_1 and $file_2 are different lengths\n"
531 verbose "$file_1 and $file_2 are different lengths" 3
532 set differences 1
535 close $file_a
536 close $file_b
538 return $differences
541 proc file_contents { filename } {
542 set file [open $filename r]
543 set contents [read $file]
544 close $file
545 return $contents
548 proc verbose_eval { expr { level 1 } } {
549 global verbose
550 if $verbose>$level then { eval verbose "$expr" $level }
553 # This definition is taken from an unreleased version of DejaGnu. Once
554 # that version gets released, and has been out in the world for a few
555 # months at least, it may be safe to delete this copy.
556 if ![string length [info proc prune_warnings]] {
558 # prune_warnings -- delete various system verbosities from TEXT.
560 # An example is:
561 # ld.so: warning: /usr/lib/libc.so.1.8.1 has older revision than expected 9
563 # Sites with particular verbose os's may wish to override this in site.exp.
565 proc prune_warnings { text } {
566 # This is from sun4's. Do it for all machines for now.
567 # The "\\1" is to try to preserve a "\n" but only if necessary.
568 regsub -all "(^|\n)(ld.so: warning:\[^\n\]*\n?)+" $text "\\1" text
570 # It might be tempting to get carried away and delete blank lines, etc.
571 # Just delete *exactly* what we're ask to, and that's it.
572 return $text