raz / commit b13f87f8

commit
b13f87f8856403dbb56aab386a3f93950e515d82
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
71db16c0
The compiler: const parameters (report 2026-10.33, section 8)

A const parameter is passed by address, as a var one, and only read: its
Obj has isVar and isConst, and an item for it or a part of it is ro, as
another module's variable is. onlyRead tells them apart, for a message
that says the name is only read here, and so that a slice of its array
may still be made to read. passArg passes a variable in place, where a
value parameter's record is copied, and anything else in a temporary of
the statement (constTemp). A match on a const parameter or a constant
gives names that are only read. Procedure types compare const as they
compare var. Tests in conform/run and conform/err; doc/compiler.md and
raz(7) say so.

patch · log

18 files changed, +295 -24

conform/err/ConstAdr.raz (history) added +12 -0

@@ -0,0 +1,12 @@
1+module ConstAdr;
2+type R = record n: INT; end;
3+proc f(const r: R): ^R;
4+begin
5+ return ADR(r);
6+end;
7+proc main*;
8+var r: R;
9+begin
10+ _ := f(r);
11+end;
12+end ConstAdr.

conform/err/ConstAssign.raz (history) added +12 -0

@@ -0,0 +1,12 @@
1+module ConstAssign;
2+type R = record n: INT; end;
3+proc f(const r: R);
4+begin
5+ r.n := 1;
6+end;
7+proc main*;
8+var r: R;
9+begin
10+ f(r);
11+end;
12+end ConstAssign.

conform/err/ConstMatchName.raz (history) added +15 -0

@@ -0,0 +1,15 @@
1+module ConstMatchName;
2+type S = variant circle(r: INT); dot; end;
3+proc f(const s: S);
4+begin
5+ match s of
6+ | circle(r): r := 0;
7+ | dot:
8+ end;
9+end;
10+proc main*;
11+var s: S;
12+begin
13+ f(s);
14+end;
15+end ConstMatchName.

conform/err/ConstProcType.raz (history) added +10 -0

@@ -0,0 +1,10 @@
1+module ConstProcType;
2+type R = record n: INT; end;
3+proc f(r: R): INT; begin return r.n; end;
4+proc main*;
5+var p: proc(const r: R): INT;
6+begin
7+ p := f;
8+ _ := p;
9+end;
10+end ConstProcType.

conform/err/ConstVarArg.raz (history) added +13 -0

@@ -0,0 +1,13 @@
1+module ConstVarArg;
2+type R = record n: INT; end;
3+proc g(var r: R); begin r.n := 0; end;
4+proc f(const r: R);
5+begin
6+ g(r);
7+end;
8+proc main*;
9+var r: R;
10+begin
11+ f(r);
12+end;
13+end ConstVarArg.

conform/run/ConstParam.expect (history) added +12 -0

@@ -0,0 +1,12 @@
1+4
2+10
3+5
4+104
5+42
6+4
7+77
8+12
9+12
10+Tbc
11+80
12+9

conform/run/ConstParam.raz (history) added +79 -0

@@ -0,0 +1,79 @@
1+module ConstParam;
2+import Out;
3+
4+type
5+ Big = record a: [100]INT; name: []U8; n: INT; end;
6+ Shape = variant circle(r: INT); rect(w, h: INT); end;
7+
8+const Ten: Big = {n: 10, name: "ten"};
9+
10+var g: Big;
11+
12+proc sum(const b: Big): INT;
13+var i, s: INT;
14+begin
15+ s := b.n;
16+ for i := 0 to 99 do s := s + b.a[i]; end;
17+ return s;
18+end;
19+
20+proc first(const s: []U8): U8;
21+begin
22+ return s[0];
23+end;
24+
25+proc twice(const n: INT): INT;
26+begin
27+ return n * 2;
28+end;
29+
30+proc seen(const b: Big; var w: Big): INT; (* sees w's change: same variable *)
31+begin
32+ w.n := 77;
33+ return b.n;
34+end;
35+
36+proc area(const s: Shape): INT;
37+begin
38+ match s of
39+ | circle(r): return 3 * r * r;
40+ | rect(w, h): return w * h;
41+ end;
42+end;
43+
44+proc make(n: INT): Big;
45+var b: Big;
46+begin
47+ b.n := n;
48+ return b;
49+end;
50+
51+proc label(const b: Big): []U8;
52+begin
53+ b.name[0] := 'T'; (* allowed: the bytes a slice in it points at *)
54+ return b.name;
55+end;
56+
57+proc main*;
58+var b: Big; sh: Shape; buf: [3]U8; f: proc(const b: Big): INT;
59+begin
60+ b.a[5] := 3; b.n := 1;
61+ Out.int(sum(b)); Out.ln;
62+ Out.int(sum(Ten)); Out.ln;
63+ Out.int(sum(make(5))); Out.ln;
64+ Out.int(I64(first("hey"))); Out.ln;
65+ Out.int(twice(21)); Out.ln;
66+ Out.int(twice(b.n + 1)); Out.ln;
67+ Out.int(seen(b, b)); Out.ln;
68+ sh := rect(3, 4);
69+ Out.int(area(sh)); Out.ln;
70+ Out.int(area(circle(2))); Out.ln;
71+ buf := "abc"; b.name := buf[0..3];
72+ Out.str(label(b)); Out.ln;
73+ f := sum;
74+ Out.int(f(b)); Out.ln;
75+ g.n := 9;
76+ Out.int(sum(g)); Out.ln;
77+end;
78+
79+end ConstParam.

doc/compiler.md (history) modified +24 -6

@@ -1,6 +1,6 @@
11 # Reading the raz compiler
22
3-The compiler is eleven files of raz, about 11,200 lines of code (the
3+The compiler is eleven files of raz, about 11,400 lines of code (the
44 budget is 14,000). It is its own compiler: the
55 frozen Pascal compiler in `seed/` builds `src/` the first time, and
66 after that a raz builds `src/` (`make fixpoint` checks that it makes
@@ -14,13 +14,13 @@
1414 | File | Lines | Code | What |
1515 |---|---|---|---|
1616 | `src/Diag.raz` | 410 | 315 | Messages: positions, source excerpts, colour, counts |
17-| `src/Scan.raz` | 1,125 | 934 | Symbols, literals, comments, positions |
18-| `src/Sym.raz` | 707 | 566 | Types, objects, scopes, layout |
17+| `src/Scan.raz` | 1,148 | 949 | Symbols, literals, comments, positions |
18+| `src/Sym.raz` | 712 | 570 | Types, objects, scopes, layout |
1919 | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes |
2020 | `src/X64.raz` | 1,517 | 1,253 | The instruction encoder, jumps within a procedure, the assembler |
2121 | `src/Gen.raz` | 2,552 | 1,821 | The stack machine, for x86-64: values, operations, checks, calls, frames |
22-| `src/Item.raz` | 2,037 | 1,651 | Items: types, constants, variables, operators, conversions, calls |
23-| `src/Parse.raz` | 4,176 | 3,615 | Declarations, statements, expressions, built-ins, recovery, warnings |
22+| `src/Item.raz` | 2,091 | 1,682 | Items: types, constants, variables, operators, conversions, calls |
23+| `src/Parse.raz` | 4,182 | 3,619 | Declarations, statements, expressions, built-ins, recovery, warnings |
2424 | `src/Link.raz` | 381 | 298 | Tree shaking, layout, patching, the places |
2525 | `src/Elf.raz` | 449 | 355 | Static and dynamic ELF, symbol table, line numbers |
2626 | `src/Raz.raz` | 326 | 253 | The command line |
@@ -1374,7 +1374,25 @@
13741374
13751375 Calls: `beginCall`, then `passArg` for each argument, then `endCall`,
13761376 which has Gen's `call` make the call. Each argument is left on Gen's
1377-stack, a slice as its two words (`pushWords`). `return f(x);` passing on
1377+stack, a slice as its two words (`pushWords`).
1378+
1379+A `const` parameter (report section 8) is a `var` one that is never
1380+changed: its `Obj` has `isVar`, so Gen passes and reaches it by
1381+address as it does a `var`, and `isConst` too, which makes an item
1382+for it, or for a part of it, `ro`, as another module's variable is.
1383+`onlyRead` tells the two apart, so that `notReadOnly` says "is only
1384+read here", and a slice of its array may still be made, to read, as a
1385+constant's may. `passArg` gives a variable's own address, where a value
1386+parameter's record is copied first:
1387+
1388+ value parameter, a record: copyToTemp(a); address(a)
1389+ const parameter: address(a) (* a variable, in place *)
1390+
1391+and anything else, a constant number, an expression, a call's result,
1392+a packed record's field, it puts in a temporary of the statement first
1393+(`constTemp`). A `match` on something only read gives names that are
1394+`isConst` too (`matchStatement`), where another module's variable is
1395+copied first and its names are the copy's. `return f(x);` passing on
13781396 f's several results, with `returnAll`, pushes them from the call's
13791397 temporary if they are two words, or else copies them to the place its
13801398 own hidden pointer names.

doc/raz.7 (history) modified +9 -1

@@ -547,7 +547,15 @@
547547 .It
548548 A parameter is a copy; a
549549 .Cm var
550-one is the caller's variable, of the parameter's type exactly.
550+one is the caller's variable, of the parameter's type exactly; a
551+.Cm const
552+one is the caller's variable too, read in place and never changed, and
553+takes any expression of its type.
554+A procedure takes what it reads as a value when it is a few words, as
555+.Cm const
556+when it is more; what it changes as
557+.Cm var ;
558+and a pointer only for what it keeps.
551559 .It
552560 .Cm "proc f(x: INT): (INT, ERROR);"
553561 returns several results.

doc/raz.md (history) modified +4 -1

@@ -1437,7 +1437,10 @@
14371437 either, write it through a view (section 6.9), or assign the names a
14381438 `match` arm gives its fields. What a slice or a pointer in it points at
14391439 isn't part of it: the procedure may write through them, as it may
1440-through a value parameter's. Its argument is any expression of the
1440+through a value parameter's; and a slice it makes of an array in it,
1441+to read, is a slice like any other, which writes the caller's array if
1442+written through. `const` keeps the procedure from changing the
1443+variable by its name, not every path to its bytes. Its argument is any expression of the
14411444 parameter's type: a variable or a constant is passed in place, anything
14421445 else as a temporary of the statement, as an array a call returns is
14431446 (below). If the variable changes while the procedure runs, through a

src/Item.raz (history) modified +60 -6

@@ -267,7 +267,8 @@
267267 end;
268268 | oVar:
269269 x.mode := mVar;
270- x.ro := (o^.lev = 0) and (o^.home # Sym.curModule);
270+ (* another module's variable, and a const parameter, are only read *)
271+ x.ro := ((o^.lev = 0) and (o^.home # Sym.curModule)) or o^.isConst;
271272 x.isVar := not x.ro;
272273 if o^.ext then
273274 Gen.pushVar(globalAdr(o^.chunk), 8, false, Gen.kWord);
@@ -538,10 +539,24 @@
538539 return true;
539540 end;
540541
542+(* x is only read: a const parameter, a part of one, or a name a match
543+ gives a field of one, or of a constant (report sections 7.4 and 8). A
544+ part reached through a pointer or a slice in it isn't: what they point
545+ at is the caller's to change, as through a value parameter's. *)
546+proc onlyRead*(x: Item): BOOL;
547+begin
548+ return (x.mode = mVar) and x.ro and (x.obj # nil) and x.obj^.isConst;
549+end;
550+
541551 (* Another module's exported variable can be read, not changed, nor
542- handed to anything that could change it (report section 3). *)
552+ handed to anything that could change it (report section 3); nor can
553+ what is only read, above. *)
543554 proc notReadOnly*(x: Item): ERROR;
544555 begin
556+ if onlyRead(x) then
557+ return Scan.markHelp(x.pos, say1("{} is only read here, and can't be changed", Diag.source(x.pos)),
558+ "a const parameter, a constant, and the names a match gives their fields, are only read: take a var parameter to change it");
559+ end;
545560 if x.ro then
546561 return Scan.markAt(x.pos, Diag.say("{} is {}'s, and can't be changed outside it",
547562 [Fmt.str(Diag.source(x.pos)), Fmt.str(Scan.nameOf(x.obj^.home^.name))]));
@@ -605,7 +620,9 @@
605620 and Sym.same(s^.base, t^.base) then
606621 x.typ := t;
607622 elsif (s^.form = Sym.fArray) and (t^.form = Sym.fSlice) and Sym.same(s^.base, t^.base) then
608- try notReadOnly(x);
623+ (* a slice of what is only read may be made, to read, as of a
624+ constant; another module's array can't be lent to be written *)
625+ if not onlyRead(x) then try notReadOnly(x); end;
609626 if x.vol then return Scan.markAt(x.pos, "can't make a slice through a volatile pointer"); end;
610627 if x.odd then return Scan.markAt(x.pos, "can't make a slice of a field of a packed record"); end;
611628 if x.view then return noAddress(x, "can't make a slice of a view"); end;
@@ -783,7 +800,7 @@
783800 if x.vol then return Scan.markAt(x.pos, "can't make a slice through a volatile pointer"); end;
784801 if x.odd then return Scan.markAt(x.pos, "can't make a slice of a field of a packed record"); end;
785802 if x.view then return noAddress(x, "can't make a slice of a view"); end;
786- if t^.form = Sym.fArray then try notReadOnly(x); end;
803+ if (t^.form = Sym.fArray) and not onlyRead(x) then try notReadOnly(x); end; (* as in coerce *)
787804 try value(hi);
788805 inFrame := x.frame and (t^.form # Sym.fPtr);
789806 tmp := Gen.allocTemp(16, 8);
@@ -1555,6 +1572,31 @@
15551572 return nil;
15561573 end;
15571574
1575+(* A const parameter's argument that isn't a variable, or is one with no
1576+ address of its own (a packed record's field, a view, through ^VOLATILE),
1577+ in a temporary of the statement, whose address then goes (report
1578+ section 8): a slice's two words, a record or an array copied, or a
1579+ single value stored. *)
1580+proc constTemp(var x: Item): ERROR;
1581+var t: I64;
1582+begin
1583+ if x.typ^.form = Sym.fSlice then
1584+ try pushWords(x);
1585+ t := Gen.allocTemp(16, 8);
1586+ Gen.popVar(frameAdr(t + 8), 8); (* the length, on top *)
1587+ Gen.popVar(frameAdr(t), 8);
1588+ elsif Sym.isAggregate(x.typ) then
1589+ if x.view then return noAddress(x, "a view of a record or an array can't be a const parameter"); end;
1590+ return copyToTemp(x);
1591+ else
1592+ try value(x);
1593+ t := Gen.allocTemp(x.typ^.size, x.typ^.align);
1594+ Gen.popVar(frameAdr(t), scalarSize(x.typ));
1595+ end;
1596+ tempItem(x, x.typ, t);
1597+ return nil;
1598+end;
1599+
15581600 proc passArg*(var c: Gen.CallInfo; var a: Item): ERROR;
15591601 var p: ^Sym.Obj;
15601602 begin
@@ -1579,7 +1621,17 @@
15791621 c.isF[c.n] := (p^.typ^.form = Sym.fReal) and not p^.isVar;
15801622 passing := p;
15811623 passingAt := a.pos;
1582- if p^.isVar then
1624+ if p^.isConst then
1625+ (* by address, as var, but any expression of the type: a variable,
1626+ or a constant in read-only data, in place; anything else in a
1627+ temporary. Before:
1628+ value parameter, a record: copied to a temporary, its address goes
1629+ after, for const:
1630+ its own address goes; nothing is copied *)
1631+ try coerce(a, p^.typ);
1632+ if (a.mode # mVar) or a.odd or a.view or a.vol then try constTemp(a); end;
1633+ try address(a);
1634+ elsif p^.isVar then
15831635 try notReadOnly(a);
15841636 if (a.mode # mVar) or not a.isVar then return Scan.markAt(a.pos, "a var parameter needs a variable"); end;
15851637 if a.odd then return Scan.markAt(a.pos, "a field of a packed record can't be a var parameter"); end;
@@ -1718,7 +1770,9 @@
17181770 if (x.mode # mVar) or (x.typ^.form # Sym.fVariant) then
17191771 return 0, Scan.markAt(x.pos, say1("match needs a variant, an enumeration, an integer or a string, found {}", what(x)));
17201772 end;
1721- if x.ro then try copyToTemp(x); end; (* its arms' names stand for a copy's fields *)
1773+ (* another module's variable: its arms' names stand for a copy's fields;
1774+ what is only read is matched in place, its names only read too *)
1775+ if x.ro and not onlyRead(x) then try copyToTemp(x); end;
17221776 try address(x);
17231777 p := Gen.allocTemp(8, 8);
17241778 Gen.popVar(frameAdr(p), 8);

src/Parse.raz (history) modified +12 -6

@@ -712,7 +712,7 @@
712712
713713 proc signature: (PType, ERROR);
714714 var
715- t, pt: PType; p, first, last: PObj; isVar: BOOL; n: INT;
715+ t, pt: PType; p, first, last: PObj; isVar, isConst: BOOL; n: INT;
716716 begin
717717 t := Sym.newType(Sym.fProc, 8, 8);
718718 last := nil; n := 0;
@@ -725,12 +725,15 @@
725725 Scan.next; t^.variadic := true; break;
726726 end;
727727 isVar := Scan.tok = Scan.sVar;
728- if isVar then Scan.next; end;
728+ isConst := Scan.tok = Scan.sConst;
729+ if isVar or isConst then Scan.next; end;
729730 first := nil;
730731 repeat
731732 if Scan.tok # Scan.sIdent then return nil, expected("a name"); end;
732733 p := Sym.newObj(Sym.oVar, Scan.id, nil);
733- p^.isVar := isVar;
734+ (* a const parameter goes by address, as a var one *)
735+ p^.isVar := isVar or isConst;
736+ p^.isConst := isConst;
734737 p^.lev := 1;
735738 if last = nil then t^.fields := p; else last^.next := p; end;
736739 last := p;
@@ -2084,11 +2087,13 @@
20842087 proc matchStatement(start: Diag.Pos; ind: INT): ERROR;
20852088 var
20862089 x: TItem; t: PType; c, f, o: PObj; p: I64; L, endL, n, first, disp, elseAt: INT;
2087- seen: [256]BOOL; hasElse, all, byValue, isStr: BOOL; endAt: Diag.Pos; ls: Labels;
2090+ seen: [256]BOOL; hasElse, all, byValue, isStr, onlyRead: BOOL; endAt: Diag.Pos; ls: Labels;
20882091 begin
20892092 Scan.next;
20902093 try expression(x, nil);
20912094 if x.typ^.form = Sym.fConst then try Item.coerce(x, Sym.tI64); end;
2095+ (* a const parameter or a constant: then so are the names its arms give *)
2096+ onlyRead := Item.onlyRead(x) or ((x.mode = Item.mVar) and (x.obj # nil) and (x.obj^.cls = Sym.oConst));
20922097 p := try Item.matchBegin(x);
20932098 t := x.typ;
20942099 isStr := t^.form = Sym.fSlice; (* matchBegin made a string a slice *)
@@ -2154,7 +2159,7 @@
21542159 return Scan.mark(say1("more names than {} has fields", nameOf(c^.name)));
21552160 end;
21562161 o := Sym.newObj(Sym.oVar, Scan.id, f^.typ);
2157- o^.lev := 1; o^.ind := true; o^.isVar := true;
2162+ o^.lev := 1; o^.ind := true; o^.isVar := true; o^.isConst := onlyRead;
21582163 o^.val := p; o^.off := f^.val;
21592164 try declare(o);
21602165 f := f^.next;
@@ -2898,6 +2903,7 @@
28982903 o^.pos := q^.pos;
28992904 o^.lev := 1;
29002905 o^.isVar := q^.isVar;
2906+ o^.isConst := q^.isConst;
29012907 o^.param := true;
29022908 if last = nil then params := o; else last^.next := o; end;
29032909 last := o;
@@ -3637,7 +3643,7 @@
36373643 Diag.warning(o^.pos, say1("{} may reach its end without return, and then returns zero", s));
36383644 end;
36393645 if o^.exported or (s[0] = '_') then return; end;
3640- if o^.used and (o^.read or (o^.cls # Sym.oVar) or o^.isVar or o^.ext) then return; end;
3646+ if o^.used and (o^.read or (o^.cls # Sym.oVar) or (o^.isVar and not o^.isConst) or o^.ext) then return; end;
36413647 match o^.cls of
36423648 | oConst: t := say1("constant {}", s);
36433649 | oType: t := say1("type {}", s);

src/Sym.raz (history) modified +9 -4

@@ -58,7 +58,9 @@
5858 lev*: INT; (* oVar: 0 for a module variable, 1 for a local *)
5959 ind*: BOOL; (* oVar: the frame slot holds the variable's address *)
6060 off*: I64; (* oVar with ind: added to that address *)
61- isVar*: BOOL; (* a var parameter *)
61+ isVar*: BOOL; (* a var parameter, or a const one: passed by address *)
62+ isConst*: BOOL; (* a const parameter, or a name a match gives a field
63+ of something only read: never changed *)
6264 param*: BOOL; (* oVar: a parameter *)
6365 used*: BOOL; (* named somewhere but in its own body *)
6466 read*: BOOL; (* oVar: named other than as what := assigns *)
@@ -472,7 +474,9 @@
472474 if (a^.base # nil) and not same(a^.base, b^.base) then return false; end;
473475 p := a^.fields; q := b^.fields;
474476 while p # nil do
475- if (p^.isVar # q^.isVar) or not same(p^.typ, q^.typ) then return false; end;
477+ if (p^.isVar # q^.isVar) or (p^.isConst # q^.isConst) or not same(p^.typ, q^.typ) then
478+ return false;
479+ end;
476480 p := p^.next; q := q^.next;
477481 end;
478482 return true;
@@ -550,7 +554,8 @@
550554 Buf.str(b, "proc(");
551555 f := t^.fields;
552556 while f # nil do
553- if f^.isVar then Buf.str(b, "var "); end;
557+ if f^.isConst then Buf.str(b, "const ");
558+ elsif f^.isVar then Buf.str(b, "var "); end;
554559 typeText(b, f^.typ);
555560 f := f^.next;
556561 if f # nil then Buf.str(b, ", "); end;
@@ -606,7 +611,7 @@
606611 while f # nil do
607612 if (t^.form # fRecord) or f^.exported then
608613 mix(h, f^.name);
609- mix(h, bit(f^.isVar));
614+ mix(h, bit(f^.isVar) + 2 * bit(f^.isConst));
610615 shape(h, f^.typ, false);
611616 g := f^.fields;
612617 while g # nil do shape(h, g^.typ, false); g := g^.next; end;

test/msg/ConstAdr.err (history) added +5 -0

@@ -0,0 +1,5 @@
1+conform/err/ConstAdr.raz:5:16: error: r is only read here, and can't be changed
2+ 5 | return ADR(r);
3+ | ^
4+help: a const parameter, a constant, and the names a match gives their fields, are only read: take a var parameter to change it
5+raz: 1 error

test/msg/ConstAssign.err (history) added +5 -0

@@ -0,0 +1,5 @@
1+conform/err/ConstAssign.raz:5:5: error: r.n is only read here, and can't be changed
2+ 5 | r.n := 1;
3+ | ^^^
4+help: a const parameter, a constant, and the names a match gives their fields, are only read: take a var parameter to change it
5+raz: 1 error

test/msg/ConstMatchName.err (history) added +5 -0

@@ -0,0 +1,5 @@
1+conform/err/ConstMatchName.raz:6:18: error: r is only read here, and can't be changed
2+ 6 | | circle(r): r := 0;
3+ | ^
4+help: a const parameter, a constant, and the names a match gives their fields, are only read: take a var parameter to change it
5+raz: 1 error

test/msg/ConstProcType.err (history) added +4 -0

@@ -0,0 +1,4 @@
1+conform/err/ConstProcType.raz:7:10: error: can't use proc(R): INT as proc(const R): INT
2+ 7 | p := f;
3+ | ^
4+raz: 1 error

test/msg/ConstVarArg.err (history) added +5 -0

@@ -0,0 +1,5 @@
1+conform/err/ConstVarArg.raz:6:7: error: r is only read here, and can't be changed
2+ 6 | g(r);
3+ | ^
4+help: a const parameter, a constant, and the names a match gives their fields, are only read: take a var parameter to change it
5+raz: 1 error