raz / commit 500f3369

commit
500f3369b975e9163b3e5ab355b1f19d0f9c599f
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
9b7ca9ea
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.

patch · log

3 files changed, +24 -16

doc/compiler.md (history) modified +8 -0

@@ -1403,6 +1403,14 @@
14031403 `isConst` too (`matchStatement`), where another module's variable is
14041404 copied first and its names are the copy's.
14051405
1406+The compiler takes its own `Item`, about 130 bytes, as `const` where
1407+it only reads it on the paths every program goes through (`adrOf`,
1408+`discard`, `onlyRead`, `zeroRange`, `forCmp`, Parse's `assignable`
1409+and `clearMissing`, ...): measured with callgrind, each such copy gone
1410+saves instructions, 0.4% in all. The routines reached only to word an
1411+error (`what`, `mismatch`, `doesntApply`, ...) take it as a value,
1412+where a copy costs nothing that is measured.
1413+
14061414 An address given where a `var` or `const` parameter of its type wants
14071415 the variable, `f(ADR(b))` or `f(p)`, is the commonest slip in a program
14081416 written before the library took `var`; `givenAddress` says what to

src/Item.raz (history) modified +11 -11

@@ -158,7 +158,7 @@
158158 return (t^.form = Sym.fReal) and (t^.size = 4);
159159 end;
160160
161-proc realOf*(x: Item): F64;
161+proc realOf*(const x: Item): F64;
162162 var v: I64; p: ^F64;
163163 begin
164164 v := x.val;
@@ -198,7 +198,7 @@
198198
199199 (* Where the variable x is, for Gen; a slice in registers has two. x is
200200 a var parameter only so as not to be copied, as it is a whole Item. *)
201-proc adrOf*(x: Item): Gen.Adr;
201+proc adrOf*(const x: Item): Gen.Adr;
202202 var hi: INT;
203203 begin
204204 hi := 0;
@@ -409,7 +409,7 @@
409409 end;
410410
411411 (* x isn't wanted: what it has on the stack is dropped *)
412-proc discard*(x: Item): ERROR;
412+proc discard*(const x: Item): ERROR;
413413 var y: Item;
414414 begin
415415 if x.mode = mCond then
@@ -543,7 +543,7 @@
543543 gives a field of one, or of a constant (report sections 7.4 and 8). A
544544 part reached through a pointer or a slice in it isn't: what they point
545545 at is the caller's to change, as through a value parameter's. *)
546-proc onlyRead*(x: Item): BOOL;
546+proc onlyRead*(const x: Item): BOOL;
547547 begin
548548 return (x.mode = mVar) and x.ro and (x.obj # nil) and x.obj^.isConst;
549549 end;
@@ -551,7 +551,7 @@
551551 (* Another module's exported variable can be read, not changed, nor
552552 handed to anything that could change it (report section 3); nor can
553553 what is only read, above. *)
554-proc notReadOnly*(x: Item): ERROR;
554+proc notReadOnly*(const x: Item): ERROR;
555555 begin
556556 if onlyRead(x) then
557557 return Scan.markHelp(x.pos, say1("{} is only read here, and can't be changed", Diag.source(x.pos)),
@@ -1576,7 +1576,7 @@
15761576 variable itself, as ADR(b) for var b: Buf: an error that says what to
15771577 write instead, b for ADR(b) and p^ for a pointer p. Mostly met in
15781578 programs written before the library took var (report 2026-10.33). *)
1579-proc givenAddress(a: Item; p: ^Sym.Obj): ERROR;
1579+proc givenAddress(const a: Item; p: ^Sym.Obj): ERROR;
15801580 var s, kind: []U8;
15811581 begin
15821582 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 @@
17491749 end;
17501750
17511751 (* size bytes, off bytes into the variable x, cleared *)
1752-proc zeroRange*(x: Item; off, size: I64);
1752+proc zeroRange*(const x: Item; off, size: I64);
17531753 var a: Gen.Adr;
17541754 begin
17551755 a := adrOf(x);
@@ -1764,7 +1764,7 @@
17641764 Gen.popVar(frameAdr(x.off), 8);
17651765 end;
17661766
1767-proc setField*(x: Item; f: ^Sym.Obj; var a: Item): ERROR;
1767+proc setField*(const x: Item; f: ^Sym.Obj; var a: Item): ERROR;
17681768 var at: Gen.Adr;
17691769 begin
17701770 at := adrOf(x);
@@ -1865,7 +1865,7 @@
18651865 (* For a for loop: pushes the variable o and the limit lim, a constant or
18661866 the slot it was put in, and jumps to L if the one is op the other, as
18671867 the type of o compares *)
1868-proc forCmp*(o: ^Sym.Obj; lim: Item; op: Scan.Sym; var L: INT): ERROR;
1868+proc forCmp*(o: ^Sym.Obj; const lim: Item; op: Scan.Sym; var L: INT): ERROR;
18691869 var v, b: Item;
18701870 begin
18711871 try makeItem(v, o);
@@ -1887,7 +1887,7 @@
18871887 For another step: v past b goes to past; else b - v (unsigned, as
18881888 v <= b) >= |step| goes on to top, and else the loop ends.
18891889 Past: v is stepped here and tested, going to body if within b. *)
1890-proc forNext*(o: ^Sym.Obj; lim: Item; step: I64; var exitL: INT; top, body: INT): ERROR;
1890+proc forNext*(o: ^Sym.Obj; const lim: Item; step: I64; var exitL: INT; top, body: INT): ERROR;
18911891 var v, b: Item; L, pastL: INT; up: BOOL; before, past, within: Scan.Sym; a: I64;
18921892 begin
18931893 up := step > 0;
@@ -1974,7 +1974,7 @@
19741974 that is one, an array literal or a temporary, or a slice of one, a
19751975 field or an element of one included; x.frame says so, set where the
19761976 variable is named and kept through fields, elements and slices. *)
1977-proc notOfFrame(x: Item; t: ^Sym.Type): ERROR;
1977+proc notOfFrame(const x: Item; t: ^Sym.Type): ERROR;
19781978 begin
19791979 if (t^.form = Sym.fSlice) and x.frame then
19801980 return Scan.markHelp(x.pos, "can't return a slice of the procedure's own frame",

src/Parse.raz (history) modified +5 -5

@@ -1262,7 +1262,7 @@
12621262
12631263 (* The padding of the record type t, the bytes between its fields and
12641264 after the last, cleared in x: a word at a time, so a few stores *)
1265-proc clearPadding(x: TItem; t: PType);
1265+proc clearPadding(const x: TItem; t: PType);
12661266 var f: PObj; at, lo, hi: I64;
12671267 begin
12681268 at := 0;
@@ -1283,7 +1283,7 @@
12831283
12841284 (* The fields of the record type t that a literal left out, of which seen
12851285 are the n names it gave, cleared in x *)
1286-proc clearMissing(x: TItem; t: PType; seen: [MaxNames]INT; n: INT);
1286+proc clearMissing(const x: TItem; t: PType; const seen: [MaxNames]INT; n: INT);
12871287 var f: PObj; i: INT; given: BOOL;
12881288 begin
12891289 f := t^.fields;
@@ -2218,7 +2218,7 @@
22182218
22192219 (* x, named first n, is a variable the program may assign: not an
22202220 array constant, nor any part of one. *)
2221-proc assignable(x: TItem; n: INT): ERROR;
2221+proc assignable(const x: TItem; n: INT): ERROR;
22222222 begin
22232223 if (x.mode = Item.mConst) and (subject(x, n) = nameOf(n)) then
22242224 return Scan.markAt(x.pos, say1("{} is a constant, and can't be changed", nameOf(n)));
@@ -2368,7 +2368,7 @@
23682368 symbol, is x changed by adding to it, taking from it or its bits:
23692369 x + e, x - e, x | e or x xor e, with x nowhere in e, so that it can be
23702370 worked out in x's own register, if x is in one (Gen's inPlace). *)
2371-proc changesItself(x: TItem): BOOL;
2371+proc changesItself(const x: TItem): BOOL;
23722372 var st: Scan.State; wasSkipping, changes: BOOL; d: INT;
23732373 begin
23742374 if (x.mode # Item.mVar) or (x.base # Gen.bReg) or (x.typ^.form = Sym.fSlice)
@@ -2400,7 +2400,7 @@
24002400
24012401 (* A call standing on its own, which drops the results x holds, if any,
24022402 to be warned of *)
2403-proc noteDrop(x: TItem);
2403+proc noteDrop(const x: TItem);
24042404 begin
24052405 if (x.mode # Item.mNone) and (numDrops < MaxDrops) then
24062406 drops[numDrops].at := x.pos;