1# Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011
2# Free Software Foundation, Inc.
3#
4# This program is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation; either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17# Miscellaneous CRIS simulator testcases in assembly code, testing
18# dv-rv.c and dv-cris.c functions.
19
20# Check whether dv-rv and dv-cris are present.
21
22proc sim_has_rv_and_cris {} {
23    global srcdir
24    global subdir
25    global SIMFLAGS
26    global global_as_options
27    global global_ld_options
28    global global_sim_options
29
30    # We need to assemble and link a trivial program and pass that, in
31    # order to test successful exit.
32
33    # A bit of duplication here for the assembling and linking part;
34    # what we want to do it to run the simulator without affecting the
35    # PASS/FAIL counters, and we can use e.g. run_sim_test for that.
36
37    if ![info exists global_as_options] {
38        set global_as_options ""
39    }
40    if ![info exists global_ld_options] {
41        set global_ld_options ""
42    }
43    if ![info exists global_sim_options] {
44        set global_sim_options ""
45    }
46
47    set comp_output [target_assemble $srcdir/$subdir/quit.s quit.o \
48			 "-I$srcdir/$subdir $global_as_options"]
49
50    if ![string match "" $comp_output] {
51	verbose -log "$comp_output" 3
52	fail "rv sim test setup (assembling)"
53	return 0
54    }
55
56    set comp_output [target_link quit.o quit.x "$global_ld_options"]
57
58    if ![string match "" $comp_output] {
59	verbose -log "$comp_output" 3
60	fail "rv sim test setup (linking)"
61	return 0
62    }
63
64    set result \
65	[sim_run quit.x \
66	     "$global_sim_options --hw-device rv --hw-device cris --hw-info" \
67	     "" "" ""]
68    set return_code [lindex $result 0]
69    set output [lindex $result 1]
70
71    if { "$return_code" == "pass" } {
72	return 1
73    }
74
75    return 0
76}
77
78# Similar to slurp_options, but lines are fixed format "^#r ..." (not
79# "^#{ws}*r:{ws}+" to avoid intruding on slurp_options syntax).  Only
80# trailing whitespace of the "..." is trimmed.  Beware that lines
81# including parameters may not contain ":".
82
83proc slurp_rv { file } {
84    if [catch { set f [open $file r] } x] {
85	#perror "couldn't open `$file': $x"
86	perror "$x"
87	return -1
88    }
89    set rv_array {}
90    # whitespace expression
91    set ws  {[ 	]*}
92    # whitespace is ignored at the end of a line.
93    set pat "^#r (.*)$ws\$"
94    # Allow arbitrary lines until the first option is seen.
95    set seen_opt 0
96    while { [gets $f line] != -1 } {
97	set line [string trim $line]
98	# Whitespace here is space-tab.
99	if [regexp $pat $line xxx cmd] {
100	    # match!
101	    lappend rv_array $cmd
102	    set seen_opt 1
103	} else {
104	    if { $seen_opt } {
105		break
106	    }
107	}
108    }
109    close $f
110    return $rv_array
111}
112
113# The main test loop.
114
115if [istarget cris*-*-*] {
116    global ASFLAGS_FOR_TARGET
117    set has_rv_and_cris [sim_has_rv_and_cris]
118    global global_as_options
119    global global_ld_options
120    global global_sim_options
121
122    set saved_global_sim_options $global_sim_options
123    set saved_global_ld_options $global_ld_options
124    set rvdummy "[file dirname [board_info target sim]]/rvdummy"
125
126    # All machines we test and the corresponding assembler option.
127    # We'll only ever test v10 and higher here.
128
129    set combos {{"crisv10" "--march=v10 --no-mul-bug-abort"}
130                {"crisv32" "--march=v32"}}
131
132    # We need to pass different assembler flags for each machine.
133    # Specifying it here rather than adding a specifier to each and every
134    # test-file is preferrable.
135
136    foreach combo $combos {
137	set mach [lindex $combo 0]
138	set ASFLAGS_FOR_TARGET "[lindex $combo 1]"
139
140	# The .ms suffix is for "miscellaneous .s".
141	foreach src [lsort [glob -nocomplain $srcdir/$subdir/*.ms]] {
142
143	    # If we're only testing specific files and this isn't one of them,
144	    # skip it.
145	    if ![runtest_file_p $runtests $src] {
146		continue
147	    }
148
149	    # Whoever runs the test should be alerted that not all
150	    # testcases have been checked; that's why we do the loop
151	    # and don't just return at the top.
152	    if !$has_rv_and_cris {
153		untested $src
154		continue
155	    }
156
157	    set sim_defaults "--hw-file $srcdir/$subdir/std.dev"
158	    set ld_defaults "--section-start=.text=0"
159
160	    # We parse options an extra time besides in run_sim_test,
161	    # to determine if our defaults should be overridden.
162
163	    set opt_array [slurp_options $src]
164	    foreach i $opt_array {
165		set opt_name [lindex $i 0]
166		set opt_machs [lindex $i 1]
167		set opt_val [lindex $i 2]
168
169		# Allow concatenating to the default options by
170		# specifying a mach.
171		if { $opt_name == "sim" && $opt_machs == "" } {
172		    set sim_defaults ""
173		}
174
175		if { $opt_name == "ld" && $opt_machs == "" } {
176		    set ld_defaults ""
177		}
178	    }
179
180	    set rvdummy_id -1
181	    set hostcmds [slurp_rv $src]
182
183	    if { $hostcmds != "" } {
184		# I guess we could ask to have rvdummy executed on a
185		# remote host, but it looks like too much trouble for
186		# a feature rarely used.
187		if [is_remote host] {
188		    untested $src
189		    continue
190		}
191
192		set src_components [file split $src]
193		set rvfile "[lindex $src_components \
194			    [expr [llength $src_components] - 1]].r"
195
196		if [catch { set f [open $rvfile w] } x] {
197		    error "$x"
198		} {
199		    set contents [join $hostcmds "\n"]
200
201		    # Make it possible to use files from the test
202		    # source directory; expected with the @-command.
203		    regsub -all "@srcdir@" $contents "$srcdir/$subdir" contents
204
205		    verbose "rv: $contents" 2
206		    puts $f $contents
207		    close $f
208		}
209
210		spawn -noecho $rvdummy "$rvfile"
211		if { $spawn_id < 0 } {
212		    error "Couldn't spawn $rvdummy"
213		    continue
214		}
215		set rvdummy_id $spawn_id
216	    }
217
218	    # Unfortunately this seems like the only way to pass
219	    # additional sim, ld etc. options to run_sim_test.
220	    set global_sim_options "$saved_global_sim_options $sim_defaults"
221	    set global_ld_options "$saved_global_ld_options $ld_defaults"
222	    run_sim_test $src $mach
223	    set global_sim_options $saved_global_sim_options
224	    set global_ld_options $saved_global_ld_options
225
226	    # Stop the rvdummy, if it's still running.  We need to
227	    # wait on it anyway to avoid it turning into a zombie.
228	    if { $rvdummy_id != -1 } {
229		close -i $rvdummy_id
230		wait -i $rvdummy_id
231
232		# Gleaned from framework.exp, this seems an indicator
233		# to whether the test had expected outcome.  If so, we
234		# want to remove the rv-file.
235		if { $exit_status == 0 } {
236		    file delete $rvfile
237		}
238	    }
239	}
240    }
241}
242