This commit was generated by cvs2svn to track changes on a CVS vendor
[binutils-gdb.git] / gas / testsuite / lib / gas-defs.exp
1 # Copyright (C) 1993, 1994 Free Software Foundation, Inc.
2
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.
12 #
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. */
16
17 # Please email any bugs, comments, and/or additions to this file to:
18 # DejaGnu@cygnus.com
19
20 # This file was written by Ken Raeburn (raeburn@cygnus.com).
21
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"
30 }
31 clone_output "[which $AS] $number\n"
32 unset version
33 }
34
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
42
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_system_crud $host_triplet $comp_output]
46 verbose "output was $comp_output"
47 return [list $comp_output ""];
48 }
49
50 proc all_ones { args } {
51 foreach x $args { if [expr $x!=1] { return 0 } }
52 return 1
53 }
54
55 proc gas_start { prog as_opts } {
56 global AS
57 global ASFLAGS
58 global srcdir
59 global subdir
60 global spawn_id
61
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"
68 }
69 }
70
71 proc gas_finish { } {
72 global spawn_id
73
74 catch "close"
75 catch "wait"
76 }
77
78 proc want_no_output { testname } {
79 global comp_output
80
81 if ![string match "" $comp_output] then {
82 send_log "$comp_output\n"
83 verbose "$comp_output" 3
84 }
85 if [string match "" $comp_output] then {
86 pass "$testname"
87 return 1
88 } else {
89 fail "$testname"
90 return 0
91 }
92 }
93
94 proc gas_test_old { file as_opts testname } {
95 gas_run $file $as_opts ""
96 return [want_no_output $testname]
97 }
98
99 proc gas_test { file as_opts var_opts testname } {
100 global comp_output
101
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
107 }
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"
118 }
119 }
120 }
121 set extra_opts [string trim $extra_opts]
122 gas_run $file "$as_opts $extra_opts" $maybe_ignore_stdout
123
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)"
129 }
130 }
131 if [info exists errorInfo] then {
132 unset errorInfo
133 }
134 }
135
136 proc gas_test_ignore_stdout { file as_opts testname } {
137 global comp_output
138
139 gas_run $file $as_opts ">/dev/null"
140 want_no_output $testname
141 }
142
143 proc gas_test_error { file as_opts testname } {
144 global comp_output
145
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
150 }
151 if [string match "" $comp_output] then {
152 fail "$testname"
153 } else {
154 pass "$testname"
155 }
156 }
157
158 proc gas_exit {} {}
159
160 proc gas_init {} {
161 global target_cpu
162 global target_cpu_family
163 global target_family
164 global target_vendor
165 global target_os
166 global stdoptlist
167
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 }
172 }
173
174 set target_family "$target_cpu_family-$target_vendor-$target_os"
175 set stdoptlist "-a>"
176 # Need to return an empty string.
177 return
178 }
179
180 # This proc requires two input files -- the .s file containing the
181 # assembly source, and a .d file containing the expected output from
182 # objdump or nm or whatever, and leading comments indicating any options
183 # to be passed to the assembler or dump program.
184
185 proc run_dump_test { name } {
186 global subdir srcdir
187 global OBJDUMP NM AS OBJCOPY
188 global OBJDUMPFLAGS NMFLAGS ASFLAGS OBJCOPYFLAGS
189 global host_triplet
190
191 if [string match "*/*" $name] {
192 set file $name
193 set name [file tail $name]
194 } else {
195 set file "$srcdir/$subdir/$name"
196 }
197 set opt_array [slurp_options "${file}.d"]
198 if { $opt_array == -1 } {
199 unresolved $subdir/$name
200 return
201 }
202 set opts(as) {}
203 set opts(objdump) {}
204 set opts(nm) {}
205 set opts(objcopy) {}
206 set opts(name) {}
207 set opts(PROG) {}
208 set opts(source) {}
209
210 foreach i $opt_array {
211 set opt_name [lindex $i 0]
212 set opt_val [lindex $i 1]
213 if ![info exists opts($opt_name)] {
214 perror "unknown option $opt_name in file $file.d"
215 unresolved $subdir/$name
216 return
217 }
218 if [string length $opts($opt_name)] {
219 perror "option $opt_name multiply set in $file.d"
220 unresolved $subdir/$name
221 return
222 }
223 set opts($opt_name) $opt_val
224 }
225
226 if {$opts(PROG) != ""} {
227 switch -- $opts(PROG) {
228 objdump
229 { set program objdump }
230 nm
231 { set program nm }
232 objcopy
233 { set program objcopy }
234 default
235 { perror "unrecognized program option $opts(PROG) in $file.d"
236 unresolved $subdir/$name
237 return }
238 }
239 } elseif {$opts(objdump) == "" && $opts(nm) != ""} {
240 set program nm
241 } elseif {$opts(objdump) != "" && $opts(nm) == ""} {
242 set program objdump
243 } elseif {$opts(objcopy) != ""} {
244 set program objcopy
245 } else {
246 perror "dump program unspecified in $file.d"
247 unresolved $subdir/$name
248 return
249 }
250 set progopts1 $opts($program)
251 eval set progopts \$[string toupper $program]FLAGS
252 eval set binary \$[string toupper $program]
253 if { $opts(name) == "" } {
254 set testname "$subdir/$name"
255 } else {
256 set testname $opts(name)
257 }
258
259 if { $opts(source) == "" } {
260 set sourcefile ${file}.s
261 } else {
262 set sourcefile $srcdir/$subdir/$opts(source)
263 }
264
265 send_log "$AS $ASFLAGS $opts(as) -o dump.o $sourcefile\n"
266 catch "exec $srcdir/lib/run $AS $ASFLAGS $opts(as) -o dump.o $sourcefile" comp_output
267 set comp_output [prune_system_crud $host_triplet $comp_output]
268
269 if ![string match "" $comp_output] then {
270 send_log "$comp_output\n"
271 verbose "$comp_output" 3
272 fail $testname
273 return
274 }
275
276 if { [which $binary] == 0 } {
277 untested $testname
278 return
279 }
280
281 if { $progopts1 == "" } { set $progopts1 "-r" }
282 verbose "running $binary $progopts $progopts1" 3
283 if { $program == "objcopy" } {
284 send_log "$binary $progopts $progopts1 dump.o dump.out\n"
285 catch "exec $binary $progopts $progopts1 dump.o dump.out" comp_output
286 set comp_output [prune_system_crud $host_triplet $comp_output]
287 if ![string match "" $comp_output] then {
288 send_log "$comp_output\n"
289 fail $testname
290 return
291 }
292 } else {
293 send_log "$binary $progopts $progopts1 dump.o > dump.out\n"
294 catch "exec $binary $progopts $progopts1 dump.o > dump.out" comp_output
295 set comp_output [prune_system_crud $host_triplet $comp_output]
296 if ![string match "" $comp_output] then {
297 send_log "$comp_output\n"
298 fail $testname
299 return
300 }
301 }
302
303 verbose_eval {[file_contents "dump.out"]} 3
304 if { [regexp_diff "dump.out" "${file}.d"] } then {
305 fail $testname
306 verbose "output is [file_contents "dump.out"]" 2
307 return
308 }
309
310 pass $testname
311 }
312
313 proc slurp_options { file } {
314 if [catch { set f [open $file r] } x] {
315 #perror "couldn't open `$file': $x"
316 perror "$x"
317 return -1
318 }
319 set opt_array {}
320 # whitespace expression
321 set ws {[ ]*}
322 set nws {[^ ]*}
323 # whitespace is ignored anywhere except within the options list;
324 # option names are alphabetic only
325 set pat "^#${ws}(\[a-zA-Z\]*)$ws:${ws}(.*)$ws\$"
326 while { [gets $f line] != -1 } {
327 set line [string trim $line]
328 # Whitespace here is space-tab.
329 if [regexp $pat $line xxx opt_name opt_val] {
330 # match!
331 lappend opt_array [list $opt_name $opt_val]
332 } else {
333 break
334 }
335 }
336 close $f
337 return $opt_array
338 }
339
340 proc objdump { opts } {
341 global OBJDUMP
342 global comp_output
343 global host_triplet
344
345 catch "exec $OBJDUMP $opts" comp_output
346 set comp_output [prune_system_crud $host_triplet $comp_output]
347 verbose "objdump output=$comp_output\n" 3
348 }
349
350 proc objdump_start_no_subdir { prog opts } {
351 global OBJDUMP
352 global srcdir
353 global spawn_id
354
355 verbose "Starting $OBJDUMP $opts $prog" 2
356 catch {
357 spawn -noecho -nottyinit $srcdir/lib/run $OBJDUMP $opts $prog
358 } foo
359 if ![regexp {^[0-9]+} $foo] then {
360 perror "Can't run $prog: $foo"
361 }
362 }
363
364 proc objdump_finish { } {
365 global spawn_id
366
367 catch "close"
368 catch "wait"
369 }
370
371 # Default timeout is 10 seconds, loses on a slow machine. But some
372 # configurations of dejagnu may override it.
373 if {$timeout<120} then { set timeout 120 }
374
375 expect_after -i {
376 timeout { perror "timeout" }
377 "virtual memory exhausted" { perror "virtual memory exhausted" }
378 buffer_full { perror "buffer full" }
379 eof { perror "eof" }
380 }
381
382 # regexp_diff, based on simple_diff taken from ld test suite
383 # compares two files line-by-line
384 # file1 contains strings, file2 contains regexps and #-comments
385 # blank lines are ignored in either file
386 # returns non-zero if differences exist
387 #
388 proc regexp_diff { file_1 file_2 } {
389
390 set eof -1
391 set end 0
392 set differences 0
393 set diff_pass 0
394
395 if [file exists $file_1] then {
396 set file_a [open $file_1 r]
397 } else {
398 warning "$file_1 doesn't exist"
399 return 1
400 }
401
402 if [file exists $file_2] then {
403 set file_b [open $file_2 r]
404 } else {
405 fail "$file_2 doesn't exist"
406 close $file_a
407 return 1
408 }
409
410 verbose " Regexp-diff'ing: $file_1 $file_2" 2
411
412 while { $differences == 0 && $end == 0 } {
413 set line_a ""
414 set line_b ""
415 while { [string length $line_a] == 0 } {
416 if { [gets $file_a line_a] == $eof } {
417 set end 1
418 break
419 }
420 }
421 while { [string length $line_b] == 0 || [string match "#*" $line_b] } {
422 if [ string match "#pass" $line_b ] {
423 set end 1
424 set diff_pass 1
425 break
426 }
427 if { [gets $file_b line_b] == $eof } {
428 set end 1
429 break
430 }
431 }
432 if { $end } { break }
433 verbose "regexp \"^$line_b$\"\nline \"$line_a\"" 3
434 if ![regexp "^$line_b$" "$line_a"] {
435 send_log "regexp_diff match failure\n"
436 send_log "regexp \"^$line_b$\"\nline \"$line_a\"\n"
437 set differences 1
438 }
439 }
440
441 if { $differences == 0 && !$diff_pass && [eof $file_a] != [eof $file_b] } {
442 send_log "different lengths\n"
443 verbose "different lengths" 3
444 set differences 1
445 }
446
447 close $file_a
448 close $file_b
449
450 return $differences
451 }
452
453 proc file_contents { filename } {
454 set file [open $filename r]
455 set contents [read $file]
456 close $file
457 return $contents
458 }
459
460 proc verbose_eval { expr { level 1 } } {
461 global verbose
462 if $verbose>$level then { eval verbose "$expr" $level }
463 }
464
465 # This definition is taken from an unreleased version of DejaGnu. Once
466 # that version gets released, and has been out in the world for a few
467 # months at least, it may be safe to delete this copy.
468 if ![string length [info proc prune_system_crud]] {
469 #
470 # prune_system_crud -- delete various system verbosities from TEXT on SYSTEM
471 #
472 # An example is:
473 # ld.so: warning: /usr/lib/libc.so.1.8.1 has older revision than expected 9
474 #
475 # SYSTEM is typical $target_triplet or $host_triplet.
476 #
477 # This is useful when trying to do pattern matches on program output.
478 # Sites with particular verbose os's may wish to override this in site.exp.
479 #
480 proc prune_system_crud { system text } {
481 # This is from sun4's. Do it for all machines for now.
482 # The "\\1" is to try to preserve a "\n" but only if necessary.
483 regsub -all "(^|\n)(ld.so: warning:\[^\n\]*\n?)+" $text "\\1" text
484
485 # It might be tempting to get carried away and delete blank lines, etc.
486 # Just delete *exactly* what we're ask to, and that's it.
487 return $text
488 }
489 }