Wed, 30 Jan 2013 12:26:45 +0100
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
Summary: Lower suffered from being a "God class" trying to do everything at once. As Nashorn code generation has grown, so has Lower. It does several post processing passes, tries to do several things at once even though all type information isn't in place, adjusting state afterwards and so on. It also performs control flow analysis, type attribution and constant folding, and everything else code generation related before byte code emission. I have now separated the compilation process into Lower (create low level nodes from high level ones, copy code such as finally block inlining etc), Attr (assign types and symbols to all nodes - freeze slot and scope information) and FinalizeTypes (insert explicit casts, specialize invoke dynamic types for scope accesses). I've removed the kludgy AccessSpecializer, as this now integrates naturally with typing. Everything is now much easier to read and each module performs only one thing. I have added separate loggers for the separate tiers. In the process I have also fixed: (1) problems with type coercion (see test/script/basic/typecoercion.js, basically our coercion was too late and our symbol inference was erroneous. This only manifested itself in very rare occasions where toNumber coercion has side effects, such as for example when valueOf is overridden) (2) copying literal nodes (literal copy did not use the superclass copy, which made all the Node specific fields not to be copied (3) erroneous literal tokenization (literals shouldn't always just inherit token information from whatever node that creates them) (4) splitter weighnodes - unary nodes were considered weightless (4) removed the hateful and kludgy "VarNode.shouldAppend", which really isn't needed when we have an attribution phase that determines self reference symbols (the only thing it was used for) (5) duplicate line number issues in the parser (6) convert bug in CodeGenerator for intermediate results of scope accesses (see test/script/basic/access-specializer.js) ... Several of these things just stopped being problems with the new architecture "can't happen anymore" and are not bug fixes per se. All tests run. No performance regressions exist that I've been able to measure. Some increases in performance were measured, but in the statistical margin of error (which is very wide as HotSpot currently has warmup issues with LambdaForms/invoke dynamic). Compile speed has not measurably increased.
Reviewed-by: jlaskey, attila
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 */
26 package jdk.nashorn.internal.ir;
28 import jdk.nashorn.internal.ir.visitor.NodeVisitor;
29 import jdk.nashorn.internal.runtime.Source;
31 /**
32 * IR representation of a catch clause.
33 *
34 */
35 public class CatchNode extends Node {
36 /** Exception identifier. */
37 private IdentNode exception;
39 /** Exception condition. */
40 private Node exceptionCondition;
42 /** Catch body. */
43 private Block body;
45 /** Is rethrow - e.g. synthetic catch block for e.g. finallies, the parser case where
46 * there has to be at least on catch for syntactic validity */
47 private boolean isSyntheticRethrow;
49 /**
50 * Constructors
51 *
52 * @param source the source
53 * @param token token
54 * @param finish finish
55 * @param exception variable name of exception
56 * @param exceptionCondition exception condition
57 * @param body catch body
58 */
59 public CatchNode(final Source source, final long token, final int finish, final IdentNode exception, final Node exceptionCondition, final Block body) {
60 super(source, token, finish);
62 this.exception = exception;
63 this.exceptionCondition = exceptionCondition;
64 this.body = body;
65 }
67 private CatchNode(final CatchNode catchNode, final CopyState cs) {
68 super(catchNode);
70 exception = (IdentNode)cs.existingOrCopy(catchNode.exception);
71 exceptionCondition = cs.existingOrCopy(catchNode.exceptionCondition);
72 body = (Block)cs.existingOrCopy(catchNode.body);
73 isSyntheticRethrow = catchNode.isSyntheticRethrow;
74 }
76 @Override
77 protected Node copy(final CopyState cs) {
78 return new CatchNode(this, cs);
79 }
81 /**
82 * Assist in IR navigation.
83 * @param visitor IR navigating visitor.
84 */
85 @Override
86 public Node accept(final NodeVisitor visitor) {
87 if (visitor.enter(this) != null) {
88 exception = (IdentNode)exception.accept(visitor);
90 if (exceptionCondition != null) {
91 exceptionCondition = exceptionCondition.accept(visitor);
92 }
94 body = (Block)body.accept(visitor);
95 return visitor.leave(this);
96 }
98 return this;
99 }
101 @Override
102 public void toString(final StringBuilder sb) {
103 sb.append(" catch (");
104 exception.toString(sb);
106 if (exceptionCondition != null) {
107 sb.append(" if ");
108 exceptionCondition.toString(sb);
109 }
110 sb.append(')');
111 }
113 /**
114 * Check if this catch is a synthetic rethrow
115 * @return true if this is a synthetic rethrow
116 */
117 public boolean isSyntheticRethrow() {
118 return isSyntheticRethrow;
119 }
121 /**
122 * Flag this as deliberatly generated catch all that rethrows the
123 * caught exception. This is used for example for generating finally
124 * expressions
125 */
126 public void setIsSyntheticRethrow() {
127 this.isSyntheticRethrow = true;
128 }
130 /**
131 * Get the identifier representing the exception thrown
132 * @return the exception identifier
133 */
134 public IdentNode getException() {
135 return exception;
136 }
138 /**
139 * Get the exception condition for this catch block
140 * @return the exception condition
141 */
142 public Node getExceptionCondition() {
143 return exceptionCondition;
144 }
146 /**
147 * Reset the exception condition for this catch block
148 * @param exceptionCondition the new exception condition
149 */
150 public void setExceptionCondition(final Node exceptionCondition) {
151 this.exceptionCondition = exceptionCondition;
152 }
154 /**
155 * Get the body for this catch block
156 * @return the catch block body
157 */
158 public Block getBody() {
159 return body;
160 }
161 }