From 72b1a71d9373973e3aeae4548b08d01323a1e2b0 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Thu, 15 Oct 2026 06:48:31 +0000 Subject: [PATCH] A var real parameter's address in an integer register chooseRegs gave a var F64 or F32 parameter, used three times or more, one of XVarRegs by its type, though it holds an address; the prologue moved that into the integer register of the same number, another variable's. A parameter passed by its address now takes one integer register whatever its type, a var slice's too. Met in rgui's slider; conform/run/VarReal tests it. --- diff --git a/PROPOSALS.md b/PROPOSALS.md index 41e664d..bd067ad 100644 --- a/PROPOSALS.md +++ b/PROPOSALS.md @@ -154,62 +154,3 @@ a failed check in it is still told at its line; 60 to 100 lines of the budget; and, across modules, the imported module's text kept to be read again, which is to be checked. - -## A var real parameter given an xmm register - -Not the language's but the compiler's, and a defect: a procedure -with a `var` parameter of type `F64` or `F32`, used three times or -more, may read another variable's value as that parameter's address, -and stop "by a nil pointer" or reach any memory. 0.12.0 and `main` -both do it: - -``` -module M; -import Out; - -proc show(var v: F64; a, b: INT): INT; -begin - Out.int(a + b); Out.int(a + b); Out.int(a + b); - Out.ln; - if v > 0.0 then return 1; end; - if v < 0.0 then return 2; end; - if v = 0.0 then return 3; end; - return 0; -end; - -proc main*; -var x: F64; -begin - Out.int(show(x, 1, 2)); - Out.ln; -end; - -end M. -``` - -stops at the first `if`, where it should print 3. - -The cause is in `Parse.chooseRegs`. `regFit` lets a parameter passed -by its address (`ind`) live in a register, which holds the address; -but the loop that hands out registers looks only at the type, so a -real's address is given one of `XVarRegs`, and a slice's address two -of `VarRegs`. The prologue then moves the address into the integer -register of the xmm register's number: xmm12, `XVarRegs[0]`, becomes -r12, which is `VarRegs[1]`, the second busiest integer variable's. In -the program above, `a` and `b` take rbx and r12, and `v`'s address -goes to r12 too, under `b`'s value. - -The fix, two lines: an `ind` candidate needs one integer register, -whatever its type. - -``` -need := 1; -if (cand[best]^.typ^.form = Sym.fSlice) and not cand[best]^.ind then need := 2; end; -if (cand[best]^.typ^.form = Sym.fReal) and not cand[best]^.ind then -``` - -Tried on `main`: the program above, and rgui's slider, then run as -they should, and `make check` passes as it does without it (but for -the manual pages, which fail alike where groff is missing). Met in -rgui, whose `Gui.slide` takes its knob's place in and gives it back, -rather than by a `var`, until this is mended. diff --git a/conform/run/VarReal.expect b/conform/run/VarReal.expect new file mode 100644 index 0000000..da437cb --- /dev/null +++ b/conform/run/VarReal.expect @@ -0,0 +1,9 @@ +333 +3 +333 +2 +121212 +65 +121212 +100 +365656568 diff --git a/conform/run/VarReal.raz b/conform/run/VarReal.raz new file mode 100644 index 0000000..a4d41b6 --- /dev/null +++ b/conform/run/VarReal.raz @@ -0,0 +1,55 @@ +(* A var parameter of a real or a slice, used often, beside integers + that are used more: each is read and written through its address. *) +module VarReal; +import Out; + +proc sign(var v: F64; a, b: INT): INT; +begin + Out.int(a + b); Out.int(a + b); Out.int(a + b); + Out.ln; + if v > 0.0 then return 1; end; + if v < 0.0 then return 2; end; + if v = 0.0 then return 3; end; + return 0; +end; + +(* a knob's place, kept between lo and hi *) +proc slide(var x: F32; lo, hi, step: INT); +begin + x := x + F32(step) + F32(step) + F32(step); + if x < F32(lo) then x := F32(lo); end; + if x > F32(hi) then x := F32(hi); end; + Out.int(lo + hi + step); Out.int(lo + hi + step); Out.int(lo + hi + step); + Out.ln; +end; + +proc fill(var s: []U8; a, b: INT); +var i: INT; +begin + for i := 0 to LEN(s) - 1 do s[i] := U8(a + b + i); end; + s := s[1..LEN(s)]; + s[0] := U8(a + b); s[1] := U8(a + b); +end; + +proc main*; +var + x: F64; + y: F32; + buf: [4]U8; + s: []U8; +begin + Out.int(sign(x, 1, 2)); Out.ln; + x := -1.5; + Out.int(sign(x, 1, 2)); Out.ln; + y := 0.5; + slide(y, 0, 10, 2); + Out.int(INT(y * 10.0)); Out.ln; + slide(y, 0, 10, 2); + Out.int(INT(y * 10.0)); Out.ln; + s := buf[0..4]; + fill(s, 60, 5); + Out.int(LEN(s)); Out.int(INT(buf[0])); Out.int(INT(buf[1])); Out.int(INT(buf[2])); Out.int(INT(buf[3])); + Out.ln; +end; + +end VarReal. diff --git a/conform/run/VarReal.reals b/conform/run/VarReal.reals new file mode 100644 index 0000000..e69de29 diff --git a/doc/compiler.md b/doc/compiler.md index a74b1ea..12f8fd6 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -609,6 +609,12 @@ nothing in the trap report, bought 3% more on that copy and nothing on NBody. + A `var` parameter of a real holds the real's address, an integer: + it takes one of `VarRegs`, not of `XVarRegs`, and a `var` slice's + one, not two. Through 0.12.0 it was given an xmm register, which the + prologue took for the integer register of its number, another + variable's (`chooseRegs`). + All told, against v0.8.0: Jacobi 251 ms to 103, 3.1 times FPC `-O2`'s time to 1.3; NBody 375 ms to 116, 4.4 times to 1.4; rat and the integer kernels as they were. What is left of Jacobi is its index, diff --git a/src/Parse.raz b/src/Parse.raz index 4c808e7..f2d58fe 100644 --- a/src/Parse.raz +++ b/src/Parse.raz @@ -2622,9 +2622,10 @@ end; if i > 4 then xmax := 4; end; end; - (* a slice's weight is shared by its two registers *) + (* a slice's weight is shared by its two registers; one passed by + its address has one *) for i := 0 to numCand - 1 do - if cand[i]^.typ^.form = Sym.fSlice then weight[i] := weight[i] div 2; end; + if (cand[i]^.typ^.form = Sym.fSlice) and not cand[i]^.ind then weight[i] := weight[i] div 2; end; end; (* the heaviest, each used at least three times a register, take the five registers in turn, while there are enough for each, and the @@ -2636,9 +2637,13 @@ if (cand[i]^.reg = 0) and (weight[i] >= 3) and ((best < 0) or (weight[i] > weight[best])) then best := i; end; end; if best < 0 then break; end; + (* a parameter passed by its address (ind) holds the address, one + integer register whatever its type: a var real's address in + an xmm register would be moved, by the prologue, into the + integer register of the same number, another variable's *) need := 1; - if cand[best]^.typ^.form = Sym.fSlice then need := 2; end; - if cand[best]^.typ^.form = Sym.fReal then + if (cand[best]^.typ^.form = Sym.fSlice) and not cand[best]^.ind then need := 2; end; + if (cand[best]^.typ^.form = Sym.fReal) and not cand[best]^.ind then if kx < xmax then cand[best]^.reg := Gen.XVarRegs[kx]; INC(kx);