| 1 | title: Records and pointers |
| 2 | part: Set |
| 3 | summary: Laid out as C lays out a struct, packed records, pointers and nil. |
| 4 | --- |
| 5 | ```raz Boxes run |
| 6 | ``` |
| 7 | |
| 8 | A record is laid out as C lays out a struct. `{lo: ..., hi: ...}` builds one, naming its fields; the ones it leaves out are zero. `^Box` is a pointer to a `Box`, `SYSTEM.ADR(b)` the address of `b`, and `b^` what a pointer points at. A pointer is never the record: it is `b^.lo`, never `b.lo`, so you always see where the memory is reached through an address. |
| 9 | |
| 10 | ## Copies and literals |
| 11 | |
| 12 | Assigning a record copies it whole, as with an array. A record is compared field by field by writing the comparison, not by `=`. |
| 13 | |
| 14 | ```raz Points run |
| 15 | module Points; |
| 16 | import Out; |
| 17 | |
| 18 | type Point = record x, y: INT; end; |
| 19 | |
| 20 | proc show(p: Point); |
| 21 | begin |
| 22 | Out.char('('); Out.int(p.x); Out.str(", "); Out.int(p.y); Out.char(')'); |
| 23 | end; |
| 24 | |
| 25 | proc mirror(p: Point): Point; |
| 26 | begin |
| 27 | return {x: p.y, y: p.x}; |
| 28 | end; |
| 29 | |
| 30 | proc main*; |
| 31 | var a, b: Point; |
| 32 | begin |
| 33 | a := {x: 3, y: 4}; |
| 34 | b := a; (* a copy *) |
| 35 | b.x := 30; |
| 36 | show(a); show(b); show(mirror(a)); Out.ln; |
| 37 | a := {y: 9}; (* x is zero *) |
| 38 | show(a); Out.ln; |
| 39 | end; |
| 40 | |
| 41 | end Points. |
| 42 | ``` |
| 43 | |
| 44 | ## Tables of constants |
| 45 | |
| 46 | A constant may be a record, or an array of them: a table the program reads but can't change. Its fields are integers, reals, `BOOL`s, strings, procedures and the like, and arrays and records of these. |
| 47 | |
| 48 | ```raz Planets run |
| 49 | module Planets; |
| 50 | import Fmt; |
| 51 | |
| 52 | type Planet = record name: []const U8; moons: INT; water: BOOL; end; |
| 53 | |
| 54 | const Inner: []Planet = [ |
| 55 | {name: "Mercury"}, |
| 56 | {name: "Venus"}, |
| 57 | {name: "Earth", moons: 1, water: true}, |
| 58 | {name: "Mars", moons: 2}]; |
| 59 | |
| 60 | proc main*; |
| 61 | var i, total: INT; |
| 62 | begin |
| 63 | for i := 0 to LEN(Inner) - 1 do |
| 64 | total := total + Inner[i].moons; |
| 65 | if Inner[i].moons = 0 then Fmt.print("{} has no moon\n", [Fmt.str(Inner[i].name)]); end; |
| 66 | end; |
| 67 | Fmt.print("{} moons in all\n", [Fmt.int(total)]); |
| 68 | end; |
| 69 | |
| 70 | end Planets. |
| 71 | ``` |
| 72 | |
| 73 | Fields left out are zero, as in any literal: Mercury's `moons` is 0 and its `water` is `false`. A constant typed `[]Planet` takes its length from its literal, here 4, so a planet added to the list needs no count changed. |
| 74 | |
| 75 | ## Layout |
| 76 | |
| 77 | A record's layout is C's: each field at the next multiple of its own alignment, and the whole padded to a multiple of its largest alignment. `SIZE(T)` says how many bytes. A `packed record` has no padding, which is what a file format or a wire protocol wants. |
| 78 | |
| 79 | ```raz Layout run |
| 80 | module Layout; |
| 81 | import Out; |
| 82 | |
| 83 | type |
| 84 | Loose = record a: U8; b: U32; c: U8; end; |
| 85 | Tight = packed record a: U8; b: U32; c: U8; end; |
| 86 | |
| 87 | proc main*; |
| 88 | begin |
| 89 | Out.str("loose: "); Out.int(SIZE(Loose)); Out.ln; (* 12 *) |
| 90 | Out.str("tight: "); Out.int(SIZE(Tight)); Out.ln; (* 6 *) |
| 91 | Out.str("INT: "); Out.int(SIZE(INT)); Out.ln; |
| 92 | Out.str("[5]I16: "); Out.int(SIZE([5]I16)); Out.ln; |
| 93 | end; |
| 94 | |
| 95 | end Layout. |
| 96 | ``` |
| 97 | |
| 98 | A field of a packed record may sit at any address, and is read and written like any other; but it has no address of its own to take with `SYSTEM.ADR`. |
| 99 | |
| 100 | ## Bytes as numbers and records: views |
| 101 | |
| 102 | A file, a socket or a device hands a program bytes. A view reads and writes them as another type: `msg[3].(U32)` is the `U32` whose four bytes start at `msg[3]`, and `msg[0].(Head)` the `Head` whose bytes start at `msg[0]`. |
| 103 | |
| 104 | ```raz Wire run |
| 105 | module Wire; |
| 106 | import Out; |
| 107 | |
| 108 | type Head = packed record magic: U16; kind: U8; len: U32; end; (* 7 bytes, as sent *) |
| 109 | |
| 110 | var msg: [32]U8; |
| 111 | |
| 112 | proc main*; |
| 113 | var h: Head; i: INT; sum: I64; |
| 114 | begin |
| 115 | (* what a socket might have handed us *) |
| 116 | msg[0] := 52H; msg[1] := 5AH; |
| 117 | msg[2] := 1; |
| 118 | msg[3].(U32) := 8; |
| 119 | msg[7].(I32) := 100; |
| 120 | msg[11].(I32) := 200; |
| 121 | |
| 122 | h := msg[0].(Head); |
| 123 | Out.hex(U64(h.magic), 4); Out.char(' '); |
| 124 | Out.int(I64(h.kind)); Out.char(' '); |
| 125 | Out.int(I64(h.len)); Out.ln; |
| 126 | sum := 0; |
| 127 | i := 7; |
| 128 | while i < 7 + INT(h.len) do |
| 129 | sum := sum + I64(msg[i].(I32)); |
| 130 | i := i + 4; |
| 131 | end; |
| 132 | Out.int(sum); Out.ln; |
| 133 | end; |
| 134 | |
| 135 | end Wire. |
| 136 | ``` |
| 137 | |
| 138 | A view isn't a copy: it *is* those bytes, so `msg[3].(U32) := 8` writes `msg[3]` to `msg[6]`, and a view goes wherever a variable does, with a field after it, in `INC`, on the left of `:=`. Nor is it a conversion: `I64(x)` is a real's number, `x.(U64)` its bits. The magic number reads `5A52`, its bytes backwards, because x86-64 keeps a number's low byte first; a format with an order of its own is read with `Str.le` or `Str.be`. |
| 139 | |
| 140 | A view of an element must end inside the array, and the program checks it as it checks an index: |
| 141 | |
| 142 | ```raz Short fail |
| 143 | module Short; |
| 144 | import Out; |
| 145 | |
| 146 | var msg: [10]U8; |
| 147 | |
| 148 | proc main*; |
| 149 | var i: INT; |
| 150 | begin |
| 151 | i := 0; |
| 152 | while i < LEN(msg) do |
| 153 | Out.int(I64(msg[i].(U32))); Out.char(' '); |
| 154 | i := i + 4; |
| 155 | end; |
| 156 | end; |
| 157 | |
| 158 | end Short. |
| 159 | ``` |
| 160 | |
| 161 | The type of a view is an integer, a real or a pointer, or a record or array made of them: types any bytes are a value of, which a `BOOL` or an enumeration isn't. A view may start at any byte, so, as a packed record's field, it has no address of its own. |
| 162 | |
| 163 | ## Pointers |
| 164 | |
| 165 | `SYSTEM.ADR(x)` (a module that uses it says `import SYSTEM;`) takes the address of a variable, `^T` is a pointer to a `T`, and `p^` is what it points at. A pointer that points nowhere is `nil`, and following it stops the program with a message that names the place, so a forgotten check is found at once instead of corrupting memory. |
| 166 | |
| 167 | ```raz Pointers run |
| 168 | module Pointers; |
| 169 | import Out; |
| 170 | import SYSTEM; |
| 171 | |
| 172 | type Counter = record hits: INT; name: []const U8; end; |
| 173 | |
| 174 | proc hit(c: ^Counter); |
| 175 | begin |
| 176 | if c = nil then return; end; |
| 177 | INC(c^.hits); |
| 178 | end; |
| 179 | |
| 180 | proc swap(a, b: ^INT); |
| 181 | var t: INT; |
| 182 | begin |
| 183 | t := a^; |
| 184 | a^ := b^; |
| 185 | b^ := t; |
| 186 | end; |
| 187 | |
| 188 | proc main*; |
| 189 | var c: Counter; x, y: INT; p: ^INT; |
| 190 | begin |
| 191 | c.name := "front door"; |
| 192 | hit(SYSTEM.ADR(c)); hit(SYSTEM.ADR(c)); hit(nil); |
| 193 | Out.str(c.name); Out.str(": "); Out.int(c.hits); Out.ln; |
| 194 | x := 1; y := 2; |
| 195 | swap(SYSTEM.ADR(x), SYSTEM.ADR(y)); |
| 196 | Out.int(x); Out.char(' '); Out.int(y); Out.ln; |
| 197 | p := SYSTEM.ADR(x); |
| 198 | p^ := p^ + 40; |
| 199 | Out.int(x); Out.ln; |
| 200 | end; |
| 201 | |
| 202 | end Pointers. |
| 203 | ``` |
| 204 | |
| 205 | ## Pointers that link |
| 206 | |
| 207 | A record may hold a pointer to its own type, which is how lists and trees are made. Memory for them comes from an allocator, which is the next lesson; here the nodes simply live in an array. |
| 208 | |
| 209 | ```raz Chain run |
| 210 | module Chain; |
| 211 | import Out; |
| 212 | import SYSTEM; |
| 213 | |
| 214 | type Node = record next: ^Node; val: INT; end; |
| 215 | |
| 216 | proc main*; |
| 217 | var nodes: [4]Node; head, n: ^Node; i: INT; |
| 218 | begin |
| 219 | head := nil; |
| 220 | for i := 0 to 3 do |
| 221 | nodes[i].val := (i + 1) * 10; |
| 222 | nodes[i].next := head; (* each new one in front *) |
| 223 | head := SYSTEM.ADR(nodes[i]); |
| 224 | end; |
| 225 | n := head; |
| 226 | while n # nil do |
| 227 | Out.int(n^.val); Out.char(' '); |
| 228 | n := n^.next; |
| 229 | end; |
| 230 | Out.ln; |
| 231 | end; |
| 232 | |
| 233 | end Chain. |
| 234 | ``` |
| 235 | |
| 236 | ## A slice's pointer |
| 237 | |
| 238 | A slice is a pointer and a length, and a record can keep the two apart: an interpreter keeps a string so, and C takes one so. `LEN(s)` is the length. The pointer looks like `SYSTEM.ADR(s[0])`, the address of the first element, and that works until the slice is empty: then there is no first element, `s[0]` is out of range, and the program stops. |
| 239 | |
| 240 | ```raz Empty fail |
| 241 | module Empty; |
| 242 | import Out; |
| 243 | import SYSTEM; |
| 244 | |
| 245 | type Span = record p: ^U8; n: INT; end; |
| 246 | |
| 247 | proc spanOf(s: []const U8): Span; |
| 248 | begin |
| 249 | return {p: SYSTEM.ADR(s[0]), n: LEN(s)}; |
| 250 | end; |
| 251 | |
| 252 | proc main*; |
| 253 | var a, b: Span; |
| 254 | begin |
| 255 | a := spanOf("one"); |
| 256 | Out.int(a.n); Out.ln; |
| 257 | b := spanOf(""); |
| 258 | Out.int(b.n); Out.ln; |
| 259 | end; |
| 260 | |
| 261 | end Empty. |
| 262 | ``` |
| 263 | |
| 264 | `SYSTEM.PTR(s)` is the pointer itself, a `^U8` for a `[]U8`, with no check. For a slice that isn't empty it is `SYSTEM.ADR(s[0])`; for an empty one it is what the slice holds, `nil` for one never set. So nothing needs testing first, and `sp.p[0..sp.n]` makes the slice again, empty when `sp.n` is 0, whatever `sp.p` is: |
| 265 | |
| 266 | ```raz Spans run |
| 267 | ``` |
| 268 | |
| 269 | Of an array, `SYSTEM.PTR(a)` is the address of its first element, a `^T` where `SYSTEM.ADR(a)` is a `^[n]T`. Reading through the pointer is as unchecked as through any pointer: the length is yours to keep with it. The compiler says so when the two are mixed up: a slice given where a pointer is wanted is an error whose help says to write `PTR(s)` and `LEN(s)`, and a pointer given where a slice is, `p[0..n]`. |