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