From c9a30772eb6c2d3c979b7736ce32767419bdcb40 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Thu, 22 Oct 2026 15:48:19 +0100 Subject: [PATCH] Gen: a var or const parameter on the stack, in a register, is its address A parameter passed by its address (ind: var, or const) that came on the stack, past the six registers, and was kept in a register (as a loop reading it makes it) was loaded as its type's value: a var BOOL as one byte, movzx r12d, byte [rbp + 16], so the first read through it stopped the program on a bad pointer. It is loaded as the 8-byte address, mov r12, [rbp + 16], as the parameters that came in registers already were. Found porting poczta (Pcompose.addrs). test/run/VarOnStack: a var BOOL, F64 and INT and a const record past six INTs, read and written in a loop; it stopped on a nil pointer before. Stage 3 of the fixpoint is 64 bytes longer: the compiler had a few of these loads itself, none run through a bad pointer. make check: 1050 passed, 0 failed. fixpoint: stages 2 and 3 agree. --- diff --git a/doc/compiler.md b/doc/compiler.md index 409edf5..0c6d872 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -21,7 +21,7 @@ | `src/Sym.raz` | 715 | 573 | Types, objects, scopes, layout | | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes | | `src/X64.raz` | 1,584 | 1,304 | The instruction encoder, jumps within a procedure, the assembler | -| `src/Gen.raz` | 2,588 | 1,841 | The stack machine, for x86-64: values, operations, checks, calls, frames | +| `src/Gen.raz` | 2,592 | 1,843 | The stack machine, for x86-64: values, operations, checks, calls, frames | | `src/Item.raz` | 2,223 | 1,785 | Items: types, constants, variables, operators, conversions, calls | | `src/Parse.raz` | 4,266 | 3,689 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/Link.raz` | 407 | 315 | Tree shaking, layout, patching, the places | diff --git a/src/Gen.raz b/src/Gen.raz index 29ad891..7c89238 100644 --- a/src/Gen.raz +++ b/src/Gen.raz @@ -2233,11 +2233,15 @@ end; end; (* a register variable's parameter that came on the stack, above the - frame (val > 0), is loaded from there *) + frame (val > 0), is loaded from there; one passed by its address + (ind: var, or const) holds the address, 8 bytes, whatever its type: + a var BOOL loaded as a byte was a bad pointer *) p := procParams; while p # nil do if (p^.reg # 0) and (p^.val > 0) then - if p^.typ^.form = Sym.fReal then + if p^.ind then + X.load(p^.reg, X.mem(RBP, p^.val), 8, false); + elsif p^.typ^.form = Sym.fReal then X.movsXM(p^.reg - XmmVar, X.mem(RBP, p^.val), p^.typ^.size = 4); elsif p^.typ^.form in {Sym.fInt, Sym.fBool, Sym.fEnum, Sym.fSet} then X.load(p^.reg, X.mem(RBP, p^.val), INT(p^.typ^.size), p^.typ^.signed); diff --git a/test/run/VarOnStack.expect b/test/run/VarOnStack.expect new file mode 100644 index 0000000..bb3dd27 --- /dev/null +++ b/test/run/VarOnStack.expect @@ -0,0 +1,3 @@ +flag set +x is 1.5 +153 diff --git a/test/run/VarOnStack.raz b/test/run/VarOnStack.raz new file mode 100644 index 0000000..0482559 --- /dev/null +++ b/test/run/VarOnStack.raz @@ -0,0 +1,36 @@ +(* A parameter passed by its address (var, const) that comes on the + stack, past the six registers, and is kept in a register because a + loop reads it: it holds the address, 8 bytes, whatever its type. It + was loaded as its type's value, a var BOOL as one byte, and the + first read through it stopped the program on a bad pointer. *) +module VarOnStack; +import Out; + +type + Pair = record + a, b: INT; + end; + +(* Six words fill the registers; flag, x, n and p come on the stack. *) +proc f(a, b, c, d, e, g: INT; var flag: BOOL; var x: F64; var n: INT; const p: Pair); +var i: INT; +begin + for i := 1 to 3 do + if not flag then flag := true; end; + x := x + 0.5; + n := n + p.a + p.b + a + b + c + d + e + g; + end; +end; + +proc main*; +var flag: BOOL; x: F64; n: INT; p: Pair; +begin + p := {a: 10, b: 20}; + f(1, 2, 3, 4, 5, 6, flag, x, n, p); + if flag then Out.str("flag set\n"); end; + if x = 1.5 then Out.str("x is 1.5\n"); end; + Out.int(n); + Out.ln; +end; + +end VarOnStack.