small bugfix: code generation for nil closures

This commit is contained in:
Araq 2012-07-25 00:12:56 +02:00
commit 1e4fc02e5e
2 changed files with 31 additions and 20 deletions

View file

@ -44,45 +44,56 @@ proc getStrLit(m: BModule, s: string): PRope =
appf(m.s[cfsData], "STRING_LITERAL($1, $2, $3);$n", appf(m.s[cfsData], "STRING_LITERAL($1, $2, $3);$n",
[result, makeCString(s), ToRope(len(s))]) [result, makeCString(s), ToRope(len(s))])
proc genLiteral(p: BProc, v: PNode, ty: PType): PRope = proc genLiteral(p: BProc, n: PNode, ty: PType): PRope =
if ty == nil: internalError(v.info, "genLiteral: ty is nil") if ty == nil: internalError(n.info, "genLiteral: ty is nil")
case v.kind case n.kind
of nkCharLit..nkUInt64Lit: of nkCharLit..nkUInt64Lit:
case skipTypes(ty, abstractVarRange).kind case skipTypes(ty, abstractVarRange).kind
of tyChar, tyInt64, tyNil: of tyChar, tyInt64, tyNil:
result = intLiteral(v.intVal) result = intLiteral(n.intVal)
of tyInt: of tyInt:
if (v.intVal >= low(int32)) and (v.intVal <= high(int32)): if (n.intVal >= low(int32)) and (n.intVal <= high(int32)):
result = int32Literal(int32(v.intVal)) result = int32Literal(int32(n.intVal))
else: else:
result = intLiteral(v.intVal) result = intLiteral(n.intVal)
of tyBool: of tyBool:
if v.intVal != 0: result = toRope("NIM_TRUE") if n.intVal != 0: result = toRope("NIM_TRUE")
else: result = toRope("NIM_FALSE") else: result = toRope("NIM_FALSE")
else: else:
result = ropef("(($1) $2)", [getTypeDesc(p.module, result = ropef("(($1) $2)", [getTypeDesc(p.module,
skipTypes(ty, abstractVarRange)), intLiteral(v.intVal)]) skipTypes(ty, abstractVarRange)), intLiteral(n.intVal)])
of nkNilLit: of nkNilLit:
result = toRope("NIM_NIL") let t = skipTypes(ty, abstractVarRange)
if t.kind == tyProc and t.callConv == ccClosure:
var id = NodeTableTestOrSet(p.module.dataCache, n, gBackendId)
result = con("TMP", toRope(id))
if id == gBackendId:
# not found in cache:
inc(gBackendId)
appf(p.module.s[cfsData],
"static NIM_CONST $1 $2 = {NIM_NIL,NIM_NIL};$n",
[getTypeDesc(p.module, t), result])
else:
result = toRope("NIM_NIL")
of nkStrLit..nkTripleStrLit: of nkStrLit..nkTripleStrLit:
if skipTypes(ty, abstractVarRange).kind == tyString: if skipTypes(ty, abstractVarRange).kind == tyString:
var id = NodeTableTestOrSet(p.module.dataCache, v, gBackendId) var id = NodeTableTestOrSet(p.module.dataCache, n, gBackendId)
if id == gBackendId: if id == gBackendId:
# string literal not found in the cache: # string literal not found in the cache:
result = ropecg(p.module, "((#NimStringDesc*) &$1)", result = ropecg(p.module, "((#NimStringDesc*) &$1)",
[getStrLit(p.module, v.strVal)]) [getStrLit(p.module, n.strVal)])
else: else:
result = ropecg(p.module, "((#NimStringDesc*) &TMP$1)", [toRope(id)]) result = ropecg(p.module, "((#NimStringDesc*) &TMP$1)", [toRope(id)])
else: else:
result = makeCString(v.strVal) result = makeCString(n.strVal)
of nkFloatLit..nkFloat64Lit: of nkFloatLit..nkFloat64Lit:
result = toRope(v.floatVal.ToStrMaxPrecision) result = toRope(n.floatVal.ToStrMaxPrecision)
else: else:
InternalError(v.info, "genLiteral(" & $v.kind & ')') InternalError(n.info, "genLiteral(" & $n.kind & ')')
result = nil result = nil
proc genLiteral(p: BProc, v: PNode): PRope = proc genLiteral(p: BProc, n: PNode): PRope =
result = genLiteral(p, v, v.typ) result = genLiteral(p, n, n.typ)
proc bitSetToWord(s: TBitSet, size: int): BiggestInt = proc bitSetToWord(s: TBitSet, size: int): BiggestInt =
result = 0 result = 0

View file

@ -9,15 +9,12 @@ New pragmas:
- document destructors - document destructors
- ``borrow`` needs to take type classes into account - ``borrow`` needs to take type classes into account
- make use of ``tyIter`` to fix the implicit items/pairs issue
- ``=`` should be overloadable; requires specialization for ``=`` - ``=`` should be overloadable; requires specialization for ``=``
- optimize genericAssign in the code generator - optimize genericAssign in the code generator
- fix remaining closure bugs: - fix remaining closure bugs:
- make toplevel but in a scope vars local; make procs there inner procs - make toplevel but in a scope vars local; make procs there inner procs
- fix evals.nim with closures - fix evals.nim with closures
- implement "closure tuple consists of a single 'ref'" optimization - implement "closure tuple consists of a single 'ref'" optimization
- make 'raiseHook' take a closure and provide push and pop for this
--> Lisp-style exception system
- document 'do' notation - document 'do' notation
- rethink the syntax: distinction between expr and stmt is unfortunate; - rethink the syntax: distinction between expr and stmt is unfortunate;
@ -43,6 +40,7 @@ version 0.9.XX
- document it - document it
- fix exception handling - fix exception handling
- make use of ``tyIter`` to fix the implicit items/pairs issue
- make templates hygienic by default: try to gensym() everything in the 'block' - make templates hygienic by default: try to gensym() everything in the 'block'
of a template; find a better solution for gensym instead of `*ident` of a template; find a better solution for gensym instead of `*ident`
- introduce 'callsite' magic and make macros and templates the same - introduce 'callsite' magic and make macros and templates the same
@ -94,6 +92,8 @@ Library
Low priority Low priority
------------ ------------
- make 'raiseHook' take a closure and provide push and pop for this
--> Lisp-style condition system
- change how comments are part of the AST - change how comments are part of the AST
- fix & document ``byCopy`` pragma - fix & document ``byCopy`` pragma
- ``with proc `+`(x, y: T): T`` for generic code - ``with proc `+`(x, y: T): T`` for generic code