From f0cc5dd697c08d882d36ed28e49334713105785a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20D=C3=B6ring?= Date: Sat, 29 Feb 2020 00:53:05 +0100 Subject: [PATCH 01/10] fix #13513 --- lib/system/arithmetics.nim | 91 ++++++++++++++++++++++---------------- lib/system/comparisons.nim | 59 +++++++++++++++--------- 2 files changed, 89 insertions(+), 61 deletions(-) diff --git a/lib/system/arithmetics.nim b/lib/system/arithmetics.nim index 757a813e8619e..710566a6ddb54 100644 --- a/lib/system/arithmetics.nim +++ b/lib/system/arithmetics.nim @@ -342,45 +342,6 @@ proc `xor`*(x, y: int16): int16 {.magic: "BitxorI", noSideEffect.} proc `xor`*(x, y: int32): int32 {.magic: "BitxorI", noSideEffect.} proc `xor`*(x, y: int64): int64 {.magic: "BitxorI", noSideEffect.} -type - IntMax32 = int|int8|int16|int32 - -proc `+%`*(x, y: IntMax32): IntMax32 {.magic: "AddU", noSideEffect.} -proc `+%`*(x, y: int64): int64 {.magic: "AddU", noSideEffect.} - ## Treats `x` and `y` as unsigned and adds them. - ## - ## The result is truncated to fit into the result. - ## This implements modulo arithmetic. No overflow errors are possible. - -proc `-%`*(x, y: IntMax32): IntMax32 {.magic: "SubU", noSideEffect.} -proc `-%`*(x, y: int64): int64 {.magic: "SubU", noSideEffect.} - ## Treats `x` and `y` as unsigned and subtracts them. - ## - ## The result is truncated to fit into the result. - ## This implements modulo arithmetic. No overflow errors are possible. - -proc `*%`*(x, y: IntMax32): IntMax32 {.magic: "MulU", noSideEffect.} -proc `*%`*(x, y: int64): int64 {.magic: "MulU", noSideEffect.} - ## Treats `x` and `y` as unsigned and multiplies them. - ## - ## The result is truncated to fit into the result. - ## This implements modulo arithmetic. No overflow errors are possible. - -proc `/%`*(x, y: IntMax32): IntMax32 {.magic: "DivU", noSideEffect.} -proc `/%`*(x, y: int64): int64 {.magic: "DivU", noSideEffect.} - ## Treats `x` and `y` as unsigned and divides them. - ## - ## The result is truncated to fit into the result. - ## This implements modulo arithmetic. No overflow errors are possible. - -proc `%%`*(x, y: IntMax32): IntMax32 {.magic: "ModU", noSideEffect.} -proc `%%`*(x, y: int64): int64 {.magic: "ModU", noSideEffect.} - ## Treats `x` and `y` as unsigned and compute the modulo of `x` and `y`. - ## - ## The result is truncated to fit into the result. - ## This implements modulo arithmetic. No overflow errors are possible. - - # unsigned integer operations: proc `not`*(x: uint): uint {.magic: "BitnotI", noSideEffect.} ## Computes the `bitwise complement` of the integer `x`. @@ -461,8 +422,60 @@ proc `mod`*(x, y: uint16): uint16 {.magic: "ModU", noSideEffect.} proc `mod`*(x, y: uint32): uint32 {.magic: "ModU", noSideEffect.} proc `mod`*(x, y: uint64): uint64 {.magic: "ModU", noSideEffect.} +proc `+%`*(x, y: int): int {.inline.} = + ## Treats `x` and `y` as unsigned and adds them. + ## + ## The result is truncated to fit into the result. + ## This implements modulo arithmetic. No overflow errors are possible. + cast[int](cast[uint](x) + cast[uint](y)) +proc `+%`*(x, y: int8): int8 {.inline.} = cast[int8](cast[uint8](x) + cast[uint8](y)) +proc `+%`*(x, y: int16): int16 {.inline.} = cast[int16](cast[uint16](x) + cast[uint16](y)) +proc `+%`*(x, y: int32): int32 {.inline.} = cast[int32](cast[uint32](x) + cast[uint32](y)) +proc `+%`*(x, y: int64): int64 {.inline.} = cast[int64](cast[uint64](x) + cast[uint64](y)) + +proc `-%`*(x, y: int): int {.inline.} = + ## Treats `x` and `y` as unsigned and subtracts them. + ## + ## The result is truncated to fit into the result. + ## This implements modulo arithmetic. No overflow errors are possible. + cast[int](cast[uint](x) - cast[uint](y)) +proc `-%`*(x, y: int8): int8 {.inline.} = cast[int8](cast[uint8](x) - cast[uint8](y)) +proc `-%`*(x, y: int16): int16 {.inline.} = cast[int16](cast[uint16](x) - cast[uint16](y)) +proc `-%`*(x, y: int32): int32 {.inline.} = cast[int32](cast[uint32](x) - cast[uint32](y)) +proc `-%`*(x, y: int64): int64 {.inline.} = cast[int64](cast[uint64](x) - cast[uint64](y)) + +proc `*%`*(x, y: int): int {.inline.} = + ## Treats `x` and `y` as unsigned and multiplies them. + ## + ## The result is truncated to fit into the result. + ## This implements modulo arithmetic. No overflow errors are possible. + cast[int](cast[uint](x) * cast[uint](y)) +proc `*%`*(x, y: int8): int8 {.inline.} = cast[int8](cast[uint8](x) * cast[uint8](y)) +proc `*%`*(x, y: int16): int16 {.inline.} = cast[int16](cast[uint16](x) * cast[uint16](y)) +proc `*%`*(x, y: int32): int32 {.inline.} = cast[int32](cast[uint32](x) * cast[uint32](y)) +proc `*%`*(x, y: int64): int64 {.inline.} = cast[int64](cast[uint64](x) * cast[uint64](y)) +proc `/%`*(x, y: int): int {.inline.} = + ## Treats `x` and `y` as unsigned and divides them. + ## + ## The result is truncated to fit into the result. + ## This implements modulo arithmetic. No overflow errors are possible. + cast[int](cast[uint](x) div cast[uint](y)) +proc `/%`*(x, y: int8): int8 {.inline.} = cast[int8](cast[uint8](x) div cast[uint8](y)) +proc `/%`*(x, y: int16): int16 {.inline.} = cast[int16](cast[uint16](x) div cast[uint16](y)) +proc `/%`*(x, y: int32): int32 {.inline.} = cast[int32](cast[uint32](x) div cast[uint32](y)) +proc `/%`*(x, y: int64): int64 {.inline.} = cast[int64](cast[uint64](x) div cast[uint64](y)) +proc `%%`*(x, y: int): int {.inline.} = + ## Treats `x` and `y` as unsigned and compute the modulo of `x` and `y`. + ## + ## The result is truncated to fit into the result. + ## This implements modulo arithmetic. No overflow errors are possible. + cast[int](cast[uint](x) mod cast[uint](y)) +proc `%%`*(x, y: int8): int8 {.inline.} = cast[int8](cast[uint8](x) mod cast[uint8](y)) +proc `%%`*(x, y: int16): int16 {.inline.} = cast[int16](cast[uint16](x) mod cast[uint16](y)) +proc `%%`*(x, y: int32): int32 {.inline.} = cast[int32](cast[uint32](x) mod cast[uint32](y)) +proc `%%`*(x, y: int64): int64 {.inline.} = cast[int64](cast[uint64](x) mod cast[uint64](y)) proc `+=`*[T: SomeInteger](x: var T, y: T) {. magic: "Inc", noSideEffect.} diff --git a/lib/system/comparisons.nim b/lib/system/comparisons.nim index 787820989e93e..13515d15effd6 100644 --- a/lib/system/comparisons.nim +++ b/lib/system/comparisons.nim @@ -161,25 +161,55 @@ proc `<`*(x, y: int16): bool {.magic: "LtI", noSideEffect.} proc `<`*(x, y: int32): bool {.magic: "LtI", noSideEffect.} proc `<`*(x, y: int64): bool {.magic: "LtI", noSideEffect.} +proc `<=`*(x, y: uint): bool {.magic: "LeU", noSideEffect.} + ## Returns true if ``x <= y``. +proc `<=`*(x, y: uint8): bool {.magic: "LeU", noSideEffect.} +proc `<=`*(x, y: uint16): bool {.magic: "LeU", noSideEffect.} +proc `<=`*(x, y: uint32): bool {.magic: "LeU", noSideEffect.} +proc `<=`*(x, y: uint64): bool {.magic: "LeU", noSideEffect.} -proc `<=%`*(x, y: IntMax32): bool {.magic: "LeU", noSideEffect.} -proc `<=%`*(x, y: int64): bool {.magic: "LeU64", noSideEffect.} +proc `<`*(x, y: uint): bool {.magic: "LtU", noSideEffect.} + ## Returns true if ``unsigned(x) < unsigned(y)``. +proc `<`*(x, y: uint8): bool {.magic: "LtU", noSideEffect.} +proc `<`*(x, y: uint16): bool {.magic: "LtU", noSideEffect.} +proc `<`*(x, y: uint32): bool {.magic: "LtU", noSideEffect.} +proc `<`*(x, y: uint64): bool {.magic: "LtU", noSideEffect.} + +proc `<=%`*(x, y: int): bool {.inline.} = ## Treats `x` and `y` as unsigned and compares them. ## Returns true if ``unsigned(x) <= unsigned(y)``. + cast[uint](x) <= cast[uint](y) +proc `<=%`*(x, y: int8): bool {.inline.} = cast[uint8](x) <= cast[uint8](y) +proc `<=%`*(x, y: int16): bool {.inline.} = cast[uint16](x) <= cast[uint16](y) +proc `<=%`*(x, y: int32): bool {.inline.} = cast[uint32](x) <= cast[uint32](y) +proc `<=%`*(x, y: int64): bool {.inline.} = cast[uint64](x) <= cast[uint64](y) -proc `<%`*(x, y: IntMax32): bool {.magic: "LtU", noSideEffect.} -proc `<%`*(x, y: int64): bool {.magic: "LtU64", noSideEffect.} +proc `<%`*(x, y: int): bool {.inline.} = ## Treats `x` and `y` as unsigned and compares them. ## Returns true if ``unsigned(x) < unsigned(y)``. + cast[uint](x) < cast[uint](y) +proc `<%`*(x, y: int8): bool {.inline.} = cast[uint8](x) < cast[uint8](y) +proc `<%`*(x, y: int16): bool {.inline.} = cast[uint16](x) < cast[uint16](y) +proc `<%`*(x, y: int32): bool {.inline.} = cast[uint32](x) < cast[uint32](y) +proc `<%`*(x, y: int64): bool {.inline.} = cast[uint64](x) < cast[uint64](y) -template `>=%`*(x, y: untyped): untyped = y <=% x +proc `>=%`*(x, y: int): bool {.inline.} = ## Treats `x` and `y` as unsigned and compares them. ## Returns true if ``unsigned(x) >= unsigned(y)``. + cast[uint](x) >= cast[uint](y) +proc `>=%`*(x, y: int8): bool {.inline.} = cast[uint8](x) >= cast[uint8](y) +proc `>=%`*(x, y: int16): bool {.inline.} = cast[uint16](x) >= cast[uint16](y) +proc `>=%`*(x, y: int32): bool {.inline.} = cast[uint32](x) >= cast[uint32](y) +proc `>=%`*(x, y: int64): bool {.inline.} = cast[uint64](x) >= cast[uint64](y) -template `>%`*(x, y: untyped): untyped = y <% x +proc `>%`*(x, y: int): bool {.inline.} = ## Treats `x` and `y` as unsigned and compares them. ## Returns true if ``unsigned(x) > unsigned(y)``. - + cast[uint](x) > cast[uint](y) +proc `>%`*(x, y: int8): bool {.inline.} = cast[uint8](x) > cast[uint8](y) +proc `>%`*(x, y: int16): bool {.inline.} = cast[uint16](x) > cast[uint16](y) +proc `>%`*(x, y: int32): bool {.inline.} = cast[uint32](x) > cast[uint32](y) +proc `>%`*(x, y: int64): bool {.inline.} = cast[uint64](x) > cast[uint64](y) proc `==`*(x, y: uint): bool {.magic: "EqI", noSideEffect.} ## Compares two unsigned integers for equality. @@ -189,21 +219,6 @@ proc `==`*(x, y: uint32): bool {.magic: "EqI", noSideEffect.} proc `==`*(x, y: uint64): bool {.magic: "EqI", noSideEffect.} -proc `<=`*(x, y: uint): bool {.magic: "LeU", noSideEffect.} - ## Returns true if ``x <= y``. -proc `<=`*(x, y: uint8): bool {.magic: "LeU", noSideEffect.} -proc `<=`*(x, y: uint16): bool {.magic: "LeU", noSideEffect.} -proc `<=`*(x, y: uint32): bool {.magic: "LeU", noSideEffect.} -proc `<=`*(x, y: uint64): bool {.magic: "LeU", noSideEffect.} - -proc `<`*(x, y: uint): bool {.magic: "LtU", noSideEffect.} - ## Returns true if ``unsigned(x) < unsigned(y)``. -proc `<`*(x, y: uint8): bool {.magic: "LtU", noSideEffect.} -proc `<`*(x, y: uint16): bool {.magic: "LtU", noSideEffect.} -proc `<`*(x, y: uint32): bool {.magic: "LtU", noSideEffect.} -proc `<`*(x, y: uint64): bool {.magic: "LtU", noSideEffect.} - - {.push stackTrace: off.} proc min*(x, y: int): int {.magic: "MinI", noSideEffect.} = From 80680139b8a010f9e8bcd21ad9390d5da4ba39a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20D=C3=B6ring?= Date: Sat, 29 Feb 2020 01:46:30 +0100 Subject: [PATCH 02/10] fix test --- tests/pragmas/t6448.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/pragmas/t6448.nim b/tests/pragmas/t6448.nim index ab2e3811f9bba..6efb56e08ae38 100644 --- a/tests/pragmas/t6448.nim +++ b/tests/pragmas/t6448.nim @@ -1,5 +1,5 @@ discard """ - errormsg: '''ambiguous call; both foobar.async''' + errormsg: '''ambiguous call''' line: 10 disabled: "32bit" """ From 6a22c863e34d28d3d15b7057499053f196b85fad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20D=C3=B6ring?= Date: Sat, 29 Feb 2020 02:37:12 +0100 Subject: [PATCH 03/10] fix another test --- tests/concepts/t3330.nim | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/concepts/t3330.nim b/tests/concepts/t3330.nim index 067e811336e2c..e70783c182df2 100644 --- a/tests/concepts/t3330.nim +++ b/tests/concepts/t3330.nim @@ -17,17 +17,17 @@ proc add[T](x: var seq[T]; y: openArray[T]) first type mismatch at position: 1 required type for x: var seq[T] but expression 'k' is of type: Alias -proc add(result: var string; x: float) +proc add(x: var string; y: cstring) first type mismatch at position: 1 - required type for result: var string + required type for x: var string but expression 'k' is of type: Alias proc add[T](x: var seq[T]; y: T) first type mismatch at position: 1 required type for x: var seq[T] but expression 'k' is of type: Alias -proc add(x: var string; y: cstring) +proc add(result: var string; x: float) first type mismatch at position: 1 - required type for x: var string + required type for result: var string but expression 'k' is of type: Alias proc add(x: var string; y: char) first type mismatch at position: 1 From 5458acec1f98507c4beb6d3fc99cb3d5dec58a90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20D=C3=B6ring?= Date: Sat, 29 Feb 2020 02:52:53 +0100 Subject: [PATCH 04/10] fix comment --- lib/system/comparisons.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/system/comparisons.nim b/lib/system/comparisons.nim index 13515d15effd6..6f98b21a233f9 100644 --- a/lib/system/comparisons.nim +++ b/lib/system/comparisons.nim @@ -169,7 +169,7 @@ proc `<=`*(x, y: uint32): bool {.magic: "LeU", noSideEffect.} proc `<=`*(x, y: uint64): bool {.magic: "LeU", noSideEffect.} proc `<`*(x, y: uint): bool {.magic: "LtU", noSideEffect.} - ## Returns true if ``unsigned(x) < unsigned(y)``. + ## Returns true if ``x < y``. proc `<`*(x, y: uint8): bool {.magic: "LtU", noSideEffect.} proc `<`*(x, y: uint16): bool {.magic: "LtU", noSideEffect.} proc `<`*(x, y: uint32): bool {.magic: "LtU", noSideEffect.} From f10c9eb9cbaf95c86ba7077997cfa84410e82cf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20D=C3=B6ring?= Date: Sun, 1 Mar 2020 12:30:07 +0100 Subject: [PATCH 05/10] remove unused compiler magics --- compiler/ast.nim | 2 -- compiler/ccgexprs.nim | 2 -- compiler/forloops.nim | 4 ++-- compiler/guards.nim | 4 ++-- compiler/jsgen.nim | 2 -- compiler/semfold.nim | 4 ++-- compiler/vmgen.nim | 4 ++-- lib/system/comparisons.nim | 14 ++------------ 8 files changed, 10 insertions(+), 26 deletions(-) diff --git a/compiler/ast.nim b/compiler/ast.nim index 70c14ccaf185e..b4b144244964a 100644 --- a/compiler/ast.nim +++ b/compiler/ast.nim @@ -616,7 +616,6 @@ type mEqI, mLeI, mLtI, mEqF64, mLeF64, mLtF64, mLeU, mLtU, - mLeU64, mLtU64, mEqEnum, mLeEnum, mLtEnum, mEqCh, mLeCh, mLtCh, mEqB, mLeB, mLtB, @@ -686,7 +685,6 @@ const mEqI, mLeI, mLtI, mEqF64, mLeF64, mLtF64, mLeU, mLtU, - mLeU64, mLtU64, mEqEnum, mLeEnum, mLtEnum, mEqCh, mLeCh, mLtCh, mEqB, mLeB, mLtB, diff --git a/compiler/ccgexprs.nim b/compiler/ccgexprs.nim index 632e4bb31cc6d..f877f4eba4af4 100644 --- a/compiler/ccgexprs.nim +++ b/compiler/ccgexprs.nim @@ -605,8 +605,6 @@ proc binaryArith(p: BProc, e: PNode, d: var TLoc, op: TMagic) = of mLtF64: applyFormat("($1 < $2)") of mLeU: applyFormat("((NU$3)($1) <= (NU$3)($2))") of mLtU: applyFormat("((NU$3)($1) < (NU$3)($2))") - of mLeU64: applyFormat("((NU64)($1) <= (NU64)($2))") - of mLtU64: applyFormat("((NU64)($1) < (NU64)($2))") of mEqEnum: applyFormat("($1 == $2)") of mLeEnum: applyFormat("($1 <= $2)") of mLtEnum: applyFormat("($1 < $2)") diff --git a/compiler/forloops.nim b/compiler/forloops.nim index d857b3c51b0f7..36b6b5513fb8e 100644 --- a/compiler/forloops.nim +++ b/compiler/forloops.nim @@ -13,8 +13,8 @@ import ast, astalgo const someCmp = {mEqI, mEqF64, mEqEnum, mEqCh, mEqB, mEqRef, mEqProc, - mEqUntracedRef, mLeI, mLeF64, mLeU, mLeU64, mLeEnum, - mLeCh, mLeB, mLePtr, mLtI, mLtF64, mLtU, mLtU64, mLtEnum, + mEqUntracedRef, mLeI, mLeF64, mLeU, mLeEnum, + mLeCh, mLeB, mLePtr, mLtI, mLtF64, mLtU, mLtEnum, mLtCh, mLtB, mLtPtr} proc isCounter(s: PSym): bool {.inline.} = diff --git a/compiler/guards.nim b/compiler/guards.nim index a2cbdbda2b2a8..6459f5e5d7adc 100644 --- a/compiler/guards.nim +++ b/compiler/guards.nim @@ -17,9 +17,9 @@ const mEqUntracedRef, mEqStr, mEqSet, mEqCString} # set excluded here as the semantics are vastly different: - someLe = {mLeI, mLeF64, mLeU, mLeU64, mLeEnum, + someLe = {mLeI, mLeF64, mLeU, mLeEnum, mLeCh, mLeB, mLePtr, mLeStr} - someLt = {mLtI, mLtF64, mLtU, mLtU64, mLtEnum, + someLt = {mLtI, mLtF64, mLtU, mLtEnum, mLtCh, mLtB, mLtPtr, mLtStr} someLen = {mLengthOpenArray, mLengthStr, mLengthArray, mLengthSeq, diff --git a/compiler/jsgen.nim b/compiler/jsgen.nim index 51d23662cb972..ae2c751ed7454 100644 --- a/compiler/jsgen.nim +++ b/compiler/jsgen.nim @@ -576,8 +576,6 @@ proc arithAux(p: PProc, n: PNode, r: var TCompRes, op: TMagic) = of mLtF64: applyFormat("($1 < $2)", "($1 < $2)") of mLeU: applyFormat("($1 <= $2)", "($1 <= $2)") of mLtU: applyFormat("($1 < $2)", "($1 < $2)") - of mLeU64: applyFormat("($1 <= $2)", "($1 <= $2)") - of mLtU64: applyFormat("($1 < $2)", "($1 < $2)") of mEqEnum: applyFormat("($1 == $2)", "($1 == $2)") of mLeEnum: applyFormat("($1 <= $2)", "($1 <= $2)") of mLtEnum: applyFormat("($1 < $2)", "($1 < $2)") diff --git a/compiler/semfold.nim b/compiler/semfold.nim index e8ca42db3c959..ebab9c3bc3a95 100644 --- a/compiler/semfold.nim +++ b/compiler/semfold.nim @@ -289,9 +289,9 @@ proc evalOp(m: TMagic, n, a, b, c: PNode; g: ModuleGraph): PNode = of mLtStr: result = newIntNodeT(toInt128(ord(getStr(a) < getStr(b))), n, g) of mLeStr: result = newIntNodeT(toInt128(ord(getStr(a) <= getStr(b))), n, g) of mEqStr: result = newIntNodeT(toInt128(ord(getStr(a) == getStr(b))), n, g) - of mLtU, mLtU64: + of mLtU: result = newIntNodeT(toInt128(ord(`<%`(toInt64(getOrdValue(a)), toInt64(getOrdValue(b))))), n, g) - of mLeU, mLeU64: + of mLeU: result = newIntNodeT(toInt128(ord(`<=%`(toInt64(getOrdValue(a)), toInt64(getOrdValue(b))))), n, g) of mBitandI, mAnd: result = newIntNodeT(bitand(a.getInt, b.getInt), n, g) of mBitorI, mOr: result = newIntNodeT(bitor(getInt(a), getInt(b)), n, g) diff --git a/compiler/vmgen.nim b/compiler/vmgen.nim index 0b8eafd17e0a7..5708b310b76c1 100644 --- a/compiler/vmgen.nim +++ b/compiler/vmgen.nim @@ -1076,8 +1076,8 @@ proc genMagic(c: PCtx; n: PNode; dest: var TDest; m: TMagic) = of mEqF64: genBinaryABC(c, n, dest, opcEqFloat) of mLeF64: genBinaryABC(c, n, dest, opcLeFloat) of mLtF64: genBinaryABC(c, n, dest, opcLtFloat) - of mLePtr, mLeU, mLeU64: genBinaryABC(c, n, dest, opcLeu) - of mLtPtr, mLtU, mLtU64: genBinaryABC(c, n, dest, opcLtu) + of mLePtr, mLeU: genBinaryABC(c, n, dest, opcLeu) + of mLtPtr, mLtU: genBinaryABC(c, n, dest, opcLtu) of mEqProc, mEqRef, mEqUntracedRef: genBinaryABC(c, n, dest, opcEqRef) of mXor: genBinaryABC(c, n, dest, opcXor) diff --git a/lib/system/comparisons.nim b/lib/system/comparisons.nim index 6f98b21a233f9..60ac554a624f4 100644 --- a/lib/system/comparisons.nim +++ b/lib/system/comparisons.nim @@ -193,23 +193,13 @@ proc `<%`*(x, y: int16): bool {.inline.} = cast[uint16](x) < cast[uint16](y) proc `<%`*(x, y: int32): bool {.inline.} = cast[uint32](x) < cast[uint32](y) proc `<%`*(x, y: int64): bool {.inline.} = cast[uint64](x) < cast[uint64](y) -proc `>=%`*(x, y: int): bool {.inline.} = +template `>=%`*(x, y: untyped): untyped = y <=% x ## Treats `x` and `y` as unsigned and compares them. ## Returns true if ``unsigned(x) >= unsigned(y)``. - cast[uint](x) >= cast[uint](y) -proc `>=%`*(x, y: int8): bool {.inline.} = cast[uint8](x) >= cast[uint8](y) -proc `>=%`*(x, y: int16): bool {.inline.} = cast[uint16](x) >= cast[uint16](y) -proc `>=%`*(x, y: int32): bool {.inline.} = cast[uint32](x) >= cast[uint32](y) -proc `>=%`*(x, y: int64): bool {.inline.} = cast[uint64](x) >= cast[uint64](y) -proc `>%`*(x, y: int): bool {.inline.} = +template `>%`*(x, y: untyped): untyped = y <% x ## Treats `x` and `y` as unsigned and compares them. ## Returns true if ``unsigned(x) > unsigned(y)``. - cast[uint](x) > cast[uint](y) -proc `>%`*(x, y: int8): bool {.inline.} = cast[uint8](x) > cast[uint8](y) -proc `>%`*(x, y: int16): bool {.inline.} = cast[uint16](x) > cast[uint16](y) -proc `>%`*(x, y: int32): bool {.inline.} = cast[uint32](x) > cast[uint32](y) -proc `>%`*(x, y: int64): bool {.inline.} = cast[uint64](x) > cast[uint64](y) proc `==`*(x, y: uint): bool {.magic: "EqI", noSideEffect.} ## Compares two unsigned integers for equality. From 1722755c1aec13dc03f105d8b56cdfb653d8898e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20D=C3=B6ring?= Date: Sun, 1 Mar 2020 13:17:49 +0100 Subject: [PATCH 06/10] add test --- tests/system/tarithmetics.nim | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 tests/system/tarithmetics.nim diff --git a/tests/system/tarithmetics.nim b/tests/system/tarithmetics.nim new file mode 100644 index 0000000000000..d6446e83ab90e --- /dev/null +++ b/tests/system/tarithmetics.nim @@ -0,0 +1,21 @@ +discard """ +nimout: "OK" +output: "OK" +""" + +proc testUnsignedOps() = + let a: int8 = -128 + let b: int8 = 127 + let c: int8 = -1 + let d: int8 = 1 + + doAssert b +% 1 == -128 + doAssert b -% -1 == -128 + doAssert b *% 2 == -2 + doAssert a /% 4 == 32 + doAssert a %% 7 == 2 + echo "OK" + +testUnsignedOps() +static: + testUnsignedOps() From 11cfb7800e9b8b3a0812ef24c1dc217d5ea37e41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20D=C3=B6ring?= Date: Sun, 1 Mar 2020 13:31:46 +0100 Subject: [PATCH 07/10] small cleanup --- compiler/jsgen.nim | 128 ++++++++++++++++++++++----------------------- 1 file changed, 63 insertions(+), 65 deletions(-) diff --git a/compiler/jsgen.nim b/compiler/jsgen.nim index ae2c751ed7454..519cc25f62b94 100644 --- a/compiler/jsgen.nim +++ b/compiler/jsgen.nim @@ -370,71 +370,69 @@ type const # magic checked op; magic unchecked op; jsMagics: TMagicOps = [ - ["addInt", ""], # AddI - ["subInt", ""], # SubI - ["mulInt", ""], # MulI - ["divInt", ""], # DivI - ["modInt", ""], # ModI - ["addInt", ""], # Succ - ["subInt", ""], # Pred - ["", ""], # AddF64 - ["", ""], # SubF64 - ["", ""], # MulF64 - ["", ""], # DivF64 - ["", ""], # ShrI - ["", ""], # ShlI - ["", ""], # AshrI - ["", ""], # BitandI - ["", ""], # BitorI - ["", ""], # BitxorI - ["nimMin", "nimMin"], # MinI - ["nimMax", "nimMax"], # MaxI - ["", ""], # addU - ["", ""], # subU - ["", ""], # mulU - ["", ""], # divU - ["", ""], # modU - ["", ""], # EqI - ["", ""], # LeI - ["", ""], # LtI - ["", ""], # EqF64 - ["", ""], # LeF64 - ["", ""], # LtF64 - ["", ""], # leU - ["", ""], # ltU - ["", ""], # leU64 - ["", ""], # ltU64 - ["", ""], # EqEnum - ["", ""], # LeEnum - ["", ""], # LtEnum - ["", ""], # EqCh - ["", ""], # LeCh - ["", ""], # LtCh - ["", ""], # EqB - ["", ""], # LeB - ["", ""], # LtB - ["", ""], # EqRef - ["", ""], # EqUntracedRef - ["", ""], # LePtr - ["", ""], # LtPtr - ["", ""], # Xor - ["", ""], # EqCString - ["", ""], # EqProc - ["negInt", ""], # UnaryMinusI - ["negInt64", ""], # UnaryMinusI64 - ["absInt", ""], # AbsI - ["", ""], # Not - ["", ""], # UnaryPlusI - ["", ""], # BitnotI - ["", ""], # UnaryPlusF64 - ["", ""], # UnaryMinusF64 - ["nimCharToStr", "nimCharToStr"], - ["nimBoolToStr", "nimBoolToStr"], - ["cstrToNimstr", "cstrToNimstr"], - ["cstrToNimstr", "cstrToNimstr"], - ["cstrToNimstr", "cstrToNimstr"], - ["cstrToNimstr", "cstrToNimstr"], - ["", ""]] + mAddI: ["addInt", ""], + mSubI: ["subInt", ""], + mMulI: ["mulInt", ""], + mDivI: ["divInt", ""], + mModI: ["modInt", ""], + mSucc: ["addInt", ""], + mPred: ["subInt", ""], + mAddF64: ["", ""], + mSubF64: ["", ""], + mMulF64: ["", ""], + mDivF64: ["", ""], + mShrI: ["", ""], + mShlI: ["", ""], + mAshrI: ["", ""], + mBitandI: ["", ""], + mBitorI: ["", ""], + mBitxorI: ["", ""], + mMinI: ["nimMin", "nimMin"], + mMaxI: ["nimMax", "nimMax"], + mAddU: ["", ""], + mSubU: ["", ""], + mMulU: ["", ""], + mDivU: ["", ""], + mModU: ["", ""], + mEqI: ["", ""], + mLeI: ["", ""], + mLtI: ["", ""], + mEqF64: ["", ""], + mLeF64: ["", ""], + mLtF64: ["", ""], + mLeU: ["", ""], + mLtU: ["", ""], + mEqEnum: ["", ""], + mLeEnum: ["", ""], + mLtEnum: ["", ""], + mEqCh: ["", ""], + mLeCh: ["", ""], + mLtCh: ["", ""], + mEqB: ["", ""], + mLeB: ["", ""], + mLtB: ["", ""], + mEqRef: ["", ""], + mEqUntracedRef: ["", ""], + mLePtr: ["", ""], + mLtPtr: ["", ""], + mXor: ["", ""], + mEqCString: ["", ""], + mEqProc: ["", ""], + mUnaryMinusI: ["negInt", ""], + mUnaryMinusI64: ["negInt64", ""], + mAbsI: ["absInt", ""], + mNot: ["", ""], + mUnaryPlusI: ["", ""], + mBitnotI: ["", ""], + mUnaryPlusF64: ["", ""], + mUnaryMinusF64: ["", ""], + mCharToStr: ["nimCharToStr", "nimCharToStr"], + mBoolToStr: ["nimBoolToStr", "nimBoolToStr"], + mIntToStr: ["cstrToNimstr", "cstrToNimstr"], + mInt64ToStr: ["cstrToNimstr", "cstrToNimstr"], + mFloatToStr: ["cstrToNimstr", "cstrToNimstr"], + mCStrToStr: ["cstrToNimstr", "cstrToNimstr"], + mStrToStr: ["", ""]] proc needsTemp(p: PProc; n: PNode): bool = # check if n contains a call to determine From a3ad1addc9c5721a89accfb7e6a81722e20ea97a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20D=C3=B6ring?= Date: Sun, 1 Mar 2020 13:44:00 +0100 Subject: [PATCH 08/10] stuff --- tests/concepts/t3330.nim | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/concepts/t3330.nim b/tests/concepts/t3330.nim index e70783c182df2..067e811336e2c 100644 --- a/tests/concepts/t3330.nim +++ b/tests/concepts/t3330.nim @@ -17,17 +17,17 @@ proc add[T](x: var seq[T]; y: openArray[T]) first type mismatch at position: 1 required type for x: var seq[T] but expression 'k' is of type: Alias -proc add(x: var string; y: cstring) +proc add(result: var string; x: float) first type mismatch at position: 1 - required type for x: var string + required type for result: var string but expression 'k' is of type: Alias proc add[T](x: var seq[T]; y: T) first type mismatch at position: 1 required type for x: var seq[T] but expression 'k' is of type: Alias -proc add(result: var string; x: float) +proc add(x: var string; y: cstring) first type mismatch at position: 1 - required type for result: var string + required type for x: var string but expression 'k' is of type: Alias proc add(x: var string; y: char) first type mismatch at position: 1 From 64a93c7fbe25b65ece9be5e19af325a6051d6104 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20D=C3=B6ring?= Date: Sun, 1 Mar 2020 13:51:49 +0100 Subject: [PATCH 09/10] minor test cleanup --- tests/system/tarithmetics.nim | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/system/tarithmetics.nim b/tests/system/tarithmetics.nim index d6446e83ab90e..876413f21f325 100644 --- a/tests/system/tarithmetics.nim +++ b/tests/system/tarithmetics.nim @@ -6,8 +6,6 @@ output: "OK" proc testUnsignedOps() = let a: int8 = -128 let b: int8 = 127 - let c: int8 = -1 - let d: int8 = 1 doAssert b +% 1 == -128 doAssert b -% -1 == -128 From 1b5a28fb8de9e4539df8b09c4302d480cc3316de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20D=C3=B6ring?= Date: Sun, 1 Mar 2020 14:01:42 +0100 Subject: [PATCH 10/10] merge tarithmetics in tarithm --- tests/arithm/tarithm.nim | 18 ++++++++++++++++++ tests/system/tarithmetics.nim | 19 ------------------- 2 files changed, 18 insertions(+), 19 deletions(-) delete mode 100644 tests/system/tarithmetics.nim diff --git a/tests/arithm/tarithm.nim b/tests/arithm/tarithm.nim index fcb78bd7a4b2c..99306b3e84dc8 100644 --- a/tests/arithm/tarithm.nim +++ b/tests/arithm/tarithm.nim @@ -12,7 +12,9 @@ int32 4294967295 2 0 +tUnsignedOps OK ''' +nimout: "tUnsignedOps OK" """ import typetraits @@ -167,3 +169,19 @@ block tissue12177: echo(a - b) echo(a * b) echo(a div b) + +block tUnsignedOps: + proc testUnsignedOps() = + let a: int8 = -128 + let b: int8 = 127 + + doAssert b +% 1 == -128 + doAssert b -% -1 == -128 + doAssert b *% 2 == -2 + doAssert a /% 4 == 32 + doAssert a %% 7 == 2 + echo "tUnsignedOps OK" + + testUnsignedOps() + static: + testUnsignedOps() diff --git a/tests/system/tarithmetics.nim b/tests/system/tarithmetics.nim deleted file mode 100644 index 876413f21f325..0000000000000 --- a/tests/system/tarithmetics.nim +++ /dev/null @@ -1,19 +0,0 @@ -discard """ -nimout: "OK" -output: "OK" -""" - -proc testUnsignedOps() = - let a: int8 = -128 - let b: int8 = 127 - - doAssert b +% 1 == -128 - doAssert b -% -1 == -128 - doAssert b *% 2 == -2 - doAssert a /% 4 == 32 - doAssert a %% 7 == 2 - echo "OK" - -testUnsignedOps() -static: - testUnsignedOps()