1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.apache.commons.nabla.automatic.analysis; |
18 | |
|
19 | |
import java.util.Iterator; |
20 | |
import java.util.List; |
21 | |
|
22 | |
import org.objectweb.asm.Type; |
23 | |
import org.objectweb.asm.tree.AbstractInsnNode; |
24 | |
import org.objectweb.asm.tree.analysis.AnalyzerException; |
25 | |
import org.objectweb.asm.tree.analysis.BasicInterpreter; |
26 | |
import org.objectweb.asm.tree.analysis.BasicValue; |
27 | |
import org.objectweb.asm.tree.analysis.Value; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
public class TrackingInterpreter extends BasicInterpreter { |
34 | |
|
35 | |
|
36 | |
|
37 | 66 | public TrackingInterpreter() { |
38 | 66 | } |
39 | |
|
40 | |
|
41 | |
@Override |
42 | |
public Value newValue(final Type type) { |
43 | 1054 | return (type == null) ? TrackingValue.UNINITIALIZED_VALUE : wrap(super.newValue(type), null); |
44 | |
} |
45 | |
|
46 | |
|
47 | |
@Override |
48 | |
public Value newOperation(final AbstractInsnNode insn) { |
49 | 32 | return wrap(super.newOperation(insn), insn); |
50 | |
} |
51 | |
|
52 | |
|
53 | |
@Override |
54 | |
public Value unaryOperation(final AbstractInsnNode insn, |
55 | |
final Value value) |
56 | |
throws AnalyzerException { |
57 | 71 | ((TrackingValue) value).addConsumer(insn); |
58 | 71 | return wrap(super.unaryOperation(insn, value), insn); |
59 | |
} |
60 | |
|
61 | |
|
62 | |
@Override |
63 | |
public Value binaryOperation(final AbstractInsnNode insn, |
64 | |
final Value value1, final Value value2) |
65 | |
throws AnalyzerException { |
66 | 34 | ((TrackingValue) value1).addConsumer(insn); |
67 | 34 | ((TrackingValue) value2).addConsumer(insn); |
68 | 34 | return wrap(super.binaryOperation(insn, value1, value2), insn); |
69 | |
} |
70 | |
|
71 | |
|
72 | |
@Override |
73 | |
public Value ternaryOperation(final AbstractInsnNode insn, |
74 | |
final Value value1, final Value value2, |
75 | |
final Value value3) |
76 | |
throws AnalyzerException { |
77 | 0 | ((TrackingValue) value1).addConsumer(insn); |
78 | 0 | ((TrackingValue) value2).addConsumer(insn); |
79 | 0 | ((TrackingValue) value3).addConsumer(insn); |
80 | 0 | return wrap(super.ternaryOperation(insn, value1, value2, value3), insn); |
81 | |
} |
82 | |
|
83 | |
|
84 | |
@SuppressWarnings("unchecked") |
85 | |
@Override |
86 | |
public Value naryOperation(final AbstractInsnNode insn, |
87 | |
final List values) |
88 | |
throws AnalyzerException { |
89 | 43 | for (final Iterator<?> iterator = values.iterator(); iterator.hasNext();) { |
90 | 54 | ((TrackingValue) iterator.next()).addConsumer(insn); |
91 | |
} |
92 | 43 | return wrap(super.naryOperation(insn, values), insn); |
93 | |
} |
94 | |
|
95 | |
|
96 | |
@Override |
97 | |
public Value copyOperation(final AbstractInsnNode insn, |
98 | |
final Value value) |
99 | |
throws AnalyzerException { |
100 | |
|
101 | |
|
102 | 95 | final TrackingValue tv = (TrackingValue) value; |
103 | 95 | tv.addConsumer(insn); |
104 | 95 | tv.addProducer(insn); |
105 | 95 | return value; |
106 | |
} |
107 | |
|
108 | |
|
109 | |
@Override |
110 | |
public Value merge(final Value v, final Value w) { |
111 | |
|
112 | 42 | final TrackingValue tv = (TrackingValue) v; |
113 | 42 | final TrackingValue tw = (TrackingValue) w; |
114 | 42 | TrackingValue.merge(tv, tw); |
115 | |
|
116 | 42 | final BasicValue superMerged = (BasicValue) super.merge(tv.getValue(), tw.getValue()); |
117 | 42 | return (superMerged == tv.getValue()) ? tv : new TrackingValue(superMerged); |
118 | |
|
119 | |
} |
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
private TrackingValue wrap(final Value value, |
127 | |
final AbstractInsnNode producer) { |
128 | |
|
129 | 355 | if (value == null) { |
130 | 68 | return null; |
131 | |
} |
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | 287 | final TrackingValue tv = (value instanceof TrackingValue) ? |
137 | |
(TrackingValue) value : |
138 | |
new TrackingValue((BasicValue) value); |
139 | |
|
140 | 287 | if (producer != null) { |
141 | 112 | tv.addProducer(producer); |
142 | |
} |
143 | |
|
144 | 287 | return tv; |
145 | |
|
146 | |
} |
147 | |
|
148 | |
} |