From 7e721e08afccacb2216093b186a71404f6052a0d Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Wed, 14 Oct 2026 11:55:58 +0100 Subject: [PATCH] Every stop of a running program exits with status 2; conformance tests for stops, zero start, evaluation order, variant match, defers and Str's byte order --- diff --git a/conform/run/DeferSkip.expect b/conform/run/DeferSkip.expect new file mode 100644 index 0000000..2cd4c3c --- /dev/null +++ b/conform/run/DeferSkip.expect @@ -0,0 +1,6 @@ +first +body +second +first +1 +.12 diff --git a/conform/run/DeferSkip.raz b/conform/run/DeferSkip.raz new file mode 100644 index 0000000..42a9459 --- /dev/null +++ b/conform/run/DeferSkip.raz @@ -0,0 +1,41 @@ +(* Only the defers that have been reached run, last first; return + evaluates its value before they do. *) +module DeferSkip; +import Out; + +proc f(early: BOOL); +begin + defer Out.str("first\n"); + if early then return; end; + defer Out.str("second\n"); + Out.str("body\n"); +end; + +proc g(): INT; +var x: INT; +begin + x := 1; + defer INC(x); + return x; +end; + +proc loop; +var i: INT; +begin + for i := 1 to 3 do + defer Out.int(i); + if i = 2 then break; end; + Out.char('.'); + end; + Out.ln; +end; + +proc main*; +begin + f(true); + f(false); + Out.int(g()); Out.ln; + loop; +end; + +end DeferSkip. diff --git a/conform/run/DeferStop.expect b/conform/run/DeferStop.expect new file mode 100644 index 0000000..90be1f3 --- /dev/null +++ b/conform/run/DeferStop.expect @@ -0,0 +1 @@ +before diff --git a/conform/run/DeferStop.raz b/conform/run/DeferStop.raz new file mode 100644 index 0000000..04cb0d7 --- /dev/null +++ b/conform/run/DeferStop.raz @@ -0,0 +1,15 @@ +(* A failed check ends the program without running the deferred + statements still pending; what the program wrote is still out. *) +module DeferStop; +import Out; + +proc main*; +var a: [3]I64; i: INT; +begin + defer Out.str("deferred\n"); + Out.str("before\n"); + i := ARGC() + 4; + Out.int(a[i]); +end; + +end DeferStop. diff --git a/conform/run/DeferStop.status b/conform/run/DeferStop.status new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/conform/run/DeferStop.status @@ -0,0 +1 @@ +2 diff --git a/conform/run/EvalOrder.expect b/conform/run/EvalOrder.expect new file mode 100644 index 0000000..929ecf0 --- /dev/null +++ b/conform/run/EvalOrder.expect @@ -0,0 +1,5 @@ +abc +abc| +ig +lh +pq! diff --git a/conform/run/EvalOrder.raz b/conform/run/EvalOrder.raz new file mode 100644 index 0000000..65e4c60 --- /dev/null +++ b/conform/run/EvalOrder.raz @@ -0,0 +1,34 @@ +(* Operands, arguments and the designators of an assignment are + evaluated from left to right, the designators before the right side. *) +module EvalOrder; +import Out; + +var t: [4]I64; + +proc say(c: U8): INT; +begin + Out.char(c); + return 1; +end; + +proc three(_x, _y, _z: INT); +begin + Out.char('|'); +end; + +proc main*; +var x: INT; s: []I64; +begin + x := say('a') + say('b') * say('c'); + Out.ln; + three(say('a'), say('b'), say('c')); + Out.ln; + t[say('i')] := say('g'); + Out.ln; + s := t[say('l') - 1 .. say('h') + 1]; + Out.ln; + if (say('p') = 1) and (say('q') = 1) then Out.char('!'); end; + Out.ln; +end; + +end EvalOrder. diff --git a/conform/run/LoudOnce.expect b/conform/run/LoudOnce.expect new file mode 100644 index 0000000..9c45e5c --- /dev/null +++ b/conform/run/LoudOnce.expect @@ -0,0 +1,2 @@ +an instance of Loud, of size 8 +sizes 8 8 diff --git a/conform/run/LoudOnce.raz b/conform/run/LoudOnce.raz new file mode 100644 index 0000000..58b8cdf --- /dev/null +++ b/conform/run/LoudOnce.raz @@ -0,0 +1,11 @@ +(* The body of a generic module runs once for an instance, however + many times it is imported. *) +module LoudOnce; +import Out, A := Loud(I64), B := Loud(I64); + +proc main*; +begin + Out.str("sizes "); Out.int(A.size); Out.char(' '); Out.int(B.size); Out.ln; +end; + +end LoudOnce. diff --git a/conform/run/StopDivLowest.expect b/conform/run/StopDivLowest.expect new file mode 100644 index 0000000..ac6e774 --- /dev/null +++ b/conform/run/StopDivLowest.expect @@ -0,0 +1,2 @@ +before +bye diff --git a/conform/run/StopDivLowest.raz b/conform/run/StopDivLowest.raz new file mode 100644 index 0000000..7b16597 --- /dev/null +++ b/conform/run/StopDivLowest.raz @@ -0,0 +1,22 @@ +(* Dividing the smallest I64 by -1 stops the program. The program ends with status 2, after the procedures registered + with Rt.atExit, so that what it wrote is out. *) +module StopDivLowest; +import Out, Rt; + + +proc bye; +begin + Out.str("bye\n"); +end; + +proc main*; +var a, b: I64; +begin + _ := Rt.atExit(bye); + a := -9223372036854775807 - 1; + b := -1; + Out.str("before\n"); + Out.int(a div b); +end; + +end StopDivLowest. diff --git a/conform/run/StopDivLowest.status b/conform/run/StopDivLowest.status new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/conform/run/StopDivLowest.status @@ -0,0 +1 @@ +2 diff --git a/conform/run/StopDivZero.expect b/conform/run/StopDivZero.expect new file mode 100644 index 0000000..ac6e774 --- /dev/null +++ b/conform/run/StopDivZero.expect @@ -0,0 +1,2 @@ +before +bye diff --git a/conform/run/StopDivZero.raz b/conform/run/StopDivZero.raz new file mode 100644 index 0000000..ffee91a --- /dev/null +++ b/conform/run/StopDivZero.raz @@ -0,0 +1,22 @@ +(* Dividing by zero stops the program. The program ends with status 2, after the procedures registered + with Rt.atExit, so that what it wrote is out. *) +module StopDivZero; +import Out, Rt; + + +proc bye; +begin + Out.str("bye\n"); +end; + +proc main*; +var a, b: I64; +begin + _ := Rt.atExit(bye); + a := 7; + b := ARGC() - 1; + Out.str("before\n"); + Out.int(a div b); +end; + +end StopDivZero. diff --git a/conform/run/StopDivZero.status b/conform/run/StopDivZero.status new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/conform/run/StopDivZero.status @@ -0,0 +1 @@ +2 diff --git a/conform/run/StopNil.expect b/conform/run/StopNil.expect new file mode 100644 index 0000000..ac6e774 --- /dev/null +++ b/conform/run/StopNil.expect @@ -0,0 +1,2 @@ +before +bye diff --git a/conform/run/StopNil.raz b/conform/run/StopNil.raz new file mode 100644 index 0000000..c07f2df --- /dev/null +++ b/conform/run/StopNil.raz @@ -0,0 +1,22 @@ +(* Following nil stops the program. The program ends with status 2, after the procedures registered + with Rt.atExit, so that what it wrote is out. *) +module StopNil; +import Out, Rt; + +type Cell = record next: ^Cell; v: I64; end; + +proc bye; +begin + Out.str("bye\n"); +end; + +proc main*; +var c: Cell; +begin + _ := Rt.atExit(bye); + c.v := 1; + Out.str("before\n"); + Out.int(c.next^.v); +end; + +end StopNil. diff --git a/conform/run/StopNil.status b/conform/run/StopNil.status new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/conform/run/StopNil.status @@ -0,0 +1 @@ +2 diff --git a/conform/run/StopNilProc.expect b/conform/run/StopNilProc.expect new file mode 100644 index 0000000..ac6e774 --- /dev/null +++ b/conform/run/StopNilProc.expect @@ -0,0 +1,2 @@ +before +bye diff --git a/conform/run/StopNilProc.raz b/conform/run/StopNilProc.raz new file mode 100644 index 0000000..b3c1238 --- /dev/null +++ b/conform/run/StopNilProc.raz @@ -0,0 +1,20 @@ +(* Calling a nil procedure stops the program. The program ends with status 2, after the procedures registered + with Rt.atExit, so that what it wrote is out. *) +module StopNilProc; +import Out, Rt; + + +proc bye; +begin + Out.str("bye\n"); +end; + +proc main*; +var p: proc; +begin + _ := Rt.atExit(bye); + Out.str("before\n"); + p(); +end; + +end StopNilProc. diff --git a/conform/run/StopNilProc.status b/conform/run/StopNilProc.status new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/conform/run/StopNilProc.status @@ -0,0 +1 @@ +2 diff --git a/conform/run/StopStack.expect b/conform/run/StopStack.expect new file mode 100644 index 0000000..ac6e774 --- /dev/null +++ b/conform/run/StopStack.expect @@ -0,0 +1,2 @@ +before +bye diff --git a/conform/run/StopStack.raz b/conform/run/StopStack.raz new file mode 100644 index 0000000..bd43d7d --- /dev/null +++ b/conform/run/StopStack.raz @@ -0,0 +1,27 @@ +(* Running out of stack stops the program. The program ends with status 2, after the procedures registered + with Rt.atExit, so that what it wrote is out. *) +module StopStack; +import Out, Rt; + +proc down(n: INT): INT; +var pad: [65536]INT; +begin + pad[n mod 65536] := n; + return down(n + 1) + pad[0]; +end; + +proc bye; +begin + Out.str("bye\n"); +end; + +proc main*; +var n: INT; +begin + _ := Rt.atExit(bye); + Out.str("before\n"); + n := down(0); + Out.int(n); +end; + +end StopStack. diff --git a/conform/run/StopStack.status b/conform/run/StopStack.status new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/conform/run/StopStack.status @@ -0,0 +1 @@ +2 diff --git a/conform/run/StrBytes.expect b/conform/run/StrBytes.expect new file mode 100644 index 0000000..3528660 --- /dev/null +++ b/conform/run/StrBytes.expect @@ -0,0 +1,9 @@ +8 7 6 5 4 3 2 1 +1 2 3 4 5 6 7 8 +578437695752307201 +72623859790382856 +2 1 +258 +513 +0 +204 187 170 diff --git a/conform/run/StrBytes.raz b/conform/run/StrBytes.raz new file mode 100644 index 0000000..f3abf39 --- /dev/null +++ b/conform/run/StrBytes.raz @@ -0,0 +1,34 @@ +(* Str's byte order: le and be read, putLE and putBE write, a number + of up to 8 bytes, whatever the machine's own order. *) +module StrBytes; +import Out, Str; + +proc bytes(s: []U8); +var i: INT; +begin + for i := 0 to LEN(s) - 1 do + if i > 0 then Out.char(' '); end; + Out.int(I64(s[i])); + end; + Out.ln; +end; + +proc main*; +var b: [8]U8; +begin + Str.putLE(b[0..8], 0102030405060708H); + bytes(b[0..8]); + Str.putBE(b[0..8], 0102030405060708H); + bytes(b[0..8]); + Out.int(I64(Str.le(b[0..8]))); Out.ln; + Out.int(I64(Str.be(b[0..8]))); Out.ln; + Str.putBE(b[0..2], 513); + bytes(b[0..2]); + Out.int(I64(Str.le(b[0..2]))); Out.ln; + Out.int(I64(Str.be(b[0..2]))); Out.ln; + Out.int(I64(Str.le(b[0..0]))); Out.ln; + Str.putLE(b[0..3], 0AABBCCH); + bytes(b[0..3]); +end; + +end StrBytes. diff --git a/conform/run/StrBytesStop.expect b/conform/run/StrBytesStop.expect new file mode 100644 index 0000000..90be1f3 --- /dev/null +++ b/conform/run/StrBytesStop.expect @@ -0,0 +1 @@ +before diff --git a/conform/run/StrBytesStop.raz b/conform/run/StrBytesStop.raz new file mode 100644 index 0000000..1b8499b --- /dev/null +++ b/conform/run/StrBytesStop.raz @@ -0,0 +1,13 @@ +(* putLE and putBE write at most 8 bytes: more stops the program. *) +module StrBytesStop; +import Out, Str; + +proc main*; +var b: [9]U8; +begin + Out.str("before\n"); + Str.putLE(b[0..9], 1); + Out.str("after\n"); +end; + +end StrBytesStop. diff --git a/conform/run/StrBytesStop.status b/conform/run/StrBytesStop.status new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/conform/run/StrBytesStop.status @@ -0,0 +1 @@ +2 diff --git a/conform/run/VariantMatch.expect b/conform/run/VariantMatch.expect new file mode 100644 index 0000000..1d8bfd3 --- /dev/null +++ b/conform/run/VariantMatch.expect @@ -0,0 +1,14 @@ +pair 30 5 +pair 30 5 +unnamed +none +num 5 +additive +additive +times +other +zero +small +negative or ten +negative or ten +big diff --git a/conform/run/VariantMatch.raz b/conform/run/VariantMatch.raz new file mode 100644 index 0000000..8ecd3b3 --- /dev/null +++ b/conform/run/VariantMatch.raz @@ -0,0 +1,74 @@ +(* match: the names of an arm stand for the fields of the variant + itself when it is a variable, and for a copy when it isn't. *) +module VariantMatch; +import Out; + +type + Shape = variant num(n: I64); pair(a, b: I64); none; end; + Op = (plus, minus, times, divide); + +var g: Shape; + +proc copyOfG(): Shape; +begin + return g; +end; + +proc show(s: Shape); +begin + match s of + | num(n): Out.str("num "); Out.int(n); + | pair(a, b): Out.str("pair "); Out.int(a); Out.char(' '); Out.int(b); + | none: Out.str("none"); + end; + Out.ln; +end; + +proc kind(op: Op); +begin + match op of + | plus, minus: Out.str("additive"); + | times: Out.str("times"); + | else: Out.str("other"); + end; + Out.ln; +end; + +proc size(n: I64); +begin + match n of + | 0: Out.str("zero"); + | 1 .. 9: Out.str("small"); + | -5 .. -1, 10: Out.str("negative or ten"); + | else: Out.str("big"); + end; + Out.ln; +end; + +proc main*; +begin + g := pair(3, 4); + match g of + | pair(a, b): a := a * 10; INC(b); (* through the names: g changes *) + | else: + end; + show(g); + match copyOfG() of + | pair(a, b): a := 0; b := 0; (* a copy: g does not change *) + | else: + end; + show(g); + match g of + | num: Out.str("no\n"); (* fields left unnamed *) + | pair: Out.str("unnamed\n"); + | none: Out.str("no\n"); + end; + g := none; + show(g); + g := num(5); + show(g); + kind(plus); kind(minus); kind(times); kind(divide); + size(0); size(5); size(-3); size(10); size(11); +end; + +end VariantMatch. diff --git a/conform/run/ZeroStart.expect b/conform/run/ZeroStart.expect new file mode 100644 index 0000000..53d6cd4 --- /dev/null +++ b/conform/run/ZeroStart.expect @@ -0,0 +1,16 @@ +number zero +bool zero +pointers zero +slice zero +enumeration and set zero +record zero +array zero +variant first 0 +local number zero +local bool zero +local pointers zero +local slice zero +local enumeration and set zero +local record zero +local array zero +local variant first 0 diff --git a/conform/run/ZeroStart.raz b/conform/run/ZeroStart.raz new file mode 100644 index 0000000..862b216 --- /dev/null +++ b/conform/run/ZeroStart.raz @@ -0,0 +1,69 @@ +(* Every variable starts at zero: a module's, and a procedure's even + where the stack held other values before. *) +module ZeroStart; +import Out; + +type + Colour = (red, green, blue); + Flags = set of Colour; + Rec = record n: I64; ok: BOOL; p: ^Rec; end; + Shape = variant first(x: I64); second(y: I64); end; + +var + n: I64; u: U8; ok: BOOL; p: ^Rec; f: proc; e: ERROR; s: []U8; + c: Colour; fl: Flags; r: Rec; a: [3]I64; v: Shape; + +proc line(name: []U8; zero: BOOL); +begin + Out.str(name); + if zero then Out.str(" zero"); else Out.str(" NOT ZERO"); end; + Out.ln; +end; + +proc which(name: []U8; v: Shape); +begin + Out.str(name); + match v of + | first(x): Out.str(" first "); Out.int(x); + | second(y): Out.str(" second "); Out.int(y); + end; + Out.ln; +end; + +(* leaves its frame full of other values *) +proc dirty; +var pad: [64]I64; i: INT; +begin + for i := 0 to 63 do pad[i] := 7; end; +end; + +proc locals; +var + n: I64; u: U8; ok: BOOL; p: ^Rec; f: proc; e: ERROR; s: []U8; + c: Colour; fl: Flags; r: Rec; a: [3]I64; v: Shape; +begin + line("local number", (n = 0) and (u = 0)); + line("local bool", not ok); + line("local pointers", (p = nil) and (f = nil) and (e = nil)); + line("local slice", LEN(s) = 0); + line("local enumeration and set", (c = red) and not (red in fl) and not (blue in fl)); + line("local record", (r.n = 0) and not r.ok and (r.p = nil)); + line("local array", (a[0] = 0) and (a[1] = 0) and (a[2] = 0)); + which("local variant", v); +end; + +proc main*; +begin + line("number", (n = 0) and (u = 0)); + line("bool", not ok); + line("pointers", (p = nil) and (f = nil) and (e = nil)); + line("slice", LEN(s) = 0); + line("enumeration and set", (c = red) and not (red in fl) and not (blue in fl)); + line("record", (r.n = 0) and not r.ok and (r.p = nil)); + line("array", (a[0] = 0) and (a[1] = 0) and (a[2] = 0)); + which("variant", v); + dirty; + locals; +end; + +end ZeroStart. diff --git a/doc/compiler.md b/doc/compiler.md index df873b2..73402ac 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -1483,9 +1483,9 @@ the program by `Rt.exit` with status 2; `Rt.stop` says its message and ends it the same way. A signal is told at the place of the statement it stopped in, or of the - procedure for a stack overflow in its prologue, and then ends the - program as the report says, with status 2 (until `Rt`'s handlers - do so, its status is the signal's). The handlers run on a stack + procedure for a stack overflow in its prologue, and then `Rt.exit` + ends the program with status 2, after the procedures registered with + `Rt.atExit`, as for a failed check. The handlers run on a stack of their own: `Rt`'s for the main thread, one in each `Thread` thread's mapping. C's threads have none, so a stack overflow in one ends the program by the signal with no message. diff --git a/lib/Rt.raz b/lib/Rt.raz index ec234e0..d1c4274 100644 --- a/lib/Rt.raz +++ b/lib/Rt.raz @@ -539,27 +539,23 @@ the enumeration called name; 3 for a slice a..b of a pointer, which runs backwards. What a target's machine doesn't catch the code tests for, and stops at with what 4, a nil pointer; 5, a division - by zero; 6, a stack overflow; these end the program by the signal - the machine would have given, as if it had. rdi and rsi are the + by zero; 6, a stack overflow; these end the program by exit with + status 2 too, as the signal handler does. rdi and rsi are the registers as the failing code had them. *) proc fail*(what, a, b, len: INT; at: UINT; name: ^U8; rdi, rsi: UINT); -var sig: INT; begin regs[5] := rdi; regs[6] := rsi; where(at); if what >= 4 then put("stopped by "); - sig := SigSegv; if what = 4 then put("a nil pointer"); - elsif what = 5 then put("a division fault"); sig := SigFpe; + elsif what = 5 then put("a division fault"); else put("a stack overflow"); end; say; if teller # nil then teller(at, callerFrame(), true); end; - act := {}; (* the signal's default action *) - _ := SYSCALL(13, sig, ADR(act), 0, 8); (* rt_sigaction *) - _ := SYSCALL(62, SYSCALL(39), sig); (* kill(getpid(), sig) *) + exit(2); end; if what = 0 then put("index "); @@ -592,10 +588,8 @@ end sigReturn; (* A signal stops the program: says where it was and why on standard - error, and returns to the instruction that faulted. The handler was - reset as it was called, so the instruction faults again and the - signal ends the program. info and ctx are the kernel's siginfo and - ucontext. *) + error, then ends the program by exit with status 2, as a failed + check does. info and ctx are the kernel's siginfo and ucontext. *) proc onSignal(sig: INT; info, ctx: ^U8); var adr, sp, ip, bp: UINT; q: ^UINT; why: []U8; begin @@ -623,6 +617,7 @@ say; (* the variables of a frame the stack overflowed in may be past it *) if teller # nil then teller(ip, bp, why # "a stack overflow"); end; + exit(2); end; (* Whether a is in the program's own code, not C's *) diff --git a/test/libc/NilC.status b/test/libc/NilC.status index 897bdc8..0cfbf08 100644 --- a/test/libc/NilC.status +++ b/test/libc/NilC.status @@ -1 +1 @@ -139 +2 diff --git a/test/run/DivLowest.status b/test/run/DivLowest.status index 7296f25..0cfbf08 100644 --- a/test/run/DivLowest.status +++ b/test/run/DivLowest.status @@ -1 +1 @@ -136 +2 diff --git a/test/run/DivLowestI16.status b/test/run/DivLowestI16.status index 7296f25..0cfbf08 100644 --- a/test/run/DivLowestI16.status +++ b/test/run/DivLowestI16.status @@ -1 +1 @@ -136 +2 diff --git a/test/run/DivLowestI32.status b/test/run/DivLowestI32.status index 7296f25..0cfbf08 100644 --- a/test/run/DivLowestI32.status +++ b/test/run/DivLowestI32.status @@ -1 +1 @@ -136 +2 diff --git a/test/run/DivLowestI8.status b/test/run/DivLowestI8.status index 7296f25..0cfbf08 100644 --- a/test/run/DivLowestI8.status +++ b/test/run/DivLowestI8.status @@ -1 +1 @@ -136 +2 diff --git a/test/run/DivZero.status b/test/run/DivZero.status index 7296f25..0cfbf08 100644 --- a/test/run/DivZero.status +++ b/test/run/DivZero.status @@ -1 +1 @@ -136 +2 diff --git a/test/run/KeptNil.expect b/test/run/KeptNil.expect index e69de29..00750ed 100644 --- a/test/run/KeptNil.expect +++ b/test/run/KeptNil.expect @@ -0,0 +1 @@ +3 diff --git a/test/run/KeptNil.status b/test/run/KeptNil.status index 897bdc8..0cfbf08 100644 --- a/test/run/KeptNil.status +++ b/test/run/KeptNil.status @@ -1 +1 @@ -139 +2 diff --git a/test/run/NilBig.status b/test/run/NilBig.status index 897bdc8..0cfbf08 100644 --- a/test/run/NilBig.status +++ b/test/run/NilBig.status @@ -1 +1 @@ -139 +2 diff --git a/test/run/NilPointer.status b/test/run/NilPointer.status index 897bdc8..0cfbf08 100644 --- a/test/run/NilPointer.status +++ b/test/run/NilPointer.status @@ -1 +1 @@ -139 +2 diff --git a/test/run/NilProc.status b/test/run/NilProc.status index 897bdc8..0cfbf08 100644 --- a/test/run/NilProc.status +++ b/test/run/NilProc.status @@ -1 +1 @@ -139 +2 diff --git a/test/run/StackOverflow.status b/test/run/StackOverflow.status index 897bdc8..0cfbf08 100644 --- a/test/run/StackOverflow.status +++ b/test/run/StackOverflow.status @@ -1 +1 @@ -139 +2 diff --git a/test/run/ThreadOverflow.status b/test/run/ThreadOverflow.status index 897bdc8..0cfbf08 100644 --- a/test/run/ThreadOverflow.status +++ b/test/run/ThreadOverflow.status @@ -1 +1 @@ -139 +2