author | lagergren |
Fri, 16 Aug 2013 18:51:53 +0200 | |
changeset 19472 | 9476460521b3 |
parent 18867 | bc91e3fcc5ba |
child 21457 | 381acbd07fe5 |
permissions | -rw-r--r-- |
16147 | 1 |
/* |
16151 | 2 |
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
16147 | 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.ir; |
|
27 |
||
28 |
import jdk.nashorn.internal.codegen.types.Type; |
|
17233 | 29 |
import jdk.nashorn.internal.ir.annotations.Immutable; |
16147 | 30 |
import jdk.nashorn.internal.ir.visitor.NodeVisitor; |
31 |
import jdk.nashorn.internal.parser.TokenType; |
|
32 |
||
33 |
/** |
|
34 |
* BinaryNode nodes represent two operand operations. |
|
35 |
*/ |
|
17233 | 36 |
@Immutable |
18867
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
37 |
public final class BinaryNode extends Expression implements Assignment<Expression> { |
16147 | 38 |
/** Left hand side argument. */ |
18867
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
39 |
private final Expression lhs; |
17233 | 40 |
|
18867
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
41 |
private final Expression rhs; |
16147 | 42 |
|
43 |
/** |
|
44 |
* Constructor |
|
45 |
* |
|
46 |
* @param token token |
|
47 |
* @param lhs left hand side |
|
48 |
* @param rhs right hand side |
|
49 |
*/ |
|
18867
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
50 |
public BinaryNode(final long token, final Expression lhs, final Expression rhs) { |
17523
cb4a7c901e0d
8013913: Removed Source field from all nodes except FunctionNode in order to save footprint
lagergren
parents:
17233
diff
changeset
|
51 |
super(token, lhs.getStart(), rhs.getFinish()); |
17233 | 52 |
this.lhs = lhs; |
53 |
this.rhs = rhs; |
|
16147 | 54 |
} |
55 |
||
18867
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
56 |
private BinaryNode(final BinaryNode binaryNode, final Expression lhs, final Expression rhs) { |
17233 | 57 |
super(binaryNode); |
58 |
this.lhs = lhs; |
|
59 |
this.rhs = rhs; |
|
16147 | 60 |
} |
61 |
||
17756
daaa1e643f71
8006069: Range analysis first iteration, runtime specializations
lagergren
parents:
17523
diff
changeset
|
62 |
@Override |
daaa1e643f71
8006069: Range analysis first iteration, runtime specializations
lagergren
parents:
17523
diff
changeset
|
63 |
public boolean isComparison() { |
daaa1e643f71
8006069: Range analysis first iteration, runtime specializations
lagergren
parents:
17523
diff
changeset
|
64 |
switch (tokenType()) { |
daaa1e643f71
8006069: Range analysis first iteration, runtime specializations
lagergren
parents:
17523
diff
changeset
|
65 |
case EQ: |
daaa1e643f71
8006069: Range analysis first iteration, runtime specializations
lagergren
parents:
17523
diff
changeset
|
66 |
case EQ_STRICT: |
daaa1e643f71
8006069: Range analysis first iteration, runtime specializations
lagergren
parents:
17523
diff
changeset
|
67 |
case NE: |
daaa1e643f71
8006069: Range analysis first iteration, runtime specializations
lagergren
parents:
17523
diff
changeset
|
68 |
case NE_STRICT: |
daaa1e643f71
8006069: Range analysis first iteration, runtime specializations
lagergren
parents:
17523
diff
changeset
|
69 |
case LE: |
daaa1e643f71
8006069: Range analysis first iteration, runtime specializations
lagergren
parents:
17523
diff
changeset
|
70 |
case LT: |
daaa1e643f71
8006069: Range analysis first iteration, runtime specializations
lagergren
parents:
17523
diff
changeset
|
71 |
case GE: |
daaa1e643f71
8006069: Range analysis first iteration, runtime specializations
lagergren
parents:
17523
diff
changeset
|
72 |
case GT: |
daaa1e643f71
8006069: Range analysis first iteration, runtime specializations
lagergren
parents:
17523
diff
changeset
|
73 |
return true; |
daaa1e643f71
8006069: Range analysis first iteration, runtime specializations
lagergren
parents:
17523
diff
changeset
|
74 |
default: |
daaa1e643f71
8006069: Range analysis first iteration, runtime specializations
lagergren
parents:
17523
diff
changeset
|
75 |
return false; |
daaa1e643f71
8006069: Range analysis first iteration, runtime specializations
lagergren
parents:
17523
diff
changeset
|
76 |
} |
daaa1e643f71
8006069: Range analysis first iteration, runtime specializations
lagergren
parents:
17523
diff
changeset
|
77 |
} |
daaa1e643f71
8006069: Range analysis first iteration, runtime specializations
lagergren
parents:
17523
diff
changeset
|
78 |
|
16147 | 79 |
/** |
80 |
* Return the widest possible type for this operation. This is used for compile time |
|
81 |
* static type inference |
|
82 |
* |
|
83 |
* @return Type |
|
84 |
*/ |
|
85 |
@Override |
|
86 |
public Type getWidestOperationType() { |
|
87 |
switch (tokenType()) { |
|
88 |
case SHR: |
|
89 |
case ASSIGN_SHR: |
|
90 |
return Type.LONG; |
|
91 |
case ASSIGN_SAR: |
|
92 |
case ASSIGN_SHL: |
|
93 |
case ASSIGN_BIT_AND: |
|
94 |
case ASSIGN_BIT_OR: |
|
95 |
case ASSIGN_BIT_XOR: |
|
96 |
case SAR: |
|
97 |
case SHL: |
|
98 |
return Type.INT; |
|
99 |
case DIV: |
|
100 |
case MOD: |
|
101 |
case MUL: |
|
19472
9476460521b3
8023017: SUB missing for widest op == number for BinaryNode
lagergren
parents:
18867
diff
changeset
|
102 |
case SUB: |
16147 | 103 |
case ASSIGN_DIV: |
104 |
case ASSIGN_MOD: |
|
105 |
case ASSIGN_MUL: |
|
106 |
case ASSIGN_SUB: |
|
107 |
return Type.NUMBER; |
|
108 |
default: |
|
109 |
return Type.OBJECT; |
|
110 |
} |
|
111 |
} |
|
112 |
||
113 |
/** |
|
114 |
* Check if this node is an assigment |
|
115 |
* |
|
116 |
* @return true if this node assigns a value |
|
117 |
*/ |
|
118 |
@Override |
|
119 |
public boolean isAssignment() { |
|
120 |
switch (tokenType()) { |
|
121 |
case ASSIGN: |
|
122 |
case ASSIGN_ADD: |
|
123 |
case ASSIGN_BIT_AND: |
|
124 |
case ASSIGN_BIT_OR: |
|
125 |
case ASSIGN_BIT_XOR: |
|
126 |
case ASSIGN_DIV: |
|
127 |
case ASSIGN_MOD: |
|
128 |
case ASSIGN_MUL: |
|
129 |
case ASSIGN_SAR: |
|
130 |
case ASSIGN_SHL: |
|
131 |
case ASSIGN_SHR: |
|
132 |
case ASSIGN_SUB: |
|
133 |
return true; |
|
134 |
default: |
|
135 |
return false; |
|
136 |
} |
|
137 |
} |
|
138 |
||
139 |
@Override |
|
140 |
public boolean isSelfModifying() { |
|
141 |
return isAssignment() && tokenType() != TokenType.ASSIGN; |
|
142 |
} |
|
143 |
||
144 |
@Override |
|
18867
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
145 |
public Expression getAssignmentDest() { |
16147 | 146 |
return isAssignment() ? lhs() : null; |
147 |
} |
|
148 |
||
149 |
@Override |
|
18867
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
150 |
public BinaryNode setAssignmentDest(Expression n) { |
16530
201d682e75f4
8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
16269
diff
changeset
|
151 |
return setLHS(n); |
201d682e75f4
8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
16269
diff
changeset
|
152 |
} |
201d682e75f4
8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
16269
diff
changeset
|
153 |
|
201d682e75f4
8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
16269
diff
changeset
|
154 |
@Override |
18867
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
155 |
public Expression getAssignmentSource() { |
16147 | 156 |
return rhs(); |
157 |
} |
|
158 |
||
159 |
/** |
|
160 |
* Assist in IR navigation. |
|
161 |
* @param visitor IR navigating visitor. |
|
162 |
*/ |
|
163 |
@Override |
|
17769
14ea7feaf658
8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
17756
diff
changeset
|
164 |
public Node accept(final NodeVisitor<? extends LexicalContext> visitor) { |
17233 | 165 |
if (visitor.enterBinaryNode(this)) { |
18867
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
166 |
return visitor.leaveBinaryNode(setLHS((Expression)lhs.accept(visitor)).setRHS((Expression)rhs.accept(visitor))); |
16147 | 167 |
} |
168 |
||
169 |
return this; |
|
170 |
} |
|
171 |
||
172 |
@Override |
|
173 |
public void toString(final StringBuilder sb) { |
|
174 |
final TokenType type = tokenType(); |
|
175 |
||
176 |
final boolean lhsParen = type.needsParens(lhs().tokenType(), true); |
|
177 |
final boolean rhsParen = type.needsParens(rhs().tokenType(), false); |
|
178 |
||
179 |
if (lhsParen) { |
|
180 |
sb.append('('); |
|
181 |
} |
|
182 |
||
183 |
lhs().toString(sb); |
|
184 |
||
185 |
if (lhsParen) { |
|
186 |
sb.append(')'); |
|
187 |
} |
|
188 |
||
189 |
sb.append(' '); |
|
190 |
||
191 |
switch (type) { |
|
192 |
case COMMALEFT: |
|
193 |
sb.append(",<"); |
|
194 |
break; |
|
195 |
case COMMARIGHT: |
|
196 |
sb.append(",>"); |
|
197 |
break; |
|
198 |
case INCPREFIX: |
|
199 |
case DECPREFIX: |
|
200 |
sb.append("++"); |
|
201 |
break; |
|
202 |
default: |
|
203 |
sb.append(type.getName()); |
|
204 |
break; |
|
205 |
} |
|
206 |
||
207 |
sb.append(' '); |
|
208 |
||
209 |
if (rhsParen) { |
|
210 |
sb.append('('); |
|
211 |
} |
|
212 |
rhs().toString(sb); |
|
213 |
if (rhsParen) { |
|
214 |
sb.append(')'); |
|
215 |
} |
|
216 |
} |
|
217 |
||
218 |
/** |
|
219 |
* Get the left hand side expression for this node |
|
220 |
* @return the left hand side expression |
|
221 |
*/ |
|
18867
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
222 |
public Expression lhs() { |
16147 | 223 |
return lhs; |
224 |
} |
|
225 |
||
226 |
/** |
|
17233 | 227 |
* Get the right hand side expression for this node |
228 |
* @return the left hand side expression |
|
229 |
*/ |
|
18867
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
230 |
public Expression rhs() { |
17233 | 231 |
return rhs; |
232 |
} |
|
233 |
||
234 |
/** |
|
16147 | 235 |
* Set the left hand side expression for this node |
236 |
* @param lhs new left hand side expression |
|
16530
201d682e75f4
8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
16269
diff
changeset
|
237 |
* @return a node equivalent to this one except for the requested change. |
16147 | 238 |
*/ |
18867
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
239 |
public BinaryNode setLHS(final Expression lhs) { |
17233 | 240 |
if (this.lhs == lhs) { |
241 |
return this; |
|
242 |
} |
|
243 |
return new BinaryNode(this, lhs, rhs); |
|
16147 | 244 |
} |
17233 | 245 |
|
246 |
/** |
|
247 |
* Set the right hand side expression for this node |
|
248 |
* @param rhs new left hand side expression |
|
249 |
* @return a node equivalent to this one except for the requested change. |
|
250 |
*/ |
|
18867
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
251 |
public BinaryNode setRHS(final Expression rhs) { |
17233 | 252 |
if (this.rhs == rhs) { |
253 |
return this; |
|
254 |
} |
|
255 |
return new BinaryNode(this, lhs, rhs); |
|
256 |
} |
|
257 |
||
16147 | 258 |
} |