| | @@ -24,7 +24,9 @@ |
| 24 | 24 | and may be indented with the code around them (section 2), and |
| 25 | 25 | `match` on a string (section 7.4); it breaks no program. 2026-10.26 |
| 26 | 26 | 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. |
| 28 | 30 | |
| 29 | 31 | The language is frozen, softly, from this version on: it changes only |
| 30 | 32 | to mend a defect, and its library grows only by what breaks no program |
| | @@ -646,7 +648,7 @@ |
| 646 | 648 | Element = Expr | ident ":" Expr . |
| 647 | 649 | Designator = QualIdent { Selector } . |
| 648 | 650 | Selector = "." ident | "^" | "[" Expr [ ".." Expr ] "]" |
| 649 | | - | "(" [ Expr { "," Expr } ] ")" . |
| 651 | + | "(" [ Expr { "," Expr } ] ")" | "." "(" Type ")" . |
| 650 | 652 | ``` |
| 651 | 653 | |
| 652 | 654 | The operators bind in the order of the grammar: `not`, `~` and `try` |
| | @@ -661,7 +663,8 @@ |
| 661 | 663 | |
| 662 | 664 | A pointer to a record isn't the record: its fields are `p^.f`, never |
| 663 | 665 | `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. |
| 665 | 668 | |
| 666 | 669 | ### 6.1 Integers |
| 667 | 670 | |
| | @@ -862,7 +865,99 @@ |
| 862 | 865 | every target. They are sequentially consistent, on every target; how a |
| 863 | 866 | machine without atomic instructions makes them so is the |
| 864 | 867 | implementation's choice (appendix A). |
| 868 | +### 6.9 Views |
| 865 | 869 | |
| 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 | + |
| 866 | 961 | ## 7. Statements |
| 867 | 962 | |
| 868 | 963 | ``` |
| | @@ -1543,7 +1638,7 @@ |
| 1543 | 1638 | Element = Expr | ident ":" Expr . |
| 1544 | 1639 | Designator = QualIdent { Selector } . |
| 1545 | 1640 | Selector = "." ident | "^" | "[" Expr [ ".." Expr ] "]" |
| 1546 | | - | "(" [ Expr { "," Expr } ] ")" . |
| 1641 | + | "(" [ Expr { "," Expr } ] ")" | "." "(" Type ")" . |
| 1547 | 1642 | |
| 1548 | 1643 | Statements = { Statement ";" } . |
| 1549 | 1644 | Statement = Designator [ ":=" Expr ] |