From c473acc2e8e8d8bbba8425c15978525c6b816f7a Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 23 Oct 2026 12:02:19 +0100 Subject: [PATCH] raz.md: OFFSET, SIZE of a field, and BYTES --- diff --git a/doc/raz.7 b/doc/raz.7 index 7eaafd5..2ca973f 100644 --- a/doc/raz.7 +++ b/doc/raz.7 @@ -414,6 +414,18 @@ The size of type .Ar T , an untyped constant. +.It Fn SIZE T.f +The size of field +.Ar f +of the record type +.Ar T , +an untyped constant. +.It Fn OFFSET T f +The byte offset of field +.Ar f +in the record type +.Ar T , +an untyped constant. .It Fn ADR x The address of the variable .Ar x . @@ -439,6 +451,16 @@ a .Cm ^Mem.Allocator , or to nil. +.It Fn BYTES x +The bytes of the variable +.Ar x +as a +.Li []U8 : +for a +.Li []T +its +.Li LEN(s) * SIZE(T) +bytes; of a type a view may take. .It Fn MSG e The message of an .Cm ERROR . diff --git a/doc/raz.md b/doc/raz.md index e13dc20..96de7cf 100644 --- a/doc/raz.md +++ b/doc/raz.md @@ -52,7 +52,10 @@ rotated (sections 6.1 and 6.8); it breaks no program. 2026-10.36 is a twelfth: `MIN(T)` and `MAX(T)`, the limits of an integer type, and `CLZ`, `CTZ` and `POPCOUNT`, which count bits (sections 6.1 and 6.8); -and `INT(b)`, a `BOOL` as an integer (section 6.7); it breaks no program. +`INT(b)`, a `BOOL` as an integer (section 6.7); `OFFSET(T, f)` and +`SIZE(T.f)`, where a field lies and how big it is; and `BYTES(x)`, the +bytes of a variable or a slice (sections 4 and 6.8); it breaks no +program. The language is frozen, softly, from this version on: it changes only to mend a defect, and its library grows only by what breaks no program @@ -379,7 +382,7 @@ procedure may be declared `forward` and defined later (section 8). **Constants** are evaluated by the compiler. Their expressions use only -literals, other constants, operators, conversions, `SIZE`, the `LEN` +literals, other constants, operators, conversions, `SIZE`, `OFFSET`, the `LEN` of arrays and strings, `MIN`, `MAX`, `ROL`, `ROR`, `CLZ`, `CTZ`, `POPCOUNT`, and a variant's cases. A constant is an integer, a real, a `BOOL`, a name of an enumeration, a set, a string, an array, @@ -793,8 +796,8 @@ taken. On a target whose `INT` is narrower, a pointer's offset is cut to an `INT`. -`SIZE(T)`, and `LEN` of an array or a string constant, are untyped -constants, like a literal. +`SIZE(T)`, `SIZE(T.f)`, `OFFSET(T, f)`, and `LEN` of an array or a +string constant, are untyped constants, like a literal. `div` rounds toward zero and `mod` takes the sign of the dividend, as in C: `-7 div 2` is `-3`, `-7 mod 2` is `-1`. Dividing by zero, and @@ -960,9 +963,12 @@ | Call | Result | |---|---| | `SIZE(T)` | the size of type `T` in bytes, an untyped constant; `T` is a type, not a variable | +| `SIZE(T.f)` | the size of field `f` of the record type `T`, an untyped constant | +| `OFFSET(T, f)` | the byte offset of field `f` in the record type `T`, an untyped constant | | `ADR(x)` | the address of variable `x`, a `^T` | | `LEN(a)` | the number of elements of an array or a string constant (an untyped constant) or of a slice (an `INT`) | | `PTR(s)` | the address the slice `s` starts at, a `^T` for `s: []T`, unchecked; for an array `a`, that of `a[0..LEN(a)]` | +| `BYTES(x)` | the bytes of `x` as a `[]U8`: for a slice `s: []T`, its `LEN(s) * SIZE(T)` bytes; for a variable of any other type `T`, its `SIZE(T)` bytes | | `NEW(p, a)` | sets `p: ^T` to a fresh, zeroed `T` from the allocator `a: ^Mem.Allocator`, or to `nil` if `a` has no room | | `MSG(e)` | the message of the error `e`, a `[]U8` | | `COPY(d, s)` | copies the elements of `s` to the start of `d`, both arrays or slices of one type `T`; `LEN(s)` must be at most `LEN(d)` | @@ -989,6 +995,31 @@ `Mem.Allocator` (the library, `doc/raz-lib.md`) is the one type of the library the language names. +`SIZE(T.f)` and `OFFSET(T, f)` ask about a type's field, not about +storage, so `SIZE` still takes no variable. `f` is one field name: not +a path, `a.b`, and not a case of a variant, whose cases share their +place. A record handed to C is checked against C's struct with them, +as C checks one with `offsetof` and `sizeof`. + +`BYTES(x)` is the bytes of `x`, as a `[]U8` that can be read and +written: for a slice `s: []T`, the `LEN(s) * SIZE(T)` bytes it holds; +for a variable of any other type `T`, its `SIZE(T)` bytes. + +``` +n := Sys.write(fd, BYTES(samples)); (* samples: []I32 *) +n := Sys.read(fd, BYTES(h)); (* fills h: Header, a record *) +``` + +`T` must be a type a view may take (section 6.9), since writing through +the slice must not make a value mean nothing: a `BOOL`, a set, an +enumeration, a slice, a variant, a procedure type, `ERROR`, or a record +or array holding one, is an error. The slice is made, and checked, as +any slice of the variable is, so its bytes follow the rules for it: the +bytes of a local can't be returned (section 7.3), and a `const` +parameter, a view, a volatile variable and a string constant are +errors, as they are for `ADR`. The bytes are in the target's order +(appendix A). + `PTR(s)` is the first of the two words of `s` (section 5.3), and is not checked. For a slice that isn't empty it is `ADR(s[0])`; for an empty one, where `s[0]` would stop the program, it is what the slice holds: @@ -1215,7 +1246,8 @@ A slice returned must not point into the procedure's own frame, which is gone once it returns. It is an error to return, as a slice, a local array, a value parameter that is an array, an array literal other than -`[]`, or an array a call returns, or a slice of one, a field or an +`[]`, or an array a call returns, or a slice of one, or the `BYTES` of +a local variable, a field or an element of one included: ``` diff --git a/doc/report-history.md b/doc/report-history.md index 011f94a..7cdaf87 100644 --- a/doc/report-history.md +++ b/doc/report-history.md @@ -242,4 +242,8 @@ predeclared name may be hidden (section 2), so it breaks no program. A `BOOL` also converts to an integer, 0 for `false` and 1 for `true` (6.7), a constant too; there is still none from an integer to a - `BOOL`. It breaks no program. + `BOOL`. `OFFSET(T, f)` and `SIZE(T.f)` are the offset of a field in + a record type and its size, untyped constants (sections 4 and 6.8); + and `BYTES(x)` is the bytes of a variable or a slice as a `[]U8`, + of a type a view may take (sections 6.8, 6.9 and 7.3). It breaks no + program.