16147
|
1 |
/*
|
|
2 |
* Copyright (c) 2010, 2012, 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.List;
|
|
29 |
import java.util.Map;
|
|
30 |
import jdk.nashorn.internal.ir.AccessNode;
|
|
31 |
import jdk.nashorn.internal.ir.BinaryNode;
|
|
32 |
import jdk.nashorn.internal.ir.Block;
|
|
33 |
import jdk.nashorn.internal.ir.BreakNode;
|
|
34 |
import jdk.nashorn.internal.ir.CallNode;
|
|
35 |
import jdk.nashorn.internal.ir.CatchNode;
|
|
36 |
import jdk.nashorn.internal.ir.ContinueNode;
|
|
37 |
import jdk.nashorn.internal.ir.DoWhileNode;
|
|
38 |
import jdk.nashorn.internal.ir.ExecuteNode;
|
|
39 |
import jdk.nashorn.internal.ir.ForNode;
|
|
40 |
import jdk.nashorn.internal.ir.FunctionNode;
|
|
41 |
import jdk.nashorn.internal.ir.IdentNode;
|
|
42 |
import jdk.nashorn.internal.ir.IfNode;
|
|
43 |
import jdk.nashorn.internal.ir.IndexNode;
|
|
44 |
import jdk.nashorn.internal.ir.LiteralNode;
|
|
45 |
import jdk.nashorn.internal.ir.LiteralNode.ArrayLiteralNode;
|
|
46 |
import jdk.nashorn.internal.ir.LiteralNode.ArrayLiteralNode.ArrayUnit;
|
|
47 |
import jdk.nashorn.internal.ir.Node;
|
|
48 |
import jdk.nashorn.internal.ir.PropertyNode;
|
|
49 |
import jdk.nashorn.internal.ir.ReferenceNode;
|
|
50 |
import jdk.nashorn.internal.ir.ReturnNode;
|
|
51 |
import jdk.nashorn.internal.ir.RuntimeNode;
|
|
52 |
import jdk.nashorn.internal.ir.SplitNode;
|
|
53 |
import jdk.nashorn.internal.ir.SwitchNode;
|
|
54 |
import jdk.nashorn.internal.ir.ThrowNode;
|
|
55 |
import jdk.nashorn.internal.ir.TryNode;
|
|
56 |
import jdk.nashorn.internal.ir.VarNode;
|
|
57 |
import jdk.nashorn.internal.ir.WhileNode;
|
|
58 |
import jdk.nashorn.internal.ir.WithNode;
|
|
59 |
import jdk.nashorn.internal.ir.visitor.NodeVisitor;
|
|
60 |
import jdk.nashorn.internal.parser.TokenType;
|
|
61 |
|
|
62 |
/**
|
|
63 |
* Computes the "byte code" weight of an AST segment. This is used
|
|
64 |
* for Splitting too large class files
|
|
65 |
*/
|
|
66 |
public class WeighNodes extends NodeVisitor {
|
|
67 |
/*
|
|
68 |
* Weight constants.
|
|
69 |
*/
|
|
70 |
static final long FUNCTION_WEIGHT = 40;
|
|
71 |
private static final long ACCESS_WEIGHT = 4;
|
|
72 |
private static final long ADD_WEIGHT = 10;
|
|
73 |
private static final long BREAK_WEIGHT = 1;
|
|
74 |
private static final long CALL_WEIGHT = 10;
|
|
75 |
private static final long CATCH_WEIGHT = 10;
|
|
76 |
private static final long CONTINUE_WEIGHT = 1;
|
|
77 |
private static final long IF_WEIGHT = 2;
|
|
78 |
private static final long LITERAL_WEIGHT = 10;
|
|
79 |
private static final long LOOP_WEIGHT = 4;
|
|
80 |
private static final long REFERENCE_WEIGHT = 20;
|
|
81 |
private static final long RETURN_WEIGHT = 2;
|
|
82 |
private static final long SPLIT_WEIGHT = 40;
|
|
83 |
private static final long SWITCH_WEIGHT = 8;
|
|
84 |
private static final long THROW_WEIGHT = 2;
|
|
85 |
private static final long VAR_WEIGHT = 40;
|
|
86 |
private static final long WITH_WEIGHT = 8;
|
|
87 |
|
|
88 |
/** Accumulated weight. */
|
|
89 |
private long weight;
|
|
90 |
|
|
91 |
/** Optional cache for weight of block nodes. */
|
|
92 |
private final Map<Node, Long> weightCache;
|
|
93 |
|
|
94 |
/*
|
|
95 |
* Constructor
|
|
96 |
*
|
|
97 |
* @param weightCache cache of already calculated block weights
|
|
98 |
*/
|
|
99 |
private WeighNodes(final Map<Node, Long> weightCache) {
|
|
100 |
super(null, null);
|
|
101 |
this.weightCache = weightCache;
|
|
102 |
}
|
|
103 |
|
|
104 |
static long weigh(final Node node) {
|
|
105 |
final WeighNodes weighNodes = new WeighNodes(null);
|
|
106 |
node.accept(weighNodes);
|
|
107 |
return weighNodes.weight;
|
|
108 |
}
|
|
109 |
|
|
110 |
static long weigh(final Node node, final Map<Node, Long> weightCache) {
|
|
111 |
final WeighNodes weighNodes = new WeighNodes(weightCache);
|
|
112 |
node.accept(weighNodes);
|
|
113 |
return weighNodes.weight;
|
|
114 |
}
|
|
115 |
|
|
116 |
@Override
|
|
117 |
public Node leave(final AccessNode accessNode) {
|
|
118 |
weight += ACCESS_WEIGHT;
|
|
119 |
return accessNode;
|
|
120 |
}
|
|
121 |
|
|
122 |
@Override
|
|
123 |
public Node leave(final BinaryNode binaryNode) {
|
|
124 |
final TokenType tokenType = binaryNode.tokenType();
|
|
125 |
|
|
126 |
if (tokenType == TokenType.ADD || tokenType == TokenType.ASSIGN_ADD) {
|
|
127 |
weight += ADD_WEIGHT;
|
|
128 |
} else {
|
|
129 |
weight += 1;
|
|
130 |
}
|
|
131 |
|
|
132 |
return binaryNode;
|
|
133 |
}
|
|
134 |
|
|
135 |
@Override
|
|
136 |
public Node enter(final Block block) {
|
|
137 |
|
|
138 |
if (weightCache != null && weightCache.containsKey(block)) {
|
|
139 |
weight += weightCache.get(block);
|
|
140 |
return null;
|
|
141 |
}
|
|
142 |
|
|
143 |
return block;
|
|
144 |
}
|
|
145 |
|
|
146 |
@Override
|
|
147 |
public Node leave(final BreakNode breakNode) {
|
|
148 |
weight += BREAK_WEIGHT;
|
|
149 |
return breakNode;
|
|
150 |
}
|
|
151 |
|
|
152 |
@Override
|
|
153 |
public Node leave(final CallNode callNode) {
|
|
154 |
weight += CALL_WEIGHT;
|
|
155 |
return callNode;
|
|
156 |
}
|
|
157 |
|
|
158 |
@Override
|
|
159 |
public Node leave(final CatchNode catchNode) {
|
|
160 |
weight += CATCH_WEIGHT;
|
|
161 |
return catchNode;
|
|
162 |
}
|
|
163 |
|
|
164 |
@Override
|
|
165 |
public Node leave(final ContinueNode continueNode) {
|
|
166 |
weight += CONTINUE_WEIGHT;
|
|
167 |
return continueNode;
|
|
168 |
}
|
|
169 |
|
|
170 |
@Override
|
|
171 |
public Node leave(final DoWhileNode doWhileNode) {
|
|
172 |
weight += LOOP_WEIGHT;
|
|
173 |
return doWhileNode;
|
|
174 |
}
|
|
175 |
|
|
176 |
@Override
|
|
177 |
public Node leave(final ExecuteNode executeNode) {
|
|
178 |
return executeNode;
|
|
179 |
}
|
|
180 |
|
|
181 |
@Override
|
|
182 |
public Node leave(final ForNode forNode) {
|
|
183 |
weight += LOOP_WEIGHT;
|
|
184 |
return forNode;
|
|
185 |
}
|
|
186 |
|
|
187 |
@Override
|
|
188 |
public Node enter(final FunctionNode functionNode) {
|
|
189 |
final List<Node> statements = functionNode.getStatements();
|
|
190 |
|
|
191 |
for (final Node statement : statements) {
|
|
192 |
statement.accept(this);
|
|
193 |
}
|
|
194 |
|
|
195 |
return null;
|
|
196 |
}
|
|
197 |
|
|
198 |
@Override
|
|
199 |
public Node leave(final IdentNode identNode) {
|
|
200 |
weight += ACCESS_WEIGHT + identNode.getName().length() * 2;
|
|
201 |
return identNode;
|
|
202 |
}
|
|
203 |
|
|
204 |
@Override
|
|
205 |
public Node leave(final IfNode ifNode) {
|
|
206 |
weight += IF_WEIGHT;
|
|
207 |
return ifNode;
|
|
208 |
}
|
|
209 |
|
|
210 |
@Override
|
|
211 |
public Node leave(final IndexNode indexNode) {
|
|
212 |
weight += ACCESS_WEIGHT;
|
|
213 |
return indexNode;
|
|
214 |
}
|
|
215 |
|
|
216 |
@SuppressWarnings("rawtypes")
|
|
217 |
@Override
|
|
218 |
public Node enter(final LiteralNode literalNode) {
|
|
219 |
weight += LITERAL_WEIGHT;
|
|
220 |
|
|
221 |
if (literalNode instanceof ArrayLiteralNode) {
|
|
222 |
final ArrayLiteralNode arrayLiteralNode = (ArrayLiteralNode)literalNode;
|
|
223 |
final Node[] value = arrayLiteralNode.getValue();
|
|
224 |
final int[] postsets = arrayLiteralNode.getPostsets();
|
|
225 |
final List<ArrayUnit> units = arrayLiteralNode.getUnits();
|
|
226 |
|
|
227 |
if (units == null) {
|
|
228 |
for (final int postset : postsets) {
|
|
229 |
final Node element = value[postset];
|
|
230 |
|
|
231 |
if (element != null) {
|
|
232 |
element.accept(this);
|
|
233 |
}
|
|
234 |
}
|
|
235 |
}
|
|
236 |
|
|
237 |
return null;
|
|
238 |
}
|
|
239 |
|
|
240 |
return literalNode;
|
|
241 |
}
|
|
242 |
|
|
243 |
@Override
|
|
244 |
public Node leave(final PropertyNode propertyNode) {
|
|
245 |
weight += LITERAL_WEIGHT;
|
|
246 |
return propertyNode;
|
|
247 |
}
|
|
248 |
|
|
249 |
@Override
|
|
250 |
public Node leave(final ReferenceNode referenceNode) {
|
|
251 |
weight += REFERENCE_WEIGHT;
|
|
252 |
return referenceNode;
|
|
253 |
}
|
|
254 |
|
|
255 |
@Override
|
|
256 |
public Node leave(final ReturnNode returnNode) {
|
|
257 |
weight += RETURN_WEIGHT;
|
|
258 |
return returnNode;
|
|
259 |
}
|
|
260 |
|
|
261 |
@Override
|
|
262 |
public Node leave(final RuntimeNode runtimeNode) {
|
|
263 |
weight += CALL_WEIGHT;
|
|
264 |
return runtimeNode;
|
|
265 |
}
|
|
266 |
|
|
267 |
@Override
|
|
268 |
public Node enter(final SplitNode splitNode) {
|
|
269 |
weight += SPLIT_WEIGHT;
|
|
270 |
return null;
|
|
271 |
}
|
|
272 |
|
|
273 |
@Override
|
|
274 |
public Node leave(final SwitchNode switchNode) {
|
|
275 |
weight += SWITCH_WEIGHT;
|
|
276 |
return switchNode;
|
|
277 |
}
|
|
278 |
|
|
279 |
@Override
|
|
280 |
public Node leave(final ThrowNode throwNode) {
|
|
281 |
weight += THROW_WEIGHT;
|
|
282 |
return throwNode;
|
|
283 |
}
|
|
284 |
|
|
285 |
@Override
|
|
286 |
public Node leave(final TryNode tryNode) {
|
|
287 |
weight += THROW_WEIGHT;
|
|
288 |
return tryNode;
|
|
289 |
}
|
|
290 |
|
|
291 |
@Override
|
|
292 |
public Node leave(final VarNode varNode) {
|
|
293 |
weight += VAR_WEIGHT;
|
|
294 |
return varNode;
|
|
295 |
}
|
|
296 |
|
|
297 |
@Override
|
|
298 |
public Node leave(final WhileNode whileNode) {
|
|
299 |
weight += LOOP_WEIGHT;
|
|
300 |
return whileNode;
|
|
301 |
}
|
|
302 |
|
|
303 |
@Override
|
|
304 |
public Node leave(final WithNode withNode) {
|
|
305 |
weight += WITH_WEIGHT;
|
|
306 |
return withNode;
|
|
307 |
}
|
|
308 |
}
|