sim-defs.exp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. # Simulator dejagnu utilities.
  2. # TODO: Switch to using dg-xxx helpers rather than parsing the files directly.
  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. # Find the simulator arch.
  8. proc sim_arch {} {
  9. global subdir
  10. set arch "$subdir"
  11. while { [file dirname $arch] != "." } {
  12. set arch [file dirname $arch]
  13. }
  14. return "$arch"
  15. }
  16. # Initialize the testrun.
  17. #
  18. # Normally dejagnu will execute ${tool}_init automatically, but since we set
  19. # --tool '' (for a simpler testsuite/ layout), we have each test call this
  20. # itself.
  21. proc sim_init { args } {
  22. global builddir
  23. global subdir
  24. global sim_path
  25. # Find the path to the simulator for executing.
  26. set sim_path "$builddir/[sim_arch]/run"
  27. # As gross as it is, we unset the linker script specified by the target
  28. # board. The simulator board file mips-sim.exp, sets ldscript to the
  29. # MIPS libgloss linker scripts which include libgcc (and possibly other
  30. # libraries), which the linker (used to link these tests rather than the
  31. # compiler) can't necessarily find. Similarly iq2000-sim.exp and
  32. # m68hc11-sim.exp. So, we make it a common rule to clear the slate for
  33. # all simulators.
  34. unset_currtarget_info ldscript
  35. sim_init_toolchain
  36. # Need to return an empty string. This tells dejagnu to *not* re-run us
  37. # with the exact test that we're about to run.
  38. return ""
  39. }
  40. # Initialize the toolchain settings for this port.
  41. # Needs to be called once per-port.
  42. proc sim_init_toolchain {} {
  43. global objdir
  44. global srcdir
  45. global cpu_option
  46. global cpu_option_sep
  47. global ASFLAGS_FOR_TARGET
  48. global CFLAGS_FOR_TARGET
  49. global LDFLAGS_FOR_TARGET
  50. global SIMFLAGS_FOR_TARGET
  51. global global_as_works
  52. global global_cpp_works
  53. global global_cc_works
  54. global global_cc_os
  55. global CFLAGS_FOR_TARGET_init
  56. # Reset all the toolchain settings. This provides a clean slate when
  57. # starting the next set of tests.
  58. set ASFLAGS_FOR_TARGET ""
  59. set CFLAGS_FOR_TARGET ""
  60. set LDFLAGS_FOR_TARGET ""
  61. set SIMFLAGS_FOR_TARGET ""
  62. unset -nocomplain cpu_option cpu_option_sep
  63. # The configure script created XXX_FOR_TARGET_$ARCH for us, so merge those
  64. # into plain XXX_FOR_TARGET for this particular arch run.
  65. global SIM_PRIMARY_TARGET
  66. set arch [sim_arch]
  67. set ARCH [string map {- _} [string toupper $arch]]
  68. foreach var {AS LD CC} {
  69. set var_for_target "${var}_FOR_TARGET"
  70. global $var_for_target
  71. set var_for_target_arch "${var_for_target}_${ARCH}"
  72. global $var_for_target_arch
  73. if [info exists $var_for_target_arch] {
  74. set $var_for_target [set $var_for_target_arch]
  75. } else {
  76. set $var_for_target ""
  77. }
  78. if { [set $var_for_target] == "" } {
  79. # If building for the primary target, use the default settings.
  80. if { $arch == $SIM_PRIMARY_TARGET } {
  81. unset -nocomplain $var_for_target
  82. } {
  83. set $var_for_target false
  84. }
  85. }
  86. }
  87. # See if an assembler is available.
  88. if { $arch != $SIM_PRIMARY_TARGET && $AS_FOR_TARGET == "false" } {
  89. verbose -log "Can't find a compatible assembler"
  90. set global_as_works 0
  91. } {
  92. verbose -log "Found a compatible assembler"
  93. set global_as_works 1
  94. }
  95. # Merge per-test settings if available.
  96. if ![info exists CFLAGS_FOR_TARGET_init] {
  97. set CFLAGS_FOR_TARGET_init ""
  98. }
  99. set cc_options [list "additional_flags=$CFLAGS_FOR_TARGET_init"]
  100. # See if we have a preprocessor available.
  101. set result [target_compile $srcdir/lib/compilercheck.c \
  102. $objdir/compilercheck.x "preprocess" $cc_options]
  103. set global_cpp_works [string equal "" "$result"]
  104. # See if we have a compiler available, and which environment it's targeting.
  105. set global_cc_os ""
  106. set global_cc_works 0
  107. if { $arch != $SIM_PRIMARY_TARGET && $CC_FOR_TARGET == "false" } {
  108. verbose -log "Can't find a compatible C compiler"
  109. } elseif { [target_compile $srcdir/lib/newlibcheck.c \
  110. $objdir/compilercheck.x "executable" $cc_options] == "" } {
  111. verbose -log "Found newlib C compiler"
  112. set global_cc_works 1
  113. set global_cc_os "newlib"
  114. } elseif { [target_compile $srcdir/lib/linuxcheck.c \
  115. $objdir/compilercheck.x "executable" $cc_options] == "" } {
  116. verbose -log "Found Linux C compiler"
  117. set global_cc_works 1
  118. set global_cc_os "linux"
  119. } elseif { [target_compile $srcdir/lib/compilercheck.c \
  120. $objdir/compilercheck.x "executable" $cc_options] == "" } {
  121. verbose -log "Found C compiler, but unknown OS"
  122. set global_cc_works 1
  123. } {
  124. verbose -log "Can't execute C compiler"
  125. }
  126. file delete $objdir/compilercheck.x
  127. unset CFLAGS_FOR_TARGET_init
  128. }
  129. # Print the version of the simulator being tested.
  130. # Required by dejagnu.
  131. proc sim_version {} {
  132. global sim_path
  133. set version 0.5
  134. clone_output "$sim_path $version\n"
  135. }
  136. # Run a program on the simulator.
  137. # Required by dejagnu (at least ${tool}_run used to be).
  138. #
  139. # SIM_OPTS are options for the simulator.
  140. # PROG_OPTS are options passed to the simulated program.
  141. # At present REDIR must be "" or "> foo".
  142. # OPTIONS is a list of options internal to this routine.
  143. # This is modelled after target_compile. We want to be able to add new
  144. # options without having to update all our users.
  145. # Currently:
  146. # env(foo)=val - set environment variable foo to val for this run
  147. # timeout=val - set the timeout to val for this run
  148. #
  149. # The result is a list of two elements.
  150. # The first is the program's exit status (0/1/etc...).
  151. # The second is the program's output.
  152. #
  153. # This is different than the sim_load routine provided by
  154. # dejagnu/config/sim.exp. It's not clear how to pass arguments to the
  155. # simulator (not the simulated program, the simulator) with sim_load.
  156. proc sim_run { prog sim_opts prog_opts redir options } {
  157. global sim_path
  158. # Set the default value of the timeout.
  159. # FIXME: The timeout value we actually want is a function of
  160. # host, target, and testcase.
  161. set testcase_timeout [board_info target sim_time_limit]
  162. if { "$testcase_timeout" == "" } {
  163. set testcase_timeout [board_info host testcase_timeout]
  164. }
  165. if { "$testcase_timeout" == "" } {
  166. set testcase_timeout 240 ;# 240 same as in dejagnu/config/sim.exp.
  167. }
  168. # Initial the environment we pass to the testcase.
  169. set testcase_env ""
  170. # Process OPTIONS ...
  171. foreach o $options {
  172. if [regexp {^env\((.*)\)=(.*)} $o full var val] {
  173. set testcase_env "$testcase_env $var=$val"
  174. } elseif [regexp {^timeout=(.*)} $o full val] {
  175. set testcase_timeout $val
  176. }
  177. }
  178. verbose "testcase timeout is set to $testcase_timeout" 1
  179. set sim $sim_path
  180. if [is_remote host] {
  181. set prog [remote_download host $prog]
  182. if { $prog == "" } {
  183. error "download failed"
  184. return -1
  185. }
  186. }
  187. set board [target_info name]
  188. if [board_info $board exists sim,options] {
  189. set always_opts [board_info $board sim,options]
  190. } else {
  191. set always_opts ""
  192. }
  193. # FIXME: this works for UNIX only
  194. if { "$testcase_env" != "" } {
  195. set sim "env $testcase_env $sim"
  196. }
  197. if { [board_info target sim,protocol] == "sid" } {
  198. set cmd ""
  199. set sim_opts "$sim_opts -e \"set cpu-loader file [list ${prog}]\""
  200. } else {
  201. set cmd "$prog"
  202. }
  203. send_log "$sim $always_opts $sim_opts $cmd $prog_opts\n"
  204. if { "$redir" == "" } {
  205. remote_spawn host "$sim $always_opts $sim_opts $cmd $prog_opts"
  206. } else {
  207. remote_spawn host "$sim $always_opts $sim_opts $cmd $prog_opts $redir" writeonly
  208. }
  209. set result [remote_wait host $testcase_timeout]
  210. set return_code [lindex $result 0]
  211. set output [lindex $result 1]
  212. # Remove the \r part of "\r\n" so we don't break all the patterns
  213. # we want to match.
  214. regsub -all -- "\r" $output "" output
  215. if [is_remote host] {
  216. # clean up after ourselves.
  217. remote_file host delete $prog
  218. }
  219. return [list $return_code $output]
  220. }
  221. # Support function for "#requires: simoption <xx>":
  222. # Looks in "run --help" output for <xx>, returns 1 iff <xx> is mentioned
  223. # there and looks like an option name, otherwise 0.
  224. proc sim_check_requires_simoption { optname } {
  225. global sim_path
  226. set testrun "$sim_path --help"
  227. verbose -log "Checking for simoption `$optname'" 3
  228. remote_spawn host $testrun
  229. set result [remote_wait host 240]
  230. set return_code [lindex $result 0]
  231. if { $return_code != 0 } {
  232. perror "Can't execute `$testrun' to check for `$optname'"
  233. return 0
  234. }
  235. set output [lindex $result 1]
  236. # Remove \r as for regular runs.
  237. regsub -all -- "\r" $output "" output
  238. # The option output format for --help for each line where an
  239. # option name is mentioned, is assumed to be two space followed
  240. # by the option name followed by a space or left square bracket,
  241. # like in (optname=--foo): " --foo " or " --foo[this|that]".
  242. # Beware not to match " --foo-bar" nor " --foobar".
  243. if [string match "*\n $optname\[\[ \]*" $output] {
  244. verbose -log "Found `$optname'" 3
  245. return 1
  246. }
  247. verbose -log "Did not find `$optname'" 3
  248. return 0
  249. }
  250. # Run testcase NAME.
  251. # NAME is either a fully specified file name, or just the file name in which
  252. # case $srcdir/$subdir will be prepended.
  253. # REQUESTED_MACHS is a list of machines to run the testcase on. If NAME isn't
  254. # for the specified machine(s), it is ignored.
  255. # Typically REQUESTED_MACHS contains just one element, it is up to the caller
  256. # to iterate over the desired machine variants.
  257. #
  258. # The file can contain options in the form "# option(mach list): value".
  259. # Possibilities:
  260. # mach: [all | machine names]
  261. # as[(mach-list)]: <assembler options>
  262. # ld[(mach-list)]: <linker options>
  263. # cc[(mach-list)]: <compiler options>
  264. # sim[(mach-list)]: <simulator options>
  265. # progopts: <arguments to the program being simulated>
  266. # progos: OS required for the test
  267. # status: program exit status to treat as "pass"
  268. # output: program output pattern to match with string-match
  269. # xerror: program is expected to return with a "failure" exit code
  270. # xfail: <PRMS-opt> <target-triplets-where-test-fails>
  271. # kfail: <PRMS> <target-triplets-where-test-fails>
  272. # If `output' is not specified, the program must output "pass" if !xerror or
  273. # "fail" if xerror.
  274. # The parens in "optname()" are optional if the specification is for all machs.
  275. # Multiple "output", "xfail" and "kfail" options concatenate.
  276. # The xfail and kfail arguments are space-separated target triplets and PRIDs.
  277. # There must be a PRMS (bug report ID) specified for kfail, while it's
  278. # optional for xfail.
  279. proc run_sim_test { name requested_machs } {
  280. global subdir srcdir objdir
  281. global sim_path
  282. global opts
  283. global cpu_option
  284. global cpu_option_sep
  285. global SIMFLAGS_FOR_TARGET
  286. global global_as_works
  287. global global_cpp_works
  288. global global_cc_works
  289. global global_cc_os
  290. if ![file exists $sim_path] {
  291. unsupported "$name: missing simulator $sim_path"
  292. return
  293. }
  294. if [string match "*/*" $name] {
  295. set file $name
  296. set name [file tail $name]
  297. } else {
  298. set file "$srcdir/$subdir/$name"
  299. }
  300. set opt_array [slurp_options "${file}"]
  301. if { $opt_array == -1 } {
  302. unresolved $subdir/$name
  303. return
  304. }
  305. # Clear default options
  306. set opts(as) ""
  307. set opts(ld) ""
  308. set opts(cc) ""
  309. set opts(progopts) ""
  310. set opts(progos) ""
  311. set opts(requires) {}
  312. set opts(sim) ""
  313. set opts(status) "0"
  314. set opts(output) ""
  315. set opts(mach) ""
  316. set opts(timeout) ""
  317. set opts(xerror) "no"
  318. set opts(xfail) ""
  319. set opts(kfail) ""
  320. set seen_output 0
  321. if ![info exists SIMFLAGS_FOR_TARGET] {
  322. set SIMFLAGS_FOR_TARGET ""
  323. }
  324. # Clear any machine specific options specified in a previous test case
  325. foreach m $requested_machs {
  326. if [info exists opts(as,$m)] {
  327. unset opts(as,$m)
  328. }
  329. if [info exists opts(ld,$m)] {
  330. unset opts(ld,$m)
  331. }
  332. if [info exists opts(cc,$m)] {
  333. unset opts(cc,$m)
  334. }
  335. if [info exists opts(sim,$m)] {
  336. unset opts(sim,$m)
  337. }
  338. }
  339. foreach i $opt_array {
  340. set opt_name [lindex $i 0]
  341. set opt_machs [lindex $i 1]
  342. set opt_val [lindex $i 2]
  343. if ![info exists opts($opt_name)] {
  344. perror "unknown option $opt_name in file $file"
  345. unresolved $subdir/$name
  346. return
  347. }
  348. # Multiple "output" specifications concatenate, they don't override.
  349. if { $opt_name == "output" } {
  350. set opt_val "$opts(output)$opt_val"
  351. set seen_output 1
  352. }
  353. # Similar with "xfail" and "kfail", but arguments are space-separated.
  354. if { $opt_name == "xfail" || $opt_name == "kfail" } {
  355. set opt_val "$opts($opt_name) $opt_val"
  356. }
  357. # Similar for "requires", except we append a pair to a list, and
  358. # that doesn't match the processing in the rest of the loop, so we
  359. # "continue" early.
  360. if { $opt_name == "requires" } {
  361. lappend opts($opt_name) [split $opt_val " "]
  362. continue
  363. }
  364. foreach m $opt_machs {
  365. set opts($opt_name,$m) $opt_val
  366. }
  367. if { "$opt_machs" == "" } {
  368. set opts($opt_name) $opt_val
  369. }
  370. }
  371. if { $opts(progos) != "" && $opts(progos) != $global_cc_os } {
  372. untested $subdir/$name
  373. return
  374. }
  375. set testname $name
  376. set sourcefile $file
  377. if { $seen_output == 0 } {
  378. if { "$opts(xerror)" == "no" } {
  379. set opts(output) "pass\n"
  380. } else {
  381. set opts(output) "fail\n"
  382. }
  383. }
  384. # Change \n sequences to newline chars.
  385. regsub -all "\\\\n" $opts(output) "\n" opts(output)
  386. set testcase_machs $opts(mach)
  387. if { "$testcase_machs" == "all" } {
  388. set testcase_machs $requested_machs
  389. }
  390. foreach mach $testcase_machs {
  391. if { [lsearch $requested_machs $mach] < 0 } {
  392. verbose -log "Skipping $mach version of $name, not requested."
  393. continue
  394. }
  395. verbose -log "Testing $name on machine $mach."
  396. # Time to setup xfailures and kfailures.
  397. if { "$opts(xfail)" != "" } {
  398. verbose -log "xfail: $opts(xfail)"
  399. # Using eval to make $opts(xfail) appear as individual
  400. # arguments.
  401. eval setup_xfail $opts(xfail)
  402. }
  403. if { "$opts(kfail)" != "" } {
  404. verbose -log "kfail: $opts(kfail)"
  405. eval setup_kfail $opts(kfail)
  406. }
  407. if ![info exists opts(as,$mach)] {
  408. set opts(as,$mach) $opts(as)
  409. }
  410. set as_options "$opts(as,$mach) -I$srcdir/$subdir"
  411. if [info exists cpu_option] {
  412. if ![info exists cpu_option_sep] {
  413. set sep "="
  414. } {
  415. set sep $cpu_option_sep
  416. }
  417. set as_options "$as_options $cpu_option$sep$mach"
  418. }
  419. regsub {(^ *| +)([^ ]+)} "$as_options" { -Wa,\2} c_as_options
  420. if ![info exists opts(ld,$mach)] {
  421. set opts(ld,$mach) $opts(ld)
  422. }
  423. regsub {(^ *| +)([^ ]+)} "$opts(ld,$mach)" { -Wl,\2} c_ld_options
  424. if ![info exists opts(cc,$mach)] {
  425. set opts(cc,$mach) $opts(cc)
  426. }
  427. foreach req $opts(requires) {
  428. set what [lindex $req 0]
  429. set what_opt [lindex $req 1]
  430. verbose -log "requires: <$what> <$what_opt>"
  431. if { [info procs sim_check_requires_${what}] != [list] } {
  432. if ![sim_check_requires_${what} $what_opt] {
  433. untested $subdir/$name
  434. return
  435. }
  436. } {
  437. perror "unknown requirement `requires: $what' in file $file"
  438. unresolved $subdir/$name
  439. return
  440. }
  441. }
  442. if [string match "*.c" $sourcefile] {
  443. # If we don't have a compiler available, skip tests :(.
  444. if { $global_cc_works == 0 } {
  445. untested $subdir/$name
  446. return
  447. }
  448. set comp_output [target_compile $sourcefile $objdir/${name}.x "executable" \
  449. [list "incdir=$srcdir/$subdir" "additional_flags=$c_as_options $c_ld_options $opts(cc,$mach)"]]
  450. set method "compiling/linking"
  451. } else {
  452. # If we don't have an assembler available, skip tests :(.
  453. if { $global_as_works == 0 } {
  454. untested $subdir/$name
  455. return
  456. }
  457. if [string match "*.S" $sourcefile] {
  458. # If we don't have a preprocessor available, skip tests :(.
  459. if { $global_cpp_works == 0 } {
  460. untested $subdir/$name
  461. return
  462. }
  463. set comp_output [target_compile $sourcefile $objdir/${name}.o "object" \
  464. [list "incdir=$srcdir/$subdir" "additional_flags=$c_as_options"]]
  465. set method "compiling"
  466. } else {
  467. set comp_output [target_assemble $sourcefile $objdir/${name}.o "$as_options"]
  468. set method "assembling"
  469. }
  470. if ![string match "" $comp_output] {
  471. verbose -log "$comp_output" 3
  472. fail "$mach $testname (${method})"
  473. continue
  474. }
  475. set comp_output [target_link $objdir/${name}.o $objdir/${name}.x "$opts(ld,$mach)"]
  476. set method "linking"
  477. }
  478. if ![string match "" $comp_output] {
  479. verbose -log "$comp_output" 3
  480. fail "$mach $testname (${method})"
  481. continue
  482. }
  483. # If no machine specific options, default to the general version.
  484. if ![info exists opts(sim,$mach)] {
  485. set opts(sim,$mach) $opts(sim)
  486. }
  487. # Build the options argument.
  488. set options ""
  489. if { "$opts(timeout)" != "" } {
  490. set options "$options timeout=$opts(timeout)"
  491. }
  492. set result [sim_run $objdir/${name}.x "$opts(sim,$mach) $SIMFLAGS_FOR_TARGET" "$opts(progopts)" "" "$options"]
  493. set return_code [lindex $result 0]
  494. set output [lindex $result 1]
  495. set status fail
  496. if { $return_code == 77 } {
  497. set status unsupported
  498. } elseif { $return_code == $opts(status) } {
  499. set status pass
  500. }
  501. if { "$status" == "pass" } {
  502. if { "$opts(xerror)" == "no" } {
  503. if [string match $opts(output) $output] {
  504. pass "$mach $testname"
  505. file delete $objdir/${name}.o $objdir/${name}.x
  506. } else {
  507. verbose -log "status: $return_code" 3
  508. verbose -log "output: $output" 3
  509. verbose -log "pattern: $opts(output)" 3
  510. fail "$mach $testname (execution)"
  511. }
  512. } else {
  513. verbose -log "`pass' return code when expecting failure" 3
  514. fail "$mach $testname (execution)"
  515. }
  516. } elseif { "$status" == "fail" } {
  517. if { "$opts(xerror)" == "no" } {
  518. fail "$mach $testname (execution)"
  519. } else {
  520. if [string match $opts(output) $output] {
  521. pass "$mach $testname"
  522. file delete $objdir/${name}.o $objdir/${name}.x
  523. } else {
  524. verbose -log "status: $return_code" 3
  525. verbose -log "output: $output" 3
  526. verbose -log "pattern: $opts(output)" 3
  527. fail "$mach $testname (execution)"
  528. }
  529. }
  530. } else {
  531. $status "$mach $testname"
  532. }
  533. }
  534. }
  535. # Subroutine of run_sim_test to process options in FILE.
  536. proc slurp_options { file } {
  537. global subdir srcdir
  538. if [catch { set f [open $file r] } x] {
  539. #perror "couldn't open `$file': $x"
  540. perror "$x"
  541. return -1
  542. }
  543. set opt_array {}
  544. # whitespace expression
  545. set ws {[ ]*}
  546. set nws {[^ ]*}
  547. # whitespace is ignored anywhere except within the options list;
  548. # option names are alphabetic only
  549. set pat "^#${ws}(\[a-zA-Z\]*)\\(?(\[^):\]*)\\)?$ws:${ws}(.*)$ws\$"
  550. # Allow arbitrary lines until the first option is seen.
  551. set seen_opt 0
  552. while { [gets $f line] != -1 } {
  553. set line [string trim $line]
  554. # Whitespace here is space-tab.
  555. if [regexp $pat $line xxx opt_name opt_machs opt_val] {
  556. # match!
  557. set opt_val [string map [list \
  558. {$pwd} [pwd] \
  559. {$srcdir} "$srcdir" \
  560. {$subdir} "$subdir" \
  561. ] "$opt_val"]
  562. lappend opt_array [list $opt_name $opt_machs $opt_val]
  563. set seen_opt 1
  564. } else {
  565. if { $seen_opt } {
  566. break
  567. }
  568. }
  569. }
  570. close $f
  571. return $opt_array
  572. }