hlo.nim 3.7 KB

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