Skip to content

Commit

Permalink
* High degree of code duplication in instanceof pattern code generation
Browse files Browse the repository at this point in the history
* Fixes #2021
  • Loading branch information
srikanth-sankaran committed Feb 14, 2024
1 parent 4398353 commit 50692f9
Showing 1 changed file with 22 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,54 +104,30 @@ public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, Fl
@Override
public void generateCode(BlockScope currentScope, CodeStream codeStream, boolean valueRequired) {

int pc = codeStream.position;
BranchLabel falseLabel = new BranchLabel(codeStream);
BranchLabel continueLabel = new BranchLabel(codeStream);

this.expression.generateCode(currentScope, codeStream, true);
if (this.secretExpressionValue != null) {
codeStream.store(this.secretExpressionValue, true);
codeStream.addVariable(this.secretExpressionValue);
}
codeStream.instance_of(this.type, this.type.resolvedType);
if (this.pattern != null) {
BranchLabel falseLabel = new BranchLabel(codeStream);
BranchLabel trueLabel = new BranchLabel(codeStream);
BranchLabel continueLabel = new BranchLabel(codeStream);
codeStream.ifeq(falseLabel);
generateOptimizedBoolean(currentScope, codeStream, null, falseLabel, valueRequired);

if (this.secretExpressionValue != null) {
codeStream.load(this.secretExpressionValue);
codeStream.removeVariable(this.secretExpressionValue);
} else {
this.expression.generateCode(currentScope, codeStream, true);
}
this.pattern.generateOptimizedBoolean(currentScope, codeStream, trueLabel, falseLabel);

trueLabel.place();
if (valueRequired) {
codeStream.iconst_1();
codeStream.goto_(continueLabel);
falseLabel.place();
}
falseLabel.place();
if (this.pattern != null) {
for (LocalVariableBinding binding : this.pattern.bindingsWhenTrue()) {
binding.recordInitializationEndPC(codeStream.position);
}
}
if (valueRequired)
codeStream.iconst_0();
continueLabel.place();

}
if (valueRequired) {
codeStream.generateImplicitConversion(this.implicitConversion);
} else {
codeStream.pop();
}
codeStream.recordPositionsFrom(pc, this.sourceStart);
continueLabel.place();
codeStream.recordPositionsFrom(codeStream.position, this.sourceEnd);
}

@Override
public void generateOptimizedBoolean(BlockScope currentScope, CodeStream codeStream, BranchLabel trueLabel, BranchLabel falseLabel, boolean valueRequired) {
// a label valued to nil means: by default we fall through the case...
// both nil means we leave the value on the stack
if (this.elementVariable == null && this.pattern == null) {
super.generateOptimizedBoolean(currentScope, codeStream, trueLabel, falseLabel, valueRequired);
return;
}

int pc = codeStream.position;

Expand All @@ -164,20 +140,20 @@ public void generateOptimizedBoolean(BlockScope currentScope, CodeStream codeStr
BranchLabel nextSibling = falseLabel != null ? falseLabel : new BranchLabel(codeStream);
codeStream.instance_of(this.type, this.type.resolvedType);
codeStream.ifeq(nextSibling);
if (this.secretExpressionValue != null) {
codeStream.load(this.secretExpressionValue);
codeStream.removeVariable(this.secretExpressionValue);
} else {
this.expression.generateCode(currentScope, codeStream, true);
if (this.pattern != null) {
if (this.secretExpressionValue != null) {
codeStream.load(this.secretExpressionValue);
codeStream.removeVariable(this.secretExpressionValue);
} else {
this.expression.generateCode(currentScope, codeStream, true);
}
this.pattern.generateOptimizedBoolean(currentScope, codeStream, trueLabel, nextSibling);
}

this.pattern.generateOptimizedBoolean(currentScope, codeStream, trueLabel, nextSibling);

if (valueRequired) {
codeStream.generateImplicitConversion(this.implicitConversion);
} else {
if (!valueRequired) {
codeStream.pop();
}

codeStream.recordPositionsFrom(pc, this.sourceStart);

int position = codeStream.position;
Expand Down

0 comments on commit 50692f9

Please sign in to comment.