LUCENE-5207: Simplify the comparisons

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/branches/lucene5207@1523375 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Uwe Schindler 2013-09-15 00:48:44 +00:00
parent 0b07ec8d0c
commit f2787a0603
1 changed files with 26 additions and 90 deletions

View File

@ -21,18 +21,11 @@ import static org.objectweb.asm.Opcodes.ACC_PUBLIC;
import static org.objectweb.asm.Opcodes.ACC_SUPER; import static org.objectweb.asm.Opcodes.ACC_SUPER;
import static org.objectweb.asm.Opcodes.ACC_SYNTHETIC; import static org.objectweb.asm.Opcodes.ACC_SYNTHETIC;
import static org.objectweb.asm.Opcodes.DADD; import static org.objectweb.asm.Opcodes.DADD;
import static org.objectweb.asm.Opcodes.DCMPG;
import static org.objectweb.asm.Opcodes.DCMPL;
import static org.objectweb.asm.Opcodes.DDIV; import static org.objectweb.asm.Opcodes.DDIV;
import static org.objectweb.asm.Opcodes.DNEG; import static org.objectweb.asm.Opcodes.DNEG;
import static org.objectweb.asm.Opcodes.DREM; import static org.objectweb.asm.Opcodes.DREM;
import static org.objectweb.asm.Opcodes.DSUB; import static org.objectweb.asm.Opcodes.DSUB;
import static org.objectweb.asm.Opcodes.GOTO;
import static org.objectweb.asm.Opcodes.IFEQ; import static org.objectweb.asm.Opcodes.IFEQ;
import static org.objectweb.asm.Opcodes.IFGE;
import static org.objectweb.asm.Opcodes.IFGT;
import static org.objectweb.asm.Opcodes.IFLE;
import static org.objectweb.asm.Opcodes.IFLT;
import static org.objectweb.asm.Opcodes.IFNE; import static org.objectweb.asm.Opcodes.IFNE;
import static org.objectweb.asm.Opcodes.LAND; import static org.objectweb.asm.Opcodes.LAND;
import static org.objectweb.asm.Opcodes.LOR; import static org.objectweb.asm.Opcodes.LOR;
@ -274,7 +267,6 @@ public class JavascriptCompiler {
gen.arrayLoad(FUNCTION_VALUES_TYPE); gen.arrayLoad(FUNCTION_VALUES_TYPE);
gen.loadArg(0); gen.loadArg(0);
gen.invokeVirtual(FUNCTION_VALUES_TYPE, DOUBLE_VAL_METHOD); gen.invokeVirtual(FUNCTION_VALUES_TYPE, DOUBLE_VAL_METHOD);
gen.cast(Type.DOUBLE_TYPE, expected); gen.cast(Type.DOUBLE_TYPE, expected);
break; break;
case JavascriptParser.HEX: case JavascriptParser.HEX:
@ -360,98 +352,27 @@ public class JavascriptCompiler {
break; break;
case JavascriptParser.AT_BIT_NOT: case JavascriptParser.AT_BIT_NOT:
recursiveCompile(current.getChild(0), Type.LONG_TYPE); recursiveCompile(current.getChild(0), Type.LONG_TYPE);
gen.visitLdcInsn(new Long(-1)); gen.push(-1L);
gen.visitInsn(LXOR); gen.visitInsn(LXOR);
gen.cast(Type.LONG_TYPE, expected); gen.cast(Type.LONG_TYPE, expected);
break; break;
case JavascriptParser.AT_COMP_EQ: case JavascriptParser.AT_COMP_EQ:
Label labelEqTrue = new Label(); compileCompare(GeneratorAdapter.EQ, current, expected);
Label labelEqReturn = new Label();
recursiveCompile(current.getChild(0), Type.DOUBLE_TYPE);
recursiveCompile(current.getChild(1), Type.DOUBLE_TYPE);
gen.visitInsn(DCMPL);
gen.visitJumpInsn(IFEQ, labelEqTrue);
pushBoolean(expected, false);
gen.visitJumpInsn(GOTO, labelEqReturn);
gen.visitLabel(labelEqTrue);
pushBoolean(expected, true);
gen.visitLabel(labelEqReturn);
break; break;
case JavascriptParser.AT_COMP_NEQ: case JavascriptParser.AT_COMP_NEQ:
Label labelNeqTrue = new Label(); compileCompare(GeneratorAdapter.NE, current, expected);
Label labelNeqReturn = new Label();
recursiveCompile(current.getChild(0), Type.DOUBLE_TYPE);
recursiveCompile(current.getChild(1), Type.DOUBLE_TYPE);
gen.visitInsn(DCMPL);
gen.visitJumpInsn(IFNE, labelNeqTrue);
pushBoolean(expected, false);
gen.visitJumpInsn(GOTO, labelNeqReturn);
gen.visitLabel(labelNeqTrue);
pushBoolean(expected, true);
gen.visitLabel(labelNeqReturn);
break; break;
case JavascriptParser.AT_COMP_LT: case JavascriptParser.AT_COMP_LT:
Label labelLtTrue = new Label(); compileCompare(GeneratorAdapter.LT, current, expected);
Label labelLtReturn = new Label();
recursiveCompile(current.getChild(0), Type.DOUBLE_TYPE);
recursiveCompile(current.getChild(1), Type.DOUBLE_TYPE);
gen.visitInsn(DCMPG);
gen.visitJumpInsn(IFLT, labelLtTrue);
pushBoolean(expected, false);
gen.visitJumpInsn(GOTO, labelLtReturn);
gen.visitLabel(labelLtTrue);
pushBoolean(expected, true);
gen.visitLabel(labelLtReturn);
break; break;
case JavascriptParser.AT_COMP_GT: case JavascriptParser.AT_COMP_GT:
Label labelGtTrue = new Label(); compileCompare(GeneratorAdapter.GT, current, expected);
Label labelGtReturn = new Label();
recursiveCompile(current.getChild(0), Type.DOUBLE_TYPE);
recursiveCompile(current.getChild(1), Type.DOUBLE_TYPE);
gen.visitInsn(DCMPL);
gen.visitJumpInsn(IFGT, labelGtTrue);
pushBoolean(expected, false);
gen.visitJumpInsn(GOTO, labelGtReturn);
gen.visitLabel(labelGtTrue);
pushBoolean(expected, true);
gen.visitLabel(labelGtReturn);
break; break;
case JavascriptParser.AT_COMP_LTE: case JavascriptParser.AT_COMP_LTE:
Label labelLteTrue = new Label(); compileCompare(GeneratorAdapter.LE, current, expected);
Label labelLteReturn = new Label();
recursiveCompile(current.getChild(0), Type.DOUBLE_TYPE);
recursiveCompile(current.getChild(1), Type.DOUBLE_TYPE);
gen.visitInsn(DCMPG);
gen.visitJumpInsn(IFLE, labelLteTrue);
pushBoolean(expected, false);
gen.visitJumpInsn(GOTO, labelLteReturn);
gen.visitLabel(labelLteTrue);
pushBoolean(expected, true);
gen.visitLabel(labelLteReturn);
break; break;
case JavascriptParser.AT_COMP_GTE: case JavascriptParser.AT_COMP_GTE:
Label labelGteTrue = new Label(); compileCompare(GeneratorAdapter.GE, current, expected);
Label labelGteReturn = new Label();
recursiveCompile(current.getChild(0), Type.DOUBLE_TYPE);
recursiveCompile(current.getChild(1), Type.DOUBLE_TYPE);
gen.visitInsn(DCMPL);
gen.visitJumpInsn(IFGE, labelGteTrue);
pushBoolean(expected, false);
gen.visitJumpInsn(GOTO, labelGteReturn);
gen.visitLabel(labelGteTrue);
pushBoolean(expected, true);
gen.visitLabel(labelGteReturn);
break; break;
case JavascriptParser.AT_BOOL_NOT: case JavascriptParser.AT_BOOL_NOT:
Label labelNotTrue = new Label(); Label labelNotTrue = new Label();
@ -460,7 +381,7 @@ public class JavascriptCompiler {
recursiveCompile(current.getChild(0), Type.INT_TYPE); recursiveCompile(current.getChild(0), Type.INT_TYPE);
gen.visitJumpInsn(IFEQ, labelNotTrue); gen.visitJumpInsn(IFEQ, labelNotTrue);
pushBoolean(expected, false); pushBoolean(expected, false);
gen.visitJumpInsn(GOTO, labelNotReturn); gen.goTo(labelNotReturn);
gen.visitLabel(labelNotTrue); gen.visitLabel(labelNotTrue);
pushBoolean(expected, true); pushBoolean(expected, true);
gen.visitLabel(labelNotReturn); gen.visitLabel(labelNotReturn);
@ -474,7 +395,7 @@ public class JavascriptCompiler {
recursiveCompile(current.getChild(1), Type.INT_TYPE); recursiveCompile(current.getChild(1), Type.INT_TYPE);
gen.visitJumpInsn(IFEQ, andFalse); gen.visitJumpInsn(IFEQ, andFalse);
pushBoolean(expected, true); pushBoolean(expected, true);
gen.visitJumpInsn(GOTO, andEnd); gen.goTo(andEnd);
gen.visitLabel(andFalse); gen.visitLabel(andFalse);
pushBoolean(expected, false); pushBoolean(expected, false);
gen.visitLabel(andEnd); gen.visitLabel(andEnd);
@ -488,7 +409,7 @@ public class JavascriptCompiler {
recursiveCompile(current.getChild(1), Type.INT_TYPE); recursiveCompile(current.getChild(1), Type.INT_TYPE);
gen.visitJumpInsn(IFNE, orTrue); gen.visitJumpInsn(IFNE, orTrue);
pushBoolean(expected, false); pushBoolean(expected, false);
gen.visitJumpInsn(GOTO, orEnd); gen.goTo(orEnd);
gen.visitLabel(orTrue); gen.visitLabel(orTrue);
pushBoolean(expected, true); pushBoolean(expected, true);
gen.visitLabel(orEnd); gen.visitLabel(orEnd);
@ -500,7 +421,7 @@ public class JavascriptCompiler {
recursiveCompile(current.getChild(0), Type.INT_TYPE); recursiveCompile(current.getChild(0), Type.INT_TYPE);
gen.visitJumpInsn(IFEQ, condFalse); gen.visitJumpInsn(IFEQ, condFalse);
recursiveCompile(current.getChild(1), expected); recursiveCompile(current.getChild(1), expected);
gen.visitJumpInsn(GOTO, condEnd); gen.goTo(condEnd);
gen.visitLabel(condFalse); gen.visitLabel(condFalse);
recursiveCompile(current.getChild(2), expected); recursiveCompile(current.getChild(2), expected);
gen.visitLabel(condEnd); gen.visitLabel(condEnd);
@ -510,6 +431,21 @@ public class JavascriptCompiler {
} }
} }
private void compileCompare(int operator, Tree current, Type expected) {
Label labelTrue = new Label();
Label labelReturn = new Label();
recursiveCompile(current.getChild(0), Type.DOUBLE_TYPE);
recursiveCompile(current.getChild(1), Type.DOUBLE_TYPE);
gen.ifCmp(Type.DOUBLE_TYPE, operator, labelTrue);
pushBoolean(expected, false);
gen.goTo(labelReturn);
gen.visitLabel(labelTrue);
pushBoolean(expected, true);
gen.visitLabel(labelReturn);
}
private void pushBoolean(Type expected, boolean truth) { private void pushBoolean(Type expected, boolean truth) {
switch (expected.getSort()) { switch (expected.getSort()) {
case Type.INT: case Type.INT: