KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashSet JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Set JavaDoc;
36
37 import oracle.toplink.libraries.asm.Constants;
38 import oracle.toplink.libraries.asm.Type;
39 import oracle.toplink.libraries.asm.tree.AbstractInsnNode;
40 import oracle.toplink.libraries.asm.tree.FieldInsnNode;
41 import oracle.toplink.libraries.asm.tree.LdcInsnNode;
42 import oracle.toplink.libraries.asm.tree.MethodInsnNode;
43
44 /**
45  * An {@link Interpreter} for {@link DataflowValue} values.
46  *
47  * @author Eric Bruneton
48  */

49
50 public class DataflowInterpreter implements Constants, Interpreter {
51
52   public Value newValue (final Type type) {
53     return new DataflowValue(type == null ? 1 : type.getSize());
54   }
55
56   public Value newOperation (final AbstractInsnNode insn) {
57     int size;
58     switch (insn.getOpcode()) {
59       case LCONST_0:
60       case LCONST_1:
61       case DCONST_0:
62       case DCONST_1:
63         size = 2;
64         break;
65       case LDC:
66         Object JavaDoc cst = ((LdcInsnNode)insn).cst;
67         size = cst instanceof Long JavaDoc || cst instanceof Double JavaDoc ? 2 : 1;
68         break;
69       case GETSTATIC:
70         size = Type.getType(((FieldInsnNode)insn).desc).getSize();
71         break;
72       default:
73         size = 1;
74     }
75     return new DataflowValue(size, insn);
76   }
77
78   public Value copyOperation (final AbstractInsnNode insn, final Value value) {
79     return new DataflowValue(value.getSize(), insn);
80   }
81
82   public Value unaryOperation (final AbstractInsnNode insn, final Value value) {
83     int size;
84     switch (insn.getOpcode()) {
85       case LNEG:
86       case DNEG:
87       case I2L:
88       case I2D:
89       case L2D:
90       case F2L:
91       case F2D:
92       case D2L:
93         size = 2;
94         break;
95       case GETFIELD:
96         size = Type.getType(((FieldInsnNode)insn).desc).getSize();
97         break;
98       default:
99         size = 1;
100     }
101     return new DataflowValue(size, insn);
102   }
103
104   public Value binaryOperation (
105     final AbstractInsnNode insn,
106     final Value value1,
107     final Value value2)
108   {
109     int size;
110     switch (insn.getOpcode()) {
111       case LALOAD:
112       case DALOAD:
113       case LADD:
114       case DADD:
115       case LSUB:
116       case DSUB:
117       case LMUL:
118       case DMUL:
119       case LDIV:
120       case DDIV:
121       case LREM:
122       case DREM:
123       case LSHL:
124       case LSHR:
125       case LUSHR:
126       case LAND:
127       case LOR:
128       case LXOR:
129         size = 2;
130         break;
131       default:
132         size = 1;
133     }
134     return new DataflowValue(size, insn);
135   }
136
137   public Value ternaryOperation (
138     final AbstractInsnNode insn,
139     final Value value1,
140     final Value value2,
141     final Value value3)
142   {
143     return new DataflowValue(1, insn);
144   }
145   
146   public Value naryOperation (final AbstractInsnNode insn, final List JavaDoc values) {
147     int size;
148     if (insn.getOpcode() == MULTIANEWARRAY) {
149       size = 1;
150     } else {
151       size = Type.getReturnType(((MethodInsnNode)insn).desc).getSize();
152     }
153     return new DataflowValue(size, insn);
154   }
155
156   public Value merge (final Value v, final Value w) {
157     DataflowValue dv = (DataflowValue)v;
158     DataflowValue dw = (DataflowValue)w;
159     if (dv.size != dw.size || !dv.insns.equals(dw.insns)) {
160       Set JavaDoc s = new HashSet JavaDoc();
161       s.addAll(dv.insns);
162       s.addAll(dw.insns);
163       return new DataflowValue(Math.min(dv.size, dw.size), s);
164     }
165     return v;
166   }
167 }
168
Popular Tags