From 1a0e8dde4b949aa5dacd9dbb7c1d5a81f67479ea Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 23 Oct 2026 14:06:15 +0100 Subject: [PATCH] ABS: a number without its sign, as report 2026-10.36 has it The built-in ABS(x): Sym enters it, Parse's builtin reads x, and Item's absolute refuses an x not an integer or a real (a string too, as no number), folds a constant as neg does, a real's by clearing the top bit of the F64 it is held as, or has Gen.abs make it. The result is of x's type, a built-in's, and widens. An unsigned integer needs no code. Gen.abs makes a signed integer's with no branch, through X64's new cmov: the negation, and the copy kept in rax back if the negation is negative, as it is for a positive x. The smallest value negates to itself and so gives itself, as -x does, with no check, as -x has none; a narrow one is negated in 64 bits and cut back to its width, as neg does. A real's sign bit is cleared in rax by X64's new btr, as neg flips it by btc: mov rax, r; neg r; cmovs r, rax; movsxd r, r (I32) movq rax, x; btr rax, 63; movq x, rax (F64; 31 for F32) ABS of the smallest untyped constant, ABS(MIN(I64)), is neg's error. Tests: conform/run/Abs (each signed type at 0, both signs, its largest and smallest, beside -x; each unsigned type; a value an operator wrapped; beside other values; constants typed and untyped); conform/run/AbsReal (F32 and F64, by their bits: ordinary values, -0.0, inf, -inf, NaN, -NaN; constants); conform/err AbsBool, AbsSet, AbsPtr, AbsString and AbsUntyped, their messages in test/msg. compiler.md, the editors' built-in lists and CLAUDE.md's readings say so. Code lines 12,327 -> 12,378. --- diff --git a/CLAUDE.md b/CLAUDE.md index 2f506fe..0e8d84e 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -355,6 +355,13 @@ 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. +- `ABS(x)` (report 2026-10.36) is of `x`'s type and widens (`made` is + false). A signed integer's is `mov rax, r; neg r; cmovs r, rax`, then + cut to a narrow width as `neg` is, so the smallest value gives itself + with no check, as `-x` does; an unsigned one's is no code; a real's + is `btr` of its sign bit in rax, as `neg`'s `btc`. Of a constant it + folds, a real's top bit cleared; of the smallest untyped constant it + is `neg`'s error, "too big for I64". A string is refused as no number. - `MIN(T)` and `MAX(T)` (report 2026-10.36) are untyped constants, but `MAX(U64)` and `MAX(UINT)`, above `high(I64)`, keep their type, as a hex literal above it is a `U64`. A `T` not an integer type is an diff --git a/conform/err/AbsBool.raz b/conform/err/AbsBool.raz new file mode 100644 index 0000000..60903b2 --- /dev/null +++ b/conform/err/AbsBool.raz @@ -0,0 +1,7 @@ +module AbsBool; +proc main*; +var b: BOOL; +begin + b := ABS(true); +end; +end AbsBool. diff --git a/conform/err/AbsPtr.raz b/conform/err/AbsPtr.raz new file mode 100644 index 0000000..38d58dc --- /dev/null +++ b/conform/err/AbsPtr.raz @@ -0,0 +1,8 @@ +module AbsPtr; +proc main*; +var p: ^INT; +begin + p := nil; + p := ABS(p); +end; +end AbsPtr. diff --git a/conform/err/AbsSet.raz b/conform/err/AbsSet.raz new file mode 100644 index 0000000..b53b229 --- /dev/null +++ b/conform/err/AbsSet.raz @@ -0,0 +1,11 @@ +module AbsSet; +type + Day = (mon, tue, wed); + Days = set of Day; +proc main*; +var s: Days; +begin + s := {mon, wed}; + s := ABS(s); +end; +end AbsSet. diff --git a/conform/err/AbsString.raz b/conform/err/AbsString.raz new file mode 100644 index 0000000..b64deb0 --- /dev/null +++ b/conform/err/AbsString.raz @@ -0,0 +1,3 @@ +module AbsString; +const N = ABS("ten"); +end AbsString. diff --git a/conform/err/AbsUntyped.raz b/conform/err/AbsUntyped.raz new file mode 100644 index 0000000..b0361aa --- /dev/null +++ b/conform/err/AbsUntyped.raz @@ -0,0 +1,3 @@ +module AbsUntyped; +const N = ABS(MIN(I64)); +end AbsUntyped. diff --git a/conform/run/Abs.expect b/conform/run/Abs.expect new file mode 100644 index 0000000..eb22ff2 --- /dev/null +++ b/conform/run/Abs.expect @@ -0,0 +1,9 @@ +0 0 7 -7 7 7 127 -127 127 127 -128 -128 +5 -5 5 5 32767 -32767 -32768 -32768 +9 -9 9 9 2147483647 -2147483647 -2147483648 -2147483648 +1 -1 1 1 9223372036854775807 -9223372036854775807 -9223372036854775808 -9223372036854775808 +42 42 42 -42 -9223372036854775808 -9223372036854775808 +200 65535 4294967295 FFFFFFFFFFFFFFFF +-128 -128 28 +51 +5 9223372036854775807 -128 65535 32 diff --git a/conform/run/Abs.raz b/conform/run/Abs.raz new file mode 100644 index 0000000..5863e5a --- /dev/null +++ b/conform/run/Abs.raz @@ -0,0 +1,70 @@ +(* ABS(x) is x without its sign, of x's type (report section 6.1): of + each signed type at its own width, where the smallest value gives + itself, as -x does; of each unsigned type, which is x itself; of an + operator's result, beside other values; and of constants, typed and + untyped, in a constant too. *) +module Abs; +import Out; + +const + Five = ABS(-5); (* untyped *) + Big = ABS(MIN(I64) + 1); (* untyped, the largest I64 *) + Byte = ABS(I8(-128)); (* typed, gives itself *) + Word = ABS(U16(65535)); + Sum = ABS(-2) + ABS(3) * 10; (* 32 *) + +proc show(n: I64); +begin + Out.int(n); Out.char(' '); +end; + +proc i8(x: I8); +begin + show(I64(ABS(x))); show(I64(-x)); +end; + +proc i16(x: I16); +begin + show(I64(ABS(x))); show(I64(-x)); +end; + +proc i32(x: I32); +begin + show(I64(ABS(x))); show(I64(-x)); +end; + +proc i64(x: I64); +begin + show(I64(ABS(x))); show(I64(-x)); +end; + +proc int(x: INT); +begin + show(I64(ABS(x))); show(I64(-x)); +end; + +proc main*; +var a, b: I8; u8: U8; u16: U16; u32: U32; u64: U64; n: INT; t: I8; +begin + i8(0); i8(7); i8(-7); i8(127); i8(-127); i8(-128); Out.ln; + i16(5); i16(-5); i16(32767); i16(-32768); Out.ln; + i32(9); i32(-9); i32(2147483647); i32(-2147483648); Out.ln; + i64(1); i64(-1); i64(MAX(I64)); i64(MIN(I64)); Out.ln; + int(-42); int(42); int(MIN(INT)); Out.ln; + + u8 := 200; u16 := 65535; u32 := 0FFFFFFFFH; u64 := 0FFFFFFFFFFFFFFFFH; + show(I64(ABS(u8))); show(I64(ABS(u16))); show(I64(ABS(u32))); Out.hex(ABS(u64), 16); Out.ln; + + (* the sum wraps to -128 in an I8, which ABS keeps *) + a := 100; b := 28; + t := a + b; + show(I64(t)); show(I64(ABS(I8(a + b)))); show(I64(ABS(a - b - 100))); Out.ln; + + (* beside other values on the stack *) + n := -3; + show(10 * ABS(n) + ABS(n - 4) * ABS(-n)); Out.ln; + + show(Five); show(Big); show(I64(Byte)); show(I64(Word)); show(Sum); Out.ln; +end; + +end Abs. diff --git a/conform/run/AbsReal.expect b/conform/run/AbsReal.expect new file mode 100644 index 0000000..c147a62 --- /dev/null +++ b/conform/run/AbsReal.expect @@ -0,0 +1,5 @@ +3FF8000000000000 3FF8000000000000 0000000000000000 0000000000000000 7FF0000000000000 7FF0000000000000 7FF8000000000000 7FF8000000000000 +3FC00000 3FC00000 00000000 00000000 7F800000 7F800000 7FC00000 7FC00000 +51 +NaN +2 2 3 0000000000000000 diff --git a/conform/run/AbsReal.raz b/conform/run/AbsReal.raz new file mode 100644 index 0000000..e11a93a --- /dev/null +++ b/conform/run/AbsReal.raz @@ -0,0 +1,48 @@ +(* ABS(x) of a real clears its sign bit (report section 6.1): of F32 + and F64 variables, ordinary values, minus zero, infinities and NaNs, + each shown by its bits, and of constants, in a constant too. *) +module AbsReal; +import Out; + +const + Half = ABS(-0.5); (* untyped *) + Wide = ABS(F64(-2.0E300)); + Narrow = ABS(F32(-1.5)); + Zero = ABS(-0.0); + +proc f64(x: F64); +var y: F64; +begin + y := ABS(x); + Out.hex(y.(U64), 16); Out.char(' '); +end; + +proc f32(x: F32); +var y: F32; +begin + y := ABS(x); + Out.hex(U64(y.(U32)), 8); Out.char(' '); +end; + +proc main*; +var zero, inf, nan, d: F64; zero32, inf32, nan32: F32; +begin + zero := 0.0; inf := 1.0 / zero; nan := zero / zero; + zero32 := 0.0; inf32 := 1.0 / zero32; nan32 := zero32 / zero32; + + f64(1.5); f64(-1.5); f64(-0.0); f64(0.0); f64(inf); f64(-inf); f64(nan); f64(-nan); Out.ln; + f32(1.5); f32(-1.5); f32(-0.0); f32(0.0); f32(inf32); f32(-inf32); f32(nan32); f32(-nan32); Out.ln; + + (* beside other values on the stack *) + d := -3.0; + Out.int(I64(10.0 * ABS(d) + ABS(d - 4.0) * ABS(-d))); Out.ln; + if ABS(nan) # ABS(nan) then Out.str("NaN"); Out.ln; end; + + Out.int(I64(Half * 4.0)); Out.char(' '); + Out.int(I64(Wide / 1.0E300)); Out.char(' '); + Out.int(I64(Narrow * 2.0)); Out.char(' '); + d := Zero; + Out.hex(d.(U64), 16); Out.ln; +end; + +end AbsReal. diff --git a/conform/run/AbsReal.reals b/conform/run/AbsReal.reals new file mode 100644 index 0000000..e69de29 diff --git a/doc/compiler.md b/doc/compiler.md index cc06cf7..0ebb5a1 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -1,6 +1,6 @@ # Reading the raz compiler -The compiler is eleven files of raz, about 12,300 lines of code (the +The compiler is eleven files of raz, about 12,400 lines of code (the budget is 14,000). It is its own compiler: a release of it, the one `BOOT` names, builds `src/`, and `make fixpoint` checks that what it makes makes itself again, byte for byte. Each release was built by the @@ -18,12 +18,12 @@ |---|---|---|---| | `src/Diag.raz` | 422 | 324 | Messages: positions, source excerpts, colour, counts | | `src/Scan.raz` | 1,169 | 965 | Symbols, literals, comments, positions | -| `src/Sym.raz` | 723 | 581 | Types, objects, scopes, layout | +| `src/Sym.raz` | 724 | 582 | Types, objects, scopes, layout | | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes | -| `src/X64.raz` | 1,605 | 1,318 | The instruction encoder, jumps within a procedure, the assembler | -| `src/Gen.raz` | 2,800 | 1,963 | The stack machine, for x86-64: values, operations, checks, calls, frames | -| `src/Item.raz` | 2,414 | 1,938 | Items: types, constants, variables, operators, conversions, calls | -| `src/Parse.raz` | 4,611 | 3,956 | Declarations, statements, expressions, built-ins, recovery, warnings | +| `src/X64.raz` | 1,620 | 1,329 | The instruction encoder, jumps within a procedure, the assembler | +| `src/Gen.raz` | 2,828 | 1,978 | The stack machine, for x86-64: values, operations, checks, calls, frames | +| `src/Item.raz` | 2,442 | 1,959 | Items: types, constants, variables, operators, conversions, calls | +| `src/Parse.raz` | 4,614 | 3,959 | 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` | 418 | 333 | The command line | @@ -1354,6 +1354,22 @@ mov rax, r; shr rax, 4; add r, rax; mov rdx, 0F0F...H; and r, rdx mov rdx, 0101...H; imul r, rdx; shr r, 56 +`abs` makes `ABS` of a signed integer with no branch: it negates `x` +and takes back the copy it kept in rax if the negation is negative, as +it is for a positive `x`. The smallest value negates to itself, which +is negative, and so gives itself, as `-x` does, with no check, as `-x` +has none. A narrow value is negated in 64 bits, where its smallest +isn't the smallest, and cut back to its width after, as `neg` does: +-128 in an `I8` negates to 128, which in 8 bits is -128 again. A real +has its sign bit cleared in rax, as `neg` flips it; an unsigned integer +needs no code. + + ABS(x), an I32 ABS(x), an F64 + mov rax, r movq rax, x + neg r btr rax, 63 + cmovs r, rax movq x, rax + movsxd r, r + 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. That code puts the values involved where `Rt.fail` wants them and calls @@ -1923,6 +1939,13 @@ type, as `rotand` does; folds a constant by counting its bits one by one (`countOf`); or has `Gen.countZeros` or `Gen.popCount` count them. The result is an `INT`. + +`ABS(x)` (report 2026-10.36) is Item's `absolute`, which refuses an `x` +not an integer or a real; folds a constant, as `neg` does, a real's by +clearing the top bit of the `F64` it is held as and the smallest +untyped constant an error, as its negation is beyond the `I64`s; or, +for a signed integer or a real, has `Gen.abs` make it. The result is +of `x`'s type, a built-in's, not an operator's, and widens. 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 056edaa..3cae605 100644 --- a/editor/emacs/raz-mode.el +++ b/editor/emacs/raz-mode.el @@ -33,7 +33,7 @@ (defconst raz-builtins '("SIZE" "OFFSET" "ADR" "LEN" "PTR" "BYTES" "NEW" "MSG" "SYSCALL" "FENCE" "CAS" "XCHG" "XADD" - "ARGC" "ARGV" "INC" "DEC" "COPY" "FILL" "ROL" "ROR" "MIN" "MAX" + "ARGC" "ARGV" "INC" "DEC" "COPY" "FILL" "ROL" "ROR" "MIN" "MAX" "ABS" "CLZ" "CTZ" "POPCOUNT")) (defvar raz-mode-syntax-table diff --git a/editor/vim/syntax/raz.vim b/editor/vim/syntax/raz.vim index c9608ce..af1a1f9 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 OFFSET ADR LEN PTR BYTES NEW MSG SYSCALL FENCE CAS XCHG XADD -syntax keyword razBuiltin ARGC ARGV INC DEC COPY FILL ROL ROR MIN MAX +syntax keyword razBuiltin ARGC ARGV INC DEC COPY FILL ROL ROR MIN MAX ABS syntax keyword razBuiltin CLZ CTZ POPCOUNT syntax keyword razConstant true false nil diff --git a/src/Gen.raz b/src/Gen.raz index d2cac41..a9a6f84 100644 --- a/src/Gen.raz +++ b/src/Gen.raz @@ -1138,6 +1138,34 @@ stack[depth - 1].k := kWord; end; +(* ABS of the real, or the signed integer of size bytes, on top (report + section 6.1); an unsigned one needs no code. A real's sign bit is + cleared, in rax, as neg flips it. An integer is negated, and its copy + in rax taken back if the negation is negative (SF), as it is for a + positive one. The smallest value negates to itself, negative, and so + gives itself, as -x does, with no branch and no check. A narrow + value is negated on all 64 bits, where its smallest is not the + smallest, so it is cut back to its width after, as neg does: + -128 in an I8 negates to 128, which is -128 again in 8 bits. + + movq rax, x; btr rax, 63; movq x, rax (F64; 31 for an F32) + mov rax, r; neg r; cmovs r, rax (I64; then movsx r, r for a narrow one) *) +proc abs*(k: Kind; size: INT); +var r: INT; +begin + r := top(); + if k in {kF32, kF64} then + X.movqRX(RAX, r); + if k = kF32 then X.btr(RAX, 31); else X.btr(RAX, 63); end; + X.movqXR(r, RAX); + else + X.movRR(RAX, r); + X.unary(3, r); (* neg *) + X.cmov(X.ccS, r, RAX); + if size < 8 then X.extend(r, size, true); end; + end; +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 db19a99..0761526 100644 --- a/src/Item.raz +++ b/src/Item.raz @@ -1643,6 +1643,34 @@ return nil; end; +(* ABS(x): x without its sign, of x's type, an integer or a real (report + section 6.1). A signed integer's smallest value gives itself, as -x + does, with no check, as -x has none; an untyped constant's, whose + result would be beyond the I64s, is an error, as it is for -x. An + unsigned integer is itself. Of a constant it is folded: a real's + sign bit, the top one of the F64 it is held as, cleared. *) +proc absolute*(var x: Item): ERROR; +begin + if x.mode in {mType, mBuiltin, mNone, mCase} then return notValue(x); end; + if (x.mode = mStr) or not (Sym.isInt(x.typ) or (x.typ^.form = Sym.fReal)) then + return Scan.markAt(x.pos, say1("ABS needs an integer or a real, found {}", what(x))); + end; + if x.mode = mConst then + if x.typ^.form = Sym.fReal then + x.val := x.val & MaxI64; + elsif (x.typ^.form = Sym.fConst) and (x.val = MinI64) then + return Scan.markAt(x.pos, "the constant is too big for I64"); + elsif signed(x.typ) and (x.val < 0) then + x.val := cut(-x.val, x.typ); + end; + else + try value(x); + if signed(x.typ) or (x.typ^.form = Sym.fReal) then Gen.abs(Gen.kindOf(x.typ), intSize(x.typ)); end; + end; + x.made := false; (* a built-in's result, not an operator's *) + return nil; +end; + (* CLZ, CTZ or POPCOUNT, which, of v, a value w bits wide, folded *) proc countOf(which: INT; v: U64; w: I64): I64; var n, i: I64; diff --git a/src/Parse.raz b/src/Parse.raz index 175ae8a..bb40223 100644 --- a/src/Parse.raz +++ b/src/Parse.raz @@ -1236,6 +1236,9 @@ at := Scan.tokPos(); t := try parseType(); try Item.limit(x, t, which = Sym.bMax, Scan.spanFrom(at)); + | Sym.bAbs: + try expression(x, nil); + try Item.absolute(x); | Sym.bClz, Sym.bCtz, Sym.bPopcount: try expression(x, nil); try Item.countBits(x, which); diff --git a/src/Sym.raz b/src/Sym.raz index 452b500..3bf254e 100644 --- a/src/Sym.raz +++ b/src/Sym.raz @@ -132,7 +132,7 @@ bFence* = 6; bCas* = 7; bXchg* = 8; bXadd* = 9; bArgc* = 10; bArgv* = 11; bInc* = 12; bDec* = 13; bCopy* = 14; bPtr* = 15; bRol* = 16; bRor* = 17; bMin* = 18; bMax* = 19; bClz* = 20; bCtz* = 21; bPopcount* = 22; - bOffset* = 23; bBytes* = 24; bFill* = 25; + bOffset* = 23; bBytes* = 24; bFill* = 25; bAbs* = 26; MaxDepth* = 64; (* scopes open at once *) @@ -717,6 +717,7 @@ enter("ROR", oBuiltin, nil, bRor); enter("MIN", oBuiltin, nil, bMin); enter("MAX", oBuiltin, nil, bMax); + enter("ABS", oBuiltin, nil, bAbs); enter("CLZ", oBuiltin, nil, bClz); enter("CTZ", oBuiltin, nil, bCtz); enter("POPCOUNT", oBuiltin, nil, bPopcount); diff --git a/src/X64.raz b/src/X64.raz index 524770a..cda87bf 100644 --- a/src/X64.raz +++ b/src/X64.raz @@ -847,6 +847,21 @@ Chunk.put1(bit); end; +(* btr r, bit: the bit cleared, 0F BA /6 *) +proc btr*(r, bit: INT); +begin + if listing then say(text2("btr", rn(r), num(bit))); end; + opR(0, true, 0BA0FH, 6, r, false); + Chunk.put1(bit); +end; + +(* cmovcc r, x: r := x if the condition holds, 0F 40+cc *) +proc cmov*(cc, r, x: INT); +begin + if listing then say(text2(Diag.say("cmov{}", [Fmt.str(CondNames[cc])]), rn(r), rn(x))); end; + opR(0, true, 400FH + (cc shl 8), r, x, false); +end; + (* Jumps. A label not yet placed is the chain of the jumps to it, each rel32 field holding the position of the one before, NoLabel ending it. Placing the label walks the chain and patches each. *) diff --git a/test/msg/AbsBool.err b/test/msg/AbsBool.err new file mode 100644 index 0000000..611c461 --- /dev/null +++ b/test/msg/AbsBool.err @@ -0,0 +1,4 @@ +conform/err/AbsBool.raz:5:14: error: ABS needs an integer or a real, found BOOL + 5 | b := ABS(true); + | ^^^^ +raz: 1 error diff --git a/test/msg/AbsPtr.err b/test/msg/AbsPtr.err new file mode 100644 index 0000000..7c5255e --- /dev/null +++ b/test/msg/AbsPtr.err @@ -0,0 +1,4 @@ +conform/err/AbsPtr.raz:6:14: error: ABS needs an integer or a real, found ^INT + 6 | p := ABS(p); + | ^ +raz: 1 error diff --git a/test/msg/AbsSet.err b/test/msg/AbsSet.err new file mode 100644 index 0000000..be20ff3 --- /dev/null +++ b/test/msg/AbsSet.err @@ -0,0 +1,4 @@ +conform/err/AbsSet.raz:9:14: error: ABS needs an integer or a real, found Days + 9 | s := ABS(s); + | ^ +raz: 1 error diff --git a/test/msg/AbsString.err b/test/msg/AbsString.err new file mode 100644 index 0000000..61e7f7a --- /dev/null +++ b/test/msg/AbsString.err @@ -0,0 +1,4 @@ +conform/err/AbsString.raz:2:15: error: ABS needs an integer or a real, found a string + 2 | const N = ABS("ten"); + | ^^^^^ +raz: 1 error diff --git a/test/msg/AbsUntyped.err b/test/msg/AbsUntyped.err new file mode 100644 index 0000000..21077f1 --- /dev/null +++ b/test/msg/AbsUntyped.err @@ -0,0 +1,4 @@ +conform/err/AbsUntyped.raz:2:15: error: the constant is too big for I64 + 2 | const N = ABS(MIN(I64)); + | ^^^^^^^^ +raz: 1 error