run_tests.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #!/bin/sh
  2. # Step-1 regression: M2 -> gen_ssa/*.ssa -> qbe -> cc -> exit code.
  3. # Usage: ./run_tests.sh (from the compiler directory)
  4. pass=0; fail=0
  5. modof() {
  6. sed -n 's/^MODULE \([A-Za-z][A-Za-z0-9]*\).*/\1/p' "tests/$1" | head -n 1
  7. }
  8. expect_run() {
  9. # $1 = test file (no dir), $2 = expected exit code
  10. name="$1"; want="$2"; mod=$(modof "$name")
  11. rm -f "gen_ssa/$mod.ssa" "gen_ssa/$mod.s" "gen_ssa/$mod"
  12. if ./M2 "tests/$name" 2>&1 | grep -q "Parsed correctly"; then :; else
  13. fail=$((fail+1)); echo "FAIL(run): $name rejected"; return
  14. fi
  15. if [ ! -f "gen_ssa/$mod.ssa" ]; then
  16. fail=$((fail+1)); echo "FAIL(run): $name no gen_ssa/$mod.ssa"; return
  17. fi
  18. if ! qbe -o "gen_ssa/$mod.s" "gen_ssa/$mod.ssa" 2> "gen_ssa/$mod.qbeerr"; then
  19. fail=$((fail+1)); echo "FAIL(run): $name qbe:"; cat "gen_ssa/$mod.qbeerr"; return
  20. fi
  21. if ! cc "gen_ssa/$mod.s" -o "gen_ssa/$mod" 2> "gen_ssa/$mod.ccerr"; then
  22. fail=$((fail+1)); echo "FAIL(run): $name cc:"; cat "gen_ssa/$mod.ccerr"; return
  23. fi
  24. timeout 10 "gen_ssa/$mod" > /dev/null 2>&1; got=$?
  25. if [ "$got" = "$want" ]; then
  26. pass=$((pass+1)); echo "PASS(run): $name -> $got"
  27. else
  28. fail=$((fail+1)); echo "FAIL(run): $name got $got want $want"
  29. fi
  30. }
  31. expect_fail() {
  32. # $1 = test file, $2 = message text expected in the .LST
  33. name="$1"; want="$2"
  34. if ./M2 "tests/$name" 2>&1 | grep -q "Incorrect source"; then :; else
  35. fail=$((fail+1)); echo "FAIL(fail): $name accepted"; return
  36. fi
  37. lst="tests/$(basename "$name" .mod).LST"
  38. if grep -q "$want" "$lst" 2>/dev/null; then
  39. pass=$((pass+1)); echo "PASS(fail): $name [$want]"
  40. else
  41. fail=$((fail+1)); echo "FAIL(fail): [$want] not found in $lst"
  42. fi
  43. }
  44. mkdir -p gen_ssa
  45. expect_run t_minimal.mod 0
  46. expect_run t_exit.mod 7
  47. expect_run t_arith.mod 25
  48. expect_run showcase1.mod 50
  49. expect_run t_types.mod 8
  50. expect_run showcase2.mod 71
  51. expect_run t_if.mod 25
  52. expect_run t_while.mod 55
  53. expect_run t_repeat.mod 55
  54. expect_run t_loop.mod 55
  55. expect_run t_for.mod 85
  56. expect_run t_case.mod 103
  57. expect_run t_bool.mod 42
  58. expect_run t_real.mod 31
  59. expect_run t_array.mod 108
  60. expect_run t_nestidx.mod 42
  61. expect_run t_constfold.mod 42
  62. expect_run t_emptystat.mod 42
  63. expect_run t_highlen.mod 18
  64. expect_run t_builtins.mod 42
  65. expect_run t_long.mod 42
  66. expect_run t_charbounds.mod 3
  67. expect_run t_record.mod 160
  68. expect_run t_with.mod 114
  69. expect_run t_set.mod 131
  70. expect_run t_setchar.mod 77
  71. expect_run t_setrange.mod 55
  72. expect_fail t_classdef.mod "not supported yet"
  73. expect_fail t_class.mod "not supported yet"
  74. expect_fail t_bad_parent.mod "undeclared identifier"
  75. expect_run showcase3.mod 183
  76. expect_run showcase4.mod 44
  77. expect_fail showcase5.mod "not supported yet"
  78. expect_run showcase6.mod 34
  79. expect_run showcase7.mod 219
  80. expect_run showcase8.mod 170
  81. expect_run showcase9.mod 140
  82. expect_run showcase10.mod 86
  83. expect_run t_pointer.mod 42
  84. expect_run t_ptrarray.mod 15
  85. expect_run t_ptrrec.mod 15
  86. expect_run t_ptrnest.mod 10
  87. expect_fail t_opaqueptr.mod ""
  88. expect_fail t_enumdecl.mod "not supported yet"
  89. expect_run t_proc.mod 0
  90. expect_run t_forward.mod 0
  91. expect_run t_call.mod 125
  92. expect_run t_varpar.mod 40
  93. expect_run t_mutual.mod 11
  94. expect_run t_withfield.mod 13
  95. expect_run t_valarray.mod 6
  96. expect_run t_openarr.mod 7
  97. expect_run t_nestedproc.mod 20
  98. expect_run t_nestdeep.mod 211
  99. expect_run t_nestrec.mod 15
  100. expect_fail t_bad_undecl.mod "undeclared identifier"
  101. expect_fail t_bad_dup.mod "duplicate identifier"
  102. expect_fail t_bad_mismatch.mod "module name mismatch"
  103. expect_fail t_bad_opaque.mod "opaque type outside definition"
  104. expect_fail t_bad_while.mod "BOOLEAN condition required"
  105. expect_fail t_bad_exit.mod "not supported yet"
  106. expect_fail t_bad_return.mod "bad RETURN"
  107. expect_fail t_bad_for.mod "FOR needs integer variable and bounds"
  108. expect_fail t_bad_mixed.mod "incompatible assignment"
  109. expect_fail t_bad_arity.mod "invalid call"
  110. expect_fail t_bad_actual.mod "invalid call"
  111. expect_fail t_bad_funstmt.mod "invalid call"
  112. expect_fail t_bad_deref.mod "not a POINTER type"
  113. expect_fail t_bad_new.mod "not a POINTER type"
  114. expect_fail t_bad_field.mod "unknown field"
  115. expect_fail t_bad_with.mod "not a RECORD type"
  116. expect_fail t_bad_recassign.mod "incompatible assignment"
  117. expect_fail t_bad_setelem.mod "set operand mismatch"
  118. expect_fail t_bad_setbase.mod "not supported yet"
  119. expect_fail t_bad_in.mod "set operand mismatch"
  120. expect_fail t_bad_index.mod "array index must be integer"
  121. expect_fail t_bad_bounds.mod "not supported yet"
  122. expect_fail t_bad_varbound.mod "not supported yet"
  123. expect_fail t_bad_manydims.mod "not supported yet"
  124. expect_run t_openformal.mod 0
  125. expect_run t_valopen.mod 15
  126. expect_fail t_bad_open.mod "not supported yet"
  127. expect_trap() {
  128. # $1 = test file — must parse, assemble, link, then abort (rc=134)
  129. name="$1"; mod=$(modof "$name")
  130. rm -f "gen_ssa/$mod.ssa" "gen_ssa/$mod.s" "gen_ssa/$mod"
  131. if ./M2 "tests/$name" 2>&1 | grep -q "Parsed correctly"; then :; else
  132. fail=$((fail+1)); echo "FAIL(trap): $name rejected"; return
  133. fi
  134. if ! qbe -o "gen_ssa/$mod.s" "gen_ssa/$mod.ssa" 2> "gen_ssa/$mod.qbeerr"; then
  135. fail=$((fail+1)); echo "FAIL(trap): $name qbe"; return
  136. fi
  137. if ! cc "gen_ssa/$mod.s" -o "gen_ssa/$mod" 2> "gen_ssa/$mod.ccerr"; then
  138. fail=$((fail+1)); echo "FAIL(trap): $name cc"; return
  139. fi
  140. timeout 10 "gen_ssa/$mod" > /dev/null 2>&1; got=$?
  141. if [ "$got" = "134" ]; then
  142. pass=$((pass+1)); echo "PASS(trap): $name -> abort"
  143. else
  144. fail=$((fail+1)); echo "FAIL(trap): $name rc=$got want 134"
  145. fi
  146. }
  147. expect_trap t_trap_index.mod
  148. expect_trap t_trap_count.mod
  149. expect_trap t_trap_setspan.mod
  150. expect_trap t_trap_emptystr.mod
  151. expect_fail t_defunit.mod ""
  152. expect_fail t_implunit.mod ""
  153. expect_fail t_nested.mod "not supported yet"
  154. # --- separate compilation (step 4.3): $1 prog module, $2 exit code,
  155. # then the session's source files (program last by convention) ---
  156. SHIM="../runtime/syslib/shim.c"
  157. # Build a session image; echoes 0 on success, non-zero on failure.
  158. build_session() {
  159. prog="$1"; shift
  160. files=""
  161. for f in "$@"; do
  162. case "$f" in */*) files="$files $f";; *) files="$files tests/$f";; esac
  163. done
  164. rm -f "gen_ssa/$prog.ssa" "gen_ssa/$prog.s" "gen_ssa/$prog"
  165. if ./M2 $files 2>&1 | grep -q "Parsed correctly"; then :; else
  166. echo "rejected"; return 1
  167. fi
  168. [ -f "gen_ssa/$prog.ssa" ] || { echo "no image"; return 1; }
  169. qbe -o "gen_ssa/$prog.s" "gen_ssa/$prog.ssa" 2> "gen_ssa/$prog.qbeerr" \
  170. || { echo "qbe"; cat "gen_ssa/$prog.qbeerr"; return 1; }
  171. cc "gen_ssa/$prog.s" "$SHIM" -o "gen_ssa/$prog" -lm 2> "gen_ssa/$prog.ccerr" \
  172. || { echo "cc"; cat "gen_ssa/$prog.ccerr"; return 1; }
  173. return 0
  174. }
  175. expect_run_files() {
  176. prog="$1"; want="$2"; shift 2
  177. if ! build_session "$prog" "$@"; then
  178. fail=$((fail+1)); echo "FAIL(session): $prog $? ($(build_session "$prog" "$@" 2>&1))"; return
  179. fi
  180. timeout 10 "gen_ssa/$prog" > /dev/null 2>&1; got=$?
  181. if [ "$got" = "$want" ]; then
  182. pass=$((pass+1)); echo "PASS(session): $prog -> $got"
  183. else
  184. fail=$((fail+1)); echo "FAIL(session): $prog got $got want $want"
  185. fi
  186. }
  187. expect_run_files_out() {
  188. # $1 prog, $2 exit code, $3 expected first output line, then files
  189. prog="$1"; want="$2"; wantline="$3"; shift 3
  190. if ! build_session "$prog" "$@"; then
  191. fail=$((fail+1)); echo "FAIL(session-out): $prog build failed"; return
  192. fi
  193. out=$(timeout 10 "gen_ssa/$prog" 2>/dev/null); got=$?
  194. first=$(printf '%s\n' "$out" | head -n 1)
  195. if [ "$got" = "$want" ] && [ "$first" = "$wantline" ]; then
  196. pass=$((pass+1)); echo "PASS(session-out): $prog -> $got [$first]"
  197. else
  198. fail=$((fail+1)); echo "FAIL(session-out): $prog rc=$got line=[$first] want=[$wantline]"
  199. fi
  200. }
  201. expect_run_files DBasicProg 49 d_basic.def d_basic.mod d_basic_prog.mod
  202. expect_run_files DQualProg 46 d_basic.def d_basic.mod d_qual_prog.mod
  203. expect_run_files DTypesProg 36 d_types.def d_types.mod d_types_prog.mod
  204. expect_run_files DOpaqueProg 55 d_opaque.def d_opaque.mod d_opaque_prog.mod
  205. expect_run_files FioProg 42 ../runtime/syslib/SysShim.def ../runtime/syslib/SysShim.mod ../runtime/syslib/FileIO.def ../runtime/syslib/FileIO.mod fio_prog.mod
  206. expect_run_files StorageProg 42 ../stdlib/storage.def ../stdlib/storage.mod storage_prog.mod
  207. expect_run_files ClashProg 60 d_clash_a.def d_clash_a.mod d_clash_b.def d_clash_b.mod d_clash_prog.mod
  208. expect_run_files_out Hello 0 "Hello, Modula-2!" ../stdlib/sysio.def ../stdlib/sysio.mod hello.mod
  209. expect_run_files_out StringsProg 0 "Hello World" ../stdlib/sysio.def ../stdlib/sysio.mod ../stdlib/strings.def ../stdlib/strings.mod strings_prog.mod
  210. expect_run_files MathProg 12 ../stdlib/sysio.def ../stdlib/sysio.mod ../stdlib/math.def ../stdlib/math.mod math_prog.mod
  211. expect_run_files_in() {
  212. # $1 prog, $2 exit code, $3 expected first line, $4 stdin, then files
  213. prog="$1"; want="$2"; wantline="$3"; input="$4"; shift 4
  214. if ! build_session "$prog" "$@"; then
  215. fail=$((fail+1)); echo "FAIL(session-in): $prog build failed"; return
  216. fi
  217. out=$(printf '%s\n' "$input" | timeout 10 "gen_ssa/$prog" 2>/dev/null); got=$?
  218. first=$(printf '%s\n' "$out" | head -n 1)
  219. if [ "$got" = "$want" ] && [ "$first" = "$wantline" ]; then
  220. pass=$((pass+1)); echo "PASS(session-in): $prog -> $got [$first]"
  221. else
  222. fail=$((fail+1)); echo "FAIL(session-in): $prog rc=$got line=[$first] want=[$wantline]"
  223. fi
  224. }
  225. expect_run_files_in ReadProg 0 "42" "20 22" ../stdlib/sysio.def ../stdlib/sysio.mod read_prog.mod
  226. expect_run_files_in WholeIOProg 0 "42" "6 7" ../stdlib/sysio.def ../stdlib/sysio.mod ../stdlib/wholeio.def ../stdlib/wholeio.mod wholeio_prog.mod
  227. expect_run_files FilesProg 42 ../stdlib/files.def ../stdlib/files.mod files_prog.mod
  228. expect_run_files TBadFile 42 ../stdlib/files.def ../stdlib/files.mod t_badfile.mod
  229. rm -f gen_ssa/_files_tmp.txt
  230. expect_run_files_in Showcase11 80 "Modula-2" "42" \
  231. ../stdlib/sysio.def ../stdlib/sysio.mod \
  232. ../stdlib/textio.def ../stdlib/textio.mod \
  233. ../stdlib/wholeio.def ../stdlib/wholeio.mod \
  234. ../stdlib/strings.def ../stdlib/strings.mod \
  235. ../stdlib/math.def ../stdlib/math.mod \
  236. ../stdlib/files.def ../stdlib/files.mod \
  237. showcase11.mod
  238. rm -f gen_ssa/_showcase11.txt
  239. # step 8.2/8.3: const folding, forward types, trailing ';', empty
  240. # statements, 1-char strings, qualified type names.
  241. expect_run_files Showcase12 42 showcase12lib.def showcase12lib.mod showcase12.mod
  242. expect_run_files_out MixProg 0 "7" ../stdlib/textio.def ../stdlib/textio.mod ../stdlib/sysio.def ../stdlib/sysio.mod ../stdlib/files.def ../stdlib/files.mod d_mix_prog.mod
  243. rm -f gen_ssa/_mix.txt
  244. expect_fail_files() {
  245. # $1 = listing basename, $2 = message (may be empty), then files
  246. lst="$1"; want="$2"; shift 2
  247. files=""
  248. for f in "$@"; do files="$files tests/$f"; done
  249. if ./M2 $files 2>&1 | grep -q "Incorrect source"; then :; else
  250. fail=$((fail+1)); echo "FAIL(session-rej): $lst accepted"; return
  251. fi
  252. if [ -z "$want" ]; then
  253. pass=$((pass+1)); echo "PASS(session-rej): $lst"
  254. elif grep -q "$want" "tests/$lst.LST" 2>/dev/null; then
  255. pass=$((pass+1)); echo "PASS(session-rej): $lst [$want]"
  256. else
  257. fail=$((fail+1)); echo "FAIL(session-rej): [$want] not in tests/$lst.LST"
  258. fi
  259. }
  260. expect_fail_files d_badimp_prog "undeclared identifier" d_basic.def d_basic.mod d_badimp_prog.mod
  261. # --- step 7 gate: compiler sources free of gm2-only imports.
  262. # FileIO is the last module to port (see docs/summary_step7.md);
  263. # it is excluded here so the gate guards everything else. ---
  264. if grep -qE "(IMPORT|FROM)[[:space:]]+.*\b(Environment|FileSysOp|TextIO|RawIO|WholeIO|IOChan|ProgramArgs)\b" \
  265. src/SymTab.def src/SymTab.mod src/QbeGen.def src/QbeGen.mod \
  266. src/M2S.mod src/M2P.mod src/M2.mod src/*.frm 2>/dev/null; then
  267. fail=$((fail+1)); echo "FAIL(gate): gm2-only imports present"
  268. else
  269. pass=$((pass+1)); echo "PASS(gate): no gm2-only imports (FileIO port pending)"
  270. fi
  271. echo "--- $pass passed, $fail failed ---"
  272. [ "$fail" -eq 0 ]