From b5aa492d04fd5c89dd82375be05bd9cd828ca6f9 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 16 Oct 2026 11:09:21 +0100 Subject: [PATCH] U8, I8 and I16 widen to INT, U8 and U16 to UINT (report 2026-10.31) smallWidens, in coerce and op2: the value is loaded as its own type, extended, and only the type changes. What an operator gives is marked made and doesn't widen (madeNarrow); a conversion clears it. A U8 into an I64 says to write I64(x). The report says return f(x) passes its results on as they are. Tests in conform/, messages in test/msg. --- diff --git a/conform/err/SmallWidenI32.raz b/conform/err/SmallWidenI32.raz new file mode 100644 index 0000000..bb810f3 --- /dev/null +++ b/conform/err/SmallWidenI32.raz @@ -0,0 +1,7 @@ +module SmallWidenI32; +proc main*; +var n: INT; a: I32; +begin + n := a; +end; +end SmallWidenI32. diff --git a/conform/err/SmallWidenI64.raz b/conform/err/SmallWidenI64.raz new file mode 100644 index 0000000..5c510c7 --- /dev/null +++ b/conform/err/SmallWidenI64.raz @@ -0,0 +1,7 @@ +module SmallWidenI64; +proc main*; +var n: I64; a: U8; +begin + n := a; +end; +end SmallWidenI64. diff --git a/conform/err/SmallWidenMade.raz b/conform/err/SmallWidenMade.raz new file mode 100644 index 0000000..84178cc --- /dev/null +++ b/conform/err/SmallWidenMade.raz @@ -0,0 +1,7 @@ +module SmallWidenMade; +proc main*; +var n: INT; a, b: U8; +begin + n := a + b; +end; +end SmallWidenMade. diff --git a/conform/err/SmallWidenMadeOp.raz b/conform/err/SmallWidenMadeOp.raz new file mode 100644 index 0000000..23cb367 --- /dev/null +++ b/conform/err/SmallWidenMadeOp.raz @@ -0,0 +1,7 @@ +module SmallWidenMadeOp; +proc main*; +var n: INT; a, b: U8; +begin + n := n + (a * b); +end; +end SmallWidenMadeOp. diff --git a/conform/err/SmallWidenNeg.raz b/conform/err/SmallWidenNeg.raz new file mode 100644 index 0000000..261ae69 --- /dev/null +++ b/conform/err/SmallWidenNeg.raz @@ -0,0 +1,7 @@ +module SmallWidenNeg; +proc main*; +var n: INT; c: I8; +begin + n := -c; +end; +end SmallWidenNeg. diff --git a/conform/err/SmallWidenU16.raz b/conform/err/SmallWidenU16.raz new file mode 100644 index 0000000..c8bbd57 --- /dev/null +++ b/conform/err/SmallWidenU16.raz @@ -0,0 +1,7 @@ +module SmallWidenU16; +proc main*; +var n: INT; a: U16; +begin + n := a; +end; +end SmallWidenU16. diff --git a/conform/run/SmallWiden.expect b/conform/run/SmallWiden.expect new file mode 100644 index 0000000..4d102bc --- /dev/null +++ b/conform/run/SmallWiden.expect @@ -0,0 +1 @@ +7319 300 44 -5 255 -30000 88 200 205 -5 65535 200 diff --git a/conform/run/SmallWiden.raz b/conform/run/SmallWiden.raz new file mode 100644 index 0000000..f577a91 --- /dev/null +++ b/conform/run/SmallWiden.raz @@ -0,0 +1,52 @@ +(* A U8, I8 or I16 widens to INT, and a U8 or U16 to UINT, where it is + moved: assigned, passed, returned, as a bound or a step, and where it + meets an INT in an operator; a conversion's result too (report + 2026-10.31, sections 5.10 and 6.1). *) +module SmallWiden; +import Out; + +var g: U8; h: I16; + +proc show(n: INT); +begin + Out.int(n); Out.str(" "); +end; + +proc byte(i: INT): U8; +begin + return U8(i); +end; + +proc twice(n: INT): INT; +begin + return 2 * n; +end; + +proc wide(b: U8): INT; +begin + return b; (* returned *) +end; + +proc main*; +var s: []U8; a, b: U8; c: I8; w: U16; n, i: INT; u: UINT; +begin + s := "7319"; + for i := 0 to LEN(s) - 1 do n := n * 10 + s[i] - '0'; end; + show(n); (* an element, met by an INT *) + a := 200; b := 100; + show(INT(a) + b); (* 300: the sum made in INT *) + show(U8(a + b)); (* 44: made in U8, then converted *) + c := -5; n := c; show(n); (* a signed one keeps its sign *) + g := 255; h := -30000; + show(g); show(h); (* passed *) + show(twice(byte(300))); (* a call's result: 88 *) + show(wide(a)); + if (a > n) and (c < n) then Out.str("compared "); end; + n := 0; INC(n, a); DEC(n, c); show(n); (* 205 *) + for i := c to c do show(i); end; (* bounds *) + w := 65535; u := w; show(INT(u)); + u := a; show(INT(u)); + Out.ln; +end; + +end SmallWiden. diff --git a/doc/compiler.md b/doc/compiler.md index d541ede..bc1fbf0 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -19,7 +19,7 @@ | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes | | `src/X64.raz` | 1,517 | 1,253 | The instruction encoder, jumps within a procedure, the assembler | | `src/Gen.raz` | 2,552 | 1,821 | The stack machine, for x86-64: values, operations, checks, calls, frames | -| `src/Item.raz` | 1,986 | 1,617 | Items: types, constants, variables, operators, conversions, calls | +| `src/Item.raz` | 2,037 | 1,651 | Items: types, constants, variables, operators, conversions, calls | | `src/Parse.raz` | 4,049 | 3,518 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/Link.raz` | 381 | 298 | Tree shaking, layout, patching, the places | | `src/Elf.raz` | 431 | 344 | Static and dynamic ELF, symbol table, line numbers | @@ -1303,7 +1303,23 @@ an `I64` in an operator is `op2`'s, which gives the result the `I64`; `return f(x)` takes results that widen by `resultsFit`. None of it makes code: an `INT` and an `I64` are the same 64 bits on x86-64, so -only the item's type changes. `assign` stores, or +only the item's type changes. + +A `U8`, `I8` or `I16` goes into an `INT`, and a `U8` or `U16` into a +`UINT`, by `smallWidens`, in `coerce` and `op2` alike (report 2026-10.31). +Gen keeps every integer on its stack extended to 64 bits as its type +says, `movzx` for a `U8` and `movsx` for an `I8`, so `coerce` loads the +value as its own type (`value`) and then changes only the type: + + before: x is a U8 variable; the INT it is wanted as is an error + after: value(x) loads it, movzx r8d, byte [...]; x.typ := INT + +What an operator gives doesn't widen, since it was computed in the +narrow type and may have wrapped: `op2`, `neg` and `complement` mark +their result `made`, which `coerce` and `op2` refuse (`madeNarrow`), +and `convert` clears, so that `U8(a + b)` widens. `return f(x)` passes +results on as they are, so `resultsFit` takes only `INT` to `I64`. +`assign` stores, or copies records and arrays. `convert` is the written conversion, `T(x)`. Views, `v.(T)` (report section 6.9): a view is the item of `v` with diff --git a/doc/raz.md b/doc/raz.md index 2aa6573..d3ba311 100644 --- a/doc/raz.md +++ b/doc/raz.md @@ -682,8 +682,9 @@ A `U8`, `I8` or `I16` widens to `INT`, and a `U8` or `U16` to `UINT`, in the same places, as an `INT` to `I64`: assigned, passed, returned, in `a, b := f(x)`, to a `for`'s bounds and `INC` and `DEC`'s step, and -in an expression, where it meets an `INT` or a `UINT` (section 6.1). -They widen only that far: a `U8` meets an `I64` only as `INT(b)` or +in an expression, where it meets an `INT` or a `UINT` (section 6.1); +not in `return f(x)`, which passes `f`'s results on as they are. They +widen only that far: a `U8` meets an `I64` only as `INT(b)` or `I64(b)`. And they widen only as values that are moved: a variable, an element, a field, a call's result or a conversion's. What an operator gives on them doesn't: it was computed in their type, and has wrapped diff --git a/src/Item.raz b/src/Item.raz index 012ef24..e4e92a3 100644 --- a/src/Item.raz +++ b/src/Item.raz @@ -60,6 +60,9 @@ anything else: lies in that frame, a local, a value parameter or a temporary, or part of one *) pos*: Diag.Pos; (* where it is in the source, for messages *) + made*: BOOL; (* an integer an operator gave, computed in its own + type, where it may have wrapped: a U8 made so + doesn't widen to INT (report section 5.10) *) end; const @@ -492,6 +495,33 @@ return ((s = Sym.tInt) and Sym.same(t, Sym.tI64)) or ((s = Sym.tUInt) and Sym.same(t, Sym.tU64)); end; +(* A U8, I8 or I16 goes silently into an INT, and a U8 or U16 into a + UINT: an INT or a UINT holds every value of theirs on any target, + since it is at least 16 bits (report sections 5.1 and 5.10). Only + one step: a U8 doesn't go into an I64. On the stack of values an + integer is kept extended to 64 bits as its type says (Gen), so the + value a U8 loads as is already the INT; only the type changes. *) +proc smallWidens*(s, t: ^Sym.Type): BOOL; +begin + if Sym.same(t, Sym.tInt) then + return Sym.same(s, Sym.tU8) or Sym.same(s, Sym.tI8) or Sym.same(s, Sym.tI16); + elsif Sym.same(t, Sym.tUInt) then + return Sym.same(s, Sym.tU8) or Sym.same(s, Sym.tU16); + end; + return false; +end; + +(* An error at x, a U8, I8, I16 or U16 an operator made, which would + otherwise widen to t: the operator worked in x's type, and its + result may have wrapped, so the program says where to widen. *) +proc madeNarrow(x: Item; t: ^Sym.Type): ERROR; +begin + return Scan.markHelp(x.pos, + Diag.say("can't use {} as {}: an operator made it, in {}, where it may have wrapped", + [Fmt.str(Sym.typeName(x.typ)), Fmt.str(Sym.typeName(t)), Fmt.str(Sym.typeName(x.typ))]), + say1("convert an operand to {} first, so that the operator works in it", Sym.typeName(t))); +end; + (* Whether the several results s may be passed on as t: as many, each of the same type or one that widens to it, as in return f(x) (report section 5.10). An INT and an I64 have one layout here, so the values @@ -559,6 +589,10 @@ if x.mode in {mType, mBuiltin, mNone, mCase} then return notValue(x); end; if Sym.same(s, t) then return nil; end; if widens(s, t) then x.typ := t; (* the same bits, on a 64-bit target *) + elsif smallWidens(s, t) then + if x.made then return madeNarrow(x, t); end; + if x.mode # mConst then try value(x); end; (* loaded as s, extended *) + x.typ := t; elsif (s = Sym.tRConst) and (t^.form = Sym.fReal) then realTo(x, t); elsif (s^.form = Sym.fConst) and (t^.form = Sym.fInt) then if (x.mode = mConst) and not Sym.fits(x.val, t) then @@ -585,6 +619,11 @@ return Scan.markHelp(x.pos, say2("can't use {} as {}", Sym.typeName(s), Sym.typeName(t)), say1("to call it, write {}()", Diag.source(x.pos))); + elsif (widens(Sym.tInt, t) and smallWidens(s, Sym.tInt)) or (widens(Sym.tUInt, t) and smallWidens(s, Sym.tUInt)) then + (* a U8 widens one step, to INT, not on to an I64 *) + return Scan.markHelp(x.pos, say2("can't use {} as {}", Sym.typeName(s), Sym.typeName(t)), + Diag.say("{} widens to {} only as written, {}({})", [Fmt.str(Sym.typeName(s)), + Fmt.str(Sym.typeName(t)), Fmt.str(Sym.typeName(t)), Fmt.str(Diag.source(x.pos))])); elsif widens(t, s) then (* the other way, an I64 into an INT, is written: it may drop bits *) return Scan.markHelp(x.pos, say2("can't use {} as {}", Sym.typeName(s), Sym.typeName(t)), @@ -906,6 +945,7 @@ return Scan.markAt(x.pos, "the constant is too big for I64"); elsif x.mode = mConst then x.val := cut(-x.val, x.typ); else try value(x); Gen.neg(Gen.kWord, intSize(x.typ), signed(x.typ)); end; + x.made := true; else return Scan.markAt(x.pos, say1("can't negate {}", Sym.typeName(x.typ))); end; return nil; end; @@ -937,6 +977,7 @@ end; if x.mode = mConst then x.val := cut(~x.val, x.typ); else try value(x); Gen.complement(intSize(x.typ), signed(x.typ)); end; + x.made := true; return nil; end; @@ -1270,6 +1311,13 @@ (* an INT meeting an I64 widens to it, a UINT to a U64 (report section 6.1): the same 64 bits here, so only the type changes *) if widens(xt, yt) then t := yt; else t := xt; end; + elsif smallWidens(xt, yt) or smallWidens(yt, xt) then + (* a U8, I8 or I16 meeting an INT widens to it, unless an + operator made it (report section 6.1); each operand is + loaded as its own type, extended, so t is all that changes *) + if x.made and smallWidens(xt, yt) then return madeNarrow(x, yt); end; + if y.made and smallWidens(yt, xt) then return madeNarrow(y, xt); end; + if smallWidens(xt, yt) then t := yt; else t := xt; end; else return mismatchOps(x, y, xt, yt); end; if not intOp(op) then return doesntApply(op, x, y, t); end; elsif (xt^.form = Sym.fReal) and (yt^.form = Sym.fReal) then @@ -1337,6 +1385,7 @@ else v := try foldInt(op, x.val, y.val, t, Diag.join(x.pos, y.pos)); makeConst(x, t, cut(v, t)); + x.made := true; end; return nil; end; @@ -1375,6 +1424,7 @@ end; x.mode := mStack; if isRel(op) or (op = Scan.sIn) then x.typ := Sym.tBool; else x.typ := t; end; + x.made := true; return nil; end; @@ -1393,6 +1443,7 @@ if x.mode = mStr then return Scan.markAt(x.pos, "can't convert a string"); end; return notValue(x); end; + x.made := false; (* what a conversion gives widens, U8(a + b) too *) if Sym.same(s, t) then return nil; end; if (Sym.isInt(s) or (s^.form in {Sym.fEnum, Sym.fSet})) and (t^.form = Sym.fInt) then if x.mode = mConst then x.val := cut(x.val, t); diff --git a/src/check.sh b/src/check.sh index 8fce9ad..6b96e3b 100755 --- a/src/check.sh +++ b/src/check.sh @@ -14,8 +14,9 @@ # whose tests are conform/*/View*.raz, and which Math, examples/Elf.raz # and, through Math, test/bench/NBody.raz use; and returning a slice of # the frame as an error (report 2026-10.27); an array constant typed -# []T (report 2026-10.29); and [] as the empty slice, seen through a -# view (report 2026-10.30). +# []T (report 2026-10.29); [] as the empty slice, seen through a view +# (report 2026-10.30); and U8, I8 and I16 widening to INT (report +# 2026-10.31). newer="conform/run/RawStrings.raz conform/err/ScanRawOpen.raz conform/err/ScanRawIndent.raz conform/run/MatchStr.raz conform/err/MatchStrTwice.raz conform/err/MatchStrNoElse.raz conform/err/MatchStrVar.raz conform/err/MatchReal.raz conform/err/AtMatch.raz @@ -25,7 +26,9 @@ lib/Math.raz examples/Elf.raz test/bench/NBody.raz conform/err/FrameSlice.raz conform/run/ArrayConstLen.raz conform/err/ArrayConstLenEmpty.raz conform/err/ArrayConstLenLiteral.raz - conform/run/EmptySlice.raz" + conform/run/EmptySlice.raz + conform/run/SmallWiden.raz conform/err/SmallWidenMade.raz conform/err/SmallWidenNeg.raz + conform/err/SmallWidenI64.raz conform/err/SmallWidenMadeOp.raz" newer() { case $1 in conform/*/View*.raz) return 0 ;; esac for g in $newer; do [ $g = $1 ] && return 0; done diff --git a/test/msg/SmallWidenI32.err b/test/msg/SmallWidenI32.err new file mode 100644 index 0000000..6911431 --- /dev/null +++ b/test/msg/SmallWidenI32.err @@ -0,0 +1,4 @@ +conform/err/SmallWidenI32.raz:5:10: error: can't use I32 as INT + 5 | n := a; + | ^ +raz: 1 error diff --git a/test/msg/SmallWidenI64.err b/test/msg/SmallWidenI64.err new file mode 100644 index 0000000..13ccaa1 --- /dev/null +++ b/test/msg/SmallWidenI64.err @@ -0,0 +1,5 @@ +conform/err/SmallWidenI64.raz:5:10: error: can't use U8 as I64 + 5 | n := a; + | ^ +help: U8 widens to I64 only as written, I64(a) +raz: 1 error diff --git a/test/msg/SmallWidenMade.err b/test/msg/SmallWidenMade.err new file mode 100644 index 0000000..fc7fa33 --- /dev/null +++ b/test/msg/SmallWidenMade.err @@ -0,0 +1,5 @@ +conform/err/SmallWidenMade.raz:5:10: error: can't use U8 as INT: an operator made it, in U8, where it may have wrapped + 5 | n := a + b; + | ^^^^^ +help: convert an operand to INT first, so that the operator works in it +raz: 1 error diff --git a/test/msg/SmallWidenMadeOp.err b/test/msg/SmallWidenMadeOp.err new file mode 100644 index 0000000..7439cf5 --- /dev/null +++ b/test/msg/SmallWidenMadeOp.err @@ -0,0 +1,5 @@ +conform/err/SmallWidenMadeOp.raz:5:14: error: can't use U8 as INT: an operator made it, in U8, where it may have wrapped + 5 | n := n + (a * b); + | ^^^^^^^ +help: convert an operand to INT first, so that the operator works in it +raz: 1 error diff --git a/test/msg/SmallWidenNeg.err b/test/msg/SmallWidenNeg.err new file mode 100644 index 0000000..56161fa --- /dev/null +++ b/test/msg/SmallWidenNeg.err @@ -0,0 +1,5 @@ +conform/err/SmallWidenNeg.raz:5:10: error: can't use I8 as INT: an operator made it, in I8, where it may have wrapped + 5 | n := -c; + | ^^ +help: convert an operand to INT first, so that the operator works in it +raz: 1 error diff --git a/test/msg/SmallWidenU16.err b/test/msg/SmallWidenU16.err new file mode 100644 index 0000000..488489c --- /dev/null +++ b/test/msg/SmallWidenU16.err @@ -0,0 +1,4 @@ +conform/err/SmallWidenU16.raz:5:10: error: can't use U16 as INT + 5 | n := a; + | ^ +raz: 1 error