OSDN Git Service

* public snapshot of sid simulator
[pf3gnuchains/pf3gnuchains3x.git] / sid / component / testsuite / lib / cpudefs.exp
1 global prefix
2
3 # In general, this belongs in a file in $tool/testsuite/lib,
4 # but that doesn't exist [nor do we want to create N lib directories].
5
6 # Run the tests that match PATTERN using the sid executable.
7 # Each testcase must have a `sim' entry specifying the sid configuration file.
8 # See the docs for run-one-sid-test.
9
10 set build_ltldpath [join [glob "../*" "$srcdir/../*"] ":"]
11 set install_ltldpath "$prefix/lib/sidcomp"
12 set sid_run_repetitions 1
13
14 proc run-sid-tests { toolchain-prefix all-machs testcase-srcdir pattern as-opts ld-opts sid-opts exe-suffix } {
15     set as-prog [sid_find_gas ${toolchain-prefix}]
16     set ld-prog [sid_find_ld ${toolchain-prefix}]
17     set sid-prog [sid_find_sid]
18
19     run-sid-test \
20             ${as-prog} ${as-opts} \
21             ${ld-prog} ${ld-opts} \
22             {} {} \
23             ${sid-prog} ${sid-opts} \
24             ${all-machs} ${testcase-srcdir} $pattern ${exe-suffix} \
25             sid-run-conf
26 }
27
28 # Run the tests that match PATTERN using the ${toolchain-prefix}-run
29 # SID cover script.
30
31 proc run-sid-bsp-tests { toolchain-prefix all-machs testcase-srcdir pattern as-opts ld-opts sid-opts exe-suffix } {
32     set as-prog [sid_find_gas ${toolchain-prefix}]
33     set ld-prog [sid_find_ld ${toolchain-prefix}]
34     set sid-bsp-prog [sid_find_sid_bsp ${toolchain-prefix}]
35
36     run-sid-test \
37             ${as-prog} ${as-opts} \
38             ${ld-prog} ${ld-opts} \
39             {} {}  \
40             ${sid-bsp-prog} ${sid-opts} \
41             ${all-machs} ${testcase-srcdir} $pattern ${exe-suffix} \
42             sid-run-bsp
43 }
44
45 # Run the tests that match PATTERN using the ${toolchain-prefix}-run
46 # SID cover script.
47
48 proc run-sid-c-bsp-tests { toolchain-prefix all-machs testcase-srcdir pattern gcc-opts sid-opts exe-suffix } {
49     set gcc-prog [sid_find_gcc ${toolchain-prefix}]
50     set sid-bsp-prog [sid_find_sid_bsp ${toolchain-prefix}]
51
52     run-sid-test \
53             {} {} \
54             {} {} \
55             ${gcc-prog} ${gcc-opts}  \
56             ${sid-bsp-prog} ${sid-opts} \
57             ${all-machs} ${testcase-srcdir} $pattern ${exe-suffix} \
58             sid-run-bsp
59 }
60
61
62 proc run-sid-test { as as-opts ld ld-opts gcc gcc-opts sid sid-opts all-machs testcase-srcdir pattern exe-suffix runner } {
63     global runtests
64
65     verbose "as = $as ${as-opts}"
66     verbose "ld = $ld ${ld-opts}"
67     verbose "gcc = $gcc ${gcc-opts}"
68     verbose "sid = $sid ${sid-opts}"
69     verbose "all-machs = ${all-machs}"
70     verbose "testcase-srcdir = ${testcase-srcdir}"
71     verbose "pattern = ${pattern}"
72
73     foreach src [lsort [glob -nocomplain ${testcase-srcdir}/$pattern]] {
74         # If we're only testing specific files and this isn't one of them,
75         # skip it.
76         if ![runtest_file_p $runtests $src] {
77             continue
78         }
79
80         run-one-sid-test \
81                 $as ${as-opts} \
82                 $ld ${ld-opts} \
83                 $gcc ${gcc-opts} \
84                 $sid ${sid-opts} \
85                 ${all-machs} $src ${exe-suffix} \
86                 $runner
87     }
88 }
89
90 # Argument to run-one-sid-test to compute the command_line argument for
91 # testcases using a .conf file.
92
93 proc sid-run-conf { sid sid-opts conf-file test-prog run-options } {
94     global srcdir subdir
95     global prefix
96     # Install a usable .conf file in the build directory.
97     set filename [file tail ${conf-file}]
98     set rc [remote_download host $srcdir/$subdir/${conf-file} ${filename}.in]
99     if { $rc == "" } {
100         verbose -log "download failed, $srcdir/$subdir/${conf-file}"
101         return [list unresolved ""]
102     }
103     set remote-conf-file $rc
104     # s/@-prefix-@/$prefix/
105     # ??? Another way to do this would be to do it entirely in tcl [open,
106     # read,regsub,write].
107     # ??? Another way would be to be able to pass command line arguments
108     # to sid that are usable in the .conf file.
109     set rc [remote_exec host sed "-e s,@-prefix-@,$prefix, -e s,@-srcdir-@,$srcdir, -e s,@-testprog-@,${test-prog}, " \
110             "${remote-conf-file}" "${filename}" 10]
111     if { [lindex $rc 0] == -1 } {
112         verbose -log "@-prefix-@ substitution failed, [lindex $rc 1]"
113         return [list unresolved ""]
114     }
115
116     # compute component library search path: build tree, then install tree
117     global prefix build_ltldpath install_ltldpath
118     set sidpath [sid_find_sid]
119     global build_ltldpath
120     set ltldpath ":$build_ltldpath:$install_ltldpath:"
121     return [sid_run "$sid ${sid-opts} ${conf-file}" "" "" "${run-options} \"env(SID)=$sidpath\" \"env(SID_LIBRARY_PATH)=$ltldpath\""]
122 }
123
124 # Argument to run-one-sid-test to compute the command_line argument for
125 # testcases using ${target-alias}-run.
126
127 proc sid-run-bsp { sid sid-opts sid-mach-opts test-prog run-options } {
128     # compute component library search path: build tree, then install tree
129     global prefix build_ltldpath install_ltldpath
130     set ltldpath ":$build_ltldpath:$install_ltldpath:"
131     set sidpath [sid_find_sid]
132
133     return [sid_run "$sid ${sid-opts} ${sid-mach-opts} ${test-prog}" "" "" "${run-options} \"env(SID)=$sidpath\" \"env(SID_LIBRARY_PATH)=$ltldpath\""]
134 }
135
136 # Run testcase SRC.
137 # SRC is a fully specified file name.
138 # AS,LD,GCC,SID are file names of their respective executables.
139 # EXE-SUFFIX is the suffix to give the executable.  This is to distinguish the
140 # same testcase being used multiple times (each with different compilation
141 # options).
142 #
143 # This works with either the main sid executable (that takes a configuration
144 # file for an argument) or with the easy-to-use(TM) ${target-alias}-run cover
145 # script.
146 #
147 # ALL_MACHS is a list of machs to use if "mach: all" is specified in the file.
148 # The file can contain options in the form "# option(mach list): value"
149 # Possibilities:
150 # mach: [all | machine names]
151 # as[(mach-list)]: <assembler options>
152 # ld[(mach-list)]: <linker options>
153 # sim[(mach-list)]: <simulator options>
154 # output[(mach-list)]: program output pattern to match with string-match
155 # xerror[(mach-list)]: program is expected to return with a "failure" exit code
156 # If `output' is not specified, the program must output "pass" if !xerror or
157 # "fail" if xerror.
158 # The parens in "optname()" are optional if the specification is for all machs.
159
160 proc run-one-sid-test { as as-opts ld ld-opts gcc gcc-opts sid sid-opts all-machs src exe-suffix runner } {
161     verbose "Testing $src."
162
163     set file $src
164     set name [file tail $src]
165     set testcase-dir [file dirname $src]
166
167     set opt_array [slurp_options "${file}"]
168     if { $opt_array == -1 } {
169         unresolved $name
170         return
171     }
172     set opts(as) {}
173     set opts(ld) {}
174     set opts(gcc) {}
175     set opts(sim) {}
176     set opts(output) {}
177     set opts(mach) {}
178     set opts(timeout) {}
179     set opts(xerror) "no"
180
181     foreach i $opt_array {
182         set opt_name [lindex $i 0]
183         set opt_machs [lindex $i 1]
184         set opt_val [lindex $i 2]
185         if ![info exists opts($opt_name)] {
186             perror "unknown option $opt_name in file $file"
187             unresolved $name
188             return
189         }
190         foreach m $opt_machs {
191             set opts($opt_name,$m) $opt_val
192         }
193         if { "$opt_machs" == "" } {
194             set opts($opt_name) $opt_val
195         }
196     }
197
198     set testname "$name (${as-opts},${ld-opts},${gcc-opts},${sid-opts})"
199     set sourcefile $file
200     if { $opts(output) == "" } {
201         if { "$opts(xerror)" == "no" } {
202             set opts(output) "pass\n"
203         } else {
204             set opts(output) "fail\n"
205         }
206     }
207     # Change \n sequences to newline chars.
208     regsub -all "\\\\n" $opts(output) "\n" opts(output)
209
210     # ??? Need a mach-suffix to go with exe-suffix.
211     if {$opts(mach) == "all"} then { set opts(mach) ${all-machs} }
212     foreach mach $opts(mach) {
213         verbose "Testing $name on $mach."
214         global test
215         set test "$mach $testname"
216
217         set exe-name ${name}.${exe-suffix}
218
219         if [string match "*.c" $sourcefile] {
220             if ![info exists opts(gcc,$mach)] {
221                 set opts(gcc,$mach) $opts(gcc)
222             }
223             send_log "$gcc ${gcc-opts} $opts(gcc,$mach) -I${testcase-dir} -o ${exe-name} $sourcefile\n"
224             catch "exec $gcc ${gcc-opts} $opts(gcc,$mach) -I${testcase-dir} -o ${exe-name} $sourcefile" comp_output
225
226             if ![string match "" $comp_output] {
227                 verbose -log "$comp_output" 3
228                 perror "$mach $testname - compile failed"
229                 continue
230             }
231         } else {
232             if ![info exists opts(as,$mach)] {
233                 set opts(as,$mach) $opts(as)
234             }
235             send_log "$as ${as-opts} $opts(as,$mach) -I${testcase-dir} -o ${name}.o $sourcefile\n"
236             catch "exec $as ${as-opts} $opts(as,$mach) -I${testcase-dir} -o ${name}.o $sourcefile" comp_output
237
238             if ![string match "" $comp_output] {
239                 verbose -log "$comp_output" 3
240                 perror "$mach $testname - assembly failed"
241                 continue
242             }
243             
244             if ![info exists opts(ld,$mach)] {
245                 set opts(ld,$mach) $opts(ld)
246             }
247             send_log "$ld ${ld-opts} $opts(ld,$mach) -o ${exe-name} ${name}.o\n"
248             catch "exec $ld ${ld-opts} $opts(ld,$mach) -o ${exe-name} ${name}.o" comp_output
249             
250             if ![string match "" $comp_output] {
251                 verbose -log "$comp_output" 3
252                 perror "$mach $testname - linking failed"
253                 continue
254             }
255         }
256
257         # If no machine specific options, default to the general version.
258         if ![info exists opts(sim,$mach)] {
259             set opts(sim,$mach) $opts(sim)
260         }
261
262         # Build the options argument.
263         set options ""
264         if { "$opts(timeout)" != "" } {
265             set options "$options timeout=$opts(timeout)"
266         }
267
268         global sid_run_repetitions
269         global last_sid_run_time
270         set last_sid_run_time 99999
271         for {set iter 0} {$iter < $sid_run_repetitions} {incr iter} {
272             set result [$runner $sid "${sid-opts}" "$opts(sim,$mach)" ${exe-name} "$options"]
273             if {$iter == 0} { 
274                 set min_sid_run_time $last_sid_run_time
275             } elseif {$last_sid_run_time < $min_sid_run_time} {
276                 set min_sid_run_time $last_sid_run_time
277             }
278             set status [lindex $result 0]
279             set output [lindex $result 1]
280
281             if { "$status" == "pass" } {
282                 if { "$opts(xerror)" == "no" } {
283                     if [string match $opts(output) $output] {
284                         pass $test
285                     } else {
286                         verbose -log "output:  $output" 3
287                         verbose -log "pattern: $opts(output)" 3
288                         fail "$mach $testname"
289                         return
290                     }
291                 } else {
292                     verbose -log "`pass' return code when expecting failure" 3
293                     fail $test
294                     return
295                 }
296             } elseif { "$status" == "fail" } {
297                 if { "$opts(xerror)" == "no" } {
298                     fail "$mach $testname"
299                     return
300                 } else {
301                     if [string match $opts(output) $output] {
302                         pass $test
303                     } else {
304                         verbose -log "output:  $output" 3
305                         verbose -log "pattern: $opts(output)" 3
306                         fail $test
307                         return
308                     }
309                 }
310             } else {
311                 $status "$mach $testname"
312             }
313         }
314         if {$sid_run_repetitions > 1} then {
315             note "$test simulation time: $min_sid_run_time ms"
316         }
317     }
318 }
319
320 # Run a program on the simulator.
321 #
322 # SIM_OPTS are options for the simulator.
323 # PROG_OPTS are options passed to the simulated program.
324 # At present REDIR must be "" or "> foo".
325 # OPTIONS is a list of options internal to this routine.
326 # This is modelled after target_compile.  We want to be able to add new
327 # options without having to update all our users.
328 # Currently:
329 #       env(foo)=val    - set environment variable foo to val for this run
330 #       timeout=val     - set the timeout to val for this run
331 #
332 # The result is a list of two elements.
333 # The first is one of pass/fail/etc.
334 # The second is the program's output.
335 #
336 # This is different than the sim_load routine provided by
337 # dejagnu/config/sim.exp.  It's not clear how to pass arguments to the
338 # simulator (not the simulated program, the simulator) with sim_load.
339 #
340 # This used to support running dejagnu on a different host, but due to
341 # the need for multiple files and having one file refering to the other files,
342 # this is much more difficult.  Thus this support has been deleted.  Should
343 # it ever be needed it can be revisited.
344 # REQUIRED_FILES is unused, but it's meant to contain the list of files that
345 # need to be downloaded to the host.  The catch is to update the sid .conf
346 # file to point to where they were put.
347
348 # sim sim_opts prog prog_opts
349 proc sid_run { command_line required_files redir options } {
350     # Set the default value of the timeout.
351     # FIXME: The timeout value we actually want is a function of
352     # host, target, and testcase.
353     set testcase_timeout [board_info target sim_time_limit]
354     if { "$testcase_timeout" == "" } {
355         set testcase_timeout [board_info host testcase_timeout]
356     }
357     if { "$testcase_timeout" == "" } {
358         set testcase_timeout 240 ;# 240 same as in dejagnu/config/sim.exp.
359     }
360
361     # Initialize the environment we pass to the testcase.
362     set testcase_env ""
363
364     # Process OPTIONS ...
365     foreach o $options {
366         if [regexp {^env\((.*)\)=(.*)} $o full var val] {
367             set testcase_env "$testcase_env $var=$val"
368         } elseif [regexp {^timeout=(.*)} $o full val] {
369             set testcase_timeout $val
370         }
371         
372     }
373         
374     verbose "testcase timeout is set to $testcase_timeout" 1
375
376     if [is_remote host] {
377         # Let's make this a catastrophic error so should the time ever come
378         # for it's need, the problem stands out.
379         error "remote host operation isn't supported"
380     }
381
382     # FIXME: this works for UNIX only
383     if { "$testcase_env" != "" } {
384         set command_line "env $testcase_env $command_line"
385     }
386
387     send_log "$command_line\n"
388
389     set start_time [clock clicks]
390     if { "$redir" == "" } {
391         remote_spawn host "$command_line"
392     } else {
393         remote_spawn host "$command_line $redir" writeonly
394     }
395     set result [remote_wait host $testcase_timeout]
396     set end_time [clock clicks]
397     set run_time [expr {($end_time - $start_time) / 1000}]
398     global test
399     global last_sid_run_time
400     set last_sid_run_time $run_time
401     # note "$test simulation time: $run_time ms"
402
403     set return_code [lindex $result 0]
404     set output [lindex $result 1]
405     # Remove the \r part of "\r\n" so we don't break all the patterns
406     # we want to match.
407     regsub -all -- "\r" $output "" output
408
409     # ??? Not sure the test for pass/fail is right.
410     # We just care that the simulator ran correctly, not whether the simulated
411     # program return 0 or non-zero from `main'.
412     set status fail
413     if { $return_code == 0 } {
414         set status pass
415     }
416
417     return [list $status $output]
418 }
419
420 # Subroutine of run_sim_test to process options in FILE.
421
422 proc slurp_options { file } {
423     #verbose "Slurping $file"
424     if [catch { set f [open $file r] } x] {
425         #perror "couldn't open `$file': $x"
426         perror "$x"
427         return -1
428     }
429     set opt_array {}
430     # whitespace expression
431     set ws  {[  ]*}
432     set nws {[^         ]*}
433     # whitespace is ignored anywhere except within the options list;
434     # option names are alphabetic only
435     set pat "^#${ws}(\[a-zA-Z\]*)\\(?(\[^):\]*)\\)?$ws:${ws}(.*)$ws\$"
436     # Allow comment as first line of file.
437     set firstline 1
438     while { [gets $f line] != -1 } {
439         set line [string trim $line]
440         # Whitespace here is space-tab.
441         if [regexp $pat $line xxx opt_name opt_machs opt_val] {
442             # match!
443             lappend opt_array [list $opt_name $opt_machs $opt_val]
444         } else {
445             if { ! $firstline } {
446                 break
447             }
448         }
449         set firstline 0
450     }
451     close $f
452     return $opt_array
453 }