From 61fa4786089d5f85adf9ccc33a5c5a1f846a4b58 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Thu, 22 Oct 2026 11:03:05 +0100 Subject: [PATCH] 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. --- diff --git a/CLAUDE.md b/CLAUDE.md index 82175aa..d167536 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -332,3 +332,10 @@ type there, and its help says it would be `nil`. A slice where its element's pointer is wanted, and a pointer where a slice is, are errors whose help names `PTR(s)` and `LEN(s)`, or `p[0..n]`. +- `ROL(x, n)` and `ROR(x, n)` (report 2026-10.35) are done at `x`'s + own width, `rol al, cl` for a `U8`, with no mask of the count: x86 + takes it modulo 32 or 64, and a byte's or a word's rotation goes + round modulo 8 or 16 itself. A negative count is taken modulo the + width as a shift's is, `n & (w - 1)`: `ROL(x, -1)` is `ROR(x, 1)`. + The result is a built-in's, not an operator's, and widens (`made` + is false). Of two constants they fold, in a constant too. diff --git a/PROPOSALS.md b/PROPOSALS.md index 4579710..1465c67 100644 --- a/PROPOSALS.md +++ b/PROPOSALS.md @@ -918,7 +918,7 @@ procedure, would serve SHA, the other hashes and checksums without any inlining, but it is a language change, for a thaw. **Taken into the report (2026-10.35):** the built-ins `ROL(x, n)` and `ROR(x, n)`, -by the owner's word, for review; the compiler is next. rcrypto met the +by the owner's word; the compiler has them. rcrypto met the same (its `RAZ-NOTES.md`): SHA-256 at 32 MB/s with `ror` a procedure, 80 with it written out. On 100 MB of new files `sauce commit` takes 1.2 s against the Pascal sauce's 0.7, diff --git a/conform/err/RotateArgs.raz b/conform/err/RotateArgs.raz new file mode 100644 index 0000000..4df1548 --- /dev/null +++ b/conform/err/RotateArgs.raz @@ -0,0 +1,8 @@ +module RotateArgs; +proc main*; +var x: U16; +begin + x := 1; + x := ROL(x); +end; +end RotateArgs. diff --git a/conform/err/RotateCount.raz b/conform/err/RotateCount.raz new file mode 100644 index 0000000..a833b04 --- /dev/null +++ b/conform/err/RotateCount.raz @@ -0,0 +1,8 @@ +module RotateCount; +proc main*; +var x: U8; +begin + x := 1; + x := ROR(x, 1.5); +end; +end RotateCount. diff --git a/conform/err/RotateReal.raz b/conform/err/RotateReal.raz new file mode 100644 index 0000000..355648e --- /dev/null +++ b/conform/err/RotateReal.raz @@ -0,0 +1,8 @@ +module RotateReal; +proc main*; +var x: F64; +begin + x := 1.0; + x := ROL(x, 1); +end; +end RotateReal. diff --git a/conform/err/RotateSigned.raz b/conform/err/RotateSigned.raz new file mode 100644 index 0000000..f4b32dc --- /dev/null +++ b/conform/err/RotateSigned.raz @@ -0,0 +1,8 @@ +module RotateSigned; +proc main*; +var x: I32; +begin + x := 1; + x := ROR(x, 1); +end; +end RotateSigned. diff --git a/conform/err/RotateUntyped.raz b/conform/err/RotateUntyped.raz new file mode 100644 index 0000000..128c471 --- /dev/null +++ b/conform/err/RotateUntyped.raz @@ -0,0 +1,7 @@ +module RotateUntyped; +proc main*; +var x: U32; +begin + x := ROL(1, 3); +end; +end RotateUntyped. diff --git a/conform/run/Rotate.expect b/conform/run/Rotate.expect new file mode 100644 index 0000000..e7cd8d5 --- /dev/null +++ b/conform/run/Rotate.expect @@ -0,0 +1,28 @@ +03 C0 +81 81 +81 81 +03 C0 +03 C0 +C0 03 +4B 4B +0003 C000 +8001 8001 +0003 C000 +4123 2341 +00000003 C0000000 +80000001 80000001 +00000003 C0000000 +1A2B3C09 F02468AC +0000000000000003 C000000000000000 +8000000000000001 8000000000000001 +0000000000000003 C000000000000000 +3456789ABCDEF012 DEF0123456789ABC +8000000000000000 0000000000000002 +0000000000000100 0100000000000000 +03 C0 C0 +0003 8001 +F02468AC 12345678 +3456789ABCDEF012 F0123456789ABCDE +0C 20000000 +03 C0 C000 F02468AC +8 8000000000000001 diff --git a/conform/run/Rotate.raz b/conform/run/Rotate.raz new file mode 100644 index 0000000..d646b51 --- /dev/null +++ b/conform/run/Rotate.raz @@ -0,0 +1,66 @@ +(* ROL(x, n) and ROR(x, n) rotate x by n bits modulo its width (report + section 6.1): of each unsigned type, by 0, by the width and past it, + by a negative or signed count, with x and n variables or constants; + and a constant made by ROL. Each line is the left rotation, then the + right one. *) +module Rotate; +import Out; + +const + Eight = ROL(U32(1), 3); + Top = ROR(U64(1), 1) | ROL(U64(1), 64); + +(* the two rotations of x by n, a variable count *) +proc u8(x: U8; n: INT); +begin + Out.hex(U64(ROL(x, n)), 2); Out.str(" "); Out.hex(U64(ROR(x, n)), 2); Out.ln; +end; + +proc u16(x: U16; n: I8); +begin + Out.hex(U64(ROL(x, n)), 4); Out.str(" "); Out.hex(U64(ROR(x, n)), 4); Out.ln; +end; + +proc u32(x: U32; n: U64); +begin + Out.hex(U64(ROL(x, n)), 8); Out.str(" "); Out.hex(U64(ROR(x, n)), 8); Out.ln; +end; + +proc u64(x: U64; n: I64); +begin + Out.hex(ROL(x, n), 16); Out.str(" "); Out.hex(ROR(x, n), 16); Out.ln; +end; + +proc uint(x: UINT; n: U8); +begin + Out.hex(U64(ROL(x, n)), 16); Out.str(" "); Out.hex(U64(ROR(x, n)), 16); Out.ln; +end; + +proc main*; +var a: U8; b: U16; c: U32; d: U64; n: INT; +begin + u8(81H, 1); u8(81H, 0); u8(81H, 8); u8(81H, 9); u8(81H, 33); u8(81H, -1); u8(0B4H, 4); + u16(8001H, 1); u16(8001H, 16); u16(8001H, 33); u16(1234H, -4); + u32(80000001H, 1); u32(80000001H, 32); u32(80000001H, 33); u32(12345678H, 7); + u64(8000000000000001H, 1); u64(8000000000000001H, 64); u64(8000000000000001H, -63); + u64(0123456789ABCDEFH, 12); + uint(1, 63); uint(1, 200); + + (* a constant count, an immediate *) + a := 81H; b := 8001H; c := 12345678H; d := 0123456789ABCDEFH; + 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; + Out.hex(U64(ROL(b, 17)), 4); Out.str(" "); Out.hex(U64(ROR(b, 0)), 4); Out.ln; + Out.hex(U64(ROR(c, 7)), 8); Out.str(" "); Out.hex(U64(ROL(c, 32)), 8); Out.ln; + Out.hex(ROL(d, 12), 16); Out.str(" "); Out.hex(ROR(d, 68), 16); Out.ln; + + (* a constant x, a variable count *) + n := 3; + Out.hex(U64(ROL(U8(81H), n)), 2); Out.str(" "); Out.hex(U64(ROR(U32(1), n)), 8); Out.ln; + + (* both constants, folded *) + Out.hex(U64(ROL(U8(81H), 1)), 2); Out.str(" "); Out.hex(U64(ROR(U8(81H), 1)), 2); Out.str(" "); + Out.hex(U64(ROR(U16(8001H), 17)), 4); Out.str(" "); Out.hex(U64(ROR(U32(12345678H), -25)), 8); Out.ln; + Out.int(I64(Eight)); Out.str(" "); Out.hex(Top, 16); Out.ln; +end; + +end Rotate. diff --git a/doc/compiler.md b/doc/compiler.md index 5311aff..409edf5 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -18,12 +18,12 @@ |---|---|---|---| | `src/Diag.raz` | 410 | 315 | Messages: positions, source excerpts, colour, counts | | `src/Scan.raz` | 1,148 | 949 | Symbols, literals, comments, positions | -| `src/Sym.raz` | 713 | 571 | Types, objects, scopes, layout | +| `src/Sym.raz` | 715 | 573 | Types, objects, scopes, layout | | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes | -| `src/X64.raz` | 1,567 | 1,292 | The instruction encoder, jumps within a procedure, the assembler | -| `src/Gen.raz` | 2,561 | 1,824 | The stack machine, for x86-64: values, operations, checks, calls, frames | -| `src/Item.raz` | 2,175 | 1,747 | Items: types, constants, variables, operators, conversions, calls | -| `src/Parse.raz` | 4,260 | 3,683 | Declarations, statements, expressions, built-ins, recovery, warnings | +| `src/X64.raz` | 1,584 | 1,304 | The instruction encoder, jumps within a procedure, the assembler | +| `src/Gen.raz` | 2,588 | 1,841 | The stack machine, for x86-64: values, operations, checks, calls, frames | +| `src/Item.raz` | 2,223 | 1,785 | Items: types, constants, variables, operators, conversions, calls | +| `src/Parse.raz` | 4,266 | 3,689 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/Link.raz` | 407 | 315 | Tree shaking, layout, patching, the places | | `src/Elf.raz` | 449 | 355 | Static and dynamic ELF, symbol table, line numbers | | `src/Raz.raz` | 417 | 332 | The command line | @@ -1146,7 +1146,12 @@ are brought back to their width after each operation, which is how `U8` arithmetic wraps; since they are computed in 64 bits, a shift's count is cut to their width first, and `divTrap` makes the smallest value of one -divided by -1 fault as an `I64`'s does. +divided by -1 fault as an `I64`'s does. A rotation, `ROL` and `ROR` +(`Gen.rotate`), is the one operation done at its value's own width, +`rol al, cl` and not `rol rax, cl`, so that the bits leaving one end of +a `U8` come back in at its other end; and its count needs no mask, as +x86 takes it modulo 32, or 64, and a byte's or a word's rotation then +goes round modulo 8 or 16 itself, the count modulo the width. Checks: `check` jumps, when the check fails, to code at the end of the procedure (`endFails`), so that a check that holds is a jump not taken. @@ -1617,6 +1622,13 @@ ADR(s[0]) check 0 < LEN(s), then s's first word (stops on "") PTR(s) s's first word (no check) + +`ROL(x, n)` and `ROR(x, n)` (report 2026-10.35) are Item's `rotand`, +which refuses an `x` not of an unsigned type, an untyped constant with +help that says to give it one, and loads `x` before `n` is read, as +`op1` loads an operator's left; and `rotate`, which folds two constants, +right by k as left by the width less k, or has `Gen.rotate` make `rol` +or `ror`. Two mismatches in `coerce` that people meet on the way to it, a slice where its element's pointer is wanted and a pointer where a slice is, diff --git a/editor/emacs/raz-mode.el b/editor/emacs/raz-mode.el index 5050b7d..241de3c 100644 --- a/editor/emacs/raz-mode.el +++ b/editor/emacs/raz-mode.el @@ -33,7 +33,7 @@ (defconst raz-builtins '("SIZE" "ADR" "LEN" "PTR" "NEW" "MSG" "SYSCALL" "FENCE" "CAS" "XCHG" "XADD" - "ARGC" "ARGV" "INC" "DEC" "COPY")) + "ARGC" "ARGV" "INC" "DEC" "COPY" "ROL" "ROR")) (defvar raz-mode-syntax-table (let ((st (make-syntax-table))) diff --git a/editor/vim/syntax/raz.vim b/editor/vim/syntax/raz.vim index 8863ccf..3b8109e 100644 --- a/editor/vim/syntax/raz.vim +++ b/editor/vim/syntax/raz.vim @@ -15,7 +15,7 @@ syntax keyword razKeyword var variant VOLATILE while xor syntax keyword razType I8 I16 I32 I64 INT UINT U8 U16 U32 U64 BOOL F32 F64 ERROR syntax keyword razBuiltin SIZE ADR LEN PTR NEW MSG SYSCALL FENCE CAS XCHG XADD -syntax keyword razBuiltin ARGC ARGV INC DEC COPY +syntax keyword razBuiltin ARGC ARGV INC DEC COPY ROL ROR syntax keyword razConstant true false nil " 123, 0FFH, 1.5, 2.0E-3 diff --git a/src/Gen.raz b/src/Gen.raz index 0bdcbfc..29ad891 100644 --- a/src/Gen.raz +++ b/src/Gen.raz @@ -1038,6 +1038,33 @@ if o # 0 then putOff(o); end; end; +(* ROL or ROR, by left, of the two values on top: an unsigned integer of + size bytes, under the count (report section 6.1). x86 takes the count + of rol and ror modulo 32, or 64 for a quadword, and a byte's or a + word's rotation then goes round modulo 8 or 16 itself: as 8 and 16 + divide 32, that is the count modulo the width, as the report has it, + with no mask of the count's own, which a shift needs. A constant + count is cut to the width here, an imm8; 0 rotates nothing. The bits + above the width are zero and stay so: rol al and rol ax leave them, + rol eax clears them. *) +proc rotate*(left: BOOL; size: INT); +var c: BOOL; v: I64; r, y, op: INT; +begin + if left then op := X.shRol; else op := X.shRor; end; + c, v := topConst(); + if c then + drop(); + r := top(); + v := v & I64(size * 8 - 1); + if v # 0 then X.rotate(op, r, size, INT(v)); end; + else + r, y := operands(); + if y # RCX then X.movRR(RCX, y); end; + X.rotate(op, r, size, -1); + end; + stack[depth - 1].k := kWord; +end; + (* The left op the right of the two values on top, as a BOOL *) proc compare*(sy: Scan.Sym; k: Kind; signed: BOOL); var f: BOOL; r, x, y, i: INT; v: I64; c: BOOL; m: X.Mem; diff --git a/src/Item.raz b/src/Item.raz index b28e33a..066a1cd 100644 --- a/src/Item.raz +++ b/src/Item.raz @@ -1512,6 +1512,54 @@ return Sym.same(t, Sym.tInt) or Sym.same(t, Sym.tUInt); end; +(* The x of ROL(x, n), left, or ROR(x, n), checked: of an unsigned + integer type, since a rotation goes round within a width, which an + untyped constant hasn't (report section 6.1). It is loaded, unless a + constant, before n is read, as the left of an operator is by op1. *) +proc rotand*(var x: Item; left: BOOL): ERROR; +var name, s: []U8; +begin + if x.mode in {mType, mBuiltin, mNone, mCase, mStr} then return notValue(x); end; + if left then name := "ROL"; else name := "ROR"; end; + s := say2("{} needs an unsigned integer, found {}", name, what(x)); + if x.typ = Sym.tConst then + return Scan.markHelp(x.pos, s, + say1("an untyped constant has no width to rotate in: give it a type, as in U32({})", Diag.source(x.pos))); + end; + if (x.typ^.form # Sym.fInt) or x.typ^.signed then return Scan.markAt(x.pos, s); end; + if x.mode # mConst then try value(x); end; + return nil; +end; + +(* ROL(x, n), left, or ROR(x, n): x, of rotand, rotated by n modulo its + width. Of two constants it is folded: right by k is left by w - k, + and left by k is x shl k | x shr (w - k), cut to the width. *) +proc rotate*(var x, n: Item; left: BOOL): ERROR; +var w, k: I64; v: U64; +begin + if n.mode in {mType, mBuiltin, mNone, mCase, mStr} then return notValue(n); end; + if not Sym.isInt(n.typ) then return Scan.markAt(n.pos, say1("expected an integer, found {}", what(n))); end; + x.made := false; (* a built-in's result, not an operator's: it widens *) + if (x.mode = mConst) and (n.mode = mConst) then + w := bits(x.typ); + k := n.val & (w - 1); + if not left then k := (w - k) & (w - 1); end; + v := U64(x.val); + if k # 0 then v := v shl k | v shr (w - k); end; + x.val := cut(I64(v), x.typ); + return nil; + end; + try value(n); + if x.mode = mConst then + (* x goes on after n, which rotand didn't load, and under it *) + try value(x); + Gen.swap(); + end; + Gen.rotate(left, intSize(x.typ)); + x.mode := mStack; + return nil; +end; + proc convert*(var x: Item; t: ^Sym.Type): ERROR; var s: ^Sym.Type; r: F64; begin diff --git a/src/Parse.raz b/src/Parse.raz index 9ae8e95..9fa9ab6 100644 --- a/src/Parse.raz +++ b/src/Parse.raz @@ -1085,6 +1085,12 @@ Item.makeConst(x, Sym.tNone, 0); x.mode := Item.mNone; | Sym.bNew: try new_(x); + | Sym.bRol, Sym.bRor: + try expression(x, nil); + try Item.rotand(x, which = Sym.bRol); + try expect(Scan.sComma); + try expression(y, nil); + try Item.rotate(x, y, which = Sym.bRol); | Sym.bCopy: try expression(x, nil); try Item.copyDest(x); diff --git a/src/Raz.raz b/src/Raz.raz index 92e817e..2c2324a 100644 --- a/src/Raz.raz +++ b/src/Raz.raz @@ -32,7 +32,7 @@ import Sys, Out, Version, Files, Fmt, Diag, Scan, Sym, Chunk, X64, Gen, Parse, Link, Elf; const - Report = "2026-10.34"; (* the version of doc/raz.md it follows *) + Report = "2026-10.35"; (* the version of doc/raz.md it follows *) MemfdCreate = 319; (* the system call *) MfdCloexec = 1; (* an ELF still executes: the kernel opens it first *) Execve = 59; diff --git a/src/Sym.raz b/src/Sym.raz index eb21387..49a4380 100644 --- a/src/Sym.raz +++ b/src/Sym.raz @@ -131,7 +131,7 @@ (* built-in procedures *) bSize* = 0; bAdr* = 1; bLen* = 2; bNew* = 3; bMsg* = 4; bSyscall* = 5; bFence* = 6; bCas* = 7; bXchg* = 8; bXadd* = 9; bArgc* = 10; bArgv* = 11; - bInc* = 12; bDec* = 13; bCopy* = 14; bPtr* = 15; + bInc* = 12; bDec* = 13; bCopy* = 14; bPtr* = 15; bRol* = 16; bRor* = 17; MaxDepth* = 64; (* scopes open at once *) @@ -710,4 +710,6 @@ enter("INC", oBuiltin, nil, bInc); enter("DEC", oBuiltin, nil, bDec); enter("COPY", oBuiltin, nil, bCopy); + enter("ROL", oBuiltin, nil, bRol); + enter("ROR", oBuiltin, nil, bRor); end Sym. diff --git a/src/X64.raz b/src/X64.raz index 1b047af..201c6e8 100644 --- a/src/X64.raz +++ b/src/X64.raz @@ -28,8 +28,8 @@ (* group 1 operations, the /digit of 81 and 83 *) aluAdd* = 0; aluOr* = 1; aluAnd* = 4; aluSub* = 5; aluXor* = 6; aluCmp* = 7; - (* the /digit of D3 and C1 *) - shShl* = 4; shShr* = 5; shSar* = 7; + (* the /digit of D3 and C1, and of D2 and C0 for a byte *) + shRol* = 0; shRor* = 1; shShl* = 4; shShr* = 5; shSar* = 7; NoLabel* = -1; @@ -513,6 +513,23 @@ if listing then say(text2(ShiftNames[op], rn(r), num(n))); end; opR(0, true, 0C1H, op, r, false); Chunk.put1(n); +end; + +(* rol or ror, op, of the size bytes of r, by cl, or by n if n >= 0: + D3 by cl and C1 by an imm8, D2 and C0 for a byte. Unlike a shift, a + rotation is done at its value's own width, rol eax and not rol rax, + so the bits leaving one end come back in at the other end of it. *) +proc rotate*(op, r, size, n: INT); +var opc: INT; +begin + if listing then + if n < 0 then say(text2(ShiftNames[op], rnSized(r, size), "cl")); + else say(text2(ShiftNames[op], rnSized(r, size), num(n))); end; + end; + if n < 0 then opc := 0D2H; else opc := 0C0H; end; + if size > 1 then INC(opc); end; + opR(pfx16(size), size = 8, opc, op, r, size = 1); + if n >= 0 then Chunk.put1(n); end; end; (* F7 /ext: 2 not, 3 neg, 6 div, 7 idiv *) diff --git a/test/msg/RotateArgs.err b/test/msg/RotateArgs.err new file mode 100644 index 0000000..447fda0 --- /dev/null +++ b/test/msg/RotateArgs.err @@ -0,0 +1,4 @@ +conform/err/RotateArgs.raz:6:15: error: expected ",", found ")" + 6 | x := ROL(x); + | ^ +raz: 1 error diff --git a/test/msg/RotateCount.err b/test/msg/RotateCount.err new file mode 100644 index 0000000..f702e51 --- /dev/null +++ b/test/msg/RotateCount.err @@ -0,0 +1,4 @@ +conform/err/RotateCount.raz:6:17: error: expected an integer, found 1.5 + 6 | x := ROR(x, 1.5); + | ^^^ +raz: 1 error diff --git a/test/msg/RotateReal.err b/test/msg/RotateReal.err new file mode 100644 index 0000000..d2a5a0b --- /dev/null +++ b/test/msg/RotateReal.err @@ -0,0 +1,4 @@ +conform/err/RotateReal.raz:6:14: error: ROL needs an unsigned integer, found F64 + 6 | x := ROL(x, 1); + | ^ +raz: 1 error diff --git a/test/msg/RotateSigned.err b/test/msg/RotateSigned.err new file mode 100644 index 0000000..09b506c --- /dev/null +++ b/test/msg/RotateSigned.err @@ -0,0 +1,4 @@ +conform/err/RotateSigned.raz:6:14: error: ROR needs an unsigned integer, found I32 + 6 | x := ROR(x, 1); + | ^ +raz: 1 error diff --git a/test/msg/RotateUntyped.err b/test/msg/RotateUntyped.err new file mode 100644 index 0000000..a84d665 --- /dev/null +++ b/test/msg/RotateUntyped.err @@ -0,0 +1,5 @@ +conform/err/RotateUntyped.raz:5:14: error: ROL needs an unsigned integer, found 1 + 5 | x := ROL(1, 3); + | ^ +help: an untyped constant has no width to rotate in: give it a type, as in U32(1) +raz: 1 error