ProgramPoints.java revision 953:221a84ef44c0
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 */ 25package jdk.nashorn.internal.codegen; 26 27import static jdk.nashorn.internal.runtime.UnwarrantedOptimismException.FIRST_PROGRAM_POINT; 28import static jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor.MAX_PROGRAM_POINT_VALUE; 29 30import java.util.HashSet; 31import java.util.Set; 32import jdk.nashorn.internal.IntDeque; 33import jdk.nashorn.internal.ir.AccessNode; 34import jdk.nashorn.internal.ir.BinaryNode; 35import jdk.nashorn.internal.ir.CallNode; 36import jdk.nashorn.internal.ir.Expression; 37import jdk.nashorn.internal.ir.FunctionNode; 38import jdk.nashorn.internal.ir.IdentNode; 39import jdk.nashorn.internal.ir.IndexNode; 40import jdk.nashorn.internal.ir.LexicalContext; 41import jdk.nashorn.internal.ir.Node; 42import jdk.nashorn.internal.ir.Optimistic; 43import jdk.nashorn.internal.ir.UnaryNode; 44import jdk.nashorn.internal.ir.VarNode; 45import jdk.nashorn.internal.ir.visitor.NodeVisitor; 46 47/** 48 * Find program points in the code that are needed for optimistic assumptions 49 */ 50class ProgramPoints extends NodeVisitor<LexicalContext> { 51 52 private final IntDeque nextProgramPoint = new IntDeque(); 53 private final Set<Node> noProgramPoint = new HashSet<>(); 54 55 ProgramPoints() { 56 super(new LexicalContext()); 57 } 58 59 private int next() { 60 final int next = nextProgramPoint.getAndIncrement(); 61 if(next > MAX_PROGRAM_POINT_VALUE) { 62 throw new AssertionError("Function has more than " + MAX_PROGRAM_POINT_VALUE + " program points"); 63 } 64 return next; 65 } 66 67 @Override 68 public boolean enterFunctionNode(final FunctionNode functionNode) { 69 nextProgramPoint.push(FIRST_PROGRAM_POINT); 70 return true; 71 } 72 73 @Override 74 public Node leaveFunctionNode(final FunctionNode functionNode) { 75 nextProgramPoint.pop(); 76 return functionNode; 77 } 78 79 private Expression setProgramPoint(final Optimistic optimistic) { 80 if (noProgramPoint.contains(optimistic)) { 81 return (Expression)optimistic; 82 } 83 return (Expression)(optimistic.canBeOptimistic() ? optimistic.setProgramPoint(next()) : optimistic); 84 } 85 86 @Override 87 public boolean enterVarNode(final VarNode varNode) { 88 noProgramPoint.add(varNode.getAssignmentDest()); 89 return true; 90 } 91 92 @Override 93 public boolean enterIdentNode(final IdentNode identNode) { 94 if (identNode.isInternal()) { 95 noProgramPoint.add(identNode); 96 } 97 return true; 98 } 99 100 @Override 101 public Node leaveIdentNode(final IdentNode identNode) { 102 if(identNode.isPropertyName()) { 103 return identNode; 104 } 105 return setProgramPoint(identNode); 106 } 107 108 @Override 109 public Node leaveCallNode(final CallNode callNode) { 110 return setProgramPoint(callNode); 111 } 112 113 @Override 114 public Node leaveAccessNode(final AccessNode accessNode) { 115 return setProgramPoint(accessNode); 116 } 117 118 @Override 119 public Node leaveIndexNode(final IndexNode indexNode) { 120 return setProgramPoint(indexNode); 121 } 122 123 @Override 124 public Node leaveBinaryNode(final BinaryNode binaryNode) { 125 return setProgramPoint(binaryNode); 126 } 127 128 @Override 129 public Node leaveUnaryNode(final UnaryNode unaryNode) { 130 return setProgramPoint(unaryNode); 131 } 132} 133