raz / commit 76f10fb8

commit
76f10fb80fee3c02dcca5d63ee9e06b0c1905b55
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
2056ec68
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.

patch · log

13 files changed, +196 -12

doc/compiler.md (history) modified +2 -2

@@ -12,8 +12,8 @@
1212 | `src/scan.pas` | 851 | 722 | Symbols, literals, comments, positions |
1313 | `src/sym.pas` | 593 | 494 | Types, objects, scopes, layout |
1414 | `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 |
1717 | `src/link.pas` | 180 | 139 | Tree shaking, layout, patching, the places |
1818 | `src/elf.pas` | 425 | 352 | Static and dynamic ELF, symbol table, line numbers |
1919 | `src/raz.pas` | 227 | 179 | The command line, the entry points |

doc/raz.md (history) modified +42 -7

@@ -354,10 +354,21 @@
354354 size is then 0.
355355
356356 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).
361372
362373 ### 5.6 Variants
363374
@@ -1068,6 +1079,7 @@
10681079 ```
10691080 const
10701081 stdin* = 0; stdout* = 1; stderr* = 2;
1082+ outSize* = 4096; (* the bytes Out keeps; each target has its own *)
10711083 (* signals, by their numbers on Linux; each target has its own *)
10721084 sigHup* = 1; sigInt* = 2; sigQuit* = 3; sigKill* = 9; sigPipe* = 13;
10731085 sigTerm* = 15; sigChld* = 17; sigCont* = 18; sigStop* = 19;
@@ -1277,6 +1289,10 @@
12771289 proc encode*(c: I64): ([4]U8, I64); (* c in UTF-8: its bytes, and how many *)
12781290 proc decode*(s: []U8): (I64, I64); (* the character s starts with, and its length in bytes *)
12791291 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 *)
12801296
12811297 module Map(K, V; hash: proc(k: K): U64; eq: proc(a, b: K): BOOL);
12821298 proc remove*(m: ^Map; k: K): BOOL; (* false if k wasn't there *)
@@ -1306,6 +1322,16 @@
13061322 one rightly, and 0 and 0 for an empty `s`. `toInt` reads a decimal
13071323 number, with a `-` or `+` before it if any, and nothing else.
13081324
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+
13091335 `Map.next` walks a map: from 0, it gives the next entry and the number
13101336 to pass for the one after it, and -1 when there are no more. A walk
13111337 sees each entry once if the map doesn't change during it:
@@ -1455,7 +1481,8 @@
14551481 name with `=` in it, a letter `-`, and `-h` or `--help` in the table,
14561482 since they are `Opt`'s.
14571483
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
14591486 when it is full, at each line feed when standard output is a terminal,
14601487 at `Out.flush`, and when the program ends by `Rt.exit`. C's `stdio`
14611488 keeps a buffer of its own for the same standard output. A program that
@@ -1526,7 +1553,11 @@
15261553 the library's module names are kept for it (section 3); a record,
15271554 variant or enumeration is a type of its own wherever it is written
15281555 (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:
15301561 arithmetic on untyped constants that overflows is an error (section
15311562 2); a slice of a pointer can't run backwards (section 6.4). Ordinary
15321563 accesses are no longer promised to be made exactly as written, only
@@ -1548,7 +1579,11 @@
15481579 - **What is the target's.** Code procedures (section 8) hold the
15491580 target's machine code, and exist only where the implementation says
15501581 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.
15521587 - **The executable.** Its format, and the names and line numbers it
15531588 carries for a debugger (section 10).
15541589 - **How a failure is caught.** A `nil` pointer, a division by zero or

lib/Out.raz (history) modified +1 -1

@@ -8,7 +8,7 @@
88 import Rt, Sys, Lock;
99
1010 var
11- buf: [4096]U8;
11+ buf: [Sys.outSize]U8;
1212 n: I64; (* bytes in buf *)
1313 tty: BOOL; (* standard output is a terminal *)
1414 lock: Lock.Lock;

lib/Str.raz (history) modified +53 -0

@@ -129,6 +129,58 @@
129129 return n, true;
130130 end;
131131
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+
132184 proc testUtf8;
133185 var b: [4]U8; n, c, i: I64;
134186 begin
@@ -214,6 +266,7 @@
214266 if not hasPrefix("hello", "he") or hasPrefix("he", "hello") then Rt.stop("Str.hasPrefix"); end;
215267 testUtf8;
216268 testToInt;
269+ testOrder;
217270 end;
218271
219272 end Str.

lib/Sys.linux-x86_64.raz (history) modified +3 -0

@@ -6,6 +6,9 @@
66 stdin* = 0;
77 stdout* = 1;
88 stderr* = 2;
9+ (* the bytes Out keeps before it writes them; a target with little
10+ memory keeps fewer *)
11+ outSize* = 4096;
912 (* signals, by their numbers on Linux *)
1013 sigHup* = 1; sigInt* = 2; sigQuit* = 3; sigKill* = 9; sigPipe* = 13;
1114 sigTerm* = 15; sigChld* = 17; sigCont* = 18; sigStop* = 19;

lib/Sys.raz (history) modified +3 -0

@@ -15,6 +15,9 @@
1515 stdin* = 0;
1616 stdout* = 1;
1717 stderr* = 2;
18+ (* the bytes Out keeps before it writes them; a target with little
19+ memory keeps fewer *)
20+ outSize* = 4096;
1821 (* signals, by their numbers on Linux; another target has its own *)
1922 sigHup* = 1; sigInt* = 2; sigQuit* = 3; sigKill* = 9; sigPipe* = 13;
2023 sigTerm* = 15; sigChld* = 17; sigCont* = 18; sigStop* = 19;

src/gen.pas modified +14 -2

@@ -43,6 +43,7 @@
4343 chunk: longint; { mVar with bGlobal, mStr: the chunk }
4444 vol: boolean; { mVar: reached through ^VOLATILE }
4545 isVar: boolean; { mVar: a variable of the program, not a temporary }
46+ odd: boolean; { mVar: in a packed record, so maybe misaligned }
4647 obj: PObj; { mProc, mBuiltin, mCase }
4748 pos: TPos; { where it is in the source, for messages }
4849 end;
@@ -514,6 +515,7 @@
514515 x.typ := t
515516 else if (s^.form = fArray) and (t^.form = fSlice) and Same(s^.base, t^.base) then begin
516517 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');
517519 Address(x, RAX);
518520 SliceOf(x, t, s^.len);
519521 end else
@@ -594,11 +596,19 @@
594596 if t^.base = nil then MarkAt(x.pos, TypeName(t) + ' points to a type never declared');
595597 Value(x);
596598 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;
598600 end;
599601
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). }
600606 procedure Field(var x: TItem; f: PObj);
601607 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;
602612 x.off := x.off + f^.val;
603613 x.typ := f^.typ;
604614 end;
@@ -627,7 +637,7 @@
627637 PopR(RCX);
628638 Alu(aluAdd, RAX, RCX);
629639 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;
631641 end else if (t^.form = fArray) and (y.mode = mConst) then begin
632642 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));
633643 x.off := x.off + y.val * s;
@@ -670,6 +680,7 @@
670680 s := t^.base^.size;
671681 if not IsInt(hi.typ) then MarkAt(hi.pos, 'an index is an integer, found ' + What(hi));
672682 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');
673684 Value(hi);
674685 MovRR(RDX, RAX);
675686 PopR(RCX);
@@ -1243,6 +1254,7 @@
12431254 c.isF[c.n] := (p^.typ^.form = fReal) and not p^.isVar;
12441255 if p^.isVar then begin
12451256 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');
12461258 if not Same(a.typ, p^.typ) then MarkAt(a.pos, 'a var parameter needs exactly ' + TypeName(p^.typ) + ', found ' + What(a));
12471259 if a.vol then MarkAt(a.pos, 'can''t pass through a volatile pointer to a var parameter');
12481260 Address(a, RAX);

src/parse.pas modified +1 -0

@@ -778,6 +778,7 @@
778778 bAdr: begin
779779 Expression(y, nil);
780780 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');
781782 t := NewPtr(y.typ, y.vol);
782783 Address(y, RAX);
783784 MakeConst(x, t, 0);

test/doc/Str.expect (history) modified +15 -0

@@ -31,5 +31,20 @@
3131 s as a decimal number, with a - or + before it if any, and nothing
3232 else; false if it isn't one, or an I64 can't hold it.
3333
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+
3449 proc main*;
3550 The module's test: stops the program if something is wrong.

test/err/PackedAddress.err added +13 -0

@@ -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

test/err/PackedAddress.raz added +24 -0

@@ -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.

test/run/PackedFields.expect added +2 -0

@@ -0,0 +1,2 @@
1+14 7 DEADBEF0 -5 9
2+240

test/run/PackedFields.raz added +23 -0

@@ -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.