Class IRRewriter

java.lang.Object
org.ek9lang.compiler.ir.support.IRRewriter

public final class IRRewriter extends Object
THE IR rewriter: rebuilds an instruction list bottom-up. Every composite is rebuilt with its rewritten nested lists (CompositeInstr.withNestedLists(UnaryOperator)), then every instruction - composite or not - is given to the rewrite, which returns what replaces it (itself, nothing, or several). Instructions are immutable, so this is how any pass changes IR: the ownership lowering now, the IR_OPTIMISATION passes later. Nothing unchanged is copied: an untouched list comes back as the same list. Stateless: hold one.
  • Constructor Details

    • IRRewriter

      public IRRewriter()
  • Method Details

    • rewrite

      public void rewrite(OperationInstr operation, Function<IRInstr, List<IRInstr>> rewrite)
      Rewrite an operation's body in place (the operation takes the rebuilt body, or keeps its own when nothing changed).
    • rewrite

      public void rewrite(OperationInstr operation, BiFunction<IRInstr, IRInstr, List<IRInstr>> rewrite)
      Rewrite an operation's body in place with a rewrite that sees each instruction TWICE: as it WAS (so a decision made by identity over the body before the rewrite - a placement's map - can be looked up) and as it IS now (its nested lists rebuilt, which is what a composite whose lists changed has become; the same object otherwise), and returns what replaces the latter. A composite that gained an instruction inside one of its lists is a new object by the time a per-instruction rewrite sees it, so a lookup by its identity would miss it - the N-V placement found that on a list literal whose elements were stamped.
    • rewrite

      public List<IRInstr> rewrite(List<IRInstr> instructions, Function<IRInstr, List<IRInstr>> rewrite)
      Rewrite an instruction list; the same list when nothing in it (at any depth) changed.
    • rewriteLists

      public void rewriteLists(OperationInstr operation, UnaryOperator<List<IRInstr>> listRewrite)
      Rewrite an operation's body list by list: every nested list first (bottom-up), then the list that holds it. For a pass that needs to see a whole list at once - one instruction's replacement depending on what follows it in the same list.
    • rewriteLists

      public List<IRInstr> rewriteLists(List<IRInstr> instructions, UnaryOperator<List<IRInstr>> listRewrite)
      Rewrite a list and every list nested in it, bottom-up; listRewrite returns its argument unchanged (the same list) when it changes nothing.