From 2d43fcafe0cedd4f78611dddccc31e1bef432aab Mon Sep 17 00:00:00 2001 From: Araq Date: Wed, 12 Nov 2014 00:04:04 +0100 Subject: [PATCH] fixes #1593 --- compiler/ccgexprs.nim | 14 ++++++++++++-- tests/ccgbugs/tcvarargs.nim | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 tests/ccgbugs/tcvarargs.nim diff --git a/compiler/ccgexprs.nim b/compiler/ccgexprs.nim index 75f0f3a42d..b6feb78b20 100644 --- a/compiler/ccgexprs.nim +++ b/compiler/ccgexprs.nim @@ -11,6 +11,14 @@ # -------------------------- constant expressions ------------------------ +proc int64Literal(i: BiggestInt): PRope = + if i > low(int64): + result = rfmt(nil, "IL64($1)", toRope(i)) + else: + result = ~"(IL64(-9223372036854775807) - IL64(1))" + +proc uint64Literal(i: uint64): PRope = toRope($i & "ULL") + proc intLiteral(i: BiggestInt): PRope = if (i > low(int32)) and (i <= high(int32)): result = toRope(i) @@ -46,16 +54,18 @@ proc genLiteral(p: BProc, n: PNode, ty: PType): PRope = case n.kind of nkCharLit..nkUInt64Lit: case skipTypes(ty, abstractVarRange).kind - of tyChar, tyInt64, tyNil: + of tyChar, tyNil: result = intLiteral(n.intVal) of tyInt: - if (n.intVal >= low(int32)) and (n.intVal <= high(int32)): + if n.intVal >= low(int32) and n.intVal <= high(int32): result = int32Literal(int32(n.intVal)) else: result = intLiteral(n.intVal) of tyBool: if n.intVal != 0: result = ~"NIM_TRUE" else: result = ~"NIM_FALSE" + of tyInt64: result = int64Literal(n.intVal) + of tyUInt64: result = uint64Literal(uint64(n.intVal)) else: result = ropef("(($1) $2)", [getTypeDesc(p.module, skipTypes(ty, abstractVarRange)), intLiteral(n.intVal)]) diff --git a/tests/ccgbugs/tcvarargs.nim b/tests/ccgbugs/tcvarargs.nim new file mode 100644 index 0000000000..ebaf83a4ae --- /dev/null +++ b/tests/ccgbugs/tcvarargs.nim @@ -0,0 +1,34 @@ +discard """ + output: '''17 +17 +17 +17 +17 +17 +''' +""" + +# bug #1593 + +{.emit: """ +#include + +void foo(int n, ...) { + NI64 k; + int i; + va_list argp; + va_start(argp, n); + for (i = 1; i <= n; i++) { + k = va_arg(argp, NI64); + printf("%lld\n", (long long)k); + } + va_end(argp); +} +""".} + +proc foo(x: cint) {.importc, varargs, nodecl.} + +proc main() = + const k = 17'i64 + foo(6, k, k, k, k, k, k) +main()