hlo.nim 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2013 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. # This include implements the high level optimization pass.
  10. # included from sem.nim
  11. proc hlo(c: PContext, n: PNode): PNode
  12. proc evalPattern(c: PContext, n, orig: PNode): PNode =
  13. internalAssert c.config, n.kind == nkCall and n[0].kind == nkSym
  14. # we need to ensure that the resulting AST is semchecked. However, it's
  15. # awful to semcheck before macro invocation, so we don't and treat
  16. # templates and macros as immediate in this context.
  17. var rule: string =
  18. if c.config.hasHint(hintPattern):
  19. renderTree(n, {renderNoComments})
  20. else:
  21. ""
  22. let s = n[0].sym
  23. case s.kind
  24. of skMacro:
  25. result = semMacroExpr(c, n, orig, s)
  26. of skTemplate:
  27. result = semTemplateExpr(c, n, s, {efFromHlo})
  28. else:
  29. result = semDirectOp(c, n, {})
  30. if c.config.hasHint(hintPattern):
  31. message(c.config, orig.info, hintPattern, rule & " --> '" &
  32. renderTree(result, {renderNoComments}) & "'")
  33. proc applyPatterns(c: PContext, n: PNode): PNode =
  34. result = n
  35. # we apply the last pattern first, so that pattern overriding is possible;
  36. # however the resulting AST would better not trigger the old rule then
  37. # anymore ;-)
  38. for i in countdown(c.patterns.len-1, 0):
  39. let pattern = c.patterns[i]
  40. if not isNil(pattern):
  41. let x = applyRule(c, pattern, result)
  42. if not isNil(x):
  43. assert x.kind in {nkStmtList, nkCall}
  44. # better be safe than sorry, so check evalTemplateCounter too:
  45. inc(c.config.evalTemplateCounter)
  46. if c.config.evalTemplateCounter > evalTemplateLimit:
  47. globalError(c.config, n.info, "template instantiation too nested")
  48. # deactivate this pattern:
  49. c.patterns[i] = nil
  50. if x.kind == nkStmtList:
  51. assert x.len == 3
  52. x[1] = evalPattern(c, x[1], result)
  53. result = flattenStmts(x)
  54. else:
  55. result = evalPattern(c, x, result)
  56. dec(c.config.evalTemplateCounter)
  57. # activate this pattern again:
  58. c.patterns[i] = pattern
  59. proc hlo(c: PContext, n: PNode): PNode =
  60. inc(c.hloLoopDetector)
  61. # simply stop and do not perform any further transformations:
  62. if c.hloLoopDetector > 300: return n
  63. case n.kind
  64. of nkMacroDef, nkTemplateDef, procDefs:
  65. # already processed (special cases in semstmts.nim)
  66. result = n
  67. else:
  68. if n.kind in {nkFastAsgn, nkAsgn, nkSinkAsgn, nkIdentDefs, nkVarTuple} and
  69. n[0].kind == nkSym and
  70. {sfGlobal, sfPure} <= n[0].sym.flags:
  71. # do not optimize 'var g {.global} = re(...)' again!
  72. return n
  73. result = applyPatterns(c, n)
  74. if result == n:
  75. # no optimization applied, try subtrees:
  76. for i in 0..<result.safeLen:
  77. let a = result[i]
  78. let h = hlo(c, a)
  79. if h != a: result[i] = h
  80. else:
  81. # perform type checking, so that the replacement still fits:
  82. if isEmptyType(n.typ) and isEmptyType(result.typ):
  83. discard
  84. else:
  85. result = fitNode(c, n.typ, result, n.info)
  86. # optimization has been applied so check again:
  87. result = commonOptimizations(c.graph, c.idgen, c.module, result)
  88. result = hlo(c, result)
  89. result = commonOptimizations(c.graph, c.idgen, c.module, result)
  90. proc hloBody(c: PContext, n: PNode): PNode =
  91. # fast exit:
  92. if c.patterns.len == 0 or optTrMacros notin c.config.options: return n
  93. c.hloLoopDetector = 0
  94. result = hlo(c, n)
  95. proc hloStmt(c: PContext, n: PNode): PNode =
  96. # fast exit:
  97. if c.patterns.len == 0 or optTrMacros notin c.config.options: return n
  98. c.hloLoopDetector = 0
  99. result = hlo(c, n)