View Javadoc
1   /*
2    * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  
26  package jdk.nashorn.internal.codegen;
27  
28  import java.util.ArrayList;
29  import java.util.List;
30  
31  import static jdk.nashorn.internal.codegen.CompilerConstants.SCOPE;
32  
33  import jdk.internal.org.objectweb.asm.MethodVisitor;
34  import jdk.nashorn.internal.codegen.types.Type;
35  import jdk.nashorn.internal.ir.LexicalContext;
36  import jdk.nashorn.internal.ir.SplitNode;
37  import jdk.nashorn.internal.runtime.Scope;
38  
39  /**
40   * Emitter used for splitting methods. Needs to keep track of if there are jump targets
41   * outside the current split node. All external jump targets encountered at method
42   * emission are logged, and {@code CodeGenerator#leaveSplitNode(SplitNode)} creates
43   * an appropriate jump table when the SplitNode has been iterated through
44   */
45  public class SplitMethodEmitter extends MethodEmitter {
46  
47      private final SplitNode splitNode;
48  
49      private final List<Label> externalTargets = new ArrayList<>();
50  
51      SplitMethodEmitter(final ClassEmitter classEmitter, final MethodVisitor mv, SplitNode splitNode) {
52          super(classEmitter, mv);
53          this.splitNode = splitNode;
54      }
55  
56      @Override
57      void splitAwareGoto(final LexicalContext lc, final Label label) {
58          assert splitNode != null;
59          final int index = findExternalTarget(lc, label);
60          if (index >= 0) {
61              loadCompilerConstant(SCOPE);
62              checkcast(Scope.class);
63              load(index + 1);
64              invoke(Scope.SET_SPLIT_STATE);
65              loadUndefined(Type.OBJECT);
66              _return(functionNode.getReturnType());
67              return;
68          }
69          super.splitAwareGoto(lc, label);
70      }
71  
72      private int findExternalTarget(final LexicalContext lc, final Label label) {
73          final int index = externalTargets.indexOf(label);
74  
75          if (index >= 0) {
76              return index;
77          }
78  
79          if (lc.isExternalTarget(splitNode, label)) {
80              externalTargets.add(label);
81              return externalTargets.size() - 1;
82          }
83          return -1;
84      }
85  
86      @Override
87      MethodEmitter registerReturn() {
88          setHasReturn();
89          loadCompilerConstant(SCOPE);
90          checkcast(Scope.class);
91          load(0);
92          invoke(Scope.SET_SPLIT_STATE);
93          return this;
94      }
95  
96      @Override
97      final List<Label> getExternalTargets() {
98          return externalTargets;
99      }
100 }