Initial import
[jpf-core.git] / src / main / gov / nasa / jpf / jvm / bytecode / PUTSTATIC.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.util.InstructionState;
21 import gov.nasa.jpf.vm.ClassInfo;
22 import gov.nasa.jpf.vm.ElementInfo;
23 import gov.nasa.jpf.vm.FieldInfo;
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.Scheduler;
28 import gov.nasa.jpf.vm.StackFrame;
29 import gov.nasa.jpf.vm.ThreadInfo;
30 import gov.nasa.jpf.vm.bytecode.WriteInstruction;
31
32
33 /**
34  * Set static field in class
35  * ..., value => ...
36  */
37 public class PUTSTATIC extends JVMStaticFieldInstruction implements WriteInstruction {
38
39   public PUTSTATIC(String fieldName, String clsDescriptor, String fieldDescriptor){
40     super(fieldName, clsDescriptor, fieldDescriptor);
41   }
42
43   /**
44    * where do we get the value from?
45    * NOTE: only makes sense in a executeInstruction() context 
46    */
47   @Override
48   public int getValueSlot (StackFrame frame){
49     return frame.getTopPos();
50   }
51     
52   @Override
53   public Instruction execute (ThreadInfo ti) {
54     StackFrame frame = ti.getModifiableTopFrame();
55     FieldInfo fieldInfo;
56     
57     //--- check if this causes a class load by a user defined classloader
58     try {
59       fieldInfo = getFieldInfo();
60     } catch (LoadOnJPFRequired lre) {
61       return ti.getPC();
62     }
63     
64     if (fieldInfo == null) {
65       return ti.createAndThrowException("java.lang.NoSuchFieldError", (className + '.' + fname));
66     }
67
68     //--- check if this has to trigger class initialization
69     ClassInfo ciField = fieldInfo.getClassInfo();
70     if (!mi.isClinit(ciField) && ciField.initializeClass(ti)) {
71       return ti.getPC(); // this returns the next insn in the topmost clinit that just got pushed
72     }
73     ElementInfo eiFieldOwner = ciField.getModifiableStaticElementInfo();
74
75     //--- check scheduling point due to shared class access
76     Scheduler scheduler = ti.getScheduler();
77     if (scheduler.canHaveSharedClassCG( ti, this, eiFieldOwner, fieldInfo)){
78       eiFieldOwner = scheduler.updateClassSharedness(ti, eiFieldOwner, fi);
79       if (scheduler.setsSharedClassCG( ti, this, eiFieldOwner, fieldInfo)){
80         return this; // re-execute
81       }
82     }
83     
84     // check if this gets re-executed from a exposure CG (which already did the assignment
85     if (frame.getAndResetFrameAttr(InstructionState.class) == null){
86       lastValue = PutHelper.setField(ti, frame, eiFieldOwner, fieldInfo);
87     }
88       
89     //--- check scheduling point due to exposure through shared class
90     if (isReferenceField()){
91       int refValue = frame.peek();
92       if (refValue != MJIEnv.NULL){
93         ElementInfo eiExposed = ti.getElementInfo(refValue);
94         if (scheduler.setsSharedClassExposureCG(ti,this,eiFieldOwner,fieldInfo,eiExposed)){
95           frame.addFrameAttr( InstructionState.processed);
96           return this; // re-execute AFTER assignment
97         }
98       }        
99     }
100     
101     popOperands(frame);      
102     return getNext();
103   }
104   
105   protected void popOperands (StackFrame frame){
106     if (size == 1){
107       frame.pop(); // .. val => ..
108     } else {
109       frame.pop(2);  // .. highVal, lowVal => ..
110     }
111   }
112   
113   @Override
114   public int getLength() {
115     return 3; // opcode, index1, index2
116   }
117
118   @Override
119   public int getByteCode () {
120     return 0xB3;
121   }
122
123   @Override
124   public boolean isRead() {
125     return false;
126   }
127   
128   @Override
129   public void accept(JVMInstructionVisitor insVisitor) {
130           insVisitor.visit(this);
131   }
132 }