summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/transform/TailCalls.scala
blob: c2681b2d3bf4be84bf8dcf469aa76caed0c5ccf9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/* NSC -- new scala compiler
 * Copyright 2005 LAMP/EPFL
 * @author
 */
// $Id$
package scala.tools.nsc.transform;

import scala.tools.nsc.symtab.Flags;

/** Perform tail recursive call elimination.
 */
abstract class TailCalls extends Transform
                         /* with JavaLogging() */ {
  // inherits abstract value `global' and class `Phase' from Transform

  import global._;                  // the global environment
  import definitions._;             // standard classes and methods
  import typer.{typed, atOwner};               // methods to type trees
  import posAssigner.atPos;         // for filling in tree positions

  val phaseName: String = "tailcalls";
  def newTransformer(unit: CompilationUnit): Transformer = new TailCallElimination(unit);

  /**
   * A Tail Call Transformer
   *
   * @author     Erik Stenman, Iulian Dragos
   * @version    1.1
   *
   * What it does:
   *
   * Finds method calls in tail-position and replaces them with jumps.
   * A call is in a tail-position if it is the last instruction to be
   * executed in the body of a method.  This is done by recursing over
   * the trees that may contain calls in tail-position (trees that can't
   * contain such calls are not transformed). However, they are not that
   * many.
   *
   * Self-recursive calls in tail-position are replaced by jumps to a
   * label at the beginning of the method. As the JVM provides no way to
   * jump from a method to another one, non-recursive calls in
   * tail-position are not optimized.
   *
   * A method call is self-recursive if it calls the current method on
   * the current instance and the method is final (otherwise, it could
   * be a call to an overridden method in a subclass). Furthermore, If
   * the method has type parameters, the call must contain these
   * parameters as type arguments.
   *
   * This phase has been moved before pattern matching to catch more
   * of the common cases of tail recursive functions. This means that
   * more cases should be taken into account (like nested function, and
   * pattern cases).
   *
   * If a method contains self-recursive calls, a label is added to at
   * the beginning of its body and the calls are replaced by jumps to
   * that label.
   *
   * Assumes: Uncurry has been run already, and no multiple parameter
   *          lists exit.
   */
  class TailCallElimination(unit: CompilationUnit) extends Transformer {

    class Context {
      /** The current method */
      var currentMethod: Symbol = NoSymbol;

      /** The current tail-call label */
      var label: Symbol = NoSymbol;

      /** The expected type arguments of self-recursive calls */
      var tparams: List[Symbol] = Nil;

      /** Tells whether we are in a (possible) tail position */
      var tailPos = false;

      /** Is the label accessed? */
      var accessed = false;

      def this(that: Context) = {
        this();
        this.currentMethod = that.currentMethod;
        this.label         = that.label;
        this.tparams       = that.tparams;
        this.tailPos       = that.tailPos;
        this.accessed      = that.accessed;
      }

      /** Create a new method symbol for the current method and store it in
        * the label field.
        */
      def makeLabel(): Unit = {
        label = currentMethod.newLabel(currentMethod.pos, "_" + currentMethod.name);
        accessed = false;
      }

      override def toString(): String = (
        "" + currentMethod.name + " tparams: " + tparams + " tailPos: " + tailPos +
        " accessed: " + accessed + "\nLabel: " + label + "\nLabel type: " + label.info
      );
    }

    private def mkContext(that: Context) = new Context(that);
    private def mkContext(that: Context, tp: Boolean): Context = {
      val t = mkContext(that);
      t.tailPos = tp;
      t
    }

    private var ctx: Context = new Context();

    /** Rewrite this tree to contain no tail recursive calls */
    def transform(tree: Tree, nctx: Context): Tree = {
      val oldCtx = ctx;
      ctx = nctx;
      val t = transform(tree);
      this.ctx = oldCtx;
      t
    }

    override def transform(tree: Tree): Tree = {
      tree match {

        case DefDef(mods, name, tparams, vparams, tpt, rhs) =>
          log("Entering DefDef: " + name);
          val newCtx = mkContext(ctx);
          newCtx.currentMethod = tree.symbol;
          newCtx.makeLabel();
          newCtx.label.setInfo(tree.symbol.info);
          newCtx.tailPos = true;

          val t1 = if (newCtx.currentMethod.isFinal ||
                       newCtx.currentMethod.enclClass.hasFlag(Flags.MODULE)) {
            newCtx.tparams = Nil;
            log("  Considering " + name + " for tailcalls");
            tree.symbol.tpe match {
              case PolyType(tpes, restpe) =>
                newCtx.tparams = tparams map (.symbol);
                newCtx.label.setInfo(
                  restpe.substSym(tpes, tparams map (.symbol)));
                log("adding types: " + newCtx.tparams);
                log("setting return resultType to: " + newCtx.label.tpe);
              case _ => ();
            }

            var newRHS = transform(rhs, newCtx);
            if (newCtx.accessed) {
              log("Rewrote def " + newCtx.currentMethod);

              newRHS =
                  typed(atPos(tree.pos)(
                    LabelDef(newCtx.label,
                             List.flatten(vparams) map (.symbol),
                             newRHS)));
              copy.DefDef(tree, mods, name, tparams, vparams, tpt, newRHS);
            } else
              copy.DefDef(tree, mods, name, tparams, vparams, tpt, newRHS);
          } else {
            log("Non-final method: " + name);
            // Martin: OK like that?
            copy.DefDef(tree, mods, name, tparams, vparams, tpt, transform(rhs, newCtx))
          }
          log("Leaving DefDef: " + name);
          t1;

        case EmptyTree => tree;

        case PackageDef(name, stats) => super.transform(tree);
        case ClassDef(mods, name, tparams, tpt, impl) =>
          log("Entering class " + name);
          val res = super.transform(tree);
          log("Leaving class " + name);
          res

        case ValDef(mods, name, tpt, rhs) => tree;
        case AbsTypeDef(mods, name, lo, hi) => tree; //  (eliminated by erasure)
        case AliasTypeDef(mods, name, tparams, rhs) => tree; // (eliminated by erasure)
        case LabelDef(name, params, rhs) => super.transform(tree);

        case Template(parents, body) => super.transform(tree);

        case Block(stats, expr) =>
          copy.Block(tree,
                     transformTrees(stats, mkContext(ctx, false)),
                     transform(expr));

        case CaseDef(pat, guard, body) =>
          copy.CaseDef(tree, pat, guard, transform(body));

        case Sequence(_) | Alternative(_) |
             Star(_)     | Bind(_, _) =>
               throw new RuntimeException("We should've never gotten inside a pattern");

        case Function(vparams, body) =>
          tree
          //throw new RuntimeException("Anonymous function should not exist at this point. at: " + unit.position(tree.pos));

        case Assign(lhs, rhs) => super.transform(tree);
        case If(cond, thenp, elsep) =>
          copy.If(tree, cond, transform(thenp), transform(elsep));

        case Match(selector, cases) => super.transform(tree);
        case Return(expr) =>           super.transform(tree);
        case Try(block, catches, finalizer) => super.transform(tree);

        case Throw(expr) => super.transform(tree);
        case New(tpt) => super.transform(tree);
        case Typed(expr, tpt) => super.transform(tree);

        case Apply(tapply @ TypeApply(fun, targs), vargs) =>
          if ( ctx.currentMethod.isFinal &&
               ctx.tailPos &&
               isSameTypes(ctx.tparams, targs map (.tpe.symbol)) &&
               isRecursiveCall(fun))
                 rewriteTailCall(fun, transformTrees(vargs, mkContext(ctx, false)))
               else
                 copy.Apply(tree, tapply, transformTrees(vargs, mkContext(ctx, false)));

        case TypeApply(fun, args) =>
          super.transform(tree);
//          throw new RuntimeException("Lonely TypeApply found -- we can only handle them inside Apply(TypeApply()): " + tree + " at: " + unit);

        case Apply(fun, args) =>
          if (ctx.currentMethod.isFinal &&
              ctx.tailPos &&
              isRecursiveCall(fun))
            rewriteTailCall(fun, transformTrees(args, mkContext(ctx, false)));
          else
            copy.Apply(tree, fun, transformTrees(args, mkContext(ctx, false)));

        case Super(qual, mix) =>
          tree;
        case This(qual) =>
          tree;
        case Select(qualifier, selector) =>
          tree;
        case Ident(name) =>
          tree;
        case Literal(value) =>
          tree;
        case TypeTree() =>  tree;

        case _ =>
          tree
      }
    }

    def transformTrees(trees: List[Tree], nctx: Context): List[Tree] =
      trees map ((tree) => transform(tree, nctx));

    private def rewriteTailCall(fun: Tree, args: List[Tree]): Tree = {
      log("Rewriting tail recursive method call at: " +
                      unit.position(fun.pos));
      ctx.accessed = true;
      typed(atPos(fun.pos)(
        Apply(Ident(ctx.label), args)));
    }

    private def isSameTypes(ts1: List[Symbol], ts2: List[Symbol]): Boolean = {
      def isSameType(t1: Symbol, t2: Symbol) = {
        log("" + t1 + " vs " + t2);
        t1 == t2
      }
      List.forall2(ts1, ts2)(isSameType)
    }

    /** Return true if the fun tree refers to the same method as the one
      * saved in ctx. If it is a method call, we check that it is applied to
      * "this"
      */
    private def isRecursiveCall(fun: Tree): Boolean =
      if (fun.symbol eq ctx.currentMethod)
        fun match {
          case Select(t @ This(_), _) =>
            assert(t.symbol == ctx.currentMethod.owner, "This refers to other class: " +
                   t.symbol + ": " + ctx.currentMethod.owner);
            true;

          case Ident(_) => true;
          case _ => false;
        }
    else
      false;
  }

}