add width info for comparisons
[qbe.git] / tools / test.sh
blob23c666356747aa9642523543de2f0c3547905c98
1 #!/bin/sh
3 dir=`dirname "$0"`
4 bin=$dir/../qbe
5 binref=$dir/../qbe.ref
7 tmp=/tmp/qbe.zzzz
9 drv=$tmp.c
10 asm=$tmp.s
11 asmref=$tmp.ref.s
12 exe=$tmp.exe
13 out=$tmp.out
15 init() {
16 case "$TARGET" in
17 arm64)
18 for p in aarch64-linux-musl aarch64-linux-gnu
20 cc="$p-gcc -no-pie -static"
21 qemu="qemu-aarch64"
23 $cc -v >/dev/null 2>&1 &&
24 $qemu -version >/dev/null 2>&1
25 then
26 if sysroot=$($cc -print-sysroot) && test -n "$sysroot"
27 then
28 qemu="$qemu -L $sysroot"
30 break
32 cc=
33 done
34 if test -z "$cc"
35 then
36 echo "Cannot find arm64 compiler or qemu."
37 exit 1
39 bin="$bin -t arm64"
41 rv64)
42 for p in riscv64-linux-musl riscv64-linux-gnu
44 cc="$p-gcc -no-pie -static"
45 qemu="qemu-riscv64"
47 $cc -v >/dev/null 2>&1 &&
48 $qemu -version >/dev/null 2>&1
49 then
50 if sysroot=$($cc -print-sysroot) && test -n "$sysroot"
51 then
52 qemu="$qemu -L $sysroot"
54 break
56 cc=
57 done
58 if test -z "$cc"
59 then
60 echo "Cannot find riscv64 compiler or qemu."
61 exit 1
63 bin="$bin -t rv64"
65 "")
66 case `uname` in
67 *Darwin*)
68 cc="cc"
70 *OpenBSD*)
71 cc="cc -nopie -lpthread"
73 *FreeBSD*)
74 cc="cc -lpthread"
77 cc="${CC:-cc}"
78 ccpost="-lpthread"
80 esac
81 TARGET=`$bin -t?`
84 echo "Unknown target '$TARGET'."
85 exit 1
87 esac
90 cleanup() {
91 rm -f $drv $asm $exe $out
94 extract() {
95 WHAT="$1"
96 FILE="$2"
98 awk "
99 /^# >>> $WHAT/ {
100 p = 1
101 next
103 /^# <<</ {
104 p = 0
107 " $FILE \
108 | sed -e 's/# //' \
109 | sed -e 's/#$//'
112 once() {
113 t="$1"
115 if ! test -f $t
116 then
117 echo "invalid test file $t" >&2
118 exit 1
122 sed -e 1q $t |
123 grep "skip.* $TARGET\( .*\)\?$" \
124 >/dev/null
125 then
126 return 0
129 printf "%-45s" "$(basename $t)..."
131 if ! $bin -o $asm $t
132 then
133 echo "[qbe fail]"
134 return 1
137 if test -x $binref
138 then
139 $binref -o $asmref $t 2>/dev/null
142 extract driver $t > $drv
143 extract output $t > $out
145 if test -s $drv
146 then
147 src="$drv $asm"
148 else
149 src="$asm"
152 if ! $cc -g -o $exe $src $ccpost
153 then
154 echo "[cc fail]"
155 return 1
158 if test -s $out
159 then
160 $qemu $exe a b c | diff -u - $out
161 ret=$?
162 reason="output"
163 else
164 $qemu $exe a b c
165 ret=$?
166 reason="returned $ret"
169 if test $ret -ne 0
170 then
171 echo "[$reason fail]"
172 return 1
175 echo "[ok]"
177 if test -f $asmref && ! cmp -s $asm $asmref
178 then
179 loc0=`wc -l $asm | cut -d' ' -f1`
180 loc1=`wc -l $asmref | cut -d' ' -f1`
181 printf " asm diff: %+d\n" $(($loc0 - $loc1))
182 return 0
186 #trap cleanup TERM QUIT
188 init
190 if test -z "$1"
191 then
192 echo "usage: tools/test.sh {all, SSAFILE}" 2>&1
193 exit 1
196 case "$1" in
197 "all")
198 fail=0
199 count=0
200 for t in $dir/../test/[!_]*.ssa
202 once $t
203 fail=`expr $fail + $?`
204 count=`expr $count + 1`
205 done
206 if test $fail -ge 1
207 then
208 echo
209 echo "$fail of $count tests failed!"
210 else
211 echo
212 echo "All is fine!"
214 exit $fail
217 once $1
218 exit $?
220 esac