* lib/gas-defs.exp (run_dump_test): If the program to run does not
[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 "version (cygnus-|)\[-0-9.a-zA-Z-\]+" $tmp version
28 set tmp $version
29 clone_output "[which $AS] $version\n"
30 unset tmp
31 unset version
32 }
33
34 proc gas_run { prog as_opts redir } {
35 global AS
36 global ASFLAGS
37 global comp_output
38 global srcdir
39 global subdir
40
41 verbose "Executing $AS $ASFLAGS $as_opts $prog $redir"
42 catch "exec $srcdir/lib/run $AS $ASFLAGS $as_opts $srcdir/$subdir/$prog $redir" comp_output
43 }
44
45 proc all_ones { args } {
46 foreach x $args { if [expr $x!=1] { return 0 } }
47 return 1
48 }
49
50 proc gas_start { prog as_opts } {
51 global AS
52 global ASFLAGS
53 global srcdir
54 global subdir
55 global spawn_id
56
57 verbose "Starting $AS $ASFLAGS $as_opts $prog" 2
58 catch {
59 spawn -noecho -nottycopy $srcdir/lib/run $AS $ASFLAGS $as_opts $srcdir/$subdir/$prog
60 } foo
61 if ![regexp {^[0-9]+} $foo] then {
62 perror "Can't run $subdir/$prog: $foo"
63 }
64 }
65
66 proc gas_finish { } {
67 global spawn_id
68
69 catch "close"
70 catch "wait"
71 }
72
73 proc want_no_output { testname } {
74 global comp_output
75
76 if ![string match "" $comp_output] then {
77 send_log "$comp_output\n"
78 verbose "$comp_output" 3
79 }
80 if [string match "" $comp_output] then {
81 pass "$testname"
82 return 1
83 } else {
84 fail "$testname"
85 return 0
86 }
87 }
88
89 proc gas_test_old { file as_opts testname } {
90 gas_run $file $as_opts ""
91 return [want_no_output $testname]
92 }
93
94 proc gas_test { file as_opts var_opts testname } {
95 global comp_output
96
97 set i 0
98 foreach word $var_opts {
99 set ignore_stdout($i) [string match "*>" $word]
100 set opt($i) [string trim $word {>}]
101 incr i
102 }
103 set max [expr 1<<$i]
104 for {set i 0} {[expr $i<$max]} {incr i} {
105 set maybe_ignore_stdout ""
106 set extra_opts ""
107 for {set bit 0} {(1<<$bit)<$max} {incr bit} {
108 set num [expr 1<<$bit]
109 if [expr $i&$num] then {
110 set extra_opts "$extra_opts $opt($bit)"
111 if $ignore_stdout($bit) then {
112 set maybe_ignore_stdout ">/dev/null"
113 }
114 }
115 }
116 set extra_opts [string trim $extra_opts]
117 gas_run $file "$as_opts $extra_opts" $maybe_ignore_stdout
118
119 # Should I be able to use a conditional expression here?
120 if [string match "" $extra_opts] then {
121 want_no_output $testname
122 } else {
123 want_no_output "$testname ($extra_opts)"
124 }
125 }
126 if [info exists errorInfo] then {
127 unset errorInfo
128 }
129 }
130
131 proc gas_test_ignore_stdout { file as_opts testname } {
132 global comp_output
133
134 gas_run $file $as_opts ">/dev/null"
135 want_no_output $testname
136 }
137
138 proc gas_test_error { file as_opts testname } {
139 global comp_output
140
141 gas_run $file $as_opts ">/dev/null"
142 if ![string match "" $comp_output] then {
143 send_log "$comp_output\n"
144 verbose "$comp_output" 3
145 }
146 if [string match "" $comp_output] then {
147 fail "$testname"
148 } else {
149 pass "$testname"
150 }
151 }
152
153 proc gas_exit {} {}
154
155 proc gas_init {} {
156 global target_cpu
157 global target_cpu_family
158 global target_family
159 global target_vendor
160 global target_os
161 global stdoptlist
162
163 case "$target_cpu" in {
164 "m68???" { set target_cpu_family m68k }
165 "i[34]86" { set target_cpu_family i386 }
166 default { set target_cpu_family $target_cpu }
167 }
168
169 set target_family "$target_cpu_family-$target_vendor-$target_os"
170 set stdoptlist "-a>"
171 # Need to return an empty string.
172 return
173 }
174
175 # This proc requires two input files -- the .s file containing the
176 # assembly source, and a .d file containing the expected output from
177 # objdump or nm or whatever, and leading comments indicating any options
178 # to be passed to the assembler or dump program.
179
180 proc run_dump_test { name } {
181 global subdir srcdir
182 global OBJDUMP NM AS OBJCOPY
183 global OBJDUMPFLAGS NMFLAGS ASFLAGS OBJCOPYFLAGS
184
185 if [string match "*/*" $name] {
186 set file $name
187 set name [file tail $name]
188 } else {
189 set file "$srcdir/$subdir/$name"
190 }
191 set opt_array [slurp_options "${file}.d"]
192 if { $opt_array == -1 } {
193 unresolved $subdir/$name
194 return
195 }
196 set opts(as) {}
197 set opts(objdump) {}
198 set opts(nm) {}
199 set opts(objcopy) {}
200 set opts(name) {}
201 set opts(PROG) {}
202 set opts(source) {}
203
204 foreach i $opt_array {
205 set opt_name [lindex $i 0]
206 set opt_val [lindex $i 1]
207 if ![info exists opts($opt_name)] {
208 perror "unknown option $opt_name in file $file.d"
209 unresolved $subdir/$name
210 return
211 }
212 if [string length $opts($opt_name)] {
213 perror "option $opt_name multiply set in $file.d"
214 unresolved $subdir/$name
215 return
216 }
217 set opts($opt_name) $opt_val
218 }
219
220 if {$opts(PROG) != ""} {
221 switch -- $opts(PROG) {
222 objdump
223 { set program objdump }
224 nm
225 { set program nm }
226 objcopy
227 { set program objcopy }
228 default
229 { perror "unrecognized program option $opts(PROG) in $file.d"
230 unresolved $subdir/$name
231 return }
232 }
233 } elseif {$opts(objdump) == "" && $opts(nm) != ""} {
234 set program nm
235 } elseif {$opts(objdump) != "" && $opts(nm) == ""} {
236 set program objdump
237 } elseif {$opts(objcopy) != ""} {
238 set program objcopy
239 } else {
240 perror "dump program unspecified in $file.d"
241 unresolved $subdir/$name
242 return
243 }
244 set progopts1 $opts($program)
245 eval set progopts \$[string toupper $program]FLAGS
246 eval set binary \$[string toupper $program]
247 if { $opts(name) == "" } {
248 set testname "$subdir/$name"
249 } else {
250 set testname $opts(name)
251 }
252
253 if { $opts(source) == "" } {
254 set sourcefile ${file}.s
255 } else {
256 set sourcefile $srcdir/$subdir/$opts(source)
257 }
258
259 send_log "$AS $ASFLAGS $opts(as) -o dump.o $sourcefile\n"
260 catch "exec $srcdir/lib/run $AS $ASFLAGS $opts(as) -o dump.o $sourcefile" comp_output
261
262 if ![string match "" $comp_output] then {
263 send_log "$comp_output\n"
264 verbose "$comp_output" 3
265 fail $testname
266 return
267 }
268
269 if { [which $binary] == 0 } {
270 untested $testname
271 return
272 }
273
274 if { $progopts1 == "" } { set $progopts1 "-r" }
275 verbose "running $binary $progopts $progopts1" 3
276 if { $program == "objcopy" } {
277 send_log "$binary $progopts $progopts1 dump.o dump.out\n"
278 if [catch "exec $binary $progopts $progopts1 dump.o dump.out" comp_output] {
279 send_log "$comp_output\n"
280 fail $testname
281 return
282 }
283 } else {
284 send_log "$binary $progopts $progopts1 dump.o > dump.out\n"
285 if [catch "exec $binary $progopts $progopts1 dump.o > dump.out" comp_output] {
286 send_log "$comp_output\n"
287 fail $testname
288 return
289 }
290 }
291
292 verbose_eval {[file_contents "dump.out"]} 3
293 if { [regexp_diff "dump.out" "${file}.d"] } then {
294 fail $testname
295 return
296 }
297
298 pass $testname
299 }
300
301 proc slurp_options { file } {
302 if [catch { set f [open $file r] } x] {
303 #perror "couldn't open `$file': $x"
304 perror "$x"
305 return -1
306 }
307 set opt_array {}
308 # whitespace expression
309 set ws {[ ]*}
310 set nws {[^ ]*}
311 # whitespace is ignored anywhere except within the options list;
312 # option names are alphabetic only
313 set pat "^#${ws}(\[a-zA-Z\]*)$ws:${ws}(.*)$ws\$"
314 while { [gets $f line] != -1 } {
315 set line [string trim $line]
316 # Whitespace here is space-tab.
317 if [regexp $pat $line xxx opt_name opt_val] {
318 # match!
319 lappend opt_array [list $opt_name $opt_val]
320 } else {
321 break
322 }
323 }
324 close $f
325 return $opt_array
326 }
327
328 proc objdump { opts } {
329 global OBJDUMP
330 global comp_output
331
332 catch "exec $OBJDUMP $opts" comp_output
333 verbose "objdump output=$comp_output\n" 3
334 }
335
336 proc objdump_start_no_subdir { prog opts } {
337 global OBJDUMP
338 global srcdir
339 global spawn_id
340
341 verbose "Starting $OBJDUMP $opts $prog" 2
342 catch {
343 spawn -noecho -nottyinit $srcdir/lib/run $OBJDUMP $opts $prog
344 } foo
345 if ![regexp {^[0-9]+} $foo] then {
346 perror "Can't run $prog: $foo"
347 }
348 }
349
350 proc objdump_finish { } {
351 global spawn_id
352
353 catch "close"
354 catch "wait"
355 }
356
357 # Default timeout is 10 seconds, loses on a slow machine. But some
358 # configurations of dejagnu may override it.
359 if {$timeout<120} then { set timeout 120 }
360
361 expect_after -i {
362 timeout { perror "timeout" }
363 "virtual memory exhausted" { perror "virtual memory exhausted" }
364 buffer_full { perror "buffer full" }
365 eof { perror "eof" }
366 }
367
368 # regexp_diff, based on simple_diff taken from ld test suite
369 # compares two files line-by-line
370 # file1 contains strings, file2 contains regexps and #-comments
371 # blank lines are ignored in either file
372 # returns non-zero if differences exist
373 #
374 proc regexp_diff { file_1 file_2 } {
375
376 set eof -1
377 set end 0
378 set differences 0
379 set diff_pass 0
380
381 if [file exists $file_1] then {
382 set file_a [open $file_1 r]
383 } else {
384 warning "$file_1 doesn't exist"
385 return 1
386 }
387
388 if [file exists $file_2] then {
389 set file_b [open $file_2 r]
390 } else {
391 fail "$file_2 doesn't exist"
392 close $file_a
393 return 1
394 }
395
396 verbose " Regexp-diff'ing: $file_1 $file_2" 2
397
398 while { $differences == 0 && $end == 0 } {
399 set line_a ""
400 set line_b ""
401 while { [string length $line_a] == 0 } {
402 if { [gets $file_a line_a] == $eof } {
403 set end 1
404 break
405 }
406 }
407 while { [string length $line_b] == 0 || [string match "#*" $line_b] } {
408 if [ string match "#pass" $line_b ] {
409 set end 1
410 set diff_pass 1
411 break
412 }
413 if { [gets $file_b line_b] == $eof } {
414 set end 1
415 break
416 }
417 }
418 if { $end } { break }
419 verbose "regexp \"^$line_b$\"\nline \"$line_a\"" 3
420 if ![regexp "^$line_b$" "$line_a"] {
421 send_log "regexp_diff match failure\n"
422 send_log "regexp \"^$line_b$\"\nline \"$line_a\"\n"
423 set differences 1
424 }
425 }
426
427 if { $differences == 0 && !$diff_pass && [eof $file_a] != [eof $file_b] } {
428 send_log "different lengths\n"
429 verbose "different lengths" 3
430 set differences 1
431 }
432
433 close $file_a
434 close $file_b
435
436 return $differences
437 }
438
439 proc file_contents { filename } {
440 set file [open $filename r]
441 set contents [read $file]
442 close $file
443 return $contents
444 }
445
446 proc verbose_eval { expr { level 1 } } {
447 global verbose
448 if $verbose>$level then { eval verbose "$expr" $level }
449 }