From b13f87f8856403dbb56aab386a3f93950e515d82 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Sat, 17 Oct 2026 08:35:44 +0000 Subject: [PATCH] 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. --- diff --git a/conform/err/ConstAdr.raz b/conform/err/ConstAdr.raz new file mode 100644 index 0000000..9b77ed3 --- /dev/null +++ b/conform/err/ConstAdr.raz @@ -0,0 +1,12 @@ +module ConstAdr; +type R = record n: INT; end; +proc f(const r: R): ^R; +begin + return ADR(r); +end; +proc main*; +var r: R; +begin + _ := f(r); +end; +end ConstAdr. diff --git a/conform/err/ConstAssign.raz b/conform/err/ConstAssign.raz new file mode 100644 index 0000000..677c04f --- /dev/null +++ b/conform/err/ConstAssign.raz @@ -0,0 +1,12 @@ +module ConstAssign; +type R = record n: INT; end; +proc f(const r: R); +begin + r.n := 1; +end; +proc main*; +var r: R; +begin + f(r); +end; +end ConstAssign. diff --git a/conform/err/ConstMatchName.raz b/conform/err/ConstMatchName.raz new file mode 100644 index 0000000..49f5154 --- /dev/null +++ b/conform/err/ConstMatchName.raz @@ -0,0 +1,15 @@ +module ConstMatchName; +type S = variant circle(r: INT); dot; end; +proc f(const s: S); +begin + match s of + | circle(r): r := 0; + | dot: + end; +end; +proc main*; +var s: S; +begin + f(s); +end; +end ConstMatchName. diff --git a/conform/err/ConstProcType.raz b/conform/err/ConstProcType.raz new file mode 100644 index 0000000..7ad3bdc --- /dev/null +++ b/conform/err/ConstProcType.raz @@ -0,0 +1,10 @@ +module ConstProcType; +type R = record n: INT; end; +proc f(r: R): INT; begin return r.n; end; +proc main*; +var p: proc(const r: R): INT; +begin + p := f; + _ := p; +end; +end ConstProcType. diff --git a/conform/err/ConstVarArg.raz b/conform/err/ConstVarArg.raz new file mode 100644 index 0000000..1d15c45 --- /dev/null +++ b/conform/err/ConstVarArg.raz @@ -0,0 +1,13 @@ +module ConstVarArg; +type R = record n: INT; end; +proc g(var r: R); begin r.n := 0; end; +proc f(const r: R); +begin + g(r); +end; +proc main*; +var r: R; +begin + f(r); +end; +end ConstVarArg. diff --git a/conform/run/ConstParam.expect b/conform/run/ConstParam.expect new file mode 100644 index 0000000..ff32a5b --- /dev/null +++ b/conform/run/ConstParam.expect @@ -0,0 +1,12 @@ +4 +10 +5 +104 +42 +4 +77 +12 +12 +Tbc +80 +9 diff --git a/conform/run/ConstParam.raz b/conform/run/ConstParam.raz new file mode 100644 index 0000000..0cb42a7 --- /dev/null +++ b/conform/run/ConstParam.raz @@ -0,0 +1,79 @@ +module ConstParam; +import Out; + +type + Big = record a: [100]INT; name: []U8; n: INT; end; + Shape = variant circle(r: INT); rect(w, h: INT); end; + +const Ten: Big = {n: 10, name: "ten"}; + +var g: Big; + +proc sum(const b: Big): INT; +var i, s: INT; +begin + s := b.n; + for i := 0 to 99 do s := s + b.a[i]; end; + return s; +end; + +proc first(const s: []U8): U8; +begin + return s[0]; +end; + +proc twice(const n: INT): INT; +begin + return n * 2; +end; + +proc seen(const b: Big; var w: Big): INT; (* sees w's change: same variable *) +begin + w.n := 77; + return b.n; +end; + +proc area(const s: Shape): INT; +begin + match s of + | circle(r): return 3 * r * r; + | rect(w, h): return w * h; + end; +end; + +proc make(n: INT): Big; +var b: Big; +begin + b.n := n; + return b; +end; + +proc label(const b: Big): []U8; +begin + b.name[0] := 'T'; (* allowed: the bytes a slice in it points at *) + return b.name; +end; + +proc main*; +var b: Big; sh: Shape; buf: [3]U8; f: proc(const b: Big): INT; +begin + b.a[5] := 3; b.n := 1; + Out.int(sum(b)); Out.ln; + Out.int(sum(Ten)); Out.ln; + Out.int(sum(make(5))); Out.ln; + Out.int(I64(first("hey"))); Out.ln; + Out.int(twice(21)); Out.ln; + Out.int(twice(b.n + 1)); Out.ln; + Out.int(seen(b, b)); Out.ln; + sh := rect(3, 4); + Out.int(area(sh)); Out.ln; + Out.int(area(circle(2))); Out.ln; + buf := "abc"; b.name := buf[0..3]; + Out.str(label(b)); Out.ln; + f := sum; + Out.int(f(b)); Out.ln; + g.n := 9; + Out.int(sum(g)); Out.ln; +end; + +end ConstParam. diff --git a/doc/compiler.md b/doc/compiler.md index 6c57db3..8a17358 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -1,6 +1,6 @@ # Reading the raz compiler -The compiler is eleven files of raz, about 11,200 lines of code (the +The compiler is eleven files of raz, about 11,400 lines of code (the budget is 14,000). It is its own compiler: the frozen Pascal compiler in `seed/` builds `src/` the first time, and after that a raz builds `src/` (`make fixpoint` checks that it makes @@ -14,13 +14,13 @@ | File | Lines | Code | What | |---|---|---|---| | `src/Diag.raz` | 410 | 315 | Messages: positions, source excerpts, colour, counts | -| `src/Scan.raz` | 1,125 | 934 | Symbols, literals, comments, positions | -| `src/Sym.raz` | 707 | 566 | Types, objects, scopes, layout | +| `src/Scan.raz` | 1,148 | 949 | Symbols, literals, comments, positions | +| `src/Sym.raz` | 712 | 570 | Types, objects, scopes, layout | | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes | | `src/X64.raz` | 1,517 | 1,253 | The instruction encoder, jumps within a procedure, the assembler | | `src/Gen.raz` | 2,552 | 1,821 | The stack machine, for x86-64: values, operations, checks, calls, frames | -| `src/Item.raz` | 2,037 | 1,651 | Items: types, constants, variables, operators, conversions, calls | -| `src/Parse.raz` | 4,176 | 3,615 | Declarations, statements, expressions, built-ins, recovery, warnings | +| `src/Item.raz` | 2,091 | 1,682 | Items: types, constants, variables, operators, conversions, calls | +| `src/Parse.raz` | 4,182 | 3,619 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/Link.raz` | 381 | 298 | Tree shaking, layout, patching, the places | | `src/Elf.raz` | 449 | 355 | Static and dynamic ELF, symbol table, line numbers | | `src/Raz.raz` | 326 | 253 | The command line | @@ -1374,7 +1374,25 @@ Calls: `beginCall`, then `passArg` for each argument, then `endCall`, which has Gen's `call` make the call. Each argument is left on Gen's -stack, a slice as its two words (`pushWords`). `return f(x);` passing on +stack, a slice as its two words (`pushWords`). + +A `const` parameter (report section 8) is a `var` one that is never +changed: its `Obj` has `isVar`, so Gen passes and reaches it by +address as it does a `var`, and `isConst` too, which makes an item +for it, or for a part of it, `ro`, as another module's variable is. +`onlyRead` tells the two apart, so that `notReadOnly` says "is only +read here", and a slice of its array may still be made, to read, as a +constant's may. `passArg` gives a variable's own address, where a value +parameter's record is copied first: + + value parameter, a record: copyToTemp(a); address(a) + const parameter: address(a) (* a variable, in place *) + +and anything else, a constant number, an expression, a call's result, +a packed record's field, it puts in a temporary of the statement first +(`constTemp`). A `match` on something only read gives names that are +`isConst` too (`matchStatement`), where another module's variable is +copied first and its names are the copy's. `return f(x);` passing on f's several results, with `returnAll`, pushes them from the call's temporary if they are two words, or else copies them to the place its own hidden pointer names. diff --git a/doc/raz.7 b/doc/raz.7 index 185c251..f3e3da6 100644 --- a/doc/raz.7 +++ b/doc/raz.7 @@ -547,7 +547,15 @@ .It A parameter is a copy; a .Cm var -one is the caller's variable, of the parameter's type exactly. +one is the caller's variable, of the parameter's type exactly; a +.Cm const +one is the caller's variable too, read in place and never changed, and +takes any expression of its type. +A procedure takes what it reads as a value when it is a few words, as +.Cm const +when it is more; what it changes as +.Cm var ; +and a pointer only for what it keeps. .It .Cm "proc f(x: INT): (INT, ERROR);" returns several results. diff --git a/doc/raz.md b/doc/raz.md index c53edea..1482adb 100644 --- a/doc/raz.md +++ b/doc/raz.md @@ -1437,7 +1437,10 @@ either, write it through a view (section 6.9), or assign the names a `match` arm gives its fields. What a slice or a pointer in it points at isn't part of it: the procedure may write through them, as it may -through a value parameter's. Its argument is any expression of the +through a value parameter's; and a slice it makes of an array in it, +to read, is a slice like any other, which writes the caller's array if +written through. `const` keeps the procedure from changing the +variable by its name, not every path to its bytes. Its argument is any expression of the parameter's type: a variable or a constant is passed in place, anything else as a temporary of the statement, as an array a call returns is (below). If the variable changes while the procedure runs, through a diff --git a/src/Item.raz b/src/Item.raz index befd3d5..99118fe 100644 --- a/src/Item.raz +++ b/src/Item.raz @@ -267,7 +267,8 @@ end; | oVar: x.mode := mVar; - x.ro := (o^.lev = 0) and (o^.home # Sym.curModule); + (* another module's variable, and a const parameter, are only read *) + x.ro := ((o^.lev = 0) and (o^.home # Sym.curModule)) or o^.isConst; x.isVar := not x.ro; if o^.ext then Gen.pushVar(globalAdr(o^.chunk), 8, false, Gen.kWord); @@ -538,10 +539,24 @@ return true; end; +(* x is only read: a const parameter, a part of one, or a name a match + gives a field of one, or of a constant (report sections 7.4 and 8). A + part reached through a pointer or a slice in it isn't: what they point + at is the caller's to change, as through a value parameter's. *) +proc onlyRead*(x: Item): BOOL; +begin + return (x.mode = mVar) and x.ro and (x.obj # nil) and x.obj^.isConst; +end; + (* Another module's exported variable can be read, not changed, nor - handed to anything that could change it (report section 3). *) + handed to anything that could change it (report section 3); nor can + what is only read, above. *) proc notReadOnly*(x: Item): ERROR; begin + if onlyRead(x) then + return Scan.markHelp(x.pos, say1("{} is only read here, and can't be changed", Diag.source(x.pos)), + "a const parameter, a constant, and the names a match gives their fields, are only read: take a var parameter to change it"); + end; if x.ro then return Scan.markAt(x.pos, Diag.say("{} is {}'s, and can't be changed outside it", [Fmt.str(Diag.source(x.pos)), Fmt.str(Scan.nameOf(x.obj^.home^.name))])); @@ -605,7 +620,9 @@ and Sym.same(s^.base, t^.base) then x.typ := t; elsif (s^.form = Sym.fArray) and (t^.form = Sym.fSlice) and Sym.same(s^.base, t^.base) then - try notReadOnly(x); + (* a slice of what is only read may be made, to read, as of a + constant; another module's array can't be lent to be written *) + if not onlyRead(x) then try notReadOnly(x); end; if x.vol then return Scan.markAt(x.pos, "can't make a slice through a volatile pointer"); end; if x.odd then return Scan.markAt(x.pos, "can't make a slice of a field of a packed record"); end; if x.view then return noAddress(x, "can't make a slice of a view"); end; @@ -783,7 +800,7 @@ if x.vol then return Scan.markAt(x.pos, "can't make a slice through a volatile pointer"); end; if x.odd then return Scan.markAt(x.pos, "can't make a slice of a field of a packed record"); end; if x.view then return noAddress(x, "can't make a slice of a view"); end; - if t^.form = Sym.fArray then try notReadOnly(x); end; + if (t^.form = Sym.fArray) and not onlyRead(x) then try notReadOnly(x); end; (* as in coerce *) try value(hi); inFrame := x.frame and (t^.form # Sym.fPtr); tmp := Gen.allocTemp(16, 8); @@ -1555,6 +1572,31 @@ return nil; end; +(* A const parameter's argument that isn't a variable, or is one with no + address of its own (a packed record's field, a view, through ^VOLATILE), + in a temporary of the statement, whose address then goes (report + section 8): a slice's two words, a record or an array copied, or a + single value stored. *) +proc constTemp(var x: Item): ERROR; +var t: I64; +begin + if x.typ^.form = Sym.fSlice then + try pushWords(x); + t := Gen.allocTemp(16, 8); + Gen.popVar(frameAdr(t + 8), 8); (* the length, on top *) + Gen.popVar(frameAdr(t), 8); + elsif Sym.isAggregate(x.typ) then + if x.view then return noAddress(x, "a view of a record or an array can't be a const parameter"); end; + return copyToTemp(x); + else + try value(x); + t := Gen.allocTemp(x.typ^.size, x.typ^.align); + Gen.popVar(frameAdr(t), scalarSize(x.typ)); + end; + tempItem(x, x.typ, t); + return nil; +end; + proc passArg*(var c: Gen.CallInfo; var a: Item): ERROR; var p: ^Sym.Obj; begin @@ -1579,7 +1621,17 @@ c.isF[c.n] := (p^.typ^.form = Sym.fReal) and not p^.isVar; passing := p; passingAt := a.pos; - if p^.isVar then + if p^.isConst then + (* by address, as var, but any expression of the type: a variable, + or a constant in read-only data, in place; anything else in a + temporary. Before: + value parameter, a record: copied to a temporary, its address goes + after, for const: + its own address goes; nothing is copied *) + try coerce(a, p^.typ); + if (a.mode # mVar) or a.odd or a.view or a.vol then try constTemp(a); end; + try address(a); + elsif p^.isVar then try notReadOnly(a); if (a.mode # mVar) or not a.isVar then return Scan.markAt(a.pos, "a var parameter needs a variable"); end; 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 @@ if (x.mode # mVar) or (x.typ^.form # Sym.fVariant) then return 0, Scan.markAt(x.pos, say1("match needs a variant, an enumeration, an integer or a string, found {}", what(x))); end; - if x.ro then try copyToTemp(x); end; (* its arms' names stand for a copy's fields *) + (* another module's variable: its arms' names stand for a copy's fields; + what is only read is matched in place, its names only read too *) + if x.ro and not onlyRead(x) then try copyToTemp(x); end; try address(x); p := Gen.allocTemp(8, 8); Gen.popVar(frameAdr(p), 8); diff --git a/src/Parse.raz b/src/Parse.raz index 1dea194..f3485dc 100644 --- a/src/Parse.raz +++ b/src/Parse.raz @@ -712,7 +712,7 @@ proc signature: (PType, ERROR); var - t, pt: PType; p, first, last: PObj; isVar: BOOL; n: INT; + t, pt: PType; p, first, last: PObj; isVar, isConst: BOOL; n: INT; begin t := Sym.newType(Sym.fProc, 8, 8); last := nil; n := 0; @@ -725,12 +725,15 @@ Scan.next; t^.variadic := true; break; end; isVar := Scan.tok = Scan.sVar; - if isVar then Scan.next; end; + isConst := Scan.tok = Scan.sConst; + if isVar or isConst then Scan.next; end; first := nil; repeat if Scan.tok # Scan.sIdent then return nil, expected("a name"); end; p := Sym.newObj(Sym.oVar, Scan.id, nil); - p^.isVar := isVar; + (* a const parameter goes by address, as a var one *) + p^.isVar := isVar or isConst; + p^.isConst := isConst; p^.lev := 1; if last = nil then t^.fields := p; else last^.next := p; end; last := p; @@ -2084,11 +2087,13 @@ proc matchStatement(start: Diag.Pos; ind: INT): ERROR; var x: TItem; t: PType; c, f, o: PObj; p: I64; L, endL, n, first, disp, elseAt: INT; - seen: [256]BOOL; hasElse, all, byValue, isStr: BOOL; endAt: Diag.Pos; ls: Labels; + seen: [256]BOOL; hasElse, all, byValue, isStr, onlyRead: BOOL; endAt: Diag.Pos; ls: Labels; begin Scan.next; try expression(x, nil); if x.typ^.form = Sym.fConst then try Item.coerce(x, Sym.tI64); end; + (* a const parameter or a constant: then so are the names its arms give *) + onlyRead := Item.onlyRead(x) or ((x.mode = Item.mVar) and (x.obj # nil) and (x.obj^.cls = Sym.oConst)); p := try Item.matchBegin(x); t := x.typ; isStr := t^.form = Sym.fSlice; (* matchBegin made a string a slice *) @@ -2154,7 +2159,7 @@ return Scan.mark(say1("more names than {} has fields", nameOf(c^.name))); end; o := Sym.newObj(Sym.oVar, Scan.id, f^.typ); - o^.lev := 1; o^.ind := true; o^.isVar := true; + o^.lev := 1; o^.ind := true; o^.isVar := true; o^.isConst := onlyRead; o^.val := p; o^.off := f^.val; try declare(o); f := f^.next; @@ -2898,6 +2903,7 @@ o^.pos := q^.pos; o^.lev := 1; o^.isVar := q^.isVar; + o^.isConst := q^.isConst; o^.param := true; if last = nil then params := o; else last^.next := o; end; last := o; @@ -3637,7 +3643,7 @@ Diag.warning(o^.pos, say1("{} may reach its end without return, and then returns zero", s)); end; if o^.exported or (s[0] = '_') then return; end; - if o^.used and (o^.read or (o^.cls # Sym.oVar) or o^.isVar or o^.ext) then return; end; + if o^.used and (o^.read or (o^.cls # Sym.oVar) or (o^.isVar and not o^.isConst) or o^.ext) then return; end; match o^.cls of | oConst: t := say1("constant {}", s); | oType: t := say1("type {}", s); diff --git a/src/Sym.raz b/src/Sym.raz index 7b0bdf4..810c771 100644 --- a/src/Sym.raz +++ b/src/Sym.raz @@ -58,7 +58,9 @@ lev*: INT; (* oVar: 0 for a module variable, 1 for a local *) ind*: BOOL; (* oVar: the frame slot holds the variable's address *) off*: I64; (* oVar with ind: added to that address *) - isVar*: BOOL; (* a var parameter *) + isVar*: BOOL; (* a var parameter, or a const one: passed by address *) + isConst*: BOOL; (* a const parameter, or a name a match gives a field + of something only read: never changed *) param*: BOOL; (* oVar: a parameter *) used*: BOOL; (* named somewhere but in its own body *) read*: BOOL; (* oVar: named other than as what := assigns *) @@ -472,7 +474,9 @@ if (a^.base # nil) and not same(a^.base, b^.base) then return false; end; p := a^.fields; q := b^.fields; while p # nil do - if (p^.isVar # q^.isVar) or not same(p^.typ, q^.typ) then return false; end; + if (p^.isVar # q^.isVar) or (p^.isConst # q^.isConst) or not same(p^.typ, q^.typ) then + return false; + end; p := p^.next; q := q^.next; end; return true; @@ -550,7 +554,8 @@ Buf.str(b, "proc("); f := t^.fields; while f # nil do - if f^.isVar then Buf.str(b, "var "); end; + if f^.isConst then Buf.str(b, "const "); + elsif f^.isVar then Buf.str(b, "var "); end; typeText(b, f^.typ); f := f^.next; if f # nil then Buf.str(b, ", "); end; @@ -606,7 +611,7 @@ while f # nil do if (t^.form # fRecord) or f^.exported then mix(h, f^.name); - mix(h, bit(f^.isVar)); + mix(h, bit(f^.isVar) + 2 * bit(f^.isConst)); shape(h, f^.typ, false); g := f^.fields; while g # nil do shape(h, g^.typ, false); g := g^.next; end; diff --git a/test/msg/ConstAdr.err b/test/msg/ConstAdr.err new file mode 100644 index 0000000..1753246 --- /dev/null +++ b/test/msg/ConstAdr.err @@ -0,0 +1,5 @@ +conform/err/ConstAdr.raz:5:16: error: r is only read here, and can't be changed + 5 | return ADR(r); + | ^ +help: a const parameter, a constant, and the names a match gives their fields, are only read: take a var parameter to change it +raz: 1 error diff --git a/test/msg/ConstAssign.err b/test/msg/ConstAssign.err new file mode 100644 index 0000000..a31484c --- /dev/null +++ b/test/msg/ConstAssign.err @@ -0,0 +1,5 @@ +conform/err/ConstAssign.raz:5:5: error: r.n is only read here, and can't be changed + 5 | r.n := 1; + | ^^^ +help: a const parameter, a constant, and the names a match gives their fields, are only read: take a var parameter to change it +raz: 1 error diff --git a/test/msg/ConstMatchName.err b/test/msg/ConstMatchName.err new file mode 100644 index 0000000..e27cb5a --- /dev/null +++ b/test/msg/ConstMatchName.err @@ -0,0 +1,5 @@ +conform/err/ConstMatchName.raz:6:18: error: r is only read here, and can't be changed + 6 | | circle(r): r := 0; + | ^ +help: a const parameter, a constant, and the names a match gives their fields, are only read: take a var parameter to change it +raz: 1 error diff --git a/test/msg/ConstProcType.err b/test/msg/ConstProcType.err new file mode 100644 index 0000000..b6c994d --- /dev/null +++ b/test/msg/ConstProcType.err @@ -0,0 +1,4 @@ +conform/err/ConstProcType.raz:7:10: error: can't use proc(R): INT as proc(const R): INT + 7 | p := f; + | ^ +raz: 1 error diff --git a/test/msg/ConstVarArg.err b/test/msg/ConstVarArg.err new file mode 100644 index 0000000..b1316a0 --- /dev/null +++ b/test/msg/ConstVarArg.err @@ -0,0 +1,5 @@ +conform/err/ConstVarArg.raz:6:7: error: r is only read here, and can't be changed + 6 | g(r); + | ^ +help: a const parameter, a constant, and the names a match gives their fields, are only read: take a var parameter to change it +raz: 1 error