MIN and MAX: the limits of an integer type, as report 2026-10.36 has them The built-ins MIN(T) and MAX(T): Sym enters them, Parse's builtin reads T with parseType, and Item's limit refuses a T not an integer type and makes the constant: as many ones as T has bits, shifted once more if T is signed, is MAX, and minus that less 1 a signed MIN. They are untyped constants but for MAX(U64) and MAX(UINT), which are above high(I64), where an untyped constant ends: they keep their type, as a hexadecimal number above it is a U64. Tests: conform/run/Limits (each integer type, in a constant, untyped arithmetic on them, each into its own type and one past it); conform/err MinReal, MaxEnum and MaxValue, their messages in test/msg. compiler.md, the editors' built-in lists and CLAUDE.md's readings say so. Code lines 11,880 -> 11,902.
| @@ -339,3 +339,7 @@ | ||
| 339 | 339 | width as a shift's is, `n & (w - 1)`: `ROL(x, -1)` is `ROR(x, 1)`. |
| 340 | 340 | The result is a built-in's, not an operator's, and widens (`made` |
| 341 | 341 | is false). Of two constants they fold, in a constant too. |
| 342 | +- `MIN(T)` and `MAX(T)` (report 2026-10.36) are untyped constants, but | |
| 343 | + `MAX(U64)` and `MAX(UINT)`, above `high(I64)`, keep their type, as a | |
| 344 | + hex literal above it is a `U64`. A `T` not an integer type is an | |
| 345 | + error; `MAX(x)` of a variable is "x isn't a type". |
| @@ -0,0 +1,8 @@ | ||
| 1 | +module MaxEnum; | |
| 2 | +type Color = (red, green, blue); | |
| 3 | +proc main*; | |
| 4 | +var n: INT; | |
| 5 | +begin | |
| 6 | + n := MAX(Color); | |
| 7 | +end; | |
| 8 | +end MaxEnum. |
| @@ -0,0 +1,7 @@ | ||
| 1 | +module MaxValue; | |
| 2 | +proc main*; | |
| 3 | +var n, x: INT; | |
| 4 | +begin | |
| 5 | + n := MAX(x); | |
| 6 | +end; | |
| 7 | +end MaxValue. |
| @@ -0,0 +1,3 @@ | ||
| 1 | +module MinReal; | |
| 2 | +const Least = MIN(F64); | |
| 3 | +end MinReal. |
| @@ -0,0 +1,13 @@ | ||
| 1 | +-128 127 | |
| 2 | +-32768 32767 | |
| 3 | +-2147483648 2147483647 | |
| 4 | +-9223372036854775808 9223372036854775807 | |
| 5 | +-9223372036854775808 9223372036854775807 | |
| 6 | +0 255 | |
| 7 | +0 65535 | |
| 8 | +0 4294967295 | |
| 9 | +0 FFFFFFFFFFFFFFFF | |
| 10 | +0 FFFFFFFFFFFFFFFF | |
| 11 | +256 65535 FFFFFFFFFFFFFFFF | |
| 12 | +-128 32767 -2147483648 9223372036854775807 -9223372036854775808 | |
| 13 | +0 0 0 0 0 |
| @@ -0,0 +1,41 @@ | ||
| 1 | +(* MIN(T) and MAX(T), the smallest and the largest value of each integer | |
| 2 | + type (report section 6.1), untyped constants: in a constant, in | |
| 3 | + arithmetic of their own, and given to each type. MAX(U64) and | |
| 4 | + MAX(UINT) are above the largest I64. *) | |
| 5 | +module Limits; | |
| 6 | +import Out; | |
| 7 | + | |
| 8 | +const | |
| 9 | + Byte = MAX(U8) + 1; (* untyped: no wrap at 255 *) | |
| 10 | + Span = MAX(I16) - MIN(I16); | |
| 11 | + All = MAX(U64); | |
| 12 | + | |
| 13 | +var | |
| 14 | + i8: I8; i16: I16; i32: I32; i64: I64; int: INT; | |
| 15 | + u8: U8; u16: U16; u32: U32; u64: U64; uint: UINT; | |
| 16 | + | |
| 17 | +proc main*; | |
| 18 | +begin | |
| 19 | + Out.int(MIN(I8)); Out.str(" "); Out.int(MAX(I8)); Out.ln; | |
| 20 | + Out.int(MIN(I16)); Out.str(" "); Out.int(MAX(I16)); Out.ln; | |
| 21 | + Out.int(MIN(I32)); Out.str(" "); Out.int(MAX(I32)); Out.ln; | |
| 22 | + Out.int(MIN(I64)); Out.str(" "); Out.int(MAX(I64)); Out.ln; | |
| 23 | + Out.int(MIN(INT)); Out.str(" "); Out.int(MAX(INT)); Out.ln; | |
| 24 | + Out.int(MIN(U8)); Out.str(" "); Out.int(MAX(U8)); Out.ln; | |
| 25 | + Out.int(MIN(U16)); Out.str(" "); Out.int(MAX(U16)); Out.ln; | |
| 26 | + Out.int(MIN(U32)); Out.str(" "); Out.int(MAX(U32)); Out.ln; | |
| 27 | + Out.int(MIN(U64)); Out.str(" "); Out.hex(MAX(U64), 16); Out.ln; | |
| 28 | + Out.int(MIN(UINT)); Out.str(" "); Out.hex(U64(MAX(UINT)), 16); Out.ln; | |
| 29 | + Out.int(Byte); Out.str(" "); Out.int(Span); Out.str(" "); Out.hex(All, 16); Out.ln; | |
| 30 | + (* each into its own type, and one past it wraps *) | |
| 31 | + i8 := MAX(I8); i16 := MIN(I16); i32 := MAX(I32); i64 := MIN(I64); int := MAX(INT); | |
| 32 | + u8 := MAX(U8); u16 := MAX(U16); u32 := MAX(U32); u64 := MAX(U64); uint := MAX(UINT); | |
| 33 | + INC(i8); DEC(i16); INC(i32); DEC(i64); INC(int); | |
| 34 | + INC(u8); INC(u16); INC(u32); INC(u64); INC(uint); | |
| 35 | + Out.int(I64(i8)); Out.str(" "); Out.int(I64(i16)); Out.str(" "); Out.int(I64(i32)); Out.str(" "); | |
| 36 | + Out.int(i64); Out.str(" "); Out.int(int); Out.ln; | |
| 37 | + Out.int(I64(u8)); Out.str(" "); Out.int(I64(u16)); Out.str(" "); Out.int(I64(u32)); Out.str(" "); | |
| 38 | + Out.hex(u64, 1); Out.str(" "); Out.hex(U64(uint), 1); Out.ln; | |
| 39 | +end; | |
| 40 | + | |
| 41 | +end Limits. |
| @@ -18,12 +18,12 @@ | ||
| 18 | 18 | |---|---|---|---| |
| 19 | 19 | | `src/Diag.raz` | 422 | 324 | Messages: positions, source excerpts, colour, counts | |
| 20 | 20 | | `src/Scan.raz` | 1,169 | 965 | Symbols, literals, comments, positions | |
| 21 | -| `src/Sym.raz` | 715 | 573 | Types, objects, scopes, layout | | |
| 21 | +| `src/Sym.raz` | 718 | 576 | Types, objects, scopes, layout | | |
| 22 | 22 | | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes | |
| 23 | 23 | | `src/X64.raz` | 1,584 | 1,304 | The instruction encoder, jumps within a procedure, the assembler | |
| 24 | 24 | | `src/Gen.raz` | 2,592 | 1,843 | 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,396 | 3,796 | Declarations, statements, expressions, built-ins, recovery, warnings | | |
| 25 | +| `src/Item.raz` | 2,244 | 1,800 | Items: types, constants, variables, operators, conversions, calls | | |
| 26 | +| `src/Parse.raz` | 4,400 | 3,800 | Declarations, statements, expressions, built-ins, recovery, warnings | | |
| 27 | 27 | | `src/Link.raz` | 407 | 315 | Tree shaking, layout, patching, the places | |
| 28 | 28 | | `src/Elf.raz` | 449 | 355 | Static and dynamic ELF, symbol table, line numbers | |
| 29 | 29 | | `src/Raz.raz` | 418 | 333 | The command line | |
| @@ -1636,6 +1636,14 @@ | ||
| 1636 | 1636 | `op1` loads an operator's left; and `rotate`, which folds two constants, |
| 1637 | 1637 | right by k as left by the width less k, or has `Gen.rotate` make `rol` |
| 1638 | 1638 | or `ror`. |
| 1639 | + | |
| 1640 | +`MIN(T)` and `MAX(T)` (report 2026-10.36) are Item's `limit`, which | |
| 1641 | +refuses a `T` not an integer type and makes the constant: as many ones | |
| 1642 | +as `T` has bits, shifted right once more if `T` is signed, is `MAX`, | |
| 1643 | +and minus that less 1 is a signed `MIN`. They are untyped constants | |
| 1644 | +but for `MAX(U64)` and `MAX(UINT)`, which are above `high(I64)`, where | |
| 1645 | +an untyped constant ends, and keep their type, as a hexadecimal number | |
| 1646 | +above it is a `U64`. | |
| 1639 | 1647 | |
| 1640 | 1648 | Two mismatches in `coerce` that people meet on the way to it, a slice |
| 1641 | 1649 | where its element's pointer is wanted and a pointer where a slice is, |
| @@ -33,7 +33,7 @@ | ||
| 33 | 33 | |
| 34 | 34 | (defconst raz-builtins |
| 35 | 35 | '("SIZE" "ADR" "LEN" "PTR" "NEW" "MSG" "SYSCALL" "FENCE" "CAS" "XCHG" "XADD" |
| 36 | - "ARGC" "ARGV" "INC" "DEC" "COPY" "ROL" "ROR")) | |
| 36 | + "ARGC" "ARGV" "INC" "DEC" "COPY" "ROL" "ROR" "MIN" "MAX")) | |
| 37 | 37 | |
| 38 | 38 | (defvar raz-mode-syntax-table |
| 39 | 39 | (let ((st (make-syntax-table))) |
| @@ -15,7 +15,7 @@ | ||
| 15 | 15 | syntax keyword razKeyword var variant VOLATILE while xor |
| 16 | 16 | syntax keyword razType I8 I16 I32 I64 INT UINT U8 U16 U32 U64 BOOL F32 F64 ERROR |
| 17 | 17 | syntax keyword razBuiltin SIZE ADR LEN PTR NEW MSG SYSCALL FENCE CAS XCHG XADD |
| 18 | -syntax keyword razBuiltin ARGC ARGV INC DEC COPY ROL ROR | |
| 18 | +syntax keyword razBuiltin ARGC ARGV INC DEC COPY ROL ROR MIN MAX | |
| 19 | 19 | syntax keyword razConstant true false nil |
| 20 | 20 | |
| 21 | 21 | " 123, 0FFH, 1.5, 2.0E-3 |
| @@ -1560,6 +1560,27 @@ | ||
| 1560 | 1560 | return nil; |
| 1561 | 1561 | end; |
| 1562 | 1562 | |
| 1563 | +(* MIN(T), or MAX(T) if max: the smallest or the largest value of the | |
| 1564 | + integer type t, an untyped constant (report section 6.1); p is where | |
| 1565 | + T is written. MAX(U64) and MAX(UINT) are above high(I64), where the | |
| 1566 | + untyped constants end, so they keep their type, as a hexadecimal | |
| 1567 | + number above it is a U64 (report section 3). *) | |
| 1568 | +proc limit*(var x: Item; t: ^Sym.Type; max: BOOL; p: Diag.Pos): ERROR; | |
| 1569 | +var name: []U8; ones: U64; v: I64; | |
| 1570 | +begin | |
| 1571 | + if t^.form # Sym.fInt then | |
| 1572 | + if max then name := "MAX"; else name := "MIN"; end; | |
| 1573 | + return Scan.markAt(p, say2("{} needs an integer type, found {}", name, Sym.typeName(t))); | |
| 1574 | + end; | |
| 1575 | + ones := (~U64(0)) shr (64 - bits(t)); (* as many ones as t has bits *) | |
| 1576 | + if t^.signed then ones := ones shr 1; end; | |
| 1577 | + if max then v := I64(ones); | |
| 1578 | + elsif t^.signed then v := -I64(ones) - 1; | |
| 1579 | + else v := 0; end; | |
| 1580 | + if max and (v < 0) then makeConst(x, t, v); else makeConst(x, Sym.tConst, v); end; | |
| 1581 | + return nil; | |
| 1582 | +end; | |
| 1583 | + | |
| 1563 | 1584 | proc convert*(var x: Item; t: ^Sym.Type): ERROR; |
| 1564 | 1585 | var s: ^Sym.Type; r: F64; |
| 1565 | 1586 | begin |
| @@ -1003,7 +1003,7 @@ | ||
| 1003 | 1003 | end; |
| 1004 | 1004 | |
| 1005 | 1005 | proc builtin(var x: TItem): ERROR; |
| 1006 | -var which, n: INT; y: TItem; t: PType; | |
| 1006 | +var which, n: INT; y: TItem; t: PType; start: Diag.Pos; | |
| 1007 | 1007 | begin |
| 1008 | 1008 | which := INT(x.obj^.val); |
| 1009 | 1009 | if (which = Sym.bFence) or (which = Sym.bArgc) or (which = Sym.bArgv) then |
| @@ -1102,6 +1102,10 @@ | ||
| 1102 | 1102 | try expect(Scan.sComma); |
| 1103 | 1103 | try expression(y, nil); |
| 1104 | 1104 | try Item.rotate(x, y, which = Sym.bRol); |
| 1105 | + | Sym.bMin, Sym.bMax: | |
| 1106 | + start := Scan.tokPos(); | |
| 1107 | + t := try parseType(); | |
| 1108 | + try Item.limit(x, t, which = Sym.bMax, Scan.spanFrom(start)); | |
| 1105 | 1109 | | Sym.bCopy: |
| 1106 | 1110 | try expression(x, nil); |
| 1107 | 1111 | try Item.copyDest(x); |
| @@ -132,6 +132,7 @@ | ||
| 132 | 132 | bSize* = 0; bAdr* = 1; bLen* = 2; bNew* = 3; bMsg* = 4; bSyscall* = 5; |
| 133 | 133 | bFence* = 6; bCas* = 7; bXchg* = 8; bXadd* = 9; bArgc* = 10; bArgv* = 11; |
| 134 | 134 | bInc* = 12; bDec* = 13; bCopy* = 14; bPtr* = 15; bRol* = 16; bRor* = 17; |
| 135 | + bMin* = 18; bMax* = 19; | |
| 135 | 136 | |
| 136 | 137 | MaxDepth* = 64; (* scopes open at once *) |
| 137 | 138 | |
| @@ -712,4 +713,6 @@ | ||
| 712 | 713 | enter("COPY", oBuiltin, nil, bCopy); |
| 713 | 714 | enter("ROL", oBuiltin, nil, bRol); |
| 714 | 715 | enter("ROR", oBuiltin, nil, bRor); |
| 716 | + enter("MIN", oBuiltin, nil, bMin); | |
| 717 | + enter("MAX", oBuiltin, nil, bMax); | |
| 715 | 718 | end Sym. |
| @@ -0,0 +1,4 @@ | ||
| 1 | +conform/err/MaxEnum.raz:6:14: error: MAX needs an integer type, found Color | |
| 2 | + 6 | n := MAX(Color); | |
| 3 | + | ^^^^^ | |
| 4 | +raz: 1 error |
| @@ -0,0 +1,4 @@ | ||
| 1 | +conform/err/MaxValue.raz:5:14: error: x isn't a type | |
| 2 | + 5 | n := MAX(x); | |
| 3 | + | ^ | |
| 4 | +raz: 1 error |
| @@ -0,0 +1,4 @@ | ||
| 1 | +conform/err/MinReal.raz:2:19: error: MIN needs an integer type, found F64 | |
| 2 | + 2 | const Least = MIN(F64); | |
| 3 | + | ^^^ | |
| 4 | +raz: 1 error |