Use InvokeDynamic to optimize some math bytecode In generated code, use InvokeDynamic to dispatch to operations that will aggressively try integer-based operations for some math operations. Fall back to the old floating-point-based operation if the operands are not integers, and replace the optimized call path if the fall back happens repeatedly. Operations currently optimized are add, subtract, multiply, divide, mod, and, or, left-shift, and right-shift. This results in a measurable speedup for some of the V8 benchmarks, and doesn't seem to have a measurable impact on the others.
diff --git a/benchmarks/org/mozilla/javascript/benchmarks/V8Benchmark.java b/benchmarks/org/mozilla/javascript/benchmarks/V8Benchmark.java index fd48813..165889b 100644 --- a/benchmarks/org/mozilla/javascript/benchmarks/V8Benchmark.java +++ b/benchmarks/org/mozilla/javascript/benchmarks/V8Benchmark.java
@@ -9,6 +9,8 @@ import org.openjdk.jmh.annotations.*; public class V8Benchmark { + private static final int OPT_LEVEL = 9; + static Object[] emptyArgs = new Object[]{}; static abstract class AbstractState { @@ -39,7 +41,7 @@ void initialize() { cx = Context.enter(); cx.setLanguageVersion(Context.VERSION_ES6); - cx.setOptimizationLevel(9); + cx.setOptimizationLevel(OPT_LEVEL); scope = cx.initStandardObjects(); evaluateSource(cx, scope, "testsrc/benchmarks/framework.js"); }
diff --git a/src/org/mozilla/javascript/optimizer/BodyCodegen.java b/src/org/mozilla/javascript/optimizer/BodyCodegen.java index 76c4495..b9e941f 100644 --- a/src/org/mozilla/javascript/optimizer/BodyCodegen.java +++ b/src/org/mozilla/javascript/optimizer/BodyCodegen.java
@@ -1222,11 +1222,9 @@ break; default: cfw.addALoad(contextLocal); - addScriptRuntimeInvoke("add", - "(Ljava/lang/Object;" - +"Ljava/lang/Object;" - +"Lorg/mozilla/javascript/Context;" - +")Ljava/lang/Object;"); + cfw.addInvokeDynamic(DynamicMathRuntime.ADD_OP, + DynamicMathRuntime.METHOD_SIGNATURE, + DynamicMathRuntime.BOOTSTRAP_HANDLE); } } break; @@ -3421,19 +3419,31 @@ generateExpression(child, node); generateExpression(child.getNext(), node); cfw.add(opCode); - } - else { - boolean childOfArithmetic = isArithmeticNode(parent); - generateExpression(child, node); - if (!isArithmeticNode(child)) - addObjectToDouble(); - generateExpression(child.getNext(), node); - if (!isArithmeticNode(child.getNext())) - addObjectToDouble(); - cfw.add(opCode); - if (!childOfArithmetic) { - addDoubleWrap(); + + } else { + String bootstrapFunc; + switch (opCode) { + case ByteCode.DSUB: + bootstrapFunc = DynamicMathRuntime.SUBTRACT_OP; + break; + case ByteCode.DREM: + bootstrapFunc = DynamicMathRuntime.MOD_OP; + break; + case ByteCode.DDIV: + bootstrapFunc = DynamicMathRuntime.DIVIDE_OP; + break; + case ByteCode.DMUL: + bootstrapFunc = DynamicMathRuntime.MULTIPLY_OP; + break; + default: + throw Kit.codeBug("Invalid opcode"); } + generateExpression(child, node); + generateExpression(child.getNext(), node); + cfw.addALoad(contextLocal); + cfw.addInvokeDynamic(bootstrapFunc, + DynamicMathRuntime.METHOD_SIGNATURE, + DynamicMathRuntime.BOOTSTRAP_HANDLE); } } @@ -3480,38 +3490,58 @@ addDoubleWrap(); return; } - if (childNumberFlag == -1) { - addScriptRuntimeInvoke("toInt32", "(Ljava/lang/Object;)I"); - generateExpression(child.getNext(), node); - addScriptRuntimeInvoke("toInt32", "(Ljava/lang/Object;)I"); - } - else { + if (childNumberFlag != -1) { addScriptRuntimeInvoke("toInt32", "(D)I"); generateExpression(child.getNext(), node); addScriptRuntimeInvoke("toInt32", "(D)I"); - } - switch (type) { - case Token.BITOR: - cfw.add(ByteCode.IOR); - break; - case Token.BITXOR: - cfw.add(ByteCode.IXOR); - break; - case Token.BITAND: - cfw.add(ByteCode.IAND); - break; - case Token.RSH: - cfw.add(ByteCode.ISHR); - break; - case Token.LSH: - cfw.add(ByteCode.ISHL); - break; - default: - throw Codegen.badTree(); - } - cfw.add(ByteCode.I2D); - if (childNumberFlag == -1) { - addDoubleWrap(); + + switch (type) { + case Token.BITOR: + cfw.add(ByteCode.IOR); + break; + case Token.BITXOR: + cfw.add(ByteCode.IXOR); + break; + case Token.BITAND: + cfw.add(ByteCode.IAND); + break; + case Token.RSH: + cfw.add(ByteCode.ISHR); + break; + case Token.LSH: + cfw.add(ByteCode.ISHL); + break; + default: + throw Codegen.badTree(); + } + cfw.add(ByteCode.I2D); + + } else { + String bootstrapFunc; + switch (type) { + case Token.BITOR: + bootstrapFunc = DynamicMathRuntime.OR_OP; + break; + case Token.BITXOR: + bootstrapFunc = DynamicMathRuntime.XOR_OP; + break; + case Token.BITAND: + bootstrapFunc = DynamicMathRuntime.AND_OP; + break; + case Token.RSH: + bootstrapFunc = DynamicMathRuntime.RSH_OP; + break; + case Token.LSH: + bootstrapFunc = DynamicMathRuntime.LSH_OP; + break; + default: + throw Kit.codeBug("Invalid token type"); + } + generateExpression(child.getNext(), node); + cfw.addALoad(contextLocal); + cfw.addInvokeDynamic(bootstrapFunc, + DynamicMathRuntime.METHOD_SIGNATURE, + DynamicMathRuntime.BOOTSTRAP_HANDLE); } }
diff --git a/src/org/mozilla/javascript/optimizer/DynamicMathRuntime.java b/src/org/mozilla/javascript/optimizer/DynamicMathRuntime.java new file mode 100644 index 0000000..2b84b2e --- /dev/null +++ b/src/org/mozilla/javascript/optimizer/DynamicMathRuntime.java
@@ -0,0 +1,346 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +package org.mozilla.javascript.optimizer; + +import java.lang.invoke.CallSite; +import java.lang.invoke.ConstantCallSite; +import java.lang.invoke.MethodHandle; +import java.lang.invoke.MethodHandles; +import java.lang.invoke.MethodType; +import org.mozilla.classfile.ByteCode; +import org.mozilla.classfile.ClassFileWriter; +import org.mozilla.javascript.Context; +import org.mozilla.javascript.Kit; +import org.mozilla.javascript.ScriptRuntime; + +/** + * This class contains bootstrap methods for InvokeDynamic instructions for math operations. + * The operations try to aggressively optimize for integer operations by using the + * following algorithm: + * + * <ul> + * <li>Attempt the operation assuming that both operands are Integer objects</li> + * <li>Catch the ClassCastException that will result if this fails</li> + * <li>If the operation failed, retry using a more generic method, usually by + * converting both operands to double.</li> + * <li>After a number of failures, permanently switch to the fallback method + * and invoke it directly after that</li> + * </ul> + * + * Since this works using InvokeDynamic's CallSite mechanism, the algorithm above is + * executed for each place in the code. As a result, parts of scripts that usually see + * integer objects created will continue to use integer operations, with the ability to + * fall back to "correct" JavaScript operations at any point. + */ +public class DynamicMathRuntime { + + /** The signature of the "boostrap" method in this method */ + public static final String BOOTSTRAP_SIGNATURE = + "(Ljava/lang/invoke/MethodHandles$Lookup;" + + "Ljava/lang/String;" + + "Ljava/lang/invoke/MethodType;" + + ")Ljava/lang/invoke/CallSite;"; + /** The signature for the method that will be the target for all calls */ + public static final String METHOD_SIGNATURE = + "(Ljava/lang/Object;Ljava/lang/Object;Lorg/mozilla/javascript/Context;)Ljava/lang/Object;"; + + public static final String ADD_OP = "add"; + public static final String SUBTRACT_OP = "subtract"; + public static final String MULTIPLY_OP = "multiply"; + public static final String DIVIDE_OP = "divide"; + public static final String MOD_OP = "mod"; + public static final String AND_OP = "and"; + public static final String OR_OP = "or"; + public static final String XOR_OP = "xor"; + public static final String LSH_OP = "lsh"; + public static final String RSH_OP = "rsh"; + + /** An object that can be used to write the InvokeDynamic instruction for the bootstrap */ + public static final ClassFileWriter.MHandle BOOTSTRAP_HANDLE = + new ClassFileWriter.MHandle(ByteCode.MH_INVOKESTATIC, + "org.mozilla.javascript.optimizer.DynamicMathRuntime", + "bootstrap", + DynamicMathRuntime.BOOTSTRAP_SIGNATURE); + + /** + * Handle an exception in one of the optimized calls by causing a failure count to be incremented, + * and if exceeded, then the method handle is reset to point permanently to the fallback handle. + */ + public static Object fallback(MustFallbackException fe, + Object o1, Object o2, Context cx, + FallbackCallSite site) throws Throwable { + site.handleFailure(); + return site.getFallbackHandle().invoke(o1, o2, cx); + } + + /** + * Construct a CallSite that invokes the main method, and then calls "fallback" when it + * receives a MustFallbackException. + */ + private static CallSite makeCallSite(MethodHandles.Lookup lookup, + MethodHandle fastHandle, + MethodHandle genericHandle, MethodType mType) + throws NoSuchMethodException, IllegalAccessException { + + // Get the method handle of the "fallback" method, which prepends a MustFallbackException + // to the argument list. + MethodType fallbackType = mType.insertParameterTypes(0, MustFallbackException.class) + .insertParameterTypes(4, FallbackCallSite.class); + MethodHandle fallbackSub = lookup.findStatic(DynamicMathRuntime.class, + "fallback", fallbackType); + + FallbackCallSite site = new FallbackCallSite(mType); + // Bind the CallSite to the method invocation of the "fallback" method so that + // it can be modified. + MethodHandle fallbackSubInvoke = MethodHandles.insertArguments(fallbackSub, 4, site); + site.setFallbackHandle(genericHandle); + site.setTarget(MethodHandles.catchException(fastHandle, + MustFallbackException.class, fallbackSubInvoke)); + return site; + } + + /** + * Add two objects, assuming that each is an Integer and return the result as an Integer or a + * Double depending on size of the result. If either is not an Integer, + * throw MustFallbackException. + */ + public static Object integerAdd(Object o1, Object o2, Context cx) { + try { + // Do addition using 64-bit values to account for overflow + // since JavaScript has only one "number" type + final long i1 = (Integer) o1; + final long i2 = (Integer) o2; + final long r = i1 + i2; + if ((r < Integer.MIN_VALUE) || (r > Integer.MAX_VALUE)) { + // Overflow, so return result as a Double. + return ScriptRuntime.wrapNumber(r); + } + return ScriptRuntime.wrapInt((int) r); + } catch (ClassCastException | NullPointerException e) { + throw new MustFallbackException(); + } + } + + /** + * Same as integerAdd, but subtract. + */ + public static Object integerSubtract(Object o1, Object o2, Context cx) { + try { + final long i1 = (Integer) o1; + final long i2 = (Integer) o2; + final long r = i1 - i2; + if ((r < Integer.MIN_VALUE) || (r > Integer.MAX_VALUE)) { + return ScriptRuntime.wrapNumber(r); + } + return ScriptRuntime.wrapInt((int) r); + } catch (ClassCastException | NullPointerException e) { + throw new MustFallbackException(); + } + } + + /** + * Subtract any two values by using JavaScript's ToNumber to turn them into floating-point + * numbers, and return the result as a Double. + */ + public static Object genericSubtract(Object o1, Object o2, Context cx) { + final double d1 = ScriptRuntime.toNumber(o1); + final double d2 = ScriptRuntime.toNumber(o2); + return ScriptRuntime.wrapNumber(d1 - d2); + } + + public static Object genericDivide(Object o1, Object o2, Context cx) { + final double d1 = ScriptRuntime.toNumber(o1); + final double d2 = ScriptRuntime.toNumber(o2); + return ScriptRuntime.wrapNumber(d1 / d2); + } + + /** + * Same as integerAdd, but multiply. + */ + public static Object integerMultiply(Object o1, Object o2, Context cx) { + try { + final long i1 = (Integer) o1; + final long i2 = (Integer) o2; + final long r = i1 * i2; + if ((r < Integer.MIN_VALUE) || (r > Integer.MAX_VALUE)) { + return ScriptRuntime.wrapNumber(r); + } + return ScriptRuntime.wrapInt((int) r); + } catch (ClassCastException | NullPointerException e) { + throw new MustFallbackException(); + } + } + + public static Object genericMultiply(Object o1, Object o2, Context cx) { + final double d1 = ScriptRuntime.toNumber(o1); + final double d2 = ScriptRuntime.toNumber(o2); + return ScriptRuntime.wrapNumber(d1 * d2); + } + + public static Object genericMod(Object o1, Object o2, Context cx) { + final double d1 = ScriptRuntime.toNumber(o1); + final double d2 = ScriptRuntime.toNumber(o2); + return ScriptRuntime.wrapNumber(d1 % d2); + } + + /** + * Apply the bitwise "or" operation on two objects, assuming that both are Integers, and return + * the result as an Integer. If either is not an Integer, throw MustFallbackException. + */ + public static Object integerOr(Object o1, Object o2, Context cx) { + try { + final int i1 = (Integer) o1; + final int i2 = (Integer) o2; + return ScriptRuntime.wrapInt(i1 | i2); + } catch (ClassCastException | NullPointerException e) { + throw new MustFallbackException(); + } + } + + public static Object genericOr(Object o1, Object o2, Context cx) { + final int i1 = ScriptRuntime.toInt32(o1); + final int i2 = ScriptRuntime.toInt32(o2); + return ScriptRuntime.wrapInt(i1 | i2); + } + + /** + * Same as integerOr, but for an Xor. + */ + public static Object integerXor(Object o1, Object o2, Context cx) { + try { + final int i1 = (Integer) o1; + final int i2 = (Integer) o2; + return ScriptRuntime.wrapInt(i1 ^ i2); + } catch (ClassCastException | NullPointerException e) { + throw new MustFallbackException(); + } + } + + public static Object genericXor(Object o1, Object o2, Context cx) { + final int i1 = ScriptRuntime.toInt32(o1); + final int i2 = ScriptRuntime.toInt32(o2); + return ScriptRuntime.wrapInt(i1 ^ i2); + } + + /** + * Same as integerOr, but for an "and". + */ + public static Object integerAnd(Object o1, Object o2, Context cx) { + try { + final int i1 = (Integer) o1; + final int i2 = (Integer) o2; + return ScriptRuntime.wrapInt(i1 & i2); + } catch (ClassCastException | NullPointerException e) { + throw new MustFallbackException(); + } + } + + public static Object genericAnd(Object o1, Object o2, Context cx) { + final int i1 = ScriptRuntime.toInt32(o1); + final int i2 = ScriptRuntime.toInt32(o2); + return ScriptRuntime.wrapInt(i1 & i2); + } + + /** + * Same as integerOr, but for a right-shift. + */ + public static Object integerRsh(Object o1, Object o2, Context cx) { + try { + final int i1 = (Integer) o1; + final int i2 = (Integer) o2; + return ScriptRuntime.wrapInt(i1 >> i2); + } catch (ClassCastException | NullPointerException e) { + throw new MustFallbackException(); + } + } + + public static Object genericRsh(Object o1, Object o2, Context cx) { + final int i1 = ScriptRuntime.toInt32(o1); + final int i2 = ScriptRuntime.toInt32(o2); + return ScriptRuntime.wrapInt(i1 >> i2); + } + + /** + * Same as integerOr, but for a left-shift. + */ + public static Object integerLsh(Object o1, Object o2, Context cx) { + try { + final int i1 = (Integer) o1; + final int i2 = (Integer) o2; + return ScriptRuntime.wrapInt(i1 << i2); + } catch (ClassCastException | NullPointerException e) { + throw new MustFallbackException(); + } + } + + public static Object genericLsh(Object o1, Object o2, Context cx) { + final int i1 = ScriptRuntime.toInt32(o1); + final int i2 = ScriptRuntime.toInt32(o2); + return ScriptRuntime.wrapInt(i1 << i2); + } + + /** + * A bootstrap method that may be called by an InvokeDynamic instruction. The name + * of the operation passed in the instruction will be used to construct the method + * handle. + */ + @SuppressWarnings("unused") + public static CallSite bootstrap(MethodHandles.Lookup lookup, + String name, + MethodType mType) throws NoSuchMethodException, IllegalAccessException { + switch (name) { + case ADD_OP: + return makeCallSite(lookup, + lookup.findStatic(DynamicMathRuntime.class, "integerAdd", mType), + lookup.findStatic(ScriptRuntime.class, "add", mType), + mType); + case SUBTRACT_OP: + return makeCallSite(lookup, + lookup.findStatic(DynamicMathRuntime.class, "integerSubtract", mType), + lookup.findStatic(DynamicMathRuntime.class, "genericSubtract", mType), + mType); + case MULTIPLY_OP: + return makeCallSite(lookup, + lookup.findStatic(DynamicMathRuntime.class, "integerMultiply", mType), + lookup.findStatic(DynamicMathRuntime.class, "genericMultiply", mType), + mType); + case DIVIDE_OP: + // There is no integer optimization for divide and mod that makes sense to return + // an integer result, so always use the generic method. + return new ConstantCallSite( + lookup.findStatic(DynamicMathRuntime.class, "genericDivide", mType)); + case MOD_OP: + return new ConstantCallSite( + lookup.findStatic(DynamicMathRuntime.class, "genericMod", mType)); + case AND_OP: + return makeCallSite(lookup, + lookup.findStatic(DynamicMathRuntime.class, "integerAnd", mType), + lookup.findStatic(DynamicMathRuntime.class, "genericAnd", mType), + mType); + case OR_OP: + return makeCallSite(lookup, + lookup.findStatic(DynamicMathRuntime.class, "integerOr", mType), + lookup.findStatic(DynamicMathRuntime.class, "genericOr", mType), + mType); + case XOR_OP: + return makeCallSite(lookup, + lookup.findStatic(DynamicMathRuntime.class, "integerXor", mType), + lookup.findStatic(DynamicMathRuntime.class, "genericXor", mType), + mType); + case LSH_OP: + return makeCallSite(lookup, + lookup.findStatic(DynamicMathRuntime.class, "integerLsh", mType), + lookup.findStatic(DynamicMathRuntime.class, "genericLsh", mType), + mType); + case RSH_OP: + return makeCallSite(lookup, + lookup.findStatic(DynamicMathRuntime.class, "integerRsh", mType), + lookup.findStatic(DynamicMathRuntime.class, "genericRsh", mType), + mType); + default: + throw Kit.codeBug("Invalid bootstrap operation: " + name); + } + } +}
diff --git a/src/org/mozilla/javascript/optimizer/FallbackCallSite.java b/src/org/mozilla/javascript/optimizer/FallbackCallSite.java new file mode 100644 index 0000000..b128c36 --- /dev/null +++ b/src/org/mozilla/javascript/optimizer/FallbackCallSite.java
@@ -0,0 +1,39 @@ +package org.mozilla.javascript.optimizer; + +import java.lang.invoke.MethodHandle; +import java.lang.invoke.MethodType; +import java.lang.invoke.MutableCallSite; + +/* + * This class keeps track of a set of fallback handles. When a call fails, + * it switches to a new handle. + */ +public class FallbackCallSite + extends MutableCallSite +{ + public static final int MAX_FAILURES = 10; + + public FallbackCallSite(MethodType type) { + super(type); + } + + void handleFailure() { + failureCount++; + if (failureCount >= MAX_FAILURES) { + setTarget(fallbackHandle); + } + } + + public int getFailureCount() { + return failureCount; + } + + void setFallbackHandle(MethodHandle mh) { + this.fallbackHandle = mh; + } + + MethodHandle getFallbackHandle() { return fallbackHandle; } + + protected int failureCount = 0; + protected MethodHandle fallbackHandle; +}
diff --git a/src/org/mozilla/javascript/optimizer/MustFallbackException.java b/src/org/mozilla/javascript/optimizer/MustFallbackException.java new file mode 100644 index 0000000..9065d85 --- /dev/null +++ b/src/org/mozilla/javascript/optimizer/MustFallbackException.java
@@ -0,0 +1,7 @@ +package org.mozilla.javascript.optimizer; + +public class MustFallbackException + extends RuntimeException +{ + public MustFallbackException() {} +}
diff --git a/testsrc/benchmarks/v8-benchmarks-v6/run.js b/testsrc/benchmarks/v8-benchmarks-v6/run.js index 09960cd..09f39a2 100644 --- a/testsrc/benchmarks/v8-benchmarks-v6/run.js +++ b/testsrc/benchmarks/v8-benchmarks-v6/run.js
@@ -63,7 +63,7 @@ } // Run more than once to warm up JVM -var runs = 1; +var runs = 10; for (var i = 1; i <= runs; i++) {
diff --git a/testsrc/org/mozilla/javascript/tests/DynamicMathRuntimeTest.java b/testsrc/org/mozilla/javascript/tests/DynamicMathRuntimeTest.java new file mode 100644 index 0000000..208cff5 --- /dev/null +++ b/testsrc/org/mozilla/javascript/tests/DynamicMathRuntimeTest.java
@@ -0,0 +1,262 @@ +package org.mozilla.javascript.tests; + +import java.lang.invoke.CallSite; +import java.lang.invoke.MethodHandle; +import java.lang.invoke.MethodHandles; +import java.lang.invoke.MethodType; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.mozilla.javascript.Context; +import org.mozilla.javascript.Undefined; +import org.mozilla.javascript.optimizer.DynamicMathRuntime; +import org.mozilla.javascript.optimizer.FallbackCallSite; + +import static org.junit.Assert.*; + +public class DynamicMathRuntimeTest { + + private static final MethodHandles.Lookup lookup = MethodHandles.lookup(); + private static final MethodType invokeType = + MethodType.methodType(Object.class, Object.class, Object.class, Context.class); + + private Context cx; + + @Before + public void init() { + cx = Context.enter(); + } + + @After + public void close() { + Context.exit(); + } + + @Test + public void testIntegerAdd() throws Throwable { + FallbackCallSite site = (FallbackCallSite) DynamicMathRuntime.bootstrap(lookup, + DynamicMathRuntime.ADD_OP, invokeType); + MethodHandle call = site.dynamicInvoker(); + // Integer ops + assertEquals(0, call.invoke(0, 0, cx)); + assertEquals(4, call.invoke(2, 2, cx)); + assertEquals(4, call.invoke(6, -2, cx)); + // Overflow + double bigResult = (double) ((long) Integer.MAX_VALUE + (long) 10); + assertEquals(bigResult, call.invoke(Integer.MAX_VALUE, 10, cx)); + double tinyResult = (double) ((long) Integer.MIN_VALUE - 10000); + assertEquals(tinyResult, call.invoke(Integer.MIN_VALUE, -10000, cx)); + // All of this should have been handled by the fast path + assertEquals(0, site.getFailureCount()); + } + + @Test + public void testMixedAdd() throws Throwable { + FallbackCallSite site = (FallbackCallSite) DynamicMathRuntime.bootstrap(lookup, + DynamicMathRuntime.ADD_OP, invokeType); + MethodHandle call = site.dynamicInvoker(); + assertEquals(0, call.invoke(0, 0, cx)); + assertEquals(4.0, call.invoke(2.0, 2, cx)); + assertEquals(4.0, call.invoke(6, -2.0, cx)); + double bigResult = (double) ((long) Integer.MAX_VALUE + (long) 10); + assertEquals(bigResult, call.invoke(Integer.MAX_VALUE, 10, cx)); + double tinyResult = (double) ((long) Integer.MIN_VALUE - 10000); + assertEquals(tinyResult, call.invoke(Integer.MIN_VALUE, -10000, cx)); + assertEquals("123 Sesame Street", call.invoke(123, " Sesame Street", cx).toString()); + assertEquals(100.0, call.invoke(100, null, cx)); + assertTrue(Double.isNaN((double) call.invoke(Undefined.instance, 100, cx))); + // Should not have gone over yet + assertTrue(site.getFailureCount() < FallbackCallSite.MAX_FAILURES); + + // Now switch over to the slow path + for (int i = 0; i < FallbackCallSite.MAX_FAILURES * 2; i++) { + assertEquals(3.14, call.invoke(3.0, 0.14, cx)); + } + assertTrue(site.getFailureCount() >= FallbackCallSite.MAX_FAILURES); + } + + @Test + public void testMixedSubtract() throws Throwable { + FallbackCallSite site = (FallbackCallSite) DynamicMathRuntime.bootstrap(lookup, + DynamicMathRuntime.SUBTRACT_OP, invokeType); + MethodHandle call = site.dynamicInvoker(); + assertEquals(0, call.invoke(0, 0, cx)); + assertEquals(0.0, call.invoke(2.0, 2, cx)); + assertEquals(8.0, call.invoke(6, -2.0, cx)); + double tinyResult = (double) ((long) Integer.MIN_VALUE - 10000); + assertEquals(tinyResult, call.invoke(Integer.MIN_VALUE, 10000, cx)); + // Should not have gone over yet + assertTrue(site.getFailureCount() < FallbackCallSite.MAX_FAILURES); + + // Now switch over to the slow path + for (int i = 0; i < FallbackCallSite.MAX_FAILURES * 2; i++) { + assertEquals(3.0, call.invoke(3.14, 0.14, cx)); + } + assertTrue(site.getFailureCount() >= FallbackCallSite.MAX_FAILURES); + } + + @Test + public void testMixedMultiply() throws Throwable { + FallbackCallSite site = (FallbackCallSite) DynamicMathRuntime.bootstrap(lookup, + DynamicMathRuntime.MULTIPLY_OP, invokeType); + MethodHandle call = site.dynamicInvoker(); + assertEquals(0, call.invoke(0, 0, cx)); + assertEquals(16, call.invoke(4, 4, cx)); + assertEquals(4.0, call.invoke(2.0, 2, cx)); + assertEquals(-12, call.invoke(6, -2, cx)); + assertEquals(-12.0, call.invoke(6, -2.0, cx)); + double bigResult = (double) ((long) Integer.MAX_VALUE * 4); + assertEquals(bigResult, call.invoke(Integer.MAX_VALUE, 4, cx)); + assertEquals(0.0, call.invoke(100, null, cx)); + assertTrue(Double.isNaN((double) call.invoke(Undefined.instance, 100, cx))); + // Should not have gone over yet + assertTrue(site.getFailureCount() < FallbackCallSite.MAX_FAILURES); + + // Now switch over to the slow path + for (int i = 0; i < FallbackCallSite.MAX_FAILURES * 2; i++) { + assertEquals(36.0, call.invoke(6.0, 6.0, cx)); + } + assertTrue(site.getFailureCount() >= FallbackCallSite.MAX_FAILURES); + } + + @Test + public void testMixedDivide() throws Throwable { + // No fallback for this operation -- all done using floating-point + CallSite site = DynamicMathRuntime.bootstrap(lookup, + DynamicMathRuntime.DIVIDE_OP, invokeType); + MethodHandle call = site.dynamicInvoker(); + assertEquals(4.0, call.invoke(16, 4, cx)); + assertEquals(4.0, call.invoke(8.0, 2, cx)); + assertEquals(-3.0, call.invoke(6, -2, cx)); + assertEquals(-3.0, call.invoke(6, -2.0, cx)); + assertTrue(Double.isInfinite((double) call.invoke(100, 0, cx))); + assertTrue(Double.isInfinite((double) call.invoke(100, 0.0, cx))); + assertTrue(Double.isInfinite((double) call.invoke(100, null, cx))); + assertEquals(0.0, call.invoke(null, 10, cx)); + assertTrue(Double.isNaN((double) call.invoke(Undefined.instance, 100, cx))); + assertTrue(Double.isNaN((double) call.invoke(100, Undefined.instance, cx))); + } + + @Test + public void testMixedMod() throws Throwable { + // No fallback for this operation -- all done using floating-point + CallSite site = DynamicMathRuntime.bootstrap(lookup, + DynamicMathRuntime.MOD_OP, invokeType); + MethodHandle call = site.dynamicInvoker(); + assertEquals(0.0, call.invoke(16, 4, cx)); + assertEquals(2.0, call.invoke(12.0, 5, cx)); + assertEquals(2.0, call.invoke(12, 5.0, cx)); + assertEquals(2.0, call.invoke(12.0, 5.0, cx)); + assertTrue(Double.isNaN((double) call.invoke(100, 0, cx))); + assertTrue(Double.isNaN((double) call.invoke(100, 0.0, cx))); + assertTrue(Double.isNaN((double) call.invoke(100, null, cx))); + assertEquals(0.0, call.invoke(null, 100, cx)); + assertTrue(Double.isNaN((double) call.invoke(100, Undefined.instance, cx))); + assertTrue(Double.isNaN((double) call.invoke(Undefined.instance, 100, cx))); + } + + @Test + public void testIntegerAnd() throws Throwable { + FallbackCallSite site = (FallbackCallSite) DynamicMathRuntime.bootstrap(lookup, + DynamicMathRuntime.AND_OP, invokeType); + MethodHandle call = site.dynamicInvoker(); + assertEquals(0, call.invoke(2, 4, cx)); + assertEquals(4, call.invoke(15, 4, cx)); + assertEquals(0, call.invoke(1 << 31, 1, cx)); + assertEquals(0xffff, call.invoke(0xefffffff, 0xffff, cx)); + assertEquals(0, site.getFailureCount()); + } + + @Test + public void testMixedAnd() throws Throwable { + FallbackCallSite site = (FallbackCallSite) DynamicMathRuntime.bootstrap(lookup, + DynamicMathRuntime.AND_OP, invokeType); + MethodHandle call = site.dynamicInvoker(); + // Even mixed operations end up as Integers + assertEquals(0, call.invoke(2, 4, cx)); + assertEquals(4, call.invoke(15, 4.0, cx)); + assertEquals(0, call.invoke(1 << 31, 1.0, cx)); + assertTrue(site.getFailureCount() < FallbackCallSite.MAX_FAILURES); + assertEquals(0, call.invoke(100, null, cx)); + assertEquals(0, call.invoke(null, 100, cx)); + assertEquals(0, call.invoke(100, Undefined.instance, cx)); + assertEquals(0, call.invoke(Undefined.instance, 100, cx)); + + for (int i = 0; i < FallbackCallSite.MAX_FAILURES * 2; i++) { + assertEquals(4, call.invoke(15, 4.0, cx)); + } + assertTrue(site.getFailureCount() >= FallbackCallSite.MAX_FAILURES); + } + + @Test + public void testMixedOr() throws Throwable { + FallbackCallSite site = (FallbackCallSite) DynamicMathRuntime.bootstrap(lookup, + DynamicMathRuntime.OR_OP, invokeType); + MethodHandle call = site.dynamicInvoker(); + // Even mixed operations end up as Integers + assertEquals(6, call.invoke(2, 4, cx)); + assertEquals(15, call.invoke(15, 4.0, cx)); + assertEquals(1 << 30 | 4, call.invoke(1 << 30, 4.0, cx)); + assertTrue(site.getFailureCount() < FallbackCallSite.MAX_FAILURES); + + for (int i = 0; i < FallbackCallSite.MAX_FAILURES * 2; i++) { + assertEquals(15, call.invoke(15, 4.0, cx)); + } + assertTrue(site.getFailureCount() >= FallbackCallSite.MAX_FAILURES); + } + + @Test + public void testMixedXor() throws Throwable { + FallbackCallSite site = (FallbackCallSite) DynamicMathRuntime.bootstrap(lookup, + DynamicMathRuntime.XOR_OP, invokeType); + MethodHandle call = site.dynamicInvoker(); + // Even mixed operations end up as Integers + assertEquals(6, call.invoke(2, 4, cx)); + assertEquals(11, call.invoke(15, 4.0, cx)); + assertEquals(1 << 30 ^ 4, call.invoke(1 << 30, 4.0, cx)); + assertTrue(site.getFailureCount() < FallbackCallSite.MAX_FAILURES); + + for (int i = 0; i < FallbackCallSite.MAX_FAILURES * 2; i++) { + assertEquals(11, call.invoke(15, 4.0, cx)); + } + assertTrue(site.getFailureCount() >= FallbackCallSite.MAX_FAILURES); + } + + @Test + public void testMixedLsh() throws Throwable { + FallbackCallSite site = (FallbackCallSite) DynamicMathRuntime.bootstrap(lookup, + DynamicMathRuntime.LSH_OP, invokeType); + MethodHandle call = site.dynamicInvoker(); + // Even mixed operations end up as Integers + assertEquals(1024, call.invoke(1, 10, cx)); + assertEquals(1 << 31, call.invoke(1.0, 31, cx)); + assertEquals(1, call.invoke(1, 32.0, cx)); + assertTrue(site.getFailureCount() < FallbackCallSite.MAX_FAILURES); + assertEquals(1, call.invoke(1, null, cx)); + assertEquals(0, call.invoke(null, 1, cx)); + assertEquals(1, call.invoke(1, Undefined.instance, cx)); + assertEquals(0, call.invoke(Undefined.instance, 1, cx)); + + for (int i = 0; i < FallbackCallSite.MAX_FAILURES * 2; i++) { + assertEquals(1024, call.invoke(1, 10.0, cx)); + } + assertTrue(site.getFailureCount() >= FallbackCallSite.MAX_FAILURES); + } + + @Test + public void testMixedRsh() throws Throwable { + FallbackCallSite site = (FallbackCallSite) DynamicMathRuntime.bootstrap(lookup, + DynamicMathRuntime.RSH_OP, invokeType); + MethodHandle call = site.dynamicInvoker(); + // Even mixed operations end up as Integers + assertEquals(64, call.invoke(1024, 4, cx)); + assertEquals(64, call.invoke(1024, 4.0, cx)); + assertEquals(0, call.invoke(1024.0, 31, cx)); + assertTrue(site.getFailureCount() < FallbackCallSite.MAX_FAILURES); + + for (int i = 0; i < FallbackCallSite.MAX_FAILURES * 2; i++) { + assertEquals(255, call.invoke(0xffff, 8.0, cx)); + } + assertTrue(site.getFailureCount() >= FallbackCallSite.MAX_FAILURES); + } +}