test.sh 2.9 KB

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