Initial import
[jpf-core.git] / src / main / gov / nasa / jpf / jvm / bytecode / GETFIELD.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.ElementInfo;
21 import gov.nasa.jpf.vm.FieldInfo;
22 import gov.nasa.jpf.vm.Instruction;
23 import gov.nasa.jpf.vm.MJIEnv;
24 import gov.nasa.jpf.vm.Scheduler;
25 import gov.nasa.jpf.vm.StackFrame;
26 import gov.nasa.jpf.vm.ThreadInfo;
27 import gov.nasa.jpf.vm.bytecode.ReadInstruction;
28
29
30 /**
31  * Fetch field from object
32  * ..., objectref => ..., value
33  */
34 public class GETFIELD extends JVMInstanceFieldInstruction implements ReadInstruction {
35
36   public GETFIELD (String fieldName, String classType, String fieldDescriptor){
37     super(fieldName, classType, fieldDescriptor);
38   }
39   
40   
41   @Override
42   public int getObjectSlot (StackFrame frame){
43     return frame.getTopPos();
44   }
45   
46   @Override
47   public Instruction execute (ThreadInfo ti) {
48     StackFrame frame = ti.getModifiableTopFrame();
49     int objRef = frame.peek(); // don't pop yet, we might re-enter
50     lastThis = objRef;
51
52     //--- check for obvious exceptions
53     if (objRef == MJIEnv.NULL) {
54       return ti.createAndThrowException("java.lang.NullPointerException",
55               "referencing field '" + fname + "' on null object");
56     }
57
58     ElementInfo eiFieldOwner = ti.getElementInfo(objRef);
59     FieldInfo fieldInfo = getFieldInfo();
60     if (fieldInfo == null) {
61       return ti.createAndThrowException("java.lang.NoSuchFieldError",
62               "referencing field '" + fname + "' in " + eiFieldOwner);
63     }
64
65     //--- check for potential transition breaks (be aware everything above gets re-executed)
66     Scheduler scheduler = ti.getScheduler();
67     if (scheduler.canHaveSharedObjectCG( ti, this, eiFieldOwner, fieldInfo)){
68       eiFieldOwner = scheduler.updateObjectSharedness( ti, eiFieldOwner, fieldInfo);
69       if (scheduler.setsSharedObjectCG( ti, this, eiFieldOwner, fieldInfo)){
70         return this; // re-execute
71       }
72     }
73     
74     frame.pop(); // Ok, now we can remove the object ref from the stack
75     Object fieldAttr = eiFieldOwner.getFieldAttr(fieldInfo);
76
77     // We could encapsulate the push in ElementInfo, but not the GET, so we keep it at the same level
78     if (fieldInfo.getStorageSize() == 1) { // 1 slotter
79       int ival = eiFieldOwner.get1SlotField(fieldInfo);
80       lastValue = ival;
81       
82       if (fieldInfo.isReference()){
83         frame.pushRef(ival);
84         
85       } else {
86         frame.push(ival);
87       }
88       
89       if (fieldAttr != null) {
90         frame.setOperandAttr(fieldAttr);
91       }
92
93     } else {  // 2 slotter
94       long lval = eiFieldOwner.get2SlotField(fieldInfo);
95       lastValue = lval;
96
97       frame.pushLong( lval);
98       if (fieldAttr != null) {
99         frame.setLongOperandAttr(fieldAttr);
100       }
101     }
102
103     return getNext(ti);
104   }
105
106   @Override
107   public ElementInfo peekElementInfo (ThreadInfo ti) {
108     StackFrame frame = ti.getTopFrame();
109     int objRef = frame.peek();
110     ElementInfo ei = ti.getElementInfo(objRef);
111     return ei;
112   }
113
114   @Override
115   public boolean isMonitorEnterPrologue(){
116     return GetHelper.isMonitorEnterPrologue(mi, insnIndex);
117   }
118   
119   @Override
120   public int getLength() {
121     return 3; // opcode, index1, index2
122   }
123
124   @Override
125   public int getByteCode () {
126     return 0xB4;
127   }
128
129   @Override
130   public boolean isRead() {
131     return true;
132   }
133
134   @Override
135   public void accept(JVMInstructionVisitor insVisitor) {
136           insVisitor.visit(this);
137   }
138 }