Initial import
[jpf-core.git] / src / main / gov / nasa / jpf / jvm / bytecode / INVOKEDYNAMIC.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
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
9  * 
10  *        http://www.apache.org/licenses/LICENSE-2.0. 
11  *
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.
17  */
18 package gov.nasa.jpf.jvm.bytecode;
19
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;
31
32 /**
33  * @author Nastaran Shafiei <nastaran.shafiei@gmail.com>
34  * 
35  * Invoke dynamic method. It allows dynamic linkage between a call site and a method implementation.
36  *
37  * ..., [arg1, [arg2 ...]]  => ...
38  */
39 public class INVOKEDYNAMIC extends Instruction {
40   
41   // index of a bootstrap method (index to the array bootstrapMethods[] declared in ClassInfo
42   // containing this bytecode instruction)
43   int bootstrapMethodIndex;
44   
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;
50   
51   String functionalInterfaceName;
52   
53   String samMethodName;
54   
55   int funcObjRef = MJIEnv.NULL;
56   
57   ElementInfo lastFuncObj = null;
58   
59   public INVOKEDYNAMIC () {}
60
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);
67   }
68
69   @Override
70   public int getByteCode () {
71     return 0xBA;
72   }
73   
74   @Override
75   public String toString() {
76     String args = "";
77     for(String type: freeVariableTypeNames) {
78       if(args.length()>0) {
79         type += ','+ type;
80       }
81       args += type;
82     }
83     return "invokedynamic " + bootstrapMethodIndex + " " + 
84     samMethodName + '(' + args +"):" + functionalInterfaceName;
85   }
86
87   /**
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.
91    */
92   @Override
93   public Instruction execute (ThreadInfo ti) {
94     StackFrame frame = ti.getModifiableTopFrame();
95     
96     ElementInfo ei = ti.getHeap().get(funcObjRef);
97     
98     if(ei==null || ei!=lastFuncObj || freeVariableTypes.length>0) {
99       ClassInfo fiClassInfo;
100
101       // First, resolve the functional interface
102       try {
103         fiClassInfo = ti.resolveReferencedClass(functionalInterfaceName);
104       } catch(LoadOnJPFRequired lre) {
105         return ti.getPC();
106       }
107
108       if (fiClassInfo.initializeClass(ti)) {
109         return ti.getPC();
110       }
111       
112       ClassInfo enclosingClass = this.getMethodInfo().getClassInfo();
113       
114       BootstrapMethodInfo bmi = enclosingClass.getBootstrapMethodInfo(bootstrapMethodIndex);
115       
116       VM vm = VM.getVM();
117       FunctionObjectFactory funcObjFactory = vm.getFunctionObjectFacotry();
118       
119       Object[] freeVariableValues = frame.getArgumentsValues(ti, freeVariableTypes);
120       
121       funcObjRef = funcObjFactory.getFunctionObject(bootstrapMethodIndex, ti, fiClassInfo, samMethodName, bmi, freeVariableTypeNames, freeVariableValues);
122       lastFuncObj = ti.getHeap().get(funcObjRef);
123     }
124     
125     frame.pop(freeVariableTypes.length);
126     frame.pushRef(funcObjRef);
127     
128     return getNext(ti);
129   }
130 }