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