run_tests.sh 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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. SHIM="../runtime/syslib/shim.c"
  6. modof() {
  7. sed -n 's/^MODULE \([A-Za-z][A-Za-z0-9]*\).*/\1/p' "tests/$1" | head -n 1
  8. }
  9. expect_run() {
  10. # $1 = test file (no dir), $2 = expected exit code
  11. name="$1"; want="$2"; mod=$(modof "$name")
  12. rm -f "gen_ssa/$mod.ssa" "gen_ssa/$mod.s" "gen_ssa/$mod"
  13. if ./M2 "tests/$name" 2>&1 | grep -q "Parsed correctly"; then :; else
  14. fail=$((fail+1)); echo "FAIL(run): $name rejected"; return
  15. fi
  16. if [ ! -f "gen_ssa/$mod.ssa" ]; then
  17. fail=$((fail+1)); echo "FAIL(run): $name no gen_ssa/$mod.ssa"; return
  18. fi
  19. if ! qbe -o "gen_ssa/$mod.s" "gen_ssa/$mod.ssa" 2> "gen_ssa/$mod.qbeerr"; then
  20. fail=$((fail+1)); echo "FAIL(run): $name qbe:"; cat "gen_ssa/$mod.qbeerr"; return
  21. fi
  22. if ! cc "gen_ssa/$mod.s" "$SHIM" -o "gen_ssa/$mod" 2> "gen_ssa/$mod.ccerr"; then
  23. fail=$((fail+1)); echo "FAIL(run): $name cc:"; cat "gen_ssa/$mod.ccerr"; return
  24. fi
  25. timeout 10 "gen_ssa/$mod" > /dev/null 2>&1; got=$?
  26. if [ "$got" = "$want" ]; then
  27. pass=$((pass+1)); echo "PASS(run): $name -> $got"
  28. else
  29. fail=$((fail+1)); echo "FAIL(run): $name got $got want $want"
  30. fi
  31. }
  32. expect_fail() {
  33. # $1 = test file, $2 = message text expected in the .LST
  34. name="$1"; want="$2"
  35. if ./M2 "tests/$name" 2>&1 | grep -q "Incorrect source"; then :; else
  36. fail=$((fail+1)); echo "FAIL(fail): $name accepted"; return
  37. fi
  38. lst="tests/$(basename "$name" .mod).LST"
  39. if grep -q "$want" "$lst" 2>/dev/null; then
  40. pass=$((pass+1)); echo "PASS(fail): $name [$want]"
  41. else
  42. fail=$((fail+1)); echo "FAIL(fail): [$want] not found in $lst"
  43. fi
  44. }
  45. mkdir -p gen_ssa
  46. expect_run t_minimal.mod 0
  47. expect_run t_exit.mod 7
  48. expect_run t_arith.mod 25
  49. expect_run showcase1.mod 50
  50. expect_run t_types.mod 8
  51. expect_run showcase2.mod 71
  52. expect_run t_if.mod 25
  53. expect_run t_while.mod 55
  54. expect_run t_repeat.mod 55
  55. expect_run t_loop.mod 55
  56. expect_run t_for.mod 85
  57. expect_run t_case.mod 103
  58. expect_run t_bool.mod 42
  59. expect_run t_real.mod 31
  60. expect_run t_array.mod 108
  61. expect_run t_ctor1.mod 0
  62. expect_run t_ctor2.mod 0
  63. expect_run t_ctor3.mod 0
  64. expect_run t_ctor4.mod 0
  65. expect_run t_ctor5.mod 0
  66. expect_run t_ctor6.mod 0
  67. expect_run t_ctor7.mod 42
  68. expect_run t_minmax.mod 42
  69. expect_run t_minmax2.mod 42
  70. expect_run t_setops.mod 42
  71. expect_run t_ptrfwd.mod 42
  72. expect_run t_libc.mod 42
  73. expect_run t_variant2.mod 42
  74. expect_run t_addrptr.mod 42
  75. expect_run t_nestedmod.mod 42
  76. expect_run t_nestidx.mod 42
  77. expect_run t_proctype.mod 42
  78. expect_run t_compat.mod 42
  79. expect_run t_constfold.mod 42
  80. expect_run t_resultsfx.mod 42
  81. expect_run t_coroutine.mod 42
  82. expect_run t_fwdvar.mod 42
  83. expect_run t_fwdsel.mod 42
  84. expect_run t_fwdprop.mod 42
  85. expect_run t_fwdproc.mod 7
  86. expect_run t_emptystat.mod 42
  87. expect_run t_highlen.mod 18
  88. expect_run t_builtins.mod 42
  89. expect_run t_long.mod 42
  90. expect_run t_truncfloat.mod 42
  91. expect_run t_enumidx.mod 42
  92. expect_run t_setenum.mod 42
  93. expect_run t_setconst.mod 42
  94. expect_run t_constbuiltin.mod 42
  95. expect_run t_constreal.mod 42
  96. expect_run t_constlong.mod 42
  97. expect_run t_procaddr.mod 42
  98. expect_run t_convert.mod 42
  99. expect_run t_setword.mod 42
  100. expect_run t_procval.mod 42
  101. expect_run t_real64.mod 42
  102. expect_run t_shiftneg.mod 42
  103. expect_run t_unicode.mod 31
  104. expect_run t_charbounds.mod 3
  105. expect_run t_record.mod 160
  106. expect_run t_recarr.mod 42
  107. expect_run t_recptr.mod 42
  108. expect_run t_nestarr.mod 42
  109. expect_run t_arrrecfld.mod 42
  110. expect_run t_arrrecbig.mod 42
  111. expect_run t_stmtsfx.mod 42
  112. expect_run t_retrec.mod 42
  113. expect_run t_retmeth.mod 42
  114. expect_run t_chain.mod 42
  115. expect_run t_goto.mod 42
  116. expect_fail t_gotobad.mod "undeclared identifier"
  117. expect_run t_under.mod 42
  118. expect_run t_with.mod 114
  119. expect_run t_set.mod 131
  120. expect_run t_setchar.mod 77
  121. expect_run t_setrange.mod 55
  122. expect_run t_classmethod.mod 7
  123. expect_run t_classinherit.mod 7
  124. expect_run t_virtual.mod 3
  125. expect_run t_valconv.mod 15
  126. expect_run t_conv.mod 42
  127. expect_run t_convconst.mod 42
  128. expect_run t_classsibling.mod 7
  129. expect_run t_classinit.mod 42
  130. expect_run t_class.mod 0
  131. expect_run t_longline.mod 0
  132. expect_fail t_bad_parent.mod "undeclared identifier"
  133. expect_run showcase3.mod 183
  134. expect_run showcase4.mod 44
  135. expect_fail showcase5.mod "not supported yet"
  136. expect_run showcase6.mod 34
  137. expect_run showcase7.mod 219
  138. expect_run showcase8.mod 170
  139. expect_run showcase9.mod 140
  140. expect_run showcase10.mod 86
  141. expect_run t_pointer.mod 42
  142. expect_run t_ptrarray.mod 15
  143. expect_run t_ptrrec.mod 15
  144. expect_run t_ptrnest.mod 10
  145. expect_fail t_opaqueptr.mod ""
  146. expect_run t_enumdecl.mod 0
  147. expect_run t_enum.mod 15
  148. expect_run t_strcat.mod 63
  149. expect_run t_charcat.mod 15
  150. expect_run t_variant.mod 31
  151. expect_run t_ustrassign.mod 63
  152. expect_run t_ustrformal.mod 3
  153. expect_run t_ustrcat.mod 63
  154. expect_run t_proc.mod 0
  155. expect_run t_forward.mod 0
  156. expect_run t_call.mod 125
  157. expect_run t_varpar.mod 40
  158. expect_run t_mutual.mod 11
  159. expect_run t_withfield.mod 13
  160. expect_run t_valarray.mod 6
  161. expect_run t_openarr.mod 7
  162. expect_run t_nestedproc.mod 20
  163. expect_run t_nestdeep.mod 211
  164. expect_run t_nestrec.mod 15
  165. expect_fail t_bad_undecl.mod "undeclared identifier"
  166. expect_fail t_bad_dup.mod "duplicate identifier"
  167. expect_fail t_bad_mismatch.mod "module name mismatch"
  168. expect_fail t_bad_opaque.mod "opaque type outside definition"
  169. expect_fail t_bad_while.mod "BOOLEAN condition required"
  170. expect_fail t_bad_exit.mod "not supported yet"
  171. expect_fail t_bad_return.mod "bad RETURN"
  172. expect_fail t_bad_for.mod "FOR needs integer variable and bounds"
  173. expect_fail t_bad_mixed.mod "incompatible assignment"
  174. expect_fail t_bad_fwd.mod "incompatible assignment"
  175. expect_fail t_bad_nestfwd.mod "undeclared identifier"
  176. expect_fail t_fwdtype.mod "undeclared identifier"
  177. expect_fail t_fwdset.mod "undeclared identifier"
  178. expect_fail t_bad_overlong.mod "invalid UTF-8"
  179. expect_fail t_bad_surrogate.mod "invalid UTF-8"
  180. expect_fail t_bad_utf8big.mod "invalid UTF-8"
  181. expect_fail t_bad_truncutf.mod "invalid UTF-8"
  182. expect_fail t_bad_arity.mod "invalid call"
  183. expect_fail t_bad_actual.mod "invalid call"
  184. expect_fail t_bad_funstmt.mod "invalid call"
  185. expect_fail t_bad_deref.mod "not a POINTER type"
  186. expect_fail t_bad_new.mod "not a POINTER type"
  187. expect_fail t_bad_field.mod "unknown field"
  188. expect_fail t_bad_with.mod "not a RECORD type"
  189. expect_fail t_bad_recassign.mod "incompatible assignment"
  190. expect_fail t_bad_setelem.mod "set operand mismatch"
  191. expect_fail t_bad_setbase.mod "not supported yet"
  192. expect_fail t_bad_setconst.mod "not supported yet"
  193. expect_fail t_bad_in.mod "set operand mismatch"
  194. expect_fail t_bad_index.mod "array index must be integer"
  195. expect_fail t_bad_bounds.mod "not supported yet"
  196. expect_fail t_bad_varbound.mod "not supported yet"
  197. expect_fail t_bad_manydims.mod "not supported yet"
  198. expect_run t_openformal.mod 0
  199. expect_run t_valopen.mod 15
  200. expect_fail t_bad_open.mod "not supported yet"
  201. expect_trap() {
  202. # $1 = test file — must parse, assemble, link, then abort (rc=134)
  203. name="$1"; mod=$(modof "$name")
  204. rm -f "gen_ssa/$mod.ssa" "gen_ssa/$mod.s" "gen_ssa/$mod"
  205. if ./M2 "tests/$name" 2>&1 | grep -q "Parsed correctly"; then :; else
  206. fail=$((fail+1)); echo "FAIL(trap): $name rejected"; return
  207. fi
  208. if ! qbe -o "gen_ssa/$mod.s" "gen_ssa/$mod.ssa" 2> "gen_ssa/$mod.qbeerr"; then
  209. fail=$((fail+1)); echo "FAIL(trap): $name qbe"; return
  210. fi
  211. if ! cc "gen_ssa/$mod.s" "$SHIM" -o "gen_ssa/$mod" 2> "gen_ssa/$mod.ccerr"; then
  212. fail=$((fail+1)); echo "FAIL(trap): $name cc"; return
  213. fi
  214. timeout 10 "gen_ssa/$mod" > /dev/null 2>&1; got=$?
  215. if [ "$got" = "134" ]; then
  216. pass=$((pass+1)); echo "PASS(trap): $name -> abort"
  217. else
  218. fail=$((fail+1)); echo "FAIL(trap): $name rc=$got want 134"
  219. fi
  220. }
  221. expect_trap t_trap_index.mod
  222. expect_run t_strtrunc.mod 42
  223. expect_trap t_trap_setspan.mod
  224. expect_run t_strempty.mod 42
  225. expect_trap t_trap_chr8.mod
  226. expect_fail t_defunit.mod ""
  227. expect_fail t_implunit.mod ""
  228. expect_run t_nested.mod 42
  229. # --- separate compilation (step 4.3): $1 prog module, $2 exit code,
  230. # then the session's source files (program last by convention) ---
  231. # Build a session image; echoes 0 on success, non-zero on failure.
  232. build_session() {
  233. prog="$1"; shift
  234. files=""
  235. for f in "$@"; do
  236. case "$f" in */*) files="$files $f";; *) files="$files tests/$f";; esac
  237. done
  238. rm -f "gen_ssa/$prog.ssa" "gen_ssa/$prog.s" "gen_ssa/$prog"
  239. if ./M2 $files 2>&1 | grep -q "Parsed correctly"; then :; else
  240. echo "rejected"; return 1
  241. fi
  242. [ -f "gen_ssa/$prog.ssa" ] || { echo "no image"; return 1; }
  243. qbe -o "gen_ssa/$prog.s" "gen_ssa/$prog.ssa" 2> "gen_ssa/$prog.qbeerr" \
  244. || { echo "qbe"; cat "gen_ssa/$prog.qbeerr"; return 1; }
  245. cc "gen_ssa/$prog.s" "$SHIM" -o "gen_ssa/$prog" -lm 2> "gen_ssa/$prog.ccerr" \
  246. || { echo "cc"; cat "gen_ssa/$prog.ccerr"; return 1; }
  247. return 0
  248. }
  249. expect_run_files() {
  250. prog="$1"; want="$2"; shift 2
  251. if ! build_session "$prog" "$@"; then
  252. fail=$((fail+1)); echo "FAIL(session): $prog $? ($(build_session "$prog" "$@" 2>&1))"; return
  253. fi
  254. timeout 10 "gen_ssa/$prog" > /dev/null 2>&1; got=$?
  255. if [ "$got" = "$want" ]; then
  256. pass=$((pass+1)); echo "PASS(session): $prog -> $got"
  257. else
  258. fail=$((fail+1)); echo "FAIL(session): $prog got $got want $want"
  259. fi
  260. }
  261. expect_run_files_out() {
  262. # $1 prog, $2 exit code, $3 expected first output line, then files
  263. prog="$1"; want="$2"; wantline="$3"; shift 3
  264. if ! build_session "$prog" "$@"; then
  265. fail=$((fail+1)); echo "FAIL(session-out): $prog build failed"; return
  266. fi
  267. out=$(timeout 10 "gen_ssa/$prog" 2>/dev/null); got=$?
  268. first=$(printf '%s\n' "$out" | head -n 1)
  269. if [ "$got" = "$want" ] && [ "$first" = "$wantline" ]; then
  270. pass=$((pass+1)); echo "PASS(session-out): $prog -> $got [$first]"
  271. else
  272. fail=$((fail+1)); echo "FAIL(session-out): $prog rc=$got line=[$first] want=[$wantline]"
  273. fi
  274. }
  275. expect_run_files DBasicProg 49 d_basic.def d_basic.mod d_basic_prog.mod
  276. expect_run_files CForeignProg 42 c_foreign.def c_foreign.mod c_foreign_prog.mod
  277. expect_run_files EEnumProg 42 e_enum.def e_enum_re.def e_enum_prog.mod
  278. expect_run_files TClassDefProg 8 t_classdef.def t_classdef_prog.mod
  279. expect_run_files DQualProg 46 d_basic.def d_basic.mod d_qual_prog.mod
  280. expect_run_files DTypesProg 36 d_types.def d_types.mod d_types_prog.mod
  281. expect_run_files DOpaqueProg 55 d_opaque.def d_opaque.mod d_opaque_prog.mod
  282. expect_run_files TString 42 d_string.def d_string.mod t_string.mod
  283. expect_run_files FioProg 42 ../runtime/syslib/SysShim.def ../runtime/syslib/SysShim.mod ../runtime/syslib/FileIO.def ../runtime/syslib/FileIO.mod fio_prog.mod
  284. expect_run_files StorageProg 42 ../stdlib/storage.def ../stdlib/storage.mod storage_prog.mod
  285. expect_run_files TAst 42 \
  286. ../runtime/syslib/SysShim.def ../runtime/syslib/SysShim.mod \
  287. ../runtime/syslib/FileIO.def ../runtime/syslib/FileIO.mod \
  288. src/AST.def src/AST.mod \
  289. t_ast.mod
  290. expect_run_files GenericProg 42 \
  291. g_ident.def g_ident.mod g_intid.def g_intid.mod g_prog.mod
  292. expect_run_files PairProg 73 \
  293. g_pair.def g_pair.mod g_intchar.def g_intchar.mod g_pair_prog.mod
  294. expect_run_files BufProg 42 \
  295. g_buf.def g_buf.mod g_buf10.def g_buf10.mod g_bufprog.mod
  296. expect_run_files SortProg 3 \
  297. g_cmp.def g_cmp.mod g_sort.def g_sort.mod g_isort.def g_isort.mod g_sortprog.mod
  298. expect_run_files OrdProg 42 \
  299. g_ordbox.def g_ordbox.mod g_intbox.def g_intbox.mod g_ordprog.mod
  300. expect_run_files GWrapProg 42 \
  301. g_ordbox.def g_ordbox.mod g_intbox.def g_intbox.mod \
  302. g_wrap.def g_wrap.mod g_wrapb.def g_wrapb.mod g_wrapprog.mod
  303. expect_run_files GCBoxProg 42 \
  304. g_gcbox.def g_gcbox.mod g_gcint.def g_gcint.mod g_gcprog.mod
  305. expect_run_files GStkProg 42 \
  306. g_gstk.def g_gstk.mod g_gstki.def g_gstki.mod g_gstkp.mod
  307. expect_run_files WrapProg 42 \
  308. ../runtime/syslib/SysShim.def ../runtime/syslib/SysShim.mod \
  309. ../stdlib/iochan.def ../stdlib/iochan.mod \
  310. ../stdlib/stdchans.def ../stdlib/stdchans.mod \
  311. ../stdlib/timestring.def ../stdlib/timestring.mod \
  312. ../stdlib/wrapclock.def ../stdlib/wrapclock.mod \
  313. ../stdlib/wraptime.def ../stdlib/wraptime.mod \
  314. ../stdlib/wrapc.def ../stdlib/wrapc.mod \
  315. wrapprog.mod
  316. expect_run_files_out Showcase23 35 "list: 5 10 20" \
  317. ../stdlib/sysio.def ../stdlib/sysio.mod \
  318. showcase23lib.def showcase23lib.mod \
  319. showcase23int.def showcase23int.mod \
  320. showcase23vis.def showcase23vis.mod showcase23.mod
  321. expect_run_files TImportProc 7 pvi.def pvi.mod t_importproc.mod
  322. expect_run_files_out Showcase22 42 "showcase22 total=42" \
  323. ../runtime/syslib/SysShim.def ../runtime/syslib/SysShim.mod \
  324. ../stdlib/iochan.def ../stdlib/iochan.mod \
  325. ../stdlib/stdchans.def ../stdlib/stdchans.mod \
  326. ../stdlib/timestring.def ../stdlib/timestring.mod \
  327. ../stdlib/wrapclock.def ../stdlib/wrapclock.mod \
  328. ../stdlib/wraptime.def ../stdlib/wraptime.mod \
  329. ../stdlib/wrapc.def ../stdlib/wrapc.mod \
  330. showcase22.mod
  331. expect_run_files Library1 42 \
  332. ../runtime/syslib/SysShim.def ../runtime/syslib/SysShim.mod \
  333. ../stdlib/convtypes.def ../stdlib/convtypes.mod \
  334. ../stdlib/ioconsts.def ../stdlib/ioconsts.mod \
  335. ../stdlib/conversions.def ../stdlib/conversions.mod \
  336. ../stdlib/math.def ../stdlib/math.mod \
  337. ../stdlib/sioresult.def ../stdlib/sioresult.mod \
  338. ../stdlib/TERMINATION.def ../stdlib/TERMINATION.mod \
  339. ../stdlib/wholestr.def ../stdlib/wholestr.mod \
  340. ../stdlib/realstr.def ../stdlib/realstr.mod \
  341. ../stdlib/longstr.def ../stdlib/longstr.mod \
  342. ../stdlib/longmath.def ../stdlib/longmath.mod \
  343. ../stdlib/iochan.def ../stdlib/iochan.mod \
  344. ../stdlib/longio.def ../stdlib/longio.mod \
  345. ../stdlib/strio.def ../stdlib/strio.mod \
  346. ../stdlib/inout.def ../stdlib/inout.mod \
  347. ../stdlib/stdio.def ../stdlib/stdio.mod \
  348. ../stdlib/stextio.def ../stdlib/stextio.mod \
  349. ../stdlib/swholeio.def ../stdlib/swholeio.mod \
  350. ../stdlib/srealio.def ../stdlib/srealio.mod \
  351. library1.mod
  352. expect_run_files TDynStr 42 \
  353. ../stdlib/dynamicstrings.def ../stdlib/dynamicstrings.mod \
  354. ../stdlib/sysstorage.def ../stdlib/sysstorage.mod \
  355. t_dynstr.mod
  356. 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
  357. expect_run_files_out Hello 0 "Hello, Modula-2!" ../stdlib/sysio.def ../stdlib/sysio.mod hello.mod
  358. expect_run_files_out StringsProg 0 "Hello World" ../stdlib/sysio.def ../stdlib/sysio.mod ../stdlib/strings.def ../stdlib/strings.mod strings_prog.mod
  359. expect_run_files StringsSliceProg 255 ../stdlib/strings.def ../stdlib/strings.mod strings_slice_prog.mod
  360. expect_run_files_out Stdlib2Prog 42 "stdlib2 n=42" ../stdlib/sysio.def ../stdlib/sysio.mod ../stdlib/math.def ../stdlib/math.mod ../stdlib/strings.def ../stdlib/strings.mod ../stdlib/charclass.def ../stdlib/charclass.mod stdlib2_prog.mod
  361. expect_run_files MathProg 12 ../stdlib/sysio.def ../stdlib/sysio.mod ../stdlib/math.def ../stdlib/math.mod math_prog.mod
  362. expect_run_files Math2Prog 31 ../stdlib/math.def ../stdlib/math.mod math2_prog.mod
  363. expect_run_files SysClockProg 3 ../stdlib/sysclock.def ../stdlib/sysclock.mod sysclock_prog.mod
  364. expect_run_files Utf8Prog 127 ../runtime/syslib/Utf8.def ../runtime/syslib/Utf8.mod utf8_prog.mod
  365. expect_run_files ConvProg 127 ../stdlib/conversions.def ../stdlib/conversions.mod conv_prog.mod
  366. expect_run_files ConvResultsProg 127 ../stdlib/conversions.def ../stdlib/conversions.mod convresults_prog.mod
  367. expect_run_files TStrEq 15 t_streq.mod
  368. expect_run_files ProgArgsProg 7 ../runtime/syslib/SysShim.def ../runtime/syslib/SysShim.mod ../stdlib/programargs.def ../stdlib/programargs.mod progargs_prog.mod
  369. expect_run_files_out UTextProg 42 "héllo" ../runtime/syslib/Utf8.def ../runtime/syslib/Utf8.mod ../stdlib/textio.def ../stdlib/textio.mod utext_prog.mod
  370. expect_run_files_in() {
  371. # $1 prog, $2 exit code, $3 expected first line, $4 stdin, then files
  372. prog="$1"; want="$2"; wantline="$3"; input="$4"; shift 4
  373. if ! build_session "$prog" "$@"; then
  374. fail=$((fail+1)); echo "FAIL(session-in): $prog build failed"; return
  375. fi
  376. out=$(printf '%s\n' "$input" | timeout 10 "gen_ssa/$prog" 2>/dev/null); got=$?
  377. first=$(printf '%s\n' "$out" | head -n 1)
  378. if [ "$got" = "$want" ] && [ "$first" = "$wantline" ]; then
  379. pass=$((pass+1)); echo "PASS(session-in): $prog -> $got [$first]"
  380. else
  381. fail=$((fail+1)); echo "FAIL(session-in): $prog rc=$got line=[$first] want=[$wantline]"
  382. fi
  383. }
  384. expect_run_files_in ReadProg 0 "42" "20 22" ../stdlib/sysio.def ../stdlib/sysio.mod read_prog.mod
  385. expect_run_files_in WholeIOProg 0 "42" "6 7" ../stdlib/sysio.def ../stdlib/sysio.mod ../stdlib/wholeio.def ../stdlib/wholeio.mod wholeio_prog.mod
  386. expect_run_files_in RealProg 42 "3.500000E+00" "1.25" ../stdlib/realio.def ../stdlib/realio.mod ../stdlib/sysio.def ../stdlib/sysio.mod realio_prog.mod
  387. expect_run_files_in IOChanProg 0 "hello 42" "Z" ../runtime/syslib/SysShim.def ../runtime/syslib/SysShim.mod ../stdlib/iochan.def ../stdlib/iochan.mod iochan_prog.mod
  388. rm -f gen_ssa/_iochan_tmp.txt
  389. expect_run_files IOChanFileProg 3 ../runtime/syslib/SysShim.def ../runtime/syslib/SysShim.mod ../stdlib/strings.def ../stdlib/strings.mod ../stdlib/iochan.def ../stdlib/iochan.mod iochanfile_prog.mod
  390. rm -f gen_ssa/_iochan_tmp.txt
  391. rm -f gen_ssa/_iochanseek.txt
  392. expect_run_files IOChanSeekProg 15 ../runtime/syslib/SysShim.def ../runtime/syslib/SysShim.mod ../stdlib/strings.def ../stdlib/strings.mod ../stdlib/iochan.def ../stdlib/iochan.mod iochanseek_prog.mod
  393. rm -f gen_ssa/_iochanseek.txt
  394. expect_run_files FilesProg 42 ../runtime/syslib/SysShim.def ../runtime/syslib/SysShim.mod ../stdlib/iochan.def ../stdlib/iochan.mod ../stdlib/files.def ../stdlib/files.mod files_prog.mod
  395. expect_run_files TBadFile 42 ../runtime/syslib/SysShim.def ../runtime/syslib/SysShim.mod ../stdlib/iochan.def ../stdlib/iochan.mod ../stdlib/files.def ../stdlib/files.mod t_badfile.mod
  396. rm -f gen_ssa/_files_tmp.txt
  397. expect_run_files_in Showcase11 80 "Modula-2" "42" \
  398. ../stdlib/sysio.def ../stdlib/sysio.mod \
  399. ../runtime/syslib/Utf8.def ../runtime/syslib/Utf8.mod \
  400. ../stdlib/textio.def ../stdlib/textio.mod \
  401. ../stdlib/wholeio.def ../stdlib/wholeio.mod \
  402. ../stdlib/strings.def ../stdlib/strings.mod \
  403. ../stdlib/math.def ../stdlib/math.mod \
  404. ../runtime/syslib/SysShim.def ../runtime/syslib/SysShim.mod ../stdlib/iochan.def ../stdlib/iochan.mod ../stdlib/files.def ../stdlib/files.mod \
  405. showcase11.mod
  406. rm -f gen_ssa/_showcase11.txt
  407. # step 8.2/8.3: const folding, forward types, trailing ';', empty
  408. # statements, 1-char strings, qualified type names.
  409. expect_run_files Showcase12 42 showcase12lib.def showcase12lib.mod showcase12.mod
  410. expect_run_files_out Showcase13 42 "Even(8)=T Odd(7)=T total=42" ../stdlib/sysio.def ../stdlib/sysio.mod showcase13.mod
  411. expect_run_files_out Showcase19 42 "showcase19 total=42" \
  412. ../runtime/syslib/Utf8.def ../runtime/syslib/Utf8.mod \
  413. ../stdlib/textio.def ../stdlib/textio.mod \
  414. ../stdlib/conversions.def ../stdlib/conversions.mod \
  415. showcase19.mod
  416. expect_run_files_out Showcase20 42 "showcase20 total=42" \
  417. ../runtime/syslib/Utf8.def ../runtime/syslib/Utf8.mod \
  418. ../stdlib/textio.def ../stdlib/textio.mod \
  419. ../stdlib/conversions.def ../stdlib/conversions.mod \
  420. showcase20.mod
  421. expect_run_files_out Showcase21 42 "showcase21 total=42" \
  422. ../stdlib/convtypes.def ../stdlib/convtypes.mod \
  423. ../stdlib/conversions.def ../stdlib/conversions.mod \
  424. ../stdlib/math.def ../stdlib/math.mod \
  425. ../stdlib/wholestr.def ../stdlib/wholestr.mod \
  426. ../stdlib/realstr.def ../stdlib/realstr.mod \
  427. ../stdlib/longmath.def ../stdlib/longmath.mod \
  428. ../stdlib/dynamicstrings.def ../stdlib/dynamicstrings.mod \
  429. ../stdlib/strio.def ../stdlib/strio.mod \
  430. showcase21.mod
  431. expect_run_files_out Showcase18 42 "showcase18 areas sum=139" \
  432. ../runtime/syslib/Utf8.def ../runtime/syslib/Utf8.mod \
  433. ../stdlib/textio.def ../stdlib/textio.mod \
  434. ../stdlib/conversions.def ../stdlib/conversions.mod \
  435. showcase18.mod
  436. expect_run_files_out Showcase17 42 "showcase17 total=42" \
  437. ../runtime/syslib/Utf8.def ../runtime/syslib/Utf8.mod \
  438. ../stdlib/textio.def ../stdlib/textio.mod \
  439. ../stdlib/conversions.def ../stdlib/conversions.mod \
  440. showcase17.mod
  441. rm -f gen_ssa/_showcase16.txt
  442. expect_run_files_out Showcase16 42 "showcase16: id=3 total=42" \
  443. ../runtime/syslib/SysShim.def ../runtime/syslib/SysShim.mod \
  444. ../runtime/syslib/Utf8.def ../runtime/syslib/Utf8.mod \
  445. ../stdlib/iochan.def ../stdlib/iochan.mod \
  446. ../stdlib/files.def ../stdlib/files.mod \
  447. ../stdlib/textio.def ../stdlib/textio.mod \
  448. ../stdlib/strings.def ../stdlib/strings.mod \
  449. ../stdlib/conversions.def ../stdlib/conversions.mod \
  450. showcase16.mod
  451. rm -f gen_ssa/_showcase16.txt
  452. rm -f gen_ssa/_showcase15.txt
  453. expect_run_files_out Showcase15 42 "read back: point 42" \
  454. ../runtime/syslib/SysShim.def ../runtime/syslib/SysShim.mod \
  455. ../runtime/syslib/Utf8.def ../runtime/syslib/Utf8.mod \
  456. ../stdlib/iochan.def ../stdlib/iochan.mod \
  457. ../stdlib/textio.def ../stdlib/textio.mod \
  458. ../stdlib/strings.def ../stdlib/strings.mod \
  459. ../stdlib/conversions.def ../stdlib/conversions.mod \
  460. showcase15lib.def showcase15lib.mod showcase15.mod
  461. rm -f gen_ssa/_showcase15.txt
  462. expect_run_files_out Showcase14 42 "showcase14 n=42" \
  463. ../runtime/syslib/Utf8.def ../runtime/syslib/Utf8.mod \
  464. ../runtime/syslib/SysShim.def ../runtime/syslib/SysShim.mod \
  465. ../stdlib/sysio.def ../stdlib/sysio.mod \
  466. ../stdlib/textio.def ../stdlib/textio.mod \
  467. ../stdlib/conversions.def ../stdlib/conversions.mod \
  468. ../stdlib/realio.def ../stdlib/realio.mod \
  469. ../stdlib/programargs.def ../stdlib/programargs.mod \
  470. ../stdlib/iochan.def ../stdlib/iochan.mod \
  471. showcase14.mod
  472. expect_run_files_out MixProg 0 "7" ../runtime/syslib/Utf8.def ../runtime/syslib/Utf8.mod ../stdlib/textio.def ../stdlib/textio.mod ../stdlib/sysio.def ../stdlib/sysio.mod ../runtime/syslib/SysShim.def ../runtime/syslib/SysShim.mod ../stdlib/iochan.def ../stdlib/iochan.mod ../stdlib/files.def ../stdlib/files.mod d_mix_prog.mod
  473. rm -f gen_ssa/_mix.txt
  474. expect_fail_files() {
  475. # $1 = listing basename, $2 = message (may be empty), then files
  476. lst="$1"; want="$2"; shift 2
  477. files=""
  478. for f in "$@"; do files="$files tests/$f"; done
  479. if ./M2 $files 2>&1 | grep -q "Incorrect source"; then :; else
  480. fail=$((fail+1)); echo "FAIL(session-rej): $lst accepted"; return
  481. fi
  482. if [ -z "$want" ]; then
  483. pass=$((pass+1)); echo "PASS(session-rej): $lst"
  484. elif grep -q "$want" "tests/$lst.LST" 2>/dev/null; then
  485. pass=$((pass+1)); echo "PASS(session-rej): $lst [$want]"
  486. else
  487. fail=$((fail+1)); echo "FAIL(session-rej): [$want] not in tests/$lst.LST"
  488. fi
  489. }
  490. expect_fail_files d_badimp_prog "undeclared identifier" d_basic.def d_basic.mod d_badimp_prog.mod
  491. expect_fail_files sig "implementation does not match definition" sig.def sig.mod sig_prog.mod
  492. expect_fail_files g_badbuf "invalid call" g_buf.def g_buf.mod g_badbuf.def
  493. expect_fail_files g_realbox "invalid call" g_ordbox.def g_ordbox.mod g_realbox.def
  494. # --- two-phase parse gate. Deferral is now the DEFAULT (procedure
  495. # bodies are parsed after the unit's declarations); -notwophase
  496. # restores the old inline parse for debugging. Forward-free code
  497. # must be byte-identical either way; forward-reference code must
  498. # still work. ---
  499. twophase_same() {
  500. # $1 = test file (no dir): default (deferral) vs -notwophase inline
  501. name="$1"; mod=$(modof "$name")
  502. rm -f "gen_ssa/$mod.ssa" "gen_ssa/$mod.off.ssa"
  503. ./M2 -notwophase "tests/$name" >/dev/null 2>&1
  504. cp "gen_ssa/$mod.ssa" "gen_ssa/$mod.off.ssa"
  505. if ./M2 "tests/$name" 2>&1 | grep -q "twophase .*mismatch"; then
  506. fail=$((fail+1)); echo "FAIL(twophase): $name probe mismatch"; return
  507. fi
  508. if cmp -s "gen_ssa/$mod.off.ssa" "gen_ssa/$mod.ssa"; then
  509. pass=$((pass+1)); echo "PASS(twophase): $name identical"
  510. else
  511. fail=$((fail+1)); echo "FAIL(twophase): $name .ssa differs"
  512. fi
  513. rm -f "gen_ssa/$mod.off.ssa"
  514. }
  515. twophase_same t_nestedproc.mod
  516. twophase_same t_classmethod.mod
  517. # Forward references compile *differently* with deferral than with the
  518. # legacy FwdVar path (the deferral makes them ordinary, so the codegen is
  519. # the normal one) — that is the point. Gate them functionally: same exit
  520. # code, and the forward diagnostic still fires.
  521. twophase_run() {
  522. # $1 = test file, $2 = expected exit code
  523. name="$1"; want="$2"; mod=$(modof "$name")
  524. rm -f "gen_ssa/$mod.ssa" "gen_ssa/$mod.s" "gen_ssa/$mod"
  525. if ./M2 "tests/$name" 2>&1 | grep -q "Parsed correctly"; then :; else
  526. fail=$((fail+1)); echo "FAIL(twophase-run): $name rejected"; return
  527. fi
  528. qbe -o "gen_ssa/$mod.s" "gen_ssa/$mod.ssa" 2>/dev/null || { fail=$((fail+1)); echo "FAIL(twophase-run): $name qbe"; return; }
  529. cc "gen_ssa/$mod.s" "$SHIM" -o "gen_ssa/$mod" 2>/dev/null || { fail=$((fail+1)); echo "FAIL(twophase-run): $name cc"; return; }
  530. timeout 10 "gen_ssa/$mod" >/dev/null 2>&1; got=$?
  531. if [ "$got" = "$want" ]; then pass=$((pass+1)); echo "PASS(twophase-run): $name -> $got"
  532. else fail=$((fail+1)); echo "FAIL(twophase-run): $name got $got want $want"; fi
  533. }
  534. twophase_fail() {
  535. # $1 = test file, $2 = message expected in the .LST
  536. name="$1"; want="$2"
  537. if ./M2 "tests/$name" 2>&1 | grep -q "Incorrect source"; then :; else
  538. fail=$((fail+1)); echo "FAIL(twophase-fail): $name accepted"; return
  539. fi
  540. if grep -q "$want" "tests/$(basename "$name" .mod).LST" 2>/dev/null; then
  541. pass=$((pass+1)); echo "PASS(twophase-fail): $name [$want]"
  542. else fail=$((fail+1)); echo "FAIL(twophase-fail): [$want] not found"; fi
  543. }
  544. twophase_run t_fwdvar.mod 42
  545. twophase_run t_fwdsel.mod 42
  546. twophase_run t_fwdprop.mod 42
  547. twophase_fail t_bad_fwd.mod "incompatible assignment"
  548. # --- M2 process exit codes: 0 = ok, 1 = compile errors, 2 = usage/IO ---
  549. check_exit() {
  550. # $1 = description, $2 = expected rc, rest = args
  551. desc="$1"; want="$2"; shift 2
  552. ./M2 "$@" >/dev/null 2>&1; got=$?
  553. if [ "$got" = "$want" ]; then pass=$((pass+1)); echo "PASS(exit): $desc -> $got"
  554. else fail=$((fail+1)); echo "FAIL(exit): $desc got $got want $want"; fi
  555. }
  556. check_exit "ok" 0 tests/t_minimal.mod
  557. check_exit "errors" 1 tests/t_bad_undecl.mod
  558. check_exit "no input" 2
  559. check_exit "missing file" 2 /tmp/opencode/_m2_no_such_file_.mod
  560. # --- step 7 gate: compiler sources free of gm2-only imports.
  561. # FileIO is the last module to port (see docs/summary_step7.md);
  562. # it is excluded here so the gate guards everything else. ---
  563. if grep -qE "(IMPORT|FROM)[[:space:]]+.*\b(Environment|FileSysOp|TextIO|RawIO|WholeIO|IOChan|ProgramArgs)\b" \
  564. src/SymTab.def src/SymTab.mod src/QbeGen.def src/QbeGen.mod \
  565. src/M2S.mod src/M2P.mod src/M2.mod src/*.frm 2>/dev/null; then
  566. fail=$((fail+1)); echo "FAIL(gate): gm2-only imports present"
  567. else
  568. pass=$((pass+1)); echo "PASS(gate): no gm2-only imports (FileIO port pending)"
  569. fi
  570. echo "--- $pass passed, $fail failed ---"
  571. [ "$fail" -eq 0 ]