2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
6 * The Java Pathfinder core (jpf-core) platform is licensed under the
7 * Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0.
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package gov.nasa.jpf.jvm.bytecode;
20 import gov.nasa.jpf.vm.BootstrapMethodInfo;
21 import gov.nasa.jpf.vm.ClassInfo;
22 import gov.nasa.jpf.vm.ElementInfo;
23 import gov.nasa.jpf.vm.FunctionObjectFactory;
24 import gov.nasa.jpf.vm.Instruction;
25 import gov.nasa.jpf.vm.LoadOnJPFRequired;
26 import gov.nasa.jpf.vm.MJIEnv;
27 import gov.nasa.jpf.vm.StackFrame;
28 import gov.nasa.jpf.vm.ThreadInfo;
29 import gov.nasa.jpf.vm.Types;
30 import gov.nasa.jpf.vm.VM;
33 * @author Nastaran Shafiei <nastaran.shafiei@gmail.com>
35 * Invoke dynamic method. It allows dynamic linkage between a call site and a method implementation.
37 * ..., [arg1, [arg2 ...]] => ...
39 public class INVOKEDYNAMIC extends Instruction {
41 // index of a bootstrap method (index to the array bootstrapMethods[] declared in ClassInfo
42 // containing this bytecode instruction)
43 int bootstrapMethodIndex;
45 // Free variables are those that are not defined within the lamabda body and
46 // are captured from the lexical scope. Note that for instance lambda methods
47 // the first captured variable always represents "this"
48 String[] freeVariableTypeNames;
49 byte[] freeVariableTypes;
51 String functionalInterfaceName;
55 int funcObjRef = MJIEnv.NULL;
57 ElementInfo lastFuncObj = null;
59 public INVOKEDYNAMIC () {}
61 protected INVOKEDYNAMIC (int bmIndex, String methodName, String descriptor){
62 bootstrapMethodIndex = bmIndex;
63 samMethodName = methodName;
64 freeVariableTypeNames = Types.getArgumentTypeNames(descriptor);
65 freeVariableTypes = Types.getArgumentTypes(descriptor);
66 functionalInterfaceName = Types.getReturnTypeSignature(descriptor);
70 public int getByteCode () {
75 public String toString() {
77 for(String type: freeVariableTypeNames) {
83 return "invokedynamic " + bootstrapMethodIndex + " " +
84 samMethodName + '(' + args +"):" + functionalInterfaceName;
88 * For now, INVOKEDYNAMIC works only in the context of lambda expressions.
89 * Executing this returns an object that implements the functional interface
90 * and contains a method which captures the behavior of the lambda expression.
93 public Instruction execute (ThreadInfo ti) {
94 StackFrame frame = ti.getModifiableTopFrame();
96 ElementInfo ei = ti.getHeap().get(funcObjRef);
98 if(ei==null || ei!=lastFuncObj || freeVariableTypes.length>0) {
99 ClassInfo fiClassInfo;
101 // First, resolve the functional interface
103 fiClassInfo = ti.resolveReferencedClass(functionalInterfaceName);
104 } catch(LoadOnJPFRequired lre) {
108 if (fiClassInfo.initializeClass(ti)) {
112 ClassInfo enclosingClass = this.getMethodInfo().getClassInfo();
114 BootstrapMethodInfo bmi = enclosingClass.getBootstrapMethodInfo(bootstrapMethodIndex);
117 FunctionObjectFactory funcObjFactory = vm.getFunctionObjectFacotry();
119 Object[] freeVariableValues = frame.getArgumentsValues(ti, freeVariableTypes);
121 funcObjRef = funcObjFactory.getFunctionObject(bootstrapMethodIndex, ti, fiClassInfo, samMethodName, bmi, freeVariableTypeNames, freeVariableValues);
122 lastFuncObj = ti.getHeap().get(funcObjRef);
125 frame.pop(freeVariableTypes.length);
126 frame.pushRef(funcObjRef);