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