This commit was generated by cvs2svn to track changes on a CVS vendor
[external/binutils.git] / sim / testsuite / lib / sim-defs.exp
1 # Simulator dejagnu utilities.
2
3 # Communicate simulator path from sim_init to sim_version.
4 # For some reason [board_info target sim] doesn't work in sim_version.
5 # [Presumubly because the target has been "popped" by then.  Odd though.]
6 set sim_path "unknown-run"
7
8 # Initialize the testrun.
9 # Required by dejagnu.
10
11 proc sim_init { args } {
12     global sim_path
13     set sim_path [board_info target sim]
14     # Need to return an empty string (copied from GAS).
15     return ""
16 }
17
18 # Print the version of the simulator being tested.
19 # Required by dejagnu.
20
21 proc sim_version {} {
22     global sim_path
23     set version 0.5
24     clone_output "$sim_path $version\n"
25 }
26
27 # Cover function to target_compile.
28 # Copied from gdb_compile.
29
30 proc sim_compile { source dest type options } {
31     set result [target_compile $source $dest $type $options]
32     regsub "\[\r\n\]*$" "$result" "" result
33     regsub "^\[\r\n\]*" "$result" "" result
34     if { $result != "" } {
35         clone_output "sim compile output: $result"
36     }
37     return $result
38 }
39
40 # Run a program on the simulator.
41 # Required by dejagnu (at least ${tool}_run used to be).
42 #
43 # SIM_OPTS are options for the simulator.
44 # PROG_OPTS are options passed to the simulated program.
45 # At present REDIR must be "" or "> foo".
46 # OPTIONS is a list of options internal to this routine.
47 # This is modelled after target_compile.  We want to be able to add new
48 # options without having to update all our users.
49 # Currently:
50 #       env(foo)=val    - set environment variable foo to val for this run
51 #       timeout=val     - set the timeout to val for this run
52 #
53 # The result is a list of two elements.
54 # The first is one of pass/fail/etc.
55 # The second is the program's output.
56 #
57 # This is different than the sim_load routine provided by
58 # dejagnu/config/sim.exp.  It's not clear how to pass arguments to the
59 # simulator (not the simulated program, the simulator) with sim_load.
60
61 proc sim_run { prog sim_opts prog_opts redir options } {
62     global SIMFLAGS
63
64     # Set the default value of the timeout.
65     # FIXME: The timeout value we actually want is a function of
66     # host, target, and testcase.
67     set testcase_timeout [board_info target sim_time_limit]
68     if { "$testcase_timeout" == "" } {
69         set testcase_timeout [board_info host testcase_timeout]
70     }
71     if { "$testcase_timeout" == "" } {
72         set testcase_timeout 240 ;# 240 same as in dejagnu/config/sim.exp.
73     }
74
75     # Initial the environment we pass to the testcase.
76     set testcase_env ""
77
78     # Process OPTIONS ...
79     foreach o $options {
80         if [regexp {^env\((.*)\)=(.*)} $o full var val] {
81             set testcase_env "$testcase_env $var=$val"
82         } elseif [regexp {^timeout=(.*)} $o full val] {
83             set testcase_timeout $val
84         }
85     }
86
87     set sim [board_info target sim]
88
89     # FIXME: this works for UNIX only
90     if { "$testcase_env" != "" } {
91         set sim "env $testcase_env $sim"
92     }
93
94     if { "$redir" == "" } {
95         remote_spawn host "$sim $SIMFLAGS $sim_opts $prog $prog_opts"
96     } else {
97         remote_spawn host "$sim $SIMFLAGS $sim_opts $prog $prog_opts $redir" writeonly
98     }
99     set result [remote_wait host $testcase_timeout]
100
101     set return_code [lindex $result 0]
102     set output [lindex $result 1]
103     # Remove the \r part of "\r\n" so we don't break all the patterns
104     # we want to match.
105     regsub -all -- "\r" $output "" output
106
107     # ??? Not sure the test for pass/fail is right.
108     # We just care that the simulator ran correctly, not whether the simulated
109     # program return 0 or non-zero from `main'.
110     set status fail
111     if { $return_code == 0 } {
112         set status pass
113     }
114
115     return [list $status $output]
116 }
117
118 # Run testcase NAME.
119 # NAME is either a fully specified file name, or just the file name in which
120 # case $srcdir/$subdir will be prepended.
121 # The file can contain options in the form "# option(mach list): value"
122 # Possibilities:
123 # mach(): machine names
124 # as(mach): <assembler options>
125 # ld(mach): <linker options>
126 # sim(mach): <simulator options>
127 # output(): program output pattern to match with string-match
128 # If `output' is not specified, the program must output "pass".
129
130 proc run_sim_test { name } {
131     global subdir srcdir
132     global AS ASFLAGS LD LDFLAGS SIMFLAGS
133     global opts
134
135     if [string match "*/*" $name] {
136         set file $name
137         set name [file tail $name]
138     } else {
139         set file "$srcdir/$subdir/$name"
140     }
141
142     set opt_array [slurp_options "${file}"]
143     if { $opt_array == -1 } {
144         unresolved $subdir/$name
145         return
146     }
147     set opts(as) {}
148     set opts(ld) {}
149     set opts(sim) {}
150     set opts(output) {}
151     set opts(mach) {}
152     set opts(timeout) {}
153
154     foreach i $opt_array {
155         set opt_name [lindex $i 0]
156         set opt_machs [lindex $i 1]
157         set opt_val [lindex $i 2]
158         if ![info exists opts($opt_name)] {
159             perror "unknown option $opt_name in file $file"
160             unresolved $subdir/$name
161             return
162         }
163         foreach m $opt_machs {
164             set opts($opt_name,$m) $opt_val
165         }
166         if { "$opt_machs" == "" } {
167             set opts($opt_name) $opt_val
168         }
169     }
170
171     set testname $name
172     set sourcefile $file
173     if { $opts(output) == "" } {
174         set opts(output) "pass\n"
175     }
176
177     foreach mach $opts(mach) {
178         verbose "Testing $name on $mach."
179
180         if ![info exists opts(as,$mach)] {
181             set opts(as,$mach) $opts(as)
182         }
183         send_log "$AS $ASFLAGS $opts(as,$mach) -I$srcdir/$subdir -o ${name}.o $sourcefile\n"
184         catch "exec $AS $ASFLAGS $opts(as,$mach) -I$srcdir/$subdir -o ${name}.o $sourcefile" comp_output
185
186         if ![string match "" $comp_output] {
187             verbose -log "$comp_output" 3
188             fail "$mach $testname"
189             continue
190         }
191
192         if ![info exists opts(ld,$mach)] {
193             set opts(ld,$mach) $opts(ld)
194         }
195         send_log "$LD $LDFLAGS $opts(ld,$mach) -o ${name}.x ${name}.o\n"
196         catch "exec $LD $LDFLAGS $opts(ld,$mach) -o ${name}.x ${name}.o" comp_output
197
198         if ![string match "" $comp_output] {
199             verbose -log "$comp_output" 3
200             fail "$mach $testname"
201             continue
202         }
203
204         # If no machine specific options, default to the general version.
205         if ![info exists opts(sim,$mach)] {
206             set opts(sim,$mach) $opts(sim)
207         }
208
209         # Build the options argument.
210         set options ""
211         if { "$opts(timeout)" != "" } {
212             set options "$options timeout=$opts(timeout)"
213         }
214
215         set result [sim_run ${name}.x "$opts(sim,$mach)" "" "" "$options"]
216         set status [lindex $result 0]
217         set output [lindex $result 1]
218
219         if { "$status" == "pass" } {
220             if ![string match $opts(output) $output] {
221                 verbose -log "output:  $output" 3
222                 verbose -log "pattern: $opts(output)" 3
223             }
224         }
225
226         $status "$mach $testname"
227     }
228 }
229
230 # Subroutine of run_sim_test to process options in FILE.
231
232 proc slurp_options { file } {
233     if [catch { set f [open $file r] } x] {
234         #perror "couldn't open `$file': $x"
235         perror "$x"
236         return -1
237     }
238     set opt_array {}
239     # whitespace expression
240     set ws  {[  ]*}
241     set nws {[^         ]*}
242     # whitespace is ignored anywhere except within the options list;
243     # option names are alphabetic only
244     set pat "^#${ws}(\[a-zA-Z\]*)\\((.*)\\)$ws:${ws}(.*)$ws\$"
245     # Allow comment as first line of file.
246     set firstline 1
247     while { [gets $f line] != -1 } {
248         set line [string trim $line]
249         # Whitespace here is space-tab.
250         if [regexp $pat $line xxx opt_name opt_machs opt_val] {
251             # match!
252             lappend opt_array [list $opt_name $opt_machs $opt_val]
253         } else {
254             if { ! $firstline } {
255                 break
256             }
257         }
258         set firstline 0
259     }
260     close $f
261     return $opt_array
262 }