hlo.nim 3.6 KB

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