Initial import
[jpf-core.git] / src / main / gov / nasa / jpf / jvm / bytecode / INVOKECG.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
19 package gov.nasa.jpf.jvm.bytecode;
20
21 import gov.nasa.jpf.vm.Instruction;
22 import gov.nasa.jpf.jvm.JVMInstructionFactory;
23 import gov.nasa.jpf.util.Invocation;
24 import gov.nasa.jpf.vm.MethodInfo;
25 import gov.nasa.jpf.vm.ObjRef;
26 import gov.nasa.jpf.vm.StackFrame;
27 import gov.nasa.jpf.vm.ThreadInfo;
28 import gov.nasa.jpf.vm.Types;
29 import gov.nasa.jpf.vm.choice.InvocationCG;
30
31 import java.util.List;
32
33 /**
34  * a sytnthetic INVOKE instruction that gets it's parameters from an
35  * InvocationCG. Whoever uses this better makes sure the frame this
36  * executes in has enough operand space (e.g. a DirectCallStackFrame).
37  * 
38  */
39 public class INVOKECG extends Instruction implements JVMInstruction {
40
41   List<Invocation>  invokes;
42   JVMInvokeInstruction realInvoke;
43
44   public INVOKECG(List<Invocation> invokes){
45     this.invokes = invokes;
46   }
47
48
49   public void setInvokes(List<Invocation> invokes) {
50     this.invokes = invokes;
51   }
52   
53   @Override
54   public Instruction execute (ThreadInfo ti) {
55     
56     if (!ti.isFirstStepInsn()) {
57       InvocationCG cg = new InvocationCG( "INVOKECG", invokes);
58       if (ti.getVM().setNextChoiceGenerator(cg)){
59         return this;
60       }
61       
62     } else {
63       InvocationCG cg = ti.getVM().getCurrentChoiceGenerator( "INVOKECG", InvocationCG.class);
64       assert (cg != null) : "no current InvocationCG";
65
66       Invocation call = cg.getNextChoice();
67       MethodInfo callee = call.getMethodInfo();
68       JVMInstructionFactory insnFactory = JVMInstructionFactory.getFactory();
69
70       String clsName = callee.getClassInfo().getName();
71       String mthName = callee.getName();
72       String signature = callee.getSignature();
73
74       Instruction realInvoke;
75       if (callee.isStatic()){
76         realInvoke = insnFactory.invokestatic(clsName, mthName, signature);
77       } else {
78         realInvoke = insnFactory.invokevirtual(clsName, mthName, signature);
79       }
80       
81       pushArguments(ti, call.getArguments(), call.getAttrs());
82       
83       return realInvoke;
84     }
85
86     return getNext();
87   }
88
89   void pushArguments (ThreadInfo ti, Object[] args, Object[] attrs){
90     StackFrame frame = ti.getModifiableTopFrame();
91     
92     if (args != null){
93       for (int i=0; i<args.length; i++){
94         Object a = args[i];
95         boolean isLong = false;
96         
97         if (a != null){
98           if (a instanceof ObjRef){
99             frame.pushRef(((ObjRef)a).getReference());
100           } else if (a instanceof Boolean){
101             frame.push((Boolean)a ? 1 : 0, false);
102           } else if (a instanceof Integer){
103             frame.push((Integer)a, false);
104           } else if (a instanceof Long){
105             frame.pushLong((Long)a);
106             isLong = true;
107           } else if (a instanceof Double){
108             frame.pushLong(Types.doubleToLong((Double)a));
109             isLong = true;
110           } else if (a instanceof Byte){
111             frame.push((Byte)a, false);
112           } else if (a instanceof Short){
113             frame.push((Short)a, false);
114           } else if (a instanceof Float){
115             frame.push(Types.floatToInt((Float)a), false);
116           }
117         }
118
119         if (attrs != null && attrs[i] != null){
120           if (isLong){
121             frame.setLongOperandAttr(attrs[i]);
122           } else {
123             frame.setOperandAttr(attrs[i]);
124           }
125         }
126       }
127     }
128   }
129   
130   @Override
131   public boolean isExtendedInstruction() {
132     return true;
133   }
134
135   public static final int OPCODE = 258;
136
137   @Override
138   public int getByteCode () {
139     return OPCODE;
140   }
141   
142   @Override
143   public void accept(JVMInstructionVisitor insVisitor) {
144           insVisitor.visit(this);
145   }
146   
147 }