Initial import
[jpf-core.git] / src / main / gov / nasa / jpf / jvm / bytecode / INSTANCEOF.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.Instruction;
21 import gov.nasa.jpf.vm.LoadOnJPFRequired;
22 import gov.nasa.jpf.vm.MJIEnv;
23 import gov.nasa.jpf.vm.StackFrame;
24 import gov.nasa.jpf.vm.ThreadInfo;
25 import gov.nasa.jpf.vm.Types;
26
27
28 /**
29  * Determine if object is of given type
30  * ..., objectref => ..., result
31  */
32 public class INSTANCEOF extends Instruction implements JVMInstruction {
33   private String type;
34
35
36   /**
37    * typeName is of a/b/C notation
38    */
39   public INSTANCEOF (String typeName){
40     type = Types.getTypeSignature(typeName, false);
41   }
42
43   @Override
44   public Instruction execute (ThreadInfo ti) {
45     if(Types.isReferenceSignature(type)) {
46       String t;
47       if(Types.isArray(type)) {
48         // retrieve the component terminal
49         t = Types.getComponentTerminal(type);
50       } else {
51         t = type;
52       }
53
54       // resolve the referenced class
55       try {
56         ti.resolveReferencedClass(t);
57       } catch(LoadOnJPFRequired lre) {
58         return ti.getPC();
59       }
60     }
61
62     StackFrame frame = ti.getModifiableTopFrame();
63     int objref = frame.pop();
64
65     if (objref == MJIEnv.NULL) {
66       frame.push(0);
67     } else if (ti.getElementInfo(objref).instanceOf(type)) {
68       frame.push(1);
69     } else {
70       frame.push(0);
71     }
72
73     return getNext(ti);
74   }
75   
76   public String getType() {
77           return type;
78   }
79
80   @Override
81   public int getLength() {
82     return 3; // opcode, index1, index2
83   }
84   
85   @Override
86   public int getByteCode () {
87     return 0xC1;
88   }
89   
90   @Override
91   public void accept(JVMInstructionVisitor insVisitor) {
92           insVisitor.visit(this);
93   }
94 }