KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > libraries > asm > tree > analysis > BasicInterpreter


1 /***
2  * ASM: a very small and fast Java bytecode manipulation framework
3  * Copyright (c) 2000,2002,2003 INRIA, France Telecom
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the copyright holders nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31 package oracle.toplink.libraries.asm.tree.analysis;
32
33 import java.util.List JavaDoc;
34
35 import oracle.toplink.libraries.asm.Constants;
36 import oracle.toplink.libraries.asm.Type;
37 import oracle.toplink.libraries.asm.tree.AbstractInsnNode;
38 import oracle.toplink.libraries.asm.tree.FieldInsnNode;
39 import oracle.toplink.libraries.asm.tree.IntInsnNode;
40 import oracle.toplink.libraries.asm.tree.LdcInsnNode;
41 import oracle.toplink.libraries.asm.tree.MethodInsnNode;
42 import oracle.toplink.libraries.asm.tree.MultiANewArrayInsnNode;
43 import oracle.toplink.libraries.asm.tree.TypeInsnNode;
44
45 /**
46  * An {@link Interpreter} for {@link BasicValue} values.
47  *
48  * @author Eric Bruneton
49  * @author Bing Ran
50  */

51
52 public class BasicInterpreter implements Constants, Interpreter {
53
54   public Value newValue (final Type type) {
55     if (type == null) {
56       return BasicValue.UNINITIALIZED_VALUE;
57     }
58     switch (type.getSort()) {
59       case Type.VOID:
60         return null;
61       case Type.BOOLEAN:
62       case Type.CHAR:
63       case Type.BYTE:
64       case Type.SHORT:
65       case Type.INT:
66         return BasicValue.INT_VALUE;
67       case Type.FLOAT:
68         return BasicValue.FLOAT_VALUE;
69       case Type.LONG:
70         return BasicValue.LONG_VALUE;
71       case Type.DOUBLE:
72         return BasicValue.DOUBLE_VALUE;
73       case Type.ARRAY:
74       case Type.OBJECT:
75         return BasicValue.REFERENCE_VALUE;
76       default:
77         throw new RuntimeException JavaDoc("Internal error.");
78     }
79   }
80
81   public Value newOperation (final AbstractInsnNode insn) {
82     switch (insn.getOpcode()) {
83       case ACONST_NULL:
84         return newValue(Type.getType("Lnull;"));
85       case ICONST_M1:
86       case ICONST_0:
87       case ICONST_1:
88       case ICONST_2:
89       case ICONST_3:
90       case ICONST_4:
91       case ICONST_5:
92         return BasicValue.INT_VALUE;
93       case LCONST_0:
94       case LCONST_1:
95         return BasicValue.LONG_VALUE;
96       case FCONST_0:
97       case FCONST_1:
98       case FCONST_2:
99         return BasicValue.FLOAT_VALUE;
100       case DCONST_0:
101       case DCONST_1:
102         return BasicValue.DOUBLE_VALUE;
103       case BIPUSH:
104       case SIPUSH:
105         return BasicValue.INT_VALUE;
106       case LDC:
107         Object JavaDoc cst = ((LdcInsnNode)insn).cst;
108         if (cst instanceof Integer JavaDoc) {
109           return BasicValue.INT_VALUE;
110         } else if (cst instanceof Float JavaDoc) {
111           return BasicValue.FLOAT_VALUE;
112         } else if (cst instanceof Long JavaDoc) {
113           return BasicValue.LONG_VALUE;
114         } else if (cst instanceof Double JavaDoc) {
115           return BasicValue.DOUBLE_VALUE;
116         } else {
117           return newValue(Type.getType(cst.getClass()));
118         }
119       case JSR:
120         return BasicValue.RETURNADDRESS_VALUE;
121       case GETSTATIC:
122         return newValue(Type.getType(((FieldInsnNode)insn).desc));
123       case NEW:
124         return newValue(Type.getType("L" + ((TypeInsnNode)insn).desc + ";"));
125       default:
126         throw new RuntimeException JavaDoc("Internal error.");
127     }
128   }
129
130   public Value copyOperation (final AbstractInsnNode insn, final Value value)
131     throws AnalyzerException
132   {
133     return value;
134   }
135
136   public Value unaryOperation (final AbstractInsnNode insn, final Value value)
137     throws AnalyzerException
138   {
139     switch (insn.getOpcode()) {
140       case INEG:
141       case IINC:
142       case L2I:
143       case F2I:
144       case D2I:
145       case I2B:
146       case I2C:
147       case I2S:
148         return BasicValue.INT_VALUE;
149       case FNEG:
150       case I2F:
151       case L2F:
152       case D2F:
153         return BasicValue.FLOAT_VALUE;
154       case LNEG:
155       case I2L:
156       case F2L:
157       case D2L:
158         return BasicValue.LONG_VALUE;
159       case DNEG:
160       case I2D:
161       case L2D:
162       case F2D:
163         return BasicValue.DOUBLE_VALUE;
164       case IFEQ:
165       case IFNE:
166       case IFLT:
167       case IFGE:
168       case IFGT:
169       case IFLE:
170       case TABLESWITCH:
171       case LOOKUPSWITCH:
172       case IRETURN:
173       case LRETURN:
174       case FRETURN:
175       case DRETURN:
176       case ARETURN:
177       case PUTSTATIC:
178         return null;
179       case GETFIELD:
180         return newValue(Type.getType(((FieldInsnNode)insn).desc));
181       case NEWARRAY:
182         switch (((IntInsnNode)insn).operand) {
183           case T_BOOLEAN:
184           case T_CHAR:
185           case T_BYTE:
186           case T_SHORT:
187           case T_INT:
188             return newValue(Type.getType("[I"));
189           case T_FLOAT:
190             return newValue(Type.getType("[F"));
191           case T_DOUBLE:
192             return newValue(Type.getType("[D"));
193           case T_LONG:
194             return newValue(Type.getType("[J"));
195           default:
196             throw new AnalyzerException("Invalid array type");
197         }
198       case ANEWARRAY:
199         String JavaDoc desc = ((TypeInsnNode)insn).desc;
200         if (desc.charAt(0) == '[') {
201           return newValue(Type.getType("[" + desc));
202         } else {
203           return newValue(Type.getType("[L" + desc + ";"));
204         }
205       case ARRAYLENGTH:
206         return BasicValue.INT_VALUE;
207       case ATHROW:
208         return null;
209       case CHECKCAST:
210         desc = ((TypeInsnNode)insn).desc;
211         if (desc.charAt(0) == '[') {
212           return newValue(Type.getType(desc));
213         } else {
214           return newValue(Type.getType("L" + desc + ";"));
215         }
216       case INSTANCEOF:
217         return BasicValue.INT_VALUE;
218       case MONITORENTER:
219       case MONITOREXIT:
220       case IFNULL:
221       case IFNONNULL:
222         return null;
223       default:
224         throw new RuntimeException JavaDoc("Internal error.");
225     }
226   }
227
228   public Value binaryOperation (
229     final AbstractInsnNode insn,
230     final Value value1,
231     final Value value2) throws AnalyzerException
232   {
233     switch (insn.getOpcode()) {
234       case IALOAD:
235       case BALOAD:
236       case CALOAD:
237       case SALOAD:
238       case IADD:
239       case ISUB:
240       case IMUL:
241       case IDIV:
242       case IREM:
243       case ISHL:
244       case ISHR:
245       case IUSHR:
246       case IAND:
247       case IOR:
248       case IXOR:
249         return BasicValue.INT_VALUE;
250       case FALOAD:
251       case FADD:
252       case FSUB:
253       case FMUL:
254       case FDIV:
255       case FREM:
256         return BasicValue.FLOAT_VALUE;
257       case LALOAD:
258       case LADD:
259       case LSUB:
260       case LMUL:
261       case LDIV:
262       case LREM:
263       case LSHL:
264       case LSHR:
265       case LUSHR:
266       case LAND:
267       case LOR:
268       case LXOR:
269         return BasicValue.LONG_VALUE;
270       case DALOAD:
271       case DADD:
272       case DSUB:
273       case DMUL:
274       case DDIV:
275       case DREM:
276         return BasicValue.DOUBLE_VALUE;
277       case AALOAD:
278         Type t = ((BasicValue)value1).getType();
279         if (t != null && t.getSort() == Type.ARRAY) {
280           return newValue(t.getElementType());
281         } else {
282           return BasicValue.REFERENCE_VALUE;
283         }
284       case LCMP:
285       case FCMPL:
286       case FCMPG:
287       case DCMPL:
288       case DCMPG:
289         return BasicValue.INT_VALUE;
290       case IF_ICMPEQ:
291       case IF_ICMPNE:
292       case IF_ICMPLT:
293       case IF_ICMPGE:
294       case IF_ICMPGT:
295       case IF_ICMPLE:
296       case IF_ACMPEQ:
297       case IF_ACMPNE:
298       case PUTFIELD:
299         return null;
300       default:
301         throw new RuntimeException JavaDoc("Internal error.");
302     }
303   }
304
305   public Value ternaryOperation (
306     final AbstractInsnNode insn,
307     final Value value1,
308     final Value value2,
309     final Value value3) throws AnalyzerException
310   {
311     return null;
312   }
313   
314   public Value naryOperation (final AbstractInsnNode insn, final List JavaDoc values)
315     throws AnalyzerException
316   {
317     if (insn.getOpcode() == MULTIANEWARRAY) {
318       return newValue(Type.getType(((MultiANewArrayInsnNode)insn).desc));
319     } else {
320       return newValue(Type.getReturnType(((MethodInsnNode)insn).desc));
321     }
322   }
323
324   public Value merge (final Value v, final Value w) {
325     if (!v.equals(w)) {
326       return BasicValue.UNINITIALIZED_VALUE;
327     }
328     return v;
329   }
330 }
331
Popular Tags