run_tests.sh 29 KB

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