From fb62dd1fae7a405d80f0d4257ddb1f4d48e204f0 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Mon, 18 Jun 2018 14:30:18 +0200 Subject: Fix constant folding for shl/not Since the source and destination types are the same the result should be trimmed to fit. --- compiler/semfold.nim | 11 ++++++++-- tests/arithm/tnot.nim | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++ tests/arithm/tshl.nim | 34 ++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 tests/arithm/tnot.nim create mode 100644 tests/arithm/tshl.nim diff --git a/compiler/semfold.nim b/compiler/semfold.nim index 10a223ea2..eceb10470 100644 --- a/compiler/semfold.nim +++ b/compiler/semfold.nim @@ -211,7 +211,12 @@ proc evalOp(m: TMagic, n, a, b, c: PNode; g: ModuleGraph): PNode = of mUnaryMinusF64: result = newFloatNodeT(- getFloat(a), n, g) of mNot: result = newIntNodeT(1 - getInt(a), n, g) of mCard: result = newIntNodeT(nimsets.cardSet(g.config, a), n, g) - of mBitnotI: result = newIntNodeT(not getInt(a), n, g) + of mBitnotI: + case skipTypes(n.typ, abstractRange).kind + of tyUInt..tyUInt64: + result = newIntNodeT((not getInt(a)) and lastOrd(g.config, a.typ, fixedUnsigned=true), n, g) + else: + result = newIntNodeT(not getInt(a), n, g) of mLengthArray: result = newIntNodeT(lengthOrd(g.config, a.typ), n, g) of mLengthSeq, mLengthOpenArray, mXLenSeq, mLengthStr, mXLenStr: if a.kind == nkNilLit: @@ -250,8 +255,10 @@ proc evalOp(m: TMagic, n, a, b, c: PNode; g: ModuleGraph): PNode = of tyInt8: result = newIntNodeT(int8(getInt(a)) shl int8(getInt(b)), n, g) of tyInt16: result = newIntNodeT(int16(getInt(a)) shl int16(getInt(b)), n, g) of tyInt32: result = newIntNodeT(int32(getInt(a)) shl int32(getInt(b)), n, g) - of tyInt64, tyInt, tyUInt..tyUInt64: + of tyInt64, tyInt: result = newIntNodeT(`shl`(getInt(a), getInt(b)), n, g) + of tyUInt..tyUInt64: + result = newIntNodeT(`shl`(getInt(a), getInt(b)) and lastOrd(g.config, a.typ, fixedUnsigned=true), n, g) else: internalError(g.config, n.info, "constant folding for shl") of mShrI: case skipTypes(n.typ, abstractRange).kind diff --git a/tests/arithm/tnot.nim b/tests/arithm/tnot.nim new file mode 100644 index 000000000..6a4877b2c --- /dev/null +++ b/tests/arithm/tnot.nim @@ -0,0 +1,58 @@ +discard """ + output: ''' +-5 +-5 +-5 +-5 +4 +4 +4 +4 +251 +65531 +4294967291 +18446744073709551611 +4 +4 +4 +4 +''' +""" + +# Signed types +block: + const t0: int8 = not 4 + const t1: int16 = not 4 + const t2: int32 = not 4 + const t3: int64 = not 4 + const t4: int8 = not -5 + const t5: int16 = not -5 + const t6: int32 = not -5 + const t7: int64 = not -5 + echo t0 + echo t1 + echo t2 + echo t3 + echo t4 + echo t5 + echo t6 + echo t7 + +# Unsigned types +block: + const t0: uint8 = not 4'u8 + const t1: uint16 = not 4'u16 + const t2: uint32 = not 4'u32 + const t3: uint64 = not 4'u64 + const t4: uint8 = not 251'u8 + const t5: uint16 = not 65531'u16 + const t6: uint32 = not 4294967291'u32 + const t7: uint64 = not 18446744073709551611'u64 + echo t0 + echo t1 + echo t2 + echo t3 + echo t4 + echo t5 + echo t6 + echo t7 diff --git a/tests/arithm/tshl.nim b/tests/arithm/tshl.nim new file mode 100644 index 000000000..0aa46d021 --- /dev/null +++ b/tests/arithm/tshl.nim @@ -0,0 +1,34 @@ +discard """ + output: ''' +0 +0 +1 +1 +0 +0 +0 +1 +''' +""" + +# Signed types +block: + const t0: int8 = 1'i8 shl 8 + const t1: int16 = 1'i16 shl 16 + const t2: int32 = 1'i32 shl 32 + const t3: int64 = 1'i64 shl 64 + echo t0 + echo t1 + echo t2 + echo t3 + +# Unsigned types +block: + const t0: uint8 = 1'u8 shl 8 + const t1: uint16 = 1'u16 shl 16 + const t2: uint32 = 1'u32 shl 32 + const t3: uint64 = 1'u64 shl 64 + echo t0 + echo t1 + echo t2 + echo t3 -- cgit 1.4.1-2-gfad0 From 9adfaa7f0704b46f29f3b2f1cdf7d2e83f73ce07 Mon Sep 17 00:00:00 2001 From: Araq Date: Wed, 20 Jun 2018 10:20:42 +0200 Subject: fixes #8076 --- compiler/ccgstmts.nim | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/compiler/ccgstmts.nim b/compiler/ccgstmts.nim index 71d212282..f9654bb1f 100644 --- a/compiler/ccgstmts.nim +++ b/compiler/ccgstmts.nim @@ -978,16 +978,17 @@ proc genAsmOrEmitStmt(p: BProc, t: PNode, isAsmStmt=false): Rope = if isAsmStmt and hasGnuAsm in CC[p.config.cCompiler].props: for x in splitLines(res): var j = 0 - while x[j] in {' ', '\t'}: inc(j) - if x[j] in {'"', ':'}: - # don't modify the line if already in quotes or - # some clobber register list: - add(result, x); add(result, "\L") - elif x[j] != '\0': - # ignore empty lines - add(result, "\"") - add(result, x) - add(result, "\\n\"\n") + while j < x.len and x[j] in {' ', '\t'}: inc(j) + if j < x.len: + if x[j] in {'"', ':'}: + # don't modify the line if already in quotes or + # some clobber register list: + add(result, x); add(result, "\L") + else: + # ignore empty lines + add(result, "\"") + add(result, x) + add(result, "\\n\"\n") else: res.add("\L") result = res.rope -- cgit 1.4.1-2-gfad0 From 1be82d96a682338ba53832bcc2772b7865b47b57 Mon Sep 17 00:00:00 2001 From: Araq Date: Wed, 20 Jun 2018 11:35:36 +0200 Subject: nimpretty: bugfixes; refs #8078 --- compiler/layouter.nim | 11 +++++++---- nimpretty/tester.nim | 2 +- nimpretty/tests/exhaustive.nim | 16 ++++++++++++++++ nimpretty/tests/expected/exhaustive.nim | 18 ++++++++++++++++++ 4 files changed, 42 insertions(+), 5 deletions(-) diff --git a/compiler/layouter.nim b/compiler/layouter.nim index 409b656c9..62844db4b 100644 --- a/compiler/layouter.nim +++ b/compiler/layouter.nim @@ -74,9 +74,10 @@ template wr(x) = template goodCol(col): bool = col in 40..MaxLineLen const - splitters = {tkComma, tkSemicolon, tkParLe, tkParDotLe, - tkBracketLe, tkBracketLeColon, tkCurlyDotLe, - tkCurlyLe} + openPars = {tkParLe, tkParDotLe, + tkBracketLe, tkBracketLeColon, tkCurlyDotLe, + tkCurlyLe} + splitters = openPars + {tkComma, tkSemicolon} oprSet = {tkOpr, tkDiv, tkMod, tkShl, tkShr, tkIn, tkNotin, tkIs, tkIsnot, tkNot, tkOf, tkAs, tkDotDot, tkAnd, tkOr, tkXor} @@ -169,7 +170,9 @@ proc emitTok*(em: var Emitter; L: TLexer; tok: TToken) = of tokKeywordLow..tokKeywordHigh: if endsInAlpha(em): wr(" ") - elif not em.inquote and not endsInWhite(em) and tok.tokType in oprSet: + elif not em.inquote and not endsInWhite(em) and + em.lastTok notin openPars: + #and tok.tokType in oprSet wr(" ") if not em.inquote: diff --git a/nimpretty/tester.nim b/nimpretty/tester.nim index 7db245b5f..8798ce06a 100644 --- a/nimpretty/tester.nim +++ b/nimpretty/tester.nim @@ -16,7 +16,7 @@ proc test(infile, outfile: string) = let produced = dir / nimFile & ".pretty" if strip(readFile(expected)) != strip(readFile(produced)): echo "FAILURE: files differ: ", nimFile - discard execShellCmd("diff -uNdr " & produced & " " & expected) + discard execShellCmd("diff -uNdr " & expected & " " & produced) failures += 1 else: echo "SUCCESS: files identical: ", nimFile diff --git a/nimpretty/tests/exhaustive.nim b/nimpretty/tests/exhaustive.nim index 0ce3bde89..9f2141fbb 100644 --- a/nimpretty/tests/exhaustive.nim +++ b/nimpretty/tests/exhaustive.nim @@ -8,6 +8,22 @@ import verylongnamehere,verylongnamehere,verylongnamehereverylongnamehereverylon proc `[]=`() = discard "index setter" proc `putter=`() = discard cast[pointer](cast[int](buffer) + size) +(not false) + +let expr = if true: "true" else: "false" + +var body = newNimNode(nnkIfExpr).add( + newNimNode(nnkElifBranch).add( + infix(newDotExpr(ident("a"), ident("kind")), "==", newDotExpr(ident("b"), ident("kind"))), + condition + ), + newNimNode(nnkElse).add(newStmtList(newNimNode(nnkReturnStmt).add(ident("false")))) +) + +# comment + +var x = 1 + type GeneralTokenizer* = object of RootObj ## comment here kind*: TokenClass ## and here diff --git a/nimpretty/tests/expected/exhaustive.nim b/nimpretty/tests/expected/exhaustive.nim index dd3ff74e8..95071fce3 100644 --- a/nimpretty/tests/expected/exhaustive.nim +++ b/nimpretty/tests/expected/exhaustive.nim @@ -9,6 +9,24 @@ import verylongnamehere, verylongnamehere, proc `[]=`() = discard "index setter" proc `putter=`() = discard cast[pointer](cast[int](buffer) + size) +(not false) + +let expr = if true: "true" else: "false" + +var body = newNimNode(nnkIfExpr).add( + newNimNode(nnkElifBranch).add( + infix(newDotExpr(ident("a"), ident("kind")), "==", newDotExpr(ident("b"), + ident("kind"))), + condition + ), + newNimNode(nnkElse).add(newStmtList(newNimNode(nnkReturnStmt).add(ident( + "false")))) +) + +# comment + +var x = 1 + type GeneralTokenizer* = object of RootObj ## comment here kind*: TokenClass ## and here -- cgit 1.4.1-2-gfad0