Lines 73-79
Link Here
|
73 |
* Code generation for a binary operation |
73 |
* Code generation for a binary operation |
74 |
*/ |
74 |
*/ |
75 |
public void generateCode(BlockScope currentScope, CodeStream codeStream, boolean valueRequired) { |
75 |
public void generateCode(BlockScope currentScope, CodeStream codeStream, boolean valueRequired) { |
76 |
|
|
|
77 |
int pc = codeStream.position; |
76 |
int pc = codeStream.position; |
78 |
if (constant != Constant.NotAConstant) { |
77 |
if (constant != Constant.NotAConstant) { |
79 |
// inlined value |
78 |
// inlined value |
Lines 176-182
Link Here
|
176 |
* Boolean operator code generation Optimized operations are: || |
175 |
* Boolean operator code generation Optimized operations are: || |
177 |
*/ |
176 |
*/ |
178 |
public void generateOptimizedBoolean(BlockScope currentScope, CodeStream codeStream, BranchLabel trueLabel, BranchLabel falseLabel, boolean valueRequired) { |
177 |
public void generateOptimizedBoolean(BlockScope currentScope, CodeStream codeStream, BranchLabel trueLabel, BranchLabel falseLabel, boolean valueRequired) { |
179 |
|
|
|
180 |
if (constant != Constant.NotAConstant) { |
178 |
if (constant != Constant.NotAConstant) { |
181 |
super.generateOptimizedBoolean(currentScope, codeStream, trueLabel, falseLabel, valueRequired); |
179 |
super.generateOptimizedBoolean(currentScope, codeStream, trueLabel, falseLabel, valueRequired); |
182 |
return; |
180 |
return; |
Lines 209-225
Link Here
|
209 |
// implicit falling through the FALSE case |
207 |
// implicit falling through the FALSE case |
210 |
left.generateOptimizedBoolean(currentScope, codeStream, trueLabel, null, !leftIsConst); |
208 |
left.generateOptimizedBoolean(currentScope, codeStream, trueLabel, null, !leftIsConst); |
211 |
// need value, e.g. if (a == 1 || ((b = 2) > 0)) {} -> shouldn't initialize 'b' if a==1 |
209 |
// need value, e.g. if (a == 1 || ((b = 2) > 0)) {} -> shouldn't initialize 'b' if a==1 |
212 |
if (leftIsConst && leftIsTrue) { |
210 |
if (leftIsTrue) { |
213 |
codeStream.goto_(trueLabel); |
211 |
if (valueRequired) codeStream.goto_(trueLabel); |
214 |
codeStream.updateLastRecordedEndPC(currentScope, codeStream.position); |
212 |
codeStream.updateLastRecordedEndPC(currentScope, codeStream.position); |
215 |
break generateOperands; // no need to generate right operand |
213 |
break generateOperands; // no need to generate right operand |
216 |
} |
214 |
} |
217 |
if (rightInitStateIndex != -1) { |
215 |
if (rightInitStateIndex != -1) { |
218 |
codeStream |
216 |
codeStream.addDefinitelyAssignedVariables(currentScope, rightInitStateIndex); |
219 |
.addDefinitelyAssignedVariables(currentScope, rightInitStateIndex); |
|
|
220 |
} |
217 |
} |
221 |
right.generateOptimizedBoolean(currentScope, codeStream, trueLabel, null, valueRequired && !rightIsConst); |
218 |
right.generateOptimizedBoolean(currentScope, codeStream, trueLabel, null, valueRequired && !rightIsConst); |
222 |
if (valueRequired && rightIsConst && rightIsTrue) { |
219 |
if (valueRequired && rightIsTrue) { |
223 |
codeStream.goto_(trueLabel); |
220 |
codeStream.goto_(trueLabel); |
224 |
codeStream.updateLastRecordedEndPC(currentScope, codeStream.position); |
221 |
codeStream.updateLastRecordedEndPC(currentScope, codeStream.position); |
225 |
} |
222 |
} |
Lines 230-236
Link Here
|
230 |
BranchLabel internalTrueLabel = new BranchLabel(codeStream); |
227 |
BranchLabel internalTrueLabel = new BranchLabel(codeStream); |
231 |
left.generateOptimizedBoolean(currentScope, codeStream, internalTrueLabel, null, !leftIsConst); |
228 |
left.generateOptimizedBoolean(currentScope, codeStream, internalTrueLabel, null, !leftIsConst); |
232 |
// need value, e.g. if (a == 1 || ((b = 2) > 0)) {} -> shouldn't initialize 'b' if a==1 |
229 |
// need value, e.g. if (a == 1 || ((b = 2) > 0)) {} -> shouldn't initialize 'b' if a==1 |
233 |
if (leftIsConst && leftIsTrue) { |
230 |
if (leftIsTrue) { |
234 |
internalTrueLabel.place(); |
231 |
internalTrueLabel.place(); |
235 |
break generateOperands; // no need to generate right operand |
232 |
break generateOperands; // no need to generate right operand |
236 |
} |
233 |
} |
Lines 239-249
Link Here
|
239 |
.addDefinitelyAssignedVariables(currentScope, rightInitStateIndex); |
236 |
.addDefinitelyAssignedVariables(currentScope, rightInitStateIndex); |
240 |
} |
237 |
} |
241 |
right.generateOptimizedBoolean(currentScope, codeStream, null, falseLabel, valueRequired && !rightIsConst); |
238 |
right.generateOptimizedBoolean(currentScope, codeStream, null, falseLabel, valueRequired && !rightIsConst); |
242 |
if (valueRequired && rightIsConst) { |
239 |
if (valueRequired && rightIsConst && !rightIsTrue) { |
243 |
if (!rightIsTrue) { |
240 |
codeStream.goto_(falseLabel); |
244 |
codeStream.goto_(falseLabel); |
241 |
codeStream.updateLastRecordedEndPC(currentScope, codeStream.position); |
245 |
codeStream.updateLastRecordedEndPC(currentScope, codeStream.position); |
|
|
246 |
} |
247 |
} |
242 |
} |
248 |
internalTrueLabel.place(); |
243 |
internalTrueLabel.place(); |
249 |
} else { |
244 |
} else { |