2003-10-08 Dave Brolley <brolley@redhat.com>
[deliverable/binutils-gdb.git] / gas / testsuite / lib / gas-defs.exp
CommitLineData
139e4a70
AM
1# Copyright (C) 1993, 1994, 1997, 1998, 1999, 2000, 2001, 2002
2# Free Software Foundation, Inc.
252b5132
RH
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 2 of the License, or
7# (at your option) any later version.
139e4a70 8#
252b5132
RH
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.
139e4a70 13#
252b5132
RH
14# You should have received a copy of the GNU General Public License
15# along with this program; if not, write to the Free Software
139e4a70 16# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
252b5132
RH
17
18# Please email any bugs, comments, and/or additions to this file to:
fc697c14 19# dejagnu@gnu.org
252b5132
RH
20
21# This file was written by Ken Raeburn (raeburn@cygnus.com).
22
23proc gas_version {} {
24 global AS
25 catch "exec $AS -version < /dev/null" tmp
26 # Should find a way to discard constant parts, keep whatever's
27 # left, so the version string could be almost anything at all...
28 regexp "\[^\n\]* (cygnus-|)(\[-0-9.a-zA-Z-\]+)\[\r\n\].*" $tmp version cyg number
29 if ![info exists number] then {
30 return "[which $AS] (no version number)\n"
31 }
32 clone_output "[which $AS] $number\n"
33 unset version
34}
35
36proc gas_run { prog as_opts redir } {
37 global AS
38 global ASFLAGS
39 global comp_output
40 global srcdir
41 global subdir
42 global host_triplet
43
44 verbose "Executing $srcdir/lib/run $AS $ASFLAGS $as_opts $srcdir/$subdir/$prog $redir"
45 catch "exec $srcdir/lib/run $AS $ASFLAGS $as_opts $srcdir/$subdir/$prog $redir" comp_output
46 set comp_output [prune_warnings $comp_output]
47 verbose "output was $comp_output"
48 return [list $comp_output ""];
49}
50
51proc all_ones { args } {
52 foreach x $args { if [expr $x!=1] { return 0 } }
53 return 1
54}
55
56proc gas_start { prog as_opts } {
57 global AS
58 global ASFLAGS
59 global srcdir
60 global subdir
61 global spawn_id
62
63 verbose "Starting $AS $ASFLAGS $as_opts $prog" 2
64 catch {
65 spawn -noecho -nottycopy $srcdir/lib/run $AS $ASFLAGS $as_opts $srcdir/$subdir/$prog
66 } foo
67 if ![regexp {^[0-9]+} $foo] then {
68 perror "Can't run $subdir/$prog: $foo"
69 }
70}
71
72proc gas_finish { } {
73 global spawn_id
74
75 catch "close"
76 catch "wait"
77}
78
79proc want_no_output { testname } {
80 global comp_output
81
82 if ![string match "" $comp_output] then {
83 send_log "$comp_output\n"
84 verbose "$comp_output" 3
85 }
86 if [string match "" $comp_output] then {
87 pass "$testname"
88 return 1
89 } else {
90 fail "$testname"
91 return 0
92 }
93}
94
95proc gas_test_old { file as_opts testname } {
96 gas_run $file $as_opts ""
97 return [want_no_output $testname]
98}
99
100proc gas_test { file as_opts var_opts testname } {
101 global comp_output
102
103 set i 0
104 foreach word $var_opts {
105 set ignore_stdout($i) [string match "*>" $word]
106 set opt($i) [string trim $word {>}]
107 incr i
108 }
109 set max [expr 1<<$i]
110 for {set i 0} {[expr $i<$max]} {incr i} {
111 set maybe_ignore_stdout ""
112 set extra_opts ""
113 for {set bit 0} {(1<<$bit)<$max} {incr bit} {
114 set num [expr 1<<$bit]
115 if [expr $i&$num] then {
116 set extra_opts "$extra_opts $opt($bit)"
117 if $ignore_stdout($bit) then {
118 set maybe_ignore_stdout ">/dev/null"
119 }
120 }
121 }
122 set extra_opts [string trim $extra_opts]
123 gas_run $file "$as_opts $extra_opts" $maybe_ignore_stdout
124
125 # Should I be able to use a conditional expression here?
126 if [string match "" $extra_opts] then {
127 want_no_output $testname
128 } else {
129 want_no_output "$testname ($extra_opts)"
130 }
131 }
132 if [info exists errorInfo] then {
133 unset errorInfo
134 }
135}
136
137proc gas_test_ignore_stdout { file as_opts testname } {
138 global comp_output
139
140 gas_run $file $as_opts ">/dev/null"
141 want_no_output $testname
142}
143
144proc gas_test_error { file as_opts testname } {
145 global comp_output
146
147 gas_run $file $as_opts ">/dev/null"
148 if ![string match "" $comp_output] then {
149 send_log "$comp_output\n"
150 verbose "$comp_output" 3
151 }
152 if [string match "" $comp_output] then {
153 fail "$testname"
154 } else {
155 pass "$testname"
156 }
157}
158
159proc gas_exit {} {}
160
161proc gas_init { args } {
162 global target_cpu
163 global target_cpu_family
164 global target_family
165 global target_vendor
166 global target_os
167 global stdoptlist
168
169 case "$target_cpu" in {
170 "m68???" { set target_cpu_family m68k }
80c7c40a 171 "i[3-7]86" { set target_cpu_family i386 }
252b5132
RH
172 default { set target_cpu_family $target_cpu }
173 }
174
175 set target_family "$target_cpu_family-$target_vendor-$target_os"
176 set stdoptlist "-a>"
177
178 if ![istarget "*-*-*"] {
179 perror "Target name [istarget] is not a triple."
180 }
181 # Need to return an empty string.
182 return
183}
184
d04428bd
AM
185#
186# is_elf_format
187# true if the object format is known to be ELF
188#
189proc is_elf_format {} {
190 if { ![istarget *-*-sysv4*] \
191 && ![istarget *-*-unixware*] \
192 && ![istarget *-*-elf*] \
193 && ![istarget *-*-eabi*] \
194 && ![istarget hppa*64*-*-hpux*] \
195 && ![istarget *-*-linux*] \
196 && ![istarget *-*-irix5*] \
197 && ![istarget *-*-irix6*] \
198 && ![istarget *-*-netbsd*] \
199 && ![istarget *-*-solaris2*] } {
200 return 0
201 }
202
203 if { [istarget *-*-linux*aout*] \
204 || [istarget *-*-linux*oldld*] } {
205 return 0
206 }
207
208 if { ![istarget *-*-netbsdelf*] \
209 && ([istarget *-*-netbsd*aout*] \
210 || [istarget *-*-netbsdpe*] \
211 || [istarget arm*-*-netbsd*] \
212 || [istarget sparc-*-netbsd*] \
213 || [istarget i*86-*-netbsd*] \
214 || [istarget m68*-*-netbsd*] \
215 || [istarget vax-*-netbsd*] \
216 || [istarget ns32k-*-netbsd*]) } {
217 return 0
218 }
219 return 1
220}
221
252b5132 222
e8119602 223# run_dump_test FILE (optional:) EXTRA_OPTIONS
252b5132
RH
224#
225# Assemble a .s file, then run some utility on it and check the output.
139e4a70 226#
252b5132
RH
227# There should be an assembly language file named FILE.s in the test
228# suite directory, and a pattern file called FILE.d. `run_dump_test'
229# will assemble FILE.s, run some tool like `objdump', `objcopy', or
230# `nm' on the .o file to produce textual output, and then analyze that
231# with regexps. The FILE.d file specifies what program to run, and
232# what to expect in its output.
233#
234# The FILE.d file begins with zero or more option lines, which specify
235# flags to pass to the assembler, the program to run to dump the
236# assembler's output, and the options it wants. The option lines have
237# the syntax:
139e4a70 238#
252b5132 239# # OPTION: VALUE
139e4a70 240#
252b5132
RH
241# OPTION is the name of some option, like "name" or "objdump", and
242# VALUE is OPTION's value. The valid options are described below.
243# Whitespace is ignored everywhere, except within VALUE. The option
244# list ends with the first line that doesn't match the above syntax
245# (hmm, not great for error detection).
246#
e8119602
CD
247# The optional EXTRA_OPTIONS argument to `run_dump_test' is a list of
248# two-element lists. The first element of each is an option name, and
249# the second additional arguments to be added on to the end of the
250# option list as given in FILE.d. (If omitted, no additional options
251# are added.)
252#
252b5132 253# The interesting options are:
139e4a70 254#
252b5132
RH
255# name: TEST-NAME
256# The name of this test, passed to DejaGNU's `pass' and `fail'
257# commands. If omitted, this defaults to FILE, the root of the
258# .s and .d files' names.
139e4a70 259#
252b5132
RH
260# as: FLAGS
261# When assembling FILE.s, pass FLAGS to the assembler.
139e4a70 262#
252b5132
RH
263# PROG: PROGRAM-NAME
264# The name of the program to run to analyze the .o file produced
265# by the assembler. This can be omitted; run_dump_test will guess
266# which program to run by seeing which of the flags options below
267# is present.
139e4a70 268#
252b5132
RH
269# objdump: FLAGS
270# nm: FLAGS
271# objcopy: FLAGS
272# Use the specified program to analyze the .o file, and pass it
139e4a70
AM
273# FLAGS, in addition to the .o file name. Note that they are run
274# with LC_ALL=C in the environment to give consistent sorting
275# of symbols.
252b5132
RH
276#
277# source: SOURCE
278# Assemble the file SOURCE.s. If omitted, this defaults to FILE.s.
279# This is useful if several .d files want to share a .s file.
280#
8c2784a5
TR
281# error: REGEX
282# An error with message matching REGEX must be emitted for the test
283# to pass. The PROG, objdump, nm and objcopy options have no
284# meaning and need not supplied if this is present.
285#
252b5132
RH
286# Each option may occur at most once.
287#
288# After the option lines come regexp lines. `run_dump_test' calls
289# `regexp_diff' to compare the output of the dumping tool against the
290# regexps in FILE.d. `regexp_diff' is defined later in this file; see
291# further comments there.
292
e8119602 293proc run_dump_test { name {extra_options {}} } {
252b5132 294 global subdir srcdir
4b0d96c2
HPN
295 global OBJDUMP NM AS OBJCOPY READELF
296 global OBJDUMPFLAGS NMFLAGS ASFLAGS OBJCOPYFLAGS READELFFLAGS
252b5132 297 global host_triplet
139e4a70 298 global env
252b5132
RH
299
300 if [string match "*/*" $name] {
301 set file $name
302 set name [file tail $name]
303 } else {
304 set file "$srcdir/$subdir/$name"
305 }
306 set opt_array [slurp_options "${file}.d"]
307 if { $opt_array == -1 } {
61feeec2 308 perror "error reading options from $file.d"
252b5132
RH
309 unresolved $subdir/$name
310 return
311 }
312 set opts(as) {}
313 set opts(objdump) {}
314 set opts(nm) {}
315 set opts(objcopy) {}
4b0d96c2 316 set opts(readelf) {}
252b5132
RH
317 set opts(name) {}
318 set opts(PROG) {}
319 set opts(source) {}
ff970196 320 set opts(stderr) {}
8c2784a5 321 set opts(error) {}
252b5132
RH
322
323 foreach i $opt_array {
324 set opt_name [lindex $i 0]
325 set opt_val [lindex $i 1]
326 if ![info exists opts($opt_name)] {
327 perror "unknown option $opt_name in file $file.d"
328 unresolved $subdir/$name
329 return
330 }
331 if [string length $opts($opt_name)] {
332 perror "option $opt_name multiply set in $file.d"
333 unresolved $subdir/$name
334 return
335 }
336 set opts($opt_name) $opt_val
337 }
338
e8119602
CD
339 foreach i $extra_options {
340 set opt_name [lindex $i 0]
341 set opt_val [lindex $i 1]
342 if ![info exists opts($opt_name)] {
343 perror "unknown option $opt_name given in extra_opts"
344 unresolved $subdir/$name
345 return
346 }
347 # add extra option to end of existing option, adding space
348 # if necessary.
349 if [string length $opts($opt_name)] {
350 append opts($opt_name) " "
351 }
352 append opts($opt_name) $opt_val
353 }
354
252b5132
RH
355 if {$opts(PROG) != ""} {
356 switch -- $opts(PROG) {
357 objdump
358 { set program objdump }
359 nm
360 { set program nm }
361 objcopy
362 { set program objcopy }
4b0d96c2
HPN
363 readelf
364 { set program readelf }
252b5132
RH
365 default
366 { perror "unrecognized program option $opts(PROG) in $file.d"
367 unresolved $subdir/$name
368 return }
369 }
8c2784a5
TR
370 } elseif { $opts(error) != "" } {
371 # It's meaningless to require an output-testing method when we
372 # expect an error. For simplicity, we fake an arbitrary method.
373 set program "nm"
252b5132
RH
374 } else {
375 # Guess which program to run, by seeing which option was specified.
376 set program ""
4b0d96c2 377 foreach p {objdump objcopy nm readelf} {
252b5132
RH
378 if {$opts($p) != ""} {
379 if {$program != ""} {
380 perror "ambiguous dump program in $file.d"
381 unresolved $subdir/$name
382 return
383 } else {
384 set program $p
385 }
386 }
387 }
388 if {$program == ""} {
389 perror "dump program unspecified in $file.d"
390 unresolved $subdir/$name
391 return
392 }
393 }
394
395 set progopts1 $opts($program)
396 eval set progopts \$[string toupper $program]FLAGS
397 eval set binary \$[string toupper $program]
398 if { $opts(name) == "" } {
399 set testname "$subdir/$name"
400 } else {
401 set testname $opts(name)
402 }
403
404 if { $opts(source) == "" } {
405 set sourcefile ${file}.s
406 } else {
407 set sourcefile $srcdir/$subdir/$opts(source)
408 }
409
410 send_log "$AS $ASFLAGS $opts(as) -o dump.o $sourcefile\n"
411 catch "exec $srcdir/lib/run $AS $ASFLAGS $opts(as) -o dump.o $sourcefile" comp_output
412 set comp_output [prune_warnings $comp_output]
413
30c378fd 414 if { ![string match "" $comp_output] || $opts(stderr) != "" } then {
ff970196
CD
415 if { $opts(stderr) == "" } then {
416 send_log "$comp_output\n"
417 verbose "$comp_output" 3
8c2784a5
TR
418
419 if { $opts(error) != "" } {
420 verbose -log "failed with: <$comp_output>, expected: <$opts(error)>"
421 if [regexp $opts(error) $comp_output] {
422 pass $testname
423 return
424 }
425 }
ff970196
CD
426 fail $testname
427 return
428 } else {
429 catch {write_file dump.stderr "$comp_output"} write_output
430 if ![string match "" $write_output] then {
431 send_log "error writing dump.stderr: $write_output\n"
432 verbose "error writing dump.stderr: $write_output" 3
433 send_log "$comp_output\n"
434 verbose "$comp_output" 3
435 fail $testname
436 return
437 }
438 set stderrfile $srcdir/$subdir/$opts(stderr)
439 send_log "wrote pruned stderr to dump.stderr\n"
440 verbose "wrote pruned stderr to dump.stderr" 3
441 if { [regexp_diff "dump.stderr" "$stderrfile"] } then {
8c2784a5
TR
442 if { $opts(error) != "" } {
443 verbose -log "failed with: <$comp_output>, expected: <$opts(error)>"
444 if [regexp $opts(error) $comp_output] {
445 pass $testname
446 return
447 }
448 }
ff970196
CD
449 fail $testname
450 verbose "pruned stderr is [file_contents "dump.stderr"]" 2
451 return
452 }
453 }
252b5132
RH
454 }
455
456 if { [which $binary] == 0 } {
457 untested $testname
458 return
459 }
460
461 if { $progopts1 == "" } { set $progopts1 "-r" }
462 verbose "running $binary $progopts $progopts1" 3
463
464 # Objcopy, unlike the other two, won't send its output to stdout,
465 # so we have to run it specially.
139e4a70 466 set cmd "$binary $progopts $progopts1 dump.o > dump.out"
252b5132 467 if { $program == "objcopy" } {
139e4a70
AM
468 set cmd "$binary $progopts $progopts1 dump.o dump.out"
469 }
470
471 # Ensure consistent sorting of symbols
472 if {[info exists env(LC_ALL)]} {
473 set old_lc_all $env(LC_ALL)
474 }
475 set env(LC_ALL) "C"
476 send_log "$cmd\n"
477 catch "exec $cmd" comp_output
478 if {[info exists old_lc_all]} {
479 set env(LC_ALL) $old_lc_all
252b5132 480 } else {
139e4a70
AM
481 unset env(LC_ALL)
482 }
483 set comp_output [prune_warnings $comp_output]
484 if ![string match "" $comp_output] then {
485 send_log "$comp_output\n"
486 fail $testname
487 return
252b5132
RH
488 }
489
490 verbose_eval {[file_contents "dump.out"]} 3
491 if { [regexp_diff "dump.out" "${file}.d"] } then {
492 fail $testname
493 verbose "output is [file_contents "dump.out"]" 2
494 return
495 }
496
497 pass $testname
498}
499
500proc slurp_options { file } {
501 if [catch { set f [open $file r] } x] {
502 #perror "couldn't open `$file': $x"
503 perror "$x"
504 return -1
505 }
506 set opt_array {}
507 # whitespace expression
508 set ws {[ ]*}
509 set nws {[^ ]*}
510 # whitespace is ignored anywhere except within the options list;
511 # option names are alphabetic only
512 set pat "^#${ws}(\[a-zA-Z\]*)$ws:${ws}(.*)$ws\$"
513 while { [gets $f line] != -1 } {
514 set line [string trim $line]
515 # Whitespace here is space-tab.
516 if [regexp $pat $line xxx opt_name opt_val] {
517 # match!
518 lappend opt_array [list $opt_name $opt_val]
519 } else {
520 break
521 }
522 }
523 close $f
524 return $opt_array
525}
526
527proc objdump { opts } {
528 global OBJDUMP
529 global comp_output
530 global host_triplet
531
532 catch "exec $OBJDUMP $opts" comp_output
533 set comp_output [prune_warnings $comp_output]
534 verbose "objdump output=$comp_output\n" 3
535}
536
537proc objdump_start_no_subdir { prog opts } {
538 global OBJDUMP
539 global srcdir
540 global spawn_id
541
542 verbose "Starting $OBJDUMP $opts $prog" 2
543 catch {
544 spawn -noecho -nottyinit $srcdir/lib/run $OBJDUMP $opts $prog
545 } foo
546 if ![regexp {^[0-9]+} $foo] then {
547 perror "Can't run $prog: $foo"
548 }
549}
550
551proc objdump_finish { } {
552 global spawn_id
553
554 catch "close"
555 catch "wait"
556}
557
558# Default timeout is 10 seconds, loses on a slow machine. But some
559# configurations of dejagnu may override it.
560if {$timeout<120} then { set timeout 120 }
561
562expect_after -i {
563 timeout { perror "timeout" }
564 "virtual memory exhausted" { perror "virtual memory exhausted" }
565 buffer_full { perror "buffer full" }
566 eof { perror "eof" }
567}
568
569# regexp_diff, based on simple_diff taken from ld test suite
570# compares two files line-by-line
571# file1 contains strings, file2 contains regexps and #-comments
572# blank lines are ignored in either file
573# returns non-zero if differences exist
574#
575proc regexp_diff { file_1 file_2 } {
576
577 set eof -1
578 set end_1 0
579 set end_2 0
580 set differences 0
581 set diff_pass 0
582
583 if [file exists $file_1] then {
584 set file_a [open $file_1 r]
585 } else {
586 warning "$file_1 doesn't exist"
587 return 1
588 }
589
590 if [file exists $file_2] then {
591 set file_b [open $file_2 r]
592 } else {
593 fail "$file_2 doesn't exist"
594 close $file_a
595 return 1
596 }
597
598 verbose " Regexp-diff'ing: $file_1 $file_2" 2
599
600 while { 1 } {
601 set line_a ""
602 set line_b ""
603 while { [string length $line_a] == 0 } {
604 if { [gets $file_a line_a] == $eof } {
605 set end_1 1
606 break
607 }
608 }
609 while { [string length $line_b] == 0 || [string match "#*" $line_b] } {
610 if [ string match "#pass" $line_b ] {
611 set end_2 1
612 set diff_pass 1
613 break
a6ea59ce
GK
614 } elseif [ string match "#..." $line_b ] {
615 if { [gets $file_b line_b] == $eof } {
616 set end_2 1
617 break
618 }
619 verbose "looking for \"^$line_b$\"" 3
620 while { ![regexp "^$line_b$" "$line_a"] } {
621 verbose "skipping \"$line_a\"" 3
622 if { [gets $file_a line_a] == $eof } {
623 set end_1 1
624 break
625 }
626 }
627 break
252b5132
RH
628 }
629 if { [gets $file_b line_b] == $eof } {
630 set end_2 1
631 break
632 }
633 }
634
139e4a70
AM
635 if { $diff_pass } {
636 break
637 } elseif { $end_1 && $end_2 } {
252b5132
RH
638 break
639 } elseif { $end_1 } {
640 send_log "extra regexps in $file_2 starting with \"^$line_b$\"\nEOF from $file_1\n"
641 verbose "extra regexps in $file_2 starting with \"^$line_b$\"\nEOF from $file_1" 3
642 set differences 1
643 break
644 } elseif { $end_2 } {
645 send_log "extra lines in $file_1 starting with \"^$line_a$\"\nEOF from $file_2\n"
646 verbose "extra lines in $file_1 starting with \"^$line_a$\"\nEOF from $file_2\n" 3
647 set differences 1
648 break
649 } else {
650 verbose "regexp \"^$line_b$\"\nline \"$line_a\"" 3
651 if ![regexp "^$line_b$" "$line_a"] {
652 send_log "regexp_diff match failure\n"
653 send_log "regexp \"^$line_b$\"\nline \"$line_a\"\n"
3ad62fc4 654 verbose "regexp_diff match failure\n" 3
252b5132 655 set differences 1
252b5132
RH
656 }
657 }
658 }
659
660 if { $differences == 0 && !$diff_pass && [eof $file_a] != [eof $file_b] } {
661 send_log "$file_1 and $file_2 are different lengths\n"
662 verbose "$file_1 and $file_2 are different lengths" 3
663 set differences 1
664 }
665
666 close $file_a
667 close $file_b
668
669 return $differences
670}
671
672proc file_contents { filename } {
673 set file [open $filename r]
674 set contents [read $file]
675 close $file
676 return $contents
677}
678
ff970196
CD
679proc write_file { filename contents } {
680 set file [open $filename w]
681 puts $file "$contents"
682 close $file
683}
684
252b5132
RH
685proc verbose_eval { expr { level 1 } } {
686 global verbose
687 if $verbose>$level then { eval verbose "$expr" $level }
688}
689
690# This definition is taken from an unreleased version of DejaGnu. Once
691# that version gets released, and has been out in the world for a few
692# months at least, it may be safe to delete this copy.
693if ![string length [info proc prune_warnings]] {
694 #
695 # prune_warnings -- delete various system verbosities from TEXT.
696 #
697 # An example is:
698 # ld.so: warning: /usr/lib/libc.so.1.8.1 has older revision than expected 9
699 #
700 # Sites with particular verbose os's may wish to override this in site.exp.
701 #
702 proc prune_warnings { text } {
703 # This is from sun4's. Do it for all machines for now.
704 # The "\\1" is to try to preserve a "\n" but only if necessary.
705 regsub -all "(^|\n)(ld.so: warning:\[^\n\]*\n?)+" $text "\\1" text
706
707 # It might be tempting to get carried away and delete blank lines, etc.
708 # Just delete *exactly* what we're ask to, and that's it.
709 return $text
710 }
711}
This page took 0.208255 seconds and 4 git commands to generate.