raz / commit 7cf6e50c

commit
7cf6e50c700c8b610b398e86f676c3d4dae04260
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
90cd0ae0
Report 2026-10.26: views, v.(T)

patch · log

3 files changed, +103 -28

PROPOSALS.md modified +0 -22

@@ -15,28 +15,6 @@
1515 Measured once the port built: not what stands out, the compiler's
1616 profile is flat (`Scan.tokPos` is 9%).
1717
18-## Dereferencing a conversion
19-
20-`^I64(ADR(mem[a]))^` is an error today ("a conversion can't be
21-dereferenced"), and a statement can't start with one, so a program
22-that reads and writes typed values in an array of bytes declares a
23-pointer variable for each access:
24-
25-```
26-p := ^I64(ADR(mem[a]));
27-p^ := v;
28-```
29-
30-The proposal: a conversion to a pointer type may be followed by `^`,
31-and the designator it makes may stand where any other does, on the
32-left of `:=` too, so that the two lines are `^I64(ADR(mem[a]))^ := v;`.
33-The grammar's `Designator` would start with a `QualIdent` or such a
34-conversion.
35-
36-Met in tery, whose memory is an array of bytes holding cells: every
37-fetch and store of a cell, and in any program that reads a file
38-format or a device's registers through a byte buffer.
39-
4018 ## A minus sign after an operator
4119
4220 `x & -8` and `n * -1` are errors: a unary minus belongs to the start

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

@@ -24,7 +24,9 @@
2424 and may be indented with the code around them (section 2), and
2525 `match` on a string (section 7.4); it breaks no program. 2026-10.26
2626 is a fifth: an `INT` widens to an `I64` wherever it goes to one, in
27-an expression too (sections 5.10 and 6.1); it breaks no program.
27+an expression too (sections 5.10 and 6.1); and a view, `v.(T)`,
28+reads and writes the bytes of a variable as a `T` (section 6.9); it
29+breaks no program.
2830
2931 The language is frozen, softly, from this version on: it changes only
3032 to mend a defect, and its library grows only by what breaks no program
@@ -646,7 +648,7 @@
646648 Element = Expr | ident ":" Expr .
647649 Designator = QualIdent { Selector } .
648650 Selector = "." ident | "^" | "[" Expr [ ".." Expr ] "]"
649- | "(" [ Expr { "," Expr } ] ")" .
651+ | "(" [ Expr { "," Expr } ] ")" | "." "(" Type ")" .
650652 ```
651653
652654 The operators bind in the order of the grammar: `not`, `~` and `try`
@@ -661,7 +663,8 @@
661663
662664 A pointer to a record isn't the record: its fields are `p^.f`, never
663665 `p.f`, and the elements of a pointer to an array are `p^[i]`. A
664-statement starts with a name or `try`, so `^T(x)^ := v` isn't one.
666+statement starts with a name or `try`, so `^T(x)^ := v` isn't one; a
667+view, `mem[a].(I64) := v` (section 6.9), says it without a pointer.
665668
666669 ### 6.1 Integers
667670
@@ -862,7 +865,99 @@
862865 every target. They are sequentially consistent, on every target; how a
863866 machine without atomic instructions makes them so is the
864867 implementation's choice (appendix A).
868+### 6.9 Views
865869
870+A view reads and writes the bytes of a variable as a value of another
871+type. `v.(T)`, with `v` a variable, is the `T` whose bytes start where
872+`v` starts:
873+
874+```
875+var mem: [4096]U8; n: I64; x: F64; bits: U64;
876+
877+mem[16].(I64) := -1; (* the 8 bytes mem[16] to mem[23] now hold -1 *)
878+n := mem[16].(I64); (* and are read back as an I64: n is -1 *)
879+bits := x.(U64); (* the 8 bytes of x, as a U64 *)
880+```
881+
882+A view is a variable, not a value: `mem[16].(I64)` is made of the 8
883+bytes `mem[16]` to `mem[23]`, as `p^` is the variable `p` points at.
884+Reading it reads those bytes; assigning it writes them, and so changes
885+`mem`. Nothing is copied to make it. It stands wherever a variable
886+may: on the left of `:=`, as the variable of `INC`, and with selectors
887+after it:
888+
889+```
890+type Header = record magic: U32; count: U32; end;
891+var buf: [512]U8; k: U32;
892+
893+k := buf[0].(Header).count; (* bytes 4 to 7 of buf, as a U32 *)
894+buf[0].(Header).count := 0;
895+INC(mem[a].(I64), 8);
896+```
897+
898+**A view isn't a conversion.** A conversion works on a value, a view
899+on bytes. With `x: F64` holding 1.5, `I64(x)` is 1, the number rounded
900+toward zero, while `x.(I64)` is 3FF8000000000000H, the bits that hold
901+1.5. With `n: I64` holding 258, `U8(n)` is 2, the number cut to its low
902+byte, while `n.(U8)` is the first byte of `n` in memory, which is the
903+low one only on a target that puts its low byte first (appendix A).
904+When the program means a number, it wants a conversion; when it means
905+the bytes as they lie, a view.
906+
907+**The types.** `T` is an integer type, a real type, a pointer type, or
908+a record or array made of these alone, at any depth: the types for
909+which any bytes at all are a value. `BOOL`, an enumeration, a set, a
910+slice, a variant, a procedure type and `ERROR` are left out, since
911+bytes that are none of their values would make the program mean
912+nothing. To read one of them from bytes, view the integer and convert
913+it: `Colour(mem[a])`. `v` may be of any type, and is a variable: a
914+name of one, a field or element of one, or what a pointer points at;
915+not a constant, nor the result of a call or of an operator.
916+
917+**Where its bytes lie.** The `T` lies within the variable `v` is part
918+of:
919+
920+- for an element of an array or a slice, `a[i].(T)`, from that element
921+ to the end of `a`: `mem[4088].(I64)` is the last 8 bytes of `mem`,
922+ and `mem[4090].(I64)` would run 2 bytes past its end;
923+- for anything else, within `v` itself: `x.(U64)` is allowed only if
924+ `x` has at least 8 bytes.
925+
926+This is checked, as an index is: by the compiler when it can tell,
927+otherwise while the program runs, where a view that runs past the end
928+is a failed check (section 6.5), unless the module says `check off;`.
929+An element reached through a pointer, `p[i].(T)`, is checked no more
930+than `p[i]` is, since a pointer has no length.
931+
932+**Any address.** The `T` may start at any byte: `mem[3].(I64)` is an
933+`I64` whose first byte is `mem[3]`. As a field of a packed record is
934+(section 5.5), it is read and written on every target, in parts where
935+the machine needs that; and, as such a field, a view has no address:
936+`ADR` of it, a `var` argument of it and a slice of it are errors. Copy
937+it out to a variable, and back in. A view of a volatile variable is an
938+error too, since its access can't be promised as one (section 7.6): a
939+device's registers are reached through a `^VOLATILE` pointer to a
940+record of them.
941+
942+**Byte order.** The bytes are in the target's order (appendix A). A
943+file or a message whose format fixes an order of its own is read with
944+`Str.le`, `Str.be` and the like (section 5.5), if the program is to
945+read it right on every target.
946+
947+**Before views.** A program used to make a pointer for each access,
948+and may still: `mem[a].(I64) := n` does what
949+
950+```
951+p := ^I64(ADR(mem[a]));
952+p^ := n;
953+```
954+
955+does, but with `a` checked against the length of `mem`, no pointer
956+variable, and no address of a word that may be misaligned.
957+
958+`v` is evaluated once, and its check made, before the bytes are read
959+or written.
960+
866961 ## 7. Statements
867962
868963 ```
@@ -1543,7 +1638,7 @@
15431638 Element = Expr | ident ":" Expr .
15441639 Designator = QualIdent { Selector } .
15451640 Selector = "." ident | "^" | "[" Expr [ ".." Expr ] "]"
1546- | "(" [ Expr { "," Expr } ] ")" .
1641+ | "(" [ Expr { "," Expr } ] ")" | "." "(" Type ")" .
15471642
15481643 Statements = { Statement ";" } .
15491644 Statement = Designator [ ":=" Expr ]

doc/report-history.md (history) modified +4 -2

@@ -142,10 +142,12 @@
142142 `field`, `lower`, `upper`, `join`, `replace`, `decodeLast`,
143143 `next`, `valid`, `chars`, `toHex` and `fromInt`; `Math` gains
144144 `toReal` (volume 2).
145-- **2026-10.26.** A thaw, for `INT` and `I64`. An `INT` widens to an
145+- **2026-10.26.** A thaw, for `INT` and `I64`, and for views. An `INT` widens to an
146146 `I64`, and a `UINT` to a `U64`, wherever its value goes to one: in an
147147 expression, where it meets one, and in `a, b := f(x)`, `return
148148 f(x)`, a `for`'s bounds and the step of `INC` and `DEC` (sections
149- 5.10, 6.1 and 7.2). It breaks no program. `Sys` gains `position`,
149+ 5.10, 6.1 and 7.2). A view, `v.(T)`, reads and writes the bytes of
150+ the variable `v` as a `T`, checked to lie within it (section 6.9 and
151+ appendix B). It breaks no program. `Sys` gains `position`,
150152 `truncate`, `sync` and `infoOf`; `Opt` gains `at`, where the
151153 operands are (volume 2).