Initial import
[jpf-core.git] / src / main / gov / nasa / jpf / jvm / bytecode / GETSTATIC.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.ClassInfo;
21 import gov.nasa.jpf.vm.ElementInfo;
22 import gov.nasa.jpf.vm.FieldInfo;
23 import gov.nasa.jpf.vm.Instruction;
24 import gov.nasa.jpf.vm.LoadOnJPFRequired;
25 import gov.nasa.jpf.vm.Scheduler;
26 import gov.nasa.jpf.vm.StackFrame;
27 import gov.nasa.jpf.vm.ThreadInfo;
28 import gov.nasa.jpf.vm.bytecode.ReadInstruction;
29
30
31 /**
32  * Get static fieldInfo from class
33  * ..., => ..., value 
34  */
35 public class GETSTATIC extends JVMStaticFieldInstruction  implements ReadInstruction {
36
37   public GETSTATIC(String fieldName, String clsDescriptor, String fieldDescriptor){
38     super(fieldName, clsDescriptor, fieldDescriptor);
39   }
40
41   @Override
42   public Instruction execute (ThreadInfo ti) {
43     FieldInfo fieldInfo;
44
45     //--- check if this causes a class load by a user defined classloader
46     try {
47       fieldInfo = getFieldInfo();
48     } catch (LoadOnJPFRequired lre) {
49       return ti.getPC();
50     }
51     
52     if (fieldInfo == null) {
53       return ti.createAndThrowException("java.lang.NoSuchFieldError",
54               (className + '.' + fname));
55     }
56
57     //--- check if this has to trigger class initialization
58     ClassInfo ciField = fieldInfo.getClassInfo();
59     if (!mi.isClinit(ciField) && ciField.initializeClass(ti)) {
60       // note - this returns the next insn in the topmost clinit that just got pushed
61       return ti.getPC();
62     }
63     ElementInfo eiFieldOwner = ciField.getStaticElementInfo();
64
65     //--- check if this breaks the transition
66     Scheduler scheduler = ti.getScheduler();
67     if (scheduler.canHaveSharedClassCG( ti, this, eiFieldOwner, fieldInfo)){
68       eiFieldOwner = scheduler.updateClassSharedness(ti, eiFieldOwner, fieldInfo);
69       if (scheduler.setsSharedClassCG( ti, this, eiFieldOwner, fieldInfo)){
70         return this; // re-execute
71       }
72     }
73         
74     Object fieldAttr = eiFieldOwner.getFieldAttr(fieldInfo);
75     StackFrame frame = ti.getModifiableTopFrame();
76
77     if (size == 1) {
78       int ival = eiFieldOwner.get1SlotField(fieldInfo);
79       lastValue = ival;
80
81       if (fieldInfo.isReference()) {
82         frame.pushRef(ival);
83       } else {
84         frame.push(ival);
85       }
86       
87       if (fieldAttr != null) {
88         frame.setOperandAttr(fieldAttr);
89       }
90
91     } else {
92       long lval = eiFieldOwner.get2SlotField(fieldInfo);
93       lastValue = lval;
94       
95       frame.pushLong(lval);
96       
97       if (fieldAttr != null) {
98         frame.setLongOperandAttr(fieldAttr);
99       }
100     }
101         
102     return getNext(ti);
103   }
104   
105   @Override
106   public boolean isMonitorEnterPrologue(){
107     return GetHelper.isMonitorEnterPrologue(mi, insnIndex);
108   }
109   
110   @Override
111   public int getLength() {
112     return 3; // opcode, index1, index2
113   }
114   
115   @Override
116   public int getByteCode () {
117     return 0xB2;
118   }
119
120   @Override
121   public boolean isRead() {
122     return true;
123   }
124   
125   @Override
126   public void accept(JVMInstructionVisitor insVisitor) {
127           insVisitor.visit(this);
128   }
129 }