Packed fields have no address; byte order; Out's buffer is the target's A field of a packed record aligned to more than a byte may sit anywhere, so it is read and written but has no address, no var parameter, no slice and no volatile access: what a machine that can't reach a misaligned word could not keep. Byte order is the target's; Str gains le, be, putLE and putBE for a fixed one. Out keeps Sys.outSize bytes, each target's own.
| @@ -12,8 +12,8 @@ | ||
| 12 | 12 | | `src/scan.pas` | 851 | 722 | Symbols, literals, comments, positions | |
| 13 | 13 | | `src/sym.pas` | 593 | 494 | Types, objects, scopes, layout | |
| 14 | 14 | | `src/x64.pas` | 693 | 557 | Code buffer, chunks, fixups, places, instruction encoder | |
| 15 | -| `src/gen.pas` | 1,619 | 1,398 | Items: loading, storing, copying, frames, calls | | |
| 16 | -| `src/parse.pas` | 2,443 | 2,202 | Declarations, statements, expressions, built-ins, recovery, warnings | | |
| 15 | +| `src/gen.pas` | 1,631 | 1,408 | Items: loading, storing, copying, frames, calls | | |
| 16 | +| `src/parse.pas` | 2,444 | 2,203 | Declarations, statements, expressions, built-ins, recovery, warnings | | |
| 17 | 17 | | `src/link.pas` | 180 | 139 | Tree shaking, layout, patching, the places | |
| 18 | 18 | | `src/elf.pas` | 425 | 352 | Static and dynamic ELF, symbol table, line numbers | |
| 19 | 19 | | `src/raz.pas` | 227 | 179 | The command line, the entry points | |
| @@ -354,10 +354,21 @@ | ||
| 354 | 354 | size is then 0. |
| 355 | 355 | |
| 356 | 356 | A `packed record` has no padding: each field starts at the byte after |
| 357 | -the field before it, and the record's alignment is 1. A field of a | |
| 358 | -packed record may be misaligned. x86-64 reads and writes it anyway, but | |
| 359 | -an atomic operation on it is slow, and a volatile access to it is one | |
| 360 | -instruction that needn't be atomic. | |
| 357 | +the field before it, and the record's alignment is 1. So a field of a | |
| 358 | +packed record may sit at any address. It is read and written like any | |
| 359 | +other, on every target, even one whose machine can't reach a | |
| 360 | +misaligned word in one access: the compiled code reaches it in parts | |
| 361 | +there. But a field of a packed record whose type is aligned to more | |
| 362 | +than 1 byte, and anything inside it, has no address: `ADR` of it, a | |
| 363 | +`var` parameter of it and a slice of it are errors. Nor can it be | |
| 364 | +reached through a `^VOLATILE` pointer, since a misaligned access can't | |
| 365 | +be promised as one access (section 7.6). Its value can be copied out | |
| 366 | +and back in. | |
| 367 | + | |
| 368 | +The bytes of a value of more than one byte are in the target's order | |
| 369 | +(appendix A). A file or a message in an order of its own is read and | |
| 370 | +written a byte at a time, as `Str.le`, `Str.be`, `Str.putLE` and | |
| 371 | +`Str.putBE` do (section 11). | |
| 361 | 372 | |
| 362 | 373 | ### 5.6 Variants |
| 363 | 374 | |
| @@ -1068,6 +1079,7 @@ | ||
| 1068 | 1079 | ``` |
| 1069 | 1080 | const |
| 1070 | 1081 | stdin* = 0; stdout* = 1; stderr* = 2; |
| 1082 | + outSize* = 4096; (* the bytes Out keeps; each target has its own *) | |
| 1071 | 1083 | (* signals, by their numbers on Linux; each target has its own *) |
| 1072 | 1084 | sigHup* = 1; sigInt* = 2; sigQuit* = 3; sigKill* = 9; sigPipe* = 13; |
| 1073 | 1085 | sigTerm* = 15; sigChld* = 17; sigCont* = 18; sigStop* = 19; |
| @@ -1277,6 +1289,10 @@ | ||
| 1277 | 1289 | proc encode*(c: I64): ([4]U8, I64); (* c in UTF-8: its bytes, and how many *) |
| 1278 | 1290 | proc decode*(s: []U8): (I64, I64); (* the character s starts with, and its length in bytes *) |
| 1279 | 1291 | proc toInt*(s: []U8): (I64, BOOL); (* s, in decimal; false if it isn't a number an I64 holds *) |
| 1292 | +proc le*(s: []U8): U64; (* s, at most 8 bytes, the first the lowest *) | |
| 1293 | +proc be*(s: []U8): U64; (* s, at most 8 bytes, the first the highest *) | |
| 1294 | +proc putLE*(s: []U8; v: U64); (* v into s, the lowest byte first *) | |
| 1295 | +proc putBE*(s: []U8; v: U64); (* v into s, the highest byte first *) | |
| 1280 | 1296 | |
| 1281 | 1297 | module Map(K, V; hash: proc(k: K): U64; eq: proc(a, b: K): BOOL); |
| 1282 | 1298 | proc remove*(m: ^Map; k: K): BOOL; (* false if k wasn't there *) |
| @@ -1306,6 +1322,16 @@ | ||
| 1306 | 1322 | one rightly, and 0 and 0 for an empty `s`. `toInt` reads a decimal |
| 1307 | 1323 | number, with a `-` or `+` before it if any, and nothing else. |
| 1308 | 1324 | |
| 1325 | +`le` and `be` read an unsigned number of up to 8 bytes in a fixed | |
| 1326 | +order, little-endian or big-endian, whatever the machine's own, and | |
| 1327 | +`putLE` and `putBE` write one; more than 8 bytes stops the program. | |
| 1328 | +They are how a file format or a network protocol is read and written: | |
| 1329 | + | |
| 1330 | +``` | |
| 1331 | +len := U32(Str.le(header[4..8])); | |
| 1332 | +Str.putBE(packet[0..2], U64(port)); | |
| 1333 | +``` | |
| 1334 | + | |
| 1309 | 1335 | `Map.next` walks a map: from 0, it gives the next entry and the number |
| 1310 | 1336 | to pass for the one after it, and -1 when there are no more. A walk |
| 1311 | 1337 | sees each entry once if the map doesn't change during it: |
| @@ -1455,7 +1481,8 @@ | ||
| 1455 | 1481 | name with `=` in it, a letter `-`, and `-h` or `--help` in the table, |
| 1456 | 1482 | since they are `Opt`'s. |
| 1457 | 1483 | |
| 1458 | -**Out and C's stdio.** `Out` keeps a buffer of 4 KB. It writes it out | |
| 1484 | +**Out and C's stdio.** `Out` keeps a buffer of `Sys.outSize` bytes, | |
| 1485 | +4 KB on Linux. It writes it out | |
| 1459 | 1486 | when it is full, at each line feed when standard output is a terminal, |
| 1460 | 1487 | at `Out.flush`, and when the program ends by `Rt.exit`. C's `stdio` |
| 1461 | 1488 | keeps a buffer of its own for the same standard output. A program that |
| @@ -1526,7 +1553,11 @@ | ||
| 1526 | 1553 | the library's module names are kept for it (section 3); a record, |
| 1527 | 1554 | variant or enumeration is a type of its own wherever it is written |
| 1528 | 1555 | (section 5.10); a `nil` pointer, a division by zero and a stack |
| 1529 | - overflow stop the program on every target (section 10). Mended: | |
| 1556 | + overflow stop the program on every target (section 10); byte order | |
| 1557 | + is the target's, and `Str` reads and writes a fixed one (sections | |
| 1558 | + 5.5 and 11); `Out`'s buffer is `Sys.outSize` bytes (section 11). | |
| 1559 | + Changed: a field of a packed record that may be misaligned has no | |
| 1560 | + address and no volatile access (section 5.5). Mended: | |
| 1530 | 1561 | arithmetic on untyped constants that overflows is an error (section |
| 1531 | 1562 | 2); a slice of a pointer can't run backwards (section 6.4). Ordinary |
| 1532 | 1563 | accesses are no longer promised to be made exactly as written, only |
| @@ -1548,7 +1579,11 @@ | ||
| 1548 | 1579 | - **What is the target's.** Code procedures (section 8) hold the |
| 1549 | 1580 | target's machine code, and exist only where the implementation says |
| 1550 | 1581 | so; `SYSCALL` exists only on Linux targets (section 6.8). A record's |
| 1551 | - layout is the target's C layout (section 5.5). | |
| 1582 | + layout is the target's C layout (section 5.5), and the order of a | |
| 1583 | + value's bytes is the target's (little-endian on x86-64). | |
| 1584 | +- **Sizes in the library.** How many bytes `Out` keeps before it | |
| 1585 | + writes them, `Sys.outSize` (section 11): 4 KB on Linux, fewer on a | |
| 1586 | + small machine. | |
| 1552 | 1587 | - **The executable.** Its format, and the names and line numbers it |
| 1553 | 1588 | carries for a debugger (section 10). |
| 1554 | 1589 | - **How a failure is caught.** A `nil` pointer, a division by zero or |
| @@ -8,7 +8,7 @@ | ||
| 8 | 8 | import Rt, Sys, Lock; |
| 9 | 9 | |
| 10 | 10 | var |
| 11 | - buf: [4096]U8; | |
| 11 | + buf: [Sys.outSize]U8; | |
| 12 | 12 | n: I64; (* bytes in buf *) |
| 13 | 13 | tty: BOOL; (* standard output is a terminal *) |
| 14 | 14 | lock: Lock.Lock; |
| @@ -129,6 +129,58 @@ | ||
| 129 | 129 | return n, true; |
| 130 | 130 | end; |
| 131 | 131 | |
| 132 | +(* The bytes of s, at most 8, as an unsigned number, the first byte | |
| 133 | + the lowest: how a file or a message in little-endian order is read, | |
| 134 | + whatever the machine's own order. *) | |
| 135 | +proc le*(s: []U8): U64; | |
| 136 | +var v: U64; i: I64; | |
| 137 | +begin | |
| 138 | + if LEN(s) > 8 then Rt.stop("Str.le: more than 8 bytes"); end; | |
| 139 | + for i := LEN(s) - 1 to 0 by -1 do v := v shl 8 | U64(s[i]); end; | |
| 140 | + return v; | |
| 141 | +end; | |
| 142 | + | |
| 143 | +(* As le, the first byte the highest: big-endian, network order. *) | |
| 144 | +proc be*(s: []U8): U64; | |
| 145 | +var v: U64; i: I64; | |
| 146 | +begin | |
| 147 | + if LEN(s) > 8 then Rt.stop("Str.be: more than 8 bytes"); end; | |
| 148 | + for i := 0 to LEN(s) - 1 do v := v shl 8 | U64(s[i]); end; | |
| 149 | + return v; | |
| 150 | +end; | |
| 151 | + | |
| 152 | +(* v into the bytes of s, at most 8, the lowest first; what doesn't | |
| 153 | + fit is left out. *) | |
| 154 | +proc putLE*(s: []U8; v: U64); | |
| 155 | +var i: I64; | |
| 156 | +begin | |
| 157 | + if LEN(s) > 8 then Rt.stop("Str.putLE: more than 8 bytes"); end; | |
| 158 | + for i := 0 to LEN(s) - 1 do s[i] := U8(v); v := v shr 8; end; | |
| 159 | +end; | |
| 160 | + | |
| 161 | +(* As putLE, the highest first. *) | |
| 162 | +proc putBE*(s: []U8; v: U64); | |
| 163 | +var i: I64; | |
| 164 | +begin | |
| 165 | + if LEN(s) > 8 then Rt.stop("Str.putBE: more than 8 bytes"); end; | |
| 166 | + for i := LEN(s) - 1 to 0 by -1 do s[i] := U8(v); v := v shr 8; end; | |
| 167 | +end; | |
| 168 | + | |
| 169 | +proc testOrder; | |
| 170 | +var b: [8]U8; | |
| 171 | +begin | |
| 172 | + putLE(b[0..4], 12345678H); | |
| 173 | + if (b[0] # 78H) or (b[3] # 12H) or (le(b[0..4]) # 12345678H) or (be(b[0..2]) # 7856H) then | |
| 174 | + Rt.stop("Str.putLE"); | |
| 175 | + end; | |
| 176 | + putBE(b, 0102030405060708H); | |
| 177 | + if (b[0] # 1) or (b[7] # 8) or (be(b) # 0102030405060708H) or (le(b[6..8]) # 0807H) then | |
| 178 | + Rt.stop("Str.putBE"); | |
| 179 | + end; | |
| 180 | + putBE(b[0..2], 0ABCDH); | |
| 181 | + if (be(b[0..2]) # 0ABCDH) or (le(b[0..0]) # 0) then Rt.stop("Str.be"); end; | |
| 182 | +end; | |
| 183 | + | |
| 132 | 184 | proc testUtf8; |
| 133 | 185 | var b: [4]U8; n, c, i: I64; |
| 134 | 186 | begin |
| @@ -214,6 +266,7 @@ | ||
| 214 | 266 | if not hasPrefix("hello", "he") or hasPrefix("he", "hello") then Rt.stop("Str.hasPrefix"); end; |
| 215 | 267 | testUtf8; |
| 216 | 268 | testToInt; |
| 269 | + testOrder; | |
| 217 | 270 | end; |
| 218 | 271 | |
| 219 | 272 | end Str. |
| @@ -6,6 +6,9 @@ | ||
| 6 | 6 | stdin* = 0; |
| 7 | 7 | stdout* = 1; |
| 8 | 8 | stderr* = 2; |
| 9 | + (* the bytes Out keeps before it writes them; a target with little | |
| 10 | + memory keeps fewer *) | |
| 11 | + outSize* = 4096; | |
| 9 | 12 | (* signals, by their numbers on Linux *) |
| 10 | 13 | sigHup* = 1; sigInt* = 2; sigQuit* = 3; sigKill* = 9; sigPipe* = 13; |
| 11 | 14 | sigTerm* = 15; sigChld* = 17; sigCont* = 18; sigStop* = 19; |
| @@ -15,6 +15,9 @@ | ||
| 15 | 15 | stdin* = 0; |
| 16 | 16 | stdout* = 1; |
| 17 | 17 | stderr* = 2; |
| 18 | + (* the bytes Out keeps before it writes them; a target with little | |
| 19 | + memory keeps fewer *) | |
| 20 | + outSize* = 4096; | |
| 18 | 21 | (* signals, by their numbers on Linux; another target has its own *) |
| 19 | 22 | sigHup* = 1; sigInt* = 2; sigQuit* = 3; sigKill* = 9; sigPipe* = 13; |
| 20 | 23 | sigTerm* = 15; sigChld* = 17; sigCont* = 18; sigStop* = 19; |
| @@ -43,6 +43,7 @@ | ||
| 43 | 43 | chunk: longint; { mVar with bGlobal, mStr: the chunk } |
| 44 | 44 | vol: boolean; { mVar: reached through ^VOLATILE } |
| 45 | 45 | isVar: boolean; { mVar: a variable of the program, not a temporary } |
| 46 | + odd: boolean; { mVar: in a packed record, so maybe misaligned } | |
| 46 | 47 | obj: PObj; { mProc, mBuiltin, mCase } |
| 47 | 48 | pos: TPos; { where it is in the source, for messages } |
| 48 | 49 | end; |
| @@ -514,6 +515,7 @@ | ||
| 514 | 515 | x.typ := t |
| 515 | 516 | else if (s^.form = fArray) and (t^.form = fSlice) and Same(s^.base, t^.base) then begin |
| 516 | 517 | if x.vol then MarkAt(x.pos, 'can''t make a slice through a volatile pointer'); |
| 518 | + if x.odd then MarkAt(x.pos, 'can''t make a slice of a field of a packed record'); | |
| 517 | 519 | Address(x, RAX); |
| 518 | 520 | SliceOf(x, t, s^.len); |
| 519 | 521 | end else |
| @@ -594,11 +596,19 @@ | ||
| 594 | 596 | if t^.base = nil then MarkAt(x.pos, TypeName(t) + ' points to a type never declared'); |
| 595 | 597 | Value(x); |
| 596 | 598 | x.mode := mVar; x.base := bReg; x.reg := RAX; x.off := 0; |
| 597 | - x.typ := t^.base; x.vol := t^.vol; x.isVar := true; | |
| 599 | + x.typ := t^.base; x.vol := t^.vol; x.isVar := true; x.odd := false; | |
| 598 | 600 | end; |
| 599 | 601 | |
| 602 | +{ A field of a packed record, of more than one byte's alignment, may | |
| 603 | + sit anywhere; it is read and written, but has no address and no | |
| 604 | + volatile access, which a machine that can't reach a misaligned word | |
| 605 | + couldn't make one access (report section 5.5). } | |
| 600 | 606 | procedure Field(var x: TItem; f: PObj); |
| 601 | 607 | begin |
| 608 | + if x.typ^.packd and (f^.typ^.align > 1) then begin | |
| 609 | + if x.vol then MarkAt(x.pos, 'a field of a packed record can''t be volatile'); | |
| 610 | + x.odd := true; | |
| 611 | + end; | |
| 602 | 612 | x.off := x.off + f^.val; |
| 603 | 613 | x.typ := f^.typ; |
| 604 | 614 | end; |
| @@ -627,7 +637,7 @@ | ||
| 627 | 637 | PopR(RCX); |
| 628 | 638 | Alu(aluAdd, RAX, RCX); |
| 629 | 639 | x.mode := mVar; x.base := bReg; x.reg := RAX; x.off := 0; |
| 630 | - x.vol := t^.vol; x.isVar := true; | |
| 640 | + x.vol := t^.vol; x.isVar := true; x.odd := false; | |
| 631 | 641 | end else if (t^.form = fArray) and (y.mode = mConst) then begin |
| 632 | 642 | if (y.val < 0) or (y.val >= t^.len) then MarkAt(y.pos, 'index ' + IntStr(y.val) + ' is out of range for a length of ' + IntStr(t^.len)); |
| 633 | 643 | x.off := x.off + y.val * s; |
| @@ -670,6 +680,7 @@ | ||
| 670 | 680 | s := t^.base^.size; |
| 671 | 681 | if not IsInt(hi.typ) then MarkAt(hi.pos, 'an index is an integer, found ' + What(hi)); |
| 672 | 682 | if x.vol then MarkAt(x.pos, 'can''t make a slice through a volatile pointer'); |
| 683 | + if x.odd then MarkAt(x.pos, 'can''t make a slice of a field of a packed record'); | |
| 673 | 684 | Value(hi); |
| 674 | 685 | MovRR(RDX, RAX); |
| 675 | 686 | PopR(RCX); |
| @@ -1243,6 +1254,7 @@ | ||
| 1243 | 1254 | c.isF[c.n] := (p^.typ^.form = fReal) and not p^.isVar; |
| 1244 | 1255 | if p^.isVar then begin |
| 1245 | 1256 | if (a.mode <> mVar) or not a.isVar then MarkAt(a.pos, 'a var parameter needs a variable'); |
| 1257 | + if a.odd then MarkAt(a.pos, 'a field of a packed record can''t be a var parameter'); | |
| 1246 | 1258 | if not Same(a.typ, p^.typ) then MarkAt(a.pos, 'a var parameter needs exactly ' + TypeName(p^.typ) + ', found ' + What(a)); |
| 1247 | 1259 | if a.vol then MarkAt(a.pos, 'can''t pass through a volatile pointer to a var parameter'); |
| 1248 | 1260 | Address(a, RAX); |
| @@ -778,6 +778,7 @@ | ||
| 778 | 778 | bAdr: begin |
| 779 | 779 | Expression(y, nil); |
| 780 | 780 | if (y.mode <> mVar) or not y.isVar then MarkAt(y.pos, 'ADR needs a variable'); |
| 781 | + if y.odd then MarkAt(y.pos, 'a field of a packed record has no address'); | |
| 781 | 782 | t := NewPtr(y.typ, y.vol); |
| 782 | 783 | Address(y, RAX); |
| 783 | 784 | MakeConst(x, t, 0); |
| @@ -31,5 +31,20 @@ | ||
| 31 | 31 | s as a decimal number, with a - or + before it if any, and nothing |
| 32 | 32 | else; false if it isn't one, or an I64 can't hold it. |
| 33 | 33 | |
| 34 | +proc le*(s: []U8): U64; | |
| 35 | + The bytes of s, at most 8, as an unsigned number, the first byte | |
| 36 | + the lowest: how a file or a message in little-endian order is read, | |
| 37 | + whatever the machine's own order. | |
| 38 | + | |
| 39 | +proc be*(s: []U8): U64; | |
| 40 | + As le, the first byte the highest: big-endian, network order. | |
| 41 | + | |
| 42 | +proc putLE*(s: []U8; v: U64); | |
| 43 | + v into the bytes of s, at most 8, the lowest first; what doesn't | |
| 44 | + fit is left out. | |
| 45 | + | |
| 46 | +proc putBE*(s: []U8; v: U64); | |
| 47 | + As putLE, the highest first. | |
| 48 | + | |
| 34 | 49 | proc main*; |
| 35 | 50 | The module's test: stops the program if something is wrong. |
| @@ -0,0 +1,13 @@ | ||
| 1 | +test/err/PackedAddress.raz:15:14: error: a field of a packed record has no address | |
| 2 | + 15 | p := ADR(h.len); | |
| 3 | + | ^^^^^ | |
| 4 | +test/err/PackedAddress.raz:16:9: error: a field of a packed record can't be a var parameter | |
| 5 | + 16 | put(h.len); | |
| 6 | + | ^^^^^ | |
| 7 | +test/err/PackedAddress.raz:17:10: error: can't make a slice of a field of a packed record | |
| 8 | + 17 | s := h.vals[0..1]; | |
| 9 | + | ^^^^^^ | |
| 10 | +test/err/PackedAddress.raz:19:5: error: a field of a packed record can't be volatile | |
| 11 | + 19 | q^.len := 2; | |
| 12 | + | ^^ | |
| 13 | +raz: 4 errors |
| @@ -0,0 +1,24 @@ | ||
| 1 | +module PackedAddress; | |
| 2 | +(* A field of a packed record may be misaligned: it has no address, | |
| 3 | + and no volatile access *) | |
| 4 | +type | |
| 5 | + Header = packed record kind: U8; len: U32; tag: U8; vals: [2]U16; end; | |
| 6 | + | |
| 7 | +proc put(var n: U32); | |
| 8 | +begin | |
| 9 | + n := 1; | |
| 10 | +end; | |
| 11 | + | |
| 12 | +proc main*; | |
| 13 | +var h: Header; p: ^U32; q: ^VOLATILE Header; k: ^U8; s: []U16; | |
| 14 | +begin | |
| 15 | + p := ADR(h.len); | |
| 16 | + put(h.len); | |
| 17 | + s := h.vals[0..1]; | |
| 18 | + q := ADR(h); | |
| 19 | + q^.len := 2; | |
| 20 | + k := ADR(h.tag); | |
| 21 | + h.len := 3; | |
| 22 | +end; | |
| 23 | + | |
| 24 | +end PackedAddress. |
| @@ -0,0 +1,2 @@ | ||
| 1 | +14 7 DEADBEF0 -5 9 | |
| 2 | +240 |
| @@ -0,0 +1,23 @@ | ||
| 1 | +module PackedFields; | |
| 2 | +(* A packed record's fields are read and written wherever they sit *) | |
| 3 | +import Out; | |
| 4 | + | |
| 5 | +type | |
| 6 | + Header = packed record kind: U8; len: U32; big: I64; tag: U8; end; | |
| 7 | + | |
| 8 | +proc main*; | |
| 9 | +var h, g: Header; b: ^U8; | |
| 10 | +begin | |
| 11 | + h.kind := 7; h.len := 0DEADBEEFH; h.big := -5; h.tag := 9; | |
| 12 | + INC(h.len); | |
| 13 | + g := h; | |
| 14 | + Out.int(SIZE(Header)); Out.str(" "); | |
| 15 | + Out.int(I64(g.kind)); Out.str(" "); | |
| 16 | + Out.hex(U64(g.len), 8); Out.str(" "); | |
| 17 | + Out.int(g.big); Out.str(" "); | |
| 18 | + Out.int(I64(g.tag)); Out.ln; | |
| 19 | + b := ADR(g.kind); | |
| 20 | + Out.int(I64(b[1])); Out.ln; | |
| 21 | +end; | |
| 22 | + | |
| 23 | +end PackedFields. |