From da52ade86e9df306b03958c5525a0e6973fc1cb5 Mon Sep 17 00:00:00 2001 From: Parashurama Date: Wed, 31 May 2017 21:05:14 +0200 Subject: [PATCH] fix right shift c codegen bug. (#5919) * fix right shift c codegen bug. signed int must first be cast as unsigned before converting to larger integer. The C compiler will auto convert operands to the largest type. --- compiler/ccgexprs.nim | 7 ++++--- tests/arithm/tshr.nim | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 tests/arithm/tshr.nim diff --git a/compiler/ccgexprs.nim b/compiler/ccgexprs.nim index 2e6656028..350a98fd9 100644 --- a/compiler/ccgexprs.nim +++ b/compiler/ccgexprs.nim @@ -545,7 +545,7 @@ proc binaryArith(p: BProc, e: PNode, d: var TLoc, op: TMagic) = "(($4)($1) * ($4)($2))", # MulF64 "(($4)($1) / ($4)($2))", # DivF64 - "($4)((NU$3)($1) >> (NU$3)($2))", # ShrI + "($4)((NU$5)($1) >> (NU$3)($2))", # ShrI "($4)((NU$3)($1) << (NU$3)($2))", # ShlI "($4)($1 & $2)", # BitandI "($4)($1 | $2)", # BitorI @@ -585,16 +585,17 @@ proc binaryArith(p: BProc, e: PNode, d: var TLoc, op: TMagic) = "($1 != $2)"] # Xor var a, b: TLoc - s: BiggestInt + s, k: BiggestInt assert(e.sons[1].typ != nil) assert(e.sons[2].typ != nil) initLocExpr(p, e.sons[1], a) initLocExpr(p, e.sons[2], b) # BUGFIX: cannot use result-type here, as it may be a boolean s = max(getSize(a.t), getSize(b.t)) * 8 + k = getSize(a.t) * 8 putIntoDest(p, d, e.typ, binArithTab[op] % [rdLoc(a), rdLoc(b), rope(s), - getSimpleTypeDesc(p.module, e.typ)]) + getSimpleTypeDesc(p.module, e.typ), rope(k)]) proc genEqProc(p: BProc, e: PNode, d: var TLoc) = var a, b: TLoc diff --git a/tests/arithm/tshr.nim b/tests/arithm/tshr.nim new file mode 100644 index 000000000..09e6e570c --- /dev/null +++ b/tests/arithm/tshr.nim @@ -0,0 +1,18 @@ +discard """ + output: '''''' +""" + +proc T() = + let VI = -8 + let VI64 = -8'i64 + let VI32 = -8'i32 + let VI16 = -8'i16 + let VI8 = -8'i8 + doAssert( (VI shr 1) == 9223372036854775804) + doAssert( (VI64 shr 1) == 9223372036854775804) + doAssert( (VI32 shr 1) == 2147483644) + doAssert( (VI16 shr 1) == 32764) + doAssert( (VI8 shr 1) == 124) + + +T()