comparison graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/node/SpecializationGroup.java @ 11201:7fc3e1fb3965

Truffle-DSL: specialization group fixes.
author Christian Humer <christian.humer@gmail.com>
date Mon, 05 Aug 2013 19:50:34 +0200
parents 4f52b08bd2f9
children 2868b55001d4
comparison
equal deleted inserted replaced
11200:380e0248f873 11201:7fc3e1fb3965
32 * and generic execute methods. 32 * and generic execute methods.
33 */ 33 */
34 public final class SpecializationGroup { 34 public final class SpecializationGroup {
35 35
36 private final List<String> assumptions; 36 private final List<String> assumptions;
37 private final List<TypeData> typeGuards; 37 private final List<TypeGuard> typeGuards;
38 private final List<GuardData> guards; 38 private final List<GuardData> guards;
39 39
40 private final SpecializationData specialization; 40 private final SpecializationData specialization;
41 private final List<SpecializationGroup> children = new ArrayList<>(); 41 private final List<SpecializationGroup> children = new ArrayList<>();
42 42
49 this.specialization = data; 49 this.specialization = data;
50 50
51 this.assumptions.addAll(data.getAssumptions()); 51 this.assumptions.addAll(data.getAssumptions());
52 Signature sig = data.getSignature(); 52 Signature sig = data.getSignature();
53 for (int i = 1; i < sig.size(); i++) { 53 for (int i = 1; i < sig.size(); i++) {
54 typeGuards.add(sig.get(i)); 54 typeGuards.add(new TypeGuard(sig.get(i), i - 1));
55 } 55 }
56 this.guards.addAll(data.getGuards()); 56 this.guards.addAll(data.getGuards());
57 } 57 }
58 58
59 public SpecializationGroup(List<SpecializationGroup> children, List<String> assumptionMatches, List<TypeData> typeGuardsMatches, List<GuardData> guardMatches) { 59 public SpecializationGroup(List<SpecializationGroup> children, List<String> assumptionMatches, List<TypeGuard> typeGuardsMatches, List<GuardData> guardMatches) {
60 this.assumptions = assumptionMatches; 60 this.assumptions = assumptionMatches;
61 this.typeGuards = typeGuardsMatches; 61 this.typeGuards = typeGuardsMatches;
62 this.guards = guardMatches; 62 this.guards = guardMatches;
63 this.specialization = null; 63 this.specialization = null;
64 updateChildren(children); 64 updateChildren(children);
65 } 65 }
66 66
67 public TypeGuard findTypeGuard(int signatureIndex) {
68 for (TypeGuard guard : typeGuards) {
69 if (guard.getSignatureIndex() == signatureIndex) {
70 return guard;
71 }
72 }
73 return null;
74 }
75
76 public GuardData getElseConnectableGuard() {
77 if (!getTypeGuards().isEmpty() || !getAssumptions().isEmpty()) {
78 return null;
79 }
80 SpecializationGroup previousGroup = getPreviousGroup();
81 if (previousGroup != null && getGuards().size() >= 1 && previousGroup.getGuards().size() == 1) {
82 GuardData guard = getGuards().get(0);
83 GuardData previousGuard = previousGroup.getGuards().get(0);
84
85 if (guard.getMethod().equals(previousGuard.getMethod())) {
86 assert guard.isNegated() != previousGuard.isNegated();
87 return guard;
88 }
89 }
90 return null;
91 }
92
67 private void updateChildren(List<SpecializationGroup> childs) { 93 private void updateChildren(List<SpecializationGroup> childs) {
68 if (!children.isEmpty()) { 94 if (!children.isEmpty()) {
69 children.clear(); 95 children.clear();
70 } 96 }
71 this.children.addAll(childs); 97 this.children.addAll(childs);
72 for (SpecializationGroup child : childs) { 98 for (SpecializationGroup child : childs) {
73 child.parent = this; 99 child.parent = this;
74 } 100 }
75 } 101 }
76 102
77 public int getTypeGuardOffset() {
78 return (parent != null ? parent.getTypeGuardOffsetRec() : 0);
79 }
80
81 private int getTypeGuardOffsetRec() {
82 return typeGuards.size() + (parent != null ? parent.getTypeGuardOffsetRec() : 0);
83 }
84
85 public SpecializationGroup getParent() { 103 public SpecializationGroup getParent() {
86 return parent; 104 return parent;
87 } 105 }
88 106
89 public List<String> getAssumptions() { 107 public List<String> getAssumptions() {
90 return assumptions; 108 return assumptions;
91 } 109 }
92 110
93 public List<TypeData> getTypeGuards() { 111 public List<TypeGuard> getTypeGuards() {
94 return typeGuards; 112 return typeGuards;
95 } 113 }
96 114
97 public List<GuardData> getGuards() { 115 public List<GuardData> getGuards() {
98 return guards; 116 return guards;
113 if (groups.size() == 1) { 131 if (groups.size() == 1) {
114 return null; 132 return null;
115 } 133 }
116 134
117 List<String> assumptionMatches = new ArrayList<>(); 135 List<String> assumptionMatches = new ArrayList<>();
118 List<TypeData> typeGuardsMatches = new ArrayList<>(); 136 List<TypeGuard> typeGuardsMatches = new ArrayList<>();
119 List<GuardData> guardMatches = new ArrayList<>(); 137 List<GuardData> guardMatches = new ArrayList<>();
120 138
121 SpecializationGroup first = groups.get(0); 139 SpecializationGroup first = groups.get(0);
122 List<SpecializationGroup> others = groups.subList(1, groups.size()); 140 List<SpecializationGroup> others = groups.subList(1, groups.size());
123 141
129 } 147 }
130 } 148 }
131 assumptionMatches.add(assumption); 149 assumptionMatches.add(assumption);
132 } 150 }
133 151
134 int typeGuardIndex = 0; 152 outer: for (TypeGuard typeGuard : first.typeGuards) {
135 outer: for (TypeData typeGuard : first.typeGuards) {
136 for (SpecializationGroup other : others) { 153 for (SpecializationGroup other : others) {
137 if (typeGuardIndex >= other.typeGuards.size()) { 154 if (!other.typeGuards.contains(typeGuard)) {
138 break outer; 155 // type guards can be combined unordered
156 continue outer;
139 } 157 }
140
141 if (!other.typeGuards.get(typeGuardIndex).equals(typeGuard)) {
142 break outer;
143 }
144 } 158 }
145 typeGuardsMatches.add(typeGuard); 159 typeGuardsMatches.add(typeGuard);
146 typeGuardIndex++;
147 } 160 }
148 161
149 outer: for (GuardData guard : first.guards) { 162 outer: for (GuardData guard : first.guards) {
150 for (SpecializationGroup other : others) { 163 for (SpecializationGroup other : others) {
151 if (!other.guards.contains(guard)) { 164 if (!other.guards.contains(guard)) {
160 return null; 173 return null;
161 } 174 }
162 175
163 for (SpecializationGroup group : groups) { 176 for (SpecializationGroup group : groups) {
164 group.assumptions.removeAll(assumptionMatches); 177 group.assumptions.removeAll(assumptionMatches);
165 group.typeGuards.subList(0, typeGuardIndex).clear(); 178 group.typeGuards.removeAll(typeGuardsMatches);
166 group.guards.removeAll(guardMatches); 179 group.guards.removeAll(guardMatches);
167 } 180 }
168 181
169 List<SpecializationGroup> newChildren = new ArrayList<>(groups); 182 List<SpecializationGroup> newChildren = new ArrayList<>(groups);
170 return new SpecializationGroup(newChildren, assumptionMatches, typeGuardsMatches, guardMatches); 183 return new SpecializationGroup(newChildren, assumptionMatches, typeGuardsMatches, guardMatches);
171 } 184 }
172 185
173 public static List<SpecializationGroup> create(List<SpecializationData> specializations) { 186 public static SpecializationGroup create(List<SpecializationData> specializations) {
174 List<SpecializationGroup> groups = new ArrayList<>(); 187 List<SpecializationGroup> groups = new ArrayList<>();
175 for (SpecializationData specialization : specializations) { 188 for (SpecializationData specialization : specializations) {
176 groups.add(new SpecializationGroup(specialization)); 189 groups.add(new SpecializationGroup(specialization));
177 } 190 }
178 return createCombinationalGroups(groups); 191 return new SpecializationGroup(createCombinationalGroups(groups), Collections.<String> emptyList(), Collections.<TypeGuard> emptyList(), Collections.<GuardData> emptyList());
179 } 192 }
180 193
181 @Override 194 @Override
182 public String toString() { 195 public String toString() {
183 return "SpecializationGroup [assumptions=" + assumptions + ", typeGuards=" + typeGuards + ", guards=" + guards + "]"; 196 return "SpecializationGroup [assumptions=" + assumptions + ", typeGuards=" + typeGuards + ", guards=" + guards + "]";
236 max = Math.max(max, childGroup.getMaxSpecializationIndex()); 249 max = Math.max(max, childGroup.getMaxSpecializationIndex());
237 } 250 }
238 return max; 251 return max;
239 } 252 }
240 } 253 }
254
255 public static final class TypeGuard {
256
257 private final int signatureIndex;
258 private final TypeData type;
259
260 public TypeGuard(TypeData type, int signatureIndex) {
261 this.type = type;
262 this.signatureIndex = signatureIndex;
263 }
264
265 @Override
266 public int hashCode() {
267 final int prime = 31;
268 int result = 1;
269 result = prime * result + signatureIndex;
270 result = prime * result + type.hashCode();
271 return result;
272 }
273
274 @Override
275 public boolean equals(Object obj) {
276 if (this == obj) {
277 return true;
278 } else if (obj == null) {
279 return false;
280 } else if (getClass() != obj.getClass()) {
281 return false;
282 }
283
284 TypeGuard other = (TypeGuard) obj;
285 if (signatureIndex != other.signatureIndex) {
286 return false;
287 } else if (!type.equals(other.type)) {
288 return false;
289 }
290 return true;
291 }
292
293 public int getSignatureIndex() {
294 return signatureIndex;
295 }
296
297 public TypeData getType() {
298 return type;
299 }
300 }
241 } 301 }