changeset 38:fa252dd74a58

indy-demo: add InlineCache demo
author jrose
date Sat, 08 May 2010 14:35:22 -0700
parents 6801a67b2bfd
children ea314633878b
files netbeans/indy-demo/nbproject/project.properties netbeans/indy-demo/src/Hello.java netbeans/indy-demo/src/Main.java netbeans/indy-demo/src/recipes/FastAndSlow.java netbeans/indy-demo/src/recipes/InlineCache.java
diffstat 5 files changed, 90 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/netbeans/indy-demo/nbproject/project.properties	Wed Mar 24 21:44:42 2010 -0700
+++ b/netbeans/indy-demo/nbproject/project.properties	Sat May 08 14:35:22 2010 -0700
@@ -27,8 +27,6 @@
     ${reference.meth.jar}
 # Space-separated list of extra javac options
 javac.compilerargs=-XDinvokedynamic -Xlint:deprecation
-#    -J-ea -J-esa -J-Xbootclasspath/p:${reference.langtools}/build/bootstrap/classes
-#    -Xbootclasspath/p:"${reference.meth.jar}"
 javac.deprecation=false
 javac.source=1.5
 javac.target=1.5
--- a/netbeans/indy-demo/src/Hello.java	Wed Mar 24 21:44:42 2010 -0700
+++ b/netbeans/indy-demo/src/Hello.java	Sat May 08 14:35:22 2010 -0700
@@ -40,7 +40,7 @@
             // previous line generates invokevirtual MethodHandle.invokeExact(String)void
             Object x = whom;
             InvokeDynamic.hail(x);               // weakly typed invokedynamic
-            // previous line generates invokedynamic MethodHandle.invokeExact(Object)Object
+            // previous line generates invokedynamic hail(Object)Object
         }
     }
 
@@ -53,7 +53,7 @@
     // Set up a class-local bootstrap method.
     static { Linkage.registerBootstrapMethod("bootstrapDynamic"); }
     private static CallSite bootstrapDynamic(Class caller, String name, MethodType type) {
-        assert(type.parameterCount() == 1 && (Object)name == "hail");  // in lieu of MOP
+        assert(type.parameterCount() == 1 && name.equals("hail"));  // in lieu of MOP
         System.out.println("set target to adapt "+greeter);
         return new CallSite(greeter.asType(type));
     }
--- a/netbeans/indy-demo/src/Main.java	Wed Mar 24 21:44:42 2010 -0700
+++ b/netbeans/indy-demo/src/Main.java	Sat May 08 14:35:22 2010 -0700
@@ -1,5 +1,5 @@
 /*
- Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
+ Copyright 2009,2010 Sun Microsystems, Inc.  All Rights Reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
@@ -43,6 +43,7 @@
         FidgetDemo.class,
         recipes.PlainOldJava.class,
         recipes.FastAndSlow.class,
+        recipes.InlineCache.class,
         recipes.Curry.class
     };
     public static void doClasses(Class[] classes, String[] args) throws Throwable {
--- a/netbeans/indy-demo/src/recipes/FastAndSlow.java	Wed Mar 24 21:44:42 2010 -0700
+++ b/netbeans/indy-demo/src/recipes/FastAndSlow.java	Sat May 08 14:35:22 2010 -0700
@@ -1,5 +1,5 @@
 /*
- Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
+ Copyright 2009,2010 Sun Microsystems, Inc.  All Rights Reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
@@ -65,18 +65,25 @@
         return x instanceof Integer && y instanceof Integer;
     }
 
-    public static void main(String... av) throws Throwable {
-        MethodHandle fastAdd =
+    // ---- MH constants (could be loaded from constant pool, also)
+    static private final MethodHandle
+        fastAdd =
             LOOKUP.findStatic(FastAndSlow.class, "fastAdd",
-                methodType(Object.class, int.class, int.class));
-        MethodHandle slowAdd =
+                methodType(Object.class, int.class, int.class)),
+        slowAdd =
             LOOKUP.findStatic(FastAndSlow.class, "slowAdd",
-                methodType(Object.class, Object.class, Object.class));
-        MethodHandle bothInts =
+                methodType(Object.class, Object.class, Object.class)),
+        bothInts =
             LOOKUP.findStatic(FastAndSlow.class, "bothInts",
                 methodType(boolean.class, Object.class, Object.class));
-        fastAdd = convertArguments(fastAdd, slowAdd.type());
-        MethodHandle combo = guardWithTest(bothInts, fastAdd, slowAdd);
+
+    static MethodHandle makeCheckedFastAdd() {
+        MethodHandle fastPath = fastAdd.asType(slowAdd.type());
+        return guardWithTest(bothInts, fastPath, slowAdd);
+    }
+
+    public static void main(String... av) throws Throwable {
+        MethodHandle combo = makeCheckedFastAdd();
         println(combo.invokeVarargs(2, 3));
         println(combo.invokeVarargs(2.1, 3.1));
         println(combo.invokeVarargs(Integer.MAX_VALUE, -1));
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/netbeans/indy-demo/src/recipes/InlineCache.java	Sat May 08 14:35:22 2010 -0700
@@ -0,0 +1,70 @@
+/*
+ Copyright 2010 Sun Microsystems, Inc.  All Rights Reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+
+   - Redistributions of source code must retain the above copyright
+     notice, this list of conditions and the following disclaimer.
+
+   - Redistributions in binary form must reproduce the above copyright
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
+
+   - Neither the name of Sun Microsystems nor the names of its
+     contributors may be used to endorse or promote products derived
+     from this software without specific prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+package recipes;
+
+/* Run like this:
+   java -XX:+UnlockExperimentalVMOptions -XX:+EnableInvokeDynamic -cp dist/InvokeDynamicDemo.jar recipes.InlineCache 100000
+   To examine the code, use: -XX:CompileCommand=print,*.doOneCall
+   Make sure a copy of the 'hsdis' plugin is on your dynamic library search path.
+   This plugin can be built from sources in the OpenJDK.
+   For x86, it can also be obtained from: http://kenai.com/projects/base-hsdis
+ */
+
+import java.dyn.*;
+import java.dyn.MethodHandles.*;
+
+public class InlineCache {
+    public static void main(String... av) throws Throwable {
+        int limit = 100000;
+        if (av.length > 0)
+            limit = Integer.parseInt(av[0]);
+        for (int i = 0; i < limit; i++) {
+            int x = i%97, y = (i*11)%91;
+            int z = (Integer) doOneCall((Integer)x, (Integer)y);
+            if (z != x + y)  throw new RuntimeException("fail");
+        }
+    }
+
+    static Object doOneCall(Object x, Object y) throws Throwable {
+        return InvokeDynamic.facciaDerPrammaKudasai(x, y); // do the thing, please
+    }
+
+    // ---- invokedynamic infrastructure
+
+    static {
+        Linkage.registerBootstrapMethod("bootstrapDynamic");
+    }
+    private static CallSite bootstrapDynamic(Class caller, String name, MethodType type) {
+        // ignore caller and name, but get the type right:
+        return new CallSite(FastAndSlow.makeCheckedFastAdd().asType(type));
+    }
+}