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