From 76f10fb80fee3c02dcca5d63ee9e06b0c1905b55 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Sun, 11 Oct 2026 20:49:49 +0000 Subject: [PATCH] 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. --- diff --git a/doc/compiler.md b/doc/compiler.md index ebaae5a..00f3935 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -12,8 +12,8 @@ | `src/scan.pas` | 851 | 722 | Symbols, literals, comments, positions | | `src/sym.pas` | 593 | 494 | Types, objects, scopes, layout | | `src/x64.pas` | 693 | 557 | Code buffer, chunks, fixups, places, instruction encoder | -| `src/gen.pas` | 1,619 | 1,398 | Items: loading, storing, copying, frames, calls | -| `src/parse.pas` | 2,443 | 2,202 | Declarations, statements, expressions, built-ins, recovery, warnings | +| `src/gen.pas` | 1,631 | 1,408 | Items: loading, storing, copying, frames, calls | +| `src/parse.pas` | 2,444 | 2,203 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/link.pas` | 180 | 139 | Tree shaking, layout, patching, the places | | `src/elf.pas` | 425 | 352 | Static and dynamic ELF, symbol table, line numbers | | `src/raz.pas` | 227 | 179 | The command line, the entry points | diff --git a/doc/raz.md b/doc/raz.md index 51e4040..29628b6 100644 --- a/doc/raz.md +++ b/doc/raz.md @@ -354,10 +354,21 @@ size is then 0. A `packed record` has no padding: each field starts at the byte after -the field before it, and the record's alignment is 1. A field of a -packed record may be misaligned. x86-64 reads and writes it anyway, but -an atomic operation on it is slow, and a volatile access to it is one -instruction that needn't be atomic. +the field before it, and the record's alignment is 1. So a field of a +packed record may sit at any address. It is read and written like any +other, on every target, even one whose machine can't reach a +misaligned word in one access: the compiled code reaches it in parts +there. But a field of a packed record whose type is aligned to more +than 1 byte, and anything inside it, has no address: `ADR` of it, a +`var` parameter of it and a slice of it are errors. Nor can it be +reached through a `^VOLATILE` pointer, since a misaligned access can't +be promised as one access (section 7.6). Its value can be copied out +and back in. + +The bytes of a value of more than one byte are in the target's order +(appendix A). A file or a message in an order of its own is read and +written a byte at a time, as `Str.le`, `Str.be`, `Str.putLE` and +`Str.putBE` do (section 11). ### 5.6 Variants @@ -1068,6 +1079,7 @@ ``` const stdin* = 0; stdout* = 1; stderr* = 2; + outSize* = 4096; (* the bytes Out keeps; each target has its own *) (* signals, by their numbers on Linux; each target has its own *) sigHup* = 1; sigInt* = 2; sigQuit* = 3; sigKill* = 9; sigPipe* = 13; sigTerm* = 15; sigChld* = 17; sigCont* = 18; sigStop* = 19; @@ -1277,6 +1289,10 @@ proc encode*(c: I64): ([4]U8, I64); (* c in UTF-8: its bytes, and how many *) proc decode*(s: []U8): (I64, I64); (* the character s starts with, and its length in bytes *) proc toInt*(s: []U8): (I64, BOOL); (* s, in decimal; false if it isn't a number an I64 holds *) +proc le*(s: []U8): U64; (* s, at most 8 bytes, the first the lowest *) +proc be*(s: []U8): U64; (* s, at most 8 bytes, the first the highest *) +proc putLE*(s: []U8; v: U64); (* v into s, the lowest byte first *) +proc putBE*(s: []U8; v: U64); (* v into s, the highest byte first *) module Map(K, V; hash: proc(k: K): U64; eq: proc(a, b: K): BOOL); proc remove*(m: ^Map; k: K): BOOL; (* false if k wasn't there *) @@ -1306,6 +1322,16 @@ one rightly, and 0 and 0 for an empty `s`. `toInt` reads a decimal number, with a `-` or `+` before it if any, and nothing else. +`le` and `be` read an unsigned number of up to 8 bytes in a fixed +order, little-endian or big-endian, whatever the machine's own, and +`putLE` and `putBE` write one; more than 8 bytes stops the program. +They are how a file format or a network protocol is read and written: + +``` +len := U32(Str.le(header[4..8])); +Str.putBE(packet[0..2], U64(port)); +``` + `Map.next` walks a map: from 0, it gives the next entry and the number to pass for the one after it, and -1 when there are no more. A walk sees each entry once if the map doesn't change during it: @@ -1455,7 +1481,8 @@ name with `=` in it, a letter `-`, and `-h` or `--help` in the table, since they are `Opt`'s. -**Out and C's stdio.** `Out` keeps a buffer of 4 KB. It writes it out +**Out and C's stdio.** `Out` keeps a buffer of `Sys.outSize` bytes, +4 KB on Linux. It writes it out when it is full, at each line feed when standard output is a terminal, at `Out.flush`, and when the program ends by `Rt.exit`. C's `stdio` keeps a buffer of its own for the same standard output. A program that @@ -1526,7 +1553,11 @@ the library's module names are kept for it (section 3); a record, variant or enumeration is a type of its own wherever it is written (section 5.10); a `nil` pointer, a division by zero and a stack - overflow stop the program on every target (section 10). Mended: + overflow stop the program on every target (section 10); byte order + is the target's, and `Str` reads and writes a fixed one (sections + 5.5 and 11); `Out`'s buffer is `Sys.outSize` bytes (section 11). + Changed: a field of a packed record that may be misaligned has no + address and no volatile access (section 5.5). Mended: arithmetic on untyped constants that overflows is an error (section 2); a slice of a pointer can't run backwards (section 6.4). Ordinary accesses are no longer promised to be made exactly as written, only @@ -1548,7 +1579,11 @@ - **What is the target's.** Code procedures (section 8) hold the target's machine code, and exist only where the implementation says so; `SYSCALL` exists only on Linux targets (section 6.8). A record's - layout is the target's C layout (section 5.5). + layout is the target's C layout (section 5.5), and the order of a + value's bytes is the target's (little-endian on x86-64). +- **Sizes in the library.** How many bytes `Out` keeps before it + writes them, `Sys.outSize` (section 11): 4 KB on Linux, fewer on a + small machine. - **The executable.** Its format, and the names and line numbers it carries for a debugger (section 10). - **How a failure is caught.** A `nil` pointer, a division by zero or diff --git a/lib/Out.raz b/lib/Out.raz index ce3937a..e0b3c18 100644 --- a/lib/Out.raz +++ b/lib/Out.raz @@ -8,7 +8,7 @@ import Rt, Sys, Lock; var - buf: [4096]U8; + buf: [Sys.outSize]U8; n: I64; (* bytes in buf *) tty: BOOL; (* standard output is a terminal *) lock: Lock.Lock; diff --git a/lib/Str.raz b/lib/Str.raz index 324b9d8..d6dd5a2 100644 --- a/lib/Str.raz +++ b/lib/Str.raz @@ -129,6 +129,58 @@ return n, true; end; +(* The bytes of s, at most 8, as an unsigned number, the first byte + the lowest: how a file or a message in little-endian order is read, + whatever the machine's own order. *) +proc le*(s: []U8): U64; +var v: U64; i: I64; +begin + if LEN(s) > 8 then Rt.stop("Str.le: more than 8 bytes"); end; + for i := LEN(s) - 1 to 0 by -1 do v := v shl 8 | U64(s[i]); end; + return v; +end; + +(* As le, the first byte the highest: big-endian, network order. *) +proc be*(s: []U8): U64; +var v: U64; i: I64; +begin + if LEN(s) > 8 then Rt.stop("Str.be: more than 8 bytes"); end; + for i := 0 to LEN(s) - 1 do v := v shl 8 | U64(s[i]); end; + return v; +end; + +(* v into the bytes of s, at most 8, the lowest first; what doesn't + fit is left out. *) +proc putLE*(s: []U8; v: U64); +var i: I64; +begin + if LEN(s) > 8 then Rt.stop("Str.putLE: more than 8 bytes"); end; + for i := 0 to LEN(s) - 1 do s[i] := U8(v); v := v shr 8; end; +end; + +(* As putLE, the highest first. *) +proc putBE*(s: []U8; v: U64); +var i: I64; +begin + if LEN(s) > 8 then Rt.stop("Str.putBE: more than 8 bytes"); end; + for i := LEN(s) - 1 to 0 by -1 do s[i] := U8(v); v := v shr 8; end; +end; + +proc testOrder; +var b: [8]U8; +begin + putLE(b[0..4], 12345678H); + if (b[0] # 78H) or (b[3] # 12H) or (le(b[0..4]) # 12345678H) or (be(b[0..2]) # 7856H) then + Rt.stop("Str.putLE"); + end; + putBE(b, 0102030405060708H); + if (b[0] # 1) or (b[7] # 8) or (be(b) # 0102030405060708H) or (le(b[6..8]) # 0807H) then + Rt.stop("Str.putBE"); + end; + putBE(b[0..2], 0ABCDH); + if (be(b[0..2]) # 0ABCDH) or (le(b[0..0]) # 0) then Rt.stop("Str.be"); end; +end; + proc testUtf8; var b: [4]U8; n, c, i: I64; begin @@ -214,6 +266,7 @@ if not hasPrefix("hello", "he") or hasPrefix("he", "hello") then Rt.stop("Str.hasPrefix"); end; testUtf8; testToInt; + testOrder; end; end Str. diff --git a/lib/Sys.linux-x86_64.raz b/lib/Sys.linux-x86_64.raz index 0ba9751..3c01069 100644 --- a/lib/Sys.linux-x86_64.raz +++ b/lib/Sys.linux-x86_64.raz @@ -6,6 +6,9 @@ stdin* = 0; stdout* = 1; stderr* = 2; + (* the bytes Out keeps before it writes them; a target with little + memory keeps fewer *) + outSize* = 4096; (* signals, by their numbers on Linux *) sigHup* = 1; sigInt* = 2; sigQuit* = 3; sigKill* = 9; sigPipe* = 13; sigTerm* = 15; sigChld* = 17; sigCont* = 18; sigStop* = 19; diff --git a/lib/Sys.raz b/lib/Sys.raz index d188553..248228d 100644 --- a/lib/Sys.raz +++ b/lib/Sys.raz @@ -15,6 +15,9 @@ stdin* = 0; stdout* = 1; stderr* = 2; + (* the bytes Out keeps before it writes them; a target with little + memory keeps fewer *) + outSize* = 4096; (* signals, by their numbers on Linux; another target has its own *) sigHup* = 1; sigInt* = 2; sigQuit* = 3; sigKill* = 9; sigPipe* = 13; sigTerm* = 15; sigChld* = 17; sigCont* = 18; sigStop* = 19; diff --git a/src/gen.pas b/src/gen.pas index 8ec34d6..fb4d2a7 100644 --- a/src/gen.pas +++ b/src/gen.pas @@ -43,6 +43,7 @@ chunk: longint; { mVar with bGlobal, mStr: the chunk } vol: boolean; { mVar: reached through ^VOLATILE } isVar: boolean; { mVar: a variable of the program, not a temporary } + odd: boolean; { mVar: in a packed record, so maybe misaligned } obj: PObj; { mProc, mBuiltin, mCase } pos: TPos; { where it is in the source, for messages } end; @@ -514,6 +515,7 @@ x.typ := t else if (s^.form = fArray) and (t^.form = fSlice) and Same(s^.base, t^.base) then begin if x.vol then MarkAt(x.pos, 'can''t make a slice through a volatile pointer'); + if x.odd then MarkAt(x.pos, 'can''t make a slice of a field of a packed record'); Address(x, RAX); SliceOf(x, t, s^.len); end else @@ -594,11 +596,19 @@ if t^.base = nil then MarkAt(x.pos, TypeName(t) + ' points to a type never declared'); Value(x); x.mode := mVar; x.base := bReg; x.reg := RAX; x.off := 0; - x.typ := t^.base; x.vol := t^.vol; x.isVar := true; + x.typ := t^.base; x.vol := t^.vol; x.isVar := true; x.odd := false; end; +{ A field of a packed record, of more than one byte's alignment, may + sit anywhere; it is read and written, but has no address and no + volatile access, which a machine that can't reach a misaligned word + couldn't make one access (report section 5.5). } procedure Field(var x: TItem; f: PObj); begin + if x.typ^.packd and (f^.typ^.align > 1) then begin + if x.vol then MarkAt(x.pos, 'a field of a packed record can''t be volatile'); + x.odd := true; + end; x.off := x.off + f^.val; x.typ := f^.typ; end; @@ -627,7 +637,7 @@ PopR(RCX); Alu(aluAdd, RAX, RCX); x.mode := mVar; x.base := bReg; x.reg := RAX; x.off := 0; - x.vol := t^.vol; x.isVar := true; + x.vol := t^.vol; x.isVar := true; x.odd := false; end else if (t^.form = fArray) and (y.mode = mConst) then begin 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)); x.off := x.off + y.val * s; @@ -670,6 +680,7 @@ s := t^.base^.size; if not IsInt(hi.typ) then MarkAt(hi.pos, 'an index is an integer, found ' + What(hi)); if x.vol then MarkAt(x.pos, 'can''t make a slice through a volatile pointer'); + if x.odd then MarkAt(x.pos, 'can''t make a slice of a field of a packed record'); Value(hi); MovRR(RDX, RAX); PopR(RCX); @@ -1243,6 +1254,7 @@ c.isF[c.n] := (p^.typ^.form = fReal) and not p^.isVar; if p^.isVar then begin if (a.mode <> mVar) or not a.isVar then MarkAt(a.pos, 'a var parameter needs a variable'); + if a.odd then MarkAt(a.pos, 'a field of a packed record can''t be a var parameter'); if not Same(a.typ, p^.typ) then MarkAt(a.pos, 'a var parameter needs exactly ' + TypeName(p^.typ) + ', found ' + What(a)); if a.vol then MarkAt(a.pos, 'can''t pass through a volatile pointer to a var parameter'); Address(a, RAX); diff --git a/src/parse.pas b/src/parse.pas index b5d8c24..6347aef 100644 --- a/src/parse.pas +++ b/src/parse.pas @@ -778,6 +778,7 @@ bAdr: begin Expression(y, nil); if (y.mode <> mVar) or not y.isVar then MarkAt(y.pos, 'ADR needs a variable'); + if y.odd then MarkAt(y.pos, 'a field of a packed record has no address'); t := NewPtr(y.typ, y.vol); Address(y, RAX); MakeConst(x, t, 0); diff --git a/test/doc/Str.expect b/test/doc/Str.expect index 9522e08..b26d269 100644 --- a/test/doc/Str.expect +++ b/test/doc/Str.expect @@ -31,5 +31,20 @@ s as a decimal number, with a - or + before it if any, and nothing else; false if it isn't one, or an I64 can't hold it. +proc le*(s: []U8): U64; + The bytes of s, at most 8, as an unsigned number, the first byte + the lowest: how a file or a message in little-endian order is read, + whatever the machine's own order. + +proc be*(s: []U8): U64; + As le, the first byte the highest: big-endian, network order. + +proc putLE*(s: []U8; v: U64); + v into the bytes of s, at most 8, the lowest first; what doesn't + fit is left out. + +proc putBE*(s: []U8; v: U64); + As putLE, the highest first. + proc main*; The module's test: stops the program if something is wrong. diff --git a/test/err/PackedAddress.err b/test/err/PackedAddress.err new file mode 100644 index 0000000..ff62129 --- /dev/null +++ b/test/err/PackedAddress.err @@ -0,0 +1,13 @@ +test/err/PackedAddress.raz:15:14: error: a field of a packed record has no address + 15 | p := ADR(h.len); + | ^^^^^ +test/err/PackedAddress.raz:16:9: error: a field of a packed record can't be a var parameter + 16 | put(h.len); + | ^^^^^ +test/err/PackedAddress.raz:17:10: error: can't make a slice of a field of a packed record + 17 | s := h.vals[0..1]; + | ^^^^^^ +test/err/PackedAddress.raz:19:5: error: a field of a packed record can't be volatile + 19 | q^.len := 2; + | ^^ +raz: 4 errors diff --git a/test/err/PackedAddress.raz b/test/err/PackedAddress.raz new file mode 100644 index 0000000..d226bbb --- /dev/null +++ b/test/err/PackedAddress.raz @@ -0,0 +1,24 @@ +module PackedAddress; +(* A field of a packed record may be misaligned: it has no address, + and no volatile access *) +type + Header = packed record kind: U8; len: U32; tag: U8; vals: [2]U16; end; + +proc put(var n: U32); +begin + n := 1; +end; + +proc main*; +var h: Header; p: ^U32; q: ^VOLATILE Header; k: ^U8; s: []U16; +begin + p := ADR(h.len); + put(h.len); + s := h.vals[0..1]; + q := ADR(h); + q^.len := 2; + k := ADR(h.tag); + h.len := 3; +end; + +end PackedAddress. diff --git a/test/run/PackedFields.expect b/test/run/PackedFields.expect new file mode 100644 index 0000000..a2757ae --- /dev/null +++ b/test/run/PackedFields.expect @@ -0,0 +1,2 @@ +14 7 DEADBEF0 -5 9 +240 diff --git a/test/run/PackedFields.raz b/test/run/PackedFields.raz new file mode 100644 index 0000000..9e8463a --- /dev/null +++ b/test/run/PackedFields.raz @@ -0,0 +1,23 @@ +module PackedFields; +(* A packed record's fields are read and written wherever they sit *) +import Out; + +type + Header = packed record kind: U8; len: U32; big: I64; tag: U8; end; + +proc main*; +var h, g: Header; b: ^U8; +begin + h.kind := 7; h.len := 0DEADBEEFH; h.big := -5; h.tag := 9; + INC(h.len); + g := h; + Out.int(SIZE(Header)); Out.str(" "); + Out.int(I64(g.kind)); Out.str(" "); + Out.hex(U64(g.len), 8); Out.str(" "); + Out.int(g.big); Out.str(" "); + Out.int(I64(g.tag)); Out.ln; + b := ADR(g.kind); + Out.int(I64(b[1])); Out.ln; +end; + +end PackedFields.