Initial import
[jpf-core.git] / src / main / gov / nasa / jpf / jvm / JVMStackFrame.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;
20
21 import gov.nasa.jpf.util.FixedBitSet;
22 import gov.nasa.jpf.vm.MethodInfo;
23 import gov.nasa.jpf.vm.StackFrame;
24 import gov.nasa.jpf.vm.ThreadInfo;
25
26 /**
27  * a stackframe that is used for executing Java bytecode, supporting both
28  * locals and an operand stack. This is essentially the JVm stack machine
29  * implementation
30  */
31 public class JVMStackFrame extends StackFrame {
32
33   public JVMStackFrame (MethodInfo callee){
34     super( callee);
35   }
36   
37   /**
38    * creates callerSlots dummy Stackframe for testing of operand/local operations
39    * NOTE - TESTING ONLY! this does not have callerSlots MethodInfo
40    */
41   protected JVMStackFrame (int nLocals, int nOperands){
42     super( nLocals, nOperands);
43   }
44   
45   /**
46    * this sets up arguments from a bytecode caller 
47    */
48   protected void setCallArguments (ThreadInfo ti){
49     StackFrame caller = ti.getTopFrame();
50     MethodInfo miCallee = mi;
51     int nArgSlots = miCallee.getArgumentsSize();
52     
53     if (nArgSlots > 0){
54       int[] calleeSlots = slots;
55       FixedBitSet calleeRefs = isRef;
56       int[] callerSlots = caller.getSlots();
57       FixedBitSet callerRefs = caller.getReferenceMap();
58
59       for (int i = 0, j = caller.getTopPos() - nArgSlots + 1; i < nArgSlots; i++, j++) {
60         calleeSlots[i] = callerSlots[j];
61         if (callerRefs.get(j)) {
62           calleeRefs.set(i);
63         }
64         Object a = caller.getSlotAttr(j);
65         if (a != null) {
66           setSlotAttr(i, a);
67         }
68       }
69
70       if (!miCallee.isStatic()) {
71         thisRef = calleeSlots[0];
72       }
73     }
74   }
75
76   @Override
77   public void setExceptionReference (int exRef){
78     clearOperandStack();
79     pushRef( exRef);
80   }
81   
82   //--- these are for setting up arguments from a VM / listener caller
83
84   /*
85    * to be used to initialize locals of a stackframe (only required for explicit construction without a caller,
86    * otherwise the Stackframe ctor/invoke insn will take care of copying the values from its caller)
87    */
88   @Override
89   public void setArgumentLocal (int idx, int v, Object attr){
90     setLocalVariable( idx, v);
91     if (attr != null){
92       setLocalAttr( idx, attr);
93     }
94   }
95   @Override
96   public void setReferenceArgumentLocal (int idx, int ref, Object attr){
97     setLocalReferenceVariable( idx, ref);
98     if (attr != null){
99       setLocalAttr( idx, attr);
100     }
101   }
102   @Override
103   public void setLongArgumentLocal (int idx, long v, Object attr){
104     setLongLocalVariable( idx, v);
105     if (attr != null){
106       setLocalAttr( idx, attr);
107     }
108   }
109
110 }