src/jdk/nashorn/internal/ir/LoopNode.java

Wed, 27 Apr 2016 01:36:41 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:36:41 +0800
changeset 0
b1a7da25b547
child 952
6d5471a497fb
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/nashorn/
changeset: 1034:4b9cc65dd24d
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package jdk.nashorn.internal.ir;
aoqi@0 27
aoqi@0 28 import java.util.Arrays;
aoqi@0 29 import java.util.List;
aoqi@0 30 import jdk.nashorn.internal.codegen.Label;
aoqi@0 31
aoqi@0 32 /**
aoqi@0 33 * A loop node, for example a while node, do while node or for node
aoqi@0 34 */
aoqi@0 35 public abstract class LoopNode extends BreakableStatement {
aoqi@0 36 /** loop continue label. */
aoqi@0 37 protected final Label continueLabel;
aoqi@0 38
aoqi@0 39 /** Loop test node, null if infinite */
aoqi@0 40 protected final Expression test;
aoqi@0 41
aoqi@0 42 /** Loop body */
aoqi@0 43 protected final Block body;
aoqi@0 44
aoqi@0 45 /** Can control flow escape from loop, e.g. through breaks or continues to outer loops? */
aoqi@0 46 protected final boolean controlFlowEscapes;
aoqi@0 47
aoqi@0 48 /**
aoqi@0 49 * Constructor
aoqi@0 50 *
aoqi@0 51 * @param lineNumber lineNumber
aoqi@0 52 * @param token token
aoqi@0 53 * @param finish finish
aoqi@0 54 * @param test test, or null if infinite loop
aoqi@0 55 * @param body loop body
aoqi@0 56 * @param controlFlowEscapes controlFlowEscapes
aoqi@0 57 */
aoqi@0 58 protected LoopNode(final int lineNumber, final long token, final int finish, final Expression test, final Block body, final boolean controlFlowEscapes) {
aoqi@0 59 super(lineNumber, token, finish, new Label("while_break"));
aoqi@0 60 this.continueLabel = new Label("while_continue");
aoqi@0 61 this.test = test;
aoqi@0 62 this.body = body;
aoqi@0 63 this.controlFlowEscapes = controlFlowEscapes;
aoqi@0 64 }
aoqi@0 65
aoqi@0 66 /**
aoqi@0 67 * Constructor
aoqi@0 68 *
aoqi@0 69 * @param loopNode loop node
aoqi@0 70 * @param test new test
aoqi@0 71 * @param body new body
aoqi@0 72 * @param controlFlowEscapes controlFlowEscapes
aoqi@0 73 */
aoqi@0 74 protected LoopNode(final LoopNode loopNode, final Expression test, final Block body, final boolean controlFlowEscapes) {
aoqi@0 75 super(loopNode);
aoqi@0 76 this.continueLabel = new Label(loopNode.continueLabel);
aoqi@0 77 this.test = test;
aoqi@0 78 this.body = body;
aoqi@0 79 this.controlFlowEscapes = controlFlowEscapes;
aoqi@0 80 }
aoqi@0 81
aoqi@0 82 @Override
aoqi@0 83 public abstract Node ensureUniqueLabels(final LexicalContext lc);
aoqi@0 84
aoqi@0 85 /**
aoqi@0 86 * Does the control flow escape from this loop, i.e. through breaks or
aoqi@0 87 * continues to outer loops?
aoqi@0 88 * @return true if control flow escapes
aoqi@0 89 */
aoqi@0 90 public boolean controlFlowEscapes() {
aoqi@0 91 return controlFlowEscapes;
aoqi@0 92 }
aoqi@0 93
aoqi@0 94
aoqi@0 95 @Override
aoqi@0 96 public boolean isTerminal() {
aoqi@0 97 if (!mustEnter()) {
aoqi@0 98 return false;
aoqi@0 99 }
aoqi@0 100 //must enter but control flow may escape - then not terminal
aoqi@0 101 if (controlFlowEscapes) {
aoqi@0 102 return false;
aoqi@0 103 }
aoqi@0 104 //must enter, but body ends with return - then terminal
aoqi@0 105 if (body.isTerminal()) {
aoqi@0 106 return true;
aoqi@0 107 }
aoqi@0 108 //no breaks or returns, it is still terminal if we can never exit
aoqi@0 109 return test == null;
aoqi@0 110 }
aoqi@0 111
aoqi@0 112 /**
aoqi@0 113 * Conservative check: does this loop have to be entered?
aoqi@0 114 * @return true if body will execute at least once
aoqi@0 115 */
aoqi@0 116 public abstract boolean mustEnter();
aoqi@0 117
aoqi@0 118 /**
aoqi@0 119 * Get the continue label for this while node, i.e. location to go to on continue
aoqi@0 120 * @return continue label
aoqi@0 121 */
aoqi@0 122 public Label getContinueLabel() {
aoqi@0 123 return continueLabel;
aoqi@0 124 }
aoqi@0 125
aoqi@0 126 @Override
aoqi@0 127 public List<Label> getLabels() {
aoqi@0 128 return Arrays.asList(breakLabel, continueLabel);
aoqi@0 129 }
aoqi@0 130
aoqi@0 131 @Override
aoqi@0 132 public boolean isLoop() {
aoqi@0 133 return true;
aoqi@0 134 }
aoqi@0 135
aoqi@0 136 /**
aoqi@0 137 * Get the body for this for node
aoqi@0 138 * @return the body
aoqi@0 139 */
aoqi@0 140 public abstract Block getBody();
aoqi@0 141
aoqi@0 142 /**
aoqi@0 143 * @param lc lexical context
aoqi@0 144 * @param body new body
aoqi@0 145 * @return new for node if changed or existing if not
aoqi@0 146 */
aoqi@0 147 public abstract LoopNode setBody(final LexicalContext lc, final Block body);
aoqi@0 148
aoqi@0 149 /**
aoqi@0 150 * Get the test for this for node
aoqi@0 151 * @return the test
aoqi@0 152 */
aoqi@0 153 public abstract Expression getTest();
aoqi@0 154
aoqi@0 155 /**
aoqi@0 156 * Set the test for this for node
aoqi@0 157 *
aoqi@0 158 * @param lc lexical context
aoqi@0 159 * @param test new test
aoqi@0 160 * @return same or new node depending on if test was changed
aoqi@0 161 */
aoqi@0 162 public abstract LoopNode setTest(final LexicalContext lc, final Expression test);
aoqi@0 163
aoqi@0 164 /**
aoqi@0 165 * Set the control flow escapes flag for this node.
aoqi@0 166 * TODO - integrate this with Lowering in a better way
aoqi@0 167 *
aoqi@0 168 * @param lc lexical context
aoqi@0 169 * @param controlFlowEscapes control flow escapes value
aoqi@0 170 * @return new loop node if changed otherwise the same
aoqi@0 171 */
aoqi@0 172 public abstract LoopNode setControlFlowEscapes(final LexicalContext lc, final boolean controlFlowEscapes);
aoqi@0 173
aoqi@0 174 }

mercurial