1 # Simulator dejagnu utilities.
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"
8 # Initialize the testrun.
11 proc sim_init { args } {
13 set sim_path [board_info target sim]
14 # Need to return an empty string (copied from GAS).
18 # Print the version of the simulator being tested.
19 # Required by dejagnu.
24 clone_output "$sim_path $version\n"
27 # Cover function to target_compile.
28 # Copied from gdb_compile.
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"
40 # Run a program on the simulator.
41 # Required by dejagnu (at least ${tool}_run used to be).
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.
50 # env(foo)=val - set environment variable foo to val for this run
51 # timeout=val - set the timeout to val for this run
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.
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.
61 proc sim_run { prog sim_opts prog_opts redir options } {
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]
71 if { "$testcase_timeout" == "" } {
72 set testcase_timeout 240 ;# 240 same as in dejagnu/config/sim.exp.
75 # Initial the environment we pass to the testcase.
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
88 verbose "testcase timeout is set to $testcase_timeout" 1
90 set sim [board_info target sim]
93 set prog [remote_download host $prog]
95 error "download failed"
100 set board [target_info name]
101 if [board_info $board exists sim,options] {
102 set always_opts [board_info $board sim,options]
107 # FIXME: this works for UNIX only
108 if { "$testcase_env" != "" } {
109 set sim "env $testcase_env $sim"
112 if { [board_info target sim,protocol] == "sid" } {
114 set sim_opts "$sim_opts -e \"set cpu-loader file [list ${prog}]\""
119 send_log "$sim $always_opts $SIMFLAGS $sim_opts $cmd $prog_opts\n"
121 if { "$redir" == "" } {
122 remote_spawn host "$sim $always_opts $SIMFLAGS $sim_opts $cmd $prog_opts"
124 remote_spawn host "$sim $always_opts $SIMFLAGS $sim_opts $cmd $prog_opts $redir" writeonly
126 set result [remote_wait host $testcase_timeout]
128 set return_code [lindex $result 0]
129 set output [lindex $result 1]
130 # Remove the \r part of "\r\n" so we don't break all the patterns
132 regsub -all -- "\r" $output "" output
134 if [is_remote host] {
135 # clean up after ourselves.
136 remote_file host delete $prog
139 # ??? Not sure the test for pass/fail is right.
140 # We just care that the simulator ran correctly, not whether the simulated
141 # program return 0 or non-zero from `main'.
143 if { $return_code == 0 } {
147 return [list $status $output]
151 # NAME is either a fully specified file name, or just the file name in which
152 # case $srcdir/$subdir will be prepended.
153 # REQUESTED_MACHS is a list of machines to run the testcase on. If NAME isn't
154 # for the specified machine(s), it is ignored.
155 # Typically REQUESTED_MACHS contains just one element, it is up to the caller
156 # to iterate over the desired machine variants.
158 # The file can contain options in the form "# option(mach list): value".
160 # mach: [all | machine names]
161 # as[(mach-list)]: <assembler options>
162 # ld[(mach-list)]: <linker options>
163 # sim[(mach-list)]: <simulator options>
164 # output: program output pattern to match with string-match
165 # xerror: program is expected to return with a "failure" exit code
166 # If `output' is not specified, the program must output "pass" if !xerror or
168 # The parens in "optname()" are optional if the specification is for all machs.
170 proc run_sim_test { name requested_machs } {
175 global global_as_options
176 global global_ld_options
177 global global_sim_options
179 if [string match "*/*" $name] {
181 set name [file tail $name]
183 set file "$srcdir/$subdir/$name"
186 set opt_array [slurp_options "${file}"]
187 if { $opt_array == -1 } {
188 unresolved $subdir/$name
191 # Clear default options
198 set opts(xerror) "no"
200 if ![info exists global_as_options] {
201 set global_as_options ""
203 if ![info exists global_ld_options] {
204 set global_ld_options ""
206 if ![info exists global_sim_options] {
207 set global_sim_options ""
210 # Clear any machine specific options specified in a previous test case
211 foreach m $requested_machs {
212 if [info exists opts(as,$m)] {
215 if [info exists opts(ld,$m)] {
218 if [info exists opts(sim,$m)] {
223 foreach i $opt_array {
224 set opt_name [lindex $i 0]
225 set opt_machs [lindex $i 1]
226 set opt_val [lindex $i 2]
227 if ![info exists opts($opt_name)] {
228 perror "unknown option $opt_name in file $file"
229 unresolved $subdir/$name
232 foreach m $opt_machs {
233 set opts($opt_name,$m) $opt_val
235 if { "$opt_machs" == "" } {
236 set opts($opt_name) $opt_val
242 if { $opts(output) == "" } {
243 if { "$opts(xerror)" == "no" } {
244 set opts(output) "pass\n"
246 set opts(output) "fail\n"
249 # Change \n sequences to newline chars.
250 regsub -all "\\\\n" $opts(output) "\n" opts(output)
252 set testcase_machs $opts(mach)
253 if { "$testcase_machs" == "all" } {
254 set testcase_machs $requested_machs
257 foreach mach $testcase_machs {
258 if { [lsearch $requested_machs $mach] < 0 } {
259 verbose -log "Skipping $mach version of $name, not requested."
263 verbose -log "Testing $name on machine $mach."
265 if ![info exists opts(as,$mach)] {
266 set opts(as,$mach) $opts(as)
269 set as_options "$opts(as,$mach) -I$srcdir/$subdir"
270 if [info exists cpu_option] {
271 set as_options "$as_options $cpu_option=$mach"
273 set comp_output [target_assemble $sourcefile ${name}.o "$as_options $global_as_options"]
275 if ![string match "" $comp_output] {
276 verbose -log "$comp_output" 3
277 fail "$mach $testname (assembling)"
281 if ![info exists opts(ld,$mach)] {
282 set opts(ld,$mach) $opts(ld)
285 set comp_output [target_link ${name}.o ${name}.x "$opts(ld,$mach) $global_ld_options"]
287 if ![string match "" $comp_output] {
288 verbose -log "$comp_output" 3
289 fail "$mach $testname (linking)"
293 # If no machine specific options, default to the general version.
294 if ![info exists opts(sim,$mach)] {
295 set opts(sim,$mach) $opts(sim)
298 # Build the options argument.
300 if { "$opts(timeout)" != "" } {
301 set options "$options timeout=$opts(timeout)"
304 set result [sim_run ${name}.x "$opts(sim,$mach) $global_sim_options" "" "" "$options"]
305 set status [lindex $result 0]
306 set output [lindex $result 1]
308 if { "$status" == "pass" } {
309 if { "$opts(xerror)" == "no" } {
310 if [string match $opts(output) $output] {
311 pass "$mach $testname"
312 file delete ${name}.o ${name}.x
314 verbose -log "output: $output" 3
315 verbose -log "pattern: $opts(output)" 3
316 fail "$mach $testname (execution)"
319 verbose -log "`pass' return code when expecting failure" 3
320 fail "$mach $testname (execution)"
322 } elseif { "$status" == "fail" } {
323 if { "$opts(xerror)" == "no" } {
324 fail "$mach $testname (execution)"
326 if [string match $opts(output) $output] {
327 pass "$mach $testname"
328 file delete ${name}.o ${name}.x
330 verbose -log "output: $output" 3
331 verbose -log "pattern: $opts(output)" 3
332 fail "$mach $testname (execution)"
336 $status "$mach $testname"
341 # Subroutine of run_sim_test to process options in FILE.
343 proc slurp_options { file } {
344 if [catch { set f [open $file r] } x] {
345 #perror "couldn't open `$file': $x"
350 # whitespace expression
353 # whitespace is ignored anywhere except within the options list;
354 # option names are alphabetic only
355 set pat "^#${ws}(\[a-zA-Z\]*)\\(?(\[^):\]*)\\)?$ws:${ws}(.*)$ws\$"
356 # Allow arbitrary lines until the first option is seen.
358 while { [gets $f line] != -1 } {
359 set line [string trim $line]
360 # Whitespace here is space-tab.
361 if [regexp $pat $line xxx opt_name opt_machs opt_val] {
363 lappend opt_array [list $opt_name $opt_machs $opt_val]