Views, v.(T): the bytes of a variable read and written as another type
| @@ -0,0 +1,7 @@ | ||
| 1 | +module ViewAdr; | |
| 2 | +var mem: [16]U8; p: ^I64; | |
| 3 | +proc main*; | |
| 4 | +begin | |
| 5 | + p := ADR(mem[0].(I64)); | |
| 6 | +end; | |
| 7 | +end ViewAdr. |
| @@ -0,0 +1,7 @@ | ||
| 1 | +module ViewArrayTooSmall; | |
| 2 | +var mem: [4]U8; i: INT; n: I64; | |
| 3 | +proc main*; | |
| 4 | +begin | |
| 5 | + n := mem[i].(I64); | |
| 6 | +end; | |
| 7 | +end ViewArrayTooSmall. |
| @@ -0,0 +1,7 @@ | ||
| 1 | +module ViewBool; | |
| 2 | +var mem: [8]U8; b: BOOL; | |
| 3 | +proc main*; | |
| 4 | +begin | |
| 5 | + b := mem[0].(BOOL); | |
| 6 | +end; | |
| 7 | +end ViewBool. |
| @@ -0,0 +1,8 @@ | ||
| 1 | +module ViewCall; | |
| 2 | +proc f: F64; begin return 1.0; end; | |
| 3 | +var u: U64; | |
| 4 | +proc main*; | |
| 5 | +begin | |
| 6 | + u := f().(U64); | |
| 7 | +end; | |
| 8 | +end ViewCall. |
| @@ -0,0 +1,8 @@ | ||
| 1 | +module ViewConst; | |
| 2 | +const Table: [4]U8 = [1, 2, 3, 4]; | |
| 3 | +var n: U32; | |
| 4 | +proc main*; | |
| 5 | +begin | |
| 6 | + n := Table[0].(U32); | |
| 7 | +end; | |
| 8 | +end ViewConst. |
| @@ -0,0 +1,8 @@ | ||
| 1 | +module ViewEnum; | |
| 2 | +type Colour = (red, green); | |
| 3 | +var mem: [8]U8; c: Colour; | |
| 4 | +proc main*; | |
| 5 | +begin | |
| 6 | + c := mem[0].(Colour); | |
| 7 | +end; | |
| 8 | +end ViewEnum. |
| @@ -0,0 +1,7 @@ | ||
| 1 | +module ViewPastEnd; | |
| 2 | +var mem: [16]U8; n: I64; | |
| 3 | +proc main*; | |
| 4 | +begin | |
| 5 | + n := mem[10].(I64); | |
| 6 | +end; | |
| 7 | +end ViewPastEnd. |
| @@ -0,0 +1,7 @@ | ||
| 1 | +module ViewReadOnly; | |
| 2 | +import ViewMod; | |
| 3 | +proc main*; | |
| 4 | +begin | |
| 5 | + ViewMod.mem[0].(I64) := 1; | |
| 6 | +end; | |
| 7 | +end ViewReadOnly. |
| @@ -0,0 +1,8 @@ | ||
| 1 | +module ViewRecordBool; | |
| 2 | +type Flags = record on: BOOL; n: I32; end; | |
| 3 | +var mem: [32]U8; f: Flags; | |
| 4 | +proc main*; | |
| 5 | +begin | |
| 6 | + f := mem[0].(Flags); | |
| 7 | +end; | |
| 8 | +end ViewRecordBool. |
| @@ -0,0 +1,7 @@ | ||
| 1 | +module ViewSlice; | |
| 2 | +var mem: [32]U8; s: []U8; | |
| 3 | +proc main*; | |
| 4 | +begin | |
| 5 | + s := mem[0].([]U8); | |
| 6 | +end; | |
| 7 | +end ViewSlice. |
| @@ -0,0 +1,7 @@ | ||
| 1 | +module ViewSliceOf; | |
| 2 | +var mem: [16]U8; s: []U8; | |
| 3 | +proc main*; | |
| 4 | +begin | |
| 5 | + s := mem[0].([8]U8)[0..4]; | |
| 6 | +end; | |
| 7 | +end ViewSliceOf. |
| @@ -0,0 +1,7 @@ | ||
| 1 | +module ViewTooBig; | |
| 2 | +var n: I32; u: U64; | |
| 3 | +proc main*; | |
| 4 | +begin | |
| 5 | + u := n.(U64); | |
| 6 | +end; | |
| 7 | +end ViewTooBig. |
| @@ -0,0 +1,8 @@ | ||
| 1 | +module ViewVarParam; | |
| 2 | +var mem: [16]U8; | |
| 3 | +proc put(var x: I64); begin x := 1; end; | |
| 4 | +proc main*; | |
| 5 | +begin | |
| 6 | + put(mem[3].(I64)); | |
| 7 | +end; | |
| 8 | +end ViewVarParam. |
| @@ -0,0 +1,7 @@ | ||
| 1 | +module ViewVolatile; | |
| 2 | +var p: ^VOLATILE [16]U8; n: U32; | |
| 3 | +proc main*; | |
| 4 | +begin | |
| 5 | + n := p^[4].(U32); | |
| 6 | +end; | |
| 7 | +end ViewVolatile. |
| @@ -0,0 +1,3 @@ | ||
| 1 | +module ViewMod; | |
| 2 | +var mem*: [16]U8; | |
| 3 | +end ViewMod. |
| @@ -0,0 +1 @@ | ||
| 1 | +0 |
| @@ -0,0 +1,18 @@ | ||
| 1 | +module ViewNegative; | |
| 2 | +(* A negative index to a view is past the end too *) | |
| 3 | +import Out; | |
| 4 | + | |
| 5 | +var mem: [16]U8; | |
| 6 | + | |
| 7 | +proc at(i: INT): I64; | |
| 8 | +begin | |
| 9 | + return mem[i].(I64); | |
| 10 | +end; | |
| 11 | + | |
| 12 | +proc main*; | |
| 13 | +begin | |
| 14 | + Out.int(at(0)); Out.ln; | |
| 15 | + Out.int(at(-1)); Out.ln; | |
| 16 | +end; | |
| 17 | + | |
| 18 | +end ViewNegative. |
| @@ -0,0 +1 @@ | ||
| 1 | +2 |
| @@ -0,0 +1 @@ | ||
| 1 | +0123456789 | |
| \ No newline at end of file |
| @@ -0,0 +1,16 @@ | ||
| 1 | +module ViewPast; | |
| 2 | +(* A view of an array's element that runs past the array's end *) | |
| 3 | +import Out; | |
| 4 | + | |
| 5 | +var mem: [16]U8; | |
| 6 | + | |
| 7 | +proc main*; | |
| 8 | +var i: INT; | |
| 9 | +begin | |
| 10 | + for i := 0 to 15 do | |
| 11 | + Out.int(i); | |
| 12 | + mem[i].(I64) := 1; | |
| 13 | + end; | |
| 14 | +end; | |
| 15 | + | |
| 16 | +end ViewPast. |
| @@ -0,0 +1 @@ | ||
| 1 | +2 |
| @@ -0,0 +1 @@ | ||
| 1 | +01234567 | |
| \ No newline at end of file |
| @@ -0,0 +1,15 @@ | ||
| 1 | +module ViewPastSlice; | |
| 2 | +(* A view of a slice's element that runs past the slice's end *) | |
| 3 | +import Out; | |
| 4 | + | |
| 5 | +proc main*; | |
| 6 | +var a: [16]U8; s: []U8; i: INT; | |
| 7 | +begin | |
| 8 | + s := a[2..12]; | |
| 9 | + for i := 0 to 9 do | |
| 10 | + Out.int(i); | |
| 11 | + s[i].(U32) := 1; | |
| 12 | + end; | |
| 13 | +end; | |
| 14 | + | |
| 15 | +end ViewPastSlice. |
| @@ -0,0 +1 @@ | ||
| 1 | +2 |
| @@ -0,0 +1,5 @@ | ||
| 1 | +1.5's bits 3FF8000000000000 | |
| 2 | +x from bits, times 2: 5 | |
| 3 | +F32 1.0's bits 3F800000 | |
| 4 | +mem[3].(F64) times 8: 2 | |
| 5 | +its bits 3FD0000000000000 |
| @@ -0,0 +1,26 @@ | ||
| 1 | +(* Views of reals: their bits, read and written (report section 6.9) *) | |
| 2 | +module ViewReals; | |
| 3 | +import Out; | |
| 4 | + | |
| 5 | +var mem: [16]U8; | |
| 6 | + | |
| 7 | +proc main*; | |
| 8 | +var x: F64; f: F32; u: U64; k: U32; | |
| 9 | +begin | |
| 10 | + (* locals kept in registers otherwise *) | |
| 11 | + x := 1.5; | |
| 12 | + u := x.(U64); | |
| 13 | + Out.str("1.5's bits "); Out.hex(u, 16); Out.ln; | |
| 14 | + u := 4004000000000000H; | |
| 15 | + x.(U64) := u; | |
| 16 | + Out.str("x from bits, times 2: "); Out.int(I64(x * 2.0)); Out.ln; | |
| 17 | + f := 1.0; | |
| 18 | + k := f.(U32); | |
| 19 | + Out.str("F32 1.0's bits "); Out.hex(U64(k), 8); Out.ln; | |
| 20 | + (* a real in an array of bytes *) | |
| 21 | + mem[3].(F64) := 0.25; | |
| 22 | + Out.str("mem[3].(F64) times 8: "); Out.int(I64(mem[3].(F64) * 8.0)); Out.ln; | |
| 23 | + Out.str("its bits "); Out.hex(mem[3].(U64), 16); Out.ln; | |
| 24 | +end; | |
| 25 | + | |
| 26 | +end ViewReals. |
An empty file.
| @@ -0,0 +1,16 @@ | ||
| 1 | +mem[16].(I64) -1 | |
| 2 | +mem[23] 255 | |
| 3 | +mem[3].(I64), misaligned 1008 | |
| 4 | +the last 8 bytes 7 | |
| 5 | +258's first byte 2 | |
| 6 | +U8(258) 2 | |
| 7 | +h.count 2 | |
| 8 | +b[4] 9 | |
| 9 | +mem[36].(I32) -5 | |
| 10 | +Pair's value 300 | |
| 11 | +n through a pointer from bytes 77 | |
| 12 | +b[8].(I64) 123456789 | |
| 13 | +sum 59 | |
| 14 | +regs[1] 5 | |
| 15 | +regs[3] 6 | |
| 16 | +ViewData.bytes[0].(I64) 42 |
| @@ -0,0 +1,101 @@ | ||
| 1 | +(* Views, v.(T): the bytes of a variable read and written as another | |
| 2 | + type (report section 6.9). The output holds on a target that puts a | |
| 3 | + word's low byte first, as appendix A lets a target choose; the | |
| 4 | + bytes are written here a byte at a time where order matters. *) | |
| 5 | +module Views; | |
| 6 | +import Out, ViewData; | |
| 7 | + | |
| 8 | +type | |
| 9 | + Header = record magic: U32; count: U32; end; | |
| 10 | + Pair = packed record tag: U8; value: I32; end; | |
| 11 | + | |
| 12 | +var | |
| 13 | + mem: [64]U8; (* a module's array *) | |
| 14 | + regs: [4]U64; | |
| 15 | + | |
| 16 | +proc show(label: []U8; v: I64); | |
| 17 | +begin | |
| 18 | + Out.str(label); Out.str(" "); Out.int(v); Out.ln; | |
| 19 | +end; | |
| 20 | + | |
| 21 | +(* a var parameter, an array reached through a pointer, viewed *) | |
| 22 | +proc fill(var b: [16]U8; v: I64); | |
| 23 | +begin | |
| 24 | + b[8].(I64) := v; | |
| 25 | +end; | |
| 26 | + | |
| 27 | +(* a slice parameter's element, viewed *) | |
| 28 | +proc sum(s: []U8): I64; | |
| 29 | +var i, t: INT; | |
| 30 | +begin | |
| 31 | + t := 0; | |
| 32 | + i := 0; | |
| 33 | + while i + 4 <= LEN(s) do | |
| 34 | + t := t + INT(s[i].(I32)); | |
| 35 | + i := i + 4; | |
| 36 | + end; | |
| 37 | + return t; | |
| 38 | +end; | |
| 39 | + | |
| 40 | +proc main*; | |
| 41 | +var | |
| 42 | + n, a: I64; b: [16]U8; s: []U8; | |
| 43 | + p: ^I64; h: Header; | |
| 44 | +begin | |
| 45 | + (* an I64 in an array of bytes, at an index known and one not *) | |
| 46 | + mem[16].(I64) := -1; | |
| 47 | + show("mem[16].(I64)", mem[16].(I64)); | |
| 48 | + show("mem[23]", I64(mem[23])); | |
| 49 | + a := 3; | |
| 50 | + mem[a].(I64) := 1000; | |
| 51 | + INC(mem[a].(I64), 8); | |
| 52 | + show("mem[3].(I64), misaligned", mem[a].(I64)); | |
| 53 | + mem[56].(I64) := 7; | |
| 54 | + show("the last 8 bytes", mem[56].(I64)); | |
| 55 | + | |
| 56 | + (* a view of a whole variable, a local kept in a register | |
| 57 | + otherwise: the first byte of a number, its low byte, low byte | |
| 58 | + first *) | |
| 59 | + n := 258; | |
| 60 | + show("258's first byte", I64(n.(U8))); | |
| 61 | + show("U8(258)", I64(U8(n))); | |
| 62 | + | |
| 63 | + (* a record over bytes, and a field of it, read and written *) | |
| 64 | + b[0] := 1; b[4] := 2; | |
| 65 | + h := b[0].(Header); | |
| 66 | + show("h.count", I64(h.count)); | |
| 67 | + b[0].(Header).count := 9; | |
| 68 | + show("b[4]", I64(b[4])); | |
| 69 | + | |
| 70 | + (* an array as the type, indexed past the view *) | |
| 71 | + mem[32].([2]I32)[1] := -5; | |
| 72 | + show("mem[36].(I32)", I64(mem[36].(I32))); | |
| 73 | + | |
| 74 | + (* a packed record, and a pointer read from bytes, then followed *) | |
| 75 | + mem[40].(Pair) := {tag: 1, value: 300}; | |
| 76 | + show("Pair's value", I64(mem[41].(I32))); | |
| 77 | + p := ADR(n); | |
| 78 | + mem[48].(^I64) := p; | |
| 79 | + mem[48].(^I64)^ := 77; | |
| 80 | + show("n through a pointer from bytes", n); | |
| 81 | + | |
| 82 | + (* a var parameter's array, a slice parameter, a slice's element *) | |
| 83 | + fill(b, 123456789); | |
| 84 | + show("b[8].(I64)", b[8].(I64)); | |
| 85 | + s := mem[0..16]; | |
| 86 | + s[0].(I32) := 10; s[4].(I32) := 20; s[8].(I32) := 30; s[12].(I32) := -1; | |
| 87 | + show("sum", sum(s)); | |
| 88 | + | |
| 89 | + (* a module's array of U64s viewed as bytes, and an element of | |
| 90 | + bigger elements viewed from its start *) | |
| 91 | + regs[1] := 0; | |
| 92 | + regs[0].([32]U8)[8] := 5; | |
| 93 | + show("regs[1]", I64(regs[1])); | |
| 94 | + regs[2].([2]U64)[1] := 6; | |
| 95 | + show("regs[3]", I64(regs[3])); | |
| 96 | + | |
| 97 | + (* another module's variable, read through a view *) | |
| 98 | + show("ViewData.bytes[0].(I64)", ViewData.bytes[0].(I64)); | |
| 99 | +end; | |
| 100 | + | |
| 101 | +end Views. |
| @@ -0,0 +1,8 @@ | ||
| 1 | +(* An exported variable, which an importer may read through a view *) | |
| 2 | +module ViewData; | |
| 3 | + | |
| 4 | +var bytes*: [8]U8; | |
| 5 | + | |
| 6 | +begin | |
| 7 | + bytes[0] := 42; | |
| 8 | +end ViewData. |
| @@ -18,9 +18,9 @@ | ||
| 18 | 18 | | `src/Sym.raz` | 687 | 556 | Types, objects, scopes, layout | |
| 19 | 19 | | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes | |
| 20 | 20 | | `src/X64.raz` | 1,517 | 1,253 | The instruction encoder, jumps within a procedure, the assembler | |
| 21 | -| `src/Gen.raz` | 2,516 | 1,800 | The stack machine, for x86-64: values, operations, checks, calls, frames | | |
| 22 | -| `src/Item.raz` | 1,792 | 1,478 | Items: types, constants, variables, operators, conversions, calls | | |
| 23 | -| `src/Parse.raz` | 3,960 | 3,445 | Declarations, statements, expressions, built-ins, recovery, warnings | | |
| 21 | +| `src/Gen.raz` | 2,552 | 1,821 | The stack machine, for x86-64: values, operations, checks, calls, frames | | |
| 22 | +| `src/Item.raz` | 1,948 | 1,594 | Items: types, constants, variables, operators, conversions, calls | | |
| 23 | +| `src/Parse.raz` | 4,000 | 3,478 | Declarations, statements, expressions, built-ins, recovery, warnings | | |
| 24 | 24 | | `src/Link.raz` | 381 | 298 | Tree shaking, layout, patching, the places | |
| 25 | 25 | | `src/Elf.raz` | 431 | 344 | Static and dynamic ELF, symbol table, line numbers | |
| 26 | 26 | | `src/Raz.raz` | 305 | 238 | The command line | |
| @@ -1068,7 +1068,7 @@ | ||
| 1068 | 1068 | - jumps: `jump`, `jumpIf` (on a `BOOL`), `jumpCmp` (on two integers), |
| 1069 | 1069 | `jumpOut` (`jumpCmp` from within an expression) and `condValue` for |
| 1070 | 1070 | `and` and `or`, `fix` where the label is, and `joinJumps`; |
| 1071 | -- checks: `checkIndex`, `checkEnum`, and `indexSlice`, `slice` and | |
| 1071 | +- checks: `checkIndex`, `checkView`, `checkEnum`, and `indexSlice`, `slice` and | |
| 1072 | 1072 | `slicePtr`, which check as they make their address or slice; and |
| 1073 | 1073 | `indexArray`, an element's address in an array in the frame or a |
| 1074 | 1074 | chunk, the index checked before; |
| @@ -1282,6 +1282,37 @@ | ||
| 1282 | 1282 | only the item's type changes. `assign` stores, or |
| 1283 | 1283 | copies records and arrays. `convert` is the written conversion, `T(x)`. |
| 1284 | 1284 | |
| 1285 | +Views, `v.(T)` (report section 6.9): a view is the item of `v` with | |
| 1286 | +`T` for its type. It is at the same place, so nothing is copied or | |
| 1287 | +computed to make it: `mem[16].(I64) := n` compiles to the store | |
| 1288 | +`mem[16] := n` would be, but of 8 bytes. What `view` and `indexView` | |
| 1289 | +add is the checks: | |
| 1290 | + | |
| 1291 | +- `viewable`: `v` is a variable (not a constant or a temporary; another | |
| 1292 | + module's is read only, by its `ro`), not volatile, and `T` is plain | |
| 1293 | + data (`notPlain`: integers, reals, pointers, and records and arrays | |
| 1294 | + of them alone); | |
| 1295 | +- the room: `view` wants `T` to fit in `v`, which the compiler always | |
| 1296 | + knows; `indexView`, for `a[i].(T)`, wants `T` from `a[i]` to `a`'s | |
| 1297 | + end. `T` takes `k` elements (`elementsFor`), so in an array of `n` | |
| 1298 | + the index must be below `n - k + 1`: one unsigned compare | |
| 1299 | + (`Gen.checkView`) in place of `index`'s `i < n`, which a negative | |
| 1300 | + `i`, huge unsigned, fails too. A slice's length is known only as the | |
| 1301 | + program runs: `Gen.indexSlice` checks `i < len` as ever, then, with | |
| 1302 | + a view's `room` of `k - 1`, that `len - i`, which can't wrap once `i | |
| 1303 | + < len`, is more than `room` (`checkRoom`). A failed one is kind 7 of | |
| 1304 | + `Rt.fail`: "a view at index 9 runs past a length of 16". | |
| 1305 | + | |
| 1306 | +The item is marked `view`, which keeps `ADR`, a `var` argument and a | |
| 1307 | +slice from it (`noAddress`), as `odd` does for a field of a packed | |
| 1308 | +record: a view may be misaligned. A variable kept in a register has no | |
| 1309 | +bytes to view, so `becomeView` demotes it to memory as `address` does | |
| 1310 | +(`demote`), and the procedure is compiled again. The parser sees a view | |
| 1311 | +of an element only after the `]`: `designator` reads the index, then | |
| 1312 | +looks past the `]` for `.(` (`viewNext`, by `Scan.saveScan` and | |
| 1313 | +`restoreScan`), so that `indexView` has the index and the array | |
| 1314 | +together. | |
| 1315 | + | |
| 1285 | 1316 | Calls: `beginCall`, then `passArg` for each argument, then `endCall`, |
| 1286 | 1317 | which has Gen's `call` make the call. Each argument is left on Gen's |
| 1287 | 1318 | stack, a slice as its two words (`pushWords`). `return f(x);` passing on |
| @@ -1300,7 +1331,8 @@ | ||
| 1300 | 1331 | |
| 1301 | 1332 | Start with expressions, bottom up: `factor`, `term`, `simpleExpr`, |
| 1302 | 1333 | `relation`, `conjunct` (the `and`s), `expression` (the `or`s). Then `designator`, which reads a name and what follows |
| 1303 | -it: `.field`, `^`, `[i]`, `[lo..hi]`, a call, a conversion. Each step | |
| 1334 | +it: `.field`, `^`, `[i]`, `[lo..hi]`, a call, a conversion, a view | |
| 1335 | +`.(T)`. Each step | |
| 1304 | 1336 | calls the matching routine in `Item.raz` on the item it has. The |
| 1305 | 1337 | `want` parameter carries the type an expression must have, where it is |
| 1306 | 1338 | known, so that `[1, 2, 3]`, `{x: 1}` and `{red}` know what to build |
| @@ -537,7 +537,8 @@ | ||
| 537 | 537 | status 2. what is 0 for an index a and 1 for a slice a..b, of |
| 538 | 538 | something whose length is len; 2 for a, which is no position of |
| 539 | 539 | the enumeration called name; 3 for a slice a..b of a pointer, which |
| 540 | - runs backwards. What a target's machine doesn't catch the code | |
| 540 | + runs backwards; 7 for a view at index a that runs past the end of | |
| 541 | + something whose length is len. What a target's machine doesn't catch the code | |
| 541 | 542 | tests for, and stops at with what 4, a nil pointer; 5, a division |
| 542 | 543 | by zero; 6, a stack overflow; these end the program by exit with |
| 543 | 544 | status 2 too, as the signal handler does. rdi and rsi are the |
| @@ -547,7 +548,7 @@ | ||
| 547 | 548 | regs[5] := rdi; |
| 548 | 549 | regs[6] := rsi; |
| 549 | 550 | where(at); |
| 550 | - if what >= 4 then | |
| 551 | + if (what >= 4) and (what <= 6) then | |
| 551 | 552 | put("stopped by "); |
| 552 | 553 | if what = 4 then put("a nil pointer"); |
| 553 | 554 | elsif what = 5 then put("a division fault"); |
| @@ -560,6 +561,11 @@ | ||
| 560 | 561 | if what = 0 then |
| 561 | 562 | put("index "); |
| 562 | 563 | putInt(a); |
| 564 | + elsif what = 7 then | |
| 565 | + put("a view at index "); | |
| 566 | + putInt(a); | |
| 567 | + put(" runs past a length of "); | |
| 568 | + putInt(len); | |
| 563 | 569 | elsif (what = 1) or (what = 3) then |
| 564 | 570 | put("slice "); |
| 565 | 571 | putInt(a); |
| @@ -134,7 +134,7 @@ | ||
| 134 | 134 | rtFail*, argcChunk*, argvChunk*: INT; |
| 135 | 135 | (* the stubs that check's code calls, one for each kind of failure, |
| 136 | 136 | made by entryPoint for those used *) |
| 137 | - failStubs: [6]INT; | |
| 137 | + failStubs: [8]INT; | |
| 138 | 138 | lost*: BOOL; (* a parameter kept where it arrived was wanted |
| 139 | 139 | for other work: the procedure is compiled again, |
| 140 | 140 | the parameter in memory *) |
| @@ -191,7 +191,7 @@ | ||
| 191 | 191 | end; |
| 192 | 192 | |
| 193 | 193 | const |
| 194 | - StubNames: [6][]U8 = ["check0", "check1", "check2", "check3", "check4", "check5"]; | |
| 194 | + StubNames: [8][]U8 = ["check0", "check1", "check2", "check3", "check4", "check5", "check6", "check7"]; | |
| 195 | 195 | (* the registers of the stack: the value at depth d is in Regs[d mod |
| 196 | 196 | NumRegs]. rax, rdx and rcx, which division and shifts need, and rsi |
| 197 | 197 | and rdi, which copying needs, aren't among them. *) |
| @@ -1451,7 +1451,8 @@ | ||
| 1451 | 1451 | found by at, which a stub works out from where it was called; what is |
| 1452 | 1452 | an index a (0), a slice a..b (1), each for a length len, a position a |
| 1453 | 1453 | of the enumeration called name (2), a slice of a pointer a..b that |
| 1454 | - runs backwards (3), or a nil pointer (4). Rt.fail knows 5 and 6, a | |
| 1454 | + runs backwards (3), a nil pointer (4), or a view at index a that runs | |
| 1455 | + past a length len (7). Rt.fail knows 5 and 6, a | |
| 1455 | 1456 | division fault and a stack overflow, which this machine catches |
| 1456 | 1457 | itself, so no check here has them. The arguments go in rdi, rsi, rdx, |
| 1457 | 1458 | rcx, r8 and r9: a is in register ra, b in RDX; len is in memory at m, |
| @@ -1528,6 +1529,38 @@ | ||
| 1528 | 1529 | check(X.ccB, 0, p, r, noMem(), n, -1); |
| 1529 | 1530 | end; |
| 1530 | 1531 | |
| 1532 | +(* The index on top is the element a view starts at, in an array of n | |
| 1533 | + elements: the view needs k elements from there, so the index must be | |
| 1534 | + below n - k + 1, which is below. A negative index is a huge one, | |
| 1535 | + unsigned, so one comparison does (report section 6.9). *) | |
| 1536 | +proc checkView*(n, below: I64; p: Diag.Pos); | |
| 1537 | +var r: INT; | |
| 1538 | +begin | |
| 1539 | + r := peek(depth - 1); | |
| 1540 | + cmpI(r, below); | |
| 1541 | + check(X.ccB, 7, p, r, noMem(), n, -1); | |
| 1542 | +end; | |
| 1543 | + | |
| 1544 | +(* What a failed index check is: an index past the length (0), or a | |
| 1545 | + view that runs past it (7), when the view needs room elements more | |
| 1546 | + than the one indexed. *) | |
| 1547 | +proc kindOfIndex(room: I64): INT; | |
| 1548 | +begin | |
| 1549 | + if room > 0 then return 7; end; | |
| 1550 | + return 0; | |
| 1551 | +end; | |
| 1552 | + | |
| 1553 | +(* A view of a slice's element, at the index in r, needs room elements | |
| 1554 | + after it: rdx holds the slice's length, which r is below already, | |
| 1555 | + so length - r, at least 1, can't wrap, and must be more than room. | |
| 1556 | + The length is at m for the message. *) | |
| 1557 | +proc checkRoom(r: INT; room: I64; p: Diag.Pos; m: X.Mem); | |
| 1558 | +begin | |
| 1559 | + X.alu(X.aluSub, RDX, r); | |
| 1560 | + X.aluI(X.aluCmp, RDX, INT(room)); | |
| 1561 | + check(X.ccA, 7, p, r, m, 0, -1); | |
| 1562 | +end; | |
| 1563 | + | |
| 1531 | 1564 | (* The integer on top is a position of an enumeration of n names *) |
| 1532 | 1565 | proc checkEnum*(n: I64; name: INT; p: Diag.Pos); |
| 1533 | 1566 | var r: INT; |
| @@ -1568,7 +1601,7 @@ | ||
| 1568 | 1601 | compared with and added to them; for an element of 1, 2, 4 or 8 |
| 1569 | 1602 | bytes, its address is only noted, as the operand [pointer + index * |
| 1570 | 1603 | size], to be read or written where it is used (lIdx). *) |
| 1571 | -proc indexSlice*(a: Adr; size: I64; checked: BOOL; p: Diag.Pos); | |
| 1604 | +proc indexSlice*(a: Adr; size, room: I64; checked: BOOL; p: Diag.Pos); | |
| 1572 | 1605 | var m, ml: X.Mem; r: INT; inRax: BOOL; o: I64; |
| 1573 | 1606 | begin |
| 1574 | 1607 | if (a.base = bReg) and (((size = 1) or (size = 2) or (size = 4) or (size = 8))) then |
| @@ -1577,7 +1610,8 @@ | ||
| 1577 | 1610 | r := peek(depth - 1); |
| 1578 | 1611 | if checked then |
| 1579 | 1612 | X.alu(X.aluCmp, r, a.hi); |
| 1580 | - check(X.ccB, 0, p, r, inRegister(a.hi), 0, -1); | |
| 1613 | + check(X.ccB, kindOfIndex(room), p, r, inRegister(a.hi), 0, -1); | |
| 1614 | + if room > 0 then X.movRR(RDX, a.hi); checkRoom(r, room, p, inRegister(a.hi)); end; | |
| 1581 | 1615 | end; |
| 1582 | 1616 | drop(); |
| 1583 | 1617 | newMem(lIdx, X.memIdx(a.chunk, r, INT(size), o), kWord, 8, false); |
| @@ -1589,7 +1623,8 @@ | ||
| 1589 | 1623 | if a.base = bReg then |
| 1590 | 1624 | if checked then |
| 1591 | 1625 | X.alu(X.aluCmp, r, a.hi); |
| 1592 | - check(X.ccB, 0, p, r, inRegister(a.hi), 0, -1); | |
| 1626 | + check(X.ccB, kindOfIndex(room), p, r, inRegister(a.hi), 0, -1); | |
| 1627 | + if room > 0 then X.movRR(RDX, a.hi); checkRoom(r, room, p, inRegister(a.hi)); end; | |
| 1593 | 1628 | end; |
| 1594 | 1629 | mulI(r, size); |
| 1595 | 1630 | X.alu(X.aluAdd, r, a.chunk); |
| @@ -1598,7 +1633,8 @@ | ||
| 1598 | 1633 | ml := m; ml.disp := ml.disp + 8; |
| 1599 | 1634 | if checked then |
| 1600 | 1635 | X.aluM(X.aluCmp, r, ml); |
| 1601 | - check(X.ccB, 0, p, r, ml, 0, -1); | |
| 1636 | + check(X.ccB, kindOfIndex(room), p, r, ml, 0, -1); | |
| 1637 | + if room > 0 then X.load(RDX, ml, 8, false); checkRoom(r, room, p, ml); end; | |
| 1602 | 1638 | end; |
| 1603 | 1639 | mulI(r, size); |
| 1604 | 1640 | X.aluM(X.aluAdd, r, m); |
| @@ -48,6 +48,8 @@ | ||
| 48 | 48 | vol*: BOOL; (* mVar: reached through ^VOLATILE *) |
| 49 | 49 | isVar*: BOOL; (* mVar: a variable of the program, not a temporary *) |
| 50 | 50 | odd*: BOOL; (* mVar: in a packed record, so maybe misaligned *) |
| 51 | + view*: BOOL; (* mVar: a view, v.(T), or part of one: maybe | |
| 52 | + misaligned, and with no address *) | |
| 51 | 53 | ro*: BOOL; (* mVar: another module's exported variable, or part |
| 52 | 54 | of one, which only that module changes *) |
| 53 | 55 | deref*: BOOL; (* mVar: at an address from a pointer, which may be |
| @@ -363,19 +365,25 @@ | ||
| 363 | 365 | return nil; |
| 364 | 366 | end; |
| 365 | 367 | |
| 366 | -(* The address of the variable or string x, on top of the stack *) | |
| 367 | -proc address*(var x: Item): ERROR; | |
| 368 | +(* x, a variable kept in a register, is wanted in memory after all, for | |
| 369 | + its address or a view of its bytes: the procedure is compiled again | |
| 370 | + with the variable in memory; till then, this code is thrown away *) | |
| 371 | +proc demote(var x: Item); | |
| 368 | 372 | begin |
| 369 | - nilTest(x); | |
| 370 | 373 | if (x.mode = mVar) and (x.base = Gen.bReg) then |
| 371 | - (* its address is wanted after all: the procedure is compiled again | |
| 372 | - with the variable in memory; till then, this code is thrown away *) | |
| 373 | 374 | x.obj^.reg := 0; |
| 374 | 375 | x.obj^.reg2 := 0; |
| 375 | 376 | demoted := true; |
| 376 | 377 | x.base := Gen.bFrame; |
| 377 | 378 | x.off := x.obj^.val; |
| 378 | 379 | end; |
| 380 | +end; | |
| 381 | + | |
| 382 | +(* The address of the variable or string x, on top of the stack *) | |
| 383 | +proc address*(var x: Item): ERROR; | |
| 384 | +begin | |
| 385 | + nilTest(x); | |
| 386 | + demote(x); | |
| 379 | 387 | if x.mode = mStr then Gen.pushAdr(globalAdr(x.chunk)); |
| 380 | 388 | elsif x.mode = mVar then |
| 381 | 389 | Gen.pushAdr(adrOf(x)); |
| @@ -509,6 +517,13 @@ | ||
| 509 | 517 | return Scan.Failed; |
| 510 | 518 | end; |
| 511 | 519 | |
| 520 | +(* An error at x, which is a view, or part of one, and so has no | |
| 521 | + address: text, and help that says what to do instead. *) | |
| 522 | +proc noAddress*(x: Item; text: []U8): ERROR; | |
| 523 | +begin | |
| 524 | + return Scan.markHelp(x.pos, text, "a view has no address, since it may be misaligned: copy it to a variable"); | |
| 525 | +end; | |
| 526 | + | |
| 512 | 527 | proc coerce*(var x: Item; t: ^Sym.Type): ERROR; |
| 513 | 528 | var s: ^Sym.Type; tmp: I64; |
| 514 | 529 | begin |
| @@ -546,6 +561,7 @@ | ||
| 546 | 561 | try notReadOnly(x); |
| 547 | 562 | if x.vol then return Scan.markAt(x.pos, "can't make a slice through a volatile pointer"); end; |
| 548 | 563 | if x.odd then return Scan.markAt(x.pos, "can't make a slice of a field of a packed record"); end; |
| 564 | + if x.view then return noAddress(x, "can't make a slice of a view"); end; | |
| 549 | 565 | try address(x); |
| 550 | 566 | sliceOf(x, t, s^.len); |
| 551 | 567 | elsif (x.mode = mProc) and (t^.form # Sym.fProc) and (s^.base = nil) then |
| @@ -612,6 +628,7 @@ | ||
| 612 | 628 | try value(x); |
| 613 | 629 | x.mode := mVar; x.base := Gen.bStack; x.off := 0; x.deref := true; |
| 614 | 630 | x.typ := t^.base; x.vol := t^.vol; x.isVar := true; x.odd := false; x.ro := false; |
| 631 | + x.view := false; | |
| 615 | 632 | return nil; |
| 616 | 633 | end; |
| 617 | 634 | |
| @@ -669,7 +686,7 @@ | ||
| 669 | 686 | Gen.op(Scan.sPlus, Gen.kWord, 8, false); |
| 670 | 687 | else Gen.indexPtr(s); end; |
| 671 | 688 | x.mode := mVar; x.base := Gen.bStack; x.off := 0; |
| 672 | - x.vol := t^.vol; x.isVar := true; x.odd := false; x.ro := false; | |
| 689 | + x.vol := t^.vol; x.isVar := true; x.odd := false; x.ro := false; x.view := false; | |
| 673 | 690 | elsif (t^.form = Sym.fArray) and (y.mode = mConst) then |
| 674 | 691 | if (y.val < 0) or (y.val >= t^.len) then |
| 675 | 692 | return Scan.markAt(y.pos, Diag.say("index {} is out of range for a length of {}", [Fmt.int(y.val), Fmt.int(t^.len)])); |
| @@ -683,7 +700,7 @@ | ||
| 683 | 700 | x.base := Gen.bStack; |
| 684 | 701 | else |
| 685 | 702 | try value(y); |
| 686 | - Gen.indexSlice(adrOf(x), s, checks, y.pos); | |
| 703 | + Gen.indexSlice(adrOf(x), s, 0, checks, y.pos); | |
| 687 | 704 | x.base := Gen.bStack; x.off := 0; x.isVar := true; x.vol := false; x.ro := false; |
| 688 | 705 | end; |
| 689 | 706 | x.typ := t^.base; |
| @@ -707,6 +724,7 @@ | ||
| 707 | 724 | end; |
| 708 | 725 | if x.vol then return Scan.markAt(x.pos, "can't make a slice through a volatile pointer"); end; |
| 709 | 726 | if x.odd then return Scan.markAt(x.pos, "can't make a slice of a field of a packed record"); end; |
| 727 | + if x.view then return noAddress(x, "can't make a slice of a view"); end; | |
| 710 | 728 | if t^.form = Sym.fArray then try notReadOnly(x); end; |
| 711 | 729 | try value(hi); |
| 712 | 730 | tmp := Gen.allocTemp(16, 8); |
| @@ -718,6 +736,143 @@ | ||
| 718 | 736 | return nil; |
| 719 | 737 | end; |
| 720 | 738 | |
| 739 | +(* Views, v.(T): the bytes of the variable v read and written as a T | |
| 740 | + (report section 6.9). A view is the item of v with T for its type: | |
| 741 | + the same place, so nothing is copied or computed to make one. What | |
| 742 | + is left is to check the type, and that T's bytes lie where v's room | |
| 743 | + is; and to mark the item a view, which may be misaligned, as a field | |
| 744 | + of a packed record may, so that nothing takes its address. *) | |
| 745 | + | |
| 746 | +(* Any bytes at all are a value of an integer, a real or a pointer, | |
| 747 | + and of a record or array made of them alone. A BOOL, an enumeration | |
| 748 | + or a set has bytes that are none of its values, and a slice, | |
| 749 | + variant, procedure or ERROR holds more than bytes can say. The type | |
| 750 | + in t that isn't plain data, or nil if all of it is. *) | |
| 751 | +proc notPlain(t: ^Sym.Type): ^Sym.Type; | |
| 752 | +var f: ^Sym.Obj; u: ^Sym.Type; | |
| 753 | +begin | |
| 754 | + match t^.form of | |
| 755 | + | fInt, fReal, fPtr: return nil; | |
| 756 | + | fArray: return notPlain(t^.base); | |
| 757 | + | fRecord: | |
| 758 | + if t^.tuple then return t; end; | |
| 759 | + f := t^.fields; | |
| 760 | + while f # nil do | |
| 761 | + u := notPlain(f^.typ); | |
| 762 | + if u # nil then return u; end; | |
| 763 | + f := f^.next; | |
| 764 | + end; | |
| 765 | + return nil; | |
| 766 | + | else: return t; | |
| 767 | + end; | |
| 768 | +end; | |
| 769 | + | |
| 770 | +(* What x must be to have a view made of it, as a T: a variable, not | |
| 771 | + volatile, and T a type of plain data. *) | |
| 772 | +proc viewable(x: Item; t: ^Sym.Type): ERROR; | |
| 773 | +var u: ^Sym.Type; text: []U8; | |
| 774 | +begin | |
| 775 | + (* another module's variable, ro, is one too, read and not written *) | |
| 776 | + if (x.mode # mVar) or not (x.isVar or x.ro) then | |
| 777 | + return Scan.markHelp(x.pos, "a view needs a variable", | |
| 778 | + "a constant, or the result of a call or an operator, has no bytes to view: copy it to a variable"); | |
| 779 | + end; | |
| 780 | + if x.vol then | |
| 781 | + return Scan.markHelp(x.pos, "can't make a view of a volatile variable", | |
| 782 | + "a view may be misaligned, which can't be one access: reach a device through a ^VOLATILE pointer to a record"); | |
| 783 | + end; | |
| 784 | + u := notPlain(t); | |
| 785 | + if u = nil then return nil; end; | |
| 786 | + if u # t then | |
| 787 | + text := say2("can't view bytes as {}, which holds {}", Sym.typeName(t), Sym.typeName(u)); | |
| 788 | + else | |
| 789 | + text := say1("can't view bytes as {}", Sym.typeName(t)); | |
| 790 | + end; | |
| 791 | + if u^.form = Sym.fBool then | |
| 792 | + return Scan.markHelp(x.pos, text, "some bytes are no BOOL: view an integer, and compare it with 0"); | |
| 793 | + elsif u^.form in {Sym.fEnum, Sym.fSet} then | |
| 794 | + return Scan.markHelp(x.pos, text, | |
| 795 | + say1("some bytes are no {}: view an integer, and convert it", Sym.typeName(u))); | |
| 796 | + end; | |
| 797 | + return Scan.markHelp(x.pos, text, "a view is an integer, a real or a pointer, or a record or array made of them"); | |
| 798 | +end; | |
| 799 | + | |
| 800 | +(* x becomes the view of itself as a T, its type changed and nothing | |
| 801 | + else: the bytes are where x's are. A variable in a register has no | |
| 802 | + bytes to view, so it goes to memory. *) | |
| 803 | +proc becomeView(var x: Item; t: ^Sym.Type); | |
| 804 | +begin | |
| 805 | + demote(x); | |
| 806 | + x.typ := t; | |
| 807 | + x.view := true; | |
| 808 | +end; | |
| 809 | + | |
| 810 | +(* v.(T), where v is not an element: T must fit in v itself. *) | |
| 811 | +proc view*(var x: Item; t: ^Sym.Type): ERROR; | |
| 812 | +begin | |
| 813 | + try viewable(x, t); | |
| 814 | + if t^.size > x.typ^.size then | |
| 815 | + return Scan.markAt(x.pos, Diag.say("a view of {} bytes doesn't fit in {}, of {}", | |
| 816 | + [Fmt.int(t^.size), Fmt.str(Sym.typeName(x.typ)), Fmt.int(x.typ^.size)])); | |
| 817 | + end; | |
| 818 | + becomeView(x, t); | |
| 819 | + return nil; | |
| 820 | +end; | |
| 821 | + | |
| 822 | +(* The number of elements of size s that n bytes reach into, at least | |
| 823 | + one: those of a[i] up to a[i + k - 1]. *) | |
| 824 | +proc elementsFor(n, s: I64): I64; | |
| 825 | +begin | |
| 826 | + if (s = 0) or (n <= s) then return 1; end; | |
| 827 | + return (n + s - 1) div s; | |
| 828 | +end; | |
| 829 | + | |
| 830 | +(* a[i].(T): x is the array, slice or pointer, y the index. T must lie | |
| 831 | + from a[i] to a's end: in an array of n elements, k of them from i on, | |
| 832 | + so i is below n - k + 1; a slice's length is known only as the | |
| 833 | + program runs, which checks that length - i, at least 1 since i is | |
| 834 | + below it, is at least k. Through a pointer, no check: p[i] has none | |
| 835 | + (report section 6.9). *) | |
| 836 | +proc indexView*(var x, y: Item; t: ^Sym.Type): ERROR; | |
| 837 | +var at: ^Sym.Type; s, k: I64; | |
| 838 | +begin | |
| 839 | + at := x.typ; | |
| 840 | + s := at^.base^.size; | |
| 841 | + k := elementsFor(t^.size, s); | |
| 842 | + if not Sym.isInt(y.typ) then | |
| 843 | + return Scan.markAt(y.pos, say1("an index is an integer, found {}", what(y))); | |
| 844 | + end; | |
| 845 | + if (at^.form = Sym.fArray) and (k > at^.len) then | |
| 846 | + return Scan.markAt(Diag.join(x.pos, y.pos), Diag.say("a view of {} bytes doesn't fit in {}, of {}", | |
| 847 | + [Fmt.int(t^.size), Fmt.str(Sym.typeName(at)), Fmt.int(at^.size)])); | |
| 848 | + end; | |
| 849 | + if (at^.form = Sym.fArray) and (y.mode = mConst) then | |
| 850 | + if (y.val < 0) or (y.val > at^.len - k) then | |
| 851 | + return Scan.markAt(y.pos, Diag.say("a view of {} bytes at index {} runs past a length of {}", | |
| 852 | + [Fmt.int(t^.size), Fmt.int(y.val), Fmt.int(at^.len)])); | |
| 853 | + end; | |
| 854 | + try index(x, y); | |
| 855 | + elsif (at^.form = Sym.fArray) and checks then | |
| 856 | + (* as index does, with the view's check in place of i < n *) | |
| 857 | + try value(y); | |
| 858 | + Gen.checkView(at^.len, at^.len - k + 1, y.pos); | |
| 859 | + if x.base = Gen.bStack then scaleBy(s); Gen.op(Scan.sPlus, Gen.kWord, 8, false); | |
| 860 | + else Gen.indexArray(adrOf(x), s); x.off := 0; end; | |
| 861 | + x.base := Gen.bStack; | |
| 862 | + x.typ := at^.base; | |
| 863 | + elsif at^.form = Sym.fSlice then | |
| 864 | + try value(y); | |
| 865 | + Gen.indexSlice(adrOf(x), s, k - 1, checks, y.pos); | |
| 866 | + x.base := Gen.bStack; x.off := 0; x.isVar := true; x.vol := false; x.ro := false; | |
| 867 | + x.typ := at^.base; | |
| 868 | + else | |
| 869 | + try index(x, y); | |
| 870 | + end; | |
| 871 | + try viewable(x, t); | |
| 872 | + becomeView(x, t); | |
| 873 | + return nil; | |
| 874 | +end; | |
| 875 | + | |
| 721 | 876 | (* Operators *) |
| 722 | 877 | |
| 723 | 878 | proc neg*(var x: Item): ERROR; |
| @@ -1090,10 +1245,10 @@ | ||
| 1090 | 1245 | end; |
| 1091 | 1246 | t := xt; |
| 1092 | 1247 | elsif Sym.same(xt, yt) then t := xt; |
| 1093 | - (* an INT meeting an I64 widens to it, a UINT to a U64 (report | |
| 1094 | - section 6.1): the same 64 bits here, so only the type changes *) | |
| 1095 | - elsif widens(xt, yt) then t := yt; | |
| 1096 | - elsif widens(yt, xt) then t := xt; | |
| 1248 | + elsif widens(xt, yt) or widens(yt, xt) then | |
| 1249 | + (* an INT meeting an I64 widens to it, a UINT to a U64 (report | |
| 1250 | + section 6.1): the same 64 bits here, so only the type changes *) | |
| 1251 | + if widens(xt, yt) then t := yt; else t := xt; end; | |
| 1097 | 1252 | else return mismatchOps(x, y, xt, yt); end; |
| 1098 | 1253 | if not intOp(op) then return doesntApply(op, x, y, t); end; |
| 1099 | 1254 | elsif (xt^.form = Sym.fReal) and (yt^.form = Sym.fReal) then |
| @@ -1356,6 +1511,7 @@ | ||
| 1356 | 1511 | try notReadOnly(a); |
| 1357 | 1512 | if (a.mode # mVar) or not a.isVar then return Scan.markAt(a.pos, "a var parameter needs a variable"); end; |
| 1358 | 1513 | if a.odd then return Scan.markAt(a.pos, "a field of a packed record can't be a var parameter"); end; |
| 1514 | + if a.view then return noAddress(a, "a view can't be a var parameter"); end; | |
| 1359 | 1515 | if not Sym.same(a.typ, p^.typ) then |
| 1360 | 1516 | return mismatch(a, say2("a var parameter needs exactly {}, found {}", Sym.typeName(p^.typ), what(a))); |
| 1361 | 1517 | end; |
| @@ -905,6 +905,7 @@ | ||
| 905 | 905 | try Item.notReadOnly(y); |
| 906 | 906 | if (y.mode # Item.mVar) or not y.isVar then return Scan.markAt(y.pos, "ADR needs a variable"); end; |
| 907 | 907 | if y.odd then return Scan.markAt(y.pos, "a field of a packed record has no address"); end; |
| 908 | + if y.view then return Item.noAddress(y, "ADR can't take a view"); end; | |
| 908 | 909 | t := newPtr(y.typ, y.vol); |
| 909 | 910 | try Item.address(y); |
| 910 | 911 | Item.makeConst(x, t, 0); |
| @@ -1012,6 +1013,28 @@ | ||
| 1012 | 1013 | |
| 1013 | 1014 | (* A designator, with any selectors and calls after it. called tells |
| 1014 | 1015 | whether the last thing done was a call. *) |
| 1016 | +(* Whether a view, ".(", is next: a "." and, past it, a "(". *) | |
| 1017 | +proc viewNext: BOOL; | |
| 1018 | +var st: Scan.State; yes: BOOL; | |
| 1019 | +begin | |
| 1020 | + if Scan.tok # Scan.sPeriod then return false; end; | |
| 1021 | + Scan.saveScan(st); | |
| 1022 | + Scan.next; | |
| 1023 | + yes := Scan.tok = Scan.sLparen; | |
| 1024 | + Scan.restoreScan(st); | |
| 1025 | + return yes; | |
| 1026 | +end; | |
| 1027 | + | |
| 1028 | +(* The "(" T ")" of a view, at the "(": T. *) | |
| 1029 | +proc viewType: (PType, ERROR); | |
| 1030 | +var t: PType; | |
| 1031 | +begin | |
| 1032 | + Scan.next; | |
| 1033 | + t := try parseType(); | |
| 1034 | + try expect(Scan.sRparen); | |
| 1035 | + return t, nil; | |
| 1036 | +end; | |
| 1037 | + | |
| 1015 | 1038 | proc designator(var x: TItem; var called: BOOL): ERROR; |
| 1016 | 1039 | var o, f: PObj; y, z: TItem; t: PType; start: INT; |
| 1017 | 1040 | begin |
| @@ -1032,12 +1055,18 @@ | ||
| 1032 | 1055 | x.pos := Scan.spanFromAt(start); |
| 1033 | 1056 | match Scan.tok of |
| 1034 | 1057 | | sPeriod: |
| 1035 | - Scan.next; | |
| 1036 | - if (x.mode # Item.mVar) or (x.typ^.form # Sym.fRecord) then return fieldOfWhat(x); end; | |
| 1037 | - if Scan.tok # Scan.sIdent then return expected("a field"); end; | |
| 1038 | - f := try fieldOf(x.typ); | |
| 1039 | 1058 | Scan.next; |
| 1040 | - try Item.field(x, f); | |
| 1059 | + if Scan.tok = Scan.sLparen then | |
| 1060 | + t := try viewType(); | |
| 1061 | + x.pos := Scan.spanFromAt(start); | |
| 1062 | + try Item.view(x, t); | |
| 1063 | + else | |
| 1064 | + if (x.mode # Item.mVar) or (x.typ^.form # Sym.fRecord) then return fieldOfWhat(x); end; | |
| 1065 | + if Scan.tok # Scan.sIdent then return expected("a field"); end; | |
| 1066 | + f := try fieldOf(x.typ); | |
| 1067 | + Scan.next; | |
| 1068 | + try Item.field(x, f); | |
| 1069 | + end; | |
| 1041 | 1070 | called := false; |
| 1042 | 1071 | | sArrow: |
| 1043 | 1072 | Scan.next; |
| @@ -1052,10 +1081,21 @@ | ||
| 1052 | 1081 | try Item.sliceLo(y); |
| 1053 | 1082 | try expression(z, nil); |
| 1054 | 1083 | try Item.slice(x, y, z); |
| 1084 | + try expect(Scan.sRbrak); | |
| 1055 | 1085 | else |
| 1056 | - try Item.index(x, y); | |
| 1086 | + (* a[i].(T), a view of an element, is checked against the | |
| 1087 | + end of a, not of the element: so the parser looks past | |
| 1088 | + the "]" for ".(" before the index is made *) | |
| 1089 | + try expect(Scan.sRbrak); | |
| 1090 | + if viewNext() then | |
| 1091 | + Scan.next; | |
| 1092 | + t := try viewType(); | |
| 1093 | + x.pos := Scan.spanFromAt(start); | |
| 1094 | + try Item.indexView(x, y, t); | |
| 1095 | + else | |
| 1096 | + try Item.index(x, y); | |
| 1097 | + end; | |
| 1057 | 1098 | end; |
| 1058 | - try expect(Scan.sRbrak); | |
| 1059 | 1099 | called := false; |
| 1060 | 1100 | | sLparen: |
| 1061 | 1101 | if x.mode = Item.mType then |
| @@ -9,7 +9,8 @@ | ||
| 9 | 9 | # 2026-10.25): the files that use them are the compiler's alone, and |
| 10 | 10 | # so are those whose message says what match takes. It predates, too, |
| 11 | 11 | # an INT widened in an expression and the rest (report 2026-10.26), |
| 12 | -# and the messages for an I64 that goes to an INT. | |
| 12 | +# and the messages for an I64 that goes to an INT; and views, v.(T), | |
| 13 | +# whose tests are conform/*/View*.raz. | |
| 13 | 14 | newer="conform/run/RawStrings.raz conform/err/ScanRawOpen.raz conform/err/ScanRawIndent.raz |
| 14 | 15 | conform/run/MatchStr.raz conform/err/MatchStrTwice.raz conform/err/MatchStrNoElse.raz |
| 15 | 16 | conform/err/MatchStrVar.raz conform/err/MatchReal.raz conform/err/AtMatch.raz |
| @@ -17,6 +18,7 @@ | ||
| 17 | 18 | conform/run/IntWiden.raz conform/err/IntIncNarrow.raz conform/err/IntNarrow.raz conform/err/IntNarrowSum.raz |
| 18 | 19 | conform/err/IntResultsNarrow.raz" |
| 19 | 20 | newer() { |
| 21 | + case $1 in conform/*/View*.raz) return 0 ;; esac | |
| 20 | 22 | for g in $newer; do [ $g = $1 ] && return 0; done |
| 21 | 23 | return 1 |
| 22 | 24 | } |
| @@ -0,0 +1,5 @@ | ||
| 1 | +conform/err/ViewAdr.raz:5:14: error: ADR can't take a view | |
| 2 | + 5 | p := ADR(mem[0].(I64)); | |
| 3 | + | ^^^^^^^^^^^^ | |
| 4 | +help: a view has no address, since it may be misaligned: copy it to a variable | |
| 5 | +raz: 1 error |
| @@ -0,0 +1,4 @@ | ||
| 1 | +conform/err/ViewArrayTooSmall.raz:5:10: error: a view of 8 bytes doesn't fit in [4]U8, of 4 | |
| 2 | + 5 | n := mem[i].(I64); | |
| 3 | + | ^^^^^ | |
| 4 | +raz: 1 error |
| @@ -0,0 +1,5 @@ | ||
| 1 | +conform/err/ViewBool.raz:5:10: error: can't view bytes as BOOL | |
| 2 | + 5 | b := mem[0].(BOOL); | |
| 3 | + | ^^^^^^^^^^^^^ | |
| 4 | +help: some bytes are no BOOL: view an integer, and compare it with 0 | |
| 5 | +raz: 1 error |
| @@ -0,0 +1,5 @@ | ||
| 1 | +conform/err/ViewCall.raz:6:10: error: a view needs a variable | |
| 2 | + 6 | u := f().(U64); | |
| 3 | + | ^^^^^^^^^ | |
| 4 | +help: a constant, or the result of a call or an operator, has no bytes to view: copy it to a variable | |
| 5 | +raz: 1 error |
| @@ -0,0 +1,5 @@ | ||
| 1 | +conform/err/ViewConst.raz:6:10: error: a view needs a variable | |
| 2 | + 6 | n := Table[0].(U32); | |
| 3 | + | ^^^^^^^^^^^^^^ | |
| 4 | +help: a constant, or the result of a call or an operator, has no bytes to view: copy it to a variable | |
| 5 | +raz: 1 error |
| @@ -0,0 +1,5 @@ | ||
| 1 | +conform/err/ViewEnum.raz:6:10: error: can't view bytes as Colour | |
| 2 | + 6 | c := mem[0].(Colour); | |
| 3 | + | ^^^^^^^^^^^^^^^ | |
| 4 | +help: some bytes are no Colour: view an integer, and convert it | |
| 5 | +raz: 1 error |
| @@ -0,0 +1,3 @@ | ||
| 1 | +conform/run/ViewNegative.raz:9:16: a view at index -1 runs past a length of 16 | |
| 2 | + in ViewNegative.at at conform/run/ViewNegative.raz:9:16: i = -1 | |
| 3 | + in ViewNegative.main at conform/run/ViewNegative.raz:15:5 |
| @@ -0,0 +1,2 @@ | ||
| 1 | +conform/run/ViewPast.raz:12:13: a view at index 9 runs past a length of 16 | |
| 2 | + in ViewPast.main at conform/run/ViewPast.raz:12:13: i = 9 |
| @@ -0,0 +1,4 @@ | ||
| 1 | +conform/err/ViewPastEnd.raz:5:14: error: a view of 8 bytes at index 10 runs past a length of 16 | |
| 2 | + 5 | n := mem[10].(I64); | |
| 3 | + | ^^ | |
| 4 | +raz: 1 error |
| @@ -0,0 +1,2 @@ | ||
| 1 | +conform/run/ViewPastSlice.raz:11:11: a view at index 7 runs past a length of 10 | |
| 2 | + in ViewPastSlice.main at conform/run/ViewPastSlice.raz:11:11: i = 7 |
| @@ -0,0 +1,4 @@ | ||
| 1 | +conform/err/ViewReadOnly.raz:5:5: error: ViewMod.mem[0].(I64) is ViewMod's, and can't be changed outside it | |
| 2 | + 5 | ViewMod.mem[0].(I64) := 1; | |
| 3 | + | ^^^^^^^^^^^^^^^^^^^^ | |
| 4 | +raz: 1 error |
| @@ -0,0 +1,5 @@ | ||
| 1 | +conform/err/ViewRecordBool.raz:6:10: error: can't view bytes as Flags, which holds BOOL | |
| 2 | + 6 | f := mem[0].(Flags); | |
| 3 | + | ^^^^^^^^^^^^^^ | |
| 4 | +help: some bytes are no BOOL: view an integer, and compare it with 0 | |
| 5 | +raz: 1 error |
| @@ -0,0 +1,5 @@ | ||
| 1 | +conform/err/ViewSlice.raz:5:10: error: can't view bytes as []U8 | |
| 2 | + 5 | s := mem[0].([]U8); | |
| 3 | + | ^^^^^^^^^^^^^ | |
| 4 | +help: a view is an integer, a real or a pointer, or a record or array made of them | |
| 5 | +raz: 1 error |
| @@ -0,0 +1,5 @@ | ||
| 1 | +conform/err/ViewSliceOf.raz:5:10: error: can't make a slice of a view | |
| 2 | + 5 | s := mem[0].([8]U8)[0..4]; | |
| 3 | + | ^^^^^^^^^^^^^^ | |
| 4 | +help: a view has no address, since it may be misaligned: copy it to a variable | |
| 5 | +raz: 1 error |
| @@ -0,0 +1,4 @@ | ||
| 1 | +conform/err/ViewTooBig.raz:5:10: error: a view of 8 bytes doesn't fit in I32, of 4 | |
| 2 | + 5 | u := n.(U64); | |
| 3 | + | ^^^^^^^ | |
| 4 | +raz: 1 error |
| @@ -0,0 +1,5 @@ | ||
| 1 | +conform/err/ViewVarParam.raz:6:9: error: a view can't be a var parameter | |
| 2 | + 6 | put(mem[3].(I64)); | |
| 3 | + | ^^^^^^^^^^^^ | |
| 4 | +help: a view has no address, since it may be misaligned: copy it to a variable | |
| 5 | +raz: 1 error |
| @@ -0,0 +1,5 @@ | ||
| 1 | +conform/err/ViewVolatile.raz:5:10: error: can't make a view of a volatile variable | |
| 2 | + 5 | n := p^[4].(U32); | |
| 3 | + | ^^^^^^^^^^^ | |
| 4 | +help: a view may be misaligned, which can't be one access: reach a device through a ^VOLATILE pointer to a record | |
| 5 | +raz: 1 error |