annotate samples/Main.asm @ 1555:376a63a077ee

8144186: Update Main.asm sample to use dynalink public API Reviewed-by: hannesw, mhaupt
author sundar
date Fri, 27 Nov 2015 18:28:16 +0530
parents bfe6bd5d57bf
children
rev   line source
sundar@1412 1 /*
sundar@1412 2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
sundar@1412 3 *
sundar@1412 4 * Redistribution and use in source and binary forms, with or without
sundar@1412 5 * modification, are permitted provided that the following conditions
sundar@1412 6 * are met:
sundar@1412 7 *
sundar@1412 8 * - Redistributions of source code must retain the above copyright
sundar@1412 9 * notice, this list of conditions and the following disclaimer.
sundar@1412 10 *
sundar@1412 11 * - Redistributions in binary form must reproduce the above copyright
sundar@1412 12 * notice, this list of conditions and the following disclaimer in the
sundar@1412 13 * documentation and/or other materials provided with the distribution.
sundar@1412 14 *
sundar@1412 15 * - Neither the name of Oracle nor the names of its
sundar@1412 16 * contributors may be used to endorse or promote products derived
sundar@1412 17 * from this software without specific prior written permission.
sundar@1412 18 *
sundar@1412 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
sundar@1412 20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
sundar@1412 21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
sundar@1412 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
sundar@1412 23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
sundar@1412 24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
sundar@1412 25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
sundar@1412 26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
sundar@1412 27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
sundar@1412 28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
sundar@1412 29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
sundar@1412 30 */
sundar@1412 31
sundar@1412 32 // Simple sample to demonstrate openjdk asmtools assembler with
sundar@1412 33 // nashorn dynalink linker in a invokedynamic instruction.
sundar@1412 34 //
sundar@1412 35 // To assemble this file, use the following command:
sundar@1412 36 //
sundar@1412 37 // java -cp <asmtools.jar> org.openjdk.asmtools.Main jasm Main.asm
sundar@1412 38 //
sundar@1412 39 // See also: https://wiki.openjdk.java.net/display/CodeTools/asmtools
sundar@1412 40 //
sundar@1555 41 // NOTE: Uses dynalink API defined by JEP-276 (http://openjdk.java.net/jeps/276)
sundar@1412 42
sundar@1412 43 super public class Main
sundar@1412 44 version 52:0
sundar@1412 45 {
sundar@1412 46
sundar@1555 47 private static final Field linker:"Ljdk/dynalink/DynamicLinker;";
sundar@1412 48
sundar@1555 49 /*
sundar@1555 50 static {
sundar@1555 51 // create dynalink linker factory
sundar@1555 52 final DynamicLinkerFactory factory = new DynamicLinkerFactory();
sundar@1555 53
sundar@1555 54 // create standard linker that can handle Java POJOs
sundar@1555 55 linker = factory.createLinker();
sundar@1555 56 }
sundar@1555 57 */
sundar@1555 58 static Method "<clinit>":"()V"
sundar@1555 59 stack 2 locals 1
sundar@1555 60 {
sundar@1555 61 new class jdk/dynalink/DynamicLinkerFactory;
sundar@1555 62 dup;
sundar@1555 63 invokespecial Method jdk/dynalink/DynamicLinkerFactory."<init>":"()V";
sundar@1555 64 astore_0;
sundar@1555 65 aload_0;
sundar@1555 66 invokevirtual Method jdk/dynalink/DynamicLinkerFactory.createLinker:"()Ljdk/dynalink/DynamicLinker;";
sundar@1555 67 putstatic Field linker:"Ljdk/dynalink/DynamicLinker;";
sundar@1555 68 return;
sundar@1555 69 }
sundar@1555 70
sundar@1555 71 /*
sundar@1555 72 // Bootstrap method used with invokedynamic methods
sundar@1555 73
sundar@1555 74 public static CallSite bootstrap(MethodHandles.Lookup caller, String name, MethodType type) {
sundar@1555 75 // use dynalink linker to perform the actual linking
sundar@1555 76 return linker.link(
sundar@1555 77 new SimpleRelinkableCallSite(
sundar@1555 78 new CallSiteDescriptor(
sundar@1555 79 MethodHandles.publicLookup(),
sundar@1555 80 new NamedOperation(StandardOperation.GET_PROPERTY, name), type)
sundar@1555 81 )
sundar@1555 82 );
sundar@1555 83 }
sundar@1555 84
sundar@1555 85 */
sundar@1555 86 public static Method bootstrap:"(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;"
sundar@1555 87 stack 10 locals 3
sundar@1555 88 {
sundar@1555 89 getstatic Field linker:"Ljdk/dynalink/DynamicLinker;";
sundar@1555 90 new class jdk/dynalink/support/SimpleRelinkableCallSite;
sundar@1555 91 dup;
sundar@1555 92 new class jdk/dynalink/CallSiteDescriptor;
sundar@1555 93 dup;
sundar@1555 94 invokestatic Method java/lang/invoke/MethodHandles.publicLookup:"()Ljava/lang/invoke/MethodHandles$Lookup;";
sundar@1555 95 new class jdk/dynalink/NamedOperation;
sundar@1555 96 dup;
sundar@1555 97 getstatic Field jdk/dynalink/StandardOperation.GET_PROPERTY:"Ljdk/dynalink/StandardOperation;";
sundar@1555 98 aload_1;
sundar@1555 99 invokespecial Method jdk/dynalink/NamedOperation."<init>":"(Ljdk/dynalink/Operation;Ljava/lang/Object;)V";
sundar@1555 100 aload_2;
sundar@1555 101 invokespecial Method jdk/dynalink/CallSiteDescriptor."<init>":"(Ljava/lang/invoke/MethodHandles$Lookup;Ljdk/dynalink/Operation;Ljava/lang/invoke/MethodType;)V";
sundar@1555 102 invokespecial Method jdk/dynalink/support/SimpleRelinkableCallSite."<init>":"(Ljdk/dynalink/CallSiteDescriptor;)V";
sundar@1555 103 invokevirtual Method jdk/dynalink/DynamicLinker.link:"(Ljdk/dynalink/RelinkableCallSite;)Ljdk/dynalink/RelinkableCallSite;";
sundar@1555 104 checkcast class java/lang/invoke/CallSite;
sundar@1555 105 areturn;
sundar@1555 106 }
sundar@1555 107
sundar@1555 108 // default constructor that does nothing!
sundar@1412 109 public Method "<init>":"()V"
sundar@1412 110 stack 1 locals 1
sundar@1412 111 {
sundar@1555 112 aload_0;
sundar@1555 113 invokespecial Method java/lang/Object."<init>":"()V";
sundar@1555 114 return;
sundar@1412 115 }
sundar@1412 116
sundar@1412 117 public static Method main:"([Ljava/lang/String;)V"
sundar@1412 118 stack 2 locals 2
sundar@1412 119 {
sundar@1555 120 // List l = new ArrayList();
sundar@1555 121 new class java/util/ArrayList;
sundar@1555 122 dup;
sundar@1555 123 invokespecial Method java/util/ArrayList."<init>":"()V";
sundar@1555 124 astore_1;
sundar@1555 125 aload_1;
sundar@1412 126
sundar@1555 127 // l.add("hello");
sundar@1555 128 ldc String "hello";
sundar@1555 129 invokeinterface InterfaceMethod java/util/List.add:"(Ljava/lang/Object;)Z", 2;
sundar@1555 130 pop;
sundar@1412 131
sundar@1555 132 // l.add("world");
sundar@1555 133 aload_1;
sundar@1555 134 ldc String "world";
sundar@1555 135 invokeinterface InterfaceMethod java/util/List.add:"(Ljava/lang/Object;)Z", 2;
sundar@1555 136 pop;
sundar@1412 137
sundar@1555 138 // printLength(l);
sundar@1555 139 aload_1;
sundar@1555 140 invokestatic Method printLength:"(Ljava/lang/Object;)V";
sundar@1412 141
sundar@1555 142 // printLength(args); // args is argument of main method
sundar@1555 143 aload_0;
sundar@1555 144 invokestatic Method printLength:"(Ljava/lang/Object;)V";
sundar@1555 145 return;
sundar@1412 146 }
sundar@1412 147
sundar@1412 148 private static Method printLength:"(Ljava/lang/Object;)V"
sundar@1412 149 stack 2 locals 1
sundar@1412 150 {
sundar@1555 151 getstatic Field java/lang/System.out:"Ljava/io/PrintStream;";
sundar@1555 152 aload_0;
sundar@1412 153
sundar@1555 154 // Using bootstrap method in this class with the following invokedynamic
sundar@1555 155 // which uses dynalink linker. Dynalink's bean linker handles Java beans.
sundar@1555 156 // 'length' property on a bean - arrays, lists supported
sundar@1412 157
sundar@1555 158 invokedynamic InvokeDynamic REF_invokeStatic:Main.bootstrap:"(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;":"length":"(Ljava/lang/Object;)Ljava/lang/Object;";
sundar@1412 159
sundar@1555 160 // print 'length' value
sundar@1555 161 invokevirtual Method java/io/PrintStream.println:"(Ljava/lang/Object;)V";
sundar@1555 162 return;
sundar@1412 163 }
sundar@1412 164
sundar@1412 165 } // end Class Main