qbe

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

test.sh (2535B)


      1 #!/bin/sh
      2 
      3 dir=`cd $(dirname "$0"); pwd`
      4 bin=$dir/../obj/qbe
      5 binref=$dir/../obj/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 testcc() {
     16 	echo "int main() { }" | $1 -x c -o /dev/null - >/dev/null 2>&1
     17 	return $?
     18 }
     19 
     20 init() {
     21 	case "$TARGET" in
     22 	arm64)
     23 		for p in aarch64-linux-musl aarch64-linux-gnu
     24 		do
     25 			cc="$p-gcc -no-pie -static"
     26 			qemu="qemu-aarch64"
     27 			if
     28 				$cc -v >/dev/null 2>&1 &&
     29 				$qemu -version >/dev/null 2>&1
     30 			then
     31 				if sysroot=$($cc -print-sysroot) && test -n "$sysroot"
     32 				then
     33 					qemu="$qemu -L $sysroot"
     34 				fi
     35 				break
     36 			fi
     37 			cc=
     38 		done
     39 		if test -z "$cc"
     40 		then
     41 			echo "Cannot find arm64 compiler or qemu."
     42 			exit 1
     43 		fi
     44 		bin="$bin -t arm64"
     45 		;;
     46 	"")
     47 		case `uname` in
     48 		*Darwin*)
     49 			cc="cc -Wl,-no_pie"
     50 			;;
     51 		*OpenBSD*)
     52 			cc="cc -nopie"
     53 			;;
     54 		*FreeBSD*)
     55 			cc="cc"
     56 			;;
     57 		*)
     58 			cc="cc -no-pie"
     59 			testcc "$cc" || cc="cc"
     60 			;;
     61 		esac
     62 		;;
     63 	*)
     64 		echo "Unknown target '$TARGET'."
     65 		exit 1
     66 		;;
     67 	esac
     68 }
     69 
     70 cleanup() {
     71 	rm -f $drv $asm $exe $out
     72 }
     73 
     74 extract() {
     75 	WHAT="$1"
     76 	FILE="$2"
     77 
     78 	awk "
     79 		/^# >>> $WHAT/ {
     80 			p = 1
     81 			next
     82 		}
     83 		/^# <<</ {
     84 			p = 0
     85 		}
     86 		p
     87 	" $FILE \
     88 	| sed -e 's/# //' \
     89 	| sed -e 's/#$//'
     90 }
     91 
     92 once() {
     93 	t="$1"
     94 
     95 	if ! test -f $t
     96 	then
     97 		echo "invalid test file $t" >&2
     98 		exit 1
     99 	fi
    100 
    101 	if
    102 		sed -e 1q $t |
    103 		grep "skip.* $TARGET\( .*\)\?$" \
    104 		>/dev/null
    105 	then
    106 		return 0
    107 	fi
    108 
    109 	printf "%-45s" "$(basename $t)..."
    110 
    111 	if ! $bin -o $asm $t
    112 	then
    113 		echo "[qbe fail]"
    114 		return 1
    115 	fi
    116 
    117 	if test -x $binref
    118 	then
    119 		$binref -o $asmref $t 2>/dev/null
    120 	fi
    121 
    122 	extract driver $t > $drv
    123 	extract output $t > $out
    124 
    125 	if test -s $drv
    126 	then
    127 		src="$drv $asm"
    128 	else
    129 		src="$asm"
    130 	fi
    131 
    132 	if ! $cc -g -o $exe $src
    133 	then
    134 		echo "[cc fail]"
    135 		return 1
    136 	fi
    137 
    138 	if test -s $out
    139 	then
    140 		$qemu $exe a b c | diff -u - $out
    141 		ret=$?
    142 		reason="output"
    143 	else
    144 		$qemu $exe a b c
    145 		ret=$?
    146 		reason="returned $ret"
    147 	fi
    148 
    149 	if test $ret -ne 0
    150 	then
    151 		echo "[$reason fail]"
    152 		return 1
    153 	fi
    154 
    155 	echo "[ok]"
    156 
    157 	if test -f $asmref && ! cmp -s $asm $asmref
    158 	then
    159 		loc0=`wc -l $asm    | cut -d' ' -f1`
    160 		loc1=`wc -l $asmref | cut -d' ' -f1`
    161 		printf "    asm diff: %+d\n" $(($loc0 - $loc1))
    162 		return 0
    163 	fi
    164 }
    165 
    166 #trap cleanup TERM QUIT
    167 
    168 init
    169 
    170 if test -z "$1"
    171 then
    172 	echo "usage: tools/test.sh {all, SSAFILE}" 2>&1
    173 	exit 1
    174 fi
    175 
    176 case "$1" in
    177 "all")
    178 	fail=0
    179 	for t in $dir/../test/[!_]*.ssa
    180 	do
    181 		once $t
    182 		fail=`expr $fail + $?`
    183 	done
    184 	if test $fail -ge 1
    185 	then
    186 		echo
    187 		echo "$fail test(s) failed!"
    188 	else
    189 		echo
    190 		echo "All is fine!"
    191 	fi
    192 	exit $fail
    193 	;;
    194 *)
    195 	once $1
    196 	exit $?
    197 	;;
    198 esac