From 500f3369b975e9163b3e5ab355b1f19d0f9c599f Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Sat, 17 Oct 2026 09:35:39 +0000 Subject: [PATCH] src/'s Items only read on every program's paths are const Sixteen routines of Item and Parse that only read an Item, or Parse's array of names seen, take it as const, now that the seed doesn't build src/: callgrind counts 0.4% fewer instructions on a generated program, each of them saving some. The eleven reached only to word an error save nothing measured and keep the value. --- diff --git a/doc/compiler.md b/doc/compiler.md index 148a27b..d324274 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -1403,6 +1403,14 @@ `isConst` too (`matchStatement`), where another module's variable is copied first and its names are the copy's. +The compiler takes its own `Item`, about 130 bytes, as `const` where +it only reads it on the paths every program goes through (`adrOf`, +`discard`, `onlyRead`, `zeroRange`, `forCmp`, Parse's `assignable` +and `clearMissing`, ...): measured with callgrind, each such copy gone +saves instructions, 0.4% in all. The routines reached only to word an +error (`what`, `mismatch`, `doesntApply`, ...) take it as a value, +where a copy costs nothing that is measured. + An address given where a `var` or `const` parameter of its type wants the variable, `f(ADR(b))` or `f(p)`, is the commonest slip in a program written before the library took `var`; `givenAddress` says what to diff --git a/src/Item.raz b/src/Item.raz index ee74e81..234ce1b 100644 --- a/src/Item.raz +++ b/src/Item.raz @@ -158,7 +158,7 @@ return (t^.form = Sym.fReal) and (t^.size = 4); end; -proc realOf*(x: Item): F64; +proc realOf*(const x: Item): F64; var v: I64; p: ^F64; begin v := x.val; @@ -198,7 +198,7 @@ (* Where the variable x is, for Gen; a slice in registers has two. x is a var parameter only so as not to be copied, as it is a whole Item. *) -proc adrOf*(x: Item): Gen.Adr; +proc adrOf*(const x: Item): Gen.Adr; var hi: INT; begin hi := 0; @@ -409,7 +409,7 @@ end; (* x isn't wanted: what it has on the stack is dropped *) -proc discard*(x: Item): ERROR; +proc discard*(const x: Item): ERROR; var y: Item; begin if x.mode = mCond then @@ -543,7 +543,7 @@ 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; +proc onlyRead*(const x: Item): BOOL; begin return (x.mode = mVar) and x.ro and (x.obj # nil) and x.obj^.isConst; end; @@ -551,7 +551,7 @@ (* Another module's exported variable can be read, not changed, nor handed to anything that could change it (report section 3); nor can what is only read, above. *) -proc notReadOnly*(x: Item): ERROR; +proc notReadOnly*(const 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)), @@ -1576,7 +1576,7 @@ variable itself, as ADR(b) for var b: Buf: an error that says what to write instead, b for ADR(b) and p^ for a pointer p. Mostly met in programs written before the library took var (report 2026-10.33). *) -proc givenAddress(a: Item; p: ^Sym.Obj): ERROR; +proc givenAddress(const a: Item; p: ^Sym.Obj): ERROR; var s, kind: []U8; begin if (a.typ^.form # Sym.fPtr) or (a.typ^.base = nil) or not Sym.same(a.typ^.base, p^.typ) then @@ -1749,7 +1749,7 @@ end; (* size bytes, off bytes into the variable x, cleared *) -proc zeroRange*(x: Item; off, size: I64); +proc zeroRange*(const x: Item; off, size: I64); var a: Gen.Adr; begin a := adrOf(x); @@ -1764,7 +1764,7 @@ Gen.popVar(frameAdr(x.off), 8); end; -proc setField*(x: Item; f: ^Sym.Obj; var a: Item): ERROR; +proc setField*(const x: Item; f: ^Sym.Obj; var a: Item): ERROR; var at: Gen.Adr; begin at := adrOf(x); @@ -1865,7 +1865,7 @@ (* For a for loop: pushes the variable o and the limit lim, a constant or the slot it was put in, and jumps to L if the one is op the other, as the type of o compares *) -proc forCmp*(o: ^Sym.Obj; lim: Item; op: Scan.Sym; var L: INT): ERROR; +proc forCmp*(o: ^Sym.Obj; const lim: Item; op: Scan.Sym; var L: INT): ERROR; var v, b: Item; begin try makeItem(v, o); @@ -1887,7 +1887,7 @@ For another step: v past b goes to past; else b - v (unsigned, as v <= b) >= |step| goes on to top, and else the loop ends. Past: v is stepped here and tested, going to body if within b. *) -proc forNext*(o: ^Sym.Obj; lim: Item; step: I64; var exitL: INT; top, body: INT): ERROR; +proc forNext*(o: ^Sym.Obj; const lim: Item; step: I64; var exitL: INT; top, body: INT): ERROR; var v, b: Item; L, pastL: INT; up: BOOL; before, past, within: Scan.Sym; a: I64; begin up := step > 0; @@ -1974,7 +1974,7 @@ that is one, an array literal or a temporary, or a slice of one, a field or an element of one included; x.frame says so, set where the variable is named and kept through fields, elements and slices. *) -proc notOfFrame(x: Item; t: ^Sym.Type): ERROR; +proc notOfFrame(const x: Item; t: ^Sym.Type): ERROR; begin if (t^.form = Sym.fSlice) and x.frame then return Scan.markHelp(x.pos, "can't return a slice of the procedure's own frame", diff --git a/src/Parse.raz b/src/Parse.raz index c7cfa5e..88eb65e 100644 --- a/src/Parse.raz +++ b/src/Parse.raz @@ -1262,7 +1262,7 @@ (* The padding of the record type t, the bytes between its fields and after the last, cleared in x: a word at a time, so a few stores *) -proc clearPadding(x: TItem; t: PType); +proc clearPadding(const x: TItem; t: PType); var f: PObj; at, lo, hi: I64; begin at := 0; @@ -1283,7 +1283,7 @@ (* The fields of the record type t that a literal left out, of which seen are the n names it gave, cleared in x *) -proc clearMissing(x: TItem; t: PType; seen: [MaxNames]INT; n: INT); +proc clearMissing(const x: TItem; t: PType; const seen: [MaxNames]INT; n: INT); var f: PObj; i: INT; given: BOOL; begin f := t^.fields; @@ -2218,7 +2218,7 @@ (* x, named first n, is a variable the program may assign: not an array constant, nor any part of one. *) -proc assignable(x: TItem; n: INT): ERROR; +proc assignable(const x: TItem; n: INT): ERROR; begin if (x.mode = Item.mConst) and (subject(x, n) = nameOf(n)) then return Scan.markAt(x.pos, say1("{} is a constant, and can't be changed", nameOf(n))); @@ -2368,7 +2368,7 @@ symbol, is x changed by adding to it, taking from it or its bits: x + e, x - e, x | e or x xor e, with x nowhere in e, so that it can be worked out in x's own register, if x is in one (Gen's inPlace). *) -proc changesItself(x: TItem): BOOL; +proc changesItself(const x: TItem): BOOL; var st: Scan.State; wasSkipping, changes: BOOL; d: INT; begin if (x.mode # Item.mVar) or (x.base # Gen.bReg) or (x.typ^.form = Sym.fSlice) @@ -2400,7 +2400,7 @@ (* A call standing on its own, which drops the results x holds, if any, to be warned of *) -proc noteDrop(x: TItem); +proc noteDrop(const x: TItem); begin if (x.mode # Item.mNone) and (numDrops < MaxDrops) then drops[numDrops].at := x.pos;