qbe

Internal scc patchset buffer for QBE
Log | Files | Refs | README | LICENSE

test.sh (2900B)


      1 #!/bin/sh
      2 
      3 dir=`dirname "$0"`
      4 bin=$dir/../qbe
      5 binref=$dir/../qbe.ref
      6 
      7 tmp=/tmp/qbe.zzzz
      8 
      9 drv=$tmp.c
     10 asm=$tmp.s
     11 asmref=$tmp.ref.s
     12 exe=$tmp.exe
     13 out=$tmp.out
     14 
     15 init() {
     16 	case "$TARGET" in
     17 	arm64)
     18 		for p in aarch64-linux-musl aarch64-linux-gnu
     19 		do
     20 			cc="$p-gcc -no-pie -static"
     21 			qemu="qemu-aarch64"
     22 			if
     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"
     29 				fi
     30 				break
     31 			fi
     32 			cc=
     33 		done
     34 		if test -z "$cc"
     35 		then
     36 			echo "Cannot find arm64 compiler or qemu."
     37 			exit 1
     38 		fi
     39 		bin="$bin -t arm64"
     40 		;;
     41 	rv64)
     42 		for p in riscv64-linux-musl riscv64-linux-gnu
     43 		do
     44 			cc="$p-gcc -no-pie -static"
     45 			qemu="qemu-riscv64"
     46 			if
     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"
     53 				fi
     54 				break
     55 			fi
     56 			cc=
     57 		done
     58 		if test -z "$cc"
     59 		then
     60 			echo "Cannot find riscv64 compiler or qemu."
     61 			exit 1
     62 		fi
     63 		bin="$bin -t rv64"
     64 		;;
     65 	"")
     66 		case `uname` in
     67 		*Darwin*)
     68 			cc="cc"
     69 			;;
     70 		*OpenBSD*)
     71 			cc="cc -nopie -lpthread"
     72 			;;
     73 		*FreeBSD*)
     74 			cc="cc -lpthread"
     75 			;;
     76 		*)
     77 			cc="${CC:-cc}"
     78 			ccpost="-lpthread"
     79 			;;
     80 		esac
     81 		TARGET=`$bin -t?`
     82 		;;
     83 	*)
     84 		echo "Unknown target '$TARGET'."
     85 		exit 1
     86 		;;
     87 	esac
     88 }
     89 
     90 cleanup() {
     91 	rm -f $drv $asm $exe $out
     92 }
     93 
     94 extract() {
     95 	WHAT="$1"
     96 	FILE="$2"
     97 
     98 	awk "
     99 		/^# >>> $WHAT/ {
    100 			p = 1
    101 			next
    102 		}
    103 		/^# <<</ {
    104 			p = 0
    105 		}
    106 		p
    107 	" $FILE \
    108 	| sed -e 's/# //' \
    109 	| sed -e 's/#$//'
    110 }
    111 
    112 once() {
    113 	t="$1"
    114 
    115 	if ! test -f $t
    116 	then
    117 		echo "invalid test file $t" >&2
    118 		exit 1
    119 	fi
    120 
    121 	if
    122 		sed -e 1q $t |
    123 		grep "skip.* $TARGET\( .*\)\?$" \
    124 		>/dev/null
    125 	then
    126 		return 0
    127 	fi
    128 
    129 	printf "%-45s" "$(basename $t)..."
    130 
    131 	if ! $bin -o $asm $t
    132 	then
    133 		echo "[qbe fail]"
    134 		return 1
    135 	fi
    136 
    137 	if test -x $binref
    138 	then
    139 		$binref -o $asmref $t 2>/dev/null
    140 	fi
    141 
    142 	extract driver $t > $drv
    143 	extract output $t > $out
    144 
    145 	if test -s $drv
    146 	then
    147 		src="$drv $asm"
    148 	else
    149 		src="$asm"
    150 	fi
    151 
    152 	if ! $cc -g -o $exe $src $ccpost
    153 	then
    154 		echo "[cc fail]"
    155 		return 1
    156 	fi
    157 
    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"
    167 	fi
    168 
    169 	if test $ret -ne 0
    170 	then
    171 		echo "[$reason fail]"
    172 		return 1
    173 	fi
    174 
    175 	echo "[ok]"
    176 
    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
    183 	fi
    184 }
    185 
    186 #trap cleanup TERM QUIT
    187 
    188 init
    189 
    190 if test -z "$1"
    191 then
    192 	echo "usage: tools/test.sh {all, SSAFILE}" 2>&1
    193 	exit 1
    194 fi
    195 
    196 case "$1" in
    197 "all")
    198 	fail=0
    199 	for t in $dir/../test/[!_]*.ssa
    200 	do
    201 		once $t
    202 		fail=`expr $fail + $?`
    203 	done
    204 	if test $fail -ge 1
    205 	then
    206 		echo
    207 		echo "$fail test(s) failed!"
    208 	else
    209 		echo
    210 		echo "All is fine!"
    211 	fi
    212 	exit $fail
    213 	;;
    214 *)
    215 	once $1
    216 	exit $?
    217 	;;
    218 esac