Fixing a subtle bug: the method isParameterWithType() did not consider arbitrary...
[jpf-core.git] / src / main / gov / nasa / jpf / jvm / JVMNativeStackFrame.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.vm.NativeMethodInfo;
22 import gov.nasa.jpf.vm.NativeStackFrame;
23 import gov.nasa.jpf.vm.StackFrame;
24 import gov.nasa.jpf.vm.ThreadInfo;
25 import gov.nasa.jpf.vm.Types;
26
27 /**
28  * a NativeStackFrame used for calling NativeMethods from Java bytecode
29  */
30 public class JVMNativeStackFrame extends NativeStackFrame {
31
32   public JVMNativeStackFrame (NativeMethodInfo callee){
33     super(callee);
34   }
35   
36   public void setArguments (ThreadInfo ti){
37     StackFrame callerFrame = ti.getTopFrame(); // we are not going to modify it
38     NativeMethodInfo nmi = (NativeMethodInfo) mi;
39     int      nArgs = nmi.getNumberOfArguments();
40     byte[]   argTypes = nmi.getArgumentTypes();
41
42     Object[] a = new Object[nArgs+2];
43
44     int      stackOffset;
45     int      i, j, k;
46     int      ival;
47     long     lval;
48
49     for (i = 0, stackOffset = 0, j = nArgs + 1, k = nArgs - 1;
50          i < nArgs;
51          i++, j--, k--) {
52       switch (argTypes[k]) {
53       case Types.T_BOOLEAN:
54         ival = callerFrame.peek(stackOffset);
55         a[j] = Boolean.valueOf(Types.intToBoolean(ival));
56
57         break;
58
59       case Types.T_BYTE:
60         ival = callerFrame.peek(stackOffset);
61         a[j] = Byte.valueOf((byte) ival);
62
63         break;
64
65       case Types.T_CHAR:
66         ival = callerFrame.peek(stackOffset);
67         a[j] = Character.valueOf((char) ival);
68
69         break;
70
71       case Types.T_SHORT:
72         ival = callerFrame.peek(stackOffset);
73         a[j] = new Short((short) ival);
74
75         break;
76
77       case Types.T_INT:
78         ival = callerFrame.peek(stackOffset);
79         a[j] = new Integer(ival);
80
81         break;
82
83       case Types.T_LONG:
84         lval = callerFrame.peekLong(stackOffset);
85         stackOffset++; // 2 stack words
86         a[j] = new Long(lval);
87
88         break;
89
90       case Types.T_FLOAT:
91         ival = callerFrame.peek(stackOffset);
92         a[j] = new Float(Types.intToFloat(ival));
93
94         break;
95
96       case Types.T_DOUBLE:
97         lval = callerFrame.peekLong(stackOffset);
98         stackOffset++; // 2 stack words
99         a[j] = new Double(Types.longToDouble(lval));
100
101         break;
102
103       default:
104         // NOTE - we have to store T_REFERENCE as an Integer, because
105         // it shows up in our native method as an 'int'
106         ival = callerFrame.peek(stackOffset);
107         a[j] = new Integer(ival);
108       }
109
110       stackOffset++;
111     }
112
113     //--- set  our standard MJI header arguments
114     a[0] = ti.getMJIEnv();
115     
116     if (nmi.isStatic()) {
117       a[1] = new Integer( nmi.getClassInfo().getClassObjectRef());
118     } else {
119       int thisRef = callerFrame.getCalleeThis(nmi);
120       a[1] = new Integer( thisRef);
121       
122       setThis(thisRef);
123     }
124
125     setArgs(a);
126   }
127 }