From f1ef1ab0bfee480555dfa2c806ba08713fcdc499 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 23 Oct 2026 12:18:22 +0100 Subject: [PATCH] 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. --- diff --git a/CLAUDE.md b/CLAUDE.md index d167536..aac4ab9 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -339,3 +339,7 @@ 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. +- `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 + error; `MAX(x)` of a variable is "x isn't a type". diff --git a/conform/err/MaxEnum.raz b/conform/err/MaxEnum.raz new file mode 100644 index 0000000..879a168 --- /dev/null +++ b/conform/err/MaxEnum.raz @@ -0,0 +1,8 @@ +module MaxEnum; +type Color = (red, green, blue); +proc main*; +var n: INT; +begin + n := MAX(Color); +end; +end MaxEnum. diff --git a/conform/err/MaxValue.raz b/conform/err/MaxValue.raz new file mode 100644 index 0000000..7d6477f --- /dev/null +++ b/conform/err/MaxValue.raz @@ -0,0 +1,7 @@ +module MaxValue; +proc main*; +var n, x: INT; +begin + n := MAX(x); +end; +end MaxValue. diff --git a/conform/err/MinReal.raz b/conform/err/MinReal.raz new file mode 100644 index 0000000..8640dcd --- /dev/null +++ b/conform/err/MinReal.raz @@ -0,0 +1,3 @@ +module MinReal; +const Least = MIN(F64); +end MinReal. diff --git a/conform/run/Limits.expect b/conform/run/Limits.expect new file mode 100644 index 0000000..9b5e411 --- /dev/null +++ b/conform/run/Limits.expect @@ -0,0 +1,13 @@ +-128 127 +-32768 32767 +-2147483648 2147483647 +-9223372036854775808 9223372036854775807 +-9223372036854775808 9223372036854775807 +0 255 +0 65535 +0 4294967295 +0 FFFFFFFFFFFFFFFF +0 FFFFFFFFFFFFFFFF +256 65535 FFFFFFFFFFFFFFFF +-128 32767 -2147483648 9223372036854775807 -9223372036854775808 +0 0 0 0 0 diff --git a/conform/run/Limits.raz b/conform/run/Limits.raz new file mode 100644 index 0000000..1be6720 --- /dev/null +++ b/conform/run/Limits.raz @@ -0,0 +1,41 @@ +(* MIN(T) and MAX(T), the smallest and the largest value of each integer + type (report section 6.1), untyped constants: in a constant, in + arithmetic of their own, and given to each type. MAX(U64) and + MAX(UINT) are above the largest I64. *) +module Limits; +import Out; + +const + Byte = MAX(U8) + 1; (* untyped: no wrap at 255 *) + Span = MAX(I16) - MIN(I16); + All = MAX(U64); + +var + i8: I8; i16: I16; i32: I32; i64: I64; int: INT; + u8: U8; u16: U16; u32: U32; u64: U64; uint: UINT; + +proc main*; +begin + Out.int(MIN(I8)); Out.str(" "); Out.int(MAX(I8)); Out.ln; + Out.int(MIN(I16)); Out.str(" "); Out.int(MAX(I16)); Out.ln; + Out.int(MIN(I32)); Out.str(" "); Out.int(MAX(I32)); Out.ln; + Out.int(MIN(I64)); Out.str(" "); Out.int(MAX(I64)); Out.ln; + Out.int(MIN(INT)); Out.str(" "); Out.int(MAX(INT)); Out.ln; + Out.int(MIN(U8)); Out.str(" "); Out.int(MAX(U8)); Out.ln; + Out.int(MIN(U16)); Out.str(" "); Out.int(MAX(U16)); Out.ln; + Out.int(MIN(U32)); Out.str(" "); Out.int(MAX(U32)); Out.ln; + Out.int(MIN(U64)); Out.str(" "); Out.hex(MAX(U64), 16); Out.ln; + Out.int(MIN(UINT)); Out.str(" "); Out.hex(U64(MAX(UINT)), 16); Out.ln; + Out.int(Byte); Out.str(" "); Out.int(Span); Out.str(" "); Out.hex(All, 16); Out.ln; + (* each into its own type, and one past it wraps *) + i8 := MAX(I8); i16 := MIN(I16); i32 := MAX(I32); i64 := MIN(I64); int := MAX(INT); + u8 := MAX(U8); u16 := MAX(U16); u32 := MAX(U32); u64 := MAX(U64); uint := MAX(UINT); + INC(i8); DEC(i16); INC(i32); DEC(i64); INC(int); + INC(u8); INC(u16); INC(u32); INC(u64); INC(uint); + Out.int(I64(i8)); Out.str(" "); Out.int(I64(i16)); Out.str(" "); Out.int(I64(i32)); Out.str(" "); + Out.int(i64); Out.str(" "); Out.int(int); Out.ln; + Out.int(I64(u8)); Out.str(" "); Out.int(I64(u16)); Out.str(" "); Out.int(I64(u32)); Out.str(" "); + Out.hex(u64, 1); Out.str(" "); Out.hex(U64(uint), 1); Out.ln; +end; + +end Limits. diff --git a/doc/compiler.md b/doc/compiler.md index f67f801..54a336c 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -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` | 715 | 573 | Types, objects, scopes, layout | +| `src/Sym.raz` | 718 | 576 | Types, objects, scopes, layout | | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes | | `src/X64.raz` | 1,584 | 1,304 | The instruction encoder, jumps within a procedure, the assembler | | `src/Gen.raz` | 2,592 | 1,843 | 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,396 | 3,796 | Declarations, statements, expressions, built-ins, recovery, warnings | +| `src/Item.raz` | 2,244 | 1,800 | Items: types, constants, variables, operators, conversions, calls | +| `src/Parse.raz` | 4,400 | 3,800 | 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 | @@ -1636,6 +1636,14 @@ `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`. + +`MIN(T)` and `MAX(T)` (report 2026-10.36) are Item's `limit`, which +refuses a `T` not an integer type and makes the constant: as many ones +as `T` has bits, shifted right once more if `T` is signed, is `MAX`, +and minus that less 1 is a signed `MIN`. They are untyped constants +but for `MAX(U64)` and `MAX(UINT)`, which are above `high(I64)`, where +an untyped constant ends, and keep their type, as a hexadecimal number +above it is a `U64`. 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 241de3c..6ce46d2 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" "ROL" "ROR")) + "ARGC" "ARGV" "INC" "DEC" "COPY" "ROL" "ROR" "MIN" "MAX")) (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 3b8109e..1bc7feb 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 ROL ROR +syntax keyword razBuiltin ARGC ARGV INC DEC COPY ROL ROR MIN MAX syntax keyword razConstant true false nil " 123, 0FFH, 1.5, 2.0E-3 diff --git a/src/Item.raz b/src/Item.raz index 066a1cd..cce88bf 100644 --- a/src/Item.raz +++ b/src/Item.raz @@ -1560,6 +1560,27 @@ return nil; end; +(* MIN(T), or MAX(T) if max: the smallest or the largest value of the + integer type t, an untyped constant (report section 6.1); p is where + T is written. MAX(U64) and MAX(UINT) are above high(I64), where the + untyped constants end, so they keep their type, as a hexadecimal + number above it is a U64 (report section 3). *) +proc limit*(var x: Item; t: ^Sym.Type; max: BOOL; p: Diag.Pos): ERROR; +var name: []U8; ones: U64; v: I64; +begin + if t^.form # Sym.fInt then + if max then name := "MAX"; else name := "MIN"; end; + return Scan.markAt(p, say2("{} needs an integer type, found {}", name, Sym.typeName(t))); + end; + ones := (~U64(0)) shr (64 - bits(t)); (* as many ones as t has bits *) + if t^.signed then ones := ones shr 1; end; + if max then v := I64(ones); + elsif t^.signed then v := -I64(ones) - 1; + else v := 0; end; + if max and (v < 0) then makeConst(x, t, v); else makeConst(x, Sym.tConst, v); end; + 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 44df728..8517818 100644 --- a/src/Parse.raz +++ b/src/Parse.raz @@ -1003,7 +1003,7 @@ end; proc builtin(var x: TItem): ERROR; -var which, n: INT; y: TItem; t: PType; +var which, n: INT; y: TItem; t: PType; start: Diag.Pos; begin which := INT(x.obj^.val); if (which = Sym.bFence) or (which = Sym.bArgc) or (which = Sym.bArgv) then @@ -1102,6 +1102,10 @@ try expect(Scan.sComma); try expression(y, nil); try Item.rotate(x, y, which = Sym.bRol); + | Sym.bMin, Sym.bMax: + start := Scan.tokPos(); + t := try parseType(); + try Item.limit(x, t, which = Sym.bMax, Scan.spanFrom(start)); | Sym.bCopy: try expression(x, nil); try Item.copyDest(x); diff --git a/src/Sym.raz b/src/Sym.raz index 49a4380..f141974 100644 --- a/src/Sym.raz +++ b/src/Sym.raz @@ -132,6 +132,7 @@ 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; bRol* = 16; bRor* = 17; + bMin* = 18; bMax* = 19; MaxDepth* = 64; (* scopes open at once *) @@ -712,4 +713,6 @@ enter("COPY", oBuiltin, nil, bCopy); enter("ROL", oBuiltin, nil, bRol); enter("ROR", oBuiltin, nil, bRor); + enter("MIN", oBuiltin, nil, bMin); + enter("MAX", oBuiltin, nil, bMax); end Sym. diff --git a/test/msg/MaxEnum.err b/test/msg/MaxEnum.err new file mode 100644 index 0000000..c89232d --- /dev/null +++ b/test/msg/MaxEnum.err @@ -0,0 +1,4 @@ +conform/err/MaxEnum.raz:6:14: error: MAX needs an integer type, found Color + 6 | n := MAX(Color); + | ^^^^^ +raz: 1 error diff --git a/test/msg/MaxValue.err b/test/msg/MaxValue.err new file mode 100644 index 0000000..2380d4e --- /dev/null +++ b/test/msg/MaxValue.err @@ -0,0 +1,4 @@ +conform/err/MaxValue.raz:5:14: error: x isn't a type + 5 | n := MAX(x); + | ^ +raz: 1 error diff --git a/test/msg/MinReal.err b/test/msg/MinReal.err new file mode 100644 index 0000000..2be8cc2 --- /dev/null +++ b/test/msg/MinReal.err @@ -0,0 +1,4 @@ +conform/err/MinReal.raz:2:19: error: MIN needs an integer type, found F64 + 2 | const Least = MIN(F64); + | ^^^ +raz: 1 error