From e4d0f4fee0d154c6af90b160d615be17034ffcdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20D=C3=B6ring?= Date: Wed, 12 Jun 2019 09:26:30 +0200 Subject: [PATCH] fix regression in semfold for old right shift (#11477) --- compiler/semfold.nim | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/compiler/semfold.nim b/compiler/semfold.nim index cb88ee6ddc..3957baef4a 100644 --- a/compiler/semfold.nim +++ b/compiler/semfold.nim @@ -248,8 +248,23 @@ proc evalOp(m: TMagic, n, a, b, c: PNode; g: ModuleGraph): PNode = result = doAndFit(newIntNodeT(`shl`(getInt(a), getInt(b)), n, g)) else: internalError(g.config, n.info, "constant folding for shl") of mShrI: - let a = cast[uint64](getInt(a)) + var a = cast[uint64](getInt(a)) let b = cast[uint64](getInt(b)) + # To support the ``-d:nimOldShiftRight`` flag, we need to mask the + # signed integers to cut off the extended sign bit in the internal + # representation. + if 0'u64 < b: # do not cut off the sign extension, when there is + # no bit shifting happening. + case skipTypes(n.typ, abstractRange).kind + of tyInt8: a = a and 0xff'u64 + of tyInt16: a = a and 0xffff'u64 + of tyInt32: a = a and 0xffffffff'u64 + of tyInt: + if g.config.target.intSize == 4: + a = a and 0xffffffff'u64 + else: + # unsigned and 64 bit integers don't need masking + discard let c = cast[BiggestInt](a shr b) result = newIntNodeT(c, n, g) of mAshrI: