From 9de3bc8ef64fbd2fa1e336b05a09ad8f898a5d45 Mon Sep 17 00:00:00 2001 From: Araq Date: Tue, 17 Sep 2013 23:43:07 +0200 Subject: [PATCH] improvements for TR macros --- compiler/hlo.nim | 21 ++++++++++++++------- compiler/patterns.nim | 9 --------- compiler/semdata.nim | 1 + compiler/semstmts.nim | 11 ++++++++--- compiler/transf.nim | 30 ++++++++++++++++++++++++++++++ todo.txt | 2 -- 6 files changed, 53 insertions(+), 21 deletions(-) diff --git a/compiler/hlo.nim b/compiler/hlo.nim index 152fd44147..f19f6dd92d 100644 --- a/compiler/hlo.nim +++ b/compiler/hlo.nim @@ -1,7 +1,7 @@ # # # The Nimrod Compiler -# (c) Copyright 2012 Andreas Rumpf +# (c) Copyright 2013 Andreas Rumpf # # See the file "copying.txt", included in this # distribution, for details about the copyright. @@ -30,8 +30,6 @@ proc evalPattern(c: PContext, n, orig: PNode): PNode = if optHints in gOptions and hintPattern in gNotes: Message(orig.info, hintPattern, rule & " --> '" & renderTree(result, {renderNoComments}) & "'") - # check the resulting AST for optimization rules again: - result = hlo(c, result) proc applyPatterns(c: PContext, n: PNode): PNode = result = n @@ -44,11 +42,12 @@ proc applyPatterns(c: PContext, n: PNode): PNode = let x = applyRule(c, pattern, result) if not isNil(x): assert x.kind in {nkStmtList, nkCall} + # better be safe than sorry, so check evalTemplateCounter too: inc(evalTemplateCounter) if evalTemplateCounter > 100: GlobalError(n.info, errTemplateInstantiationTooNested) # deactivate this pattern: - c.patterns[i] = nil + #c.patterns[i] = nil if x.kind == nkStmtList: assert x.len == 3 x.sons[1] = evalPattern(c, x.sons[1], result) @@ -57,9 +56,12 @@ proc applyPatterns(c: PContext, n: PNode): PNode = result = evalPattern(c, x, result) dec(evalTemplateCounter) # activate this pattern again: - c.patterns[i] = pattern + #c.patterns[i] = pattern proc hlo(c: PContext, n: PNode): PNode = + inc(c.hloLoopDetector) + # simply stop and do not perform any further transformations: + if c.hloLoopDetector > 300: result = n case n.kind of nkMacroDef, nkTemplateDef, procDefs: # already processed (special cases in semstmts.nim) @@ -74,18 +76,23 @@ proc hlo(c: PContext, n: PNode): PNode = if h != a: result.sons[i] = h else: # perform type checking, so that the replacement still fits: - if n.typ == nil and (result.typ == nil or - result.typ.kind in {tyStmt, tyEmpty}): + if isEmptyType(n.typ) and isEmptyType(result.typ): nil else: result = fitNode(c, n.typ, result) + # optimization has been applied so check again: + result = commonOptimizations(c.module, result) + result = hlo(c, result) + result = commonOptimizations(c.module, result) proc hloBody(c: PContext, n: PNode): PNode = # fast exit: if c.patterns.len == 0 or optPatterns notin gOptions: return n + c.hloLoopDetector = 0 result = hlo(c, n) proc hloStmt(c: PContext, n: PNode): PNode = # fast exit: if c.patterns.len == 0 or optPatterns notin gOptions: return n + c.hloLoopDetector = 0 result = hlo(c, n) diff --git a/compiler/patterns.nim b/compiler/patterns.nim index ff7f18ac00..b7792100f5 100644 --- a/compiler/patterns.nim +++ b/compiler/patterns.nim @@ -237,15 +237,6 @@ proc addToArgList(result, n: PNode) = else: for i in 0 .. = 3): + result = newNodeIT(nkCall, n.info, n.typ) + add(result, n.sons[0]) + var args = newNode(nkArgList) + flattenTreeAux(args, n, op) + var j = 0 + while j < sonsLen(args): + var a = args.sons[j] + inc(j) + if isConstExpr(a): + while j < sonsLen(args): + let b = args.sons[j] + if not isConstExpr(b): break + a = evalOp(op.magic, result, a, b, nil) + inc(j) + add(result, a) + if len(result) == 2: result = result[1] + else: + var cnst = getConstExpr(c, n) + # we inline constants if they are not complex constants: + if cnst != nil and not dontInlineConstant(n, cnst): + result = cnst + else: + result = n + proc transform(c: PTransf, n: PNode): PTransNode = case n.kind of nkSym: diff --git a/todo.txt b/todo.txt index c745630618..e963a3ae42 100644 --- a/todo.txt +++ b/todo.txt @@ -12,8 +12,6 @@ version 0.9.4 - special rule for ``[]=`` - ``=`` should be overloadable; requires specialization for ``=``; general lift mechanism in the compiler is already implemented for 'fields' -- mocking support with ``tyProxy`` that does: fallback for ``.`` operator - - overloading of ``.``? Special case ``.=``? - built-in 'getImpl' - optimize 'genericReset'; 'newException' leads to code bloat - stack-less GC