test.sh 3.4 KB

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