raz / commit c9a30772

commit
c9a30772eb6c2d3c979b7736ce32767419bdcb40
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
c9ee56dd
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.

patch · log

4 files changed, +46 -3

doc/compiler.md (history) modified +1 -1

@@ -21,7 +21,7 @@
2121 | `src/Sym.raz` | 715 | 573 | Types, objects, scopes, layout |
2222 | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes |
2323 | `src/X64.raz` | 1,584 | 1,304 | The instruction encoder, jumps within a procedure, the assembler |
24-| `src/Gen.raz` | 2,588 | 1,841 | The stack machine, for x86-64: values, operations, checks, calls, frames |
24+| `src/Gen.raz` | 2,592 | 1,843 | The stack machine, for x86-64: values, operations, checks, calls, frames |
2525 | `src/Item.raz` | 2,223 | 1,785 | Items: types, constants, variables, operators, conversions, calls |
2626 | `src/Parse.raz` | 4,266 | 3,689 | Declarations, statements, expressions, built-ins, recovery, warnings |
2727 | `src/Link.raz` | 407 | 315 | Tree shaking, layout, patching, the places |

src/Gen.raz modified +6 -2

@@ -2233,11 +2233,15 @@
22332233 end;
22342234 end;
22352235 (* a register variable's parameter that came on the stack, above the
2236- frame (val > 0), is loaded from there *)
2236+ frame (val > 0), is loaded from there; one passed by its address
2237+ (ind: var, or const) holds the address, 8 bytes, whatever its type:
2238+ a var BOOL loaded as a byte was a bad pointer *)
22372239 p := procParams;
22382240 while p # nil do
22392241 if (p^.reg # 0) and (p^.val > 0) then
2240- if p^.typ^.form = Sym.fReal then
2242+ if p^.ind then
2243+ X.load(p^.reg, X.mem(RBP, p^.val), 8, false);
2244+ elsif p^.typ^.form = Sym.fReal then
22412245 X.movsXM(p^.reg - XmmVar, X.mem(RBP, p^.val), p^.typ^.size = 4);
22422246 elsif p^.typ^.form in {Sym.fInt, Sym.fBool, Sym.fEnum, Sym.fSet} then
22432247 X.load(p^.reg, X.mem(RBP, p^.val), INT(p^.typ^.size), p^.typ^.signed);

test/run/VarOnStack.expect (history) added +3 -0

@@ -0,0 +1,3 @@
1+flag set
2+x is 1.5
3+153

test/run/VarOnStack.raz (history) added +36 -0

@@ -0,0 +1,36 @@
1+(* A parameter passed by its address (var, const) that comes on the
2+ stack, past the six registers, and is kept in a register because a
3+ loop reads it: it holds the address, 8 bytes, whatever its type. It
4+ was loaded as its type's value, a var BOOL as one byte, and the
5+ first read through it stopped the program on a bad pointer. *)
6+module VarOnStack;
7+import Out;
8+
9+type
10+ Pair = record
11+ a, b: INT;
12+ end;
13+
14+(* Six words fill the registers; flag, x, n and p come on the stack. *)
15+proc f(a, b, c, d, e, g: INT; var flag: BOOL; var x: F64; var n: INT; const p: Pair);
16+var i: INT;
17+begin
18+ for i := 1 to 3 do
19+ if not flag then flag := true; end;
20+ x := x + 0.5;
21+ n := n + p.a + p.b + a + b + c + d + e + g;
22+ end;
23+end;
24+
25+proc main*;
26+var flag: BOOL; x: F64; n: INT; p: Pair;
27+begin
28+ p := {a: 10, b: 20};
29+ f(1, 2, 3, 4, 5, 6, flag, x, n, p);
30+ if flag then Out.str("flag set\n"); end;
31+ if x = 1.5 then Out.str("x is 1.5\n"); end;
32+ Out.int(n);
33+ Out.ln;
34+end;
35+
36+end VarOnStack.