raz / commit 61fa4786

commit
61fa4786089d5f85adf9ccc33a5c5a1f846a4b58
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
280d4d6f
ROL and ROR: rotations, as report 2026-10.35 has them

The built-ins ROL(x, n) and ROR(x, n): Sym enters them, Parse's
builtin reads them, Item's rotand checks x (an unsigned integer type;
an untyped constant is refused, with help to give it a type) and loads
it before n, rotate folds two constants (in a constant too) or has
Gen.rotate emit rol/ror, by cl or an imm8, through X64.rotate.

The rotation is done at x's own width (rol al, rol ax, rol eax), so
the bits leaving a U8 come back in at its other end. The count needs
no mask: x86 takes it modulo 32 (64 for a quadword) and a byte's or
a word's rotation then goes round modulo 8 or 16 itself, which is n
modulo the width; a constant count is cut to the width.

Tests: conform/run/Rotate (each type, n = 0, = width, > width,
negative and signed counts, variable and constant forms, constants
made by ROL); conform/err Rotate{Untyped,Signed,Real,Count,Args},
their messages in test/msg. The compiler follows report 2026-10.35;
compiler.md, the editors' built-in lists and CLAUDE.md's readings
say so. Code lines 11,662 -> 11,737.

patch · log

23 files changed, +286 -13

CLAUDE.md modified +7 -0

@@ -332,3 +332,10 @@
332332 type there, and its help says it would be `nil`. A slice where its
333333 element's pointer is wanted, and a pointer where a slice is, are
334334 errors whose help names `PTR(s)` and `LEN(s)`, or `p[0..n]`.
335+- `ROL(x, n)` and `ROR(x, n)` (report 2026-10.35) are done at `x`'s
336+ own width, `rol al, cl` for a `U8`, with no mask of the count: x86
337+ takes it modulo 32 or 64, and a byte's or a word's rotation goes
338+ round modulo 8 or 16 itself. A negative count is taken modulo the
339+ width as a shift's is, `n & (w - 1)`: `ROL(x, -1)` is `ROR(x, 1)`.
340+ The result is a built-in's, not an operator's, and widens (`made`
341+ is false). Of two constants they fold, in a constant too.

PROPOSALS.md modified +1 -1

@@ -918,7 +918,7 @@
918918 procedure, would serve SHA, the other hashes and checksums without
919919 any inlining, but it is a language change, for a thaw. **Taken into
920920 the report (2026-10.35):** the built-ins `ROL(x, n)` and `ROR(x, n)`,
921-by the owner's word, for review; the compiler is next. rcrypto met the
921+by the owner's word; the compiler has them. rcrypto met the
922922 same (its `RAZ-NOTES.md`): SHA-256 at 32 MB/s with `ror` a procedure,
923923 80 with it written out. On 100 MB of
924924 new files `sauce commit` takes 1.2 s against the Pascal sauce's 0.7,

conform/err/RotateArgs.raz (history) added +8 -0

@@ -0,0 +1,8 @@
1+module RotateArgs;
2+proc main*;
3+var x: U16;
4+begin
5+ x := 1;
6+ x := ROL(x);
7+end;
8+end RotateArgs.

conform/err/RotateCount.raz (history) added +8 -0

@@ -0,0 +1,8 @@
1+module RotateCount;
2+proc main*;
3+var x: U8;
4+begin
5+ x := 1;
6+ x := ROR(x, 1.5);
7+end;
8+end RotateCount.

conform/err/RotateReal.raz (history) added +8 -0

@@ -0,0 +1,8 @@
1+module RotateReal;
2+proc main*;
3+var x: F64;
4+begin
5+ x := 1.0;
6+ x := ROL(x, 1);
7+end;
8+end RotateReal.

conform/err/RotateSigned.raz (history) added +8 -0

@@ -0,0 +1,8 @@
1+module RotateSigned;
2+proc main*;
3+var x: I32;
4+begin
5+ x := 1;
6+ x := ROR(x, 1);
7+end;
8+end RotateSigned.

conform/err/RotateUntyped.raz (history) added +7 -0

@@ -0,0 +1,7 @@
1+module RotateUntyped;
2+proc main*;
3+var x: U32;
4+begin
5+ x := ROL(1, 3);
6+end;
7+end RotateUntyped.

conform/run/Rotate.expect (history) added +28 -0

@@ -0,0 +1,28 @@
1+03 C0
2+81 81
3+81 81
4+03 C0
5+03 C0
6+C0 03
7+4B 4B
8+0003 C000
9+8001 8001
10+0003 C000
11+4123 2341
12+00000003 C0000000
13+80000001 80000001
14+00000003 C0000000
15+1A2B3C09 F02468AC
16+0000000000000003 C000000000000000
17+8000000000000001 8000000000000001
18+0000000000000003 C000000000000000
19+3456789ABCDEF012 DEF0123456789ABC
20+8000000000000000 0000000000000002
21+0000000000000100 0100000000000000
22+03 C0 C0
23+0003 8001
24+F02468AC 12345678
25+3456789ABCDEF012 F0123456789ABCDE
26+0C 20000000
27+03 C0 C000 F02468AC
28+8 8000000000000001

conform/run/Rotate.raz (history) added +66 -0

@@ -0,0 +1,66 @@
1+(* ROL(x, n) and ROR(x, n) rotate x by n bits modulo its width (report
2+ section 6.1): of each unsigned type, by 0, by the width and past it,
3+ by a negative or signed count, with x and n variables or constants;
4+ and a constant made by ROL. Each line is the left rotation, then the
5+ right one. *)
6+module Rotate;
7+import Out;
8+
9+const
10+ Eight = ROL(U32(1), 3);
11+ Top = ROR(U64(1), 1) | ROL(U64(1), 64);
12+
13+(* the two rotations of x by n, a variable count *)
14+proc u8(x: U8; n: INT);
15+begin
16+ Out.hex(U64(ROL(x, n)), 2); Out.str(" "); Out.hex(U64(ROR(x, n)), 2); Out.ln;
17+end;
18+
19+proc u16(x: U16; n: I8);
20+begin
21+ Out.hex(U64(ROL(x, n)), 4); Out.str(" "); Out.hex(U64(ROR(x, n)), 4); Out.ln;
22+end;
23+
24+proc u32(x: U32; n: U64);
25+begin
26+ Out.hex(U64(ROL(x, n)), 8); Out.str(" "); Out.hex(U64(ROR(x, n)), 8); Out.ln;
27+end;
28+
29+proc u64(x: U64; n: I64);
30+begin
31+ Out.hex(ROL(x, n), 16); Out.str(" "); Out.hex(ROR(x, n), 16); Out.ln;
32+end;
33+
34+proc uint(x: UINT; n: U8);
35+begin
36+ Out.hex(U64(ROL(x, n)), 16); Out.str(" "); Out.hex(U64(ROR(x, n)), 16); Out.ln;
37+end;
38+
39+proc main*;
40+var a: U8; b: U16; c: U32; d: U64; n: INT;
41+begin
42+ u8(81H, 1); u8(81H, 0); u8(81H, 8); u8(81H, 9); u8(81H, 33); u8(81H, -1); u8(0B4H, 4);
43+ u16(8001H, 1); u16(8001H, 16); u16(8001H, 33); u16(1234H, -4);
44+ u32(80000001H, 1); u32(80000001H, 32); u32(80000001H, 33); u32(12345678H, 7);
45+ u64(8000000000000001H, 1); u64(8000000000000001H, 64); u64(8000000000000001H, -63);
46+ u64(0123456789ABCDEFH, 12);
47+ uint(1, 63); uint(1, 200);
48+
49+ (* a constant count, an immediate *)
50+ a := 81H; b := 8001H; c := 12345678H; d := 0123456789ABCDEFH;
51+ Out.hex(U64(ROL(a, 1)), 2); Out.str(" "); Out.hex(U64(ROR(a, 9)), 2); Out.str(" "); Out.hex(U64(ROL(a, -1)), 2); Out.ln;
52+ Out.hex(U64(ROL(b, 17)), 4); Out.str(" "); Out.hex(U64(ROR(b, 0)), 4); Out.ln;
53+ Out.hex(U64(ROR(c, 7)), 8); Out.str(" "); Out.hex(U64(ROL(c, 32)), 8); Out.ln;
54+ Out.hex(ROL(d, 12), 16); Out.str(" "); Out.hex(ROR(d, 68), 16); Out.ln;
55+
56+ (* a constant x, a variable count *)
57+ n := 3;
58+ Out.hex(U64(ROL(U8(81H), n)), 2); Out.str(" "); Out.hex(U64(ROR(U32(1), n)), 8); Out.ln;
59+
60+ (* both constants, folded *)
61+ Out.hex(U64(ROL(U8(81H), 1)), 2); Out.str(" "); Out.hex(U64(ROR(U8(81H), 1)), 2); Out.str(" ");
62+ Out.hex(U64(ROR(U16(8001H), 17)), 4); Out.str(" "); Out.hex(U64(ROR(U32(12345678H), -25)), 8); Out.ln;
63+ Out.int(I64(Eight)); Out.str(" "); Out.hex(Top, 16); Out.ln;
64+end;
65+
66+end Rotate.

doc/compiler.md (history) modified +18 -6

@@ -18,12 +18,12 @@
1818 |---|---|---|---|
1919 | `src/Diag.raz` | 410 | 315 | Messages: positions, source excerpts, colour, counts |
2020 | `src/Scan.raz` | 1,148 | 949 | Symbols, literals, comments, positions |
21-| `src/Sym.raz` | 713 | 571 | Types, objects, scopes, layout |
21+| `src/Sym.raz` | 715 | 573 | Types, objects, scopes, layout |
2222 | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes |
23-| `src/X64.raz` | 1,567 | 1,292 | The instruction encoder, jumps within a procedure, the assembler |
24-| `src/Gen.raz` | 2,561 | 1,824 | The stack machine, for x86-64: values, operations, checks, calls, frames |
25-| `src/Item.raz` | 2,175 | 1,747 | Items: types, constants, variables, operators, conversions, calls |
26-| `src/Parse.raz` | 4,260 | 3,683 | Declarations, statements, expressions, built-ins, recovery, warnings |
23+| `src/X64.raz` | 1,584 | 1,304 | The instruction encoder, jumps within a procedure, the assembler |
24+| `src/Gen.raz` | 2,588 | 1,841 | The stack machine, for x86-64: values, operations, checks, calls, frames |
25+| `src/Item.raz` | 2,223 | 1,785 | Items: types, constants, variables, operators, conversions, calls |
26+| `src/Parse.raz` | 4,266 | 3,689 | Declarations, statements, expressions, built-ins, recovery, warnings |
2727 | `src/Link.raz` | 407 | 315 | Tree shaking, layout, patching, the places |
2828 | `src/Elf.raz` | 449 | 355 | Static and dynamic ELF, symbol table, line numbers |
2929 | `src/Raz.raz` | 417 | 332 | The command line |
@@ -1146,7 +1146,12 @@
11461146 are brought back to their width after each operation, which is how `U8`
11471147 arithmetic wraps; since they are computed in 64 bits, a shift's count is
11481148 cut to their width first, and `divTrap` makes the smallest value of one
1149-divided by -1 fault as an `I64`'s does.
1149+divided by -1 fault as an `I64`'s does. A rotation, `ROL` and `ROR`
1150+(`Gen.rotate`), is the one operation done at its value's own width,
1151+`rol al, cl` and not `rol rax, cl`, so that the bits leaving one end of
1152+a `U8` come back in at its other end; and its count needs no mask, as
1153+x86 takes it modulo 32, or 64, and a byte's or a word's rotation then
1154+goes round modulo 8 or 16 itself, the count modulo the width.
11501155
11511156 Checks: `check` jumps, when the check fails, to code at the end of the
11521157 procedure (`endFails`), so that a check that holds is a jump not taken.
@@ -1617,6 +1622,13 @@
16171622
16181623 ADR(s[0]) check 0 < LEN(s), then s's first word (stops on "")
16191624 PTR(s) s's first word (no check)
1625+
1626+`ROL(x, n)` and `ROR(x, n)` (report 2026-10.35) are Item's `rotand`,
1627+which refuses an `x` not of an unsigned type, an untyped constant with
1628+help that says to give it one, and loads `x` before `n` is read, as
1629+`op1` loads an operator's left; and `rotate`, which folds two constants,
1630+right by k as left by the width less k, or has `Gen.rotate` make `rol`
1631+or `ror`.
16201632
16211633 Two mismatches in `coerce` that people meet on the way to it, a slice
16221634 where its element's pointer is wanted and a pointer where a slice is,

editor/emacs/raz-mode.el (history) modified +1 -1

@@ -33,7 +33,7 @@
3333
3434 (defconst raz-builtins
3535 '("SIZE" "ADR" "LEN" "PTR" "NEW" "MSG" "SYSCALL" "FENCE" "CAS" "XCHG" "XADD"
36- "ARGC" "ARGV" "INC" "DEC" "COPY"))
36+ "ARGC" "ARGV" "INC" "DEC" "COPY" "ROL" "ROR"))
3737
3838 (defvar raz-mode-syntax-table
3939 (let ((st (make-syntax-table)))

editor/vim/syntax/raz.vim (history) modified +1 -1

@@ -15,7 +15,7 @@
1515 syntax keyword razKeyword var variant VOLATILE while xor
1616 syntax keyword razType I8 I16 I32 I64 INT UINT U8 U16 U32 U64 BOOL F32 F64 ERROR
1717 syntax keyword razBuiltin SIZE ADR LEN PTR NEW MSG SYSCALL FENCE CAS XCHG XADD
18-syntax keyword razBuiltin ARGC ARGV INC DEC COPY
18+syntax keyword razBuiltin ARGC ARGV INC DEC COPY ROL ROR
1919 syntax keyword razConstant true false nil
2020
2121 " 123, 0FFH, 1.5, 2.0E-3

src/Gen.raz modified +27 -0

@@ -1038,6 +1038,33 @@
10381038 if o # 0 then putOff(o); end;
10391039 end;
10401040
1041+(* ROL or ROR, by left, of the two values on top: an unsigned integer of
1042+ size bytes, under the count (report section 6.1). x86 takes the count
1043+ of rol and ror modulo 32, or 64 for a quadword, and a byte's or a
1044+ word's rotation then goes round modulo 8 or 16 itself: as 8 and 16
1045+ divide 32, that is the count modulo the width, as the report has it,
1046+ with no mask of the count's own, which a shift needs. A constant
1047+ count is cut to the width here, an imm8; 0 rotates nothing. The bits
1048+ above the width are zero and stay so: rol al and rol ax leave them,
1049+ rol eax clears them. *)
1050+proc rotate*(left: BOOL; size: INT);
1051+var c: BOOL; v: I64; r, y, op: INT;
1052+begin
1053+ if left then op := X.shRol; else op := X.shRor; end;
1054+ c, v := topConst();
1055+ if c then
1056+ drop();
1057+ r := top();
1058+ v := v & I64(size * 8 - 1);
1059+ if v # 0 then X.rotate(op, r, size, INT(v)); end;
1060+ else
1061+ r, y := operands();
1062+ if y # RCX then X.movRR(RCX, y); end;
1063+ X.rotate(op, r, size, -1);
1064+ end;
1065+ stack[depth - 1].k := kWord;
1066+end;
1067+
10411068 (* The left op the right of the two values on top, as a BOOL *)
10421069 proc compare*(sy: Scan.Sym; k: Kind; signed: BOOL);
10431070 var f: BOOL; r, x, y, i: INT; v: I64; c: BOOL; m: X.Mem;

src/Item.raz (history) modified +48 -0

@@ -1512,6 +1512,54 @@
15121512 return Sym.same(t, Sym.tInt) or Sym.same(t, Sym.tUInt);
15131513 end;
15141514
1515+(* The x of ROL(x, n), left, or ROR(x, n), checked: of an unsigned
1516+ integer type, since a rotation goes round within a width, which an
1517+ untyped constant hasn't (report section 6.1). It is loaded, unless a
1518+ constant, before n is read, as the left of an operator is by op1. *)
1519+proc rotand*(var x: Item; left: BOOL): ERROR;
1520+var name, s: []U8;
1521+begin
1522+ if x.mode in {mType, mBuiltin, mNone, mCase, mStr} then return notValue(x); end;
1523+ if left then name := "ROL"; else name := "ROR"; end;
1524+ s := say2("{} needs an unsigned integer, found {}", name, what(x));
1525+ if x.typ = Sym.tConst then
1526+ return Scan.markHelp(x.pos, s,
1527+ say1("an untyped constant has no width to rotate in: give it a type, as in U32({})", Diag.source(x.pos)));
1528+ end;
1529+ if (x.typ^.form # Sym.fInt) or x.typ^.signed then return Scan.markAt(x.pos, s); end;
1530+ if x.mode # mConst then try value(x); end;
1531+ return nil;
1532+end;
1533+
1534+(* ROL(x, n), left, or ROR(x, n): x, of rotand, rotated by n modulo its
1535+ width. Of two constants it is folded: right by k is left by w - k,
1536+ and left by k is x shl k | x shr (w - k), cut to the width. *)
1537+proc rotate*(var x, n: Item; left: BOOL): ERROR;
1538+var w, k: I64; v: U64;
1539+begin
1540+ if n.mode in {mType, mBuiltin, mNone, mCase, mStr} then return notValue(n); end;
1541+ if not Sym.isInt(n.typ) then return Scan.markAt(n.pos, say1("expected an integer, found {}", what(n))); end;
1542+ x.made := false; (* a built-in's result, not an operator's: it widens *)
1543+ if (x.mode = mConst) and (n.mode = mConst) then
1544+ w := bits(x.typ);
1545+ k := n.val & (w - 1);
1546+ if not left then k := (w - k) & (w - 1); end;
1547+ v := U64(x.val);
1548+ if k # 0 then v := v shl k | v shr (w - k); end;
1549+ x.val := cut(I64(v), x.typ);
1550+ return nil;
1551+ end;
1552+ try value(n);
1553+ if x.mode = mConst then
1554+ (* x goes on after n, which rotand didn't load, and under it *)
1555+ try value(x);
1556+ Gen.swap();
1557+ end;
1558+ Gen.rotate(left, intSize(x.typ));
1559+ x.mode := mStack;
1560+ return nil;
1561+end;
1562+
15151563 proc convert*(var x: Item; t: ^Sym.Type): ERROR;
15161564 var s: ^Sym.Type; r: F64;
15171565 begin

src/Parse.raz (history) modified +6 -0

@@ -1085,6 +1085,12 @@
10851085 Item.makeConst(x, Sym.tNone, 0);
10861086 x.mode := Item.mNone;
10871087 | Sym.bNew: try new_(x);
1088+ | Sym.bRol, Sym.bRor:
1089+ try expression(x, nil);
1090+ try Item.rotand(x, which = Sym.bRol);
1091+ try expect(Scan.sComma);
1092+ try expression(y, nil);
1093+ try Item.rotate(x, y, which = Sym.bRol);
10881094 | Sym.bCopy:
10891095 try expression(x, nil);
10901096 try Item.copyDest(x);

src/Raz.raz (history) modified +1 -1

@@ -32,7 +32,7 @@
3232 import Sys, Out, Version, Files, Fmt, Diag, Scan, Sym, Chunk, X64, Gen, Parse, Link, Elf;
3333
3434 const
35- Report = "2026-10.34"; (* the version of doc/raz.md it follows *)
35+ Report = "2026-10.35"; (* the version of doc/raz.md it follows *)
3636 MemfdCreate = 319; (* the system call *)
3737 MfdCloexec = 1; (* an ELF still executes: the kernel opens it first *)
3838 Execve = 59;

src/Sym.raz (history) modified +3 -1

@@ -131,7 +131,7 @@
131131 (* built-in procedures *)
132132 bSize* = 0; bAdr* = 1; bLen* = 2; bNew* = 3; bMsg* = 4; bSyscall* = 5;
133133 bFence* = 6; bCas* = 7; bXchg* = 8; bXadd* = 9; bArgc* = 10; bArgv* = 11;
134- bInc* = 12; bDec* = 13; bCopy* = 14; bPtr* = 15;
134+ bInc* = 12; bDec* = 13; bCopy* = 14; bPtr* = 15; bRol* = 16; bRor* = 17;
135135
136136 MaxDepth* = 64; (* scopes open at once *)
137137
@@ -710,4 +710,6 @@
710710 enter("INC", oBuiltin, nil, bInc);
711711 enter("DEC", oBuiltin, nil, bDec);
712712 enter("COPY", oBuiltin, nil, bCopy);
713+ enter("ROL", oBuiltin, nil, bRol);
714+ enter("ROR", oBuiltin, nil, bRor);
713715 end Sym.

src/X64.raz (history) modified +19 -2

@@ -28,8 +28,8 @@
2828 (* group 1 operations, the /digit of 81 and 83 *)
2929 aluAdd* = 0; aluOr* = 1; aluAnd* = 4; aluSub* = 5; aluXor* = 6; aluCmp* = 7;
3030
31- (* the /digit of D3 and C1 *)
32- shShl* = 4; shShr* = 5; shSar* = 7;
31+ (* the /digit of D3 and C1, and of D2 and C0 for a byte *)
32+ shRol* = 0; shRor* = 1; shShl* = 4; shShr* = 5; shSar* = 7;
3333
3434 NoLabel* = -1;
3535
@@ -513,6 +513,23 @@
513513 if listing then say(text2(ShiftNames[op], rn(r), num(n))); end;
514514 opR(0, true, 0C1H, op, r, false);
515515 Chunk.put1(n);
516+end;
517+
518+(* rol or ror, op, of the size bytes of r, by cl, or by n if n >= 0:
519+ D3 by cl and C1 by an imm8, D2 and C0 for a byte. Unlike a shift, a
520+ rotation is done at its value's own width, rol eax and not rol rax,
521+ so the bits leaving one end come back in at the other end of it. *)
522+proc rotate*(op, r, size, n: INT);
523+var opc: INT;
524+begin
525+ if listing then
526+ if n < 0 then say(text2(ShiftNames[op], rnSized(r, size), "cl"));
527+ else say(text2(ShiftNames[op], rnSized(r, size), num(n))); end;
528+ end;
529+ if n < 0 then opc := 0D2H; else opc := 0C0H; end;
530+ if size > 1 then INC(opc); end;
531+ opR(pfx16(size), size = 8, opc, op, r, size = 1);
532+ if n >= 0 then Chunk.put1(n); end;
516533 end;
517534
518535 (* F7 /ext: 2 not, 3 neg, 6 div, 7 idiv *)

test/msg/RotateArgs.err (history) added +4 -0

@@ -0,0 +1,4 @@
1+conform/err/RotateArgs.raz:6:15: error: expected ",", found ")"
2+ 6 | x := ROL(x);
3+ | ^
4+raz: 1 error

test/msg/RotateCount.err (history) added +4 -0

@@ -0,0 +1,4 @@
1+conform/err/RotateCount.raz:6:17: error: expected an integer, found 1.5
2+ 6 | x := ROR(x, 1.5);
3+ | ^^^
4+raz: 1 error

test/msg/RotateReal.err (history) added +4 -0

@@ -0,0 +1,4 @@
1+conform/err/RotateReal.raz:6:14: error: ROL needs an unsigned integer, found F64
2+ 6 | x := ROL(x, 1);
3+ | ^
4+raz: 1 error

test/msg/RotateSigned.err (history) added +4 -0

@@ -0,0 +1,4 @@
1+conform/err/RotateSigned.raz:6:14: error: ROR needs an unsigned integer, found I32
2+ 6 | x := ROR(x, 1);
3+ | ^
4+raz: 1 error

test/msg/RotateUntyped.err (history) added +5 -0

@@ -0,0 +1,5 @@
1+conform/err/RotateUntyped.raz:5:14: error: ROL needs an unsigned integer, found 1
2+ 5 | x := ROL(1, 3);
3+ | ^
4+help: an untyped constant has no width to rotate in: give it a type, as in U32(1)
5+raz: 1 error