raz / commit 1a0e8dde

commit
1a0e8dde4b949aa5dacd9dbb7c1d5a81f67479ea
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
ac13ab97
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.

patch · log

24 files changed, +298 -9

CLAUDE.md modified +7 -0

@@ -355,6 +355,13 @@
355355 width as a shift's is, `n & (w - 1)`: `ROL(x, -1)` is `ROR(x, 1)`.
356356 The result is a built-in's, not an operator's, and widens (`made`
357357 is false). Of two constants they fold, in a constant too.
358+- `ABS(x)` (report 2026-10.36) is of `x`'s type and widens (`made` is
359+ false). A signed integer's is `mov rax, r; neg r; cmovs r, rax`, then
360+ cut to a narrow width as `neg` is, so the smallest value gives itself
361+ with no check, as `-x` does; an unsigned one's is no code; a real's
362+ is `btr` of its sign bit in rax, as `neg`'s `btc`. Of a constant it
363+ folds, a real's top bit cleared; of the smallest untyped constant it
364+ is `neg`'s error, "too big for I64". A string is refused as no number.
358365 - `MIN(T)` and `MAX(T)` (report 2026-10.36) are untyped constants, but
359366 `MAX(U64)` and `MAX(UINT)`, above `high(I64)`, keep their type, as a
360367 hex literal above it is a `U64`. A `T` not an integer type is an

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

@@ -0,0 +1,7 @@
1+module AbsBool;
2+proc main*;
3+var b: BOOL;
4+begin
5+ b := ABS(true);
6+end;
7+end AbsBool.

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

@@ -0,0 +1,8 @@
1+module AbsPtr;
2+proc main*;
3+var p: ^INT;
4+begin
5+ p := nil;
6+ p := ABS(p);
7+end;
8+end AbsPtr.

conform/err/AbsSet.raz (history) added +11 -0

@@ -0,0 +1,11 @@
1+module AbsSet;
2+type
3+ Day = (mon, tue, wed);
4+ Days = set of Day;
5+proc main*;
6+var s: Days;
7+begin
8+ s := {mon, wed};
9+ s := ABS(s);
10+end;
11+end AbsSet.

conform/err/AbsString.raz (history) added +3 -0

@@ -0,0 +1,3 @@
1+module AbsString;
2+const N = ABS("ten");
3+end AbsString.

conform/err/AbsUntyped.raz (history) added +3 -0

@@ -0,0 +1,3 @@
1+module AbsUntyped;
2+const N = ABS(MIN(I64));
3+end AbsUntyped.

conform/run/Abs.expect (history) added +9 -0

@@ -0,0 +1,9 @@
1+0 0 7 -7 7 7 127 -127 127 127 -128 -128
2+5 -5 5 5 32767 -32767 -32768 -32768
3+9 -9 9 9 2147483647 -2147483647 -2147483648 -2147483648
4+1 -1 1 1 9223372036854775807 -9223372036854775807 -9223372036854775808 -9223372036854775808
5+42 42 42 -42 -9223372036854775808 -9223372036854775808
6+200 65535 4294967295 FFFFFFFFFFFFFFFF
7+-128 -128 28
8+51
9+5 9223372036854775807 -128 65535 32

conform/run/Abs.raz (history) added +70 -0

@@ -0,0 +1,70 @@
1+(* ABS(x) is x without its sign, of x's type (report section 6.1): of
2+ each signed type at its own width, where the smallest value gives
3+ itself, as -x does; of each unsigned type, which is x itself; of an
4+ operator's result, beside other values; and of constants, typed and
5+ untyped, in a constant too. *)
6+module Abs;
7+import Out;
8+
9+const
10+ Five = ABS(-5); (* untyped *)
11+ Big = ABS(MIN(I64) + 1); (* untyped, the largest I64 *)
12+ Byte = ABS(I8(-128)); (* typed, gives itself *)
13+ Word = ABS(U16(65535));
14+ Sum = ABS(-2) + ABS(3) * 10; (* 32 *)
15+
16+proc show(n: I64);
17+begin
18+ Out.int(n); Out.char(' ');
19+end;
20+
21+proc i8(x: I8);
22+begin
23+ show(I64(ABS(x))); show(I64(-x));
24+end;
25+
26+proc i16(x: I16);
27+begin
28+ show(I64(ABS(x))); show(I64(-x));
29+end;
30+
31+proc i32(x: I32);
32+begin
33+ show(I64(ABS(x))); show(I64(-x));
34+end;
35+
36+proc i64(x: I64);
37+begin
38+ show(I64(ABS(x))); show(I64(-x));
39+end;
40+
41+proc int(x: INT);
42+begin
43+ show(I64(ABS(x))); show(I64(-x));
44+end;
45+
46+proc main*;
47+var a, b: I8; u8: U8; u16: U16; u32: U32; u64: U64; n: INT; t: I8;
48+begin
49+ i8(0); i8(7); i8(-7); i8(127); i8(-127); i8(-128); Out.ln;
50+ i16(5); i16(-5); i16(32767); i16(-32768); Out.ln;
51+ i32(9); i32(-9); i32(2147483647); i32(-2147483648); Out.ln;
52+ i64(1); i64(-1); i64(MAX(I64)); i64(MIN(I64)); Out.ln;
53+ int(-42); int(42); int(MIN(INT)); Out.ln;
54+
55+ u8 := 200; u16 := 65535; u32 := 0FFFFFFFFH; u64 := 0FFFFFFFFFFFFFFFFH;
56+ show(I64(ABS(u8))); show(I64(ABS(u16))); show(I64(ABS(u32))); Out.hex(ABS(u64), 16); Out.ln;
57+
58+ (* the sum wraps to -128 in an I8, which ABS keeps *)
59+ a := 100; b := 28;
60+ t := a + b;
61+ show(I64(t)); show(I64(ABS(I8(a + b)))); show(I64(ABS(a - b - 100))); Out.ln;
62+
63+ (* beside other values on the stack *)
64+ n := -3;
65+ show(10 * ABS(n) + ABS(n - 4) * ABS(-n)); Out.ln;
66+
67+ show(Five); show(Big); show(I64(Byte)); show(I64(Word)); show(Sum); Out.ln;
68+end;
69+
70+end Abs.

conform/run/AbsReal.expect (history) added +5 -0

@@ -0,0 +1,5 @@
1+3FF8000000000000 3FF8000000000000 0000000000000000 0000000000000000 7FF0000000000000 7FF0000000000000 7FF8000000000000 7FF8000000000000
2+3FC00000 3FC00000 00000000 00000000 7F800000 7F800000 7FC00000 7FC00000
3+51
4+NaN
5+2 2 3 0000000000000000

conform/run/AbsReal.raz (history) added +48 -0

@@ -0,0 +1,48 @@
1+(* ABS(x) of a real clears its sign bit (report section 6.1): of F32
2+ and F64 variables, ordinary values, minus zero, infinities and NaNs,
3+ each shown by its bits, and of constants, in a constant too. *)
4+module AbsReal;
5+import Out;
6+
7+const
8+ Half = ABS(-0.5); (* untyped *)
9+ Wide = ABS(F64(-2.0E300));
10+ Narrow = ABS(F32(-1.5));
11+ Zero = ABS(-0.0);
12+
13+proc f64(x: F64);
14+var y: F64;
15+begin
16+ y := ABS(x);
17+ Out.hex(y.(U64), 16); Out.char(' ');
18+end;
19+
20+proc f32(x: F32);
21+var y: F32;
22+begin
23+ y := ABS(x);
24+ Out.hex(U64(y.(U32)), 8); Out.char(' ');
25+end;
26+
27+proc main*;
28+var zero, inf, nan, d: F64; zero32, inf32, nan32: F32;
29+begin
30+ zero := 0.0; inf := 1.0 / zero; nan := zero / zero;
31+ zero32 := 0.0; inf32 := 1.0 / zero32; nan32 := zero32 / zero32;
32+
33+ f64(1.5); f64(-1.5); f64(-0.0); f64(0.0); f64(inf); f64(-inf); f64(nan); f64(-nan); Out.ln;
34+ f32(1.5); f32(-1.5); f32(-0.0); f32(0.0); f32(inf32); f32(-inf32); f32(nan32); f32(-nan32); Out.ln;
35+
36+ (* beside other values on the stack *)
37+ d := -3.0;
38+ Out.int(I64(10.0 * ABS(d) + ABS(d - 4.0) * ABS(-d))); Out.ln;
39+ if ABS(nan) # ABS(nan) then Out.str("NaN"); Out.ln; end;
40+
41+ Out.int(I64(Half * 4.0)); Out.char(' ');
42+ Out.int(I64(Wide / 1.0E300)); Out.char(' ');
43+ Out.int(I64(Narrow * 2.0)); Out.char(' ');
44+ d := Zero;
45+ Out.hex(d.(U64), 16); Out.ln;
46+end;
47+
48+end AbsReal.

conform/run/AbsReal.reals (history) added +0 -0

An empty file.

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

@@ -1,6 +1,6 @@
11 # Reading the raz compiler
22
3-The compiler is eleven files of raz, about 12,300 lines of code (the
3+The compiler is eleven files of raz, about 12,400 lines of code (the
44 budget is 14,000). It is its own compiler: a release of it, the one
55 `BOOT` names, builds `src/`, and `make fixpoint` checks that what it
66 makes makes itself again, byte for byte. Each release was built by the
@@ -18,12 +18,12 @@
1818 |---|---|---|---|
1919 | `src/Diag.raz` | 422 | 324 | Messages: positions, source excerpts, colour, counts |
2020 | `src/Scan.raz` | 1,169 | 965 | Symbols, literals, comments, positions |
21-| `src/Sym.raz` | 723 | 581 | Types, objects, scopes, layout |
21+| `src/Sym.raz` | 724 | 582 | Types, objects, scopes, layout |
2222 | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes |
23-| `src/X64.raz` | 1,605 | 1,318 | The instruction encoder, jumps within a procedure, the assembler |
24-| `src/Gen.raz` | 2,800 | 1,963 | The stack machine, for x86-64: values, operations, checks, calls, frames |
25-| `src/Item.raz` | 2,414 | 1,938 | Items: types, constants, variables, operators, conversions, calls |
26-| `src/Parse.raz` | 4,611 | 3,956 | Declarations, statements, expressions, built-ins, recovery, warnings |
23+| `src/X64.raz` | 1,620 | 1,329 | The instruction encoder, jumps within a procedure, the assembler |
24+| `src/Gen.raz` | 2,828 | 1,978 | The stack machine, for x86-64: values, operations, checks, calls, frames |
25+| `src/Item.raz` | 2,442 | 1,959 | Items: types, constants, variables, operators, conversions, calls |
26+| `src/Parse.raz` | 4,614 | 3,959 | 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` | 418 | 333 | The command line |
@@ -1354,6 +1354,22 @@
13541354 mov rax, r; shr rax, 4; add r, rax; mov rdx, 0F0F...H; and r, rdx
13551355 mov rdx, 0101...H; imul r, rdx; shr r, 56
13561356
1357+`abs` makes `ABS` of a signed integer with no branch: it negates `x`
1358+and takes back the copy it kept in rax if the negation is negative, as
1359+it is for a positive `x`. The smallest value negates to itself, which
1360+is negative, and so gives itself, as `-x` does, with no check, as `-x`
1361+has none. A narrow value is negated in 64 bits, where its smallest
1362+isn't the smallest, and cut back to its width after, as `neg` does:
1363+-128 in an `I8` negates to 128, which in 8 bits is -128 again. A real
1364+has its sign bit cleared in rax, as `neg` flips it; an unsigned integer
1365+needs no code.
1366+
1367+ ABS(x), an I32 ABS(x), an F64
1368+ mov rax, r movq rax, x
1369+ neg r btr rax, 63
1370+ cmovs r, rax movq x, rax
1371+ movsxd r, r
1372+
13571373 Checks: `check` jumps, when the check fails, to code at the end of the
13581374 procedure (`endFails`), so that a check that holds is a jump not taken.
13591375 That code puts the values involved where `Rt.fail` wants them and calls
@@ -1923,6 +1939,13 @@
19231939 type, as `rotand` does; folds a constant by counting its bits one by one
19241940 (`countOf`); or has `Gen.countZeros` or `Gen.popCount` count them. The
19251941 result is an `INT`.
1942+
1943+`ABS(x)` (report 2026-10.36) is Item's `absolute`, which refuses an `x`
1944+not an integer or a real; folds a constant, as `neg` does, a real's by
1945+clearing the top bit of the `F64` it is held as and the smallest
1946+untyped constant an error, as its negation is beyond the `I64`s; or,
1947+for a signed integer or a real, has `Gen.abs` make it. The result is
1948+of `x`'s type, a built-in's, not an operator's, and widens.
19261949
19271950 Two mismatches in `coerce` that people meet on the way to it, a slice
19281951 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" "OFFSET" "ADR" "LEN" "PTR" "BYTES" "NEW" "MSG" "SYSCALL" "FENCE" "CAS" "XCHG" "XADD"
36- "ARGC" "ARGV" "INC" "DEC" "COPY" "FILL" "ROL" "ROR" "MIN" "MAX"
36+ "ARGC" "ARGV" "INC" "DEC" "COPY" "FILL" "ROL" "ROR" "MIN" "MAX" "ABS"
3737 "CLZ" "CTZ" "POPCOUNT"))
3838
3939 (defvar raz-mode-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 OFFSET ADR LEN PTR BYTES NEW MSG SYSCALL FENCE CAS XCHG XADD
18-syntax keyword razBuiltin ARGC ARGV INC DEC COPY FILL ROL ROR MIN MAX
18+syntax keyword razBuiltin ARGC ARGV INC DEC COPY FILL ROL ROR MIN MAX ABS
1919 syntax keyword razBuiltin CLZ CTZ POPCOUNT
2020 syntax keyword razConstant true false nil
2121

src/Gen.raz modified +28 -0

@@ -1138,6 +1138,34 @@
11381138 stack[depth - 1].k := kWord;
11391139 end;
11401140
1141+(* ABS of the real, or the signed integer of size bytes, on top (report
1142+ section 6.1); an unsigned one needs no code. A real's sign bit is
1143+ cleared, in rax, as neg flips it. An integer is negated, and its copy
1144+ in rax taken back if the negation is negative (SF), as it is for a
1145+ positive one. The smallest value negates to itself, negative, and so
1146+ gives itself, as -x does, with no branch and no check. A narrow
1147+ value is negated on all 64 bits, where its smallest is not the
1148+ smallest, so it is cut back to its width after, as neg does:
1149+ -128 in an I8 negates to 128, which is -128 again in 8 bits.
1150+
1151+ movq rax, x; btr rax, 63; movq x, rax (F64; 31 for an F32)
1152+ mov rax, r; neg r; cmovs r, rax (I64; then movsx r, r for a narrow one) *)
1153+proc abs*(k: Kind; size: INT);
1154+var r: INT;
1155+begin
1156+ r := top();
1157+ if k in {kF32, kF64} then
1158+ X.movqRX(RAX, r);
1159+ if k = kF32 then X.btr(RAX, 31); else X.btr(RAX, 63); end;
1160+ X.movqXR(r, RAX);
1161+ else
1162+ X.movRR(RAX, r);
1163+ X.unary(3, r); (* neg *)
1164+ X.cmov(X.ccS, r, RAX);
1165+ if size < 8 then X.extend(r, size, true); end;
1166+ end;
1167+end;
1168+
11411169 (* The left op the right of the two values on top, as a BOOL *)
11421170 proc compare*(sy: Scan.Sym; k: Kind; signed: BOOL);
11431171 var f: BOOL; r, x, y, i: INT; v: I64; c: BOOL; m: X.Mem;

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

@@ -1643,6 +1643,34 @@
16431643 return nil;
16441644 end;
16451645
1646+(* ABS(x): x without its sign, of x's type, an integer or a real (report
1647+ section 6.1). A signed integer's smallest value gives itself, as -x
1648+ does, with no check, as -x has none; an untyped constant's, whose
1649+ result would be beyond the I64s, is an error, as it is for -x. An
1650+ unsigned integer is itself. Of a constant it is folded: a real's
1651+ sign bit, the top one of the F64 it is held as, cleared. *)
1652+proc absolute*(var x: Item): ERROR;
1653+begin
1654+ if x.mode in {mType, mBuiltin, mNone, mCase} then return notValue(x); end;
1655+ if (x.mode = mStr) or not (Sym.isInt(x.typ) or (x.typ^.form = Sym.fReal)) then
1656+ return Scan.markAt(x.pos, say1("ABS needs an integer or a real, found {}", what(x)));
1657+ end;
1658+ if x.mode = mConst then
1659+ if x.typ^.form = Sym.fReal then
1660+ x.val := x.val & MaxI64;
1661+ elsif (x.typ^.form = Sym.fConst) and (x.val = MinI64) then
1662+ return Scan.markAt(x.pos, "the constant is too big for I64");
1663+ elsif signed(x.typ) and (x.val < 0) then
1664+ x.val := cut(-x.val, x.typ);
1665+ end;
1666+ else
1667+ try value(x);
1668+ if signed(x.typ) or (x.typ^.form = Sym.fReal) then Gen.abs(Gen.kindOf(x.typ), intSize(x.typ)); end;
1669+ end;
1670+ x.made := false; (* a built-in's result, not an operator's *)
1671+ return nil;
1672+end;
1673+
16461674 (* CLZ, CTZ or POPCOUNT, which, of v, a value w bits wide, folded *)
16471675 proc countOf(which: INT; v: U64; w: I64): I64;
16481676 var n, i: I64;

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

@@ -1236,6 +1236,9 @@
12361236 at := Scan.tokPos();
12371237 t := try parseType();
12381238 try Item.limit(x, t, which = Sym.bMax, Scan.spanFrom(at));
1239+ | Sym.bAbs:
1240+ try expression(x, nil);
1241+ try Item.absolute(x);
12391242 | Sym.bClz, Sym.bCtz, Sym.bPopcount:
12401243 try expression(x, nil);
12411244 try Item.countBits(x, which);

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

@@ -132,7 +132,7 @@
132132 bFence* = 6; bCas* = 7; bXchg* = 8; bXadd* = 9; bArgc* = 10; bArgv* = 11;
133133 bInc* = 12; bDec* = 13; bCopy* = 14; bPtr* = 15; bRol* = 16; bRor* = 17;
134134 bMin* = 18; bMax* = 19; bClz* = 20; bCtz* = 21; bPopcount* = 22;
135- bOffset* = 23; bBytes* = 24; bFill* = 25;
135+ bOffset* = 23; bBytes* = 24; bFill* = 25; bAbs* = 26;
136136
137137 MaxDepth* = 64; (* scopes open at once *)
138138
@@ -717,6 +717,7 @@
717717 enter("ROR", oBuiltin, nil, bRor);
718718 enter("MIN", oBuiltin, nil, bMin);
719719 enter("MAX", oBuiltin, nil, bMax);
720+ enter("ABS", oBuiltin, nil, bAbs);
720721 enter("CLZ", oBuiltin, nil, bClz);
721722 enter("CTZ", oBuiltin, nil, bCtz);
722723 enter("POPCOUNT", oBuiltin, nil, bPopcount);

src/X64.raz (history) modified +15 -0

@@ -847,6 +847,21 @@
847847 Chunk.put1(bit);
848848 end;
849849
850+(* btr r, bit: the bit cleared, 0F BA /6 *)
851+proc btr*(r, bit: INT);
852+begin
853+ if listing then say(text2("btr", rn(r), num(bit))); end;
854+ opR(0, true, 0BA0FH, 6, r, false);
855+ Chunk.put1(bit);
856+end;
857+
858+(* cmovcc r, x: r := x if the condition holds, 0F 40+cc *)
859+proc cmov*(cc, r, x: INT);
860+begin
861+ if listing then say(text2(Diag.say("cmov{}", [Fmt.str(CondNames[cc])]), rn(r), rn(x))); end;
862+ opR(0, true, 400FH + (cc shl 8), r, x, false);
863+end;
864+
850865 (* Jumps. A label not yet placed is the chain of the jumps to it, each
851866 rel32 field holding the position of the one before, NoLabel ending
852867 it. Placing the label walks the chain and patches each. *)

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

@@ -0,0 +1,4 @@
1+conform/err/AbsBool.raz:5:14: error: ABS needs an integer or a real, found BOOL
2+ 5 | b := ABS(true);
3+ | ^^^^
4+raz: 1 error

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

@@ -0,0 +1,4 @@
1+conform/err/AbsPtr.raz:6:14: error: ABS needs an integer or a real, found ^INT
2+ 6 | p := ABS(p);
3+ | ^
4+raz: 1 error

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

@@ -0,0 +1,4 @@
1+conform/err/AbsSet.raz:9:14: error: ABS needs an integer or a real, found Days
2+ 9 | s := ABS(s);
3+ | ^
4+raz: 1 error

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

@@ -0,0 +1,4 @@
1+conform/err/AbsString.raz:2:15: error: ABS needs an integer or a real, found a string
2+ 2 | const N = ABS("ten");
3+ | ^^^^^
4+raz: 1 error

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

@@ -0,0 +1,4 @@
1+conform/err/AbsUntyped.raz:2:15: error: the constant is too big for I64
2+ 2 | const N = ABS(MIN(I64));
3+ | ^^^^^^^^
4+raz: 1 error