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.
  • Method Details

    • rewrite

      public static 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 static 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 static 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 static 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.