raz / commit 8892c580

commit
8892c5805076c5181cb41776290b44a4f333ce2d
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
fc966ca0
PROPOSALS: a var real parameter given an xmm register, and the fix

patch · log

1 file changed, +59 -0

PROPOSALS.md modified +59 -0

@@ -154,3 +154,62 @@
154154 a failed check in it is still told at its line; 60 to 100 lines of
155155 the 164 left below 11,000; and, across modules, the imported module's
156156 text kept to be read again, which is to be checked.
157+
158+## A var real parameter given an xmm register
159+
160+Not the language's but the compiler's, and a defect: a procedure
161+with a `var` parameter of type `F64` or `F32`, used three times or
162+more, may read another variable's value as that parameter's address,
163+and stop "by a nil pointer" or reach any memory. 0.12.0 and `main`
164+both do it:
165+
166+```
167+module M;
168+import Out;
169+
170+proc show(var v: F64; a, b: INT): INT;
171+begin
172+ Out.int(a + b); Out.int(a + b); Out.int(a + b);
173+ Out.ln;
174+ if v > 0.0 then return 1; end;
175+ if v < 0.0 then return 2; end;
176+ if v = 0.0 then return 3; end;
177+ return 0;
178+end;
179+
180+proc main*;
181+var x: F64;
182+begin
183+ Out.int(show(x, 1, 2));
184+ Out.ln;
185+end;
186+
187+end M.
188+```
189+
190+stops at the first `if`, where it should print 3.
191+
192+The cause is in `Parse.chooseRegs`. `regFit` lets a parameter passed
193+by its address (`ind`) live in a register, which holds the address;
194+but the loop that hands out registers looks only at the type, so a
195+real's address is given one of `XVarRegs`, and a slice's address two
196+of `VarRegs`. The prologue then moves the address into the integer
197+register of the xmm register's number: xmm12, `XVarRegs[0]`, becomes
198+r12, which is `VarRegs[1]`, the second busiest integer variable's. In
199+the program above, `a` and `b` take rbx and r12, and `v`'s address
200+goes to r12 too, under `b`'s value.
201+
202+The fix, two lines: an `ind` candidate needs one integer register,
203+whatever its type.
204+
205+```
206+need := 1;
207+if (cand[best]^.typ^.form = Sym.fSlice) and not cand[best]^.ind then need := 2; end;
208+if (cand[best]^.typ^.form = Sym.fReal) and not cand[best]^.ind then
209+```
210+
211+Tried on `main`: the program above, and rgui's slider, then run as
212+they should, and `make check` passes as it does without it (but for
213+the manual pages, which fail alike where groff is missing). Met in
214+rgui, whose `Gui.slide` takes its knob's place in and gives it back,
215+rather than by a `var`, until this is mended.