| | @@ -52,7 +52,10 @@ |
| 52 | 52 | rotated (sections 6.1 and 6.8); it breaks no program. 2026-10.36 is a |
| 53 | 53 | twelfth: `MIN(T)` and `MAX(T)`, the limits of an integer type, and |
| 54 | 54 | `CLZ`, `CTZ` and `POPCOUNT`, which count bits (sections 6.1 and 6.8); |
| 55 | | -and `INT(b)`, a `BOOL` as an integer (section 6.7); it breaks no program. |
| 55 | +`INT(b)`, a `BOOL` as an integer (section 6.7); `OFFSET(T, f)` and |
| 56 | +`SIZE(T.f)`, where a field lies and how big it is; and `BYTES(x)`, the |
| 57 | +bytes of a variable or a slice (sections 4 and 6.8); it breaks no |
| 58 | +program. |
| 56 | 59 | |
| 57 | 60 | The language is frozen, softly, from this version on: it changes only |
| 58 | 61 | to mend a defect, and its library grows only by what breaks no program |
| | @@ -379,7 +382,7 @@ |
| 379 | 382 | procedure may be declared `forward` and defined later (section 8). |
| 380 | 383 | |
| 381 | 384 | **Constants** are evaluated by the compiler. Their expressions use only |
| 382 | | -literals, other constants, operators, conversions, `SIZE`, the `LEN` |
| 385 | +literals, other constants, operators, conversions, `SIZE`, `OFFSET`, the `LEN` |
| 383 | 386 | of arrays and strings, `MIN`, `MAX`, `ROL`, `ROR`, `CLZ`, `CTZ`, |
| 384 | 387 | `POPCOUNT`, and a variant's cases. A constant is an integer, |
| 385 | 388 | a real, a `BOOL`, a name of an enumeration, a set, a string, an array, |
| | @@ -793,8 +796,8 @@ |
| 793 | 796 | taken. On a target whose `INT` is narrower, a pointer's offset is cut |
| 794 | 797 | to an `INT`. |
| 795 | 798 | |
| 796 | | -`SIZE(T)`, and `LEN` of an array or a string constant, are untyped |
| 797 | | -constants, like a literal. |
| 799 | +`SIZE(T)`, `SIZE(T.f)`, `OFFSET(T, f)`, and `LEN` of an array or a |
| 800 | +string constant, are untyped constants, like a literal. |
| 798 | 801 | |
| 799 | 802 | `div` rounds toward zero and `mod` takes the sign of the dividend, as |
| 800 | 803 | in C: `-7 div 2` is `-3`, `-7 mod 2` is `-1`. Dividing by zero, and |
| | @@ -960,9 +963,12 @@ |
| 960 | 963 | | Call | Result | |
| 961 | 964 | |---|---| |
| 962 | 965 | | `SIZE(T)` | the size of type `T` in bytes, an untyped constant; `T` is a type, not a variable | |
| 966 | +| `SIZE(T.f)` | the size of field `f` of the record type `T`, an untyped constant | |
| 967 | +| `OFFSET(T, f)` | the byte offset of field `f` in the record type `T`, an untyped constant | |
| 963 | 968 | | `ADR(x)` | the address of variable `x`, a `^T` | |
| 964 | 969 | | `LEN(a)` | the number of elements of an array or a string constant (an untyped constant) or of a slice (an `INT`) | |
| 965 | 970 | | `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)]` | |
| 971 | +| `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 | |
| 966 | 972 | | `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 | |
| 967 | 973 | | `MSG(e)` | the message of the error `e`, a `[]U8` | |
| 968 | 974 | | `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 @@ |
| 989 | 995 | `Mem.Allocator` (the library, `doc/raz-lib.md`) is the one type of the library the |
| 990 | 996 | language names. |
| 991 | 997 | |
| 998 | +`SIZE(T.f)` and `OFFSET(T, f)` ask about a type's field, not about |
| 999 | +storage, so `SIZE` still takes no variable. `f` is one field name: not |
| 1000 | +a path, `a.b`, and not a case of a variant, whose cases share their |
| 1001 | +place. A record handed to C is checked against C's struct with them, |
| 1002 | +as C checks one with `offsetof` and `sizeof`. |
| 1003 | + |
| 1004 | +`BYTES(x)` is the bytes of `x`, as a `[]U8` that can be read and |
| 1005 | +written: for a slice `s: []T`, the `LEN(s) * SIZE(T)` bytes it holds; |
| 1006 | +for a variable of any other type `T`, its `SIZE(T)` bytes. |
| 1007 | + |
| 1008 | +``` |
| 1009 | +n := Sys.write(fd, BYTES(samples)); (* samples: []I32 *) |
| 1010 | +n := Sys.read(fd, BYTES(h)); (* fills h: Header, a record *) |
| 1011 | +``` |
| 1012 | + |
| 1013 | +`T` must be a type a view may take (section 6.9), since writing through |
| 1014 | +the slice must not make a value mean nothing: a `BOOL`, a set, an |
| 1015 | +enumeration, a slice, a variant, a procedure type, `ERROR`, or a record |
| 1016 | +or array holding one, is an error. The slice is made, and checked, as |
| 1017 | +any slice of the variable is, so its bytes follow the rules for it: the |
| 1018 | +bytes of a local can't be returned (section 7.3), and a `const` |
| 1019 | +parameter, a view, a volatile variable and a string constant are |
| 1020 | +errors, as they are for `ADR`. The bytes are in the target's order |
| 1021 | +(appendix A). |
| 1022 | + |
| 992 | 1023 | `PTR(s)` is the first of the two words of `s` (section 5.3), and is not |
| 993 | 1024 | checked. For a slice that isn't empty it is `ADR(s[0])`; for an empty |
| 994 | 1025 | one, where `s[0]` would stop the program, it is what the slice holds: |
| | @@ -1215,7 +1246,8 @@ |
| 1215 | 1246 | A slice returned must not point into the procedure's own frame, which |
| 1216 | 1247 | is gone once it returns. It is an error to return, as a slice, a local |
| 1217 | 1248 | array, a value parameter that is an array, an array literal other than |
| 1218 | | -`[]`, or an array a call returns, or a slice of one, a field or an |
| 1249 | +`[]`, or an array a call returns, or a slice of one, or the `BYTES` of |
| 1250 | +a local variable, a field or an |
| 1219 | 1251 | element of one included: |
| 1220 | 1252 | |
| 1221 | 1253 | ``` |