| | @@ -1558,3 +1558,35 @@ |
| 1558 | 1558 | taken among the built-ins, which a program can't declare anyway. |
| 1559 | 1559 | |
| 1560 | 1560 | Met in mosaico, rfuzz and raudio. |
| 1561 | + |
| 1562 | +## `OFFSET(T, f)`, and `SIZE` of a field |
| 1563 | + |
| 1564 | +A record handed to C by pointer must match C's struct byte for byte; |
| 1565 | +a field one word off is read wrong with no error. mosaico and rx11 test |
| 1566 | +their Xlib records against C's `offsetof` and `sizeof`, and today |
| 1567 | +measure an offset on a dummy variable, `INT(ADR(v.f)) - INT(ADR(v))`: |
| 1568 | +a run-time value, a variable declared for it, and pointer arithmetic |
| 1569 | +where the test means "where `f` is". rx11's `test/layout.sh` copies |
| 1570 | +each field's type out of the source text, since `SIZE` takes only a |
| 1571 | +type. |
| 1572 | + |
| 1573 | +The proposal, two additions to the built-in procedures: |
| 1574 | + |
| 1575 | +| Call | Result | |
| 1576 | +|---|---| |
| 1577 | +| `OFFSET(T, f)` | the byte offset of field `f` in the record type `T`, an untyped constant | |
| 1578 | +| `SIZE(T.f)` | the size of field `f` of the record type `T`, an untyped constant | |
| 1579 | + |
| 1580 | +`f` is one field name; a path, `OFFSET(T, a.b)`, would be the sum of |
| 1581 | +two and is left out, and so is a variant, whose cases share their |
| 1582 | +place. `SIZE` still takes no |
| 1583 | +variable: `T.f` names a type's field, not storage, so `SIZE` stays a |
| 1584 | +question about types. |
| 1585 | + |
| 1586 | +C has `offsetof`; Modula-2 and Oberon kept such things in `SYSTEM`, |
| 1587 | +and raz has no `SYSTEM`. It is constant folding of what the compiler |
| 1588 | +already knows, and it is also what a view through raw memory wants, |
| 1589 | +`mem[a + OFFSET(Hdr, len)].(U32)`, in place of a number, and what a |
| 1590 | +kernel's `code` procedures would want if their operands take constants. |
| 1591 | + |
| 1592 | +Met in mosaico and rx11. |